ASTContext.cpp revision e7cff2c00c3d74e38984a4db5c48d7e303d3ab8e
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  }
1967530c034c0c71a64c5a9173206d9742ae847af8bDavid Blaikie  llvm_unreachable("Invalid CXXABI type!");
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),
227a6ea10e22b600d92e084f6b11b9b9a92d0eb2412Douglas Gregor    ObjCIdDecl(0), ObjCSelDecl(0), ObjCClassDecl(0), ObjCProtocolClassDecl(0),
228e97179c675b341927807c718be215c8d1aab8acbDouglas Gregor    CFConstantStringTypeDecl(0), ObjCInstanceTypeDecl(0),
229bcfd1f55bfbb3e5944cd5e03d07b343e280838c4Douglas Gregor    FILEDecl(0),
230e2d4f4ed44a32f179c6d48cd1dba8346ab2129d9Rafael Espindola    jmp_bufDecl(0), sigjmp_bufDecl(0), ucontext_tDecl(0),
231e2d4f4ed44a32f179c6d48cd1dba8346ab2129d9Rafael Espindola    BlockDescriptorType(0), BlockDescriptorExtendedType(0),
232e2d4f4ed44a32f179c6d48cd1dba8346ab2129d9Rafael Espindola    cudaConfigureCallDecl(0),
233e664977aca2a05a77abab5a06dc0fb69e870cfb9Douglas Gregor    NullTypeSourceInfo(QualType()),
234e664977aca2a05a77abab5a06dc0fb69e870cfb9Douglas Gregor    FirstLocalImport(), LastLocalImport(),
235bcfd1f55bfbb3e5944cd5e03d07b343e280838c4Douglas Gregor    SourceMgr(SM), LangOpts(LOpts),
23630c42404202d2e2512e51efc6066bd614cfdb5a4Douglas Gregor    AddrSpaceMap(0), Target(t), PrintingPolicy(LOpts),
237bcfd1f55bfbb3e5944cd5e03d07b343e280838c4Douglas Gregor    Idents(idents), Selectors(sels),
238bcfd1f55bfbb3e5944cd5e03d07b343e280838c4Douglas Gregor    BuiltinInfo(builtins),
239bcfd1f55bfbb3e5944cd5e03d07b343e280838c4Douglas Gregor    DeclarationNames(*this),
24030c42404202d2e2512e51efc6066bd614cfdb5a4Douglas Gregor    ExternalSource(0), Listener(0),
241bcfd1f55bfbb3e5944cd5e03d07b343e280838c4Douglas Gregor    LastSDM(0, 0),
242bcfd1f55bfbb3e5944cd5e03d07b343e280838c4Douglas Gregor    UniqueBlockByRefTypeID(0)
243bcfd1f55bfbb3e5944cd5e03d07b343e280838c4Douglas Gregor{
2441eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  if (size_reserve > 0) Types.reserve(size_reserve);
245e91593ef084479340582b2ba177b44be50a717b7Daniel Dunbar  TUDecl = TranslationUnitDecl::Create(*this);
246bcfd1f55bfbb3e5944cd5e03d07b343e280838c4Douglas Gregor
247bcfd1f55bfbb3e5944cd5e03d07b343e280838c4Douglas Gregor  if (!DelayInitialization) {
248bcfd1f55bfbb3e5944cd5e03d07b343e280838c4Douglas Gregor    assert(t && "No target supplied for ASTContext initialization");
249bcfd1f55bfbb3e5944cd5e03d07b343e280838c4Douglas Gregor    InitBuiltinTypes(*t);
250bcfd1f55bfbb3e5944cd5e03d07b343e280838c4Douglas Gregor  }
251e91593ef084479340582b2ba177b44be50a717b7Daniel Dunbar}
252e91593ef084479340582b2ba177b44be50a717b7Daniel Dunbar
2535f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid SpencerASTContext::~ASTContext() {
2543478eb6872d836600caf45b0f81c2065d685d6e0Ted Kremenek  // Release the DenseMaps associated with DeclContext objects.
2553478eb6872d836600caf45b0f81c2065d685d6e0Ted Kremenek  // FIXME: Is this the ideal solution?
2563478eb6872d836600caf45b0f81c2065d685d6e0Ted Kremenek  ReleaseDeclContextMaps();
2577d10b7eb670b821741b4c96f6cf7afbc3bb39abeDouglas Gregor
25863fe86bee66fc145942c56b2cc564ea0b9b9ea12Douglas Gregor  // Call all of the deallocation functions.
25963fe86bee66fc145942c56b2cc564ea0b9b9ea12Douglas Gregor  for (unsigned I = 0, N = Deallocations.size(); I != N; ++I)
26063fe86bee66fc145942c56b2cc564ea0b9b9ea12Douglas Gregor    Deallocations[I].first(Deallocations[I].second);
2610054531488928a424666ac11fcdc6bcc5112de52Douglas Gregor
2627d10b7eb670b821741b4c96f6cf7afbc3bb39abeDouglas Gregor  // Release all of the memory associated with overridden C++ methods.
2637d10b7eb670b821741b4c96f6cf7afbc3bb39abeDouglas Gregor  for (llvm::DenseMap<const CXXMethodDecl *, CXXMethodVector>::iterator
2647d10b7eb670b821741b4c96f6cf7afbc3bb39abeDouglas Gregor         OM = OverriddenMethods.begin(), OMEnd = OverriddenMethods.end();
2657d10b7eb670b821741b4c96f6cf7afbc3bb39abeDouglas Gregor       OM != OMEnd; ++OM)
2667d10b7eb670b821741b4c96f6cf7afbc3bb39abeDouglas Gregor    OM->second.Destroy();
2673478eb6872d836600caf45b0f81c2065d685d6e0Ted Kremenek
268dcfcfbec478f7ed96cd8d92f30c29bd4e30d5b9cTed Kremenek  // ASTRecordLayout objects in ASTRecordLayouts must always be destroyed
26963fe86bee66fc145942c56b2cc564ea0b9b9ea12Douglas Gregor  // because they can contain DenseMaps.
27063fe86bee66fc145942c56b2cc564ea0b9b9ea12Douglas Gregor  for (llvm::DenseMap<const ObjCContainerDecl*,
27163fe86bee66fc145942c56b2cc564ea0b9b9ea12Douglas Gregor       const ASTRecordLayout*>::iterator
27263fe86bee66fc145942c56b2cc564ea0b9b9ea12Douglas Gregor       I = ObjCLayouts.begin(), E = ObjCLayouts.end(); I != E; )
27363fe86bee66fc145942c56b2cc564ea0b9b9ea12Douglas Gregor    // Increment in loop to prevent using deallocated memory.
27463fe86bee66fc145942c56b2cc564ea0b9b9ea12Douglas Gregor    if (ASTRecordLayout *R = const_cast<ASTRecordLayout*>((I++)->second))
27563fe86bee66fc145942c56b2cc564ea0b9b9ea12Douglas Gregor      R->Destroy(*this);
27663fe86bee66fc145942c56b2cc564ea0b9b9ea12Douglas Gregor
277dcfcfbec478f7ed96cd8d92f30c29bd4e30d5b9cTed Kremenek  for (llvm::DenseMap<const RecordDecl*, const ASTRecordLayout*>::iterator
278dcfcfbec478f7ed96cd8d92f30c29bd4e30d5b9cTed Kremenek       I = ASTRecordLayouts.begin(), E = ASTRecordLayouts.end(); I != E; ) {
279dcfcfbec478f7ed96cd8d92f30c29bd4e30d5b9cTed Kremenek    // Increment in loop to prevent using deallocated memory.
280dcfcfbec478f7ed96cd8d92f30c29bd4e30d5b9cTed Kremenek    if (ASTRecordLayout *R = const_cast<ASTRecordLayout*>((I++)->second))
281dcfcfbec478f7ed96cd8d92f30c29bd4e30d5b9cTed Kremenek      R->Destroy(*this);
282dcfcfbec478f7ed96cd8d92f30c29bd4e30d5b9cTed Kremenek  }
2836320064d0c60fa8683f5623881c9394fd4aa7689Douglas Gregor
2846320064d0c60fa8683f5623881c9394fd4aa7689Douglas Gregor  for (llvm::DenseMap<const Decl*, AttrVec*>::iterator A = DeclAttrs.begin(),
2856320064d0c60fa8683f5623881c9394fd4aa7689Douglas Gregor                                                    AEnd = DeclAttrs.end();
2866320064d0c60fa8683f5623881c9394fd4aa7689Douglas Gregor       A != AEnd; ++A)
2876320064d0c60fa8683f5623881c9394fd4aa7689Douglas Gregor    A->second->~AttrVec();
2886320064d0c60fa8683f5623881c9394fd4aa7689Douglas Gregor}
289ab452ba8323d1985e08bade2bced588cddf2cc28Douglas Gregor
2900054531488928a424666ac11fcdc6bcc5112de52Douglas Gregorvoid ASTContext::AddDeallocation(void (*Callback)(void*), void *Data) {
2910054531488928a424666ac11fcdc6bcc5112de52Douglas Gregor  Deallocations.push_back(std::make_pair(Callback, Data));
2920054531488928a424666ac11fcdc6bcc5112de52Douglas Gregor}
2930054531488928a424666ac11fcdc6bcc5112de52Douglas Gregor
2941eb4433ac451dc16f4133a88af2d002ac26c58efMike Stumpvoid
2952cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas GregorASTContext::setExternalSource(llvm::OwningPtr<ExternalASTSource> &Source) {
2962cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor  ExternalSource.reset(Source.take());
2972cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor}
2982cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor
2995f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencervoid ASTContext::PrintStats() const {
300cd92a65edc7cbbbb7e3aee8d31008594de90fa51Chandler Carruth  llvm::errs() << "\n*** AST Context Stats:\n";
301cd92a65edc7cbbbb7e3aee8d31008594de90fa51Chandler Carruth  llvm::errs() << "  " << Types.size() << " types total.\n";
3027c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl
303dbe833da54e1b6192991b64fc453cd50b5ee7909Douglas Gregor  unsigned counts[] = {
3041eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump#define TYPE(Name, Parent) 0,
305dbe833da54e1b6192991b64fc453cd50b5ee7909Douglas Gregor#define ABSTRACT_TYPE(Name, Parent)
306dbe833da54e1b6192991b64fc453cd50b5ee7909Douglas Gregor#include "clang/AST/TypeNodes.def"
307dbe833da54e1b6192991b64fc453cd50b5ee7909Douglas Gregor    0 // Extra
308dbe833da54e1b6192991b64fc453cd50b5ee7909Douglas Gregor  };
309c2ee10d79f70036af652a395ac1f8273f3d04e12Douglas Gregor
3105f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  for (unsigned i = 0, e = Types.size(); i != e; ++i) {
3115f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    Type *T = Types[i];
312dbe833da54e1b6192991b64fc453cd50b5ee7909Douglas Gregor    counts[(unsigned)T->getTypeClass()]++;
3135f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
3145f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
315dbe833da54e1b6192991b64fc453cd50b5ee7909Douglas Gregor  unsigned Idx = 0;
316dbe833da54e1b6192991b64fc453cd50b5ee7909Douglas Gregor  unsigned TotalBytes = 0;
317dbe833da54e1b6192991b64fc453cd50b5ee7909Douglas Gregor#define TYPE(Name, Parent)                                              \
318dbe833da54e1b6192991b64fc453cd50b5ee7909Douglas Gregor  if (counts[Idx])                                                      \
319cd92a65edc7cbbbb7e3aee8d31008594de90fa51Chandler Carruth    llvm::errs() << "    " << counts[Idx] << " " << #Name               \
320cd92a65edc7cbbbb7e3aee8d31008594de90fa51Chandler Carruth                 << " types\n";                                         \
321dbe833da54e1b6192991b64fc453cd50b5ee7909Douglas Gregor  TotalBytes += counts[Idx] * sizeof(Name##Type);                       \
322dbe833da54e1b6192991b64fc453cd50b5ee7909Douglas Gregor  ++Idx;
323dbe833da54e1b6192991b64fc453cd50b5ee7909Douglas Gregor#define ABSTRACT_TYPE(Name, Parent)
324dbe833da54e1b6192991b64fc453cd50b5ee7909Douglas Gregor#include "clang/AST/TypeNodes.def"
3251eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
326cd92a65edc7cbbbb7e3aee8d31008594de90fa51Chandler Carruth  llvm::errs() << "Total bytes = " << TotalBytes << "\n";
327cd92a65edc7cbbbb7e3aee8d31008594de90fa51Chandler Carruth
3284923aa25eb39d64106a5817c02d560a3aecf8b2cDouglas Gregor  // Implicit special member functions.
329cd92a65edc7cbbbb7e3aee8d31008594de90fa51Chandler Carruth  llvm::errs() << NumImplicitDefaultConstructorsDeclared << "/"
330cd92a65edc7cbbbb7e3aee8d31008594de90fa51Chandler Carruth               << NumImplicitDefaultConstructors
331cd92a65edc7cbbbb7e3aee8d31008594de90fa51Chandler Carruth               << " implicit default constructors created\n";
332cd92a65edc7cbbbb7e3aee8d31008594de90fa51Chandler Carruth  llvm::errs() << NumImplicitCopyConstructorsDeclared << "/"
333cd92a65edc7cbbbb7e3aee8d31008594de90fa51Chandler Carruth               << NumImplicitCopyConstructors
334cd92a65edc7cbbbb7e3aee8d31008594de90fa51Chandler Carruth               << " implicit copy constructors created\n";
335ffe37fdda5b4b4f162a45155c30d9f60ce110c12Sean Hunt  if (getLangOptions().CPlusPlus)
336cd92a65edc7cbbbb7e3aee8d31008594de90fa51Chandler Carruth    llvm::errs() << NumImplicitMoveConstructorsDeclared << "/"
337cd92a65edc7cbbbb7e3aee8d31008594de90fa51Chandler Carruth                 << NumImplicitMoveConstructors
338cd92a65edc7cbbbb7e3aee8d31008594de90fa51Chandler Carruth                 << " implicit move constructors created\n";
339cd92a65edc7cbbbb7e3aee8d31008594de90fa51Chandler Carruth  llvm::errs() << NumImplicitCopyAssignmentOperatorsDeclared << "/"
340cd92a65edc7cbbbb7e3aee8d31008594de90fa51Chandler Carruth               << NumImplicitCopyAssignmentOperators
341cd92a65edc7cbbbb7e3aee8d31008594de90fa51Chandler Carruth               << " implicit copy assignment operators created\n";
342ffe37fdda5b4b4f162a45155c30d9f60ce110c12Sean Hunt  if (getLangOptions().CPlusPlus)
343cd92a65edc7cbbbb7e3aee8d31008594de90fa51Chandler Carruth    llvm::errs() << NumImplicitMoveAssignmentOperatorsDeclared << "/"
344cd92a65edc7cbbbb7e3aee8d31008594de90fa51Chandler Carruth                 << NumImplicitMoveAssignmentOperators
345cd92a65edc7cbbbb7e3aee8d31008594de90fa51Chandler Carruth                 << " implicit move assignment operators created\n";
346cd92a65edc7cbbbb7e3aee8d31008594de90fa51Chandler Carruth  llvm::errs() << NumImplicitDestructorsDeclared << "/"
347cd92a65edc7cbbbb7e3aee8d31008594de90fa51Chandler Carruth               << NumImplicitDestructors
348cd92a65edc7cbbbb7e3aee8d31008594de90fa51Chandler Carruth               << " implicit destructors created\n";
349cd92a65edc7cbbbb7e3aee8d31008594de90fa51Chandler Carruth
3502cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor  if (ExternalSource.get()) {
351cd92a65edc7cbbbb7e3aee8d31008594de90fa51Chandler Carruth    llvm::errs() << "\n";
3522cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor    ExternalSource->PrintStats();
3532cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor  }
354cd92a65edc7cbbbb7e3aee8d31008594de90fa51Chandler Carruth
35563fe86bee66fc145942c56b2cc564ea0b9b9ea12Douglas Gregor  BumpAlloc.PrintStats();
3565f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
3575f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
358772eeaefef2c883aabe35caf4543e7e32d290183Douglas GregorTypedefDecl *ASTContext::getInt128Decl() const {
359772eeaefef2c883aabe35caf4543e7e32d290183Douglas Gregor  if (!Int128Decl) {
360772eeaefef2c883aabe35caf4543e7e32d290183Douglas Gregor    TypeSourceInfo *TInfo = getTrivialTypeSourceInfo(Int128Ty);
361772eeaefef2c883aabe35caf4543e7e32d290183Douglas Gregor    Int128Decl = TypedefDecl::Create(const_cast<ASTContext &>(*this),
362772eeaefef2c883aabe35caf4543e7e32d290183Douglas Gregor                                     getTranslationUnitDecl(),
363772eeaefef2c883aabe35caf4543e7e32d290183Douglas Gregor                                     SourceLocation(),
364772eeaefef2c883aabe35caf4543e7e32d290183Douglas Gregor                                     SourceLocation(),
365772eeaefef2c883aabe35caf4543e7e32d290183Douglas Gregor                                     &Idents.get("__int128_t"),
366772eeaefef2c883aabe35caf4543e7e32d290183Douglas Gregor                                     TInfo);
367772eeaefef2c883aabe35caf4543e7e32d290183Douglas Gregor  }
368772eeaefef2c883aabe35caf4543e7e32d290183Douglas Gregor
369772eeaefef2c883aabe35caf4543e7e32d290183Douglas Gregor  return Int128Decl;
370772eeaefef2c883aabe35caf4543e7e32d290183Douglas Gregor}
371772eeaefef2c883aabe35caf4543e7e32d290183Douglas Gregor
372772eeaefef2c883aabe35caf4543e7e32d290183Douglas GregorTypedefDecl *ASTContext::getUInt128Decl() const {
373772eeaefef2c883aabe35caf4543e7e32d290183Douglas Gregor  if (!UInt128Decl) {
374772eeaefef2c883aabe35caf4543e7e32d290183Douglas Gregor    TypeSourceInfo *TInfo = getTrivialTypeSourceInfo(UnsignedInt128Ty);
375772eeaefef2c883aabe35caf4543e7e32d290183Douglas Gregor    UInt128Decl = TypedefDecl::Create(const_cast<ASTContext &>(*this),
376772eeaefef2c883aabe35caf4543e7e32d290183Douglas Gregor                                     getTranslationUnitDecl(),
377772eeaefef2c883aabe35caf4543e7e32d290183Douglas Gregor                                     SourceLocation(),
378772eeaefef2c883aabe35caf4543e7e32d290183Douglas Gregor                                     SourceLocation(),
379772eeaefef2c883aabe35caf4543e7e32d290183Douglas Gregor                                     &Idents.get("__uint128_t"),
380772eeaefef2c883aabe35caf4543e7e32d290183Douglas Gregor                                     TInfo);
381772eeaefef2c883aabe35caf4543e7e32d290183Douglas Gregor  }
382772eeaefef2c883aabe35caf4543e7e32d290183Douglas Gregor
383772eeaefef2c883aabe35caf4543e7e32d290183Douglas Gregor  return UInt128Decl;
384772eeaefef2c883aabe35caf4543e7e32d290183Douglas Gregor}
3855f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
386e27ec8ad56dbf1efb2de004b90fbbb86f740e3f1John McCallvoid ASTContext::InitBuiltinType(CanQualType &R, BuiltinType::Kind K) {
3876b304a0254a13f42390b865ff5ba668a49cc58aeJohn McCall  BuiltinType *Ty = new (*this, TypeAlignment) BuiltinType(K);
388e27ec8ad56dbf1efb2de004b90fbbb86f740e3f1John McCall  R = CanQualType::CreateUnsafe(QualType(Ty, 0));
3896b304a0254a13f42390b865ff5ba668a49cc58aeJohn McCall  Types.push_back(Ty);
3905f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
3915f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
392bcfd1f55bfbb3e5944cd5e03d07b343e280838c4Douglas Gregorvoid ASTContext::InitBuiltinTypes(const TargetInfo &Target) {
393bcfd1f55bfbb3e5944cd5e03d07b343e280838c4Douglas Gregor  assert((!this->Target || this->Target == &Target) &&
394bcfd1f55bfbb3e5944cd5e03d07b343e280838c4Douglas Gregor         "Incorrect target reinitialization");
3955f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  assert(VoidTy.isNull() && "Context reinitialized?");
3961eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
397bcfd1f55bfbb3e5944cd5e03d07b343e280838c4Douglas Gregor  this->Target = &Target;
398bcfd1f55bfbb3e5944cd5e03d07b343e280838c4Douglas Gregor
399bcfd1f55bfbb3e5944cd5e03d07b343e280838c4Douglas Gregor  ABI.reset(createCXXABI(Target));
400bcfd1f55bfbb3e5944cd5e03d07b343e280838c4Douglas Gregor  AddrSpaceMap = getAddressSpaceMap(Target, LangOpts);
401bcfd1f55bfbb3e5944cd5e03d07b343e280838c4Douglas Gregor
4025f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // C99 6.2.5p19.
4035f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  InitBuiltinType(VoidTy,              BuiltinType::Void);
4041eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
4055f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // C99 6.2.5p2.
4065f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  InitBuiltinType(BoolTy,              BuiltinType::Bool);
4075f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // C99 6.2.5p3.
40815b91764d08e886391c865c4a444d7b51141c284Eli Friedman  if (LangOpts.CharIsSigned)
4095f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    InitBuiltinType(CharTy,            BuiltinType::Char_S);
4105f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  else
4115f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    InitBuiltinType(CharTy,            BuiltinType::Char_U);
4125f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // C99 6.2.5p4.
4135f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  InitBuiltinType(SignedCharTy,        BuiltinType::SChar);
4145f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  InitBuiltinType(ShortTy,             BuiltinType::Short);
4155f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  InitBuiltinType(IntTy,               BuiltinType::Int);
4165f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  InitBuiltinType(LongTy,              BuiltinType::Long);
4175f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  InitBuiltinType(LongLongTy,          BuiltinType::LongLong);
4181eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
4195f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // C99 6.2.5p6.
4205f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  InitBuiltinType(UnsignedCharTy,      BuiltinType::UChar);
4215f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  InitBuiltinType(UnsignedShortTy,     BuiltinType::UShort);
4225f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  InitBuiltinType(UnsignedIntTy,       BuiltinType::UInt);
4235f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  InitBuiltinType(UnsignedLongTy,      BuiltinType::ULong);
4245f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  InitBuiltinType(UnsignedLongLongTy,  BuiltinType::ULongLong);
4251eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
4265f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // C99 6.2.5p10.
4275f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  InitBuiltinType(FloatTy,             BuiltinType::Float);
4285f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  InitBuiltinType(DoubleTy,            BuiltinType::Double);
4295f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  InitBuiltinType(LongDoubleTy,        BuiltinType::LongDouble);
43064c438a4be2a871fa43c78264663ba1e9788b94dArgyrios Kyrtzidis
4312df9ced9fd1e8c7d7b38443db07e0e811de22571Chris Lattner  // GNU extension, 128-bit integers.
4322df9ced9fd1e8c7d7b38443db07e0e811de22571Chris Lattner  InitBuiltinType(Int128Ty,            BuiltinType::Int128);
4332df9ced9fd1e8c7d7b38443db07e0e811de22571Chris Lattner  InitBuiltinType(UnsignedInt128Ty,    BuiltinType::UInt128);
4342df9ced9fd1e8c7d7b38443db07e0e811de22571Chris Lattner
4353f59c975aa5d047f7edd1b900b5e885c38af0ef7Chris Lattner  if (LangOpts.CPlusPlus) { // C++ 3.9.1p5
436d3d77cd138f8e830f6547b6f83fcd5721ccf5f5dEli Friedman    if (TargetInfo::isTypeSigned(Target.getWCharType()))
4373f59c975aa5d047f7edd1b900b5e885c38af0ef7Chris Lattner      InitBuiltinType(WCharTy,           BuiltinType::WChar_S);
4383f59c975aa5d047f7edd1b900b5e885c38af0ef7Chris Lattner    else  // -fshort-wchar makes wchar_t be unsigned.
4393f59c975aa5d047f7edd1b900b5e885c38af0ef7Chris Lattner      InitBuiltinType(WCharTy,           BuiltinType::WChar_U);
4403f59c975aa5d047f7edd1b900b5e885c38af0ef7Chris Lattner  } else // C99
4413a2503227c3db04a3619735127483263c1075ef7Chris Lattner    WCharTy = getFromTargetType(Target.getWCharType());
44264c438a4be2a871fa43c78264663ba1e9788b94dArgyrios Kyrtzidis
443f5c209d23b20ada4a9b6235db50317239cbf6ae1Alisdair Meredith  if (LangOpts.CPlusPlus) // C++0x 3.9.1p5, extension for C++
444f5c209d23b20ada4a9b6235db50317239cbf6ae1Alisdair Meredith    InitBuiltinType(Char16Ty,           BuiltinType::Char16);
445f5c209d23b20ada4a9b6235db50317239cbf6ae1Alisdair Meredith  else // C99
446f5c209d23b20ada4a9b6235db50317239cbf6ae1Alisdair Meredith    Char16Ty = getFromTargetType(Target.getChar16Type());
447f5c209d23b20ada4a9b6235db50317239cbf6ae1Alisdair Meredith
448f5c209d23b20ada4a9b6235db50317239cbf6ae1Alisdair Meredith  if (LangOpts.CPlusPlus) // C++0x 3.9.1p5, extension for C++
449f5c209d23b20ada4a9b6235db50317239cbf6ae1Alisdair Meredith    InitBuiltinType(Char32Ty,           BuiltinType::Char32);
450f5c209d23b20ada4a9b6235db50317239cbf6ae1Alisdair Meredith  else // C99
451f5c209d23b20ada4a9b6235db50317239cbf6ae1Alisdair Meredith    Char32Ty = getFromTargetType(Target.getChar32Type());
452f5c209d23b20ada4a9b6235db50317239cbf6ae1Alisdair Meredith
453898574e7496ba8fd76290079d3a9d06954992734Douglas Gregor  // Placeholder type for type-dependent expressions whose type is
454898574e7496ba8fd76290079d3a9d06954992734Douglas Gregor  // completely unknown. No code should ever check a type against
455898574e7496ba8fd76290079d3a9d06954992734Douglas Gregor  // DependentTy and users should never see it; however, it is here to
456898574e7496ba8fd76290079d3a9d06954992734Douglas Gregor  // help diagnose failures to properly check for type-dependent
457898574e7496ba8fd76290079d3a9d06954992734Douglas Gregor  // expressions.
458898574e7496ba8fd76290079d3a9d06954992734Douglas Gregor  InitBuiltinType(DependentTy,         BuiltinType::Dependent);
4598e9bebdea69c590dedfbf27374114cb76fe12fbdDouglas Gregor
4602a984cad5ac3fdceeff2bd99daa7b90979313475John McCall  // Placeholder type for functions.
4612a984cad5ac3fdceeff2bd99daa7b90979313475John McCall  InitBuiltinType(OverloadTy,          BuiltinType::Overload);
4622a984cad5ac3fdceeff2bd99daa7b90979313475John McCall
463864c041e118155c2b1ce0ba36942a3da5a4a055eJohn McCall  // Placeholder type for bound members.
464864c041e118155c2b1ce0ba36942a3da5a4a055eJohn McCall  InitBuiltinType(BoundMemberTy,       BuiltinType::BoundMember);
465864c041e118155c2b1ce0ba36942a3da5a4a055eJohn McCall
4663c3b7f90a863af43fa63043d396553ecf205351cJohn McCall  // Placeholder type for pseudo-objects.
4673c3b7f90a863af43fa63043d396553ecf205351cJohn McCall  InitBuiltinType(PseudoObjectTy,      BuiltinType::PseudoObject);
4683c3b7f90a863af43fa63043d396553ecf205351cJohn McCall
4691de4d4e8cb2e9c88809fea8092bc6e835a5473d2John McCall  // "any" type; useful for debugger-like clients.
4701de4d4e8cb2e9c88809fea8092bc6e835a5473d2John McCall  InitBuiltinType(UnknownAnyTy,        BuiltinType::UnknownAny);
4711de4d4e8cb2e9c88809fea8092bc6e835a5473d2John McCall
4720ddaeb9b031070ec64afe92d9892875ac44df427John McCall  // Placeholder type for unbridged ARC casts.
4730ddaeb9b031070ec64afe92d9892875ac44df427John McCall  InitBuiltinType(ARCUnbridgedCastTy,  BuiltinType::ARCUnbridgedCast);
4740ddaeb9b031070ec64afe92d9892875ac44df427John McCall
4755f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // C99 6.2.5p11.
4765f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  FloatComplexTy      = getComplexType(FloatTy);
4775f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  DoubleComplexTy     = getComplexType(DoubleTy);
4785f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  LongDoubleComplexTy = getComplexType(LongDoubleTy);
4798e9bebdea69c590dedfbf27374114cb76fe12fbdDouglas Gregor
4807e219e47de26346885d667131977bd9ca2d7662aSteve Naroff  BuiltinVaListType = QualType();
4811eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
48213dcd00615de5c4279d97bdf63cd5f0a14fd9dccFariborz Jahanian  // Builtin types for 'id', 'Class', and 'SEL'.
483de2e22d33afec98324a66a358dfe0951b3c7259aSteve Naroff  InitBuiltinType(ObjCBuiltinIdTy, BuiltinType::ObjCId);
484de2e22d33afec98324a66a358dfe0951b3c7259aSteve Naroff  InitBuiltinType(ObjCBuiltinClassTy, BuiltinType::ObjCClass);
48513dcd00615de5c4279d97bdf63cd5f0a14fd9dccFariborz Jahanian  InitBuiltinType(ObjCBuiltinSelTy, BuiltinType::ObjCSel);
48614108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff
487a526c5c67e5a0473c340903ee542ce570119665fTed Kremenek  ObjCConstantStringType = QualType();
4881eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
48933e1d64ab5cd5d27f8530ccd056191fe2c9f3f2eFariborz Jahanian  // void * type
49033e1d64ab5cd5d27f8530ccd056191fe2c9f3f2eFariborz Jahanian  VoidPtrTy = getPointerType(VoidTy);
4916e8ed16ffef02b82995a90bdcf10ffff7d63839aSebastian Redl
4926e8ed16ffef02b82995a90bdcf10ffff7d63839aSebastian Redl  // nullptr type (C++0x 2.14.7)
4936e8ed16ffef02b82995a90bdcf10ffff7d63839aSebastian Redl  InitBuiltinType(NullPtrTy,           BuiltinType::NullPtr);
494aa4a99b4a62615db243f7a5c433169f2fc704420Anton Korobeynikov
495aa4a99b4a62615db243f7a5c433169f2fc704420Anton Korobeynikov  // half type (OpenCL 6.1.1.1) / ARM NEON __fp16
496aa4a99b4a62615db243f7a5c433169f2fc704420Anton Korobeynikov  InitBuiltinType(HalfTy, BuiltinType::Half);
4975f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
4985f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
499d6471f7c1921c7802804ce3ff6fe9768310f72b9David BlaikieDiagnosticsEngine &ASTContext::getDiagnostics() const {
50078a916ec5ff5b66adec3c499e1b9af7b87668309Argyrios Kyrtzidis  return SourceMgr.getDiagnostics();
50178a916ec5ff5b66adec3c499e1b9af7b87668309Argyrios Kyrtzidis}
50278a916ec5ff5b66adec3c499e1b9af7b87668309Argyrios Kyrtzidis
5036320064d0c60fa8683f5623881c9394fd4aa7689Douglas GregorAttrVec& ASTContext::getDeclAttrs(const Decl *D) {
5046320064d0c60fa8683f5623881c9394fd4aa7689Douglas Gregor  AttrVec *&Result = DeclAttrs[D];
5056320064d0c60fa8683f5623881c9394fd4aa7689Douglas Gregor  if (!Result) {
5066320064d0c60fa8683f5623881c9394fd4aa7689Douglas Gregor    void *Mem = Allocate(sizeof(AttrVec));
5076320064d0c60fa8683f5623881c9394fd4aa7689Douglas Gregor    Result = new (Mem) AttrVec;
5086320064d0c60fa8683f5623881c9394fd4aa7689Douglas Gregor  }
5096320064d0c60fa8683f5623881c9394fd4aa7689Douglas Gregor
5106320064d0c60fa8683f5623881c9394fd4aa7689Douglas Gregor  return *Result;
5116320064d0c60fa8683f5623881c9394fd4aa7689Douglas Gregor}
5126320064d0c60fa8683f5623881c9394fd4aa7689Douglas Gregor
5136320064d0c60fa8683f5623881c9394fd4aa7689Douglas Gregor/// \brief Erase the attributes corresponding to the given declaration.
5146320064d0c60fa8683f5623881c9394fd4aa7689Douglas Gregorvoid ASTContext::eraseDeclAttrs(const Decl *D) {
5156320064d0c60fa8683f5623881c9394fd4aa7689Douglas Gregor  llvm::DenseMap<const Decl*, AttrVec*>::iterator Pos = DeclAttrs.find(D);
5166320064d0c60fa8683f5623881c9394fd4aa7689Douglas Gregor  if (Pos != DeclAttrs.end()) {
5176320064d0c60fa8683f5623881c9394fd4aa7689Douglas Gregor    Pos->second->~AttrVec();
5186320064d0c60fa8683f5623881c9394fd4aa7689Douglas Gregor    DeclAttrs.erase(Pos);
5196320064d0c60fa8683f5623881c9394fd4aa7689Douglas Gregor  }
5206320064d0c60fa8683f5623881c9394fd4aa7689Douglas Gregor}
5216320064d0c60fa8683f5623881c9394fd4aa7689Douglas Gregor
522251b4ff2578e26959a4c036140ccd61c5e9292f2Douglas GregorMemberSpecializationInfo *
523663b5a0be7261c29bc4c526a71cffcfa02d4153eDouglas GregorASTContext::getInstantiatedFromStaticDataMember(const VarDecl *Var) {
5247caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  assert(Var->isStaticDataMember() && "Not a static data member");
525663b5a0be7261c29bc4c526a71cffcfa02d4153eDouglas Gregor  llvm::DenseMap<const VarDecl *, MemberSpecializationInfo *>::iterator Pos
5267caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor    = InstantiatedFromStaticDataMember.find(Var);
5277caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  if (Pos == InstantiatedFromStaticDataMember.end())
5287caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor    return 0;
5291eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
5307caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  return Pos->second;
5317caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor}
5327caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor
5331eb4433ac451dc16f4133a88af2d002ac26c58efMike Stumpvoid
534251b4ff2578e26959a4c036140ccd61c5e9292f2Douglas GregorASTContext::setInstantiatedFromStaticDataMember(VarDecl *Inst, VarDecl *Tmpl,
5359421adc43891e272156fab640e5d5ee5054b779cArgyrios Kyrtzidis                                                TemplateSpecializationKind TSK,
5369421adc43891e272156fab640e5d5ee5054b779cArgyrios Kyrtzidis                                          SourceLocation PointOfInstantiation) {
5377caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  assert(Inst->isStaticDataMember() && "Not a static data member");
5387caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  assert(Tmpl->isStaticDataMember() && "Not a static data member");
5397caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  assert(!InstantiatedFromStaticDataMember[Inst] &&
5407caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor         "Already noted what static data member was instantiated from");
541251b4ff2578e26959a4c036140ccd61c5e9292f2Douglas Gregor  InstantiatedFromStaticDataMember[Inst]
5429421adc43891e272156fab640e5d5ee5054b779cArgyrios Kyrtzidis    = new (*this) MemberSpecializationInfo(Tmpl, TSK, PointOfInstantiation);
5437caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor}
5447caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor
545af0f4d0b2e38c810effc8b024ad2fb6604eec5d3Francois PichetFunctionDecl *ASTContext::getClassScopeSpecializationPattern(
546af0f4d0b2e38c810effc8b024ad2fb6604eec5d3Francois Pichet                                                     const FunctionDecl *FD){
547af0f4d0b2e38c810effc8b024ad2fb6604eec5d3Francois Pichet  assert(FD && "Specialization is 0");
548af0f4d0b2e38c810effc8b024ad2fb6604eec5d3Francois Pichet  llvm::DenseMap<const FunctionDecl*, FunctionDecl *>::const_iterator Pos
5490d95f0d7b81110f77e99e833f766d19be7b7e072Francois Pichet    = ClassScopeSpecializationPattern.find(FD);
5500d95f0d7b81110f77e99e833f766d19be7b7e072Francois Pichet  if (Pos == ClassScopeSpecializationPattern.end())
551af0f4d0b2e38c810effc8b024ad2fb6604eec5d3Francois Pichet    return 0;
552af0f4d0b2e38c810effc8b024ad2fb6604eec5d3Francois Pichet
553af0f4d0b2e38c810effc8b024ad2fb6604eec5d3Francois Pichet  return Pos->second;
554af0f4d0b2e38c810effc8b024ad2fb6604eec5d3Francois Pichet}
555af0f4d0b2e38c810effc8b024ad2fb6604eec5d3Francois Pichet
556af0f4d0b2e38c810effc8b024ad2fb6604eec5d3Francois Pichetvoid ASTContext::setClassScopeSpecializationPattern(FunctionDecl *FD,
557af0f4d0b2e38c810effc8b024ad2fb6604eec5d3Francois Pichet                                        FunctionDecl *Pattern) {
558af0f4d0b2e38c810effc8b024ad2fb6604eec5d3Francois Pichet  assert(FD && "Specialization is 0");
559af0f4d0b2e38c810effc8b024ad2fb6604eec5d3Francois Pichet  assert(Pattern && "Class scope specialization pattern is 0");
5600d95f0d7b81110f77e99e833f766d19be7b7e072Francois Pichet  ClassScopeSpecializationPattern[FD] = Pattern;
561af0f4d0b2e38c810effc8b024ad2fb6604eec5d3Francois Pichet}
562af0f4d0b2e38c810effc8b024ad2fb6604eec5d3Francois Pichet
5637ba107a1863ddfa1664555854f0d7bdb3c491c92John McCallNamedDecl *
564ed97649e9574b9d854fa4d6109c9333ae0993554John McCallASTContext::getInstantiatedFromUsingDecl(UsingDecl *UUD) {
5657ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall  llvm::DenseMap<UsingDecl *, NamedDecl *>::const_iterator Pos
566ed97649e9574b9d854fa4d6109c9333ae0993554John McCall    = InstantiatedFromUsingDecl.find(UUD);
567ed97649e9574b9d854fa4d6109c9333ae0993554John McCall  if (Pos == InstantiatedFromUsingDecl.end())
5680d8df780aef1acda5962347a32591efc629b6748Anders Carlsson    return 0;
5691eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
5700d8df780aef1acda5962347a32591efc629b6748Anders Carlsson  return Pos->second;
5710d8df780aef1acda5962347a32591efc629b6748Anders Carlsson}
5720d8df780aef1acda5962347a32591efc629b6748Anders Carlsson
5730d8df780aef1acda5962347a32591efc629b6748Anders Carlssonvoid
574ed97649e9574b9d854fa4d6109c9333ae0993554John McCallASTContext::setInstantiatedFromUsingDecl(UsingDecl *Inst, NamedDecl *Pattern) {
575ed97649e9574b9d854fa4d6109c9333ae0993554John McCall  assert((isa<UsingDecl>(Pattern) ||
576ed97649e9574b9d854fa4d6109c9333ae0993554John McCall          isa<UnresolvedUsingValueDecl>(Pattern) ||
577ed97649e9574b9d854fa4d6109c9333ae0993554John McCall          isa<UnresolvedUsingTypenameDecl>(Pattern)) &&
578ed97649e9574b9d854fa4d6109c9333ae0993554John McCall         "pattern decl is not a using decl");
579ed97649e9574b9d854fa4d6109c9333ae0993554John McCall  assert(!InstantiatedFromUsingDecl[Inst] && "pattern already exists");
580ed97649e9574b9d854fa4d6109c9333ae0993554John McCall  InstantiatedFromUsingDecl[Inst] = Pattern;
581ed97649e9574b9d854fa4d6109c9333ae0993554John McCall}
582ed97649e9574b9d854fa4d6109c9333ae0993554John McCall
583ed97649e9574b9d854fa4d6109c9333ae0993554John McCallUsingShadowDecl *
584ed97649e9574b9d854fa4d6109c9333ae0993554John McCallASTContext::getInstantiatedFromUsingShadowDecl(UsingShadowDecl *Inst) {
585ed97649e9574b9d854fa4d6109c9333ae0993554John McCall  llvm::DenseMap<UsingShadowDecl*, UsingShadowDecl*>::const_iterator Pos
586ed97649e9574b9d854fa4d6109c9333ae0993554John McCall    = InstantiatedFromUsingShadowDecl.find(Inst);
587ed97649e9574b9d854fa4d6109c9333ae0993554John McCall  if (Pos == InstantiatedFromUsingShadowDecl.end())
588ed97649e9574b9d854fa4d6109c9333ae0993554John McCall    return 0;
589ed97649e9574b9d854fa4d6109c9333ae0993554John McCall
590ed97649e9574b9d854fa4d6109c9333ae0993554John McCall  return Pos->second;
591ed97649e9574b9d854fa4d6109c9333ae0993554John McCall}
592ed97649e9574b9d854fa4d6109c9333ae0993554John McCall
593ed97649e9574b9d854fa4d6109c9333ae0993554John McCallvoid
594ed97649e9574b9d854fa4d6109c9333ae0993554John McCallASTContext::setInstantiatedFromUsingShadowDecl(UsingShadowDecl *Inst,
595ed97649e9574b9d854fa4d6109c9333ae0993554John McCall                                               UsingShadowDecl *Pattern) {
596ed97649e9574b9d854fa4d6109c9333ae0993554John McCall  assert(!InstantiatedFromUsingShadowDecl[Inst] && "pattern already exists");
597ed97649e9574b9d854fa4d6109c9333ae0993554John McCall  InstantiatedFromUsingShadowDecl[Inst] = Pattern;
5980d8df780aef1acda5962347a32591efc629b6748Anders Carlsson}
5990d8df780aef1acda5962347a32591efc629b6748Anders Carlsson
600d8b285fee4471f393da8ee30f552ceacdc362afaAnders CarlssonFieldDecl *ASTContext::getInstantiatedFromUnnamedFieldDecl(FieldDecl *Field) {
601d8b285fee4471f393da8ee30f552ceacdc362afaAnders Carlsson  llvm::DenseMap<FieldDecl *, FieldDecl *>::iterator Pos
602d8b285fee4471f393da8ee30f552ceacdc362afaAnders Carlsson    = InstantiatedFromUnnamedFieldDecl.find(Field);
603d8b285fee4471f393da8ee30f552ceacdc362afaAnders Carlsson  if (Pos == InstantiatedFromUnnamedFieldDecl.end())
604d8b285fee4471f393da8ee30f552ceacdc362afaAnders Carlsson    return 0;
6051eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
606d8b285fee4471f393da8ee30f552ceacdc362afaAnders Carlsson  return Pos->second;
607d8b285fee4471f393da8ee30f552ceacdc362afaAnders Carlsson}
608d8b285fee4471f393da8ee30f552ceacdc362afaAnders Carlsson
609d8b285fee4471f393da8ee30f552ceacdc362afaAnders Carlssonvoid ASTContext::setInstantiatedFromUnnamedFieldDecl(FieldDecl *Inst,
610d8b285fee4471f393da8ee30f552ceacdc362afaAnders Carlsson                                                     FieldDecl *Tmpl) {
611d8b285fee4471f393da8ee30f552ceacdc362afaAnders Carlsson  assert(!Inst->getDeclName() && "Instantiated field decl is not unnamed");
612d8b285fee4471f393da8ee30f552ceacdc362afaAnders Carlsson  assert(!Tmpl->getDeclName() && "Template field decl is not unnamed");
613d8b285fee4471f393da8ee30f552ceacdc362afaAnders Carlsson  assert(!InstantiatedFromUnnamedFieldDecl[Inst] &&
614d8b285fee4471f393da8ee30f552ceacdc362afaAnders Carlsson         "Already noted what unnamed field was instantiated from");
6151eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
616d8b285fee4471f393da8ee30f552ceacdc362afaAnders Carlsson  InstantiatedFromUnnamedFieldDecl[Inst] = Tmpl;
617d8b285fee4471f393da8ee30f552ceacdc362afaAnders Carlsson}
618d8b285fee4471f393da8ee30f552ceacdc362afaAnders Carlsson
61914d56ef43ff4921c6749f7340212fbb743fdbb9bFariborz Jahanianbool ASTContext::ZeroBitfieldFollowsNonBitfield(const FieldDecl *FD,
62014d56ef43ff4921c6749f7340212fbb743fdbb9bFariborz Jahanian                                    const FieldDecl *LastFD) const {
62114d56ef43ff4921c6749f7340212fbb743fdbb9bFariborz Jahanian  return (FD->isBitField() && LastFD && !LastFD->isBitField() &&
622a6b8b2c09610b8bc4330e948ece8b940c2386406Richard Smith          FD->getBitWidthValue(*this) == 0);
62314d56ef43ff4921c6749f7340212fbb743fdbb9bFariborz Jahanian}
62414d56ef43ff4921c6749f7340212fbb743fdbb9bFariborz Jahanian
625340fa242130c2d8d74c83edca0952e771aebe0e6Fariborz Jahanianbool ASTContext::ZeroBitfieldFollowsBitfield(const FieldDecl *FD,
626340fa242130c2d8d74c83edca0952e771aebe0e6Fariborz Jahanian                                             const FieldDecl *LastFD) const {
627340fa242130c2d8d74c83edca0952e771aebe0e6Fariborz Jahanian  return (FD->isBitField() && LastFD && LastFD->isBitField() &&
628a6b8b2c09610b8bc4330e948ece8b940c2386406Richard Smith          FD->getBitWidthValue(*this) == 0 &&
629a6b8b2c09610b8bc4330e948ece8b940c2386406Richard Smith          LastFD->getBitWidthValue(*this) != 0);
630340fa242130c2d8d74c83edca0952e771aebe0e6Fariborz Jahanian}
631340fa242130c2d8d74c83edca0952e771aebe0e6Fariborz Jahanian
6329b3acaa32548d0ce78b9c39a3911397f6738a47cFariborz Jahanianbool ASTContext::BitfieldFollowsBitfield(const FieldDecl *FD,
6339b3acaa32548d0ce78b9c39a3911397f6738a47cFariborz Jahanian                                         const FieldDecl *LastFD) const {
6349b3acaa32548d0ce78b9c39a3911397f6738a47cFariborz Jahanian  return (FD->isBitField() && LastFD && LastFD->isBitField() &&
635a6b8b2c09610b8bc4330e948ece8b940c2386406Richard Smith          FD->getBitWidthValue(*this) &&
636a6b8b2c09610b8bc4330e948ece8b940c2386406Richard Smith          LastFD->getBitWidthValue(*this));
6379b3acaa32548d0ce78b9c39a3911397f6738a47cFariborz Jahanian}
6389b3acaa32548d0ce78b9c39a3911397f6738a47cFariborz Jahanian
639dd7fddb5b6883326e52b278a9b7e9cefea29aae0Chad Rosierbool ASTContext::NonBitfieldFollowsBitfield(const FieldDecl *FD,
64052bbe7a1133c3cb57e9246f1b96c12940ea3821aFariborz Jahanian                                         const FieldDecl *LastFD) const {
64152bbe7a1133c3cb57e9246f1b96c12940ea3821aFariborz Jahanian  return (!FD->isBitField() && LastFD && LastFD->isBitField() &&
642a6b8b2c09610b8bc4330e948ece8b940c2386406Richard Smith          LastFD->getBitWidthValue(*this));
64352bbe7a1133c3cb57e9246f1b96c12940ea3821aFariborz Jahanian}
64452bbe7a1133c3cb57e9246f1b96c12940ea3821aFariborz Jahanian
645dd7fddb5b6883326e52b278a9b7e9cefea29aae0Chad Rosierbool ASTContext::BitfieldFollowsNonBitfield(const FieldDecl *FD,
64652bbe7a1133c3cb57e9246f1b96c12940ea3821aFariborz Jahanian                                             const FieldDecl *LastFD) const {
64752bbe7a1133c3cb57e9246f1b96c12940ea3821aFariborz Jahanian  return (FD->isBitField() && LastFD && !LastFD->isBitField() &&
648a6b8b2c09610b8bc4330e948ece8b940c2386406Richard Smith          FD->getBitWidthValue(*this));
64952bbe7a1133c3cb57e9246f1b96c12940ea3821aFariborz Jahanian}
65052bbe7a1133c3cb57e9246f1b96c12940ea3821aFariborz Jahanian
6517d10b7eb670b821741b4c96f6cf7afbc3bb39abeDouglas GregorASTContext::overridden_cxx_method_iterator
6527d10b7eb670b821741b4c96f6cf7afbc3bb39abeDouglas GregorASTContext::overridden_methods_begin(const CXXMethodDecl *Method) const {
6537d10b7eb670b821741b4c96f6cf7afbc3bb39abeDouglas Gregor  llvm::DenseMap<const CXXMethodDecl *, CXXMethodVector>::const_iterator Pos
6547d10b7eb670b821741b4c96f6cf7afbc3bb39abeDouglas Gregor    = OverriddenMethods.find(Method);
6557d10b7eb670b821741b4c96f6cf7afbc3bb39abeDouglas Gregor  if (Pos == OverriddenMethods.end())
6567d10b7eb670b821741b4c96f6cf7afbc3bb39abeDouglas Gregor    return 0;
6577d10b7eb670b821741b4c96f6cf7afbc3bb39abeDouglas Gregor
6587d10b7eb670b821741b4c96f6cf7afbc3bb39abeDouglas Gregor  return Pos->second.begin();
6597d10b7eb670b821741b4c96f6cf7afbc3bb39abeDouglas Gregor}
6607d10b7eb670b821741b4c96f6cf7afbc3bb39abeDouglas Gregor
6617d10b7eb670b821741b4c96f6cf7afbc3bb39abeDouglas GregorASTContext::overridden_cxx_method_iterator
6627d10b7eb670b821741b4c96f6cf7afbc3bb39abeDouglas GregorASTContext::overridden_methods_end(const CXXMethodDecl *Method) const {
6637d10b7eb670b821741b4c96f6cf7afbc3bb39abeDouglas Gregor  llvm::DenseMap<const CXXMethodDecl *, CXXMethodVector>::const_iterator Pos
6647d10b7eb670b821741b4c96f6cf7afbc3bb39abeDouglas Gregor    = OverriddenMethods.find(Method);
6657d10b7eb670b821741b4c96f6cf7afbc3bb39abeDouglas Gregor  if (Pos == OverriddenMethods.end())
6667d10b7eb670b821741b4c96f6cf7afbc3bb39abeDouglas Gregor    return 0;
6677d10b7eb670b821741b4c96f6cf7afbc3bb39abeDouglas Gregor
6687d10b7eb670b821741b4c96f6cf7afbc3bb39abeDouglas Gregor  return Pos->second.end();
6697d10b7eb670b821741b4c96f6cf7afbc3bb39abeDouglas Gregor}
6707d10b7eb670b821741b4c96f6cf7afbc3bb39abeDouglas Gregor
671c91e9f439ae85d5f79a6b65672f1d7d1b55ccda0Argyrios Kyrtzidisunsigned
672c91e9f439ae85d5f79a6b65672f1d7d1b55ccda0Argyrios KyrtzidisASTContext::overridden_methods_size(const CXXMethodDecl *Method) const {
673c91e9f439ae85d5f79a6b65672f1d7d1b55ccda0Argyrios Kyrtzidis  llvm::DenseMap<const CXXMethodDecl *, CXXMethodVector>::const_iterator Pos
674c91e9f439ae85d5f79a6b65672f1d7d1b55ccda0Argyrios Kyrtzidis    = OverriddenMethods.find(Method);
675c91e9f439ae85d5f79a6b65672f1d7d1b55ccda0Argyrios Kyrtzidis  if (Pos == OverriddenMethods.end())
676c91e9f439ae85d5f79a6b65672f1d7d1b55ccda0Argyrios Kyrtzidis    return 0;
677c91e9f439ae85d5f79a6b65672f1d7d1b55ccda0Argyrios Kyrtzidis
678c91e9f439ae85d5f79a6b65672f1d7d1b55ccda0Argyrios Kyrtzidis  return Pos->second.size();
679c91e9f439ae85d5f79a6b65672f1d7d1b55ccda0Argyrios Kyrtzidis}
680c91e9f439ae85d5f79a6b65672f1d7d1b55ccda0Argyrios Kyrtzidis
6817d10b7eb670b821741b4c96f6cf7afbc3bb39abeDouglas Gregorvoid ASTContext::addOverriddenMethod(const CXXMethodDecl *Method,
6827d10b7eb670b821741b4c96f6cf7afbc3bb39abeDouglas Gregor                                     const CXXMethodDecl *Overridden) {
6837d10b7eb670b821741b4c96f6cf7afbc3bb39abeDouglas Gregor  OverriddenMethods[Method].push_back(Overridden);
6847d10b7eb670b821741b4c96f6cf7afbc3bb39abeDouglas Gregor}
6857d10b7eb670b821741b4c96f6cf7afbc3bb39abeDouglas Gregor
686e664977aca2a05a77abab5a06dc0fb69e870cfb9Douglas Gregorvoid ASTContext::addedLocalImportDecl(ImportDecl *Import) {
687e664977aca2a05a77abab5a06dc0fb69e870cfb9Douglas Gregor  assert(!Import->NextLocalImport && "Import declaration already in the chain");
688e664977aca2a05a77abab5a06dc0fb69e870cfb9Douglas Gregor  assert(!Import->isFromASTFile() && "Non-local import declaration");
689e664977aca2a05a77abab5a06dc0fb69e870cfb9Douglas Gregor  if (!FirstLocalImport) {
690e664977aca2a05a77abab5a06dc0fb69e870cfb9Douglas Gregor    FirstLocalImport = Import;
691e664977aca2a05a77abab5a06dc0fb69e870cfb9Douglas Gregor    LastLocalImport = Import;
692e664977aca2a05a77abab5a06dc0fb69e870cfb9Douglas Gregor    return;
693e664977aca2a05a77abab5a06dc0fb69e870cfb9Douglas Gregor  }
694e664977aca2a05a77abab5a06dc0fb69e870cfb9Douglas Gregor
695e664977aca2a05a77abab5a06dc0fb69e870cfb9Douglas Gregor  LastLocalImport->NextLocalImport = Import;
696e664977aca2a05a77abab5a06dc0fb69e870cfb9Douglas Gregor  LastLocalImport = Import;
697e664977aca2a05a77abab5a06dc0fb69e870cfb9Douglas Gregor}
698e664977aca2a05a77abab5a06dc0fb69e870cfb9Douglas Gregor
699464175bba1318bef7905122e9fda20cff926df78Chris Lattner//===----------------------------------------------------------------------===//
700464175bba1318bef7905122e9fda20cff926df78Chris Lattner//                         Type Sizing and Analysis
701464175bba1318bef7905122e9fda20cff926df78Chris Lattner//===----------------------------------------------------------------------===//
702a7674d8a9a69f3f6fe16e70cf2a3b2b15fb7c43dChris Lattner
703b7cfe88e88cb4f46308de89cf3f0c81bfe624128Chris Lattner/// getFloatTypeSemantics - Return the APFloat 'semantics' for the specified
704b7cfe88e88cb4f46308de89cf3f0c81bfe624128Chris Lattner/// scalar floating point type.
705b7cfe88e88cb4f46308de89cf3f0c81bfe624128Chris Lattnerconst llvm::fltSemantics &ASTContext::getFloatTypeSemantics(QualType T) const {
706183700f494ec9b6701b6efe82bcb25f4c79ba561John McCall  const BuiltinType *BT = T->getAs<BuiltinType>();
707b7cfe88e88cb4f46308de89cf3f0c81bfe624128Chris Lattner  assert(BT && "Not a floating point type!");
708b7cfe88e88cb4f46308de89cf3f0c81bfe624128Chris Lattner  switch (BT->getKind()) {
709b219cfc4d75f0a03630b7c4509ef791b7e97b2c8David Blaikie  default: llvm_unreachable("Not a floating point type!");
710aa4a99b4a62615db243f7a5c433169f2fc704420Anton Korobeynikov  case BuiltinType::Half:       return Target->getHalfFormat();
711bcfd1f55bfbb3e5944cd5e03d07b343e280838c4Douglas Gregor  case BuiltinType::Float:      return Target->getFloatFormat();
712bcfd1f55bfbb3e5944cd5e03d07b343e280838c4Douglas Gregor  case BuiltinType::Double:     return Target->getDoubleFormat();
713bcfd1f55bfbb3e5944cd5e03d07b343e280838c4Douglas Gregor  case BuiltinType::LongDouble: return Target->getLongDoubleFormat();
714b7cfe88e88cb4f46308de89cf3f0c81bfe624128Chris Lattner  }
715b7cfe88e88cb4f46308de89cf3f0c81bfe624128Chris Lattner}
716b7cfe88e88cb4f46308de89cf3f0c81bfe624128Chris Lattner
7178b752f10c394b140f9ef89e049cbad1a7676fc25Ken Dyck/// getDeclAlign - Return a conservative estimate of the alignment of the
718af707ab8fbb9451e8febb8d766f6c043628125c4Chris Lattner/// specified decl.  Note that bitfields do not have a valid alignment, so
719af707ab8fbb9451e8febb8d766f6c043628125c4Chris Lattner/// this method will assert on them.
7205d484e8cf710207010720589d89602233de61d01Sebastian Redl/// If @p RefAsPointee, references are treated like their underlying type
7215d484e8cf710207010720589d89602233de61d01Sebastian Redl/// (for alignof), else they're treated like pointers (for CodeGen).
7224ba2a17694148e16eaa8d3917f657ffcd3667be4Jay FoadCharUnits ASTContext::getDeclAlign(const Decl *D, bool RefAsPointee) const {
723bcfd1f55bfbb3e5944cd5e03d07b343e280838c4Douglas Gregor  unsigned Align = Target->getCharWidth();
724dcdafb6a701aa9d81edcb088915f58933315dc05Eli Friedman
7254081a5c5f1381c4ec77f8ab3866693917e4329c4John McCall  bool UseAlignAttrOnly = false;
7264081a5c5f1381c4ec77f8ab3866693917e4329c4John McCall  if (unsigned AlignFromAttr = D->getMaxAlignment()) {
7274081a5c5f1381c4ec77f8ab3866693917e4329c4John McCall    Align = AlignFromAttr;
7284081a5c5f1381c4ec77f8ab3866693917e4329c4John McCall
7294081a5c5f1381c4ec77f8ab3866693917e4329c4John McCall    // __attribute__((aligned)) can increase or decrease alignment
7304081a5c5f1381c4ec77f8ab3866693917e4329c4John McCall    // *except* on a struct or struct member, where it only increases
7314081a5c5f1381c4ec77f8ab3866693917e4329c4John McCall    // alignment unless 'packed' is also specified.
7324081a5c5f1381c4ec77f8ab3866693917e4329c4John McCall    //
73382d0b0aab9088e977c2a44c4a5a90479c63149fePeter Collingbourne    // It is an error for alignas to decrease alignment, so we can
7344081a5c5f1381c4ec77f8ab3866693917e4329c4John McCall    // ignore that possibility;  Sema should diagnose it.
7354081a5c5f1381c4ec77f8ab3866693917e4329c4John McCall    if (isa<FieldDecl>(D)) {
7364081a5c5f1381c4ec77f8ab3866693917e4329c4John McCall      UseAlignAttrOnly = D->hasAttr<PackedAttr>() ||
7374081a5c5f1381c4ec77f8ab3866693917e4329c4John McCall        cast<FieldDecl>(D)->getParent()->hasAttr<PackedAttr>();
7384081a5c5f1381c4ec77f8ab3866693917e4329c4John McCall    } else {
7394081a5c5f1381c4ec77f8ab3866693917e4329c4John McCall      UseAlignAttrOnly = true;
7404081a5c5f1381c4ec77f8ab3866693917e4329c4John McCall    }
7414081a5c5f1381c4ec77f8ab3866693917e4329c4John McCall  }
74278a7d7d79964119a3f35b262eb154b5cbf1001edFariborz Jahanian  else if (isa<FieldDecl>(D))
74378a7d7d79964119a3f35b262eb154b5cbf1001edFariborz Jahanian      UseAlignAttrOnly =
74478a7d7d79964119a3f35b262eb154b5cbf1001edFariborz Jahanian        D->hasAttr<PackedAttr>() ||
74578a7d7d79964119a3f35b262eb154b5cbf1001edFariborz Jahanian        cast<FieldDecl>(D)->getParent()->hasAttr<PackedAttr>();
746dcdafb6a701aa9d81edcb088915f58933315dc05Eli Friedman
747ba4f5d5754c8291690d01ca9581926673d69b24cJohn McCall  // If we're using the align attribute only, just ignore everything
748ba4f5d5754c8291690d01ca9581926673d69b24cJohn McCall  // else about the declaration and its type.
7494081a5c5f1381c4ec77f8ab3866693917e4329c4John McCall  if (UseAlignAttrOnly) {
750ba4f5d5754c8291690d01ca9581926673d69b24cJohn McCall    // do nothing
751ba4f5d5754c8291690d01ca9581926673d69b24cJohn McCall
7524081a5c5f1381c4ec77f8ab3866693917e4329c4John McCall  } else if (const ValueDecl *VD = dyn_cast<ValueDecl>(D)) {
753af707ab8fbb9451e8febb8d766f6c043628125c4Chris Lattner    QualType T = VD->getType();
7546217b80b7a1379b74cced1c076338262c3c980b3Ted Kremenek    if (const ReferenceType* RT = T->getAs<ReferenceType>()) {
7555d484e8cf710207010720589d89602233de61d01Sebastian Redl      if (RefAsPointee)
7565d484e8cf710207010720589d89602233de61d01Sebastian Redl        T = RT->getPointeeType();
7575d484e8cf710207010720589d89602233de61d01Sebastian Redl      else
7585d484e8cf710207010720589d89602233de61d01Sebastian Redl        T = getPointerType(RT->getPointeeType());
7595d484e8cf710207010720589d89602233de61d01Sebastian Redl    }
7605d484e8cf710207010720589d89602233de61d01Sebastian Redl    if (!T->isIncompleteType() && !T->isFunctionType()) {
7613b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall      // Adjust alignments of declarations with array type by the
7623b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall      // large-array alignment on the target.
763bcfd1f55bfbb3e5944cd5e03d07b343e280838c4Douglas Gregor      unsigned MinWidth = Target->getLargeArrayMinWidth();
7643b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall      const ArrayType *arrayType;
7653b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall      if (MinWidth && (arrayType = getAsArrayType(T))) {
7663b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall        if (isa<VariableArrayType>(arrayType))
767bcfd1f55bfbb3e5944cd5e03d07b343e280838c4Douglas Gregor          Align = std::max(Align, Target->getLargeArrayAlign());
7683b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall        else if (isa<ConstantArrayType>(arrayType) &&
7693b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall                 MinWidth <= getTypeSize(cast<ConstantArrayType>(arrayType)))
770bcfd1f55bfbb3e5944cd5e03d07b343e280838c4Douglas Gregor          Align = std::max(Align, Target->getLargeArrayAlign());
7713b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall
7723b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall        // Walk through any array types while we're at it.
7733b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall        T = getBaseElementType(arrayType);
7746deecb0d46bcfd048e651d2db7c4fb0d6407da96Rafael Espindola      }
7759f1210c3280104417a4ad30f0a00825ac8fa718aChad Rosier      Align = std::max(Align, getPreferredTypeAlign(T.getTypePtr()));
776dcdafb6a701aa9d81edcb088915f58933315dc05Eli Friedman    }
777ba4f5d5754c8291690d01ca9581926673d69b24cJohn McCall
778ba4f5d5754c8291690d01ca9581926673d69b24cJohn McCall    // Fields can be subject to extra alignment constraints, like if
779ba4f5d5754c8291690d01ca9581926673d69b24cJohn McCall    // the field is packed, the struct is packed, or the struct has a
780ba4f5d5754c8291690d01ca9581926673d69b24cJohn McCall    // a max-field-alignment constraint (#pragma pack).  So calculate
781ba4f5d5754c8291690d01ca9581926673d69b24cJohn McCall    // the actual alignment of the field within the struct, and then
782ba4f5d5754c8291690d01ca9581926673d69b24cJohn McCall    // (as we're expected to) constrain that by the alignment of the type.
783ba4f5d5754c8291690d01ca9581926673d69b24cJohn McCall    if (const FieldDecl *field = dyn_cast<FieldDecl>(VD)) {
784ba4f5d5754c8291690d01ca9581926673d69b24cJohn McCall      // So calculate the alignment of the field.
785ba4f5d5754c8291690d01ca9581926673d69b24cJohn McCall      const ASTRecordLayout &layout = getASTRecordLayout(field->getParent());
786ba4f5d5754c8291690d01ca9581926673d69b24cJohn McCall
787ba4f5d5754c8291690d01ca9581926673d69b24cJohn McCall      // Start with the record's overall alignment.
788dac54c124e302d6f028ea5723c425b7f66fc7c71Ken Dyck      unsigned fieldAlign = toBits(layout.getAlignment());
789ba4f5d5754c8291690d01ca9581926673d69b24cJohn McCall
790ba4f5d5754c8291690d01ca9581926673d69b24cJohn McCall      // Use the GCD of that and the offset within the record.
791ba4f5d5754c8291690d01ca9581926673d69b24cJohn McCall      uint64_t offset = layout.getFieldOffset(field->getFieldIndex());
792ba4f5d5754c8291690d01ca9581926673d69b24cJohn McCall      if (offset > 0) {
793ba4f5d5754c8291690d01ca9581926673d69b24cJohn McCall        // Alignment is always a power of 2, so the GCD will be a power of 2,
794ba4f5d5754c8291690d01ca9581926673d69b24cJohn McCall        // which means we get to do this crazy thing instead of Euclid's.
795ba4f5d5754c8291690d01ca9581926673d69b24cJohn McCall        uint64_t lowBitOfOffset = offset & (~offset + 1);
796ba4f5d5754c8291690d01ca9581926673d69b24cJohn McCall        if (lowBitOfOffset < fieldAlign)
797ba4f5d5754c8291690d01ca9581926673d69b24cJohn McCall          fieldAlign = static_cast<unsigned>(lowBitOfOffset);
798ba4f5d5754c8291690d01ca9581926673d69b24cJohn McCall      }
799ba4f5d5754c8291690d01ca9581926673d69b24cJohn McCall
800ba4f5d5754c8291690d01ca9581926673d69b24cJohn McCall      Align = std::min(Align, fieldAlign);
80105f62474dd2b0f1cb69adbe0787f2868788aa949Charles Davis    }
802af707ab8fbb9451e8febb8d766f6c043628125c4Chris Lattner  }
803dcdafb6a701aa9d81edcb088915f58933315dc05Eli Friedman
804eb6f5dc86531f794ba7746a2da4d28e37cf5da7eKen Dyck  return toCharUnitsFromBits(Align);
805af707ab8fbb9451e8febb8d766f6c043628125c4Chris Lattner}
806b7cfe88e88cb4f46308de89cf3f0c81bfe624128Chris Lattner
807ea1471e0e967548c596a71469702f8846dbaf3c0John McCallstd::pair<CharUnits, CharUnits>
808bee5a79fc95e3003d755031e3d2bb4410a71e1c1Ken DyckASTContext::getTypeInfoInChars(const Type *T) const {
809ea1471e0e967548c596a71469702f8846dbaf3c0John McCall  std::pair<uint64_t, unsigned> Info = getTypeInfo(T);
810eb6f5dc86531f794ba7746a2da4d28e37cf5da7eKen Dyck  return std::make_pair(toCharUnitsFromBits(Info.first),
811eb6f5dc86531f794ba7746a2da4d28e37cf5da7eKen Dyck                        toCharUnitsFromBits(Info.second));
812ea1471e0e967548c596a71469702f8846dbaf3c0John McCall}
813ea1471e0e967548c596a71469702f8846dbaf3c0John McCall
814ea1471e0e967548c596a71469702f8846dbaf3c0John McCallstd::pair<CharUnits, CharUnits>
815bee5a79fc95e3003d755031e3d2bb4410a71e1c1Ken DyckASTContext::getTypeInfoInChars(QualType T) const {
816ea1471e0e967548c596a71469702f8846dbaf3c0John McCall  return getTypeInfoInChars(T.getTypePtr());
817ea1471e0e967548c596a71469702f8846dbaf3c0John McCall}
818ea1471e0e967548c596a71469702f8846dbaf3c0John McCall
819a7674d8a9a69f3f6fe16e70cf2a3b2b15fb7c43dChris Lattner/// getTypeSize - Return the size of the specified type, in bits.  This method
820a7674d8a9a69f3f6fe16e70cf2a3b2b15fb7c43dChris Lattner/// does not work on incomplete types.
8210953e767ff7817f97b3ab20896b229891eeff45bJohn McCall///
8220953e767ff7817f97b3ab20896b229891eeff45bJohn McCall/// FIXME: Pointers into different addr spaces could have different sizes and
8230953e767ff7817f97b3ab20896b229891eeff45bJohn McCall/// alignment requirements: getPointerInfo should take an AddrSpace, this
8240953e767ff7817f97b3ab20896b229891eeff45bJohn McCall/// should take a QualType, &c.
825d2d2a11a91d7ddf468bfb70f66362d24806ed601Chris Lattnerstd::pair<uint64_t, unsigned>
8264ba2a17694148e16eaa8d3917f657ffcd3667be4Jay FoadASTContext::getTypeInfo(const Type *T) const {
8275e301007e31e14c8ff647288e1b8bd8dbf8a5fe4Mike Stump  uint64_t Width=0;
8285e301007e31e14c8ff647288e1b8bd8dbf8a5fe4Mike Stump  unsigned Align=8;
829a7674d8a9a69f3f6fe16e70cf2a3b2b15fb7c43dChris Lattner  switch (T->getTypeClass()) {
83072564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor#define TYPE(Class, Base)
83172564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor#define ABSTRACT_TYPE(Class, Base)
83218857644059c45da6776f1a288eec7b4cf3a844aDouglas Gregor#define NON_CANONICAL_TYPE(Class, Base)
83372564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor#define DEPENDENT_TYPE(Class, Base) case Type::Class:
83472564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor#include "clang/AST/TypeNodes.def"
835d3d49bb27c7ffd9accc0a6c00e887111c0348845John McCall    llvm_unreachable("Should not see dependent types");
83672564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor
8375d2a6303467184b1f159bb6556efc434e50e3c28Chris Lattner  case Type::FunctionNoProto:
8385d2a6303467184b1f159bb6556efc434e50e3c28Chris Lattner  case Type::FunctionProto:
83918857644059c45da6776f1a288eec7b4cf3a844aDouglas Gregor    // GCC extension: alignof(function) = 32 bits
84018857644059c45da6776f1a288eec7b4cf3a844aDouglas Gregor    Width = 0;
84118857644059c45da6776f1a288eec7b4cf3a844aDouglas Gregor    Align = 32;
84218857644059c45da6776f1a288eec7b4cf3a844aDouglas Gregor    break;
84318857644059c45da6776f1a288eec7b4cf3a844aDouglas Gregor
84472564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor  case Type::IncompleteArray:
845fb22d96692c5240fb8d611290dbf7eeed3759c73Steve Naroff  case Type::VariableArray:
84618857644059c45da6776f1a288eec7b4cf3a844aDouglas Gregor    Width = 0;
84718857644059c45da6776f1a288eec7b4cf3a844aDouglas Gregor    Align = getTypeAlign(cast<ArrayType>(T)->getElementType());
84818857644059c45da6776f1a288eec7b4cf3a844aDouglas Gregor    break;
84918857644059c45da6776f1a288eec7b4cf3a844aDouglas Gregor
850fb22d96692c5240fb8d611290dbf7eeed3759c73Steve Naroff  case Type::ConstantArray: {
8511d75118af76cae2bfc06389cde410e14bd0a19fcDaniel Dunbar    const ConstantArrayType *CAT = cast<ConstantArrayType>(T);
8521eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
85398be4943e8dc4f3905629a7102668960873cf863Chris Lattner    std::pair<uint64_t, unsigned> EltInfo = getTypeInfo(CAT->getElementType());
854fea966a4103ed9c018d1494b95e9d09b161f5a70Abramo Bagnara    uint64_t Size = CAT->getSize().getZExtValue();
855fea966a4103ed9c018d1494b95e9d09b161f5a70Abramo Bagnara    assert((Size == 0 || EltInfo.first <= (uint64_t)(-1)/Size) && "Overflow in array type bit size evaluation");
856fea966a4103ed9c018d1494b95e9d09b161f5a70Abramo Bagnara    Width = EltInfo.first*Size;
857030d8846c7e520330007087e949f621989876e3aChris Lattner    Align = EltInfo.second;
858cd88b4171753dcb2bc0a21d78f1597c796bb8a20Argyrios Kyrtzidis    Width = llvm::RoundUpToAlignment(Width, Align);
859030d8846c7e520330007087e949f621989876e3aChris Lattner    break;
8605c09a02a5db85e08a432b6eeced9aa656349710dChristopher Lamb  }
861213541a68a3e137d11d2cefb612c6cdb410d7e8eNate Begeman  case Type::ExtVector:
862030d8846c7e520330007087e949f621989876e3aChris Lattner  case Type::Vector: {
8639fcfe926432f3c3f7e9a61219e55c352fd358e45Chris Lattner    const VectorType *VT = cast<VectorType>(T);
8649fcfe926432f3c3f7e9a61219e55c352fd358e45Chris Lattner    std::pair<uint64_t, unsigned> EltInfo = getTypeInfo(VT->getElementType());
8659fcfe926432f3c3f7e9a61219e55c352fd358e45Chris Lattner    Width = EltInfo.first*VT->getNumElements();
8664bd998bbc228915d2b9cae5b67879de48940d05eEli Friedman    Align = Width;
8676fe7c8aa8c7546743ecd0ac0138c2cf5d8155386Nate Begeman    // If the alignment is not a power of 2, round up to the next power of 2.
8686fe7c8aa8c7546743ecd0ac0138c2cf5d8155386Nate Begeman    // This happens for non-power-of-2 length vectors.
8698eefcd353c1d06a10104f69e5079ebab3183f9a3Dan Gohman    if (Align & (Align-1)) {
8709fcfe926432f3c3f7e9a61219e55c352fd358e45Chris Lattner      Align = llvm::NextPowerOf2(Align);
8719fcfe926432f3c3f7e9a61219e55c352fd358e45Chris Lattner      Width = llvm::RoundUpToAlignment(Width, Align);
8729fcfe926432f3c3f7e9a61219e55c352fd358e45Chris Lattner    }
873030d8846c7e520330007087e949f621989876e3aChris Lattner    break;
874030d8846c7e520330007087e949f621989876e3aChris Lattner  }
8755d2a6303467184b1f159bb6556efc434e50e3c28Chris Lattner
8769e9b6dc3fd413f5341fab54b681420eeb21cd169Chris Lattner  case Type::Builtin:
877a7674d8a9a69f3f6fe16e70cf2a3b2b15fb7c43dChris Lattner    switch (cast<BuiltinType>(T)->getKind()) {
878b219cfc4d75f0a03630b7c4509ef791b7e97b2c8David Blaikie    default: llvm_unreachable("Unknown builtin type!");
879d2d2a11a91d7ddf468bfb70f66362d24806ed601Chris Lattner    case BuiltinType::Void:
88018857644059c45da6776f1a288eec7b4cf3a844aDouglas Gregor      // GCC extension: alignof(void) = 8 bits.
88118857644059c45da6776f1a288eec7b4cf3a844aDouglas Gregor      Width = 0;
88218857644059c45da6776f1a288eec7b4cf3a844aDouglas Gregor      Align = 8;
88318857644059c45da6776f1a288eec7b4cf3a844aDouglas Gregor      break;
88418857644059c45da6776f1a288eec7b4cf3a844aDouglas Gregor
8856f62c2abd8077bf70d2166d37e8caa426b34d8e4Chris Lattner    case BuiltinType::Bool:
886bcfd1f55bfbb3e5944cd5e03d07b343e280838c4Douglas Gregor      Width = Target->getBoolWidth();
887bcfd1f55bfbb3e5944cd5e03d07b343e280838c4Douglas Gregor      Align = Target->getBoolAlign();
8886f62c2abd8077bf70d2166d37e8caa426b34d8e4Chris Lattner      break;
889692233e90a99c3a81dd04879d36eb9688f137c44Chris Lattner    case BuiltinType::Char_S:
890692233e90a99c3a81dd04879d36eb9688f137c44Chris Lattner    case BuiltinType::Char_U:
891692233e90a99c3a81dd04879d36eb9688f137c44Chris Lattner    case BuiltinType::UChar:
8926f62c2abd8077bf70d2166d37e8caa426b34d8e4Chris Lattner    case BuiltinType::SChar:
893bcfd1f55bfbb3e5944cd5e03d07b343e280838c4Douglas Gregor      Width = Target->getCharWidth();
894bcfd1f55bfbb3e5944cd5e03d07b343e280838c4Douglas Gregor      Align = Target->getCharAlign();
8956f62c2abd8077bf70d2166d37e8caa426b34d8e4Chris Lattner      break;
8963f59c975aa5d047f7edd1b900b5e885c38af0ef7Chris Lattner    case BuiltinType::WChar_S:
8973f59c975aa5d047f7edd1b900b5e885c38af0ef7Chris Lattner    case BuiltinType::WChar_U:
898bcfd1f55bfbb3e5944cd5e03d07b343e280838c4Douglas Gregor      Width = Target->getWCharWidth();
899bcfd1f55bfbb3e5944cd5e03d07b343e280838c4Douglas Gregor      Align = Target->getWCharAlign();
90064c438a4be2a871fa43c78264663ba1e9788b94dArgyrios Kyrtzidis      break;
901f5c209d23b20ada4a9b6235db50317239cbf6ae1Alisdair Meredith    case BuiltinType::Char16:
902bcfd1f55bfbb3e5944cd5e03d07b343e280838c4Douglas Gregor      Width = Target->getChar16Width();
903bcfd1f55bfbb3e5944cd5e03d07b343e280838c4Douglas Gregor      Align = Target->getChar16Align();
904f5c209d23b20ada4a9b6235db50317239cbf6ae1Alisdair Meredith      break;
905f5c209d23b20ada4a9b6235db50317239cbf6ae1Alisdair Meredith    case BuiltinType::Char32:
906bcfd1f55bfbb3e5944cd5e03d07b343e280838c4Douglas Gregor      Width = Target->getChar32Width();
907bcfd1f55bfbb3e5944cd5e03d07b343e280838c4Douglas Gregor      Align = Target->getChar32Align();
908f5c209d23b20ada4a9b6235db50317239cbf6ae1Alisdair Meredith      break;
909692233e90a99c3a81dd04879d36eb9688f137c44Chris Lattner    case BuiltinType::UShort:
9106f62c2abd8077bf70d2166d37e8caa426b34d8e4Chris Lattner    case BuiltinType::Short:
911bcfd1f55bfbb3e5944cd5e03d07b343e280838c4Douglas Gregor      Width = Target->getShortWidth();
912bcfd1f55bfbb3e5944cd5e03d07b343e280838c4Douglas Gregor      Align = Target->getShortAlign();
9136f62c2abd8077bf70d2166d37e8caa426b34d8e4Chris Lattner      break;
914692233e90a99c3a81dd04879d36eb9688f137c44Chris Lattner    case BuiltinType::UInt:
9156f62c2abd8077bf70d2166d37e8caa426b34d8e4Chris Lattner    case BuiltinType::Int:
916bcfd1f55bfbb3e5944cd5e03d07b343e280838c4Douglas Gregor      Width = Target->getIntWidth();
917bcfd1f55bfbb3e5944cd5e03d07b343e280838c4Douglas Gregor      Align = Target->getIntAlign();
9186f62c2abd8077bf70d2166d37e8caa426b34d8e4Chris Lattner      break;
919692233e90a99c3a81dd04879d36eb9688f137c44Chris Lattner    case BuiltinType::ULong:
9206f62c2abd8077bf70d2166d37e8caa426b34d8e4Chris Lattner    case BuiltinType::Long:
921bcfd1f55bfbb3e5944cd5e03d07b343e280838c4Douglas Gregor      Width = Target->getLongWidth();
922bcfd1f55bfbb3e5944cd5e03d07b343e280838c4Douglas Gregor      Align = Target->getLongAlign();
9236f62c2abd8077bf70d2166d37e8caa426b34d8e4Chris Lattner      break;
924692233e90a99c3a81dd04879d36eb9688f137c44Chris Lattner    case BuiltinType::ULongLong:
9256f62c2abd8077bf70d2166d37e8caa426b34d8e4Chris Lattner    case BuiltinType::LongLong:
926bcfd1f55bfbb3e5944cd5e03d07b343e280838c4Douglas Gregor      Width = Target->getLongLongWidth();
927bcfd1f55bfbb3e5944cd5e03d07b343e280838c4Douglas Gregor      Align = Target->getLongLongAlign();
9286f62c2abd8077bf70d2166d37e8caa426b34d8e4Chris Lattner      break;
929ec16cb9b5a481d62a73ad47fa59034ced4d62022Chris Lattner    case BuiltinType::Int128:
930ec16cb9b5a481d62a73ad47fa59034ced4d62022Chris Lattner    case BuiltinType::UInt128:
931ec16cb9b5a481d62a73ad47fa59034ced4d62022Chris Lattner      Width = 128;
932ec16cb9b5a481d62a73ad47fa59034ced4d62022Chris Lattner      Align = 128; // int128_t is 128-bit aligned on all targets.
933ec16cb9b5a481d62a73ad47fa59034ced4d62022Chris Lattner      break;
934aa4a99b4a62615db243f7a5c433169f2fc704420Anton Korobeynikov    case BuiltinType::Half:
935aa4a99b4a62615db243f7a5c433169f2fc704420Anton Korobeynikov      Width = Target->getHalfWidth();
936aa4a99b4a62615db243f7a5c433169f2fc704420Anton Korobeynikov      Align = Target->getHalfAlign();
937aa4a99b4a62615db243f7a5c433169f2fc704420Anton Korobeynikov      break;
9386f62c2abd8077bf70d2166d37e8caa426b34d8e4Chris Lattner    case BuiltinType::Float:
939bcfd1f55bfbb3e5944cd5e03d07b343e280838c4Douglas Gregor      Width = Target->getFloatWidth();
940bcfd1f55bfbb3e5944cd5e03d07b343e280838c4Douglas Gregor      Align = Target->getFloatAlign();
9416f62c2abd8077bf70d2166d37e8caa426b34d8e4Chris Lattner      break;
9426f62c2abd8077bf70d2166d37e8caa426b34d8e4Chris Lattner    case BuiltinType::Double:
943bcfd1f55bfbb3e5944cd5e03d07b343e280838c4Douglas Gregor      Width = Target->getDoubleWidth();
944bcfd1f55bfbb3e5944cd5e03d07b343e280838c4Douglas Gregor      Align = Target->getDoubleAlign();
9456f62c2abd8077bf70d2166d37e8caa426b34d8e4Chris Lattner      break;
9466f62c2abd8077bf70d2166d37e8caa426b34d8e4Chris Lattner    case BuiltinType::LongDouble:
947bcfd1f55bfbb3e5944cd5e03d07b343e280838c4Douglas Gregor      Width = Target->getLongDoubleWidth();
948bcfd1f55bfbb3e5944cd5e03d07b343e280838c4Douglas Gregor      Align = Target->getLongDoubleAlign();
9496f62c2abd8077bf70d2166d37e8caa426b34d8e4Chris Lattner      break;
9506e8ed16ffef02b82995a90bdcf10ffff7d63839aSebastian Redl    case BuiltinType::NullPtr:
951bcfd1f55bfbb3e5944cd5e03d07b343e280838c4Douglas Gregor      Width = Target->getPointerWidth(0); // C++ 3.9.1p11: sizeof(nullptr_t)
952bcfd1f55bfbb3e5944cd5e03d07b343e280838c4Douglas Gregor      Align = Target->getPointerAlign(0); //   == sizeof(void*)
9531590d9c0fec4c710c2962e4bb71f76979b5163d3Sebastian Redl      break;
954e04f5fc25cf49e8a5b836459d836c20dc3229a95Fariborz Jahanian    case BuiltinType::ObjCId:
955e04f5fc25cf49e8a5b836459d836c20dc3229a95Fariborz Jahanian    case BuiltinType::ObjCClass:
956e04f5fc25cf49e8a5b836459d836c20dc3229a95Fariborz Jahanian    case BuiltinType::ObjCSel:
957bcfd1f55bfbb3e5944cd5e03d07b343e280838c4Douglas Gregor      Width = Target->getPointerWidth(0);
958bcfd1f55bfbb3e5944cd5e03d07b343e280838c4Douglas Gregor      Align = Target->getPointerAlign(0);
959e04f5fc25cf49e8a5b836459d836c20dc3229a95Fariborz Jahanian      break;
960a7674d8a9a69f3f6fe16e70cf2a3b2b15fb7c43dChris Lattner    }
961bfef6d7c67831a135d6ab79931f010f750a730adChris Lattner    break;
962d1b3c2dd5bc1f3103bee6137957aa7c5f8f2f0bcSteve Naroff  case Type::ObjCObjectPointer:
963bcfd1f55bfbb3e5944cd5e03d07b343e280838c4Douglas Gregor    Width = Target->getPointerWidth(0);
964bcfd1f55bfbb3e5944cd5e03d07b343e280838c4Douglas Gregor    Align = Target->getPointerAlign(0);
9656f62c2abd8077bf70d2166d37e8caa426b34d8e4Chris Lattner    break;
966485eeff9ba73376c8e01179bf1a501b1723446cbSteve Naroff  case Type::BlockPointer: {
967207f4d8543529221932af82836016a2ef066c917Peter Collingbourne    unsigned AS = getTargetAddressSpace(
968207f4d8543529221932af82836016a2ef066c917Peter Collingbourne        cast<BlockPointerType>(T)->getPointeeType());
969bcfd1f55bfbb3e5944cd5e03d07b343e280838c4Douglas Gregor    Width = Target->getPointerWidth(AS);
970bcfd1f55bfbb3e5944cd5e03d07b343e280838c4Douglas Gregor    Align = Target->getPointerAlign(AS);
971485eeff9ba73376c8e01179bf1a501b1723446cbSteve Naroff    break;
972485eeff9ba73376c8e01179bf1a501b1723446cbSteve Naroff  }
9735d484e8cf710207010720589d89602233de61d01Sebastian Redl  case Type::LValueReference:
9745d484e8cf710207010720589d89602233de61d01Sebastian Redl  case Type::RValueReference: {
9755d484e8cf710207010720589d89602233de61d01Sebastian Redl    // alignof and sizeof should never enter this code path here, so we go
9765d484e8cf710207010720589d89602233de61d01Sebastian Redl    // the pointer route.
977207f4d8543529221932af82836016a2ef066c917Peter Collingbourne    unsigned AS = getTargetAddressSpace(
978207f4d8543529221932af82836016a2ef066c917Peter Collingbourne        cast<ReferenceType>(T)->getPointeeType());
979bcfd1f55bfbb3e5944cd5e03d07b343e280838c4Douglas Gregor    Width = Target->getPointerWidth(AS);
980bcfd1f55bfbb3e5944cd5e03d07b343e280838c4Douglas Gregor    Align = Target->getPointerAlign(AS);
9815d484e8cf710207010720589d89602233de61d01Sebastian Redl    break;
9825d484e8cf710207010720589d89602233de61d01Sebastian Redl  }
983f72a44330b9d9a4b2d93e9b91cfb8ab7bd4a0643Chris Lattner  case Type::Pointer: {
984207f4d8543529221932af82836016a2ef066c917Peter Collingbourne    unsigned AS = getTargetAddressSpace(cast<PointerType>(T)->getPointeeType());
985bcfd1f55bfbb3e5944cd5e03d07b343e280838c4Douglas Gregor    Width = Target->getPointerWidth(AS);
986bcfd1f55bfbb3e5944cd5e03d07b343e280838c4Douglas Gregor    Align = Target->getPointerAlign(AS);
987f72a44330b9d9a4b2d93e9b91cfb8ab7bd4a0643Chris Lattner    break;
988f72a44330b9d9a4b2d93e9b91cfb8ab7bd4a0643Chris Lattner  }
989f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl  case Type::MemberPointer: {
990071cc7deffad608165b1ddd5263e8bf181861520Charles Davis    const MemberPointerType *MPT = cast<MemberPointerType>(T);
9911eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    std::pair<uint64_t, unsigned> PtrDiffInfo =
9921cca74ef3627a3a0ab14501d23e336548f6611b2Anders Carlsson      getTypeInfo(getPointerDiffType());
993071cc7deffad608165b1ddd5263e8bf181861520Charles Davis    Width = PtrDiffInfo.first * ABI->getMemberPointerSize(MPT);
9941cca74ef3627a3a0ab14501d23e336548f6611b2Anders Carlsson    Align = PtrDiffInfo.second;
9951cca74ef3627a3a0ab14501d23e336548f6611b2Anders Carlsson    break;
996f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl  }
9975d2a6303467184b1f159bb6556efc434e50e3c28Chris Lattner  case Type::Complex: {
9985d2a6303467184b1f159bb6556efc434e50e3c28Chris Lattner    // Complex types have the same alignment as their elements, but twice the
9995d2a6303467184b1f159bb6556efc434e50e3c28Chris Lattner    // size.
10001eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    std::pair<uint64_t, unsigned> EltInfo =
100198be4943e8dc4f3905629a7102668960873cf863Chris Lattner      getTypeInfo(cast<ComplexType>(T)->getElementType());
10029e9b6dc3fd413f5341fab54b681420eeb21cd169Chris Lattner    Width = EltInfo.first*2;
10035d2a6303467184b1f159bb6556efc434e50e3c28Chris Lattner    Align = EltInfo.second;
10045d2a6303467184b1f159bb6556efc434e50e3c28Chris Lattner    break;
10055d2a6303467184b1f159bb6556efc434e50e3c28Chris Lattner  }
1006c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall  case Type::ObjCObject:
1007c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall    return getTypeInfo(cast<ObjCObjectType>(T)->getBaseType().getTypePtr());
100844a3dded8080c5c9cfdad208ade8f8f7850d9a4fDevang Patel  case Type::ObjCInterface: {
10091d75118af76cae2bfc06389cde410e14bd0a19fcDaniel Dunbar    const ObjCInterfaceType *ObjCI = cast<ObjCInterfaceType>(T);
101044a3dded8080c5c9cfdad208ade8f8f7850d9a4fDevang Patel    const ASTRecordLayout &Layout = getASTObjCInterfaceLayout(ObjCI->getDecl());
1011dd76a9ab9ea675671200f94b18ce95766841952bKen Dyck    Width = toBits(Layout.getSize());
1012dac54c124e302d6f028ea5723c425b7f66fc7c71Ken Dyck    Align = toBits(Layout.getAlignment());
101344a3dded8080c5c9cfdad208ade8f8f7850d9a4fDevang Patel    break;
101444a3dded8080c5c9cfdad208ade8f8f7850d9a4fDevang Patel  }
101572564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor  case Type::Record:
101672564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor  case Type::Enum: {
10171d75118af76cae2bfc06389cde410e14bd0a19fcDaniel Dunbar    const TagType *TT = cast<TagType>(T);
10181d75118af76cae2bfc06389cde410e14bd0a19fcDaniel Dunbar
10191d75118af76cae2bfc06389cde410e14bd0a19fcDaniel Dunbar    if (TT->getDecl()->isInvalidDecl()) {
102022ce41d9fc8509da65858c75bf5b3c4dae2d8c04Douglas Gregor      Width = 8;
102122ce41d9fc8509da65858c75bf5b3c4dae2d8c04Douglas Gregor      Align = 8;
10228389eab190afef3462f6418b8d8fb70fb01c4005Chris Lattner      break;
10238389eab190afef3462f6418b8d8fb70fb01c4005Chris Lattner    }
10241eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
10251d75118af76cae2bfc06389cde410e14bd0a19fcDaniel Dunbar    if (const EnumType *ET = dyn_cast<EnumType>(TT))
10267176331b0f5cfaaa2b5aa487a6660e859e371119Chris Lattner      return getTypeInfo(ET->getDecl()->getIntegerType());
10277176331b0f5cfaaa2b5aa487a6660e859e371119Chris Lattner
10281d75118af76cae2bfc06389cde410e14bd0a19fcDaniel Dunbar    const RecordType *RT = cast<RecordType>(TT);
10297176331b0f5cfaaa2b5aa487a6660e859e371119Chris Lattner    const ASTRecordLayout &Layout = getASTRecordLayout(RT->getDecl());
1030dd76a9ab9ea675671200f94b18ce95766841952bKen Dyck    Width = toBits(Layout.getSize());
1031dac54c124e302d6f028ea5723c425b7f66fc7c71Ken Dyck    Align = toBits(Layout.getAlignment());
1032dc0d73e6495404418acf8548875aeaff07791a74Chris Lattner    break;
1033a7674d8a9a69f3f6fe16e70cf2a3b2b15fb7c43dChris Lattner  }
10347532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor
10359fcfe926432f3c3f7e9a61219e55c352fd358e45Chris Lattner  case Type::SubstTemplateTypeParm:
103649a832bd499d6f61c23655f1fac99f0dd229756eJohn McCall    return getTypeInfo(cast<SubstTemplateTypeParmType>(T)->
103749a832bd499d6f61c23655f1fac99f0dd229756eJohn McCall                       getReplacementType().getTypePtr());
103849a832bd499d6f61c23655f1fac99f0dd229756eJohn McCall
103934b41d939a1328f484511c6002ba2456db879a29Richard Smith  case Type::Auto: {
104034b41d939a1328f484511c6002ba2456db879a29Richard Smith    const AutoType *A = cast<AutoType>(T);
104134b41d939a1328f484511c6002ba2456db879a29Richard Smith    assert(A->isDeduced() && "Cannot request the size of a dependent type");
1042dc856aff4428380baa9afb5577ea04f8fb6beb13Matt Beaumont-Gay    return getTypeInfo(A->getDeducedType().getTypePtr());
104334b41d939a1328f484511c6002ba2456db879a29Richard Smith  }
104434b41d939a1328f484511c6002ba2456db879a29Richard Smith
1045075f8f1b6bed4d1b224c74f87508534cc6392ce6Abramo Bagnara  case Type::Paren:
1046075f8f1b6bed4d1b224c74f87508534cc6392ce6Abramo Bagnara    return getTypeInfo(cast<ParenType>(T)->getInnerType().getTypePtr());
1047075f8f1b6bed4d1b224c74f87508534cc6392ce6Abramo Bagnara
104818857644059c45da6776f1a288eec7b4cf3a844aDouglas Gregor  case Type::Typedef: {
1049162e1c1b487352434552147967c3dd296ebee2f7Richard Smith    const TypedefNameDecl *Typedef = cast<TypedefType>(T)->getDecl();
1050df1367af26cb2959775e9511108f12dcd2370a27Douglas Gregor    std::pair<uint64_t, unsigned> Info
1051df1367af26cb2959775e9511108f12dcd2370a27Douglas Gregor      = getTypeInfo(Typedef->getUnderlyingType().getTypePtr());
1052c1de52de64725945e5ae87e6f99ddedf161856e5Chris Lattner    // If the typedef has an aligned attribute on it, it overrides any computed
1053c1de52de64725945e5ae87e6f99ddedf161856e5Chris Lattner    // alignment we have.  This violates the GCC documentation (which says that
1054c1de52de64725945e5ae87e6f99ddedf161856e5Chris Lattner    // attribute(aligned) can only round up) but matches its implementation.
1055c1de52de64725945e5ae87e6f99ddedf161856e5Chris Lattner    if (unsigned AttrAlign = Typedef->getMaxAlignment())
1056c1de52de64725945e5ae87e6f99ddedf161856e5Chris Lattner      Align = AttrAlign;
1057c1de52de64725945e5ae87e6f99ddedf161856e5Chris Lattner    else
1058c1de52de64725945e5ae87e6f99ddedf161856e5Chris Lattner      Align = Info.second;
1059df1367af26cb2959775e9511108f12dcd2370a27Douglas Gregor    Width = Info.first;
10607532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor    break;
10617176331b0f5cfaaa2b5aa487a6660e859e371119Chris Lattner  }
106218857644059c45da6776f1a288eec7b4cf3a844aDouglas Gregor
106318857644059c45da6776f1a288eec7b4cf3a844aDouglas Gregor  case Type::TypeOfExpr:
106418857644059c45da6776f1a288eec7b4cf3a844aDouglas Gregor    return getTypeInfo(cast<TypeOfExprType>(T)->getUnderlyingExpr()->getType()
106518857644059c45da6776f1a288eec7b4cf3a844aDouglas Gregor                         .getTypePtr());
106618857644059c45da6776f1a288eec7b4cf3a844aDouglas Gregor
106718857644059c45da6776f1a288eec7b4cf3a844aDouglas Gregor  case Type::TypeOf:
106818857644059c45da6776f1a288eec7b4cf3a844aDouglas Gregor    return getTypeInfo(cast<TypeOfType>(T)->getUnderlyingType().getTypePtr());
106918857644059c45da6776f1a288eec7b4cf3a844aDouglas Gregor
1070395b475a4474f1c7574d927ad142ca0c7997cbcaAnders Carlsson  case Type::Decltype:
1071395b475a4474f1c7574d927ad142ca0c7997cbcaAnders Carlsson    return getTypeInfo(cast<DecltypeType>(T)->getUnderlyingExpr()->getType()
1072395b475a4474f1c7574d927ad142ca0c7997cbcaAnders Carlsson                        .getTypePtr());
1073395b475a4474f1c7574d927ad142ca0c7997cbcaAnders Carlsson
1074ca63c200346c0ca9e00194ec6e34a5a7b0ed9321Sean Hunt  case Type::UnaryTransform:
1075ca63c200346c0ca9e00194ec6e34a5a7b0ed9321Sean Hunt    return getTypeInfo(cast<UnaryTransformType>(T)->getUnderlyingType());
1076ca63c200346c0ca9e00194ec6e34a5a7b0ed9321Sean Hunt
1077465d41b92b2c862f3062c412a0538db65c6a2661Abramo Bagnara  case Type::Elaborated:
1078465d41b92b2c862f3062c412a0538db65c6a2661Abramo Bagnara    return getTypeInfo(cast<ElaboratedType>(T)->getNamedType().getTypePtr());
10791eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
10809d156a7b1b2771e191f2f5a45a7b7a694129463bJohn McCall  case Type::Attributed:
10819d156a7b1b2771e191f2f5a45a7b7a694129463bJohn McCall    return getTypeInfo(
10829d156a7b1b2771e191f2f5a45a7b7a694129463bJohn McCall                  cast<AttributedType>(T)->getEquivalentType().getTypePtr());
10839d156a7b1b2771e191f2f5a45a7b7a694129463bJohn McCall
10843e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith  case Type::TemplateSpecialization: {
10851eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    assert(getCanonicalType(T) != T &&
108618857644059c45da6776f1a288eec7b4cf3a844aDouglas Gregor           "Cannot request the size of a dependent type");
10873e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith    const TemplateSpecializationType *TST = cast<TemplateSpecializationType>(T);
10883e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith    // A type alias template specialization may refer to a typedef with the
10893e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith    // aligned attribute on it.
10903e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith    if (TST->isTypeAlias())
10913e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith      return getTypeInfo(TST->getAliasedType().getTypePtr());
10923e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith    else
10933e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith      return getTypeInfo(getCanonicalType(T));
10943e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith  }
10953e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith
1096b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman  case Type::Atomic: {
10972be460723940f8184ec36529b6f6ddf59c04e411Eli Friedman    std::pair<uint64_t, unsigned> Info
10982be460723940f8184ec36529b6f6ddf59c04e411Eli Friedman      = getTypeInfo(cast<AtomicType>(T)->getValueType());
10992be460723940f8184ec36529b6f6ddf59c04e411Eli Friedman    Width = Info.first;
11002be460723940f8184ec36529b6f6ddf59c04e411Eli Friedman    Align = Info.second;
11012be460723940f8184ec36529b6f6ddf59c04e411Eli Friedman    if (Width != 0 && Width <= Target->getMaxAtomicPromoteWidth() &&
11022be460723940f8184ec36529b6f6ddf59c04e411Eli Friedman        llvm::isPowerOf2_64(Width)) {
11032be460723940f8184ec36529b6f6ddf59c04e411Eli Friedman      // We can potentially perform lock-free atomic operations for this
11042be460723940f8184ec36529b6f6ddf59c04e411Eli Friedman      // type; promote the alignment appropriately.
11052be460723940f8184ec36529b6f6ddf59c04e411Eli Friedman      // FIXME: We could potentially promote the width here as well...
11062be460723940f8184ec36529b6f6ddf59c04e411Eli Friedman      // is that worthwhile?  (Non-struct atomic types generally have
11072be460723940f8184ec36529b6f6ddf59c04e411Eli Friedman      // power-of-two size anyway, but structs might not.  Requires a bit
11082be460723940f8184ec36529b6f6ddf59c04e411Eli Friedman      // of implementation work to make sure we zero out the extra bits.)
11092be460723940f8184ec36529b6f6ddf59c04e411Eli Friedman      Align = static_cast<unsigned>(Width);
11102be460723940f8184ec36529b6f6ddf59c04e411Eli Friedman    }
1111b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman  }
1112b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman
111318857644059c45da6776f1a288eec7b4cf3a844aDouglas Gregor  }
11141eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
11152be460723940f8184ec36529b6f6ddf59c04e411Eli Friedman  assert(llvm::isPowerOf2_32(Align) && "Alignment must be power of 2");
11169e9b6dc3fd413f5341fab54b681420eeb21cd169Chris Lattner  return std::make_pair(Width, Align);
1117a7674d8a9a69f3f6fe16e70cf2a3b2b15fb7c43dChris Lattner}
1118a7674d8a9a69f3f6fe16e70cf2a3b2b15fb7c43dChris Lattner
1119eb6f5dc86531f794ba7746a2da4d28e37cf5da7eKen Dyck/// toCharUnitsFromBits - Convert a size in bits to a size in characters.
1120eb6f5dc86531f794ba7746a2da4d28e37cf5da7eKen DyckCharUnits ASTContext::toCharUnitsFromBits(int64_t BitSize) const {
1121eb6f5dc86531f794ba7746a2da4d28e37cf5da7eKen Dyck  return CharUnits::fromQuantity(BitSize / getCharWidth());
1122eb6f5dc86531f794ba7746a2da4d28e37cf5da7eKen Dyck}
1123eb6f5dc86531f794ba7746a2da4d28e37cf5da7eKen Dyck
1124dd76a9ab9ea675671200f94b18ce95766841952bKen Dyck/// toBits - Convert a size in characters to a size in characters.
1125dd76a9ab9ea675671200f94b18ce95766841952bKen Dyckint64_t ASTContext::toBits(CharUnits CharSize) const {
1126dd76a9ab9ea675671200f94b18ce95766841952bKen Dyck  return CharSize.getQuantity() * getCharWidth();
1127dd76a9ab9ea675671200f94b18ce95766841952bKen Dyck}
1128dd76a9ab9ea675671200f94b18ce95766841952bKen Dyck
1129bdc601b196c48d4cd56a5ceb45d41ae4e87371abKen Dyck/// getTypeSizeInChars - Return the size of the specified type, in characters.
1130bdc601b196c48d4cd56a5ceb45d41ae4e87371abKen Dyck/// This method does not work on incomplete types.
11314ba2a17694148e16eaa8d3917f657ffcd3667be4Jay FoadCharUnits ASTContext::getTypeSizeInChars(QualType T) const {
1132eb6f5dc86531f794ba7746a2da4d28e37cf5da7eKen Dyck  return toCharUnitsFromBits(getTypeSize(T));
1133bdc601b196c48d4cd56a5ceb45d41ae4e87371abKen Dyck}
11344ba2a17694148e16eaa8d3917f657ffcd3667be4Jay FoadCharUnits ASTContext::getTypeSizeInChars(const Type *T) const {
1135eb6f5dc86531f794ba7746a2da4d28e37cf5da7eKen Dyck  return toCharUnitsFromBits(getTypeSize(T));
1136bdc601b196c48d4cd56a5ceb45d41ae4e87371abKen Dyck}
1137bdc601b196c48d4cd56a5ceb45d41ae4e87371abKen Dyck
113816e20cce43385001f33f8e3f90ee345609c805d1Ken Dyck/// getTypeAlignInChars - Return the ABI-specified alignment of a type, in
113986fa4311c8a330957ff5b765fbb0a7750ecd38c9Ken Dyck/// characters. This method does not work on incomplete types.
11404ba2a17694148e16eaa8d3917f657ffcd3667be4Jay FoadCharUnits ASTContext::getTypeAlignInChars(QualType T) const {
1141eb6f5dc86531f794ba7746a2da4d28e37cf5da7eKen Dyck  return toCharUnitsFromBits(getTypeAlign(T));
114286fa4311c8a330957ff5b765fbb0a7750ecd38c9Ken Dyck}
11434ba2a17694148e16eaa8d3917f657ffcd3667be4Jay FoadCharUnits ASTContext::getTypeAlignInChars(const Type *T) const {
1144eb6f5dc86531f794ba7746a2da4d28e37cf5da7eKen Dyck  return toCharUnitsFromBits(getTypeAlign(T));
114586fa4311c8a330957ff5b765fbb0a7750ecd38c9Ken Dyck}
114686fa4311c8a330957ff5b765fbb0a7750ecd38c9Ken Dyck
114734ebde404dc17d89487b07e6daaf1b47d5dfee39Chris Lattner/// getPreferredTypeAlign - Return the "preferred" alignment of the specified
114834ebde404dc17d89487b07e6daaf1b47d5dfee39Chris Lattner/// type for the current target in bits.  This can be different than the ABI
114934ebde404dc17d89487b07e6daaf1b47d5dfee39Chris Lattner/// alignment in cases where it is beneficial for performance to overalign
115034ebde404dc17d89487b07e6daaf1b47d5dfee39Chris Lattner/// a data type.
11514ba2a17694148e16eaa8d3917f657ffcd3667be4Jay Foadunsigned ASTContext::getPreferredTypeAlign(const Type *T) const {
115234ebde404dc17d89487b07e6daaf1b47d5dfee39Chris Lattner  unsigned ABIAlign = getTypeAlign(T);
11531eed60297ef4701b899c6a3b9680bf08f3403422Eli Friedman
11541eed60297ef4701b899c6a3b9680bf08f3403422Eli Friedman  // Double and long long should be naturally aligned if possible.
1155183700f494ec9b6701b6efe82bcb25f4c79ba561John McCall  if (const ComplexType* CT = T->getAs<ComplexType>())
11561eed60297ef4701b899c6a3b9680bf08f3403422Eli Friedman    T = CT->getElementType().getTypePtr();
11571eed60297ef4701b899c6a3b9680bf08f3403422Eli Friedman  if (T->isSpecificBuiltinType(BuiltinType::Double) ||
11581eed60297ef4701b899c6a3b9680bf08f3403422Eli Friedman      T->isSpecificBuiltinType(BuiltinType::LongLong))
11591eed60297ef4701b899c6a3b9680bf08f3403422Eli Friedman    return std::max(ABIAlign, (unsigned)getTypeSize(T));
11601eed60297ef4701b899c6a3b9680bf08f3403422Eli Friedman
116134ebde404dc17d89487b07e6daaf1b47d5dfee39Chris Lattner  return ABIAlign;
116234ebde404dc17d89487b07e6daaf1b47d5dfee39Chris Lattner}
116334ebde404dc17d89487b07e6daaf1b47d5dfee39Chris Lattner
11642c18bb7c9fca66c30b6eabbdcbc6399d24a54fa9Fariborz Jahanian/// DeepCollectObjCIvars -
11652c18bb7c9fca66c30b6eabbdcbc6399d24a54fa9Fariborz Jahanian/// This routine first collects all declared, but not synthesized, ivars in
11662c18bb7c9fca66c30b6eabbdcbc6399d24a54fa9Fariborz Jahanian/// super class and then collects all ivars, including those synthesized for
11672c18bb7c9fca66c30b6eabbdcbc6399d24a54fa9Fariborz Jahanian/// current class. This routine is used for implementation of current class
11682c18bb7c9fca66c30b6eabbdcbc6399d24a54fa9Fariborz Jahanian/// when all ivars, declared and synthesized are known.
11699820074dd47d37681085e964cd3392ac0b3e67b9Fariborz Jahanian///
11702c18bb7c9fca66c30b6eabbdcbc6399d24a54fa9Fariborz Jahanianvoid ASTContext::DeepCollectObjCIvars(const ObjCInterfaceDecl *OI,
11712c18bb7c9fca66c30b6eabbdcbc6399d24a54fa9Fariborz Jahanian                                      bool leafClass,
1172db8264e4c5ffd7af6fbad4ca4306bd382bb02691Jordy Rose                            SmallVectorImpl<const ObjCIvarDecl*> &Ivars) const {
11732c18bb7c9fca66c30b6eabbdcbc6399d24a54fa9Fariborz Jahanian  if (const ObjCInterfaceDecl *SuperClass = OI->getSuperClass())
11742c18bb7c9fca66c30b6eabbdcbc6399d24a54fa9Fariborz Jahanian    DeepCollectObjCIvars(SuperClass, false, Ivars);
11752c18bb7c9fca66c30b6eabbdcbc6399d24a54fa9Fariborz Jahanian  if (!leafClass) {
11762c18bb7c9fca66c30b6eabbdcbc6399d24a54fa9Fariborz Jahanian    for (ObjCInterfaceDecl::ivar_iterator I = OI->ivar_begin(),
11772c18bb7c9fca66c30b6eabbdcbc6399d24a54fa9Fariborz Jahanian         E = OI->ivar_end(); I != E; ++I)
117811062e11236b7bc689dad150e8b490fd6b063ec3Fariborz Jahanian      Ivars.push_back(*I);
11793060178ad9df29789505c1e6debcfc80a3a13587Chad Rosier  } else {
1180bf9eb88792e022e54a658657bf22e1925948e384Fariborz Jahanian    ObjCInterfaceDecl *IDecl = const_cast<ObjCInterfaceDecl *>(OI);
1181db8264e4c5ffd7af6fbad4ca4306bd382bb02691Jordy Rose    for (const ObjCIvarDecl *Iv = IDecl->all_declared_ivar_begin(); Iv;
1182bf9eb88792e022e54a658657bf22e1925948e384Fariborz Jahanian         Iv= Iv->getNextIvar())
1183bf9eb88792e022e54a658657bf22e1925948e384Fariborz Jahanian      Ivars.push_back(Iv);
1184bf9eb88792e022e54a658657bf22e1925948e384Fariborz Jahanian  }
11859820074dd47d37681085e964cd3392ac0b3e67b9Fariborz Jahanian}
11869820074dd47d37681085e964cd3392ac0b3e67b9Fariborz Jahanian
1187e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanian/// CollectInheritedProtocols - Collect all protocols in current class and
1188e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanian/// those inherited by it.
1189e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanianvoid ASTContext::CollectInheritedProtocols(const Decl *CDecl,
1190432a8893f7e30d141d7f279bd00b741a3cdac81fFariborz Jahanian                          llvm::SmallPtrSet<ObjCProtocolDecl*, 8> &Protocols) {
1191e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanian  if (const ObjCInterfaceDecl *OI = dyn_cast<ObjCInterfaceDecl>(CDecl)) {
119253b9441b5a81a24fa1f66f3f6416f1e36baa9c2fTed Kremenek    // We can use protocol_iterator here instead of
119353b9441b5a81a24fa1f66f3f6416f1e36baa9c2fTed Kremenek    // all_referenced_protocol_iterator since we are walking all categories.
119453b9441b5a81a24fa1f66f3f6416f1e36baa9c2fTed Kremenek    for (ObjCInterfaceDecl::all_protocol_iterator P = OI->all_referenced_protocol_begin(),
119553b9441b5a81a24fa1f66f3f6416f1e36baa9c2fTed Kremenek         PE = OI->all_referenced_protocol_end(); P != PE; ++P) {
1196e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanian      ObjCProtocolDecl *Proto = (*P);
11973fc73ee0c613715ebce78e30b4d050ea715a007dDouglas Gregor      Protocols.insert(Proto->getCanonicalDecl());
1198e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanian      for (ObjCProtocolDecl::protocol_iterator P = Proto->protocol_begin(),
1199b2f812165676230bce5d0215e49a4749c451ca9cFariborz Jahanian           PE = Proto->protocol_end(); P != PE; ++P) {
12003fc73ee0c613715ebce78e30b4d050ea715a007dDouglas Gregor        Protocols.insert((*P)->getCanonicalDecl());
1201e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanian        CollectInheritedProtocols(*P, Protocols);
1202e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanian      }
1203b2f812165676230bce5d0215e49a4749c451ca9cFariborz Jahanian    }
1204e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanian
1205e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanian    // Categories of this Interface.
1206e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanian    for (const ObjCCategoryDecl *CDeclChain = OI->getCategoryList();
1207e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanian         CDeclChain; CDeclChain = CDeclChain->getNextClassCategory())
1208e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanian      CollectInheritedProtocols(CDeclChain, Protocols);
1209e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanian    if (ObjCInterfaceDecl *SD = OI->getSuperClass())
1210e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanian      while (SD) {
1211e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanian        CollectInheritedProtocols(SD, Protocols);
1212e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanian        SD = SD->getSuperClass();
1213e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanian      }
1214b170ca5f4a8397c10e52050ff5df6885a3e6eca9Benjamin Kramer  } else if (const ObjCCategoryDecl *OC = dyn_cast<ObjCCategoryDecl>(CDecl)) {
121553b9441b5a81a24fa1f66f3f6416f1e36baa9c2fTed Kremenek    for (ObjCCategoryDecl::protocol_iterator P = OC->protocol_begin(),
1216e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanian         PE = OC->protocol_end(); P != PE; ++P) {
1217e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanian      ObjCProtocolDecl *Proto = (*P);
12183fc73ee0c613715ebce78e30b4d050ea715a007dDouglas Gregor      Protocols.insert(Proto->getCanonicalDecl());
1219e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanian      for (ObjCProtocolDecl::protocol_iterator P = Proto->protocol_begin(),
1220e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanian           PE = Proto->protocol_end(); P != PE; ++P)
1221e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanian        CollectInheritedProtocols(*P, Protocols);
1222e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanian    }
1223b170ca5f4a8397c10e52050ff5df6885a3e6eca9Benjamin Kramer  } else if (const ObjCProtocolDecl *OP = dyn_cast<ObjCProtocolDecl>(CDecl)) {
1224e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanian    for (ObjCProtocolDecl::protocol_iterator P = OP->protocol_begin(),
1225e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanian         PE = OP->protocol_end(); P != PE; ++P) {
1226e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanian      ObjCProtocolDecl *Proto = (*P);
12273fc73ee0c613715ebce78e30b4d050ea715a007dDouglas Gregor      Protocols.insert(Proto->getCanonicalDecl());
1228e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanian      for (ObjCProtocolDecl::protocol_iterator P = Proto->protocol_begin(),
1229e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanian           PE = Proto->protocol_end(); P != PE; ++P)
1230e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanian        CollectInheritedProtocols(*P, Protocols);
1231e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanian    }
1232e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanian  }
1233e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanian}
1234e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanian
12354ba2a17694148e16eaa8d3917f657ffcd3667be4Jay Foadunsigned ASTContext::CountNonClassIvars(const ObjCInterfaceDecl *OI) const {
12363bfacdf6f0706987c1b33c625cd68fb880881c22Fariborz Jahanian  unsigned count = 0;
12373bfacdf6f0706987c1b33c625cd68fb880881c22Fariborz Jahanian  // Count ivars declared in class extension.
123880aa1cd7973561889e51c1c152c8990a8de9c953Fariborz Jahanian  for (const ObjCCategoryDecl *CDecl = OI->getFirstClassExtension(); CDecl;
123980aa1cd7973561889e51c1c152c8990a8de9c953Fariborz Jahanian       CDecl = CDecl->getNextClassExtension())
1240b170ca5f4a8397c10e52050ff5df6885a3e6eca9Benjamin Kramer    count += CDecl->ivar_size();
1241b170ca5f4a8397c10e52050ff5df6885a3e6eca9Benjamin Kramer
12423bfacdf6f0706987c1b33c625cd68fb880881c22Fariborz Jahanian  // Count ivar defined in this class's implementation.  This
12433bfacdf6f0706987c1b33c625cd68fb880881c22Fariborz Jahanian  // includes synthesized ivars.
12443bfacdf6f0706987c1b33c625cd68fb880881c22Fariborz Jahanian  if (ObjCImplementationDecl *ImplDecl = OI->getImplementation())
1245b170ca5f4a8397c10e52050ff5df6885a3e6eca9Benjamin Kramer    count += ImplDecl->ivar_size();
1246b170ca5f4a8397c10e52050ff5df6885a3e6eca9Benjamin Kramer
12478e6ac1d80055fa37b9b84029c7e751624ba7f84cFariborz Jahanian  return count;
12488e6ac1d80055fa37b9b84029c7e751624ba7f84cFariborz Jahanian}
12498e6ac1d80055fa37b9b84029c7e751624ba7f84cFariborz Jahanian
12508a1d722f13df383600f36d77f842957c8adb5f1bArgyrios Kyrtzidis/// \brief Get the implementation of ObjCInterfaceDecl,or NULL if none exists.
12518a1d722f13df383600f36d77f842957c8adb5f1bArgyrios KyrtzidisObjCImplementationDecl *ASTContext::getObjCImplementation(ObjCInterfaceDecl *D) {
12528a1d722f13df383600f36d77f842957c8adb5f1bArgyrios Kyrtzidis  llvm::DenseMap<ObjCContainerDecl*, ObjCImplDecl*>::iterator
12538a1d722f13df383600f36d77f842957c8adb5f1bArgyrios Kyrtzidis    I = ObjCImpls.find(D);
12548a1d722f13df383600f36d77f842957c8adb5f1bArgyrios Kyrtzidis  if (I != ObjCImpls.end())
12558a1d722f13df383600f36d77f842957c8adb5f1bArgyrios Kyrtzidis    return cast<ObjCImplementationDecl>(I->second);
12568a1d722f13df383600f36d77f842957c8adb5f1bArgyrios Kyrtzidis  return 0;
12578a1d722f13df383600f36d77f842957c8adb5f1bArgyrios Kyrtzidis}
12588a1d722f13df383600f36d77f842957c8adb5f1bArgyrios Kyrtzidis/// \brief Get the implementation of ObjCCategoryDecl, or NULL if none exists.
12598a1d722f13df383600f36d77f842957c8adb5f1bArgyrios KyrtzidisObjCCategoryImplDecl *ASTContext::getObjCImplementation(ObjCCategoryDecl *D) {
12608a1d722f13df383600f36d77f842957c8adb5f1bArgyrios Kyrtzidis  llvm::DenseMap<ObjCContainerDecl*, ObjCImplDecl*>::iterator
12618a1d722f13df383600f36d77f842957c8adb5f1bArgyrios Kyrtzidis    I = ObjCImpls.find(D);
12628a1d722f13df383600f36d77f842957c8adb5f1bArgyrios Kyrtzidis  if (I != ObjCImpls.end())
12638a1d722f13df383600f36d77f842957c8adb5f1bArgyrios Kyrtzidis    return cast<ObjCCategoryImplDecl>(I->second);
12648a1d722f13df383600f36d77f842957c8adb5f1bArgyrios Kyrtzidis  return 0;
12658a1d722f13df383600f36d77f842957c8adb5f1bArgyrios Kyrtzidis}
12668a1d722f13df383600f36d77f842957c8adb5f1bArgyrios Kyrtzidis
12678a1d722f13df383600f36d77f842957c8adb5f1bArgyrios Kyrtzidis/// \brief Set the implementation of ObjCInterfaceDecl.
12688a1d722f13df383600f36d77f842957c8adb5f1bArgyrios Kyrtzidisvoid ASTContext::setObjCImplementation(ObjCInterfaceDecl *IFaceD,
12698a1d722f13df383600f36d77f842957c8adb5f1bArgyrios Kyrtzidis                           ObjCImplementationDecl *ImplD) {
12708a1d722f13df383600f36d77f842957c8adb5f1bArgyrios Kyrtzidis  assert(IFaceD && ImplD && "Passed null params");
12718a1d722f13df383600f36d77f842957c8adb5f1bArgyrios Kyrtzidis  ObjCImpls[IFaceD] = ImplD;
12728a1d722f13df383600f36d77f842957c8adb5f1bArgyrios Kyrtzidis}
12738a1d722f13df383600f36d77f842957c8adb5f1bArgyrios Kyrtzidis/// \brief Set the implementation of ObjCCategoryDecl.
12748a1d722f13df383600f36d77f842957c8adb5f1bArgyrios Kyrtzidisvoid ASTContext::setObjCImplementation(ObjCCategoryDecl *CatD,
12758a1d722f13df383600f36d77f842957c8adb5f1bArgyrios Kyrtzidis                           ObjCCategoryImplDecl *ImplD) {
12768a1d722f13df383600f36d77f842957c8adb5f1bArgyrios Kyrtzidis  assert(CatD && ImplD && "Passed null params");
12778a1d722f13df383600f36d77f842957c8adb5f1bArgyrios Kyrtzidis  ObjCImpls[CatD] = ImplD;
12788a1d722f13df383600f36d77f842957c8adb5f1bArgyrios Kyrtzidis}
12798a1d722f13df383600f36d77f842957c8adb5f1bArgyrios Kyrtzidis
128087ec9c257c99b4136af6c7f5be5a2d486906ba84Argyrios KyrtzidisObjCInterfaceDecl *ASTContext::getObjContainingInterface(NamedDecl *ND) const {
128187ec9c257c99b4136af6c7f5be5a2d486906ba84Argyrios Kyrtzidis  if (ObjCInterfaceDecl *ID = dyn_cast<ObjCInterfaceDecl>(ND->getDeclContext()))
128287ec9c257c99b4136af6c7f5be5a2d486906ba84Argyrios Kyrtzidis    return ID;
128387ec9c257c99b4136af6c7f5be5a2d486906ba84Argyrios Kyrtzidis  if (ObjCCategoryDecl *CD = dyn_cast<ObjCCategoryDecl>(ND->getDeclContext()))
128487ec9c257c99b4136af6c7f5be5a2d486906ba84Argyrios Kyrtzidis    return CD->getClassInterface();
128587ec9c257c99b4136af6c7f5be5a2d486906ba84Argyrios Kyrtzidis  if (ObjCImplDecl *IMD = dyn_cast<ObjCImplDecl>(ND->getDeclContext()))
128687ec9c257c99b4136af6c7f5be5a2d486906ba84Argyrios Kyrtzidis    return IMD->getClassInterface();
128787ec9c257c99b4136af6c7f5be5a2d486906ba84Argyrios Kyrtzidis
128887ec9c257c99b4136af6c7f5be5a2d486906ba84Argyrios Kyrtzidis  return 0;
128987ec9c257c99b4136af6c7f5be5a2d486906ba84Argyrios Kyrtzidis}
129087ec9c257c99b4136af6c7f5be5a2d486906ba84Argyrios Kyrtzidis
12911ceee5c42d5c410217f67d384eecc6ea4a2bba9bFariborz Jahanian/// \brief Get the copy initialization expression of VarDecl,or NULL if
12921ceee5c42d5c410217f67d384eecc6ea4a2bba9bFariborz Jahanian/// none exists.
1293830937bc1100fba7682f7c32c40512085870f50cFariborz JahanianExpr *ASTContext::getBlockVarCopyInits(const VarDecl*VD) {
1294d016ec204c54d766c4561d43548551ffde0e6b43Fariborz Jahanian  assert(VD && "Passed null params");
1295d016ec204c54d766c4561d43548551ffde0e6b43Fariborz Jahanian  assert(VD->hasAttr<BlocksAttr>() &&
1296d016ec204c54d766c4561d43548551ffde0e6b43Fariborz Jahanian         "getBlockVarCopyInits - not __block var");
1297830937bc1100fba7682f7c32c40512085870f50cFariborz Jahanian  llvm::DenseMap<const VarDecl*, Expr*>::iterator
1298d016ec204c54d766c4561d43548551ffde0e6b43Fariborz Jahanian    I = BlockVarCopyInits.find(VD);
12991ceee5c42d5c410217f67d384eecc6ea4a2bba9bFariborz Jahanian  return (I != BlockVarCopyInits.end()) ? cast<Expr>(I->second) : 0;
13001ceee5c42d5c410217f67d384eecc6ea4a2bba9bFariborz Jahanian}
13011ceee5c42d5c410217f67d384eecc6ea4a2bba9bFariborz Jahanian
13021ceee5c42d5c410217f67d384eecc6ea4a2bba9bFariborz Jahanian/// \brief Set the copy inialization expression of a block var decl.
13031ceee5c42d5c410217f67d384eecc6ea4a2bba9bFariborz Jahanianvoid ASTContext::setBlockVarCopyInits(VarDecl*VD, Expr* Init) {
13041ceee5c42d5c410217f67d384eecc6ea4a2bba9bFariborz Jahanian  assert(VD && Init && "Passed null params");
1305d016ec204c54d766c4561d43548551ffde0e6b43Fariborz Jahanian  assert(VD->hasAttr<BlocksAttr>() &&
1306d016ec204c54d766c4561d43548551ffde0e6b43Fariborz Jahanian         "setBlockVarCopyInits - not __block var");
13071ceee5c42d5c410217f67d384eecc6ea4a2bba9bFariborz Jahanian  BlockVarCopyInits[VD] = Init;
13081ceee5c42d5c410217f67d384eecc6ea4a2bba9bFariborz Jahanian}
13091ceee5c42d5c410217f67d384eecc6ea4a2bba9bFariborz Jahanian
1310a93c934af4fbf97cbe8e649d82e68ccacfe57c95John McCall/// \brief Allocate an uninitialized TypeSourceInfo.
1311b17166c8077cd900cca83a895c43b30ea6660598Argyrios Kyrtzidis///
1312a93c934af4fbf97cbe8e649d82e68ccacfe57c95John McCall/// The caller should initialize the memory held by TypeSourceInfo using
1313b17166c8077cd900cca83a895c43b30ea6660598Argyrios Kyrtzidis/// the TypeLoc wrappers.
1314b17166c8077cd900cca83a895c43b30ea6660598Argyrios Kyrtzidis///
1315b17166c8077cd900cca83a895c43b30ea6660598Argyrios Kyrtzidis/// \param T the type that will be the basis for type source info. This type
1316b17166c8077cd900cca83a895c43b30ea6660598Argyrios Kyrtzidis/// should refer to how the declarator was written in source code, not to
1317b17166c8077cd900cca83a895c43b30ea6660598Argyrios Kyrtzidis/// what type semantic analysis resolved the declarator to.
1318a93c934af4fbf97cbe8e649d82e68ccacfe57c95John McCallTypeSourceInfo *ASTContext::CreateTypeSourceInfo(QualType T,
13194ba2a17694148e16eaa8d3917f657ffcd3667be4Jay Foad                                                 unsigned DataSize) const {
1320109de5ead1dfcb3bc985cddb8cb3ed5bcecad88dJohn McCall  if (!DataSize)
1321109de5ead1dfcb3bc985cddb8cb3ed5bcecad88dJohn McCall    DataSize = TypeLoc::getFullDataSizeForType(T);
1322109de5ead1dfcb3bc985cddb8cb3ed5bcecad88dJohn McCall  else
1323109de5ead1dfcb3bc985cddb8cb3ed5bcecad88dJohn McCall    assert(DataSize == TypeLoc::getFullDataSizeForType(T) &&
1324a93c934af4fbf97cbe8e649d82e68ccacfe57c95John McCall           "incorrect data size provided to CreateTypeSourceInfo!");
1325109de5ead1dfcb3bc985cddb8cb3ed5bcecad88dJohn McCall
1326a93c934af4fbf97cbe8e649d82e68ccacfe57c95John McCall  TypeSourceInfo *TInfo =
1327a93c934af4fbf97cbe8e649d82e68ccacfe57c95John McCall    (TypeSourceInfo*)BumpAlloc.Allocate(sizeof(TypeSourceInfo) + DataSize, 8);
1328a93c934af4fbf97cbe8e649d82e68ccacfe57c95John McCall  new (TInfo) TypeSourceInfo(T);
1329a93c934af4fbf97cbe8e649d82e68ccacfe57c95John McCall  return TInfo;
1330b17166c8077cd900cca83a895c43b30ea6660598Argyrios Kyrtzidis}
1331b17166c8077cd900cca83a895c43b30ea6660598Argyrios Kyrtzidis
1332a93c934af4fbf97cbe8e649d82e68ccacfe57c95John McCallTypeSourceInfo *ASTContext::getTrivialTypeSourceInfo(QualType T,
13336952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor                                                     SourceLocation L) const {
1334a93c934af4fbf97cbe8e649d82e68ccacfe57c95John McCall  TypeSourceInfo *DI = CreateTypeSourceInfo(T);
1335c21c7e9c2cded68f91be15be6847c9649242dc17Douglas Gregor  DI->getTypeLoc().initialize(const_cast<ASTContext &>(*this), L);
1336a4eb74d4dfe126c686dc708fec444c85ffb73b47John McCall  return DI;
1337a4eb74d4dfe126c686dc708fec444c85ffb73b47John McCall}
1338a4eb74d4dfe126c686dc708fec444c85ffb73b47John McCall
1339b2dbbb99e12806eaaf53b7ccabc32f42b5719443Daniel Dunbarconst ASTRecordLayout &
13404ba2a17694148e16eaa8d3917f657ffcd3667be4Jay FoadASTContext::getASTObjCInterfaceLayout(const ObjCInterfaceDecl *D) const {
1341b2dbbb99e12806eaaf53b7ccabc32f42b5719443Daniel Dunbar  return getObjCLayout(D, 0);
1342b2dbbb99e12806eaaf53b7ccabc32f42b5719443Daniel Dunbar}
1343b2dbbb99e12806eaaf53b7ccabc32f42b5719443Daniel Dunbar
1344b2dbbb99e12806eaaf53b7ccabc32f42b5719443Daniel Dunbarconst ASTRecordLayout &
13454ba2a17694148e16eaa8d3917f657ffcd3667be4Jay FoadASTContext::getASTObjCImplementationLayout(
13464ba2a17694148e16eaa8d3917f657ffcd3667be4Jay Foad                                        const ObjCImplementationDecl *D) const {
1347b2dbbb99e12806eaaf53b7ccabc32f42b5719443Daniel Dunbar  return getObjCLayout(D->getClassInterface(), D);
1348b2dbbb99e12806eaaf53b7ccabc32f42b5719443Daniel Dunbar}
1349b2dbbb99e12806eaaf53b7ccabc32f42b5719443Daniel Dunbar
1350a7674d8a9a69f3f6fe16e70cf2a3b2b15fb7c43dChris Lattner//===----------------------------------------------------------------------===//
1351a7674d8a9a69f3f6fe16e70cf2a3b2b15fb7c43dChris Lattner//                   Type creation/memoization methods
1352a7674d8a9a69f3f6fe16e70cf2a3b2b15fb7c43dChris Lattner//===----------------------------------------------------------------------===//
1353a7674d8a9a69f3f6fe16e70cf2a3b2b15fb7c43dChris Lattner
13544ba2a17694148e16eaa8d3917f657ffcd3667be4Jay FoadQualType
13553b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCallASTContext::getExtQualType(const Type *baseType, Qualifiers quals) const {
13563b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall  unsigned fastQuals = quals.getFastQualifiers();
13573b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall  quals.removeFastQualifiers();
13580953e767ff7817f97b3ab20896b229891eeff45bJohn McCall
13590953e767ff7817f97b3ab20896b229891eeff45bJohn McCall  // Check if we've already instantiated this type.
13600953e767ff7817f97b3ab20896b229891eeff45bJohn McCall  llvm::FoldingSetNodeID ID;
13613b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall  ExtQuals::Profile(ID, baseType, quals);
13623b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall  void *insertPos = 0;
13633b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall  if (ExtQuals *eq = ExtQualNodes.FindNodeOrInsertPos(ID, insertPos)) {
13643b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall    assert(eq->getQualifiers() == quals);
13653b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall    return QualType(eq, fastQuals);
13660953e767ff7817f97b3ab20896b229891eeff45bJohn McCall  }
13670953e767ff7817f97b3ab20896b229891eeff45bJohn McCall
13683b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall  // If the base type is not canonical, make the appropriate canonical type.
13693b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall  QualType canon;
13703b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall  if (!baseType->isCanonicalUnqualified()) {
13713b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall    SplitQualType canonSplit = baseType->getCanonicalTypeInternal().split();
13723b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall    canonSplit.second.addConsistentQualifiers(quals);
13733b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall    canon = getExtQualType(canonSplit.first, canonSplit.second);
13743b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall
13753b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall    // Re-find the insert position.
13763b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall    (void) ExtQualNodes.FindNodeOrInsertPos(ID, insertPos);
13773b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall  }
13783b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall
13793b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall  ExtQuals *eq = new (*this, TypeAlignment) ExtQuals(baseType, canon, quals);
13803b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall  ExtQualNodes.InsertNode(eq, insertPos);
13813b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall  return QualType(eq, fastQuals);
13820953e767ff7817f97b3ab20896b229891eeff45bJohn McCall}
13830953e767ff7817f97b3ab20896b229891eeff45bJohn McCall
13844ba2a17694148e16eaa8d3917f657ffcd3667be4Jay FoadQualType
13854ba2a17694148e16eaa8d3917f657ffcd3667be4Jay FoadASTContext::getAddrSpaceQualType(QualType T, unsigned AddressSpace) const {
1386f52ab250ff92bc51a9ac9a8e19bd43b63a5f844fChris Lattner  QualType CanT = getCanonicalType(T);
1387f52ab250ff92bc51a9ac9a8e19bd43b63a5f844fChris Lattner  if (CanT.getAddressSpace() == AddressSpace)
1388f46699ce225811d8d9dbab9d00189a0e54469457Chris Lattner    return T;
1389b7d2553edd2532d29b98b9e76bcf6a62bc48b417Chris Lattner
13900953e767ff7817f97b3ab20896b229891eeff45bJohn McCall  // If we are composing extended qualifiers together, merge together
13910953e767ff7817f97b3ab20896b229891eeff45bJohn McCall  // into one ExtQuals node.
13920953e767ff7817f97b3ab20896b229891eeff45bJohn McCall  QualifierCollector Quals;
13930953e767ff7817f97b3ab20896b229891eeff45bJohn McCall  const Type *TypeNode = Quals.strip(T);
13941eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
13950953e767ff7817f97b3ab20896b229891eeff45bJohn McCall  // If this type already has an address space specified, it cannot get
13960953e767ff7817f97b3ab20896b229891eeff45bJohn McCall  // another one.
13970953e767ff7817f97b3ab20896b229891eeff45bJohn McCall  assert(!Quals.hasAddressSpace() &&
13980953e767ff7817f97b3ab20896b229891eeff45bJohn McCall         "Type cannot be in multiple addr spaces!");
13990953e767ff7817f97b3ab20896b229891eeff45bJohn McCall  Quals.addAddressSpace(AddressSpace);
14001eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
14010953e767ff7817f97b3ab20896b229891eeff45bJohn McCall  return getExtQualType(TypeNode, Quals);
1402ebb97e98c03f8d7034bd3748a10e35f39a95c289Christopher Lamb}
1403ebb97e98c03f8d7034bd3748a10e35f39a95c289Christopher Lamb
1404b7d2553edd2532d29b98b9e76bcf6a62bc48b417Chris LattnerQualType ASTContext::getObjCGCQualType(QualType T,
14054ba2a17694148e16eaa8d3917f657ffcd3667be4Jay Foad                                       Qualifiers::GC GCAttr) const {
1406d33d9c0cc0cfdcd0b10f35a6acdfb25da4a64f19Fariborz Jahanian  QualType CanT = getCanonicalType(T);
1407b7d2553edd2532d29b98b9e76bcf6a62bc48b417Chris Lattner  if (CanT.getObjCGCAttr() == GCAttr)
1408d33d9c0cc0cfdcd0b10f35a6acdfb25da4a64f19Fariborz Jahanian    return T;
14091eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
14107f040a9d817cd1c72b565e92abff473510bf9e1dJohn McCall  if (const PointerType *ptr = T->getAs<PointerType>()) {
14117f040a9d817cd1c72b565e92abff473510bf9e1dJohn McCall    QualType Pointee = ptr->getPointeeType();
141258f9f2c884af6b72d036b746a016d8031d31cb7aSteve Naroff    if (Pointee->isAnyPointerType()) {
14134027cd1b924e29784a49085b1717f35cdd719146Fariborz Jahanian      QualType ResultType = getObjCGCQualType(Pointee, GCAttr);
14144027cd1b924e29784a49085b1717f35cdd719146Fariborz Jahanian      return getPointerType(ResultType);
14154027cd1b924e29784a49085b1717f35cdd719146Fariborz Jahanian    }
14164027cd1b924e29784a49085b1717f35cdd719146Fariborz Jahanian  }
14171eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
14180953e767ff7817f97b3ab20896b229891eeff45bJohn McCall  // If we are composing extended qualifiers together, merge together
14190953e767ff7817f97b3ab20896b229891eeff45bJohn McCall  // into one ExtQuals node.
14200953e767ff7817f97b3ab20896b229891eeff45bJohn McCall  QualifierCollector Quals;
14210953e767ff7817f97b3ab20896b229891eeff45bJohn McCall  const Type *TypeNode = Quals.strip(T);
14221eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
14230953e767ff7817f97b3ab20896b229891eeff45bJohn McCall  // If this type already has an ObjCGC specified, it cannot get
14240953e767ff7817f97b3ab20896b229891eeff45bJohn McCall  // another one.
14250953e767ff7817f97b3ab20896b229891eeff45bJohn McCall  assert(!Quals.hasObjCGCAttr() &&
14260953e767ff7817f97b3ab20896b229891eeff45bJohn McCall         "Type cannot have multiple ObjCGCs!");
14270953e767ff7817f97b3ab20896b229891eeff45bJohn McCall  Quals.addObjCGCAttr(GCAttr);
14281eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
14290953e767ff7817f97b3ab20896b229891eeff45bJohn McCall  return getExtQualType(TypeNode, Quals);
1430d33d9c0cc0cfdcd0b10f35a6acdfb25da4a64f19Fariborz Jahanian}
1431a7674d8a9a69f3f6fe16e70cf2a3b2b15fb7c43dChris Lattner
1432e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCallconst FunctionType *ASTContext::adjustFunctionType(const FunctionType *T,
1433e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall                                                   FunctionType::ExtInfo Info) {
1434e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall  if (T->getExtInfo() == Info)
1435e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall    return T;
1436e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall
1437e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall  QualType Result;
1438e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall  if (const FunctionNoProtoType *FNPT = dyn_cast<FunctionNoProtoType>(T)) {
1439e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall    Result = getFunctionNoProtoType(FNPT->getResultType(), Info);
1440e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall  } else {
1441e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall    const FunctionProtoType *FPT = cast<FunctionProtoType>(T);
1442e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall    FunctionProtoType::ExtProtoInfo EPI = FPT->getExtProtoInfo();
1443e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall    EPI.ExtInfo = Info;
1444e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall    Result = getFunctionType(FPT->getResultType(), FPT->arg_type_begin(),
1445e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall                             FPT->getNumArgs(), EPI);
1446e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall  }
1447e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall
1448e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall  return cast<FunctionType>(Result.getTypePtr());
1449e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall}
1450e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall
14515f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// getComplexType - Return the uniqued reference to the type for a complex
14525f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// number with the specified element type.
14534ba2a17694148e16eaa8d3917f657ffcd3667be4Jay FoadQualType ASTContext::getComplexType(QualType T) const {
14545f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // Unique pointers, to guarantee there is only one pointer of a particular
14555f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // structure.
14565f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  llvm::FoldingSetNodeID ID;
14575f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  ComplexType::Profile(ID, T);
14581eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
14595f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  void *InsertPos = 0;
14605f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  if (ComplexType *CT = ComplexTypes.FindNodeOrInsertPos(ID, InsertPos))
14615f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    return QualType(CT, 0);
14621eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
14635f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // If the pointee type isn't canonical, this won't be a canonical type either,
14645f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // so fill in the canonical type field.
14655f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  QualType Canonical;
1466467b27b9a24bdc823218ad1ad0e37673b6cc1e83John McCall  if (!T.isCanonical()) {
1467f52ab250ff92bc51a9ac9a8e19bd43b63a5f844fChris Lattner    Canonical = getComplexType(getCanonicalType(T));
14681eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
14695f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    // Get the new insert position for the node we care about.
14705f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    ComplexType *NewIP = ComplexTypes.FindNodeOrInsertPos(ID, InsertPos);
1471c6ed729f669044f5072a49d79041f455d971ece3Jeffrey Yasskin    assert(NewIP == 0 && "Shouldn't be in the map!"); (void)NewIP;
14725f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
14736b304a0254a13f42390b865ff5ba668a49cc58aeJohn McCall  ComplexType *New = new (*this, TypeAlignment) ComplexType(T, Canonical);
14745f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  Types.push_back(New);
14755f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  ComplexTypes.InsertNode(New, InsertPos);
14765f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  return QualType(New, 0);
14775f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
14785f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
14795f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// getPointerType - Return the uniqued reference to the type for a pointer to
14805f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// the specified type.
14814ba2a17694148e16eaa8d3917f657ffcd3667be4Jay FoadQualType ASTContext::getPointerType(QualType T) const {
14825f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // Unique pointers, to guarantee there is only one pointer of a particular
14835f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // structure.
14845f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  llvm::FoldingSetNodeID ID;
14855f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  PointerType::Profile(ID, T);
14861eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
14875f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  void *InsertPos = 0;
14885f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  if (PointerType *PT = PointerTypes.FindNodeOrInsertPos(ID, InsertPos))
14895f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    return QualType(PT, 0);
14901eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
14915f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // If the pointee type isn't canonical, this won't be a canonical type either,
14925f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // so fill in the canonical type field.
14935f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  QualType Canonical;
1494467b27b9a24bdc823218ad1ad0e37673b6cc1e83John McCall  if (!T.isCanonical()) {
1495f52ab250ff92bc51a9ac9a8e19bd43b63a5f844fChris Lattner    Canonical = getPointerType(getCanonicalType(T));
14961eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
14975f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    // Get the new insert position for the node we care about.
14985f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    PointerType *NewIP = PointerTypes.FindNodeOrInsertPos(ID, InsertPos);
1499c6ed729f669044f5072a49d79041f455d971ece3Jeffrey Yasskin    assert(NewIP == 0 && "Shouldn't be in the map!"); (void)NewIP;
15005f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
15016b304a0254a13f42390b865ff5ba668a49cc58aeJohn McCall  PointerType *New = new (*this, TypeAlignment) PointerType(T, Canonical);
15025f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  Types.push_back(New);
15035f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  PointerTypes.InsertNode(New, InsertPos);
15045f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  return QualType(New, 0);
15055f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
15065f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
15071eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// getBlockPointerType - Return the uniqued reference to the type for
15085618bd4a52c45fbbb605e3ba885663b2164db8a3Steve Naroff/// a pointer to the specified block.
15094ba2a17694148e16eaa8d3917f657ffcd3667be4Jay FoadQualType ASTContext::getBlockPointerType(QualType T) const {
1510296e8d5fdcf9946f51e866adc8d281379e51efe9Steve Naroff  assert(T->isFunctionType() && "block of function types only");
1511296e8d5fdcf9946f51e866adc8d281379e51efe9Steve Naroff  // Unique pointers, to guarantee there is only one block of a particular
15125618bd4a52c45fbbb605e3ba885663b2164db8a3Steve Naroff  // structure.
15135618bd4a52c45fbbb605e3ba885663b2164db8a3Steve Naroff  llvm::FoldingSetNodeID ID;
15145618bd4a52c45fbbb605e3ba885663b2164db8a3Steve Naroff  BlockPointerType::Profile(ID, T);
15151eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
15165618bd4a52c45fbbb605e3ba885663b2164db8a3Steve Naroff  void *InsertPos = 0;
15175618bd4a52c45fbbb605e3ba885663b2164db8a3Steve Naroff  if (BlockPointerType *PT =
15185618bd4a52c45fbbb605e3ba885663b2164db8a3Steve Naroff        BlockPointerTypes.FindNodeOrInsertPos(ID, InsertPos))
15195618bd4a52c45fbbb605e3ba885663b2164db8a3Steve Naroff    return QualType(PT, 0);
15201eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
15211eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  // If the block pointee type isn't canonical, this won't be a canonical
15225618bd4a52c45fbbb605e3ba885663b2164db8a3Steve Naroff  // type either so fill in the canonical type field.
15235618bd4a52c45fbbb605e3ba885663b2164db8a3Steve Naroff  QualType Canonical;
1524467b27b9a24bdc823218ad1ad0e37673b6cc1e83John McCall  if (!T.isCanonical()) {
15255618bd4a52c45fbbb605e3ba885663b2164db8a3Steve Naroff    Canonical = getBlockPointerType(getCanonicalType(T));
15261eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
15275618bd4a52c45fbbb605e3ba885663b2164db8a3Steve Naroff    // Get the new insert position for the node we care about.
15285618bd4a52c45fbbb605e3ba885663b2164db8a3Steve Naroff    BlockPointerType *NewIP =
15295618bd4a52c45fbbb605e3ba885663b2164db8a3Steve Naroff      BlockPointerTypes.FindNodeOrInsertPos(ID, InsertPos);
1530c6ed729f669044f5072a49d79041f455d971ece3Jeffrey Yasskin    assert(NewIP == 0 && "Shouldn't be in the map!"); (void)NewIP;
15315618bd4a52c45fbbb605e3ba885663b2164db8a3Steve Naroff  }
15326b304a0254a13f42390b865ff5ba668a49cc58aeJohn McCall  BlockPointerType *New
15336b304a0254a13f42390b865ff5ba668a49cc58aeJohn McCall    = new (*this, TypeAlignment) BlockPointerType(T, Canonical);
15345618bd4a52c45fbbb605e3ba885663b2164db8a3Steve Naroff  Types.push_back(New);
15355618bd4a52c45fbbb605e3ba885663b2164db8a3Steve Naroff  BlockPointerTypes.InsertNode(New, InsertPos);
15365618bd4a52c45fbbb605e3ba885663b2164db8a3Steve Naroff  return QualType(New, 0);
15375618bd4a52c45fbbb605e3ba885663b2164db8a3Steve Naroff}
15385618bd4a52c45fbbb605e3ba885663b2164db8a3Steve Naroff
15397c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl/// getLValueReferenceType - Return the uniqued reference to the type for an
15407c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl/// lvalue reference to the specified type.
15414ba2a17694148e16eaa8d3917f657ffcd3667be4Jay FoadQualType
15424ba2a17694148e16eaa8d3917f657ffcd3667be4Jay FoadASTContext::getLValueReferenceType(QualType T, bool SpelledAsLValue) const {
15439625e44c0252485277a340746ed8ac950686156fDouglas Gregor  assert(getCanonicalType(T) != OverloadTy &&
15449625e44c0252485277a340746ed8ac950686156fDouglas Gregor         "Unresolved overloaded function type");
15459625e44c0252485277a340746ed8ac950686156fDouglas Gregor
15465f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // Unique pointers, to guarantee there is only one pointer of a particular
15475f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // structure.
15485f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  llvm::FoldingSetNodeID ID;
154954e14c4db764c0636160d26c5bbf491637c83a76John McCall  ReferenceType::Profile(ID, T, SpelledAsLValue);
15505f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
15515f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  void *InsertPos = 0;
15527c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl  if (LValueReferenceType *RT =
15537c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl        LValueReferenceTypes.FindNodeOrInsertPos(ID, InsertPos))
15545f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    return QualType(RT, 0);
15557c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl
155654e14c4db764c0636160d26c5bbf491637c83a76John McCall  const ReferenceType *InnerRef = T->getAs<ReferenceType>();
155754e14c4db764c0636160d26c5bbf491637c83a76John McCall
15585f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // If the referencee type isn't canonical, this won't be a canonical type
15595f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // either, so fill in the canonical type field.
15605f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  QualType Canonical;
156154e14c4db764c0636160d26c5bbf491637c83a76John McCall  if (!SpelledAsLValue || InnerRef || !T.isCanonical()) {
156254e14c4db764c0636160d26c5bbf491637c83a76John McCall    QualType PointeeType = (InnerRef ? InnerRef->getPointeeType() : T);
156354e14c4db764c0636160d26c5bbf491637c83a76John McCall    Canonical = getLValueReferenceType(getCanonicalType(PointeeType));
15647c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl
15657c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl    // Get the new insert position for the node we care about.
15667c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl    LValueReferenceType *NewIP =
15677c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl      LValueReferenceTypes.FindNodeOrInsertPos(ID, InsertPos);
1568c6ed729f669044f5072a49d79041f455d971ece3Jeffrey Yasskin    assert(NewIP == 0 && "Shouldn't be in the map!"); (void)NewIP;
15697c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl  }
15707c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl
15716b304a0254a13f42390b865ff5ba668a49cc58aeJohn McCall  LValueReferenceType *New
157254e14c4db764c0636160d26c5bbf491637c83a76John McCall    = new (*this, TypeAlignment) LValueReferenceType(T, Canonical,
157354e14c4db764c0636160d26c5bbf491637c83a76John McCall                                                     SpelledAsLValue);
15747c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl  Types.push_back(New);
15757c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl  LValueReferenceTypes.InsertNode(New, InsertPos);
157654e14c4db764c0636160d26c5bbf491637c83a76John McCall
15777c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl  return QualType(New, 0);
15787c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl}
15797c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl
15807c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl/// getRValueReferenceType - Return the uniqued reference to the type for an
15817c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl/// rvalue reference to the specified type.
15824ba2a17694148e16eaa8d3917f657ffcd3667be4Jay FoadQualType ASTContext::getRValueReferenceType(QualType T) const {
15837c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl  // Unique pointers, to guarantee there is only one pointer of a particular
15847c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl  // structure.
15857c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl  llvm::FoldingSetNodeID ID;
158654e14c4db764c0636160d26c5bbf491637c83a76John McCall  ReferenceType::Profile(ID, T, false);
15877c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl
15887c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl  void *InsertPos = 0;
15897c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl  if (RValueReferenceType *RT =
15907c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl        RValueReferenceTypes.FindNodeOrInsertPos(ID, InsertPos))
15917c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl    return QualType(RT, 0);
15927c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl
159354e14c4db764c0636160d26c5bbf491637c83a76John McCall  const ReferenceType *InnerRef = T->getAs<ReferenceType>();
159454e14c4db764c0636160d26c5bbf491637c83a76John McCall
15957c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl  // If the referencee type isn't canonical, this won't be a canonical type
15967c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl  // either, so fill in the canonical type field.
15977c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl  QualType Canonical;
159854e14c4db764c0636160d26c5bbf491637c83a76John McCall  if (InnerRef || !T.isCanonical()) {
159954e14c4db764c0636160d26c5bbf491637c83a76John McCall    QualType PointeeType = (InnerRef ? InnerRef->getPointeeType() : T);
160054e14c4db764c0636160d26c5bbf491637c83a76John McCall    Canonical = getRValueReferenceType(getCanonicalType(PointeeType));
16017c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl
16025f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    // Get the new insert position for the node we care about.
16037c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl    RValueReferenceType *NewIP =
16047c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl      RValueReferenceTypes.FindNodeOrInsertPos(ID, InsertPos);
1605c6ed729f669044f5072a49d79041f455d971ece3Jeffrey Yasskin    assert(NewIP == 0 && "Shouldn't be in the map!"); (void)NewIP;
16065f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
16075f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
16086b304a0254a13f42390b865ff5ba668a49cc58aeJohn McCall  RValueReferenceType *New
16096b304a0254a13f42390b865ff5ba668a49cc58aeJohn McCall    = new (*this, TypeAlignment) RValueReferenceType(T, Canonical);
16105f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  Types.push_back(New);
16117c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl  RValueReferenceTypes.InsertNode(New, InsertPos);
16125f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  return QualType(New, 0);
16135f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
16145f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
1615f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl/// getMemberPointerType - Return the uniqued reference to the type for a
1616f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl/// member pointer to the specified type, in the specified class.
16174ba2a17694148e16eaa8d3917f657ffcd3667be4Jay FoadQualType ASTContext::getMemberPointerType(QualType T, const Type *Cls) const {
1618f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl  // Unique pointers, to guarantee there is only one pointer of a particular
1619f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl  // structure.
1620f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl  llvm::FoldingSetNodeID ID;
1621f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl  MemberPointerType::Profile(ID, T, Cls);
1622f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl
1623f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl  void *InsertPos = 0;
1624f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl  if (MemberPointerType *PT =
1625f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl      MemberPointerTypes.FindNodeOrInsertPos(ID, InsertPos))
1626f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl    return QualType(PT, 0);
1627f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl
1628f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl  // If the pointee or class type isn't canonical, this won't be a canonical
1629f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl  // type either, so fill in the canonical type field.
1630f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl  QualType Canonical;
163187c12c4a4667279dacb3d4a93c64b49148a0ff79Douglas Gregor  if (!T.isCanonical() || !Cls->isCanonicalUnqualified()) {
1632f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl    Canonical = getMemberPointerType(getCanonicalType(T),getCanonicalType(Cls));
1633f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl
1634f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl    // Get the new insert position for the node we care about.
1635f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl    MemberPointerType *NewIP =
1636f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl      MemberPointerTypes.FindNodeOrInsertPos(ID, InsertPos);
1637c6ed729f669044f5072a49d79041f455d971ece3Jeffrey Yasskin    assert(NewIP == 0 && "Shouldn't be in the map!"); (void)NewIP;
1638f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl  }
16396b304a0254a13f42390b865ff5ba668a49cc58aeJohn McCall  MemberPointerType *New
16406b304a0254a13f42390b865ff5ba668a49cc58aeJohn McCall    = new (*this, TypeAlignment) MemberPointerType(T, Cls, Canonical);
1641f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl  Types.push_back(New);
1642f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl  MemberPointerTypes.InsertNode(New, InsertPos);
1643f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl  return QualType(New, 0);
1644f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl}
1645f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl
16461eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// getConstantArrayType - Return the unique reference to the type for an
1647fb22d96692c5240fb8d611290dbf7eeed3759c73Steve Naroff/// array of the specified element type.
16481eb4433ac451dc16f4133a88af2d002ac26c58efMike StumpQualType ASTContext::getConstantArrayType(QualType EltTy,
164938aeec7299c48cb79523f7f89776fb258c84aeeaChris Lattner                                          const llvm::APInt &ArySizeIn,
1650c9406125e2cac9208098655ac8058c095c2c3a65Steve Naroff                                          ArrayType::ArraySizeModifier ASM,
165163e7d25d2e6036616b42f744fd4a39cd5f911960Abramo Bagnara                                          unsigned IndexTypeQuals) const {
1652923d56d436f750bc1f29db50e641078725558a1bSebastian Redl  assert((EltTy->isDependentType() ||
1653923d56d436f750bc1f29db50e641078725558a1bSebastian Redl          EltTy->isIncompleteType() || EltTy->isConstantSizeType()) &&
1654587cbdfd95f4b0aaccc14b31f5debe85d5daf7edEli Friedman         "Constant array of VLAs is illegal!");
1655587cbdfd95f4b0aaccc14b31f5debe85d5daf7edEli Friedman
165638aeec7299c48cb79523f7f89776fb258c84aeeaChris Lattner  // Convert the array size into a canonical width matching the pointer size for
165738aeec7299c48cb79523f7f89776fb258c84aeeaChris Lattner  // the target.
165838aeec7299c48cb79523f7f89776fb258c84aeeaChris Lattner  llvm::APInt ArySize(ArySizeIn);
16599f71a8f4c7a182a5236da9e747d57cc1d1bd24c2Jay Foad  ArySize =
1660bcfd1f55bfbb3e5944cd5e03d07b343e280838c4Douglas Gregor    ArySize.zextOrTrunc(Target->getPointerWidth(getTargetAddressSpace(EltTy)));
16611eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
16625f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  llvm::FoldingSetNodeID ID;
166363e7d25d2e6036616b42f744fd4a39cd5f911960Abramo Bagnara  ConstantArrayType::Profile(ID, EltTy, ArySize, ASM, IndexTypeQuals);
16641eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
16655f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  void *InsertPos = 0;
16661eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  if (ConstantArrayType *ATP =
16677192f8e9592729882a09d84d77838db26e39ebd4Ted Kremenek      ConstantArrayTypes.FindNodeOrInsertPos(ID, InsertPos))
16685f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    return QualType(ATP, 0);
16691eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
16703b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall  // If the element type isn't canonical or has qualifiers, this won't
16713b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall  // be a canonical type either, so fill in the canonical type field.
16723b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall  QualType Canon;
16733b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall  if (!EltTy.isCanonical() || EltTy.hasLocalQualifiers()) {
16743b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall    SplitQualType canonSplit = getCanonicalType(EltTy).split();
16753b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall    Canon = getConstantArrayType(QualType(canonSplit.first, 0), ArySize,
167663e7d25d2e6036616b42f744fd4a39cd5f911960Abramo Bagnara                                 ASM, IndexTypeQuals);
16773b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall    Canon = getQualifiedType(Canon, canonSplit.second);
16783b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall
16795f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    // Get the new insert position for the node we care about.
16801eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    ConstantArrayType *NewIP =
16817192f8e9592729882a09d84d77838db26e39ebd4Ted Kremenek      ConstantArrayTypes.FindNodeOrInsertPos(ID, InsertPos);
1682c6ed729f669044f5072a49d79041f455d971ece3Jeffrey Yasskin    assert(NewIP == 0 && "Shouldn't be in the map!"); (void)NewIP;
16835f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
16841eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
16856b304a0254a13f42390b865ff5ba668a49cc58aeJohn McCall  ConstantArrayType *New = new(*this,TypeAlignment)
168663e7d25d2e6036616b42f744fd4a39cd5f911960Abramo Bagnara    ConstantArrayType(EltTy, Canon, ArySize, ASM, IndexTypeQuals);
16877192f8e9592729882a09d84d77838db26e39ebd4Ted Kremenek  ConstantArrayTypes.InsertNode(New, InsertPos);
16885f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  Types.push_back(New);
16895f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  return QualType(New, 0);
16905f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
16915f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
1692ce8890371fcdb983ae487c87fa40606a34896ff7John McCall/// getVariableArrayDecayedType - Turns the given type, which may be
1693ce8890371fcdb983ae487c87fa40606a34896ff7John McCall/// variably-modified, into the corresponding type with all the known
1694ce8890371fcdb983ae487c87fa40606a34896ff7John McCall/// sizes replaced with [*].
1695ce8890371fcdb983ae487c87fa40606a34896ff7John McCallQualType ASTContext::getVariableArrayDecayedType(QualType type) const {
1696ce8890371fcdb983ae487c87fa40606a34896ff7John McCall  // Vastly most common case.
1697ce8890371fcdb983ae487c87fa40606a34896ff7John McCall  if (!type->isVariablyModifiedType()) return type;
1698ce8890371fcdb983ae487c87fa40606a34896ff7John McCall
1699ce8890371fcdb983ae487c87fa40606a34896ff7John McCall  QualType result;
1700ce8890371fcdb983ae487c87fa40606a34896ff7John McCall
1701ce8890371fcdb983ae487c87fa40606a34896ff7John McCall  SplitQualType split = type.getSplitDesugaredType();
1702ce8890371fcdb983ae487c87fa40606a34896ff7John McCall  const Type *ty = split.first;
1703ce8890371fcdb983ae487c87fa40606a34896ff7John McCall  switch (ty->getTypeClass()) {
1704ce8890371fcdb983ae487c87fa40606a34896ff7John McCall#define TYPE(Class, Base)
1705ce8890371fcdb983ae487c87fa40606a34896ff7John McCall#define ABSTRACT_TYPE(Class, Base)
1706ce8890371fcdb983ae487c87fa40606a34896ff7John McCall#define NON_CANONICAL_TYPE(Class, Base) case Type::Class:
1707ce8890371fcdb983ae487c87fa40606a34896ff7John McCall#include "clang/AST/TypeNodes.def"
1708ce8890371fcdb983ae487c87fa40606a34896ff7John McCall    llvm_unreachable("didn't desugar past all non-canonical types?");
1709ce8890371fcdb983ae487c87fa40606a34896ff7John McCall
1710ce8890371fcdb983ae487c87fa40606a34896ff7John McCall  // These types should never be variably-modified.
1711ce8890371fcdb983ae487c87fa40606a34896ff7John McCall  case Type::Builtin:
1712ce8890371fcdb983ae487c87fa40606a34896ff7John McCall  case Type::Complex:
1713ce8890371fcdb983ae487c87fa40606a34896ff7John McCall  case Type::Vector:
1714ce8890371fcdb983ae487c87fa40606a34896ff7John McCall  case Type::ExtVector:
1715ce8890371fcdb983ae487c87fa40606a34896ff7John McCall  case Type::DependentSizedExtVector:
1716ce8890371fcdb983ae487c87fa40606a34896ff7John McCall  case Type::ObjCObject:
1717ce8890371fcdb983ae487c87fa40606a34896ff7John McCall  case Type::ObjCInterface:
1718ce8890371fcdb983ae487c87fa40606a34896ff7John McCall  case Type::ObjCObjectPointer:
1719ce8890371fcdb983ae487c87fa40606a34896ff7John McCall  case Type::Record:
1720ce8890371fcdb983ae487c87fa40606a34896ff7John McCall  case Type::Enum:
1721ce8890371fcdb983ae487c87fa40606a34896ff7John McCall  case Type::UnresolvedUsing:
1722ce8890371fcdb983ae487c87fa40606a34896ff7John McCall  case Type::TypeOfExpr:
1723ce8890371fcdb983ae487c87fa40606a34896ff7John McCall  case Type::TypeOf:
1724ce8890371fcdb983ae487c87fa40606a34896ff7John McCall  case Type::Decltype:
1725ca63c200346c0ca9e00194ec6e34a5a7b0ed9321Sean Hunt  case Type::UnaryTransform:
1726ce8890371fcdb983ae487c87fa40606a34896ff7John McCall  case Type::DependentName:
1727ce8890371fcdb983ae487c87fa40606a34896ff7John McCall  case Type::InjectedClassName:
1728ce8890371fcdb983ae487c87fa40606a34896ff7John McCall  case Type::TemplateSpecialization:
1729ce8890371fcdb983ae487c87fa40606a34896ff7John McCall  case Type::DependentTemplateSpecialization:
1730ce8890371fcdb983ae487c87fa40606a34896ff7John McCall  case Type::TemplateTypeParm:
1731ce8890371fcdb983ae487c87fa40606a34896ff7John McCall  case Type::SubstTemplateTypeParmPack:
173234b41d939a1328f484511c6002ba2456db879a29Richard Smith  case Type::Auto:
1733ce8890371fcdb983ae487c87fa40606a34896ff7John McCall  case Type::PackExpansion:
1734ce8890371fcdb983ae487c87fa40606a34896ff7John McCall    llvm_unreachable("type should never be variably-modified");
1735ce8890371fcdb983ae487c87fa40606a34896ff7John McCall
1736ce8890371fcdb983ae487c87fa40606a34896ff7John McCall  // These types can be variably-modified but should never need to
1737ce8890371fcdb983ae487c87fa40606a34896ff7John McCall  // further decay.
1738ce8890371fcdb983ae487c87fa40606a34896ff7John McCall  case Type::FunctionNoProto:
1739ce8890371fcdb983ae487c87fa40606a34896ff7John McCall  case Type::FunctionProto:
1740ce8890371fcdb983ae487c87fa40606a34896ff7John McCall  case Type::BlockPointer:
1741ce8890371fcdb983ae487c87fa40606a34896ff7John McCall  case Type::MemberPointer:
1742ce8890371fcdb983ae487c87fa40606a34896ff7John McCall    return type;
1743ce8890371fcdb983ae487c87fa40606a34896ff7John McCall
1744ce8890371fcdb983ae487c87fa40606a34896ff7John McCall  // These types can be variably-modified.  All these modifications
1745ce8890371fcdb983ae487c87fa40606a34896ff7John McCall  // preserve structure except as noted by comments.
1746ce8890371fcdb983ae487c87fa40606a34896ff7John McCall  // TODO: if we ever care about optimizing VLAs, there are no-op
1747ce8890371fcdb983ae487c87fa40606a34896ff7John McCall  // optimizations available here.
1748ce8890371fcdb983ae487c87fa40606a34896ff7John McCall  case Type::Pointer:
1749ce8890371fcdb983ae487c87fa40606a34896ff7John McCall    result = getPointerType(getVariableArrayDecayedType(
1750ce8890371fcdb983ae487c87fa40606a34896ff7John McCall                              cast<PointerType>(ty)->getPointeeType()));
1751ce8890371fcdb983ae487c87fa40606a34896ff7John McCall    break;
1752ce8890371fcdb983ae487c87fa40606a34896ff7John McCall
1753ce8890371fcdb983ae487c87fa40606a34896ff7John McCall  case Type::LValueReference: {
1754ce8890371fcdb983ae487c87fa40606a34896ff7John McCall    const LValueReferenceType *lv = cast<LValueReferenceType>(ty);
1755ce8890371fcdb983ae487c87fa40606a34896ff7John McCall    result = getLValueReferenceType(
1756ce8890371fcdb983ae487c87fa40606a34896ff7John McCall                 getVariableArrayDecayedType(lv->getPointeeType()),
1757ce8890371fcdb983ae487c87fa40606a34896ff7John McCall                                    lv->isSpelledAsLValue());
1758ce8890371fcdb983ae487c87fa40606a34896ff7John McCall    break;
1759745da3a5bb4ea35f93f50301e7fbbb7d78d3b6bbFariborz Jahanian  }
1760ce8890371fcdb983ae487c87fa40606a34896ff7John McCall
1761ce8890371fcdb983ae487c87fa40606a34896ff7John McCall  case Type::RValueReference: {
1762ce8890371fcdb983ae487c87fa40606a34896ff7John McCall    const RValueReferenceType *lv = cast<RValueReferenceType>(ty);
1763ce8890371fcdb983ae487c87fa40606a34896ff7John McCall    result = getRValueReferenceType(
1764ce8890371fcdb983ae487c87fa40606a34896ff7John McCall                 getVariableArrayDecayedType(lv->getPointeeType()));
1765ce8890371fcdb983ae487c87fa40606a34896ff7John McCall    break;
1766745da3a5bb4ea35f93f50301e7fbbb7d78d3b6bbFariborz Jahanian  }
1767745da3a5bb4ea35f93f50301e7fbbb7d78d3b6bbFariborz Jahanian
1768b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman  case Type::Atomic: {
1769b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman    const AtomicType *at = cast<AtomicType>(ty);
1770b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman    result = getAtomicType(getVariableArrayDecayedType(at->getValueType()));
1771b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman    break;
1772b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman  }
1773b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman
1774ce8890371fcdb983ae487c87fa40606a34896ff7John McCall  case Type::ConstantArray: {
1775ce8890371fcdb983ae487c87fa40606a34896ff7John McCall    const ConstantArrayType *cat = cast<ConstantArrayType>(ty);
1776ce8890371fcdb983ae487c87fa40606a34896ff7John McCall    result = getConstantArrayType(
1777ce8890371fcdb983ae487c87fa40606a34896ff7John McCall                 getVariableArrayDecayedType(cat->getElementType()),
1778ce8890371fcdb983ae487c87fa40606a34896ff7John McCall                                  cat->getSize(),
1779ce8890371fcdb983ae487c87fa40606a34896ff7John McCall                                  cat->getSizeModifier(),
1780ce8890371fcdb983ae487c87fa40606a34896ff7John McCall                                  cat->getIndexTypeCVRQualifiers());
1781ce8890371fcdb983ae487c87fa40606a34896ff7John McCall    break;
1782745da3a5bb4ea35f93f50301e7fbbb7d78d3b6bbFariborz Jahanian  }
1783745da3a5bb4ea35f93f50301e7fbbb7d78d3b6bbFariborz Jahanian
1784ce8890371fcdb983ae487c87fa40606a34896ff7John McCall  case Type::DependentSizedArray: {
1785ce8890371fcdb983ae487c87fa40606a34896ff7John McCall    const DependentSizedArrayType *dat = cast<DependentSizedArrayType>(ty);
1786ce8890371fcdb983ae487c87fa40606a34896ff7John McCall    result = getDependentSizedArrayType(
1787ce8890371fcdb983ae487c87fa40606a34896ff7John McCall                 getVariableArrayDecayedType(dat->getElementType()),
1788ce8890371fcdb983ae487c87fa40606a34896ff7John McCall                                        dat->getSizeExpr(),
1789ce8890371fcdb983ae487c87fa40606a34896ff7John McCall                                        dat->getSizeModifier(),
1790ce8890371fcdb983ae487c87fa40606a34896ff7John McCall                                        dat->getIndexTypeCVRQualifiers(),
1791ce8890371fcdb983ae487c87fa40606a34896ff7John McCall                                        dat->getBracketsRange());
1792ce8890371fcdb983ae487c87fa40606a34896ff7John McCall    break;
1793ce8890371fcdb983ae487c87fa40606a34896ff7John McCall  }
1794ce8890371fcdb983ae487c87fa40606a34896ff7John McCall
1795ce8890371fcdb983ae487c87fa40606a34896ff7John McCall  // Turn incomplete types into [*] types.
1796ce8890371fcdb983ae487c87fa40606a34896ff7John McCall  case Type::IncompleteArray: {
1797ce8890371fcdb983ae487c87fa40606a34896ff7John McCall    const IncompleteArrayType *iat = cast<IncompleteArrayType>(ty);
1798ce8890371fcdb983ae487c87fa40606a34896ff7John McCall    result = getVariableArrayType(
1799ce8890371fcdb983ae487c87fa40606a34896ff7John McCall                 getVariableArrayDecayedType(iat->getElementType()),
1800ce8890371fcdb983ae487c87fa40606a34896ff7John McCall                                  /*size*/ 0,
1801ce8890371fcdb983ae487c87fa40606a34896ff7John McCall                                  ArrayType::Normal,
1802ce8890371fcdb983ae487c87fa40606a34896ff7John McCall                                  iat->getIndexTypeCVRQualifiers(),
1803ce8890371fcdb983ae487c87fa40606a34896ff7John McCall                                  SourceRange());
1804ce8890371fcdb983ae487c87fa40606a34896ff7John McCall    break;
1805ce8890371fcdb983ae487c87fa40606a34896ff7John McCall  }
1806ce8890371fcdb983ae487c87fa40606a34896ff7John McCall
1807ce8890371fcdb983ae487c87fa40606a34896ff7John McCall  // Turn VLA types into [*] types.
1808ce8890371fcdb983ae487c87fa40606a34896ff7John McCall  case Type::VariableArray: {
1809ce8890371fcdb983ae487c87fa40606a34896ff7John McCall    const VariableArrayType *vat = cast<VariableArrayType>(ty);
1810ce8890371fcdb983ae487c87fa40606a34896ff7John McCall    result = getVariableArrayType(
1811ce8890371fcdb983ae487c87fa40606a34896ff7John McCall                 getVariableArrayDecayedType(vat->getElementType()),
1812ce8890371fcdb983ae487c87fa40606a34896ff7John McCall                                  /*size*/ 0,
1813ce8890371fcdb983ae487c87fa40606a34896ff7John McCall                                  ArrayType::Star,
1814ce8890371fcdb983ae487c87fa40606a34896ff7John McCall                                  vat->getIndexTypeCVRQualifiers(),
1815ce8890371fcdb983ae487c87fa40606a34896ff7John McCall                                  vat->getBracketsRange());
1816ce8890371fcdb983ae487c87fa40606a34896ff7John McCall    break;
1817ce8890371fcdb983ae487c87fa40606a34896ff7John McCall  }
1818ce8890371fcdb983ae487c87fa40606a34896ff7John McCall  }
1819ce8890371fcdb983ae487c87fa40606a34896ff7John McCall
1820ce8890371fcdb983ae487c87fa40606a34896ff7John McCall  // Apply the top-level qualifiers from the original.
1821ce8890371fcdb983ae487c87fa40606a34896ff7John McCall  return getQualifiedType(result, split.second);
1822ce8890371fcdb983ae487c87fa40606a34896ff7John McCall}
1823745da3a5bb4ea35f93f50301e7fbbb7d78d3b6bbFariborz Jahanian
1824bdbf7b030a3e0ddb95240076683830e6f78c79a5Steve Naroff/// getVariableArrayType - Returns a non-unique reference to the type for a
1825bdbf7b030a3e0ddb95240076683830e6f78c79a5Steve Naroff/// variable array of the specified element type.
18267e7eb3da052a6d80ddf2377cab0384c798f73f75Douglas GregorQualType ASTContext::getVariableArrayType(QualType EltTy,
18277e7eb3da052a6d80ddf2377cab0384c798f73f75Douglas Gregor                                          Expr *NumElts,
1828c9406125e2cac9208098655ac8058c095c2c3a65Steve Naroff                                          ArrayType::ArraySizeModifier ASM,
182963e7d25d2e6036616b42f744fd4a39cd5f911960Abramo Bagnara                                          unsigned IndexTypeQuals,
18304ba2a17694148e16eaa8d3917f657ffcd3667be4Jay Foad                                          SourceRange Brackets) const {
1831c5773c4b8ce1ed6ed5c7112c9020c954a47dce96Eli Friedman  // Since we don't unique expressions, it isn't possible to unique VLA's
1832c5773c4b8ce1ed6ed5c7112c9020c954a47dce96Eli Friedman  // that have an expression provided for their size.
18333b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall  QualType Canon;
1834715e9c8a39437347e838aa108df443fe1086d359Douglas Gregor
18353b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall  // Be sure to pull qualifiers off the element type.
18363b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall  if (!EltTy.isCanonical() || EltTy.hasLocalQualifiers()) {
18373b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall    SplitQualType canonSplit = getCanonicalType(EltTy).split();
18383b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall    Canon = getVariableArrayType(QualType(canonSplit.first, 0), NumElts, ASM,
183963e7d25d2e6036616b42f744fd4a39cd5f911960Abramo Bagnara                                 IndexTypeQuals, Brackets);
18403b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall    Canon = getQualifiedType(Canon, canonSplit.second);
1841715e9c8a39437347e838aa108df443fe1086d359Douglas Gregor  }
1842715e9c8a39437347e838aa108df443fe1086d359Douglas Gregor
18436b304a0254a13f42390b865ff5ba668a49cc58aeJohn McCall  VariableArrayType *New = new(*this, TypeAlignment)
184463e7d25d2e6036616b42f744fd4a39cd5f911960Abramo Bagnara    VariableArrayType(EltTy, Canon, NumElts, ASM, IndexTypeQuals, Brackets);
1845c5773c4b8ce1ed6ed5c7112c9020c954a47dce96Eli Friedman
1846c5773c4b8ce1ed6ed5c7112c9020c954a47dce96Eli Friedman  VariableArrayTypes.push_back(New);
1847c5773c4b8ce1ed6ed5c7112c9020c954a47dce96Eli Friedman  Types.push_back(New);
1848c5773c4b8ce1ed6ed5c7112c9020c954a47dce96Eli Friedman  return QualType(New, 0);
1849c5773c4b8ce1ed6ed5c7112c9020c954a47dce96Eli Friedman}
1850c5773c4b8ce1ed6ed5c7112c9020c954a47dce96Eli Friedman
1851898574e7496ba8fd76290079d3a9d06954992734Douglas Gregor/// getDependentSizedArrayType - Returns a non-unique reference to
1852898574e7496ba8fd76290079d3a9d06954992734Douglas Gregor/// the type for a dependently-sized array of the specified element
185304d4beee4b86af20a9e4457023d3925cab8f9908Douglas Gregor/// type.
18543b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCallQualType ASTContext::getDependentSizedArrayType(QualType elementType,
18553b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall                                                Expr *numElements,
1856898574e7496ba8fd76290079d3a9d06954992734Douglas Gregor                                                ArrayType::ArraySizeModifier ASM,
18573b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall                                                unsigned elementTypeQuals,
18583b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall                                                SourceRange brackets) const {
18593b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall  assert((!numElements || numElements->isTypeDependent() ||
18603b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall          numElements->isValueDependent()) &&
1861898574e7496ba8fd76290079d3a9d06954992734Douglas Gregor         "Size must be type- or value-dependent!");
1862898574e7496ba8fd76290079d3a9d06954992734Douglas Gregor
18633b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall  // Dependently-sized array types that do not have a specified number
18643b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall  // of elements will have their sizes deduced from a dependent
18653b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall  // initializer.  We do no canonicalization here at all, which is okay
18663b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall  // because they can't be used in most locations.
18673b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall  if (!numElements) {
18683b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall    DependentSizedArrayType *newType
18693b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall      = new (*this, TypeAlignment)
18703b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall          DependentSizedArrayType(*this, elementType, QualType(),
18713b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall                                  numElements, ASM, elementTypeQuals,
18723b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall                                  brackets);
18733b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall    Types.push_back(newType);
18743b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall    return QualType(newType, 0);
1875cb78d8852a454684c987220132cdb5e54dd00121Douglas Gregor  }
1876cb78d8852a454684c987220132cdb5e54dd00121Douglas Gregor
18773b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall  // Otherwise, we actually build a new type every time, but we
18783b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall  // also build a canonical type.
18791eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
18803b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall  SplitQualType canonElementType = getCanonicalType(elementType).split();
1881898574e7496ba8fd76290079d3a9d06954992734Douglas Gregor
18823b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall  void *insertPos = 0;
18833b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall  llvm::FoldingSetNodeID ID;
18843b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall  DependentSizedArrayType::Profile(ID, *this,
18853b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall                                   QualType(canonElementType.first, 0),
18863b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall                                   ASM, elementTypeQuals, numElements);
18873b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall
18883b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall  // Look for an existing type with these properties.
18893b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall  DependentSizedArrayType *canonTy =
18903b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall    DependentSizedArrayTypes.FindNodeOrInsertPos(ID, insertPos);
18913b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall
18923b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall  // If we don't have one, build one.
18933b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall  if (!canonTy) {
18943b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall    canonTy = new (*this, TypeAlignment)
18953b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall      DependentSizedArrayType(*this, QualType(canonElementType.first, 0),
18963b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall                              QualType(), numElements, ASM, elementTypeQuals,
18973b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall                              brackets);
18983b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall    DependentSizedArrayTypes.InsertNode(canonTy, insertPos);
18993b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall    Types.push_back(canonTy);
19003b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall  }
19013b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall
19023b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall  // Apply qualifiers from the element type to the array.
19033b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall  QualType canon = getQualifiedType(QualType(canonTy,0),
19043b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall                                    canonElementType.second);
19053b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall
19063b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall  // If we didn't need extra canonicalization for the element type,
19073b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall  // then just use that as our result.
19083b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall  if (QualType(canonElementType.first, 0) == elementType)
19093b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall    return canon;
19103b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall
19113b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall  // Otherwise, we need to build a type which follows the spelling
19123b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall  // of the element type.
19133b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall  DependentSizedArrayType *sugaredType
19143b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall    = new (*this, TypeAlignment)
19153b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall        DependentSizedArrayType(*this, elementType, canon, numElements,
19163b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall                                ASM, elementTypeQuals, brackets);
19173b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall  Types.push_back(sugaredType);
19183b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall  return QualType(sugaredType, 0);
19193b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall}
19203b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall
19213b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCallQualType ASTContext::getIncompleteArrayType(QualType elementType,
1922c5773c4b8ce1ed6ed5c7112c9020c954a47dce96Eli Friedman                                            ArrayType::ArraySizeModifier ASM,
19233b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall                                            unsigned elementTypeQuals) const {
1924c5773c4b8ce1ed6ed5c7112c9020c954a47dce96Eli Friedman  llvm::FoldingSetNodeID ID;
19253b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall  IncompleteArrayType::Profile(ID, elementType, ASM, elementTypeQuals);
1926c5773c4b8ce1ed6ed5c7112c9020c954a47dce96Eli Friedman
19273b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall  void *insertPos = 0;
19283b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall  if (IncompleteArrayType *iat =
19293b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall       IncompleteArrayTypes.FindNodeOrInsertPos(ID, insertPos))
19303b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall    return QualType(iat, 0);
1931c5773c4b8ce1ed6ed5c7112c9020c954a47dce96Eli Friedman
1932c5773c4b8ce1ed6ed5c7112c9020c954a47dce96Eli Friedman  // If the element type isn't canonical, this won't be a canonical type
19333b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall  // either, so fill in the canonical type field.  We also have to pull
19343b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall  // qualifiers off the element type.
19353b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall  QualType canon;
1936c5773c4b8ce1ed6ed5c7112c9020c954a47dce96Eli Friedman
19373b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall  if (!elementType.isCanonical() || elementType.hasLocalQualifiers()) {
19383b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall    SplitQualType canonSplit = getCanonicalType(elementType).split();
19393b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall    canon = getIncompleteArrayType(QualType(canonSplit.first, 0),
19403b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall                                   ASM, elementTypeQuals);
19413b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall    canon = getQualifiedType(canon, canonSplit.second);
1942c5773c4b8ce1ed6ed5c7112c9020c954a47dce96Eli Friedman
1943c5773c4b8ce1ed6ed5c7112c9020c954a47dce96Eli Friedman    // Get the new insert position for the node we care about.
19443b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall    IncompleteArrayType *existing =
19453b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall      IncompleteArrayTypes.FindNodeOrInsertPos(ID, insertPos);
19463b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall    assert(!existing && "Shouldn't be in the map!"); (void) existing;
19472bd24ba6d10f8c811c8e2a57c8397e07082ba497Ted Kremenek  }
1948c5773c4b8ce1ed6ed5c7112c9020c954a47dce96Eli Friedman
19493b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall  IncompleteArrayType *newType = new (*this, TypeAlignment)
19503b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall    IncompleteArrayType(elementType, canon, ASM, elementTypeQuals);
1951c5773c4b8ce1ed6ed5c7112c9020c954a47dce96Eli Friedman
19523b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall  IncompleteArrayTypes.InsertNode(newType, insertPos);
19533b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall  Types.push_back(newType);
19543b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall  return QualType(newType, 0);
1955fb22d96692c5240fb8d611290dbf7eeed3759c73Steve Naroff}
1956fb22d96692c5240fb8d611290dbf7eeed3759c73Steve Naroff
195773322924127c873c13101b705dd823f5539ffa5fSteve Naroff/// getVectorType - Return the unique reference to a vector type of
195873322924127c873c13101b705dd823f5539ffa5fSteve Naroff/// the specified element type and size. VectorType must be a built-in type.
195982287d19ded35248c4ce6a425ce74116a13ce44eJohn ThompsonQualType ASTContext::getVectorType(QualType vecType, unsigned NumElts,
19604ba2a17694148e16eaa8d3917f657ffcd3667be4Jay Foad                                   VectorType::VectorKind VecKind) const {
19613b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall  assert(vecType->isBuiltinType());
19621eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
19635f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // Check if we've already instantiated a vector of this type.
19645f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  llvm::FoldingSetNodeID ID;
1965e86d78cf4754a6aef2cf9a33d847aa15338e276fBob Wilson  VectorType::Profile(ID, vecType, NumElts, Type::Vector, VecKind);
1966788b0fd67e1992f23555454efcdb16a19dfefac3Chris Lattner
19675f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  void *InsertPos = 0;
19685f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  if (VectorType *VTP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos))
19695f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    return QualType(VTP, 0);
19705f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
19715f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // If the element type isn't canonical, this won't be a canonical type either,
19725f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // so fill in the canonical type field.
19735f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  QualType Canonical;
1974255210ef415b9893f0e3794e8d9a704194c12f3cDouglas Gregor  if (!vecType.isCanonical()) {
1975231da7eb3dd13007e5e40fffe48998e5ef284e06Bob Wilson    Canonical = getVectorType(getCanonicalType(vecType), NumElts, VecKind);
19761eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
19775f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    // Get the new insert position for the node we care about.
19785f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    VectorType *NewIP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos);
1979c6ed729f669044f5072a49d79041f455d971ece3Jeffrey Yasskin    assert(NewIP == 0 && "Shouldn't be in the map!"); (void)NewIP;
19805f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
19816b304a0254a13f42390b865ff5ba668a49cc58aeJohn McCall  VectorType *New = new (*this, TypeAlignment)
1982e86d78cf4754a6aef2cf9a33d847aa15338e276fBob Wilson    VectorType(vecType, NumElts, Canonical, VecKind);
19835f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  VectorTypes.InsertNode(New, InsertPos);
19845f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  Types.push_back(New);
19855f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  return QualType(New, 0);
19865f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
19875f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
1988213541a68a3e137d11d2cefb612c6cdb410d7e8eNate Begeman/// getExtVectorType - Return the unique reference to an extended vector type of
198973322924127c873c13101b705dd823f5539ffa5fSteve Naroff/// the specified element type and size. VectorType must be a built-in type.
19904ba2a17694148e16eaa8d3917f657ffcd3667be4Jay FoadQualType
19914ba2a17694148e16eaa8d3917f657ffcd3667be4Jay FoadASTContext::getExtVectorType(QualType vecType, unsigned NumElts) const {
19924ac01401b1ec602a1f58c217544d3dcb5fcbd7f1Douglas Gregor  assert(vecType->isBuiltinType() || vecType->isDependentType());
19931eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
199473322924127c873c13101b705dd823f5539ffa5fSteve Naroff  // Check if we've already instantiated a vector of this type.
199573322924127c873c13101b705dd823f5539ffa5fSteve Naroff  llvm::FoldingSetNodeID ID;
1996788b0fd67e1992f23555454efcdb16a19dfefac3Chris Lattner  VectorType::Profile(ID, vecType, NumElts, Type::ExtVector,
1997e86d78cf4754a6aef2cf9a33d847aa15338e276fBob Wilson                      VectorType::GenericVector);
199873322924127c873c13101b705dd823f5539ffa5fSteve Naroff  void *InsertPos = 0;
199973322924127c873c13101b705dd823f5539ffa5fSteve Naroff  if (VectorType *VTP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos))
200073322924127c873c13101b705dd823f5539ffa5fSteve Naroff    return QualType(VTP, 0);
200173322924127c873c13101b705dd823f5539ffa5fSteve Naroff
200273322924127c873c13101b705dd823f5539ffa5fSteve Naroff  // If the element type isn't canonical, this won't be a canonical type either,
200373322924127c873c13101b705dd823f5539ffa5fSteve Naroff  // so fill in the canonical type field.
200473322924127c873c13101b705dd823f5539ffa5fSteve Naroff  QualType Canonical;
2005467b27b9a24bdc823218ad1ad0e37673b6cc1e83John McCall  if (!vecType.isCanonical()) {
2006213541a68a3e137d11d2cefb612c6cdb410d7e8eNate Begeman    Canonical = getExtVectorType(getCanonicalType(vecType), NumElts);
20071eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
200873322924127c873c13101b705dd823f5539ffa5fSteve Naroff    // Get the new insert position for the node we care about.
200973322924127c873c13101b705dd823f5539ffa5fSteve Naroff    VectorType *NewIP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos);
2010c6ed729f669044f5072a49d79041f455d971ece3Jeffrey Yasskin    assert(NewIP == 0 && "Shouldn't be in the map!"); (void)NewIP;
201173322924127c873c13101b705dd823f5539ffa5fSteve Naroff  }
20126b304a0254a13f42390b865ff5ba668a49cc58aeJohn McCall  ExtVectorType *New = new (*this, TypeAlignment)
20136b304a0254a13f42390b865ff5ba668a49cc58aeJohn McCall    ExtVectorType(vecType, NumElts, Canonical);
201473322924127c873c13101b705dd823f5539ffa5fSteve Naroff  VectorTypes.InsertNode(New, InsertPos);
201573322924127c873c13101b705dd823f5539ffa5fSteve Naroff  Types.push_back(New);
201673322924127c873c13101b705dd823f5539ffa5fSteve Naroff  return QualType(New, 0);
201773322924127c873c13101b705dd823f5539ffa5fSteve Naroff}
201873322924127c873c13101b705dd823f5539ffa5fSteve Naroff
20194ba2a17694148e16eaa8d3917f657ffcd3667be4Jay FoadQualType
20204ba2a17694148e16eaa8d3917f657ffcd3667be4Jay FoadASTContext::getDependentSizedExtVectorType(QualType vecType,
20214ba2a17694148e16eaa8d3917f657ffcd3667be4Jay Foad                                           Expr *SizeExpr,
20224ba2a17694148e16eaa8d3917f657ffcd3667be4Jay Foad                                           SourceLocation AttrLoc) const {
20232ec09f1dc123e1942ed756e8ee4fef86451eac9eDouglas Gregor  llvm::FoldingSetNodeID ID;
20241eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  DependentSizedExtVectorType::Profile(ID, *this, getCanonicalType(vecType),
20252ec09f1dc123e1942ed756e8ee4fef86451eac9eDouglas Gregor                                       SizeExpr);
20261eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
20272ec09f1dc123e1942ed756e8ee4fef86451eac9eDouglas Gregor  void *InsertPos = 0;
20282ec09f1dc123e1942ed756e8ee4fef86451eac9eDouglas Gregor  DependentSizedExtVectorType *Canon
20292ec09f1dc123e1942ed756e8ee4fef86451eac9eDouglas Gregor    = DependentSizedExtVectorTypes.FindNodeOrInsertPos(ID, InsertPos);
20302ec09f1dc123e1942ed756e8ee4fef86451eac9eDouglas Gregor  DependentSizedExtVectorType *New;
20312ec09f1dc123e1942ed756e8ee4fef86451eac9eDouglas Gregor  if (Canon) {
20322ec09f1dc123e1942ed756e8ee4fef86451eac9eDouglas Gregor    // We already have a canonical version of this array type; use it as
20332ec09f1dc123e1942ed756e8ee4fef86451eac9eDouglas Gregor    // the canonical type for a newly-built type.
20346b304a0254a13f42390b865ff5ba668a49cc58aeJohn McCall    New = new (*this, TypeAlignment)
20356b304a0254a13f42390b865ff5ba668a49cc58aeJohn McCall      DependentSizedExtVectorType(*this, vecType, QualType(Canon, 0),
20366b304a0254a13f42390b865ff5ba668a49cc58aeJohn McCall                                  SizeExpr, AttrLoc);
20372ec09f1dc123e1942ed756e8ee4fef86451eac9eDouglas Gregor  } else {
20382ec09f1dc123e1942ed756e8ee4fef86451eac9eDouglas Gregor    QualType CanonVecTy = getCanonicalType(vecType);
20392ec09f1dc123e1942ed756e8ee4fef86451eac9eDouglas Gregor    if (CanonVecTy == vecType) {
20406b304a0254a13f42390b865ff5ba668a49cc58aeJohn McCall      New = new (*this, TypeAlignment)
20416b304a0254a13f42390b865ff5ba668a49cc58aeJohn McCall        DependentSizedExtVectorType(*this, vecType, QualType(), SizeExpr,
20426b304a0254a13f42390b865ff5ba668a49cc58aeJohn McCall                                    AttrLoc);
2043789b1f640205e81b5af250693246120f1ce9d147Douglas Gregor
2044789b1f640205e81b5af250693246120f1ce9d147Douglas Gregor      DependentSizedExtVectorType *CanonCheck
2045789b1f640205e81b5af250693246120f1ce9d147Douglas Gregor        = DependentSizedExtVectorTypes.FindNodeOrInsertPos(ID, InsertPos);
2046789b1f640205e81b5af250693246120f1ce9d147Douglas Gregor      assert(!CanonCheck && "Dependent-sized ext_vector canonical type broken");
2047789b1f640205e81b5af250693246120f1ce9d147Douglas Gregor      (void)CanonCheck;
20482ec09f1dc123e1942ed756e8ee4fef86451eac9eDouglas Gregor      DependentSizedExtVectorTypes.InsertNode(New, InsertPos);
20492ec09f1dc123e1942ed756e8ee4fef86451eac9eDouglas Gregor    } else {
20502ec09f1dc123e1942ed756e8ee4fef86451eac9eDouglas Gregor      QualType Canon = getDependentSizedExtVectorType(CanonVecTy, SizeExpr,
20512ec09f1dc123e1942ed756e8ee4fef86451eac9eDouglas Gregor                                                      SourceLocation());
20526b304a0254a13f42390b865ff5ba668a49cc58aeJohn McCall      New = new (*this, TypeAlignment)
20536b304a0254a13f42390b865ff5ba668a49cc58aeJohn McCall        DependentSizedExtVectorType(*this, vecType, Canon, SizeExpr, AttrLoc);
20542ec09f1dc123e1942ed756e8ee4fef86451eac9eDouglas Gregor    }
20552ec09f1dc123e1942ed756e8ee4fef86451eac9eDouglas Gregor  }
20561eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
20579cdda0cf8528e3d595be9bfa002f0450074beb4dDouglas Gregor  Types.push_back(New);
20589cdda0cf8528e3d595be9bfa002f0450074beb4dDouglas Gregor  return QualType(New, 0);
20599cdda0cf8528e3d595be9bfa002f0450074beb4dDouglas Gregor}
20609cdda0cf8528e3d595be9bfa002f0450074beb4dDouglas Gregor
206172564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor/// getFunctionNoProtoType - Return a K&R style C function type like 'int()'.
20625f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer///
20634ba2a17694148e16eaa8d3917f657ffcd3667be4Jay FoadQualType
20644ba2a17694148e16eaa8d3917f657ffcd3667be4Jay FoadASTContext::getFunctionNoProtoType(QualType ResultTy,
20654ba2a17694148e16eaa8d3917f657ffcd3667be4Jay Foad                                   const FunctionType::ExtInfo &Info) const {
2066cfe9af250f466e7e38becea4428990448ae07737Roman Divacky  const CallingConv DefaultCC = Info.getCC();
2067cfe9af250f466e7e38becea4428990448ae07737Roman Divacky  const CallingConv CallConv = (LangOpts.MRTD && DefaultCC == CC_Default) ?
2068cfe9af250f466e7e38becea4428990448ae07737Roman Divacky                               CC_X86StdCall : DefaultCC;
20695f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // Unique functions, to guarantee there is only one function of a particular
20705f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // structure.
20715f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  llvm::FoldingSetNodeID ID;
2072264ba48dc98f3f843935a485d5b086f7e0fdc4f1Rafael Espindola  FunctionNoProtoType::Profile(ID, ResultTy, Info);
20731eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
20745f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  void *InsertPos = 0;
20751eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  if (FunctionNoProtoType *FT =
207672564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor        FunctionNoProtoTypes.FindNodeOrInsertPos(ID, InsertPos))
20775f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    return QualType(FT, 0);
20781eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
20795f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  QualType Canonical;
2080ab8bbf4ebd3e3e6eab913cb044772a62b7581941Douglas Gregor  if (!ResultTy.isCanonical() ||
208104a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall      getCanonicalCallConv(CallConv) != CallConv) {
2082264ba48dc98f3f843935a485d5b086f7e0fdc4f1Rafael Espindola    Canonical =
2083264ba48dc98f3f843935a485d5b086f7e0fdc4f1Rafael Espindola      getFunctionNoProtoType(getCanonicalType(ResultTy),
2084264ba48dc98f3f843935a485d5b086f7e0fdc4f1Rafael Espindola                     Info.withCallingConv(getCanonicalCallConv(CallConv)));
20851eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
20865f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    // Get the new insert position for the node we care about.
208772564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor    FunctionNoProtoType *NewIP =
208872564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor      FunctionNoProtoTypes.FindNodeOrInsertPos(ID, InsertPos);
2089c6ed729f669044f5072a49d79041f455d971ece3Jeffrey Yasskin    assert(NewIP == 0 && "Shouldn't be in the map!"); (void)NewIP;
20905f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
20911eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
2092cfe9af250f466e7e38becea4428990448ae07737Roman Divacky  FunctionProtoType::ExtInfo newInfo = Info.withCallingConv(CallConv);
20936b304a0254a13f42390b865ff5ba668a49cc58aeJohn McCall  FunctionNoProtoType *New = new (*this, TypeAlignment)
2094cfe9af250f466e7e38becea4428990448ae07737Roman Divacky    FunctionNoProtoType(ResultTy, Canonical, newInfo);
20955f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  Types.push_back(New);
209672564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor  FunctionNoProtoTypes.InsertNode(New, InsertPos);
20975f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  return QualType(New, 0);
20985f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
20995f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
21005f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// getFunctionType - Return a normal function type with a typed argument
21015f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// list.  isVariadic indicates whether the argument list includes '...'.
21024ba2a17694148e16eaa8d3917f657ffcd3667be4Jay FoadQualType
21034ba2a17694148e16eaa8d3917f657ffcd3667be4Jay FoadASTContext::getFunctionType(QualType ResultTy,
21044ba2a17694148e16eaa8d3917f657ffcd3667be4Jay Foad                            const QualType *ArgArray, unsigned NumArgs,
21054ba2a17694148e16eaa8d3917f657ffcd3667be4Jay Foad                            const FunctionProtoType::ExtProtoInfo &EPI) const {
21065f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // Unique functions, to guarantee there is only one function of a particular
21075f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // structure.
21085f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  llvm::FoldingSetNodeID ID;
21098026f6d82f7fa544bc0453714fe94bca62a1196eSebastian Redl  FunctionProtoType::Profile(ID, ResultTy, ArgArray, NumArgs, EPI, *this);
21105f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
21115f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  void *InsertPos = 0;
21121eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  if (FunctionProtoType *FTP =
211372564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor        FunctionProtoTypes.FindNodeOrInsertPos(ID, InsertPos))
21145f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    return QualType(FTP, 0);
2115465226e23a3008bd68973513dda1f9e3cd27dbddSebastian Redl
2116465226e23a3008bd68973513dda1f9e3cd27dbddSebastian Redl  // Determine whether the type being created is already canonical or not.
21178b5b4099c61a136e9a1714c4d8a593febe942268Sebastian Redl  bool isCanonical= EPI.ExceptionSpecType == EST_None && ResultTy.isCanonical();
21185f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  for (unsigned i = 0; i != NumArgs && isCanonical; ++i)
211954e14c4db764c0636160d26c5bbf491637c83a76John McCall    if (!ArgArray[i].isCanonicalAsParam())
21205f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      isCanonical = false;
21215f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
2122cfe9af250f466e7e38becea4428990448ae07737Roman Divacky  const CallingConv DefaultCC = EPI.ExtInfo.getCC();
2123cfe9af250f466e7e38becea4428990448ae07737Roman Divacky  const CallingConv CallConv = (LangOpts.MRTD && DefaultCC == CC_Default) ?
2124cfe9af250f466e7e38becea4428990448ae07737Roman Divacky                               CC_X86StdCall : DefaultCC;
2125e23cf437fe76b1ed02d63c3f61b456fd48a915f5John McCall
21265f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // If this type isn't canonical, get the canonical version of it.
2127465226e23a3008bd68973513dda1f9e3cd27dbddSebastian Redl  // The exception spec is not part of the canonical type.
21285f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  QualType Canonical;
212904a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall  if (!isCanonical || getCanonicalCallConv(CallConv) != CallConv) {
21305f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner    SmallVector<QualType, 16> CanonicalArgs;
21315f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    CanonicalArgs.reserve(NumArgs);
21325f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    for (unsigned i = 0; i != NumArgs; ++i)
213354e14c4db764c0636160d26c5bbf491637c83a76John McCall      CanonicalArgs.push_back(getCanonicalParamType(ArgArray[i]));
2134465226e23a3008bd68973513dda1f9e3cd27dbddSebastian Redl
2135e23cf437fe76b1ed02d63c3f61b456fd48a915f5John McCall    FunctionProtoType::ExtProtoInfo CanonicalEPI = EPI;
21368b5b4099c61a136e9a1714c4d8a593febe942268Sebastian Redl    CanonicalEPI.ExceptionSpecType = EST_None;
21378b5b4099c61a136e9a1714c4d8a593febe942268Sebastian Redl    CanonicalEPI.NumExceptions = 0;
2138e23cf437fe76b1ed02d63c3f61b456fd48a915f5John McCall    CanonicalEPI.ExtInfo
2139e23cf437fe76b1ed02d63c3f61b456fd48a915f5John McCall      = CanonicalEPI.ExtInfo.withCallingConv(getCanonicalCallConv(CallConv));
2140e23cf437fe76b1ed02d63c3f61b456fd48a915f5John McCall
2141f52ab250ff92bc51a9ac9a8e19bd43b63a5f844fChris Lattner    Canonical = getFunctionType(getCanonicalType(ResultTy),
2142beaaccd8e2a8748f77b66e2b330fb9136937e14cJay Foad                                CanonicalArgs.data(), NumArgs,
2143e23cf437fe76b1ed02d63c3f61b456fd48a915f5John McCall                                CanonicalEPI);
2144465226e23a3008bd68973513dda1f9e3cd27dbddSebastian Redl
21455f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    // Get the new insert position for the node we care about.
214672564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor    FunctionProtoType *NewIP =
214772564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor      FunctionProtoTypes.FindNodeOrInsertPos(ID, InsertPos);
2148c6ed729f669044f5072a49d79041f455d971ece3Jeffrey Yasskin    assert(NewIP == 0 && "Shouldn't be in the map!"); (void)NewIP;
21495f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
2150465226e23a3008bd68973513dda1f9e3cd27dbddSebastian Redl
2151f85e193739c953358c865005855253af4f68a497John McCall  // FunctionProtoType objects are allocated with extra bytes after
2152f85e193739c953358c865005855253af4f68a497John McCall  // them for three variable size arrays at the end:
2153f85e193739c953358c865005855253af4f68a497John McCall  //  - parameter types
2154f85e193739c953358c865005855253af4f68a497John McCall  //  - exception types
2155f85e193739c953358c865005855253af4f68a497John McCall  //  - consumed-arguments flags
2156f85e193739c953358c865005855253af4f68a497John McCall  // Instead of the exception types, there could be a noexcept
2157f85e193739c953358c865005855253af4f68a497John McCall  // expression.
2158e23cf437fe76b1ed02d63c3f61b456fd48a915f5John McCall  size_t Size = sizeof(FunctionProtoType) +
215960618fa7f88d5162bb5b40988b6b38d4d75d6fc6Sebastian Redl                NumArgs * sizeof(QualType);
216060618fa7f88d5162bb5b40988b6b38d4d75d6fc6Sebastian Redl  if (EPI.ExceptionSpecType == EST_Dynamic)
216160618fa7f88d5162bb5b40988b6b38d4d75d6fc6Sebastian Redl    Size += EPI.NumExceptions * sizeof(QualType);
216260618fa7f88d5162bb5b40988b6b38d4d75d6fc6Sebastian Redl  else if (EPI.ExceptionSpecType == EST_ComputedNoexcept) {
21638026f6d82f7fa544bc0453714fe94bca62a1196eSebastian Redl    Size += sizeof(Expr*);
216460618fa7f88d5162bb5b40988b6b38d4d75d6fc6Sebastian Redl  }
2165f85e193739c953358c865005855253af4f68a497John McCall  if (EPI.ConsumedArguments)
2166f85e193739c953358c865005855253af4f68a497John McCall    Size += NumArgs * sizeof(bool);
2167f85e193739c953358c865005855253af4f68a497John McCall
2168e23cf437fe76b1ed02d63c3f61b456fd48a915f5John McCall  FunctionProtoType *FTP = (FunctionProtoType*) Allocate(Size, TypeAlignment);
2169cfe9af250f466e7e38becea4428990448ae07737Roman Divacky  FunctionProtoType::ExtProtoInfo newEPI = EPI;
2170cfe9af250f466e7e38becea4428990448ae07737Roman Divacky  newEPI.ExtInfo = EPI.ExtInfo.withCallingConv(CallConv);
21718026f6d82f7fa544bc0453714fe94bca62a1196eSebastian Redl  new (FTP) FunctionProtoType(ResultTy, ArgArray, NumArgs, Canonical, newEPI);
21725f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  Types.push_back(FTP);
217372564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor  FunctionProtoTypes.InsertNode(FTP, InsertPos);
21745f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  return QualType(FTP, 0);
21755f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
21765f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
21773cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall#ifndef NDEBUG
21783cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCallstatic bool NeedsInjectedClassNameType(const RecordDecl *D) {
21793cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall  if (!isa<CXXRecordDecl>(D)) return false;
21803cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall  const CXXRecordDecl *RD = cast<CXXRecordDecl>(D);
21813cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall  if (isa<ClassTemplatePartialSpecializationDecl>(RD))
21823cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall    return true;
21833cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall  if (RD->getDescribedClassTemplate() &&
21843cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall      !isa<ClassTemplateSpecializationDecl>(RD))
21853cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall    return true;
21863cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall  return false;
21873cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall}
21883cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall#endif
21893cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall
21903cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall/// getInjectedClassNameType - Return the unique reference to the
21913cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall/// injected class name type for the specified templated declaration.
21923cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCallQualType ASTContext::getInjectedClassNameType(CXXRecordDecl *Decl,
21934ba2a17694148e16eaa8d3917f657ffcd3667be4Jay Foad                                              QualType TST) const {
21943cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall  assert(NeedsInjectedClassNameType(Decl));
21953cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall  if (Decl->TypeForDecl) {
21963cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall    assert(isa<InjectedClassNameType>(Decl->TypeForDecl));
2197ef96ee0be5f100789f451641542a69cd719144d2Douglas Gregor  } else if (CXXRecordDecl *PrevDecl = Decl->getPreviousDecl()) {
21983cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall    assert(PrevDecl->TypeForDecl && "previous declaration has no type");
21993cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall    Decl->TypeForDecl = PrevDecl->TypeForDecl;
22003cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall    assert(isa<InjectedClassNameType>(Decl->TypeForDecl));
22013cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall  } else {
2202f4c7371fb1d3cebcfb40abad4537bb82515704eaJohn McCall    Type *newType =
220331f17ecbef57b5679c017c375db330546b7b5145John McCall      new (*this, TypeAlignment) InjectedClassNameType(Decl, TST);
2204f4c7371fb1d3cebcfb40abad4537bb82515704eaJohn McCall    Decl->TypeForDecl = newType;
2205f4c7371fb1d3cebcfb40abad4537bb82515704eaJohn McCall    Types.push_back(newType);
22063cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall  }
22073cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall  return QualType(Decl->TypeForDecl, 0);
22083cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall}
22093cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall
22102ce52f3fb95bf544db6bd3d91a72bce7d9cceb6cDouglas Gregor/// getTypeDeclType - Return the unique reference to the type for the
22112ce52f3fb95bf544db6bd3d91a72bce7d9cceb6cDouglas Gregor/// specified type declaration.
22124ba2a17694148e16eaa8d3917f657ffcd3667be4Jay FoadQualType ASTContext::getTypeDeclTypeSlow(const TypeDecl *Decl) const {
22131e6759e9e33dcaa73ce14c8a908ac9f87ac16463Argyrios Kyrtzidis  assert(Decl && "Passed null for Decl param");
2214becb8d5a6ab5103393eac5344ae69bcb860601ddJohn McCall  assert(!Decl->TypeForDecl && "TypeForDecl present in slow case");
22151eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
2216162e1c1b487352434552147967c3dd296ebee2f7Richard Smith  if (const TypedefNameDecl *Typedef = dyn_cast<TypedefNameDecl>(Decl))
22172ce52f3fb95bf544db6bd3d91a72bce7d9cceb6cDouglas Gregor    return getTypedefType(Typedef);
2218becb8d5a6ab5103393eac5344ae69bcb860601ddJohn McCall
2219becb8d5a6ab5103393eac5344ae69bcb860601ddJohn McCall  assert(!isa<TemplateTypeParmDecl>(Decl) &&
2220becb8d5a6ab5103393eac5344ae69bcb860601ddJohn McCall         "Template type parameter types are always available.");
2221becb8d5a6ab5103393eac5344ae69bcb860601ddJohn McCall
222219c8576b7328f4dc2d07682f5da552875c1912efJohn McCall  if (const RecordDecl *Record = dyn_cast<RecordDecl>(Decl)) {
2223ef96ee0be5f100789f451641542a69cd719144d2Douglas Gregor    assert(!Record->getPreviousDecl() &&
2224becb8d5a6ab5103393eac5344ae69bcb860601ddJohn McCall           "struct/union has previous declaration");
2225becb8d5a6ab5103393eac5344ae69bcb860601ddJohn McCall    assert(!NeedsInjectedClassNameType(Record));
2226400f5125e2432d648f2c8a31b36a7f318a880c47Argyrios Kyrtzidis    return getRecordType(Record);
222719c8576b7328f4dc2d07682f5da552875c1912efJohn McCall  } else if (const EnumDecl *Enum = dyn_cast<EnumDecl>(Decl)) {
2228ef96ee0be5f100789f451641542a69cd719144d2Douglas Gregor    assert(!Enum->getPreviousDecl() &&
2229becb8d5a6ab5103393eac5344ae69bcb860601ddJohn McCall           "enum has previous declaration");
2230400f5125e2432d648f2c8a31b36a7f318a880c47Argyrios Kyrtzidis    return getEnumType(Enum);
223119c8576b7328f4dc2d07682f5da552875c1912efJohn McCall  } else if (const UnresolvedUsingTypenameDecl *Using =
2232ed97649e9574b9d854fa4d6109c9333ae0993554John McCall               dyn_cast<UnresolvedUsingTypenameDecl>(Decl)) {
2233f4c7371fb1d3cebcfb40abad4537bb82515704eaJohn McCall    Type *newType = new (*this, TypeAlignment) UnresolvedUsingType(Using);
2234f4c7371fb1d3cebcfb40abad4537bb82515704eaJohn McCall    Decl->TypeForDecl = newType;
2235f4c7371fb1d3cebcfb40abad4537bb82515704eaJohn McCall    Types.push_back(newType);
22369fdbab3cbc2fc04bcaf5768023d83707f3151144Mike Stump  } else
2237becb8d5a6ab5103393eac5344ae69bcb860601ddJohn McCall    llvm_unreachable("TypeDecl without a type?");
223849aa7ff1245abd03e6e998e01302df31e4c6f8f6Argyrios Kyrtzidis
223949aa7ff1245abd03e6e998e01302df31e4c6f8f6Argyrios Kyrtzidis  return QualType(Decl->TypeForDecl, 0);
22402ce52f3fb95bf544db6bd3d91a72bce7d9cceb6cDouglas Gregor}
22412ce52f3fb95bf544db6bd3d91a72bce7d9cceb6cDouglas Gregor
22425f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// getTypedefType - Return the unique reference to the type for the
2243162e1c1b487352434552147967c3dd296ebee2f7Richard Smith/// specified typedef name decl.
22449763e221e16026ddf487d2564ed349d2c874a1a1Argyrios KyrtzidisQualType
2245162e1c1b487352434552147967c3dd296ebee2f7Richard SmithASTContext::getTypedefType(const TypedefNameDecl *Decl,
2246162e1c1b487352434552147967c3dd296ebee2f7Richard Smith                           QualType Canonical) const {
22475f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  if (Decl->TypeForDecl) return QualType(Decl->TypeForDecl, 0);
22481eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
22499763e221e16026ddf487d2564ed349d2c874a1a1Argyrios Kyrtzidis  if (Canonical.isNull())
22509763e221e16026ddf487d2564ed349d2c874a1a1Argyrios Kyrtzidis    Canonical = getCanonicalType(Decl->getUnderlyingType());
2251f4c7371fb1d3cebcfb40abad4537bb82515704eaJohn McCall  TypedefType *newType = new(*this, TypeAlignment)
22526b304a0254a13f42390b865ff5ba668a49cc58aeJohn McCall    TypedefType(Type::Typedef, Decl, Canonical);
2253f4c7371fb1d3cebcfb40abad4537bb82515704eaJohn McCall  Decl->TypeForDecl = newType;
2254f4c7371fb1d3cebcfb40abad4537bb82515704eaJohn McCall  Types.push_back(newType);
2255f4c7371fb1d3cebcfb40abad4537bb82515704eaJohn McCall  return QualType(newType, 0);
22565f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
22575f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
22584ba2a17694148e16eaa8d3917f657ffcd3667be4Jay FoadQualType ASTContext::getRecordType(const RecordDecl *Decl) const {
2259400f5125e2432d648f2c8a31b36a7f318a880c47Argyrios Kyrtzidis  if (Decl->TypeForDecl) return QualType(Decl->TypeForDecl, 0);
2260400f5125e2432d648f2c8a31b36a7f318a880c47Argyrios Kyrtzidis
2261ef96ee0be5f100789f451641542a69cd719144d2Douglas Gregor  if (const RecordDecl *PrevDecl = Decl->getPreviousDecl())
2262400f5125e2432d648f2c8a31b36a7f318a880c47Argyrios Kyrtzidis    if (PrevDecl->TypeForDecl)
2263400f5125e2432d648f2c8a31b36a7f318a880c47Argyrios Kyrtzidis      return QualType(Decl->TypeForDecl = PrevDecl->TypeForDecl, 0);
2264400f5125e2432d648f2c8a31b36a7f318a880c47Argyrios Kyrtzidis
2265f4c7371fb1d3cebcfb40abad4537bb82515704eaJohn McCall  RecordType *newType = new (*this, TypeAlignment) RecordType(Decl);
2266f4c7371fb1d3cebcfb40abad4537bb82515704eaJohn McCall  Decl->TypeForDecl = newType;
2267f4c7371fb1d3cebcfb40abad4537bb82515704eaJohn McCall  Types.push_back(newType);
2268f4c7371fb1d3cebcfb40abad4537bb82515704eaJohn McCall  return QualType(newType, 0);
2269400f5125e2432d648f2c8a31b36a7f318a880c47Argyrios Kyrtzidis}
2270400f5125e2432d648f2c8a31b36a7f318a880c47Argyrios Kyrtzidis
22714ba2a17694148e16eaa8d3917f657ffcd3667be4Jay FoadQualType ASTContext::getEnumType(const EnumDecl *Decl) const {
2272400f5125e2432d648f2c8a31b36a7f318a880c47Argyrios Kyrtzidis  if (Decl->TypeForDecl) return QualType(Decl->TypeForDecl, 0);
2273400f5125e2432d648f2c8a31b36a7f318a880c47Argyrios Kyrtzidis
2274ef96ee0be5f100789f451641542a69cd719144d2Douglas Gregor  if (const EnumDecl *PrevDecl = Decl->getPreviousDecl())
2275400f5125e2432d648f2c8a31b36a7f318a880c47Argyrios Kyrtzidis    if (PrevDecl->TypeForDecl)
2276400f5125e2432d648f2c8a31b36a7f318a880c47Argyrios Kyrtzidis      return QualType(Decl->TypeForDecl = PrevDecl->TypeForDecl, 0);
2277400f5125e2432d648f2c8a31b36a7f318a880c47Argyrios Kyrtzidis
2278f4c7371fb1d3cebcfb40abad4537bb82515704eaJohn McCall  EnumType *newType = new (*this, TypeAlignment) EnumType(Decl);
2279f4c7371fb1d3cebcfb40abad4537bb82515704eaJohn McCall  Decl->TypeForDecl = newType;
2280f4c7371fb1d3cebcfb40abad4537bb82515704eaJohn McCall  Types.push_back(newType);
2281f4c7371fb1d3cebcfb40abad4537bb82515704eaJohn McCall  return QualType(newType, 0);
2282400f5125e2432d648f2c8a31b36a7f318a880c47Argyrios Kyrtzidis}
2283400f5125e2432d648f2c8a31b36a7f318a880c47Argyrios Kyrtzidis
22849d156a7b1b2771e191f2f5a45a7b7a694129463bJohn McCallQualType ASTContext::getAttributedType(AttributedType::Kind attrKind,
22859d156a7b1b2771e191f2f5a45a7b7a694129463bJohn McCall                                       QualType modifiedType,
22869d156a7b1b2771e191f2f5a45a7b7a694129463bJohn McCall                                       QualType equivalentType) {
22879d156a7b1b2771e191f2f5a45a7b7a694129463bJohn McCall  llvm::FoldingSetNodeID id;
22889d156a7b1b2771e191f2f5a45a7b7a694129463bJohn McCall  AttributedType::Profile(id, attrKind, modifiedType, equivalentType);
22899d156a7b1b2771e191f2f5a45a7b7a694129463bJohn McCall
22909d156a7b1b2771e191f2f5a45a7b7a694129463bJohn McCall  void *insertPos = 0;
22919d156a7b1b2771e191f2f5a45a7b7a694129463bJohn McCall  AttributedType *type = AttributedTypes.FindNodeOrInsertPos(id, insertPos);
22929d156a7b1b2771e191f2f5a45a7b7a694129463bJohn McCall  if (type) return QualType(type, 0);
22939d156a7b1b2771e191f2f5a45a7b7a694129463bJohn McCall
22949d156a7b1b2771e191f2f5a45a7b7a694129463bJohn McCall  QualType canon = getCanonicalType(equivalentType);
22959d156a7b1b2771e191f2f5a45a7b7a694129463bJohn McCall  type = new (*this, TypeAlignment)
22969d156a7b1b2771e191f2f5a45a7b7a694129463bJohn McCall           AttributedType(canon, attrKind, modifiedType, equivalentType);
22979d156a7b1b2771e191f2f5a45a7b7a694129463bJohn McCall
22989d156a7b1b2771e191f2f5a45a7b7a694129463bJohn McCall  Types.push_back(type);
22999d156a7b1b2771e191f2f5a45a7b7a694129463bJohn McCall  AttributedTypes.InsertNode(type, insertPos);
23009d156a7b1b2771e191f2f5a45a7b7a694129463bJohn McCall
23019d156a7b1b2771e191f2f5a45a7b7a694129463bJohn McCall  return QualType(type, 0);
23029d156a7b1b2771e191f2f5a45a7b7a694129463bJohn McCall}
23039d156a7b1b2771e191f2f5a45a7b7a694129463bJohn McCall
23049d156a7b1b2771e191f2f5a45a7b7a694129463bJohn McCall
230549a832bd499d6f61c23655f1fac99f0dd229756eJohn McCall/// \brief Retrieve a substitution-result type.
230649a832bd499d6f61c23655f1fac99f0dd229756eJohn McCallQualType
230749a832bd499d6f61c23655f1fac99f0dd229756eJohn McCallASTContext::getSubstTemplateTypeParmType(const TemplateTypeParmType *Parm,
23084ba2a17694148e16eaa8d3917f657ffcd3667be4Jay Foad                                         QualType Replacement) const {
2309467b27b9a24bdc823218ad1ad0e37673b6cc1e83John McCall  assert(Replacement.isCanonical()
231049a832bd499d6f61c23655f1fac99f0dd229756eJohn McCall         && "replacement types must always be canonical");
231149a832bd499d6f61c23655f1fac99f0dd229756eJohn McCall
231249a832bd499d6f61c23655f1fac99f0dd229756eJohn McCall  llvm::FoldingSetNodeID ID;
231349a832bd499d6f61c23655f1fac99f0dd229756eJohn McCall  SubstTemplateTypeParmType::Profile(ID, Parm, Replacement);
231449a832bd499d6f61c23655f1fac99f0dd229756eJohn McCall  void *InsertPos = 0;
231549a832bd499d6f61c23655f1fac99f0dd229756eJohn McCall  SubstTemplateTypeParmType *SubstParm
231649a832bd499d6f61c23655f1fac99f0dd229756eJohn McCall    = SubstTemplateTypeParmTypes.FindNodeOrInsertPos(ID, InsertPos);
231749a832bd499d6f61c23655f1fac99f0dd229756eJohn McCall
231849a832bd499d6f61c23655f1fac99f0dd229756eJohn McCall  if (!SubstParm) {
231949a832bd499d6f61c23655f1fac99f0dd229756eJohn McCall    SubstParm = new (*this, TypeAlignment)
232049a832bd499d6f61c23655f1fac99f0dd229756eJohn McCall      SubstTemplateTypeParmType(Parm, Replacement);
232149a832bd499d6f61c23655f1fac99f0dd229756eJohn McCall    Types.push_back(SubstParm);
232249a832bd499d6f61c23655f1fac99f0dd229756eJohn McCall    SubstTemplateTypeParmTypes.InsertNode(SubstParm, InsertPos);
232349a832bd499d6f61c23655f1fac99f0dd229756eJohn McCall  }
232449a832bd499d6f61c23655f1fac99f0dd229756eJohn McCall
232549a832bd499d6f61c23655f1fac99f0dd229756eJohn McCall  return QualType(SubstParm, 0);
232649a832bd499d6f61c23655f1fac99f0dd229756eJohn McCall}
232749a832bd499d6f61c23655f1fac99f0dd229756eJohn McCall
2328c3069d618f4661d923cb1b5c4525b082fce73b04Douglas Gregor/// \brief Retrieve a
2329c3069d618f4661d923cb1b5c4525b082fce73b04Douglas GregorQualType ASTContext::getSubstTemplateTypeParmPackType(
2330c3069d618f4661d923cb1b5c4525b082fce73b04Douglas Gregor                                          const TemplateTypeParmType *Parm,
2331c3069d618f4661d923cb1b5c4525b082fce73b04Douglas Gregor                                              const TemplateArgument &ArgPack) {
2332c3069d618f4661d923cb1b5c4525b082fce73b04Douglas Gregor#ifndef NDEBUG
2333c3069d618f4661d923cb1b5c4525b082fce73b04Douglas Gregor  for (TemplateArgument::pack_iterator P = ArgPack.pack_begin(),
2334c3069d618f4661d923cb1b5c4525b082fce73b04Douglas Gregor                                    PEnd = ArgPack.pack_end();
2335c3069d618f4661d923cb1b5c4525b082fce73b04Douglas Gregor       P != PEnd; ++P) {
2336c3069d618f4661d923cb1b5c4525b082fce73b04Douglas Gregor    assert(P->getKind() == TemplateArgument::Type &&"Pack contains a non-type");
2337c3069d618f4661d923cb1b5c4525b082fce73b04Douglas Gregor    assert(P->getAsType().isCanonical() && "Pack contains non-canonical type");
2338c3069d618f4661d923cb1b5c4525b082fce73b04Douglas Gregor  }
2339c3069d618f4661d923cb1b5c4525b082fce73b04Douglas Gregor#endif
2340c3069d618f4661d923cb1b5c4525b082fce73b04Douglas Gregor
2341c3069d618f4661d923cb1b5c4525b082fce73b04Douglas Gregor  llvm::FoldingSetNodeID ID;
2342c3069d618f4661d923cb1b5c4525b082fce73b04Douglas Gregor  SubstTemplateTypeParmPackType::Profile(ID, Parm, ArgPack);
2343c3069d618f4661d923cb1b5c4525b082fce73b04Douglas Gregor  void *InsertPos = 0;
2344c3069d618f4661d923cb1b5c4525b082fce73b04Douglas Gregor  if (SubstTemplateTypeParmPackType *SubstParm
2345c3069d618f4661d923cb1b5c4525b082fce73b04Douglas Gregor        = SubstTemplateTypeParmPackTypes.FindNodeOrInsertPos(ID, InsertPos))
2346c3069d618f4661d923cb1b5c4525b082fce73b04Douglas Gregor    return QualType(SubstParm, 0);
2347c3069d618f4661d923cb1b5c4525b082fce73b04Douglas Gregor
2348c3069d618f4661d923cb1b5c4525b082fce73b04Douglas Gregor  QualType Canon;
2349c3069d618f4661d923cb1b5c4525b082fce73b04Douglas Gregor  if (!Parm->isCanonicalUnqualified()) {
2350c3069d618f4661d923cb1b5c4525b082fce73b04Douglas Gregor    Canon = getCanonicalType(QualType(Parm, 0));
2351c3069d618f4661d923cb1b5c4525b082fce73b04Douglas Gregor    Canon = getSubstTemplateTypeParmPackType(cast<TemplateTypeParmType>(Canon),
2352c3069d618f4661d923cb1b5c4525b082fce73b04Douglas Gregor                                             ArgPack);
2353c3069d618f4661d923cb1b5c4525b082fce73b04Douglas Gregor    SubstTemplateTypeParmPackTypes.FindNodeOrInsertPos(ID, InsertPos);
2354c3069d618f4661d923cb1b5c4525b082fce73b04Douglas Gregor  }
2355c3069d618f4661d923cb1b5c4525b082fce73b04Douglas Gregor
2356c3069d618f4661d923cb1b5c4525b082fce73b04Douglas Gregor  SubstTemplateTypeParmPackType *SubstParm
2357c3069d618f4661d923cb1b5c4525b082fce73b04Douglas Gregor    = new (*this, TypeAlignment) SubstTemplateTypeParmPackType(Parm, Canon,
2358c3069d618f4661d923cb1b5c4525b082fce73b04Douglas Gregor                                                               ArgPack);
2359c3069d618f4661d923cb1b5c4525b082fce73b04Douglas Gregor  Types.push_back(SubstParm);
2360c3069d618f4661d923cb1b5c4525b082fce73b04Douglas Gregor  SubstTemplateTypeParmTypes.InsertNode(SubstParm, InsertPos);
2361c3069d618f4661d923cb1b5c4525b082fce73b04Douglas Gregor  return QualType(SubstParm, 0);
2362c3069d618f4661d923cb1b5c4525b082fce73b04Douglas Gregor}
2363c3069d618f4661d923cb1b5c4525b082fce73b04Douglas Gregor
2364fab9d67cebb87be968e7ae31a3b549a5279b5d51Douglas Gregor/// \brief Retrieve the template type parameter type for a template
23651eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// parameter or parameter pack with the given depth, index, and (optionally)
236676e4ce42a30cee4dc40ce7c6014874fbc4f9baa7Anders Carlsson/// name.
23671eb4433ac451dc16f4133a88af2d002ac26c58efMike StumpQualType ASTContext::getTemplateTypeParmType(unsigned Depth, unsigned Index,
236876e4ce42a30cee4dc40ce7c6014874fbc4f9baa7Anders Carlsson                                             bool ParameterPack,
23694fb86f8c4585e53c21c847ad3de9e3b2de123cd9Chandler Carruth                                             TemplateTypeParmDecl *TTPDecl) const {
2370fab9d67cebb87be968e7ae31a3b549a5279b5d51Douglas Gregor  llvm::FoldingSetNodeID ID;
23714fb86f8c4585e53c21c847ad3de9e3b2de123cd9Chandler Carruth  TemplateTypeParmType::Profile(ID, Depth, Index, ParameterPack, TTPDecl);
2372fab9d67cebb87be968e7ae31a3b549a5279b5d51Douglas Gregor  void *InsertPos = 0;
23731eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  TemplateTypeParmType *TypeParm
2374fab9d67cebb87be968e7ae31a3b549a5279b5d51Douglas Gregor    = TemplateTypeParmTypes.FindNodeOrInsertPos(ID, InsertPos);
2375fab9d67cebb87be968e7ae31a3b549a5279b5d51Douglas Gregor
2376fab9d67cebb87be968e7ae31a3b549a5279b5d51Douglas Gregor  if (TypeParm)
2377fab9d67cebb87be968e7ae31a3b549a5279b5d51Douglas Gregor    return QualType(TypeParm, 0);
23781eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
23794fb86f8c4585e53c21c847ad3de9e3b2de123cd9Chandler Carruth  if (TTPDecl) {
238076e4ce42a30cee4dc40ce7c6014874fbc4f9baa7Anders Carlsson    QualType Canon = getTemplateTypeParmType(Depth, Index, ParameterPack);
23814fb86f8c4585e53c21c847ad3de9e3b2de123cd9Chandler Carruth    TypeParm = new (*this, TypeAlignment) TemplateTypeParmType(TTPDecl, Canon);
2382789b1f640205e81b5af250693246120f1ce9d147Douglas Gregor
2383789b1f640205e81b5af250693246120f1ce9d147Douglas Gregor    TemplateTypeParmType *TypeCheck
2384789b1f640205e81b5af250693246120f1ce9d147Douglas Gregor      = TemplateTypeParmTypes.FindNodeOrInsertPos(ID, InsertPos);
2385789b1f640205e81b5af250693246120f1ce9d147Douglas Gregor    assert(!TypeCheck && "Template type parameter canonical type broken");
2386789b1f640205e81b5af250693246120f1ce9d147Douglas Gregor    (void)TypeCheck;
238776e4ce42a30cee4dc40ce7c6014874fbc4f9baa7Anders Carlsson  } else
23886b304a0254a13f42390b865ff5ba668a49cc58aeJohn McCall    TypeParm = new (*this, TypeAlignment)
23896b304a0254a13f42390b865ff5ba668a49cc58aeJohn McCall      TemplateTypeParmType(Depth, Index, ParameterPack);
2390fab9d67cebb87be968e7ae31a3b549a5279b5d51Douglas Gregor
2391fab9d67cebb87be968e7ae31a3b549a5279b5d51Douglas Gregor  Types.push_back(TypeParm);
2392fab9d67cebb87be968e7ae31a3b549a5279b5d51Douglas Gregor  TemplateTypeParmTypes.InsertNode(TypeParm, InsertPos);
2393fab9d67cebb87be968e7ae31a3b549a5279b5d51Douglas Gregor
2394fab9d67cebb87be968e7ae31a3b549a5279b5d51Douglas Gregor  return QualType(TypeParm, 0);
2395fab9d67cebb87be968e7ae31a3b549a5279b5d51Douglas Gregor}
2396fab9d67cebb87be968e7ae31a3b549a5279b5d51Douglas Gregor
23973cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCallTypeSourceInfo *
23983cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCallASTContext::getTemplateSpecializationTypeInfo(TemplateName Name,
23993cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall                                              SourceLocation NameLoc,
24003cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall                                        const TemplateArgumentListInfo &Args,
24013e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith                                              QualType Underlying) const {
24027c3179cf463c3b3b8c21dbb955f933ba50b74f28Douglas Gregor  assert(!Name.getAsDependentTemplateName() &&
24037c3179cf463c3b3b8c21dbb955f933ba50b74f28Douglas Gregor         "No dependent template names here!");
24043e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith  QualType TST = getTemplateSpecializationType(Name, Args, Underlying);
24053cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall
24063cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall  TypeSourceInfo *DI = CreateTypeSourceInfo(TST);
24073cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall  TemplateSpecializationTypeLoc TL
24083cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall    = cast<TemplateSpecializationTypeLoc>(DI->getTypeLoc());
24093cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall  TL.setTemplateNameLoc(NameLoc);
24103cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall  TL.setLAngleLoc(Args.getLAngleLoc());
24113cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall  TL.setRAngleLoc(Args.getRAngleLoc());
24123cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall  for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i)
24133cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall    TL.setArgLocInfo(i, Args[i].getLocInfo());
24143cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall  return DI;
24153cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall}
24163cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall
24171eb4433ac451dc16f4133a88af2d002ac26c58efMike StumpQualType
24187532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas GregorASTContext::getTemplateSpecializationType(TemplateName Template,
2419d5532b6cfff2977e0c59fa6ead7f7973984a620dJohn McCall                                          const TemplateArgumentListInfo &Args,
24203e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith                                          QualType Underlying) const {
24217c3179cf463c3b3b8c21dbb955f933ba50b74f28Douglas Gregor  assert(!Template.getAsDependentTemplateName() &&
24227c3179cf463c3b3b8c21dbb955f933ba50b74f28Douglas Gregor         "No dependent template names here!");
24237c3179cf463c3b3b8c21dbb955f933ba50b74f28Douglas Gregor
2424d5532b6cfff2977e0c59fa6ead7f7973984a620dJohn McCall  unsigned NumArgs = Args.size();
2425d5532b6cfff2977e0c59fa6ead7f7973984a620dJohn McCall
24265f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner  SmallVector<TemplateArgument, 4> ArgVec;
2427833ca991c1bfc967f0995974ca86f66ba1f666b5John McCall  ArgVec.reserve(NumArgs);
2428833ca991c1bfc967f0995974ca86f66ba1f666b5John McCall  for (unsigned i = 0; i != NumArgs; ++i)
2429833ca991c1bfc967f0995974ca86f66ba1f666b5John McCall    ArgVec.push_back(Args[i].getArgument());
2430833ca991c1bfc967f0995974ca86f66ba1f666b5John McCall
243131f17ecbef57b5679c017c375db330546b7b5145John McCall  return getTemplateSpecializationType(Template, ArgVec.data(), NumArgs,
24323e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith                                       Underlying);
2433833ca991c1bfc967f0995974ca86f66ba1f666b5John McCall}
2434833ca991c1bfc967f0995974ca86f66ba1f666b5John McCall
2435833ca991c1bfc967f0995974ca86f66ba1f666b5John McCallQualType
2436833ca991c1bfc967f0995974ca86f66ba1f666b5John McCallASTContext::getTemplateSpecializationType(TemplateName Template,
24377532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor                                          const TemplateArgument *Args,
24387532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor                                          unsigned NumArgs,
24393e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith                                          QualType Underlying) const {
24407c3179cf463c3b3b8c21dbb955f933ba50b74f28Douglas Gregor  assert(!Template.getAsDependentTemplateName() &&
24417c3179cf463c3b3b8c21dbb955f933ba50b74f28Douglas Gregor         "No dependent template names here!");
24420f0ea2a96534c615ff5fdd81363989b23cf2164aDouglas Gregor  // Look through qualified template names.
24430f0ea2a96534c615ff5fdd81363989b23cf2164aDouglas Gregor  if (QualifiedTemplateName *QTN = Template.getAsQualifiedTemplateName())
24440f0ea2a96534c615ff5fdd81363989b23cf2164aDouglas Gregor    Template = TemplateName(QTN->getTemplateDecl());
24457c3179cf463c3b3b8c21dbb955f933ba50b74f28Douglas Gregor
24463e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith  bool isTypeAlias =
24473e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith    Template.getAsTemplateDecl() &&
24483e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith    isa<TypeAliasTemplateDecl>(Template.getAsTemplateDecl());
24493e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith
24503e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith  QualType CanonType;
24513e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith  if (!Underlying.isNull())
24523e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith    CanonType = getCanonicalType(Underlying);
24533e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith  else {
24543e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith    assert(!isTypeAlias &&
24553e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith           "Underlying type for template alias must be computed by caller");
24563e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith    CanonType = getCanonicalTemplateSpecializationType(Template, Args,
24573e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith                                                       NumArgs);
24583e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith  }
2459fc705b84347e6fb4746a1a7e26949f64c2f2f358Douglas Gregor
24601275ae098acda31fe0e434510c729fcfed0458a1Douglas Gregor  // Allocate the (non-canonical) template specialization type, but don't
24611275ae098acda31fe0e434510c729fcfed0458a1Douglas Gregor  // try to unique it: these types typically have location information that
24621275ae098acda31fe0e434510c729fcfed0458a1Douglas Gregor  // we don't unique and don't want to lose.
24633e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith  void *Mem = Allocate(sizeof(TemplateSpecializationType) +
24643e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith                       sizeof(TemplateArgument) * NumArgs +
24653e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith                       (isTypeAlias ? sizeof(QualType) : 0),
24666b304a0254a13f42390b865ff5ba668a49cc58aeJohn McCall                       TypeAlignment);
24671eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  TemplateSpecializationType *Spec
2468ef99001908e799c388f1363b1e607dad5f5b57d3John McCall    = new (Mem) TemplateSpecializationType(Template,
246931f17ecbef57b5679c017c375db330546b7b5145John McCall                                           Args, NumArgs,
24703e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith                                           CanonType,
24713e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith                                         isTypeAlias ? Underlying : QualType());
24721eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
247355f6b14230c94272efbbcdd89a92224c8db9f225Douglas Gregor  Types.push_back(Spec);
24741eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  return QualType(Spec, 0);
247555f6b14230c94272efbbcdd89a92224c8db9f225Douglas Gregor}
247655f6b14230c94272efbbcdd89a92224c8db9f225Douglas Gregor
24771eb4433ac451dc16f4133a88af2d002ac26c58efMike StumpQualType
24789763e221e16026ddf487d2564ed349d2c874a1a1Argyrios KyrtzidisASTContext::getCanonicalTemplateSpecializationType(TemplateName Template,
24799763e221e16026ddf487d2564ed349d2c874a1a1Argyrios Kyrtzidis                                                   const TemplateArgument *Args,
24804ba2a17694148e16eaa8d3917f657ffcd3667be4Jay Foad                                                   unsigned NumArgs) const {
24817c3179cf463c3b3b8c21dbb955f933ba50b74f28Douglas Gregor  assert(!Template.getAsDependentTemplateName() &&
24827c3179cf463c3b3b8c21dbb955f933ba50b74f28Douglas Gregor         "No dependent template names here!");
24833e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith  assert((!Template.getAsTemplateDecl() ||
24843e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith          !isa<TypeAliasTemplateDecl>(Template.getAsTemplateDecl())) &&
24853e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith         "Underlying type for template alias must be computed by caller");
24863e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith
24870f0ea2a96534c615ff5fdd81363989b23cf2164aDouglas Gregor  // Look through qualified template names.
24880f0ea2a96534c615ff5fdd81363989b23cf2164aDouglas Gregor  if (QualifiedTemplateName *QTN = Template.getAsQualifiedTemplateName())
24890f0ea2a96534c615ff5fdd81363989b23cf2164aDouglas Gregor    Template = TemplateName(QTN->getTemplateDecl());
24907c3179cf463c3b3b8c21dbb955f933ba50b74f28Douglas Gregor
24919763e221e16026ddf487d2564ed349d2c874a1a1Argyrios Kyrtzidis  // Build the canonical template specialization type.
24929763e221e16026ddf487d2564ed349d2c874a1a1Argyrios Kyrtzidis  TemplateName CanonTemplate = getCanonicalTemplateName(Template);
24935f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner  SmallVector<TemplateArgument, 4> CanonArgs;
24949763e221e16026ddf487d2564ed349d2c874a1a1Argyrios Kyrtzidis  CanonArgs.reserve(NumArgs);
24959763e221e16026ddf487d2564ed349d2c874a1a1Argyrios Kyrtzidis  for (unsigned I = 0; I != NumArgs; ++I)
24969763e221e16026ddf487d2564ed349d2c874a1a1Argyrios Kyrtzidis    CanonArgs.push_back(getCanonicalTemplateArgument(Args[I]));
24979763e221e16026ddf487d2564ed349d2c874a1a1Argyrios Kyrtzidis
24989763e221e16026ddf487d2564ed349d2c874a1a1Argyrios Kyrtzidis  // Determine whether this canonical template specialization type already
24999763e221e16026ddf487d2564ed349d2c874a1a1Argyrios Kyrtzidis  // exists.
25009763e221e16026ddf487d2564ed349d2c874a1a1Argyrios Kyrtzidis  llvm::FoldingSetNodeID ID;
25019763e221e16026ddf487d2564ed349d2c874a1a1Argyrios Kyrtzidis  TemplateSpecializationType::Profile(ID, CanonTemplate,
25029763e221e16026ddf487d2564ed349d2c874a1a1Argyrios Kyrtzidis                                      CanonArgs.data(), NumArgs, *this);
25039763e221e16026ddf487d2564ed349d2c874a1a1Argyrios Kyrtzidis
25049763e221e16026ddf487d2564ed349d2c874a1a1Argyrios Kyrtzidis  void *InsertPos = 0;
25059763e221e16026ddf487d2564ed349d2c874a1a1Argyrios Kyrtzidis  TemplateSpecializationType *Spec
25069763e221e16026ddf487d2564ed349d2c874a1a1Argyrios Kyrtzidis    = TemplateSpecializationTypes.FindNodeOrInsertPos(ID, InsertPos);
25079763e221e16026ddf487d2564ed349d2c874a1a1Argyrios Kyrtzidis
25089763e221e16026ddf487d2564ed349d2c874a1a1Argyrios Kyrtzidis  if (!Spec) {
25099763e221e16026ddf487d2564ed349d2c874a1a1Argyrios Kyrtzidis    // Allocate a new canonical template specialization type.
25109763e221e16026ddf487d2564ed349d2c874a1a1Argyrios Kyrtzidis    void *Mem = Allocate((sizeof(TemplateSpecializationType) +
25119763e221e16026ddf487d2564ed349d2c874a1a1Argyrios Kyrtzidis                          sizeof(TemplateArgument) * NumArgs),
25129763e221e16026ddf487d2564ed349d2c874a1a1Argyrios Kyrtzidis                         TypeAlignment);
25139763e221e16026ddf487d2564ed349d2c874a1a1Argyrios Kyrtzidis    Spec = new (Mem) TemplateSpecializationType(CanonTemplate,
25149763e221e16026ddf487d2564ed349d2c874a1a1Argyrios Kyrtzidis                                                CanonArgs.data(), NumArgs,
25153e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith                                                QualType(), QualType());
25169763e221e16026ddf487d2564ed349d2c874a1a1Argyrios Kyrtzidis    Types.push_back(Spec);
25179763e221e16026ddf487d2564ed349d2c874a1a1Argyrios Kyrtzidis    TemplateSpecializationTypes.InsertNode(Spec, InsertPos);
25189763e221e16026ddf487d2564ed349d2c874a1a1Argyrios Kyrtzidis  }
25199763e221e16026ddf487d2564ed349d2c874a1a1Argyrios Kyrtzidis
25209763e221e16026ddf487d2564ed349d2c874a1a1Argyrios Kyrtzidis  assert(Spec->isDependentType() &&
25219763e221e16026ddf487d2564ed349d2c874a1a1Argyrios Kyrtzidis         "Non-dependent template-id type must have a canonical type");
25229763e221e16026ddf487d2564ed349d2c874a1a1Argyrios Kyrtzidis  return QualType(Spec, 0);
25239763e221e16026ddf487d2564ed349d2c874a1a1Argyrios Kyrtzidis}
25249763e221e16026ddf487d2564ed349d2c874a1a1Argyrios Kyrtzidis
25259763e221e16026ddf487d2564ed349d2c874a1a1Argyrios KyrtzidisQualType
2526465d41b92b2c862f3062c412a0538db65c6a2661Abramo BagnaraASTContext::getElaboratedType(ElaboratedTypeKeyword Keyword,
2527465d41b92b2c862f3062c412a0538db65c6a2661Abramo Bagnara                              NestedNameSpecifier *NNS,
25284ba2a17694148e16eaa8d3917f657ffcd3667be4Jay Foad                              QualType NamedType) const {
2529e4e5b054b4917f0ee493bb2fda5b1ec749bfb9a1Douglas Gregor  llvm::FoldingSetNodeID ID;
2530465d41b92b2c862f3062c412a0538db65c6a2661Abramo Bagnara  ElaboratedType::Profile(ID, Keyword, NNS, NamedType);
2531e4e5b054b4917f0ee493bb2fda5b1ec749bfb9a1Douglas Gregor
2532e4e5b054b4917f0ee493bb2fda5b1ec749bfb9a1Douglas Gregor  void *InsertPos = 0;
2533465d41b92b2c862f3062c412a0538db65c6a2661Abramo Bagnara  ElaboratedType *T = ElaboratedTypes.FindNodeOrInsertPos(ID, InsertPos);
2534e4e5b054b4917f0ee493bb2fda5b1ec749bfb9a1Douglas Gregor  if (T)
2535e4e5b054b4917f0ee493bb2fda5b1ec749bfb9a1Douglas Gregor    return QualType(T, 0);
2536e4e5b054b4917f0ee493bb2fda5b1ec749bfb9a1Douglas Gregor
2537789b1f640205e81b5af250693246120f1ce9d147Douglas Gregor  QualType Canon = NamedType;
2538789b1f640205e81b5af250693246120f1ce9d147Douglas Gregor  if (!Canon.isCanonical()) {
2539789b1f640205e81b5af250693246120f1ce9d147Douglas Gregor    Canon = getCanonicalType(NamedType);
2540465d41b92b2c862f3062c412a0538db65c6a2661Abramo Bagnara    ElaboratedType *CheckT = ElaboratedTypes.FindNodeOrInsertPos(ID, InsertPos);
2541465d41b92b2c862f3062c412a0538db65c6a2661Abramo Bagnara    assert(!CheckT && "Elaborated canonical type broken");
2542789b1f640205e81b5af250693246120f1ce9d147Douglas Gregor    (void)CheckT;
2543789b1f640205e81b5af250693246120f1ce9d147Douglas Gregor  }
2544789b1f640205e81b5af250693246120f1ce9d147Douglas Gregor
2545465d41b92b2c862f3062c412a0538db65c6a2661Abramo Bagnara  T = new (*this) ElaboratedType(Keyword, NNS, NamedType, Canon);
2546e4e5b054b4917f0ee493bb2fda5b1ec749bfb9a1Douglas Gregor  Types.push_back(T);
2547465d41b92b2c862f3062c412a0538db65c6a2661Abramo Bagnara  ElaboratedTypes.InsertNode(T, InsertPos);
2548e4e5b054b4917f0ee493bb2fda5b1ec749bfb9a1Douglas Gregor  return QualType(T, 0);
2549e4e5b054b4917f0ee493bb2fda5b1ec749bfb9a1Douglas Gregor}
2550e4e5b054b4917f0ee493bb2fda5b1ec749bfb9a1Douglas Gregor
2551075f8f1b6bed4d1b224c74f87508534cc6392ce6Abramo BagnaraQualType
25524ba2a17694148e16eaa8d3917f657ffcd3667be4Jay FoadASTContext::getParenType(QualType InnerType) const {
2553075f8f1b6bed4d1b224c74f87508534cc6392ce6Abramo Bagnara  llvm::FoldingSetNodeID ID;
2554075f8f1b6bed4d1b224c74f87508534cc6392ce6Abramo Bagnara  ParenType::Profile(ID, InnerType);
2555075f8f1b6bed4d1b224c74f87508534cc6392ce6Abramo Bagnara
2556075f8f1b6bed4d1b224c74f87508534cc6392ce6Abramo Bagnara  void *InsertPos = 0;
2557075f8f1b6bed4d1b224c74f87508534cc6392ce6Abramo Bagnara  ParenType *T = ParenTypes.FindNodeOrInsertPos(ID, InsertPos);
2558075f8f1b6bed4d1b224c74f87508534cc6392ce6Abramo Bagnara  if (T)
2559075f8f1b6bed4d1b224c74f87508534cc6392ce6Abramo Bagnara    return QualType(T, 0);
2560075f8f1b6bed4d1b224c74f87508534cc6392ce6Abramo Bagnara
2561075f8f1b6bed4d1b224c74f87508534cc6392ce6Abramo Bagnara  QualType Canon = InnerType;
2562075f8f1b6bed4d1b224c74f87508534cc6392ce6Abramo Bagnara  if (!Canon.isCanonical()) {
2563075f8f1b6bed4d1b224c74f87508534cc6392ce6Abramo Bagnara    Canon = getCanonicalType(InnerType);
2564075f8f1b6bed4d1b224c74f87508534cc6392ce6Abramo Bagnara    ParenType *CheckT = ParenTypes.FindNodeOrInsertPos(ID, InsertPos);
2565075f8f1b6bed4d1b224c74f87508534cc6392ce6Abramo Bagnara    assert(!CheckT && "Paren canonical type broken");
2566075f8f1b6bed4d1b224c74f87508534cc6392ce6Abramo Bagnara    (void)CheckT;
2567075f8f1b6bed4d1b224c74f87508534cc6392ce6Abramo Bagnara  }
2568075f8f1b6bed4d1b224c74f87508534cc6392ce6Abramo Bagnara
2569075f8f1b6bed4d1b224c74f87508534cc6392ce6Abramo Bagnara  T = new (*this) ParenType(InnerType, Canon);
2570075f8f1b6bed4d1b224c74f87508534cc6392ce6Abramo Bagnara  Types.push_back(T);
2571075f8f1b6bed4d1b224c74f87508534cc6392ce6Abramo Bagnara  ParenTypes.InsertNode(T, InsertPos);
2572075f8f1b6bed4d1b224c74f87508534cc6392ce6Abramo Bagnara  return QualType(T, 0);
2573075f8f1b6bed4d1b224c74f87508534cc6392ce6Abramo Bagnara}
2574075f8f1b6bed4d1b224c74f87508534cc6392ce6Abramo Bagnara
25754a2023f5014e82389d5980d307b89c545dbbac81Douglas GregorQualType ASTContext::getDependentNameType(ElaboratedTypeKeyword Keyword,
25764a2023f5014e82389d5980d307b89c545dbbac81Douglas Gregor                                          NestedNameSpecifier *NNS,
25774a2023f5014e82389d5980d307b89c545dbbac81Douglas Gregor                                          const IdentifierInfo *Name,
25784ba2a17694148e16eaa8d3917f657ffcd3667be4Jay Foad                                          QualType Canon) const {
2579d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor  assert(NNS->isDependent() && "nested-name-specifier must be dependent");
2580d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor
2581d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor  if (Canon.isNull()) {
2582d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor    NestedNameSpecifier *CanonNNS = getCanonicalNestedNameSpecifier(NNS);
25834a2023f5014e82389d5980d307b89c545dbbac81Douglas Gregor    ElaboratedTypeKeyword CanonKeyword = Keyword;
25844a2023f5014e82389d5980d307b89c545dbbac81Douglas Gregor    if (Keyword == ETK_None)
25854a2023f5014e82389d5980d307b89c545dbbac81Douglas Gregor      CanonKeyword = ETK_Typename;
25864a2023f5014e82389d5980d307b89c545dbbac81Douglas Gregor
25874a2023f5014e82389d5980d307b89c545dbbac81Douglas Gregor    if (CanonNNS != NNS || CanonKeyword != Keyword)
25884a2023f5014e82389d5980d307b89c545dbbac81Douglas Gregor      Canon = getDependentNameType(CanonKeyword, CanonNNS, Name);
2589d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor  }
2590d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor
2591d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor  llvm::FoldingSetNodeID ID;
25924a2023f5014e82389d5980d307b89c545dbbac81Douglas Gregor  DependentNameType::Profile(ID, Keyword, NNS, Name);
2593d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor
2594d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor  void *InsertPos = 0;
25954714c12a1ab759156b78be8f109ea4c12213af57Douglas Gregor  DependentNameType *T
25964714c12a1ab759156b78be8f109ea4c12213af57Douglas Gregor    = DependentNameTypes.FindNodeOrInsertPos(ID, InsertPos);
2597d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor  if (T)
2598d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor    return QualType(T, 0);
2599d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor
26004a2023f5014e82389d5980d307b89c545dbbac81Douglas Gregor  T = new (*this) DependentNameType(Keyword, NNS, Name, Canon);
2601d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor  Types.push_back(T);
26024714c12a1ab759156b78be8f109ea4c12213af57Douglas Gregor  DependentNameTypes.InsertNode(T, InsertPos);
26031eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  return QualType(T, 0);
2604d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor}
2605d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor
26061eb4433ac451dc16f4133a88af2d002ac26c58efMike StumpQualType
260733500955d731c73717af52088b7fc0e7a85681e7John McCallASTContext::getDependentTemplateSpecializationType(
260833500955d731c73717af52088b7fc0e7a85681e7John McCall                                 ElaboratedTypeKeyword Keyword,
26094a2023f5014e82389d5980d307b89c545dbbac81Douglas Gregor                                 NestedNameSpecifier *NNS,
261033500955d731c73717af52088b7fc0e7a85681e7John McCall                                 const IdentifierInfo *Name,
26114ba2a17694148e16eaa8d3917f657ffcd3667be4Jay Foad                                 const TemplateArgumentListInfo &Args) const {
261233500955d731c73717af52088b7fc0e7a85681e7John McCall  // TODO: avoid this copy
26135f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner  SmallVector<TemplateArgument, 16> ArgCopy;
261433500955d731c73717af52088b7fc0e7a85681e7John McCall  for (unsigned I = 0, E = Args.size(); I != E; ++I)
261533500955d731c73717af52088b7fc0e7a85681e7John McCall    ArgCopy.push_back(Args[I].getArgument());
261633500955d731c73717af52088b7fc0e7a85681e7John McCall  return getDependentTemplateSpecializationType(Keyword, NNS, Name,
261733500955d731c73717af52088b7fc0e7a85681e7John McCall                                                ArgCopy.size(),
261833500955d731c73717af52088b7fc0e7a85681e7John McCall                                                ArgCopy.data());
261933500955d731c73717af52088b7fc0e7a85681e7John McCall}
262033500955d731c73717af52088b7fc0e7a85681e7John McCall
262133500955d731c73717af52088b7fc0e7a85681e7John McCallQualType
262233500955d731c73717af52088b7fc0e7a85681e7John McCallASTContext::getDependentTemplateSpecializationType(
262333500955d731c73717af52088b7fc0e7a85681e7John McCall                                 ElaboratedTypeKeyword Keyword,
262433500955d731c73717af52088b7fc0e7a85681e7John McCall                                 NestedNameSpecifier *NNS,
262533500955d731c73717af52088b7fc0e7a85681e7John McCall                                 const IdentifierInfo *Name,
262633500955d731c73717af52088b7fc0e7a85681e7John McCall                                 unsigned NumArgs,
26274ba2a17694148e16eaa8d3917f657ffcd3667be4Jay Foad                                 const TemplateArgument *Args) const {
2628aa2187de137e5b809dcbbe14f3b61ae907a3d8aaDouglas Gregor  assert((!NNS || NNS->isDependent()) &&
2629aa2187de137e5b809dcbbe14f3b61ae907a3d8aaDouglas Gregor         "nested-name-specifier must be dependent");
26301734317845d60307d474b5da8a8d33adbaf5e723Douglas Gregor
2631789b1f640205e81b5af250693246120f1ce9d147Douglas Gregor  llvm::FoldingSetNodeID ID;
263233500955d731c73717af52088b7fc0e7a85681e7John McCall  DependentTemplateSpecializationType::Profile(ID, *this, Keyword, NNS,
263333500955d731c73717af52088b7fc0e7a85681e7John McCall                                               Name, NumArgs, Args);
2634789b1f640205e81b5af250693246120f1ce9d147Douglas Gregor
2635789b1f640205e81b5af250693246120f1ce9d147Douglas Gregor  void *InsertPos = 0;
263633500955d731c73717af52088b7fc0e7a85681e7John McCall  DependentTemplateSpecializationType *T
263733500955d731c73717af52088b7fc0e7a85681e7John McCall    = DependentTemplateSpecializationTypes.FindNodeOrInsertPos(ID, InsertPos);
2638789b1f640205e81b5af250693246120f1ce9d147Douglas Gregor  if (T)
2639789b1f640205e81b5af250693246120f1ce9d147Douglas Gregor    return QualType(T, 0);
2640789b1f640205e81b5af250693246120f1ce9d147Douglas Gregor
264133500955d731c73717af52088b7fc0e7a85681e7John McCall  NestedNameSpecifier *CanonNNS = getCanonicalNestedNameSpecifier(NNS);
264233500955d731c73717af52088b7fc0e7a85681e7John McCall
264333500955d731c73717af52088b7fc0e7a85681e7John McCall  ElaboratedTypeKeyword CanonKeyword = Keyword;
264433500955d731c73717af52088b7fc0e7a85681e7John McCall  if (Keyword == ETK_None) CanonKeyword = ETK_Typename;
26451734317845d60307d474b5da8a8d33adbaf5e723Douglas Gregor
264633500955d731c73717af52088b7fc0e7a85681e7John McCall  bool AnyNonCanonArgs = false;
26475f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner  SmallVector<TemplateArgument, 16> CanonArgs(NumArgs);
264833500955d731c73717af52088b7fc0e7a85681e7John McCall  for (unsigned I = 0; I != NumArgs; ++I) {
264933500955d731c73717af52088b7fc0e7a85681e7John McCall    CanonArgs[I] = getCanonicalTemplateArgument(Args[I]);
265033500955d731c73717af52088b7fc0e7a85681e7John McCall    if (!CanonArgs[I].structurallyEquals(Args[I]))
265133500955d731c73717af52088b7fc0e7a85681e7John McCall      AnyNonCanonArgs = true;
2652789b1f640205e81b5af250693246120f1ce9d147Douglas Gregor  }
26531734317845d60307d474b5da8a8d33adbaf5e723Douglas Gregor
265433500955d731c73717af52088b7fc0e7a85681e7John McCall  QualType Canon;
265533500955d731c73717af52088b7fc0e7a85681e7John McCall  if (AnyNonCanonArgs || CanonNNS != NNS || CanonKeyword != Keyword) {
265633500955d731c73717af52088b7fc0e7a85681e7John McCall    Canon = getDependentTemplateSpecializationType(CanonKeyword, CanonNNS,
265733500955d731c73717af52088b7fc0e7a85681e7John McCall                                                   Name, NumArgs,
265833500955d731c73717af52088b7fc0e7a85681e7John McCall                                                   CanonArgs.data());
265933500955d731c73717af52088b7fc0e7a85681e7John McCall
266033500955d731c73717af52088b7fc0e7a85681e7John McCall    // Find the insert position again.
266133500955d731c73717af52088b7fc0e7a85681e7John McCall    DependentTemplateSpecializationTypes.FindNodeOrInsertPos(ID, InsertPos);
266233500955d731c73717af52088b7fc0e7a85681e7John McCall  }
266333500955d731c73717af52088b7fc0e7a85681e7John McCall
266433500955d731c73717af52088b7fc0e7a85681e7John McCall  void *Mem = Allocate((sizeof(DependentTemplateSpecializationType) +
266533500955d731c73717af52088b7fc0e7a85681e7John McCall                        sizeof(TemplateArgument) * NumArgs),
266633500955d731c73717af52088b7fc0e7a85681e7John McCall                       TypeAlignment);
2667ef99001908e799c388f1363b1e607dad5f5b57d3John McCall  T = new (Mem) DependentTemplateSpecializationType(Keyword, NNS,
266833500955d731c73717af52088b7fc0e7a85681e7John McCall                                                    Name, NumArgs, Args, Canon);
26691734317845d60307d474b5da8a8d33adbaf5e723Douglas Gregor  Types.push_back(T);
267033500955d731c73717af52088b7fc0e7a85681e7John McCall  DependentTemplateSpecializationTypes.InsertNode(T, InsertPos);
26711eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  return QualType(T, 0);
26721734317845d60307d474b5da8a8d33adbaf5e723Douglas Gregor}
26731734317845d60307d474b5da8a8d33adbaf5e723Douglas Gregor
2674cded4f649cd4b7ba7d461c25c6482ef52b8d3a2aDouglas GregorQualType ASTContext::getPackExpansionType(QualType Pattern,
2675cded4f649cd4b7ba7d461c25c6482ef52b8d3a2aDouglas Gregor                                      llvm::Optional<unsigned> NumExpansions) {
26767536dd5e6c99584481b7dab68b7e7d8df9c54054Douglas Gregor  llvm::FoldingSetNodeID ID;
2677cded4f649cd4b7ba7d461c25c6482ef52b8d3a2aDouglas Gregor  PackExpansionType::Profile(ID, Pattern, NumExpansions);
26787536dd5e6c99584481b7dab68b7e7d8df9c54054Douglas Gregor
26797536dd5e6c99584481b7dab68b7e7d8df9c54054Douglas Gregor  assert(Pattern->containsUnexpandedParameterPack() &&
26807536dd5e6c99584481b7dab68b7e7d8df9c54054Douglas Gregor         "Pack expansions must expand one or more parameter packs");
26817536dd5e6c99584481b7dab68b7e7d8df9c54054Douglas Gregor  void *InsertPos = 0;
26827536dd5e6c99584481b7dab68b7e7d8df9c54054Douglas Gregor  PackExpansionType *T
26837536dd5e6c99584481b7dab68b7e7d8df9c54054Douglas Gregor    = PackExpansionTypes.FindNodeOrInsertPos(ID, InsertPos);
26847536dd5e6c99584481b7dab68b7e7d8df9c54054Douglas Gregor  if (T)
26857536dd5e6c99584481b7dab68b7e7d8df9c54054Douglas Gregor    return QualType(T, 0);
26867536dd5e6c99584481b7dab68b7e7d8df9c54054Douglas Gregor
26877536dd5e6c99584481b7dab68b7e7d8df9c54054Douglas Gregor  QualType Canon;
26887536dd5e6c99584481b7dab68b7e7d8df9c54054Douglas Gregor  if (!Pattern.isCanonical()) {
2689cded4f649cd4b7ba7d461c25c6482ef52b8d3a2aDouglas Gregor    Canon = getPackExpansionType(getCanonicalType(Pattern), NumExpansions);
26907536dd5e6c99584481b7dab68b7e7d8df9c54054Douglas Gregor
26917536dd5e6c99584481b7dab68b7e7d8df9c54054Douglas Gregor    // Find the insert position again.
26927536dd5e6c99584481b7dab68b7e7d8df9c54054Douglas Gregor    PackExpansionTypes.FindNodeOrInsertPos(ID, InsertPos);
26937536dd5e6c99584481b7dab68b7e7d8df9c54054Douglas Gregor  }
26947536dd5e6c99584481b7dab68b7e7d8df9c54054Douglas Gregor
2695cded4f649cd4b7ba7d461c25c6482ef52b8d3a2aDouglas Gregor  T = new (*this) PackExpansionType(Pattern, Canon, NumExpansions);
26967536dd5e6c99584481b7dab68b7e7d8df9c54054Douglas Gregor  Types.push_back(T);
26977536dd5e6c99584481b7dab68b7e7d8df9c54054Douglas Gregor  PackExpansionTypes.InsertNode(T, InsertPos);
26987536dd5e6c99584481b7dab68b7e7d8df9c54054Douglas Gregor  return QualType(T, 0);
26997536dd5e6c99584481b7dab68b7e7d8df9c54054Douglas Gregor}
27007536dd5e6c99584481b7dab68b7e7d8df9c54054Douglas Gregor
270188cb27a160adc305783a44f922ee4b216006ebf9Chris Lattner/// CmpProtocolNames - Comparison predicate for sorting protocols
270288cb27a160adc305783a44f922ee4b216006ebf9Chris Lattner/// alphabetically.
270388cb27a160adc305783a44f922ee4b216006ebf9Chris Lattnerstatic bool CmpProtocolNames(const ObjCProtocolDecl *LHS,
270488cb27a160adc305783a44f922ee4b216006ebf9Chris Lattner                            const ObjCProtocolDecl *RHS) {
27052e1cd4264d363ca869bf37ef160902f211d21b8cDouglas Gregor  return LHS->getDeclName() < RHS->getDeclName();
270688cb27a160adc305783a44f922ee4b216006ebf9Chris Lattner}
270788cb27a160adc305783a44f922ee4b216006ebf9Chris Lattner
2708c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCallstatic bool areSortedAndUniqued(ObjCProtocolDecl * const *Protocols,
270954e14c4db764c0636160d26c5bbf491637c83a76John McCall                                unsigned NumProtocols) {
271054e14c4db764c0636160d26c5bbf491637c83a76John McCall  if (NumProtocols == 0) return true;
271154e14c4db764c0636160d26c5bbf491637c83a76John McCall
271261cc296de6c1f82fa84c0abb3ecd142a584838efDouglas Gregor  if (Protocols[0]->getCanonicalDecl() != Protocols[0])
271361cc296de6c1f82fa84c0abb3ecd142a584838efDouglas Gregor    return false;
271461cc296de6c1f82fa84c0abb3ecd142a584838efDouglas Gregor
271554e14c4db764c0636160d26c5bbf491637c83a76John McCall  for (unsigned i = 1; i != NumProtocols; ++i)
271661cc296de6c1f82fa84c0abb3ecd142a584838efDouglas Gregor    if (!CmpProtocolNames(Protocols[i-1], Protocols[i]) ||
271761cc296de6c1f82fa84c0abb3ecd142a584838efDouglas Gregor        Protocols[i]->getCanonicalDecl() != Protocols[i])
271854e14c4db764c0636160d26c5bbf491637c83a76John McCall      return false;
271954e14c4db764c0636160d26c5bbf491637c83a76John McCall  return true;
272054e14c4db764c0636160d26c5bbf491637c83a76John McCall}
272154e14c4db764c0636160d26c5bbf491637c83a76John McCall
272254e14c4db764c0636160d26c5bbf491637c83a76John McCallstatic void SortAndUniqueProtocols(ObjCProtocolDecl **Protocols,
272388cb27a160adc305783a44f922ee4b216006ebf9Chris Lattner                                   unsigned &NumProtocols) {
272488cb27a160adc305783a44f922ee4b216006ebf9Chris Lattner  ObjCProtocolDecl **ProtocolsEnd = Protocols+NumProtocols;
27251eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
272688cb27a160adc305783a44f922ee4b216006ebf9Chris Lattner  // Sort protocols, keyed by name.
272788cb27a160adc305783a44f922ee4b216006ebf9Chris Lattner  std::sort(Protocols, Protocols+NumProtocols, CmpProtocolNames);
272888cb27a160adc305783a44f922ee4b216006ebf9Chris Lattner
272961cc296de6c1f82fa84c0abb3ecd142a584838efDouglas Gregor  // Canonicalize.
273061cc296de6c1f82fa84c0abb3ecd142a584838efDouglas Gregor  for (unsigned I = 0, N = NumProtocols; I != N; ++I)
273161cc296de6c1f82fa84c0abb3ecd142a584838efDouglas Gregor    Protocols[I] = Protocols[I]->getCanonicalDecl();
273261cc296de6c1f82fa84c0abb3ecd142a584838efDouglas Gregor
273388cb27a160adc305783a44f922ee4b216006ebf9Chris Lattner  // Remove duplicates.
273488cb27a160adc305783a44f922ee4b216006ebf9Chris Lattner  ProtocolsEnd = std::unique(Protocols, ProtocolsEnd);
273588cb27a160adc305783a44f922ee4b216006ebf9Chris Lattner  NumProtocols = ProtocolsEnd-Protocols;
273688cb27a160adc305783a44f922ee4b216006ebf9Chris Lattner}
273788cb27a160adc305783a44f922ee4b216006ebf9Chris Lattner
2738c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCallQualType ASTContext::getObjCObjectType(QualType BaseType,
2739c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall                                       ObjCProtocolDecl * const *Protocols,
27404ba2a17694148e16eaa8d3917f657ffcd3667be4Jay Foad                                       unsigned NumProtocols) const {
2741c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall  // If the base type is an interface and there aren't any protocols
2742c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall  // to add, then the interface type will do just fine.
2743c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall  if (!NumProtocols && isa<ObjCInterfaceType>(BaseType))
2744c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall    return BaseType;
2745d1b3c2dd5bc1f3103bee6137957aa7c5f8f2f0bcSteve Naroff
2746c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall  // Look in the folding set for an existing type.
2747c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall  llvm::FoldingSetNodeID ID;
2748c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall  ObjCObjectTypeImpl::Profile(ID, BaseType, Protocols, NumProtocols);
2749d1b3c2dd5bc1f3103bee6137957aa7c5f8f2f0bcSteve Naroff  void *InsertPos = 0;
2750c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall  if (ObjCObjectType *QT = ObjCObjectTypes.FindNodeOrInsertPos(ID, InsertPos))
2751c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall    return QualType(QT, 0);
2752d1b3c2dd5bc1f3103bee6137957aa7c5f8f2f0bcSteve Naroff
2753c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall  // Build the canonical type, which has the canonical base type and
2754c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall  // a sorted-and-uniqued list of protocols.
275554e14c4db764c0636160d26c5bbf491637c83a76John McCall  QualType Canonical;
2756c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall  bool ProtocolsSorted = areSortedAndUniqued(Protocols, NumProtocols);
2757c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall  if (!ProtocolsSorted || !BaseType.isCanonical()) {
2758c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall    if (!ProtocolsSorted) {
27595f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner      SmallVector<ObjCProtocolDecl*, 8> Sorted(Protocols,
27600237941e0beb0c929934b66ad29443b484d987feBenjamin Kramer                                                     Protocols + NumProtocols);
276154e14c4db764c0636160d26c5bbf491637c83a76John McCall      unsigned UniqueCount = NumProtocols;
276254e14c4db764c0636160d26c5bbf491637c83a76John McCall
276354e14c4db764c0636160d26c5bbf491637c83a76John McCall      SortAndUniqueProtocols(&Sorted[0], UniqueCount);
2764c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall      Canonical = getObjCObjectType(getCanonicalType(BaseType),
2765c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall                                    &Sorted[0], UniqueCount);
276654e14c4db764c0636160d26c5bbf491637c83a76John McCall    } else {
2767c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall      Canonical = getObjCObjectType(getCanonicalType(BaseType),
2768c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall                                    Protocols, NumProtocols);
276954e14c4db764c0636160d26c5bbf491637c83a76John McCall    }
277054e14c4db764c0636160d26c5bbf491637c83a76John McCall
277154e14c4db764c0636160d26c5bbf491637c83a76John McCall    // Regenerate InsertPos.
2772c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall    ObjCObjectTypes.FindNodeOrInsertPos(ID, InsertPos);
277354e14c4db764c0636160d26c5bbf491637c83a76John McCall  }
277454e14c4db764c0636160d26c5bbf491637c83a76John McCall
2775c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall  unsigned Size = sizeof(ObjCObjectTypeImpl);
2776c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall  Size += NumProtocols * sizeof(ObjCProtocolDecl *);
2777fd6a0887a099256c35a5b23e9afd517ffe95fa0aDouglas Gregor  void *Mem = Allocate(Size, TypeAlignment);
2778c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall  ObjCObjectTypeImpl *T =
2779c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall    new (Mem) ObjCObjectTypeImpl(Canonical, BaseType, Protocols, NumProtocols);
27801eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
2781c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall  Types.push_back(T);
2782c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall  ObjCObjectTypes.InsertNode(T, InsertPos);
2783c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall  return QualType(T, 0);
2784d1b3c2dd5bc1f3103bee6137957aa7c5f8f2f0bcSteve Naroff}
278588cb27a160adc305783a44f922ee4b216006ebf9Chris Lattner
2786c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall/// getObjCObjectPointerType - Return a ObjCObjectPointerType type for
2787c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall/// the given object type.
27884ba2a17694148e16eaa8d3917f657ffcd3667be4Jay FoadQualType ASTContext::getObjCObjectPointerType(QualType ObjectT) const {
27894b6c9051c6522894978c9ba6a819a659d102db36Fariborz Jahanian  llvm::FoldingSetNodeID ID;
2790c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall  ObjCObjectPointerType::Profile(ID, ObjectT);
27911eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
27924b6c9051c6522894978c9ba6a819a659d102db36Fariborz Jahanian  void *InsertPos = 0;
2793c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall  if (ObjCObjectPointerType *QT =
2794c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall              ObjCObjectPointerTypes.FindNodeOrInsertPos(ID, InsertPos))
27954b6c9051c6522894978c9ba6a819a659d102db36Fariborz Jahanian    return QualType(QT, 0);
27961eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
2797c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall  // Find the canonical object type.
279854e14c4db764c0636160d26c5bbf491637c83a76John McCall  QualType Canonical;
2799c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall  if (!ObjectT.isCanonical()) {
2800c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall    Canonical = getObjCObjectPointerType(getCanonicalType(ObjectT));
280154e14c4db764c0636160d26c5bbf491637c83a76John McCall
2802c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall    // Regenerate InsertPos.
2803c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall    ObjCObjectPointerTypes.FindNodeOrInsertPos(ID, InsertPos);
280454e14c4db764c0636160d26c5bbf491637c83a76John McCall  }
280554e14c4db764c0636160d26c5bbf491637c83a76John McCall
2806c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall  // No match.
2807c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall  void *Mem = Allocate(sizeof(ObjCObjectPointerType), TypeAlignment);
2808c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall  ObjCObjectPointerType *QType =
2809c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall    new (Mem) ObjCObjectPointerType(Canonical, ObjectT);
281024fab41057e4b67ed69a6b4027d5ae0f2f6934dcArgyrios Kyrtzidis
281124fab41057e4b67ed69a6b4027d5ae0f2f6934dcArgyrios Kyrtzidis  Types.push_back(QType);
2812c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall  ObjCObjectPointerTypes.InsertNode(QType, InsertPos);
281324fab41057e4b67ed69a6b4027d5ae0f2f6934dcArgyrios Kyrtzidis  return QualType(QType, 0);
281424fab41057e4b67ed69a6b4027d5ae0f2f6934dcArgyrios Kyrtzidis}
281524fab41057e4b67ed69a6b4027d5ae0f2f6934dcArgyrios Kyrtzidis
2816deacbdca554298ccdf636f19c6094a8825ec6b34Douglas Gregor/// getObjCInterfaceType - Return the unique reference to the type for the
2817deacbdca554298ccdf636f19c6094a8825ec6b34Douglas Gregor/// specified ObjC interface decl. The list of protocols is optional.
28180af550115df1f57f17a4f125ff0e8b34820c65d1Douglas GregorQualType ASTContext::getObjCInterfaceType(const ObjCInterfaceDecl *Decl,
28190af550115df1f57f17a4f125ff0e8b34820c65d1Douglas Gregor                                          ObjCInterfaceDecl *PrevDecl) const {
2820deacbdca554298ccdf636f19c6094a8825ec6b34Douglas Gregor  if (Decl->TypeForDecl)
2821deacbdca554298ccdf636f19c6094a8825ec6b34Douglas Gregor    return QualType(Decl->TypeForDecl, 0);
282274c730ad1f6818b676b0bad46d806a9176950328Sebastian Redl
28230af550115df1f57f17a4f125ff0e8b34820c65d1Douglas Gregor  if (PrevDecl) {
28240af550115df1f57f17a4f125ff0e8b34820c65d1Douglas Gregor    assert(PrevDecl->TypeForDecl && "previous decl has no TypeForDecl");
28250af550115df1f57f17a4f125ff0e8b34820c65d1Douglas Gregor    Decl->TypeForDecl = PrevDecl->TypeForDecl;
28260af550115df1f57f17a4f125ff0e8b34820c65d1Douglas Gregor    return QualType(PrevDecl->TypeForDecl, 0);
28270af550115df1f57f17a4f125ff0e8b34820c65d1Douglas Gregor  }
28280af550115df1f57f17a4f125ff0e8b34820c65d1Douglas Gregor
28298d2dbbf9ddfd9d762a341d83f83d840ff68ce03dDouglas Gregor  // Prefer the definition, if there is one.
28308d2dbbf9ddfd9d762a341d83f83d840ff68ce03dDouglas Gregor  if (const ObjCInterfaceDecl *Def = Decl->getDefinition())
28318d2dbbf9ddfd9d762a341d83f83d840ff68ce03dDouglas Gregor    Decl = Def;
28328d2dbbf9ddfd9d762a341d83f83d840ff68ce03dDouglas Gregor
2833deacbdca554298ccdf636f19c6094a8825ec6b34Douglas Gregor  void *Mem = Allocate(sizeof(ObjCInterfaceType), TypeAlignment);
2834deacbdca554298ccdf636f19c6094a8825ec6b34Douglas Gregor  ObjCInterfaceType *T = new (Mem) ObjCInterfaceType(Decl);
2835deacbdca554298ccdf636f19c6094a8825ec6b34Douglas Gregor  Decl->TypeForDecl = T;
2836deacbdca554298ccdf636f19c6094a8825ec6b34Douglas Gregor  Types.push_back(T);
2837deacbdca554298ccdf636f19c6094a8825ec6b34Douglas Gregor  return QualType(T, 0);
2838c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall}
2839c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall
284072564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor/// getTypeOfExprType - Unlike many "get<Type>" functions, we can't unique
284172564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor/// TypeOfExprType AST's (since expression's are never shared). For example,
28429752f25748d954df99087d741ea35db37ff16beaSteve Naroff/// multiple declarations that refer to "typeof(x)" all contain different
28431eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// DeclRefExpr's. This doesn't effect the type checker, since it operates
28449752f25748d954df99087d741ea35db37ff16beaSteve Naroff/// on canonical type's (which are always unique).
28454ba2a17694148e16eaa8d3917f657ffcd3667be4Jay FoadQualType ASTContext::getTypeOfExprType(Expr *tofExpr) const {
2846dd0257c77719a13d4acd513df40b04300cbfc871Douglas Gregor  TypeOfExprType *toe;
2847b197572cf1cd70a817a1f546478cb2cb9112c48eDouglas Gregor  if (tofExpr->isTypeDependent()) {
2848b197572cf1cd70a817a1f546478cb2cb9112c48eDouglas Gregor    llvm::FoldingSetNodeID ID;
2849b197572cf1cd70a817a1f546478cb2cb9112c48eDouglas Gregor    DependentTypeOfExprType::Profile(ID, *this, tofExpr);
28501eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
2851b197572cf1cd70a817a1f546478cb2cb9112c48eDouglas Gregor    void *InsertPos = 0;
2852b197572cf1cd70a817a1f546478cb2cb9112c48eDouglas Gregor    DependentTypeOfExprType *Canon
2853b197572cf1cd70a817a1f546478cb2cb9112c48eDouglas Gregor      = DependentTypeOfExprTypes.FindNodeOrInsertPos(ID, InsertPos);
2854b197572cf1cd70a817a1f546478cb2cb9112c48eDouglas Gregor    if (Canon) {
2855b197572cf1cd70a817a1f546478cb2cb9112c48eDouglas Gregor      // We already have a "canonical" version of an identical, dependent
2856b197572cf1cd70a817a1f546478cb2cb9112c48eDouglas Gregor      // typeof(expr) type. Use that as our canonical type.
28576b304a0254a13f42390b865ff5ba668a49cc58aeJohn McCall      toe = new (*this, TypeAlignment) TypeOfExprType(tofExpr,
2858b197572cf1cd70a817a1f546478cb2cb9112c48eDouglas Gregor                                          QualType((TypeOfExprType*)Canon, 0));
28593060178ad9df29789505c1e6debcfc80a3a13587Chad Rosier    } else {
2860b197572cf1cd70a817a1f546478cb2cb9112c48eDouglas Gregor      // Build a new, canonical typeof(expr) type.
28616b304a0254a13f42390b865ff5ba668a49cc58aeJohn McCall      Canon
28626b304a0254a13f42390b865ff5ba668a49cc58aeJohn McCall        = new (*this, TypeAlignment) DependentTypeOfExprType(*this, tofExpr);
2863b197572cf1cd70a817a1f546478cb2cb9112c48eDouglas Gregor      DependentTypeOfExprTypes.InsertNode(Canon, InsertPos);
2864b197572cf1cd70a817a1f546478cb2cb9112c48eDouglas Gregor      toe = Canon;
2865b197572cf1cd70a817a1f546478cb2cb9112c48eDouglas Gregor    }
2866b197572cf1cd70a817a1f546478cb2cb9112c48eDouglas Gregor  } else {
2867dd0257c77719a13d4acd513df40b04300cbfc871Douglas Gregor    QualType Canonical = getCanonicalType(tofExpr->getType());
28686b304a0254a13f42390b865ff5ba668a49cc58aeJohn McCall    toe = new (*this, TypeAlignment) TypeOfExprType(tofExpr, Canonical);
2869dd0257c77719a13d4acd513df40b04300cbfc871Douglas Gregor  }
28709752f25748d954df99087d741ea35db37ff16beaSteve Naroff  Types.push_back(toe);
28719752f25748d954df99087d741ea35db37ff16beaSteve Naroff  return QualType(toe, 0);
2872d1861fd633d5096a00777c918eb8575ea7162fe7Steve Naroff}
2873d1861fd633d5096a00777c918eb8575ea7162fe7Steve Naroff
28749752f25748d954df99087d741ea35db37ff16beaSteve Naroff/// getTypeOfType -  Unlike many "get<Type>" functions, we don't unique
28759752f25748d954df99087d741ea35db37ff16beaSteve Naroff/// TypeOfType AST's. The only motivation to unique these nodes would be
28769752f25748d954df99087d741ea35db37ff16beaSteve Naroff/// memory savings. Since typeof(t) is fairly uncommon, space shouldn't be
28771eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// an issue. This doesn't effect the type checker, since it operates
28789752f25748d954df99087d741ea35db37ff16beaSteve Naroff/// on canonical type's (which are always unique).
28794ba2a17694148e16eaa8d3917f657ffcd3667be4Jay FoadQualType ASTContext::getTypeOfType(QualType tofType) const {
2880f52ab250ff92bc51a9ac9a8e19bd43b63a5f844fChris Lattner  QualType Canonical = getCanonicalType(tofType);
28816b304a0254a13f42390b865ff5ba668a49cc58aeJohn McCall  TypeOfType *tot = new (*this, TypeAlignment) TypeOfType(tofType, Canonical);
28829752f25748d954df99087d741ea35db37ff16beaSteve Naroff  Types.push_back(tot);
28839752f25748d954df99087d741ea35db37ff16beaSteve Naroff  return QualType(tot, 0);
2884d1861fd633d5096a00777c918eb8575ea7162fe7Steve Naroff}
2885d1861fd633d5096a00777c918eb8575ea7162fe7Steve Naroff
288660a9a2a404a4cf259d39133383e922aa00ca9043Anders Carlsson/// getDecltypeForExpr - Given an expr, will return the decltype for that
288760a9a2a404a4cf259d39133383e922aa00ca9043Anders Carlsson/// expression, according to the rules in C++0x [dcl.type.simple]p4
28884ba2a17694148e16eaa8d3917f657ffcd3667be4Jay Foadstatic QualType getDecltypeForExpr(const Expr *e, const ASTContext &Context) {
2889a07c33e64e1169e4261f7748c7f9191091a3ad2eAnders Carlsson  if (e->isTypeDependent())
2890a07c33e64e1169e4261f7748c7f9191091a3ad2eAnders Carlsson    return Context.DependentTy;
28911eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
289260a9a2a404a4cf259d39133383e922aa00ca9043Anders Carlsson  // If e is an id expression or a class member access, decltype(e) is defined
289360a9a2a404a4cf259d39133383e922aa00ca9043Anders Carlsson  // as the type of the entity named by e.
289460a9a2a404a4cf259d39133383e922aa00ca9043Anders Carlsson  if (const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(e)) {
289560a9a2a404a4cf259d39133383e922aa00ca9043Anders Carlsson    if (const ValueDecl *VD = dyn_cast<ValueDecl>(DRE->getDecl()))
289660a9a2a404a4cf259d39133383e922aa00ca9043Anders Carlsson      return VD->getType();
289760a9a2a404a4cf259d39133383e922aa00ca9043Anders Carlsson  }
289860a9a2a404a4cf259d39133383e922aa00ca9043Anders Carlsson  if (const MemberExpr *ME = dyn_cast<MemberExpr>(e)) {
289960a9a2a404a4cf259d39133383e922aa00ca9043Anders Carlsson    if (const FieldDecl *FD = dyn_cast<FieldDecl>(ME->getMemberDecl()))
290060a9a2a404a4cf259d39133383e922aa00ca9043Anders Carlsson      return FD->getType();
290160a9a2a404a4cf259d39133383e922aa00ca9043Anders Carlsson  }
290260a9a2a404a4cf259d39133383e922aa00ca9043Anders Carlsson  // If e is a function call or an invocation of an overloaded operator,
290360a9a2a404a4cf259d39133383e922aa00ca9043Anders Carlsson  // (parentheses around e are ignored), decltype(e) is defined as the
290460a9a2a404a4cf259d39133383e922aa00ca9043Anders Carlsson  // return type of that function.
290560a9a2a404a4cf259d39133383e922aa00ca9043Anders Carlsson  if (const CallExpr *CE = dyn_cast<CallExpr>(e->IgnoreParens()))
290660a9a2a404a4cf259d39133383e922aa00ca9043Anders Carlsson    return CE->getCallReturnType();
29071eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
290860a9a2a404a4cf259d39133383e922aa00ca9043Anders Carlsson  QualType T = e->getType();
29091eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
29101eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  // Otherwise, where T is the type of e, if e is an lvalue, decltype(e) is
291160a9a2a404a4cf259d39133383e922aa00ca9043Anders Carlsson  // defined as T&, otherwise decltype(e) is defined as T.
29127eb0a9eb0cde8444b97f9c5b713d9be7a6f1e607John McCall  if (e->isLValue())
291360a9a2a404a4cf259d39133383e922aa00ca9043Anders Carlsson    T = Context.getLValueReferenceType(T);
29141eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
291560a9a2a404a4cf259d39133383e922aa00ca9043Anders Carlsson  return T;
291660a9a2a404a4cf259d39133383e922aa00ca9043Anders Carlsson}
291760a9a2a404a4cf259d39133383e922aa00ca9043Anders Carlsson
2918395b475a4474f1c7574d927ad142ca0c7997cbcaAnders Carlsson/// getDecltypeType -  Unlike many "get<Type>" functions, we don't unique
2919395b475a4474f1c7574d927ad142ca0c7997cbcaAnders Carlsson/// DecltypeType AST's. The only motivation to unique these nodes would be
2920395b475a4474f1c7574d927ad142ca0c7997cbcaAnders Carlsson/// memory savings. Since decltype(t) is fairly uncommon, space shouldn't be
29211eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// an issue. This doesn't effect the type checker, since it operates
292239e02032b01874a0d02ba85a4cd3922adda81376David Blaikie/// on canonical types (which are always unique).
29234ba2a17694148e16eaa8d3917f657ffcd3667be4Jay FoadQualType ASTContext::getDecltypeType(Expr *e) const {
2924dd0257c77719a13d4acd513df40b04300cbfc871Douglas Gregor  DecltypeType *dt;
2925561f81243f665cf2001caadc45df505f826b72d6Douglas Gregor
2926561f81243f665cf2001caadc45df505f826b72d6Douglas Gregor  // C++0x [temp.type]p2:
2927561f81243f665cf2001caadc45df505f826b72d6Douglas Gregor  //   If an expression e involves a template parameter, decltype(e) denotes a
2928561f81243f665cf2001caadc45df505f826b72d6Douglas Gregor  //   unique dependent type. Two such decltype-specifiers refer to the same
2929561f81243f665cf2001caadc45df505f826b72d6Douglas Gregor  //   type only if their expressions are equivalent (14.5.6.1).
2930561f81243f665cf2001caadc45df505f826b72d6Douglas Gregor  if (e->isInstantiationDependent()) {
29319d702ae1cd5cfa19d884cbef77e1df99395138bbDouglas Gregor    llvm::FoldingSetNodeID ID;
29329d702ae1cd5cfa19d884cbef77e1df99395138bbDouglas Gregor    DependentDecltypeType::Profile(ID, *this, e);
29331eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
29349d702ae1cd5cfa19d884cbef77e1df99395138bbDouglas Gregor    void *InsertPos = 0;
29359d702ae1cd5cfa19d884cbef77e1df99395138bbDouglas Gregor    DependentDecltypeType *Canon
29369d702ae1cd5cfa19d884cbef77e1df99395138bbDouglas Gregor      = DependentDecltypeTypes.FindNodeOrInsertPos(ID, InsertPos);
29379d702ae1cd5cfa19d884cbef77e1df99395138bbDouglas Gregor    if (Canon) {
29389d702ae1cd5cfa19d884cbef77e1df99395138bbDouglas Gregor      // We already have a "canonical" version of an equivalent, dependent
29399d702ae1cd5cfa19d884cbef77e1df99395138bbDouglas Gregor      // decltype type. Use that as our canonical type.
29406b304a0254a13f42390b865ff5ba668a49cc58aeJohn McCall      dt = new (*this, TypeAlignment) DecltypeType(e, DependentTy,
29419d702ae1cd5cfa19d884cbef77e1df99395138bbDouglas Gregor                                       QualType((DecltypeType*)Canon, 0));
29423060178ad9df29789505c1e6debcfc80a3a13587Chad Rosier    } else {
29439d702ae1cd5cfa19d884cbef77e1df99395138bbDouglas Gregor      // Build a new, canonical typeof(expr) type.
29446b304a0254a13f42390b865ff5ba668a49cc58aeJohn McCall      Canon = new (*this, TypeAlignment) DependentDecltypeType(*this, e);
29459d702ae1cd5cfa19d884cbef77e1df99395138bbDouglas Gregor      DependentDecltypeTypes.InsertNode(Canon, InsertPos);
29469d702ae1cd5cfa19d884cbef77e1df99395138bbDouglas Gregor      dt = Canon;
29479d702ae1cd5cfa19d884cbef77e1df99395138bbDouglas Gregor    }
29489d702ae1cd5cfa19d884cbef77e1df99395138bbDouglas Gregor  } else {
2949dd0257c77719a13d4acd513df40b04300cbfc871Douglas Gregor    QualType T = getDecltypeForExpr(e, *this);
29506b304a0254a13f42390b865ff5ba668a49cc58aeJohn McCall    dt = new (*this, TypeAlignment) DecltypeType(e, T, getCanonicalType(T));
2951dd0257c77719a13d4acd513df40b04300cbfc871Douglas Gregor  }
2952395b475a4474f1c7574d927ad142ca0c7997cbcaAnders Carlsson  Types.push_back(dt);
2953395b475a4474f1c7574d927ad142ca0c7997cbcaAnders Carlsson  return QualType(dt, 0);
2954395b475a4474f1c7574d927ad142ca0c7997cbcaAnders Carlsson}
2955395b475a4474f1c7574d927ad142ca0c7997cbcaAnders Carlsson
2956ca63c200346c0ca9e00194ec6e34a5a7b0ed9321Sean Hunt/// getUnaryTransformationType - We don't unique these, since the memory
2957ca63c200346c0ca9e00194ec6e34a5a7b0ed9321Sean Hunt/// savings are minimal and these are rare.
2958ca63c200346c0ca9e00194ec6e34a5a7b0ed9321Sean HuntQualType ASTContext::getUnaryTransformType(QualType BaseType,
2959ca63c200346c0ca9e00194ec6e34a5a7b0ed9321Sean Hunt                                           QualType UnderlyingType,
2960ca63c200346c0ca9e00194ec6e34a5a7b0ed9321Sean Hunt                                           UnaryTransformType::UTTKind Kind)
2961ca63c200346c0ca9e00194ec6e34a5a7b0ed9321Sean Hunt    const {
2962ca63c200346c0ca9e00194ec6e34a5a7b0ed9321Sean Hunt  UnaryTransformType *Ty =
296369d9775da47a4b9f165dbc33277f02982928a94eDouglas Gregor    new (*this, TypeAlignment) UnaryTransformType (BaseType, UnderlyingType,
296469d9775da47a4b9f165dbc33277f02982928a94eDouglas Gregor                                                   Kind,
296569d9775da47a4b9f165dbc33277f02982928a94eDouglas Gregor                                 UnderlyingType->isDependentType() ?
296669d9775da47a4b9f165dbc33277f02982928a94eDouglas Gregor                                    QualType() : UnderlyingType);
2967ca63c200346c0ca9e00194ec6e34a5a7b0ed9321Sean Hunt  Types.push_back(Ty);
2968ca63c200346c0ca9e00194ec6e34a5a7b0ed9321Sean Hunt  return QualType(Ty, 0);
2969ca63c200346c0ca9e00194ec6e34a5a7b0ed9321Sean Hunt}
2970ca63c200346c0ca9e00194ec6e34a5a7b0ed9321Sean Hunt
2971483b9f3bc05c5409e2c6643f1c9d91e21c8ff9d2Richard Smith/// getAutoType - We only unique auto types after they've been deduced.
297234b41d939a1328f484511c6002ba2456db879a29Richard SmithQualType ASTContext::getAutoType(QualType DeducedType) const {
2973483b9f3bc05c5409e2c6643f1c9d91e21c8ff9d2Richard Smith  void *InsertPos = 0;
2974483b9f3bc05c5409e2c6643f1c9d91e21c8ff9d2Richard Smith  if (!DeducedType.isNull()) {
2975483b9f3bc05c5409e2c6643f1c9d91e21c8ff9d2Richard Smith    // Look in the folding set for an existing type.
2976483b9f3bc05c5409e2c6643f1c9d91e21c8ff9d2Richard Smith    llvm::FoldingSetNodeID ID;
2977483b9f3bc05c5409e2c6643f1c9d91e21c8ff9d2Richard Smith    AutoType::Profile(ID, DeducedType);
2978483b9f3bc05c5409e2c6643f1c9d91e21c8ff9d2Richard Smith    if (AutoType *AT = AutoTypes.FindNodeOrInsertPos(ID, InsertPos))
2979483b9f3bc05c5409e2c6643f1c9d91e21c8ff9d2Richard Smith      return QualType(AT, 0);
2980483b9f3bc05c5409e2c6643f1c9d91e21c8ff9d2Richard Smith  }
2981483b9f3bc05c5409e2c6643f1c9d91e21c8ff9d2Richard Smith
2982483b9f3bc05c5409e2c6643f1c9d91e21c8ff9d2Richard Smith  AutoType *AT = new (*this, TypeAlignment) AutoType(DeducedType);
2983483b9f3bc05c5409e2c6643f1c9d91e21c8ff9d2Richard Smith  Types.push_back(AT);
2984483b9f3bc05c5409e2c6643f1c9d91e21c8ff9d2Richard Smith  if (InsertPos)
2985483b9f3bc05c5409e2c6643f1c9d91e21c8ff9d2Richard Smith    AutoTypes.InsertNode(AT, InsertPos);
2986483b9f3bc05c5409e2c6643f1c9d91e21c8ff9d2Richard Smith  return QualType(AT, 0);
298734b41d939a1328f484511c6002ba2456db879a29Richard Smith}
298834b41d939a1328f484511c6002ba2456db879a29Richard Smith
2989b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman/// getAtomicType - Return the uniqued reference to the atomic type for
2990b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman/// the given value type.
2991b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli FriedmanQualType ASTContext::getAtomicType(QualType T) const {
2992b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman  // Unique pointers, to guarantee there is only one pointer of a particular
2993b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman  // structure.
2994b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman  llvm::FoldingSetNodeID ID;
2995b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman  AtomicType::Profile(ID, T);
2996b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman
2997b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman  void *InsertPos = 0;
2998b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman  if (AtomicType *AT = AtomicTypes.FindNodeOrInsertPos(ID, InsertPos))
2999b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman    return QualType(AT, 0);
3000b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman
3001b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman  // If the atomic value type isn't canonical, this won't be a canonical type
3002b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman  // either, so fill in the canonical type field.
3003b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman  QualType Canonical;
3004b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman  if (!T.isCanonical()) {
3005b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman    Canonical = getAtomicType(getCanonicalType(T));
3006b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman
3007b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman    // Get the new insert position for the node we care about.
3008b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman    AtomicType *NewIP = AtomicTypes.FindNodeOrInsertPos(ID, InsertPos);
3009b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman    assert(NewIP == 0 && "Shouldn't be in the map!"); (void)NewIP;
3010b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman  }
3011b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman  AtomicType *New = new (*this, TypeAlignment) AtomicType(T, Canonical);
3012b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman  Types.push_back(New);
3013b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman  AtomicTypes.InsertNode(New, InsertPos);
3014b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman  return QualType(New, 0);
3015b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman}
3016b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman
3017ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith/// getAutoDeductType - Get type pattern for deducing against 'auto'.
3018ad762fcdc16b9e4705b12b09d92b8c026212b906Richard SmithQualType ASTContext::getAutoDeductType() const {
3019ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith  if (AutoDeductTy.isNull())
3020ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith    AutoDeductTy = getAutoType(QualType());
3021ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith  assert(!AutoDeductTy.isNull() && "can't build 'auto' pattern");
3022ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith  return AutoDeductTy;
3023ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith}
3024ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith
3025ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith/// getAutoRRefDeductType - Get type pattern for deducing against 'auto &&'.
3026ad762fcdc16b9e4705b12b09d92b8c026212b906Richard SmithQualType ASTContext::getAutoRRefDeductType() const {
3027ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith  if (AutoRRefDeductTy.isNull())
3028ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith    AutoRRefDeductTy = getRValueReferenceType(getAutoDeductType());
3029ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith  assert(!AutoRRefDeductTy.isNull() && "can't build 'auto &&' pattern");
3030ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith  return AutoRRefDeductTy;
3031ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith}
3032ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith
30335f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// getTagDeclType - Return the unique reference to the type for the
30345f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// specified TagDecl (struct/union/class/enum) decl.
30354ba2a17694148e16eaa8d3917f657ffcd3667be4Jay FoadQualType ASTContext::getTagDeclType(const TagDecl *Decl) const {
3036d778f88d32b96a74c9edb7342c81357606a7cdc0Ted Kremenek  assert (Decl);
3037e607ed068334bacb8d7b093996b4671c6ca79e25Mike Stump  // FIXME: What is the design on getTagDeclType when it requires casting
3038e607ed068334bacb8d7b093996b4671c6ca79e25Mike Stump  // away const?  mutable?
3039e607ed068334bacb8d7b093996b4671c6ca79e25Mike Stump  return getTypeDeclType(const_cast<TagDecl*>(Decl));
30405f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
30415f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
30421eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// getSizeType - Return the unique type for "size_t" (C99 7.17), the result
30431eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// of the sizeof operator (C99 6.5.3.4p4). The value is target dependent and
30441eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// needs to agree with the definition in <stddef.h>.
3045a3ccda58913cc1a4b8564e349448b12acc462da7Anders CarlssonCanQualType ASTContext::getSizeType() const {
3046bcfd1f55bfbb3e5944cd5e03d07b343e280838c4Douglas Gregor  return getFromTargetType(Target->getSizeType());
30475f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
30485f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
304929e97cb35fab314388f62b68fefa78947e93c1dcHans Wennborg/// getIntMaxType - Return the unique type for "intmax_t" (C99 7.18.1.5).
305029e97cb35fab314388f62b68fefa78947e93c1dcHans WennborgCanQualType ASTContext::getIntMaxType() const {
305129e97cb35fab314388f62b68fefa78947e93c1dcHans Wennborg  return getFromTargetType(Target->getIntMaxType());
305229e97cb35fab314388f62b68fefa78947e93c1dcHans Wennborg}
305329e97cb35fab314388f62b68fefa78947e93c1dcHans Wennborg
305429e97cb35fab314388f62b68fefa78947e93c1dcHans Wennborg/// getUIntMaxType - Return the unique type for "uintmax_t" (C99 7.18.1.5).
305529e97cb35fab314388f62b68fefa78947e93c1dcHans WennborgCanQualType ASTContext::getUIntMaxType() const {
305629e97cb35fab314388f62b68fefa78947e93c1dcHans Wennborg  return getFromTargetType(Target->getUIntMaxType());
305729e97cb35fab314388f62b68fefa78947e93c1dcHans Wennborg}
305829e97cb35fab314388f62b68fefa78947e93c1dcHans Wennborg
305964c438a4be2a871fa43c78264663ba1e9788b94dArgyrios Kyrtzidis/// getSignedWCharType - Return the type of "signed wchar_t".
306064c438a4be2a871fa43c78264663ba1e9788b94dArgyrios Kyrtzidis/// Used when in C++, as a GCC extension.
306164c438a4be2a871fa43c78264663ba1e9788b94dArgyrios KyrtzidisQualType ASTContext::getSignedWCharType() const {
306264c438a4be2a871fa43c78264663ba1e9788b94dArgyrios Kyrtzidis  // FIXME: derive from "Target" ?
306364c438a4be2a871fa43c78264663ba1e9788b94dArgyrios Kyrtzidis  return WCharTy;
306464c438a4be2a871fa43c78264663ba1e9788b94dArgyrios Kyrtzidis}
306564c438a4be2a871fa43c78264663ba1e9788b94dArgyrios Kyrtzidis
306664c438a4be2a871fa43c78264663ba1e9788b94dArgyrios Kyrtzidis/// getUnsignedWCharType - Return the type of "unsigned wchar_t".
306764c438a4be2a871fa43c78264663ba1e9788b94dArgyrios Kyrtzidis/// Used when in C++, as a GCC extension.
306864c438a4be2a871fa43c78264663ba1e9788b94dArgyrios KyrtzidisQualType ASTContext::getUnsignedWCharType() const {
306964c438a4be2a871fa43c78264663ba1e9788b94dArgyrios Kyrtzidis  // FIXME: derive from "Target" ?
307064c438a4be2a871fa43c78264663ba1e9788b94dArgyrios Kyrtzidis  return UnsignedIntTy;
307164c438a4be2a871fa43c78264663ba1e9788b94dArgyrios Kyrtzidis}
307264c438a4be2a871fa43c78264663ba1e9788b94dArgyrios Kyrtzidis
307329e97cb35fab314388f62b68fefa78947e93c1dcHans Wennborg/// getPointerDiffType - Return the unique type for "ptrdiff_t" (C99 7.17)
30748b9023ba35a86838789e2c9034a6128728c547aaChris Lattner/// defined in <stddef.h>. Pointer - pointer requires this (C99 6.5.6p9).
30758b9023ba35a86838789e2c9034a6128728c547aaChris LattnerQualType ASTContext::getPointerDiffType() const {
3076bcfd1f55bfbb3e5944cd5e03d07b343e280838c4Douglas Gregor  return getFromTargetType(Target->getPtrDiffType(0));
30778b9023ba35a86838789e2c9034a6128728c547aaChris Lattner}
30788b9023ba35a86838789e2c9034a6128728c547aaChris Lattner
3079e6327747b72bb687c948270f702ff53c30f411a6Chris Lattner//===----------------------------------------------------------------------===//
3080e6327747b72bb687c948270f702ff53c30f411a6Chris Lattner//                              Type Operators
3081e6327747b72bb687c948270f702ff53c30f411a6Chris Lattner//===----------------------------------------------------------------------===//
3082e6327747b72bb687c948270f702ff53c30f411a6Chris Lattner
30834ba2a17694148e16eaa8d3917f657ffcd3667be4Jay FoadCanQualType ASTContext::getCanonicalParamType(QualType T) const {
308454e14c4db764c0636160d26c5bbf491637c83a76John McCall  // Push qualifiers into arrays, and then discard any remaining
308554e14c4db764c0636160d26c5bbf491637c83a76John McCall  // qualifiers.
308654e14c4db764c0636160d26c5bbf491637c83a76John McCall  T = getCanonicalType(T);
3087745da3a5bb4ea35f93f50301e7fbbb7d78d3b6bbFariborz Jahanian  T = getVariableArrayDecayedType(T);
308854e14c4db764c0636160d26c5bbf491637c83a76John McCall  const Type *Ty = T.getTypePtr();
308954e14c4db764c0636160d26c5bbf491637c83a76John McCall  QualType Result;
309054e14c4db764c0636160d26c5bbf491637c83a76John McCall  if (isa<ArrayType>(Ty)) {
309154e14c4db764c0636160d26c5bbf491637c83a76John McCall    Result = getArrayDecayedType(QualType(Ty,0));
309254e14c4db764c0636160d26c5bbf491637c83a76John McCall  } else if (isa<FunctionType>(Ty)) {
309354e14c4db764c0636160d26c5bbf491637c83a76John McCall    Result = getPointerType(QualType(Ty, 0));
309454e14c4db764c0636160d26c5bbf491637c83a76John McCall  } else {
309554e14c4db764c0636160d26c5bbf491637c83a76John McCall    Result = QualType(Ty, 0);
309654e14c4db764c0636160d26c5bbf491637c83a76John McCall  }
309754e14c4db764c0636160d26c5bbf491637c83a76John McCall
309854e14c4db764c0636160d26c5bbf491637c83a76John McCall  return CanQualType::CreateUnsafe(Result);
309954e14c4db764c0636160d26c5bbf491637c83a76John McCall}
310054e14c4db764c0636160d26c5bbf491637c83a76John McCall
310162c28c831bbf207cc36e683e7c321fc33bf8928cJohn McCallQualType ASTContext::getUnqualifiedArrayType(QualType type,
310262c28c831bbf207cc36e683e7c321fc33bf8928cJohn McCall                                             Qualifiers &quals) {
310362c28c831bbf207cc36e683e7c321fc33bf8928cJohn McCall  SplitQualType splitType = type.getSplitUnqualifiedType();
310462c28c831bbf207cc36e683e7c321fc33bf8928cJohn McCall
310562c28c831bbf207cc36e683e7c321fc33bf8928cJohn McCall  // FIXME: getSplitUnqualifiedType() actually walks all the way to
310662c28c831bbf207cc36e683e7c321fc33bf8928cJohn McCall  // the unqualified desugared type and then drops it on the floor.
310762c28c831bbf207cc36e683e7c321fc33bf8928cJohn McCall  // We then have to strip that sugar back off with
310862c28c831bbf207cc36e683e7c321fc33bf8928cJohn McCall  // getUnqualifiedDesugaredType(), which is silly.
310962c28c831bbf207cc36e683e7c321fc33bf8928cJohn McCall  const ArrayType *AT =
311062c28c831bbf207cc36e683e7c321fc33bf8928cJohn McCall    dyn_cast<ArrayType>(splitType.first->getUnqualifiedDesugaredType());
311162c28c831bbf207cc36e683e7c321fc33bf8928cJohn McCall
311262c28c831bbf207cc36e683e7c321fc33bf8928cJohn McCall  // If we don't have an array, just use the results in splitType.
31139dadd94e1c17fa030d1f88d8f2113ff59ccc6714Douglas Gregor  if (!AT) {
311462c28c831bbf207cc36e683e7c321fc33bf8928cJohn McCall    quals = splitType.second;
311562c28c831bbf207cc36e683e7c321fc33bf8928cJohn McCall    return QualType(splitType.first, 0);
311628e318cc6008c2bc008f0caee70dc736a03d6289Chandler Carruth  }
311728e318cc6008c2bc008f0caee70dc736a03d6289Chandler Carruth
311862c28c831bbf207cc36e683e7c321fc33bf8928cJohn McCall  // Otherwise, recurse on the array's element type.
311962c28c831bbf207cc36e683e7c321fc33bf8928cJohn McCall  QualType elementType = AT->getElementType();
312062c28c831bbf207cc36e683e7c321fc33bf8928cJohn McCall  QualType unqualElementType = getUnqualifiedArrayType(elementType, quals);
312162c28c831bbf207cc36e683e7c321fc33bf8928cJohn McCall
312262c28c831bbf207cc36e683e7c321fc33bf8928cJohn McCall  // If that didn't change the element type, AT has no qualifiers, so we
312362c28c831bbf207cc36e683e7c321fc33bf8928cJohn McCall  // can just use the results in splitType.
312462c28c831bbf207cc36e683e7c321fc33bf8928cJohn McCall  if (elementType == unqualElementType) {
312562c28c831bbf207cc36e683e7c321fc33bf8928cJohn McCall    assert(quals.empty()); // from the recursive call
312662c28c831bbf207cc36e683e7c321fc33bf8928cJohn McCall    quals = splitType.second;
312762c28c831bbf207cc36e683e7c321fc33bf8928cJohn McCall    return QualType(splitType.first, 0);
312862c28c831bbf207cc36e683e7c321fc33bf8928cJohn McCall  }
312962c28c831bbf207cc36e683e7c321fc33bf8928cJohn McCall
313062c28c831bbf207cc36e683e7c321fc33bf8928cJohn McCall  // Otherwise, add in the qualifiers from the outermost type, then
313162c28c831bbf207cc36e683e7c321fc33bf8928cJohn McCall  // build the type back up.
313262c28c831bbf207cc36e683e7c321fc33bf8928cJohn McCall  quals.addConsistentQualifiers(splitType.second);
313328e318cc6008c2bc008f0caee70dc736a03d6289Chandler Carruth
31349dadd94e1c17fa030d1f88d8f2113ff59ccc6714Douglas Gregor  if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(AT)) {
313562c28c831bbf207cc36e683e7c321fc33bf8928cJohn McCall    return getConstantArrayType(unqualElementType, CAT->getSize(),
313628e318cc6008c2bc008f0caee70dc736a03d6289Chandler Carruth                                CAT->getSizeModifier(), 0);
313728e318cc6008c2bc008f0caee70dc736a03d6289Chandler Carruth  }
313828e318cc6008c2bc008f0caee70dc736a03d6289Chandler Carruth
31399dadd94e1c17fa030d1f88d8f2113ff59ccc6714Douglas Gregor  if (const IncompleteArrayType *IAT = dyn_cast<IncompleteArrayType>(AT)) {
314062c28c831bbf207cc36e683e7c321fc33bf8928cJohn McCall    return getIncompleteArrayType(unqualElementType, IAT->getSizeModifier(), 0);
314128e318cc6008c2bc008f0caee70dc736a03d6289Chandler Carruth  }
314228e318cc6008c2bc008f0caee70dc736a03d6289Chandler Carruth
31439dadd94e1c17fa030d1f88d8f2113ff59ccc6714Douglas Gregor  if (const VariableArrayType *VAT = dyn_cast<VariableArrayType>(AT)) {
314462c28c831bbf207cc36e683e7c321fc33bf8928cJohn McCall    return getVariableArrayType(unqualElementType,
31453fa5cae9b3812cab9fab6c042c3329bb70a3d046John McCall                                VAT->getSizeExpr(),
31469dadd94e1c17fa030d1f88d8f2113ff59ccc6714Douglas Gregor                                VAT->getSizeModifier(),
31479dadd94e1c17fa030d1f88d8f2113ff59ccc6714Douglas Gregor                                VAT->getIndexTypeCVRQualifiers(),
31489dadd94e1c17fa030d1f88d8f2113ff59ccc6714Douglas Gregor                                VAT->getBracketsRange());
31499dadd94e1c17fa030d1f88d8f2113ff59ccc6714Douglas Gregor  }
31509dadd94e1c17fa030d1f88d8f2113ff59ccc6714Douglas Gregor
31519dadd94e1c17fa030d1f88d8f2113ff59ccc6714Douglas Gregor  const DependentSizedArrayType *DSAT = cast<DependentSizedArrayType>(AT);
315262c28c831bbf207cc36e683e7c321fc33bf8928cJohn McCall  return getDependentSizedArrayType(unqualElementType, DSAT->getSizeExpr(),
315328e318cc6008c2bc008f0caee70dc736a03d6289Chandler Carruth                                    DSAT->getSizeModifier(), 0,
315428e318cc6008c2bc008f0caee70dc736a03d6289Chandler Carruth                                    SourceRange());
315528e318cc6008c2bc008f0caee70dc736a03d6289Chandler Carruth}
315628e318cc6008c2bc008f0caee70dc736a03d6289Chandler Carruth
31575a57efd7bf88a4a13018e0471ded8063a4abe8afDouglas Gregor/// UnwrapSimilarPointerTypes - If T1 and T2 are pointer types  that
31585a57efd7bf88a4a13018e0471ded8063a4abe8afDouglas Gregor/// may be similar (C++ 4.4), replaces T1 and T2 with the type that
31595a57efd7bf88a4a13018e0471ded8063a4abe8afDouglas Gregor/// they point to and return true. If T1 and T2 aren't pointer types
31605a57efd7bf88a4a13018e0471ded8063a4abe8afDouglas Gregor/// or pointer-to-member types, or if they are not similar at this
31615a57efd7bf88a4a13018e0471ded8063a4abe8afDouglas Gregor/// level, returns false and leaves T1 and T2 unchanged. Top-level
31625a57efd7bf88a4a13018e0471ded8063a4abe8afDouglas Gregor/// qualifiers on T1 and T2 are ignored. This function will typically
31635a57efd7bf88a4a13018e0471ded8063a4abe8afDouglas Gregor/// be called in a loop that successively "unwraps" pointer and
31645a57efd7bf88a4a13018e0471ded8063a4abe8afDouglas Gregor/// pointer-to-member types to compare them at each level.
31655a57efd7bf88a4a13018e0471ded8063a4abe8afDouglas Gregorbool ASTContext::UnwrapSimilarPointerTypes(QualType &T1, QualType &T2) {
31665a57efd7bf88a4a13018e0471ded8063a4abe8afDouglas Gregor  const PointerType *T1PtrType = T1->getAs<PointerType>(),
31675a57efd7bf88a4a13018e0471ded8063a4abe8afDouglas Gregor                    *T2PtrType = T2->getAs<PointerType>();
31685a57efd7bf88a4a13018e0471ded8063a4abe8afDouglas Gregor  if (T1PtrType && T2PtrType) {
31695a57efd7bf88a4a13018e0471ded8063a4abe8afDouglas Gregor    T1 = T1PtrType->getPointeeType();
31705a57efd7bf88a4a13018e0471ded8063a4abe8afDouglas Gregor    T2 = T2PtrType->getPointeeType();
31715a57efd7bf88a4a13018e0471ded8063a4abe8afDouglas Gregor    return true;
31725a57efd7bf88a4a13018e0471ded8063a4abe8afDouglas Gregor  }
31735a57efd7bf88a4a13018e0471ded8063a4abe8afDouglas Gregor
31745a57efd7bf88a4a13018e0471ded8063a4abe8afDouglas Gregor  const MemberPointerType *T1MPType = T1->getAs<MemberPointerType>(),
31755a57efd7bf88a4a13018e0471ded8063a4abe8afDouglas Gregor                          *T2MPType = T2->getAs<MemberPointerType>();
31765a57efd7bf88a4a13018e0471ded8063a4abe8afDouglas Gregor  if (T1MPType && T2MPType &&
31775a57efd7bf88a4a13018e0471ded8063a4abe8afDouglas Gregor      hasSameUnqualifiedType(QualType(T1MPType->getClass(), 0),
31785a57efd7bf88a4a13018e0471ded8063a4abe8afDouglas Gregor                             QualType(T2MPType->getClass(), 0))) {
31795a57efd7bf88a4a13018e0471ded8063a4abe8afDouglas Gregor    T1 = T1MPType->getPointeeType();
31805a57efd7bf88a4a13018e0471ded8063a4abe8afDouglas Gregor    T2 = T2MPType->getPointeeType();
31815a57efd7bf88a4a13018e0471ded8063a4abe8afDouglas Gregor    return true;
31825a57efd7bf88a4a13018e0471ded8063a4abe8afDouglas Gregor  }
31835a57efd7bf88a4a13018e0471ded8063a4abe8afDouglas Gregor
31845a57efd7bf88a4a13018e0471ded8063a4abe8afDouglas Gregor  if (getLangOptions().ObjC1) {
31855a57efd7bf88a4a13018e0471ded8063a4abe8afDouglas Gregor    const ObjCObjectPointerType *T1OPType = T1->getAs<ObjCObjectPointerType>(),
31865a57efd7bf88a4a13018e0471ded8063a4abe8afDouglas Gregor                                *T2OPType = T2->getAs<ObjCObjectPointerType>();
31875a57efd7bf88a4a13018e0471ded8063a4abe8afDouglas Gregor    if (T1OPType && T2OPType) {
31885a57efd7bf88a4a13018e0471ded8063a4abe8afDouglas Gregor      T1 = T1OPType->getPointeeType();
31895a57efd7bf88a4a13018e0471ded8063a4abe8afDouglas Gregor      T2 = T2OPType->getPointeeType();
31905a57efd7bf88a4a13018e0471ded8063a4abe8afDouglas Gregor      return true;
31915a57efd7bf88a4a13018e0471ded8063a4abe8afDouglas Gregor    }
31925a57efd7bf88a4a13018e0471ded8063a4abe8afDouglas Gregor  }
31935a57efd7bf88a4a13018e0471ded8063a4abe8afDouglas Gregor
31945a57efd7bf88a4a13018e0471ded8063a4abe8afDouglas Gregor  // FIXME: Block pointers, too?
31955a57efd7bf88a4a13018e0471ded8063a4abe8afDouglas Gregor
31965a57efd7bf88a4a13018e0471ded8063a4abe8afDouglas Gregor  return false;
31975a57efd7bf88a4a13018e0471ded8063a4abe8afDouglas Gregor}
31985a57efd7bf88a4a13018e0471ded8063a4abe8afDouglas Gregor
31994ba2a17694148e16eaa8d3917f657ffcd3667be4Jay FoadDeclarationNameInfo
32004ba2a17694148e16eaa8d3917f657ffcd3667be4Jay FoadASTContext::getNameForTemplate(TemplateName Name,
32014ba2a17694148e16eaa8d3917f657ffcd3667be4Jay Foad                               SourceLocation NameLoc) const {
3202146060435c3efce95c95a092c7a1eb651cfb9ae0John McCall  switch (Name.getKind()) {
3203146060435c3efce95c95a092c7a1eb651cfb9ae0John McCall  case TemplateName::QualifiedTemplate:
3204146060435c3efce95c95a092c7a1eb651cfb9ae0John McCall  case TemplateName::Template:
32052577743c5650c646fb705df01403707e94f2df04Abramo Bagnara    // DNInfo work in progress: CHECKME: what about DNLoc?
3206146060435c3efce95c95a092c7a1eb651cfb9ae0John McCall    return DeclarationNameInfo(Name.getAsTemplateDecl()->getDeclName(),
3207146060435c3efce95c95a092c7a1eb651cfb9ae0John McCall                               NameLoc);
32082577743c5650c646fb705df01403707e94f2df04Abramo Bagnara
3209146060435c3efce95c95a092c7a1eb651cfb9ae0John McCall  case TemplateName::OverloadedTemplate: {
3210146060435c3efce95c95a092c7a1eb651cfb9ae0John McCall    OverloadedTemplateStorage *Storage = Name.getAsOverloadedTemplate();
3211146060435c3efce95c95a092c7a1eb651cfb9ae0John McCall    // DNInfo work in progress: CHECKME: what about DNLoc?
3212146060435c3efce95c95a092c7a1eb651cfb9ae0John McCall    return DeclarationNameInfo((*Storage->begin())->getDeclName(), NameLoc);
3213146060435c3efce95c95a092c7a1eb651cfb9ae0John McCall  }
3214146060435c3efce95c95a092c7a1eb651cfb9ae0John McCall
3215146060435c3efce95c95a092c7a1eb651cfb9ae0John McCall  case TemplateName::DependentTemplate: {
3216146060435c3efce95c95a092c7a1eb651cfb9ae0John McCall    DependentTemplateName *DTN = Name.getAsDependentTemplateName();
32172577743c5650c646fb705df01403707e94f2df04Abramo Bagnara    DeclarationName DName;
321880ad16f4b2b350ddbaae21a52975e63df5aafc2cJohn McCall    if (DTN->isIdentifier()) {
32192577743c5650c646fb705df01403707e94f2df04Abramo Bagnara      DName = DeclarationNames.getIdentifier(DTN->getIdentifier());
32202577743c5650c646fb705df01403707e94f2df04Abramo Bagnara      return DeclarationNameInfo(DName, NameLoc);
322180ad16f4b2b350ddbaae21a52975e63df5aafc2cJohn McCall    } else {
32222577743c5650c646fb705df01403707e94f2df04Abramo Bagnara      DName = DeclarationNames.getCXXOperatorName(DTN->getOperator());
32232577743c5650c646fb705df01403707e94f2df04Abramo Bagnara      // DNInfo work in progress: FIXME: source locations?
32242577743c5650c646fb705df01403707e94f2df04Abramo Bagnara      DeclarationNameLoc DNLoc;
32252577743c5650c646fb705df01403707e94f2df04Abramo Bagnara      DNLoc.CXXOperatorName.BeginOpNameLoc = SourceLocation().getRawEncoding();
32262577743c5650c646fb705df01403707e94f2df04Abramo Bagnara      DNLoc.CXXOperatorName.EndOpNameLoc = SourceLocation().getRawEncoding();
32272577743c5650c646fb705df01403707e94f2df04Abramo Bagnara      return DeclarationNameInfo(DName, NameLoc, DNLoc);
322880ad16f4b2b350ddbaae21a52975e63df5aafc2cJohn McCall    }
322980ad16f4b2b350ddbaae21a52975e63df5aafc2cJohn McCall  }
323080ad16f4b2b350ddbaae21a52975e63df5aafc2cJohn McCall
3231146060435c3efce95c95a092c7a1eb651cfb9ae0John McCall  case TemplateName::SubstTemplateTemplateParm: {
3232146060435c3efce95c95a092c7a1eb651cfb9ae0John McCall    SubstTemplateTemplateParmStorage *subst
3233146060435c3efce95c95a092c7a1eb651cfb9ae0John McCall      = Name.getAsSubstTemplateTemplateParm();
3234146060435c3efce95c95a092c7a1eb651cfb9ae0John McCall    return DeclarationNameInfo(subst->getParameter()->getDeclName(),
3235146060435c3efce95c95a092c7a1eb651cfb9ae0John McCall                               NameLoc);
3236146060435c3efce95c95a092c7a1eb651cfb9ae0John McCall  }
3237146060435c3efce95c95a092c7a1eb651cfb9ae0John McCall
3238146060435c3efce95c95a092c7a1eb651cfb9ae0John McCall  case TemplateName::SubstTemplateTemplateParmPack: {
3239146060435c3efce95c95a092c7a1eb651cfb9ae0John McCall    SubstTemplateTemplateParmPackStorage *subst
3240146060435c3efce95c95a092c7a1eb651cfb9ae0John McCall      = Name.getAsSubstTemplateTemplateParmPack();
3241146060435c3efce95c95a092c7a1eb651cfb9ae0John McCall    return DeclarationNameInfo(subst->getParameterPack()->getDeclName(),
3242146060435c3efce95c95a092c7a1eb651cfb9ae0John McCall                               NameLoc);
3243146060435c3efce95c95a092c7a1eb651cfb9ae0John McCall  }
3244146060435c3efce95c95a092c7a1eb651cfb9ae0John McCall  }
3245146060435c3efce95c95a092c7a1eb651cfb9ae0John McCall
3246146060435c3efce95c95a092c7a1eb651cfb9ae0John McCall  llvm_unreachable("bad template name kind!");
324780ad16f4b2b350ddbaae21a52975e63df5aafc2cJohn McCall}
324880ad16f4b2b350ddbaae21a52975e63df5aafc2cJohn McCall
32494ba2a17694148e16eaa8d3917f657ffcd3667be4Jay FoadTemplateName ASTContext::getCanonicalTemplateName(TemplateName Name) const {
3250146060435c3efce95c95a092c7a1eb651cfb9ae0John McCall  switch (Name.getKind()) {
3251146060435c3efce95c95a092c7a1eb651cfb9ae0John McCall  case TemplateName::QualifiedTemplate:
3252146060435c3efce95c95a092c7a1eb651cfb9ae0John McCall  case TemplateName::Template: {
3253146060435c3efce95c95a092c7a1eb651cfb9ae0John McCall    TemplateDecl *Template = Name.getAsTemplateDecl();
32543e1274f2b99cb99c03cc8e2c6517c37d330b597aDouglas Gregor    if (TemplateTemplateParmDecl *TTP
3255146060435c3efce95c95a092c7a1eb651cfb9ae0John McCall          = dyn_cast<TemplateTemplateParmDecl>(Template))
32563e1274f2b99cb99c03cc8e2c6517c37d330b597aDouglas Gregor      Template = getCanonicalTemplateTemplateParmDecl(TTP);
32573e1274f2b99cb99c03cc8e2c6517c37d330b597aDouglas Gregor
32583e1274f2b99cb99c03cc8e2c6517c37d330b597aDouglas Gregor    // The canonical template name is the canonical template declaration.
325997fbaa2a38804268a024f1a104b43fcf8b4411b0Argyrios Kyrtzidis    return TemplateName(cast<TemplateDecl>(Template->getCanonicalDecl()));
32603e1274f2b99cb99c03cc8e2c6517c37d330b597aDouglas Gregor  }
326125a3ef7cc5fd55dc8cc67c6e6770c8595657e082Douglas Gregor
3262146060435c3efce95c95a092c7a1eb651cfb9ae0John McCall  case TemplateName::OverloadedTemplate:
3263146060435c3efce95c95a092c7a1eb651cfb9ae0John McCall    llvm_unreachable("cannot canonicalize overloaded template");
3264146060435c3efce95c95a092c7a1eb651cfb9ae0John McCall
3265146060435c3efce95c95a092c7a1eb651cfb9ae0John McCall  case TemplateName::DependentTemplate: {
3266146060435c3efce95c95a092c7a1eb651cfb9ae0John McCall    DependentTemplateName *DTN = Name.getAsDependentTemplateName();
3267146060435c3efce95c95a092c7a1eb651cfb9ae0John McCall    assert(DTN && "Non-dependent template names must refer to template decls.");
3268146060435c3efce95c95a092c7a1eb651cfb9ae0John McCall    return DTN->CanonicalTemplateName;
3269146060435c3efce95c95a092c7a1eb651cfb9ae0John McCall  }
3270146060435c3efce95c95a092c7a1eb651cfb9ae0John McCall
3271146060435c3efce95c95a092c7a1eb651cfb9ae0John McCall  case TemplateName::SubstTemplateTemplateParm: {
3272146060435c3efce95c95a092c7a1eb651cfb9ae0John McCall    SubstTemplateTemplateParmStorage *subst
3273146060435c3efce95c95a092c7a1eb651cfb9ae0John McCall      = Name.getAsSubstTemplateTemplateParm();
3274146060435c3efce95c95a092c7a1eb651cfb9ae0John McCall    return getCanonicalTemplateName(subst->getReplacement());
32751aee05d08b2184acadeb36de300e216390780d6cDouglas Gregor  }
32761eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
3277146060435c3efce95c95a092c7a1eb651cfb9ae0John McCall  case TemplateName::SubstTemplateTemplateParmPack: {
3278146060435c3efce95c95a092c7a1eb651cfb9ae0John McCall    SubstTemplateTemplateParmPackStorage *subst
3279146060435c3efce95c95a092c7a1eb651cfb9ae0John McCall                                  = Name.getAsSubstTemplateTemplateParmPack();
3280146060435c3efce95c95a092c7a1eb651cfb9ae0John McCall    TemplateTemplateParmDecl *canonParameter
3281146060435c3efce95c95a092c7a1eb651cfb9ae0John McCall      = getCanonicalTemplateTemplateParmDecl(subst->getParameterPack());
3282146060435c3efce95c95a092c7a1eb651cfb9ae0John McCall    TemplateArgument canonArgPack
3283146060435c3efce95c95a092c7a1eb651cfb9ae0John McCall      = getCanonicalTemplateArgument(subst->getArgumentPack());
3284146060435c3efce95c95a092c7a1eb651cfb9ae0John McCall    return getSubstTemplateTemplateParmPack(canonParameter, canonArgPack);
3285146060435c3efce95c95a092c7a1eb651cfb9ae0John McCall  }
3286146060435c3efce95c95a092c7a1eb651cfb9ae0John McCall  }
3287146060435c3efce95c95a092c7a1eb651cfb9ae0John McCall
3288146060435c3efce95c95a092c7a1eb651cfb9ae0John McCall  llvm_unreachable("bad template name!");
328925a3ef7cc5fd55dc8cc67c6e6770c8595657e082Douglas Gregor}
329025a3ef7cc5fd55dc8cc67c6e6770c8595657e082Douglas Gregor
3291db0d4b751e83b8841b8f48f913f17e50467f13d4Douglas Gregorbool ASTContext::hasSameTemplateName(TemplateName X, TemplateName Y) {
3292db0d4b751e83b8841b8f48f913f17e50467f13d4Douglas Gregor  X = getCanonicalTemplateName(X);
3293db0d4b751e83b8841b8f48f913f17e50467f13d4Douglas Gregor  Y = getCanonicalTemplateName(Y);
3294db0d4b751e83b8841b8f48f913f17e50467f13d4Douglas Gregor  return X.getAsVoidPointer() == Y.getAsVoidPointer();
3295db0d4b751e83b8841b8f48f913f17e50467f13d4Douglas Gregor}
3296db0d4b751e83b8841b8f48f913f17e50467f13d4Douglas Gregor
32971eb4433ac451dc16f4133a88af2d002ac26c58efMike StumpTemplateArgument
32984ba2a17694148e16eaa8d3917f657ffcd3667be4Jay FoadASTContext::getCanonicalTemplateArgument(const TemplateArgument &Arg) const {
32991275ae098acda31fe0e434510c729fcfed0458a1Douglas Gregor  switch (Arg.getKind()) {
33001275ae098acda31fe0e434510c729fcfed0458a1Douglas Gregor    case TemplateArgument::Null:
33011275ae098acda31fe0e434510c729fcfed0458a1Douglas Gregor      return Arg;
33021eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
33031275ae098acda31fe0e434510c729fcfed0458a1Douglas Gregor    case TemplateArgument::Expression:
33041275ae098acda31fe0e434510c729fcfed0458a1Douglas Gregor      return Arg;
33051eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
33061275ae098acda31fe0e434510c729fcfed0458a1Douglas Gregor    case TemplateArgument::Declaration:
3307833ca991c1bfc967f0995974ca86f66ba1f666b5John McCall      return TemplateArgument(Arg.getAsDecl()->getCanonicalDecl());
33081eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
3309788cd06cf8e868a67158aafec5de3a1f408d14f3Douglas Gregor    case TemplateArgument::Template:
3310788cd06cf8e868a67158aafec5de3a1f408d14f3Douglas Gregor      return TemplateArgument(getCanonicalTemplateName(Arg.getAsTemplate()));
3311a7fc901a2e39bfe55bfcff5934b2d9fdf9656491Douglas Gregor
3312a7fc901a2e39bfe55bfcff5934b2d9fdf9656491Douglas Gregor    case TemplateArgument::TemplateExpansion:
3313a7fc901a2e39bfe55bfcff5934b2d9fdf9656491Douglas Gregor      return TemplateArgument(getCanonicalTemplateName(
3314a7fc901a2e39bfe55bfcff5934b2d9fdf9656491Douglas Gregor                                         Arg.getAsTemplateOrTemplatePattern()),
33152be29f423acad3bbe39099a78db2805acb5bdf17Douglas Gregor                              Arg.getNumTemplateExpansions());
3316a7fc901a2e39bfe55bfcff5934b2d9fdf9656491Douglas Gregor
33171275ae098acda31fe0e434510c729fcfed0458a1Douglas Gregor    case TemplateArgument::Integral:
3318833ca991c1bfc967f0995974ca86f66ba1f666b5John McCall      return TemplateArgument(*Arg.getAsIntegral(),
33191275ae098acda31fe0e434510c729fcfed0458a1Douglas Gregor                              getCanonicalType(Arg.getIntegralType()));
33201eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
33211275ae098acda31fe0e434510c729fcfed0458a1Douglas Gregor    case TemplateArgument::Type:
3322833ca991c1bfc967f0995974ca86f66ba1f666b5John McCall      return TemplateArgument(getCanonicalType(Arg.getAsType()));
33231eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
33241275ae098acda31fe0e434510c729fcfed0458a1Douglas Gregor    case TemplateArgument::Pack: {
332587dd697dcc8ecb64df73ae64d61b8c80ff0c157cDouglas Gregor      if (Arg.pack_size() == 0)
332687dd697dcc8ecb64df73ae64d61b8c80ff0c157cDouglas Gregor        return Arg;
332787dd697dcc8ecb64df73ae64d61b8c80ff0c157cDouglas Gregor
3328910f8008fea79120489a53593fe971b0b8a4a740Douglas Gregor      TemplateArgument *CanonArgs
3329910f8008fea79120489a53593fe971b0b8a4a740Douglas Gregor        = new (*this) TemplateArgument[Arg.pack_size()];
33301275ae098acda31fe0e434510c729fcfed0458a1Douglas Gregor      unsigned Idx = 0;
33311eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      for (TemplateArgument::pack_iterator A = Arg.pack_begin(),
33321275ae098acda31fe0e434510c729fcfed0458a1Douglas Gregor                                        AEnd = Arg.pack_end();
33331275ae098acda31fe0e434510c729fcfed0458a1Douglas Gregor           A != AEnd; (void)++A, ++Idx)
33341275ae098acda31fe0e434510c729fcfed0458a1Douglas Gregor        CanonArgs[Idx] = getCanonicalTemplateArgument(*A);
33351eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
3336910f8008fea79120489a53593fe971b0b8a4a740Douglas Gregor      return TemplateArgument(CanonArgs, Arg.pack_size());
33371275ae098acda31fe0e434510c729fcfed0458a1Douglas Gregor    }
33381275ae098acda31fe0e434510c729fcfed0458a1Douglas Gregor  }
33391275ae098acda31fe0e434510c729fcfed0458a1Douglas Gregor
33401275ae098acda31fe0e434510c729fcfed0458a1Douglas Gregor  // Silence GCC warning
3341b219cfc4d75f0a03630b7c4509ef791b7e97b2c8David Blaikie  llvm_unreachable("Unhandled template argument kind");
33421275ae098acda31fe0e434510c729fcfed0458a1Douglas Gregor}
33431275ae098acda31fe0e434510c729fcfed0458a1Douglas Gregor
3344d57959af02b4af695276f4204443afe6e5d86bd8Douglas GregorNestedNameSpecifier *
33454ba2a17694148e16eaa8d3917f657ffcd3667be4Jay FoadASTContext::getCanonicalNestedNameSpecifier(NestedNameSpecifier *NNS) const {
33461eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  if (!NNS)
3347d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor    return 0;
3348d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor
3349d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor  switch (NNS->getKind()) {
3350d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor  case NestedNameSpecifier::Identifier:
3351d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor    // Canonicalize the prefix but keep the identifier the same.
33521eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    return NestedNameSpecifier::Create(*this,
3353d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor                         getCanonicalNestedNameSpecifier(NNS->getPrefix()),
3354d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor                                       NNS->getAsIdentifier());
3355d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor
3356d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor  case NestedNameSpecifier::Namespace:
3357d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor    // A namespace is canonical; build a nested-name-specifier with
3358d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor    // this namespace and no prefix.
335914aba76042e041b2c5e439bf4ae353a0a3c7fd73Douglas Gregor    return NestedNameSpecifier::Create(*this, 0,
336014aba76042e041b2c5e439bf4ae353a0a3c7fd73Douglas Gregor                                 NNS->getAsNamespace()->getOriginalNamespace());
336114aba76042e041b2c5e439bf4ae353a0a3c7fd73Douglas Gregor
336214aba76042e041b2c5e439bf4ae353a0a3c7fd73Douglas Gregor  case NestedNameSpecifier::NamespaceAlias:
336314aba76042e041b2c5e439bf4ae353a0a3c7fd73Douglas Gregor    // A namespace is canonical; build a nested-name-specifier with
336414aba76042e041b2c5e439bf4ae353a0a3c7fd73Douglas Gregor    // this namespace and no prefix.
336514aba76042e041b2c5e439bf4ae353a0a3c7fd73Douglas Gregor    return NestedNameSpecifier::Create(*this, 0,
336614aba76042e041b2c5e439bf4ae353a0a3c7fd73Douglas Gregor                                    NNS->getAsNamespaceAlias()->getNamespace()
336714aba76042e041b2c5e439bf4ae353a0a3c7fd73Douglas Gregor                                                      ->getOriginalNamespace());
3368d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor
3369d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor  case NestedNameSpecifier::TypeSpec:
3370d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor  case NestedNameSpecifier::TypeSpecWithTemplate: {
3371d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor    QualType T = getCanonicalType(QualType(NNS->getAsType(), 0));
3372264bf66d55563dd86a3d7e06738aa427de512d2cDouglas Gregor
3373264bf66d55563dd86a3d7e06738aa427de512d2cDouglas Gregor    // If we have some kind of dependent-named type (e.g., "typename T::type"),
3374264bf66d55563dd86a3d7e06738aa427de512d2cDouglas Gregor    // break it apart into its prefix and identifier, then reconsititute those
3375264bf66d55563dd86a3d7e06738aa427de512d2cDouglas Gregor    // as the canonical nested-name-specifier. This is required to canonicalize
3376264bf66d55563dd86a3d7e06738aa427de512d2cDouglas Gregor    // a dependent nested-name-specifier involving typedefs of dependent-name
3377264bf66d55563dd86a3d7e06738aa427de512d2cDouglas Gregor    // types, e.g.,
3378264bf66d55563dd86a3d7e06738aa427de512d2cDouglas Gregor    //   typedef typename T::type T1;
3379264bf66d55563dd86a3d7e06738aa427de512d2cDouglas Gregor    //   typedef typename T1::type T2;
3380264bf66d55563dd86a3d7e06738aa427de512d2cDouglas Gregor    if (const DependentNameType *DNT = T->getAs<DependentNameType>()) {
3381264bf66d55563dd86a3d7e06738aa427de512d2cDouglas Gregor      NestedNameSpecifier *Prefix
3382264bf66d55563dd86a3d7e06738aa427de512d2cDouglas Gregor        = getCanonicalNestedNameSpecifier(DNT->getQualifier());
3383264bf66d55563dd86a3d7e06738aa427de512d2cDouglas Gregor      return NestedNameSpecifier::Create(*this, Prefix,
3384264bf66d55563dd86a3d7e06738aa427de512d2cDouglas Gregor                           const_cast<IdentifierInfo *>(DNT->getIdentifier()));
3385264bf66d55563dd86a3d7e06738aa427de512d2cDouglas Gregor    }
3386264bf66d55563dd86a3d7e06738aa427de512d2cDouglas Gregor
3387643f84353b3b7bbf9b73bcbc7da0ef8a39a5ca99Douglas Gregor    // Do the same thing as above, but with dependent-named specializations.
3388264bf66d55563dd86a3d7e06738aa427de512d2cDouglas Gregor    if (const DependentTemplateSpecializationType *DTST
3389264bf66d55563dd86a3d7e06738aa427de512d2cDouglas Gregor          = T->getAs<DependentTemplateSpecializationType>()) {
3390264bf66d55563dd86a3d7e06738aa427de512d2cDouglas Gregor      NestedNameSpecifier *Prefix
3391264bf66d55563dd86a3d7e06738aa427de512d2cDouglas Gregor        = getCanonicalNestedNameSpecifier(DTST->getQualifier());
3392aa2187de137e5b809dcbbe14f3b61ae907a3d8aaDouglas Gregor
3393aa2187de137e5b809dcbbe14f3b61ae907a3d8aaDouglas Gregor      T = getDependentTemplateSpecializationType(DTST->getKeyword(),
3394aa2187de137e5b809dcbbe14f3b61ae907a3d8aaDouglas Gregor                                                 Prefix, DTST->getIdentifier(),
3395aa2187de137e5b809dcbbe14f3b61ae907a3d8aaDouglas Gregor                                                 DTST->getNumArgs(),
3396aa2187de137e5b809dcbbe14f3b61ae907a3d8aaDouglas Gregor                                                 DTST->getArgs());
3397264bf66d55563dd86a3d7e06738aa427de512d2cDouglas Gregor      T = getCanonicalType(T);
3398264bf66d55563dd86a3d7e06738aa427de512d2cDouglas Gregor    }
3399264bf66d55563dd86a3d7e06738aa427de512d2cDouglas Gregor
34003b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall    return NestedNameSpecifier::Create(*this, 0, false,
34013b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall                                       const_cast<Type*>(T.getTypePtr()));
3402d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor  }
3403d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor
3404d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor  case NestedNameSpecifier::Global:
3405d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor    // The global specifier is canonical and unique.
3406d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor    return NNS;
3407d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor  }
3408d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor
34097530c034c0c71a64c5a9173206d9742ae847af8bDavid Blaikie  llvm_unreachable("Invalid NestedNameSpecifier::Kind!");
3410d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor}
3411d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor
3412c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner
34134ba2a17694148e16eaa8d3917f657ffcd3667be4Jay Foadconst ArrayType *ASTContext::getAsArrayType(QualType T) const {
3414c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner  // Handle the non-qualified case efficiently.
3415a4923eb7c4b04d360cb2747641a5e92818edf804Douglas Gregor  if (!T.hasLocalQualifiers()) {
3416c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner    // Handle the common positive case fast.
3417c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner    if (const ArrayType *AT = dyn_cast<ArrayType>(T))
3418c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner      return AT;
3419c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner  }
34201eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
34210953e767ff7817f97b3ab20896b229891eeff45bJohn McCall  // Handle the common negative case fast.
34223b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall  if (!isa<ArrayType>(T.getCanonicalType()))
3423c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner    return 0;
34241eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
34250953e767ff7817f97b3ab20896b229891eeff45bJohn McCall  // Apply any qualifiers from the array type to the element type.  This
3426c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner  // implements C99 6.7.3p8: "If the specification of an array type includes
3427c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner  // any type qualifiers, the element type is so qualified, not the array type."
34281eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
3429c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner  // If we get here, we either have type qualifiers on the type, or we have
3430c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner  // sugar such as a typedef in the way.  If we have type qualifiers on the type
343150d62d1b4a98adbc83de8f8cd1379ea1c25656f7Douglas Gregor  // we must propagate them down into the element type.
34320953e767ff7817f97b3ab20896b229891eeff45bJohn McCall
34333b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall  SplitQualType split = T.getSplitDesugaredType();
34343b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall  Qualifiers qs = split.second;
34351eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
3436c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner  // If we have a simple case, just return now.
34373b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall  const ArrayType *ATy = dyn_cast<ArrayType>(split.first);
34383b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall  if (ATy == 0 || qs.empty())
3439c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner    return ATy;
34401eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
3441c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner  // Otherwise, we have an array and we have qualifiers on it.  Push the
3442c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner  // qualifiers into the array element type and return a new array type.
34433b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall  QualType NewEltTy = getQualifiedType(ATy->getElementType(), qs);
34441eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
3445c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner  if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(ATy))
3446c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner    return cast<ArrayType>(getConstantArrayType(NewEltTy, CAT->getSize(),
3447c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner                                                CAT->getSizeModifier(),
34480953e767ff7817f97b3ab20896b229891eeff45bJohn McCall                                           CAT->getIndexTypeCVRQualifiers()));
3449c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner  if (const IncompleteArrayType *IAT = dyn_cast<IncompleteArrayType>(ATy))
3450c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner    return cast<ArrayType>(getIncompleteArrayType(NewEltTy,
3451c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner                                                  IAT->getSizeModifier(),
34520953e767ff7817f97b3ab20896b229891eeff45bJohn McCall                                           IAT->getIndexTypeCVRQualifiers()));
3453898574e7496ba8fd76290079d3a9d06954992734Douglas Gregor
34541eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  if (const DependentSizedArrayType *DSAT
3455898574e7496ba8fd76290079d3a9d06954992734Douglas Gregor        = dyn_cast<DependentSizedArrayType>(ATy))
3456898574e7496ba8fd76290079d3a9d06954992734Douglas Gregor    return cast<ArrayType>(
34571eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump                     getDependentSizedArrayType(NewEltTy,
34583fa5cae9b3812cab9fab6c042c3329bb70a3d046John McCall                                                DSAT->getSizeExpr(),
3459898574e7496ba8fd76290079d3a9d06954992734Douglas Gregor                                                DSAT->getSizeModifier(),
34600953e767ff7817f97b3ab20896b229891eeff45bJohn McCall                                              DSAT->getIndexTypeCVRQualifiers(),
34617e7eb3da052a6d80ddf2377cab0384c798f73f75Douglas Gregor                                                DSAT->getBracketsRange()));
34621eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
3463c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner  const VariableArrayType *VAT = cast<VariableArrayType>(ATy);
34647e7eb3da052a6d80ddf2377cab0384c798f73f75Douglas Gregor  return cast<ArrayType>(getVariableArrayType(NewEltTy,
34653fa5cae9b3812cab9fab6c042c3329bb70a3d046John McCall                                              VAT->getSizeExpr(),
3466c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner                                              VAT->getSizeModifier(),
34670953e767ff7817f97b3ab20896b229891eeff45bJohn McCall                                              VAT->getIndexTypeCVRQualifiers(),
34687e7eb3da052a6d80ddf2377cab0384c798f73f75Douglas Gregor                                              VAT->getBracketsRange()));
346977c9647cae939104c6cb2b6a4dd8ca859d2e5770Chris Lattner}
347077c9647cae939104c6cb2b6a4dd8ca859d2e5770Chris Lattner
347179e6bd379773447a74cc3e579d9081e4c5cb6d63Douglas GregorQualType ASTContext::getAdjustedParameterType(QualType T) {
347279e6bd379773447a74cc3e579d9081e4c5cb6d63Douglas Gregor  // C99 6.7.5.3p7:
347379e6bd379773447a74cc3e579d9081e4c5cb6d63Douglas Gregor  //   A declaration of a parameter as "array of type" shall be
347479e6bd379773447a74cc3e579d9081e4c5cb6d63Douglas Gregor  //   adjusted to "qualified pointer to type", where the type
347579e6bd379773447a74cc3e579d9081e4c5cb6d63Douglas Gregor  //   qualifiers (if any) are those specified within the [ and ] of
347679e6bd379773447a74cc3e579d9081e4c5cb6d63Douglas Gregor  //   the array type derivation.
347779e6bd379773447a74cc3e579d9081e4c5cb6d63Douglas Gregor  if (T->isArrayType())
347879e6bd379773447a74cc3e579d9081e4c5cb6d63Douglas Gregor    return getArrayDecayedType(T);
347979e6bd379773447a74cc3e579d9081e4c5cb6d63Douglas Gregor
348079e6bd379773447a74cc3e579d9081e4c5cb6d63Douglas Gregor  // C99 6.7.5.3p8:
348179e6bd379773447a74cc3e579d9081e4c5cb6d63Douglas Gregor  //   A declaration of a parameter as "function returning type"
348279e6bd379773447a74cc3e579d9081e4c5cb6d63Douglas Gregor  //   shall be adjusted to "pointer to function returning type", as
348379e6bd379773447a74cc3e579d9081e4c5cb6d63Douglas Gregor  //   in 6.3.2.1.
348479e6bd379773447a74cc3e579d9081e4c5cb6d63Douglas Gregor  if (T->isFunctionType())
348579e6bd379773447a74cc3e579d9081e4c5cb6d63Douglas Gregor    return getPointerType(T);
348679e6bd379773447a74cc3e579d9081e4c5cb6d63Douglas Gregor
348779e6bd379773447a74cc3e579d9081e4c5cb6d63Douglas Gregor  return T;
348879e6bd379773447a74cc3e579d9081e4c5cb6d63Douglas Gregor}
348979e6bd379773447a74cc3e579d9081e4c5cb6d63Douglas Gregor
349079e6bd379773447a74cc3e579d9081e4c5cb6d63Douglas GregorQualType ASTContext::getSignatureParameterType(QualType T) {
349179e6bd379773447a74cc3e579d9081e4c5cb6d63Douglas Gregor  T = getVariableArrayDecayedType(T);
349279e6bd379773447a74cc3e579d9081e4c5cb6d63Douglas Gregor  T = getAdjustedParameterType(T);
349379e6bd379773447a74cc3e579d9081e4c5cb6d63Douglas Gregor  return T.getUnqualifiedType();
349479e6bd379773447a74cc3e579d9081e4c5cb6d63Douglas Gregor}
349579e6bd379773447a74cc3e579d9081e4c5cb6d63Douglas Gregor
3496e6327747b72bb687c948270f702ff53c30f411a6Chris Lattner/// getArrayDecayedType - Return the properly qualified result of decaying the
3497e6327747b72bb687c948270f702ff53c30f411a6Chris Lattner/// specified array type to a pointer.  This operation is non-trivial when
3498e6327747b72bb687c948270f702ff53c30f411a6Chris Lattner/// handling typedefs etc.  The canonical type of "T" must be an array type,
3499e6327747b72bb687c948270f702ff53c30f411a6Chris Lattner/// this returns a pointer to a properly qualified element of the array.
3500e6327747b72bb687c948270f702ff53c30f411a6Chris Lattner///
3501e6327747b72bb687c948270f702ff53c30f411a6Chris Lattner/// See C99 6.7.5.3p7 and C99 6.3.2.1p3.
35024ba2a17694148e16eaa8d3917f657ffcd3667be4Jay FoadQualType ASTContext::getArrayDecayedType(QualType Ty) const {
3503c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner  // Get the element type with 'getAsArrayType' so that we don't lose any
3504c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner  // typedefs in the element type of the array.  This also handles propagation
3505c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner  // of type qualifiers from the array type into the element type if present
3506c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner  // (C99 6.7.3p8).
3507c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner  const ArrayType *PrettyArrayType = getAsArrayType(Ty);
3508c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner  assert(PrettyArrayType && "Not an array type!");
35091eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
3510c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner  QualType PtrTy = getPointerType(PrettyArrayType->getElementType());
3511e6327747b72bb687c948270f702ff53c30f411a6Chris Lattner
3512e6327747b72bb687c948270f702ff53c30f411a6Chris Lattner  // int x[restrict 4] ->  int *restrict
35130953e767ff7817f97b3ab20896b229891eeff45bJohn McCall  return getQualifiedType(PtrTy, PrettyArrayType->getIndexTypeQualifiers());
3514e6327747b72bb687c948270f702ff53c30f411a6Chris Lattner}
3515e6327747b72bb687c948270f702ff53c30f411a6Chris Lattner
35163b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCallQualType ASTContext::getBaseElementType(const ArrayType *array) const {
35173b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall  return getBaseElementType(array->getElementType());
35185e03f9ea8174ae588c5e69ec6b5ef4c68f8fd766Douglas Gregor}
35195e03f9ea8174ae588c5e69ec6b5ef4c68f8fd766Douglas Gregor
35203b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCallQualType ASTContext::getBaseElementType(QualType type) const {
35213b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall  Qualifiers qs;
35223b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall  while (true) {
35233b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall    SplitQualType split = type.getSplitDesugaredType();
35243b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall    const ArrayType *array = split.first->getAsArrayTypeUnsafe();
35253b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall    if (!array) break;
35261eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
35273b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall    type = array->getElementType();
35283b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall    qs.addConsistentQualifiers(split.second);
35293b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall  }
35301eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
35313b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall  return getQualifiedType(type, qs);
35326183a99b064b397d98297904fbd6cf00fe1f453dAnders Carlsson}
35336183a99b064b397d98297904fbd6cf00fe1f453dAnders Carlsson
35340de78998e7bda473b408437053e48661b510d453Fariborz Jahanian/// getConstantArrayElementCount - Returns number of constant array elements.
35351eb4433ac451dc16f4133a88af2d002ac26c58efMike Stumpuint64_t
35360de78998e7bda473b408437053e48661b510d453Fariborz JahanianASTContext::getConstantArrayElementCount(const ConstantArrayType *CA)  const {
35370de78998e7bda473b408437053e48661b510d453Fariborz Jahanian  uint64_t ElementCount = 1;
35380de78998e7bda473b408437053e48661b510d453Fariborz Jahanian  do {
35390de78998e7bda473b408437053e48661b510d453Fariborz Jahanian    ElementCount *= CA->getSize().getZExtValue();
35400de78998e7bda473b408437053e48661b510d453Fariborz Jahanian    CA = dyn_cast<ConstantArrayType>(CA->getElementType());
35410de78998e7bda473b408437053e48661b510d453Fariborz Jahanian  } while (CA);
35420de78998e7bda473b408437053e48661b510d453Fariborz Jahanian  return ElementCount;
35430de78998e7bda473b408437053e48661b510d453Fariborz Jahanian}
35440de78998e7bda473b408437053e48661b510d453Fariborz Jahanian
35455f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// getFloatingRank - Return a relative rank for floating point types.
35465f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// This routine will assert if passed a built-in type that isn't a float.
3547a75cea3f6be0daa8054d36af81a6ffda1713f82dChris Lattnerstatic FloatingRank getFloatingRank(QualType T) {
3548183700f494ec9b6701b6efe82bcb25f4c79ba561John McCall  if (const ComplexType *CT = T->getAs<ComplexType>())
35495f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    return getFloatingRank(CT->getElementType());
3550a75cea3f6be0daa8054d36af81a6ffda1713f82dChris Lattner
3551183700f494ec9b6701b6efe82bcb25f4c79ba561John McCall  assert(T->getAs<BuiltinType>() && "getFloatingRank(): not a floating type");
3552183700f494ec9b6701b6efe82bcb25f4c79ba561John McCall  switch (T->getAs<BuiltinType>()->getKind()) {
3553b219cfc4d75f0a03630b7c4509ef791b7e97b2c8David Blaikie  default: llvm_unreachable("getFloatingRank(): not a floating type");
3554aa4a99b4a62615db243f7a5c433169f2fc704420Anton Korobeynikov  case BuiltinType::Half:       return HalfRank;
35555f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case BuiltinType::Float:      return FloatRank;
35565f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case BuiltinType::Double:     return DoubleRank;
35575f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case BuiltinType::LongDouble: return LongDoubleRank;
35585f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
35595f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
35605f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
35611eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// getFloatingTypeOfSizeWithinDomain - Returns a real floating
35621eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// point or a complex type (based on typeDomain/typeSize).
3563716c7304ff5d27a95e1e7823acd1d09d5ec3e37fSteve Naroff/// 'typeDomain' is a real floating point or complex type.
3564716c7304ff5d27a95e1e7823acd1d09d5ec3e37fSteve Naroff/// 'typeSize' is a real floating point or complex type.
35651361b11066239ea15764a2a844405352d87296b3Chris LattnerQualType ASTContext::getFloatingTypeOfSizeWithinDomain(QualType Size,
35661361b11066239ea15764a2a844405352d87296b3Chris Lattner                                                       QualType Domain) const {
35671361b11066239ea15764a2a844405352d87296b3Chris Lattner  FloatingRank EltRank = getFloatingRank(Size);
35681361b11066239ea15764a2a844405352d87296b3Chris Lattner  if (Domain->isComplexType()) {
35691361b11066239ea15764a2a844405352d87296b3Chris Lattner    switch (EltRank) {
3570561d3abc881033776ece385a01a510e1cbc1fa92David Blaikie    case HalfRank: llvm_unreachable("Complex half is not supported");
3571f1448a0e4a1e868ff873a8530a61a09cb68666ccSteve Naroff    case FloatRank:      return FloatComplexTy;
3572f1448a0e4a1e868ff873a8530a61a09cb68666ccSteve Naroff    case DoubleRank:     return DoubleComplexTy;
3573f1448a0e4a1e868ff873a8530a61a09cb68666ccSteve Naroff    case LongDoubleRank: return LongDoubleComplexTy;
3574f1448a0e4a1e868ff873a8530a61a09cb68666ccSteve Naroff    }
3575f1448a0e4a1e868ff873a8530a61a09cb68666ccSteve Naroff  }
35761361b11066239ea15764a2a844405352d87296b3Chris Lattner
35771361b11066239ea15764a2a844405352d87296b3Chris Lattner  assert(Domain->isRealFloatingType() && "Unknown domain!");
35781361b11066239ea15764a2a844405352d87296b3Chris Lattner  switch (EltRank) {
3579561d3abc881033776ece385a01a510e1cbc1fa92David Blaikie  case HalfRank: llvm_unreachable("Half ranks are not valid here");
35801361b11066239ea15764a2a844405352d87296b3Chris Lattner  case FloatRank:      return FloatTy;
35811361b11066239ea15764a2a844405352d87296b3Chris Lattner  case DoubleRank:     return DoubleTy;
35821361b11066239ea15764a2a844405352d87296b3Chris Lattner  case LongDoubleRank: return LongDoubleTy;
35835f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
3584561d3abc881033776ece385a01a510e1cbc1fa92David Blaikie  llvm_unreachable("getFloatingRank(): illegal value for rank");
35855f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
35865f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
35877cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner/// getFloatingTypeOrder - Compare the rank of the two specified floating
35887cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner/// point types, ignoring the domain of the type (i.e. 'double' ==
35897cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner/// '_Complex double').  If LHS > RHS, return 1.  If LHS == RHS, return 0. If
35901eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// LHS < RHS, return -1.
35914ba2a17694148e16eaa8d3917f657ffcd3667be4Jay Foadint ASTContext::getFloatingTypeOrder(QualType LHS, QualType RHS) const {
3592a75cea3f6be0daa8054d36af81a6ffda1713f82dChris Lattner  FloatingRank LHSR = getFloatingRank(LHS);
3593a75cea3f6be0daa8054d36af81a6ffda1713f82dChris Lattner  FloatingRank RHSR = getFloatingRank(RHS);
35941eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
3595a75cea3f6be0daa8054d36af81a6ffda1713f82dChris Lattner  if (LHSR == RHSR)
3596fb0d49669aa370b4c0993c5cee60275ef9fd6518Steve Naroff    return 0;
3597a75cea3f6be0daa8054d36af81a6ffda1713f82dChris Lattner  if (LHSR > RHSR)
3598fb0d49669aa370b4c0993c5cee60275ef9fd6518Steve Naroff    return 1;
3599fb0d49669aa370b4c0993c5cee60275ef9fd6518Steve Naroff  return -1;
36005f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
36015f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
3602f52ab250ff92bc51a9ac9a8e19bd43b63a5f844fChris Lattner/// getIntegerRank - Return an integer conversion rank (C99 6.3.1.1p1). This
3603f52ab250ff92bc51a9ac9a8e19bd43b63a5f844fChris Lattner/// routine will assert if passed a built-in type that isn't an integer or enum,
3604f52ab250ff92bc51a9ac9a8e19bd43b63a5f844fChris Lattner/// or if it is not canonicalized.
3605f4c7371fb1d3cebcfb40abad4537bb82515704eaJohn McCallunsigned ASTContext::getIntegerRank(const Type *T) const {
3606467b27b9a24bdc823218ad1ad0e37673b6cc1e83John McCall  assert(T->isCanonicalUnqualified() && "T should be canonicalized");
3607f5c209d23b20ada4a9b6235db50317239cbf6ae1Alisdair Meredith
3608f52ab250ff92bc51a9ac9a8e19bd43b63a5f844fChris Lattner  switch (cast<BuiltinType>(T)->getKind()) {
3609b219cfc4d75f0a03630b7c4509ef791b7e97b2c8David Blaikie  default: llvm_unreachable("getIntegerRank(): not a built-in integer");
36107cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner  case BuiltinType::Bool:
3611f98aba35e6c3da5aae61843fc01334939e4e12ecEli Friedman    return 1 + (getIntWidth(BoolTy) << 3);
36127cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner  case BuiltinType::Char_S:
36137cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner  case BuiltinType::Char_U:
36147cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner  case BuiltinType::SChar:
36157cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner  case BuiltinType::UChar:
3616f98aba35e6c3da5aae61843fc01334939e4e12ecEli Friedman    return 2 + (getIntWidth(CharTy) << 3);
36177cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner  case BuiltinType::Short:
36187cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner  case BuiltinType::UShort:
3619f98aba35e6c3da5aae61843fc01334939e4e12ecEli Friedman    return 3 + (getIntWidth(ShortTy) << 3);
36207cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner  case BuiltinType::Int:
36217cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner  case BuiltinType::UInt:
3622f98aba35e6c3da5aae61843fc01334939e4e12ecEli Friedman    return 4 + (getIntWidth(IntTy) << 3);
36237cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner  case BuiltinType::Long:
36247cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner  case BuiltinType::ULong:
3625f98aba35e6c3da5aae61843fc01334939e4e12ecEli Friedman    return 5 + (getIntWidth(LongTy) << 3);
36267cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner  case BuiltinType::LongLong:
36277cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner  case BuiltinType::ULongLong:
3628f98aba35e6c3da5aae61843fc01334939e4e12ecEli Friedman    return 6 + (getIntWidth(LongLongTy) << 3);
36292df9ced9fd1e8c7d7b38443db07e0e811de22571Chris Lattner  case BuiltinType::Int128:
36302df9ced9fd1e8c7d7b38443db07e0e811de22571Chris Lattner  case BuiltinType::UInt128:
36312df9ced9fd1e8c7d7b38443db07e0e811de22571Chris Lattner    return 7 + (getIntWidth(Int128Ty) << 3);
3632f52ab250ff92bc51a9ac9a8e19bd43b63a5f844fChris Lattner  }
3633f52ab250ff92bc51a9ac9a8e19bd43b63a5f844fChris Lattner}
3634f52ab250ff92bc51a9ac9a8e19bd43b63a5f844fChris Lattner
363504e8357f6801e9ff52673e7e899a67bbabf9de93Eli Friedman/// \brief Whether this is a promotable bitfield reference according
363604e8357f6801e9ff52673e7e899a67bbabf9de93Eli Friedman/// to C99 6.3.1.1p2, bullet 2 (and GCC extensions).
363704e8357f6801e9ff52673e7e899a67bbabf9de93Eli Friedman///
363804e8357f6801e9ff52673e7e899a67bbabf9de93Eli Friedman/// \returns the type this bit-field will promote to, or NULL if no
363904e8357f6801e9ff52673e7e899a67bbabf9de93Eli Friedman/// promotion occurs.
36404ba2a17694148e16eaa8d3917f657ffcd3667be4Jay FoadQualType ASTContext::isPromotableBitField(Expr *E) const {
3641ceafbdeb93ecf323cca74e660bf54504c86f3b71Douglas Gregor  if (E->isTypeDependent() || E->isValueDependent())
3642ceafbdeb93ecf323cca74e660bf54504c86f3b71Douglas Gregor    return QualType();
3643ceafbdeb93ecf323cca74e660bf54504c86f3b71Douglas Gregor
364404e8357f6801e9ff52673e7e899a67bbabf9de93Eli Friedman  FieldDecl *Field = E->getBitField();
364504e8357f6801e9ff52673e7e899a67bbabf9de93Eli Friedman  if (!Field)
364604e8357f6801e9ff52673e7e899a67bbabf9de93Eli Friedman    return QualType();
364704e8357f6801e9ff52673e7e899a67bbabf9de93Eli Friedman
364804e8357f6801e9ff52673e7e899a67bbabf9de93Eli Friedman  QualType FT = Field->getType();
364904e8357f6801e9ff52673e7e899a67bbabf9de93Eli Friedman
3650a6b8b2c09610b8bc4330e948ece8b940c2386406Richard Smith  uint64_t BitWidth = Field->getBitWidthValue(*this);
365104e8357f6801e9ff52673e7e899a67bbabf9de93Eli Friedman  uint64_t IntSize = getTypeSize(IntTy);
365204e8357f6801e9ff52673e7e899a67bbabf9de93Eli Friedman  // GCC extension compatibility: if the bit-field size is less than or equal
365304e8357f6801e9ff52673e7e899a67bbabf9de93Eli Friedman  // to the size of int, it gets promoted no matter what its type is.
365404e8357f6801e9ff52673e7e899a67bbabf9de93Eli Friedman  // For instance, unsigned long bf : 4 gets promoted to signed int.
365504e8357f6801e9ff52673e7e899a67bbabf9de93Eli Friedman  if (BitWidth < IntSize)
365604e8357f6801e9ff52673e7e899a67bbabf9de93Eli Friedman    return IntTy;
365704e8357f6801e9ff52673e7e899a67bbabf9de93Eli Friedman
365804e8357f6801e9ff52673e7e899a67bbabf9de93Eli Friedman  if (BitWidth == IntSize)
365904e8357f6801e9ff52673e7e899a67bbabf9de93Eli Friedman    return FT->isSignedIntegerType() ? IntTy : UnsignedIntTy;
366004e8357f6801e9ff52673e7e899a67bbabf9de93Eli Friedman
366104e8357f6801e9ff52673e7e899a67bbabf9de93Eli Friedman  // Types bigger than int are not subject to promotions, and therefore act
366204e8357f6801e9ff52673e7e899a67bbabf9de93Eli Friedman  // like the base type.
366304e8357f6801e9ff52673e7e899a67bbabf9de93Eli Friedman  // FIXME: This doesn't quite match what gcc does, but what gcc does here
366404e8357f6801e9ff52673e7e899a67bbabf9de93Eli Friedman  // is ridiculous.
366504e8357f6801e9ff52673e7e899a67bbabf9de93Eli Friedman  return QualType();
366604e8357f6801e9ff52673e7e899a67bbabf9de93Eli Friedman}
366704e8357f6801e9ff52673e7e899a67bbabf9de93Eli Friedman
3668a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman/// getPromotedIntegerType - Returns the type that Promotable will
3669a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman/// promote to: C99 6.3.1.1p2, assuming that Promotable is a promotable
3670a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman/// integer type.
36714ba2a17694148e16eaa8d3917f657ffcd3667be4Jay FoadQualType ASTContext::getPromotedIntegerType(QualType Promotable) const {
3672a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman  assert(!Promotable.isNull());
3673a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman  assert(Promotable->isPromotableIntegerType());
3674842aef8d942a880eeb9535d40de31a86838264cbJohn McCall  if (const EnumType *ET = Promotable->getAs<EnumType>())
3675842aef8d942a880eeb9535d40de31a86838264cbJohn McCall    return ET->getDecl()->getPromotionType();
367668a2dc446fe6d32d5da3557902100ed06b21b12bEli Friedman
367768a2dc446fe6d32d5da3557902100ed06b21b12bEli Friedman  if (const BuiltinType *BT = Promotable->getAs<BuiltinType>()) {
367868a2dc446fe6d32d5da3557902100ed06b21b12bEli Friedman    // C++ [conv.prom]: A prvalue of type char16_t, char32_t, or wchar_t
367968a2dc446fe6d32d5da3557902100ed06b21b12bEli Friedman    // (3.9.1) can be converted to a prvalue of the first of the following
368068a2dc446fe6d32d5da3557902100ed06b21b12bEli Friedman    // types that can represent all the values of its underlying type:
368168a2dc446fe6d32d5da3557902100ed06b21b12bEli Friedman    // int, unsigned int, long int, unsigned long int, long long int, or
368268a2dc446fe6d32d5da3557902100ed06b21b12bEli Friedman    // unsigned long long int [...]
368368a2dc446fe6d32d5da3557902100ed06b21b12bEli Friedman    // FIXME: Is there some better way to compute this?
368468a2dc446fe6d32d5da3557902100ed06b21b12bEli Friedman    if (BT->getKind() == BuiltinType::WChar_S ||
368568a2dc446fe6d32d5da3557902100ed06b21b12bEli Friedman        BT->getKind() == BuiltinType::WChar_U ||
368668a2dc446fe6d32d5da3557902100ed06b21b12bEli Friedman        BT->getKind() == BuiltinType::Char16 ||
368768a2dc446fe6d32d5da3557902100ed06b21b12bEli Friedman        BT->getKind() == BuiltinType::Char32) {
368868a2dc446fe6d32d5da3557902100ed06b21b12bEli Friedman      bool FromIsSigned = BT->getKind() == BuiltinType::WChar_S;
368968a2dc446fe6d32d5da3557902100ed06b21b12bEli Friedman      uint64_t FromSize = getTypeSize(BT);
369068a2dc446fe6d32d5da3557902100ed06b21b12bEli Friedman      QualType PromoteTypes[] = { IntTy, UnsignedIntTy, LongTy, UnsignedLongTy,
369168a2dc446fe6d32d5da3557902100ed06b21b12bEli Friedman                                  LongLongTy, UnsignedLongLongTy };
369268a2dc446fe6d32d5da3557902100ed06b21b12bEli Friedman      for (size_t Idx = 0; Idx < llvm::array_lengthof(PromoteTypes); ++Idx) {
369368a2dc446fe6d32d5da3557902100ed06b21b12bEli Friedman        uint64_t ToSize = getTypeSize(PromoteTypes[Idx]);
369468a2dc446fe6d32d5da3557902100ed06b21b12bEli Friedman        if (FromSize < ToSize ||
369568a2dc446fe6d32d5da3557902100ed06b21b12bEli Friedman            (FromSize == ToSize &&
369668a2dc446fe6d32d5da3557902100ed06b21b12bEli Friedman             FromIsSigned == PromoteTypes[Idx]->isSignedIntegerType()))
369768a2dc446fe6d32d5da3557902100ed06b21b12bEli Friedman          return PromoteTypes[Idx];
369868a2dc446fe6d32d5da3557902100ed06b21b12bEli Friedman      }
369968a2dc446fe6d32d5da3557902100ed06b21b12bEli Friedman      llvm_unreachable("char type should fit into long long");
370068a2dc446fe6d32d5da3557902100ed06b21b12bEli Friedman    }
370168a2dc446fe6d32d5da3557902100ed06b21b12bEli Friedman  }
370268a2dc446fe6d32d5da3557902100ed06b21b12bEli Friedman
370368a2dc446fe6d32d5da3557902100ed06b21b12bEli Friedman  // At this point, we should have a signed or unsigned integer type.
3704a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman  if (Promotable->isSignedIntegerType())
3705a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman    return IntTy;
3706a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman  uint64_t PromotableSize = getTypeSize(Promotable);
3707a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman  uint64_t IntSize = getTypeSize(IntTy);
3708a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman  assert(Promotable->isUnsignedIntegerType() && PromotableSize <= IntSize);
3709a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman  return (PromotableSize != IntSize) ? IntTy : UnsignedIntTy;
3710a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman}
3711a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman
371231862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis/// \brief Recurses in pointer/array types until it finds an objc retainable
371331862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis/// type and returns its ownership.
371431862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios KyrtzidisQualifiers::ObjCLifetime ASTContext::getInnerObjCOwnership(QualType T) const {
371531862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis  while (!T.isNull()) {
371631862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis    if (T.getObjCLifetime() != Qualifiers::OCL_None)
371731862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis      return T.getObjCLifetime();
371831862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis    if (T->isArrayType())
371931862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis      T = getBaseElementType(T);
372031862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis    else if (const PointerType *PT = T->getAs<PointerType>())
372131862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis      T = PT->getPointeeType();
372231862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis    else if (const ReferenceType *RT = T->getAs<ReferenceType>())
372328445f0b62f6aed851ff87ce64d9b19200d3211fArgyrios Kyrtzidis      T = RT->getPointeeType();
372431862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis    else
372531862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis      break;
372631862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis  }
372731862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis
372831862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis  return Qualifiers::OCL_None;
372931862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis}
373031862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis
37311eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// getIntegerTypeOrder - Returns the highest ranked integer type:
37327cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner/// C99 6.3.1.8p1.  If LHS > RHS, return 1.  If LHS == RHS, return 0. If
37331eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// LHS < RHS, return -1.
37344ba2a17694148e16eaa8d3917f657ffcd3667be4Jay Foadint ASTContext::getIntegerTypeOrder(QualType LHS, QualType RHS) const {
3735f4c7371fb1d3cebcfb40abad4537bb82515704eaJohn McCall  const Type *LHSC = getCanonicalType(LHS).getTypePtr();
3736f4c7371fb1d3cebcfb40abad4537bb82515704eaJohn McCall  const Type *RHSC = getCanonicalType(RHS).getTypePtr();
37377cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner  if (LHSC == RHSC) return 0;
37381eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
3739f52ab250ff92bc51a9ac9a8e19bd43b63a5f844fChris Lattner  bool LHSUnsigned = LHSC->isUnsignedIntegerType();
3740f52ab250ff92bc51a9ac9a8e19bd43b63a5f844fChris Lattner  bool RHSUnsigned = RHSC->isUnsignedIntegerType();
37411eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
37427cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner  unsigned LHSRank = getIntegerRank(LHSC);
37437cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner  unsigned RHSRank = getIntegerRank(RHSC);
37441eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
37457cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner  if (LHSUnsigned == RHSUnsigned) {  // Both signed or both unsigned.
37467cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner    if (LHSRank == RHSRank) return 0;
37477cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner    return LHSRank > RHSRank ? 1 : -1;
37487cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner  }
37491eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
37507cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner  // Otherwise, the LHS is signed and the RHS is unsigned or visa versa.
37517cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner  if (LHSUnsigned) {
37527cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner    // If the unsigned [LHS] type is larger, return it.
37537cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner    if (LHSRank >= RHSRank)
37547cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner      return 1;
37551eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
37567cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner    // If the signed type can represent all values of the unsigned type, it
37577cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner    // wins.  Because we are dealing with 2's complement and types that are
37581eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    // powers of two larger than each other, this is always safe.
37597cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner    return -1;
37607cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner  }
37617cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner
37627cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner  // If the unsigned [RHS] type is larger, return it.
37637cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner  if (RHSRank >= LHSRank)
37647cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner    return -1;
37651eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
37667cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner  // If the signed type can represent all values of the unsigned type, it
37677cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner  // wins.  Because we are dealing with 2's complement and types that are
37681eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  // powers of two larger than each other, this is always safe.
37697cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner  return 1;
37705f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
377171993dd85eed9cc42c6b2fa61ee5c53026b74817Anders Carlsson
377279cbc7dd2aacd85a28f469b5dc73c4ea296e7072Anders Carlssonstatic RecordDecl *
3773ba877adeb49ed6dc17f27fa3a3bcd0cca713fd68Abramo BagnaraCreateRecordDecl(const ASTContext &Ctx, RecordDecl::TagKind TK,
3774ba877adeb49ed6dc17f27fa3a3bcd0cca713fd68Abramo Bagnara                 DeclContext *DC, IdentifierInfo *Id) {
3775ba877adeb49ed6dc17f27fa3a3bcd0cca713fd68Abramo Bagnara  SourceLocation Loc;
377679cbc7dd2aacd85a28f469b5dc73c4ea296e7072Anders Carlsson  if (Ctx.getLangOptions().CPlusPlus)
3777ba877adeb49ed6dc17f27fa3a3bcd0cca713fd68Abramo Bagnara    return CXXRecordDecl::Create(Ctx, TK, DC, Loc, Loc, Id);
377879cbc7dd2aacd85a28f469b5dc73c4ea296e7072Anders Carlsson  else
3779ba877adeb49ed6dc17f27fa3a3bcd0cca713fd68Abramo Bagnara    return RecordDecl::Create(Ctx, TK, DC, Loc, Loc, Id);
378079cbc7dd2aacd85a28f469b5dc73c4ea296e7072Anders Carlsson}
3781ba877adeb49ed6dc17f27fa3a3bcd0cca713fd68Abramo Bagnara
37821eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump// getCFConstantStringType - Return the type used for constant CFStrings.
37834ba2a17694148e16eaa8d3917f657ffcd3667be4Jay FoadQualType ASTContext::getCFConstantStringType() const {
378471993dd85eed9cc42c6b2fa61ee5c53026b74817Anders Carlsson  if (!CFConstantStringTypeDecl) {
37851eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    CFConstantStringTypeDecl =
3786ba877adeb49ed6dc17f27fa3a3bcd0cca713fd68Abramo Bagnara      CreateRecordDecl(*this, TTK_Struct, TUDecl,
378779cbc7dd2aacd85a28f469b5dc73c4ea296e7072Anders Carlsson                       &Idents.get("NSConstantString"));
37885cfa011e61e14e6f2e1659047d809706c0e4c6a3John McCall    CFConstantStringTypeDecl->startDefinition();
378979cbc7dd2aacd85a28f469b5dc73c4ea296e7072Anders Carlsson
3790f06273f8bbacb086a46bde456429c8d08f6d07eeAnders Carlsson    QualType FieldTypes[4];
37911eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
379271993dd85eed9cc42c6b2fa61ee5c53026b74817Anders Carlsson    // const int *isa;
37930953e767ff7817f97b3ab20896b229891eeff45bJohn McCall    FieldTypes[0] = getPointerType(IntTy.withConst());
3794f06273f8bbacb086a46bde456429c8d08f6d07eeAnders Carlsson    // int flags;
3795f06273f8bbacb086a46bde456429c8d08f6d07eeAnders Carlsson    FieldTypes[1] = IntTy;
379671993dd85eed9cc42c6b2fa61ee5c53026b74817Anders Carlsson    // const char *str;
37970953e767ff7817f97b3ab20896b229891eeff45bJohn McCall    FieldTypes[2] = getPointerType(CharTy.withConst());
379871993dd85eed9cc42c6b2fa61ee5c53026b74817Anders Carlsson    // long length;
37991eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    FieldTypes[3] = LongTy;
38001eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
380144b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor    // Create fields
380244b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor    for (unsigned i = 0; i < 4; ++i) {
38031eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      FieldDecl *Field = FieldDecl::Create(*this, CFConstantStringTypeDecl,
3804ff676cb48fe8bf7be2feaa251dc7c5fb15af4730Abramo Bagnara                                           SourceLocation(),
380544b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor                                           SourceLocation(), 0,
3806a93c934af4fbf97cbe8e649d82e68ccacfe57c95John McCall                                           FieldTypes[i], /*TInfo=*/0,
38071eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump                                           /*BitWidth=*/0,
38087a614d8380297fcd2bc23986241905d97222948cRichard Smith                                           /*Mutable=*/false,
38097a614d8380297fcd2bc23986241905d97222948cRichard Smith                                           /*HasInit=*/false);
38102888b65aae768f54062505330df7be230a0510c7John McCall      Field->setAccess(AS_public);
381117945a0f64fe03ff6ec0c2146005a87636e3ac12Argyrios Kyrtzidis      CFConstantStringTypeDecl->addDecl(Field);
381244b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor    }
381344b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor
3814838db383b69b9fb55f55c8e9546477df198a4faaDouglas Gregor    CFConstantStringTypeDecl->completeDefinition();
381571993dd85eed9cc42c6b2fa61ee5c53026b74817Anders Carlsson  }
38161eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
381771993dd85eed9cc42c6b2fa61ee5c53026b74817Anders Carlsson  return getTagDeclType(CFConstantStringTypeDecl);
38188467583c2704e7a9691ea56939a029015f0ade0aGabor Greif}
3819b2cf3573d7351094f6247fcca94703ce88eb9ee0Anders Carlsson
3820319ac896a0fef7365d5589b8021db7e41207fe42Douglas Gregorvoid ASTContext::setCFConstantStringType(QualType T) {
38216217b80b7a1379b74cced1c076338262c3c980b3Ted Kremenek  const RecordType *Rec = T->getAs<RecordType>();
3822319ac896a0fef7365d5589b8021db7e41207fe42Douglas Gregor  assert(Rec && "Invalid CFConstantStringType");
3823319ac896a0fef7365d5589b8021db7e41207fe42Douglas Gregor  CFConstantStringTypeDecl = Rec->getDecl();
3824319ac896a0fef7365d5589b8021db7e41207fe42Douglas Gregor}
3825319ac896a0fef7365d5589b8021db7e41207fe42Douglas Gregor
38264ba2a17694148e16eaa8d3917f657ffcd3667be4Jay FoadQualType ASTContext::getBlockDescriptorType() const {
3827adaaad3715c9c26cdcfdfe3401a13d7b4423ddcfMike Stump  if (BlockDescriptorType)
3828adaaad3715c9c26cdcfdfe3401a13d7b4423ddcfMike Stump    return getTagDeclType(BlockDescriptorType);
3829adaaad3715c9c26cdcfdfe3401a13d7b4423ddcfMike Stump
3830adaaad3715c9c26cdcfdfe3401a13d7b4423ddcfMike Stump  RecordDecl *T;
3831adaaad3715c9c26cdcfdfe3401a13d7b4423ddcfMike Stump  // FIXME: Needs the FlagAppleBlock bit.
3832ba877adeb49ed6dc17f27fa3a3bcd0cca713fd68Abramo Bagnara  T = CreateRecordDecl(*this, TTK_Struct, TUDecl,
383379cbc7dd2aacd85a28f469b5dc73c4ea296e7072Anders Carlsson                       &Idents.get("__block_descriptor"));
38345cfa011e61e14e6f2e1659047d809706c0e4c6a3John McCall  T->startDefinition();
3835adaaad3715c9c26cdcfdfe3401a13d7b4423ddcfMike Stump
3836adaaad3715c9c26cdcfdfe3401a13d7b4423ddcfMike Stump  QualType FieldTypes[] = {
3837adaaad3715c9c26cdcfdfe3401a13d7b4423ddcfMike Stump    UnsignedLongTy,
3838adaaad3715c9c26cdcfdfe3401a13d7b4423ddcfMike Stump    UnsignedLongTy,
3839adaaad3715c9c26cdcfdfe3401a13d7b4423ddcfMike Stump  };
3840adaaad3715c9c26cdcfdfe3401a13d7b4423ddcfMike Stump
3841adaaad3715c9c26cdcfdfe3401a13d7b4423ddcfMike Stump  const char *FieldNames[] = {
3842adaaad3715c9c26cdcfdfe3401a13d7b4423ddcfMike Stump    "reserved",
3843083c25eea14bb4cc4ecc3ec763c60e2e609e22bdMike Stump    "Size"
3844adaaad3715c9c26cdcfdfe3401a13d7b4423ddcfMike Stump  };
3845adaaad3715c9c26cdcfdfe3401a13d7b4423ddcfMike Stump
3846adaaad3715c9c26cdcfdfe3401a13d7b4423ddcfMike Stump  for (size_t i = 0; i < 2; ++i) {
3847ff676cb48fe8bf7be2feaa251dc7c5fb15af4730Abramo Bagnara    FieldDecl *Field = FieldDecl::Create(*this, T, SourceLocation(),
3848adaaad3715c9c26cdcfdfe3401a13d7b4423ddcfMike Stump                                         SourceLocation(),
3849adaaad3715c9c26cdcfdfe3401a13d7b4423ddcfMike Stump                                         &Idents.get(FieldNames[i]),
3850a93c934af4fbf97cbe8e649d82e68ccacfe57c95John McCall                                         FieldTypes[i], /*TInfo=*/0,
3851adaaad3715c9c26cdcfdfe3401a13d7b4423ddcfMike Stump                                         /*BitWidth=*/0,
38527a614d8380297fcd2bc23986241905d97222948cRichard Smith                                         /*Mutable=*/false,
38537a614d8380297fcd2bc23986241905d97222948cRichard Smith                                         /*HasInit=*/false);
38542888b65aae768f54062505330df7be230a0510c7John McCall    Field->setAccess(AS_public);
3855adaaad3715c9c26cdcfdfe3401a13d7b4423ddcfMike Stump    T->addDecl(Field);
3856adaaad3715c9c26cdcfdfe3401a13d7b4423ddcfMike Stump  }
3857adaaad3715c9c26cdcfdfe3401a13d7b4423ddcfMike Stump
3858838db383b69b9fb55f55c8e9546477df198a4faaDouglas Gregor  T->completeDefinition();
3859adaaad3715c9c26cdcfdfe3401a13d7b4423ddcfMike Stump
3860adaaad3715c9c26cdcfdfe3401a13d7b4423ddcfMike Stump  BlockDescriptorType = T;
3861adaaad3715c9c26cdcfdfe3401a13d7b4423ddcfMike Stump
3862adaaad3715c9c26cdcfdfe3401a13d7b4423ddcfMike Stump  return getTagDeclType(BlockDescriptorType);
3863adaaad3715c9c26cdcfdfe3401a13d7b4423ddcfMike Stump}
3864adaaad3715c9c26cdcfdfe3401a13d7b4423ddcfMike Stump
38654ba2a17694148e16eaa8d3917f657ffcd3667be4Jay FoadQualType ASTContext::getBlockDescriptorExtendedType() const {
3866083c25eea14bb4cc4ecc3ec763c60e2e609e22bdMike Stump  if (BlockDescriptorExtendedType)
3867083c25eea14bb4cc4ecc3ec763c60e2e609e22bdMike Stump    return getTagDeclType(BlockDescriptorExtendedType);
3868083c25eea14bb4cc4ecc3ec763c60e2e609e22bdMike Stump
3869083c25eea14bb4cc4ecc3ec763c60e2e609e22bdMike Stump  RecordDecl *T;
3870083c25eea14bb4cc4ecc3ec763c60e2e609e22bdMike Stump  // FIXME: Needs the FlagAppleBlock bit.
3871ba877adeb49ed6dc17f27fa3a3bcd0cca713fd68Abramo Bagnara  T = CreateRecordDecl(*this, TTK_Struct, TUDecl,
387279cbc7dd2aacd85a28f469b5dc73c4ea296e7072Anders Carlsson                       &Idents.get("__block_descriptor_withcopydispose"));
38735cfa011e61e14e6f2e1659047d809706c0e4c6a3John McCall  T->startDefinition();
3874083c25eea14bb4cc4ecc3ec763c60e2e609e22bdMike Stump
3875083c25eea14bb4cc4ecc3ec763c60e2e609e22bdMike Stump  QualType FieldTypes[] = {
3876083c25eea14bb4cc4ecc3ec763c60e2e609e22bdMike Stump    UnsignedLongTy,
3877083c25eea14bb4cc4ecc3ec763c60e2e609e22bdMike Stump    UnsignedLongTy,
3878083c25eea14bb4cc4ecc3ec763c60e2e609e22bdMike Stump    getPointerType(VoidPtrTy),
3879083c25eea14bb4cc4ecc3ec763c60e2e609e22bdMike Stump    getPointerType(VoidPtrTy)
3880083c25eea14bb4cc4ecc3ec763c60e2e609e22bdMike Stump  };
3881083c25eea14bb4cc4ecc3ec763c60e2e609e22bdMike Stump
3882083c25eea14bb4cc4ecc3ec763c60e2e609e22bdMike Stump  const char *FieldNames[] = {
3883083c25eea14bb4cc4ecc3ec763c60e2e609e22bdMike Stump    "reserved",
3884083c25eea14bb4cc4ecc3ec763c60e2e609e22bdMike Stump    "Size",
3885083c25eea14bb4cc4ecc3ec763c60e2e609e22bdMike Stump    "CopyFuncPtr",
3886083c25eea14bb4cc4ecc3ec763c60e2e609e22bdMike Stump    "DestroyFuncPtr"
3887083c25eea14bb4cc4ecc3ec763c60e2e609e22bdMike Stump  };
3888083c25eea14bb4cc4ecc3ec763c60e2e609e22bdMike Stump
3889083c25eea14bb4cc4ecc3ec763c60e2e609e22bdMike Stump  for (size_t i = 0; i < 4; ++i) {
3890ff676cb48fe8bf7be2feaa251dc7c5fb15af4730Abramo Bagnara    FieldDecl *Field = FieldDecl::Create(*this, T, SourceLocation(),
3891083c25eea14bb4cc4ecc3ec763c60e2e609e22bdMike Stump                                         SourceLocation(),
3892083c25eea14bb4cc4ecc3ec763c60e2e609e22bdMike Stump                                         &Idents.get(FieldNames[i]),
3893a93c934af4fbf97cbe8e649d82e68ccacfe57c95John McCall                                         FieldTypes[i], /*TInfo=*/0,
3894083c25eea14bb4cc4ecc3ec763c60e2e609e22bdMike Stump                                         /*BitWidth=*/0,
38957a614d8380297fcd2bc23986241905d97222948cRichard Smith                                         /*Mutable=*/false,
38967a614d8380297fcd2bc23986241905d97222948cRichard Smith                                         /*HasInit=*/false);
38972888b65aae768f54062505330df7be230a0510c7John McCall    Field->setAccess(AS_public);
3898083c25eea14bb4cc4ecc3ec763c60e2e609e22bdMike Stump    T->addDecl(Field);
3899083c25eea14bb4cc4ecc3ec763c60e2e609e22bdMike Stump  }
3900083c25eea14bb4cc4ecc3ec763c60e2e609e22bdMike Stump
3901838db383b69b9fb55f55c8e9546477df198a4faaDouglas Gregor  T->completeDefinition();
3902083c25eea14bb4cc4ecc3ec763c60e2e609e22bdMike Stump
3903083c25eea14bb4cc4ecc3ec763c60e2e609e22bdMike Stump  BlockDescriptorExtendedType = T;
3904083c25eea14bb4cc4ecc3ec763c60e2e609e22bdMike Stump
3905083c25eea14bb4cc4ecc3ec763c60e2e609e22bdMike Stump  return getTagDeclType(BlockDescriptorExtendedType);
3906083c25eea14bb4cc4ecc3ec763c60e2e609e22bdMike Stump}
3907083c25eea14bb4cc4ecc3ec763c60e2e609e22bdMike Stump
39084ba2a17694148e16eaa8d3917f657ffcd3667be4Jay Foadbool ASTContext::BlockRequiresCopying(QualType Ty) const {
3909f85e193739c953358c865005855253af4f68a497John McCall  if (Ty->isObjCRetainableType())
3910af7b44d847d3e44c43346d508b2e55a6254b6e9dMike Stump    return true;
3911e38be617437ccdcc180c5a49e447cbcd07539292Fariborz Jahanian  if (getLangOptions().CPlusPlus) {
3912e38be617437ccdcc180c5a49e447cbcd07539292Fariborz Jahanian    if (const RecordType *RT = Ty->getAs<RecordType>()) {
3913e38be617437ccdcc180c5a49e447cbcd07539292Fariborz Jahanian      CXXRecordDecl *RD = cast<CXXRecordDecl>(RT->getDecl());
3914ffe37fdda5b4b4f162a45155c30d9f60ce110c12Sean Hunt      return RD->hasConstCopyConstructor();
3915e38be617437ccdcc180c5a49e447cbcd07539292Fariborz Jahanian
3916e38be617437ccdcc180c5a49e447cbcd07539292Fariborz Jahanian    }
3917e38be617437ccdcc180c5a49e447cbcd07539292Fariborz Jahanian  }
3918af7b44d847d3e44c43346d508b2e55a6254b6e9dMike Stump  return false;
3919af7b44d847d3e44c43346d508b2e55a6254b6e9dMike Stump}
3920af7b44d847d3e44c43346d508b2e55a6254b6e9dMike Stump
39214ba2a17694148e16eaa8d3917f657ffcd3667be4Jay FoadQualType
39225f9e272e632e951b1efe824cd16acb4d96077930Chris LattnerASTContext::BuildByRefType(StringRef DeclName, QualType Ty) const {
3923af7b44d847d3e44c43346d508b2e55a6254b6e9dMike Stump  //  type = struct __Block_byref_1_X {
3924ea26cb522e88fc86b0d1cae61dcefcfe4cc20231Mike Stump  //    void *__isa;
3925af7b44d847d3e44c43346d508b2e55a6254b6e9dMike Stump  //    struct __Block_byref_1_X *__forwarding;
3926ea26cb522e88fc86b0d1cae61dcefcfe4cc20231Mike Stump  //    unsigned int __flags;
3927ea26cb522e88fc86b0d1cae61dcefcfe4cc20231Mike Stump  //    unsigned int __size;
3928a7e6845660f91ec611427e1db842780e1ec12bdbEli Friedman  //    void *__copy_helper;            // as needed
3929a7e6845660f91ec611427e1db842780e1ec12bdbEli Friedman  //    void *__destroy_help            // as needed
3930af7b44d847d3e44c43346d508b2e55a6254b6e9dMike Stump  //    int X;
3931ea26cb522e88fc86b0d1cae61dcefcfe4cc20231Mike Stump  //  } *
3932ea26cb522e88fc86b0d1cae61dcefcfe4cc20231Mike Stump
3933af7b44d847d3e44c43346d508b2e55a6254b6e9dMike Stump  bool HasCopyAndDispose = BlockRequiresCopying(Ty);
3934af7b44d847d3e44c43346d508b2e55a6254b6e9dMike Stump
3935af7b44d847d3e44c43346d508b2e55a6254b6e9dMike Stump  // FIXME: Move up
3936f5942a44880be26878592eb052b737579349411eBenjamin Kramer  llvm::SmallString<36> Name;
3937f5942a44880be26878592eb052b737579349411eBenjamin Kramer  llvm::raw_svector_ostream(Name) << "__Block_byref_" <<
3938f5942a44880be26878592eb052b737579349411eBenjamin Kramer                                  ++UniqueBlockByRefTypeID << '_' << DeclName;
3939af7b44d847d3e44c43346d508b2e55a6254b6e9dMike Stump  RecordDecl *T;
3940ba877adeb49ed6dc17f27fa3a3bcd0cca713fd68Abramo Bagnara  T = CreateRecordDecl(*this, TTK_Struct, TUDecl, &Idents.get(Name.str()));
3941af7b44d847d3e44c43346d508b2e55a6254b6e9dMike Stump  T->startDefinition();
3942af7b44d847d3e44c43346d508b2e55a6254b6e9dMike Stump  QualType Int32Ty = IntTy;
3943af7b44d847d3e44c43346d508b2e55a6254b6e9dMike Stump  assert(getIntWidth(IntTy) == 32 && "non-32bit int not supported");
3944af7b44d847d3e44c43346d508b2e55a6254b6e9dMike Stump  QualType FieldTypes[] = {
3945af7b44d847d3e44c43346d508b2e55a6254b6e9dMike Stump    getPointerType(VoidPtrTy),
3946af7b44d847d3e44c43346d508b2e55a6254b6e9dMike Stump    getPointerType(getTagDeclType(T)),
3947af7b44d847d3e44c43346d508b2e55a6254b6e9dMike Stump    Int32Ty,
3948af7b44d847d3e44c43346d508b2e55a6254b6e9dMike Stump    Int32Ty,
3949af7b44d847d3e44c43346d508b2e55a6254b6e9dMike Stump    getPointerType(VoidPtrTy),
3950af7b44d847d3e44c43346d508b2e55a6254b6e9dMike Stump    getPointerType(VoidPtrTy),
3951af7b44d847d3e44c43346d508b2e55a6254b6e9dMike Stump    Ty
3952af7b44d847d3e44c43346d508b2e55a6254b6e9dMike Stump  };
3953af7b44d847d3e44c43346d508b2e55a6254b6e9dMike Stump
39545f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner  StringRef FieldNames[] = {
3955af7b44d847d3e44c43346d508b2e55a6254b6e9dMike Stump    "__isa",
3956af7b44d847d3e44c43346d508b2e55a6254b6e9dMike Stump    "__forwarding",
3957af7b44d847d3e44c43346d508b2e55a6254b6e9dMike Stump    "__flags",
3958af7b44d847d3e44c43346d508b2e55a6254b6e9dMike Stump    "__size",
3959af7b44d847d3e44c43346d508b2e55a6254b6e9dMike Stump    "__copy_helper",
3960af7b44d847d3e44c43346d508b2e55a6254b6e9dMike Stump    "__destroy_helper",
3961af7b44d847d3e44c43346d508b2e55a6254b6e9dMike Stump    DeclName,
3962af7b44d847d3e44c43346d508b2e55a6254b6e9dMike Stump  };
3963af7b44d847d3e44c43346d508b2e55a6254b6e9dMike Stump
3964af7b44d847d3e44c43346d508b2e55a6254b6e9dMike Stump  for (size_t i = 0; i < 7; ++i) {
3965af7b44d847d3e44c43346d508b2e55a6254b6e9dMike Stump    if (!HasCopyAndDispose && i >=4 && i <= 5)
3966af7b44d847d3e44c43346d508b2e55a6254b6e9dMike Stump      continue;
3967af7b44d847d3e44c43346d508b2e55a6254b6e9dMike Stump    FieldDecl *Field = FieldDecl::Create(*this, T, SourceLocation(),
3968ff676cb48fe8bf7be2feaa251dc7c5fb15af4730Abramo Bagnara                                         SourceLocation(),
3969af7b44d847d3e44c43346d508b2e55a6254b6e9dMike Stump                                         &Idents.get(FieldNames[i]),
3970a93c934af4fbf97cbe8e649d82e68ccacfe57c95John McCall                                         FieldTypes[i], /*TInfo=*/0,
39717a614d8380297fcd2bc23986241905d97222948cRichard Smith                                         /*BitWidth=*/0, /*Mutable=*/false,
39727a614d8380297fcd2bc23986241905d97222948cRichard Smith                                         /*HasInit=*/false);
39732888b65aae768f54062505330df7be230a0510c7John McCall    Field->setAccess(AS_public);
3974af7b44d847d3e44c43346d508b2e55a6254b6e9dMike Stump    T->addDecl(Field);
3975af7b44d847d3e44c43346d508b2e55a6254b6e9dMike Stump  }
3976af7b44d847d3e44c43346d508b2e55a6254b6e9dMike Stump
3977838db383b69b9fb55f55c8e9546477df198a4faaDouglas Gregor  T->completeDefinition();
3978af7b44d847d3e44c43346d508b2e55a6254b6e9dMike Stump
3979af7b44d847d3e44c43346d508b2e55a6254b6e9dMike Stump  return getPointerType(getTagDeclType(T));
3980ea26cb522e88fc86b0d1cae61dcefcfe4cc20231Mike Stump}
3981ea26cb522e88fc86b0d1cae61dcefcfe4cc20231Mike Stump
3982e97179c675b341927807c718be215c8d1aab8acbDouglas GregorTypedefDecl *ASTContext::getObjCInstanceTypeDecl() {
3983e97179c675b341927807c718be215c8d1aab8acbDouglas Gregor  if (!ObjCInstanceTypeDecl)
3984e97179c675b341927807c718be215c8d1aab8acbDouglas Gregor    ObjCInstanceTypeDecl = TypedefDecl::Create(*this,
3985e97179c675b341927807c718be215c8d1aab8acbDouglas Gregor                                               getTranslationUnitDecl(),
3986e97179c675b341927807c718be215c8d1aab8acbDouglas Gregor                                               SourceLocation(),
3987e97179c675b341927807c718be215c8d1aab8acbDouglas Gregor                                               SourceLocation(),
3988e97179c675b341927807c718be215c8d1aab8acbDouglas Gregor                                               &Idents.get("instancetype"),
3989e97179c675b341927807c718be215c8d1aab8acbDouglas Gregor                                     getTrivialTypeSourceInfo(getObjCIdType()));
3990e97179c675b341927807c718be215c8d1aab8acbDouglas Gregor  return ObjCInstanceTypeDecl;
3991e97179c675b341927807c718be215c8d1aab8acbDouglas Gregor}
3992e97179c675b341927807c718be215c8d1aab8acbDouglas Gregor
3993e8c49533521c40643653f943d47229e62d277f88Anders Carlsson// This returns true if a type has been typedefed to BOOL:
3994e8c49533521c40643653f943d47229e62d277f88Anders Carlsson// typedef <type> BOOL;
39952d99833e8c956775f2183601cd120b65b569c867Chris Lattnerstatic bool isTypeTypedefedAsBOOL(QualType T) {
3996e8c49533521c40643653f943d47229e62d277f88Anders Carlsson  if (const TypedefType *TT = dyn_cast<TypedefType>(T))
3997bb49c3ee5d270485f4b273691fd14bc97403fa5dChris Lattner    if (IdentifierInfo *II = TT->getDecl()->getIdentifier())
3998bb49c3ee5d270485f4b273691fd14bc97403fa5dChris Lattner      return II->isStr("BOOL");
39991eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
400085f9bceab1542aafff012d4d28e998f4ba16e362Anders Carlsson  return false;
400185f9bceab1542aafff012d4d28e998f4ba16e362Anders Carlsson}
400285f9bceab1542aafff012d4d28e998f4ba16e362Anders Carlsson
4003a526c5c67e5a0473c340903ee542ce570119665fTed Kremenek/// getObjCEncodingTypeSize returns size of type for objective-c encoding
400433e1d64ab5cd5d27f8530ccd056191fe2c9f3f2eFariborz Jahanian/// purpose.
40054ba2a17694148e16eaa8d3917f657ffcd3667be4Jay FoadCharUnits ASTContext::getObjCEncodingTypeSize(QualType type) const {
4006f968d8374791c37bc464efd9168c2d33dd73605fDouglas Gregor  if (!type->isIncompleteArrayType() && type->isIncompleteType())
4007f968d8374791c37bc464efd9168c2d33dd73605fDouglas Gregor    return CharUnits::Zero();
4008f968d8374791c37bc464efd9168c2d33dd73605fDouglas Gregor
4009199c3d6cd16aebbb9c7f0d42af9d922c9628bf70Ken Dyck  CharUnits sz = getTypeSizeInChars(type);
40101eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
401133e1d64ab5cd5d27f8530ccd056191fe2c9f3f2eFariborz Jahanian  // Make all integer and enum types at least as large as an int
40122ade35e2cfd554e49d35a52047cea98a82787af9Douglas Gregor  if (sz.isPositive() && type->isIntegralOrEnumerationType())
4013199c3d6cd16aebbb9c7f0d42af9d922c9628bf70Ken Dyck    sz = std::max(sz, getTypeSizeInChars(IntTy));
401433e1d64ab5cd5d27f8530ccd056191fe2c9f3f2eFariborz Jahanian  // Treat arrays as pointers, since that's how they're passed in.
401533e1d64ab5cd5d27f8530ccd056191fe2c9f3f2eFariborz Jahanian  else if (type->isArrayType())
4016199c3d6cd16aebbb9c7f0d42af9d922c9628bf70Ken Dyck    sz = getTypeSizeInChars(VoidPtrTy);
4017aa8741a1db98eef05f09b1200dba94aa5dc3bc3dKen Dyck  return sz;
4018199c3d6cd16aebbb9c7f0d42af9d922c9628bf70Ken Dyck}
4019199c3d6cd16aebbb9c7f0d42af9d922c9628bf70Ken Dyck
4020199c3d6cd16aebbb9c7f0d42af9d922c9628bf70Ken Dyckstatic inline
4021199c3d6cd16aebbb9c7f0d42af9d922c9628bf70Ken Dyckstd::string charUnitsToString(const CharUnits &CU) {
4022199c3d6cd16aebbb9c7f0d42af9d922c9628bf70Ken Dyck  return llvm::itostr(CU.getQuantity());
402333e1d64ab5cd5d27f8530ccd056191fe2c9f3f2eFariborz Jahanian}
402433e1d64ab5cd5d27f8530ccd056191fe2c9f3f2eFariborz Jahanian
40256b5a61b6dc400027fd793dcadceeb9da944a37eaJohn McCall/// getObjCEncodingForBlock - Return the encoded type for this block
40265e530af5d51572a0ed5dbe50da54bd333840c63dDavid Chisnall/// declaration.
40276b5a61b6dc400027fd793dcadceeb9da944a37eaJohn McCallstd::string ASTContext::getObjCEncodingForBlock(const BlockExpr *Expr) const {
40286b5a61b6dc400027fd793dcadceeb9da944a37eaJohn McCall  std::string S;
40296b5a61b6dc400027fd793dcadceeb9da944a37eaJohn McCall
40305e530af5d51572a0ed5dbe50da54bd333840c63dDavid Chisnall  const BlockDecl *Decl = Expr->getBlockDecl();
40315e530af5d51572a0ed5dbe50da54bd333840c63dDavid Chisnall  QualType BlockTy =
40325e530af5d51572a0ed5dbe50da54bd333840c63dDavid Chisnall      Expr->getType()->getAs<BlockPointerType>()->getPointeeType();
40335e530af5d51572a0ed5dbe50da54bd333840c63dDavid Chisnall  // Encode result type.
4034c71a4915ca216847599d03cab4ed1c5086b0eb43John McCall  getObjCEncodingForType(BlockTy->getAs<FunctionType>()->getResultType(), S);
40355e530af5d51572a0ed5dbe50da54bd333840c63dDavid Chisnall  // Compute size of all parameters.
40365e530af5d51572a0ed5dbe50da54bd333840c63dDavid Chisnall  // Start with computing size of a pointer in number of bytes.
40375e530af5d51572a0ed5dbe50da54bd333840c63dDavid Chisnall  // FIXME: There might(should) be a better way of doing this computation!
40385e530af5d51572a0ed5dbe50da54bd333840c63dDavid Chisnall  SourceLocation Loc;
4039199c3d6cd16aebbb9c7f0d42af9d922c9628bf70Ken Dyck  CharUnits PtrSize = getTypeSizeInChars(VoidPtrTy);
4040199c3d6cd16aebbb9c7f0d42af9d922c9628bf70Ken Dyck  CharUnits ParmOffset = PtrSize;
40416f46c2653c1545cc3fef0c0df996d18160160ce8Fariborz Jahanian  for (BlockDecl::param_const_iterator PI = Decl->param_begin(),
40425e530af5d51572a0ed5dbe50da54bd333840c63dDavid Chisnall       E = Decl->param_end(); PI != E; ++PI) {
40435e530af5d51572a0ed5dbe50da54bd333840c63dDavid Chisnall    QualType PType = (*PI)->getType();
4044aa8741a1db98eef05f09b1200dba94aa5dc3bc3dKen Dyck    CharUnits sz = getObjCEncodingTypeSize(PType);
4045199c3d6cd16aebbb9c7f0d42af9d922c9628bf70Ken Dyck    assert (sz.isPositive() && "BlockExpr - Incomplete param type");
40465e530af5d51572a0ed5dbe50da54bd333840c63dDavid Chisnall    ParmOffset += sz;
40475e530af5d51572a0ed5dbe50da54bd333840c63dDavid Chisnall  }
40485e530af5d51572a0ed5dbe50da54bd333840c63dDavid Chisnall  // Size of the argument frame
4049199c3d6cd16aebbb9c7f0d42af9d922c9628bf70Ken Dyck  S += charUnitsToString(ParmOffset);
40505e530af5d51572a0ed5dbe50da54bd333840c63dDavid Chisnall  // Block pointer and offset.
40515e530af5d51572a0ed5dbe50da54bd333840c63dDavid Chisnall  S += "@?0";
40525e530af5d51572a0ed5dbe50da54bd333840c63dDavid Chisnall
40535e530af5d51572a0ed5dbe50da54bd333840c63dDavid Chisnall  // Argument types.
40545e530af5d51572a0ed5dbe50da54bd333840c63dDavid Chisnall  ParmOffset = PtrSize;
40555e530af5d51572a0ed5dbe50da54bd333840c63dDavid Chisnall  for (BlockDecl::param_const_iterator PI = Decl->param_begin(), E =
40565e530af5d51572a0ed5dbe50da54bd333840c63dDavid Chisnall       Decl->param_end(); PI != E; ++PI) {
40575e530af5d51572a0ed5dbe50da54bd333840c63dDavid Chisnall    ParmVarDecl *PVDecl = *PI;
40585e530af5d51572a0ed5dbe50da54bd333840c63dDavid Chisnall    QualType PType = PVDecl->getOriginalType();
40595e530af5d51572a0ed5dbe50da54bd333840c63dDavid Chisnall    if (const ArrayType *AT =
40605e530af5d51572a0ed5dbe50da54bd333840c63dDavid Chisnall          dyn_cast<ArrayType>(PType->getCanonicalTypeInternal())) {
40615e530af5d51572a0ed5dbe50da54bd333840c63dDavid Chisnall      // Use array's original type only if it has known number of
40625389f48b24937ad7b4093307128b3cbf25235654David Chisnall      // elements.
40635389f48b24937ad7b4093307128b3cbf25235654David Chisnall      if (!isa<ConstantArrayType>(AT))
40645389f48b24937ad7b4093307128b3cbf25235654David Chisnall        PType = PVDecl->getType();
40655389f48b24937ad7b4093307128b3cbf25235654David Chisnall    } else if (PType->isFunctionType())
40665389f48b24937ad7b4093307128b3cbf25235654David Chisnall      PType = PVDecl->getType();
40675389f48b24937ad7b4093307128b3cbf25235654David Chisnall    getObjCEncodingForType(PType, S);
40685389f48b24937ad7b4093307128b3cbf25235654David Chisnall    S += charUnitsToString(ParmOffset);
40695389f48b24937ad7b4093307128b3cbf25235654David Chisnall    ParmOffset += getObjCEncodingTypeSize(PType);
40705389f48b24937ad7b4093307128b3cbf25235654David Chisnall  }
40716b5a61b6dc400027fd793dcadceeb9da944a37eaJohn McCall
40726b5a61b6dc400027fd793dcadceeb9da944a37eaJohn McCall  return S;
40735389f48b24937ad7b4093307128b3cbf25235654David Chisnall}
40745389f48b24937ad7b4093307128b3cbf25235654David Chisnall
4075f968d8374791c37bc464efd9168c2d33dd73605fDouglas Gregorbool ASTContext::getObjCEncodingForFunctionDecl(const FunctionDecl *Decl,
40765389f48b24937ad7b4093307128b3cbf25235654David Chisnall                                                std::string& S) {
40775389f48b24937ad7b4093307128b3cbf25235654David Chisnall  // Encode result type.
40785389f48b24937ad7b4093307128b3cbf25235654David Chisnall  getObjCEncodingForType(Decl->getResultType(), S);
40795389f48b24937ad7b4093307128b3cbf25235654David Chisnall  CharUnits ParmOffset;
40805389f48b24937ad7b4093307128b3cbf25235654David Chisnall  // Compute size of all parameters.
40815389f48b24937ad7b4093307128b3cbf25235654David Chisnall  for (FunctionDecl::param_const_iterator PI = Decl->param_begin(),
40825389f48b24937ad7b4093307128b3cbf25235654David Chisnall       E = Decl->param_end(); PI != E; ++PI) {
40835389f48b24937ad7b4093307128b3cbf25235654David Chisnall    QualType PType = (*PI)->getType();
40845389f48b24937ad7b4093307128b3cbf25235654David Chisnall    CharUnits sz = getObjCEncodingTypeSize(PType);
4085f968d8374791c37bc464efd9168c2d33dd73605fDouglas Gregor    if (sz.isZero())
4086f968d8374791c37bc464efd9168c2d33dd73605fDouglas Gregor      return true;
4087f968d8374791c37bc464efd9168c2d33dd73605fDouglas Gregor
40885389f48b24937ad7b4093307128b3cbf25235654David Chisnall    assert (sz.isPositive() &&
4089f968d8374791c37bc464efd9168c2d33dd73605fDouglas Gregor        "getObjCEncodingForFunctionDecl - Incomplete param type");
40905389f48b24937ad7b4093307128b3cbf25235654David Chisnall    ParmOffset += sz;
40915389f48b24937ad7b4093307128b3cbf25235654David Chisnall  }
40925389f48b24937ad7b4093307128b3cbf25235654David Chisnall  S += charUnitsToString(ParmOffset);
40935389f48b24937ad7b4093307128b3cbf25235654David Chisnall  ParmOffset = CharUnits::Zero();
40945389f48b24937ad7b4093307128b3cbf25235654David Chisnall
40955389f48b24937ad7b4093307128b3cbf25235654David Chisnall  // Argument types.
40965389f48b24937ad7b4093307128b3cbf25235654David Chisnall  for (FunctionDecl::param_const_iterator PI = Decl->param_begin(),
40975389f48b24937ad7b4093307128b3cbf25235654David Chisnall       E = Decl->param_end(); PI != E; ++PI) {
40985389f48b24937ad7b4093307128b3cbf25235654David Chisnall    ParmVarDecl *PVDecl = *PI;
40995389f48b24937ad7b4093307128b3cbf25235654David Chisnall    QualType PType = PVDecl->getOriginalType();
41005389f48b24937ad7b4093307128b3cbf25235654David Chisnall    if (const ArrayType *AT =
41015389f48b24937ad7b4093307128b3cbf25235654David Chisnall          dyn_cast<ArrayType>(PType->getCanonicalTypeInternal())) {
41025389f48b24937ad7b4093307128b3cbf25235654David Chisnall      // Use array's original type only if it has known number of
41035e530af5d51572a0ed5dbe50da54bd333840c63dDavid Chisnall      // elements.
41045e530af5d51572a0ed5dbe50da54bd333840c63dDavid Chisnall      if (!isa<ConstantArrayType>(AT))
41055e530af5d51572a0ed5dbe50da54bd333840c63dDavid Chisnall        PType = PVDecl->getType();
41065e530af5d51572a0ed5dbe50da54bd333840c63dDavid Chisnall    } else if (PType->isFunctionType())
41075e530af5d51572a0ed5dbe50da54bd333840c63dDavid Chisnall      PType = PVDecl->getType();
41085e530af5d51572a0ed5dbe50da54bd333840c63dDavid Chisnall    getObjCEncodingForType(PType, S);
4109199c3d6cd16aebbb9c7f0d42af9d922c9628bf70Ken Dyck    S += charUnitsToString(ParmOffset);
4110aa8741a1db98eef05f09b1200dba94aa5dc3bc3dKen Dyck    ParmOffset += getObjCEncodingTypeSize(PType);
41115e530af5d51572a0ed5dbe50da54bd333840c63dDavid Chisnall  }
4112f968d8374791c37bc464efd9168c2d33dd73605fDouglas Gregor
4113f968d8374791c37bc464efd9168c2d33dd73605fDouglas Gregor  return false;
41145e530af5d51572a0ed5dbe50da54bd333840c63dDavid Chisnall}
41155e530af5d51572a0ed5dbe50da54bd333840c63dDavid Chisnall
4116dc8dab6fabf4bfd4f4b94bf572ac3342a5bbfcd7Bob Wilson/// getObjCEncodingForMethodParameter - Return the encoded type for a single
4117dc8dab6fabf4bfd4f4b94bf572ac3342a5bbfcd7Bob Wilson/// method parameter or return type. If Extended, include class names and
4118dc8dab6fabf4bfd4f4b94bf572ac3342a5bbfcd7Bob Wilson/// block object types.
4119dc8dab6fabf4bfd4f4b94bf572ac3342a5bbfcd7Bob Wilsonvoid ASTContext::getObjCEncodingForMethodParameter(Decl::ObjCDeclQualifier QT,
4120dc8dab6fabf4bfd4f4b94bf572ac3342a5bbfcd7Bob Wilson                                                   QualType T, std::string& S,
4121dc8dab6fabf4bfd4f4b94bf572ac3342a5bbfcd7Bob Wilson                                                   bool Extended) const {
4122dc8dab6fabf4bfd4f4b94bf572ac3342a5bbfcd7Bob Wilson  // Encode type qualifer, 'in', 'inout', etc. for the parameter.
4123dc8dab6fabf4bfd4f4b94bf572ac3342a5bbfcd7Bob Wilson  getObjCEncodingForTypeQualifier(QT, S);
4124dc8dab6fabf4bfd4f4b94bf572ac3342a5bbfcd7Bob Wilson  // Encode parameter type.
4125dc8dab6fabf4bfd4f4b94bf572ac3342a5bbfcd7Bob Wilson  getObjCEncodingForTypeImpl(T, S, true, true, 0,
4126dc8dab6fabf4bfd4f4b94bf572ac3342a5bbfcd7Bob Wilson                             true     /*OutermostType*/,
4127dc8dab6fabf4bfd4f4b94bf572ac3342a5bbfcd7Bob Wilson                             false    /*EncodingProperty*/,
4128dc8dab6fabf4bfd4f4b94bf572ac3342a5bbfcd7Bob Wilson                             false    /*StructField*/,
4129dc8dab6fabf4bfd4f4b94bf572ac3342a5bbfcd7Bob Wilson                             Extended /*EncodeBlockParameters*/,
4130dc8dab6fabf4bfd4f4b94bf572ac3342a5bbfcd7Bob Wilson                             Extended /*EncodeClassNames*/);
4131dc8dab6fabf4bfd4f4b94bf572ac3342a5bbfcd7Bob Wilson}
4132dc8dab6fabf4bfd4f4b94bf572ac3342a5bbfcd7Bob Wilson
4133a526c5c67e5a0473c340903ee542ce570119665fTed Kremenek/// getObjCEncodingForMethodDecl - Return the encoded type for this method
413433e1d64ab5cd5d27f8530ccd056191fe2c9f3f2eFariborz Jahanian/// declaration.
4135f968d8374791c37bc464efd9168c2d33dd73605fDouglas Gregorbool ASTContext::getObjCEncodingForMethodDecl(const ObjCMethodDecl *Decl,
4136dc8dab6fabf4bfd4f4b94bf572ac3342a5bbfcd7Bob Wilson                                              std::string& S,
4137dc8dab6fabf4bfd4f4b94bf572ac3342a5bbfcd7Bob Wilson                                              bool Extended) const {
4138c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar  // FIXME: This is not very efficient.
4139dc8dab6fabf4bfd4f4b94bf572ac3342a5bbfcd7Bob Wilson  // Encode return type.
4140dc8dab6fabf4bfd4f4b94bf572ac3342a5bbfcd7Bob Wilson  getObjCEncodingForMethodParameter(Decl->getObjCDeclQualifier(),
4141dc8dab6fabf4bfd4f4b94bf572ac3342a5bbfcd7Bob Wilson                                    Decl->getResultType(), S, Extended);
414233e1d64ab5cd5d27f8530ccd056191fe2c9f3f2eFariborz Jahanian  // Compute size of all parameters.
414333e1d64ab5cd5d27f8530ccd056191fe2c9f3f2eFariborz Jahanian  // Start with computing size of a pointer in number of bytes.
414433e1d64ab5cd5d27f8530ccd056191fe2c9f3f2eFariborz Jahanian  // FIXME: There might(should) be a better way of doing this computation!
414533e1d64ab5cd5d27f8530ccd056191fe2c9f3f2eFariborz Jahanian  SourceLocation Loc;
4146199c3d6cd16aebbb9c7f0d42af9d922c9628bf70Ken Dyck  CharUnits PtrSize = getTypeSizeInChars(VoidPtrTy);
414733e1d64ab5cd5d27f8530ccd056191fe2c9f3f2eFariborz Jahanian  // The first two arguments (self and _cmd) are pointers; account for
414833e1d64ab5cd5d27f8530ccd056191fe2c9f3f2eFariborz Jahanian  // their size.
4149199c3d6cd16aebbb9c7f0d42af9d922c9628bf70Ken Dyck  CharUnits ParmOffset = 2 * PtrSize;
4150491306a83c4f0f49f95a3bcbca8580cb98a91c7aArgyrios Kyrtzidis  for (ObjCMethodDecl::param_const_iterator PI = Decl->param_begin(),
41517732cc9c0fdc97a2f8cce4e5933d8103213d1aefFariborz Jahanian       E = Decl->sel_param_end(); PI != E; ++PI) {
415289951a86b594513c2a013532ed45d197413b1087Chris Lattner    QualType PType = (*PI)->getType();
4153aa8741a1db98eef05f09b1200dba94aa5dc3bc3dKen Dyck    CharUnits sz = getObjCEncodingTypeSize(PType);
4154f968d8374791c37bc464efd9168c2d33dd73605fDouglas Gregor    if (sz.isZero())
4155f968d8374791c37bc464efd9168c2d33dd73605fDouglas Gregor      return true;
4156f968d8374791c37bc464efd9168c2d33dd73605fDouglas Gregor
4157199c3d6cd16aebbb9c7f0d42af9d922c9628bf70Ken Dyck    assert (sz.isPositive() &&
4158199c3d6cd16aebbb9c7f0d42af9d922c9628bf70Ken Dyck        "getObjCEncodingForMethodDecl - Incomplete param type");
415933e1d64ab5cd5d27f8530ccd056191fe2c9f3f2eFariborz Jahanian    ParmOffset += sz;
416033e1d64ab5cd5d27f8530ccd056191fe2c9f3f2eFariborz Jahanian  }
4161199c3d6cd16aebbb9c7f0d42af9d922c9628bf70Ken Dyck  S += charUnitsToString(ParmOffset);
416233e1d64ab5cd5d27f8530ccd056191fe2c9f3f2eFariborz Jahanian  S += "@0:";
4163199c3d6cd16aebbb9c7f0d42af9d922c9628bf70Ken Dyck  S += charUnitsToString(PtrSize);
41641eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
416533e1d64ab5cd5d27f8530ccd056191fe2c9f3f2eFariborz Jahanian  // Argument types.
416633e1d64ab5cd5d27f8530ccd056191fe2c9f3f2eFariborz Jahanian  ParmOffset = 2 * PtrSize;
4167491306a83c4f0f49f95a3bcbca8580cb98a91c7aArgyrios Kyrtzidis  for (ObjCMethodDecl::param_const_iterator PI = Decl->param_begin(),
41687732cc9c0fdc97a2f8cce4e5933d8103213d1aefFariborz Jahanian       E = Decl->sel_param_end(); PI != E; ++PI) {
4169491306a83c4f0f49f95a3bcbca8580cb98a91c7aArgyrios Kyrtzidis    const ParmVarDecl *PVDecl = *PI;
41701eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    QualType PType = PVDecl->getOriginalType();
41714306d3cb9116605728252e2738df24b9f6ab53c3Fariborz Jahanian    if (const ArrayType *AT =
4172ab76d45e023fc5ae966968344e180cd09fdcc746Steve Naroff          dyn_cast<ArrayType>(PType->getCanonicalTypeInternal())) {
4173ab76d45e023fc5ae966968344e180cd09fdcc746Steve Naroff      // Use array's original type only if it has known number of
4174ab76d45e023fc5ae966968344e180cd09fdcc746Steve Naroff      // elements.
4175bb3fde337fb712c0e6da8790d431621be4793048Steve Naroff      if (!isa<ConstantArrayType>(AT))
4176ab76d45e023fc5ae966968344e180cd09fdcc746Steve Naroff        PType = PVDecl->getType();
4177ab76d45e023fc5ae966968344e180cd09fdcc746Steve Naroff    } else if (PType->isFunctionType())
4178ab76d45e023fc5ae966968344e180cd09fdcc746Steve Naroff      PType = PVDecl->getType();
4179dc8dab6fabf4bfd4f4b94bf572ac3342a5bbfcd7Bob Wilson    getObjCEncodingForMethodParameter(PVDecl->getObjCDeclQualifier(),
4180dc8dab6fabf4bfd4f4b94bf572ac3342a5bbfcd7Bob Wilson                                      PType, S, Extended);
4181199c3d6cd16aebbb9c7f0d42af9d922c9628bf70Ken Dyck    S += charUnitsToString(ParmOffset);
4182aa8741a1db98eef05f09b1200dba94aa5dc3bc3dKen Dyck    ParmOffset += getObjCEncodingTypeSize(PType);
418333e1d64ab5cd5d27f8530ccd056191fe2c9f3f2eFariborz Jahanian  }
4184f968d8374791c37bc464efd9168c2d33dd73605fDouglas Gregor
4185f968d8374791c37bc464efd9168c2d33dd73605fDouglas Gregor  return false;
418633e1d64ab5cd5d27f8530ccd056191fe2c9f3f2eFariborz Jahanian}
418733e1d64ab5cd5d27f8530ccd056191fe2c9f3f2eFariborz Jahanian
4188c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar/// getObjCEncodingForPropertyDecl - Return the encoded type for this
418983bccb85ff4b9981c4250c45494b439df8cbf983Fariborz Jahanian/// property declaration. If non-NULL, Container must be either an
4190c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar/// ObjCCategoryImplDecl or ObjCImplementationDecl; it should only be
4191c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar/// NULL when getting encodings for protocol properties.
41921eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// Property attributes are stored as a comma-delimited C string. The simple
41931eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// attributes readonly and bycopy are encoded as single characters. The
41941eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// parametrized attributes, getter=name, setter=name, and ivar=name, are
41951eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// encoded as single characters, followed by an identifier. Property types
41961eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// are also encoded as a parametrized attribute. The characters used to encode
419783bccb85ff4b9981c4250c45494b439df8cbf983Fariborz Jahanian/// these attributes are defined by the following enumeration:
419883bccb85ff4b9981c4250c45494b439df8cbf983Fariborz Jahanian/// @code
419983bccb85ff4b9981c4250c45494b439df8cbf983Fariborz Jahanian/// enum PropertyAttributes {
420083bccb85ff4b9981c4250c45494b439df8cbf983Fariborz Jahanian/// kPropertyReadOnly = 'R',   // property is read-only.
420183bccb85ff4b9981c4250c45494b439df8cbf983Fariborz Jahanian/// kPropertyBycopy = 'C',     // property is a copy of the value last assigned
420283bccb85ff4b9981c4250c45494b439df8cbf983Fariborz Jahanian/// kPropertyByref = '&',  // property is a reference to the value last assigned
420383bccb85ff4b9981c4250c45494b439df8cbf983Fariborz Jahanian/// kPropertyDynamic = 'D',    // property is dynamic
420483bccb85ff4b9981c4250c45494b439df8cbf983Fariborz Jahanian/// kPropertyGetter = 'G',     // followed by getter selector name
420583bccb85ff4b9981c4250c45494b439df8cbf983Fariborz Jahanian/// kPropertySetter = 'S',     // followed by setter selector name
420683bccb85ff4b9981c4250c45494b439df8cbf983Fariborz Jahanian/// kPropertyInstanceVariable = 'V'  // followed by instance variable  name
420783bccb85ff4b9981c4250c45494b439df8cbf983Fariborz Jahanian/// kPropertyType = 't'              // followed by old-style type encoding.
420883bccb85ff4b9981c4250c45494b439df8cbf983Fariborz Jahanian/// kPropertyWeak = 'W'              // 'weak' property
420983bccb85ff4b9981c4250c45494b439df8cbf983Fariborz Jahanian/// kPropertyStrong = 'P'            // property GC'able
421083bccb85ff4b9981c4250c45494b439df8cbf983Fariborz Jahanian/// kPropertyNonAtomic = 'N'         // property non-atomic
421183bccb85ff4b9981c4250c45494b439df8cbf983Fariborz Jahanian/// };
421283bccb85ff4b9981c4250c45494b439df8cbf983Fariborz Jahanian/// @endcode
42131eb4433ac451dc16f4133a88af2d002ac26c58efMike Stumpvoid ASTContext::getObjCEncodingForPropertyDecl(const ObjCPropertyDecl *PD,
4214c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar                                                const Decl *Container,
42154ba2a17694148e16eaa8d3917f657ffcd3667be4Jay Foad                                                std::string& S) const {
4216c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar  // Collect information from the property implementation decl(s).
4217c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar  bool Dynamic = false;
4218c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar  ObjCPropertyImplDecl *SynthesizePID = 0;
4219c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar
4220c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar  // FIXME: Duplicated code due to poor abstraction.
4221c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar  if (Container) {
42221eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    if (const ObjCCategoryImplDecl *CID =
4223c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar        dyn_cast<ObjCCategoryImplDecl>(Container)) {
4224c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar      for (ObjCCategoryImplDecl::propimpl_iterator
422517945a0f64fe03ff6ec0c2146005a87636e3ac12Argyrios Kyrtzidis             i = CID->propimpl_begin(), e = CID->propimpl_end();
4226653f1b1bf293a9bd96fd4dd6372e779cc7af1597Douglas Gregor           i != e; ++i) {
4227c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar        ObjCPropertyImplDecl *PID = *i;
4228c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar        if (PID->getPropertyDecl() == PD) {
4229c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar          if (PID->getPropertyImplementation()==ObjCPropertyImplDecl::Dynamic) {
4230c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar            Dynamic = true;
4231c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar          } else {
4232c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar            SynthesizePID = PID;
4233c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar          }
4234c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar        }
4235c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar      }
4236c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar    } else {
423761710854be2b098428aff5316e64bd34b30fbcb7Chris Lattner      const ObjCImplementationDecl *OID=cast<ObjCImplementationDecl>(Container);
4238c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar      for (ObjCCategoryImplDecl::propimpl_iterator
423917945a0f64fe03ff6ec0c2146005a87636e3ac12Argyrios Kyrtzidis             i = OID->propimpl_begin(), e = OID->propimpl_end();
4240653f1b1bf293a9bd96fd4dd6372e779cc7af1597Douglas Gregor           i != e; ++i) {
4241c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar        ObjCPropertyImplDecl *PID = *i;
4242c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar        if (PID->getPropertyDecl() == PD) {
4243c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar          if (PID->getPropertyImplementation()==ObjCPropertyImplDecl::Dynamic) {
4244c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar            Dynamic = true;
4245c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar          } else {
4246c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar            SynthesizePID = PID;
4247c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar          }
4248c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar        }
42491eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      }
4250c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar    }
4251c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar  }
4252c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar
4253c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar  // FIXME: This is not very efficient.
4254c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar  S = "T";
4255c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar
4256c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar  // Encode result type.
4257090b3f71702c5626d8520f9608d77c6f26dcfa15Fariborz Jahanian  // GCC has some special rules regarding encoding of properties which
4258090b3f71702c5626d8520f9608d77c6f26dcfa15Fariborz Jahanian  // closely resembles encoding of ivars.
42591eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  getObjCEncodingForTypeImpl(PD->getType(), S, true, true, 0,
4260090b3f71702c5626d8520f9608d77c6f26dcfa15Fariborz Jahanian                             true /* outermost type */,
4261090b3f71702c5626d8520f9608d77c6f26dcfa15Fariborz Jahanian                             true /* encoding for property */);
4262c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar
4263c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar  if (PD->isReadOnly()) {
4264c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar    S += ",R";
4265c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar  } else {
4266c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar    switch (PD->getSetterKind()) {
4267c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar    case ObjCPropertyDecl::Assign: break;
4268c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar    case ObjCPropertyDecl::Copy:   S += ",C"; break;
42691eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    case ObjCPropertyDecl::Retain: S += ",&"; break;
42703a02b44e3948f7762dbfba94b7961281ca29d022Fariborz Jahanian    case ObjCPropertyDecl::Weak:   S += ",W"; break;
4271c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar    }
4272c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar  }
4273c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar
4274c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar  // It really isn't clear at all what this means, since properties
4275c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar  // are "dynamic by default".
4276c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar  if (Dynamic)
4277c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar    S += ",D";
4278c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar
4279090b3f71702c5626d8520f9608d77c6f26dcfa15Fariborz Jahanian  if (PD->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_nonatomic)
4280090b3f71702c5626d8520f9608d77c6f26dcfa15Fariborz Jahanian    S += ",N";
42811eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
4282c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar  if (PD->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_getter) {
4283c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar    S += ",G";
4284077bf5e2f48acfa9e7d69429b6e4ba86ea14896dChris Lattner    S += PD->getGetterName().getAsString();
4285c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar  }
4286c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar
4287c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar  if (PD->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_setter) {
4288c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar    S += ",S";
4289077bf5e2f48acfa9e7d69429b6e4ba86ea14896dChris Lattner    S += PD->getSetterName().getAsString();
4290c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar  }
4291c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar
4292c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar  if (SynthesizePID) {
4293c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar    const ObjCIvarDecl *OID = SynthesizePID->getPropertyIvarDecl();
4294c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar    S += ",V";
429539f34e97d6a468f0a7dfa5664c61217cffc65b74Chris Lattner    S += OID->getNameAsString();
4296c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar  }
4297c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar
4298c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar  // FIXME: OBJCGC: weak & strong
4299c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar}
4300c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar
4301a1c033e9514865f3a7b0d8b3b20e6de926cfec6cFariborz Jahanian/// getLegacyIntegralTypeEncoding -
43021eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// Another legacy compatibility encoding: 32-bit longs are encoded as
43031eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// 'l' or 'L' , but not always.  For typedefs, we need to use
4304a1c033e9514865f3a7b0d8b3b20e6de926cfec6cFariborz Jahanian/// 'i' or 'I' instead if encoding a struct field, or a pointer!
4305a1c033e9514865f3a7b0d8b3b20e6de926cfec6cFariborz Jahanian///
4306a1c033e9514865f3a7b0d8b3b20e6de926cfec6cFariborz Jahanianvoid ASTContext::getLegacyIntegralTypeEncoding (QualType &PointeeTy) const {
43078e1fab243ab8023b7ee3899745386b3b3a4258f8Mike Stump  if (isa<TypedefType>(PointeeTy.getTypePtr())) {
4308183700f494ec9b6701b6efe82bcb25f4c79ba561John McCall    if (const BuiltinType *BT = PointeeTy->getAs<BuiltinType>()) {
43094ba2a17694148e16eaa8d3917f657ffcd3667be4Jay Foad      if (BT->getKind() == BuiltinType::ULong && getIntWidth(PointeeTy) == 32)
4310a1c033e9514865f3a7b0d8b3b20e6de926cfec6cFariborz Jahanian        PointeeTy = UnsignedIntTy;
43111eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      else
43124ba2a17694148e16eaa8d3917f657ffcd3667be4Jay Foad        if (BT->getKind() == BuiltinType::Long && getIntWidth(PointeeTy) == 32)
4313a1c033e9514865f3a7b0d8b3b20e6de926cfec6cFariborz Jahanian          PointeeTy = IntTy;
4314a1c033e9514865f3a7b0d8b3b20e6de926cfec6cFariborz Jahanian    }
4315a1c033e9514865f3a7b0d8b3b20e6de926cfec6cFariborz Jahanian  }
4316a1c033e9514865f3a7b0d8b3b20e6de926cfec6cFariborz Jahanian}
4317a1c033e9514865f3a7b0d8b3b20e6de926cfec6cFariborz Jahanian
43187d6b46d9a9d75dea8ef9f6973dd50633c1f37963Fariborz Jahanianvoid ASTContext::getObjCEncodingForType(QualType T, std::string& S,
43194ba2a17694148e16eaa8d3917f657ffcd3667be4Jay Foad                                        const FieldDecl *Field) const {
432082a6cfbc421cc99c5b7313271f399f7ef95056ecDaniel Dunbar  // We follow the behavior of gcc, expanding structures which are
432182a6cfbc421cc99c5b7313271f399f7ef95056ecDaniel Dunbar  // directly pointed to, and expanding embedded structures. Note that
432282a6cfbc421cc99c5b7313271f399f7ef95056ecDaniel Dunbar  // these rules are sufficient to prevent recursive encoding of the
432382a6cfbc421cc99c5b7313271f399f7ef95056ecDaniel Dunbar  // same type.
43241eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  getObjCEncodingForTypeImpl(T, S, true, true, Field,
43255b8c7d9fb620ba3a71e996d61e7b9bdf763b5c09Fariborz Jahanian                             true /* outermost type */);
432682a6cfbc421cc99c5b7313271f399f7ef95056ecDaniel Dunbar}
432782a6cfbc421cc99c5b7313271f399f7ef95056ecDaniel Dunbar
432864fd7e86c1a90d9ff78e4a0bd79f69499667a4e3David Chisnallstatic char ObjCEncodingForPrimitiveKind(const ASTContext *C, QualType T) {
432964fd7e86c1a90d9ff78e4a0bd79f69499667a4e3David Chisnall    switch (T->getAs<BuiltinType>()->getKind()) {
4330b219cfc4d75f0a03630b7c4509ef791b7e97b2c8David Blaikie    default: llvm_unreachable("Unhandled builtin type kind");
433164fd7e86c1a90d9ff78e4a0bd79f69499667a4e3David Chisnall    case BuiltinType::Void:       return 'v';
433264fd7e86c1a90d9ff78e4a0bd79f69499667a4e3David Chisnall    case BuiltinType::Bool:       return 'B';
433364fd7e86c1a90d9ff78e4a0bd79f69499667a4e3David Chisnall    case BuiltinType::Char_U:
433464fd7e86c1a90d9ff78e4a0bd79f69499667a4e3David Chisnall    case BuiltinType::UChar:      return 'C';
433564fd7e86c1a90d9ff78e4a0bd79f69499667a4e3David Chisnall    case BuiltinType::UShort:     return 'S';
433664fd7e86c1a90d9ff78e4a0bd79f69499667a4e3David Chisnall    case BuiltinType::UInt:       return 'I';
433764fd7e86c1a90d9ff78e4a0bd79f69499667a4e3David Chisnall    case BuiltinType::ULong:
43384ba2a17694148e16eaa8d3917f657ffcd3667be4Jay Foad        return C->getIntWidth(T) == 32 ? 'L' : 'Q';
433964fd7e86c1a90d9ff78e4a0bd79f69499667a4e3David Chisnall    case BuiltinType::UInt128:    return 'T';
434064fd7e86c1a90d9ff78e4a0bd79f69499667a4e3David Chisnall    case BuiltinType::ULongLong:  return 'Q';
434164fd7e86c1a90d9ff78e4a0bd79f69499667a4e3David Chisnall    case BuiltinType::Char_S:
434264fd7e86c1a90d9ff78e4a0bd79f69499667a4e3David Chisnall    case BuiltinType::SChar:      return 'c';
434364fd7e86c1a90d9ff78e4a0bd79f69499667a4e3David Chisnall    case BuiltinType::Short:      return 's';
43443f59c975aa5d047f7edd1b900b5e885c38af0ef7Chris Lattner    case BuiltinType::WChar_S:
43453f59c975aa5d047f7edd1b900b5e885c38af0ef7Chris Lattner    case BuiltinType::WChar_U:
434664fd7e86c1a90d9ff78e4a0bd79f69499667a4e3David Chisnall    case BuiltinType::Int:        return 'i';
434764fd7e86c1a90d9ff78e4a0bd79f69499667a4e3David Chisnall    case BuiltinType::Long:
43484ba2a17694148e16eaa8d3917f657ffcd3667be4Jay Foad      return C->getIntWidth(T) == 32 ? 'l' : 'q';
434964fd7e86c1a90d9ff78e4a0bd79f69499667a4e3David Chisnall    case BuiltinType::LongLong:   return 'q';
435064fd7e86c1a90d9ff78e4a0bd79f69499667a4e3David Chisnall    case BuiltinType::Int128:     return 't';
435164fd7e86c1a90d9ff78e4a0bd79f69499667a4e3David Chisnall    case BuiltinType::Float:      return 'f';
435264fd7e86c1a90d9ff78e4a0bd79f69499667a4e3David Chisnall    case BuiltinType::Double:     return 'd';
43533a0be84b2aed8563150cdbd976a98838afa261ebDaniel Dunbar    case BuiltinType::LongDouble: return 'D';
435464fd7e86c1a90d9ff78e4a0bd79f69499667a4e3David Chisnall    }
435564fd7e86c1a90d9ff78e4a0bd79f69499667a4e3David Chisnall}
435664fd7e86c1a90d9ff78e4a0bd79f69499667a4e3David Chisnall
43575471bc85b69912e3b448de004498a80c0de32296Douglas Gregorstatic char ObjCEncodingForEnumType(const ASTContext *C, const EnumType *ET) {
43585471bc85b69912e3b448de004498a80c0de32296Douglas Gregor  EnumDecl *Enum = ET->getDecl();
43595471bc85b69912e3b448de004498a80c0de32296Douglas Gregor
43605471bc85b69912e3b448de004498a80c0de32296Douglas Gregor  // The encoding of an non-fixed enum type is always 'i', regardless of size.
43615471bc85b69912e3b448de004498a80c0de32296Douglas Gregor  if (!Enum->isFixed())
43625471bc85b69912e3b448de004498a80c0de32296Douglas Gregor    return 'i';
43635471bc85b69912e3b448de004498a80c0de32296Douglas Gregor
43645471bc85b69912e3b448de004498a80c0de32296Douglas Gregor  // The encoding of a fixed enum type matches its fixed underlying type.
43655471bc85b69912e3b448de004498a80c0de32296Douglas Gregor  return ObjCEncodingForPrimitiveKind(C, Enum->getIntegerType());
43665471bc85b69912e3b448de004498a80c0de32296Douglas Gregor}
43675471bc85b69912e3b448de004498a80c0de32296Douglas Gregor
43684ba2a17694148e16eaa8d3917f657ffcd3667be4Jay Foadstatic void EncodeBitField(const ASTContext *Ctx, std::string& S,
436964fd7e86c1a90d9ff78e4a0bd79f69499667a4e3David Chisnall                           QualType T, const FieldDecl *FD) {
4370a6b8b2c09610b8bc4330e948ece8b940c2386406Richard Smith  assert(FD->isBitField() && "not a bitfield - getObjCEncodingForTypeImpl");
43718b4bf90eb6d3d08cf3bfb86705f0fdb20b9c5875Fariborz Jahanian  S += 'b';
437264fd7e86c1a90d9ff78e4a0bd79f69499667a4e3David Chisnall  // The NeXT runtime encodes bit fields as b followed by the number of bits.
437364fd7e86c1a90d9ff78e4a0bd79f69499667a4e3David Chisnall  // The GNU runtime requires more information; bitfields are encoded as b,
437464fd7e86c1a90d9ff78e4a0bd79f69499667a4e3David Chisnall  // then the offset (in bits) of the first element, then the type of the
437564fd7e86c1a90d9ff78e4a0bd79f69499667a4e3David Chisnall  // bitfield, then the size in bits.  For example, in this structure:
437664fd7e86c1a90d9ff78e4a0bd79f69499667a4e3David Chisnall  //
437764fd7e86c1a90d9ff78e4a0bd79f69499667a4e3David Chisnall  // struct
437864fd7e86c1a90d9ff78e4a0bd79f69499667a4e3David Chisnall  // {
437964fd7e86c1a90d9ff78e4a0bd79f69499667a4e3David Chisnall  //    int integer;
438064fd7e86c1a90d9ff78e4a0bd79f69499667a4e3David Chisnall  //    int flags:2;
438164fd7e86c1a90d9ff78e4a0bd79f69499667a4e3David Chisnall  // };
438264fd7e86c1a90d9ff78e4a0bd79f69499667a4e3David Chisnall  // On a 32-bit system, the encoding for flags would be b2 for the NeXT
438364fd7e86c1a90d9ff78e4a0bd79f69499667a4e3David Chisnall  // runtime, but b32i2 for the GNU runtime.  The reason for this extra
438464fd7e86c1a90d9ff78e4a0bd79f69499667a4e3David Chisnall  // information is not especially sensible, but we're stuck with it for
438564fd7e86c1a90d9ff78e4a0bd79f69499667a4e3David Chisnall  // compatibility with GCC, although providing it breaks anything that
438664fd7e86c1a90d9ff78e4a0bd79f69499667a4e3David Chisnall  // actually uses runtime introspection and wants to work on both runtimes...
438764fd7e86c1a90d9ff78e4a0bd79f69499667a4e3David Chisnall  if (!Ctx->getLangOptions().NeXTRuntime) {
438864fd7e86c1a90d9ff78e4a0bd79f69499667a4e3David Chisnall    const RecordDecl *RD = FD->getParent();
438964fd7e86c1a90d9ff78e4a0bd79f69499667a4e3David Chisnall    const ASTRecordLayout &RL = Ctx->getASTRecordLayout(RD);
439082905749d5c8d8b4edec11de754a73349cb96603Eli Friedman    S += llvm::utostr(RL.getFieldOffset(FD->getFieldIndex()));
43915471bc85b69912e3b448de004498a80c0de32296Douglas Gregor    if (const EnumType *ET = T->getAs<EnumType>())
43925471bc85b69912e3b448de004498a80c0de32296Douglas Gregor      S += ObjCEncodingForEnumType(Ctx, ET);
4393c7ff82c2040f45eaad2eddea0e4461dddc972cd1David Chisnall    else
43944ba2a17694148e16eaa8d3917f657ffcd3667be4Jay Foad      S += ObjCEncodingForPrimitiveKind(Ctx, T);
439564fd7e86c1a90d9ff78e4a0bd79f69499667a4e3David Chisnall  }
4396a6b8b2c09610b8bc4330e948ece8b940c2386406Richard Smith  S += llvm::utostr(FD->getBitWidthValue(*Ctx));
43978b4bf90eb6d3d08cf3bfb86705f0fdb20b9c5875Fariborz Jahanian}
43988b4bf90eb6d3d08cf3bfb86705f0fdb20b9c5875Fariborz Jahanian
439901eb9b9683535d8a65c704ad2c545903409e2d36Daniel Dunbar// FIXME: Use SmallString for accumulating string.
440082a6cfbc421cc99c5b7313271f399f7ef95056ecDaniel Dunbarvoid ASTContext::getObjCEncodingForTypeImpl(QualType T, std::string& S,
440182a6cfbc421cc99c5b7313271f399f7ef95056ecDaniel Dunbar                                            bool ExpandPointedToStructures,
440282a6cfbc421cc99c5b7313271f399f7ef95056ecDaniel Dunbar                                            bool ExpandStructures,
4403153bfe5795e2c1a5a738e73d3784964e082237fcDaniel Dunbar                                            const FieldDecl *FD,
4404090b3f71702c5626d8520f9608d77c6f26dcfa15Fariborz Jahanian                                            bool OutermostType,
44052636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis                                            bool EncodingProperty,
4406dc8dab6fabf4bfd4f4b94bf572ac3342a5bbfcd7Bob Wilson                                            bool StructField,
4407dc8dab6fabf4bfd4f4b94bf572ac3342a5bbfcd7Bob Wilson                                            bool EncodeBlockParameters,
4408dc8dab6fabf4bfd4f4b94bf572ac3342a5bbfcd7Bob Wilson                                            bool EncodeClassNames) const {
440964fd7e86c1a90d9ff78e4a0bd79f69499667a4e3David Chisnall  if (T->getAs<BuiltinType>()) {
4410ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner    if (FD && FD->isBitField())
441164fd7e86c1a90d9ff78e4a0bd79f69499667a4e3David Chisnall      return EncodeBitField(this, S, T, FD);
441264fd7e86c1a90d9ff78e4a0bd79f69499667a4e3David Chisnall    S += ObjCEncodingForPrimitiveKind(this, T);
4413ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner    return;
4414ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner  }
44151eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
4416183700f494ec9b6701b6efe82bcb25f4c79ba561John McCall  if (const ComplexType *CT = T->getAs<ComplexType>()) {
4417c612f7bc9a6379cd7e7c2dd306d05938e890051bAnders Carlsson    S += 'j';
44181eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    getObjCEncodingForTypeImpl(CT->getElementType(), S, false, false, 0, false,
4419c612f7bc9a6379cd7e7c2dd306d05938e890051bAnders Carlsson                               false);
4420ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner    return;
4421ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner  }
442260bce3ef20a4c9684e3825cdd739fefb9810327dFariborz Jahanian
4423aa1d76163e4b0b1cc54e222be67379f8c02e8ffaFariborz Jahanian  // encoding for pointer or r3eference types.
4424aa1d76163e4b0b1cc54e222be67379f8c02e8ffaFariborz Jahanian  QualType PointeeTy;
44256217b80b7a1379b74cced1c076338262c3c980b3Ted Kremenek  if (const PointerType *PT = T->getAs<PointerType>()) {
44268d2c0a9814e56c2b22e22d1045181c735aef62fdFariborz Jahanian    if (PT->isObjCSelType()) {
44278d2c0a9814e56c2b22e22d1045181c735aef62fdFariborz Jahanian      S += ':';
44288d2c0a9814e56c2b22e22d1045181c735aef62fdFariborz Jahanian      return;
44298d2c0a9814e56c2b22e22d1045181c735aef62fdFariborz Jahanian    }
4430aa1d76163e4b0b1cc54e222be67379f8c02e8ffaFariborz Jahanian    PointeeTy = PT->getPointeeType();
4431aa1d76163e4b0b1cc54e222be67379f8c02e8ffaFariborz Jahanian  }
4432aa1d76163e4b0b1cc54e222be67379f8c02e8ffaFariborz Jahanian  else if (const ReferenceType *RT = T->getAs<ReferenceType>())
4433aa1d76163e4b0b1cc54e222be67379f8c02e8ffaFariborz Jahanian    PointeeTy = RT->getPointeeType();
4434aa1d76163e4b0b1cc54e222be67379f8c02e8ffaFariborz Jahanian  if (!PointeeTy.isNull()) {
4435a1c033e9514865f3a7b0d8b3b20e6de926cfec6cFariborz Jahanian    bool isReadOnly = false;
4436a1c033e9514865f3a7b0d8b3b20e6de926cfec6cFariborz Jahanian    // For historical/compatibility reasons, the read-only qualifier of the
4437a1c033e9514865f3a7b0d8b3b20e6de926cfec6cFariborz Jahanian    // pointee gets emitted _before_ the '^'.  The read-only qualifier of
4438a1c033e9514865f3a7b0d8b3b20e6de926cfec6cFariborz Jahanian    // the pointer itself gets ignored, _unless_ we are looking at a typedef!
44391eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    // Also, do not emit the 'r' for anything but the outermost type!
44408e1fab243ab8023b7ee3899745386b3b3a4258f8Mike Stump    if (isa<TypedefType>(T.getTypePtr())) {
4441a1c033e9514865f3a7b0d8b3b20e6de926cfec6cFariborz Jahanian      if (OutermostType && T.isConstQualified()) {
4442a1c033e9514865f3a7b0d8b3b20e6de926cfec6cFariborz Jahanian        isReadOnly = true;
4443a1c033e9514865f3a7b0d8b3b20e6de926cfec6cFariborz Jahanian        S += 'r';
4444a1c033e9514865f3a7b0d8b3b20e6de926cfec6cFariborz Jahanian      }
44459fdbab3cbc2fc04bcaf5768023d83707f3151144Mike Stump    } else if (OutermostType) {
4446a1c033e9514865f3a7b0d8b3b20e6de926cfec6cFariborz Jahanian      QualType P = PointeeTy;
44476217b80b7a1379b74cced1c076338262c3c980b3Ted Kremenek      while (P->getAs<PointerType>())
44486217b80b7a1379b74cced1c076338262c3c980b3Ted Kremenek        P = P->getAs<PointerType>()->getPointeeType();
4449a1c033e9514865f3a7b0d8b3b20e6de926cfec6cFariborz Jahanian      if (P.isConstQualified()) {
4450a1c033e9514865f3a7b0d8b3b20e6de926cfec6cFariborz Jahanian        isReadOnly = true;
4451a1c033e9514865f3a7b0d8b3b20e6de926cfec6cFariborz Jahanian        S += 'r';
4452a1c033e9514865f3a7b0d8b3b20e6de926cfec6cFariborz Jahanian      }
4453a1c033e9514865f3a7b0d8b3b20e6de926cfec6cFariborz Jahanian    }
4454a1c033e9514865f3a7b0d8b3b20e6de926cfec6cFariborz Jahanian    if (isReadOnly) {
4455a1c033e9514865f3a7b0d8b3b20e6de926cfec6cFariborz Jahanian      // Another legacy compatibility encoding. Some ObjC qualifier and type
4456a1c033e9514865f3a7b0d8b3b20e6de926cfec6cFariborz Jahanian      // combinations need to be rearranged.
4457a1c033e9514865f3a7b0d8b3b20e6de926cfec6cFariborz Jahanian      // Rewrite "in const" from "nr" to "rn"
44585f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner      if (StringRef(S).endswith("nr"))
44590237941e0beb0c929934b66ad29443b484d987feBenjamin Kramer        S.replace(S.end()-2, S.end(), "rn");
4460a1c033e9514865f3a7b0d8b3b20e6de926cfec6cFariborz Jahanian    }
44611eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
446285f9bceab1542aafff012d4d28e998f4ba16e362Anders Carlsson    if (PointeeTy->isCharType()) {
446385f9bceab1542aafff012d4d28e998f4ba16e362Anders Carlsson      // char pointer types should be encoded as '*' unless it is a
446485f9bceab1542aafff012d4d28e998f4ba16e362Anders Carlsson      // type that has been typedef'd to 'BOOL'.
4465e8c49533521c40643653f943d47229e62d277f88Anders Carlsson      if (!isTypeTypedefedAsBOOL(PointeeTy)) {
446685f9bceab1542aafff012d4d28e998f4ba16e362Anders Carlsson        S += '*';
446785f9bceab1542aafff012d4d28e998f4ba16e362Anders Carlsson        return;
446885f9bceab1542aafff012d4d28e998f4ba16e362Anders Carlsson      }
44696217b80b7a1379b74cced1c076338262c3c980b3Ted Kremenek    } else if (const RecordType *RTy = PointeeTy->getAs<RecordType>()) {
44709533a7fdb8397421f3be52e879442460a87389f6Steve Naroff      // GCC binary compat: Need to convert "struct objc_class *" to "#".
44719533a7fdb8397421f3be52e879442460a87389f6Steve Naroff      if (RTy->getDecl()->getIdentifier() == &Idents.get("objc_class")) {
44729533a7fdb8397421f3be52e879442460a87389f6Steve Naroff        S += '#';
44739533a7fdb8397421f3be52e879442460a87389f6Steve Naroff        return;
44749533a7fdb8397421f3be52e879442460a87389f6Steve Naroff      }
44759533a7fdb8397421f3be52e879442460a87389f6Steve Naroff      // GCC binary compat: Need to convert "struct objc_object *" to "@".
44769533a7fdb8397421f3be52e879442460a87389f6Steve Naroff      if (RTy->getDecl()->getIdentifier() == &Idents.get("objc_object")) {
44779533a7fdb8397421f3be52e879442460a87389f6Steve Naroff        S += '@';
44789533a7fdb8397421f3be52e879442460a87389f6Steve Naroff        return;
44799533a7fdb8397421f3be52e879442460a87389f6Steve Naroff      }
44809533a7fdb8397421f3be52e879442460a87389f6Steve Naroff      // fall through...
448185f9bceab1542aafff012d4d28e998f4ba16e362Anders Carlsson    }
448285f9bceab1542aafff012d4d28e998f4ba16e362Anders Carlsson    S += '^';
4483a1c033e9514865f3a7b0d8b3b20e6de926cfec6cFariborz Jahanian    getLegacyIntegralTypeEncoding(PointeeTy);
4484a1c033e9514865f3a7b0d8b3b20e6de926cfec6cFariborz Jahanian
44851eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    getObjCEncodingForTypeImpl(PointeeTy, S, false, ExpandPointedToStructures,
448643822eaeddeaa419b90f23c68af6b23c46788a58Fariborz Jahanian                               NULL);
4487ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner    return;
4488ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner  }
4489aa1d76163e4b0b1cc54e222be67379f8c02e8ffaFariborz Jahanian
4490ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner  if (const ArrayType *AT =
4491ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner      // Ignore type qualifiers etc.
4492ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner        dyn_cast<ArrayType>(T->getCanonicalTypeInternal())) {
44932636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis    if (isa<IncompleteArrayType>(AT) && !StructField) {
4494559a83330416affb0e341a2c53800cbf924a5178Anders Carlsson      // Incomplete arrays are encoded as a pointer to the array element.
4495559a83330416affb0e341a2c53800cbf924a5178Anders Carlsson      S += '^';
4496559a83330416affb0e341a2c53800cbf924a5178Anders Carlsson
44971eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      getObjCEncodingForTypeImpl(AT->getElementType(), S,
4498559a83330416affb0e341a2c53800cbf924a5178Anders Carlsson                                 false, ExpandStructures, FD);
4499559a83330416affb0e341a2c53800cbf924a5178Anders Carlsson    } else {
4500559a83330416affb0e341a2c53800cbf924a5178Anders Carlsson      S += '[';
45011eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
45022636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis      if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(AT)) {
45032636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis        if (getTypeSize(CAT->getElementType()) == 0)
45042636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis          S += '0';
45052636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis        else
45062636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis          S += llvm::utostr(CAT->getSize().getZExtValue());
45072636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis      } else {
4508559a83330416affb0e341a2c53800cbf924a5178Anders Carlsson        //Variable length arrays are encoded as a regular array with 0 elements.
45092636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis        assert((isa<VariableArrayType>(AT) || isa<IncompleteArrayType>(AT)) &&
45102636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis               "Unknown array type!");
4511559a83330416affb0e341a2c53800cbf924a5178Anders Carlsson        S += '0';
4512559a83330416affb0e341a2c53800cbf924a5178Anders Carlsson      }
45131eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
45141eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      getObjCEncodingForTypeImpl(AT->getElementType(), S,
4515559a83330416affb0e341a2c53800cbf924a5178Anders Carlsson                                 false, ExpandStructures, FD);
4516559a83330416affb0e341a2c53800cbf924a5178Anders Carlsson      S += ']';
4517559a83330416affb0e341a2c53800cbf924a5178Anders Carlsson    }
4518ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner    return;
4519ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner  }
45201eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
4521183700f494ec9b6701b6efe82bcb25f4c79ba561John McCall  if (T->getAs<FunctionType>()) {
4522c0a87b7db06643178ad2cbce0767548c139ea387Anders Carlsson    S += '?';
4523ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner    return;
4524ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner  }
45251eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
45266217b80b7a1379b74cced1c076338262c3c980b3Ted Kremenek  if (const RecordType *RTy = T->getAs<RecordType>()) {
452782a6cfbc421cc99c5b7313271f399f7ef95056ecDaniel Dunbar    RecordDecl *RDecl = RTy->getDecl();
4528d96b35bc6becf8db00d140c11e3d0e53f27567a1Daniel Dunbar    S += RDecl->isUnion() ? '(' : '{';
4529502a4a1ce4c34cf78c8182d9798da0a51d9b7302Daniel Dunbar    // Anonymous structures print as '?'
4530502a4a1ce4c34cf78c8182d9798da0a51d9b7302Daniel Dunbar    if (const IdentifierInfo *II = RDecl->getIdentifier()) {
4531502a4a1ce4c34cf78c8182d9798da0a51d9b7302Daniel Dunbar      S += II->getName();
45326fb94391dc7cb11fd4bbdb969bbab11b6b48c223Fariborz Jahanian      if (ClassTemplateSpecializationDecl *Spec
45336fb94391dc7cb11fd4bbdb969bbab11b6b48c223Fariborz Jahanian          = dyn_cast<ClassTemplateSpecializationDecl>(RDecl)) {
45346fb94391dc7cb11fd4bbdb969bbab11b6b48c223Fariborz Jahanian        const TemplateArgumentList &TemplateArgs = Spec->getTemplateArgs();
45356fb94391dc7cb11fd4bbdb969bbab11b6b48c223Fariborz Jahanian        std::string TemplateArgsStr
45366fb94391dc7cb11fd4bbdb969bbab11b6b48c223Fariborz Jahanian          = TemplateSpecializationType::PrintTemplateArgumentList(
4537910f8008fea79120489a53593fe971b0b8a4a740Douglas Gregor                                            TemplateArgs.data(),
4538910f8008fea79120489a53593fe971b0b8a4a740Douglas Gregor                                            TemplateArgs.size(),
453930c42404202d2e2512e51efc6066bd614cfdb5a4Douglas Gregor                                            (*this).getPrintingPolicy());
45406fb94391dc7cb11fd4bbdb969bbab11b6b48c223Fariborz Jahanian
45416fb94391dc7cb11fd4bbdb969bbab11b6b48c223Fariborz Jahanian        S += TemplateArgsStr;
45426fb94391dc7cb11fd4bbdb969bbab11b6b48c223Fariborz Jahanian      }
4543502a4a1ce4c34cf78c8182d9798da0a51d9b7302Daniel Dunbar    } else {
4544502a4a1ce4c34cf78c8182d9798da0a51d9b7302Daniel Dunbar      S += '?';
4545502a4a1ce4c34cf78c8182d9798da0a51d9b7302Daniel Dunbar    }
45460d504c1da852e58ff802545c823ecff3b6c654b8Daniel Dunbar    if (ExpandStructures) {
45477d6b46d9a9d75dea8ef9f6973dd50633c1f37963Fariborz Jahanian      S += '=';
45482636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis      if (!RDecl->isUnion()) {
45492636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis        getObjCEncodingForStructureImpl(RDecl, S, FD);
45502636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis      } else {
45512636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis        for (RecordDecl::field_iterator Field = RDecl->field_begin(),
45522636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis                                     FieldEnd = RDecl->field_end();
45532636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis             Field != FieldEnd; ++Field) {
45542636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis          if (FD) {
45552636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis            S += '"';
45562636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis            S += Field->getNameAsString();
45572636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis            S += '"';
45582636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis          }
45591eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
45602636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis          // Special case bit-fields.
45612636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis          if (Field->isBitField()) {
45622636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis            getObjCEncodingForTypeImpl(Field->getType(), S, false, true,
45632636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis                                       (*Field));
45642636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis          } else {
45652636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis            QualType qt = Field->getType();
45662636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis            getLegacyIntegralTypeEncoding(qt);
45672636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis            getObjCEncodingForTypeImpl(qt, S, false, true,
45682636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis                                       FD, /*OutermostType*/false,
45692636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis                                       /*EncodingProperty*/false,
45702636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis                                       /*StructField*/true);
45712636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis          }
4572d96b35bc6becf8db00d140c11e3d0e53f27567a1Daniel Dunbar        }
45737d6b46d9a9d75dea8ef9f6973dd50633c1f37963Fariborz Jahanian      }
45746de88a873a4cbe06d72602eef57d68006730a80bFariborz Jahanian    }
4575d96b35bc6becf8db00d140c11e3d0e53f27567a1Daniel Dunbar    S += RDecl->isUnion() ? ')' : '}';
4576ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner    return;
4577ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner  }
4578e6012c7ecb9d848f4091c8c48e7d9946cc36b23fFariborz Jahanian
45795471bc85b69912e3b448de004498a80c0de32296Douglas Gregor  if (const EnumType *ET = T->getAs<EnumType>()) {
45808b4bf90eb6d3d08cf3bfb86705f0fdb20b9c5875Fariborz Jahanian    if (FD && FD->isBitField())
458164fd7e86c1a90d9ff78e4a0bd79f69499667a4e3David Chisnall      EncodeBitField(this, S, T, FD);
45828b4bf90eb6d3d08cf3bfb86705f0fdb20b9c5875Fariborz Jahanian    else
45835471bc85b69912e3b448de004498a80c0de32296Douglas Gregor      S += ObjCEncodingForEnumType(this, ET);
4584ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner    return;
4585ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner  }
45861eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
4587dc8dab6fabf4bfd4f4b94bf572ac3342a5bbfcd7Bob Wilson  if (const BlockPointerType *BT = T->getAs<BlockPointerType>()) {
458821a98b188857d690aa4510c52ac4317ffa0908a8Steve Naroff    S += "@?"; // Unlike a pointer-to-function, which is "^?".
4589dc8dab6fabf4bfd4f4b94bf572ac3342a5bbfcd7Bob Wilson    if (EncodeBlockParameters) {
4590dc8dab6fabf4bfd4f4b94bf572ac3342a5bbfcd7Bob Wilson      const FunctionType *FT = BT->getPointeeType()->getAs<FunctionType>();
4591dc8dab6fabf4bfd4f4b94bf572ac3342a5bbfcd7Bob Wilson
4592dc8dab6fabf4bfd4f4b94bf572ac3342a5bbfcd7Bob Wilson      S += '<';
4593dc8dab6fabf4bfd4f4b94bf572ac3342a5bbfcd7Bob Wilson      // Block return type
4594dc8dab6fabf4bfd4f4b94bf572ac3342a5bbfcd7Bob Wilson      getObjCEncodingForTypeImpl(FT->getResultType(), S,
4595dc8dab6fabf4bfd4f4b94bf572ac3342a5bbfcd7Bob Wilson                                 ExpandPointedToStructures, ExpandStructures,
4596dc8dab6fabf4bfd4f4b94bf572ac3342a5bbfcd7Bob Wilson                                 FD,
4597dc8dab6fabf4bfd4f4b94bf572ac3342a5bbfcd7Bob Wilson                                 false /* OutermostType */,
4598dc8dab6fabf4bfd4f4b94bf572ac3342a5bbfcd7Bob Wilson                                 EncodingProperty,
4599dc8dab6fabf4bfd4f4b94bf572ac3342a5bbfcd7Bob Wilson                                 false /* StructField */,
4600dc8dab6fabf4bfd4f4b94bf572ac3342a5bbfcd7Bob Wilson                                 EncodeBlockParameters,
4601dc8dab6fabf4bfd4f4b94bf572ac3342a5bbfcd7Bob Wilson                                 EncodeClassNames);
4602dc8dab6fabf4bfd4f4b94bf572ac3342a5bbfcd7Bob Wilson      // Block self
4603dc8dab6fabf4bfd4f4b94bf572ac3342a5bbfcd7Bob Wilson      S += "@?";
4604dc8dab6fabf4bfd4f4b94bf572ac3342a5bbfcd7Bob Wilson      // Block parameters
4605dc8dab6fabf4bfd4f4b94bf572ac3342a5bbfcd7Bob Wilson      if (const FunctionProtoType *FPT = dyn_cast<FunctionProtoType>(FT)) {
4606dc8dab6fabf4bfd4f4b94bf572ac3342a5bbfcd7Bob Wilson        for (FunctionProtoType::arg_type_iterator I = FPT->arg_type_begin(),
4607dc8dab6fabf4bfd4f4b94bf572ac3342a5bbfcd7Bob Wilson               E = FPT->arg_type_end(); I && (I != E); ++I) {
4608dc8dab6fabf4bfd4f4b94bf572ac3342a5bbfcd7Bob Wilson          getObjCEncodingForTypeImpl(*I, S,
4609dc8dab6fabf4bfd4f4b94bf572ac3342a5bbfcd7Bob Wilson                                     ExpandPointedToStructures,
4610dc8dab6fabf4bfd4f4b94bf572ac3342a5bbfcd7Bob Wilson                                     ExpandStructures,
4611dc8dab6fabf4bfd4f4b94bf572ac3342a5bbfcd7Bob Wilson                                     FD,
4612dc8dab6fabf4bfd4f4b94bf572ac3342a5bbfcd7Bob Wilson                                     false /* OutermostType */,
4613dc8dab6fabf4bfd4f4b94bf572ac3342a5bbfcd7Bob Wilson                                     EncodingProperty,
4614dc8dab6fabf4bfd4f4b94bf572ac3342a5bbfcd7Bob Wilson                                     false /* StructField */,
4615dc8dab6fabf4bfd4f4b94bf572ac3342a5bbfcd7Bob Wilson                                     EncodeBlockParameters,
4616dc8dab6fabf4bfd4f4b94bf572ac3342a5bbfcd7Bob Wilson                                     EncodeClassNames);
4617dc8dab6fabf4bfd4f4b94bf572ac3342a5bbfcd7Bob Wilson        }
4618dc8dab6fabf4bfd4f4b94bf572ac3342a5bbfcd7Bob Wilson      }
4619dc8dab6fabf4bfd4f4b94bf572ac3342a5bbfcd7Bob Wilson      S += '>';
4620dc8dab6fabf4bfd4f4b94bf572ac3342a5bbfcd7Bob Wilson    }
4621ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner    return;
4622ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner  }
46231eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
4624c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall  // Ignore protocol qualifiers when mangling at this level.
4625c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall  if (const ObjCObjectType *OT = T->getAs<ObjCObjectType>())
4626c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall    T = OT->getBaseType();
4627c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall
46280953e767ff7817f97b3ab20896b229891eeff45bJohn McCall  if (const ObjCInterfaceType *OIT = T->getAs<ObjCInterfaceType>()) {
462943822eaeddeaa419b90f23c68af6b23c46788a58Fariborz Jahanian    // @encode(class_name)
46300953e767ff7817f97b3ab20896b229891eeff45bJohn McCall    ObjCInterfaceDecl *OI = OIT->getDecl();
463143822eaeddeaa419b90f23c68af6b23c46788a58Fariborz Jahanian    S += '{';
463243822eaeddeaa419b90f23c68af6b23c46788a58Fariborz Jahanian    const IdentifierInfo *II = OI->getIdentifier();
463343822eaeddeaa419b90f23c68af6b23c46788a58Fariborz Jahanian    S += II->getName();
463443822eaeddeaa419b90f23c68af6b23c46788a58Fariborz Jahanian    S += '=';
4635db8264e4c5ffd7af6fbad4ca4306bd382bb02691Jordy Rose    SmallVector<const ObjCIvarDecl*, 32> Ivars;
46362c18bb7c9fca66c30b6eabbdcbc6399d24a54fa9Fariborz Jahanian    DeepCollectObjCIvars(OI, true, Ivars);
46372c18bb7c9fca66c30b6eabbdcbc6399d24a54fa9Fariborz Jahanian    for (unsigned i = 0, e = Ivars.size(); i != e; ++i) {
4638db8264e4c5ffd7af6fbad4ca4306bd382bb02691Jordy Rose      const FieldDecl *Field = cast<FieldDecl>(Ivars[i]);
46392c18bb7c9fca66c30b6eabbdcbc6399d24a54fa9Fariborz Jahanian      if (Field->isBitField())
46402c18bb7c9fca66c30b6eabbdcbc6399d24a54fa9Fariborz Jahanian        getObjCEncodingForTypeImpl(Field->getType(), S, false, true, Field);
464143822eaeddeaa419b90f23c68af6b23c46788a58Fariborz Jahanian      else
46422c18bb7c9fca66c30b6eabbdcbc6399d24a54fa9Fariborz Jahanian        getObjCEncodingForTypeImpl(Field->getType(), S, false, true, FD);
464343822eaeddeaa419b90f23c68af6b23c46788a58Fariborz Jahanian    }
464443822eaeddeaa419b90f23c68af6b23c46788a58Fariborz Jahanian    S += '}';
4645ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner    return;
464643822eaeddeaa419b90f23c68af6b23c46788a58Fariborz Jahanian  }
46471eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
4648183700f494ec9b6701b6efe82bcb25f4c79ba561John McCall  if (const ObjCObjectPointerType *OPT = T->getAs<ObjCObjectPointerType>()) {
464914108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff    if (OPT->isObjCIdType()) {
465014108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff      S += '@';
465114108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff      return;
4652ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner    }
46531eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
465427d20a24a1b816cecbd142727d2c81af5a6a111bSteve Naroff    if (OPT->isObjCClassType() || OPT->isObjCQualifiedClassType()) {
465527d20a24a1b816cecbd142727d2c81af5a6a111bSteve Naroff      // FIXME: Consider if we need to output qualifiers for 'Class<p>'.
465627d20a24a1b816cecbd142727d2c81af5a6a111bSteve Naroff      // Since this is a binary compatibility issue, need to consult with runtime
465727d20a24a1b816cecbd142727d2c81af5a6a111bSteve Naroff      // folks. Fortunately, this is a *very* obsure construct.
465814108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff      S += '#';
465914108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff      return;
4660ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner    }
46611eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
4662ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner    if (OPT->isObjCQualifiedIdType()) {
46631eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      getObjCEncodingForTypeImpl(getObjCIdType(), S,
466414108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff                                 ExpandPointedToStructures,
466514108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff                                 ExpandStructures, FD);
4666dc8dab6fabf4bfd4f4b94bf572ac3342a5bbfcd7Bob Wilson      if (FD || EncodingProperty || EncodeClassNames) {
466714108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff        // Note that we do extended encoding of protocol qualifer list
466814108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff        // Only when doing ivar or property encoding.
466914108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff        S += '"';
467067ef8eaea8a0a2073147a8d863f0e3f30d525802Steve Naroff        for (ObjCObjectPointerType::qual_iterator I = OPT->qual_begin(),
467167ef8eaea8a0a2073147a8d863f0e3f30d525802Steve Naroff             E = OPT->qual_end(); I != E; ++I) {
467214108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff          S += '<';
467314108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff          S += (*I)->getNameAsString();
467414108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff          S += '>';
467514108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff        }
467614108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff        S += '"';
467714108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff      }
467814108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff      return;
4679ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner    }
46801eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
4681ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner    QualType PointeeTy = OPT->getPointeeType();
4682ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner    if (!EncodingProperty &&
4683ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner        isa<TypedefType>(PointeeTy.getTypePtr())) {
4684ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner      // Another historical/compatibility reason.
46851eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      // We encode the underlying type which comes out as
4686ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner      // {...};
4687ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner      S += '^';
46881eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      getObjCEncodingForTypeImpl(PointeeTy, S,
46891eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump                                 false, ExpandPointedToStructures,
4690ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner                                 NULL);
469114108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff      return;
469214108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff    }
4693ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner
4694ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner    S += '@';
4695dc8dab6fabf4bfd4f4b94bf572ac3342a5bbfcd7Bob Wilson    if (OPT->getInterfaceDecl() &&
4696dc8dab6fabf4bfd4f4b94bf572ac3342a5bbfcd7Bob Wilson        (FD || EncodingProperty || EncodeClassNames)) {
4697ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner      S += '"';
469827d20a24a1b816cecbd142727d2c81af5a6a111bSteve Naroff      S += OPT->getInterfaceDecl()->getIdentifier()->getName();
469967ef8eaea8a0a2073147a8d863f0e3f30d525802Steve Naroff      for (ObjCObjectPointerType::qual_iterator I = OPT->qual_begin(),
470067ef8eaea8a0a2073147a8d863f0e3f30d525802Steve Naroff           E = OPT->qual_end(); I != E; ++I) {
4701ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner        S += '<';
4702ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner        S += (*I)->getNameAsString();
4703ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner        S += '>';
47041eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      }
4705ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner      S += '"';
4706ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner    }
4707ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner    return;
4708ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner  }
47091eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
4710532ec7baf2d0791abc9551ef856a537711c5774aJohn McCall  // gcc just blithely ignores member pointers.
4711532ec7baf2d0791abc9551ef856a537711c5774aJohn McCall  // TODO: maybe there should be a mangling for these
4712532ec7baf2d0791abc9551ef856a537711c5774aJohn McCall  if (T->getAs<MemberPointerType>())
4713532ec7baf2d0791abc9551ef856a537711c5774aJohn McCall    return;
4714e6012c7ecb9d848f4091c8c48e7d9946cc36b23fFariborz Jahanian
4715e6012c7ecb9d848f4091c8c48e7d9946cc36b23fFariborz Jahanian  if (T->isVectorType()) {
4716e6012c7ecb9d848f4091c8c48e7d9946cc36b23fFariborz Jahanian    // This matches gcc's encoding, even though technically it is
4717e6012c7ecb9d848f4091c8c48e7d9946cc36b23fFariborz Jahanian    // insufficient.
4718e6012c7ecb9d848f4091c8c48e7d9946cc36b23fFariborz Jahanian    // FIXME. We should do a better job than gcc.
4719e6012c7ecb9d848f4091c8c48e7d9946cc36b23fFariborz Jahanian    return;
4720e6012c7ecb9d848f4091c8c48e7d9946cc36b23fFariborz Jahanian  }
4721e6012c7ecb9d848f4091c8c48e7d9946cc36b23fFariborz Jahanian
4722b219cfc4d75f0a03630b7c4509ef791b7e97b2c8David Blaikie  llvm_unreachable("@encode for type not implemented!");
472385f9bceab1542aafff012d4d28e998f4ba16e362Anders Carlsson}
472485f9bceab1542aafff012d4d28e998f4ba16e362Anders Carlsson
47252636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidisvoid ASTContext::getObjCEncodingForStructureImpl(RecordDecl *RDecl,
47262636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis                                                 std::string &S,
47272636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis                                                 const FieldDecl *FD,
47282636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis                                                 bool includeVBases) const {
47292636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis  assert(RDecl && "Expected non-null RecordDecl");
47302636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis  assert(!RDecl->isUnion() && "Should not be called for unions");
47312636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis  if (!RDecl->getDefinition())
47322636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis    return;
47332636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis
47342636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis  CXXRecordDecl *CXXRec = dyn_cast<CXXRecordDecl>(RDecl);
47352636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis  std::multimap<uint64_t, NamedDecl *> FieldOrBaseOffsets;
47362636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis  const ASTRecordLayout &layout = getASTRecordLayout(RDecl);
47372636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis
47382636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis  if (CXXRec) {
47392636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis    for (CXXRecordDecl::base_class_iterator
47402636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis           BI = CXXRec->bases_begin(),
47412636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis           BE = CXXRec->bases_end(); BI != BE; ++BI) {
47422636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis      if (!BI->isVirtual()) {
47432636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis        CXXRecordDecl *base = BI->getType()->getAsCXXRecordDecl();
4744829f20097e030229fd4c234b84948f53312eaf55Argyrios Kyrtzidis        if (base->isEmpty())
4745829f20097e030229fd4c234b84948f53312eaf55Argyrios Kyrtzidis          continue;
47462636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis        uint64_t offs = layout.getBaseClassOffsetInBits(base);
47472636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis        FieldOrBaseOffsets.insert(FieldOrBaseOffsets.upper_bound(offs),
47482636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis                                  std::make_pair(offs, base));
47492636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis      }
47502636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis    }
47512636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis  }
47522636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis
47532636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis  unsigned i = 0;
47542636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis  for (RecordDecl::field_iterator Field = RDecl->field_begin(),
47552636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis                               FieldEnd = RDecl->field_end();
47562636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis       Field != FieldEnd; ++Field, ++i) {
47572636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis    uint64_t offs = layout.getFieldOffset(i);
47582636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis    FieldOrBaseOffsets.insert(FieldOrBaseOffsets.upper_bound(offs),
47592636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis                              std::make_pair(offs, *Field));
47602636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis  }
47612636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis
47622636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis  if (CXXRec && includeVBases) {
47632636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis    for (CXXRecordDecl::base_class_iterator
47642636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis           BI = CXXRec->vbases_begin(),
47652636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis           BE = CXXRec->vbases_end(); BI != BE; ++BI) {
47662636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis      CXXRecordDecl *base = BI->getType()->getAsCXXRecordDecl();
4767829f20097e030229fd4c234b84948f53312eaf55Argyrios Kyrtzidis      if (base->isEmpty())
4768829f20097e030229fd4c234b84948f53312eaf55Argyrios Kyrtzidis        continue;
47692636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis      uint64_t offs = layout.getVBaseClassOffsetInBits(base);
477019aa8609a5d04eed9b81aa1c4608e8a4f3df6ccdArgyrios Kyrtzidis      if (FieldOrBaseOffsets.find(offs) == FieldOrBaseOffsets.end())
477119aa8609a5d04eed9b81aa1c4608e8a4f3df6ccdArgyrios Kyrtzidis        FieldOrBaseOffsets.insert(FieldOrBaseOffsets.end(),
477219aa8609a5d04eed9b81aa1c4608e8a4f3df6ccdArgyrios Kyrtzidis                                  std::make_pair(offs, base));
47732636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis    }
47742636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis  }
47752636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis
47762636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis  CharUnits size;
47772636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis  if (CXXRec) {
47782636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis    size = includeVBases ? layout.getSize() : layout.getNonVirtualSize();
47792636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis  } else {
47802636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis    size = layout.getSize();
47812636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis  }
47822636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis
47832636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis  uint64_t CurOffs = 0;
47842636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis  std::multimap<uint64_t, NamedDecl *>::iterator
47852636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis    CurLayObj = FieldOrBaseOffsets.begin();
47862636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis
4787cb8061e321bc6f46f154529885028256c7ae1fb2Argyrios Kyrtzidis  if ((CurLayObj != FieldOrBaseOffsets.end() && CurLayObj->first != 0) ||
4788cb8061e321bc6f46f154529885028256c7ae1fb2Argyrios Kyrtzidis      (CurLayObj == FieldOrBaseOffsets.end() &&
4789cb8061e321bc6f46f154529885028256c7ae1fb2Argyrios Kyrtzidis         CXXRec && CXXRec->isDynamicClass())) {
47902636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis    assert(CXXRec && CXXRec->isDynamicClass() &&
47912636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis           "Offset 0 was empty but no VTable ?");
47922636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis    if (FD) {
47932636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis      S += "\"_vptr$";
47942636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis      std::string recname = CXXRec->getNameAsString();
47952636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis      if (recname.empty()) recname = "?";
47962636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis      S += recname;
47972636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis      S += '"';
47982636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis    }
47992636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis    S += "^^?";
48002636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis    CurOffs += getTypeSize(VoidPtrTy);
48012636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis  }
48022636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis
48032636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis  if (!RDecl->hasFlexibleArrayMember()) {
48042636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis    // Mark the end of the structure.
48052636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis    uint64_t offs = toBits(size);
48062636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis    FieldOrBaseOffsets.insert(FieldOrBaseOffsets.upper_bound(offs),
48072636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis                              std::make_pair(offs, (NamedDecl*)0));
48082636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis  }
48092636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis
48102636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis  for (; CurLayObj != FieldOrBaseOffsets.end(); ++CurLayObj) {
48112636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis    assert(CurOffs <= CurLayObj->first);
48122636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis
48132636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis    if (CurOffs < CurLayObj->first) {
48142636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis      uint64_t padding = CurLayObj->first - CurOffs;
48152636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis      // FIXME: There doesn't seem to be a way to indicate in the encoding that
48162636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis      // packing/alignment of members is different that normal, in which case
48172636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis      // the encoding will be out-of-sync with the real layout.
48182636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis      // If the runtime switches to just consider the size of types without
48192636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis      // taking into account alignment, we could make padding explicit in the
48202636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis      // encoding (e.g. using arrays of chars). The encoding strings would be
48212636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis      // longer then though.
48222636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis      CurOffs += padding;
48232636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis    }
48242636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis
48252636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis    NamedDecl *dcl = CurLayObj->second;
48262636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis    if (dcl == 0)
48272636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis      break; // reached end of structure.
48282636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis
48292636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis    if (CXXRecordDecl *base = dyn_cast<CXXRecordDecl>(dcl)) {
48302636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis      // We expand the bases without their virtual bases since those are going
48312636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis      // in the initial structure. Note that this differs from gcc which
48322636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis      // expands virtual bases each time one is encountered in the hierarchy,
48332636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis      // making the encoding type bigger than it really is.
48342636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis      getObjCEncodingForStructureImpl(base, S, FD, /*includeVBases*/false);
4835829f20097e030229fd4c234b84948f53312eaf55Argyrios Kyrtzidis      assert(!base->isEmpty());
4836829f20097e030229fd4c234b84948f53312eaf55Argyrios Kyrtzidis      CurOffs += toBits(getASTRecordLayout(base).getNonVirtualSize());
48372636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis    } else {
48382636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis      FieldDecl *field = cast<FieldDecl>(dcl);
48392636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis      if (FD) {
48402636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis        S += '"';
48412636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis        S += field->getNameAsString();
48422636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis        S += '"';
48432636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis      }
48442636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis
48452636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis      if (field->isBitField()) {
48462636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis        EncodeBitField(this, S, field->getType(), field);
4847a6b8b2c09610b8bc4330e948ece8b940c2386406Richard Smith        CurOffs += field->getBitWidthValue(*this);
48482636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis      } else {
48492636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis        QualType qt = field->getType();
48502636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis        getLegacyIntegralTypeEncoding(qt);
48512636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis        getObjCEncodingForTypeImpl(qt, S, false, true, FD,
48522636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis                                   /*OutermostType*/false,
48532636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis                                   /*EncodingProperty*/false,
48542636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis                                   /*StructField*/true);
48552636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis        CurOffs += getTypeSize(field->getType());
48562636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis      }
48572636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis    }
48582636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis  }
48592636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis}
48602636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis
48611eb4433ac451dc16f4133a88af2d002ac26c58efMike Stumpvoid ASTContext::getObjCEncodingForTypeQualifier(Decl::ObjCDeclQualifier QT,
4862ecb01e666665efabd2aa76a76f6080e2a78965faFariborz Jahanian                                                 std::string& S) const {
4863ecb01e666665efabd2aa76a76f6080e2a78965faFariborz Jahanian  if (QT & Decl::OBJC_TQ_In)
4864ecb01e666665efabd2aa76a76f6080e2a78965faFariborz Jahanian    S += 'n';
4865ecb01e666665efabd2aa76a76f6080e2a78965faFariborz Jahanian  if (QT & Decl::OBJC_TQ_Inout)
4866ecb01e666665efabd2aa76a76f6080e2a78965faFariborz Jahanian    S += 'N';
4867ecb01e666665efabd2aa76a76f6080e2a78965faFariborz Jahanian  if (QT & Decl::OBJC_TQ_Out)
4868ecb01e666665efabd2aa76a76f6080e2a78965faFariborz Jahanian    S += 'o';
4869ecb01e666665efabd2aa76a76f6080e2a78965faFariborz Jahanian  if (QT & Decl::OBJC_TQ_Bycopy)
4870ecb01e666665efabd2aa76a76f6080e2a78965faFariborz Jahanian    S += 'O';
4871ecb01e666665efabd2aa76a76f6080e2a78965faFariborz Jahanian  if (QT & Decl::OBJC_TQ_Byref)
4872ecb01e666665efabd2aa76a76f6080e2a78965faFariborz Jahanian    S += 'R';
4873ecb01e666665efabd2aa76a76f6080e2a78965faFariborz Jahanian  if (QT & Decl::OBJC_TQ_Oneway)
4874ecb01e666665efabd2aa76a76f6080e2a78965faFariborz Jahanian    S += 'V';
4875ecb01e666665efabd2aa76a76f6080e2a78965faFariborz Jahanian}
4876ecb01e666665efabd2aa76a76f6080e2a78965faFariborz Jahanian
4877ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattnervoid ASTContext::setBuiltinVaListType(QualType T) {
4878b2cf3573d7351094f6247fcca94703ce88eb9ee0Anders Carlsson  assert(BuiltinVaListType.isNull() && "__builtin_va_list type already set!");
48791eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
4880b2cf3573d7351094f6247fcca94703ce88eb9ee0Anders Carlsson  BuiltinVaListType = T;
4881b2cf3573d7351094f6247fcca94703ce88eb9ee0Anders Carlsson}
4882b2cf3573d7351094f6247fcca94703ce88eb9ee0Anders Carlsson
48834dfd02a17c6d604c72e6936527c5e1c56d3ecb7aDouglas GregorTypedefDecl *ASTContext::getObjCIdDecl() const {
48844dfd02a17c6d604c72e6936527c5e1c56d3ecb7aDouglas Gregor  if (!ObjCIdDecl) {
48854dfd02a17c6d604c72e6936527c5e1c56d3ecb7aDouglas Gregor    QualType T = getObjCObjectType(ObjCBuiltinIdTy, 0, 0);
48864dfd02a17c6d604c72e6936527c5e1c56d3ecb7aDouglas Gregor    T = getObjCObjectPointerType(T);
48874dfd02a17c6d604c72e6936527c5e1c56d3ecb7aDouglas Gregor    TypeSourceInfo *IdInfo = getTrivialTypeSourceInfo(T);
48884dfd02a17c6d604c72e6936527c5e1c56d3ecb7aDouglas Gregor    ObjCIdDecl = TypedefDecl::Create(const_cast<ASTContext &>(*this),
48894dfd02a17c6d604c72e6936527c5e1c56d3ecb7aDouglas Gregor                                     getTranslationUnitDecl(),
48904dfd02a17c6d604c72e6936527c5e1c56d3ecb7aDouglas Gregor                                     SourceLocation(), SourceLocation(),
48914dfd02a17c6d604c72e6936527c5e1c56d3ecb7aDouglas Gregor                                     &Idents.get("id"), IdInfo);
48924dfd02a17c6d604c72e6936527c5e1c56d3ecb7aDouglas Gregor  }
48934dfd02a17c6d604c72e6936527c5e1c56d3ecb7aDouglas Gregor
48944dfd02a17c6d604c72e6936527c5e1c56d3ecb7aDouglas Gregor  return ObjCIdDecl;
48957e219e47de26346885d667131977bd9ca2d7662aSteve Naroff}
48967e219e47de26346885d667131977bd9ca2d7662aSteve Naroff
48977a27ea52b7bd635c89bec5a9c521a3bf7d204238Douglas GregorTypedefDecl *ASTContext::getObjCSelDecl() const {
48987a27ea52b7bd635c89bec5a9c521a3bf7d204238Douglas Gregor  if (!ObjCSelDecl) {
48997a27ea52b7bd635c89bec5a9c521a3bf7d204238Douglas Gregor    QualType SelT = getPointerType(ObjCBuiltinSelTy);
49007a27ea52b7bd635c89bec5a9c521a3bf7d204238Douglas Gregor    TypeSourceInfo *SelInfo = getTrivialTypeSourceInfo(SelT);
49017a27ea52b7bd635c89bec5a9c521a3bf7d204238Douglas Gregor    ObjCSelDecl = TypedefDecl::Create(const_cast<ASTContext &>(*this),
49027a27ea52b7bd635c89bec5a9c521a3bf7d204238Douglas Gregor                                      getTranslationUnitDecl(),
49037a27ea52b7bd635c89bec5a9c521a3bf7d204238Douglas Gregor                                      SourceLocation(), SourceLocation(),
49047a27ea52b7bd635c89bec5a9c521a3bf7d204238Douglas Gregor                                      &Idents.get("SEL"), SelInfo);
49057a27ea52b7bd635c89bec5a9c521a3bf7d204238Douglas Gregor  }
49067a27ea52b7bd635c89bec5a9c521a3bf7d204238Douglas Gregor  return ObjCSelDecl;
4907b62f6813406a03bf8a371c4e46c9fad51d102121Fariborz Jahanian}
4908b62f6813406a03bf8a371c4e46c9fad51d102121Fariborz Jahanian
490979d6726921897811232554ed94c5d77b5b7b3fc0Douglas GregorTypedefDecl *ASTContext::getObjCClassDecl() const {
491079d6726921897811232554ed94c5d77b5b7b3fc0Douglas Gregor  if (!ObjCClassDecl) {
491179d6726921897811232554ed94c5d77b5b7b3fc0Douglas Gregor    QualType T = getObjCObjectType(ObjCBuiltinClassTy, 0, 0);
491279d6726921897811232554ed94c5d77b5b7b3fc0Douglas Gregor    T = getObjCObjectPointerType(T);
491379d6726921897811232554ed94c5d77b5b7b3fc0Douglas Gregor    TypeSourceInfo *ClassInfo = getTrivialTypeSourceInfo(T);
491479d6726921897811232554ed94c5d77b5b7b3fc0Douglas Gregor    ObjCClassDecl = TypedefDecl::Create(const_cast<ASTContext &>(*this),
491579d6726921897811232554ed94c5d77b5b7b3fc0Douglas Gregor                                        getTranslationUnitDecl(),
491679d6726921897811232554ed94c5d77b5b7b3fc0Douglas Gregor                                        SourceLocation(), SourceLocation(),
491779d6726921897811232554ed94c5d77b5b7b3fc0Douglas Gregor                                        &Idents.get("Class"), ClassInfo);
491879d6726921897811232554ed94c5d77b5b7b3fc0Douglas Gregor  }
491979d6726921897811232554ed94c5d77b5b7b3fc0Douglas Gregor
492079d6726921897811232554ed94c5d77b5b7b3fc0Douglas Gregor  return ObjCClassDecl;
4921a6ea10e22b600d92e084f6b11b9b9a92d0eb2412Douglas Gregor}
4922a6ea10e22b600d92e084f6b11b9b9a92d0eb2412Douglas Gregor
4923a6ea10e22b600d92e084f6b11b9b9a92d0eb2412Douglas GregorObjCInterfaceDecl *ASTContext::getObjCProtocolDecl() const {
4924a6ea10e22b600d92e084f6b11b9b9a92d0eb2412Douglas Gregor  if (!ObjCProtocolClassDecl) {
4925a6ea10e22b600d92e084f6b11b9b9a92d0eb2412Douglas Gregor    ObjCProtocolClassDecl
4926a6ea10e22b600d92e084f6b11b9b9a92d0eb2412Douglas Gregor      = ObjCInterfaceDecl::Create(*this, getTranslationUnitDecl(),
4927a6ea10e22b600d92e084f6b11b9b9a92d0eb2412Douglas Gregor                                  SourceLocation(),
4928a6ea10e22b600d92e084f6b11b9b9a92d0eb2412Douglas Gregor                                  &Idents.get("Protocol"),
4929a6ea10e22b600d92e084f6b11b9b9a92d0eb2412Douglas Gregor                                  /*PrevDecl=*/0,
4930a6ea10e22b600d92e084f6b11b9b9a92d0eb2412Douglas Gregor                                  SourceLocation(), true);
4931a6ea10e22b600d92e084f6b11b9b9a92d0eb2412Douglas Gregor  }
4932a6ea10e22b600d92e084f6b11b9b9a92d0eb2412Douglas Gregor
4933a6ea10e22b600d92e084f6b11b9b9a92d0eb2412Douglas Gregor  return ObjCProtocolClassDecl;
49348baaca50f07d0c10bba69c8d88c1b9078c92d06dAnders Carlsson}
49358baaca50f07d0c10bba69c8d88c1b9078c92d06dAnders Carlsson
4936a526c5c67e5a0473c340903ee542ce570119665fTed Kremenekvoid ASTContext::setObjCConstantStringInterface(ObjCInterfaceDecl *Decl) {
49371eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  assert(ObjCConstantStringType.isNull() &&
49382198891824c38d45b2279de5d5e3ef9394eb457cSteve Naroff         "'NSConstantString' type already set!");
49391eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
4940a526c5c67e5a0473c340903ee542ce570119665fTed Kremenek  ObjCConstantStringType = getObjCInterfaceType(Decl);
49412198891824c38d45b2279de5d5e3ef9394eb457cSteve Naroff}
49422198891824c38d45b2279de5d5e3ef9394eb457cSteve Naroff
49430bd6feb9e9d40fc889fd47e899985125a43dfed8John McCall/// \brief Retrieve the template name that corresponds to a non-empty
49440bd6feb9e9d40fc889fd47e899985125a43dfed8John McCall/// lookup.
49454ba2a17694148e16eaa8d3917f657ffcd3667be4Jay FoadTemplateName
49464ba2a17694148e16eaa8d3917f657ffcd3667be4Jay FoadASTContext::getOverloadedTemplateName(UnresolvedSetIterator Begin,
49474ba2a17694148e16eaa8d3917f657ffcd3667be4Jay Foad                                      UnresolvedSetIterator End) const {
49480bd6feb9e9d40fc889fd47e899985125a43dfed8John McCall  unsigned size = End - Begin;
49490bd6feb9e9d40fc889fd47e899985125a43dfed8John McCall  assert(size > 1 && "set is not overloaded!");
49507532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor
49510bd6feb9e9d40fc889fd47e899985125a43dfed8John McCall  void *memory = Allocate(sizeof(OverloadedTemplateStorage) +
49520bd6feb9e9d40fc889fd47e899985125a43dfed8John McCall                          size * sizeof(FunctionTemplateDecl*));
49530bd6feb9e9d40fc889fd47e899985125a43dfed8John McCall  OverloadedTemplateStorage *OT = new(memory) OverloadedTemplateStorage(size);
49540bd6feb9e9d40fc889fd47e899985125a43dfed8John McCall
49550bd6feb9e9d40fc889fd47e899985125a43dfed8John McCall  NamedDecl **Storage = OT->getStorage();
4956eec51cf1ba5f0e62c9cdb81b5c63babdd6e649abJohn McCall  for (UnresolvedSetIterator I = Begin; I != End; ++I) {
49570bd6feb9e9d40fc889fd47e899985125a43dfed8John McCall    NamedDecl *D = *I;
49580bd6feb9e9d40fc889fd47e899985125a43dfed8John McCall    assert(isa<FunctionTemplateDecl>(D) ||
49590bd6feb9e9d40fc889fd47e899985125a43dfed8John McCall           (isa<UsingShadowDecl>(D) &&
49600bd6feb9e9d40fc889fd47e899985125a43dfed8John McCall            isa<FunctionTemplateDecl>(D->getUnderlyingDecl())));
49610bd6feb9e9d40fc889fd47e899985125a43dfed8John McCall    *Storage++ = D;
49627532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor  }
49637532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor
49640bd6feb9e9d40fc889fd47e899985125a43dfed8John McCall  return TemplateName(OT);
49657532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor}
49667532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor
4967d99cbe66403ee39c2ee58024b9582b95649a4fc5Douglas Gregor/// \brief Retrieve the template name that represents a qualified
4968d99cbe66403ee39c2ee58024b9582b95649a4fc5Douglas Gregor/// template name such as \c std::vector.
49694ba2a17694148e16eaa8d3917f657ffcd3667be4Jay FoadTemplateName
49704ba2a17694148e16eaa8d3917f657ffcd3667be4Jay FoadASTContext::getQualifiedTemplateName(NestedNameSpecifier *NNS,
49714ba2a17694148e16eaa8d3917f657ffcd3667be4Jay Foad                                     bool TemplateKeyword,
49724ba2a17694148e16eaa8d3917f657ffcd3667be4Jay Foad                                     TemplateDecl *Template) const {
49730f0ea2a96534c615ff5fdd81363989b23cf2164aDouglas Gregor  assert(NNS && "Missing nested-name-specifier in qualified template name");
49740f0ea2a96534c615ff5fdd81363989b23cf2164aDouglas Gregor
4975789b1f640205e81b5af250693246120f1ce9d147Douglas Gregor  // FIXME: Canonicalization?
4976d99cbe66403ee39c2ee58024b9582b95649a4fc5Douglas Gregor  llvm::FoldingSetNodeID ID;
4977d99cbe66403ee39c2ee58024b9582b95649a4fc5Douglas Gregor  QualifiedTemplateName::Profile(ID, NNS, TemplateKeyword, Template);
49781eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
4979d99cbe66403ee39c2ee58024b9582b95649a4fc5Douglas Gregor  void *InsertPos = 0;
4980d99cbe66403ee39c2ee58024b9582b95649a4fc5Douglas Gregor  QualifiedTemplateName *QTN =
49810bd6feb9e9d40fc889fd47e899985125a43dfed8John McCall    QualifiedTemplateNames.FindNodeOrInsertPos(ID, InsertPos);
4982d99cbe66403ee39c2ee58024b9582b95649a4fc5Douglas Gregor  if (!QTN) {
4983d99cbe66403ee39c2ee58024b9582b95649a4fc5Douglas Gregor    QTN = new (*this,4) QualifiedTemplateName(NNS, TemplateKeyword, Template);
4984d99cbe66403ee39c2ee58024b9582b95649a4fc5Douglas Gregor    QualifiedTemplateNames.InsertNode(QTN, InsertPos);
4985d99cbe66403ee39c2ee58024b9582b95649a4fc5Douglas Gregor  }
49861eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
4987d99cbe66403ee39c2ee58024b9582b95649a4fc5Douglas Gregor  return TemplateName(QTN);
4988d99cbe66403ee39c2ee58024b9582b95649a4fc5Douglas Gregor}
4989d99cbe66403ee39c2ee58024b9582b95649a4fc5Douglas Gregor
49907532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor/// \brief Retrieve the template name that represents a dependent
49917532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor/// template name such as \c MetaFun::template apply.
49924ba2a17694148e16eaa8d3917f657ffcd3667be4Jay FoadTemplateName
49934ba2a17694148e16eaa8d3917f657ffcd3667be4Jay FoadASTContext::getDependentTemplateName(NestedNameSpecifier *NNS,
49944ba2a17694148e16eaa8d3917f657ffcd3667be4Jay Foad                                     const IdentifierInfo *Name) const {
49951eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  assert((!NNS || NNS->isDependent()) &&
49963b6afbb99a1c44b4076f8e15fb7311405941b306Douglas Gregor         "Nested name specifier must be dependent");
49977532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor
49987532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor  llvm::FoldingSetNodeID ID;
49997532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor  DependentTemplateName::Profile(ID, NNS, Name);
50007532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor
50017532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor  void *InsertPos = 0;
50027532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor  DependentTemplateName *QTN =
50037532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor    DependentTemplateNames.FindNodeOrInsertPos(ID, InsertPos);
50047532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor
50057532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor  if (QTN)
50067532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor    return TemplateName(QTN);
50077532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor
50087532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor  NestedNameSpecifier *CanonNNS = getCanonicalNestedNameSpecifier(NNS);
50097532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor  if (CanonNNS == NNS) {
50107532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor    QTN = new (*this,4) DependentTemplateName(NNS, Name);
50117532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor  } else {
50127532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor    TemplateName Canon = getDependentTemplateName(CanonNNS, Name);
50137532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor    QTN = new (*this,4) DependentTemplateName(NNS, Name, Canon);
5014789b1f640205e81b5af250693246120f1ce9d147Douglas Gregor    DependentTemplateName *CheckQTN =
5015789b1f640205e81b5af250693246120f1ce9d147Douglas Gregor      DependentTemplateNames.FindNodeOrInsertPos(ID, InsertPos);
5016789b1f640205e81b5af250693246120f1ce9d147Douglas Gregor    assert(!CheckQTN && "Dependent type name canonicalization broken");
5017789b1f640205e81b5af250693246120f1ce9d147Douglas Gregor    (void)CheckQTN;
50187532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor  }
50197532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor
50207532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor  DependentTemplateNames.InsertNode(QTN, InsertPos);
50217532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor  return TemplateName(QTN);
50227532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor}
50237532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor
5024ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor/// \brief Retrieve the template name that represents a dependent
5025ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor/// template name such as \c MetaFun::template operator+.
5026ca1bdd7c269a2390d43c040a60511edd017ee130Douglas GregorTemplateName
5027ca1bdd7c269a2390d43c040a60511edd017ee130Douglas GregorASTContext::getDependentTemplateName(NestedNameSpecifier *NNS,
50284ba2a17694148e16eaa8d3917f657ffcd3667be4Jay Foad                                     OverloadedOperatorKind Operator) const {
5029ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor  assert((!NNS || NNS->isDependent()) &&
5030ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor         "Nested name specifier must be dependent");
5031ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor
5032ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor  llvm::FoldingSetNodeID ID;
5033ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor  DependentTemplateName::Profile(ID, NNS, Operator);
5034ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor
5035ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor  void *InsertPos = 0;
5036789b1f640205e81b5af250693246120f1ce9d147Douglas Gregor  DependentTemplateName *QTN
5037789b1f640205e81b5af250693246120f1ce9d147Douglas Gregor    = DependentTemplateNames.FindNodeOrInsertPos(ID, InsertPos);
5038ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor
5039ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor  if (QTN)
5040ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor    return TemplateName(QTN);
5041ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor
5042ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor  NestedNameSpecifier *CanonNNS = getCanonicalNestedNameSpecifier(NNS);
5043ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor  if (CanonNNS == NNS) {
5044ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor    QTN = new (*this,4) DependentTemplateName(NNS, Operator);
5045ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor  } else {
5046ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor    TemplateName Canon = getDependentTemplateName(CanonNNS, Operator);
5047ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor    QTN = new (*this,4) DependentTemplateName(NNS, Operator, Canon);
5048789b1f640205e81b5af250693246120f1ce9d147Douglas Gregor
5049789b1f640205e81b5af250693246120f1ce9d147Douglas Gregor    DependentTemplateName *CheckQTN
5050789b1f640205e81b5af250693246120f1ce9d147Douglas Gregor      = DependentTemplateNames.FindNodeOrInsertPos(ID, InsertPos);
5051789b1f640205e81b5af250693246120f1ce9d147Douglas Gregor    assert(!CheckQTN && "Dependent template name canonicalization broken");
5052789b1f640205e81b5af250693246120f1ce9d147Douglas Gregor    (void)CheckQTN;
5053ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor  }
5054ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor
5055ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor  DependentTemplateNames.InsertNode(QTN, InsertPos);
5056ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor  return TemplateName(QTN);
5057ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor}
5058ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor
50591aee05d08b2184acadeb36de300e216390780d6cDouglas GregorTemplateName
5060146060435c3efce95c95a092c7a1eb651cfb9ae0John McCallASTContext::getSubstTemplateTemplateParm(TemplateTemplateParmDecl *param,
5061146060435c3efce95c95a092c7a1eb651cfb9ae0John McCall                                         TemplateName replacement) const {
5062146060435c3efce95c95a092c7a1eb651cfb9ae0John McCall  llvm::FoldingSetNodeID ID;
5063146060435c3efce95c95a092c7a1eb651cfb9ae0John McCall  SubstTemplateTemplateParmStorage::Profile(ID, param, replacement);
5064146060435c3efce95c95a092c7a1eb651cfb9ae0John McCall
5065146060435c3efce95c95a092c7a1eb651cfb9ae0John McCall  void *insertPos = 0;
5066146060435c3efce95c95a092c7a1eb651cfb9ae0John McCall  SubstTemplateTemplateParmStorage *subst
5067146060435c3efce95c95a092c7a1eb651cfb9ae0John McCall    = SubstTemplateTemplateParms.FindNodeOrInsertPos(ID, insertPos);
5068146060435c3efce95c95a092c7a1eb651cfb9ae0John McCall
5069146060435c3efce95c95a092c7a1eb651cfb9ae0John McCall  if (!subst) {
5070146060435c3efce95c95a092c7a1eb651cfb9ae0John McCall    subst = new (*this) SubstTemplateTemplateParmStorage(param, replacement);
5071146060435c3efce95c95a092c7a1eb651cfb9ae0John McCall    SubstTemplateTemplateParms.InsertNode(subst, insertPos);
5072146060435c3efce95c95a092c7a1eb651cfb9ae0John McCall  }
5073146060435c3efce95c95a092c7a1eb651cfb9ae0John McCall
5074146060435c3efce95c95a092c7a1eb651cfb9ae0John McCall  return TemplateName(subst);
5075146060435c3efce95c95a092c7a1eb651cfb9ae0John McCall}
5076146060435c3efce95c95a092c7a1eb651cfb9ae0John McCall
5077146060435c3efce95c95a092c7a1eb651cfb9ae0John McCallTemplateName
50781aee05d08b2184acadeb36de300e216390780d6cDouglas GregorASTContext::getSubstTemplateTemplateParmPack(TemplateTemplateParmDecl *Param,
50791aee05d08b2184acadeb36de300e216390780d6cDouglas Gregor                                       const TemplateArgument &ArgPack) const {
50801aee05d08b2184acadeb36de300e216390780d6cDouglas Gregor  ASTContext &Self = const_cast<ASTContext &>(*this);
50811aee05d08b2184acadeb36de300e216390780d6cDouglas Gregor  llvm::FoldingSetNodeID ID;
50821aee05d08b2184acadeb36de300e216390780d6cDouglas Gregor  SubstTemplateTemplateParmPackStorage::Profile(ID, Self, Param, ArgPack);
50831aee05d08b2184acadeb36de300e216390780d6cDouglas Gregor
50841aee05d08b2184acadeb36de300e216390780d6cDouglas Gregor  void *InsertPos = 0;
50851aee05d08b2184acadeb36de300e216390780d6cDouglas Gregor  SubstTemplateTemplateParmPackStorage *Subst
50861aee05d08b2184acadeb36de300e216390780d6cDouglas Gregor    = SubstTemplateTemplateParmPacks.FindNodeOrInsertPos(ID, InsertPos);
50871aee05d08b2184acadeb36de300e216390780d6cDouglas Gregor
50881aee05d08b2184acadeb36de300e216390780d6cDouglas Gregor  if (!Subst) {
5089146060435c3efce95c95a092c7a1eb651cfb9ae0John McCall    Subst = new (*this) SubstTemplateTemplateParmPackStorage(Param,
50901aee05d08b2184acadeb36de300e216390780d6cDouglas Gregor                                                           ArgPack.pack_size(),
50911aee05d08b2184acadeb36de300e216390780d6cDouglas Gregor                                                         ArgPack.pack_begin());
50921aee05d08b2184acadeb36de300e216390780d6cDouglas Gregor    SubstTemplateTemplateParmPacks.InsertNode(Subst, InsertPos);
50931aee05d08b2184acadeb36de300e216390780d6cDouglas Gregor  }
50941aee05d08b2184acadeb36de300e216390780d6cDouglas Gregor
50951aee05d08b2184acadeb36de300e216390780d6cDouglas Gregor  return TemplateName(Subst);
50961aee05d08b2184acadeb36de300e216390780d6cDouglas Gregor}
50971aee05d08b2184acadeb36de300e216390780d6cDouglas Gregor
5098b4e66d5259f90e9aae4d40fc5de801e046c7df94Douglas Gregor/// getFromTargetType - Given one of the integer types provided by
5099d934112e6170b0fd940d8e40db6936cea2cdcf62Douglas Gregor/// TargetInfo, produce the corresponding type. The unsigned @p Type
5100d934112e6170b0fd940d8e40db6936cea2cdcf62Douglas Gregor/// is actually a value of type @c TargetInfo::IntType.
5101e27ec8ad56dbf1efb2de004b90fbbb86f740e3f1John McCallCanQualType ASTContext::getFromTargetType(unsigned Type) const {
5102b4e66d5259f90e9aae4d40fc5de801e046c7df94Douglas Gregor  switch (Type) {
5103e27ec8ad56dbf1efb2de004b90fbbb86f740e3f1John McCall  case TargetInfo::NoInt: return CanQualType();
5104b4e66d5259f90e9aae4d40fc5de801e046c7df94Douglas Gregor  case TargetInfo::SignedShort: return ShortTy;
5105b4e66d5259f90e9aae4d40fc5de801e046c7df94Douglas Gregor  case TargetInfo::UnsignedShort: return UnsignedShortTy;
5106b4e66d5259f90e9aae4d40fc5de801e046c7df94Douglas Gregor  case TargetInfo::SignedInt: return IntTy;
5107b4e66d5259f90e9aae4d40fc5de801e046c7df94Douglas Gregor  case TargetInfo::UnsignedInt: return UnsignedIntTy;
5108b4e66d5259f90e9aae4d40fc5de801e046c7df94Douglas Gregor  case TargetInfo::SignedLong: return LongTy;
5109b4e66d5259f90e9aae4d40fc5de801e046c7df94Douglas Gregor  case TargetInfo::UnsignedLong: return UnsignedLongTy;
5110b4e66d5259f90e9aae4d40fc5de801e046c7df94Douglas Gregor  case TargetInfo::SignedLongLong: return LongLongTy;
5111b4e66d5259f90e9aae4d40fc5de801e046c7df94Douglas Gregor  case TargetInfo::UnsignedLongLong: return UnsignedLongLongTy;
5112b4e66d5259f90e9aae4d40fc5de801e046c7df94Douglas Gregor  }
5113b4e66d5259f90e9aae4d40fc5de801e046c7df94Douglas Gregor
5114b219cfc4d75f0a03630b7c4509ef791b7e97b2c8David Blaikie  llvm_unreachable("Unhandled TargetInfo::IntType value");
5115b4e66d5259f90e9aae4d40fc5de801e046c7df94Douglas Gregor}
5116b6ccaac65ca72f72954eb3893bbd940bedd23f00Ted Kremenek
5117b6ccaac65ca72f72954eb3893bbd940bedd23f00Ted Kremenek//===----------------------------------------------------------------------===//
5118b6ccaac65ca72f72954eb3893bbd940bedd23f00Ted Kremenek//                        Type Predicates.
5119b6ccaac65ca72f72954eb3893bbd940bedd23f00Ted Kremenek//===----------------------------------------------------------------------===//
5120b6ccaac65ca72f72954eb3893bbd940bedd23f00Ted Kremenek
51214fd83ea566f4a0c083001c84b75da6cc8c99c1d6Fariborz Jahanian/// getObjCGCAttr - Returns one of GCNone, Weak or Strong objc's
51224fd83ea566f4a0c083001c84b75da6cc8c99c1d6Fariborz Jahanian/// garbage collection attribute.
51234fd83ea566f4a0c083001c84b75da6cc8c99c1d6Fariborz Jahanian///
5124ae278a3a57595349a411f6474938d4dd1b263a0eJohn McCallQualifiers::GC ASTContext::getObjCGCAttrKind(QualType Ty) const {
5125e289d81369914678db386f6aa86faf8f178e245dDouglas Gregor  if (getLangOptions().getGC() == LangOptions::NonGC)
5126ae278a3a57595349a411f6474938d4dd1b263a0eJohn McCall    return Qualifiers::GCNone;
5127ae278a3a57595349a411f6474938d4dd1b263a0eJohn McCall
5128ae278a3a57595349a411f6474938d4dd1b263a0eJohn McCall  assert(getLangOptions().ObjC1);
5129ae278a3a57595349a411f6474938d4dd1b263a0eJohn McCall  Qualifiers::GC GCAttrs = Ty.getObjCGCAttr();
5130ae278a3a57595349a411f6474938d4dd1b263a0eJohn McCall
5131ae278a3a57595349a411f6474938d4dd1b263a0eJohn McCall  // Default behaviour under objective-C's gc is for ObjC pointers
5132ae278a3a57595349a411f6474938d4dd1b263a0eJohn McCall  // (or pointers to them) be treated as though they were declared
5133ae278a3a57595349a411f6474938d4dd1b263a0eJohn McCall  // as __strong.
5134ae278a3a57595349a411f6474938d4dd1b263a0eJohn McCall  if (GCAttrs == Qualifiers::GCNone) {
5135ae278a3a57595349a411f6474938d4dd1b263a0eJohn McCall    if (Ty->isObjCObjectPointerType() || Ty->isBlockPointerType())
5136ae278a3a57595349a411f6474938d4dd1b263a0eJohn McCall      return Qualifiers::Strong;
5137ae278a3a57595349a411f6474938d4dd1b263a0eJohn McCall    else if (Ty->isPointerType())
5138ae278a3a57595349a411f6474938d4dd1b263a0eJohn McCall      return getObjCGCAttrKind(Ty->getAs<PointerType>()->getPointeeType());
5139ae278a3a57595349a411f6474938d4dd1b263a0eJohn McCall  } else {
5140ae278a3a57595349a411f6474938d4dd1b263a0eJohn McCall    // It's not valid to set GC attributes on anything that isn't a
5141ae278a3a57595349a411f6474938d4dd1b263a0eJohn McCall    // pointer.
5142ae278a3a57595349a411f6474938d4dd1b263a0eJohn McCall#ifndef NDEBUG
5143ae278a3a57595349a411f6474938d4dd1b263a0eJohn McCall    QualType CT = Ty->getCanonicalTypeInternal();
5144ae278a3a57595349a411f6474938d4dd1b263a0eJohn McCall    while (const ArrayType *AT = dyn_cast<ArrayType>(CT))
5145ae278a3a57595349a411f6474938d4dd1b263a0eJohn McCall      CT = AT->getElementType();
5146ae278a3a57595349a411f6474938d4dd1b263a0eJohn McCall    assert(CT->isAnyPointerType() || CT->isBlockPointerType());
5147ae278a3a57595349a411f6474938d4dd1b263a0eJohn McCall#endif
51484fd83ea566f4a0c083001c84b75da6cc8c99c1d6Fariborz Jahanian  }
5149b7d2553edd2532d29b98b9e76bcf6a62bc48b417Chris Lattner  return GCAttrs;
51504fd83ea566f4a0c083001c84b75da6cc8c99c1d6Fariborz Jahanian}
51514fd83ea566f4a0c083001c84b75da6cc8c99c1d6Fariborz Jahanian
51526ac46a4a521366d7ab36ebe2ce4e624ab96b06f9Chris Lattner//===----------------------------------------------------------------------===//
51536ac46a4a521366d7ab36ebe2ce4e624ab96b06f9Chris Lattner//                        Type Compatibility Testing
51546ac46a4a521366d7ab36ebe2ce4e624ab96b06f9Chris Lattner//===----------------------------------------------------------------------===//
5155770951b5bb6028a8d326ddb4a13cef7d4a128162Chris Lattner
51561eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// areCompatVectorTypes - Return true if the two specified vector types are
51576ac46a4a521366d7ab36ebe2ce4e624ab96b06f9Chris Lattner/// compatible.
51586ac46a4a521366d7ab36ebe2ce4e624ab96b06f9Chris Lattnerstatic bool areCompatVectorTypes(const VectorType *LHS,
51596ac46a4a521366d7ab36ebe2ce4e624ab96b06f9Chris Lattner                                 const VectorType *RHS) {
5160467b27b9a24bdc823218ad1ad0e37673b6cc1e83John McCall  assert(LHS->isCanonicalUnqualified() && RHS->isCanonicalUnqualified());
51616ac46a4a521366d7ab36ebe2ce4e624ab96b06f9Chris Lattner  return LHS->getElementType() == RHS->getElementType() &&
516261710854be2b098428aff5316e64bd34b30fbcb7Chris Lattner         LHS->getNumElements() == RHS->getNumElements();
51636ac46a4a521366d7ab36ebe2ce4e624ab96b06f9Chris Lattner}
51646ac46a4a521366d7ab36ebe2ce4e624ab96b06f9Chris Lattner
5165255210ef415b9893f0e3794e8d9a704194c12f3cDouglas Gregorbool ASTContext::areCompatibleVectorTypes(QualType FirstVec,
5166255210ef415b9893f0e3794e8d9a704194c12f3cDouglas Gregor                                          QualType SecondVec) {
5167255210ef415b9893f0e3794e8d9a704194c12f3cDouglas Gregor  assert(FirstVec->isVectorType() && "FirstVec should be a vector type");
5168255210ef415b9893f0e3794e8d9a704194c12f3cDouglas Gregor  assert(SecondVec->isVectorType() && "SecondVec should be a vector type");
5169255210ef415b9893f0e3794e8d9a704194c12f3cDouglas Gregor
5170255210ef415b9893f0e3794e8d9a704194c12f3cDouglas Gregor  if (hasSameUnqualifiedType(FirstVec, SecondVec))
5171255210ef415b9893f0e3794e8d9a704194c12f3cDouglas Gregor    return true;
5172255210ef415b9893f0e3794e8d9a704194c12f3cDouglas Gregor
5173f69eb7cf8e616b5aad7911ec6f79b24b0a009227Bob Wilson  // Treat Neon vector types and most AltiVec vector types as if they are the
5174f69eb7cf8e616b5aad7911ec6f79b24b0a009227Bob Wilson  // equivalent GCC vector types.
5175255210ef415b9893f0e3794e8d9a704194c12f3cDouglas Gregor  const VectorType *First = FirstVec->getAs<VectorType>();
5176255210ef415b9893f0e3794e8d9a704194c12f3cDouglas Gregor  const VectorType *Second = SecondVec->getAs<VectorType>();
5177f69eb7cf8e616b5aad7911ec6f79b24b0a009227Bob Wilson  if (First->getNumElements() == Second->getNumElements() &&
5178255210ef415b9893f0e3794e8d9a704194c12f3cDouglas Gregor      hasSameType(First->getElementType(), Second->getElementType()) &&
5179f69eb7cf8e616b5aad7911ec6f79b24b0a009227Bob Wilson      First->getVectorKind() != VectorType::AltiVecPixel &&
5180f69eb7cf8e616b5aad7911ec6f79b24b0a009227Bob Wilson      First->getVectorKind() != VectorType::AltiVecBool &&
5181f69eb7cf8e616b5aad7911ec6f79b24b0a009227Bob Wilson      Second->getVectorKind() != VectorType::AltiVecPixel &&
5182f69eb7cf8e616b5aad7911ec6f79b24b0a009227Bob Wilson      Second->getVectorKind() != VectorType::AltiVecBool)
5183255210ef415b9893f0e3794e8d9a704194c12f3cDouglas Gregor    return true;
5184255210ef415b9893f0e3794e8d9a704194c12f3cDouglas Gregor
5185255210ef415b9893f0e3794e8d9a704194c12f3cDouglas Gregor  return false;
5186255210ef415b9893f0e3794e8d9a704194c12f3cDouglas Gregor}
5187255210ef415b9893f0e3794e8d9a704194c12f3cDouglas Gregor
51884084c306635b70f37029dca938444e6013f08684Steve Naroff//===----------------------------------------------------------------------===//
51894084c306635b70f37029dca938444e6013f08684Steve Naroff// ObjCQualifiedIdTypesAreCompatible - Compatibility testing for qualified id's.
51904084c306635b70f37029dca938444e6013f08684Steve Naroff//===----------------------------------------------------------------------===//
51914084c306635b70f37029dca938444e6013f08684Steve Naroff
51924084c306635b70f37029dca938444e6013f08684Steve Naroff/// ProtocolCompatibleWithProtocol - return 'true' if 'lProto' is in the
51934084c306635b70f37029dca938444e6013f08684Steve Naroff/// inheritance hierarchy of 'rProto'.
51944ba2a17694148e16eaa8d3917f657ffcd3667be4Jay Foadbool
51954ba2a17694148e16eaa8d3917f657ffcd3667be4Jay FoadASTContext::ProtocolCompatibleWithProtocol(ObjCProtocolDecl *lProto,
51964ba2a17694148e16eaa8d3917f657ffcd3667be4Jay Foad                                           ObjCProtocolDecl *rProto) const {
51973fc73ee0c613715ebce78e30b4d050ea715a007dDouglas Gregor  if (declaresSameEntity(lProto, rProto))
519814108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff    return true;
51994084c306635b70f37029dca938444e6013f08684Steve Naroff  for (ObjCProtocolDecl::protocol_iterator PI = rProto->protocol_begin(),
52004084c306635b70f37029dca938444e6013f08684Steve Naroff       E = rProto->protocol_end(); PI != E; ++PI)
52014084c306635b70f37029dca938444e6013f08684Steve Naroff    if (ProtocolCompatibleWithProtocol(lProto, *PI))
52024084c306635b70f37029dca938444e6013f08684Steve Naroff      return true;
52034084c306635b70f37029dca938444e6013f08684Steve Naroff  return false;
52044084c306635b70f37029dca938444e6013f08684Steve Naroff}
520514108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff
52064084c306635b70f37029dca938444e6013f08684Steve Naroff/// QualifiedIdConformsQualifiedId - compare id<p,...> with id<p1,...>
52074084c306635b70f37029dca938444e6013f08684Steve Naroff/// return true if lhs's protocols conform to rhs's protocol; false
52084084c306635b70f37029dca938444e6013f08684Steve Naroff/// otherwise.
52094084c306635b70f37029dca938444e6013f08684Steve Naroffbool ASTContext::QualifiedIdConformsQualifiedId(QualType lhs, QualType rhs) {
52104084c306635b70f37029dca938444e6013f08684Steve Naroff  if (lhs->isObjCQualifiedIdType() && rhs->isObjCQualifiedIdType())
52114084c306635b70f37029dca938444e6013f08684Steve Naroff    return ObjCQualifiedIdTypesAreCompatible(lhs, rhs, false);
52124084c306635b70f37029dca938444e6013f08684Steve Naroff  return false;
52134084c306635b70f37029dca938444e6013f08684Steve Naroff}
52144084c306635b70f37029dca938444e6013f08684Steve Naroff
5215a8f8dac6a29f6d33474a38a32ce9dd859b696da9Fariborz Jahanian/// ObjCQualifiedClassTypesAreCompatible - compare  Class<p,...> and
5216a8f8dac6a29f6d33474a38a32ce9dd859b696da9Fariborz Jahanian/// Class<p1, ...>.
5217a8f8dac6a29f6d33474a38a32ce9dd859b696da9Fariborz Jahanianbool ASTContext::ObjCQualifiedClassTypesAreCompatible(QualType lhs,
5218a8f8dac6a29f6d33474a38a32ce9dd859b696da9Fariborz Jahanian                                                      QualType rhs) {
5219a8f8dac6a29f6d33474a38a32ce9dd859b696da9Fariborz Jahanian  const ObjCObjectPointerType *lhsQID = lhs->getAs<ObjCObjectPointerType>();
5220a8f8dac6a29f6d33474a38a32ce9dd859b696da9Fariborz Jahanian  const ObjCObjectPointerType *rhsOPT = rhs->getAs<ObjCObjectPointerType>();
5221a8f8dac6a29f6d33474a38a32ce9dd859b696da9Fariborz Jahanian  assert ((lhsQID && rhsOPT) && "ObjCQualifiedClassTypesAreCompatible");
5222a8f8dac6a29f6d33474a38a32ce9dd859b696da9Fariborz Jahanian
5223a8f8dac6a29f6d33474a38a32ce9dd859b696da9Fariborz Jahanian  for (ObjCObjectPointerType::qual_iterator I = lhsQID->qual_begin(),
5224a8f8dac6a29f6d33474a38a32ce9dd859b696da9Fariborz Jahanian       E = lhsQID->qual_end(); I != E; ++I) {
5225a8f8dac6a29f6d33474a38a32ce9dd859b696da9Fariborz Jahanian    bool match = false;
5226a8f8dac6a29f6d33474a38a32ce9dd859b696da9Fariborz Jahanian    ObjCProtocolDecl *lhsProto = *I;
5227a8f8dac6a29f6d33474a38a32ce9dd859b696da9Fariborz Jahanian    for (ObjCObjectPointerType::qual_iterator J = rhsOPT->qual_begin(),
5228a8f8dac6a29f6d33474a38a32ce9dd859b696da9Fariborz Jahanian         E = rhsOPT->qual_end(); J != E; ++J) {
5229a8f8dac6a29f6d33474a38a32ce9dd859b696da9Fariborz Jahanian      ObjCProtocolDecl *rhsProto = *J;
5230a8f8dac6a29f6d33474a38a32ce9dd859b696da9Fariborz Jahanian      if (ProtocolCompatibleWithProtocol(lhsProto, rhsProto)) {
5231a8f8dac6a29f6d33474a38a32ce9dd859b696da9Fariborz Jahanian        match = true;
5232a8f8dac6a29f6d33474a38a32ce9dd859b696da9Fariborz Jahanian        break;
5233a8f8dac6a29f6d33474a38a32ce9dd859b696da9Fariborz Jahanian      }
5234a8f8dac6a29f6d33474a38a32ce9dd859b696da9Fariborz Jahanian    }
5235a8f8dac6a29f6d33474a38a32ce9dd859b696da9Fariborz Jahanian    if (!match)
5236a8f8dac6a29f6d33474a38a32ce9dd859b696da9Fariborz Jahanian      return false;
5237a8f8dac6a29f6d33474a38a32ce9dd859b696da9Fariborz Jahanian  }
5238a8f8dac6a29f6d33474a38a32ce9dd859b696da9Fariborz Jahanian  return true;
5239a8f8dac6a29f6d33474a38a32ce9dd859b696da9Fariborz Jahanian}
5240a8f8dac6a29f6d33474a38a32ce9dd859b696da9Fariborz Jahanian
52414084c306635b70f37029dca938444e6013f08684Steve Naroff/// ObjCQualifiedIdTypesAreCompatible - We know that one of lhs/rhs is an
52424084c306635b70f37029dca938444e6013f08684Steve Naroff/// ObjCQualifiedIDType.
52434084c306635b70f37029dca938444e6013f08684Steve Naroffbool ASTContext::ObjCQualifiedIdTypesAreCompatible(QualType lhs, QualType rhs,
52444084c306635b70f37029dca938444e6013f08684Steve Naroff                                                   bool compare) {
52454084c306635b70f37029dca938444e6013f08684Steve Naroff  // Allow id<P..> and an 'id' or void* type in all cases.
52461eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  if (lhs->isVoidPointerType() ||
52474084c306635b70f37029dca938444e6013f08684Steve Naroff      lhs->isObjCIdType() || lhs->isObjCClassType())
52484084c306635b70f37029dca938444e6013f08684Steve Naroff    return true;
52491eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  else if (rhs->isVoidPointerType() ||
52504084c306635b70f37029dca938444e6013f08684Steve Naroff           rhs->isObjCIdType() || rhs->isObjCClassType())
52514084c306635b70f37029dca938444e6013f08684Steve Naroff    return true;
52524084c306635b70f37029dca938444e6013f08684Steve Naroff
52534084c306635b70f37029dca938444e6013f08684Steve Naroff  if (const ObjCObjectPointerType *lhsQID = lhs->getAsObjCQualifiedIdType()) {
5254183700f494ec9b6701b6efe82bcb25f4c79ba561John McCall    const ObjCObjectPointerType *rhsOPT = rhs->getAs<ObjCObjectPointerType>();
52551eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
52564084c306635b70f37029dca938444e6013f08684Steve Naroff    if (!rhsOPT) return false;
52571eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
52584084c306635b70f37029dca938444e6013f08684Steve Naroff    if (rhsOPT->qual_empty()) {
52591eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      // If the RHS is a unqualified interface pointer "NSString*",
52604084c306635b70f37029dca938444e6013f08684Steve Naroff      // make sure we check the class hierarchy.
52614084c306635b70f37029dca938444e6013f08684Steve Naroff      if (ObjCInterfaceDecl *rhsID = rhsOPT->getInterfaceDecl()) {
52624084c306635b70f37029dca938444e6013f08684Steve Naroff        for (ObjCObjectPointerType::qual_iterator I = lhsQID->qual_begin(),
52634084c306635b70f37029dca938444e6013f08684Steve Naroff             E = lhsQID->qual_end(); I != E; ++I) {
52644084c306635b70f37029dca938444e6013f08684Steve Naroff          // when comparing an id<P> on lhs with a static type on rhs,
52654084c306635b70f37029dca938444e6013f08684Steve Naroff          // see if static class implements all of id's protocols, directly or
52664084c306635b70f37029dca938444e6013f08684Steve Naroff          // through its super class and categories.
52670fd8904c5f71a11d29f67716c3ebdf7ad1c855fbFariborz Jahanian          if (!rhsID->ClassImplementsProtocol(*I, true))
52684084c306635b70f37029dca938444e6013f08684Steve Naroff            return false;
52694084c306635b70f37029dca938444e6013f08684Steve Naroff        }
52704084c306635b70f37029dca938444e6013f08684Steve Naroff      }
52714084c306635b70f37029dca938444e6013f08684Steve Naroff      // If there are no qualifiers and no interface, we have an 'id'.
52724084c306635b70f37029dca938444e6013f08684Steve Naroff      return true;
52734084c306635b70f37029dca938444e6013f08684Steve Naroff    }
52741eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    // Both the right and left sides have qualifiers.
52754084c306635b70f37029dca938444e6013f08684Steve Naroff    for (ObjCObjectPointerType::qual_iterator I = lhsQID->qual_begin(),
52764084c306635b70f37029dca938444e6013f08684Steve Naroff         E = lhsQID->qual_end(); I != E; ++I) {
52774084c306635b70f37029dca938444e6013f08684Steve Naroff      ObjCProtocolDecl *lhsProto = *I;
52784084c306635b70f37029dca938444e6013f08684Steve Naroff      bool match = false;
5279de2e22d33afec98324a66a358dfe0951b3c7259aSteve Naroff
5280de2e22d33afec98324a66a358dfe0951b3c7259aSteve Naroff      // when comparing an id<P> on lhs with a static type on rhs,
5281de2e22d33afec98324a66a358dfe0951b3c7259aSteve Naroff      // see if static class implements all of id's protocols, directly or
5282de2e22d33afec98324a66a358dfe0951b3c7259aSteve Naroff      // through its super class and categories.
52834084c306635b70f37029dca938444e6013f08684Steve Naroff      for (ObjCObjectPointerType::qual_iterator J = rhsOPT->qual_begin(),
52844084c306635b70f37029dca938444e6013f08684Steve Naroff           E = rhsOPT->qual_end(); J != E; ++J) {
52854084c306635b70f37029dca938444e6013f08684Steve Naroff        ObjCProtocolDecl *rhsProto = *J;
52864084c306635b70f37029dca938444e6013f08684Steve Naroff        if (ProtocolCompatibleWithProtocol(lhsProto, rhsProto) ||
52874084c306635b70f37029dca938444e6013f08684Steve Naroff            (compare && ProtocolCompatibleWithProtocol(rhsProto, lhsProto))) {
52884084c306635b70f37029dca938444e6013f08684Steve Naroff          match = true;
52898f16756441450ed9fb39316e47d107fc2a1ef35bSteve Naroff          break;
52908f16756441450ed9fb39316e47d107fc2a1ef35bSteve Naroff        }
5291de2e22d33afec98324a66a358dfe0951b3c7259aSteve Naroff      }
52921eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      // If the RHS is a qualified interface pointer "NSString<P>*",
52934084c306635b70f37029dca938444e6013f08684Steve Naroff      // make sure we check the class hierarchy.
52944084c306635b70f37029dca938444e6013f08684Steve Naroff      if (ObjCInterfaceDecl *rhsID = rhsOPT->getInterfaceDecl()) {
52954084c306635b70f37029dca938444e6013f08684Steve Naroff        for (ObjCObjectPointerType::qual_iterator I = lhsQID->qual_begin(),
52964084c306635b70f37029dca938444e6013f08684Steve Naroff             E = lhsQID->qual_end(); I != E; ++I) {
52974084c306635b70f37029dca938444e6013f08684Steve Naroff          // when comparing an id<P> on lhs with a static type on rhs,
52984084c306635b70f37029dca938444e6013f08684Steve Naroff          // see if static class implements all of id's protocols, directly or
52994084c306635b70f37029dca938444e6013f08684Steve Naroff          // through its super class and categories.
53000fd8904c5f71a11d29f67716c3ebdf7ad1c855fbFariborz Jahanian          if (rhsID->ClassImplementsProtocol(*I, true)) {
53014084c306635b70f37029dca938444e6013f08684Steve Naroff            match = true;
53024084c306635b70f37029dca938444e6013f08684Steve Naroff            break;
53034084c306635b70f37029dca938444e6013f08684Steve Naroff          }
53044084c306635b70f37029dca938444e6013f08684Steve Naroff        }
53054084c306635b70f37029dca938444e6013f08684Steve Naroff      }
53064084c306635b70f37029dca938444e6013f08684Steve Naroff      if (!match)
5307de2e22d33afec98324a66a358dfe0951b3c7259aSteve Naroff        return false;
5308de2e22d33afec98324a66a358dfe0951b3c7259aSteve Naroff    }
53091eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
5310de2e22d33afec98324a66a358dfe0951b3c7259aSteve Naroff    return true;
5311de2e22d33afec98324a66a358dfe0951b3c7259aSteve Naroff  }
53121eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
53134084c306635b70f37029dca938444e6013f08684Steve Naroff  const ObjCObjectPointerType *rhsQID = rhs->getAsObjCQualifiedIdType();
53144084c306635b70f37029dca938444e6013f08684Steve Naroff  assert(rhsQID && "One of the LHS/RHS should be id<x>");
53154084c306635b70f37029dca938444e6013f08684Steve Naroff
53161eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  if (const ObjCObjectPointerType *lhsOPT =
53174084c306635b70f37029dca938444e6013f08684Steve Naroff        lhs->getAsObjCInterfacePointerType()) {
5318de5b17ea3c74d2844ed035a1edfa6479866139b5Fariborz Jahanian    // If both the right and left sides have qualifiers.
53194084c306635b70f37029dca938444e6013f08684Steve Naroff    for (ObjCObjectPointerType::qual_iterator I = lhsOPT->qual_begin(),
53204084c306635b70f37029dca938444e6013f08684Steve Naroff         E = lhsOPT->qual_end(); I != E; ++I) {
53214084c306635b70f37029dca938444e6013f08684Steve Naroff      ObjCProtocolDecl *lhsProto = *I;
53224084c306635b70f37029dca938444e6013f08684Steve Naroff      bool match = false;
53234084c306635b70f37029dca938444e6013f08684Steve Naroff
5324de5b17ea3c74d2844ed035a1edfa6479866139b5Fariborz Jahanian      // when comparing an id<P> on rhs with a static type on lhs,
53254084c306635b70f37029dca938444e6013f08684Steve Naroff      // see if static class implements all of id's protocols, directly or
53264084c306635b70f37029dca938444e6013f08684Steve Naroff      // through its super class and categories.
5327de5b17ea3c74d2844ed035a1edfa6479866139b5Fariborz Jahanian      // First, lhs protocols in the qualifier list must be found, direct
5328de5b17ea3c74d2844ed035a1edfa6479866139b5Fariborz Jahanian      // or indirect in rhs's qualifier list or it is a mismatch.
53294084c306635b70f37029dca938444e6013f08684Steve Naroff      for (ObjCObjectPointerType::qual_iterator J = rhsQID->qual_begin(),
53304084c306635b70f37029dca938444e6013f08684Steve Naroff           E = rhsQID->qual_end(); J != E; ++J) {
53314084c306635b70f37029dca938444e6013f08684Steve Naroff        ObjCProtocolDecl *rhsProto = *J;
53324084c306635b70f37029dca938444e6013f08684Steve Naroff        if (ProtocolCompatibleWithProtocol(lhsProto, rhsProto) ||
53334084c306635b70f37029dca938444e6013f08684Steve Naroff            (compare && ProtocolCompatibleWithProtocol(rhsProto, lhsProto))) {
53344084c306635b70f37029dca938444e6013f08684Steve Naroff          match = true;
53354084c306635b70f37029dca938444e6013f08684Steve Naroff          break;
53364084c306635b70f37029dca938444e6013f08684Steve Naroff        }
53374084c306635b70f37029dca938444e6013f08684Steve Naroff      }
53384084c306635b70f37029dca938444e6013f08684Steve Naroff      if (!match)
53394084c306635b70f37029dca938444e6013f08684Steve Naroff        return false;
53404084c306635b70f37029dca938444e6013f08684Steve Naroff    }
5341de5b17ea3c74d2844ed035a1edfa6479866139b5Fariborz Jahanian
5342de5b17ea3c74d2844ed035a1edfa6479866139b5Fariborz Jahanian    // Static class's protocols, or its super class or category protocols
5343de5b17ea3c74d2844ed035a1edfa6479866139b5Fariborz Jahanian    // must be found, direct or indirect in rhs's qualifier list or it is a mismatch.
5344de5b17ea3c74d2844ed035a1edfa6479866139b5Fariborz Jahanian    if (ObjCInterfaceDecl *lhsID = lhsOPT->getInterfaceDecl()) {
5345de5b17ea3c74d2844ed035a1edfa6479866139b5Fariborz Jahanian      llvm::SmallPtrSet<ObjCProtocolDecl *, 8> LHSInheritedProtocols;
5346de5b17ea3c74d2844ed035a1edfa6479866139b5Fariborz Jahanian      CollectInheritedProtocols(lhsID, LHSInheritedProtocols);
5347de5b17ea3c74d2844ed035a1edfa6479866139b5Fariborz Jahanian      // This is rather dubious but matches gcc's behavior. If lhs has
5348de5b17ea3c74d2844ed035a1edfa6479866139b5Fariborz Jahanian      // no type qualifier and its class has no static protocol(s)
5349de5b17ea3c74d2844ed035a1edfa6479866139b5Fariborz Jahanian      // assume that it is mismatch.
5350de5b17ea3c74d2844ed035a1edfa6479866139b5Fariborz Jahanian      if (LHSInheritedProtocols.empty() && lhsOPT->qual_empty())
5351de5b17ea3c74d2844ed035a1edfa6479866139b5Fariborz Jahanian        return false;
5352de5b17ea3c74d2844ed035a1edfa6479866139b5Fariborz Jahanian      for (llvm::SmallPtrSet<ObjCProtocolDecl*,8>::iterator I =
5353de5b17ea3c74d2844ed035a1edfa6479866139b5Fariborz Jahanian           LHSInheritedProtocols.begin(),
5354de5b17ea3c74d2844ed035a1edfa6479866139b5Fariborz Jahanian           E = LHSInheritedProtocols.end(); I != E; ++I) {
5355de5b17ea3c74d2844ed035a1edfa6479866139b5Fariborz Jahanian        bool match = false;
5356de5b17ea3c74d2844ed035a1edfa6479866139b5Fariborz Jahanian        ObjCProtocolDecl *lhsProto = (*I);
5357de5b17ea3c74d2844ed035a1edfa6479866139b5Fariborz Jahanian        for (ObjCObjectPointerType::qual_iterator J = rhsQID->qual_begin(),
5358de5b17ea3c74d2844ed035a1edfa6479866139b5Fariborz Jahanian             E = rhsQID->qual_end(); J != E; ++J) {
5359de5b17ea3c74d2844ed035a1edfa6479866139b5Fariborz Jahanian          ObjCProtocolDecl *rhsProto = *J;
5360de5b17ea3c74d2844ed035a1edfa6479866139b5Fariborz Jahanian          if (ProtocolCompatibleWithProtocol(lhsProto, rhsProto) ||
5361de5b17ea3c74d2844ed035a1edfa6479866139b5Fariborz Jahanian              (compare && ProtocolCompatibleWithProtocol(rhsProto, lhsProto))) {
5362de5b17ea3c74d2844ed035a1edfa6479866139b5Fariborz Jahanian            match = true;
5363de5b17ea3c74d2844ed035a1edfa6479866139b5Fariborz Jahanian            break;
5364de5b17ea3c74d2844ed035a1edfa6479866139b5Fariborz Jahanian          }
5365de5b17ea3c74d2844ed035a1edfa6479866139b5Fariborz Jahanian        }
5366de5b17ea3c74d2844ed035a1edfa6479866139b5Fariborz Jahanian        if (!match)
5367de5b17ea3c74d2844ed035a1edfa6479866139b5Fariborz Jahanian          return false;
5368de5b17ea3c74d2844ed035a1edfa6479866139b5Fariborz Jahanian      }
5369de5b17ea3c74d2844ed035a1edfa6479866139b5Fariborz Jahanian    }
53704084c306635b70f37029dca938444e6013f08684Steve Naroff    return true;
53714084c306635b70f37029dca938444e6013f08684Steve Naroff  }
53724084c306635b70f37029dca938444e6013f08684Steve Naroff  return false;
53734084c306635b70f37029dca938444e6013f08684Steve Naroff}
53744084c306635b70f37029dca938444e6013f08684Steve Naroff
53754084c306635b70f37029dca938444e6013f08684Steve Naroff/// canAssignObjCInterfaces - Return true if the two interface types are
53764084c306635b70f37029dca938444e6013f08684Steve Naroff/// compatible for assignment from RHS to LHS.  This handles validation of any
53774084c306635b70f37029dca938444e6013f08684Steve Naroff/// protocol qualifiers on the LHS or RHS.
53784084c306635b70f37029dca938444e6013f08684Steve Naroff///
53794084c306635b70f37029dca938444e6013f08684Steve Naroffbool ASTContext::canAssignObjCInterfaces(const ObjCObjectPointerType *LHSOPT,
53804084c306635b70f37029dca938444e6013f08684Steve Naroff                                         const ObjCObjectPointerType *RHSOPT) {
5381c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall  const ObjCObjectType* LHS = LHSOPT->getObjectType();
5382c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall  const ObjCObjectType* RHS = RHSOPT->getObjectType();
5383c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall
53844084c306635b70f37029dca938444e6013f08684Steve Naroff  // If either type represents the built-in 'id' or 'Class' types, return true.
5385c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall  if (LHS->isObjCUnqualifiedIdOrClass() ||
5386c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall      RHS->isObjCUnqualifiedIdOrClass())
53874084c306635b70f37029dca938444e6013f08684Steve Naroff    return true;
53884084c306635b70f37029dca938444e6013f08684Steve Naroff
5389c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall  if (LHS->isObjCQualifiedId() || RHS->isObjCQualifiedId())
53901eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    return ObjCQualifiedIdTypesAreCompatible(QualType(LHSOPT,0),
53911eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump                                             QualType(RHSOPT,0),
53924084c306635b70f37029dca938444e6013f08684Steve Naroff                                             false);
5393a8f8dac6a29f6d33474a38a32ce9dd859b696da9Fariborz Jahanian
5394a8f8dac6a29f6d33474a38a32ce9dd859b696da9Fariborz Jahanian  if (LHS->isObjCQualifiedClass() && RHS->isObjCQualifiedClass())
5395a8f8dac6a29f6d33474a38a32ce9dd859b696da9Fariborz Jahanian    return ObjCQualifiedClassTypesAreCompatible(QualType(LHSOPT,0),
5396a8f8dac6a29f6d33474a38a32ce9dd859b696da9Fariborz Jahanian                                                QualType(RHSOPT,0));
5397a8f8dac6a29f6d33474a38a32ce9dd859b696da9Fariborz Jahanian
5398c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall  // If we have 2 user-defined types, fall into that path.
5399c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall  if (LHS->getInterface() && RHS->getInterface())
54004084c306635b70f37029dca938444e6013f08684Steve Naroff    return canAssignObjCInterfaces(LHS, RHS);
54011eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
54024084c306635b70f37029dca938444e6013f08684Steve Naroff  return false;
540314108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff}
540414108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff
5405132f2a2da34f378fc675b9e174564b0f52c31d98Fariborz Jahanian/// canAssignObjCInterfacesInBlockPointer - This routine is specifically written
5406fc8f0e14ad142ed811e90fbd9a30e419e301c717Chris Lattner/// for providing type-safety for objective-c pointers used to pass/return
5407132f2a2da34f378fc675b9e174564b0f52c31d98Fariborz Jahanian/// arguments in block literals. When passed as arguments, passing 'A*' where
5408132f2a2da34f378fc675b9e174564b0f52c31d98Fariborz Jahanian/// 'id' is expected is not OK. Passing 'Sub *" where 'Super *" is expected is
5409132f2a2da34f378fc675b9e174564b0f52c31d98Fariborz Jahanian/// not OK. For the return type, the opposite is not OK.
5410132f2a2da34f378fc675b9e174564b0f52c31d98Fariborz Jahanianbool ASTContext::canAssignObjCInterfacesInBlockPointer(
5411132f2a2da34f378fc675b9e174564b0f52c31d98Fariborz Jahanian                                         const ObjCObjectPointerType *LHSOPT,
5412a4fdbfad150ae37bddaa4094d3925a27a1a1cf3fFariborz Jahanian                                         const ObjCObjectPointerType *RHSOPT,
5413a4fdbfad150ae37bddaa4094d3925a27a1a1cf3fFariborz Jahanian                                         bool BlockReturnType) {
5414a98344869c278295f38e106583906377520079b8Fariborz Jahanian  if (RHSOPT->isObjCBuiltinType() || LHSOPT->isObjCIdType())
5415132f2a2da34f378fc675b9e174564b0f52c31d98Fariborz Jahanian    return true;
5416132f2a2da34f378fc675b9e174564b0f52c31d98Fariborz Jahanian
5417132f2a2da34f378fc675b9e174564b0f52c31d98Fariborz Jahanian  if (LHSOPT->isObjCBuiltinType()) {
5418132f2a2da34f378fc675b9e174564b0f52c31d98Fariborz Jahanian    return RHSOPT->isObjCBuiltinType() || RHSOPT->isObjCQualifiedIdType();
5419132f2a2da34f378fc675b9e174564b0f52c31d98Fariborz Jahanian  }
5420132f2a2da34f378fc675b9e174564b0f52c31d98Fariborz Jahanian
5421a98344869c278295f38e106583906377520079b8Fariborz Jahanian  if (LHSOPT->isObjCQualifiedIdType() || RHSOPT->isObjCQualifiedIdType())
5422132f2a2da34f378fc675b9e174564b0f52c31d98Fariborz Jahanian    return ObjCQualifiedIdTypesAreCompatible(QualType(LHSOPT,0),
5423132f2a2da34f378fc675b9e174564b0f52c31d98Fariborz Jahanian                                             QualType(RHSOPT,0),
5424132f2a2da34f378fc675b9e174564b0f52c31d98Fariborz Jahanian                                             false);
5425132f2a2da34f378fc675b9e174564b0f52c31d98Fariborz Jahanian
5426132f2a2da34f378fc675b9e174564b0f52c31d98Fariborz Jahanian  const ObjCInterfaceType* LHS = LHSOPT->getInterfaceType();
5427132f2a2da34f378fc675b9e174564b0f52c31d98Fariborz Jahanian  const ObjCInterfaceType* RHS = RHSOPT->getInterfaceType();
5428132f2a2da34f378fc675b9e174564b0f52c31d98Fariborz Jahanian  if (LHS && RHS)  { // We have 2 user-defined types.
5429132f2a2da34f378fc675b9e174564b0f52c31d98Fariborz Jahanian    if (LHS != RHS) {
5430132f2a2da34f378fc675b9e174564b0f52c31d98Fariborz Jahanian      if (LHS->getDecl()->isSuperClassOf(RHS->getDecl()))
5431a4fdbfad150ae37bddaa4094d3925a27a1a1cf3fFariborz Jahanian        return BlockReturnType;
5432132f2a2da34f378fc675b9e174564b0f52c31d98Fariborz Jahanian      if (RHS->getDecl()->isSuperClassOf(LHS->getDecl()))
5433a4fdbfad150ae37bddaa4094d3925a27a1a1cf3fFariborz Jahanian        return !BlockReturnType;
5434132f2a2da34f378fc675b9e174564b0f52c31d98Fariborz Jahanian    }
5435132f2a2da34f378fc675b9e174564b0f52c31d98Fariborz Jahanian    else
5436132f2a2da34f378fc675b9e174564b0f52c31d98Fariborz Jahanian      return true;
5437132f2a2da34f378fc675b9e174564b0f52c31d98Fariborz Jahanian  }
5438132f2a2da34f378fc675b9e174564b0f52c31d98Fariborz Jahanian  return false;
5439132f2a2da34f378fc675b9e174564b0f52c31d98Fariborz Jahanian}
5440132f2a2da34f378fc675b9e174564b0f52c31d98Fariborz Jahanian
5441e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanian/// getIntersectionOfProtocols - This routine finds the intersection of set
5442e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanian/// of protocols inherited from two distinct objective-c pointer objects.
5443e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanian/// It is used to build composite qualifier list of the composite type of
5444e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanian/// the conditional expression involving two objective-c pointer objects.
5445e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanianstatic
5446e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanianvoid getIntersectionOfProtocols(ASTContext &Context,
5447e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanian                                const ObjCObjectPointerType *LHSOPT,
5448e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanian                                const ObjCObjectPointerType *RHSOPT,
54495f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner      SmallVectorImpl<ObjCProtocolDecl *> &IntersectionOfProtocols) {
5450e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanian
5451c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall  const ObjCObjectType* LHS = LHSOPT->getObjectType();
5452c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall  const ObjCObjectType* RHS = RHSOPT->getObjectType();
5453c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall  assert(LHS->getInterface() && "LHS must have an interface base");
5454c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall  assert(RHS->getInterface() && "RHS must have an interface base");
5455e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanian
5456e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanian  llvm::SmallPtrSet<ObjCProtocolDecl *, 8> InheritedProtocolSet;
5457e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanian  unsigned LHSNumProtocols = LHS->getNumProtocols();
5458e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanian  if (LHSNumProtocols > 0)
5459e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanian    InheritedProtocolSet.insert(LHS->qual_begin(), LHS->qual_end());
5460e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanian  else {
5461432a8893f7e30d141d7f279bd00b741a3cdac81fFariborz Jahanian    llvm::SmallPtrSet<ObjCProtocolDecl *, 8> LHSInheritedProtocols;
5462c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall    Context.CollectInheritedProtocols(LHS->getInterface(),
5463c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall                                      LHSInheritedProtocols);
5464e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanian    InheritedProtocolSet.insert(LHSInheritedProtocols.begin(),
5465e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanian                                LHSInheritedProtocols.end());
5466e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanian  }
5467e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanian
5468e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanian  unsigned RHSNumProtocols = RHS->getNumProtocols();
5469e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanian  if (RHSNumProtocols > 0) {
5470cb421fa690da545b58a720abe5f1c49b166dbde7Dan Gohman    ObjCProtocolDecl **RHSProtocols =
5471cb421fa690da545b58a720abe5f1c49b166dbde7Dan Gohman      const_cast<ObjCProtocolDecl **>(RHS->qual_begin());
5472e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanian    for (unsigned i = 0; i < RHSNumProtocols; ++i)
5473e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanian      if (InheritedProtocolSet.count(RHSProtocols[i]))
5474e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanian        IntersectionOfProtocols.push_back(RHSProtocols[i]);
54753060178ad9df29789505c1e6debcfc80a3a13587Chad Rosier  } else {
5476432a8893f7e30d141d7f279bd00b741a3cdac81fFariborz Jahanian    llvm::SmallPtrSet<ObjCProtocolDecl *, 8> RHSInheritedProtocols;
5477c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall    Context.CollectInheritedProtocols(RHS->getInterface(),
5478c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall                                      RHSInheritedProtocols);
5479432a8893f7e30d141d7f279bd00b741a3cdac81fFariborz Jahanian    for (llvm::SmallPtrSet<ObjCProtocolDecl*,8>::iterator I =
5480432a8893f7e30d141d7f279bd00b741a3cdac81fFariborz Jahanian         RHSInheritedProtocols.begin(),
5481432a8893f7e30d141d7f279bd00b741a3cdac81fFariborz Jahanian         E = RHSInheritedProtocols.end(); I != E; ++I)
5482432a8893f7e30d141d7f279bd00b741a3cdac81fFariborz Jahanian      if (InheritedProtocolSet.count((*I)))
5483432a8893f7e30d141d7f279bd00b741a3cdac81fFariborz Jahanian        IntersectionOfProtocols.push_back((*I));
5484e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanian  }
5485e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanian}
5486e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanian
5487db07b3f7cdcb505329c1280d7cf70791739a7cadFariborz Jahanian/// areCommonBaseCompatible - Returns common base class of the two classes if
5488db07b3f7cdcb505329c1280d7cf70791739a7cadFariborz Jahanian/// one found. Note that this is O'2 algorithm. But it will be called as the
5489db07b3f7cdcb505329c1280d7cf70791739a7cadFariborz Jahanian/// last type comparison in a ?-exp of ObjC pointer types before a
5490db07b3f7cdcb505329c1280d7cf70791739a7cadFariborz Jahanian/// warning is issued. So, its invokation is extremely rare.
5491db07b3f7cdcb505329c1280d7cf70791739a7cadFariborz JahanianQualType ASTContext::areCommonBaseCompatible(
5492c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall                                          const ObjCObjectPointerType *Lptr,
5493c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall                                          const ObjCObjectPointerType *Rptr) {
5494c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall  const ObjCObjectType *LHS = Lptr->getObjectType();
5495c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall  const ObjCObjectType *RHS = Rptr->getObjectType();
5496c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall  const ObjCInterfaceDecl* LDecl = LHS->getInterface();
5497c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall  const ObjCInterfaceDecl* RDecl = RHS->getInterface();
549860ef308e51c71b760d7f598c1b763ceb7b768148Douglas Gregor  if (!LDecl || !RDecl || (declaresSameEntity(LDecl, RDecl)))
5499db07b3f7cdcb505329c1280d7cf70791739a7cadFariborz Jahanian    return QualType();
5500db07b3f7cdcb505329c1280d7cf70791739a7cadFariborz Jahanian
55017c2bdcb4d30f2d370b4367664e6a11b075ce2cb3Fariborz Jahanian  do {
5502c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall    LHS = cast<ObjCInterfaceType>(getObjCInterfaceType(LDecl));
5503e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanian    if (canAssignObjCInterfaces(LHS, RHS)) {
55045f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner      SmallVector<ObjCProtocolDecl *, 8> Protocols;
5505c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall      getIntersectionOfProtocols(*this, Lptr, Rptr, Protocols);
5506c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall
5507c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall      QualType Result = QualType(LHS, 0);
5508c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall      if (!Protocols.empty())
5509c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall        Result = getObjCObjectType(Result, Protocols.data(), Protocols.size());
5510c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall      Result = getObjCObjectPointerType(Result);
5511c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall      return Result;
5512e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanian    }
55137c2bdcb4d30f2d370b4367664e6a11b075ce2cb3Fariborz Jahanian  } while ((LDecl = LDecl->getSuperClass()));
5514db07b3f7cdcb505329c1280d7cf70791739a7cadFariborz Jahanian
5515db07b3f7cdcb505329c1280d7cf70791739a7cadFariborz Jahanian  return QualType();
5516db07b3f7cdcb505329c1280d7cf70791739a7cadFariborz Jahanian}
5517db07b3f7cdcb505329c1280d7cf70791739a7cadFariborz Jahanian
5518c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCallbool ASTContext::canAssignObjCInterfaces(const ObjCObjectType *LHS,
5519c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall                                         const ObjCObjectType *RHS) {
5520c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall  assert(LHS->getInterface() && "LHS is not an interface type");
5521c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall  assert(RHS->getInterface() && "RHS is not an interface type");
5522c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall
55236ac46a4a521366d7ab36ebe2ce4e624ab96b06f9Chris Lattner  // Verify that the base decls are compatible: the RHS must be a subclass of
55246ac46a4a521366d7ab36ebe2ce4e624ab96b06f9Chris Lattner  // the LHS.
5525c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall  if (!LHS->getInterface()->isSuperClassOf(RHS->getInterface()))
55266ac46a4a521366d7ab36ebe2ce4e624ab96b06f9Chris Lattner    return false;
55271eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
55286ac46a4a521366d7ab36ebe2ce4e624ab96b06f9Chris Lattner  // RHS must have a superset of the protocols in the LHS.  If the LHS is not
55296ac46a4a521366d7ab36ebe2ce4e624ab96b06f9Chris Lattner  // protocol qualified at all, then we are good.
5530c15cb2af27514ecc879daba9aa01389c5203685dSteve Naroff  if (LHS->getNumProtocols() == 0)
55316ac46a4a521366d7ab36ebe2ce4e624ab96b06f9Chris Lattner    return true;
55321eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
5533b7bc34a83aff8af09f2a78aa6d1dcafe18ad8619Fariborz Jahanian  // Okay, we know the LHS has protocol qualifiers.  If the RHS doesn't,
5534b7bc34a83aff8af09f2a78aa6d1dcafe18ad8619Fariborz Jahanian  // more detailed analysis is required.
5535b7bc34a83aff8af09f2a78aa6d1dcafe18ad8619Fariborz Jahanian  if (RHS->getNumProtocols() == 0) {
5536b7bc34a83aff8af09f2a78aa6d1dcafe18ad8619Fariborz Jahanian    // OK, if LHS is a superclass of RHS *and*
5537b7bc34a83aff8af09f2a78aa6d1dcafe18ad8619Fariborz Jahanian    // this superclass is assignment compatible with LHS.
5538b7bc34a83aff8af09f2a78aa6d1dcafe18ad8619Fariborz Jahanian    // false otherwise.
5539627788c29976fbeb4ad47bcfcb3576889070e357Fariborz Jahanian    bool IsSuperClass =
5540627788c29976fbeb4ad47bcfcb3576889070e357Fariborz Jahanian      LHS->getInterface()->isSuperClassOf(RHS->getInterface());
5541627788c29976fbeb4ad47bcfcb3576889070e357Fariborz Jahanian    if (IsSuperClass) {
5542b7bc34a83aff8af09f2a78aa6d1dcafe18ad8619Fariborz Jahanian      // OK if conversion of LHS to SuperClass results in narrowing of types
5543b7bc34a83aff8af09f2a78aa6d1dcafe18ad8619Fariborz Jahanian      // ; i.e., SuperClass may implement at least one of the protocols
5544b7bc34a83aff8af09f2a78aa6d1dcafe18ad8619Fariborz Jahanian      // in LHS's protocol list. Example, SuperObj<P1> = lhs<P1,P2> is ok.
5545b7bc34a83aff8af09f2a78aa6d1dcafe18ad8619Fariborz Jahanian      // But not SuperObj<P1,P2,P3> = lhs<P1,P2>.
5546b7bc34a83aff8af09f2a78aa6d1dcafe18ad8619Fariborz Jahanian      llvm::SmallPtrSet<ObjCProtocolDecl *, 8> SuperClassInheritedProtocols;
5547627788c29976fbeb4ad47bcfcb3576889070e357Fariborz Jahanian      CollectInheritedProtocols(RHS->getInterface(), SuperClassInheritedProtocols);
5548b7bc34a83aff8af09f2a78aa6d1dcafe18ad8619Fariborz Jahanian      // If super class has no protocols, it is not a match.
5549b7bc34a83aff8af09f2a78aa6d1dcafe18ad8619Fariborz Jahanian      if (SuperClassInheritedProtocols.empty())
5550b7bc34a83aff8af09f2a78aa6d1dcafe18ad8619Fariborz Jahanian        return false;
5551b7bc34a83aff8af09f2a78aa6d1dcafe18ad8619Fariborz Jahanian
5552b7bc34a83aff8af09f2a78aa6d1dcafe18ad8619Fariborz Jahanian      for (ObjCObjectType::qual_iterator LHSPI = LHS->qual_begin(),
5553b7bc34a83aff8af09f2a78aa6d1dcafe18ad8619Fariborz Jahanian           LHSPE = LHS->qual_end();
5554b7bc34a83aff8af09f2a78aa6d1dcafe18ad8619Fariborz Jahanian           LHSPI != LHSPE; LHSPI++) {
5555b7bc34a83aff8af09f2a78aa6d1dcafe18ad8619Fariborz Jahanian        bool SuperImplementsProtocol = false;
5556b7bc34a83aff8af09f2a78aa6d1dcafe18ad8619Fariborz Jahanian        ObjCProtocolDecl *LHSProto = (*LHSPI);
5557b7bc34a83aff8af09f2a78aa6d1dcafe18ad8619Fariborz Jahanian
5558b7bc34a83aff8af09f2a78aa6d1dcafe18ad8619Fariborz Jahanian        for (llvm::SmallPtrSet<ObjCProtocolDecl*,8>::iterator I =
5559b7bc34a83aff8af09f2a78aa6d1dcafe18ad8619Fariborz Jahanian             SuperClassInheritedProtocols.begin(),
5560b7bc34a83aff8af09f2a78aa6d1dcafe18ad8619Fariborz Jahanian             E = SuperClassInheritedProtocols.end(); I != E; ++I) {
5561b7bc34a83aff8af09f2a78aa6d1dcafe18ad8619Fariborz Jahanian          ObjCProtocolDecl *SuperClassProto = (*I);
5562b7bc34a83aff8af09f2a78aa6d1dcafe18ad8619Fariborz Jahanian          if (SuperClassProto->lookupProtocolNamed(LHSProto->getIdentifier())) {
5563b7bc34a83aff8af09f2a78aa6d1dcafe18ad8619Fariborz Jahanian            SuperImplementsProtocol = true;
5564b7bc34a83aff8af09f2a78aa6d1dcafe18ad8619Fariborz Jahanian            break;
5565b7bc34a83aff8af09f2a78aa6d1dcafe18ad8619Fariborz Jahanian          }
5566b7bc34a83aff8af09f2a78aa6d1dcafe18ad8619Fariborz Jahanian        }
5567b7bc34a83aff8af09f2a78aa6d1dcafe18ad8619Fariborz Jahanian        if (!SuperImplementsProtocol)
5568b7bc34a83aff8af09f2a78aa6d1dcafe18ad8619Fariborz Jahanian          return false;
5569b7bc34a83aff8af09f2a78aa6d1dcafe18ad8619Fariborz Jahanian      }
5570b7bc34a83aff8af09f2a78aa6d1dcafe18ad8619Fariborz Jahanian      return true;
5571b7bc34a83aff8af09f2a78aa6d1dcafe18ad8619Fariborz Jahanian    }
5572b7bc34a83aff8af09f2a78aa6d1dcafe18ad8619Fariborz Jahanian    return false;
5573b7bc34a83aff8af09f2a78aa6d1dcafe18ad8619Fariborz Jahanian  }
55741eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
5575c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall  for (ObjCObjectType::qual_iterator LHSPI = LHS->qual_begin(),
5576c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall                                     LHSPE = LHS->qual_end();
557791b0b0cf6b537cbcbca0038c7032f87161a41d31Steve Naroff       LHSPI != LHSPE; LHSPI++) {
557891b0b0cf6b537cbcbca0038c7032f87161a41d31Steve Naroff    bool RHSImplementsProtocol = false;
557991b0b0cf6b537cbcbca0038c7032f87161a41d31Steve Naroff
558091b0b0cf6b537cbcbca0038c7032f87161a41d31Steve Naroff    // If the RHS doesn't implement the protocol on the left, the types
558191b0b0cf6b537cbcbca0038c7032f87161a41d31Steve Naroff    // are incompatible.
5582c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall    for (ObjCObjectType::qual_iterator RHSPI = RHS->qual_begin(),
5583c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall                                       RHSPE = RHS->qual_end();
55848f16756441450ed9fb39316e47d107fc2a1ef35bSteve Naroff         RHSPI != RHSPE; RHSPI++) {
55858f16756441450ed9fb39316e47d107fc2a1ef35bSteve Naroff      if ((*RHSPI)->lookupProtocolNamed((*LHSPI)->getIdentifier())) {
558691b0b0cf6b537cbcbca0038c7032f87161a41d31Steve Naroff        RHSImplementsProtocol = true;
55878f16756441450ed9fb39316e47d107fc2a1ef35bSteve Naroff        break;
55888f16756441450ed9fb39316e47d107fc2a1ef35bSteve Naroff      }
558991b0b0cf6b537cbcbca0038c7032f87161a41d31Steve Naroff    }
559091b0b0cf6b537cbcbca0038c7032f87161a41d31Steve Naroff    // FIXME: For better diagnostics, consider passing back the protocol name.
559191b0b0cf6b537cbcbca0038c7032f87161a41d31Steve Naroff    if (!RHSImplementsProtocol)
559291b0b0cf6b537cbcbca0038c7032f87161a41d31Steve Naroff      return false;
559391b0b0cf6b537cbcbca0038c7032f87161a41d31Steve Naroff  }
559491b0b0cf6b537cbcbca0038c7032f87161a41d31Steve Naroff  // The RHS implements all protocols listed on the LHS.
559591b0b0cf6b537cbcbca0038c7032f87161a41d31Steve Naroff  return true;
55966ac46a4a521366d7ab36ebe2ce4e624ab96b06f9Chris Lattner}
55976ac46a4a521366d7ab36ebe2ce4e624ab96b06f9Chris Lattner
5598389bf46ae41241a656ed71b00ac2177d7f385651Steve Naroffbool ASTContext::areComparableObjCPointerTypes(QualType LHS, QualType RHS) {
5599389bf46ae41241a656ed71b00ac2177d7f385651Steve Naroff  // get the "pointed to" types
5600183700f494ec9b6701b6efe82bcb25f4c79ba561John McCall  const ObjCObjectPointerType *LHSOPT = LHS->getAs<ObjCObjectPointerType>();
5601183700f494ec9b6701b6efe82bcb25f4c79ba561John McCall  const ObjCObjectPointerType *RHSOPT = RHS->getAs<ObjCObjectPointerType>();
56021eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
560314108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff  if (!LHSOPT || !RHSOPT)
5604389bf46ae41241a656ed71b00ac2177d7f385651Steve Naroff    return false;
560514108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff
560614108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff  return canAssignObjCInterfaces(LHSOPT, RHSOPT) ||
560714108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff         canAssignObjCInterfaces(RHSOPT, LHSOPT);
5608389bf46ae41241a656ed71b00ac2177d7f385651Steve Naroff}
5609389bf46ae41241a656ed71b00ac2177d7f385651Steve Naroff
5610569c3166874324c24011f8ade6978421f0d39b3cDouglas Gregorbool ASTContext::canBindObjCObjectType(QualType To, QualType From) {
5611569c3166874324c24011f8ade6978421f0d39b3cDouglas Gregor  return canAssignObjCInterfaces(
5612569c3166874324c24011f8ade6978421f0d39b3cDouglas Gregor                getObjCObjectPointerType(To)->getAs<ObjCObjectPointerType>(),
5613569c3166874324c24011f8ade6978421f0d39b3cDouglas Gregor                getObjCObjectPointerType(From)->getAs<ObjCObjectPointerType>());
5614569c3166874324c24011f8ade6978421f0d39b3cDouglas Gregor}
5615569c3166874324c24011f8ade6978421f0d39b3cDouglas Gregor
56161eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// typesAreCompatible - C99 6.7.3p9: For two qualified types to be compatible,
5617ec0550fa3653d46560bf4484a2e988329c228e39Steve Naroff/// both shall have the identically qualified version of a compatible type.
56181eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// C99 6.2.7p1: Two types have compatible types if their types are the
5619ec0550fa3653d46560bf4484a2e988329c228e39Steve Naroff/// same. See 6.7.[2,3,5] for additional rules.
5620447234dd459a00a5ed9b7c3e066162cd7a75bf2dDouglas Gregorbool ASTContext::typesAreCompatible(QualType LHS, QualType RHS,
5621447234dd459a00a5ed9b7c3e066162cd7a75bf2dDouglas Gregor                                    bool CompareUnqualified) {
56220e709abafbd939326850501f795cc7a92c88a354Douglas Gregor  if (getLangOptions().CPlusPlus)
56230e709abafbd939326850501f795cc7a92c88a354Douglas Gregor    return hasSameType(LHS, RHS);
56240e709abafbd939326850501f795cc7a92c88a354Douglas Gregor
5625447234dd459a00a5ed9b7c3e066162cd7a75bf2dDouglas Gregor  return !mergeTypes(LHS, RHS, false, CompareUnqualified).isNull();
56263d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman}
56273d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman
5628c286f3835eb6001c61664cef5d610dfaf80a6e9bFariborz Jahanianbool ASTContext::propertyTypesAreCompatible(QualType LHS, QualType RHS) {
562982378391dfc3b2fc22f63dbf1552bd9aed39dc42Fariborz Jahanian  return typesAreCompatible(LHS, RHS);
5630c286f3835eb6001c61664cef5d610dfaf80a6e9bFariborz Jahanian}
5631c286f3835eb6001c61664cef5d610dfaf80a6e9bFariborz Jahanian
5632132f2a2da34f378fc675b9e174564b0f52c31d98Fariborz Jahanianbool ASTContext::typesAreBlockPointerCompatible(QualType LHS, QualType RHS) {
5633132f2a2da34f378fc675b9e174564b0f52c31d98Fariborz Jahanian  return !mergeTypes(LHS, RHS, true).isNull();
5634132f2a2da34f378fc675b9e174564b0f52c31d98Fariborz Jahanian}
5635132f2a2da34f378fc675b9e174564b0f52c31d98Fariborz Jahanian
56364846675e0e42d1802b0ffd8972a45e72aeb3758dPeter Collingbourne/// mergeTransparentUnionType - if T is a transparent union type and a member
56374846675e0e42d1802b0ffd8972a45e72aeb3758dPeter Collingbourne/// of T is compatible with SubType, return the merged type, else return
56384846675e0e42d1802b0ffd8972a45e72aeb3758dPeter Collingbourne/// QualType()
56394846675e0e42d1802b0ffd8972a45e72aeb3758dPeter CollingbourneQualType ASTContext::mergeTransparentUnionType(QualType T, QualType SubType,
56404846675e0e42d1802b0ffd8972a45e72aeb3758dPeter Collingbourne                                               bool OfBlockPointer,
56414846675e0e42d1802b0ffd8972a45e72aeb3758dPeter Collingbourne                                               bool Unqualified) {
56424846675e0e42d1802b0ffd8972a45e72aeb3758dPeter Collingbourne  if (const RecordType *UT = T->getAsUnionType()) {
56434846675e0e42d1802b0ffd8972a45e72aeb3758dPeter Collingbourne    RecordDecl *UD = UT->getDecl();
56444846675e0e42d1802b0ffd8972a45e72aeb3758dPeter Collingbourne    if (UD->hasAttr<TransparentUnionAttr>()) {
56454846675e0e42d1802b0ffd8972a45e72aeb3758dPeter Collingbourne      for (RecordDecl::field_iterator it = UD->field_begin(),
56464846675e0e42d1802b0ffd8972a45e72aeb3758dPeter Collingbourne           itend = UD->field_end(); it != itend; ++it) {
5647f91d7572ee7fa6f2926c8e7e816039a1154a59f8Peter Collingbourne        QualType ET = it->getType().getUnqualifiedType();
56484846675e0e42d1802b0ffd8972a45e72aeb3758dPeter Collingbourne        QualType MT = mergeTypes(ET, SubType, OfBlockPointer, Unqualified);
56494846675e0e42d1802b0ffd8972a45e72aeb3758dPeter Collingbourne        if (!MT.isNull())
56504846675e0e42d1802b0ffd8972a45e72aeb3758dPeter Collingbourne          return MT;
56514846675e0e42d1802b0ffd8972a45e72aeb3758dPeter Collingbourne      }
56524846675e0e42d1802b0ffd8972a45e72aeb3758dPeter Collingbourne    }
56534846675e0e42d1802b0ffd8972a45e72aeb3758dPeter Collingbourne  }
56544846675e0e42d1802b0ffd8972a45e72aeb3758dPeter Collingbourne
56554846675e0e42d1802b0ffd8972a45e72aeb3758dPeter Collingbourne  return QualType();
56564846675e0e42d1802b0ffd8972a45e72aeb3758dPeter Collingbourne}
56574846675e0e42d1802b0ffd8972a45e72aeb3758dPeter Collingbourne
56584846675e0e42d1802b0ffd8972a45e72aeb3758dPeter Collingbourne/// mergeFunctionArgumentTypes - merge two types which appear as function
56594846675e0e42d1802b0ffd8972a45e72aeb3758dPeter Collingbourne/// argument types
56604846675e0e42d1802b0ffd8972a45e72aeb3758dPeter CollingbourneQualType ASTContext::mergeFunctionArgumentTypes(QualType lhs, QualType rhs,
56614846675e0e42d1802b0ffd8972a45e72aeb3758dPeter Collingbourne                                                bool OfBlockPointer,
56624846675e0e42d1802b0ffd8972a45e72aeb3758dPeter Collingbourne                                                bool Unqualified) {
56634846675e0e42d1802b0ffd8972a45e72aeb3758dPeter Collingbourne  // GNU extension: two types are compatible if they appear as a function
56644846675e0e42d1802b0ffd8972a45e72aeb3758dPeter Collingbourne  // argument, one of the types is a transparent union type and the other
56654846675e0e42d1802b0ffd8972a45e72aeb3758dPeter Collingbourne  // type is compatible with a union member
56664846675e0e42d1802b0ffd8972a45e72aeb3758dPeter Collingbourne  QualType lmerge = mergeTransparentUnionType(lhs, rhs, OfBlockPointer,
56674846675e0e42d1802b0ffd8972a45e72aeb3758dPeter Collingbourne                                              Unqualified);
56684846675e0e42d1802b0ffd8972a45e72aeb3758dPeter Collingbourne  if (!lmerge.isNull())
56694846675e0e42d1802b0ffd8972a45e72aeb3758dPeter Collingbourne    return lmerge;
56704846675e0e42d1802b0ffd8972a45e72aeb3758dPeter Collingbourne
56714846675e0e42d1802b0ffd8972a45e72aeb3758dPeter Collingbourne  QualType rmerge = mergeTransparentUnionType(rhs, lhs, OfBlockPointer,
56724846675e0e42d1802b0ffd8972a45e72aeb3758dPeter Collingbourne                                              Unqualified);
56734846675e0e42d1802b0ffd8972a45e72aeb3758dPeter Collingbourne  if (!rmerge.isNull())
56744846675e0e42d1802b0ffd8972a45e72aeb3758dPeter Collingbourne    return rmerge;
56754846675e0e42d1802b0ffd8972a45e72aeb3758dPeter Collingbourne
56764846675e0e42d1802b0ffd8972a45e72aeb3758dPeter Collingbourne  return mergeTypes(lhs, rhs, OfBlockPointer, Unqualified);
56774846675e0e42d1802b0ffd8972a45e72aeb3758dPeter Collingbourne}
56784846675e0e42d1802b0ffd8972a45e72aeb3758dPeter Collingbourne
5679132f2a2da34f378fc675b9e174564b0f52c31d98Fariborz JahanianQualType ASTContext::mergeFunctionTypes(QualType lhs, QualType rhs,
5680447234dd459a00a5ed9b7c3e066162cd7a75bf2dDouglas Gregor                                        bool OfBlockPointer,
5681447234dd459a00a5ed9b7c3e066162cd7a75bf2dDouglas Gregor                                        bool Unqualified) {
5682183700f494ec9b6701b6efe82bcb25f4c79ba561John McCall  const FunctionType *lbase = lhs->getAs<FunctionType>();
5683183700f494ec9b6701b6efe82bcb25f4c79ba561John McCall  const FunctionType *rbase = rhs->getAs<FunctionType>();
568472564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor  const FunctionProtoType *lproto = dyn_cast<FunctionProtoType>(lbase);
568572564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor  const FunctionProtoType *rproto = dyn_cast<FunctionProtoType>(rbase);
56863d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman  bool allLTypes = true;
56873d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman  bool allRTypes = true;
56883d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman
56893d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman  // Check return type
5690132f2a2da34f378fc675b9e174564b0f52c31d98Fariborz Jahanian  QualType retType;
5691d263fd1451299b1e5f5f1acb2bb13b0a4119aee8Fariborz Jahanian  if (OfBlockPointer) {
5692d263fd1451299b1e5f5f1acb2bb13b0a4119aee8Fariborz Jahanian    QualType RHS = rbase->getResultType();
5693d263fd1451299b1e5f5f1acb2bb13b0a4119aee8Fariborz Jahanian    QualType LHS = lbase->getResultType();
5694d263fd1451299b1e5f5f1acb2bb13b0a4119aee8Fariborz Jahanian    bool UnqualifiedResult = Unqualified;
5695d263fd1451299b1e5f5f1acb2bb13b0a4119aee8Fariborz Jahanian    if (!UnqualifiedResult)
5696d263fd1451299b1e5f5f1acb2bb13b0a4119aee8Fariborz Jahanian      UnqualifiedResult = (!RHS.hasQualifiers() && LHS.hasQualifiers());
5697a4fdbfad150ae37bddaa4094d3925a27a1a1cf3fFariborz Jahanian    retType = mergeTypes(LHS, RHS, true, UnqualifiedResult, true);
5698d263fd1451299b1e5f5f1acb2bb13b0a4119aee8Fariborz Jahanian  }
5699132f2a2da34f378fc675b9e174564b0f52c31d98Fariborz Jahanian  else
57008cc246c9a68c783a5b90d2e8b8927521cb3a49b7John McCall    retType = mergeTypes(lbase->getResultType(), rbase->getResultType(), false,
57018cc246c9a68c783a5b90d2e8b8927521cb3a49b7John McCall                         Unqualified);
57023d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman  if (retType.isNull()) return QualType();
5703447234dd459a00a5ed9b7c3e066162cd7a75bf2dDouglas Gregor
5704447234dd459a00a5ed9b7c3e066162cd7a75bf2dDouglas Gregor  if (Unqualified)
5705447234dd459a00a5ed9b7c3e066162cd7a75bf2dDouglas Gregor    retType = retType.getUnqualifiedType();
5706447234dd459a00a5ed9b7c3e066162cd7a75bf2dDouglas Gregor
5707447234dd459a00a5ed9b7c3e066162cd7a75bf2dDouglas Gregor  CanQualType LRetType = getCanonicalType(lbase->getResultType());
5708447234dd459a00a5ed9b7c3e066162cd7a75bf2dDouglas Gregor  CanQualType RRetType = getCanonicalType(rbase->getResultType());
5709447234dd459a00a5ed9b7c3e066162cd7a75bf2dDouglas Gregor  if (Unqualified) {
5710447234dd459a00a5ed9b7c3e066162cd7a75bf2dDouglas Gregor    LRetType = LRetType.getUnqualifiedType();
5711447234dd459a00a5ed9b7c3e066162cd7a75bf2dDouglas Gregor    RRetType = RRetType.getUnqualifiedType();
5712447234dd459a00a5ed9b7c3e066162cd7a75bf2dDouglas Gregor  }
5713447234dd459a00a5ed9b7c3e066162cd7a75bf2dDouglas Gregor
5714447234dd459a00a5ed9b7c3e066162cd7a75bf2dDouglas Gregor  if (getCanonicalType(retType) != LRetType)
571561710854be2b098428aff5316e64bd34b30fbcb7Chris Lattner    allLTypes = false;
5716447234dd459a00a5ed9b7c3e066162cd7a75bf2dDouglas Gregor  if (getCanonicalType(retType) != RRetType)
571761710854be2b098428aff5316e64bd34b30fbcb7Chris Lattner    allRTypes = false;
57188cc246c9a68c783a5b90d2e8b8927521cb3a49b7John McCall
57196a15c8586cc3e5d6e01c4c4e8b4374584569b58fDaniel Dunbar  // FIXME: double check this
57206a15c8586cc3e5d6e01c4c4e8b4374584569b58fDaniel Dunbar  // FIXME: should we error if lbase->getRegParmAttr() != 0 &&
57216a15c8586cc3e5d6e01c4c4e8b4374584569b58fDaniel Dunbar  //                           rbase->getRegParmAttr() != 0 &&
57226a15c8586cc3e5d6e01c4c4e8b4374584569b58fDaniel Dunbar  //                           lbase->getRegParmAttr() != rbase->getRegParmAttr()?
5723264ba48dc98f3f843935a485d5b086f7e0fdc4f1Rafael Espindola  FunctionType::ExtInfo lbaseInfo = lbase->getExtInfo();
5724264ba48dc98f3f843935a485d5b086f7e0fdc4f1Rafael Espindola  FunctionType::ExtInfo rbaseInfo = rbase->getExtInfo();
57258cc246c9a68c783a5b90d2e8b8927521cb3a49b7John McCall
57268cc246c9a68c783a5b90d2e8b8927521cb3a49b7John McCall  // Compatible functions must have compatible calling conventions
57278cc246c9a68c783a5b90d2e8b8927521cb3a49b7John McCall  if (!isSameCallConv(lbaseInfo.getCC(), rbaseInfo.getCC()))
57288cc246c9a68c783a5b90d2e8b8927521cb3a49b7John McCall    return QualType();
57298cc246c9a68c783a5b90d2e8b8927521cb3a49b7John McCall
57308cc246c9a68c783a5b90d2e8b8927521cb3a49b7John McCall  // Regparm is part of the calling convention.
5731a49218e17bcbb1acde0245773173e2c0c42f4f19Eli Friedman  if (lbaseInfo.getHasRegParm() != rbaseInfo.getHasRegParm())
5732a49218e17bcbb1acde0245773173e2c0c42f4f19Eli Friedman    return QualType();
57338cc246c9a68c783a5b90d2e8b8927521cb3a49b7John McCall  if (lbaseInfo.getRegParm() != rbaseInfo.getRegParm())
57348cc246c9a68c783a5b90d2e8b8927521cb3a49b7John McCall    return QualType();
57358cc246c9a68c783a5b90d2e8b8927521cb3a49b7John McCall
5736f85e193739c953358c865005855253af4f68a497John McCall  if (lbaseInfo.getProducesResult() != rbaseInfo.getProducesResult())
5737f85e193739c953358c865005855253af4f68a497John McCall    return QualType();
5738f85e193739c953358c865005855253af4f68a497John McCall
573953c8167d7a007daae87f342c0fedd03f1dcf1b62Fariborz Jahanian  // functypes which return are preferred over those that do not.
574053c8167d7a007daae87f342c0fedd03f1dcf1b62Fariborz Jahanian  if (lbaseInfo.getNoReturn() && !rbaseInfo.getNoReturn())
57416a15c8586cc3e5d6e01c4c4e8b4374584569b58fDaniel Dunbar    allLTypes = false;
574253c8167d7a007daae87f342c0fedd03f1dcf1b62Fariborz Jahanian  else if (!lbaseInfo.getNoReturn() && rbaseInfo.getNoReturn())
57436a15c8586cc3e5d6e01c4c4e8b4374584569b58fDaniel Dunbar    allRTypes = false;
574453c8167d7a007daae87f342c0fedd03f1dcf1b62Fariborz Jahanian  // FIXME: some uses, e.g. conditional exprs, really want this to be 'both'.
574553c8167d7a007daae87f342c0fedd03f1dcf1b62Fariborz Jahanian  bool NoReturn = lbaseInfo.getNoReturn() || rbaseInfo.getNoReturn();
57461eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
5747f85e193739c953358c865005855253af4f68a497John McCall  FunctionType::ExtInfo einfo = lbaseInfo.withNoReturn(NoReturn);
5748e23cf437fe76b1ed02d63c3f61b456fd48a915f5John McCall
57493d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman  if (lproto && rproto) { // two C99 style function prototypes
5750465226e23a3008bd68973513dda1f9e3cd27dbddSebastian Redl    assert(!lproto->hasExceptionSpec() && !rproto->hasExceptionSpec() &&
5751465226e23a3008bd68973513dda1f9e3cd27dbddSebastian Redl           "C++ shouldn't be here");
57523d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    unsigned lproto_nargs = lproto->getNumArgs();
57533d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    unsigned rproto_nargs = rproto->getNumArgs();
57543d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman
57553d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    // Compatible functions must have the same number of arguments
57563d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    if (lproto_nargs != rproto_nargs)
57573d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman      return QualType();
57583d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman
57593d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    // Variadic and non-variadic functions aren't compatible
57603d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    if (lproto->isVariadic() != rproto->isVariadic())
57613d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman      return QualType();
57623d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman
57637fb5e4888221cd36652d078c6b171ac55e7f406dArgyrios Kyrtzidis    if (lproto->getTypeQuals() != rproto->getTypeQuals())
57647fb5e4888221cd36652d078c6b171ac55e7f406dArgyrios Kyrtzidis      return QualType();
57657fb5e4888221cd36652d078c6b171ac55e7f406dArgyrios Kyrtzidis
576678213e4aec6d8d22b5828dd2687fa19116ebaa26Fariborz Jahanian    if (LangOpts.ObjCAutoRefCount &&
576778213e4aec6d8d22b5828dd2687fa19116ebaa26Fariborz Jahanian        !FunctionTypesMatchOnNSConsumedAttrs(rproto, lproto))
576878213e4aec6d8d22b5828dd2687fa19116ebaa26Fariborz Jahanian      return QualType();
576978213e4aec6d8d22b5828dd2687fa19116ebaa26Fariborz Jahanian
57703d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    // Check argument compatibility
57715f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner    SmallVector<QualType, 10> types;
57723d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    for (unsigned i = 0; i < lproto_nargs; i++) {
57733d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman      QualType largtype = lproto->getArgType(i).getUnqualifiedType();
57743d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman      QualType rargtype = rproto->getArgType(i).getUnqualifiedType();
57754846675e0e42d1802b0ffd8972a45e72aeb3758dPeter Collingbourne      QualType argtype = mergeFunctionArgumentTypes(largtype, rargtype,
57764846675e0e42d1802b0ffd8972a45e72aeb3758dPeter Collingbourne                                                    OfBlockPointer,
57774846675e0e42d1802b0ffd8972a45e72aeb3758dPeter Collingbourne                                                    Unqualified);
57783d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman      if (argtype.isNull()) return QualType();
5779447234dd459a00a5ed9b7c3e066162cd7a75bf2dDouglas Gregor
5780447234dd459a00a5ed9b7c3e066162cd7a75bf2dDouglas Gregor      if (Unqualified)
5781447234dd459a00a5ed9b7c3e066162cd7a75bf2dDouglas Gregor        argtype = argtype.getUnqualifiedType();
5782447234dd459a00a5ed9b7c3e066162cd7a75bf2dDouglas Gregor
57833d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman      types.push_back(argtype);
5784447234dd459a00a5ed9b7c3e066162cd7a75bf2dDouglas Gregor      if (Unqualified) {
5785447234dd459a00a5ed9b7c3e066162cd7a75bf2dDouglas Gregor        largtype = largtype.getUnqualifiedType();
5786447234dd459a00a5ed9b7c3e066162cd7a75bf2dDouglas Gregor        rargtype = rargtype.getUnqualifiedType();
5787447234dd459a00a5ed9b7c3e066162cd7a75bf2dDouglas Gregor      }
5788447234dd459a00a5ed9b7c3e066162cd7a75bf2dDouglas Gregor
578961710854be2b098428aff5316e64bd34b30fbcb7Chris Lattner      if (getCanonicalType(argtype) != getCanonicalType(largtype))
579061710854be2b098428aff5316e64bd34b30fbcb7Chris Lattner        allLTypes = false;
579161710854be2b098428aff5316e64bd34b30fbcb7Chris Lattner      if (getCanonicalType(argtype) != getCanonicalType(rargtype))
579261710854be2b098428aff5316e64bd34b30fbcb7Chris Lattner        allRTypes = false;
57933d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    }
579478213e4aec6d8d22b5828dd2687fa19116ebaa26Fariborz Jahanian
57953d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    if (allLTypes) return lhs;
57963d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    if (allRTypes) return rhs;
5797e23cf437fe76b1ed02d63c3f61b456fd48a915f5John McCall
5798e23cf437fe76b1ed02d63c3f61b456fd48a915f5John McCall    FunctionProtoType::ExtProtoInfo EPI = lproto->getExtProtoInfo();
5799e23cf437fe76b1ed02d63c3f61b456fd48a915f5John McCall    EPI.ExtInfo = einfo;
5800e23cf437fe76b1ed02d63c3f61b456fd48a915f5John McCall    return getFunctionType(retType, types.begin(), types.size(), EPI);
58013d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman  }
58023d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman
58033d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman  if (lproto) allRTypes = false;
58043d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman  if (rproto) allLTypes = false;
58053d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman
580672564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor  const FunctionProtoType *proto = lproto ? lproto : rproto;
58073d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman  if (proto) {
5808465226e23a3008bd68973513dda1f9e3cd27dbddSebastian Redl    assert(!proto->hasExceptionSpec() && "C++ shouldn't be here");
58093d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    if (proto->isVariadic()) return QualType();
58103d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    // Check that the types are compatible with the types that
58113d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    // would result from default argument promotions (C99 6.7.5.3p15).
58123d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    // The only types actually affected are promotable integer
58133d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    // types and floats, which would be passed as a different
58143d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    // type depending on whether the prototype is visible.
58153d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    unsigned proto_nargs = proto->getNumArgs();
58163d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    for (unsigned i = 0; i < proto_nargs; ++i) {
58173d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman      QualType argTy = proto->getArgType(i);
5818b0f8eacfdcd0d43f51e669f2d723992d4af9f746Douglas Gregor
5819b0f8eacfdcd0d43f51e669f2d723992d4af9f746Douglas Gregor      // Look at the promotion type of enum types, since that is the type used
5820b0f8eacfdcd0d43f51e669f2d723992d4af9f746Douglas Gregor      // to pass enum values.
5821b0f8eacfdcd0d43f51e669f2d723992d4af9f746Douglas Gregor      if (const EnumType *Enum = argTy->getAs<EnumType>())
5822b0f8eacfdcd0d43f51e669f2d723992d4af9f746Douglas Gregor        argTy = Enum->getDecl()->getPromotionType();
5823b0f8eacfdcd0d43f51e669f2d723992d4af9f746Douglas Gregor
58243d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman      if (argTy->isPromotableIntegerType() ||
58253d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman          getCanonicalType(argTy).getUnqualifiedType() == FloatTy)
58263d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman        return QualType();
58273d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    }
58283d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman
58293d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    if (allLTypes) return lhs;
58303d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    if (allRTypes) return rhs;
5831e23cf437fe76b1ed02d63c3f61b456fd48a915f5John McCall
5832e23cf437fe76b1ed02d63c3f61b456fd48a915f5John McCall    FunctionProtoType::ExtProtoInfo EPI = proto->getExtProtoInfo();
5833e23cf437fe76b1ed02d63c3f61b456fd48a915f5John McCall    EPI.ExtInfo = einfo;
58343d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    return getFunctionType(retType, proto->arg_type_begin(),
5835e23cf437fe76b1ed02d63c3f61b456fd48a915f5John McCall                           proto->getNumArgs(), EPI);
58363d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman  }
58373d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman
58383d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman  if (allLTypes) return lhs;
58393d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman  if (allRTypes) return rhs;
58408cc246c9a68c783a5b90d2e8b8927521cb3a49b7John McCall  return getFunctionNoProtoType(retType, einfo);
58413d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman}
58423d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman
5843132f2a2da34f378fc675b9e174564b0f52c31d98Fariborz JahanianQualType ASTContext::mergeTypes(QualType LHS, QualType RHS,
5844447234dd459a00a5ed9b7c3e066162cd7a75bf2dDouglas Gregor                                bool OfBlockPointer,
5845a4fdbfad150ae37bddaa4094d3925a27a1a1cf3fFariborz Jahanian                                bool Unqualified, bool BlockReturnType) {
584643d69750e7f7b26076e7474dec8839bb777b260fBill Wendling  // C++ [expr]: If an expression initially has the type "reference to T", the
584743d69750e7f7b26076e7474dec8839bb777b260fBill Wendling  // type is adjusted to "T" prior to any further analysis, the expression
584843d69750e7f7b26076e7474dec8839bb777b260fBill Wendling  // designates the object or function denoted by the reference, and the
58497c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl  // expression is an lvalue unless the reference is an rvalue reference and
58507c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl  // the expression is a function call (possibly inside parentheses).
58510e709abafbd939326850501f795cc7a92c88a354Douglas Gregor  assert(!LHS->getAs<ReferenceType>() && "LHS is a reference type?");
58520e709abafbd939326850501f795cc7a92c88a354Douglas Gregor  assert(!RHS->getAs<ReferenceType>() && "RHS is a reference type?");
5853447234dd459a00a5ed9b7c3e066162cd7a75bf2dDouglas Gregor
5854447234dd459a00a5ed9b7c3e066162cd7a75bf2dDouglas Gregor  if (Unqualified) {
5855447234dd459a00a5ed9b7c3e066162cd7a75bf2dDouglas Gregor    LHS = LHS.getUnqualifiedType();
5856447234dd459a00a5ed9b7c3e066162cd7a75bf2dDouglas Gregor    RHS = RHS.getUnqualifiedType();
5857447234dd459a00a5ed9b7c3e066162cd7a75bf2dDouglas Gregor  }
58580e709abafbd939326850501f795cc7a92c88a354Douglas Gregor
58593d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman  QualType LHSCan = getCanonicalType(LHS),
58603d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman           RHSCan = getCanonicalType(RHS);
58613d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman
5862f3692dc4a47dc48d10cec0415c6e9e39b7a39707Chris Lattner  // If two types are identical, they are compatible.
58633d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman  if (LHSCan == RHSCan)
58643d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    return LHS;
58653d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman
58660953e767ff7817f97b3ab20896b229891eeff45bJohn McCall  // If the qualifiers are different, the types aren't compatible... mostly.
5867a4923eb7c4b04d360cb2747641a5e92818edf804Douglas Gregor  Qualifiers LQuals = LHSCan.getLocalQualifiers();
5868a4923eb7c4b04d360cb2747641a5e92818edf804Douglas Gregor  Qualifiers RQuals = RHSCan.getLocalQualifiers();
58690953e767ff7817f97b3ab20896b229891eeff45bJohn McCall  if (LQuals != RQuals) {
58700953e767ff7817f97b3ab20896b229891eeff45bJohn McCall    // If any of these qualifiers are different, we have a type
58710953e767ff7817f97b3ab20896b229891eeff45bJohn McCall    // mismatch.
58720953e767ff7817f97b3ab20896b229891eeff45bJohn McCall    if (LQuals.getCVRQualifiers() != RQuals.getCVRQualifiers() ||
5873f85e193739c953358c865005855253af4f68a497John McCall        LQuals.getAddressSpace() != RQuals.getAddressSpace() ||
5874f85e193739c953358c865005855253af4f68a497John McCall        LQuals.getObjCLifetime() != RQuals.getObjCLifetime())
58750953e767ff7817f97b3ab20896b229891eeff45bJohn McCall      return QualType();
58760953e767ff7817f97b3ab20896b229891eeff45bJohn McCall
58770953e767ff7817f97b3ab20896b229891eeff45bJohn McCall    // Exactly one GC qualifier difference is allowed: __strong is
58780953e767ff7817f97b3ab20896b229891eeff45bJohn McCall    // okay if the other type has no GC qualifier but is an Objective
58790953e767ff7817f97b3ab20896b229891eeff45bJohn McCall    // C object pointer (i.e. implicitly strong by default).  We fix
58800953e767ff7817f97b3ab20896b229891eeff45bJohn McCall    // this by pretending that the unqualified type was actually
58810953e767ff7817f97b3ab20896b229891eeff45bJohn McCall    // qualified __strong.
58820953e767ff7817f97b3ab20896b229891eeff45bJohn McCall    Qualifiers::GC GC_L = LQuals.getObjCGCAttr();
58830953e767ff7817f97b3ab20896b229891eeff45bJohn McCall    Qualifiers::GC GC_R = RQuals.getObjCGCAttr();
58840953e767ff7817f97b3ab20896b229891eeff45bJohn McCall    assert((GC_L != GC_R) && "unequal qualifier sets had only equal elements");
58850953e767ff7817f97b3ab20896b229891eeff45bJohn McCall
58860953e767ff7817f97b3ab20896b229891eeff45bJohn McCall    if (GC_L == Qualifiers::Weak || GC_R == Qualifiers::Weak)
58870953e767ff7817f97b3ab20896b229891eeff45bJohn McCall      return QualType();
58880953e767ff7817f97b3ab20896b229891eeff45bJohn McCall
58890953e767ff7817f97b3ab20896b229891eeff45bJohn McCall    if (GC_L == Qualifiers::Strong && RHSCan->isObjCObjectPointerType()) {
58900953e767ff7817f97b3ab20896b229891eeff45bJohn McCall      return mergeTypes(LHS, getObjCGCQualType(RHS, Qualifiers::Strong));
58910953e767ff7817f97b3ab20896b229891eeff45bJohn McCall    }
58920953e767ff7817f97b3ab20896b229891eeff45bJohn McCall    if (GC_R == Qualifiers::Strong && LHSCan->isObjCObjectPointerType()) {
58930953e767ff7817f97b3ab20896b229891eeff45bJohn McCall      return mergeTypes(getObjCGCQualType(LHS, Qualifiers::Strong), RHS);
58940953e767ff7817f97b3ab20896b229891eeff45bJohn McCall    }
58953d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    return QualType();
58960953e767ff7817f97b3ab20896b229891eeff45bJohn McCall  }
58970953e767ff7817f97b3ab20896b229891eeff45bJohn McCall
58980953e767ff7817f97b3ab20896b229891eeff45bJohn McCall  // Okay, qualifiers are equal.
58993d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman
5900852d63b806c5cbd730c6b9d696e2e27d02546b49Eli Friedman  Type::TypeClass LHSClass = LHSCan->getTypeClass();
5901852d63b806c5cbd730c6b9d696e2e27d02546b49Eli Friedman  Type::TypeClass RHSClass = RHSCan->getTypeClass();
5902f3692dc4a47dc48d10cec0415c6e9e39b7a39707Chris Lattner
59031adb88370beab45af2f065afe86b51ccd59ec50dChris Lattner  // We want to consider the two function types to be the same for these
59041adb88370beab45af2f065afe86b51ccd59ec50dChris Lattner  // comparisons, just force one to the other.
59051adb88370beab45af2f065afe86b51ccd59ec50dChris Lattner  if (LHSClass == Type::FunctionProto) LHSClass = Type::FunctionNoProto;
59061adb88370beab45af2f065afe86b51ccd59ec50dChris Lattner  if (RHSClass == Type::FunctionProto) RHSClass = Type::FunctionNoProto;
59074c721d381fb279899337d120edd4a24d405e56b2Eli Friedman
59084c721d381fb279899337d120edd4a24d405e56b2Eli Friedman  // Same as above for arrays
5909a36a61f218b9f7a97f2c0f511e0b29eb42e8f78bChris Lattner  if (LHSClass == Type::VariableArray || LHSClass == Type::IncompleteArray)
5910a36a61f218b9f7a97f2c0f511e0b29eb42e8f78bChris Lattner    LHSClass = Type::ConstantArray;
5911a36a61f218b9f7a97f2c0f511e0b29eb42e8f78bChris Lattner  if (RHSClass == Type::VariableArray || RHSClass == Type::IncompleteArray)
5912a36a61f218b9f7a97f2c0f511e0b29eb42e8f78bChris Lattner    RHSClass = Type::ConstantArray;
59131eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
5914c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall  // ObjCInterfaces are just specialized ObjCObjects.
5915c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall  if (LHSClass == Type::ObjCInterface) LHSClass = Type::ObjCObject;
5916c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall  if (RHSClass == Type::ObjCInterface) RHSClass = Type::ObjCObject;
5917c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall
5918213541a68a3e137d11d2cefb612c6cdb410d7e8eNate Begeman  // Canonicalize ExtVector -> Vector.
5919213541a68a3e137d11d2cefb612c6cdb410d7e8eNate Begeman  if (LHSClass == Type::ExtVector) LHSClass = Type::Vector;
5920213541a68a3e137d11d2cefb612c6cdb410d7e8eNate Begeman  if (RHSClass == Type::ExtVector) RHSClass = Type::Vector;
59211eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
59224e78fd0a960eaa7e97467f2e8f390f3a57da279bSteve Naroff  // If the canonical type classes don't match.
59234e78fd0a960eaa7e97467f2e8f390f3a57da279bSteve Naroff  if (LHSClass != RHSClass) {
59241adb88370beab45af2f065afe86b51ccd59ec50dChris Lattner    // C99 6.7.2.2p4: Each enumerated type shall be compatible with char,
59251eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    // a signed integer type, or an unsigned integer type.
5926842aef8d942a880eeb9535d40de31a86838264cbJohn McCall    // Compatibility is based on the underlying type, not the promotion
5927842aef8d942a880eeb9535d40de31a86838264cbJohn McCall    // type.
5928183700f494ec9b6701b6efe82bcb25f4c79ba561John McCall    if (const EnumType* ETy = LHS->getAs<EnumType>()) {
59293d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman      if (ETy->getDecl()->getIntegerType() == RHSCan.getUnqualifiedType())
59303d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman        return RHS;
5931bab96968886f4b77083f4e26a28986ddb1e42d67Eli Friedman    }
5932183700f494ec9b6701b6efe82bcb25f4c79ba561John McCall    if (const EnumType* ETy = RHS->getAs<EnumType>()) {
59333d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman      if (ETy->getDecl()->getIntegerType() == LHSCan.getUnqualifiedType())
59343d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman        return LHS;
5935bab96968886f4b77083f4e26a28986ddb1e42d67Eli Friedman    }
5936e7cff2c00c3d74e38984a4db5c48d7e303d3ab8eFariborz Jahanian    // allow block pointer type to match an 'id' type.
5937e7cff2c00c3d74e38984a4db5c48d7e303d3ab8eFariborz Jahanian    if (OfBlockPointer && !BlockReturnType &&
5938e7cff2c00c3d74e38984a4db5c48d7e303d3ab8eFariborz Jahanian        LHS->isObjCIdType() && RHS->isBlockPointerType())
5939e7cff2c00c3d74e38984a4db5c48d7e303d3ab8eFariborz Jahanian      return LHS;
5940e7cff2c00c3d74e38984a4db5c48d7e303d3ab8eFariborz Jahanian
59413d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    return QualType();
5942ec0550fa3653d46560bf4484a2e988329c228e39Steve Naroff  }
59433d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman
59444a74678ed6c3dedac05d02b1ee341f1db869f049Steve Naroff  // The canonical type classes match.
59451adb88370beab45af2f065afe86b51ccd59ec50dChris Lattner  switch (LHSClass) {
594672564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor#define TYPE(Class, Base)
594772564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor#define ABSTRACT_TYPE(Class, Base)
5948ad5e73887052193afda72db8efcb812bd083a4a8John McCall#define NON_CANONICAL_UNLESS_DEPENDENT_TYPE(Class, Base) case Type::Class:
594972564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor#define NON_CANONICAL_TYPE(Class, Base) case Type::Class:
595072564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor#define DEPENDENT_TYPE(Class, Base) case Type::Class:
595172564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor#include "clang/AST/TypeNodes.def"
5952b219cfc4d75f0a03630b7c4509ef791b7e97b2c8David Blaikie    llvm_unreachable("Non-canonical and dependent types shouldn't get here");
595372564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor
59547c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl  case Type::LValueReference:
59557c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl  case Type::RValueReference:
595672564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor  case Type::MemberPointer:
5957b219cfc4d75f0a03630b7c4509ef791b7e97b2c8David Blaikie    llvm_unreachable("C++ should never be in mergeTypes");
595872564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor
5959c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall  case Type::ObjCInterface:
596072564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor  case Type::IncompleteArray:
596172564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor  case Type::VariableArray:
596272564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor  case Type::FunctionProto:
596372564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor  case Type::ExtVector:
5964b219cfc4d75f0a03630b7c4509ef791b7e97b2c8David Blaikie    llvm_unreachable("Types are eliminated above");
596572564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor
59661adb88370beab45af2f065afe86b51ccd59ec50dChris Lattner  case Type::Pointer:
59673d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman  {
59683d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    // Merge two pointer types, while trying to preserve typedef info
59696217b80b7a1379b74cced1c076338262c3c980b3Ted Kremenek    QualType LHSPointee = LHS->getAs<PointerType>()->getPointeeType();
59706217b80b7a1379b74cced1c076338262c3c980b3Ted Kremenek    QualType RHSPointee = RHS->getAs<PointerType>()->getPointeeType();
5971447234dd459a00a5ed9b7c3e066162cd7a75bf2dDouglas Gregor    if (Unqualified) {
5972447234dd459a00a5ed9b7c3e066162cd7a75bf2dDouglas Gregor      LHSPointee = LHSPointee.getUnqualifiedType();
5973447234dd459a00a5ed9b7c3e066162cd7a75bf2dDouglas Gregor      RHSPointee = RHSPointee.getUnqualifiedType();
5974447234dd459a00a5ed9b7c3e066162cd7a75bf2dDouglas Gregor    }
5975447234dd459a00a5ed9b7c3e066162cd7a75bf2dDouglas Gregor    QualType ResultType = mergeTypes(LHSPointee, RHSPointee, false,
5976447234dd459a00a5ed9b7c3e066162cd7a75bf2dDouglas Gregor                                     Unqualified);
59773d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    if (ResultType.isNull()) return QualType();
597807d258756dc856c6987c394a0972884e6ed46765Eli Friedman    if (getCanonicalType(LHSPointee) == getCanonicalType(ResultType))
597961710854be2b098428aff5316e64bd34b30fbcb7Chris Lattner      return LHS;
598007d258756dc856c6987c394a0972884e6ed46765Eli Friedman    if (getCanonicalType(RHSPointee) == getCanonicalType(ResultType))
598161710854be2b098428aff5316e64bd34b30fbcb7Chris Lattner      return RHS;
59823d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    return getPointerType(ResultType);
59833d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman  }
5984c0febd58f5cbf4a93fd12f461863564dba0af76dSteve Naroff  case Type::BlockPointer:
5985c0febd58f5cbf4a93fd12f461863564dba0af76dSteve Naroff  {
5986c0febd58f5cbf4a93fd12f461863564dba0af76dSteve Naroff    // Merge two block pointer types, while trying to preserve typedef info
59876217b80b7a1379b74cced1c076338262c3c980b3Ted Kremenek    QualType LHSPointee = LHS->getAs<BlockPointerType>()->getPointeeType();
59886217b80b7a1379b74cced1c076338262c3c980b3Ted Kremenek    QualType RHSPointee = RHS->getAs<BlockPointerType>()->getPointeeType();
5989447234dd459a00a5ed9b7c3e066162cd7a75bf2dDouglas Gregor    if (Unqualified) {
5990447234dd459a00a5ed9b7c3e066162cd7a75bf2dDouglas Gregor      LHSPointee = LHSPointee.getUnqualifiedType();
5991447234dd459a00a5ed9b7c3e066162cd7a75bf2dDouglas Gregor      RHSPointee = RHSPointee.getUnqualifiedType();
5992447234dd459a00a5ed9b7c3e066162cd7a75bf2dDouglas Gregor    }
5993447234dd459a00a5ed9b7c3e066162cd7a75bf2dDouglas Gregor    QualType ResultType = mergeTypes(LHSPointee, RHSPointee, OfBlockPointer,
5994447234dd459a00a5ed9b7c3e066162cd7a75bf2dDouglas Gregor                                     Unqualified);
5995c0febd58f5cbf4a93fd12f461863564dba0af76dSteve Naroff    if (ResultType.isNull()) return QualType();
5996c0febd58f5cbf4a93fd12f461863564dba0af76dSteve Naroff    if (getCanonicalType(LHSPointee) == getCanonicalType(ResultType))
5997c0febd58f5cbf4a93fd12f461863564dba0af76dSteve Naroff      return LHS;
5998c0febd58f5cbf4a93fd12f461863564dba0af76dSteve Naroff    if (getCanonicalType(RHSPointee) == getCanonicalType(ResultType))
5999c0febd58f5cbf4a93fd12f461863564dba0af76dSteve Naroff      return RHS;
6000c0febd58f5cbf4a93fd12f461863564dba0af76dSteve Naroff    return getBlockPointerType(ResultType);
6001c0febd58f5cbf4a93fd12f461863564dba0af76dSteve Naroff  }
6002b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman  case Type::Atomic:
6003b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman  {
6004b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman    // Merge two pointer types, while trying to preserve typedef info
6005b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman    QualType LHSValue = LHS->getAs<AtomicType>()->getValueType();
6006b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman    QualType RHSValue = RHS->getAs<AtomicType>()->getValueType();
6007b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman    if (Unqualified) {
6008b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman      LHSValue = LHSValue.getUnqualifiedType();
6009b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman      RHSValue = RHSValue.getUnqualifiedType();
6010b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman    }
6011b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman    QualType ResultType = mergeTypes(LHSValue, RHSValue, false,
6012b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman                                     Unqualified);
6013b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman    if (ResultType.isNull()) return QualType();
6014b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman    if (getCanonicalType(LHSValue) == getCanonicalType(ResultType))
6015b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman      return LHS;
6016b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman    if (getCanonicalType(RHSValue) == getCanonicalType(ResultType))
6017b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman      return RHS;
6018b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman    return getAtomicType(ResultType);
6019b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman  }
60201adb88370beab45af2f065afe86b51ccd59ec50dChris Lattner  case Type::ConstantArray:
60213d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman  {
60223d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    const ConstantArrayType* LCAT = getAsConstantArrayType(LHS);
60233d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    const ConstantArrayType* RCAT = getAsConstantArrayType(RHS);
60243d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    if (LCAT && RCAT && RCAT->getSize() != LCAT->getSize())
60253d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman      return QualType();
60263d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman
60273d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    QualType LHSElem = getAsArrayType(LHS)->getElementType();
60283d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    QualType RHSElem = getAsArrayType(RHS)->getElementType();
6029447234dd459a00a5ed9b7c3e066162cd7a75bf2dDouglas Gregor    if (Unqualified) {
6030447234dd459a00a5ed9b7c3e066162cd7a75bf2dDouglas Gregor      LHSElem = LHSElem.getUnqualifiedType();
6031447234dd459a00a5ed9b7c3e066162cd7a75bf2dDouglas Gregor      RHSElem = RHSElem.getUnqualifiedType();
6032447234dd459a00a5ed9b7c3e066162cd7a75bf2dDouglas Gregor    }
6033447234dd459a00a5ed9b7c3e066162cd7a75bf2dDouglas Gregor
6034447234dd459a00a5ed9b7c3e066162cd7a75bf2dDouglas Gregor    QualType ResultType = mergeTypes(LHSElem, RHSElem, false, Unqualified);
60353d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    if (ResultType.isNull()) return QualType();
603661710854be2b098428aff5316e64bd34b30fbcb7Chris Lattner    if (LCAT && getCanonicalType(LHSElem) == getCanonicalType(ResultType))
603761710854be2b098428aff5316e64bd34b30fbcb7Chris Lattner      return LHS;
603861710854be2b098428aff5316e64bd34b30fbcb7Chris Lattner    if (RCAT && getCanonicalType(RHSElem) == getCanonicalType(ResultType))
603961710854be2b098428aff5316e64bd34b30fbcb7Chris Lattner      return RHS;
60403bc0f45a5e65814f42b22dcdf7249d1120d16f36Eli Friedman    if (LCAT) return getConstantArrayType(ResultType, LCAT->getSize(),
60413bc0f45a5e65814f42b22dcdf7249d1120d16f36Eli Friedman                                          ArrayType::ArraySizeModifier(), 0);
60423bc0f45a5e65814f42b22dcdf7249d1120d16f36Eli Friedman    if (RCAT) return getConstantArrayType(ResultType, RCAT->getSize(),
60433bc0f45a5e65814f42b22dcdf7249d1120d16f36Eli Friedman                                          ArrayType::ArraySizeModifier(), 0);
60443d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    const VariableArrayType* LVAT = getAsVariableArrayType(LHS);
60453d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    const VariableArrayType* RVAT = getAsVariableArrayType(RHS);
604661710854be2b098428aff5316e64bd34b30fbcb7Chris Lattner    if (LVAT && getCanonicalType(LHSElem) == getCanonicalType(ResultType))
604761710854be2b098428aff5316e64bd34b30fbcb7Chris Lattner      return LHS;
604861710854be2b098428aff5316e64bd34b30fbcb7Chris Lattner    if (RVAT && getCanonicalType(RHSElem) == getCanonicalType(ResultType))
604961710854be2b098428aff5316e64bd34b30fbcb7Chris Lattner      return RHS;
60503d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    if (LVAT) {
60513d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman      // FIXME: This isn't correct! But tricky to implement because
60523d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman      // the array's size has to be the size of LHS, but the type
60533d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman      // has to be different.
60543d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman      return LHS;
60553d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    }
60563d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    if (RVAT) {
60573d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman      // FIXME: This isn't correct! But tricky to implement because
60583d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman      // the array's size has to be the size of RHS, but the type
60593d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman      // has to be different.
60603d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman      return RHS;
60613d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    }
60623bc0f45a5e65814f42b22dcdf7249d1120d16f36Eli Friedman    if (getCanonicalType(LHSElem) == getCanonicalType(ResultType)) return LHS;
60633bc0f45a5e65814f42b22dcdf7249d1120d16f36Eli Friedman    if (getCanonicalType(RHSElem) == getCanonicalType(ResultType)) return RHS;
60647e7eb3da052a6d80ddf2377cab0384c798f73f75Douglas Gregor    return getIncompleteArrayType(ResultType,
60657e7eb3da052a6d80ddf2377cab0384c798f73f75Douglas Gregor                                  ArrayType::ArraySizeModifier(), 0);
60663d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman  }
60671adb88370beab45af2f065afe86b51ccd59ec50dChris Lattner  case Type::FunctionNoProto:
6068447234dd459a00a5ed9b7c3e066162cd7a75bf2dDouglas Gregor    return mergeFunctionTypes(LHS, RHS, OfBlockPointer, Unqualified);
606972564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor  case Type::Record:
607072564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor  case Type::Enum:
60713d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    return QualType();
60721adb88370beab45af2f065afe86b51ccd59ec50dChris Lattner  case Type::Builtin:
60733cc4c0c3058a788689b8fc73c0ac139544435c97Chris Lattner    // Only exactly equal builtin types are compatible, which is tested above.
60743d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    return QualType();
607564cfdb7da3cb744642fe8a99ad5c851ad3c930b2Daniel Dunbar  case Type::Complex:
607664cfdb7da3cb744642fe8a99ad5c851ad3c930b2Daniel Dunbar    // Distinct complex types are incompatible.
607764cfdb7da3cb744642fe8a99ad5c851ad3c930b2Daniel Dunbar    return QualType();
60783cc4c0c3058a788689b8fc73c0ac139544435c97Chris Lattner  case Type::Vector:
60795a61f0e5c5aaecd5713c3fa4b78be7167a7eeff2Eli Friedman    // FIXME: The merged type should be an ExtVector!
60801c471f3e1c0ec8cbc82447bb35908dfc55463e46John McCall    if (areCompatVectorTypes(LHSCan->getAs<VectorType>(),
60811c471f3e1c0ec8cbc82447bb35908dfc55463e46John McCall                             RHSCan->getAs<VectorType>()))
60823d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman      return LHS;
608361710854be2b098428aff5316e64bd34b30fbcb7Chris Lattner    return QualType();
6084c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall  case Type::ObjCObject: {
6085c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall    // Check if the types are assignment compatible.
60865a61f0e5c5aaecd5713c3fa4b78be7167a7eeff2Eli Friedman    // FIXME: This should be type compatibility, e.g. whether
60875a61f0e5c5aaecd5713c3fa4b78be7167a7eeff2Eli Friedman    // "LHS x; RHS x;" at global scope is legal.
6088c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall    const ObjCObjectType* LHSIface = LHS->getAs<ObjCObjectType>();
6089c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall    const ObjCObjectType* RHSIface = RHS->getAs<ObjCObjectType>();
6090c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall    if (canAssignObjCInterfaces(LHSIface, RHSIface))
60915fd659db11922fc12a58e478f7b745f9656b15a7Steve Naroff      return LHS;
60925fd659db11922fc12a58e478f7b745f9656b15a7Steve Naroff
60933d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    return QualType();
609461490e9a965cfee8a78c12c6802138844f04250dCedric Venet  }
609514108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff  case Type::ObjCObjectPointer: {
6096132f2a2da34f378fc675b9e174564b0f52c31d98Fariborz Jahanian    if (OfBlockPointer) {
6097132f2a2da34f378fc675b9e174564b0f52c31d98Fariborz Jahanian      if (canAssignObjCInterfacesInBlockPointer(
6098132f2a2da34f378fc675b9e174564b0f52c31d98Fariborz Jahanian                                          LHS->getAs<ObjCObjectPointerType>(),
6099a4fdbfad150ae37bddaa4094d3925a27a1a1cf3fFariborz Jahanian                                          RHS->getAs<ObjCObjectPointerType>(),
6100a4fdbfad150ae37bddaa4094d3925a27a1a1cf3fFariborz Jahanian                                          BlockReturnType))
61017530c034c0c71a64c5a9173206d9742ae847af8bDavid Blaikie        return LHS;
6102132f2a2da34f378fc675b9e174564b0f52c31d98Fariborz Jahanian      return QualType();
6103132f2a2da34f378fc675b9e174564b0f52c31d98Fariborz Jahanian    }
6104183700f494ec9b6701b6efe82bcb25f4c79ba561John McCall    if (canAssignObjCInterfaces(LHS->getAs<ObjCObjectPointerType>(),
6105183700f494ec9b6701b6efe82bcb25f4c79ba561John McCall                                RHS->getAs<ObjCObjectPointerType>()))
610614108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff      return LHS;
610714108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff
6108bc76dd06eb881c70c9775b74bab8b88cd747f173Steve Naroff    return QualType();
61097530c034c0c71a64c5a9173206d9742ae847af8bDavid Blaikie  }
6110ec0550fa3653d46560bf4484a2e988329c228e39Steve Naroff  }
611172564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor
61127530c034c0c71a64c5a9173206d9742ae847af8bDavid Blaikie  llvm_unreachable("Invalid Type::Class!");
6113ec0550fa3653d46560bf4484a2e988329c228e39Steve Naroff}
61147192f8e9592729882a09d84d77838db26e39ebd4Ted Kremenek
611578213e4aec6d8d22b5828dd2687fa19116ebaa26Fariborz Jahanianbool ASTContext::FunctionTypesMatchOnNSConsumedAttrs(
611678213e4aec6d8d22b5828dd2687fa19116ebaa26Fariborz Jahanian                   const FunctionProtoType *FromFunctionType,
611778213e4aec6d8d22b5828dd2687fa19116ebaa26Fariborz Jahanian                   const FunctionProtoType *ToFunctionType) {
611878213e4aec6d8d22b5828dd2687fa19116ebaa26Fariborz Jahanian  if (FromFunctionType->hasAnyConsumedArgs() !=
611978213e4aec6d8d22b5828dd2687fa19116ebaa26Fariborz Jahanian      ToFunctionType->hasAnyConsumedArgs())
612078213e4aec6d8d22b5828dd2687fa19116ebaa26Fariborz Jahanian    return false;
612178213e4aec6d8d22b5828dd2687fa19116ebaa26Fariborz Jahanian  FunctionProtoType::ExtProtoInfo FromEPI =
612278213e4aec6d8d22b5828dd2687fa19116ebaa26Fariborz Jahanian    FromFunctionType->getExtProtoInfo();
612378213e4aec6d8d22b5828dd2687fa19116ebaa26Fariborz Jahanian  FunctionProtoType::ExtProtoInfo ToEPI =
612478213e4aec6d8d22b5828dd2687fa19116ebaa26Fariborz Jahanian    ToFunctionType->getExtProtoInfo();
612578213e4aec6d8d22b5828dd2687fa19116ebaa26Fariborz Jahanian  if (FromEPI.ConsumedArguments && ToEPI.ConsumedArguments)
612678213e4aec6d8d22b5828dd2687fa19116ebaa26Fariborz Jahanian    for (unsigned ArgIdx = 0, NumArgs = FromFunctionType->getNumArgs();
612778213e4aec6d8d22b5828dd2687fa19116ebaa26Fariborz Jahanian         ArgIdx != NumArgs; ++ArgIdx)  {
612878213e4aec6d8d22b5828dd2687fa19116ebaa26Fariborz Jahanian      if (FromEPI.ConsumedArguments[ArgIdx] !=
612978213e4aec6d8d22b5828dd2687fa19116ebaa26Fariborz Jahanian          ToEPI.ConsumedArguments[ArgIdx])
613078213e4aec6d8d22b5828dd2687fa19116ebaa26Fariborz Jahanian        return false;
613178213e4aec6d8d22b5828dd2687fa19116ebaa26Fariborz Jahanian    }
613278213e4aec6d8d22b5828dd2687fa19116ebaa26Fariborz Jahanian  return true;
613378213e4aec6d8d22b5828dd2687fa19116ebaa26Fariborz Jahanian}
613478213e4aec6d8d22b5828dd2687fa19116ebaa26Fariborz Jahanian
61352390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian/// mergeObjCGCQualifiers - This routine merges ObjC's GC attribute of 'LHS' and
61362390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian/// 'RHS' attributes and returns the merged version; including for function
61372390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian/// return types.
61382390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz JahanianQualType ASTContext::mergeObjCGCQualifiers(QualType LHS, QualType RHS) {
61392390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian  QualType LHSCan = getCanonicalType(LHS),
61402390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian  RHSCan = getCanonicalType(RHS);
61412390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian  // If two types are identical, they are compatible.
61422390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian  if (LHSCan == RHSCan)
61432390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian    return LHS;
61442390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian  if (RHSCan->isFunctionType()) {
61452390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian    if (!LHSCan->isFunctionType())
61462390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian      return QualType();
61472390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian    QualType OldReturnType =
61482390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian      cast<FunctionType>(RHSCan.getTypePtr())->getResultType();
61492390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian    QualType NewReturnType =
61502390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian      cast<FunctionType>(LHSCan.getTypePtr())->getResultType();
61512390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian    QualType ResReturnType =
61522390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian      mergeObjCGCQualifiers(NewReturnType, OldReturnType);
61532390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian    if (ResReturnType.isNull())
61542390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian      return QualType();
61552390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian    if (ResReturnType == NewReturnType || ResReturnType == OldReturnType) {
61562390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian      // id foo(); ... __strong id foo(); or: __strong id foo(); ... id foo();
61572390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian      // In either case, use OldReturnType to build the new function type.
61582390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian      const FunctionType *F = LHS->getAs<FunctionType>();
61592390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian      if (const FunctionProtoType *FPT = cast<FunctionProtoType>(F)) {
6160e23cf437fe76b1ed02d63c3f61b456fd48a915f5John McCall        FunctionProtoType::ExtProtoInfo EPI = FPT->getExtProtoInfo();
6161e23cf437fe76b1ed02d63c3f61b456fd48a915f5John McCall        EPI.ExtInfo = getFunctionExtInfo(LHS);
61622390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian        QualType ResultType
61632390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian          = getFunctionType(OldReturnType, FPT->arg_type_begin(),
6164e23cf437fe76b1ed02d63c3f61b456fd48a915f5John McCall                            FPT->getNumArgs(), EPI);
61652390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian        return ResultType;
61662390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian      }
61672390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian    }
61682390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian    return QualType();
61692390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian  }
61702390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian
61712390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian  // If the qualifiers are different, the types can still be merged.
61722390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian  Qualifiers LQuals = LHSCan.getLocalQualifiers();
61732390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian  Qualifiers RQuals = RHSCan.getLocalQualifiers();
61742390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian  if (LQuals != RQuals) {
61752390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian    // If any of these qualifiers are different, we have a type mismatch.
61762390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian    if (LQuals.getCVRQualifiers() != RQuals.getCVRQualifiers() ||
61772390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian        LQuals.getAddressSpace() != RQuals.getAddressSpace())
61782390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian      return QualType();
61792390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian
61802390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian    // Exactly one GC qualifier difference is allowed: __strong is
61812390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian    // okay if the other type has no GC qualifier but is an Objective
61822390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian    // C object pointer (i.e. implicitly strong by default).  We fix
61832390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian    // this by pretending that the unqualified type was actually
61842390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian    // qualified __strong.
61852390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian    Qualifiers::GC GC_L = LQuals.getObjCGCAttr();
61862390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian    Qualifiers::GC GC_R = RQuals.getObjCGCAttr();
61872390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian    assert((GC_L != GC_R) && "unequal qualifier sets had only equal elements");
61882390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian
61892390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian    if (GC_L == Qualifiers::Weak || GC_R == Qualifiers::Weak)
61902390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian      return QualType();
61912390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian
61922390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian    if (GC_L == Qualifiers::Strong)
61932390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian      return LHS;
61942390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian    if (GC_R == Qualifiers::Strong)
61952390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian      return RHS;
61962390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian    return QualType();
61972390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian  }
61982390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian
61992390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian  if (LHSCan->isObjCObjectPointerType() && RHSCan->isObjCObjectPointerType()) {
62002390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian    QualType LHSBaseQT = LHS->getAs<ObjCObjectPointerType>()->getPointeeType();
62012390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian    QualType RHSBaseQT = RHS->getAs<ObjCObjectPointerType>()->getPointeeType();
62022390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian    QualType ResQT = mergeObjCGCQualifiers(LHSBaseQT, RHSBaseQT);
62032390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian    if (ResQT == LHSBaseQT)
62042390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian      return LHS;
62052390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian    if (ResQT == RHSBaseQT)
62062390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian      return RHS;
62072390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian  }
62082390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian  return QualType();
62092390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian}
62102390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian
62115426bf6456a5aeac416a9150de157904d101c819Chris Lattner//===----------------------------------------------------------------------===//
6212ad74a758189180b8ab8faea648e4766c3bfd7fcbEli Friedman//                         Integer Predicates
6213ad74a758189180b8ab8faea648e4766c3bfd7fcbEli Friedman//===----------------------------------------------------------------------===//
621488054dee0402e4d3c1f64e6b697acc47195c0d72Chris Lattner
62154ba2a17694148e16eaa8d3917f657ffcd3667be4Jay Foadunsigned ASTContext::getIntWidth(QualType T) const {
6216f4c7371fb1d3cebcfb40abad4537bb82515704eaJohn McCall  if (const EnumType *ET = dyn_cast<EnumType>(T))
621729a7f3342c3c6dd15d914c61ae22246c36d51ce7Eli Friedman    T = ET->getDecl()->getIntegerType();
62181274ccd90aec0b205fc838c3d504821ccfb55482Douglas Gregor  if (T->isBooleanType())
62191274ccd90aec0b205fc838c3d504821ccfb55482Douglas Gregor    return 1;
6220f98aba35e6c3da5aae61843fc01334939e4e12ecEli Friedman  // For builtin types, just use the standard type sizing method
6221ad74a758189180b8ab8faea648e4766c3bfd7fcbEli Friedman  return (unsigned)getTypeSize(T);
6222ad74a758189180b8ab8faea648e4766c3bfd7fcbEli Friedman}
6223ad74a758189180b8ab8faea648e4766c3bfd7fcbEli Friedman
6224ad74a758189180b8ab8faea648e4766c3bfd7fcbEli FriedmanQualType ASTContext::getCorrespondingUnsignedType(QualType T) {
6225f60946222721d9ba3c059563935c17b84703187aDouglas Gregor  assert(T->hasSignedIntegerRepresentation() && "Unexpected type");
62266a2b9261bf9c973c7122d9d1febce24a38fa862dChris Lattner
62276a2b9261bf9c973c7122d9d1febce24a38fa862dChris Lattner  // Turn <4 x signed int> -> <4 x unsigned int>
62286a2b9261bf9c973c7122d9d1febce24a38fa862dChris Lattner  if (const VectorType *VTy = T->getAs<VectorType>())
62296a2b9261bf9c973c7122d9d1febce24a38fa862dChris Lattner    return getVectorType(getCorrespondingUnsignedType(VTy->getElementType()),
6230e86d78cf4754a6aef2cf9a33d847aa15338e276fBob Wilson                         VTy->getNumElements(), VTy->getVectorKind());
62316a2b9261bf9c973c7122d9d1febce24a38fa862dChris Lattner
62326a2b9261bf9c973c7122d9d1febce24a38fa862dChris Lattner  // For enums, we return the unsigned version of the base type.
62336a2b9261bf9c973c7122d9d1febce24a38fa862dChris Lattner  if (const EnumType *ETy = T->getAs<EnumType>())
6234ad74a758189180b8ab8faea648e4766c3bfd7fcbEli Friedman    T = ETy->getDecl()->getIntegerType();
62356a2b9261bf9c973c7122d9d1febce24a38fa862dChris Lattner
62366a2b9261bf9c973c7122d9d1febce24a38fa862dChris Lattner  const BuiltinType *BTy = T->getAs<BuiltinType>();
62376a2b9261bf9c973c7122d9d1febce24a38fa862dChris Lattner  assert(BTy && "Unexpected signed integer type");
6238ad74a758189180b8ab8faea648e4766c3bfd7fcbEli Friedman  switch (BTy->getKind()) {
6239ad74a758189180b8ab8faea648e4766c3bfd7fcbEli Friedman  case BuiltinType::Char_S:
6240ad74a758189180b8ab8faea648e4766c3bfd7fcbEli Friedman  case BuiltinType::SChar:
6241ad74a758189180b8ab8faea648e4766c3bfd7fcbEli Friedman    return UnsignedCharTy;
6242ad74a758189180b8ab8faea648e4766c3bfd7fcbEli Friedman  case BuiltinType::Short:
6243ad74a758189180b8ab8faea648e4766c3bfd7fcbEli Friedman    return UnsignedShortTy;
6244ad74a758189180b8ab8faea648e4766c3bfd7fcbEli Friedman  case BuiltinType::Int:
6245ad74a758189180b8ab8faea648e4766c3bfd7fcbEli Friedman    return UnsignedIntTy;
6246ad74a758189180b8ab8faea648e4766c3bfd7fcbEli Friedman  case BuiltinType::Long:
6247ad74a758189180b8ab8faea648e4766c3bfd7fcbEli Friedman    return UnsignedLongTy;
6248ad74a758189180b8ab8faea648e4766c3bfd7fcbEli Friedman  case BuiltinType::LongLong:
6249ad74a758189180b8ab8faea648e4766c3bfd7fcbEli Friedman    return UnsignedLongLongTy;
62502df9ced9fd1e8c7d7b38443db07e0e811de22571Chris Lattner  case BuiltinType::Int128:
62512df9ced9fd1e8c7d7b38443db07e0e811de22571Chris Lattner    return UnsignedInt128Ty;
6252ad74a758189180b8ab8faea648e4766c3bfd7fcbEli Friedman  default:
6253b219cfc4d75f0a03630b7c4509ef791b7e97b2c8David Blaikie    llvm_unreachable("Unexpected signed integer type");
6254ad74a758189180b8ab8faea648e4766c3bfd7fcbEli Friedman  }
6255ad74a758189180b8ab8faea648e4766c3bfd7fcbEli Friedman}
6256ad74a758189180b8ab8faea648e4766c3bfd7fcbEli Friedman
62577b90340c9c7d07aef4e301e72b5e8a30d5f4f0c8Argyrios KyrtzidisASTMutationListener::~ASTMutationListener() { }
62587b90340c9c7d07aef4e301e72b5e8a30d5f4f0c8Argyrios Kyrtzidis
625986df27bbdbb98c39ec2184695c0561209f91beddChris Lattner
626086df27bbdbb98c39ec2184695c0561209f91beddChris Lattner//===----------------------------------------------------------------------===//
626186df27bbdbb98c39ec2184695c0561209f91beddChris Lattner//                          Builtin Type Computation
626286df27bbdbb98c39ec2184695c0561209f91beddChris Lattner//===----------------------------------------------------------------------===//
626386df27bbdbb98c39ec2184695c0561209f91beddChris Lattner
626486df27bbdbb98c39ec2184695c0561209f91beddChris Lattner/// DecodeTypeFromStr - This decodes one type descriptor from Str, advancing the
626533daae66462e8f51ee153463b32bdefd60c801aaChris Lattner/// pointer over the consumed characters.  This returns the resultant type.  If
626633daae66462e8f51ee153463b32bdefd60c801aaChris Lattner/// AllowTypeModifiers is false then modifier like * are not parsed, just basic
626733daae66462e8f51ee153463b32bdefd60c801aaChris Lattner/// types.  This allows "v2i*" to be parsed as a pointer to a v2i instead of
626833daae66462e8f51ee153463b32bdefd60c801aaChris Lattner/// a vector of "i*".
626914e0e7436cf6650a72052baea1f8ebe644cef489Chris Lattner///
627014e0e7436cf6650a72052baea1f8ebe644cef489Chris Lattner/// RequiresICE is filled in on return to indicate whether the value is required
627114e0e7436cf6650a72052baea1f8ebe644cef489Chris Lattner/// to be an Integer Constant Expression.
62724ba2a17694148e16eaa8d3917f657ffcd3667be4Jay Foadstatic QualType DecodeTypeFromStr(const char *&Str, const ASTContext &Context,
627386df27bbdbb98c39ec2184695c0561209f91beddChris Lattner                                  ASTContext::GetBuiltinTypeError &Error,
627414e0e7436cf6650a72052baea1f8ebe644cef489Chris Lattner                                  bool &RequiresICE,
627533daae66462e8f51ee153463b32bdefd60c801aaChris Lattner                                  bool AllowTypeModifiers) {
627686df27bbdbb98c39ec2184695c0561209f91beddChris Lattner  // Modifiers.
627786df27bbdbb98c39ec2184695c0561209f91beddChris Lattner  int HowLong = 0;
627886df27bbdbb98c39ec2184695c0561209f91beddChris Lattner  bool Signed = false, Unsigned = false;
627914e0e7436cf6650a72052baea1f8ebe644cef489Chris Lattner  RequiresICE = false;
6280393bd8e185692a451b2ba16bdfc9e7d3543b4217Chris Lattner
628133daae66462e8f51ee153463b32bdefd60c801aaChris Lattner  // Read the prefixed modifiers first.
628286df27bbdbb98c39ec2184695c0561209f91beddChris Lattner  bool Done = false;
628386df27bbdbb98c39ec2184695c0561209f91beddChris Lattner  while (!Done) {
628486df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    switch (*Str++) {
62851eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    default: Done = true; --Str; break;
6286393bd8e185692a451b2ba16bdfc9e7d3543b4217Chris Lattner    case 'I':
628714e0e7436cf6650a72052baea1f8ebe644cef489Chris Lattner      RequiresICE = true;
6288393bd8e185692a451b2ba16bdfc9e7d3543b4217Chris Lattner      break;
628986df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    case 'S':
629086df27bbdbb98c39ec2184695c0561209f91beddChris Lattner      assert(!Unsigned && "Can't use both 'S' and 'U' modifiers!");
629186df27bbdbb98c39ec2184695c0561209f91beddChris Lattner      assert(!Signed && "Can't use 'S' modifier multiple times!");
629286df27bbdbb98c39ec2184695c0561209f91beddChris Lattner      Signed = true;
629386df27bbdbb98c39ec2184695c0561209f91beddChris Lattner      break;
629486df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    case 'U':
629586df27bbdbb98c39ec2184695c0561209f91beddChris Lattner      assert(!Signed && "Can't use both 'S' and 'U' modifiers!");
629686df27bbdbb98c39ec2184695c0561209f91beddChris Lattner      assert(!Unsigned && "Can't use 'S' modifier multiple times!");
629786df27bbdbb98c39ec2184695c0561209f91beddChris Lattner      Unsigned = true;
629886df27bbdbb98c39ec2184695c0561209f91beddChris Lattner      break;
629986df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    case 'L':
630086df27bbdbb98c39ec2184695c0561209f91beddChris Lattner      assert(HowLong <= 2 && "Can't have LLLL modifier");
630186df27bbdbb98c39ec2184695c0561209f91beddChris Lattner      ++HowLong;
630286df27bbdbb98c39ec2184695c0561209f91beddChris Lattner      break;
630386df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    }
630486df27bbdbb98c39ec2184695c0561209f91beddChris Lattner  }
630586df27bbdbb98c39ec2184695c0561209f91beddChris Lattner
630686df27bbdbb98c39ec2184695c0561209f91beddChris Lattner  QualType Type;
63071eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
630886df27bbdbb98c39ec2184695c0561209f91beddChris Lattner  // Read the base type.
630986df27bbdbb98c39ec2184695c0561209f91beddChris Lattner  switch (*Str++) {
6310b219cfc4d75f0a03630b7c4509ef791b7e97b2c8David Blaikie  default: llvm_unreachable("Unknown builtin type letter!");
631186df27bbdbb98c39ec2184695c0561209f91beddChris Lattner  case 'v':
631286df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    assert(HowLong == 0 && !Signed && !Unsigned &&
631386df27bbdbb98c39ec2184695c0561209f91beddChris Lattner           "Bad modifiers used with 'v'!");
631486df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    Type = Context.VoidTy;
631586df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    break;
631686df27bbdbb98c39ec2184695c0561209f91beddChris Lattner  case 'f':
631786df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    assert(HowLong == 0 && !Signed && !Unsigned &&
631886df27bbdbb98c39ec2184695c0561209f91beddChris Lattner           "Bad modifiers used with 'f'!");
631986df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    Type = Context.FloatTy;
632086df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    break;
632186df27bbdbb98c39ec2184695c0561209f91beddChris Lattner  case 'd':
632286df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    assert(HowLong < 2 && !Signed && !Unsigned &&
632386df27bbdbb98c39ec2184695c0561209f91beddChris Lattner           "Bad modifiers used with 'd'!");
632486df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    if (HowLong)
632586df27bbdbb98c39ec2184695c0561209f91beddChris Lattner      Type = Context.LongDoubleTy;
632686df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    else
632786df27bbdbb98c39ec2184695c0561209f91beddChris Lattner      Type = Context.DoubleTy;
632886df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    break;
632986df27bbdbb98c39ec2184695c0561209f91beddChris Lattner  case 's':
633086df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    assert(HowLong == 0 && "Bad modifiers used with 's'!");
633186df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    if (Unsigned)
633286df27bbdbb98c39ec2184695c0561209f91beddChris Lattner      Type = Context.UnsignedShortTy;
633386df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    else
633486df27bbdbb98c39ec2184695c0561209f91beddChris Lattner      Type = Context.ShortTy;
633586df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    break;
633686df27bbdbb98c39ec2184695c0561209f91beddChris Lattner  case 'i':
633786df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    if (HowLong == 3)
633886df27bbdbb98c39ec2184695c0561209f91beddChris Lattner      Type = Unsigned ? Context.UnsignedInt128Ty : Context.Int128Ty;
633986df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    else if (HowLong == 2)
634086df27bbdbb98c39ec2184695c0561209f91beddChris Lattner      Type = Unsigned ? Context.UnsignedLongLongTy : Context.LongLongTy;
634186df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    else if (HowLong == 1)
634286df27bbdbb98c39ec2184695c0561209f91beddChris Lattner      Type = Unsigned ? Context.UnsignedLongTy : Context.LongTy;
634386df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    else
634486df27bbdbb98c39ec2184695c0561209f91beddChris Lattner      Type = Unsigned ? Context.UnsignedIntTy : Context.IntTy;
634586df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    break;
634686df27bbdbb98c39ec2184695c0561209f91beddChris Lattner  case 'c':
634786df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    assert(HowLong == 0 && "Bad modifiers used with 'c'!");
634886df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    if (Signed)
634986df27bbdbb98c39ec2184695c0561209f91beddChris Lattner      Type = Context.SignedCharTy;
635086df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    else if (Unsigned)
635186df27bbdbb98c39ec2184695c0561209f91beddChris Lattner      Type = Context.UnsignedCharTy;
635286df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    else
635386df27bbdbb98c39ec2184695c0561209f91beddChris Lattner      Type = Context.CharTy;
635486df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    break;
635586df27bbdbb98c39ec2184695c0561209f91beddChris Lattner  case 'b': // boolean
635686df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    assert(HowLong == 0 && !Signed && !Unsigned && "Bad modifiers for 'b'!");
635786df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    Type = Context.BoolTy;
635886df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    break;
635986df27bbdbb98c39ec2184695c0561209f91beddChris Lattner  case 'z':  // size_t.
636086df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    assert(HowLong == 0 && !Signed && !Unsigned && "Bad modifiers for 'z'!");
636186df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    Type = Context.getSizeType();
636286df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    break;
636386df27bbdbb98c39ec2184695c0561209f91beddChris Lattner  case 'F':
636486df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    Type = Context.getCFConstantStringType();
636586df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    break;
6366ba8bda05fefd3bb2f1ef201784b685f715bdde29Fariborz Jahanian  case 'G':
6367ba8bda05fefd3bb2f1ef201784b685f715bdde29Fariborz Jahanian    Type = Context.getObjCIdType();
6368ba8bda05fefd3bb2f1ef201784b685f715bdde29Fariborz Jahanian    break;
6369ba8bda05fefd3bb2f1ef201784b685f715bdde29Fariborz Jahanian  case 'H':
6370ba8bda05fefd3bb2f1ef201784b685f715bdde29Fariborz Jahanian    Type = Context.getObjCSelType();
6371ba8bda05fefd3bb2f1ef201784b685f715bdde29Fariborz Jahanian    break;
637286df27bbdbb98c39ec2184695c0561209f91beddChris Lattner  case 'a':
637386df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    Type = Context.getBuiltinVaListType();
637486df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    assert(!Type.isNull() && "builtin va list type not initialized!");
637586df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    break;
637686df27bbdbb98c39ec2184695c0561209f91beddChris Lattner  case 'A':
637786df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    // This is a "reference" to a va_list; however, what exactly
637886df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    // this means depends on how va_list is defined. There are two
637986df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    // different kinds of va_list: ones passed by value, and ones
638086df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    // passed by reference.  An example of a by-value va_list is
638186df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    // x86, where va_list is a char*. An example of by-ref va_list
638286df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    // is x86-64, where va_list is a __va_list_tag[1]. For x86,
638386df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    // we want this argument to be a char*&; for x86-64, we want
638486df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    // it to be a __va_list_tag*.
638586df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    Type = Context.getBuiltinVaListType();
638686df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    assert(!Type.isNull() && "builtin va list type not initialized!");
638714e0e7436cf6650a72052baea1f8ebe644cef489Chris Lattner    if (Type->isArrayType())
638886df27bbdbb98c39ec2184695c0561209f91beddChris Lattner      Type = Context.getArrayDecayedType(Type);
638914e0e7436cf6650a72052baea1f8ebe644cef489Chris Lattner    else
639086df27bbdbb98c39ec2184695c0561209f91beddChris Lattner      Type = Context.getLValueReferenceType(Type);
639186df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    break;
639286df27bbdbb98c39ec2184695c0561209f91beddChris Lattner  case 'V': {
639386df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    char *End;
639486df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    unsigned NumElements = strtoul(Str, &End, 10);
639586df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    assert(End != Str && "Missing vector size");
639686df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    Str = End;
63971eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
639814e0e7436cf6650a72052baea1f8ebe644cef489Chris Lattner    QualType ElementType = DecodeTypeFromStr(Str, Context, Error,
639914e0e7436cf6650a72052baea1f8ebe644cef489Chris Lattner                                             RequiresICE, false);
640014e0e7436cf6650a72052baea1f8ebe644cef489Chris Lattner    assert(!RequiresICE && "Can't require vector ICE");
640133daae66462e8f51ee153463b32bdefd60c801aaChris Lattner
640233daae66462e8f51ee153463b32bdefd60c801aaChris Lattner    // TODO: No way to make AltiVec vectors in builtins yet.
6403788b0fd67e1992f23555454efcdb16a19dfefac3Chris Lattner    Type = Context.getVectorType(ElementType, NumElements,
6404e86d78cf4754a6aef2cf9a33d847aa15338e276fBob Wilson                                 VectorType::GenericVector);
640586df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    break;
640686df27bbdbb98c39ec2184695c0561209f91beddChris Lattner  }
6407d3a23b238a2b2c0f11e6ac4951c7410a8c5717bfDouglas Gregor  case 'X': {
640814e0e7436cf6650a72052baea1f8ebe644cef489Chris Lattner    QualType ElementType = DecodeTypeFromStr(Str, Context, Error, RequiresICE,
640914e0e7436cf6650a72052baea1f8ebe644cef489Chris Lattner                                             false);
641014e0e7436cf6650a72052baea1f8ebe644cef489Chris Lattner    assert(!RequiresICE && "Can't require complex ICE");
6411d3a23b238a2b2c0f11e6ac4951c7410a8c5717bfDouglas Gregor    Type = Context.getComplexType(ElementType);
6412d3a23b238a2b2c0f11e6ac4951c7410a8c5717bfDouglas Gregor    break;
6413cc075e4092900ac9eec9dc92e35096eae1ee29c7Fariborz Jahanian  }
6414cc075e4092900ac9eec9dc92e35096eae1ee29c7Fariborz Jahanian  case 'Y' : {
6415cc075e4092900ac9eec9dc92e35096eae1ee29c7Fariborz Jahanian    Type = Context.getPointerDiffType();
6416cc075e4092900ac9eec9dc92e35096eae1ee29c7Fariborz Jahanian    break;
6417cc075e4092900ac9eec9dc92e35096eae1ee29c7Fariborz Jahanian  }
64189a5a7e7351f78345a72c4956af25590f6d40ebcdChris Lattner  case 'P':
6419c29f77b769bcc5b6dc85e72c8e3cc2e348e5cf25Douglas Gregor    Type = Context.getFILEType();
6420c29f77b769bcc5b6dc85e72c8e3cc2e348e5cf25Douglas Gregor    if (Type.isNull()) {
6421f711c41dd9412a8182793259d355c4f6979ed5edMike Stump      Error = ASTContext::GE_Missing_stdio;
642286df27bbdbb98c39ec2184695c0561209f91beddChris Lattner      return QualType();
642386df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    }
6424fd612dbb23cd31c03c898ae53ff18d0dfd8488f9Mike Stump    break;
64259a5a7e7351f78345a72c4956af25590f6d40ebcdChris Lattner  case 'J':
6426f711c41dd9412a8182793259d355c4f6979ed5edMike Stump    if (Signed)
6427782fa308a765aeac2acb39c4e697c937ec21185bMike Stump      Type = Context.getsigjmp_bufType();
6428f711c41dd9412a8182793259d355c4f6979ed5edMike Stump    else
6429f711c41dd9412a8182793259d355c4f6979ed5edMike Stump      Type = Context.getjmp_bufType();
6430f711c41dd9412a8182793259d355c4f6979ed5edMike Stump
6431fd612dbb23cd31c03c898ae53ff18d0dfd8488f9Mike Stump    if (Type.isNull()) {
6432f711c41dd9412a8182793259d355c4f6979ed5edMike Stump      Error = ASTContext::GE_Missing_setjmp;
6433fd612dbb23cd31c03c898ae53ff18d0dfd8488f9Mike Stump      return QualType();
6434fd612dbb23cd31c03c898ae53ff18d0dfd8488f9Mike Stump    }
6435fd612dbb23cd31c03c898ae53ff18d0dfd8488f9Mike Stump    break;
6436e2d4f4ed44a32f179c6d48cd1dba8346ab2129d9Rafael Espindola  case 'K':
6437e2d4f4ed44a32f179c6d48cd1dba8346ab2129d9Rafael Espindola    assert(HowLong == 0 && !Signed && !Unsigned && "Bad modifiers for 'K'!");
6438e2d4f4ed44a32f179c6d48cd1dba8346ab2129d9Rafael Espindola    Type = Context.getucontext_tType();
6439e2d4f4ed44a32f179c6d48cd1dba8346ab2129d9Rafael Espindola
6440e2d4f4ed44a32f179c6d48cd1dba8346ab2129d9Rafael Espindola    if (Type.isNull()) {
6441e2d4f4ed44a32f179c6d48cd1dba8346ab2129d9Rafael Espindola      Error = ASTContext::GE_Missing_ucontext;
6442e2d4f4ed44a32f179c6d48cd1dba8346ab2129d9Rafael Espindola      return QualType();
6443e2d4f4ed44a32f179c6d48cd1dba8346ab2129d9Rafael Espindola    }
6444e2d4f4ed44a32f179c6d48cd1dba8346ab2129d9Rafael Espindola    break;
6445782fa308a765aeac2acb39c4e697c937ec21185bMike Stump  }
64461eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
644733daae66462e8f51ee153463b32bdefd60c801aaChris Lattner  // If there are modifiers and if we're allowed to parse them, go for it.
644833daae66462e8f51ee153463b32bdefd60c801aaChris Lattner  Done = !AllowTypeModifiers;
644986df27bbdbb98c39ec2184695c0561209f91beddChris Lattner  while (!Done) {
6450187ab37a05b8f7015b9f39cc8cd9129a0c6d0b48John McCall    switch (char c = *Str++) {
645133daae66462e8f51ee153463b32bdefd60c801aaChris Lattner    default: Done = true; --Str; break;
645233daae66462e8f51ee153463b32bdefd60c801aaChris Lattner    case '*':
645333daae66462e8f51ee153463b32bdefd60c801aaChris Lattner    case '&': {
645433daae66462e8f51ee153463b32bdefd60c801aaChris Lattner      // Both pointers and references can have their pointee types
645533daae66462e8f51ee153463b32bdefd60c801aaChris Lattner      // qualified with an address space.
645633daae66462e8f51ee153463b32bdefd60c801aaChris Lattner      char *End;
645733daae66462e8f51ee153463b32bdefd60c801aaChris Lattner      unsigned AddrSpace = strtoul(Str, &End, 10);
645833daae66462e8f51ee153463b32bdefd60c801aaChris Lattner      if (End != Str && AddrSpace != 0) {
645933daae66462e8f51ee153463b32bdefd60c801aaChris Lattner        Type = Context.getAddrSpaceQualType(Type, AddrSpace);
646033daae66462e8f51ee153463b32bdefd60c801aaChris Lattner        Str = End;
646133daae66462e8f51ee153463b32bdefd60c801aaChris Lattner      }
646233daae66462e8f51ee153463b32bdefd60c801aaChris Lattner      if (c == '*')
646333daae66462e8f51ee153463b32bdefd60c801aaChris Lattner        Type = Context.getPointerType(Type);
646433daae66462e8f51ee153463b32bdefd60c801aaChris Lattner      else
646533daae66462e8f51ee153463b32bdefd60c801aaChris Lattner        Type = Context.getLValueReferenceType(Type);
646633daae66462e8f51ee153463b32bdefd60c801aaChris Lattner      break;
646733daae66462e8f51ee153463b32bdefd60c801aaChris Lattner    }
646833daae66462e8f51ee153463b32bdefd60c801aaChris Lattner    // FIXME: There's no way to have a built-in with an rvalue ref arg.
646933daae66462e8f51ee153463b32bdefd60c801aaChris Lattner    case 'C':
647033daae66462e8f51ee153463b32bdefd60c801aaChris Lattner      Type = Type.withConst();
647133daae66462e8f51ee153463b32bdefd60c801aaChris Lattner      break;
647233daae66462e8f51ee153463b32bdefd60c801aaChris Lattner    case 'D':
647333daae66462e8f51ee153463b32bdefd60c801aaChris Lattner      Type = Context.getVolatileType(Type);
647433daae66462e8f51ee153463b32bdefd60c801aaChris Lattner      break;
647518932a0f2a94a7813ec461d1118c39ecf8aa936fTed Kremenek    case 'R':
647618932a0f2a94a7813ec461d1118c39ecf8aa936fTed Kremenek      Type = Type.withRestrict();
647718932a0f2a94a7813ec461d1118c39ecf8aa936fTed Kremenek      break;
647886df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    }
647986df27bbdbb98c39ec2184695c0561209f91beddChris Lattner  }
6480393bd8e185692a451b2ba16bdfc9e7d3543b4217Chris Lattner
648114e0e7436cf6650a72052baea1f8ebe644cef489Chris Lattner  assert((!RequiresICE || Type->isIntegralOrEnumerationType()) &&
6482393bd8e185692a451b2ba16bdfc9e7d3543b4217Chris Lattner         "Integer constant 'I' type must be an integer");
64831eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
648486df27bbdbb98c39ec2184695c0561209f91beddChris Lattner  return Type;
648586df27bbdbb98c39ec2184695c0561209f91beddChris Lattner}
648686df27bbdbb98c39ec2184695c0561209f91beddChris Lattner
648786df27bbdbb98c39ec2184695c0561209f91beddChris Lattner/// GetBuiltinType - Return the type for the specified builtin.
648833daae66462e8f51ee153463b32bdefd60c801aaChris LattnerQualType ASTContext::GetBuiltinType(unsigned Id,
648914e0e7436cf6650a72052baea1f8ebe644cef489Chris Lattner                                    GetBuiltinTypeError &Error,
64904ba2a17694148e16eaa8d3917f657ffcd3667be4Jay Foad                                    unsigned *IntegerConstantArgs) const {
649133daae66462e8f51ee153463b32bdefd60c801aaChris Lattner  const char *TypeStr = BuiltinInfo.GetTypeString(Id);
64921eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
64935f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner  SmallVector<QualType, 8> ArgTypes;
64941eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
649514e0e7436cf6650a72052baea1f8ebe644cef489Chris Lattner  bool RequiresICE = false;
649686df27bbdbb98c39ec2184695c0561209f91beddChris Lattner  Error = GE_None;
649714e0e7436cf6650a72052baea1f8ebe644cef489Chris Lattner  QualType ResType = DecodeTypeFromStr(TypeStr, *this, Error,
649814e0e7436cf6650a72052baea1f8ebe644cef489Chris Lattner                                       RequiresICE, true);
649986df27bbdbb98c39ec2184695c0561209f91beddChris Lattner  if (Error != GE_None)
650086df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    return QualType();
650114e0e7436cf6650a72052baea1f8ebe644cef489Chris Lattner
650214e0e7436cf6650a72052baea1f8ebe644cef489Chris Lattner  assert(!RequiresICE && "Result of intrinsic cannot be required to be an ICE");
650314e0e7436cf6650a72052baea1f8ebe644cef489Chris Lattner
650486df27bbdbb98c39ec2184695c0561209f91beddChris Lattner  while (TypeStr[0] && TypeStr[0] != '.') {
650514e0e7436cf6650a72052baea1f8ebe644cef489Chris Lattner    QualType Ty = DecodeTypeFromStr(TypeStr, *this, Error, RequiresICE, true);
650686df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    if (Error != GE_None)
650786df27bbdbb98c39ec2184695c0561209f91beddChris Lattner      return QualType();
650886df27bbdbb98c39ec2184695c0561209f91beddChris Lattner
650914e0e7436cf6650a72052baea1f8ebe644cef489Chris Lattner    // If this argument is required to be an IntegerConstantExpression and the
651014e0e7436cf6650a72052baea1f8ebe644cef489Chris Lattner    // caller cares, fill in the bitmask we return.
651114e0e7436cf6650a72052baea1f8ebe644cef489Chris Lattner    if (RequiresICE && IntegerConstantArgs)
651214e0e7436cf6650a72052baea1f8ebe644cef489Chris Lattner      *IntegerConstantArgs |= 1 << ArgTypes.size();
651314e0e7436cf6650a72052baea1f8ebe644cef489Chris Lattner
651486df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    // Do array -> pointer decay.  The builtin should use the decayed type.
651586df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    if (Ty->isArrayType())
651686df27bbdbb98c39ec2184695c0561209f91beddChris Lattner      Ty = getArrayDecayedType(Ty);
65171eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
651886df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    ArgTypes.push_back(Ty);
651986df27bbdbb98c39ec2184695c0561209f91beddChris Lattner  }
652086df27bbdbb98c39ec2184695c0561209f91beddChris Lattner
652186df27bbdbb98c39ec2184695c0561209f91beddChris Lattner  assert((TypeStr[0] != '.' || TypeStr[1] == 0) &&
652286df27bbdbb98c39ec2184695c0561209f91beddChris Lattner         "'.' should only occur at end of builtin type list!");
652386df27bbdbb98c39ec2184695c0561209f91beddChris Lattner
652400ccbefcffeb88ea3e2e6323e594fa968753ad14John McCall  FunctionType::ExtInfo EI;
652500ccbefcffeb88ea3e2e6323e594fa968753ad14John McCall  if (BuiltinInfo.isNoReturn(Id)) EI = EI.withNoReturn(true);
652600ccbefcffeb88ea3e2e6323e594fa968753ad14John McCall
652700ccbefcffeb88ea3e2e6323e594fa968753ad14John McCall  bool Variadic = (TypeStr[0] == '.');
652800ccbefcffeb88ea3e2e6323e594fa968753ad14John McCall
652900ccbefcffeb88ea3e2e6323e594fa968753ad14John McCall  // We really shouldn't be making a no-proto type here, especially in C++.
653000ccbefcffeb88ea3e2e6323e594fa968753ad14John McCall  if (ArgTypes.empty() && Variadic)
653100ccbefcffeb88ea3e2e6323e594fa968753ad14John McCall    return getFunctionNoProtoType(ResType, EI);
6532ce056bcaa1c97b89a4b2de2112c62d060863be2bDouglas Gregor
6533e23cf437fe76b1ed02d63c3f61b456fd48a915f5John McCall  FunctionProtoType::ExtProtoInfo EPI;
653400ccbefcffeb88ea3e2e6323e594fa968753ad14John McCall  EPI.ExtInfo = EI;
653500ccbefcffeb88ea3e2e6323e594fa968753ad14John McCall  EPI.Variadic = Variadic;
6536e23cf437fe76b1ed02d63c3f61b456fd48a915f5John McCall
6537e23cf437fe76b1ed02d63c3f61b456fd48a915f5John McCall  return getFunctionType(ResType, ArgTypes.data(), ArgTypes.size(), EPI);
653886df27bbdbb98c39ec2184695c0561209f91beddChris Lattner}
6539a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman
654090e99a84ddd020e8fda79643748243725a2ed071Argyrios KyrtzidisGVALinkage ASTContext::GetGVALinkageForFunction(const FunctionDecl *FD) {
654190e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis  GVALinkage External = GVA_StrongExternal;
654290e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis
654390e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis  Linkage L = FD->getLinkage();
654490e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis  switch (L) {
654590e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis  case NoLinkage:
654690e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis  case InternalLinkage:
654790e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis  case UniqueExternalLinkage:
654890e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis    return GVA_Internal;
654990e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis
655090e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis  case ExternalLinkage:
655190e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis    switch (FD->getTemplateSpecializationKind()) {
655290e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis    case TSK_Undeclared:
655390e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis    case TSK_ExplicitSpecialization:
655490e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis      External = GVA_StrongExternal;
655590e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis      break;
655690e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis
655790e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis    case TSK_ExplicitInstantiationDefinition:
655890e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis      return GVA_ExplicitTemplateInstantiation;
655990e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis
656090e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis    case TSK_ExplicitInstantiationDeclaration:
656190e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis    case TSK_ImplicitInstantiation:
656290e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis      External = GVA_TemplateInstantiation;
656390e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis      break;
656490e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis    }
656590e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis  }
656690e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis
656790e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis  if (!FD->isInlined())
656890e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis    return External;
656990e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis
657090e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis  if (!getLangOptions().CPlusPlus || FD->hasAttr<GNUInlineAttr>()) {
657190e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis    // GNU or C99 inline semantics. Determine whether this symbol should be
657290e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis    // externally visible.
657390e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis    if (FD->isInlineDefinitionExternallyVisible())
657490e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis      return External;
657590e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis
657690e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis    // C99 inline semantics, where the symbol is not externally visible.
657790e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis    return GVA_C99Inline;
657890e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis  }
657990e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis
658090e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis  // C++0x [temp.explicit]p9:
658190e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis  //   [ Note: The intent is that an inline function that is the subject of
658290e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis  //   an explicit instantiation declaration will still be implicitly
658390e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis  //   instantiated when used so that the body can be considered for
658490e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis  //   inlining, but that no out-of-line copy of the inline function would be
658590e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis  //   generated in the translation unit. -- end note ]
658690e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis  if (FD->getTemplateSpecializationKind()
658790e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis                                       == TSK_ExplicitInstantiationDeclaration)
658890e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis    return GVA_C99Inline;
658990e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis
659090e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis  return GVA_CXXInline;
659190e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis}
659290e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis
659390e99a84ddd020e8fda79643748243725a2ed071Argyrios KyrtzidisGVALinkage ASTContext::GetGVALinkageForVariable(const VarDecl *VD) {
659490e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis  // If this is a static data member, compute the kind of template
659590e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis  // specialization. Otherwise, this variable is not part of a
659690e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis  // template.
659790e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis  TemplateSpecializationKind TSK = TSK_Undeclared;
659890e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis  if (VD->isStaticDataMember())
659990e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis    TSK = VD->getTemplateSpecializationKind();
660090e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis
660190e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis  Linkage L = VD->getLinkage();
660290e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis  if (L == ExternalLinkage && getLangOptions().CPlusPlus &&
660390e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis      VD->getType()->getLinkage() == UniqueExternalLinkage)
660490e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis    L = UniqueExternalLinkage;
660590e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis
660690e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis  switch (L) {
660790e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis  case NoLinkage:
660890e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis  case InternalLinkage:
660990e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis  case UniqueExternalLinkage:
661090e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis    return GVA_Internal;
661190e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis
661290e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis  case ExternalLinkage:
661390e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis    switch (TSK) {
661490e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis    case TSK_Undeclared:
661590e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis    case TSK_ExplicitSpecialization:
661690e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis      return GVA_StrongExternal;
661790e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis
661890e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis    case TSK_ExplicitInstantiationDeclaration:
661990e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis      llvm_unreachable("Variable should not be instantiated");
662090e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis      // Fall through to treat this like any other instantiation.
662190e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis
662290e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis    case TSK_ExplicitInstantiationDefinition:
662390e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis      return GVA_ExplicitTemplateInstantiation;
662490e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis
662590e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis    case TSK_ImplicitInstantiation:
662690e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis      return GVA_TemplateInstantiation;
662790e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis    }
662890e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis  }
662990e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis
66307530c034c0c71a64c5a9173206d9742ae847af8bDavid Blaikie  llvm_unreachable("Invalid Linkage!");
663190e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis}
663290e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis
66334ac7c0bb39696e92fd220118fedc484c09a69870Argyrios Kyrtzidisbool ASTContext::DeclMustBeEmitted(const Decl *D) {
663490e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis  if (const VarDecl *VD = dyn_cast<VarDecl>(D)) {
663590e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis    if (!VD->isFileVarDecl())
663690e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis      return false;
6637144b38a7995cbe0928e34fbcc865bb2d2be4f7a3Argyrios Kyrtzidis  } else if (!isa<FunctionDecl>(D))
663890e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis    return false;
663990e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis
6640ab411c8c2efed8f2403bf8596e780c0f2f905a19Argyrios Kyrtzidis  // Weak references don't produce any output by themselves.
6641ab411c8c2efed8f2403bf8596e780c0f2f905a19Argyrios Kyrtzidis  if (D->hasAttr<WeakRefAttr>())
6642ab411c8c2efed8f2403bf8596e780c0f2f905a19Argyrios Kyrtzidis    return false;
6643ab411c8c2efed8f2403bf8596e780c0f2f905a19Argyrios Kyrtzidis
664490e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis  // Aliases and used decls are required.
664590e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis  if (D->hasAttr<AliasAttr>() || D->hasAttr<UsedAttr>())
664690e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis    return true;
664790e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis
664890e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis  if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
664990e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis    // Forward declarations aren't required.
665010620eb5164e31208fcbf0437cd79ae535ed0559Sean Hunt    if (!FD->doesThisDeclarationHaveABody())
6651dce67a70a86db8758c926a76fdd980f5369d5746Nick Lewycky      return FD->doesDeclarationForceExternallyVisibleDefinition();
665290e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis
665390e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis    // Constructors and destructors are required.
665490e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis    if (FD->hasAttr<ConstructorAttr>() || FD->hasAttr<DestructorAttr>())
665590e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis      return true;
665690e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis
665790e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis    // The key function for a class is required.
665890e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis    if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(FD)) {
665990e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis      const CXXRecordDecl *RD = MD->getParent();
666090e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis      if (MD->isOutOfLine() && RD->isDynamicClass()) {
666190e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis        const CXXMethodDecl *KeyFunc = getKeyFunction(RD);
666290e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis        if (KeyFunc && KeyFunc->getCanonicalDecl() == MD->getCanonicalDecl())
666390e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis          return true;
666490e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis      }
666590e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis    }
666690e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis
666790e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis    GVALinkage Linkage = GetGVALinkageForFunction(FD);
666890e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis
666990e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis    // static, static inline, always_inline, and extern inline functions can
667090e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis    // always be deferred.  Normal inline functions can be deferred in C99/C++.
667190e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis    // Implicit template instantiations can also be deferred in C++.
667290e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis    if (Linkage == GVA_Internal  || Linkage == GVA_C99Inline ||
667390e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis        Linkage == GVA_CXXInline || Linkage == GVA_TemplateInstantiation)
667490e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis      return false;
667590e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis    return true;
667690e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis  }
667794da1587f7d584fc61df793229d197969f204cd9Douglas Gregor
667890e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis  const VarDecl *VD = cast<VarDecl>(D);
667990e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis  assert(VD->isFileVarDecl() && "Expected file scoped var");
668090e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis
6681ab411c8c2efed8f2403bf8596e780c0f2f905a19Argyrios Kyrtzidis  if (VD->isThisDeclarationADefinition() == VarDecl::DeclarationOnly)
6682ab411c8c2efed8f2403bf8596e780c0f2f905a19Argyrios Kyrtzidis    return false;
6683ab411c8c2efed8f2403bf8596e780c0f2f905a19Argyrios Kyrtzidis
668490e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis  // Structs that have non-trivial constructors or destructors are required.
668590e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis
668690e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis  // FIXME: Handle references.
6687023df37c27ee8035664fb62f206ca58f4e2a169dSean Hunt  // FIXME: Be more selective about which constructors we care about.
668890e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis  if (const RecordType *RT = VD->getType()->getAs<RecordType>()) {
668990e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis    if (const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(RT->getDecl())) {
6690023df37c27ee8035664fb62f206ca58f4e2a169dSean Hunt      if (RD->hasDefinition() && !(RD->hasTrivialDefaultConstructor() &&
6691023df37c27ee8035664fb62f206ca58f4e2a169dSean Hunt                                   RD->hasTrivialCopyConstructor() &&
6692023df37c27ee8035664fb62f206ca58f4e2a169dSean Hunt                                   RD->hasTrivialMoveConstructor() &&
6693023df37c27ee8035664fb62f206ca58f4e2a169dSean Hunt                                   RD->hasTrivialDestructor()))
669490e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis        return true;
669590e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis    }
669690e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis  }
669790e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis
669890e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis  GVALinkage L = GetGVALinkageForVariable(VD);
669990e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis  if (L == GVA_Internal || L == GVA_TemplateInstantiation) {
670090e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis    if (!(VD->getInit() && VD->getInit()->HasSideEffects(*this)))
670190e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis      return false;
670290e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis  }
670390e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis
670490e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis  return true;
670590e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis}
6706071cc7deffad608165b1ddd5263e8bf181861520Charles Davis
6707ee743f903858e337434ac0335f147f4de4ecae05Charles DavisCallingConv ASTContext::getDefaultMethodCallConv() {
6708ee743f903858e337434ac0335f147f4de4ecae05Charles Davis  // Pass through to the C++ ABI object
6709ee743f903858e337434ac0335f147f4de4ecae05Charles Davis  return ABI->getDefaultMethodCallConv();
6710ee743f903858e337434ac0335f147f4de4ecae05Charles Davis}
6711ee743f903858e337434ac0335f147f4de4ecae05Charles Davis
67124ba2a17694148e16eaa8d3917f657ffcd3667be4Jay Foadbool ASTContext::isNearlyEmpty(const CXXRecordDecl *RD) const {
6713dae0cb52e4e3d46bbfc9a4510909522197a92e54Anders Carlsson  // Pass through to the C++ ABI object
6714dae0cb52e4e3d46bbfc9a4510909522197a92e54Anders Carlsson  return ABI->isNearlyEmpty(RD);
6715dae0cb52e4e3d46bbfc9a4510909522197a92e54Anders Carlsson}
6716dae0cb52e4e3d46bbfc9a4510909522197a92e54Anders Carlsson
671714110477887e3dc168ffc6c191e72d705051f99ePeter CollingbourneMangleContext *ASTContext::createMangleContext() {
6718bcfd1f55bfbb3e5944cd5e03d07b343e280838c4Douglas Gregor  switch (Target->getCXXABI()) {
671914110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne  case CXXABI_ARM:
672014110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne  case CXXABI_Itanium:
672114110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne    return createItaniumMangleContext(*this, getDiagnostics());
672214110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne  case CXXABI_Microsoft:
672314110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne    return createMicrosoftMangleContext(*this, getDiagnostics());
672414110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne  }
6725b219cfc4d75f0a03630b7c4509ef791b7e97b2c8David Blaikie  llvm_unreachable("Unsupported ABI");
672614110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne}
672714110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne
6728071cc7deffad608165b1ddd5263e8bf181861520Charles DavisCXXABI::~CXXABI() {}
6729ba29bd25515fbd99e98ba0fedb9d93617b27609eTed Kremenek
6730ba29bd25515fbd99e98ba0fedb9d93617b27609eTed Kremeneksize_t ASTContext::getSideTableAllocatedMemory() const {
67310c8cd1ac55720276ad41fa7b4f8785bead5afd32Ted Kremenek  return ASTRecordLayouts.getMemorySize()
67320c8cd1ac55720276ad41fa7b4f8785bead5afd32Ted Kremenek    + llvm::capacity_in_bytes(ObjCLayouts)
67330c8cd1ac55720276ad41fa7b4f8785bead5afd32Ted Kremenek    + llvm::capacity_in_bytes(KeyFunctions)
67340c8cd1ac55720276ad41fa7b4f8785bead5afd32Ted Kremenek    + llvm::capacity_in_bytes(ObjCImpls)
67350c8cd1ac55720276ad41fa7b4f8785bead5afd32Ted Kremenek    + llvm::capacity_in_bytes(BlockVarCopyInits)
67360c8cd1ac55720276ad41fa7b4f8785bead5afd32Ted Kremenek    + llvm::capacity_in_bytes(DeclAttrs)
67370c8cd1ac55720276ad41fa7b4f8785bead5afd32Ted Kremenek    + llvm::capacity_in_bytes(InstantiatedFromStaticDataMember)
67380c8cd1ac55720276ad41fa7b4f8785bead5afd32Ted Kremenek    + llvm::capacity_in_bytes(InstantiatedFromUsingDecl)
67390c8cd1ac55720276ad41fa7b4f8785bead5afd32Ted Kremenek    + llvm::capacity_in_bytes(InstantiatedFromUsingShadowDecl)
67400c8cd1ac55720276ad41fa7b4f8785bead5afd32Ted Kremenek    + llvm::capacity_in_bytes(InstantiatedFromUnnamedFieldDecl)
67410c8cd1ac55720276ad41fa7b4f8785bead5afd32Ted Kremenek    + llvm::capacity_in_bytes(OverriddenMethods)
67420c8cd1ac55720276ad41fa7b4f8785bead5afd32Ted Kremenek    + llvm::capacity_in_bytes(Types)
6743af0f4d0b2e38c810effc8b024ad2fb6604eec5d3Francois Pichet    + llvm::capacity_in_bytes(VariableArrayTypes)
67440d95f0d7b81110f77e99e833f766d19be7b7e072Francois Pichet    + llvm::capacity_in_bytes(ClassScopeSpecializationPattern);
6745ba29bd25515fbd99e98ba0fedb9d93617b27609eTed Kremenek}
6746d211cb709510fbe7e75167b9feee0050851d001aTed Kremenek
6747d211cb709510fbe7e75167b9feee0050851d001aTed Kremenekvoid ASTContext::setParameterIndex(const ParmVarDecl *D, unsigned int index) {
6748d211cb709510fbe7e75167b9feee0050851d001aTed Kremenek  ParamIndices[D] = index;
6749d211cb709510fbe7e75167b9feee0050851d001aTed Kremenek}
6750d211cb709510fbe7e75167b9feee0050851d001aTed Kremenek
6751d211cb709510fbe7e75167b9feee0050851d001aTed Kremenekunsigned ASTContext::getParameterIndex(const ParmVarDecl *D) const {
6752d211cb709510fbe7e75167b9feee0050851d001aTed Kremenek  ParameterIndexTable::const_iterator I = ParamIndices.find(D);
6753d211cb709510fbe7e75167b9feee0050851d001aTed Kremenek  assert(I != ParamIndices.end() &&
6754d211cb709510fbe7e75167b9feee0050851d001aTed Kremenek         "ParmIndices lacks entry set by ParmVarDecl");
6755d211cb709510fbe7e75167b9feee0050851d001aTed Kremenek  return I->second;
6756d211cb709510fbe7e75167b9feee0050851d001aTed Kremenek}
6757