ASTContext.cpp revision ca63c200346c0ca9e00194ec6e34a5a7b0ed9321
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"
33071cc7deffad608165b1ddd5263e8bf181861520Charles Davis#include "CXXABI.h"
342636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis#include <map>
3529445a0542d128cd7ee587ee52229670b9b54a12Anders Carlsson
365f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencerusing namespace clang;
375f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
381827403a7138946305c0058f262e02b595cf882fDouglas Gregorunsigned ASTContext::NumImplicitDefaultConstructors;
391827403a7138946305c0058f262e02b595cf882fDouglas Gregorunsigned ASTContext::NumImplicitDefaultConstructorsDeclared;
40225843186e3972ce798d3de00f86da9008b15a0eDouglas Gregorunsigned ASTContext::NumImplicitCopyConstructors;
41225843186e3972ce798d3de00f86da9008b15a0eDouglas Gregorunsigned ASTContext::NumImplicitCopyConstructorsDeclared;
42a376d10acfacf19d6dfa41069f7929739a18dd7aDouglas Gregorunsigned ASTContext::NumImplicitCopyAssignmentOperators;
43a376d10acfacf19d6dfa41069f7929739a18dd7aDouglas Gregorunsigned ASTContext::NumImplicitCopyAssignmentOperatorsDeclared;
444923aa25eb39d64106a5817c02d560a3aecf8b2cDouglas Gregorunsigned ASTContext::NumImplicitDestructors;
454923aa25eb39d64106a5817c02d560a3aecf8b2cDouglas Gregorunsigned ASTContext::NumImplicitDestructorsDeclared;
464923aa25eb39d64106a5817c02d560a3aecf8b2cDouglas Gregor
475f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencerenum FloatingRank {
485f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  FloatRank, DoubleRank, LongDoubleRank
495f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer};
505f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
513e1274f2b99cb99c03cc8e2c6517c37d330b597aDouglas Gregorvoid
523e1274f2b99cb99c03cc8e2c6517c37d330b597aDouglas GregorASTContext::CanonicalTemplateTemplateParm::Profile(llvm::FoldingSetNodeID &ID,
533e1274f2b99cb99c03cc8e2c6517c37d330b597aDouglas Gregor                                               TemplateTemplateParmDecl *Parm) {
543e1274f2b99cb99c03cc8e2c6517c37d330b597aDouglas Gregor  ID.AddInteger(Parm->getDepth());
553e1274f2b99cb99c03cc8e2c6517c37d330b597aDouglas Gregor  ID.AddInteger(Parm->getPosition());
5661c4d28e36cd3f1be392cb77f07436d1fa6b0f9fDouglas Gregor  ID.AddBoolean(Parm->isParameterPack());
573e1274f2b99cb99c03cc8e2c6517c37d330b597aDouglas Gregor
583e1274f2b99cb99c03cc8e2c6517c37d330b597aDouglas Gregor  TemplateParameterList *Params = Parm->getTemplateParameters();
593e1274f2b99cb99c03cc8e2c6517c37d330b597aDouglas Gregor  ID.AddInteger(Params->size());
603e1274f2b99cb99c03cc8e2c6517c37d330b597aDouglas Gregor  for (TemplateParameterList::const_iterator P = Params->begin(),
613e1274f2b99cb99c03cc8e2c6517c37d330b597aDouglas Gregor                                          PEnd = Params->end();
623e1274f2b99cb99c03cc8e2c6517c37d330b597aDouglas Gregor       P != PEnd; ++P) {
633e1274f2b99cb99c03cc8e2c6517c37d330b597aDouglas Gregor    if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(*P)) {
643e1274f2b99cb99c03cc8e2c6517c37d330b597aDouglas Gregor      ID.AddInteger(0);
653e1274f2b99cb99c03cc8e2c6517c37d330b597aDouglas Gregor      ID.AddBoolean(TTP->isParameterPack());
663e1274f2b99cb99c03cc8e2c6517c37d330b597aDouglas Gregor      continue;
673e1274f2b99cb99c03cc8e2c6517c37d330b597aDouglas Gregor    }
683e1274f2b99cb99c03cc8e2c6517c37d330b597aDouglas Gregor
693e1274f2b99cb99c03cc8e2c6517c37d330b597aDouglas Gregor    if (NonTypeTemplateParmDecl *NTTP = dyn_cast<NonTypeTemplateParmDecl>(*P)) {
703e1274f2b99cb99c03cc8e2c6517c37d330b597aDouglas Gregor      ID.AddInteger(1);
7161c4d28e36cd3f1be392cb77f07436d1fa6b0f9fDouglas Gregor      ID.AddBoolean(NTTP->isParameterPack());
723e1274f2b99cb99c03cc8e2c6517c37d330b597aDouglas Gregor      ID.AddPointer(NTTP->getType().getAsOpaquePtr());
736952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor      if (NTTP->isExpandedParameterPack()) {
746952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor        ID.AddBoolean(true);
756952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor        ID.AddInteger(NTTP->getNumExpansionTypes());
766952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor        for (unsigned I = 0, N = NTTP->getNumExpansionTypes(); I != N; ++I)
776952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor          ID.AddPointer(NTTP->getExpansionType(I).getAsOpaquePtr());
786952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor      } else
796952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor        ID.AddBoolean(false);
803e1274f2b99cb99c03cc8e2c6517c37d330b597aDouglas Gregor      continue;
813e1274f2b99cb99c03cc8e2c6517c37d330b597aDouglas Gregor    }
823e1274f2b99cb99c03cc8e2c6517c37d330b597aDouglas Gregor
833e1274f2b99cb99c03cc8e2c6517c37d330b597aDouglas Gregor    TemplateTemplateParmDecl *TTP = cast<TemplateTemplateParmDecl>(*P);
843e1274f2b99cb99c03cc8e2c6517c37d330b597aDouglas Gregor    ID.AddInteger(2);
853e1274f2b99cb99c03cc8e2c6517c37d330b597aDouglas Gregor    Profile(ID, TTP);
863e1274f2b99cb99c03cc8e2c6517c37d330b597aDouglas Gregor  }
873e1274f2b99cb99c03cc8e2c6517c37d330b597aDouglas Gregor}
883e1274f2b99cb99c03cc8e2c6517c37d330b597aDouglas Gregor
893e1274f2b99cb99c03cc8e2c6517c37d330b597aDouglas GregorTemplateTemplateParmDecl *
903e1274f2b99cb99c03cc8e2c6517c37d330b597aDouglas GregorASTContext::getCanonicalTemplateTemplateParmDecl(
914ba2a17694148e16eaa8d3917f657ffcd3667be4Jay Foad                                          TemplateTemplateParmDecl *TTP) const {
923e1274f2b99cb99c03cc8e2c6517c37d330b597aDouglas Gregor  // Check if we already have a canonical template template parameter.
933e1274f2b99cb99c03cc8e2c6517c37d330b597aDouglas Gregor  llvm::FoldingSetNodeID ID;
943e1274f2b99cb99c03cc8e2c6517c37d330b597aDouglas Gregor  CanonicalTemplateTemplateParm::Profile(ID, TTP);
953e1274f2b99cb99c03cc8e2c6517c37d330b597aDouglas Gregor  void *InsertPos = 0;
963e1274f2b99cb99c03cc8e2c6517c37d330b597aDouglas Gregor  CanonicalTemplateTemplateParm *Canonical
973e1274f2b99cb99c03cc8e2c6517c37d330b597aDouglas Gregor    = CanonTemplateTemplateParms.FindNodeOrInsertPos(ID, InsertPos);
983e1274f2b99cb99c03cc8e2c6517c37d330b597aDouglas Gregor  if (Canonical)
993e1274f2b99cb99c03cc8e2c6517c37d330b597aDouglas Gregor    return Canonical->getParam();
1003e1274f2b99cb99c03cc8e2c6517c37d330b597aDouglas Gregor
1013e1274f2b99cb99c03cc8e2c6517c37d330b597aDouglas Gregor  // Build a canonical template parameter list.
1023e1274f2b99cb99c03cc8e2c6517c37d330b597aDouglas Gregor  TemplateParameterList *Params = TTP->getTemplateParameters();
1033e1274f2b99cb99c03cc8e2c6517c37d330b597aDouglas Gregor  llvm::SmallVector<NamedDecl *, 4> CanonParams;
1043e1274f2b99cb99c03cc8e2c6517c37d330b597aDouglas Gregor  CanonParams.reserve(Params->size());
1053e1274f2b99cb99c03cc8e2c6517c37d330b597aDouglas Gregor  for (TemplateParameterList::const_iterator P = Params->begin(),
1063e1274f2b99cb99c03cc8e2c6517c37d330b597aDouglas Gregor                                          PEnd = Params->end();
1073e1274f2b99cb99c03cc8e2c6517c37d330b597aDouglas Gregor       P != PEnd; ++P) {
1083e1274f2b99cb99c03cc8e2c6517c37d330b597aDouglas Gregor    if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(*P))
1093e1274f2b99cb99c03cc8e2c6517c37d330b597aDouglas Gregor      CanonParams.push_back(
1103e1274f2b99cb99c03cc8e2c6517c37d330b597aDouglas Gregor                  TemplateTypeParmDecl::Create(*this, getTranslationUnitDecl(),
111344577e6b58f42d18dc8118c8903b49a85dc005eAbramo Bagnara                                               SourceLocation(),
112344577e6b58f42d18dc8118c8903b49a85dc005eAbramo Bagnara                                               SourceLocation(),
113344577e6b58f42d18dc8118c8903b49a85dc005eAbramo Bagnara                                               TTP->getDepth(),
1143e1274f2b99cb99c03cc8e2c6517c37d330b597aDouglas Gregor                                               TTP->getIndex(), 0, false,
1153e1274f2b99cb99c03cc8e2c6517c37d330b597aDouglas Gregor                                               TTP->isParameterPack()));
1163e1274f2b99cb99c03cc8e2c6517c37d330b597aDouglas Gregor    else if (NonTypeTemplateParmDecl *NTTP
1176952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor             = dyn_cast<NonTypeTemplateParmDecl>(*P)) {
1186952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor      QualType T = getCanonicalType(NTTP->getType());
1196952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor      TypeSourceInfo *TInfo = getTrivialTypeSourceInfo(T);
1206952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor      NonTypeTemplateParmDecl *Param;
1216952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor      if (NTTP->isExpandedParameterPack()) {
1226952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor        llvm::SmallVector<QualType, 2> ExpandedTypes;
1236952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor        llvm::SmallVector<TypeSourceInfo *, 2> ExpandedTInfos;
1246952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor        for (unsigned I = 0, N = NTTP->getNumExpansionTypes(); I != N; ++I) {
1256952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor          ExpandedTypes.push_back(getCanonicalType(NTTP->getExpansionType(I)));
1266952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor          ExpandedTInfos.push_back(
1276952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor                                getTrivialTypeSourceInfo(ExpandedTypes.back()));
1286952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor        }
1296952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor
1306952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor        Param = NonTypeTemplateParmDecl::Create(*this, getTranslationUnitDecl(),
131ff676cb48fe8bf7be2feaa251dc7c5fb15af4730Abramo Bagnara                                                SourceLocation(),
132ff676cb48fe8bf7be2feaa251dc7c5fb15af4730Abramo Bagnara                                                SourceLocation(),
1336952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor                                                NTTP->getDepth(),
1346952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor                                                NTTP->getPosition(), 0,
1356952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor                                                T,
1366952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor                                                TInfo,
1376952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor                                                ExpandedTypes.data(),
1386952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor                                                ExpandedTypes.size(),
1396952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor                                                ExpandedTInfos.data());
1406952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor      } else {
1416952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor        Param = NonTypeTemplateParmDecl::Create(*this, getTranslationUnitDecl(),
142ff676cb48fe8bf7be2feaa251dc7c5fb15af4730Abramo Bagnara                                                SourceLocation(),
143ff676cb48fe8bf7be2feaa251dc7c5fb15af4730Abramo Bagnara                                                SourceLocation(),
1446952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor                                                NTTP->getDepth(),
1456952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor                                                NTTP->getPosition(), 0,
1466952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor                                                T,
1476952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor                                                NTTP->isParameterPack(),
1486952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor                                                TInfo);
1496952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor      }
1506952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor      CanonParams.push_back(Param);
1516952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor
1526952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor    } else
1533e1274f2b99cb99c03cc8e2c6517c37d330b597aDouglas Gregor      CanonParams.push_back(getCanonicalTemplateTemplateParmDecl(
1543e1274f2b99cb99c03cc8e2c6517c37d330b597aDouglas Gregor                                           cast<TemplateTemplateParmDecl>(*P)));
1553e1274f2b99cb99c03cc8e2c6517c37d330b597aDouglas Gregor  }
1563e1274f2b99cb99c03cc8e2c6517c37d330b597aDouglas Gregor
1573e1274f2b99cb99c03cc8e2c6517c37d330b597aDouglas Gregor  TemplateTemplateParmDecl *CanonTTP
1583e1274f2b99cb99c03cc8e2c6517c37d330b597aDouglas Gregor    = TemplateTemplateParmDecl::Create(*this, getTranslationUnitDecl(),
1593e1274f2b99cb99c03cc8e2c6517c37d330b597aDouglas Gregor                                       SourceLocation(), TTP->getDepth(),
16061c4d28e36cd3f1be392cb77f07436d1fa6b0f9fDouglas Gregor                                       TTP->getPosition(),
16161c4d28e36cd3f1be392cb77f07436d1fa6b0f9fDouglas Gregor                                       TTP->isParameterPack(),
16261c4d28e36cd3f1be392cb77f07436d1fa6b0f9fDouglas Gregor                                       0,
1633e1274f2b99cb99c03cc8e2c6517c37d330b597aDouglas Gregor                         TemplateParameterList::Create(*this, SourceLocation(),
1643e1274f2b99cb99c03cc8e2c6517c37d330b597aDouglas Gregor                                                       SourceLocation(),
1653e1274f2b99cb99c03cc8e2c6517c37d330b597aDouglas Gregor                                                       CanonParams.data(),
1663e1274f2b99cb99c03cc8e2c6517c37d330b597aDouglas Gregor                                                       CanonParams.size(),
1673e1274f2b99cb99c03cc8e2c6517c37d330b597aDouglas Gregor                                                       SourceLocation()));
1683e1274f2b99cb99c03cc8e2c6517c37d330b597aDouglas Gregor
1693e1274f2b99cb99c03cc8e2c6517c37d330b597aDouglas Gregor  // Get the new insert position for the node we care about.
1703e1274f2b99cb99c03cc8e2c6517c37d330b597aDouglas Gregor  Canonical = CanonTemplateTemplateParms.FindNodeOrInsertPos(ID, InsertPos);
1713e1274f2b99cb99c03cc8e2c6517c37d330b597aDouglas Gregor  assert(Canonical == 0 && "Shouldn't be in the map!");
1723e1274f2b99cb99c03cc8e2c6517c37d330b597aDouglas Gregor  (void)Canonical;
1733e1274f2b99cb99c03cc8e2c6517c37d330b597aDouglas Gregor
1743e1274f2b99cb99c03cc8e2c6517c37d330b597aDouglas Gregor  // Create the canonical template template parameter entry.
1753e1274f2b99cb99c03cc8e2c6517c37d330b597aDouglas Gregor  Canonical = new (*this) CanonicalTemplateTemplateParm(CanonTTP);
1763e1274f2b99cb99c03cc8e2c6517c37d330b597aDouglas Gregor  CanonTemplateTemplateParms.InsertNode(Canonical, InsertPos);
1773e1274f2b99cb99c03cc8e2c6517c37d330b597aDouglas Gregor  return CanonTTP;
1783e1274f2b99cb99c03cc8e2c6517c37d330b597aDouglas Gregor}
1793e1274f2b99cb99c03cc8e2c6517c37d330b597aDouglas Gregor
180071cc7deffad608165b1ddd5263e8bf181861520Charles DavisCXXABI *ASTContext::createCXXABI(const TargetInfo &T) {
181ee79a4c30e5d1c5285551c9a25b8ec6d45d46aa7John McCall  if (!LangOpts.CPlusPlus) return 0;
182ee79a4c30e5d1c5285551c9a25b8ec6d45d46aa7John McCall
18320cf717034ba1f20fc47c025ecb72ed9b631ad13Charles Davis  switch (T.getCXXABI()) {
184ee79a4c30e5d1c5285551c9a25b8ec6d45d46aa7John McCall  case CXXABI_ARM:
185ee79a4c30e5d1c5285551c9a25b8ec6d45d46aa7John McCall    return CreateARMCXXABI(*this);
186ee79a4c30e5d1c5285551c9a25b8ec6d45d46aa7John McCall  case CXXABI_Itanium:
187071cc7deffad608165b1ddd5263e8bf181861520Charles Davis    return CreateItaniumCXXABI(*this);
18820cf717034ba1f20fc47c025ecb72ed9b631ad13Charles Davis  case CXXABI_Microsoft:
18920cf717034ba1f20fc47c025ecb72ed9b631ad13Charles Davis    return CreateMicrosoftCXXABI(*this);
19020cf717034ba1f20fc47c025ecb72ed9b631ad13Charles Davis  }
191ee79a4c30e5d1c5285551c9a25b8ec6d45d46aa7John McCall  return 0;
192071cc7deffad608165b1ddd5263e8bf181861520Charles Davis}
193071cc7deffad608165b1ddd5263e8bf181861520Charles Davis
194207f4d8543529221932af82836016a2ef066c917Peter Collingbournestatic const LangAS::Map &getAddressSpaceMap(const TargetInfo &T,
195207f4d8543529221932af82836016a2ef066c917Peter Collingbourne                                             const LangOptions &LOpts) {
196207f4d8543529221932af82836016a2ef066c917Peter Collingbourne  if (LOpts.FakeAddressSpaceMap) {
197207f4d8543529221932af82836016a2ef066c917Peter Collingbourne    // The fake address space map must have a distinct entry for each
198207f4d8543529221932af82836016a2ef066c917Peter Collingbourne    // language-specific address space.
199207f4d8543529221932af82836016a2ef066c917Peter Collingbourne    static const unsigned FakeAddrSpaceMap[] = {
200207f4d8543529221932af82836016a2ef066c917Peter Collingbourne      1, // opencl_global
201207f4d8543529221932af82836016a2ef066c917Peter Collingbourne      2, // opencl_local
202207f4d8543529221932af82836016a2ef066c917Peter Collingbourne      3  // opencl_constant
203207f4d8543529221932af82836016a2ef066c917Peter Collingbourne    };
204207f4d8543529221932af82836016a2ef066c917Peter Collingbourne    return FakeAddrSpaceMap;
205207f4d8543529221932af82836016a2ef066c917Peter Collingbourne  } else {
206207f4d8543529221932af82836016a2ef066c917Peter Collingbourne    return T.getAddressSpaceMap();
207207f4d8543529221932af82836016a2ef066c917Peter Collingbourne  }
208207f4d8543529221932af82836016a2ef066c917Peter Collingbourne}
209207f4d8543529221932af82836016a2ef066c917Peter Collingbourne
21061710854be2b098428aff5316e64bd34b30fbcb7Chris LattnerASTContext::ASTContext(const LangOptions& LOpts, SourceManager &SM,
211444be7366d0a1e172c0290a1ea54c1cb16b5947cDaniel Dunbar                       const TargetInfo &t,
212e91593ef084479340582b2ba177b44be50a717b7Daniel Dunbar                       IdentifierTable &idents, SelectorTable &sels,
2131b63e4f732dbc73d90abf886b4d21f8e3a165f6dChris Lattner                       Builtin::Context &builtins,
21463fe86bee66fc145942c56b2cc564ea0b9b9ea12Douglas Gregor                       unsigned size_reserve) :
2158026f6d82f7fa544bc0453714fe94bca62a1196eSebastian Redl  FunctionProtoTypes(this_()),
216ef99001908e799c388f1363b1e607dad5f5b57d3John McCall  TemplateSpecializationTypes(this_()),
217ef99001908e799c388f1363b1e607dad5f5b57d3John McCall  DependentTemplateSpecializationTypes(this_()),
218006113841bdae1edb77aef75ba1ffdf2e55a3094Argyrios Kyrtzidis  GlobalNestedNameSpecifier(0), IsInt128Installed(false),
219006113841bdae1edb77aef75ba1ffdf2e55a3094Argyrios Kyrtzidis  CFConstantStringTypeDecl(0), NSConstantStringTypeDecl(0),
220782fa308a765aeac2acb39c4e697c937ec21185bMike Stump  ObjCFastEnumerationStateTypeDecl(0), FILEDecl(0), jmp_bufDecl(0),
221083c25eea14bb4cc4ecc3ec763c60e2e609e22bdMike Stump  sigjmp_bufDecl(0), BlockDescriptorType(0), BlockDescriptorExtendedType(0),
22214b6ba77710d6431794d65c7d58c6f29c3dc956ePeter Collingbourne  cudaConfigureCallDecl(0),
223bf1a028246d884a540aeafa38e89be59a269b072John McCall  NullTypeSourceInfo(QualType()),
224207f4d8543529221932af82836016a2ef066c917Peter Collingbourne  SourceMgr(SM), LangOpts(LOpts), ABI(createCXXABI(t)),
225207f4d8543529221932af82836016a2ef066c917Peter Collingbourne  AddrSpaceMap(getAddressSpaceMap(t, LOpts)), Target(t),
2262e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor  Idents(idents), Selectors(sels),
227ac9590effa90406767a544005ed1de52e258306bTed Kremenek  BuiltinInfo(builtins),
228ac9590effa90406767a544005ed1de52e258306bTed Kremenek  DeclarationNames(*this),
2299a44b5f5593bd8a385d06b07c564795f643ec8daArgyrios Kyrtzidis  ExternalSource(0), Listener(0), PrintingPolicy(LOpts),
230f057bf78a69d37ee511430c88852a85eea19ad15Ted Kremenek  LastSDM(0, 0),
2318178df3b39ab923ff5d24538812628abee33df79John McCall  UniqueBlockByRefTypeID(0) {
2320f436560640a1cff5b6d96f80f540770f139453fDavid Chisnall  ObjCIdRedefinitionType = QualType();
2330f436560640a1cff5b6d96f80f540770f139453fDavid Chisnall  ObjCClassRedefinitionType = QualType();
23463fe86bee66fc145942c56b2cc564ea0b9b9ea12Douglas Gregor  ObjCSelRedefinitionType = QualType();
2351eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  if (size_reserve > 0) Types.reserve(size_reserve);
236e91593ef084479340582b2ba177b44be50a717b7Daniel Dunbar  TUDecl = TranslationUnitDecl::Create(*this);
23714108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff  InitBuiltinTypes();
238e91593ef084479340582b2ba177b44be50a717b7Daniel Dunbar}
239e91593ef084479340582b2ba177b44be50a717b7Daniel Dunbar
2405f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid SpencerASTContext::~ASTContext() {
2413478eb6872d836600caf45b0f81c2065d685d6e0Ted Kremenek  // Release the DenseMaps associated with DeclContext objects.
2423478eb6872d836600caf45b0f81c2065d685d6e0Ted Kremenek  // FIXME: Is this the ideal solution?
2433478eb6872d836600caf45b0f81c2065d685d6e0Ted Kremenek  ReleaseDeclContextMaps();
2447d10b7eb670b821741b4c96f6cf7afbc3bb39abeDouglas Gregor
24563fe86bee66fc145942c56b2cc564ea0b9b9ea12Douglas Gregor  // Call all of the deallocation functions.
24663fe86bee66fc145942c56b2cc564ea0b9b9ea12Douglas Gregor  for (unsigned I = 0, N = Deallocations.size(); I != N; ++I)
24763fe86bee66fc145942c56b2cc564ea0b9b9ea12Douglas Gregor    Deallocations[I].first(Deallocations[I].second);
2480054531488928a424666ac11fcdc6bcc5112de52Douglas Gregor
2497d10b7eb670b821741b4c96f6cf7afbc3bb39abeDouglas Gregor  // Release all of the memory associated with overridden C++ methods.
2507d10b7eb670b821741b4c96f6cf7afbc3bb39abeDouglas Gregor  for (llvm::DenseMap<const CXXMethodDecl *, CXXMethodVector>::iterator
2517d10b7eb670b821741b4c96f6cf7afbc3bb39abeDouglas Gregor         OM = OverriddenMethods.begin(), OMEnd = OverriddenMethods.end();
2527d10b7eb670b821741b4c96f6cf7afbc3bb39abeDouglas Gregor       OM != OMEnd; ++OM)
2537d10b7eb670b821741b4c96f6cf7afbc3bb39abeDouglas Gregor    OM->second.Destroy();
2543478eb6872d836600caf45b0f81c2065d685d6e0Ted Kremenek
255dcfcfbec478f7ed96cd8d92f30c29bd4e30d5b9cTed Kremenek  // ASTRecordLayout objects in ASTRecordLayouts must always be destroyed
25663fe86bee66fc145942c56b2cc564ea0b9b9ea12Douglas Gregor  // because they can contain DenseMaps.
25763fe86bee66fc145942c56b2cc564ea0b9b9ea12Douglas Gregor  for (llvm::DenseMap<const ObjCContainerDecl*,
25863fe86bee66fc145942c56b2cc564ea0b9b9ea12Douglas Gregor       const ASTRecordLayout*>::iterator
25963fe86bee66fc145942c56b2cc564ea0b9b9ea12Douglas Gregor       I = ObjCLayouts.begin(), E = ObjCLayouts.end(); I != E; )
26063fe86bee66fc145942c56b2cc564ea0b9b9ea12Douglas Gregor    // Increment in loop to prevent using deallocated memory.
26163fe86bee66fc145942c56b2cc564ea0b9b9ea12Douglas Gregor    if (ASTRecordLayout *R = const_cast<ASTRecordLayout*>((I++)->second))
26263fe86bee66fc145942c56b2cc564ea0b9b9ea12Douglas Gregor      R->Destroy(*this);
26363fe86bee66fc145942c56b2cc564ea0b9b9ea12Douglas Gregor
264dcfcfbec478f7ed96cd8d92f30c29bd4e30d5b9cTed Kremenek  for (llvm::DenseMap<const RecordDecl*, const ASTRecordLayout*>::iterator
265dcfcfbec478f7ed96cd8d92f30c29bd4e30d5b9cTed Kremenek       I = ASTRecordLayouts.begin(), E = ASTRecordLayouts.end(); I != E; ) {
266dcfcfbec478f7ed96cd8d92f30c29bd4e30d5b9cTed Kremenek    // Increment in loop to prevent using deallocated memory.
267dcfcfbec478f7ed96cd8d92f30c29bd4e30d5b9cTed Kremenek    if (ASTRecordLayout *R = const_cast<ASTRecordLayout*>((I++)->second))
268dcfcfbec478f7ed96cd8d92f30c29bd4e30d5b9cTed Kremenek      R->Destroy(*this);
269dcfcfbec478f7ed96cd8d92f30c29bd4e30d5b9cTed Kremenek  }
2706320064d0c60fa8683f5623881c9394fd4aa7689Douglas Gregor
2716320064d0c60fa8683f5623881c9394fd4aa7689Douglas Gregor  for (llvm::DenseMap<const Decl*, AttrVec*>::iterator A = DeclAttrs.begin(),
2726320064d0c60fa8683f5623881c9394fd4aa7689Douglas Gregor                                                    AEnd = DeclAttrs.end();
2736320064d0c60fa8683f5623881c9394fd4aa7689Douglas Gregor       A != AEnd; ++A)
2746320064d0c60fa8683f5623881c9394fd4aa7689Douglas Gregor    A->second->~AttrVec();
2756320064d0c60fa8683f5623881c9394fd4aa7689Douglas Gregor}
276ab452ba8323d1985e08bade2bced588cddf2cc28Douglas Gregor
2770054531488928a424666ac11fcdc6bcc5112de52Douglas Gregorvoid ASTContext::AddDeallocation(void (*Callback)(void*), void *Data) {
2780054531488928a424666ac11fcdc6bcc5112de52Douglas Gregor  Deallocations.push_back(std::make_pair(Callback, Data));
2790054531488928a424666ac11fcdc6bcc5112de52Douglas Gregor}
2800054531488928a424666ac11fcdc6bcc5112de52Douglas Gregor
2811eb4433ac451dc16f4133a88af2d002ac26c58efMike Stumpvoid
2822cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas GregorASTContext::setExternalSource(llvm::OwningPtr<ExternalASTSource> &Source) {
2832cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor  ExternalSource.reset(Source.take());
2842cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor}
2852cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor
2865f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencervoid ASTContext::PrintStats() const {
2875f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  fprintf(stderr, "*** AST Context Stats:\n");
2885f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  fprintf(stderr, "  %d types total.\n", (int)Types.size());
2897c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl
290dbe833da54e1b6192991b64fc453cd50b5ee7909Douglas Gregor  unsigned counts[] = {
2911eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump#define TYPE(Name, Parent) 0,
292dbe833da54e1b6192991b64fc453cd50b5ee7909Douglas Gregor#define ABSTRACT_TYPE(Name, Parent)
293dbe833da54e1b6192991b64fc453cd50b5ee7909Douglas Gregor#include "clang/AST/TypeNodes.def"
294dbe833da54e1b6192991b64fc453cd50b5ee7909Douglas Gregor    0 // Extra
295dbe833da54e1b6192991b64fc453cd50b5ee7909Douglas Gregor  };
296c2ee10d79f70036af652a395ac1f8273f3d04e12Douglas Gregor
2975f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  for (unsigned i = 0, e = Types.size(); i != e; ++i) {
2985f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    Type *T = Types[i];
299dbe833da54e1b6192991b64fc453cd50b5ee7909Douglas Gregor    counts[(unsigned)T->getTypeClass()]++;
3005f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
3015f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
302dbe833da54e1b6192991b64fc453cd50b5ee7909Douglas Gregor  unsigned Idx = 0;
303dbe833da54e1b6192991b64fc453cd50b5ee7909Douglas Gregor  unsigned TotalBytes = 0;
304dbe833da54e1b6192991b64fc453cd50b5ee7909Douglas Gregor#define TYPE(Name, Parent)                                              \
305dbe833da54e1b6192991b64fc453cd50b5ee7909Douglas Gregor  if (counts[Idx])                                                      \
306dbe833da54e1b6192991b64fc453cd50b5ee7909Douglas Gregor    fprintf(stderr, "    %d %s types\n", (int)counts[Idx], #Name);      \
307dbe833da54e1b6192991b64fc453cd50b5ee7909Douglas Gregor  TotalBytes += counts[Idx] * sizeof(Name##Type);                       \
308dbe833da54e1b6192991b64fc453cd50b5ee7909Douglas Gregor  ++Idx;
309dbe833da54e1b6192991b64fc453cd50b5ee7909Douglas Gregor#define ABSTRACT_TYPE(Name, Parent)
310dbe833da54e1b6192991b64fc453cd50b5ee7909Douglas Gregor#include "clang/AST/TypeNodes.def"
3111eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
312dbe833da54e1b6192991b64fc453cd50b5ee7909Douglas Gregor  fprintf(stderr, "Total bytes = %d\n", int(TotalBytes));
313ed8abf18329df67b0abcbb3a10458bd8c1d2a595Douglas Gregor
3144923aa25eb39d64106a5817c02d560a3aecf8b2cDouglas Gregor  // Implicit special member functions.
3151827403a7138946305c0058f262e02b595cf882fDouglas Gregor  fprintf(stderr, "  %u/%u implicit default constructors created\n",
3161827403a7138946305c0058f262e02b595cf882fDouglas Gregor          NumImplicitDefaultConstructorsDeclared,
3171827403a7138946305c0058f262e02b595cf882fDouglas Gregor          NumImplicitDefaultConstructors);
318225843186e3972ce798d3de00f86da9008b15a0eDouglas Gregor  fprintf(stderr, "  %u/%u implicit copy constructors created\n",
319225843186e3972ce798d3de00f86da9008b15a0eDouglas Gregor          NumImplicitCopyConstructorsDeclared,
320225843186e3972ce798d3de00f86da9008b15a0eDouglas Gregor          NumImplicitCopyConstructors);
321a376d10acfacf19d6dfa41069f7929739a18dd7aDouglas Gregor  fprintf(stderr, "  %u/%u implicit copy assignment operators created\n",
322a376d10acfacf19d6dfa41069f7929739a18dd7aDouglas Gregor          NumImplicitCopyAssignmentOperatorsDeclared,
323a376d10acfacf19d6dfa41069f7929739a18dd7aDouglas Gregor          NumImplicitCopyAssignmentOperators);
3244923aa25eb39d64106a5817c02d560a3aecf8b2cDouglas Gregor  fprintf(stderr, "  %u/%u implicit destructors created\n",
3254923aa25eb39d64106a5817c02d560a3aecf8b2cDouglas Gregor          NumImplicitDestructorsDeclared, NumImplicitDestructors);
3264923aa25eb39d64106a5817c02d560a3aecf8b2cDouglas Gregor
3272cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor  if (ExternalSource.get()) {
3282cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor    fprintf(stderr, "\n");
3292cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor    ExternalSource->PrintStats();
3302cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor  }
331ed8abf18329df67b0abcbb3a10458bd8c1d2a595Douglas Gregor
33263fe86bee66fc145942c56b2cc564ea0b9b9ea12Douglas Gregor  BumpAlloc.PrintStats();
3335f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
3345f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
3355f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
336e27ec8ad56dbf1efb2de004b90fbbb86f740e3f1John McCallvoid ASTContext::InitBuiltinType(CanQualType &R, BuiltinType::Kind K) {
3376b304a0254a13f42390b865ff5ba668a49cc58aeJohn McCall  BuiltinType *Ty = new (*this, TypeAlignment) BuiltinType(K);
338e27ec8ad56dbf1efb2de004b90fbbb86f740e3f1John McCall  R = CanQualType::CreateUnsafe(QualType(Ty, 0));
3396b304a0254a13f42390b865ff5ba668a49cc58aeJohn McCall  Types.push_back(Ty);
3405f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
3415f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
3425f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencervoid ASTContext::InitBuiltinTypes() {
3435f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  assert(VoidTy.isNull() && "Context reinitialized?");
3441eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
3455f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // C99 6.2.5p19.
3465f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  InitBuiltinType(VoidTy,              BuiltinType::Void);
3471eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
3485f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // C99 6.2.5p2.
3495f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  InitBuiltinType(BoolTy,              BuiltinType::Bool);
3505f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // C99 6.2.5p3.
35115b91764d08e886391c865c4a444d7b51141c284Eli Friedman  if (LangOpts.CharIsSigned)
3525f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    InitBuiltinType(CharTy,            BuiltinType::Char_S);
3535f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  else
3545f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    InitBuiltinType(CharTy,            BuiltinType::Char_U);
3555f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // C99 6.2.5p4.
3565f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  InitBuiltinType(SignedCharTy,        BuiltinType::SChar);
3575f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  InitBuiltinType(ShortTy,             BuiltinType::Short);
3585f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  InitBuiltinType(IntTy,               BuiltinType::Int);
3595f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  InitBuiltinType(LongTy,              BuiltinType::Long);
3605f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  InitBuiltinType(LongLongTy,          BuiltinType::LongLong);
3611eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
3625f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // C99 6.2.5p6.
3635f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  InitBuiltinType(UnsignedCharTy,      BuiltinType::UChar);
3645f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  InitBuiltinType(UnsignedShortTy,     BuiltinType::UShort);
3655f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  InitBuiltinType(UnsignedIntTy,       BuiltinType::UInt);
3665f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  InitBuiltinType(UnsignedLongTy,      BuiltinType::ULong);
3675f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  InitBuiltinType(UnsignedLongLongTy,  BuiltinType::ULongLong);
3681eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
3695f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // C99 6.2.5p10.
3705f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  InitBuiltinType(FloatTy,             BuiltinType::Float);
3715f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  InitBuiltinType(DoubleTy,            BuiltinType::Double);
3725f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  InitBuiltinType(LongDoubleTy,        BuiltinType::LongDouble);
37364c438a4be2a871fa43c78264663ba1e9788b94dArgyrios Kyrtzidis
3742df9ced9fd1e8c7d7b38443db07e0e811de22571Chris Lattner  // GNU extension, 128-bit integers.
3752df9ced9fd1e8c7d7b38443db07e0e811de22571Chris Lattner  InitBuiltinType(Int128Ty,            BuiltinType::Int128);
3762df9ced9fd1e8c7d7b38443db07e0e811de22571Chris Lattner  InitBuiltinType(UnsignedInt128Ty,    BuiltinType::UInt128);
3772df9ced9fd1e8c7d7b38443db07e0e811de22571Chris Lattner
3783f59c975aa5d047f7edd1b900b5e885c38af0ef7Chris Lattner  if (LangOpts.CPlusPlus) { // C++ 3.9.1p5
379d3d77cd138f8e830f6547b6f83fcd5721ccf5f5dEli Friedman    if (TargetInfo::isTypeSigned(Target.getWCharType()))
3803f59c975aa5d047f7edd1b900b5e885c38af0ef7Chris Lattner      InitBuiltinType(WCharTy,           BuiltinType::WChar_S);
3813f59c975aa5d047f7edd1b900b5e885c38af0ef7Chris Lattner    else  // -fshort-wchar makes wchar_t be unsigned.
3823f59c975aa5d047f7edd1b900b5e885c38af0ef7Chris Lattner      InitBuiltinType(WCharTy,           BuiltinType::WChar_U);
3833f59c975aa5d047f7edd1b900b5e885c38af0ef7Chris Lattner  } else // C99
3843a2503227c3db04a3619735127483263c1075ef7Chris Lattner    WCharTy = getFromTargetType(Target.getWCharType());
38564c438a4be2a871fa43c78264663ba1e9788b94dArgyrios Kyrtzidis
386f5c209d23b20ada4a9b6235db50317239cbf6ae1Alisdair Meredith  if (LangOpts.CPlusPlus) // C++0x 3.9.1p5, extension for C++
387f5c209d23b20ada4a9b6235db50317239cbf6ae1Alisdair Meredith    InitBuiltinType(Char16Ty,           BuiltinType::Char16);
388f5c209d23b20ada4a9b6235db50317239cbf6ae1Alisdair Meredith  else // C99
389f5c209d23b20ada4a9b6235db50317239cbf6ae1Alisdair Meredith    Char16Ty = getFromTargetType(Target.getChar16Type());
390f5c209d23b20ada4a9b6235db50317239cbf6ae1Alisdair Meredith
391f5c209d23b20ada4a9b6235db50317239cbf6ae1Alisdair Meredith  if (LangOpts.CPlusPlus) // C++0x 3.9.1p5, extension for C++
392f5c209d23b20ada4a9b6235db50317239cbf6ae1Alisdair Meredith    InitBuiltinType(Char32Ty,           BuiltinType::Char32);
393f5c209d23b20ada4a9b6235db50317239cbf6ae1Alisdair Meredith  else // C99
394f5c209d23b20ada4a9b6235db50317239cbf6ae1Alisdair Meredith    Char32Ty = getFromTargetType(Target.getChar32Type());
395f5c209d23b20ada4a9b6235db50317239cbf6ae1Alisdair Meredith
396898574e7496ba8fd76290079d3a9d06954992734Douglas Gregor  // Placeholder type for type-dependent expressions whose type is
397898574e7496ba8fd76290079d3a9d06954992734Douglas Gregor  // completely unknown. No code should ever check a type against
398898574e7496ba8fd76290079d3a9d06954992734Douglas Gregor  // DependentTy and users should never see it; however, it is here to
399898574e7496ba8fd76290079d3a9d06954992734Douglas Gregor  // help diagnose failures to properly check for type-dependent
400898574e7496ba8fd76290079d3a9d06954992734Douglas Gregor  // expressions.
401898574e7496ba8fd76290079d3a9d06954992734Douglas Gregor  InitBuiltinType(DependentTy,         BuiltinType::Dependent);
4028e9bebdea69c590dedfbf27374114cb76fe12fbdDouglas Gregor
4032a984cad5ac3fdceeff2bd99daa7b90979313475John McCall  // Placeholder type for functions.
4042a984cad5ac3fdceeff2bd99daa7b90979313475John McCall  InitBuiltinType(OverloadTy,          BuiltinType::Overload);
4052a984cad5ac3fdceeff2bd99daa7b90979313475John McCall
406864c041e118155c2b1ce0ba36942a3da5a4a055eJohn McCall  // Placeholder type for bound members.
407864c041e118155c2b1ce0ba36942a3da5a4a055eJohn McCall  InitBuiltinType(BoundMemberTy,       BuiltinType::BoundMember);
408864c041e118155c2b1ce0ba36942a3da5a4a055eJohn McCall
4091de4d4e8cb2e9c88809fea8092bc6e835a5473d2John McCall  // "any" type; useful for debugger-like clients.
4101de4d4e8cb2e9c88809fea8092bc6e835a5473d2John McCall  InitBuiltinType(UnknownAnyTy,        BuiltinType::UnknownAny);
4111de4d4e8cb2e9c88809fea8092bc6e835a5473d2John McCall
4125f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // C99 6.2.5p11.
4135f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  FloatComplexTy      = getComplexType(FloatTy);
4145f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  DoubleComplexTy     = getComplexType(DoubleTy);
4155f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  LongDoubleComplexTy = getComplexType(LongDoubleTy);
4168e9bebdea69c590dedfbf27374114cb76fe12fbdDouglas Gregor
4177e219e47de26346885d667131977bd9ca2d7662aSteve Naroff  BuiltinVaListType = QualType();
4181eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
419de2e22d33afec98324a66a358dfe0951b3c7259aSteve Naroff  // "Builtin" typedefs set by Sema::ActOnTranslationUnitScope().
420de2e22d33afec98324a66a358dfe0951b3c7259aSteve Naroff  ObjCIdTypedefType = QualType();
421de2e22d33afec98324a66a358dfe0951b3c7259aSteve Naroff  ObjCClassTypedefType = QualType();
42213dcd00615de5c4279d97bdf63cd5f0a14fd9dccFariborz Jahanian  ObjCSelTypedefType = QualType();
4231eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
42413dcd00615de5c4279d97bdf63cd5f0a14fd9dccFariborz Jahanian  // Builtin types for 'id', 'Class', and 'SEL'.
425de2e22d33afec98324a66a358dfe0951b3c7259aSteve Naroff  InitBuiltinType(ObjCBuiltinIdTy, BuiltinType::ObjCId);
426de2e22d33afec98324a66a358dfe0951b3c7259aSteve Naroff  InitBuiltinType(ObjCBuiltinClassTy, BuiltinType::ObjCClass);
42713dcd00615de5c4279d97bdf63cd5f0a14fd9dccFariborz Jahanian  InitBuiltinType(ObjCBuiltinSelTy, BuiltinType::ObjCSel);
42814108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff
429a526c5c67e5a0473c340903ee542ce570119665fTed Kremenek  ObjCConstantStringType = QualType();
4301eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
43133e1d64ab5cd5d27f8530ccd056191fe2c9f3f2eFariborz Jahanian  // void * type
43233e1d64ab5cd5d27f8530ccd056191fe2c9f3f2eFariborz Jahanian  VoidPtrTy = getPointerType(VoidTy);
4336e8ed16ffef02b82995a90bdcf10ffff7d63839aSebastian Redl
4346e8ed16ffef02b82995a90bdcf10ffff7d63839aSebastian Redl  // nullptr type (C++0x 2.14.7)
4356e8ed16ffef02b82995a90bdcf10ffff7d63839aSebastian Redl  InitBuiltinType(NullPtrTy,           BuiltinType::NullPtr);
4365f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
4375f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
43878a916ec5ff5b66adec3c499e1b9af7b87668309Argyrios KyrtzidisDiagnostic &ASTContext::getDiagnostics() const {
43978a916ec5ff5b66adec3c499e1b9af7b87668309Argyrios Kyrtzidis  return SourceMgr.getDiagnostics();
44078a916ec5ff5b66adec3c499e1b9af7b87668309Argyrios Kyrtzidis}
44178a916ec5ff5b66adec3c499e1b9af7b87668309Argyrios Kyrtzidis
4426320064d0c60fa8683f5623881c9394fd4aa7689Douglas GregorAttrVec& ASTContext::getDeclAttrs(const Decl *D) {
4436320064d0c60fa8683f5623881c9394fd4aa7689Douglas Gregor  AttrVec *&Result = DeclAttrs[D];
4446320064d0c60fa8683f5623881c9394fd4aa7689Douglas Gregor  if (!Result) {
4456320064d0c60fa8683f5623881c9394fd4aa7689Douglas Gregor    void *Mem = Allocate(sizeof(AttrVec));
4466320064d0c60fa8683f5623881c9394fd4aa7689Douglas Gregor    Result = new (Mem) AttrVec;
4476320064d0c60fa8683f5623881c9394fd4aa7689Douglas Gregor  }
4486320064d0c60fa8683f5623881c9394fd4aa7689Douglas Gregor
4496320064d0c60fa8683f5623881c9394fd4aa7689Douglas Gregor  return *Result;
4506320064d0c60fa8683f5623881c9394fd4aa7689Douglas Gregor}
4516320064d0c60fa8683f5623881c9394fd4aa7689Douglas Gregor
4526320064d0c60fa8683f5623881c9394fd4aa7689Douglas Gregor/// \brief Erase the attributes corresponding to the given declaration.
4536320064d0c60fa8683f5623881c9394fd4aa7689Douglas Gregorvoid ASTContext::eraseDeclAttrs(const Decl *D) {
4546320064d0c60fa8683f5623881c9394fd4aa7689Douglas Gregor  llvm::DenseMap<const Decl*, AttrVec*>::iterator Pos = DeclAttrs.find(D);
4556320064d0c60fa8683f5623881c9394fd4aa7689Douglas Gregor  if (Pos != DeclAttrs.end()) {
4566320064d0c60fa8683f5623881c9394fd4aa7689Douglas Gregor    Pos->second->~AttrVec();
4576320064d0c60fa8683f5623881c9394fd4aa7689Douglas Gregor    DeclAttrs.erase(Pos);
4586320064d0c60fa8683f5623881c9394fd4aa7689Douglas Gregor  }
4596320064d0c60fa8683f5623881c9394fd4aa7689Douglas Gregor}
4606320064d0c60fa8683f5623881c9394fd4aa7689Douglas Gregor
461251b4ff2578e26959a4c036140ccd61c5e9292f2Douglas GregorMemberSpecializationInfo *
462663b5a0be7261c29bc4c526a71cffcfa02d4153eDouglas GregorASTContext::getInstantiatedFromStaticDataMember(const VarDecl *Var) {
4637caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  assert(Var->isStaticDataMember() && "Not a static data member");
464663b5a0be7261c29bc4c526a71cffcfa02d4153eDouglas Gregor  llvm::DenseMap<const VarDecl *, MemberSpecializationInfo *>::iterator Pos
4657caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor    = InstantiatedFromStaticDataMember.find(Var);
4667caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  if (Pos == InstantiatedFromStaticDataMember.end())
4677caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor    return 0;
4681eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
4697caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  return Pos->second;
4707caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor}
4717caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor
4721eb4433ac451dc16f4133a88af2d002ac26c58efMike Stumpvoid
473251b4ff2578e26959a4c036140ccd61c5e9292f2Douglas GregorASTContext::setInstantiatedFromStaticDataMember(VarDecl *Inst, VarDecl *Tmpl,
4749421adc43891e272156fab640e5d5ee5054b779cArgyrios Kyrtzidis                                                TemplateSpecializationKind TSK,
4759421adc43891e272156fab640e5d5ee5054b779cArgyrios Kyrtzidis                                          SourceLocation PointOfInstantiation) {
4767caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  assert(Inst->isStaticDataMember() && "Not a static data member");
4777caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  assert(Tmpl->isStaticDataMember() && "Not a static data member");
4787caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  assert(!InstantiatedFromStaticDataMember[Inst] &&
4797caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor         "Already noted what static data member was instantiated from");
480251b4ff2578e26959a4c036140ccd61c5e9292f2Douglas Gregor  InstantiatedFromStaticDataMember[Inst]
4819421adc43891e272156fab640e5d5ee5054b779cArgyrios Kyrtzidis    = new (*this) MemberSpecializationInfo(Tmpl, TSK, PointOfInstantiation);
4827caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor}
4837caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor
4847ba107a1863ddfa1664555854f0d7bdb3c491c92John McCallNamedDecl *
485ed97649e9574b9d854fa4d6109c9333ae0993554John McCallASTContext::getInstantiatedFromUsingDecl(UsingDecl *UUD) {
4867ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall  llvm::DenseMap<UsingDecl *, NamedDecl *>::const_iterator Pos
487ed97649e9574b9d854fa4d6109c9333ae0993554John McCall    = InstantiatedFromUsingDecl.find(UUD);
488ed97649e9574b9d854fa4d6109c9333ae0993554John McCall  if (Pos == InstantiatedFromUsingDecl.end())
4890d8df780aef1acda5962347a32591efc629b6748Anders Carlsson    return 0;
4901eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
4910d8df780aef1acda5962347a32591efc629b6748Anders Carlsson  return Pos->second;
4920d8df780aef1acda5962347a32591efc629b6748Anders Carlsson}
4930d8df780aef1acda5962347a32591efc629b6748Anders Carlsson
4940d8df780aef1acda5962347a32591efc629b6748Anders Carlssonvoid
495ed97649e9574b9d854fa4d6109c9333ae0993554John McCallASTContext::setInstantiatedFromUsingDecl(UsingDecl *Inst, NamedDecl *Pattern) {
496ed97649e9574b9d854fa4d6109c9333ae0993554John McCall  assert((isa<UsingDecl>(Pattern) ||
497ed97649e9574b9d854fa4d6109c9333ae0993554John McCall          isa<UnresolvedUsingValueDecl>(Pattern) ||
498ed97649e9574b9d854fa4d6109c9333ae0993554John McCall          isa<UnresolvedUsingTypenameDecl>(Pattern)) &&
499ed97649e9574b9d854fa4d6109c9333ae0993554John McCall         "pattern decl is not a using decl");
500ed97649e9574b9d854fa4d6109c9333ae0993554John McCall  assert(!InstantiatedFromUsingDecl[Inst] && "pattern already exists");
501ed97649e9574b9d854fa4d6109c9333ae0993554John McCall  InstantiatedFromUsingDecl[Inst] = Pattern;
502ed97649e9574b9d854fa4d6109c9333ae0993554John McCall}
503ed97649e9574b9d854fa4d6109c9333ae0993554John McCall
504ed97649e9574b9d854fa4d6109c9333ae0993554John McCallUsingShadowDecl *
505ed97649e9574b9d854fa4d6109c9333ae0993554John McCallASTContext::getInstantiatedFromUsingShadowDecl(UsingShadowDecl *Inst) {
506ed97649e9574b9d854fa4d6109c9333ae0993554John McCall  llvm::DenseMap<UsingShadowDecl*, UsingShadowDecl*>::const_iterator Pos
507ed97649e9574b9d854fa4d6109c9333ae0993554John McCall    = InstantiatedFromUsingShadowDecl.find(Inst);
508ed97649e9574b9d854fa4d6109c9333ae0993554John McCall  if (Pos == InstantiatedFromUsingShadowDecl.end())
509ed97649e9574b9d854fa4d6109c9333ae0993554John McCall    return 0;
510ed97649e9574b9d854fa4d6109c9333ae0993554John McCall
511ed97649e9574b9d854fa4d6109c9333ae0993554John McCall  return Pos->second;
512ed97649e9574b9d854fa4d6109c9333ae0993554John McCall}
513ed97649e9574b9d854fa4d6109c9333ae0993554John McCall
514ed97649e9574b9d854fa4d6109c9333ae0993554John McCallvoid
515ed97649e9574b9d854fa4d6109c9333ae0993554John McCallASTContext::setInstantiatedFromUsingShadowDecl(UsingShadowDecl *Inst,
516ed97649e9574b9d854fa4d6109c9333ae0993554John McCall                                               UsingShadowDecl *Pattern) {
517ed97649e9574b9d854fa4d6109c9333ae0993554John McCall  assert(!InstantiatedFromUsingShadowDecl[Inst] && "pattern already exists");
518ed97649e9574b9d854fa4d6109c9333ae0993554John McCall  InstantiatedFromUsingShadowDecl[Inst] = Pattern;
5190d8df780aef1acda5962347a32591efc629b6748Anders Carlsson}
5200d8df780aef1acda5962347a32591efc629b6748Anders Carlsson
521d8b285fee4471f393da8ee30f552ceacdc362afaAnders CarlssonFieldDecl *ASTContext::getInstantiatedFromUnnamedFieldDecl(FieldDecl *Field) {
522d8b285fee4471f393da8ee30f552ceacdc362afaAnders Carlsson  llvm::DenseMap<FieldDecl *, FieldDecl *>::iterator Pos
523d8b285fee4471f393da8ee30f552ceacdc362afaAnders Carlsson    = InstantiatedFromUnnamedFieldDecl.find(Field);
524d8b285fee4471f393da8ee30f552ceacdc362afaAnders Carlsson  if (Pos == InstantiatedFromUnnamedFieldDecl.end())
525d8b285fee4471f393da8ee30f552ceacdc362afaAnders Carlsson    return 0;
5261eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
527d8b285fee4471f393da8ee30f552ceacdc362afaAnders Carlsson  return Pos->second;
528d8b285fee4471f393da8ee30f552ceacdc362afaAnders Carlsson}
529d8b285fee4471f393da8ee30f552ceacdc362afaAnders Carlsson
530d8b285fee4471f393da8ee30f552ceacdc362afaAnders Carlssonvoid ASTContext::setInstantiatedFromUnnamedFieldDecl(FieldDecl *Inst,
531d8b285fee4471f393da8ee30f552ceacdc362afaAnders Carlsson                                                     FieldDecl *Tmpl) {
532d8b285fee4471f393da8ee30f552ceacdc362afaAnders Carlsson  assert(!Inst->getDeclName() && "Instantiated field decl is not unnamed");
533d8b285fee4471f393da8ee30f552ceacdc362afaAnders Carlsson  assert(!Tmpl->getDeclName() && "Template field decl is not unnamed");
534d8b285fee4471f393da8ee30f552ceacdc362afaAnders Carlsson  assert(!InstantiatedFromUnnamedFieldDecl[Inst] &&
535d8b285fee4471f393da8ee30f552ceacdc362afaAnders Carlsson         "Already noted what unnamed field was instantiated from");
5361eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
537d8b285fee4471f393da8ee30f552ceacdc362afaAnders Carlsson  InstantiatedFromUnnamedFieldDecl[Inst] = Tmpl;
538d8b285fee4471f393da8ee30f552ceacdc362afaAnders Carlsson}
539d8b285fee4471f393da8ee30f552ceacdc362afaAnders Carlsson
54014d56ef43ff4921c6749f7340212fbb743fdbb9bFariborz Jahanianbool ASTContext::ZeroBitfieldFollowsNonBitfield(const FieldDecl *FD,
54114d56ef43ff4921c6749f7340212fbb743fdbb9bFariborz Jahanian                                    const FieldDecl *LastFD) const {
54214d56ef43ff4921c6749f7340212fbb743fdbb9bFariborz Jahanian  return (FD->isBitField() && LastFD && !LastFD->isBitField() &&
54314d56ef43ff4921c6749f7340212fbb743fdbb9bFariborz Jahanian          FD->getBitWidth()-> EvaluateAsInt(*this).getZExtValue() == 0);
54414d56ef43ff4921c6749f7340212fbb743fdbb9bFariborz Jahanian
54514d56ef43ff4921c6749f7340212fbb743fdbb9bFariborz Jahanian}
54614d56ef43ff4921c6749f7340212fbb743fdbb9bFariborz Jahanian
547340fa242130c2d8d74c83edca0952e771aebe0e6Fariborz Jahanianbool ASTContext::ZeroBitfieldFollowsBitfield(const FieldDecl *FD,
548340fa242130c2d8d74c83edca0952e771aebe0e6Fariborz Jahanian                                             const FieldDecl *LastFD) const {
549340fa242130c2d8d74c83edca0952e771aebe0e6Fariborz Jahanian  return (FD->isBitField() && LastFD && LastFD->isBitField() &&
550cc0f9f1a3b5df7fd308ff3d800fbbbbff805157dFariborz Jahanian          FD->getBitWidth()-> EvaluateAsInt(*this).getZExtValue() == 0 &&
551cc0f9f1a3b5df7fd308ff3d800fbbbbff805157dFariborz Jahanian          LastFD->getBitWidth()-> EvaluateAsInt(*this).getZExtValue() != 0);
552340fa242130c2d8d74c83edca0952e771aebe0e6Fariborz Jahanian
553340fa242130c2d8d74c83edca0952e771aebe0e6Fariborz Jahanian}
554340fa242130c2d8d74c83edca0952e771aebe0e6Fariborz Jahanian
5559b3acaa32548d0ce78b9c39a3911397f6738a47cFariborz Jahanianbool ASTContext::BitfieldFollowsBitfield(const FieldDecl *FD,
5569b3acaa32548d0ce78b9c39a3911397f6738a47cFariborz Jahanian                                         const FieldDecl *LastFD) const {
5579b3acaa32548d0ce78b9c39a3911397f6738a47cFariborz Jahanian  return (FD->isBitField() && LastFD && LastFD->isBitField() &&
5589b3acaa32548d0ce78b9c39a3911397f6738a47cFariborz Jahanian          FD->getBitWidth()-> EvaluateAsInt(*this).getZExtValue() &&
5599b3acaa32548d0ce78b9c39a3911397f6738a47cFariborz Jahanian          LastFD->getBitWidth()-> EvaluateAsInt(*this).getZExtValue());
5609b3acaa32548d0ce78b9c39a3911397f6738a47cFariborz Jahanian}
5619b3acaa32548d0ce78b9c39a3911397f6738a47cFariborz Jahanian
56252bbe7a1133c3cb57e9246f1b96c12940ea3821aFariborz Jahanianbool ASTContext::NoneBitfieldFollowsBitfield(const FieldDecl *FD,
56352bbe7a1133c3cb57e9246f1b96c12940ea3821aFariborz Jahanian                                         const FieldDecl *LastFD) const {
56452bbe7a1133c3cb57e9246f1b96c12940ea3821aFariborz Jahanian  return (!FD->isBitField() && LastFD && LastFD->isBitField() &&
56552bbe7a1133c3cb57e9246f1b96c12940ea3821aFariborz Jahanian          LastFD->getBitWidth()-> EvaluateAsInt(*this).getZExtValue());
56652bbe7a1133c3cb57e9246f1b96c12940ea3821aFariborz Jahanian}
56752bbe7a1133c3cb57e9246f1b96c12940ea3821aFariborz Jahanian
56852bbe7a1133c3cb57e9246f1b96c12940ea3821aFariborz Jahanianbool ASTContext::BitfieldFollowsNoneBitfield(const FieldDecl *FD,
56952bbe7a1133c3cb57e9246f1b96c12940ea3821aFariborz Jahanian                                             const FieldDecl *LastFD) const {
57052bbe7a1133c3cb57e9246f1b96c12940ea3821aFariborz Jahanian  return (FD->isBitField() && LastFD && !LastFD->isBitField() &&
57152bbe7a1133c3cb57e9246f1b96c12940ea3821aFariborz Jahanian          FD->getBitWidth()-> EvaluateAsInt(*this).getZExtValue());
57252bbe7a1133c3cb57e9246f1b96c12940ea3821aFariborz Jahanian}
57352bbe7a1133c3cb57e9246f1b96c12940ea3821aFariborz Jahanian
5747d10b7eb670b821741b4c96f6cf7afbc3bb39abeDouglas GregorASTContext::overridden_cxx_method_iterator
5757d10b7eb670b821741b4c96f6cf7afbc3bb39abeDouglas GregorASTContext::overridden_methods_begin(const CXXMethodDecl *Method) const {
5767d10b7eb670b821741b4c96f6cf7afbc3bb39abeDouglas Gregor  llvm::DenseMap<const CXXMethodDecl *, CXXMethodVector>::const_iterator Pos
5777d10b7eb670b821741b4c96f6cf7afbc3bb39abeDouglas Gregor    = OverriddenMethods.find(Method);
5787d10b7eb670b821741b4c96f6cf7afbc3bb39abeDouglas Gregor  if (Pos == OverriddenMethods.end())
5797d10b7eb670b821741b4c96f6cf7afbc3bb39abeDouglas Gregor    return 0;
5807d10b7eb670b821741b4c96f6cf7afbc3bb39abeDouglas Gregor
5817d10b7eb670b821741b4c96f6cf7afbc3bb39abeDouglas Gregor  return Pos->second.begin();
5827d10b7eb670b821741b4c96f6cf7afbc3bb39abeDouglas Gregor}
5837d10b7eb670b821741b4c96f6cf7afbc3bb39abeDouglas Gregor
5847d10b7eb670b821741b4c96f6cf7afbc3bb39abeDouglas GregorASTContext::overridden_cxx_method_iterator
5857d10b7eb670b821741b4c96f6cf7afbc3bb39abeDouglas GregorASTContext::overridden_methods_end(const CXXMethodDecl *Method) const {
5867d10b7eb670b821741b4c96f6cf7afbc3bb39abeDouglas Gregor  llvm::DenseMap<const CXXMethodDecl *, CXXMethodVector>::const_iterator Pos
5877d10b7eb670b821741b4c96f6cf7afbc3bb39abeDouglas Gregor    = OverriddenMethods.find(Method);
5887d10b7eb670b821741b4c96f6cf7afbc3bb39abeDouglas Gregor  if (Pos == OverriddenMethods.end())
5897d10b7eb670b821741b4c96f6cf7afbc3bb39abeDouglas Gregor    return 0;
5907d10b7eb670b821741b4c96f6cf7afbc3bb39abeDouglas Gregor
5917d10b7eb670b821741b4c96f6cf7afbc3bb39abeDouglas Gregor  return Pos->second.end();
5927d10b7eb670b821741b4c96f6cf7afbc3bb39abeDouglas Gregor}
5937d10b7eb670b821741b4c96f6cf7afbc3bb39abeDouglas Gregor
594c91e9f439ae85d5f79a6b65672f1d7d1b55ccda0Argyrios Kyrtzidisunsigned
595c91e9f439ae85d5f79a6b65672f1d7d1b55ccda0Argyrios KyrtzidisASTContext::overridden_methods_size(const CXXMethodDecl *Method) const {
596c91e9f439ae85d5f79a6b65672f1d7d1b55ccda0Argyrios Kyrtzidis  llvm::DenseMap<const CXXMethodDecl *, CXXMethodVector>::const_iterator Pos
597c91e9f439ae85d5f79a6b65672f1d7d1b55ccda0Argyrios Kyrtzidis    = OverriddenMethods.find(Method);
598c91e9f439ae85d5f79a6b65672f1d7d1b55ccda0Argyrios Kyrtzidis  if (Pos == OverriddenMethods.end())
599c91e9f439ae85d5f79a6b65672f1d7d1b55ccda0Argyrios Kyrtzidis    return 0;
600c91e9f439ae85d5f79a6b65672f1d7d1b55ccda0Argyrios Kyrtzidis
601c91e9f439ae85d5f79a6b65672f1d7d1b55ccda0Argyrios Kyrtzidis  return Pos->second.size();
602c91e9f439ae85d5f79a6b65672f1d7d1b55ccda0Argyrios Kyrtzidis}
603c91e9f439ae85d5f79a6b65672f1d7d1b55ccda0Argyrios Kyrtzidis
6047d10b7eb670b821741b4c96f6cf7afbc3bb39abeDouglas Gregorvoid ASTContext::addOverriddenMethod(const CXXMethodDecl *Method,
6057d10b7eb670b821741b4c96f6cf7afbc3bb39abeDouglas Gregor                                     const CXXMethodDecl *Overridden) {
6067d10b7eb670b821741b4c96f6cf7afbc3bb39abeDouglas Gregor  OverriddenMethods[Method].push_back(Overridden);
6077d10b7eb670b821741b4c96f6cf7afbc3bb39abeDouglas Gregor}
6087d10b7eb670b821741b4c96f6cf7afbc3bb39abeDouglas Gregor
609464175bba1318bef7905122e9fda20cff926df78Chris Lattner//===----------------------------------------------------------------------===//
610464175bba1318bef7905122e9fda20cff926df78Chris Lattner//                         Type Sizing and Analysis
611464175bba1318bef7905122e9fda20cff926df78Chris Lattner//===----------------------------------------------------------------------===//
612a7674d8a9a69f3f6fe16e70cf2a3b2b15fb7c43dChris Lattner
613b7cfe88e88cb4f46308de89cf3f0c81bfe624128Chris Lattner/// getFloatTypeSemantics - Return the APFloat 'semantics' for the specified
614b7cfe88e88cb4f46308de89cf3f0c81bfe624128Chris Lattner/// scalar floating point type.
615b7cfe88e88cb4f46308de89cf3f0c81bfe624128Chris Lattnerconst llvm::fltSemantics &ASTContext::getFloatTypeSemantics(QualType T) const {
616183700f494ec9b6701b6efe82bcb25f4c79ba561John McCall  const BuiltinType *BT = T->getAs<BuiltinType>();
617b7cfe88e88cb4f46308de89cf3f0c81bfe624128Chris Lattner  assert(BT && "Not a floating point type!");
618b7cfe88e88cb4f46308de89cf3f0c81bfe624128Chris Lattner  switch (BT->getKind()) {
619b7cfe88e88cb4f46308de89cf3f0c81bfe624128Chris Lattner  default: assert(0 && "Not a floating point type!");
620b7cfe88e88cb4f46308de89cf3f0c81bfe624128Chris Lattner  case BuiltinType::Float:      return Target.getFloatFormat();
621b7cfe88e88cb4f46308de89cf3f0c81bfe624128Chris Lattner  case BuiltinType::Double:     return Target.getDoubleFormat();
622b7cfe88e88cb4f46308de89cf3f0c81bfe624128Chris Lattner  case BuiltinType::LongDouble: return Target.getLongDoubleFormat();
623b7cfe88e88cb4f46308de89cf3f0c81bfe624128Chris Lattner  }
624b7cfe88e88cb4f46308de89cf3f0c81bfe624128Chris Lattner}
625b7cfe88e88cb4f46308de89cf3f0c81bfe624128Chris Lattner
6268b752f10c394b140f9ef89e049cbad1a7676fc25Ken Dyck/// getDeclAlign - Return a conservative estimate of the alignment of the
627af707ab8fbb9451e8febb8d766f6c043628125c4Chris Lattner/// specified decl.  Note that bitfields do not have a valid alignment, so
628af707ab8fbb9451e8febb8d766f6c043628125c4Chris Lattner/// this method will assert on them.
6295d484e8cf710207010720589d89602233de61d01Sebastian Redl/// If @p RefAsPointee, references are treated like their underlying type
6305d484e8cf710207010720589d89602233de61d01Sebastian Redl/// (for alignof), else they're treated like pointers (for CodeGen).
6314ba2a17694148e16eaa8d3917f657ffcd3667be4Jay FoadCharUnits ASTContext::getDeclAlign(const Decl *D, bool RefAsPointee) const {
632dcdafb6a701aa9d81edcb088915f58933315dc05Eli Friedman  unsigned Align = Target.getCharWidth();
633dcdafb6a701aa9d81edcb088915f58933315dc05Eli Friedman
6344081a5c5f1381c4ec77f8ab3866693917e4329c4John McCall  bool UseAlignAttrOnly = false;
6354081a5c5f1381c4ec77f8ab3866693917e4329c4John McCall  if (unsigned AlignFromAttr = D->getMaxAlignment()) {
6364081a5c5f1381c4ec77f8ab3866693917e4329c4John McCall    Align = AlignFromAttr;
6374081a5c5f1381c4ec77f8ab3866693917e4329c4John McCall
6384081a5c5f1381c4ec77f8ab3866693917e4329c4John McCall    // __attribute__((aligned)) can increase or decrease alignment
6394081a5c5f1381c4ec77f8ab3866693917e4329c4John McCall    // *except* on a struct or struct member, where it only increases
6404081a5c5f1381c4ec77f8ab3866693917e4329c4John McCall    // alignment unless 'packed' is also specified.
6414081a5c5f1381c4ec77f8ab3866693917e4329c4John McCall    //
6424081a5c5f1381c4ec77f8ab3866693917e4329c4John McCall    // It is an error for [[align]] to decrease alignment, so we can
6434081a5c5f1381c4ec77f8ab3866693917e4329c4John McCall    // ignore that possibility;  Sema should diagnose it.
6444081a5c5f1381c4ec77f8ab3866693917e4329c4John McCall    if (isa<FieldDecl>(D)) {
6454081a5c5f1381c4ec77f8ab3866693917e4329c4John McCall      UseAlignAttrOnly = D->hasAttr<PackedAttr>() ||
6464081a5c5f1381c4ec77f8ab3866693917e4329c4John McCall        cast<FieldDecl>(D)->getParent()->hasAttr<PackedAttr>();
6474081a5c5f1381c4ec77f8ab3866693917e4329c4John McCall    } else {
6484081a5c5f1381c4ec77f8ab3866693917e4329c4John McCall      UseAlignAttrOnly = true;
6494081a5c5f1381c4ec77f8ab3866693917e4329c4John McCall    }
6504081a5c5f1381c4ec77f8ab3866693917e4329c4John McCall  }
65178a7d7d79964119a3f35b262eb154b5cbf1001edFariborz Jahanian  else if (isa<FieldDecl>(D))
65278a7d7d79964119a3f35b262eb154b5cbf1001edFariborz Jahanian      UseAlignAttrOnly =
65378a7d7d79964119a3f35b262eb154b5cbf1001edFariborz Jahanian        D->hasAttr<PackedAttr>() ||
65478a7d7d79964119a3f35b262eb154b5cbf1001edFariborz Jahanian        cast<FieldDecl>(D)->getParent()->hasAttr<PackedAttr>();
655dcdafb6a701aa9d81edcb088915f58933315dc05Eli Friedman
656ba4f5d5754c8291690d01ca9581926673d69b24cJohn McCall  // If we're using the align attribute only, just ignore everything
657ba4f5d5754c8291690d01ca9581926673d69b24cJohn McCall  // else about the declaration and its type.
6584081a5c5f1381c4ec77f8ab3866693917e4329c4John McCall  if (UseAlignAttrOnly) {
659ba4f5d5754c8291690d01ca9581926673d69b24cJohn McCall    // do nothing
660ba4f5d5754c8291690d01ca9581926673d69b24cJohn McCall
6614081a5c5f1381c4ec77f8ab3866693917e4329c4John McCall  } else if (const ValueDecl *VD = dyn_cast<ValueDecl>(D)) {
662af707ab8fbb9451e8febb8d766f6c043628125c4Chris Lattner    QualType T = VD->getType();
6636217b80b7a1379b74cced1c076338262c3c980b3Ted Kremenek    if (const ReferenceType* RT = T->getAs<ReferenceType>()) {
6645d484e8cf710207010720589d89602233de61d01Sebastian Redl      if (RefAsPointee)
6655d484e8cf710207010720589d89602233de61d01Sebastian Redl        T = RT->getPointeeType();
6665d484e8cf710207010720589d89602233de61d01Sebastian Redl      else
6675d484e8cf710207010720589d89602233de61d01Sebastian Redl        T = getPointerType(RT->getPointeeType());
6685d484e8cf710207010720589d89602233de61d01Sebastian Redl    }
6695d484e8cf710207010720589d89602233de61d01Sebastian Redl    if (!T->isIncompleteType() && !T->isFunctionType()) {
6703b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall      // Adjust alignments of declarations with array type by the
6713b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall      // large-array alignment on the target.
6726deecb0d46bcfd048e651d2db7c4fb0d6407da96Rafael Espindola      unsigned MinWidth = Target.getLargeArrayMinWidth();
6733b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall      const ArrayType *arrayType;
6743b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall      if (MinWidth && (arrayType = getAsArrayType(T))) {
6753b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall        if (isa<VariableArrayType>(arrayType))
6763b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall          Align = std::max(Align, Target.getLargeArrayAlign());
6773b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall        else if (isa<ConstantArrayType>(arrayType) &&
6783b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall                 MinWidth <= getTypeSize(cast<ConstantArrayType>(arrayType)))
6793b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall          Align = std::max(Align, Target.getLargeArrayAlign());
6803b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall
6813b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall        // Walk through any array types while we're at it.
6823b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall        T = getBaseElementType(arrayType);
6836deecb0d46bcfd048e651d2db7c4fb0d6407da96Rafael Espindola      }
684dcdafb6a701aa9d81edcb088915f58933315dc05Eli Friedman      Align = std::max(Align, getPreferredTypeAlign(T.getTypePtr()));
685dcdafb6a701aa9d81edcb088915f58933315dc05Eli Friedman    }
686ba4f5d5754c8291690d01ca9581926673d69b24cJohn McCall
687ba4f5d5754c8291690d01ca9581926673d69b24cJohn McCall    // Fields can be subject to extra alignment constraints, like if
688ba4f5d5754c8291690d01ca9581926673d69b24cJohn McCall    // the field is packed, the struct is packed, or the struct has a
689ba4f5d5754c8291690d01ca9581926673d69b24cJohn McCall    // a max-field-alignment constraint (#pragma pack).  So calculate
690ba4f5d5754c8291690d01ca9581926673d69b24cJohn McCall    // the actual alignment of the field within the struct, and then
691ba4f5d5754c8291690d01ca9581926673d69b24cJohn McCall    // (as we're expected to) constrain that by the alignment of the type.
692ba4f5d5754c8291690d01ca9581926673d69b24cJohn McCall    if (const FieldDecl *field = dyn_cast<FieldDecl>(VD)) {
693ba4f5d5754c8291690d01ca9581926673d69b24cJohn McCall      // So calculate the alignment of the field.
694ba4f5d5754c8291690d01ca9581926673d69b24cJohn McCall      const ASTRecordLayout &layout = getASTRecordLayout(field->getParent());
695ba4f5d5754c8291690d01ca9581926673d69b24cJohn McCall
696ba4f5d5754c8291690d01ca9581926673d69b24cJohn McCall      // Start with the record's overall alignment.
697dac54c124e302d6f028ea5723c425b7f66fc7c71Ken Dyck      unsigned fieldAlign = toBits(layout.getAlignment());
698ba4f5d5754c8291690d01ca9581926673d69b24cJohn McCall
699ba4f5d5754c8291690d01ca9581926673d69b24cJohn McCall      // Use the GCD of that and the offset within the record.
700ba4f5d5754c8291690d01ca9581926673d69b24cJohn McCall      uint64_t offset = layout.getFieldOffset(field->getFieldIndex());
701ba4f5d5754c8291690d01ca9581926673d69b24cJohn McCall      if (offset > 0) {
702ba4f5d5754c8291690d01ca9581926673d69b24cJohn McCall        // Alignment is always a power of 2, so the GCD will be a power of 2,
703ba4f5d5754c8291690d01ca9581926673d69b24cJohn McCall        // which means we get to do this crazy thing instead of Euclid's.
704ba4f5d5754c8291690d01ca9581926673d69b24cJohn McCall        uint64_t lowBitOfOffset = offset & (~offset + 1);
705ba4f5d5754c8291690d01ca9581926673d69b24cJohn McCall        if (lowBitOfOffset < fieldAlign)
706ba4f5d5754c8291690d01ca9581926673d69b24cJohn McCall          fieldAlign = static_cast<unsigned>(lowBitOfOffset);
707ba4f5d5754c8291690d01ca9581926673d69b24cJohn McCall      }
708ba4f5d5754c8291690d01ca9581926673d69b24cJohn McCall
709ba4f5d5754c8291690d01ca9581926673d69b24cJohn McCall      Align = std::min(Align, fieldAlign);
71005f62474dd2b0f1cb69adbe0787f2868788aa949Charles Davis    }
711af707ab8fbb9451e8febb8d766f6c043628125c4Chris Lattner  }
712dcdafb6a701aa9d81edcb088915f58933315dc05Eli Friedman
713eb6f5dc86531f794ba7746a2da4d28e37cf5da7eKen Dyck  return toCharUnitsFromBits(Align);
714af707ab8fbb9451e8febb8d766f6c043628125c4Chris Lattner}
715b7cfe88e88cb4f46308de89cf3f0c81bfe624128Chris Lattner
716ea1471e0e967548c596a71469702f8846dbaf3c0John McCallstd::pair<CharUnits, CharUnits>
717bee5a79fc95e3003d755031e3d2bb4410a71e1c1Ken DyckASTContext::getTypeInfoInChars(const Type *T) const {
718ea1471e0e967548c596a71469702f8846dbaf3c0John McCall  std::pair<uint64_t, unsigned> Info = getTypeInfo(T);
719eb6f5dc86531f794ba7746a2da4d28e37cf5da7eKen Dyck  return std::make_pair(toCharUnitsFromBits(Info.first),
720eb6f5dc86531f794ba7746a2da4d28e37cf5da7eKen Dyck                        toCharUnitsFromBits(Info.second));
721ea1471e0e967548c596a71469702f8846dbaf3c0John McCall}
722ea1471e0e967548c596a71469702f8846dbaf3c0John McCall
723ea1471e0e967548c596a71469702f8846dbaf3c0John McCallstd::pair<CharUnits, CharUnits>
724bee5a79fc95e3003d755031e3d2bb4410a71e1c1Ken DyckASTContext::getTypeInfoInChars(QualType T) const {
725ea1471e0e967548c596a71469702f8846dbaf3c0John McCall  return getTypeInfoInChars(T.getTypePtr());
726ea1471e0e967548c596a71469702f8846dbaf3c0John McCall}
727ea1471e0e967548c596a71469702f8846dbaf3c0John McCall
728a7674d8a9a69f3f6fe16e70cf2a3b2b15fb7c43dChris Lattner/// getTypeSize - Return the size of the specified type, in bits.  This method
729a7674d8a9a69f3f6fe16e70cf2a3b2b15fb7c43dChris Lattner/// does not work on incomplete types.
7300953e767ff7817f97b3ab20896b229891eeff45bJohn McCall///
7310953e767ff7817f97b3ab20896b229891eeff45bJohn McCall/// FIXME: Pointers into different addr spaces could have different sizes and
7320953e767ff7817f97b3ab20896b229891eeff45bJohn McCall/// alignment requirements: getPointerInfo should take an AddrSpace, this
7330953e767ff7817f97b3ab20896b229891eeff45bJohn McCall/// should take a QualType, &c.
734d2d2a11a91d7ddf468bfb70f66362d24806ed601Chris Lattnerstd::pair<uint64_t, unsigned>
7354ba2a17694148e16eaa8d3917f657ffcd3667be4Jay FoadASTContext::getTypeInfo(const Type *T) const {
7365e301007e31e14c8ff647288e1b8bd8dbf8a5fe4Mike Stump  uint64_t Width=0;
7375e301007e31e14c8ff647288e1b8bd8dbf8a5fe4Mike Stump  unsigned Align=8;
738a7674d8a9a69f3f6fe16e70cf2a3b2b15fb7c43dChris Lattner  switch (T->getTypeClass()) {
73972564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor#define TYPE(Class, Base)
74072564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor#define ABSTRACT_TYPE(Class, Base)
74118857644059c45da6776f1a288eec7b4cf3a844aDouglas Gregor#define NON_CANONICAL_TYPE(Class, Base)
74272564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor#define DEPENDENT_TYPE(Class, Base) case Type::Class:
74372564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor#include "clang/AST/TypeNodes.def"
74418857644059c45da6776f1a288eec7b4cf3a844aDouglas Gregor    assert(false && "Should not see dependent types");
74572564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor    break;
74672564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor
7475d2a6303467184b1f159bb6556efc434e50e3c28Chris Lattner  case Type::FunctionNoProto:
7485d2a6303467184b1f159bb6556efc434e50e3c28Chris Lattner  case Type::FunctionProto:
74918857644059c45da6776f1a288eec7b4cf3a844aDouglas Gregor    // GCC extension: alignof(function) = 32 bits
75018857644059c45da6776f1a288eec7b4cf3a844aDouglas Gregor    Width = 0;
75118857644059c45da6776f1a288eec7b4cf3a844aDouglas Gregor    Align = 32;
75218857644059c45da6776f1a288eec7b4cf3a844aDouglas Gregor    break;
75318857644059c45da6776f1a288eec7b4cf3a844aDouglas Gregor
75472564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor  case Type::IncompleteArray:
755fb22d96692c5240fb8d611290dbf7eeed3759c73Steve Naroff  case Type::VariableArray:
75618857644059c45da6776f1a288eec7b4cf3a844aDouglas Gregor    Width = 0;
75718857644059c45da6776f1a288eec7b4cf3a844aDouglas Gregor    Align = getTypeAlign(cast<ArrayType>(T)->getElementType());
75818857644059c45da6776f1a288eec7b4cf3a844aDouglas Gregor    break;
75918857644059c45da6776f1a288eec7b4cf3a844aDouglas Gregor
760fb22d96692c5240fb8d611290dbf7eeed3759c73Steve Naroff  case Type::ConstantArray: {
7611d75118af76cae2bfc06389cde410e14bd0a19fcDaniel Dunbar    const ConstantArrayType *CAT = cast<ConstantArrayType>(T);
7621eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
76398be4943e8dc4f3905629a7102668960873cf863Chris Lattner    std::pair<uint64_t, unsigned> EltInfo = getTypeInfo(CAT->getElementType());
7649e9b6dc3fd413f5341fab54b681420eeb21cd169Chris Lattner    Width = EltInfo.first*CAT->getSize().getZExtValue();
765030d8846c7e520330007087e949f621989876e3aChris Lattner    Align = EltInfo.second;
766cd88b4171753dcb2bc0a21d78f1597c796bb8a20Argyrios Kyrtzidis    Width = llvm::RoundUpToAlignment(Width, Align);
767030d8846c7e520330007087e949f621989876e3aChris Lattner    break;
7685c09a02a5db85e08a432b6eeced9aa656349710dChristopher Lamb  }
769213541a68a3e137d11d2cefb612c6cdb410d7e8eNate Begeman  case Type::ExtVector:
770030d8846c7e520330007087e949f621989876e3aChris Lattner  case Type::Vector: {
7719fcfe926432f3c3f7e9a61219e55c352fd358e45Chris Lattner    const VectorType *VT = cast<VectorType>(T);
7729fcfe926432f3c3f7e9a61219e55c352fd358e45Chris Lattner    std::pair<uint64_t, unsigned> EltInfo = getTypeInfo(VT->getElementType());
7739fcfe926432f3c3f7e9a61219e55c352fd358e45Chris Lattner    Width = EltInfo.first*VT->getNumElements();
7744bd998bbc228915d2b9cae5b67879de48940d05eEli Friedman    Align = Width;
7756fe7c8aa8c7546743ecd0ac0138c2cf5d8155386Nate Begeman    // If the alignment is not a power of 2, round up to the next power of 2.
7766fe7c8aa8c7546743ecd0ac0138c2cf5d8155386Nate Begeman    // This happens for non-power-of-2 length vectors.
7778eefcd353c1d06a10104f69e5079ebab3183f9a3Dan Gohman    if (Align & (Align-1)) {
7789fcfe926432f3c3f7e9a61219e55c352fd358e45Chris Lattner      Align = llvm::NextPowerOf2(Align);
7799fcfe926432f3c3f7e9a61219e55c352fd358e45Chris Lattner      Width = llvm::RoundUpToAlignment(Width, Align);
7809fcfe926432f3c3f7e9a61219e55c352fd358e45Chris Lattner    }
781030d8846c7e520330007087e949f621989876e3aChris Lattner    break;
782030d8846c7e520330007087e949f621989876e3aChris Lattner  }
7835d2a6303467184b1f159bb6556efc434e50e3c28Chris Lattner
7849e9b6dc3fd413f5341fab54b681420eeb21cd169Chris Lattner  case Type::Builtin:
785a7674d8a9a69f3f6fe16e70cf2a3b2b15fb7c43dChris Lattner    switch (cast<BuiltinType>(T)->getKind()) {
786692233e90a99c3a81dd04879d36eb9688f137c44Chris Lattner    default: assert(0 && "Unknown builtin type!");
787d2d2a11a91d7ddf468bfb70f66362d24806ed601Chris Lattner    case BuiltinType::Void:
78818857644059c45da6776f1a288eec7b4cf3a844aDouglas Gregor      // GCC extension: alignof(void) = 8 bits.
78918857644059c45da6776f1a288eec7b4cf3a844aDouglas Gregor      Width = 0;
79018857644059c45da6776f1a288eec7b4cf3a844aDouglas Gregor      Align = 8;
79118857644059c45da6776f1a288eec7b4cf3a844aDouglas Gregor      break;
79218857644059c45da6776f1a288eec7b4cf3a844aDouglas Gregor
7936f62c2abd8077bf70d2166d37e8caa426b34d8e4Chris Lattner    case BuiltinType::Bool:
7949e9b6dc3fd413f5341fab54b681420eeb21cd169Chris Lattner      Width = Target.getBoolWidth();
7959e9b6dc3fd413f5341fab54b681420eeb21cd169Chris Lattner      Align = Target.getBoolAlign();
7966f62c2abd8077bf70d2166d37e8caa426b34d8e4Chris Lattner      break;
797692233e90a99c3a81dd04879d36eb9688f137c44Chris Lattner    case BuiltinType::Char_S:
798692233e90a99c3a81dd04879d36eb9688f137c44Chris Lattner    case BuiltinType::Char_U:
799692233e90a99c3a81dd04879d36eb9688f137c44Chris Lattner    case BuiltinType::UChar:
8006f62c2abd8077bf70d2166d37e8caa426b34d8e4Chris Lattner    case BuiltinType::SChar:
8019e9b6dc3fd413f5341fab54b681420eeb21cd169Chris Lattner      Width = Target.getCharWidth();
8029e9b6dc3fd413f5341fab54b681420eeb21cd169Chris Lattner      Align = Target.getCharAlign();
8036f62c2abd8077bf70d2166d37e8caa426b34d8e4Chris Lattner      break;
8043f59c975aa5d047f7edd1b900b5e885c38af0ef7Chris Lattner    case BuiltinType::WChar_S:
8053f59c975aa5d047f7edd1b900b5e885c38af0ef7Chris Lattner    case BuiltinType::WChar_U:
80664c438a4be2a871fa43c78264663ba1e9788b94dArgyrios Kyrtzidis      Width = Target.getWCharWidth();
80764c438a4be2a871fa43c78264663ba1e9788b94dArgyrios Kyrtzidis      Align = Target.getWCharAlign();
80864c438a4be2a871fa43c78264663ba1e9788b94dArgyrios Kyrtzidis      break;
809f5c209d23b20ada4a9b6235db50317239cbf6ae1Alisdair Meredith    case BuiltinType::Char16:
810f5c209d23b20ada4a9b6235db50317239cbf6ae1Alisdair Meredith      Width = Target.getChar16Width();
811f5c209d23b20ada4a9b6235db50317239cbf6ae1Alisdair Meredith      Align = Target.getChar16Align();
812f5c209d23b20ada4a9b6235db50317239cbf6ae1Alisdair Meredith      break;
813f5c209d23b20ada4a9b6235db50317239cbf6ae1Alisdair Meredith    case BuiltinType::Char32:
814f5c209d23b20ada4a9b6235db50317239cbf6ae1Alisdair Meredith      Width = Target.getChar32Width();
815f5c209d23b20ada4a9b6235db50317239cbf6ae1Alisdair Meredith      Align = Target.getChar32Align();
816f5c209d23b20ada4a9b6235db50317239cbf6ae1Alisdair Meredith      break;
817692233e90a99c3a81dd04879d36eb9688f137c44Chris Lattner    case BuiltinType::UShort:
8186f62c2abd8077bf70d2166d37e8caa426b34d8e4Chris Lattner    case BuiltinType::Short:
8199e9b6dc3fd413f5341fab54b681420eeb21cd169Chris Lattner      Width = Target.getShortWidth();
8209e9b6dc3fd413f5341fab54b681420eeb21cd169Chris Lattner      Align = Target.getShortAlign();
8216f62c2abd8077bf70d2166d37e8caa426b34d8e4Chris Lattner      break;
822692233e90a99c3a81dd04879d36eb9688f137c44Chris Lattner    case BuiltinType::UInt:
8236f62c2abd8077bf70d2166d37e8caa426b34d8e4Chris Lattner    case BuiltinType::Int:
8249e9b6dc3fd413f5341fab54b681420eeb21cd169Chris Lattner      Width = Target.getIntWidth();
8259e9b6dc3fd413f5341fab54b681420eeb21cd169Chris Lattner      Align = Target.getIntAlign();
8266f62c2abd8077bf70d2166d37e8caa426b34d8e4Chris Lattner      break;
827692233e90a99c3a81dd04879d36eb9688f137c44Chris Lattner    case BuiltinType::ULong:
8286f62c2abd8077bf70d2166d37e8caa426b34d8e4Chris Lattner    case BuiltinType::Long:
8299e9b6dc3fd413f5341fab54b681420eeb21cd169Chris Lattner      Width = Target.getLongWidth();
8309e9b6dc3fd413f5341fab54b681420eeb21cd169Chris Lattner      Align = Target.getLongAlign();
8316f62c2abd8077bf70d2166d37e8caa426b34d8e4Chris Lattner      break;
832692233e90a99c3a81dd04879d36eb9688f137c44Chris Lattner    case BuiltinType::ULongLong:
8336f62c2abd8077bf70d2166d37e8caa426b34d8e4Chris Lattner    case BuiltinType::LongLong:
8349e9b6dc3fd413f5341fab54b681420eeb21cd169Chris Lattner      Width = Target.getLongLongWidth();
8359e9b6dc3fd413f5341fab54b681420eeb21cd169Chris Lattner      Align = Target.getLongLongAlign();
8366f62c2abd8077bf70d2166d37e8caa426b34d8e4Chris Lattner      break;
837ec16cb9b5a481d62a73ad47fa59034ced4d62022Chris Lattner    case BuiltinType::Int128:
838ec16cb9b5a481d62a73ad47fa59034ced4d62022Chris Lattner    case BuiltinType::UInt128:
839ec16cb9b5a481d62a73ad47fa59034ced4d62022Chris Lattner      Width = 128;
840ec16cb9b5a481d62a73ad47fa59034ced4d62022Chris Lattner      Align = 128; // int128_t is 128-bit aligned on all targets.
841ec16cb9b5a481d62a73ad47fa59034ced4d62022Chris Lattner      break;
8426f62c2abd8077bf70d2166d37e8caa426b34d8e4Chris Lattner    case BuiltinType::Float:
8439e9b6dc3fd413f5341fab54b681420eeb21cd169Chris Lattner      Width = Target.getFloatWidth();
8449e9b6dc3fd413f5341fab54b681420eeb21cd169Chris Lattner      Align = Target.getFloatAlign();
8456f62c2abd8077bf70d2166d37e8caa426b34d8e4Chris Lattner      break;
8466f62c2abd8077bf70d2166d37e8caa426b34d8e4Chris Lattner    case BuiltinType::Double:
8475426bf6456a5aeac416a9150de157904d101c819Chris Lattner      Width = Target.getDoubleWidth();
8485426bf6456a5aeac416a9150de157904d101c819Chris Lattner      Align = Target.getDoubleAlign();
8496f62c2abd8077bf70d2166d37e8caa426b34d8e4Chris Lattner      break;
8506f62c2abd8077bf70d2166d37e8caa426b34d8e4Chris Lattner    case BuiltinType::LongDouble:
8519e9b6dc3fd413f5341fab54b681420eeb21cd169Chris Lattner      Width = Target.getLongDoubleWidth();
8529e9b6dc3fd413f5341fab54b681420eeb21cd169Chris Lattner      Align = Target.getLongDoubleAlign();
8536f62c2abd8077bf70d2166d37e8caa426b34d8e4Chris Lattner      break;
8546e8ed16ffef02b82995a90bdcf10ffff7d63839aSebastian Redl    case BuiltinType::NullPtr:
8556e8ed16ffef02b82995a90bdcf10ffff7d63839aSebastian Redl      Width = Target.getPointerWidth(0); // C++ 3.9.1p11: sizeof(nullptr_t)
8566e8ed16ffef02b82995a90bdcf10ffff7d63839aSebastian Redl      Align = Target.getPointerAlign(0); //   == sizeof(void*)
8571590d9c0fec4c710c2962e4bb71f76979b5163d3Sebastian Redl      break;
858e04f5fc25cf49e8a5b836459d836c20dc3229a95Fariborz Jahanian    case BuiltinType::ObjCId:
859e04f5fc25cf49e8a5b836459d836c20dc3229a95Fariborz Jahanian    case BuiltinType::ObjCClass:
860e04f5fc25cf49e8a5b836459d836c20dc3229a95Fariborz Jahanian    case BuiltinType::ObjCSel:
861e04f5fc25cf49e8a5b836459d836c20dc3229a95Fariborz Jahanian      Width = Target.getPointerWidth(0);
862e04f5fc25cf49e8a5b836459d836c20dc3229a95Fariborz Jahanian      Align = Target.getPointerAlign(0);
863e04f5fc25cf49e8a5b836459d836c20dc3229a95Fariborz Jahanian      break;
864a7674d8a9a69f3f6fe16e70cf2a3b2b15fb7c43dChris Lattner    }
865bfef6d7c67831a135d6ab79931f010f750a730adChris Lattner    break;
866d1b3c2dd5bc1f3103bee6137957aa7c5f8f2f0bcSteve Naroff  case Type::ObjCObjectPointer:
8675426bf6456a5aeac416a9150de157904d101c819Chris Lattner    Width = Target.getPointerWidth(0);
868f72a44330b9d9a4b2d93e9b91cfb8ab7bd4a0643Chris Lattner    Align = Target.getPointerAlign(0);
8696f62c2abd8077bf70d2166d37e8caa426b34d8e4Chris Lattner    break;
870485eeff9ba73376c8e01179bf1a501b1723446cbSteve Naroff  case Type::BlockPointer: {
871207f4d8543529221932af82836016a2ef066c917Peter Collingbourne    unsigned AS = getTargetAddressSpace(
872207f4d8543529221932af82836016a2ef066c917Peter Collingbourne        cast<BlockPointerType>(T)->getPointeeType());
873485eeff9ba73376c8e01179bf1a501b1723446cbSteve Naroff    Width = Target.getPointerWidth(AS);
874485eeff9ba73376c8e01179bf1a501b1723446cbSteve Naroff    Align = Target.getPointerAlign(AS);
875485eeff9ba73376c8e01179bf1a501b1723446cbSteve Naroff    break;
876485eeff9ba73376c8e01179bf1a501b1723446cbSteve Naroff  }
8775d484e8cf710207010720589d89602233de61d01Sebastian Redl  case Type::LValueReference:
8785d484e8cf710207010720589d89602233de61d01Sebastian Redl  case Type::RValueReference: {
8795d484e8cf710207010720589d89602233de61d01Sebastian Redl    // alignof and sizeof should never enter this code path here, so we go
8805d484e8cf710207010720589d89602233de61d01Sebastian Redl    // the pointer route.
881207f4d8543529221932af82836016a2ef066c917Peter Collingbourne    unsigned AS = getTargetAddressSpace(
882207f4d8543529221932af82836016a2ef066c917Peter Collingbourne        cast<ReferenceType>(T)->getPointeeType());
8835d484e8cf710207010720589d89602233de61d01Sebastian Redl    Width = Target.getPointerWidth(AS);
8845d484e8cf710207010720589d89602233de61d01Sebastian Redl    Align = Target.getPointerAlign(AS);
8855d484e8cf710207010720589d89602233de61d01Sebastian Redl    break;
8865d484e8cf710207010720589d89602233de61d01Sebastian Redl  }
887f72a44330b9d9a4b2d93e9b91cfb8ab7bd4a0643Chris Lattner  case Type::Pointer: {
888207f4d8543529221932af82836016a2ef066c917Peter Collingbourne    unsigned AS = getTargetAddressSpace(cast<PointerType>(T)->getPointeeType());
8895426bf6456a5aeac416a9150de157904d101c819Chris Lattner    Width = Target.getPointerWidth(AS);
890f72a44330b9d9a4b2d93e9b91cfb8ab7bd4a0643Chris Lattner    Align = Target.getPointerAlign(AS);
891f72a44330b9d9a4b2d93e9b91cfb8ab7bd4a0643Chris Lattner    break;
892f72a44330b9d9a4b2d93e9b91cfb8ab7bd4a0643Chris Lattner  }
893f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl  case Type::MemberPointer: {
894071cc7deffad608165b1ddd5263e8bf181861520Charles Davis    const MemberPointerType *MPT = cast<MemberPointerType>(T);
8951eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    std::pair<uint64_t, unsigned> PtrDiffInfo =
8961cca74ef3627a3a0ab14501d23e336548f6611b2Anders Carlsson      getTypeInfo(getPointerDiffType());
897071cc7deffad608165b1ddd5263e8bf181861520Charles Davis    Width = PtrDiffInfo.first * ABI->getMemberPointerSize(MPT);
8981cca74ef3627a3a0ab14501d23e336548f6611b2Anders Carlsson    Align = PtrDiffInfo.second;
8991cca74ef3627a3a0ab14501d23e336548f6611b2Anders Carlsson    break;
900f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl  }
9015d2a6303467184b1f159bb6556efc434e50e3c28Chris Lattner  case Type::Complex: {
9025d2a6303467184b1f159bb6556efc434e50e3c28Chris Lattner    // Complex types have the same alignment as their elements, but twice the
9035d2a6303467184b1f159bb6556efc434e50e3c28Chris Lattner    // size.
9041eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    std::pair<uint64_t, unsigned> EltInfo =
90598be4943e8dc4f3905629a7102668960873cf863Chris Lattner      getTypeInfo(cast<ComplexType>(T)->getElementType());
9069e9b6dc3fd413f5341fab54b681420eeb21cd169Chris Lattner    Width = EltInfo.first*2;
9075d2a6303467184b1f159bb6556efc434e50e3c28Chris Lattner    Align = EltInfo.second;
9085d2a6303467184b1f159bb6556efc434e50e3c28Chris Lattner    break;
9095d2a6303467184b1f159bb6556efc434e50e3c28Chris Lattner  }
910c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall  case Type::ObjCObject:
911c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall    return getTypeInfo(cast<ObjCObjectType>(T)->getBaseType().getTypePtr());
91244a3dded8080c5c9cfdad208ade8f8f7850d9a4fDevang Patel  case Type::ObjCInterface: {
9131d75118af76cae2bfc06389cde410e14bd0a19fcDaniel Dunbar    const ObjCInterfaceType *ObjCI = cast<ObjCInterfaceType>(T);
91444a3dded8080c5c9cfdad208ade8f8f7850d9a4fDevang Patel    const ASTRecordLayout &Layout = getASTObjCInterfaceLayout(ObjCI->getDecl());
915dd76a9ab9ea675671200f94b18ce95766841952bKen Dyck    Width = toBits(Layout.getSize());
916dac54c124e302d6f028ea5723c425b7f66fc7c71Ken Dyck    Align = toBits(Layout.getAlignment());
91744a3dded8080c5c9cfdad208ade8f8f7850d9a4fDevang Patel    break;
91844a3dded8080c5c9cfdad208ade8f8f7850d9a4fDevang Patel  }
91972564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor  case Type::Record:
92072564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor  case Type::Enum: {
9211d75118af76cae2bfc06389cde410e14bd0a19fcDaniel Dunbar    const TagType *TT = cast<TagType>(T);
9221d75118af76cae2bfc06389cde410e14bd0a19fcDaniel Dunbar
9231d75118af76cae2bfc06389cde410e14bd0a19fcDaniel Dunbar    if (TT->getDecl()->isInvalidDecl()) {
92422ce41d9fc8509da65858c75bf5b3c4dae2d8c04Douglas Gregor      Width = 8;
92522ce41d9fc8509da65858c75bf5b3c4dae2d8c04Douglas Gregor      Align = 8;
9268389eab190afef3462f6418b8d8fb70fb01c4005Chris Lattner      break;
9278389eab190afef3462f6418b8d8fb70fb01c4005Chris Lattner    }
9281eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
9291d75118af76cae2bfc06389cde410e14bd0a19fcDaniel Dunbar    if (const EnumType *ET = dyn_cast<EnumType>(TT))
9307176331b0f5cfaaa2b5aa487a6660e859e371119Chris Lattner      return getTypeInfo(ET->getDecl()->getIntegerType());
9317176331b0f5cfaaa2b5aa487a6660e859e371119Chris Lattner
9321d75118af76cae2bfc06389cde410e14bd0a19fcDaniel Dunbar    const RecordType *RT = cast<RecordType>(TT);
9337176331b0f5cfaaa2b5aa487a6660e859e371119Chris Lattner    const ASTRecordLayout &Layout = getASTRecordLayout(RT->getDecl());
934dd76a9ab9ea675671200f94b18ce95766841952bKen Dyck    Width = toBits(Layout.getSize());
935dac54c124e302d6f028ea5723c425b7f66fc7c71Ken Dyck    Align = toBits(Layout.getAlignment());
936dc0d73e6495404418acf8548875aeaff07791a74Chris Lattner    break;
937a7674d8a9a69f3f6fe16e70cf2a3b2b15fb7c43dChris Lattner  }
9387532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor
9399fcfe926432f3c3f7e9a61219e55c352fd358e45Chris Lattner  case Type::SubstTemplateTypeParm:
94049a832bd499d6f61c23655f1fac99f0dd229756eJohn McCall    return getTypeInfo(cast<SubstTemplateTypeParmType>(T)->
94149a832bd499d6f61c23655f1fac99f0dd229756eJohn McCall                       getReplacementType().getTypePtr());
94249a832bd499d6f61c23655f1fac99f0dd229756eJohn McCall
94334b41d939a1328f484511c6002ba2456db879a29Richard Smith  case Type::Auto: {
94434b41d939a1328f484511c6002ba2456db879a29Richard Smith    const AutoType *A = cast<AutoType>(T);
94534b41d939a1328f484511c6002ba2456db879a29Richard Smith    assert(A->isDeduced() && "Cannot request the size of a dependent type");
946dc856aff4428380baa9afb5577ea04f8fb6beb13Matt Beaumont-Gay    return getTypeInfo(A->getDeducedType().getTypePtr());
94734b41d939a1328f484511c6002ba2456db879a29Richard Smith  }
94834b41d939a1328f484511c6002ba2456db879a29Richard Smith
949075f8f1b6bed4d1b224c74f87508534cc6392ce6Abramo Bagnara  case Type::Paren:
950075f8f1b6bed4d1b224c74f87508534cc6392ce6Abramo Bagnara    return getTypeInfo(cast<ParenType>(T)->getInnerType().getTypePtr());
951075f8f1b6bed4d1b224c74f87508534cc6392ce6Abramo Bagnara
95218857644059c45da6776f1a288eec7b4cf3a844aDouglas Gregor  case Type::Typedef: {
953162e1c1b487352434552147967c3dd296ebee2f7Richard Smith    const TypedefNameDecl *Typedef = cast<TypedefType>(T)->getDecl();
954df1367af26cb2959775e9511108f12dcd2370a27Douglas Gregor    std::pair<uint64_t, unsigned> Info
955df1367af26cb2959775e9511108f12dcd2370a27Douglas Gregor      = getTypeInfo(Typedef->getUnderlyingType().getTypePtr());
956c1de52de64725945e5ae87e6f99ddedf161856e5Chris Lattner    // If the typedef has an aligned attribute on it, it overrides any computed
957c1de52de64725945e5ae87e6f99ddedf161856e5Chris Lattner    // alignment we have.  This violates the GCC documentation (which says that
958c1de52de64725945e5ae87e6f99ddedf161856e5Chris Lattner    // attribute(aligned) can only round up) but matches its implementation.
959c1de52de64725945e5ae87e6f99ddedf161856e5Chris Lattner    if (unsigned AttrAlign = Typedef->getMaxAlignment())
960c1de52de64725945e5ae87e6f99ddedf161856e5Chris Lattner      Align = AttrAlign;
961c1de52de64725945e5ae87e6f99ddedf161856e5Chris Lattner    else
962c1de52de64725945e5ae87e6f99ddedf161856e5Chris Lattner      Align = Info.second;
963df1367af26cb2959775e9511108f12dcd2370a27Douglas Gregor    Width = Info.first;
9647532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor    break;
9657176331b0f5cfaaa2b5aa487a6660e859e371119Chris Lattner  }
96618857644059c45da6776f1a288eec7b4cf3a844aDouglas Gregor
96718857644059c45da6776f1a288eec7b4cf3a844aDouglas Gregor  case Type::TypeOfExpr:
96818857644059c45da6776f1a288eec7b4cf3a844aDouglas Gregor    return getTypeInfo(cast<TypeOfExprType>(T)->getUnderlyingExpr()->getType()
96918857644059c45da6776f1a288eec7b4cf3a844aDouglas Gregor                         .getTypePtr());
97018857644059c45da6776f1a288eec7b4cf3a844aDouglas Gregor
97118857644059c45da6776f1a288eec7b4cf3a844aDouglas Gregor  case Type::TypeOf:
97218857644059c45da6776f1a288eec7b4cf3a844aDouglas Gregor    return getTypeInfo(cast<TypeOfType>(T)->getUnderlyingType().getTypePtr());
97318857644059c45da6776f1a288eec7b4cf3a844aDouglas Gregor
974395b475a4474f1c7574d927ad142ca0c7997cbcaAnders Carlsson  case Type::Decltype:
975395b475a4474f1c7574d927ad142ca0c7997cbcaAnders Carlsson    return getTypeInfo(cast<DecltypeType>(T)->getUnderlyingExpr()->getType()
976395b475a4474f1c7574d927ad142ca0c7997cbcaAnders Carlsson                        .getTypePtr());
977395b475a4474f1c7574d927ad142ca0c7997cbcaAnders Carlsson
978ca63c200346c0ca9e00194ec6e34a5a7b0ed9321Sean Hunt  case Type::UnaryTransform:
979ca63c200346c0ca9e00194ec6e34a5a7b0ed9321Sean Hunt    return getTypeInfo(cast<UnaryTransformType>(T)->getUnderlyingType());
980ca63c200346c0ca9e00194ec6e34a5a7b0ed9321Sean Hunt
981465d41b92b2c862f3062c412a0538db65c6a2661Abramo Bagnara  case Type::Elaborated:
982465d41b92b2c862f3062c412a0538db65c6a2661Abramo Bagnara    return getTypeInfo(cast<ElaboratedType>(T)->getNamedType().getTypePtr());
9831eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
9849d156a7b1b2771e191f2f5a45a7b7a694129463bJohn McCall  case Type::Attributed:
9859d156a7b1b2771e191f2f5a45a7b7a694129463bJohn McCall    return getTypeInfo(
9869d156a7b1b2771e191f2f5a45a7b7a694129463bJohn McCall                  cast<AttributedType>(T)->getEquivalentType().getTypePtr());
9879d156a7b1b2771e191f2f5a45a7b7a694129463bJohn McCall
9883e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith  case Type::TemplateSpecialization: {
9891eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    assert(getCanonicalType(T) != T &&
99018857644059c45da6776f1a288eec7b4cf3a844aDouglas Gregor           "Cannot request the size of a dependent type");
9913e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith    const TemplateSpecializationType *TST = cast<TemplateSpecializationType>(T);
9923e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith    // A type alias template specialization may refer to a typedef with the
9933e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith    // aligned attribute on it.
9943e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith    if (TST->isTypeAlias())
9953e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith      return getTypeInfo(TST->getAliasedType().getTypePtr());
9963e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith    else
9973e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith      return getTypeInfo(getCanonicalType(T));
9983e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith  }
9993e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith
100018857644059c45da6776f1a288eec7b4cf3a844aDouglas Gregor  }
10011eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1002464175bba1318bef7905122e9fda20cff926df78Chris Lattner  assert(Align && (Align & (Align-1)) == 0 && "Alignment must be power of 2");
10039e9b6dc3fd413f5341fab54b681420eeb21cd169Chris Lattner  return std::make_pair(Width, Align);
1004a7674d8a9a69f3f6fe16e70cf2a3b2b15fb7c43dChris Lattner}
1005a7674d8a9a69f3f6fe16e70cf2a3b2b15fb7c43dChris Lattner
1006eb6f5dc86531f794ba7746a2da4d28e37cf5da7eKen Dyck/// toCharUnitsFromBits - Convert a size in bits to a size in characters.
1007eb6f5dc86531f794ba7746a2da4d28e37cf5da7eKen DyckCharUnits ASTContext::toCharUnitsFromBits(int64_t BitSize) const {
1008eb6f5dc86531f794ba7746a2da4d28e37cf5da7eKen Dyck  return CharUnits::fromQuantity(BitSize / getCharWidth());
1009eb6f5dc86531f794ba7746a2da4d28e37cf5da7eKen Dyck}
1010eb6f5dc86531f794ba7746a2da4d28e37cf5da7eKen Dyck
1011dd76a9ab9ea675671200f94b18ce95766841952bKen Dyck/// toBits - Convert a size in characters to a size in characters.
1012dd76a9ab9ea675671200f94b18ce95766841952bKen Dyckint64_t ASTContext::toBits(CharUnits CharSize) const {
1013dd76a9ab9ea675671200f94b18ce95766841952bKen Dyck  return CharSize.getQuantity() * getCharWidth();
1014dd76a9ab9ea675671200f94b18ce95766841952bKen Dyck}
1015dd76a9ab9ea675671200f94b18ce95766841952bKen Dyck
1016bdc601b196c48d4cd56a5ceb45d41ae4e87371abKen Dyck/// getTypeSizeInChars - Return the size of the specified type, in characters.
1017bdc601b196c48d4cd56a5ceb45d41ae4e87371abKen Dyck/// This method does not work on incomplete types.
10184ba2a17694148e16eaa8d3917f657ffcd3667be4Jay FoadCharUnits ASTContext::getTypeSizeInChars(QualType T) const {
1019eb6f5dc86531f794ba7746a2da4d28e37cf5da7eKen Dyck  return toCharUnitsFromBits(getTypeSize(T));
1020bdc601b196c48d4cd56a5ceb45d41ae4e87371abKen Dyck}
10214ba2a17694148e16eaa8d3917f657ffcd3667be4Jay FoadCharUnits ASTContext::getTypeSizeInChars(const Type *T) const {
1022eb6f5dc86531f794ba7746a2da4d28e37cf5da7eKen Dyck  return toCharUnitsFromBits(getTypeSize(T));
1023bdc601b196c48d4cd56a5ceb45d41ae4e87371abKen Dyck}
1024bdc601b196c48d4cd56a5ceb45d41ae4e87371abKen Dyck
102516e20cce43385001f33f8e3f90ee345609c805d1Ken Dyck/// getTypeAlignInChars - Return the ABI-specified alignment of a type, in
102686fa4311c8a330957ff5b765fbb0a7750ecd38c9Ken Dyck/// characters. This method does not work on incomplete types.
10274ba2a17694148e16eaa8d3917f657ffcd3667be4Jay FoadCharUnits ASTContext::getTypeAlignInChars(QualType T) const {
1028eb6f5dc86531f794ba7746a2da4d28e37cf5da7eKen Dyck  return toCharUnitsFromBits(getTypeAlign(T));
102986fa4311c8a330957ff5b765fbb0a7750ecd38c9Ken Dyck}
10304ba2a17694148e16eaa8d3917f657ffcd3667be4Jay FoadCharUnits ASTContext::getTypeAlignInChars(const Type *T) const {
1031eb6f5dc86531f794ba7746a2da4d28e37cf5da7eKen Dyck  return toCharUnitsFromBits(getTypeAlign(T));
103286fa4311c8a330957ff5b765fbb0a7750ecd38c9Ken Dyck}
103386fa4311c8a330957ff5b765fbb0a7750ecd38c9Ken Dyck
103434ebde404dc17d89487b07e6daaf1b47d5dfee39Chris Lattner/// getPreferredTypeAlign - Return the "preferred" alignment of the specified
103534ebde404dc17d89487b07e6daaf1b47d5dfee39Chris Lattner/// type for the current target in bits.  This can be different than the ABI
103634ebde404dc17d89487b07e6daaf1b47d5dfee39Chris Lattner/// alignment in cases where it is beneficial for performance to overalign
103734ebde404dc17d89487b07e6daaf1b47d5dfee39Chris Lattner/// a data type.
10384ba2a17694148e16eaa8d3917f657ffcd3667be4Jay Foadunsigned ASTContext::getPreferredTypeAlign(const Type *T) const {
103934ebde404dc17d89487b07e6daaf1b47d5dfee39Chris Lattner  unsigned ABIAlign = getTypeAlign(T);
10401eed60297ef4701b899c6a3b9680bf08f3403422Eli Friedman
10411eed60297ef4701b899c6a3b9680bf08f3403422Eli Friedman  // Double and long long should be naturally aligned if possible.
1042183700f494ec9b6701b6efe82bcb25f4c79ba561John McCall  if (const ComplexType* CT = T->getAs<ComplexType>())
10431eed60297ef4701b899c6a3b9680bf08f3403422Eli Friedman    T = CT->getElementType().getTypePtr();
10441eed60297ef4701b899c6a3b9680bf08f3403422Eli Friedman  if (T->isSpecificBuiltinType(BuiltinType::Double) ||
10451eed60297ef4701b899c6a3b9680bf08f3403422Eli Friedman      T->isSpecificBuiltinType(BuiltinType::LongLong))
10461eed60297ef4701b899c6a3b9680bf08f3403422Eli Friedman    return std::max(ABIAlign, (unsigned)getTypeSize(T));
10471eed60297ef4701b899c6a3b9680bf08f3403422Eli Friedman
104834ebde404dc17d89487b07e6daaf1b47d5dfee39Chris Lattner  return ABIAlign;
104934ebde404dc17d89487b07e6daaf1b47d5dfee39Chris Lattner}
105034ebde404dc17d89487b07e6daaf1b47d5dfee39Chris Lattner
10518e6ac1d80055fa37b9b84029c7e751624ba7f84cFariborz Jahanian/// ShallowCollectObjCIvars -
10528e6ac1d80055fa37b9b84029c7e751624ba7f84cFariborz Jahanian/// Collect all ivars, including those synthesized, in the current class.
10538e6ac1d80055fa37b9b84029c7e751624ba7f84cFariborz Jahanian///
10548e6ac1d80055fa37b9b84029c7e751624ba7f84cFariborz Jahanianvoid ASTContext::ShallowCollectObjCIvars(const ObjCInterfaceDecl *OI,
10554ba2a17694148e16eaa8d3917f657ffcd3667be4Jay Foad                            llvm::SmallVectorImpl<ObjCIvarDecl*> &Ivars) const {
10562c18bb7c9fca66c30b6eabbdcbc6399d24a54fa9Fariborz Jahanian  // FIXME. This need be removed but there are two many places which
10572c18bb7c9fca66c30b6eabbdcbc6399d24a54fa9Fariborz Jahanian  // assume const-ness of ObjCInterfaceDecl
10582c18bb7c9fca66c30b6eabbdcbc6399d24a54fa9Fariborz Jahanian  ObjCInterfaceDecl *IDecl = const_cast<ObjCInterfaceDecl *>(OI);
10592c18bb7c9fca66c30b6eabbdcbc6399d24a54fa9Fariborz Jahanian  for (ObjCIvarDecl *Iv = IDecl->all_declared_ivar_begin(); Iv;
10602c18bb7c9fca66c30b6eabbdcbc6399d24a54fa9Fariborz Jahanian        Iv= Iv->getNextIvar())
10612c18bb7c9fca66c30b6eabbdcbc6399d24a54fa9Fariborz Jahanian    Ivars.push_back(Iv);
10622c18bb7c9fca66c30b6eabbdcbc6399d24a54fa9Fariborz Jahanian}
10632c18bb7c9fca66c30b6eabbdcbc6399d24a54fa9Fariborz Jahanian
10642c18bb7c9fca66c30b6eabbdcbc6399d24a54fa9Fariborz Jahanian/// DeepCollectObjCIvars -
10652c18bb7c9fca66c30b6eabbdcbc6399d24a54fa9Fariborz Jahanian/// This routine first collects all declared, but not synthesized, ivars in
10662c18bb7c9fca66c30b6eabbdcbc6399d24a54fa9Fariborz Jahanian/// super class and then collects all ivars, including those synthesized for
10672c18bb7c9fca66c30b6eabbdcbc6399d24a54fa9Fariborz Jahanian/// current class. This routine is used for implementation of current class
10682c18bb7c9fca66c30b6eabbdcbc6399d24a54fa9Fariborz Jahanian/// when all ivars, declared and synthesized are known.
10699820074dd47d37681085e964cd3392ac0b3e67b9Fariborz Jahanian///
10702c18bb7c9fca66c30b6eabbdcbc6399d24a54fa9Fariborz Jahanianvoid ASTContext::DeepCollectObjCIvars(const ObjCInterfaceDecl *OI,
10712c18bb7c9fca66c30b6eabbdcbc6399d24a54fa9Fariborz Jahanian                                      bool leafClass,
10724ba2a17694148e16eaa8d3917f657ffcd3667be4Jay Foad                            llvm::SmallVectorImpl<ObjCIvarDecl*> &Ivars) const {
10732c18bb7c9fca66c30b6eabbdcbc6399d24a54fa9Fariborz Jahanian  if (const ObjCInterfaceDecl *SuperClass = OI->getSuperClass())
10742c18bb7c9fca66c30b6eabbdcbc6399d24a54fa9Fariborz Jahanian    DeepCollectObjCIvars(SuperClass, false, Ivars);
10752c18bb7c9fca66c30b6eabbdcbc6399d24a54fa9Fariborz Jahanian  if (!leafClass) {
10762c18bb7c9fca66c30b6eabbdcbc6399d24a54fa9Fariborz Jahanian    for (ObjCInterfaceDecl::ivar_iterator I = OI->ivar_begin(),
10772c18bb7c9fca66c30b6eabbdcbc6399d24a54fa9Fariborz Jahanian         E = OI->ivar_end(); I != E; ++I)
107811062e11236b7bc689dad150e8b490fd6b063ec3Fariborz Jahanian      Ivars.push_back(*I);
107911062e11236b7bc689dad150e8b490fd6b063ec3Fariborz Jahanian  }
10802c18bb7c9fca66c30b6eabbdcbc6399d24a54fa9Fariborz Jahanian  else
10812c18bb7c9fca66c30b6eabbdcbc6399d24a54fa9Fariborz Jahanian    ShallowCollectObjCIvars(OI, Ivars);
10829820074dd47d37681085e964cd3392ac0b3e67b9Fariborz Jahanian}
10839820074dd47d37681085e964cd3392ac0b3e67b9Fariborz Jahanian
1084e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanian/// CollectInheritedProtocols - Collect all protocols in current class and
1085e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanian/// those inherited by it.
1086e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanianvoid ASTContext::CollectInheritedProtocols(const Decl *CDecl,
1087432a8893f7e30d141d7f279bd00b741a3cdac81fFariborz Jahanian                          llvm::SmallPtrSet<ObjCProtocolDecl*, 8> &Protocols) {
1088e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanian  if (const ObjCInterfaceDecl *OI = dyn_cast<ObjCInterfaceDecl>(CDecl)) {
108953b9441b5a81a24fa1f66f3f6416f1e36baa9c2fTed Kremenek    // We can use protocol_iterator here instead of
109053b9441b5a81a24fa1f66f3f6416f1e36baa9c2fTed Kremenek    // all_referenced_protocol_iterator since we are walking all categories.
109153b9441b5a81a24fa1f66f3f6416f1e36baa9c2fTed Kremenek    for (ObjCInterfaceDecl::all_protocol_iterator P = OI->all_referenced_protocol_begin(),
109253b9441b5a81a24fa1f66f3f6416f1e36baa9c2fTed Kremenek         PE = OI->all_referenced_protocol_end(); P != PE; ++P) {
1093e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanian      ObjCProtocolDecl *Proto = (*P);
1094432a8893f7e30d141d7f279bd00b741a3cdac81fFariborz Jahanian      Protocols.insert(Proto);
1095e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanian      for (ObjCProtocolDecl::protocol_iterator P = Proto->protocol_begin(),
1096b2f812165676230bce5d0215e49a4749c451ca9cFariborz Jahanian           PE = Proto->protocol_end(); P != PE; ++P) {
1097b2f812165676230bce5d0215e49a4749c451ca9cFariborz Jahanian        Protocols.insert(*P);
1098e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanian        CollectInheritedProtocols(*P, Protocols);
1099e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanian      }
1100b2f812165676230bce5d0215e49a4749c451ca9cFariborz Jahanian    }
1101e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanian
1102e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanian    // Categories of this Interface.
1103e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanian    for (const ObjCCategoryDecl *CDeclChain = OI->getCategoryList();
1104e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanian         CDeclChain; CDeclChain = CDeclChain->getNextClassCategory())
1105e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanian      CollectInheritedProtocols(CDeclChain, Protocols);
1106e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanian    if (ObjCInterfaceDecl *SD = OI->getSuperClass())
1107e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanian      while (SD) {
1108e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanian        CollectInheritedProtocols(SD, Protocols);
1109e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanian        SD = SD->getSuperClass();
1110e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanian      }
1111b170ca5f4a8397c10e52050ff5df6885a3e6eca9Benjamin Kramer  } else if (const ObjCCategoryDecl *OC = dyn_cast<ObjCCategoryDecl>(CDecl)) {
111253b9441b5a81a24fa1f66f3f6416f1e36baa9c2fTed Kremenek    for (ObjCCategoryDecl::protocol_iterator P = OC->protocol_begin(),
1113e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanian         PE = OC->protocol_end(); P != PE; ++P) {
1114e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanian      ObjCProtocolDecl *Proto = (*P);
1115432a8893f7e30d141d7f279bd00b741a3cdac81fFariborz Jahanian      Protocols.insert(Proto);
1116e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanian      for (ObjCProtocolDecl::protocol_iterator P = Proto->protocol_begin(),
1117e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanian           PE = Proto->protocol_end(); P != PE; ++P)
1118e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanian        CollectInheritedProtocols(*P, Protocols);
1119e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanian    }
1120b170ca5f4a8397c10e52050ff5df6885a3e6eca9Benjamin Kramer  } else if (const ObjCProtocolDecl *OP = dyn_cast<ObjCProtocolDecl>(CDecl)) {
1121e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanian    for (ObjCProtocolDecl::protocol_iterator P = OP->protocol_begin(),
1122e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanian         PE = OP->protocol_end(); P != PE; ++P) {
1123e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanian      ObjCProtocolDecl *Proto = (*P);
1124432a8893f7e30d141d7f279bd00b741a3cdac81fFariborz Jahanian      Protocols.insert(Proto);
1125e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanian      for (ObjCProtocolDecl::protocol_iterator P = Proto->protocol_begin(),
1126e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanian           PE = Proto->protocol_end(); P != PE; ++P)
1127e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanian        CollectInheritedProtocols(*P, Protocols);
1128e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanian    }
1129e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanian  }
1130e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanian}
1131e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanian
11324ba2a17694148e16eaa8d3917f657ffcd3667be4Jay Foadunsigned ASTContext::CountNonClassIvars(const ObjCInterfaceDecl *OI) const {
11333bfacdf6f0706987c1b33c625cd68fb880881c22Fariborz Jahanian  unsigned count = 0;
11343bfacdf6f0706987c1b33c625cd68fb880881c22Fariborz Jahanian  // Count ivars declared in class extension.
113580aa1cd7973561889e51c1c152c8990a8de9c953Fariborz Jahanian  for (const ObjCCategoryDecl *CDecl = OI->getFirstClassExtension(); CDecl;
113680aa1cd7973561889e51c1c152c8990a8de9c953Fariborz Jahanian       CDecl = CDecl->getNextClassExtension())
1137b170ca5f4a8397c10e52050ff5df6885a3e6eca9Benjamin Kramer    count += CDecl->ivar_size();
1138b170ca5f4a8397c10e52050ff5df6885a3e6eca9Benjamin Kramer
11393bfacdf6f0706987c1b33c625cd68fb880881c22Fariborz Jahanian  // Count ivar defined in this class's implementation.  This
11403bfacdf6f0706987c1b33c625cd68fb880881c22Fariborz Jahanian  // includes synthesized ivars.
11413bfacdf6f0706987c1b33c625cd68fb880881c22Fariborz Jahanian  if (ObjCImplementationDecl *ImplDecl = OI->getImplementation())
1142b170ca5f4a8397c10e52050ff5df6885a3e6eca9Benjamin Kramer    count += ImplDecl->ivar_size();
1143b170ca5f4a8397c10e52050ff5df6885a3e6eca9Benjamin Kramer
11448e6ac1d80055fa37b9b84029c7e751624ba7f84cFariborz Jahanian  return count;
11458e6ac1d80055fa37b9b84029c7e751624ba7f84cFariborz Jahanian}
11468e6ac1d80055fa37b9b84029c7e751624ba7f84cFariborz Jahanian
11478a1d722f13df383600f36d77f842957c8adb5f1bArgyrios Kyrtzidis/// \brief Get the implementation of ObjCInterfaceDecl,or NULL if none exists.
11488a1d722f13df383600f36d77f842957c8adb5f1bArgyrios KyrtzidisObjCImplementationDecl *ASTContext::getObjCImplementation(ObjCInterfaceDecl *D) {
11498a1d722f13df383600f36d77f842957c8adb5f1bArgyrios Kyrtzidis  llvm::DenseMap<ObjCContainerDecl*, ObjCImplDecl*>::iterator
11508a1d722f13df383600f36d77f842957c8adb5f1bArgyrios Kyrtzidis    I = ObjCImpls.find(D);
11518a1d722f13df383600f36d77f842957c8adb5f1bArgyrios Kyrtzidis  if (I != ObjCImpls.end())
11528a1d722f13df383600f36d77f842957c8adb5f1bArgyrios Kyrtzidis    return cast<ObjCImplementationDecl>(I->second);
11538a1d722f13df383600f36d77f842957c8adb5f1bArgyrios Kyrtzidis  return 0;
11548a1d722f13df383600f36d77f842957c8adb5f1bArgyrios Kyrtzidis}
11558a1d722f13df383600f36d77f842957c8adb5f1bArgyrios Kyrtzidis/// \brief Get the implementation of ObjCCategoryDecl, or NULL if none exists.
11568a1d722f13df383600f36d77f842957c8adb5f1bArgyrios KyrtzidisObjCCategoryImplDecl *ASTContext::getObjCImplementation(ObjCCategoryDecl *D) {
11578a1d722f13df383600f36d77f842957c8adb5f1bArgyrios Kyrtzidis  llvm::DenseMap<ObjCContainerDecl*, ObjCImplDecl*>::iterator
11588a1d722f13df383600f36d77f842957c8adb5f1bArgyrios Kyrtzidis    I = ObjCImpls.find(D);
11598a1d722f13df383600f36d77f842957c8adb5f1bArgyrios Kyrtzidis  if (I != ObjCImpls.end())
11608a1d722f13df383600f36d77f842957c8adb5f1bArgyrios Kyrtzidis    return cast<ObjCCategoryImplDecl>(I->second);
11618a1d722f13df383600f36d77f842957c8adb5f1bArgyrios Kyrtzidis  return 0;
11628a1d722f13df383600f36d77f842957c8adb5f1bArgyrios Kyrtzidis}
11638a1d722f13df383600f36d77f842957c8adb5f1bArgyrios Kyrtzidis
11648a1d722f13df383600f36d77f842957c8adb5f1bArgyrios Kyrtzidis/// \brief Set the implementation of ObjCInterfaceDecl.
11658a1d722f13df383600f36d77f842957c8adb5f1bArgyrios Kyrtzidisvoid ASTContext::setObjCImplementation(ObjCInterfaceDecl *IFaceD,
11668a1d722f13df383600f36d77f842957c8adb5f1bArgyrios Kyrtzidis                           ObjCImplementationDecl *ImplD) {
11678a1d722f13df383600f36d77f842957c8adb5f1bArgyrios Kyrtzidis  assert(IFaceD && ImplD && "Passed null params");
11688a1d722f13df383600f36d77f842957c8adb5f1bArgyrios Kyrtzidis  ObjCImpls[IFaceD] = ImplD;
11698a1d722f13df383600f36d77f842957c8adb5f1bArgyrios Kyrtzidis}
11708a1d722f13df383600f36d77f842957c8adb5f1bArgyrios Kyrtzidis/// \brief Set the implementation of ObjCCategoryDecl.
11718a1d722f13df383600f36d77f842957c8adb5f1bArgyrios Kyrtzidisvoid ASTContext::setObjCImplementation(ObjCCategoryDecl *CatD,
11728a1d722f13df383600f36d77f842957c8adb5f1bArgyrios Kyrtzidis                           ObjCCategoryImplDecl *ImplD) {
11738a1d722f13df383600f36d77f842957c8adb5f1bArgyrios Kyrtzidis  assert(CatD && ImplD && "Passed null params");
11748a1d722f13df383600f36d77f842957c8adb5f1bArgyrios Kyrtzidis  ObjCImpls[CatD] = ImplD;
11758a1d722f13df383600f36d77f842957c8adb5f1bArgyrios Kyrtzidis}
11768a1d722f13df383600f36d77f842957c8adb5f1bArgyrios Kyrtzidis
11771ceee5c42d5c410217f67d384eecc6ea4a2bba9bFariborz Jahanian/// \brief Get the copy initialization expression of VarDecl,or NULL if
11781ceee5c42d5c410217f67d384eecc6ea4a2bba9bFariborz Jahanian/// none exists.
1179830937bc1100fba7682f7c32c40512085870f50cFariborz JahanianExpr *ASTContext::getBlockVarCopyInits(const VarDecl*VD) {
1180d016ec204c54d766c4561d43548551ffde0e6b43Fariborz Jahanian  assert(VD && "Passed null params");
1181d016ec204c54d766c4561d43548551ffde0e6b43Fariborz Jahanian  assert(VD->hasAttr<BlocksAttr>() &&
1182d016ec204c54d766c4561d43548551ffde0e6b43Fariborz Jahanian         "getBlockVarCopyInits - not __block var");
1183830937bc1100fba7682f7c32c40512085870f50cFariborz Jahanian  llvm::DenseMap<const VarDecl*, Expr*>::iterator
1184d016ec204c54d766c4561d43548551ffde0e6b43Fariborz Jahanian    I = BlockVarCopyInits.find(VD);
11851ceee5c42d5c410217f67d384eecc6ea4a2bba9bFariborz Jahanian  return (I != BlockVarCopyInits.end()) ? cast<Expr>(I->second) : 0;
11861ceee5c42d5c410217f67d384eecc6ea4a2bba9bFariborz Jahanian}
11871ceee5c42d5c410217f67d384eecc6ea4a2bba9bFariborz Jahanian
11881ceee5c42d5c410217f67d384eecc6ea4a2bba9bFariborz Jahanian/// \brief Set the copy inialization expression of a block var decl.
11891ceee5c42d5c410217f67d384eecc6ea4a2bba9bFariborz Jahanianvoid ASTContext::setBlockVarCopyInits(VarDecl*VD, Expr* Init) {
11901ceee5c42d5c410217f67d384eecc6ea4a2bba9bFariborz Jahanian  assert(VD && Init && "Passed null params");
1191d016ec204c54d766c4561d43548551ffde0e6b43Fariborz Jahanian  assert(VD->hasAttr<BlocksAttr>() &&
1192d016ec204c54d766c4561d43548551ffde0e6b43Fariborz Jahanian         "setBlockVarCopyInits - not __block var");
11931ceee5c42d5c410217f67d384eecc6ea4a2bba9bFariborz Jahanian  BlockVarCopyInits[VD] = Init;
11941ceee5c42d5c410217f67d384eecc6ea4a2bba9bFariborz Jahanian}
11951ceee5c42d5c410217f67d384eecc6ea4a2bba9bFariborz Jahanian
1196a93c934af4fbf97cbe8e649d82e68ccacfe57c95John McCall/// \brief Allocate an uninitialized TypeSourceInfo.
1197b17166c8077cd900cca83a895c43b30ea6660598Argyrios Kyrtzidis///
1198a93c934af4fbf97cbe8e649d82e68ccacfe57c95John McCall/// The caller should initialize the memory held by TypeSourceInfo using
1199b17166c8077cd900cca83a895c43b30ea6660598Argyrios Kyrtzidis/// the TypeLoc wrappers.
1200b17166c8077cd900cca83a895c43b30ea6660598Argyrios Kyrtzidis///
1201b17166c8077cd900cca83a895c43b30ea6660598Argyrios Kyrtzidis/// \param T the type that will be the basis for type source info. This type
1202b17166c8077cd900cca83a895c43b30ea6660598Argyrios Kyrtzidis/// should refer to how the declarator was written in source code, not to
1203b17166c8077cd900cca83a895c43b30ea6660598Argyrios Kyrtzidis/// what type semantic analysis resolved the declarator to.
1204a93c934af4fbf97cbe8e649d82e68ccacfe57c95John McCallTypeSourceInfo *ASTContext::CreateTypeSourceInfo(QualType T,
12054ba2a17694148e16eaa8d3917f657ffcd3667be4Jay Foad                                                 unsigned DataSize) const {
1206109de5ead1dfcb3bc985cddb8cb3ed5bcecad88dJohn McCall  if (!DataSize)
1207109de5ead1dfcb3bc985cddb8cb3ed5bcecad88dJohn McCall    DataSize = TypeLoc::getFullDataSizeForType(T);
1208109de5ead1dfcb3bc985cddb8cb3ed5bcecad88dJohn McCall  else
1209109de5ead1dfcb3bc985cddb8cb3ed5bcecad88dJohn McCall    assert(DataSize == TypeLoc::getFullDataSizeForType(T) &&
1210a93c934af4fbf97cbe8e649d82e68ccacfe57c95John McCall           "incorrect data size provided to CreateTypeSourceInfo!");
1211109de5ead1dfcb3bc985cddb8cb3ed5bcecad88dJohn McCall
1212a93c934af4fbf97cbe8e649d82e68ccacfe57c95John McCall  TypeSourceInfo *TInfo =
1213a93c934af4fbf97cbe8e649d82e68ccacfe57c95John McCall    (TypeSourceInfo*)BumpAlloc.Allocate(sizeof(TypeSourceInfo) + DataSize, 8);
1214a93c934af4fbf97cbe8e649d82e68ccacfe57c95John McCall  new (TInfo) TypeSourceInfo(T);
1215a93c934af4fbf97cbe8e649d82e68ccacfe57c95John McCall  return TInfo;
1216b17166c8077cd900cca83a895c43b30ea6660598Argyrios Kyrtzidis}
1217b17166c8077cd900cca83a895c43b30ea6660598Argyrios Kyrtzidis
1218a93c934af4fbf97cbe8e649d82e68ccacfe57c95John McCallTypeSourceInfo *ASTContext::getTrivialTypeSourceInfo(QualType T,
12196952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor                                                     SourceLocation L) const {
1220a93c934af4fbf97cbe8e649d82e68ccacfe57c95John McCall  TypeSourceInfo *DI = CreateTypeSourceInfo(T);
1221c21c7e9c2cded68f91be15be6847c9649242dc17Douglas Gregor  DI->getTypeLoc().initialize(const_cast<ASTContext &>(*this), L);
1222a4eb74d4dfe126c686dc708fec444c85ffb73b47John McCall  return DI;
1223a4eb74d4dfe126c686dc708fec444c85ffb73b47John McCall}
1224a4eb74d4dfe126c686dc708fec444c85ffb73b47John McCall
1225b2dbbb99e12806eaaf53b7ccabc32f42b5719443Daniel Dunbarconst ASTRecordLayout &
12264ba2a17694148e16eaa8d3917f657ffcd3667be4Jay FoadASTContext::getASTObjCInterfaceLayout(const ObjCInterfaceDecl *D) const {
1227b2dbbb99e12806eaaf53b7ccabc32f42b5719443Daniel Dunbar  return getObjCLayout(D, 0);
1228b2dbbb99e12806eaaf53b7ccabc32f42b5719443Daniel Dunbar}
1229b2dbbb99e12806eaaf53b7ccabc32f42b5719443Daniel Dunbar
1230b2dbbb99e12806eaaf53b7ccabc32f42b5719443Daniel Dunbarconst ASTRecordLayout &
12314ba2a17694148e16eaa8d3917f657ffcd3667be4Jay FoadASTContext::getASTObjCImplementationLayout(
12324ba2a17694148e16eaa8d3917f657ffcd3667be4Jay Foad                                        const ObjCImplementationDecl *D) const {
1233b2dbbb99e12806eaaf53b7ccabc32f42b5719443Daniel Dunbar  return getObjCLayout(D->getClassInterface(), D);
1234b2dbbb99e12806eaaf53b7ccabc32f42b5719443Daniel Dunbar}
1235b2dbbb99e12806eaaf53b7ccabc32f42b5719443Daniel Dunbar
1236a7674d8a9a69f3f6fe16e70cf2a3b2b15fb7c43dChris Lattner//===----------------------------------------------------------------------===//
1237a7674d8a9a69f3f6fe16e70cf2a3b2b15fb7c43dChris Lattner//                   Type creation/memoization methods
1238a7674d8a9a69f3f6fe16e70cf2a3b2b15fb7c43dChris Lattner//===----------------------------------------------------------------------===//
1239a7674d8a9a69f3f6fe16e70cf2a3b2b15fb7c43dChris Lattner
12404ba2a17694148e16eaa8d3917f657ffcd3667be4Jay FoadQualType
12413b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCallASTContext::getExtQualType(const Type *baseType, Qualifiers quals) const {
12423b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall  unsigned fastQuals = quals.getFastQualifiers();
12433b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall  quals.removeFastQualifiers();
12440953e767ff7817f97b3ab20896b229891eeff45bJohn McCall
12450953e767ff7817f97b3ab20896b229891eeff45bJohn McCall  // Check if we've already instantiated this type.
12460953e767ff7817f97b3ab20896b229891eeff45bJohn McCall  llvm::FoldingSetNodeID ID;
12473b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall  ExtQuals::Profile(ID, baseType, quals);
12483b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall  void *insertPos = 0;
12493b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall  if (ExtQuals *eq = ExtQualNodes.FindNodeOrInsertPos(ID, insertPos)) {
12503b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall    assert(eq->getQualifiers() == quals);
12513b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall    return QualType(eq, fastQuals);
12520953e767ff7817f97b3ab20896b229891eeff45bJohn McCall  }
12530953e767ff7817f97b3ab20896b229891eeff45bJohn McCall
12543b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall  // If the base type is not canonical, make the appropriate canonical type.
12553b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall  QualType canon;
12563b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall  if (!baseType->isCanonicalUnqualified()) {
12573b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall    SplitQualType canonSplit = baseType->getCanonicalTypeInternal().split();
12583b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall    canonSplit.second.addConsistentQualifiers(quals);
12593b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall    canon = getExtQualType(canonSplit.first, canonSplit.second);
12603b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall
12613b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall    // Re-find the insert position.
12623b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall    (void) ExtQualNodes.FindNodeOrInsertPos(ID, insertPos);
12633b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall  }
12643b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall
12653b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall  ExtQuals *eq = new (*this, TypeAlignment) ExtQuals(baseType, canon, quals);
12663b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall  ExtQualNodes.InsertNode(eq, insertPos);
12673b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall  return QualType(eq, fastQuals);
12680953e767ff7817f97b3ab20896b229891eeff45bJohn McCall}
12690953e767ff7817f97b3ab20896b229891eeff45bJohn McCall
12704ba2a17694148e16eaa8d3917f657ffcd3667be4Jay FoadQualType
12714ba2a17694148e16eaa8d3917f657ffcd3667be4Jay FoadASTContext::getAddrSpaceQualType(QualType T, unsigned AddressSpace) const {
1272f52ab250ff92bc51a9ac9a8e19bd43b63a5f844fChris Lattner  QualType CanT = getCanonicalType(T);
1273f52ab250ff92bc51a9ac9a8e19bd43b63a5f844fChris Lattner  if (CanT.getAddressSpace() == AddressSpace)
1274f46699ce225811d8d9dbab9d00189a0e54469457Chris Lattner    return T;
1275b7d2553edd2532d29b98b9e76bcf6a62bc48b417Chris Lattner
12760953e767ff7817f97b3ab20896b229891eeff45bJohn McCall  // If we are composing extended qualifiers together, merge together
12770953e767ff7817f97b3ab20896b229891eeff45bJohn McCall  // into one ExtQuals node.
12780953e767ff7817f97b3ab20896b229891eeff45bJohn McCall  QualifierCollector Quals;
12790953e767ff7817f97b3ab20896b229891eeff45bJohn McCall  const Type *TypeNode = Quals.strip(T);
12801eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
12810953e767ff7817f97b3ab20896b229891eeff45bJohn McCall  // If this type already has an address space specified, it cannot get
12820953e767ff7817f97b3ab20896b229891eeff45bJohn McCall  // another one.
12830953e767ff7817f97b3ab20896b229891eeff45bJohn McCall  assert(!Quals.hasAddressSpace() &&
12840953e767ff7817f97b3ab20896b229891eeff45bJohn McCall         "Type cannot be in multiple addr spaces!");
12850953e767ff7817f97b3ab20896b229891eeff45bJohn McCall  Quals.addAddressSpace(AddressSpace);
12861eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
12870953e767ff7817f97b3ab20896b229891eeff45bJohn McCall  return getExtQualType(TypeNode, Quals);
1288ebb97e98c03f8d7034bd3748a10e35f39a95c289Christopher Lamb}
1289ebb97e98c03f8d7034bd3748a10e35f39a95c289Christopher Lamb
1290b7d2553edd2532d29b98b9e76bcf6a62bc48b417Chris LattnerQualType ASTContext::getObjCGCQualType(QualType T,
12914ba2a17694148e16eaa8d3917f657ffcd3667be4Jay Foad                                       Qualifiers::GC GCAttr) const {
1292d33d9c0cc0cfdcd0b10f35a6acdfb25da4a64f19Fariborz Jahanian  QualType CanT = getCanonicalType(T);
1293b7d2553edd2532d29b98b9e76bcf6a62bc48b417Chris Lattner  if (CanT.getObjCGCAttr() == GCAttr)
1294d33d9c0cc0cfdcd0b10f35a6acdfb25da4a64f19Fariborz Jahanian    return T;
12951eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
12967f040a9d817cd1c72b565e92abff473510bf9e1dJohn McCall  if (const PointerType *ptr = T->getAs<PointerType>()) {
12977f040a9d817cd1c72b565e92abff473510bf9e1dJohn McCall    QualType Pointee = ptr->getPointeeType();
129858f9f2c884af6b72d036b746a016d8031d31cb7aSteve Naroff    if (Pointee->isAnyPointerType()) {
12994027cd1b924e29784a49085b1717f35cdd719146Fariborz Jahanian      QualType ResultType = getObjCGCQualType(Pointee, GCAttr);
13004027cd1b924e29784a49085b1717f35cdd719146Fariborz Jahanian      return getPointerType(ResultType);
13014027cd1b924e29784a49085b1717f35cdd719146Fariborz Jahanian    }
13024027cd1b924e29784a49085b1717f35cdd719146Fariborz Jahanian  }
13031eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
13040953e767ff7817f97b3ab20896b229891eeff45bJohn McCall  // If we are composing extended qualifiers together, merge together
13050953e767ff7817f97b3ab20896b229891eeff45bJohn McCall  // into one ExtQuals node.
13060953e767ff7817f97b3ab20896b229891eeff45bJohn McCall  QualifierCollector Quals;
13070953e767ff7817f97b3ab20896b229891eeff45bJohn McCall  const Type *TypeNode = Quals.strip(T);
13081eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
13090953e767ff7817f97b3ab20896b229891eeff45bJohn McCall  // If this type already has an ObjCGC specified, it cannot get
13100953e767ff7817f97b3ab20896b229891eeff45bJohn McCall  // another one.
13110953e767ff7817f97b3ab20896b229891eeff45bJohn McCall  assert(!Quals.hasObjCGCAttr() &&
13120953e767ff7817f97b3ab20896b229891eeff45bJohn McCall         "Type cannot have multiple ObjCGCs!");
13130953e767ff7817f97b3ab20896b229891eeff45bJohn McCall  Quals.addObjCGCAttr(GCAttr);
13141eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
13150953e767ff7817f97b3ab20896b229891eeff45bJohn McCall  return getExtQualType(TypeNode, Quals);
1316d33d9c0cc0cfdcd0b10f35a6acdfb25da4a64f19Fariborz Jahanian}
1317a7674d8a9a69f3f6fe16e70cf2a3b2b15fb7c43dChris Lattner
1318e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCallconst FunctionType *ASTContext::adjustFunctionType(const FunctionType *T,
1319e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall                                                   FunctionType::ExtInfo Info) {
1320e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall  if (T->getExtInfo() == Info)
1321e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall    return T;
1322e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall
1323e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall  QualType Result;
1324e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall  if (const FunctionNoProtoType *FNPT = dyn_cast<FunctionNoProtoType>(T)) {
1325e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall    Result = getFunctionNoProtoType(FNPT->getResultType(), Info);
1326e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall  } else {
1327e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall    const FunctionProtoType *FPT = cast<FunctionProtoType>(T);
1328e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall    FunctionProtoType::ExtProtoInfo EPI = FPT->getExtProtoInfo();
1329e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall    EPI.ExtInfo = Info;
1330e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall    Result = getFunctionType(FPT->getResultType(), FPT->arg_type_begin(),
1331e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall                             FPT->getNumArgs(), EPI);
1332e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall  }
1333e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall
1334e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall  return cast<FunctionType>(Result.getTypePtr());
1335e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall}
1336e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall
13375f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// getComplexType - Return the uniqued reference to the type for a complex
13385f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// number with the specified element type.
13394ba2a17694148e16eaa8d3917f657ffcd3667be4Jay FoadQualType ASTContext::getComplexType(QualType T) const {
13405f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // Unique pointers, to guarantee there is only one pointer of a particular
13415f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // structure.
13425f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  llvm::FoldingSetNodeID ID;
13435f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  ComplexType::Profile(ID, T);
13441eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
13455f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  void *InsertPos = 0;
13465f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  if (ComplexType *CT = ComplexTypes.FindNodeOrInsertPos(ID, InsertPos))
13475f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    return QualType(CT, 0);
13481eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
13495f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // If the pointee type isn't canonical, this won't be a canonical type either,
13505f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // so fill in the canonical type field.
13515f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  QualType Canonical;
1352467b27b9a24bdc823218ad1ad0e37673b6cc1e83John McCall  if (!T.isCanonical()) {
1353f52ab250ff92bc51a9ac9a8e19bd43b63a5f844fChris Lattner    Canonical = getComplexType(getCanonicalType(T));
13541eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
13555f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    // Get the new insert position for the node we care about.
13565f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    ComplexType *NewIP = ComplexTypes.FindNodeOrInsertPos(ID, InsertPos);
1357c6ed729f669044f5072a49d79041f455d971ece3Jeffrey Yasskin    assert(NewIP == 0 && "Shouldn't be in the map!"); (void)NewIP;
13585f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
13596b304a0254a13f42390b865ff5ba668a49cc58aeJohn McCall  ComplexType *New = new (*this, TypeAlignment) ComplexType(T, Canonical);
13605f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  Types.push_back(New);
13615f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  ComplexTypes.InsertNode(New, InsertPos);
13625f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  return QualType(New, 0);
13635f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
13645f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
13655f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// getPointerType - Return the uniqued reference to the type for a pointer to
13665f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// the specified type.
13674ba2a17694148e16eaa8d3917f657ffcd3667be4Jay FoadQualType ASTContext::getPointerType(QualType T) const {
13685f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // Unique pointers, to guarantee there is only one pointer of a particular
13695f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // structure.
13705f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  llvm::FoldingSetNodeID ID;
13715f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  PointerType::Profile(ID, T);
13721eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
13735f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  void *InsertPos = 0;
13745f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  if (PointerType *PT = PointerTypes.FindNodeOrInsertPos(ID, InsertPos))
13755f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    return QualType(PT, 0);
13761eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
13775f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // If the pointee type isn't canonical, this won't be a canonical type either,
13785f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // so fill in the canonical type field.
13795f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  QualType Canonical;
1380467b27b9a24bdc823218ad1ad0e37673b6cc1e83John McCall  if (!T.isCanonical()) {
1381f52ab250ff92bc51a9ac9a8e19bd43b63a5f844fChris Lattner    Canonical = getPointerType(getCanonicalType(T));
13821eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
13835f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    // Get the new insert position for the node we care about.
13845f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    PointerType *NewIP = PointerTypes.FindNodeOrInsertPos(ID, InsertPos);
1385c6ed729f669044f5072a49d79041f455d971ece3Jeffrey Yasskin    assert(NewIP == 0 && "Shouldn't be in the map!"); (void)NewIP;
13865f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
13876b304a0254a13f42390b865ff5ba668a49cc58aeJohn McCall  PointerType *New = new (*this, TypeAlignment) PointerType(T, Canonical);
13885f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  Types.push_back(New);
13895f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  PointerTypes.InsertNode(New, InsertPos);
13905f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  return QualType(New, 0);
13915f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
13925f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
13931eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// getBlockPointerType - Return the uniqued reference to the type for
13945618bd4a52c45fbbb605e3ba885663b2164db8a3Steve Naroff/// a pointer to the specified block.
13954ba2a17694148e16eaa8d3917f657ffcd3667be4Jay FoadQualType ASTContext::getBlockPointerType(QualType T) const {
1396296e8d5fdcf9946f51e866adc8d281379e51efe9Steve Naroff  assert(T->isFunctionType() && "block of function types only");
1397296e8d5fdcf9946f51e866adc8d281379e51efe9Steve Naroff  // Unique pointers, to guarantee there is only one block of a particular
13985618bd4a52c45fbbb605e3ba885663b2164db8a3Steve Naroff  // structure.
13995618bd4a52c45fbbb605e3ba885663b2164db8a3Steve Naroff  llvm::FoldingSetNodeID ID;
14005618bd4a52c45fbbb605e3ba885663b2164db8a3Steve Naroff  BlockPointerType::Profile(ID, T);
14011eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
14025618bd4a52c45fbbb605e3ba885663b2164db8a3Steve Naroff  void *InsertPos = 0;
14035618bd4a52c45fbbb605e3ba885663b2164db8a3Steve Naroff  if (BlockPointerType *PT =
14045618bd4a52c45fbbb605e3ba885663b2164db8a3Steve Naroff        BlockPointerTypes.FindNodeOrInsertPos(ID, InsertPos))
14055618bd4a52c45fbbb605e3ba885663b2164db8a3Steve Naroff    return QualType(PT, 0);
14061eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
14071eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  // If the block pointee type isn't canonical, this won't be a canonical
14085618bd4a52c45fbbb605e3ba885663b2164db8a3Steve Naroff  // type either so fill in the canonical type field.
14095618bd4a52c45fbbb605e3ba885663b2164db8a3Steve Naroff  QualType Canonical;
1410467b27b9a24bdc823218ad1ad0e37673b6cc1e83John McCall  if (!T.isCanonical()) {
14115618bd4a52c45fbbb605e3ba885663b2164db8a3Steve Naroff    Canonical = getBlockPointerType(getCanonicalType(T));
14121eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
14135618bd4a52c45fbbb605e3ba885663b2164db8a3Steve Naroff    // Get the new insert position for the node we care about.
14145618bd4a52c45fbbb605e3ba885663b2164db8a3Steve Naroff    BlockPointerType *NewIP =
14155618bd4a52c45fbbb605e3ba885663b2164db8a3Steve Naroff      BlockPointerTypes.FindNodeOrInsertPos(ID, InsertPos);
1416c6ed729f669044f5072a49d79041f455d971ece3Jeffrey Yasskin    assert(NewIP == 0 && "Shouldn't be in the map!"); (void)NewIP;
14175618bd4a52c45fbbb605e3ba885663b2164db8a3Steve Naroff  }
14186b304a0254a13f42390b865ff5ba668a49cc58aeJohn McCall  BlockPointerType *New
14196b304a0254a13f42390b865ff5ba668a49cc58aeJohn McCall    = new (*this, TypeAlignment) BlockPointerType(T, Canonical);
14205618bd4a52c45fbbb605e3ba885663b2164db8a3Steve Naroff  Types.push_back(New);
14215618bd4a52c45fbbb605e3ba885663b2164db8a3Steve Naroff  BlockPointerTypes.InsertNode(New, InsertPos);
14225618bd4a52c45fbbb605e3ba885663b2164db8a3Steve Naroff  return QualType(New, 0);
14235618bd4a52c45fbbb605e3ba885663b2164db8a3Steve Naroff}
14245618bd4a52c45fbbb605e3ba885663b2164db8a3Steve Naroff
14257c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl/// getLValueReferenceType - Return the uniqued reference to the type for an
14267c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl/// lvalue reference to the specified type.
14274ba2a17694148e16eaa8d3917f657ffcd3667be4Jay FoadQualType
14284ba2a17694148e16eaa8d3917f657ffcd3667be4Jay FoadASTContext::getLValueReferenceType(QualType T, bool SpelledAsLValue) const {
14299625e44c0252485277a340746ed8ac950686156fDouglas Gregor  assert(getCanonicalType(T) != OverloadTy &&
14309625e44c0252485277a340746ed8ac950686156fDouglas Gregor         "Unresolved overloaded function type");
14319625e44c0252485277a340746ed8ac950686156fDouglas Gregor
14325f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // Unique pointers, to guarantee there is only one pointer of a particular
14335f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // structure.
14345f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  llvm::FoldingSetNodeID ID;
143554e14c4db764c0636160d26c5bbf491637c83a76John McCall  ReferenceType::Profile(ID, T, SpelledAsLValue);
14365f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
14375f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  void *InsertPos = 0;
14387c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl  if (LValueReferenceType *RT =
14397c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl        LValueReferenceTypes.FindNodeOrInsertPos(ID, InsertPos))
14405f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    return QualType(RT, 0);
14417c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl
144254e14c4db764c0636160d26c5bbf491637c83a76John McCall  const ReferenceType *InnerRef = T->getAs<ReferenceType>();
144354e14c4db764c0636160d26c5bbf491637c83a76John McCall
14445f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // If the referencee type isn't canonical, this won't be a canonical type
14455f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // either, so fill in the canonical type field.
14465f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  QualType Canonical;
144754e14c4db764c0636160d26c5bbf491637c83a76John McCall  if (!SpelledAsLValue || InnerRef || !T.isCanonical()) {
144854e14c4db764c0636160d26c5bbf491637c83a76John McCall    QualType PointeeType = (InnerRef ? InnerRef->getPointeeType() : T);
144954e14c4db764c0636160d26c5bbf491637c83a76John McCall    Canonical = getLValueReferenceType(getCanonicalType(PointeeType));
14507c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl
14517c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl    // Get the new insert position for the node we care about.
14527c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl    LValueReferenceType *NewIP =
14537c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl      LValueReferenceTypes.FindNodeOrInsertPos(ID, InsertPos);
1454c6ed729f669044f5072a49d79041f455d971ece3Jeffrey Yasskin    assert(NewIP == 0 && "Shouldn't be in the map!"); (void)NewIP;
14557c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl  }
14567c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl
14576b304a0254a13f42390b865ff5ba668a49cc58aeJohn McCall  LValueReferenceType *New
145854e14c4db764c0636160d26c5bbf491637c83a76John McCall    = new (*this, TypeAlignment) LValueReferenceType(T, Canonical,
145954e14c4db764c0636160d26c5bbf491637c83a76John McCall                                                     SpelledAsLValue);
14607c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl  Types.push_back(New);
14617c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl  LValueReferenceTypes.InsertNode(New, InsertPos);
146254e14c4db764c0636160d26c5bbf491637c83a76John McCall
14637c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl  return QualType(New, 0);
14647c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl}
14657c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl
14667c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl/// getRValueReferenceType - Return the uniqued reference to the type for an
14677c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl/// rvalue reference to the specified type.
14684ba2a17694148e16eaa8d3917f657ffcd3667be4Jay FoadQualType ASTContext::getRValueReferenceType(QualType T) const {
14697c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl  // Unique pointers, to guarantee there is only one pointer of a particular
14707c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl  // structure.
14717c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl  llvm::FoldingSetNodeID ID;
147254e14c4db764c0636160d26c5bbf491637c83a76John McCall  ReferenceType::Profile(ID, T, false);
14737c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl
14747c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl  void *InsertPos = 0;
14757c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl  if (RValueReferenceType *RT =
14767c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl        RValueReferenceTypes.FindNodeOrInsertPos(ID, InsertPos))
14777c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl    return QualType(RT, 0);
14787c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl
147954e14c4db764c0636160d26c5bbf491637c83a76John McCall  const ReferenceType *InnerRef = T->getAs<ReferenceType>();
148054e14c4db764c0636160d26c5bbf491637c83a76John McCall
14817c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl  // If the referencee type isn't canonical, this won't be a canonical type
14827c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl  // either, so fill in the canonical type field.
14837c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl  QualType Canonical;
148454e14c4db764c0636160d26c5bbf491637c83a76John McCall  if (InnerRef || !T.isCanonical()) {
148554e14c4db764c0636160d26c5bbf491637c83a76John McCall    QualType PointeeType = (InnerRef ? InnerRef->getPointeeType() : T);
148654e14c4db764c0636160d26c5bbf491637c83a76John McCall    Canonical = getRValueReferenceType(getCanonicalType(PointeeType));
14877c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl
14885f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    // Get the new insert position for the node we care about.
14897c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl    RValueReferenceType *NewIP =
14907c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl      RValueReferenceTypes.FindNodeOrInsertPos(ID, InsertPos);
1491c6ed729f669044f5072a49d79041f455d971ece3Jeffrey Yasskin    assert(NewIP == 0 && "Shouldn't be in the map!"); (void)NewIP;
14925f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
14935f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
14946b304a0254a13f42390b865ff5ba668a49cc58aeJohn McCall  RValueReferenceType *New
14956b304a0254a13f42390b865ff5ba668a49cc58aeJohn McCall    = new (*this, TypeAlignment) RValueReferenceType(T, Canonical);
14965f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  Types.push_back(New);
14977c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl  RValueReferenceTypes.InsertNode(New, InsertPos);
14985f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  return QualType(New, 0);
14995f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
15005f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
1501f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl/// getMemberPointerType - Return the uniqued reference to the type for a
1502f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl/// member pointer to the specified type, in the specified class.
15034ba2a17694148e16eaa8d3917f657ffcd3667be4Jay FoadQualType ASTContext::getMemberPointerType(QualType T, const Type *Cls) const {
1504f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl  // Unique pointers, to guarantee there is only one pointer of a particular
1505f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl  // structure.
1506f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl  llvm::FoldingSetNodeID ID;
1507f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl  MemberPointerType::Profile(ID, T, Cls);
1508f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl
1509f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl  void *InsertPos = 0;
1510f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl  if (MemberPointerType *PT =
1511f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl      MemberPointerTypes.FindNodeOrInsertPos(ID, InsertPos))
1512f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl    return QualType(PT, 0);
1513f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl
1514f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl  // If the pointee or class type isn't canonical, this won't be a canonical
1515f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl  // type either, so fill in the canonical type field.
1516f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl  QualType Canonical;
151787c12c4a4667279dacb3d4a93c64b49148a0ff79Douglas Gregor  if (!T.isCanonical() || !Cls->isCanonicalUnqualified()) {
1518f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl    Canonical = getMemberPointerType(getCanonicalType(T),getCanonicalType(Cls));
1519f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl
1520f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl    // Get the new insert position for the node we care about.
1521f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl    MemberPointerType *NewIP =
1522f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl      MemberPointerTypes.FindNodeOrInsertPos(ID, InsertPos);
1523c6ed729f669044f5072a49d79041f455d971ece3Jeffrey Yasskin    assert(NewIP == 0 && "Shouldn't be in the map!"); (void)NewIP;
1524f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl  }
15256b304a0254a13f42390b865ff5ba668a49cc58aeJohn McCall  MemberPointerType *New
15266b304a0254a13f42390b865ff5ba668a49cc58aeJohn McCall    = new (*this, TypeAlignment) MemberPointerType(T, Cls, Canonical);
1527f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl  Types.push_back(New);
1528f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl  MemberPointerTypes.InsertNode(New, InsertPos);
1529f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl  return QualType(New, 0);
1530f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl}
1531f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl
15321eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// getConstantArrayType - Return the unique reference to the type for an
1533fb22d96692c5240fb8d611290dbf7eeed3759c73Steve Naroff/// array of the specified element type.
15341eb4433ac451dc16f4133a88af2d002ac26c58efMike StumpQualType ASTContext::getConstantArrayType(QualType EltTy,
153538aeec7299c48cb79523f7f89776fb258c84aeeaChris Lattner                                          const llvm::APInt &ArySizeIn,
1536c9406125e2cac9208098655ac8058c095c2c3a65Steve Naroff                                          ArrayType::ArraySizeModifier ASM,
153763e7d25d2e6036616b42f744fd4a39cd5f911960Abramo Bagnara                                          unsigned IndexTypeQuals) const {
1538923d56d436f750bc1f29db50e641078725558a1bSebastian Redl  assert((EltTy->isDependentType() ||
1539923d56d436f750bc1f29db50e641078725558a1bSebastian Redl          EltTy->isIncompleteType() || EltTy->isConstantSizeType()) &&
1540587cbdfd95f4b0aaccc14b31f5debe85d5daf7edEli Friedman         "Constant array of VLAs is illegal!");
1541587cbdfd95f4b0aaccc14b31f5debe85d5daf7edEli Friedman
154238aeec7299c48cb79523f7f89776fb258c84aeeaChris Lattner  // Convert the array size into a canonical width matching the pointer size for
154338aeec7299c48cb79523f7f89776fb258c84aeeaChris Lattner  // the target.
154438aeec7299c48cb79523f7f89776fb258c84aeeaChris Lattner  llvm::APInt ArySize(ArySizeIn);
15459f71a8f4c7a182a5236da9e747d57cc1d1bd24c2Jay Foad  ArySize =
1546207f4d8543529221932af82836016a2ef066c917Peter Collingbourne    ArySize.zextOrTrunc(Target.getPointerWidth(getTargetAddressSpace(EltTy)));
15471eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
15485f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  llvm::FoldingSetNodeID ID;
154963e7d25d2e6036616b42f744fd4a39cd5f911960Abramo Bagnara  ConstantArrayType::Profile(ID, EltTy, ArySize, ASM, IndexTypeQuals);
15501eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
15515f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  void *InsertPos = 0;
15521eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  if (ConstantArrayType *ATP =
15537192f8e9592729882a09d84d77838db26e39ebd4Ted Kremenek      ConstantArrayTypes.FindNodeOrInsertPos(ID, InsertPos))
15545f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    return QualType(ATP, 0);
15551eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
15563b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall  // If the element type isn't canonical or has qualifiers, this won't
15573b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall  // be a canonical type either, so fill in the canonical type field.
15583b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall  QualType Canon;
15593b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall  if (!EltTy.isCanonical() || EltTy.hasLocalQualifiers()) {
15603b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall    SplitQualType canonSplit = getCanonicalType(EltTy).split();
15613b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall    Canon = getConstantArrayType(QualType(canonSplit.first, 0), ArySize,
156263e7d25d2e6036616b42f744fd4a39cd5f911960Abramo Bagnara                                 ASM, IndexTypeQuals);
15633b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall    Canon = getQualifiedType(Canon, canonSplit.second);
15643b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall
15655f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    // Get the new insert position for the node we care about.
15661eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    ConstantArrayType *NewIP =
15677192f8e9592729882a09d84d77838db26e39ebd4Ted Kremenek      ConstantArrayTypes.FindNodeOrInsertPos(ID, InsertPos);
1568c6ed729f669044f5072a49d79041f455d971ece3Jeffrey Yasskin    assert(NewIP == 0 && "Shouldn't be in the map!"); (void)NewIP;
15695f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
15701eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
15716b304a0254a13f42390b865ff5ba668a49cc58aeJohn McCall  ConstantArrayType *New = new(*this,TypeAlignment)
157263e7d25d2e6036616b42f744fd4a39cd5f911960Abramo Bagnara    ConstantArrayType(EltTy, Canon, ArySize, ASM, IndexTypeQuals);
15737192f8e9592729882a09d84d77838db26e39ebd4Ted Kremenek  ConstantArrayTypes.InsertNode(New, InsertPos);
15745f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  Types.push_back(New);
15755f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  return QualType(New, 0);
15765f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
15775f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
1578ce8890371fcdb983ae487c87fa40606a34896ff7John McCall/// getVariableArrayDecayedType - Turns the given type, which may be
1579ce8890371fcdb983ae487c87fa40606a34896ff7John McCall/// variably-modified, into the corresponding type with all the known
1580ce8890371fcdb983ae487c87fa40606a34896ff7John McCall/// sizes replaced with [*].
1581ce8890371fcdb983ae487c87fa40606a34896ff7John McCallQualType ASTContext::getVariableArrayDecayedType(QualType type) const {
1582ce8890371fcdb983ae487c87fa40606a34896ff7John McCall  // Vastly most common case.
1583ce8890371fcdb983ae487c87fa40606a34896ff7John McCall  if (!type->isVariablyModifiedType()) return type;
1584ce8890371fcdb983ae487c87fa40606a34896ff7John McCall
1585ce8890371fcdb983ae487c87fa40606a34896ff7John McCall  QualType result;
1586ce8890371fcdb983ae487c87fa40606a34896ff7John McCall
1587ce8890371fcdb983ae487c87fa40606a34896ff7John McCall  SplitQualType split = type.getSplitDesugaredType();
1588ce8890371fcdb983ae487c87fa40606a34896ff7John McCall  const Type *ty = split.first;
1589ce8890371fcdb983ae487c87fa40606a34896ff7John McCall  switch (ty->getTypeClass()) {
1590ce8890371fcdb983ae487c87fa40606a34896ff7John McCall#define TYPE(Class, Base)
1591ce8890371fcdb983ae487c87fa40606a34896ff7John McCall#define ABSTRACT_TYPE(Class, Base)
1592ce8890371fcdb983ae487c87fa40606a34896ff7John McCall#define NON_CANONICAL_TYPE(Class, Base) case Type::Class:
1593ce8890371fcdb983ae487c87fa40606a34896ff7John McCall#include "clang/AST/TypeNodes.def"
1594ce8890371fcdb983ae487c87fa40606a34896ff7John McCall    llvm_unreachable("didn't desugar past all non-canonical types?");
1595ce8890371fcdb983ae487c87fa40606a34896ff7John McCall
1596ce8890371fcdb983ae487c87fa40606a34896ff7John McCall  // These types should never be variably-modified.
1597ce8890371fcdb983ae487c87fa40606a34896ff7John McCall  case Type::Builtin:
1598ce8890371fcdb983ae487c87fa40606a34896ff7John McCall  case Type::Complex:
1599ce8890371fcdb983ae487c87fa40606a34896ff7John McCall  case Type::Vector:
1600ce8890371fcdb983ae487c87fa40606a34896ff7John McCall  case Type::ExtVector:
1601ce8890371fcdb983ae487c87fa40606a34896ff7John McCall  case Type::DependentSizedExtVector:
1602ce8890371fcdb983ae487c87fa40606a34896ff7John McCall  case Type::ObjCObject:
1603ce8890371fcdb983ae487c87fa40606a34896ff7John McCall  case Type::ObjCInterface:
1604ce8890371fcdb983ae487c87fa40606a34896ff7John McCall  case Type::ObjCObjectPointer:
1605ce8890371fcdb983ae487c87fa40606a34896ff7John McCall  case Type::Record:
1606ce8890371fcdb983ae487c87fa40606a34896ff7John McCall  case Type::Enum:
1607ce8890371fcdb983ae487c87fa40606a34896ff7John McCall  case Type::UnresolvedUsing:
1608ce8890371fcdb983ae487c87fa40606a34896ff7John McCall  case Type::TypeOfExpr:
1609ce8890371fcdb983ae487c87fa40606a34896ff7John McCall  case Type::TypeOf:
1610ce8890371fcdb983ae487c87fa40606a34896ff7John McCall  case Type::Decltype:
1611ca63c200346c0ca9e00194ec6e34a5a7b0ed9321Sean Hunt  case Type::UnaryTransform:
1612ce8890371fcdb983ae487c87fa40606a34896ff7John McCall  case Type::DependentName:
1613ce8890371fcdb983ae487c87fa40606a34896ff7John McCall  case Type::InjectedClassName:
1614ce8890371fcdb983ae487c87fa40606a34896ff7John McCall  case Type::TemplateSpecialization:
1615ce8890371fcdb983ae487c87fa40606a34896ff7John McCall  case Type::DependentTemplateSpecialization:
1616ce8890371fcdb983ae487c87fa40606a34896ff7John McCall  case Type::TemplateTypeParm:
1617ce8890371fcdb983ae487c87fa40606a34896ff7John McCall  case Type::SubstTemplateTypeParmPack:
161834b41d939a1328f484511c6002ba2456db879a29Richard Smith  case Type::Auto:
1619ce8890371fcdb983ae487c87fa40606a34896ff7John McCall  case Type::PackExpansion:
1620ce8890371fcdb983ae487c87fa40606a34896ff7John McCall    llvm_unreachable("type should never be variably-modified");
1621ce8890371fcdb983ae487c87fa40606a34896ff7John McCall
1622ce8890371fcdb983ae487c87fa40606a34896ff7John McCall  // These types can be variably-modified but should never need to
1623ce8890371fcdb983ae487c87fa40606a34896ff7John McCall  // further decay.
1624ce8890371fcdb983ae487c87fa40606a34896ff7John McCall  case Type::FunctionNoProto:
1625ce8890371fcdb983ae487c87fa40606a34896ff7John McCall  case Type::FunctionProto:
1626ce8890371fcdb983ae487c87fa40606a34896ff7John McCall  case Type::BlockPointer:
1627ce8890371fcdb983ae487c87fa40606a34896ff7John McCall  case Type::MemberPointer:
1628ce8890371fcdb983ae487c87fa40606a34896ff7John McCall    return type;
1629ce8890371fcdb983ae487c87fa40606a34896ff7John McCall
1630ce8890371fcdb983ae487c87fa40606a34896ff7John McCall  // These types can be variably-modified.  All these modifications
1631ce8890371fcdb983ae487c87fa40606a34896ff7John McCall  // preserve structure except as noted by comments.
1632ce8890371fcdb983ae487c87fa40606a34896ff7John McCall  // TODO: if we ever care about optimizing VLAs, there are no-op
1633ce8890371fcdb983ae487c87fa40606a34896ff7John McCall  // optimizations available here.
1634ce8890371fcdb983ae487c87fa40606a34896ff7John McCall  case Type::Pointer:
1635ce8890371fcdb983ae487c87fa40606a34896ff7John McCall    result = getPointerType(getVariableArrayDecayedType(
1636ce8890371fcdb983ae487c87fa40606a34896ff7John McCall                              cast<PointerType>(ty)->getPointeeType()));
1637ce8890371fcdb983ae487c87fa40606a34896ff7John McCall    break;
1638ce8890371fcdb983ae487c87fa40606a34896ff7John McCall
1639ce8890371fcdb983ae487c87fa40606a34896ff7John McCall  case Type::LValueReference: {
1640ce8890371fcdb983ae487c87fa40606a34896ff7John McCall    const LValueReferenceType *lv = cast<LValueReferenceType>(ty);
1641ce8890371fcdb983ae487c87fa40606a34896ff7John McCall    result = getLValueReferenceType(
1642ce8890371fcdb983ae487c87fa40606a34896ff7John McCall                 getVariableArrayDecayedType(lv->getPointeeType()),
1643ce8890371fcdb983ae487c87fa40606a34896ff7John McCall                                    lv->isSpelledAsLValue());
1644ce8890371fcdb983ae487c87fa40606a34896ff7John McCall    break;
1645745da3a5bb4ea35f93f50301e7fbbb7d78d3b6bbFariborz Jahanian  }
1646ce8890371fcdb983ae487c87fa40606a34896ff7John McCall
1647ce8890371fcdb983ae487c87fa40606a34896ff7John McCall  case Type::RValueReference: {
1648ce8890371fcdb983ae487c87fa40606a34896ff7John McCall    const RValueReferenceType *lv = cast<RValueReferenceType>(ty);
1649ce8890371fcdb983ae487c87fa40606a34896ff7John McCall    result = getRValueReferenceType(
1650ce8890371fcdb983ae487c87fa40606a34896ff7John McCall                 getVariableArrayDecayedType(lv->getPointeeType()));
1651ce8890371fcdb983ae487c87fa40606a34896ff7John McCall    break;
1652745da3a5bb4ea35f93f50301e7fbbb7d78d3b6bbFariborz Jahanian  }
1653745da3a5bb4ea35f93f50301e7fbbb7d78d3b6bbFariborz Jahanian
1654ce8890371fcdb983ae487c87fa40606a34896ff7John McCall  case Type::ConstantArray: {
1655ce8890371fcdb983ae487c87fa40606a34896ff7John McCall    const ConstantArrayType *cat = cast<ConstantArrayType>(ty);
1656ce8890371fcdb983ae487c87fa40606a34896ff7John McCall    result = getConstantArrayType(
1657ce8890371fcdb983ae487c87fa40606a34896ff7John McCall                 getVariableArrayDecayedType(cat->getElementType()),
1658ce8890371fcdb983ae487c87fa40606a34896ff7John McCall                                  cat->getSize(),
1659ce8890371fcdb983ae487c87fa40606a34896ff7John McCall                                  cat->getSizeModifier(),
1660ce8890371fcdb983ae487c87fa40606a34896ff7John McCall                                  cat->getIndexTypeCVRQualifiers());
1661ce8890371fcdb983ae487c87fa40606a34896ff7John McCall    break;
1662745da3a5bb4ea35f93f50301e7fbbb7d78d3b6bbFariborz Jahanian  }
1663745da3a5bb4ea35f93f50301e7fbbb7d78d3b6bbFariborz Jahanian
1664ce8890371fcdb983ae487c87fa40606a34896ff7John McCall  case Type::DependentSizedArray: {
1665ce8890371fcdb983ae487c87fa40606a34896ff7John McCall    const DependentSizedArrayType *dat = cast<DependentSizedArrayType>(ty);
1666ce8890371fcdb983ae487c87fa40606a34896ff7John McCall    result = getDependentSizedArrayType(
1667ce8890371fcdb983ae487c87fa40606a34896ff7John McCall                 getVariableArrayDecayedType(dat->getElementType()),
1668ce8890371fcdb983ae487c87fa40606a34896ff7John McCall                                        dat->getSizeExpr(),
1669ce8890371fcdb983ae487c87fa40606a34896ff7John McCall                                        dat->getSizeModifier(),
1670ce8890371fcdb983ae487c87fa40606a34896ff7John McCall                                        dat->getIndexTypeCVRQualifiers(),
1671ce8890371fcdb983ae487c87fa40606a34896ff7John McCall                                        dat->getBracketsRange());
1672ce8890371fcdb983ae487c87fa40606a34896ff7John McCall    break;
1673ce8890371fcdb983ae487c87fa40606a34896ff7John McCall  }
1674ce8890371fcdb983ae487c87fa40606a34896ff7John McCall
1675ce8890371fcdb983ae487c87fa40606a34896ff7John McCall  // Turn incomplete types into [*] types.
1676ce8890371fcdb983ae487c87fa40606a34896ff7John McCall  case Type::IncompleteArray: {
1677ce8890371fcdb983ae487c87fa40606a34896ff7John McCall    const IncompleteArrayType *iat = cast<IncompleteArrayType>(ty);
1678ce8890371fcdb983ae487c87fa40606a34896ff7John McCall    result = getVariableArrayType(
1679ce8890371fcdb983ae487c87fa40606a34896ff7John McCall                 getVariableArrayDecayedType(iat->getElementType()),
1680ce8890371fcdb983ae487c87fa40606a34896ff7John McCall                                  /*size*/ 0,
1681ce8890371fcdb983ae487c87fa40606a34896ff7John McCall                                  ArrayType::Normal,
1682ce8890371fcdb983ae487c87fa40606a34896ff7John McCall                                  iat->getIndexTypeCVRQualifiers(),
1683ce8890371fcdb983ae487c87fa40606a34896ff7John McCall                                  SourceRange());
1684ce8890371fcdb983ae487c87fa40606a34896ff7John McCall    break;
1685ce8890371fcdb983ae487c87fa40606a34896ff7John McCall  }
1686ce8890371fcdb983ae487c87fa40606a34896ff7John McCall
1687ce8890371fcdb983ae487c87fa40606a34896ff7John McCall  // Turn VLA types into [*] types.
1688ce8890371fcdb983ae487c87fa40606a34896ff7John McCall  case Type::VariableArray: {
1689ce8890371fcdb983ae487c87fa40606a34896ff7John McCall    const VariableArrayType *vat = cast<VariableArrayType>(ty);
1690ce8890371fcdb983ae487c87fa40606a34896ff7John McCall    result = getVariableArrayType(
1691ce8890371fcdb983ae487c87fa40606a34896ff7John McCall                 getVariableArrayDecayedType(vat->getElementType()),
1692ce8890371fcdb983ae487c87fa40606a34896ff7John McCall                                  /*size*/ 0,
1693ce8890371fcdb983ae487c87fa40606a34896ff7John McCall                                  ArrayType::Star,
1694ce8890371fcdb983ae487c87fa40606a34896ff7John McCall                                  vat->getIndexTypeCVRQualifiers(),
1695ce8890371fcdb983ae487c87fa40606a34896ff7John McCall                                  vat->getBracketsRange());
1696ce8890371fcdb983ae487c87fa40606a34896ff7John McCall    break;
1697ce8890371fcdb983ae487c87fa40606a34896ff7John McCall  }
1698ce8890371fcdb983ae487c87fa40606a34896ff7John McCall  }
1699ce8890371fcdb983ae487c87fa40606a34896ff7John McCall
1700ce8890371fcdb983ae487c87fa40606a34896ff7John McCall  // Apply the top-level qualifiers from the original.
1701ce8890371fcdb983ae487c87fa40606a34896ff7John McCall  return getQualifiedType(result, split.second);
1702ce8890371fcdb983ae487c87fa40606a34896ff7John McCall}
1703745da3a5bb4ea35f93f50301e7fbbb7d78d3b6bbFariborz Jahanian
1704bdbf7b030a3e0ddb95240076683830e6f78c79a5Steve Naroff/// getVariableArrayType - Returns a non-unique reference to the type for a
1705bdbf7b030a3e0ddb95240076683830e6f78c79a5Steve Naroff/// variable array of the specified element type.
17067e7eb3da052a6d80ddf2377cab0384c798f73f75Douglas GregorQualType ASTContext::getVariableArrayType(QualType EltTy,
17077e7eb3da052a6d80ddf2377cab0384c798f73f75Douglas Gregor                                          Expr *NumElts,
1708c9406125e2cac9208098655ac8058c095c2c3a65Steve Naroff                                          ArrayType::ArraySizeModifier ASM,
170963e7d25d2e6036616b42f744fd4a39cd5f911960Abramo Bagnara                                          unsigned IndexTypeQuals,
17104ba2a17694148e16eaa8d3917f657ffcd3667be4Jay Foad                                          SourceRange Brackets) const {
1711c5773c4b8ce1ed6ed5c7112c9020c954a47dce96Eli Friedman  // Since we don't unique expressions, it isn't possible to unique VLA's
1712c5773c4b8ce1ed6ed5c7112c9020c954a47dce96Eli Friedman  // that have an expression provided for their size.
17133b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall  QualType Canon;
1714715e9c8a39437347e838aa108df443fe1086d359Douglas Gregor
17153b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall  // Be sure to pull qualifiers off the element type.
17163b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall  if (!EltTy.isCanonical() || EltTy.hasLocalQualifiers()) {
17173b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall    SplitQualType canonSplit = getCanonicalType(EltTy).split();
17183b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall    Canon = getVariableArrayType(QualType(canonSplit.first, 0), NumElts, ASM,
171963e7d25d2e6036616b42f744fd4a39cd5f911960Abramo Bagnara                                 IndexTypeQuals, Brackets);
17203b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall    Canon = getQualifiedType(Canon, canonSplit.second);
1721715e9c8a39437347e838aa108df443fe1086d359Douglas Gregor  }
1722715e9c8a39437347e838aa108df443fe1086d359Douglas Gregor
17236b304a0254a13f42390b865ff5ba668a49cc58aeJohn McCall  VariableArrayType *New = new(*this, TypeAlignment)
172463e7d25d2e6036616b42f744fd4a39cd5f911960Abramo Bagnara    VariableArrayType(EltTy, Canon, NumElts, ASM, IndexTypeQuals, Brackets);
1725c5773c4b8ce1ed6ed5c7112c9020c954a47dce96Eli Friedman
1726c5773c4b8ce1ed6ed5c7112c9020c954a47dce96Eli Friedman  VariableArrayTypes.push_back(New);
1727c5773c4b8ce1ed6ed5c7112c9020c954a47dce96Eli Friedman  Types.push_back(New);
1728c5773c4b8ce1ed6ed5c7112c9020c954a47dce96Eli Friedman  return QualType(New, 0);
1729c5773c4b8ce1ed6ed5c7112c9020c954a47dce96Eli Friedman}
1730c5773c4b8ce1ed6ed5c7112c9020c954a47dce96Eli Friedman
1731898574e7496ba8fd76290079d3a9d06954992734Douglas Gregor/// getDependentSizedArrayType - Returns a non-unique reference to
1732898574e7496ba8fd76290079d3a9d06954992734Douglas Gregor/// the type for a dependently-sized array of the specified element
173304d4beee4b86af20a9e4457023d3925cab8f9908Douglas Gregor/// type.
17343b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCallQualType ASTContext::getDependentSizedArrayType(QualType elementType,
17353b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall                                                Expr *numElements,
1736898574e7496ba8fd76290079d3a9d06954992734Douglas Gregor                                                ArrayType::ArraySizeModifier ASM,
17373b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall                                                unsigned elementTypeQuals,
17383b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall                                                SourceRange brackets) const {
17393b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall  assert((!numElements || numElements->isTypeDependent() ||
17403b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall          numElements->isValueDependent()) &&
1741898574e7496ba8fd76290079d3a9d06954992734Douglas Gregor         "Size must be type- or value-dependent!");
1742898574e7496ba8fd76290079d3a9d06954992734Douglas Gregor
17433b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall  // Dependently-sized array types that do not have a specified number
17443b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall  // of elements will have their sizes deduced from a dependent
17453b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall  // initializer.  We do no canonicalization here at all, which is okay
17463b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall  // because they can't be used in most locations.
17473b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall  if (!numElements) {
17483b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall    DependentSizedArrayType *newType
17493b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall      = new (*this, TypeAlignment)
17503b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall          DependentSizedArrayType(*this, elementType, QualType(),
17513b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall                                  numElements, ASM, elementTypeQuals,
17523b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall                                  brackets);
17533b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall    Types.push_back(newType);
17543b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall    return QualType(newType, 0);
1755cb78d8852a454684c987220132cdb5e54dd00121Douglas Gregor  }
1756cb78d8852a454684c987220132cdb5e54dd00121Douglas Gregor
17573b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall  // Otherwise, we actually build a new type every time, but we
17583b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall  // also build a canonical type.
17591eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
17603b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall  SplitQualType canonElementType = getCanonicalType(elementType).split();
1761898574e7496ba8fd76290079d3a9d06954992734Douglas Gregor
17623b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall  void *insertPos = 0;
17633b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall  llvm::FoldingSetNodeID ID;
17643b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall  DependentSizedArrayType::Profile(ID, *this,
17653b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall                                   QualType(canonElementType.first, 0),
17663b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall                                   ASM, elementTypeQuals, numElements);
17673b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall
17683b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall  // Look for an existing type with these properties.
17693b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall  DependentSizedArrayType *canonTy =
17703b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall    DependentSizedArrayTypes.FindNodeOrInsertPos(ID, insertPos);
17713b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall
17723b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall  // If we don't have one, build one.
17733b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall  if (!canonTy) {
17743b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall    canonTy = new (*this, TypeAlignment)
17753b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall      DependentSizedArrayType(*this, QualType(canonElementType.first, 0),
17763b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall                              QualType(), numElements, ASM, elementTypeQuals,
17773b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall                              brackets);
17783b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall    DependentSizedArrayTypes.InsertNode(canonTy, insertPos);
17793b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall    Types.push_back(canonTy);
17803b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall  }
17813b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall
17823b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall  // Apply qualifiers from the element type to the array.
17833b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall  QualType canon = getQualifiedType(QualType(canonTy,0),
17843b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall                                    canonElementType.second);
17853b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall
17863b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall  // If we didn't need extra canonicalization for the element type,
17873b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall  // then just use that as our result.
17883b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall  if (QualType(canonElementType.first, 0) == elementType)
17893b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall    return canon;
17903b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall
17913b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall  // Otherwise, we need to build a type which follows the spelling
17923b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall  // of the element type.
17933b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall  DependentSizedArrayType *sugaredType
17943b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall    = new (*this, TypeAlignment)
17953b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall        DependentSizedArrayType(*this, elementType, canon, numElements,
17963b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall                                ASM, elementTypeQuals, brackets);
17973b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall  Types.push_back(sugaredType);
17983b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall  return QualType(sugaredType, 0);
17993b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall}
18003b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall
18013b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCallQualType ASTContext::getIncompleteArrayType(QualType elementType,
1802c5773c4b8ce1ed6ed5c7112c9020c954a47dce96Eli Friedman                                            ArrayType::ArraySizeModifier ASM,
18033b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall                                            unsigned elementTypeQuals) const {
1804c5773c4b8ce1ed6ed5c7112c9020c954a47dce96Eli Friedman  llvm::FoldingSetNodeID ID;
18053b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall  IncompleteArrayType::Profile(ID, elementType, ASM, elementTypeQuals);
1806c5773c4b8ce1ed6ed5c7112c9020c954a47dce96Eli Friedman
18073b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall  void *insertPos = 0;
18083b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall  if (IncompleteArrayType *iat =
18093b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall       IncompleteArrayTypes.FindNodeOrInsertPos(ID, insertPos))
18103b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall    return QualType(iat, 0);
1811c5773c4b8ce1ed6ed5c7112c9020c954a47dce96Eli Friedman
1812c5773c4b8ce1ed6ed5c7112c9020c954a47dce96Eli Friedman  // If the element type isn't canonical, this won't be a canonical type
18133b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall  // either, so fill in the canonical type field.  We also have to pull
18143b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall  // qualifiers off the element type.
18153b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall  QualType canon;
1816c5773c4b8ce1ed6ed5c7112c9020c954a47dce96Eli Friedman
18173b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall  if (!elementType.isCanonical() || elementType.hasLocalQualifiers()) {
18183b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall    SplitQualType canonSplit = getCanonicalType(elementType).split();
18193b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall    canon = getIncompleteArrayType(QualType(canonSplit.first, 0),
18203b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall                                   ASM, elementTypeQuals);
18213b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall    canon = getQualifiedType(canon, canonSplit.second);
1822c5773c4b8ce1ed6ed5c7112c9020c954a47dce96Eli Friedman
1823c5773c4b8ce1ed6ed5c7112c9020c954a47dce96Eli Friedman    // Get the new insert position for the node we care about.
18243b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall    IncompleteArrayType *existing =
18253b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall      IncompleteArrayTypes.FindNodeOrInsertPos(ID, insertPos);
18263b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall    assert(!existing && "Shouldn't be in the map!"); (void) existing;
18272bd24ba6d10f8c811c8e2a57c8397e07082ba497Ted Kremenek  }
1828c5773c4b8ce1ed6ed5c7112c9020c954a47dce96Eli Friedman
18293b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall  IncompleteArrayType *newType = new (*this, TypeAlignment)
18303b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall    IncompleteArrayType(elementType, canon, ASM, elementTypeQuals);
1831c5773c4b8ce1ed6ed5c7112c9020c954a47dce96Eli Friedman
18323b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall  IncompleteArrayTypes.InsertNode(newType, insertPos);
18333b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall  Types.push_back(newType);
18343b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall  return QualType(newType, 0);
1835fb22d96692c5240fb8d611290dbf7eeed3759c73Steve Naroff}
1836fb22d96692c5240fb8d611290dbf7eeed3759c73Steve Naroff
183773322924127c873c13101b705dd823f5539ffa5fSteve Naroff/// getVectorType - Return the unique reference to a vector type of
183873322924127c873c13101b705dd823f5539ffa5fSteve Naroff/// the specified element type and size. VectorType must be a built-in type.
183982287d19ded35248c4ce6a425ce74116a13ce44eJohn ThompsonQualType ASTContext::getVectorType(QualType vecType, unsigned NumElts,
18404ba2a17694148e16eaa8d3917f657ffcd3667be4Jay Foad                                   VectorType::VectorKind VecKind) const {
18413b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall  assert(vecType->isBuiltinType());
18421eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
18435f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // Check if we've already instantiated a vector of this type.
18445f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  llvm::FoldingSetNodeID ID;
1845e86d78cf4754a6aef2cf9a33d847aa15338e276fBob Wilson  VectorType::Profile(ID, vecType, NumElts, Type::Vector, VecKind);
1846788b0fd67e1992f23555454efcdb16a19dfefac3Chris Lattner
18475f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  void *InsertPos = 0;
18485f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  if (VectorType *VTP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos))
18495f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    return QualType(VTP, 0);
18505f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
18515f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // If the element type isn't canonical, this won't be a canonical type either,
18525f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // so fill in the canonical type field.
18535f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  QualType Canonical;
1854255210ef415b9893f0e3794e8d9a704194c12f3cDouglas Gregor  if (!vecType.isCanonical()) {
1855231da7eb3dd13007e5e40fffe48998e5ef284e06Bob Wilson    Canonical = getVectorType(getCanonicalType(vecType), NumElts, VecKind);
18561eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
18575f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    // Get the new insert position for the node we care about.
18585f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    VectorType *NewIP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos);
1859c6ed729f669044f5072a49d79041f455d971ece3Jeffrey Yasskin    assert(NewIP == 0 && "Shouldn't be in the map!"); (void)NewIP;
18605f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
18616b304a0254a13f42390b865ff5ba668a49cc58aeJohn McCall  VectorType *New = new (*this, TypeAlignment)
1862e86d78cf4754a6aef2cf9a33d847aa15338e276fBob Wilson    VectorType(vecType, NumElts, Canonical, VecKind);
18635f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  VectorTypes.InsertNode(New, InsertPos);
18645f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  Types.push_back(New);
18655f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  return QualType(New, 0);
18665f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
18675f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
1868213541a68a3e137d11d2cefb612c6cdb410d7e8eNate Begeman/// getExtVectorType - Return the unique reference to an extended vector type of
186973322924127c873c13101b705dd823f5539ffa5fSteve Naroff/// the specified element type and size. VectorType must be a built-in type.
18704ba2a17694148e16eaa8d3917f657ffcd3667be4Jay FoadQualType
18714ba2a17694148e16eaa8d3917f657ffcd3667be4Jay FoadASTContext::getExtVectorType(QualType vecType, unsigned NumElts) const {
18723b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall  assert(vecType->isBuiltinType());
18731eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
187473322924127c873c13101b705dd823f5539ffa5fSteve Naroff  // Check if we've already instantiated a vector of this type.
187573322924127c873c13101b705dd823f5539ffa5fSteve Naroff  llvm::FoldingSetNodeID ID;
1876788b0fd67e1992f23555454efcdb16a19dfefac3Chris Lattner  VectorType::Profile(ID, vecType, NumElts, Type::ExtVector,
1877e86d78cf4754a6aef2cf9a33d847aa15338e276fBob Wilson                      VectorType::GenericVector);
187873322924127c873c13101b705dd823f5539ffa5fSteve Naroff  void *InsertPos = 0;
187973322924127c873c13101b705dd823f5539ffa5fSteve Naroff  if (VectorType *VTP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos))
188073322924127c873c13101b705dd823f5539ffa5fSteve Naroff    return QualType(VTP, 0);
188173322924127c873c13101b705dd823f5539ffa5fSteve Naroff
188273322924127c873c13101b705dd823f5539ffa5fSteve Naroff  // If the element type isn't canonical, this won't be a canonical type either,
188373322924127c873c13101b705dd823f5539ffa5fSteve Naroff  // so fill in the canonical type field.
188473322924127c873c13101b705dd823f5539ffa5fSteve Naroff  QualType Canonical;
1885467b27b9a24bdc823218ad1ad0e37673b6cc1e83John McCall  if (!vecType.isCanonical()) {
1886213541a68a3e137d11d2cefb612c6cdb410d7e8eNate Begeman    Canonical = getExtVectorType(getCanonicalType(vecType), NumElts);
18871eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
188873322924127c873c13101b705dd823f5539ffa5fSteve Naroff    // Get the new insert position for the node we care about.
188973322924127c873c13101b705dd823f5539ffa5fSteve Naroff    VectorType *NewIP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos);
1890c6ed729f669044f5072a49d79041f455d971ece3Jeffrey Yasskin    assert(NewIP == 0 && "Shouldn't be in the map!"); (void)NewIP;
189173322924127c873c13101b705dd823f5539ffa5fSteve Naroff  }
18926b304a0254a13f42390b865ff5ba668a49cc58aeJohn McCall  ExtVectorType *New = new (*this, TypeAlignment)
18936b304a0254a13f42390b865ff5ba668a49cc58aeJohn McCall    ExtVectorType(vecType, NumElts, Canonical);
189473322924127c873c13101b705dd823f5539ffa5fSteve Naroff  VectorTypes.InsertNode(New, InsertPos);
189573322924127c873c13101b705dd823f5539ffa5fSteve Naroff  Types.push_back(New);
189673322924127c873c13101b705dd823f5539ffa5fSteve Naroff  return QualType(New, 0);
189773322924127c873c13101b705dd823f5539ffa5fSteve Naroff}
189873322924127c873c13101b705dd823f5539ffa5fSteve Naroff
18994ba2a17694148e16eaa8d3917f657ffcd3667be4Jay FoadQualType
19004ba2a17694148e16eaa8d3917f657ffcd3667be4Jay FoadASTContext::getDependentSizedExtVectorType(QualType vecType,
19014ba2a17694148e16eaa8d3917f657ffcd3667be4Jay Foad                                           Expr *SizeExpr,
19024ba2a17694148e16eaa8d3917f657ffcd3667be4Jay Foad                                           SourceLocation AttrLoc) const {
19032ec09f1dc123e1942ed756e8ee4fef86451eac9eDouglas Gregor  llvm::FoldingSetNodeID ID;
19041eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  DependentSizedExtVectorType::Profile(ID, *this, getCanonicalType(vecType),
19052ec09f1dc123e1942ed756e8ee4fef86451eac9eDouglas Gregor                                       SizeExpr);
19061eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
19072ec09f1dc123e1942ed756e8ee4fef86451eac9eDouglas Gregor  void *InsertPos = 0;
19082ec09f1dc123e1942ed756e8ee4fef86451eac9eDouglas Gregor  DependentSizedExtVectorType *Canon
19092ec09f1dc123e1942ed756e8ee4fef86451eac9eDouglas Gregor    = DependentSizedExtVectorTypes.FindNodeOrInsertPos(ID, InsertPos);
19102ec09f1dc123e1942ed756e8ee4fef86451eac9eDouglas Gregor  DependentSizedExtVectorType *New;
19112ec09f1dc123e1942ed756e8ee4fef86451eac9eDouglas Gregor  if (Canon) {
19122ec09f1dc123e1942ed756e8ee4fef86451eac9eDouglas Gregor    // We already have a canonical version of this array type; use it as
19132ec09f1dc123e1942ed756e8ee4fef86451eac9eDouglas Gregor    // the canonical type for a newly-built type.
19146b304a0254a13f42390b865ff5ba668a49cc58aeJohn McCall    New = new (*this, TypeAlignment)
19156b304a0254a13f42390b865ff5ba668a49cc58aeJohn McCall      DependentSizedExtVectorType(*this, vecType, QualType(Canon, 0),
19166b304a0254a13f42390b865ff5ba668a49cc58aeJohn McCall                                  SizeExpr, AttrLoc);
19172ec09f1dc123e1942ed756e8ee4fef86451eac9eDouglas Gregor  } else {
19182ec09f1dc123e1942ed756e8ee4fef86451eac9eDouglas Gregor    QualType CanonVecTy = getCanonicalType(vecType);
19192ec09f1dc123e1942ed756e8ee4fef86451eac9eDouglas Gregor    if (CanonVecTy == vecType) {
19206b304a0254a13f42390b865ff5ba668a49cc58aeJohn McCall      New = new (*this, TypeAlignment)
19216b304a0254a13f42390b865ff5ba668a49cc58aeJohn McCall        DependentSizedExtVectorType(*this, vecType, QualType(), SizeExpr,
19226b304a0254a13f42390b865ff5ba668a49cc58aeJohn McCall                                    AttrLoc);
1923789b1f640205e81b5af250693246120f1ce9d147Douglas Gregor
1924789b1f640205e81b5af250693246120f1ce9d147Douglas Gregor      DependentSizedExtVectorType *CanonCheck
1925789b1f640205e81b5af250693246120f1ce9d147Douglas Gregor        = DependentSizedExtVectorTypes.FindNodeOrInsertPos(ID, InsertPos);
1926789b1f640205e81b5af250693246120f1ce9d147Douglas Gregor      assert(!CanonCheck && "Dependent-sized ext_vector canonical type broken");
1927789b1f640205e81b5af250693246120f1ce9d147Douglas Gregor      (void)CanonCheck;
19282ec09f1dc123e1942ed756e8ee4fef86451eac9eDouglas Gregor      DependentSizedExtVectorTypes.InsertNode(New, InsertPos);
19292ec09f1dc123e1942ed756e8ee4fef86451eac9eDouglas Gregor    } else {
19302ec09f1dc123e1942ed756e8ee4fef86451eac9eDouglas Gregor      QualType Canon = getDependentSizedExtVectorType(CanonVecTy, SizeExpr,
19312ec09f1dc123e1942ed756e8ee4fef86451eac9eDouglas Gregor                                                      SourceLocation());
19326b304a0254a13f42390b865ff5ba668a49cc58aeJohn McCall      New = new (*this, TypeAlignment)
19336b304a0254a13f42390b865ff5ba668a49cc58aeJohn McCall        DependentSizedExtVectorType(*this, vecType, Canon, SizeExpr, AttrLoc);
19342ec09f1dc123e1942ed756e8ee4fef86451eac9eDouglas Gregor    }
19352ec09f1dc123e1942ed756e8ee4fef86451eac9eDouglas Gregor  }
19361eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
19379cdda0cf8528e3d595be9bfa002f0450074beb4dDouglas Gregor  Types.push_back(New);
19389cdda0cf8528e3d595be9bfa002f0450074beb4dDouglas Gregor  return QualType(New, 0);
19399cdda0cf8528e3d595be9bfa002f0450074beb4dDouglas Gregor}
19409cdda0cf8528e3d595be9bfa002f0450074beb4dDouglas Gregor
194172564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor/// getFunctionNoProtoType - Return a K&R style C function type like 'int()'.
19425f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer///
19434ba2a17694148e16eaa8d3917f657ffcd3667be4Jay FoadQualType
19444ba2a17694148e16eaa8d3917f657ffcd3667be4Jay FoadASTContext::getFunctionNoProtoType(QualType ResultTy,
19454ba2a17694148e16eaa8d3917f657ffcd3667be4Jay Foad                                   const FunctionType::ExtInfo &Info) const {
1946cfe9af250f466e7e38becea4428990448ae07737Roman Divacky  const CallingConv DefaultCC = Info.getCC();
1947cfe9af250f466e7e38becea4428990448ae07737Roman Divacky  const CallingConv CallConv = (LangOpts.MRTD && DefaultCC == CC_Default) ?
1948cfe9af250f466e7e38becea4428990448ae07737Roman Divacky                               CC_X86StdCall : DefaultCC;
19495f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // Unique functions, to guarantee there is only one function of a particular
19505f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // structure.
19515f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  llvm::FoldingSetNodeID ID;
1952264ba48dc98f3f843935a485d5b086f7e0fdc4f1Rafael Espindola  FunctionNoProtoType::Profile(ID, ResultTy, Info);
19531eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
19545f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  void *InsertPos = 0;
19551eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  if (FunctionNoProtoType *FT =
195672564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor        FunctionNoProtoTypes.FindNodeOrInsertPos(ID, InsertPos))
19575f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    return QualType(FT, 0);
19581eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
19595f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  QualType Canonical;
1960ab8bbf4ebd3e3e6eab913cb044772a62b7581941Douglas Gregor  if (!ResultTy.isCanonical() ||
196104a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall      getCanonicalCallConv(CallConv) != CallConv) {
1962264ba48dc98f3f843935a485d5b086f7e0fdc4f1Rafael Espindola    Canonical =
1963264ba48dc98f3f843935a485d5b086f7e0fdc4f1Rafael Espindola      getFunctionNoProtoType(getCanonicalType(ResultTy),
1964264ba48dc98f3f843935a485d5b086f7e0fdc4f1Rafael Espindola                     Info.withCallingConv(getCanonicalCallConv(CallConv)));
19651eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
19665f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    // Get the new insert position for the node we care about.
196772564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor    FunctionNoProtoType *NewIP =
196872564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor      FunctionNoProtoTypes.FindNodeOrInsertPos(ID, InsertPos);
1969c6ed729f669044f5072a49d79041f455d971ece3Jeffrey Yasskin    assert(NewIP == 0 && "Shouldn't be in the map!"); (void)NewIP;
19705f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
19711eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1972cfe9af250f466e7e38becea4428990448ae07737Roman Divacky  FunctionProtoType::ExtInfo newInfo = Info.withCallingConv(CallConv);
19736b304a0254a13f42390b865ff5ba668a49cc58aeJohn McCall  FunctionNoProtoType *New = new (*this, TypeAlignment)
1974cfe9af250f466e7e38becea4428990448ae07737Roman Divacky    FunctionNoProtoType(ResultTy, Canonical, newInfo);
19755f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  Types.push_back(New);
197672564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor  FunctionNoProtoTypes.InsertNode(New, InsertPos);
19775f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  return QualType(New, 0);
19785f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
19795f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
19805f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// getFunctionType - Return a normal function type with a typed argument
19815f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// list.  isVariadic indicates whether the argument list includes '...'.
19824ba2a17694148e16eaa8d3917f657ffcd3667be4Jay FoadQualType
19834ba2a17694148e16eaa8d3917f657ffcd3667be4Jay FoadASTContext::getFunctionType(QualType ResultTy,
19844ba2a17694148e16eaa8d3917f657ffcd3667be4Jay Foad                            const QualType *ArgArray, unsigned NumArgs,
19854ba2a17694148e16eaa8d3917f657ffcd3667be4Jay Foad                            const FunctionProtoType::ExtProtoInfo &EPI) const {
19865f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // Unique functions, to guarantee there is only one function of a particular
19875f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // structure.
19885f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  llvm::FoldingSetNodeID ID;
19898026f6d82f7fa544bc0453714fe94bca62a1196eSebastian Redl  FunctionProtoType::Profile(ID, ResultTy, ArgArray, NumArgs, EPI, *this);
19905f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
19915f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  void *InsertPos = 0;
19921eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  if (FunctionProtoType *FTP =
199372564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor        FunctionProtoTypes.FindNodeOrInsertPos(ID, InsertPos))
19945f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    return QualType(FTP, 0);
1995465226e23a3008bd68973513dda1f9e3cd27dbddSebastian Redl
1996465226e23a3008bd68973513dda1f9e3cd27dbddSebastian Redl  // Determine whether the type being created is already canonical or not.
19978b5b4099c61a136e9a1714c4d8a593febe942268Sebastian Redl  bool isCanonical= EPI.ExceptionSpecType == EST_None && ResultTy.isCanonical();
19985f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  for (unsigned i = 0; i != NumArgs && isCanonical; ++i)
199954e14c4db764c0636160d26c5bbf491637c83a76John McCall    if (!ArgArray[i].isCanonicalAsParam())
20005f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      isCanonical = false;
20015f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
2002cfe9af250f466e7e38becea4428990448ae07737Roman Divacky  const CallingConv DefaultCC = EPI.ExtInfo.getCC();
2003cfe9af250f466e7e38becea4428990448ae07737Roman Divacky  const CallingConv CallConv = (LangOpts.MRTD && DefaultCC == CC_Default) ?
2004cfe9af250f466e7e38becea4428990448ae07737Roman Divacky                               CC_X86StdCall : DefaultCC;
2005e23cf437fe76b1ed02d63c3f61b456fd48a915f5John McCall
20065f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // If this type isn't canonical, get the canonical version of it.
2007465226e23a3008bd68973513dda1f9e3cd27dbddSebastian Redl  // The exception spec is not part of the canonical type.
20085f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  QualType Canonical;
200904a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall  if (!isCanonical || getCanonicalCallConv(CallConv) != CallConv) {
20105f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    llvm::SmallVector<QualType, 16> CanonicalArgs;
20115f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    CanonicalArgs.reserve(NumArgs);
20125f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    for (unsigned i = 0; i != NumArgs; ++i)
201354e14c4db764c0636160d26c5bbf491637c83a76John McCall      CanonicalArgs.push_back(getCanonicalParamType(ArgArray[i]));
2014465226e23a3008bd68973513dda1f9e3cd27dbddSebastian Redl
2015e23cf437fe76b1ed02d63c3f61b456fd48a915f5John McCall    FunctionProtoType::ExtProtoInfo CanonicalEPI = EPI;
20168b5b4099c61a136e9a1714c4d8a593febe942268Sebastian Redl    CanonicalEPI.ExceptionSpecType = EST_None;
20178b5b4099c61a136e9a1714c4d8a593febe942268Sebastian Redl    CanonicalEPI.NumExceptions = 0;
2018e23cf437fe76b1ed02d63c3f61b456fd48a915f5John McCall    CanonicalEPI.ExtInfo
2019e23cf437fe76b1ed02d63c3f61b456fd48a915f5John McCall      = CanonicalEPI.ExtInfo.withCallingConv(getCanonicalCallConv(CallConv));
2020e23cf437fe76b1ed02d63c3f61b456fd48a915f5John McCall
2021f52ab250ff92bc51a9ac9a8e19bd43b63a5f844fChris Lattner    Canonical = getFunctionType(getCanonicalType(ResultTy),
2022beaaccd8e2a8748f77b66e2b330fb9136937e14cJay Foad                                CanonicalArgs.data(), NumArgs,
2023e23cf437fe76b1ed02d63c3f61b456fd48a915f5John McCall                                CanonicalEPI);
2024465226e23a3008bd68973513dda1f9e3cd27dbddSebastian Redl
20255f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    // Get the new insert position for the node we care about.
202672564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor    FunctionProtoType *NewIP =
202772564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor      FunctionProtoTypes.FindNodeOrInsertPos(ID, InsertPos);
2028c6ed729f669044f5072a49d79041f455d971ece3Jeffrey Yasskin    assert(NewIP == 0 && "Shouldn't be in the map!"); (void)NewIP;
20295f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
2030465226e23a3008bd68973513dda1f9e3cd27dbddSebastian Redl
203172564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor  // FunctionProtoType objects are allocated with extra bytes after them
2032465226e23a3008bd68973513dda1f9e3cd27dbddSebastian Redl  // for two variable size arrays (for parameter and exception types) at the
203360618fa7f88d5162bb5b40988b6b38d4d75d6fc6Sebastian Redl  // end of them. Instead of the exception types, there could be a noexcept
203460618fa7f88d5162bb5b40988b6b38d4d75d6fc6Sebastian Redl  // expression and a context pointer.
2035e23cf437fe76b1ed02d63c3f61b456fd48a915f5John McCall  size_t Size = sizeof(FunctionProtoType) +
203660618fa7f88d5162bb5b40988b6b38d4d75d6fc6Sebastian Redl                NumArgs * sizeof(QualType);
203760618fa7f88d5162bb5b40988b6b38d4d75d6fc6Sebastian Redl  if (EPI.ExceptionSpecType == EST_Dynamic)
203860618fa7f88d5162bb5b40988b6b38d4d75d6fc6Sebastian Redl    Size += EPI.NumExceptions * sizeof(QualType);
203960618fa7f88d5162bb5b40988b6b38d4d75d6fc6Sebastian Redl  else if (EPI.ExceptionSpecType == EST_ComputedNoexcept) {
20408026f6d82f7fa544bc0453714fe94bca62a1196eSebastian Redl    Size += sizeof(Expr*);
204160618fa7f88d5162bb5b40988b6b38d4d75d6fc6Sebastian Redl  }
2042e23cf437fe76b1ed02d63c3f61b456fd48a915f5John McCall  FunctionProtoType *FTP = (FunctionProtoType*) Allocate(Size, TypeAlignment);
2043cfe9af250f466e7e38becea4428990448ae07737Roman Divacky  FunctionProtoType::ExtProtoInfo newEPI = EPI;
2044cfe9af250f466e7e38becea4428990448ae07737Roman Divacky  newEPI.ExtInfo = EPI.ExtInfo.withCallingConv(CallConv);
20458026f6d82f7fa544bc0453714fe94bca62a1196eSebastian Redl  new (FTP) FunctionProtoType(ResultTy, ArgArray, NumArgs, Canonical, newEPI);
20465f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  Types.push_back(FTP);
204772564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor  FunctionProtoTypes.InsertNode(FTP, InsertPos);
20485f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  return QualType(FTP, 0);
20495f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
20505f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
20513cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall#ifndef NDEBUG
20523cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCallstatic bool NeedsInjectedClassNameType(const RecordDecl *D) {
20533cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall  if (!isa<CXXRecordDecl>(D)) return false;
20543cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall  const CXXRecordDecl *RD = cast<CXXRecordDecl>(D);
20553cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall  if (isa<ClassTemplatePartialSpecializationDecl>(RD))
20563cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall    return true;
20573cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall  if (RD->getDescribedClassTemplate() &&
20583cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall      !isa<ClassTemplateSpecializationDecl>(RD))
20593cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall    return true;
20603cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall  return false;
20613cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall}
20623cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall#endif
20633cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall
20643cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall/// getInjectedClassNameType - Return the unique reference to the
20653cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall/// injected class name type for the specified templated declaration.
20663cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCallQualType ASTContext::getInjectedClassNameType(CXXRecordDecl *Decl,
20674ba2a17694148e16eaa8d3917f657ffcd3667be4Jay Foad                                              QualType TST) const {
20683cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall  assert(NeedsInjectedClassNameType(Decl));
20693cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall  if (Decl->TypeForDecl) {
20703cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall    assert(isa<InjectedClassNameType>(Decl->TypeForDecl));
207137ffed3b7f229844cae2463ff82b527506c86c74Argyrios Kyrtzidis  } else if (CXXRecordDecl *PrevDecl = Decl->getPreviousDeclaration()) {
20723cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall    assert(PrevDecl->TypeForDecl && "previous declaration has no type");
20733cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall    Decl->TypeForDecl = PrevDecl->TypeForDecl;
20743cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall    assert(isa<InjectedClassNameType>(Decl->TypeForDecl));
20753cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall  } else {
2076f4c7371fb1d3cebcfb40abad4537bb82515704eaJohn McCall    Type *newType =
207731f17ecbef57b5679c017c375db330546b7b5145John McCall      new (*this, TypeAlignment) InjectedClassNameType(Decl, TST);
2078f4c7371fb1d3cebcfb40abad4537bb82515704eaJohn McCall    Decl->TypeForDecl = newType;
2079f4c7371fb1d3cebcfb40abad4537bb82515704eaJohn McCall    Types.push_back(newType);
20803cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall  }
20813cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall  return QualType(Decl->TypeForDecl, 0);
20823cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall}
20833cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall
20842ce52f3fb95bf544db6bd3d91a72bce7d9cceb6cDouglas Gregor/// getTypeDeclType - Return the unique reference to the type for the
20852ce52f3fb95bf544db6bd3d91a72bce7d9cceb6cDouglas Gregor/// specified type declaration.
20864ba2a17694148e16eaa8d3917f657ffcd3667be4Jay FoadQualType ASTContext::getTypeDeclTypeSlow(const TypeDecl *Decl) const {
20871e6759e9e33dcaa73ce14c8a908ac9f87ac16463Argyrios Kyrtzidis  assert(Decl && "Passed null for Decl param");
2088becb8d5a6ab5103393eac5344ae69bcb860601ddJohn McCall  assert(!Decl->TypeForDecl && "TypeForDecl present in slow case");
20891eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
2090162e1c1b487352434552147967c3dd296ebee2f7Richard Smith  if (const TypedefNameDecl *Typedef = dyn_cast<TypedefNameDecl>(Decl))
20912ce52f3fb95bf544db6bd3d91a72bce7d9cceb6cDouglas Gregor    return getTypedefType(Typedef);
2092becb8d5a6ab5103393eac5344ae69bcb860601ddJohn McCall
2093becb8d5a6ab5103393eac5344ae69bcb860601ddJohn McCall  assert(!isa<TemplateTypeParmDecl>(Decl) &&
2094becb8d5a6ab5103393eac5344ae69bcb860601ddJohn McCall         "Template type parameter types are always available.");
2095becb8d5a6ab5103393eac5344ae69bcb860601ddJohn McCall
209619c8576b7328f4dc2d07682f5da552875c1912efJohn McCall  if (const RecordDecl *Record = dyn_cast<RecordDecl>(Decl)) {
2097becb8d5a6ab5103393eac5344ae69bcb860601ddJohn McCall    assert(!Record->getPreviousDeclaration() &&
2098becb8d5a6ab5103393eac5344ae69bcb860601ddJohn McCall           "struct/union has previous declaration");
2099becb8d5a6ab5103393eac5344ae69bcb860601ddJohn McCall    assert(!NeedsInjectedClassNameType(Record));
2100400f5125e2432d648f2c8a31b36a7f318a880c47Argyrios Kyrtzidis    return getRecordType(Record);
210119c8576b7328f4dc2d07682f5da552875c1912efJohn McCall  } else if (const EnumDecl *Enum = dyn_cast<EnumDecl>(Decl)) {
2102becb8d5a6ab5103393eac5344ae69bcb860601ddJohn McCall    assert(!Enum->getPreviousDeclaration() &&
2103becb8d5a6ab5103393eac5344ae69bcb860601ddJohn McCall           "enum has previous declaration");
2104400f5125e2432d648f2c8a31b36a7f318a880c47Argyrios Kyrtzidis    return getEnumType(Enum);
210519c8576b7328f4dc2d07682f5da552875c1912efJohn McCall  } else if (const UnresolvedUsingTypenameDecl *Using =
2106ed97649e9574b9d854fa4d6109c9333ae0993554John McCall               dyn_cast<UnresolvedUsingTypenameDecl>(Decl)) {
2107f4c7371fb1d3cebcfb40abad4537bb82515704eaJohn McCall    Type *newType = new (*this, TypeAlignment) UnresolvedUsingType(Using);
2108f4c7371fb1d3cebcfb40abad4537bb82515704eaJohn McCall    Decl->TypeForDecl = newType;
2109f4c7371fb1d3cebcfb40abad4537bb82515704eaJohn McCall    Types.push_back(newType);
21109fdbab3cbc2fc04bcaf5768023d83707f3151144Mike Stump  } else
2111becb8d5a6ab5103393eac5344ae69bcb860601ddJohn McCall    llvm_unreachable("TypeDecl without a type?");
211249aa7ff1245abd03e6e998e01302df31e4c6f8f6Argyrios Kyrtzidis
211349aa7ff1245abd03e6e998e01302df31e4c6f8f6Argyrios Kyrtzidis  return QualType(Decl->TypeForDecl, 0);
21142ce52f3fb95bf544db6bd3d91a72bce7d9cceb6cDouglas Gregor}
21152ce52f3fb95bf544db6bd3d91a72bce7d9cceb6cDouglas Gregor
21165f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// getTypedefType - Return the unique reference to the type for the
2117162e1c1b487352434552147967c3dd296ebee2f7Richard Smith/// specified typedef name decl.
21189763e221e16026ddf487d2564ed349d2c874a1a1Argyrios KyrtzidisQualType
2119162e1c1b487352434552147967c3dd296ebee2f7Richard SmithASTContext::getTypedefType(const TypedefNameDecl *Decl,
2120162e1c1b487352434552147967c3dd296ebee2f7Richard Smith                           QualType Canonical) const {
21215f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  if (Decl->TypeForDecl) return QualType(Decl->TypeForDecl, 0);
21221eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
21239763e221e16026ddf487d2564ed349d2c874a1a1Argyrios Kyrtzidis  if (Canonical.isNull())
21249763e221e16026ddf487d2564ed349d2c874a1a1Argyrios Kyrtzidis    Canonical = getCanonicalType(Decl->getUnderlyingType());
2125f4c7371fb1d3cebcfb40abad4537bb82515704eaJohn McCall  TypedefType *newType = new(*this, TypeAlignment)
21266b304a0254a13f42390b865ff5ba668a49cc58aeJohn McCall    TypedefType(Type::Typedef, Decl, Canonical);
2127f4c7371fb1d3cebcfb40abad4537bb82515704eaJohn McCall  Decl->TypeForDecl = newType;
2128f4c7371fb1d3cebcfb40abad4537bb82515704eaJohn McCall  Types.push_back(newType);
2129f4c7371fb1d3cebcfb40abad4537bb82515704eaJohn McCall  return QualType(newType, 0);
21305f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
21315f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
21324ba2a17694148e16eaa8d3917f657ffcd3667be4Jay FoadQualType ASTContext::getRecordType(const RecordDecl *Decl) const {
2133400f5125e2432d648f2c8a31b36a7f318a880c47Argyrios Kyrtzidis  if (Decl->TypeForDecl) return QualType(Decl->TypeForDecl, 0);
2134400f5125e2432d648f2c8a31b36a7f318a880c47Argyrios Kyrtzidis
2135400f5125e2432d648f2c8a31b36a7f318a880c47Argyrios Kyrtzidis  if (const RecordDecl *PrevDecl = Decl->getPreviousDeclaration())
2136400f5125e2432d648f2c8a31b36a7f318a880c47Argyrios Kyrtzidis    if (PrevDecl->TypeForDecl)
2137400f5125e2432d648f2c8a31b36a7f318a880c47Argyrios Kyrtzidis      return QualType(Decl->TypeForDecl = PrevDecl->TypeForDecl, 0);
2138400f5125e2432d648f2c8a31b36a7f318a880c47Argyrios Kyrtzidis
2139f4c7371fb1d3cebcfb40abad4537bb82515704eaJohn McCall  RecordType *newType = new (*this, TypeAlignment) RecordType(Decl);
2140f4c7371fb1d3cebcfb40abad4537bb82515704eaJohn McCall  Decl->TypeForDecl = newType;
2141f4c7371fb1d3cebcfb40abad4537bb82515704eaJohn McCall  Types.push_back(newType);
2142f4c7371fb1d3cebcfb40abad4537bb82515704eaJohn McCall  return QualType(newType, 0);
2143400f5125e2432d648f2c8a31b36a7f318a880c47Argyrios Kyrtzidis}
2144400f5125e2432d648f2c8a31b36a7f318a880c47Argyrios Kyrtzidis
21454ba2a17694148e16eaa8d3917f657ffcd3667be4Jay FoadQualType ASTContext::getEnumType(const EnumDecl *Decl) const {
2146400f5125e2432d648f2c8a31b36a7f318a880c47Argyrios Kyrtzidis  if (Decl->TypeForDecl) return QualType(Decl->TypeForDecl, 0);
2147400f5125e2432d648f2c8a31b36a7f318a880c47Argyrios Kyrtzidis
2148400f5125e2432d648f2c8a31b36a7f318a880c47Argyrios Kyrtzidis  if (const EnumDecl *PrevDecl = Decl->getPreviousDeclaration())
2149400f5125e2432d648f2c8a31b36a7f318a880c47Argyrios Kyrtzidis    if (PrevDecl->TypeForDecl)
2150400f5125e2432d648f2c8a31b36a7f318a880c47Argyrios Kyrtzidis      return QualType(Decl->TypeForDecl = PrevDecl->TypeForDecl, 0);
2151400f5125e2432d648f2c8a31b36a7f318a880c47Argyrios Kyrtzidis
2152f4c7371fb1d3cebcfb40abad4537bb82515704eaJohn McCall  EnumType *newType = new (*this, TypeAlignment) EnumType(Decl);
2153f4c7371fb1d3cebcfb40abad4537bb82515704eaJohn McCall  Decl->TypeForDecl = newType;
2154f4c7371fb1d3cebcfb40abad4537bb82515704eaJohn McCall  Types.push_back(newType);
2155f4c7371fb1d3cebcfb40abad4537bb82515704eaJohn McCall  return QualType(newType, 0);
2156400f5125e2432d648f2c8a31b36a7f318a880c47Argyrios Kyrtzidis}
2157400f5125e2432d648f2c8a31b36a7f318a880c47Argyrios Kyrtzidis
21589d156a7b1b2771e191f2f5a45a7b7a694129463bJohn McCallQualType ASTContext::getAttributedType(AttributedType::Kind attrKind,
21599d156a7b1b2771e191f2f5a45a7b7a694129463bJohn McCall                                       QualType modifiedType,
21609d156a7b1b2771e191f2f5a45a7b7a694129463bJohn McCall                                       QualType equivalentType) {
21619d156a7b1b2771e191f2f5a45a7b7a694129463bJohn McCall  llvm::FoldingSetNodeID id;
21629d156a7b1b2771e191f2f5a45a7b7a694129463bJohn McCall  AttributedType::Profile(id, attrKind, modifiedType, equivalentType);
21639d156a7b1b2771e191f2f5a45a7b7a694129463bJohn McCall
21649d156a7b1b2771e191f2f5a45a7b7a694129463bJohn McCall  void *insertPos = 0;
21659d156a7b1b2771e191f2f5a45a7b7a694129463bJohn McCall  AttributedType *type = AttributedTypes.FindNodeOrInsertPos(id, insertPos);
21669d156a7b1b2771e191f2f5a45a7b7a694129463bJohn McCall  if (type) return QualType(type, 0);
21679d156a7b1b2771e191f2f5a45a7b7a694129463bJohn McCall
21689d156a7b1b2771e191f2f5a45a7b7a694129463bJohn McCall  QualType canon = getCanonicalType(equivalentType);
21699d156a7b1b2771e191f2f5a45a7b7a694129463bJohn McCall  type = new (*this, TypeAlignment)
21709d156a7b1b2771e191f2f5a45a7b7a694129463bJohn McCall           AttributedType(canon, attrKind, modifiedType, equivalentType);
21719d156a7b1b2771e191f2f5a45a7b7a694129463bJohn McCall
21729d156a7b1b2771e191f2f5a45a7b7a694129463bJohn McCall  Types.push_back(type);
21739d156a7b1b2771e191f2f5a45a7b7a694129463bJohn McCall  AttributedTypes.InsertNode(type, insertPos);
21749d156a7b1b2771e191f2f5a45a7b7a694129463bJohn McCall
21759d156a7b1b2771e191f2f5a45a7b7a694129463bJohn McCall  return QualType(type, 0);
21769d156a7b1b2771e191f2f5a45a7b7a694129463bJohn McCall}
21779d156a7b1b2771e191f2f5a45a7b7a694129463bJohn McCall
21789d156a7b1b2771e191f2f5a45a7b7a694129463bJohn McCall
217949a832bd499d6f61c23655f1fac99f0dd229756eJohn McCall/// \brief Retrieve a substitution-result type.
218049a832bd499d6f61c23655f1fac99f0dd229756eJohn McCallQualType
218149a832bd499d6f61c23655f1fac99f0dd229756eJohn McCallASTContext::getSubstTemplateTypeParmType(const TemplateTypeParmType *Parm,
21824ba2a17694148e16eaa8d3917f657ffcd3667be4Jay Foad                                         QualType Replacement) const {
2183467b27b9a24bdc823218ad1ad0e37673b6cc1e83John McCall  assert(Replacement.isCanonical()
218449a832bd499d6f61c23655f1fac99f0dd229756eJohn McCall         && "replacement types must always be canonical");
218549a832bd499d6f61c23655f1fac99f0dd229756eJohn McCall
218649a832bd499d6f61c23655f1fac99f0dd229756eJohn McCall  llvm::FoldingSetNodeID ID;
218749a832bd499d6f61c23655f1fac99f0dd229756eJohn McCall  SubstTemplateTypeParmType::Profile(ID, Parm, Replacement);
218849a832bd499d6f61c23655f1fac99f0dd229756eJohn McCall  void *InsertPos = 0;
218949a832bd499d6f61c23655f1fac99f0dd229756eJohn McCall  SubstTemplateTypeParmType *SubstParm
219049a832bd499d6f61c23655f1fac99f0dd229756eJohn McCall    = SubstTemplateTypeParmTypes.FindNodeOrInsertPos(ID, InsertPos);
219149a832bd499d6f61c23655f1fac99f0dd229756eJohn McCall
219249a832bd499d6f61c23655f1fac99f0dd229756eJohn McCall  if (!SubstParm) {
219349a832bd499d6f61c23655f1fac99f0dd229756eJohn McCall    SubstParm = new (*this, TypeAlignment)
219449a832bd499d6f61c23655f1fac99f0dd229756eJohn McCall      SubstTemplateTypeParmType(Parm, Replacement);
219549a832bd499d6f61c23655f1fac99f0dd229756eJohn McCall    Types.push_back(SubstParm);
219649a832bd499d6f61c23655f1fac99f0dd229756eJohn McCall    SubstTemplateTypeParmTypes.InsertNode(SubstParm, InsertPos);
219749a832bd499d6f61c23655f1fac99f0dd229756eJohn McCall  }
219849a832bd499d6f61c23655f1fac99f0dd229756eJohn McCall
219949a832bd499d6f61c23655f1fac99f0dd229756eJohn McCall  return QualType(SubstParm, 0);
220049a832bd499d6f61c23655f1fac99f0dd229756eJohn McCall}
220149a832bd499d6f61c23655f1fac99f0dd229756eJohn McCall
2202c3069d618f4661d923cb1b5c4525b082fce73b04Douglas Gregor/// \brief Retrieve a
2203c3069d618f4661d923cb1b5c4525b082fce73b04Douglas GregorQualType ASTContext::getSubstTemplateTypeParmPackType(
2204c3069d618f4661d923cb1b5c4525b082fce73b04Douglas Gregor                                          const TemplateTypeParmType *Parm,
2205c3069d618f4661d923cb1b5c4525b082fce73b04Douglas Gregor                                              const TemplateArgument &ArgPack) {
2206c3069d618f4661d923cb1b5c4525b082fce73b04Douglas Gregor#ifndef NDEBUG
2207c3069d618f4661d923cb1b5c4525b082fce73b04Douglas Gregor  for (TemplateArgument::pack_iterator P = ArgPack.pack_begin(),
2208c3069d618f4661d923cb1b5c4525b082fce73b04Douglas Gregor                                    PEnd = ArgPack.pack_end();
2209c3069d618f4661d923cb1b5c4525b082fce73b04Douglas Gregor       P != PEnd; ++P) {
2210c3069d618f4661d923cb1b5c4525b082fce73b04Douglas Gregor    assert(P->getKind() == TemplateArgument::Type &&"Pack contains a non-type");
2211c3069d618f4661d923cb1b5c4525b082fce73b04Douglas Gregor    assert(P->getAsType().isCanonical() && "Pack contains non-canonical type");
2212c3069d618f4661d923cb1b5c4525b082fce73b04Douglas Gregor  }
2213c3069d618f4661d923cb1b5c4525b082fce73b04Douglas Gregor#endif
2214c3069d618f4661d923cb1b5c4525b082fce73b04Douglas Gregor
2215c3069d618f4661d923cb1b5c4525b082fce73b04Douglas Gregor  llvm::FoldingSetNodeID ID;
2216c3069d618f4661d923cb1b5c4525b082fce73b04Douglas Gregor  SubstTemplateTypeParmPackType::Profile(ID, Parm, ArgPack);
2217c3069d618f4661d923cb1b5c4525b082fce73b04Douglas Gregor  void *InsertPos = 0;
2218c3069d618f4661d923cb1b5c4525b082fce73b04Douglas Gregor  if (SubstTemplateTypeParmPackType *SubstParm
2219c3069d618f4661d923cb1b5c4525b082fce73b04Douglas Gregor        = SubstTemplateTypeParmPackTypes.FindNodeOrInsertPos(ID, InsertPos))
2220c3069d618f4661d923cb1b5c4525b082fce73b04Douglas Gregor    return QualType(SubstParm, 0);
2221c3069d618f4661d923cb1b5c4525b082fce73b04Douglas Gregor
2222c3069d618f4661d923cb1b5c4525b082fce73b04Douglas Gregor  QualType Canon;
2223c3069d618f4661d923cb1b5c4525b082fce73b04Douglas Gregor  if (!Parm->isCanonicalUnqualified()) {
2224c3069d618f4661d923cb1b5c4525b082fce73b04Douglas Gregor    Canon = getCanonicalType(QualType(Parm, 0));
2225c3069d618f4661d923cb1b5c4525b082fce73b04Douglas Gregor    Canon = getSubstTemplateTypeParmPackType(cast<TemplateTypeParmType>(Canon),
2226c3069d618f4661d923cb1b5c4525b082fce73b04Douglas Gregor                                             ArgPack);
2227c3069d618f4661d923cb1b5c4525b082fce73b04Douglas Gregor    SubstTemplateTypeParmPackTypes.FindNodeOrInsertPos(ID, InsertPos);
2228c3069d618f4661d923cb1b5c4525b082fce73b04Douglas Gregor  }
2229c3069d618f4661d923cb1b5c4525b082fce73b04Douglas Gregor
2230c3069d618f4661d923cb1b5c4525b082fce73b04Douglas Gregor  SubstTemplateTypeParmPackType *SubstParm
2231c3069d618f4661d923cb1b5c4525b082fce73b04Douglas Gregor    = new (*this, TypeAlignment) SubstTemplateTypeParmPackType(Parm, Canon,
2232c3069d618f4661d923cb1b5c4525b082fce73b04Douglas Gregor                                                               ArgPack);
2233c3069d618f4661d923cb1b5c4525b082fce73b04Douglas Gregor  Types.push_back(SubstParm);
2234c3069d618f4661d923cb1b5c4525b082fce73b04Douglas Gregor  SubstTemplateTypeParmTypes.InsertNode(SubstParm, InsertPos);
2235c3069d618f4661d923cb1b5c4525b082fce73b04Douglas Gregor  return QualType(SubstParm, 0);
2236c3069d618f4661d923cb1b5c4525b082fce73b04Douglas Gregor}
2237c3069d618f4661d923cb1b5c4525b082fce73b04Douglas Gregor
2238fab9d67cebb87be968e7ae31a3b549a5279b5d51Douglas Gregor/// \brief Retrieve the template type parameter type for a template
22391eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// parameter or parameter pack with the given depth, index, and (optionally)
224076e4ce42a30cee4dc40ce7c6014874fbc4f9baa7Anders Carlsson/// name.
22411eb4433ac451dc16f4133a88af2d002ac26c58efMike StumpQualType ASTContext::getTemplateTypeParmType(unsigned Depth, unsigned Index,
224276e4ce42a30cee4dc40ce7c6014874fbc4f9baa7Anders Carlsson                                             bool ParameterPack,
22434fb86f8c4585e53c21c847ad3de9e3b2de123cd9Chandler Carruth                                             TemplateTypeParmDecl *TTPDecl) const {
2244fab9d67cebb87be968e7ae31a3b549a5279b5d51Douglas Gregor  llvm::FoldingSetNodeID ID;
22454fb86f8c4585e53c21c847ad3de9e3b2de123cd9Chandler Carruth  TemplateTypeParmType::Profile(ID, Depth, Index, ParameterPack, TTPDecl);
2246fab9d67cebb87be968e7ae31a3b549a5279b5d51Douglas Gregor  void *InsertPos = 0;
22471eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  TemplateTypeParmType *TypeParm
2248fab9d67cebb87be968e7ae31a3b549a5279b5d51Douglas Gregor    = TemplateTypeParmTypes.FindNodeOrInsertPos(ID, InsertPos);
2249fab9d67cebb87be968e7ae31a3b549a5279b5d51Douglas Gregor
2250fab9d67cebb87be968e7ae31a3b549a5279b5d51Douglas Gregor  if (TypeParm)
2251fab9d67cebb87be968e7ae31a3b549a5279b5d51Douglas Gregor    return QualType(TypeParm, 0);
22521eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
22534fb86f8c4585e53c21c847ad3de9e3b2de123cd9Chandler Carruth  if (TTPDecl) {
225476e4ce42a30cee4dc40ce7c6014874fbc4f9baa7Anders Carlsson    QualType Canon = getTemplateTypeParmType(Depth, Index, ParameterPack);
22554fb86f8c4585e53c21c847ad3de9e3b2de123cd9Chandler Carruth    TypeParm = new (*this, TypeAlignment) TemplateTypeParmType(TTPDecl, Canon);
2256789b1f640205e81b5af250693246120f1ce9d147Douglas Gregor
2257789b1f640205e81b5af250693246120f1ce9d147Douglas Gregor    TemplateTypeParmType *TypeCheck
2258789b1f640205e81b5af250693246120f1ce9d147Douglas Gregor      = TemplateTypeParmTypes.FindNodeOrInsertPos(ID, InsertPos);
2259789b1f640205e81b5af250693246120f1ce9d147Douglas Gregor    assert(!TypeCheck && "Template type parameter canonical type broken");
2260789b1f640205e81b5af250693246120f1ce9d147Douglas Gregor    (void)TypeCheck;
226176e4ce42a30cee4dc40ce7c6014874fbc4f9baa7Anders Carlsson  } else
22626b304a0254a13f42390b865ff5ba668a49cc58aeJohn McCall    TypeParm = new (*this, TypeAlignment)
22636b304a0254a13f42390b865ff5ba668a49cc58aeJohn McCall      TemplateTypeParmType(Depth, Index, ParameterPack);
2264fab9d67cebb87be968e7ae31a3b549a5279b5d51Douglas Gregor
2265fab9d67cebb87be968e7ae31a3b549a5279b5d51Douglas Gregor  Types.push_back(TypeParm);
2266fab9d67cebb87be968e7ae31a3b549a5279b5d51Douglas Gregor  TemplateTypeParmTypes.InsertNode(TypeParm, InsertPos);
2267fab9d67cebb87be968e7ae31a3b549a5279b5d51Douglas Gregor
2268fab9d67cebb87be968e7ae31a3b549a5279b5d51Douglas Gregor  return QualType(TypeParm, 0);
2269fab9d67cebb87be968e7ae31a3b549a5279b5d51Douglas Gregor}
2270fab9d67cebb87be968e7ae31a3b549a5279b5d51Douglas Gregor
22713cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCallTypeSourceInfo *
22723cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCallASTContext::getTemplateSpecializationTypeInfo(TemplateName Name,
22733cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall                                              SourceLocation NameLoc,
22743cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall                                        const TemplateArgumentListInfo &Args,
22753e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith                                              QualType Underlying) const {
22767c3179cf463c3b3b8c21dbb955f933ba50b74f28Douglas Gregor  assert(!Name.getAsDependentTemplateName() &&
22777c3179cf463c3b3b8c21dbb955f933ba50b74f28Douglas Gregor         "No dependent template names here!");
22783e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith  QualType TST = getTemplateSpecializationType(Name, Args, Underlying);
22793cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall
22803cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall  TypeSourceInfo *DI = CreateTypeSourceInfo(TST);
22813cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall  TemplateSpecializationTypeLoc TL
22823cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall    = cast<TemplateSpecializationTypeLoc>(DI->getTypeLoc());
22833cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall  TL.setTemplateNameLoc(NameLoc);
22843cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall  TL.setLAngleLoc(Args.getLAngleLoc());
22853cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall  TL.setRAngleLoc(Args.getRAngleLoc());
22863cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall  for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i)
22873cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall    TL.setArgLocInfo(i, Args[i].getLocInfo());
22883cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall  return DI;
22893cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall}
22903cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall
22911eb4433ac451dc16f4133a88af2d002ac26c58efMike StumpQualType
22927532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas GregorASTContext::getTemplateSpecializationType(TemplateName Template,
2293d5532b6cfff2977e0c59fa6ead7f7973984a620dJohn McCall                                          const TemplateArgumentListInfo &Args,
22943e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith                                          QualType Underlying) const {
22957c3179cf463c3b3b8c21dbb955f933ba50b74f28Douglas Gregor  assert(!Template.getAsDependentTemplateName() &&
22967c3179cf463c3b3b8c21dbb955f933ba50b74f28Douglas Gregor         "No dependent template names here!");
22977c3179cf463c3b3b8c21dbb955f933ba50b74f28Douglas Gregor
2298d5532b6cfff2977e0c59fa6ead7f7973984a620dJohn McCall  unsigned NumArgs = Args.size();
2299d5532b6cfff2977e0c59fa6ead7f7973984a620dJohn McCall
2300833ca991c1bfc967f0995974ca86f66ba1f666b5John McCall  llvm::SmallVector<TemplateArgument, 4> ArgVec;
2301833ca991c1bfc967f0995974ca86f66ba1f666b5John McCall  ArgVec.reserve(NumArgs);
2302833ca991c1bfc967f0995974ca86f66ba1f666b5John McCall  for (unsigned i = 0; i != NumArgs; ++i)
2303833ca991c1bfc967f0995974ca86f66ba1f666b5John McCall    ArgVec.push_back(Args[i].getArgument());
2304833ca991c1bfc967f0995974ca86f66ba1f666b5John McCall
230531f17ecbef57b5679c017c375db330546b7b5145John McCall  return getTemplateSpecializationType(Template, ArgVec.data(), NumArgs,
23063e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith                                       Underlying);
2307833ca991c1bfc967f0995974ca86f66ba1f666b5John McCall}
2308833ca991c1bfc967f0995974ca86f66ba1f666b5John McCall
2309833ca991c1bfc967f0995974ca86f66ba1f666b5John McCallQualType
2310833ca991c1bfc967f0995974ca86f66ba1f666b5John McCallASTContext::getTemplateSpecializationType(TemplateName Template,
23117532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor                                          const TemplateArgument *Args,
23127532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor                                          unsigned NumArgs,
23133e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith                                          QualType Underlying) const {
23147c3179cf463c3b3b8c21dbb955f933ba50b74f28Douglas Gregor  assert(!Template.getAsDependentTemplateName() &&
23157c3179cf463c3b3b8c21dbb955f933ba50b74f28Douglas Gregor         "No dependent template names here!");
23160f0ea2a96534c615ff5fdd81363989b23cf2164aDouglas Gregor  // Look through qualified template names.
23170f0ea2a96534c615ff5fdd81363989b23cf2164aDouglas Gregor  if (QualifiedTemplateName *QTN = Template.getAsQualifiedTemplateName())
23180f0ea2a96534c615ff5fdd81363989b23cf2164aDouglas Gregor    Template = TemplateName(QTN->getTemplateDecl());
23197c3179cf463c3b3b8c21dbb955f933ba50b74f28Douglas Gregor
23203e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith  bool isTypeAlias =
23213e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith    Template.getAsTemplateDecl() &&
23223e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith    isa<TypeAliasTemplateDecl>(Template.getAsTemplateDecl());
23233e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith
23243e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith  QualType CanonType;
23253e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith  if (!Underlying.isNull())
23263e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith    CanonType = getCanonicalType(Underlying);
23273e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith  else {
23283e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith    assert(!isTypeAlias &&
23293e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith           "Underlying type for template alias must be computed by caller");
23303e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith    CanonType = getCanonicalTemplateSpecializationType(Template, Args,
23313e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith                                                       NumArgs);
23323e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith  }
2333fc705b84347e6fb4746a1a7e26949f64c2f2f358Douglas Gregor
23341275ae098acda31fe0e434510c729fcfed0458a1Douglas Gregor  // Allocate the (non-canonical) template specialization type, but don't
23351275ae098acda31fe0e434510c729fcfed0458a1Douglas Gregor  // try to unique it: these types typically have location information that
23361275ae098acda31fe0e434510c729fcfed0458a1Douglas Gregor  // we don't unique and don't want to lose.
23373e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith  void *Mem = Allocate(sizeof(TemplateSpecializationType) +
23383e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith                       sizeof(TemplateArgument) * NumArgs +
23393e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith                       (isTypeAlias ? sizeof(QualType) : 0),
23406b304a0254a13f42390b865ff5ba668a49cc58aeJohn McCall                       TypeAlignment);
23411eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  TemplateSpecializationType *Spec
2342ef99001908e799c388f1363b1e607dad5f5b57d3John McCall    = new (Mem) TemplateSpecializationType(Template,
234331f17ecbef57b5679c017c375db330546b7b5145John McCall                                           Args, NumArgs,
23443e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith                                           CanonType,
23453e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith                                         isTypeAlias ? Underlying : QualType());
23461eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
234755f6b14230c94272efbbcdd89a92224c8db9f225Douglas Gregor  Types.push_back(Spec);
23481eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  return QualType(Spec, 0);
234955f6b14230c94272efbbcdd89a92224c8db9f225Douglas Gregor}
235055f6b14230c94272efbbcdd89a92224c8db9f225Douglas Gregor
23511eb4433ac451dc16f4133a88af2d002ac26c58efMike StumpQualType
23529763e221e16026ddf487d2564ed349d2c874a1a1Argyrios KyrtzidisASTContext::getCanonicalTemplateSpecializationType(TemplateName Template,
23539763e221e16026ddf487d2564ed349d2c874a1a1Argyrios Kyrtzidis                                                   const TemplateArgument *Args,
23544ba2a17694148e16eaa8d3917f657ffcd3667be4Jay Foad                                                   unsigned NumArgs) const {
23557c3179cf463c3b3b8c21dbb955f933ba50b74f28Douglas Gregor  assert(!Template.getAsDependentTemplateName() &&
23567c3179cf463c3b3b8c21dbb955f933ba50b74f28Douglas Gregor         "No dependent template names here!");
23573e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith  assert((!Template.getAsTemplateDecl() ||
23583e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith          !isa<TypeAliasTemplateDecl>(Template.getAsTemplateDecl())) &&
23593e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith         "Underlying type for template alias must be computed by caller");
23603e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith
23610f0ea2a96534c615ff5fdd81363989b23cf2164aDouglas Gregor  // Look through qualified template names.
23620f0ea2a96534c615ff5fdd81363989b23cf2164aDouglas Gregor  if (QualifiedTemplateName *QTN = Template.getAsQualifiedTemplateName())
23630f0ea2a96534c615ff5fdd81363989b23cf2164aDouglas Gregor    Template = TemplateName(QTN->getTemplateDecl());
23647c3179cf463c3b3b8c21dbb955f933ba50b74f28Douglas Gregor
23659763e221e16026ddf487d2564ed349d2c874a1a1Argyrios Kyrtzidis  // Build the canonical template specialization type.
23669763e221e16026ddf487d2564ed349d2c874a1a1Argyrios Kyrtzidis  TemplateName CanonTemplate = getCanonicalTemplateName(Template);
23679763e221e16026ddf487d2564ed349d2c874a1a1Argyrios Kyrtzidis  llvm::SmallVector<TemplateArgument, 4> CanonArgs;
23689763e221e16026ddf487d2564ed349d2c874a1a1Argyrios Kyrtzidis  CanonArgs.reserve(NumArgs);
23699763e221e16026ddf487d2564ed349d2c874a1a1Argyrios Kyrtzidis  for (unsigned I = 0; I != NumArgs; ++I)
23709763e221e16026ddf487d2564ed349d2c874a1a1Argyrios Kyrtzidis    CanonArgs.push_back(getCanonicalTemplateArgument(Args[I]));
23719763e221e16026ddf487d2564ed349d2c874a1a1Argyrios Kyrtzidis
23729763e221e16026ddf487d2564ed349d2c874a1a1Argyrios Kyrtzidis  // Determine whether this canonical template specialization type already
23739763e221e16026ddf487d2564ed349d2c874a1a1Argyrios Kyrtzidis  // exists.
23749763e221e16026ddf487d2564ed349d2c874a1a1Argyrios Kyrtzidis  llvm::FoldingSetNodeID ID;
23759763e221e16026ddf487d2564ed349d2c874a1a1Argyrios Kyrtzidis  TemplateSpecializationType::Profile(ID, CanonTemplate,
23769763e221e16026ddf487d2564ed349d2c874a1a1Argyrios Kyrtzidis                                      CanonArgs.data(), NumArgs, *this);
23779763e221e16026ddf487d2564ed349d2c874a1a1Argyrios Kyrtzidis
23789763e221e16026ddf487d2564ed349d2c874a1a1Argyrios Kyrtzidis  void *InsertPos = 0;
23799763e221e16026ddf487d2564ed349d2c874a1a1Argyrios Kyrtzidis  TemplateSpecializationType *Spec
23809763e221e16026ddf487d2564ed349d2c874a1a1Argyrios Kyrtzidis    = TemplateSpecializationTypes.FindNodeOrInsertPos(ID, InsertPos);
23819763e221e16026ddf487d2564ed349d2c874a1a1Argyrios Kyrtzidis
23829763e221e16026ddf487d2564ed349d2c874a1a1Argyrios Kyrtzidis  if (!Spec) {
23839763e221e16026ddf487d2564ed349d2c874a1a1Argyrios Kyrtzidis    // Allocate a new canonical template specialization type.
23849763e221e16026ddf487d2564ed349d2c874a1a1Argyrios Kyrtzidis    void *Mem = Allocate((sizeof(TemplateSpecializationType) +
23859763e221e16026ddf487d2564ed349d2c874a1a1Argyrios Kyrtzidis                          sizeof(TemplateArgument) * NumArgs),
23869763e221e16026ddf487d2564ed349d2c874a1a1Argyrios Kyrtzidis                         TypeAlignment);
23879763e221e16026ddf487d2564ed349d2c874a1a1Argyrios Kyrtzidis    Spec = new (Mem) TemplateSpecializationType(CanonTemplate,
23889763e221e16026ddf487d2564ed349d2c874a1a1Argyrios Kyrtzidis                                                CanonArgs.data(), NumArgs,
23893e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith                                                QualType(), QualType());
23909763e221e16026ddf487d2564ed349d2c874a1a1Argyrios Kyrtzidis    Types.push_back(Spec);
23919763e221e16026ddf487d2564ed349d2c874a1a1Argyrios Kyrtzidis    TemplateSpecializationTypes.InsertNode(Spec, InsertPos);
23929763e221e16026ddf487d2564ed349d2c874a1a1Argyrios Kyrtzidis  }
23939763e221e16026ddf487d2564ed349d2c874a1a1Argyrios Kyrtzidis
23949763e221e16026ddf487d2564ed349d2c874a1a1Argyrios Kyrtzidis  assert(Spec->isDependentType() &&
23959763e221e16026ddf487d2564ed349d2c874a1a1Argyrios Kyrtzidis         "Non-dependent template-id type must have a canonical type");
23969763e221e16026ddf487d2564ed349d2c874a1a1Argyrios Kyrtzidis  return QualType(Spec, 0);
23979763e221e16026ddf487d2564ed349d2c874a1a1Argyrios Kyrtzidis}
23989763e221e16026ddf487d2564ed349d2c874a1a1Argyrios Kyrtzidis
23999763e221e16026ddf487d2564ed349d2c874a1a1Argyrios KyrtzidisQualType
2400465d41b92b2c862f3062c412a0538db65c6a2661Abramo BagnaraASTContext::getElaboratedType(ElaboratedTypeKeyword Keyword,
2401465d41b92b2c862f3062c412a0538db65c6a2661Abramo Bagnara                              NestedNameSpecifier *NNS,
24024ba2a17694148e16eaa8d3917f657ffcd3667be4Jay Foad                              QualType NamedType) const {
2403e4e5b054b4917f0ee493bb2fda5b1ec749bfb9a1Douglas Gregor  llvm::FoldingSetNodeID ID;
2404465d41b92b2c862f3062c412a0538db65c6a2661Abramo Bagnara  ElaboratedType::Profile(ID, Keyword, NNS, NamedType);
2405e4e5b054b4917f0ee493bb2fda5b1ec749bfb9a1Douglas Gregor
2406e4e5b054b4917f0ee493bb2fda5b1ec749bfb9a1Douglas Gregor  void *InsertPos = 0;
2407465d41b92b2c862f3062c412a0538db65c6a2661Abramo Bagnara  ElaboratedType *T = ElaboratedTypes.FindNodeOrInsertPos(ID, InsertPos);
2408e4e5b054b4917f0ee493bb2fda5b1ec749bfb9a1Douglas Gregor  if (T)
2409e4e5b054b4917f0ee493bb2fda5b1ec749bfb9a1Douglas Gregor    return QualType(T, 0);
2410e4e5b054b4917f0ee493bb2fda5b1ec749bfb9a1Douglas Gregor
2411789b1f640205e81b5af250693246120f1ce9d147Douglas Gregor  QualType Canon = NamedType;
2412789b1f640205e81b5af250693246120f1ce9d147Douglas Gregor  if (!Canon.isCanonical()) {
2413789b1f640205e81b5af250693246120f1ce9d147Douglas Gregor    Canon = getCanonicalType(NamedType);
2414465d41b92b2c862f3062c412a0538db65c6a2661Abramo Bagnara    ElaboratedType *CheckT = ElaboratedTypes.FindNodeOrInsertPos(ID, InsertPos);
2415465d41b92b2c862f3062c412a0538db65c6a2661Abramo Bagnara    assert(!CheckT && "Elaborated canonical type broken");
2416789b1f640205e81b5af250693246120f1ce9d147Douglas Gregor    (void)CheckT;
2417789b1f640205e81b5af250693246120f1ce9d147Douglas Gregor  }
2418789b1f640205e81b5af250693246120f1ce9d147Douglas Gregor
2419465d41b92b2c862f3062c412a0538db65c6a2661Abramo Bagnara  T = new (*this) ElaboratedType(Keyword, NNS, NamedType, Canon);
2420e4e5b054b4917f0ee493bb2fda5b1ec749bfb9a1Douglas Gregor  Types.push_back(T);
2421465d41b92b2c862f3062c412a0538db65c6a2661Abramo Bagnara  ElaboratedTypes.InsertNode(T, InsertPos);
2422e4e5b054b4917f0ee493bb2fda5b1ec749bfb9a1Douglas Gregor  return QualType(T, 0);
2423e4e5b054b4917f0ee493bb2fda5b1ec749bfb9a1Douglas Gregor}
2424e4e5b054b4917f0ee493bb2fda5b1ec749bfb9a1Douglas Gregor
2425075f8f1b6bed4d1b224c74f87508534cc6392ce6Abramo BagnaraQualType
24264ba2a17694148e16eaa8d3917f657ffcd3667be4Jay FoadASTContext::getParenType(QualType InnerType) const {
2427075f8f1b6bed4d1b224c74f87508534cc6392ce6Abramo Bagnara  llvm::FoldingSetNodeID ID;
2428075f8f1b6bed4d1b224c74f87508534cc6392ce6Abramo Bagnara  ParenType::Profile(ID, InnerType);
2429075f8f1b6bed4d1b224c74f87508534cc6392ce6Abramo Bagnara
2430075f8f1b6bed4d1b224c74f87508534cc6392ce6Abramo Bagnara  void *InsertPos = 0;
2431075f8f1b6bed4d1b224c74f87508534cc6392ce6Abramo Bagnara  ParenType *T = ParenTypes.FindNodeOrInsertPos(ID, InsertPos);
2432075f8f1b6bed4d1b224c74f87508534cc6392ce6Abramo Bagnara  if (T)
2433075f8f1b6bed4d1b224c74f87508534cc6392ce6Abramo Bagnara    return QualType(T, 0);
2434075f8f1b6bed4d1b224c74f87508534cc6392ce6Abramo Bagnara
2435075f8f1b6bed4d1b224c74f87508534cc6392ce6Abramo Bagnara  QualType Canon = InnerType;
2436075f8f1b6bed4d1b224c74f87508534cc6392ce6Abramo Bagnara  if (!Canon.isCanonical()) {
2437075f8f1b6bed4d1b224c74f87508534cc6392ce6Abramo Bagnara    Canon = getCanonicalType(InnerType);
2438075f8f1b6bed4d1b224c74f87508534cc6392ce6Abramo Bagnara    ParenType *CheckT = ParenTypes.FindNodeOrInsertPos(ID, InsertPos);
2439075f8f1b6bed4d1b224c74f87508534cc6392ce6Abramo Bagnara    assert(!CheckT && "Paren canonical type broken");
2440075f8f1b6bed4d1b224c74f87508534cc6392ce6Abramo Bagnara    (void)CheckT;
2441075f8f1b6bed4d1b224c74f87508534cc6392ce6Abramo Bagnara  }
2442075f8f1b6bed4d1b224c74f87508534cc6392ce6Abramo Bagnara
2443075f8f1b6bed4d1b224c74f87508534cc6392ce6Abramo Bagnara  T = new (*this) ParenType(InnerType, Canon);
2444075f8f1b6bed4d1b224c74f87508534cc6392ce6Abramo Bagnara  Types.push_back(T);
2445075f8f1b6bed4d1b224c74f87508534cc6392ce6Abramo Bagnara  ParenTypes.InsertNode(T, InsertPos);
2446075f8f1b6bed4d1b224c74f87508534cc6392ce6Abramo Bagnara  return QualType(T, 0);
2447075f8f1b6bed4d1b224c74f87508534cc6392ce6Abramo Bagnara}
2448075f8f1b6bed4d1b224c74f87508534cc6392ce6Abramo Bagnara
24494a2023f5014e82389d5980d307b89c545dbbac81Douglas GregorQualType ASTContext::getDependentNameType(ElaboratedTypeKeyword Keyword,
24504a2023f5014e82389d5980d307b89c545dbbac81Douglas Gregor                                          NestedNameSpecifier *NNS,
24514a2023f5014e82389d5980d307b89c545dbbac81Douglas Gregor                                          const IdentifierInfo *Name,
24524ba2a17694148e16eaa8d3917f657ffcd3667be4Jay Foad                                          QualType Canon) const {
2453d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor  assert(NNS->isDependent() && "nested-name-specifier must be dependent");
2454d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor
2455d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor  if (Canon.isNull()) {
2456d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor    NestedNameSpecifier *CanonNNS = getCanonicalNestedNameSpecifier(NNS);
24574a2023f5014e82389d5980d307b89c545dbbac81Douglas Gregor    ElaboratedTypeKeyword CanonKeyword = Keyword;
24584a2023f5014e82389d5980d307b89c545dbbac81Douglas Gregor    if (Keyword == ETK_None)
24594a2023f5014e82389d5980d307b89c545dbbac81Douglas Gregor      CanonKeyword = ETK_Typename;
24604a2023f5014e82389d5980d307b89c545dbbac81Douglas Gregor
24614a2023f5014e82389d5980d307b89c545dbbac81Douglas Gregor    if (CanonNNS != NNS || CanonKeyword != Keyword)
24624a2023f5014e82389d5980d307b89c545dbbac81Douglas Gregor      Canon = getDependentNameType(CanonKeyword, CanonNNS, Name);
2463d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor  }
2464d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor
2465d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor  llvm::FoldingSetNodeID ID;
24664a2023f5014e82389d5980d307b89c545dbbac81Douglas Gregor  DependentNameType::Profile(ID, Keyword, NNS, Name);
2467d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor
2468d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor  void *InsertPos = 0;
24694714c12a1ab759156b78be8f109ea4c12213af57Douglas Gregor  DependentNameType *T
24704714c12a1ab759156b78be8f109ea4c12213af57Douglas Gregor    = DependentNameTypes.FindNodeOrInsertPos(ID, InsertPos);
2471d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor  if (T)
2472d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor    return QualType(T, 0);
2473d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor
24744a2023f5014e82389d5980d307b89c545dbbac81Douglas Gregor  T = new (*this) DependentNameType(Keyword, NNS, Name, Canon);
2475d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor  Types.push_back(T);
24764714c12a1ab759156b78be8f109ea4c12213af57Douglas Gregor  DependentNameTypes.InsertNode(T, InsertPos);
24771eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  return QualType(T, 0);
2478d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor}
2479d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor
24801eb4433ac451dc16f4133a88af2d002ac26c58efMike StumpQualType
248133500955d731c73717af52088b7fc0e7a85681e7John McCallASTContext::getDependentTemplateSpecializationType(
248233500955d731c73717af52088b7fc0e7a85681e7John McCall                                 ElaboratedTypeKeyword Keyword,
24834a2023f5014e82389d5980d307b89c545dbbac81Douglas Gregor                                 NestedNameSpecifier *NNS,
248433500955d731c73717af52088b7fc0e7a85681e7John McCall                                 const IdentifierInfo *Name,
24854ba2a17694148e16eaa8d3917f657ffcd3667be4Jay Foad                                 const TemplateArgumentListInfo &Args) const {
248633500955d731c73717af52088b7fc0e7a85681e7John McCall  // TODO: avoid this copy
248733500955d731c73717af52088b7fc0e7a85681e7John McCall  llvm::SmallVector<TemplateArgument, 16> ArgCopy;
248833500955d731c73717af52088b7fc0e7a85681e7John McCall  for (unsigned I = 0, E = Args.size(); I != E; ++I)
248933500955d731c73717af52088b7fc0e7a85681e7John McCall    ArgCopy.push_back(Args[I].getArgument());
249033500955d731c73717af52088b7fc0e7a85681e7John McCall  return getDependentTemplateSpecializationType(Keyword, NNS, Name,
249133500955d731c73717af52088b7fc0e7a85681e7John McCall                                                ArgCopy.size(),
249233500955d731c73717af52088b7fc0e7a85681e7John McCall                                                ArgCopy.data());
249333500955d731c73717af52088b7fc0e7a85681e7John McCall}
249433500955d731c73717af52088b7fc0e7a85681e7John McCall
249533500955d731c73717af52088b7fc0e7a85681e7John McCallQualType
249633500955d731c73717af52088b7fc0e7a85681e7John McCallASTContext::getDependentTemplateSpecializationType(
249733500955d731c73717af52088b7fc0e7a85681e7John McCall                                 ElaboratedTypeKeyword Keyword,
249833500955d731c73717af52088b7fc0e7a85681e7John McCall                                 NestedNameSpecifier *NNS,
249933500955d731c73717af52088b7fc0e7a85681e7John McCall                                 const IdentifierInfo *Name,
250033500955d731c73717af52088b7fc0e7a85681e7John McCall                                 unsigned NumArgs,
25014ba2a17694148e16eaa8d3917f657ffcd3667be4Jay Foad                                 const TemplateArgument *Args) const {
2502aa2187de137e5b809dcbbe14f3b61ae907a3d8aaDouglas Gregor  assert((!NNS || NNS->isDependent()) &&
2503aa2187de137e5b809dcbbe14f3b61ae907a3d8aaDouglas Gregor         "nested-name-specifier must be dependent");
25041734317845d60307d474b5da8a8d33adbaf5e723Douglas Gregor
2505789b1f640205e81b5af250693246120f1ce9d147Douglas Gregor  llvm::FoldingSetNodeID ID;
250633500955d731c73717af52088b7fc0e7a85681e7John McCall  DependentTemplateSpecializationType::Profile(ID, *this, Keyword, NNS,
250733500955d731c73717af52088b7fc0e7a85681e7John McCall                                               Name, NumArgs, Args);
2508789b1f640205e81b5af250693246120f1ce9d147Douglas Gregor
2509789b1f640205e81b5af250693246120f1ce9d147Douglas Gregor  void *InsertPos = 0;
251033500955d731c73717af52088b7fc0e7a85681e7John McCall  DependentTemplateSpecializationType *T
251133500955d731c73717af52088b7fc0e7a85681e7John McCall    = DependentTemplateSpecializationTypes.FindNodeOrInsertPos(ID, InsertPos);
2512789b1f640205e81b5af250693246120f1ce9d147Douglas Gregor  if (T)
2513789b1f640205e81b5af250693246120f1ce9d147Douglas Gregor    return QualType(T, 0);
2514789b1f640205e81b5af250693246120f1ce9d147Douglas Gregor
251533500955d731c73717af52088b7fc0e7a85681e7John McCall  NestedNameSpecifier *CanonNNS = getCanonicalNestedNameSpecifier(NNS);
251633500955d731c73717af52088b7fc0e7a85681e7John McCall
251733500955d731c73717af52088b7fc0e7a85681e7John McCall  ElaboratedTypeKeyword CanonKeyword = Keyword;
251833500955d731c73717af52088b7fc0e7a85681e7John McCall  if (Keyword == ETK_None) CanonKeyword = ETK_Typename;
25191734317845d60307d474b5da8a8d33adbaf5e723Douglas Gregor
252033500955d731c73717af52088b7fc0e7a85681e7John McCall  bool AnyNonCanonArgs = false;
252133500955d731c73717af52088b7fc0e7a85681e7John McCall  llvm::SmallVector<TemplateArgument, 16> CanonArgs(NumArgs);
252233500955d731c73717af52088b7fc0e7a85681e7John McCall  for (unsigned I = 0; I != NumArgs; ++I) {
252333500955d731c73717af52088b7fc0e7a85681e7John McCall    CanonArgs[I] = getCanonicalTemplateArgument(Args[I]);
252433500955d731c73717af52088b7fc0e7a85681e7John McCall    if (!CanonArgs[I].structurallyEquals(Args[I]))
252533500955d731c73717af52088b7fc0e7a85681e7John McCall      AnyNonCanonArgs = true;
2526789b1f640205e81b5af250693246120f1ce9d147Douglas Gregor  }
25271734317845d60307d474b5da8a8d33adbaf5e723Douglas Gregor
252833500955d731c73717af52088b7fc0e7a85681e7John McCall  QualType Canon;
252933500955d731c73717af52088b7fc0e7a85681e7John McCall  if (AnyNonCanonArgs || CanonNNS != NNS || CanonKeyword != Keyword) {
253033500955d731c73717af52088b7fc0e7a85681e7John McCall    Canon = getDependentTemplateSpecializationType(CanonKeyword, CanonNNS,
253133500955d731c73717af52088b7fc0e7a85681e7John McCall                                                   Name, NumArgs,
253233500955d731c73717af52088b7fc0e7a85681e7John McCall                                                   CanonArgs.data());
253333500955d731c73717af52088b7fc0e7a85681e7John McCall
253433500955d731c73717af52088b7fc0e7a85681e7John McCall    // Find the insert position again.
253533500955d731c73717af52088b7fc0e7a85681e7John McCall    DependentTemplateSpecializationTypes.FindNodeOrInsertPos(ID, InsertPos);
253633500955d731c73717af52088b7fc0e7a85681e7John McCall  }
253733500955d731c73717af52088b7fc0e7a85681e7John McCall
253833500955d731c73717af52088b7fc0e7a85681e7John McCall  void *Mem = Allocate((sizeof(DependentTemplateSpecializationType) +
253933500955d731c73717af52088b7fc0e7a85681e7John McCall                        sizeof(TemplateArgument) * NumArgs),
254033500955d731c73717af52088b7fc0e7a85681e7John McCall                       TypeAlignment);
2541ef99001908e799c388f1363b1e607dad5f5b57d3John McCall  T = new (Mem) DependentTemplateSpecializationType(Keyword, NNS,
254233500955d731c73717af52088b7fc0e7a85681e7John McCall                                                    Name, NumArgs, Args, Canon);
25431734317845d60307d474b5da8a8d33adbaf5e723Douglas Gregor  Types.push_back(T);
254433500955d731c73717af52088b7fc0e7a85681e7John McCall  DependentTemplateSpecializationTypes.InsertNode(T, InsertPos);
25451eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  return QualType(T, 0);
25461734317845d60307d474b5da8a8d33adbaf5e723Douglas Gregor}
25471734317845d60307d474b5da8a8d33adbaf5e723Douglas Gregor
2548cded4f649cd4b7ba7d461c25c6482ef52b8d3a2aDouglas GregorQualType ASTContext::getPackExpansionType(QualType Pattern,
2549cded4f649cd4b7ba7d461c25c6482ef52b8d3a2aDouglas Gregor                                      llvm::Optional<unsigned> NumExpansions) {
25507536dd5e6c99584481b7dab68b7e7d8df9c54054Douglas Gregor  llvm::FoldingSetNodeID ID;
2551cded4f649cd4b7ba7d461c25c6482ef52b8d3a2aDouglas Gregor  PackExpansionType::Profile(ID, Pattern, NumExpansions);
25527536dd5e6c99584481b7dab68b7e7d8df9c54054Douglas Gregor
25537536dd5e6c99584481b7dab68b7e7d8df9c54054Douglas Gregor  assert(Pattern->containsUnexpandedParameterPack() &&
25547536dd5e6c99584481b7dab68b7e7d8df9c54054Douglas Gregor         "Pack expansions must expand one or more parameter packs");
25557536dd5e6c99584481b7dab68b7e7d8df9c54054Douglas Gregor  void *InsertPos = 0;
25567536dd5e6c99584481b7dab68b7e7d8df9c54054Douglas Gregor  PackExpansionType *T
25577536dd5e6c99584481b7dab68b7e7d8df9c54054Douglas Gregor    = PackExpansionTypes.FindNodeOrInsertPos(ID, InsertPos);
25587536dd5e6c99584481b7dab68b7e7d8df9c54054Douglas Gregor  if (T)
25597536dd5e6c99584481b7dab68b7e7d8df9c54054Douglas Gregor    return QualType(T, 0);
25607536dd5e6c99584481b7dab68b7e7d8df9c54054Douglas Gregor
25617536dd5e6c99584481b7dab68b7e7d8df9c54054Douglas Gregor  QualType Canon;
25627536dd5e6c99584481b7dab68b7e7d8df9c54054Douglas Gregor  if (!Pattern.isCanonical()) {
2563cded4f649cd4b7ba7d461c25c6482ef52b8d3a2aDouglas Gregor    Canon = getPackExpansionType(getCanonicalType(Pattern), NumExpansions);
25647536dd5e6c99584481b7dab68b7e7d8df9c54054Douglas Gregor
25657536dd5e6c99584481b7dab68b7e7d8df9c54054Douglas Gregor    // Find the insert position again.
25667536dd5e6c99584481b7dab68b7e7d8df9c54054Douglas Gregor    PackExpansionTypes.FindNodeOrInsertPos(ID, InsertPos);
25677536dd5e6c99584481b7dab68b7e7d8df9c54054Douglas Gregor  }
25687536dd5e6c99584481b7dab68b7e7d8df9c54054Douglas Gregor
2569cded4f649cd4b7ba7d461c25c6482ef52b8d3a2aDouglas Gregor  T = new (*this) PackExpansionType(Pattern, Canon, NumExpansions);
25707536dd5e6c99584481b7dab68b7e7d8df9c54054Douglas Gregor  Types.push_back(T);
25717536dd5e6c99584481b7dab68b7e7d8df9c54054Douglas Gregor  PackExpansionTypes.InsertNode(T, InsertPos);
25727536dd5e6c99584481b7dab68b7e7d8df9c54054Douglas Gregor  return QualType(T, 0);
25737536dd5e6c99584481b7dab68b7e7d8df9c54054Douglas Gregor}
25747536dd5e6c99584481b7dab68b7e7d8df9c54054Douglas Gregor
257588cb27a160adc305783a44f922ee4b216006ebf9Chris Lattner/// CmpProtocolNames - Comparison predicate for sorting protocols
257688cb27a160adc305783a44f922ee4b216006ebf9Chris Lattner/// alphabetically.
257788cb27a160adc305783a44f922ee4b216006ebf9Chris Lattnerstatic bool CmpProtocolNames(const ObjCProtocolDecl *LHS,
257888cb27a160adc305783a44f922ee4b216006ebf9Chris Lattner                            const ObjCProtocolDecl *RHS) {
25792e1cd4264d363ca869bf37ef160902f211d21b8cDouglas Gregor  return LHS->getDeclName() < RHS->getDeclName();
258088cb27a160adc305783a44f922ee4b216006ebf9Chris Lattner}
258188cb27a160adc305783a44f922ee4b216006ebf9Chris Lattner
2582c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCallstatic bool areSortedAndUniqued(ObjCProtocolDecl * const *Protocols,
258354e14c4db764c0636160d26c5bbf491637c83a76John McCall                                unsigned NumProtocols) {
258454e14c4db764c0636160d26c5bbf491637c83a76John McCall  if (NumProtocols == 0) return true;
258554e14c4db764c0636160d26c5bbf491637c83a76John McCall
258654e14c4db764c0636160d26c5bbf491637c83a76John McCall  for (unsigned i = 1; i != NumProtocols; ++i)
258754e14c4db764c0636160d26c5bbf491637c83a76John McCall    if (!CmpProtocolNames(Protocols[i-1], Protocols[i]))
258854e14c4db764c0636160d26c5bbf491637c83a76John McCall      return false;
258954e14c4db764c0636160d26c5bbf491637c83a76John McCall  return true;
259054e14c4db764c0636160d26c5bbf491637c83a76John McCall}
259154e14c4db764c0636160d26c5bbf491637c83a76John McCall
259254e14c4db764c0636160d26c5bbf491637c83a76John McCallstatic void SortAndUniqueProtocols(ObjCProtocolDecl **Protocols,
259388cb27a160adc305783a44f922ee4b216006ebf9Chris Lattner                                   unsigned &NumProtocols) {
259488cb27a160adc305783a44f922ee4b216006ebf9Chris Lattner  ObjCProtocolDecl **ProtocolsEnd = Protocols+NumProtocols;
25951eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
259688cb27a160adc305783a44f922ee4b216006ebf9Chris Lattner  // Sort protocols, keyed by name.
259788cb27a160adc305783a44f922ee4b216006ebf9Chris Lattner  std::sort(Protocols, Protocols+NumProtocols, CmpProtocolNames);
259888cb27a160adc305783a44f922ee4b216006ebf9Chris Lattner
259988cb27a160adc305783a44f922ee4b216006ebf9Chris Lattner  // Remove duplicates.
260088cb27a160adc305783a44f922ee4b216006ebf9Chris Lattner  ProtocolsEnd = std::unique(Protocols, ProtocolsEnd);
260188cb27a160adc305783a44f922ee4b216006ebf9Chris Lattner  NumProtocols = ProtocolsEnd-Protocols;
260288cb27a160adc305783a44f922ee4b216006ebf9Chris Lattner}
260388cb27a160adc305783a44f922ee4b216006ebf9Chris Lattner
2604c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCallQualType ASTContext::getObjCObjectType(QualType BaseType,
2605c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall                                       ObjCProtocolDecl * const *Protocols,
26064ba2a17694148e16eaa8d3917f657ffcd3667be4Jay Foad                                       unsigned NumProtocols) const {
2607c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall  // If the base type is an interface and there aren't any protocols
2608c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall  // to add, then the interface type will do just fine.
2609c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall  if (!NumProtocols && isa<ObjCInterfaceType>(BaseType))
2610c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall    return BaseType;
2611d1b3c2dd5bc1f3103bee6137957aa7c5f8f2f0bcSteve Naroff
2612c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall  // Look in the folding set for an existing type.
2613c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall  llvm::FoldingSetNodeID ID;
2614c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall  ObjCObjectTypeImpl::Profile(ID, BaseType, Protocols, NumProtocols);
2615d1b3c2dd5bc1f3103bee6137957aa7c5f8f2f0bcSteve Naroff  void *InsertPos = 0;
2616c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall  if (ObjCObjectType *QT = ObjCObjectTypes.FindNodeOrInsertPos(ID, InsertPos))
2617c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall    return QualType(QT, 0);
2618d1b3c2dd5bc1f3103bee6137957aa7c5f8f2f0bcSteve Naroff
2619c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall  // Build the canonical type, which has the canonical base type and
2620c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall  // a sorted-and-uniqued list of protocols.
262154e14c4db764c0636160d26c5bbf491637c83a76John McCall  QualType Canonical;
2622c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall  bool ProtocolsSorted = areSortedAndUniqued(Protocols, NumProtocols);
2623c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall  if (!ProtocolsSorted || !BaseType.isCanonical()) {
2624c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall    if (!ProtocolsSorted) {
26250237941e0beb0c929934b66ad29443b484d987feBenjamin Kramer      llvm::SmallVector<ObjCProtocolDecl*, 8> Sorted(Protocols,
26260237941e0beb0c929934b66ad29443b484d987feBenjamin Kramer                                                     Protocols + NumProtocols);
262754e14c4db764c0636160d26c5bbf491637c83a76John McCall      unsigned UniqueCount = NumProtocols;
262854e14c4db764c0636160d26c5bbf491637c83a76John McCall
262954e14c4db764c0636160d26c5bbf491637c83a76John McCall      SortAndUniqueProtocols(&Sorted[0], UniqueCount);
2630c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall      Canonical = getObjCObjectType(getCanonicalType(BaseType),
2631c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall                                    &Sorted[0], UniqueCount);
263254e14c4db764c0636160d26c5bbf491637c83a76John McCall    } else {
2633c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall      Canonical = getObjCObjectType(getCanonicalType(BaseType),
2634c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall                                    Protocols, NumProtocols);
263554e14c4db764c0636160d26c5bbf491637c83a76John McCall    }
263654e14c4db764c0636160d26c5bbf491637c83a76John McCall
263754e14c4db764c0636160d26c5bbf491637c83a76John McCall    // Regenerate InsertPos.
2638c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall    ObjCObjectTypes.FindNodeOrInsertPos(ID, InsertPos);
263954e14c4db764c0636160d26c5bbf491637c83a76John McCall  }
264054e14c4db764c0636160d26c5bbf491637c83a76John McCall
2641c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall  unsigned Size = sizeof(ObjCObjectTypeImpl);
2642c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall  Size += NumProtocols * sizeof(ObjCProtocolDecl *);
2643fd6a0887a099256c35a5b23e9afd517ffe95fa0aDouglas Gregor  void *Mem = Allocate(Size, TypeAlignment);
2644c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall  ObjCObjectTypeImpl *T =
2645c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall    new (Mem) ObjCObjectTypeImpl(Canonical, BaseType, Protocols, NumProtocols);
26461eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
2647c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall  Types.push_back(T);
2648c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall  ObjCObjectTypes.InsertNode(T, InsertPos);
2649c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall  return QualType(T, 0);
2650d1b3c2dd5bc1f3103bee6137957aa7c5f8f2f0bcSteve Naroff}
265188cb27a160adc305783a44f922ee4b216006ebf9Chris Lattner
2652c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall/// getObjCObjectPointerType - Return a ObjCObjectPointerType type for
2653c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall/// the given object type.
26544ba2a17694148e16eaa8d3917f657ffcd3667be4Jay FoadQualType ASTContext::getObjCObjectPointerType(QualType ObjectT) const {
26554b6c9051c6522894978c9ba6a819a659d102db36Fariborz Jahanian  llvm::FoldingSetNodeID ID;
2656c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall  ObjCObjectPointerType::Profile(ID, ObjectT);
26571eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
26584b6c9051c6522894978c9ba6a819a659d102db36Fariborz Jahanian  void *InsertPos = 0;
2659c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall  if (ObjCObjectPointerType *QT =
2660c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall              ObjCObjectPointerTypes.FindNodeOrInsertPos(ID, InsertPos))
26614b6c9051c6522894978c9ba6a819a659d102db36Fariborz Jahanian    return QualType(QT, 0);
26621eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
2663c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall  // Find the canonical object type.
266454e14c4db764c0636160d26c5bbf491637c83a76John McCall  QualType Canonical;
2665c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall  if (!ObjectT.isCanonical()) {
2666c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall    Canonical = getObjCObjectPointerType(getCanonicalType(ObjectT));
266754e14c4db764c0636160d26c5bbf491637c83a76John McCall
2668c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall    // Regenerate InsertPos.
2669c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall    ObjCObjectPointerTypes.FindNodeOrInsertPos(ID, InsertPos);
267054e14c4db764c0636160d26c5bbf491637c83a76John McCall  }
267154e14c4db764c0636160d26c5bbf491637c83a76John McCall
2672c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall  // No match.
2673c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall  void *Mem = Allocate(sizeof(ObjCObjectPointerType), TypeAlignment);
2674c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall  ObjCObjectPointerType *QType =
2675c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall    new (Mem) ObjCObjectPointerType(Canonical, ObjectT);
267624fab41057e4b67ed69a6b4027d5ae0f2f6934dcArgyrios Kyrtzidis
267724fab41057e4b67ed69a6b4027d5ae0f2f6934dcArgyrios Kyrtzidis  Types.push_back(QType);
2678c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall  ObjCObjectPointerTypes.InsertNode(QType, InsertPos);
267924fab41057e4b67ed69a6b4027d5ae0f2f6934dcArgyrios Kyrtzidis  return QualType(QType, 0);
268024fab41057e4b67ed69a6b4027d5ae0f2f6934dcArgyrios Kyrtzidis}
268124fab41057e4b67ed69a6b4027d5ae0f2f6934dcArgyrios Kyrtzidis
2682deacbdca554298ccdf636f19c6094a8825ec6b34Douglas Gregor/// getObjCInterfaceType - Return the unique reference to the type for the
2683deacbdca554298ccdf636f19c6094a8825ec6b34Douglas Gregor/// specified ObjC interface decl. The list of protocols is optional.
26844ba2a17694148e16eaa8d3917f657ffcd3667be4Jay FoadQualType ASTContext::getObjCInterfaceType(const ObjCInterfaceDecl *Decl) const {
2685deacbdca554298ccdf636f19c6094a8825ec6b34Douglas Gregor  if (Decl->TypeForDecl)
2686deacbdca554298ccdf636f19c6094a8825ec6b34Douglas Gregor    return QualType(Decl->TypeForDecl, 0);
268774c730ad1f6818b676b0bad46d806a9176950328Sebastian Redl
2688deacbdca554298ccdf636f19c6094a8825ec6b34Douglas Gregor  // FIXME: redeclarations?
2689deacbdca554298ccdf636f19c6094a8825ec6b34Douglas Gregor  void *Mem = Allocate(sizeof(ObjCInterfaceType), TypeAlignment);
2690deacbdca554298ccdf636f19c6094a8825ec6b34Douglas Gregor  ObjCInterfaceType *T = new (Mem) ObjCInterfaceType(Decl);
2691deacbdca554298ccdf636f19c6094a8825ec6b34Douglas Gregor  Decl->TypeForDecl = T;
2692deacbdca554298ccdf636f19c6094a8825ec6b34Douglas Gregor  Types.push_back(T);
2693deacbdca554298ccdf636f19c6094a8825ec6b34Douglas Gregor  return QualType(T, 0);
2694c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall}
2695c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall
269672564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor/// getTypeOfExprType - Unlike many "get<Type>" functions, we can't unique
269772564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor/// TypeOfExprType AST's (since expression's are never shared). For example,
26989752f25748d954df99087d741ea35db37ff16beaSteve Naroff/// multiple declarations that refer to "typeof(x)" all contain different
26991eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// DeclRefExpr's. This doesn't effect the type checker, since it operates
27009752f25748d954df99087d741ea35db37ff16beaSteve Naroff/// on canonical type's (which are always unique).
27014ba2a17694148e16eaa8d3917f657ffcd3667be4Jay FoadQualType ASTContext::getTypeOfExprType(Expr *tofExpr) const {
2702dd0257c77719a13d4acd513df40b04300cbfc871Douglas Gregor  TypeOfExprType *toe;
2703b197572cf1cd70a817a1f546478cb2cb9112c48eDouglas Gregor  if (tofExpr->isTypeDependent()) {
2704b197572cf1cd70a817a1f546478cb2cb9112c48eDouglas Gregor    llvm::FoldingSetNodeID ID;
2705b197572cf1cd70a817a1f546478cb2cb9112c48eDouglas Gregor    DependentTypeOfExprType::Profile(ID, *this, tofExpr);
27061eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
2707b197572cf1cd70a817a1f546478cb2cb9112c48eDouglas Gregor    void *InsertPos = 0;
2708b197572cf1cd70a817a1f546478cb2cb9112c48eDouglas Gregor    DependentTypeOfExprType *Canon
2709b197572cf1cd70a817a1f546478cb2cb9112c48eDouglas Gregor      = DependentTypeOfExprTypes.FindNodeOrInsertPos(ID, InsertPos);
2710b197572cf1cd70a817a1f546478cb2cb9112c48eDouglas Gregor    if (Canon) {
2711b197572cf1cd70a817a1f546478cb2cb9112c48eDouglas Gregor      // We already have a "canonical" version of an identical, dependent
2712b197572cf1cd70a817a1f546478cb2cb9112c48eDouglas Gregor      // typeof(expr) type. Use that as our canonical type.
27136b304a0254a13f42390b865ff5ba668a49cc58aeJohn McCall      toe = new (*this, TypeAlignment) TypeOfExprType(tofExpr,
2714b197572cf1cd70a817a1f546478cb2cb9112c48eDouglas Gregor                                          QualType((TypeOfExprType*)Canon, 0));
2715b197572cf1cd70a817a1f546478cb2cb9112c48eDouglas Gregor    }
2716b197572cf1cd70a817a1f546478cb2cb9112c48eDouglas Gregor    else {
2717b197572cf1cd70a817a1f546478cb2cb9112c48eDouglas Gregor      // Build a new, canonical typeof(expr) type.
27186b304a0254a13f42390b865ff5ba668a49cc58aeJohn McCall      Canon
27196b304a0254a13f42390b865ff5ba668a49cc58aeJohn McCall        = new (*this, TypeAlignment) DependentTypeOfExprType(*this, tofExpr);
2720b197572cf1cd70a817a1f546478cb2cb9112c48eDouglas Gregor      DependentTypeOfExprTypes.InsertNode(Canon, InsertPos);
2721b197572cf1cd70a817a1f546478cb2cb9112c48eDouglas Gregor      toe = Canon;
2722b197572cf1cd70a817a1f546478cb2cb9112c48eDouglas Gregor    }
2723b197572cf1cd70a817a1f546478cb2cb9112c48eDouglas Gregor  } else {
2724dd0257c77719a13d4acd513df40b04300cbfc871Douglas Gregor    QualType Canonical = getCanonicalType(tofExpr->getType());
27256b304a0254a13f42390b865ff5ba668a49cc58aeJohn McCall    toe = new (*this, TypeAlignment) TypeOfExprType(tofExpr, Canonical);
2726dd0257c77719a13d4acd513df40b04300cbfc871Douglas Gregor  }
27279752f25748d954df99087d741ea35db37ff16beaSteve Naroff  Types.push_back(toe);
27289752f25748d954df99087d741ea35db37ff16beaSteve Naroff  return QualType(toe, 0);
2729d1861fd633d5096a00777c918eb8575ea7162fe7Steve Naroff}
2730d1861fd633d5096a00777c918eb8575ea7162fe7Steve Naroff
27319752f25748d954df99087d741ea35db37ff16beaSteve Naroff/// getTypeOfType -  Unlike many "get<Type>" functions, we don't unique
27329752f25748d954df99087d741ea35db37ff16beaSteve Naroff/// TypeOfType AST's. The only motivation to unique these nodes would be
27339752f25748d954df99087d741ea35db37ff16beaSteve Naroff/// memory savings. Since typeof(t) is fairly uncommon, space shouldn't be
27341eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// an issue. This doesn't effect the type checker, since it operates
27359752f25748d954df99087d741ea35db37ff16beaSteve Naroff/// on canonical type's (which are always unique).
27364ba2a17694148e16eaa8d3917f657ffcd3667be4Jay FoadQualType ASTContext::getTypeOfType(QualType tofType) const {
2737f52ab250ff92bc51a9ac9a8e19bd43b63a5f844fChris Lattner  QualType Canonical = getCanonicalType(tofType);
27386b304a0254a13f42390b865ff5ba668a49cc58aeJohn McCall  TypeOfType *tot = new (*this, TypeAlignment) TypeOfType(tofType, Canonical);
27399752f25748d954df99087d741ea35db37ff16beaSteve Naroff  Types.push_back(tot);
27409752f25748d954df99087d741ea35db37ff16beaSteve Naroff  return QualType(tot, 0);
2741d1861fd633d5096a00777c918eb8575ea7162fe7Steve Naroff}
2742d1861fd633d5096a00777c918eb8575ea7162fe7Steve Naroff
274360a9a2a404a4cf259d39133383e922aa00ca9043Anders Carlsson/// getDecltypeForExpr - Given an expr, will return the decltype for that
274460a9a2a404a4cf259d39133383e922aa00ca9043Anders Carlsson/// expression, according to the rules in C++0x [dcl.type.simple]p4
27454ba2a17694148e16eaa8d3917f657ffcd3667be4Jay Foadstatic QualType getDecltypeForExpr(const Expr *e, const ASTContext &Context) {
2746a07c33e64e1169e4261f7748c7f9191091a3ad2eAnders Carlsson  if (e->isTypeDependent())
2747a07c33e64e1169e4261f7748c7f9191091a3ad2eAnders Carlsson    return Context.DependentTy;
27481eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
274960a9a2a404a4cf259d39133383e922aa00ca9043Anders Carlsson  // If e is an id expression or a class member access, decltype(e) is defined
275060a9a2a404a4cf259d39133383e922aa00ca9043Anders Carlsson  // as the type of the entity named by e.
275160a9a2a404a4cf259d39133383e922aa00ca9043Anders Carlsson  if (const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(e)) {
275260a9a2a404a4cf259d39133383e922aa00ca9043Anders Carlsson    if (const ValueDecl *VD = dyn_cast<ValueDecl>(DRE->getDecl()))
275360a9a2a404a4cf259d39133383e922aa00ca9043Anders Carlsson      return VD->getType();
275460a9a2a404a4cf259d39133383e922aa00ca9043Anders Carlsson  }
275560a9a2a404a4cf259d39133383e922aa00ca9043Anders Carlsson  if (const MemberExpr *ME = dyn_cast<MemberExpr>(e)) {
275660a9a2a404a4cf259d39133383e922aa00ca9043Anders Carlsson    if (const FieldDecl *FD = dyn_cast<FieldDecl>(ME->getMemberDecl()))
275760a9a2a404a4cf259d39133383e922aa00ca9043Anders Carlsson      return FD->getType();
275860a9a2a404a4cf259d39133383e922aa00ca9043Anders Carlsson  }
275960a9a2a404a4cf259d39133383e922aa00ca9043Anders Carlsson  // If e is a function call or an invocation of an overloaded operator,
276060a9a2a404a4cf259d39133383e922aa00ca9043Anders Carlsson  // (parentheses around e are ignored), decltype(e) is defined as the
276160a9a2a404a4cf259d39133383e922aa00ca9043Anders Carlsson  // return type of that function.
276260a9a2a404a4cf259d39133383e922aa00ca9043Anders Carlsson  if (const CallExpr *CE = dyn_cast<CallExpr>(e->IgnoreParens()))
276360a9a2a404a4cf259d39133383e922aa00ca9043Anders Carlsson    return CE->getCallReturnType();
27641eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
276560a9a2a404a4cf259d39133383e922aa00ca9043Anders Carlsson  QualType T = e->getType();
27661eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
27671eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  // Otherwise, where T is the type of e, if e is an lvalue, decltype(e) is
276860a9a2a404a4cf259d39133383e922aa00ca9043Anders Carlsson  // defined as T&, otherwise decltype(e) is defined as T.
27697eb0a9eb0cde8444b97f9c5b713d9be7a6f1e607John McCall  if (e->isLValue())
277060a9a2a404a4cf259d39133383e922aa00ca9043Anders Carlsson    T = Context.getLValueReferenceType(T);
27711eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
277260a9a2a404a4cf259d39133383e922aa00ca9043Anders Carlsson  return T;
277360a9a2a404a4cf259d39133383e922aa00ca9043Anders Carlsson}
277460a9a2a404a4cf259d39133383e922aa00ca9043Anders Carlsson
2775395b475a4474f1c7574d927ad142ca0c7997cbcaAnders Carlsson/// getDecltypeType -  Unlike many "get<Type>" functions, we don't unique
2776395b475a4474f1c7574d927ad142ca0c7997cbcaAnders Carlsson/// DecltypeType AST's. The only motivation to unique these nodes would be
2777395b475a4474f1c7574d927ad142ca0c7997cbcaAnders Carlsson/// memory savings. Since decltype(t) is fairly uncommon, space shouldn't be
27781eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// an issue. This doesn't effect the type checker, since it operates
2779395b475a4474f1c7574d927ad142ca0c7997cbcaAnders Carlsson/// on canonical type's (which are always unique).
27804ba2a17694148e16eaa8d3917f657ffcd3667be4Jay FoadQualType ASTContext::getDecltypeType(Expr *e) const {
2781dd0257c77719a13d4acd513df40b04300cbfc871Douglas Gregor  DecltypeType *dt;
27829d702ae1cd5cfa19d884cbef77e1df99395138bbDouglas Gregor  if (e->isTypeDependent()) {
27839d702ae1cd5cfa19d884cbef77e1df99395138bbDouglas Gregor    llvm::FoldingSetNodeID ID;
27849d702ae1cd5cfa19d884cbef77e1df99395138bbDouglas Gregor    DependentDecltypeType::Profile(ID, *this, e);
27851eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
27869d702ae1cd5cfa19d884cbef77e1df99395138bbDouglas Gregor    void *InsertPos = 0;
27879d702ae1cd5cfa19d884cbef77e1df99395138bbDouglas Gregor    DependentDecltypeType *Canon
27889d702ae1cd5cfa19d884cbef77e1df99395138bbDouglas Gregor      = DependentDecltypeTypes.FindNodeOrInsertPos(ID, InsertPos);
27899d702ae1cd5cfa19d884cbef77e1df99395138bbDouglas Gregor    if (Canon) {
27909d702ae1cd5cfa19d884cbef77e1df99395138bbDouglas Gregor      // We already have a "canonical" version of an equivalent, dependent
27919d702ae1cd5cfa19d884cbef77e1df99395138bbDouglas Gregor      // decltype type. Use that as our canonical type.
27926b304a0254a13f42390b865ff5ba668a49cc58aeJohn McCall      dt = new (*this, TypeAlignment) DecltypeType(e, DependentTy,
27939d702ae1cd5cfa19d884cbef77e1df99395138bbDouglas Gregor                                       QualType((DecltypeType*)Canon, 0));
27949d702ae1cd5cfa19d884cbef77e1df99395138bbDouglas Gregor    }
27959d702ae1cd5cfa19d884cbef77e1df99395138bbDouglas Gregor    else {
27969d702ae1cd5cfa19d884cbef77e1df99395138bbDouglas Gregor      // Build a new, canonical typeof(expr) type.
27976b304a0254a13f42390b865ff5ba668a49cc58aeJohn McCall      Canon = new (*this, TypeAlignment) DependentDecltypeType(*this, e);
27989d702ae1cd5cfa19d884cbef77e1df99395138bbDouglas Gregor      DependentDecltypeTypes.InsertNode(Canon, InsertPos);
27999d702ae1cd5cfa19d884cbef77e1df99395138bbDouglas Gregor      dt = Canon;
28009d702ae1cd5cfa19d884cbef77e1df99395138bbDouglas Gregor    }
28019d702ae1cd5cfa19d884cbef77e1df99395138bbDouglas Gregor  } else {
2802dd0257c77719a13d4acd513df40b04300cbfc871Douglas Gregor    QualType T = getDecltypeForExpr(e, *this);
28036b304a0254a13f42390b865ff5ba668a49cc58aeJohn McCall    dt = new (*this, TypeAlignment) DecltypeType(e, T, getCanonicalType(T));
2804dd0257c77719a13d4acd513df40b04300cbfc871Douglas Gregor  }
2805395b475a4474f1c7574d927ad142ca0c7997cbcaAnders Carlsson  Types.push_back(dt);
2806395b475a4474f1c7574d927ad142ca0c7997cbcaAnders Carlsson  return QualType(dt, 0);
2807395b475a4474f1c7574d927ad142ca0c7997cbcaAnders Carlsson}
2808395b475a4474f1c7574d927ad142ca0c7997cbcaAnders Carlsson
2809ca63c200346c0ca9e00194ec6e34a5a7b0ed9321Sean Hunt/// getUnaryTransformationType - We don't unique these, since the memory
2810ca63c200346c0ca9e00194ec6e34a5a7b0ed9321Sean Hunt/// savings are minimal and these are rare.
2811ca63c200346c0ca9e00194ec6e34a5a7b0ed9321Sean HuntQualType ASTContext::getUnaryTransformType(QualType BaseType,
2812ca63c200346c0ca9e00194ec6e34a5a7b0ed9321Sean Hunt                                           QualType UnderlyingType,
2813ca63c200346c0ca9e00194ec6e34a5a7b0ed9321Sean Hunt                                           UnaryTransformType::UTTKind Kind)
2814ca63c200346c0ca9e00194ec6e34a5a7b0ed9321Sean Hunt    const {
2815ca63c200346c0ca9e00194ec6e34a5a7b0ed9321Sean Hunt  UnaryTransformType *Ty =
2816ca63c200346c0ca9e00194ec6e34a5a7b0ed9321Sean Hunt    new UnaryTransformType (BaseType, UnderlyingType, Kind,
2817ca63c200346c0ca9e00194ec6e34a5a7b0ed9321Sean Hunt                            UnderlyingType->isDependentType() ?
2818ca63c200346c0ca9e00194ec6e34a5a7b0ed9321Sean Hunt                              QualType() : UnderlyingType);
2819ca63c200346c0ca9e00194ec6e34a5a7b0ed9321Sean Hunt  Types.push_back(Ty);
2820ca63c200346c0ca9e00194ec6e34a5a7b0ed9321Sean Hunt  return QualType(Ty, 0);
2821ca63c200346c0ca9e00194ec6e34a5a7b0ed9321Sean Hunt}
2822ca63c200346c0ca9e00194ec6e34a5a7b0ed9321Sean Hunt
2823483b9f3bc05c5409e2c6643f1c9d91e21c8ff9d2Richard Smith/// getAutoType - We only unique auto types after they've been deduced.
282434b41d939a1328f484511c6002ba2456db879a29Richard SmithQualType ASTContext::getAutoType(QualType DeducedType) const {
2825483b9f3bc05c5409e2c6643f1c9d91e21c8ff9d2Richard Smith  void *InsertPos = 0;
2826483b9f3bc05c5409e2c6643f1c9d91e21c8ff9d2Richard Smith  if (!DeducedType.isNull()) {
2827483b9f3bc05c5409e2c6643f1c9d91e21c8ff9d2Richard Smith    // Look in the folding set for an existing type.
2828483b9f3bc05c5409e2c6643f1c9d91e21c8ff9d2Richard Smith    llvm::FoldingSetNodeID ID;
2829483b9f3bc05c5409e2c6643f1c9d91e21c8ff9d2Richard Smith    AutoType::Profile(ID, DeducedType);
2830483b9f3bc05c5409e2c6643f1c9d91e21c8ff9d2Richard Smith    if (AutoType *AT = AutoTypes.FindNodeOrInsertPos(ID, InsertPos))
2831483b9f3bc05c5409e2c6643f1c9d91e21c8ff9d2Richard Smith      return QualType(AT, 0);
2832483b9f3bc05c5409e2c6643f1c9d91e21c8ff9d2Richard Smith  }
2833483b9f3bc05c5409e2c6643f1c9d91e21c8ff9d2Richard Smith
2834483b9f3bc05c5409e2c6643f1c9d91e21c8ff9d2Richard Smith  AutoType *AT = new (*this, TypeAlignment) AutoType(DeducedType);
2835483b9f3bc05c5409e2c6643f1c9d91e21c8ff9d2Richard Smith  Types.push_back(AT);
2836483b9f3bc05c5409e2c6643f1c9d91e21c8ff9d2Richard Smith  if (InsertPos)
2837483b9f3bc05c5409e2c6643f1c9d91e21c8ff9d2Richard Smith    AutoTypes.InsertNode(AT, InsertPos);
2838483b9f3bc05c5409e2c6643f1c9d91e21c8ff9d2Richard Smith  return QualType(AT, 0);
283934b41d939a1328f484511c6002ba2456db879a29Richard Smith}
284034b41d939a1328f484511c6002ba2456db879a29Richard Smith
2841ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith/// getAutoDeductType - Get type pattern for deducing against 'auto'.
2842ad762fcdc16b9e4705b12b09d92b8c026212b906Richard SmithQualType ASTContext::getAutoDeductType() const {
2843ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith  if (AutoDeductTy.isNull())
2844ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith    AutoDeductTy = getAutoType(QualType());
2845ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith  assert(!AutoDeductTy.isNull() && "can't build 'auto' pattern");
2846ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith  return AutoDeductTy;
2847ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith}
2848ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith
2849ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith/// getAutoRRefDeductType - Get type pattern for deducing against 'auto &&'.
2850ad762fcdc16b9e4705b12b09d92b8c026212b906Richard SmithQualType ASTContext::getAutoRRefDeductType() const {
2851ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith  if (AutoRRefDeductTy.isNull())
2852ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith    AutoRRefDeductTy = getRValueReferenceType(getAutoDeductType());
2853ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith  assert(!AutoRRefDeductTy.isNull() && "can't build 'auto &&' pattern");
2854ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith  return AutoRRefDeductTy;
2855ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith}
2856ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith
28575f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// getTagDeclType - Return the unique reference to the type for the
28585f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// specified TagDecl (struct/union/class/enum) decl.
28594ba2a17694148e16eaa8d3917f657ffcd3667be4Jay FoadQualType ASTContext::getTagDeclType(const TagDecl *Decl) const {
2860d778f88d32b96a74c9edb7342c81357606a7cdc0Ted Kremenek  assert (Decl);
2861e607ed068334bacb8d7b093996b4671c6ca79e25Mike Stump  // FIXME: What is the design on getTagDeclType when it requires casting
2862e607ed068334bacb8d7b093996b4671c6ca79e25Mike Stump  // away const?  mutable?
2863e607ed068334bacb8d7b093996b4671c6ca79e25Mike Stump  return getTypeDeclType(const_cast<TagDecl*>(Decl));
28645f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
28655f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
28661eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// getSizeType - Return the unique type for "size_t" (C99 7.17), the result
28671eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// of the sizeof operator (C99 6.5.3.4p4). The value is target dependent and
28681eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// needs to agree with the definition in <stddef.h>.
2869a3ccda58913cc1a4b8564e349448b12acc462da7Anders CarlssonCanQualType ASTContext::getSizeType() const {
2870b4e66d5259f90e9aae4d40fc5de801e046c7df94Douglas Gregor  return getFromTargetType(Target.getSizeType());
28715f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
28725f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
287364c438a4be2a871fa43c78264663ba1e9788b94dArgyrios Kyrtzidis/// getSignedWCharType - Return the type of "signed wchar_t".
287464c438a4be2a871fa43c78264663ba1e9788b94dArgyrios Kyrtzidis/// Used when in C++, as a GCC extension.
287564c438a4be2a871fa43c78264663ba1e9788b94dArgyrios KyrtzidisQualType ASTContext::getSignedWCharType() const {
287664c438a4be2a871fa43c78264663ba1e9788b94dArgyrios Kyrtzidis  // FIXME: derive from "Target" ?
287764c438a4be2a871fa43c78264663ba1e9788b94dArgyrios Kyrtzidis  return WCharTy;
287864c438a4be2a871fa43c78264663ba1e9788b94dArgyrios Kyrtzidis}
287964c438a4be2a871fa43c78264663ba1e9788b94dArgyrios Kyrtzidis
288064c438a4be2a871fa43c78264663ba1e9788b94dArgyrios Kyrtzidis/// getUnsignedWCharType - Return the type of "unsigned wchar_t".
288164c438a4be2a871fa43c78264663ba1e9788b94dArgyrios Kyrtzidis/// Used when in C++, as a GCC extension.
288264c438a4be2a871fa43c78264663ba1e9788b94dArgyrios KyrtzidisQualType ASTContext::getUnsignedWCharType() const {
288364c438a4be2a871fa43c78264663ba1e9788b94dArgyrios Kyrtzidis  // FIXME: derive from "Target" ?
288464c438a4be2a871fa43c78264663ba1e9788b94dArgyrios Kyrtzidis  return UnsignedIntTy;
288564c438a4be2a871fa43c78264663ba1e9788b94dArgyrios Kyrtzidis}
288664c438a4be2a871fa43c78264663ba1e9788b94dArgyrios Kyrtzidis
28878b9023ba35a86838789e2c9034a6128728c547aaChris Lattner/// getPointerDiffType - Return the unique type for "ptrdiff_t" (ref?)
28888b9023ba35a86838789e2c9034a6128728c547aaChris Lattner/// defined in <stddef.h>. Pointer - pointer requires this (C99 6.5.6p9).
28898b9023ba35a86838789e2c9034a6128728c547aaChris LattnerQualType ASTContext::getPointerDiffType() const {
2890b4e66d5259f90e9aae4d40fc5de801e046c7df94Douglas Gregor  return getFromTargetType(Target.getPtrDiffType(0));
28918b9023ba35a86838789e2c9034a6128728c547aaChris Lattner}
28928b9023ba35a86838789e2c9034a6128728c547aaChris Lattner
2893e6327747b72bb687c948270f702ff53c30f411a6Chris Lattner//===----------------------------------------------------------------------===//
2894e6327747b72bb687c948270f702ff53c30f411a6Chris Lattner//                              Type Operators
2895e6327747b72bb687c948270f702ff53c30f411a6Chris Lattner//===----------------------------------------------------------------------===//
2896e6327747b72bb687c948270f702ff53c30f411a6Chris Lattner
28974ba2a17694148e16eaa8d3917f657ffcd3667be4Jay FoadCanQualType ASTContext::getCanonicalParamType(QualType T) const {
289854e14c4db764c0636160d26c5bbf491637c83a76John McCall  // Push qualifiers into arrays, and then discard any remaining
289954e14c4db764c0636160d26c5bbf491637c83a76John McCall  // qualifiers.
290054e14c4db764c0636160d26c5bbf491637c83a76John McCall  T = getCanonicalType(T);
2901745da3a5bb4ea35f93f50301e7fbbb7d78d3b6bbFariborz Jahanian  T = getVariableArrayDecayedType(T);
290254e14c4db764c0636160d26c5bbf491637c83a76John McCall  const Type *Ty = T.getTypePtr();
290354e14c4db764c0636160d26c5bbf491637c83a76John McCall  QualType Result;
290454e14c4db764c0636160d26c5bbf491637c83a76John McCall  if (isa<ArrayType>(Ty)) {
290554e14c4db764c0636160d26c5bbf491637c83a76John McCall    Result = getArrayDecayedType(QualType(Ty,0));
290654e14c4db764c0636160d26c5bbf491637c83a76John McCall  } else if (isa<FunctionType>(Ty)) {
290754e14c4db764c0636160d26c5bbf491637c83a76John McCall    Result = getPointerType(QualType(Ty, 0));
290854e14c4db764c0636160d26c5bbf491637c83a76John McCall  } else {
290954e14c4db764c0636160d26c5bbf491637c83a76John McCall    Result = QualType(Ty, 0);
291054e14c4db764c0636160d26c5bbf491637c83a76John McCall  }
291154e14c4db764c0636160d26c5bbf491637c83a76John McCall
291254e14c4db764c0636160d26c5bbf491637c83a76John McCall  return CanQualType::CreateUnsafe(Result);
291354e14c4db764c0636160d26c5bbf491637c83a76John McCall}
291454e14c4db764c0636160d26c5bbf491637c83a76John McCall
2915c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner
291662c28c831bbf207cc36e683e7c321fc33bf8928cJohn McCallQualType ASTContext::getUnqualifiedArrayType(QualType type,
291762c28c831bbf207cc36e683e7c321fc33bf8928cJohn McCall                                             Qualifiers &quals) {
291862c28c831bbf207cc36e683e7c321fc33bf8928cJohn McCall  SplitQualType splitType = type.getSplitUnqualifiedType();
291962c28c831bbf207cc36e683e7c321fc33bf8928cJohn McCall
292062c28c831bbf207cc36e683e7c321fc33bf8928cJohn McCall  // FIXME: getSplitUnqualifiedType() actually walks all the way to
292162c28c831bbf207cc36e683e7c321fc33bf8928cJohn McCall  // the unqualified desugared type and then drops it on the floor.
292262c28c831bbf207cc36e683e7c321fc33bf8928cJohn McCall  // We then have to strip that sugar back off with
292362c28c831bbf207cc36e683e7c321fc33bf8928cJohn McCall  // getUnqualifiedDesugaredType(), which is silly.
292462c28c831bbf207cc36e683e7c321fc33bf8928cJohn McCall  const ArrayType *AT =
292562c28c831bbf207cc36e683e7c321fc33bf8928cJohn McCall    dyn_cast<ArrayType>(splitType.first->getUnqualifiedDesugaredType());
292662c28c831bbf207cc36e683e7c321fc33bf8928cJohn McCall
292762c28c831bbf207cc36e683e7c321fc33bf8928cJohn McCall  // If we don't have an array, just use the results in splitType.
29289dadd94e1c17fa030d1f88d8f2113ff59ccc6714Douglas Gregor  if (!AT) {
292962c28c831bbf207cc36e683e7c321fc33bf8928cJohn McCall    quals = splitType.second;
293062c28c831bbf207cc36e683e7c321fc33bf8928cJohn McCall    return QualType(splitType.first, 0);
293128e318cc6008c2bc008f0caee70dc736a03d6289Chandler Carruth  }
293228e318cc6008c2bc008f0caee70dc736a03d6289Chandler Carruth
293362c28c831bbf207cc36e683e7c321fc33bf8928cJohn McCall  // Otherwise, recurse on the array's element type.
293462c28c831bbf207cc36e683e7c321fc33bf8928cJohn McCall  QualType elementType = AT->getElementType();
293562c28c831bbf207cc36e683e7c321fc33bf8928cJohn McCall  QualType unqualElementType = getUnqualifiedArrayType(elementType, quals);
293662c28c831bbf207cc36e683e7c321fc33bf8928cJohn McCall
293762c28c831bbf207cc36e683e7c321fc33bf8928cJohn McCall  // If that didn't change the element type, AT has no qualifiers, so we
293862c28c831bbf207cc36e683e7c321fc33bf8928cJohn McCall  // can just use the results in splitType.
293962c28c831bbf207cc36e683e7c321fc33bf8928cJohn McCall  if (elementType == unqualElementType) {
294062c28c831bbf207cc36e683e7c321fc33bf8928cJohn McCall    assert(quals.empty()); // from the recursive call
294162c28c831bbf207cc36e683e7c321fc33bf8928cJohn McCall    quals = splitType.second;
294262c28c831bbf207cc36e683e7c321fc33bf8928cJohn McCall    return QualType(splitType.first, 0);
294362c28c831bbf207cc36e683e7c321fc33bf8928cJohn McCall  }
294462c28c831bbf207cc36e683e7c321fc33bf8928cJohn McCall
294562c28c831bbf207cc36e683e7c321fc33bf8928cJohn McCall  // Otherwise, add in the qualifiers from the outermost type, then
294662c28c831bbf207cc36e683e7c321fc33bf8928cJohn McCall  // build the type back up.
294762c28c831bbf207cc36e683e7c321fc33bf8928cJohn McCall  quals.addConsistentQualifiers(splitType.second);
294828e318cc6008c2bc008f0caee70dc736a03d6289Chandler Carruth
29499dadd94e1c17fa030d1f88d8f2113ff59ccc6714Douglas Gregor  if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(AT)) {
295062c28c831bbf207cc36e683e7c321fc33bf8928cJohn McCall    return getConstantArrayType(unqualElementType, CAT->getSize(),
295128e318cc6008c2bc008f0caee70dc736a03d6289Chandler Carruth                                CAT->getSizeModifier(), 0);
295228e318cc6008c2bc008f0caee70dc736a03d6289Chandler Carruth  }
295328e318cc6008c2bc008f0caee70dc736a03d6289Chandler Carruth
29549dadd94e1c17fa030d1f88d8f2113ff59ccc6714Douglas Gregor  if (const IncompleteArrayType *IAT = dyn_cast<IncompleteArrayType>(AT)) {
295562c28c831bbf207cc36e683e7c321fc33bf8928cJohn McCall    return getIncompleteArrayType(unqualElementType, IAT->getSizeModifier(), 0);
295628e318cc6008c2bc008f0caee70dc736a03d6289Chandler Carruth  }
295728e318cc6008c2bc008f0caee70dc736a03d6289Chandler Carruth
29589dadd94e1c17fa030d1f88d8f2113ff59ccc6714Douglas Gregor  if (const VariableArrayType *VAT = dyn_cast<VariableArrayType>(AT)) {
295962c28c831bbf207cc36e683e7c321fc33bf8928cJohn McCall    return getVariableArrayType(unqualElementType,
29603fa5cae9b3812cab9fab6c042c3329bb70a3d046John McCall                                VAT->getSizeExpr(),
29619dadd94e1c17fa030d1f88d8f2113ff59ccc6714Douglas Gregor                                VAT->getSizeModifier(),
29629dadd94e1c17fa030d1f88d8f2113ff59ccc6714Douglas Gregor                                VAT->getIndexTypeCVRQualifiers(),
29639dadd94e1c17fa030d1f88d8f2113ff59ccc6714Douglas Gregor                                VAT->getBracketsRange());
29649dadd94e1c17fa030d1f88d8f2113ff59ccc6714Douglas Gregor  }
29659dadd94e1c17fa030d1f88d8f2113ff59ccc6714Douglas Gregor
29669dadd94e1c17fa030d1f88d8f2113ff59ccc6714Douglas Gregor  const DependentSizedArrayType *DSAT = cast<DependentSizedArrayType>(AT);
296762c28c831bbf207cc36e683e7c321fc33bf8928cJohn McCall  return getDependentSizedArrayType(unqualElementType, DSAT->getSizeExpr(),
296828e318cc6008c2bc008f0caee70dc736a03d6289Chandler Carruth                                    DSAT->getSizeModifier(), 0,
296928e318cc6008c2bc008f0caee70dc736a03d6289Chandler Carruth                                    SourceRange());
297028e318cc6008c2bc008f0caee70dc736a03d6289Chandler Carruth}
297128e318cc6008c2bc008f0caee70dc736a03d6289Chandler Carruth
29725a57efd7bf88a4a13018e0471ded8063a4abe8afDouglas Gregor/// UnwrapSimilarPointerTypes - If T1 and T2 are pointer types  that
29735a57efd7bf88a4a13018e0471ded8063a4abe8afDouglas Gregor/// may be similar (C++ 4.4), replaces T1 and T2 with the type that
29745a57efd7bf88a4a13018e0471ded8063a4abe8afDouglas Gregor/// they point to and return true. If T1 and T2 aren't pointer types
29755a57efd7bf88a4a13018e0471ded8063a4abe8afDouglas Gregor/// or pointer-to-member types, or if they are not similar at this
29765a57efd7bf88a4a13018e0471ded8063a4abe8afDouglas Gregor/// level, returns false and leaves T1 and T2 unchanged. Top-level
29775a57efd7bf88a4a13018e0471ded8063a4abe8afDouglas Gregor/// qualifiers on T1 and T2 are ignored. This function will typically
29785a57efd7bf88a4a13018e0471ded8063a4abe8afDouglas Gregor/// be called in a loop that successively "unwraps" pointer and
29795a57efd7bf88a4a13018e0471ded8063a4abe8afDouglas Gregor/// pointer-to-member types to compare them at each level.
29805a57efd7bf88a4a13018e0471ded8063a4abe8afDouglas Gregorbool ASTContext::UnwrapSimilarPointerTypes(QualType &T1, QualType &T2) {
29815a57efd7bf88a4a13018e0471ded8063a4abe8afDouglas Gregor  const PointerType *T1PtrType = T1->getAs<PointerType>(),
29825a57efd7bf88a4a13018e0471ded8063a4abe8afDouglas Gregor                    *T2PtrType = T2->getAs<PointerType>();
29835a57efd7bf88a4a13018e0471ded8063a4abe8afDouglas Gregor  if (T1PtrType && T2PtrType) {
29845a57efd7bf88a4a13018e0471ded8063a4abe8afDouglas Gregor    T1 = T1PtrType->getPointeeType();
29855a57efd7bf88a4a13018e0471ded8063a4abe8afDouglas Gregor    T2 = T2PtrType->getPointeeType();
29865a57efd7bf88a4a13018e0471ded8063a4abe8afDouglas Gregor    return true;
29875a57efd7bf88a4a13018e0471ded8063a4abe8afDouglas Gregor  }
29885a57efd7bf88a4a13018e0471ded8063a4abe8afDouglas Gregor
29895a57efd7bf88a4a13018e0471ded8063a4abe8afDouglas Gregor  const MemberPointerType *T1MPType = T1->getAs<MemberPointerType>(),
29905a57efd7bf88a4a13018e0471ded8063a4abe8afDouglas Gregor                          *T2MPType = T2->getAs<MemberPointerType>();
29915a57efd7bf88a4a13018e0471ded8063a4abe8afDouglas Gregor  if (T1MPType && T2MPType &&
29925a57efd7bf88a4a13018e0471ded8063a4abe8afDouglas Gregor      hasSameUnqualifiedType(QualType(T1MPType->getClass(), 0),
29935a57efd7bf88a4a13018e0471ded8063a4abe8afDouglas Gregor                             QualType(T2MPType->getClass(), 0))) {
29945a57efd7bf88a4a13018e0471ded8063a4abe8afDouglas Gregor    T1 = T1MPType->getPointeeType();
29955a57efd7bf88a4a13018e0471ded8063a4abe8afDouglas Gregor    T2 = T2MPType->getPointeeType();
29965a57efd7bf88a4a13018e0471ded8063a4abe8afDouglas Gregor    return true;
29975a57efd7bf88a4a13018e0471ded8063a4abe8afDouglas Gregor  }
29985a57efd7bf88a4a13018e0471ded8063a4abe8afDouglas Gregor
29995a57efd7bf88a4a13018e0471ded8063a4abe8afDouglas Gregor  if (getLangOptions().ObjC1) {
30005a57efd7bf88a4a13018e0471ded8063a4abe8afDouglas Gregor    const ObjCObjectPointerType *T1OPType = T1->getAs<ObjCObjectPointerType>(),
30015a57efd7bf88a4a13018e0471ded8063a4abe8afDouglas Gregor                                *T2OPType = T2->getAs<ObjCObjectPointerType>();
30025a57efd7bf88a4a13018e0471ded8063a4abe8afDouglas Gregor    if (T1OPType && T2OPType) {
30035a57efd7bf88a4a13018e0471ded8063a4abe8afDouglas Gregor      T1 = T1OPType->getPointeeType();
30045a57efd7bf88a4a13018e0471ded8063a4abe8afDouglas Gregor      T2 = T2OPType->getPointeeType();
30055a57efd7bf88a4a13018e0471ded8063a4abe8afDouglas Gregor      return true;
30065a57efd7bf88a4a13018e0471ded8063a4abe8afDouglas Gregor    }
30075a57efd7bf88a4a13018e0471ded8063a4abe8afDouglas Gregor  }
30085a57efd7bf88a4a13018e0471ded8063a4abe8afDouglas Gregor
30095a57efd7bf88a4a13018e0471ded8063a4abe8afDouglas Gregor  // FIXME: Block pointers, too?
30105a57efd7bf88a4a13018e0471ded8063a4abe8afDouglas Gregor
30115a57efd7bf88a4a13018e0471ded8063a4abe8afDouglas Gregor  return false;
30125a57efd7bf88a4a13018e0471ded8063a4abe8afDouglas Gregor}
30135a57efd7bf88a4a13018e0471ded8063a4abe8afDouglas Gregor
30144ba2a17694148e16eaa8d3917f657ffcd3667be4Jay FoadDeclarationNameInfo
30154ba2a17694148e16eaa8d3917f657ffcd3667be4Jay FoadASTContext::getNameForTemplate(TemplateName Name,
30164ba2a17694148e16eaa8d3917f657ffcd3667be4Jay Foad                               SourceLocation NameLoc) const {
301780ad16f4b2b350ddbaae21a52975e63df5aafc2cJohn McCall  if (TemplateDecl *TD = Name.getAsTemplateDecl())
30182577743c5650c646fb705df01403707e94f2df04Abramo Bagnara    // DNInfo work in progress: CHECKME: what about DNLoc?
30192577743c5650c646fb705df01403707e94f2df04Abramo Bagnara    return DeclarationNameInfo(TD->getDeclName(), NameLoc);
30202577743c5650c646fb705df01403707e94f2df04Abramo Bagnara
302180ad16f4b2b350ddbaae21a52975e63df5aafc2cJohn McCall  if (DependentTemplateName *DTN = Name.getAsDependentTemplateName()) {
30222577743c5650c646fb705df01403707e94f2df04Abramo Bagnara    DeclarationName DName;
302380ad16f4b2b350ddbaae21a52975e63df5aafc2cJohn McCall    if (DTN->isIdentifier()) {
30242577743c5650c646fb705df01403707e94f2df04Abramo Bagnara      DName = DeclarationNames.getIdentifier(DTN->getIdentifier());
30252577743c5650c646fb705df01403707e94f2df04Abramo Bagnara      return DeclarationNameInfo(DName, NameLoc);
302680ad16f4b2b350ddbaae21a52975e63df5aafc2cJohn McCall    } else {
30272577743c5650c646fb705df01403707e94f2df04Abramo Bagnara      DName = DeclarationNames.getCXXOperatorName(DTN->getOperator());
30282577743c5650c646fb705df01403707e94f2df04Abramo Bagnara      // DNInfo work in progress: FIXME: source locations?
30292577743c5650c646fb705df01403707e94f2df04Abramo Bagnara      DeclarationNameLoc DNLoc;
30302577743c5650c646fb705df01403707e94f2df04Abramo Bagnara      DNLoc.CXXOperatorName.BeginOpNameLoc = SourceLocation().getRawEncoding();
30312577743c5650c646fb705df01403707e94f2df04Abramo Bagnara      DNLoc.CXXOperatorName.EndOpNameLoc = SourceLocation().getRawEncoding();
30322577743c5650c646fb705df01403707e94f2df04Abramo Bagnara      return DeclarationNameInfo(DName, NameLoc, DNLoc);
303380ad16f4b2b350ddbaae21a52975e63df5aafc2cJohn McCall    }
303480ad16f4b2b350ddbaae21a52975e63df5aafc2cJohn McCall  }
303580ad16f4b2b350ddbaae21a52975e63df5aafc2cJohn McCall
30360bd6feb9e9d40fc889fd47e899985125a43dfed8John McCall  OverloadedTemplateStorage *Storage = Name.getAsOverloadedTemplate();
30370bd6feb9e9d40fc889fd47e899985125a43dfed8John McCall  assert(Storage);
30382577743c5650c646fb705df01403707e94f2df04Abramo Bagnara  // DNInfo work in progress: CHECKME: what about DNLoc?
30392577743c5650c646fb705df01403707e94f2df04Abramo Bagnara  return DeclarationNameInfo((*Storage->begin())->getDeclName(), NameLoc);
304080ad16f4b2b350ddbaae21a52975e63df5aafc2cJohn McCall}
304180ad16f4b2b350ddbaae21a52975e63df5aafc2cJohn McCall
30424ba2a17694148e16eaa8d3917f657ffcd3667be4Jay FoadTemplateName ASTContext::getCanonicalTemplateName(TemplateName Name) const {
30433e1274f2b99cb99c03cc8e2c6517c37d330b597aDouglas Gregor  if (TemplateDecl *Template = Name.getAsTemplateDecl()) {
30443e1274f2b99cb99c03cc8e2c6517c37d330b597aDouglas Gregor    if (TemplateTemplateParmDecl *TTP
30453e1274f2b99cb99c03cc8e2c6517c37d330b597aDouglas Gregor                              = dyn_cast<TemplateTemplateParmDecl>(Template))
30463e1274f2b99cb99c03cc8e2c6517c37d330b597aDouglas Gregor      Template = getCanonicalTemplateTemplateParmDecl(TTP);
30473e1274f2b99cb99c03cc8e2c6517c37d330b597aDouglas Gregor
30483e1274f2b99cb99c03cc8e2c6517c37d330b597aDouglas Gregor    // The canonical template name is the canonical template declaration.
304997fbaa2a38804268a024f1a104b43fcf8b4411b0Argyrios Kyrtzidis    return TemplateName(cast<TemplateDecl>(Template->getCanonicalDecl()));
30503e1274f2b99cb99c03cc8e2c6517c37d330b597aDouglas Gregor  }
305125a3ef7cc5fd55dc8cc67c6e6770c8595657e082Douglas Gregor
30521aee05d08b2184acadeb36de300e216390780d6cDouglas Gregor  if (SubstTemplateTemplateParmPackStorage *SubstPack
30531aee05d08b2184acadeb36de300e216390780d6cDouglas Gregor                                  = Name.getAsSubstTemplateTemplateParmPack()) {
30541aee05d08b2184acadeb36de300e216390780d6cDouglas Gregor    TemplateTemplateParmDecl *CanonParam
30551aee05d08b2184acadeb36de300e216390780d6cDouglas Gregor      = getCanonicalTemplateTemplateParmDecl(SubstPack->getParameterPack());
30561aee05d08b2184acadeb36de300e216390780d6cDouglas Gregor    TemplateArgument CanonArgPack
30571aee05d08b2184acadeb36de300e216390780d6cDouglas Gregor      = getCanonicalTemplateArgument(SubstPack->getArgumentPack());
30581aee05d08b2184acadeb36de300e216390780d6cDouglas Gregor    return getSubstTemplateTemplateParmPack(CanonParam, CanonArgPack);
30591aee05d08b2184acadeb36de300e216390780d6cDouglas Gregor  }
30601aee05d08b2184acadeb36de300e216390780d6cDouglas Gregor
30610bd6feb9e9d40fc889fd47e899985125a43dfed8John McCall  assert(!Name.getAsOverloadedTemplate());
30621eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
306325a3ef7cc5fd55dc8cc67c6e6770c8595657e082Douglas Gregor  DependentTemplateName *DTN = Name.getAsDependentTemplateName();
306425a3ef7cc5fd55dc8cc67c6e6770c8595657e082Douglas Gregor  assert(DTN && "Non-dependent template names must refer to template decls.");
306525a3ef7cc5fd55dc8cc67c6e6770c8595657e082Douglas Gregor  return DTN->CanonicalTemplateName;
306625a3ef7cc5fd55dc8cc67c6e6770c8595657e082Douglas Gregor}
306725a3ef7cc5fd55dc8cc67c6e6770c8595657e082Douglas Gregor
3068db0d4b751e83b8841b8f48f913f17e50467f13d4Douglas Gregorbool ASTContext::hasSameTemplateName(TemplateName X, TemplateName Y) {
3069db0d4b751e83b8841b8f48f913f17e50467f13d4Douglas Gregor  X = getCanonicalTemplateName(X);
3070db0d4b751e83b8841b8f48f913f17e50467f13d4Douglas Gregor  Y = getCanonicalTemplateName(Y);
3071db0d4b751e83b8841b8f48f913f17e50467f13d4Douglas Gregor  return X.getAsVoidPointer() == Y.getAsVoidPointer();
3072db0d4b751e83b8841b8f48f913f17e50467f13d4Douglas Gregor}
3073db0d4b751e83b8841b8f48f913f17e50467f13d4Douglas Gregor
30741eb4433ac451dc16f4133a88af2d002ac26c58efMike StumpTemplateArgument
30754ba2a17694148e16eaa8d3917f657ffcd3667be4Jay FoadASTContext::getCanonicalTemplateArgument(const TemplateArgument &Arg) const {
30761275ae098acda31fe0e434510c729fcfed0458a1Douglas Gregor  switch (Arg.getKind()) {
30771275ae098acda31fe0e434510c729fcfed0458a1Douglas Gregor    case TemplateArgument::Null:
30781275ae098acda31fe0e434510c729fcfed0458a1Douglas Gregor      return Arg;
30791eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
30801275ae098acda31fe0e434510c729fcfed0458a1Douglas Gregor    case TemplateArgument::Expression:
30811275ae098acda31fe0e434510c729fcfed0458a1Douglas Gregor      return Arg;
30821eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
30831275ae098acda31fe0e434510c729fcfed0458a1Douglas Gregor    case TemplateArgument::Declaration:
3084833ca991c1bfc967f0995974ca86f66ba1f666b5John McCall      return TemplateArgument(Arg.getAsDecl()->getCanonicalDecl());
30851eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
3086788cd06cf8e868a67158aafec5de3a1f408d14f3Douglas Gregor    case TemplateArgument::Template:
3087788cd06cf8e868a67158aafec5de3a1f408d14f3Douglas Gregor      return TemplateArgument(getCanonicalTemplateName(Arg.getAsTemplate()));
3088a7fc901a2e39bfe55bfcff5934b2d9fdf9656491Douglas Gregor
3089a7fc901a2e39bfe55bfcff5934b2d9fdf9656491Douglas Gregor    case TemplateArgument::TemplateExpansion:
3090a7fc901a2e39bfe55bfcff5934b2d9fdf9656491Douglas Gregor      return TemplateArgument(getCanonicalTemplateName(
3091a7fc901a2e39bfe55bfcff5934b2d9fdf9656491Douglas Gregor                                         Arg.getAsTemplateOrTemplatePattern()),
30922be29f423acad3bbe39099a78db2805acb5bdf17Douglas Gregor                              Arg.getNumTemplateExpansions());
3093a7fc901a2e39bfe55bfcff5934b2d9fdf9656491Douglas Gregor
30941275ae098acda31fe0e434510c729fcfed0458a1Douglas Gregor    case TemplateArgument::Integral:
3095833ca991c1bfc967f0995974ca86f66ba1f666b5John McCall      return TemplateArgument(*Arg.getAsIntegral(),
30961275ae098acda31fe0e434510c729fcfed0458a1Douglas Gregor                              getCanonicalType(Arg.getIntegralType()));
30971eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
30981275ae098acda31fe0e434510c729fcfed0458a1Douglas Gregor    case TemplateArgument::Type:
3099833ca991c1bfc967f0995974ca86f66ba1f666b5John McCall      return TemplateArgument(getCanonicalType(Arg.getAsType()));
31001eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
31011275ae098acda31fe0e434510c729fcfed0458a1Douglas Gregor    case TemplateArgument::Pack: {
310287dd697dcc8ecb64df73ae64d61b8c80ff0c157cDouglas Gregor      if (Arg.pack_size() == 0)
310387dd697dcc8ecb64df73ae64d61b8c80ff0c157cDouglas Gregor        return Arg;
310487dd697dcc8ecb64df73ae64d61b8c80ff0c157cDouglas Gregor
3105910f8008fea79120489a53593fe971b0b8a4a740Douglas Gregor      TemplateArgument *CanonArgs
3106910f8008fea79120489a53593fe971b0b8a4a740Douglas Gregor        = new (*this) TemplateArgument[Arg.pack_size()];
31071275ae098acda31fe0e434510c729fcfed0458a1Douglas Gregor      unsigned Idx = 0;
31081eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      for (TemplateArgument::pack_iterator A = Arg.pack_begin(),
31091275ae098acda31fe0e434510c729fcfed0458a1Douglas Gregor                                        AEnd = Arg.pack_end();
31101275ae098acda31fe0e434510c729fcfed0458a1Douglas Gregor           A != AEnd; (void)++A, ++Idx)
31111275ae098acda31fe0e434510c729fcfed0458a1Douglas Gregor        CanonArgs[Idx] = getCanonicalTemplateArgument(*A);
31121eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
3113910f8008fea79120489a53593fe971b0b8a4a740Douglas Gregor      return TemplateArgument(CanonArgs, Arg.pack_size());
31141275ae098acda31fe0e434510c729fcfed0458a1Douglas Gregor    }
31151275ae098acda31fe0e434510c729fcfed0458a1Douglas Gregor  }
31161275ae098acda31fe0e434510c729fcfed0458a1Douglas Gregor
31171275ae098acda31fe0e434510c729fcfed0458a1Douglas Gregor  // Silence GCC warning
31181275ae098acda31fe0e434510c729fcfed0458a1Douglas Gregor  assert(false && "Unhandled template argument kind");
31191275ae098acda31fe0e434510c729fcfed0458a1Douglas Gregor  return TemplateArgument();
31201275ae098acda31fe0e434510c729fcfed0458a1Douglas Gregor}
31211275ae098acda31fe0e434510c729fcfed0458a1Douglas Gregor
3122d57959af02b4af695276f4204443afe6e5d86bd8Douglas GregorNestedNameSpecifier *
31234ba2a17694148e16eaa8d3917f657ffcd3667be4Jay FoadASTContext::getCanonicalNestedNameSpecifier(NestedNameSpecifier *NNS) const {
31241eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  if (!NNS)
3125d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor    return 0;
3126d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor
3127d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor  switch (NNS->getKind()) {
3128d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor  case NestedNameSpecifier::Identifier:
3129d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor    // Canonicalize the prefix but keep the identifier the same.
31301eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    return NestedNameSpecifier::Create(*this,
3131d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor                         getCanonicalNestedNameSpecifier(NNS->getPrefix()),
3132d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor                                       NNS->getAsIdentifier());
3133d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor
3134d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor  case NestedNameSpecifier::Namespace:
3135d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor    // A namespace is canonical; build a nested-name-specifier with
3136d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor    // this namespace and no prefix.
313714aba76042e041b2c5e439bf4ae353a0a3c7fd73Douglas Gregor    return NestedNameSpecifier::Create(*this, 0,
313814aba76042e041b2c5e439bf4ae353a0a3c7fd73Douglas Gregor                                 NNS->getAsNamespace()->getOriginalNamespace());
313914aba76042e041b2c5e439bf4ae353a0a3c7fd73Douglas Gregor
314014aba76042e041b2c5e439bf4ae353a0a3c7fd73Douglas Gregor  case NestedNameSpecifier::NamespaceAlias:
314114aba76042e041b2c5e439bf4ae353a0a3c7fd73Douglas Gregor    // A namespace is canonical; build a nested-name-specifier with
314214aba76042e041b2c5e439bf4ae353a0a3c7fd73Douglas Gregor    // this namespace and no prefix.
314314aba76042e041b2c5e439bf4ae353a0a3c7fd73Douglas Gregor    return NestedNameSpecifier::Create(*this, 0,
314414aba76042e041b2c5e439bf4ae353a0a3c7fd73Douglas Gregor                                    NNS->getAsNamespaceAlias()->getNamespace()
314514aba76042e041b2c5e439bf4ae353a0a3c7fd73Douglas Gregor                                                      ->getOriginalNamespace());
3146d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor
3147d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor  case NestedNameSpecifier::TypeSpec:
3148d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor  case NestedNameSpecifier::TypeSpecWithTemplate: {
3149d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor    QualType T = getCanonicalType(QualType(NNS->getAsType(), 0));
3150264bf66d55563dd86a3d7e06738aa427de512d2cDouglas Gregor
3151264bf66d55563dd86a3d7e06738aa427de512d2cDouglas Gregor    // If we have some kind of dependent-named type (e.g., "typename T::type"),
3152264bf66d55563dd86a3d7e06738aa427de512d2cDouglas Gregor    // break it apart into its prefix and identifier, then reconsititute those
3153264bf66d55563dd86a3d7e06738aa427de512d2cDouglas Gregor    // as the canonical nested-name-specifier. This is required to canonicalize
3154264bf66d55563dd86a3d7e06738aa427de512d2cDouglas Gregor    // a dependent nested-name-specifier involving typedefs of dependent-name
3155264bf66d55563dd86a3d7e06738aa427de512d2cDouglas Gregor    // types, e.g.,
3156264bf66d55563dd86a3d7e06738aa427de512d2cDouglas Gregor    //   typedef typename T::type T1;
3157264bf66d55563dd86a3d7e06738aa427de512d2cDouglas Gregor    //   typedef typename T1::type T2;
3158264bf66d55563dd86a3d7e06738aa427de512d2cDouglas Gregor    if (const DependentNameType *DNT = T->getAs<DependentNameType>()) {
3159264bf66d55563dd86a3d7e06738aa427de512d2cDouglas Gregor      NestedNameSpecifier *Prefix
3160264bf66d55563dd86a3d7e06738aa427de512d2cDouglas Gregor        = getCanonicalNestedNameSpecifier(DNT->getQualifier());
3161264bf66d55563dd86a3d7e06738aa427de512d2cDouglas Gregor      return NestedNameSpecifier::Create(*this, Prefix,
3162264bf66d55563dd86a3d7e06738aa427de512d2cDouglas Gregor                           const_cast<IdentifierInfo *>(DNT->getIdentifier()));
3163264bf66d55563dd86a3d7e06738aa427de512d2cDouglas Gregor    }
3164264bf66d55563dd86a3d7e06738aa427de512d2cDouglas Gregor
3165643f84353b3b7bbf9b73bcbc7da0ef8a39a5ca99Douglas Gregor    // Do the same thing as above, but with dependent-named specializations.
3166264bf66d55563dd86a3d7e06738aa427de512d2cDouglas Gregor    if (const DependentTemplateSpecializationType *DTST
3167264bf66d55563dd86a3d7e06738aa427de512d2cDouglas Gregor          = T->getAs<DependentTemplateSpecializationType>()) {
3168264bf66d55563dd86a3d7e06738aa427de512d2cDouglas Gregor      NestedNameSpecifier *Prefix
3169264bf66d55563dd86a3d7e06738aa427de512d2cDouglas Gregor        = getCanonicalNestedNameSpecifier(DTST->getQualifier());
3170aa2187de137e5b809dcbbe14f3b61ae907a3d8aaDouglas Gregor
3171aa2187de137e5b809dcbbe14f3b61ae907a3d8aaDouglas Gregor      T = getDependentTemplateSpecializationType(DTST->getKeyword(),
3172aa2187de137e5b809dcbbe14f3b61ae907a3d8aaDouglas Gregor                                                 Prefix, DTST->getIdentifier(),
3173aa2187de137e5b809dcbbe14f3b61ae907a3d8aaDouglas Gregor                                                 DTST->getNumArgs(),
3174aa2187de137e5b809dcbbe14f3b61ae907a3d8aaDouglas Gregor                                                 DTST->getArgs());
3175264bf66d55563dd86a3d7e06738aa427de512d2cDouglas Gregor      T = getCanonicalType(T);
3176264bf66d55563dd86a3d7e06738aa427de512d2cDouglas Gregor    }
3177264bf66d55563dd86a3d7e06738aa427de512d2cDouglas Gregor
31783b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall    return NestedNameSpecifier::Create(*this, 0, false,
31793b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall                                       const_cast<Type*>(T.getTypePtr()));
3180d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor  }
3181d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor
3182d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor  case NestedNameSpecifier::Global:
3183d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor    // The global specifier is canonical and unique.
3184d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor    return NNS;
3185d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor  }
3186d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor
3187d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor  // Required to silence a GCC warning
3188d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor  return 0;
3189d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor}
3190d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor
3191c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner
31924ba2a17694148e16eaa8d3917f657ffcd3667be4Jay Foadconst ArrayType *ASTContext::getAsArrayType(QualType T) const {
3193c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner  // Handle the non-qualified case efficiently.
3194a4923eb7c4b04d360cb2747641a5e92818edf804Douglas Gregor  if (!T.hasLocalQualifiers()) {
3195c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner    // Handle the common positive case fast.
3196c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner    if (const ArrayType *AT = dyn_cast<ArrayType>(T))
3197c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner      return AT;
3198c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner  }
31991eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
32000953e767ff7817f97b3ab20896b229891eeff45bJohn McCall  // Handle the common negative case fast.
32013b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall  if (!isa<ArrayType>(T.getCanonicalType()))
3202c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner    return 0;
32031eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
32040953e767ff7817f97b3ab20896b229891eeff45bJohn McCall  // Apply any qualifiers from the array type to the element type.  This
3205c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner  // implements C99 6.7.3p8: "If the specification of an array type includes
3206c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner  // any type qualifiers, the element type is so qualified, not the array type."
32071eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
3208c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner  // If we get here, we either have type qualifiers on the type, or we have
3209c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner  // sugar such as a typedef in the way.  If we have type qualifiers on the type
321050d62d1b4a98adbc83de8f8cd1379ea1c25656f7Douglas Gregor  // we must propagate them down into the element type.
32110953e767ff7817f97b3ab20896b229891eeff45bJohn McCall
32123b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall  SplitQualType split = T.getSplitDesugaredType();
32133b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall  Qualifiers qs = split.second;
32141eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
3215c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner  // If we have a simple case, just return now.
32163b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall  const ArrayType *ATy = dyn_cast<ArrayType>(split.first);
32173b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall  if (ATy == 0 || qs.empty())
3218c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner    return ATy;
32191eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
3220c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner  // Otherwise, we have an array and we have qualifiers on it.  Push the
3221c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner  // qualifiers into the array element type and return a new array type.
32223b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall  QualType NewEltTy = getQualifiedType(ATy->getElementType(), qs);
32231eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
3224c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner  if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(ATy))
3225c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner    return cast<ArrayType>(getConstantArrayType(NewEltTy, CAT->getSize(),
3226c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner                                                CAT->getSizeModifier(),
32270953e767ff7817f97b3ab20896b229891eeff45bJohn McCall                                           CAT->getIndexTypeCVRQualifiers()));
3228c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner  if (const IncompleteArrayType *IAT = dyn_cast<IncompleteArrayType>(ATy))
3229c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner    return cast<ArrayType>(getIncompleteArrayType(NewEltTy,
3230c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner                                                  IAT->getSizeModifier(),
32310953e767ff7817f97b3ab20896b229891eeff45bJohn McCall                                           IAT->getIndexTypeCVRQualifiers()));
3232898574e7496ba8fd76290079d3a9d06954992734Douglas Gregor
32331eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  if (const DependentSizedArrayType *DSAT
3234898574e7496ba8fd76290079d3a9d06954992734Douglas Gregor        = dyn_cast<DependentSizedArrayType>(ATy))
3235898574e7496ba8fd76290079d3a9d06954992734Douglas Gregor    return cast<ArrayType>(
32361eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump                     getDependentSizedArrayType(NewEltTy,
32373fa5cae9b3812cab9fab6c042c3329bb70a3d046John McCall                                                DSAT->getSizeExpr(),
3238898574e7496ba8fd76290079d3a9d06954992734Douglas Gregor                                                DSAT->getSizeModifier(),
32390953e767ff7817f97b3ab20896b229891eeff45bJohn McCall                                              DSAT->getIndexTypeCVRQualifiers(),
32407e7eb3da052a6d80ddf2377cab0384c798f73f75Douglas Gregor                                                DSAT->getBracketsRange()));
32411eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
3242c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner  const VariableArrayType *VAT = cast<VariableArrayType>(ATy);
32437e7eb3da052a6d80ddf2377cab0384c798f73f75Douglas Gregor  return cast<ArrayType>(getVariableArrayType(NewEltTy,
32443fa5cae9b3812cab9fab6c042c3329bb70a3d046John McCall                                              VAT->getSizeExpr(),
3245c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner                                              VAT->getSizeModifier(),
32460953e767ff7817f97b3ab20896b229891eeff45bJohn McCall                                              VAT->getIndexTypeCVRQualifiers(),
32477e7eb3da052a6d80ddf2377cab0384c798f73f75Douglas Gregor                                              VAT->getBracketsRange()));
324877c9647cae939104c6cb2b6a4dd8ca859d2e5770Chris Lattner}
324977c9647cae939104c6cb2b6a4dd8ca859d2e5770Chris Lattner
3250e6327747b72bb687c948270f702ff53c30f411a6Chris Lattner/// getArrayDecayedType - Return the properly qualified result of decaying the
3251e6327747b72bb687c948270f702ff53c30f411a6Chris Lattner/// specified array type to a pointer.  This operation is non-trivial when
3252e6327747b72bb687c948270f702ff53c30f411a6Chris Lattner/// handling typedefs etc.  The canonical type of "T" must be an array type,
3253e6327747b72bb687c948270f702ff53c30f411a6Chris Lattner/// this returns a pointer to a properly qualified element of the array.
3254e6327747b72bb687c948270f702ff53c30f411a6Chris Lattner///
3255e6327747b72bb687c948270f702ff53c30f411a6Chris Lattner/// See C99 6.7.5.3p7 and C99 6.3.2.1p3.
32564ba2a17694148e16eaa8d3917f657ffcd3667be4Jay FoadQualType ASTContext::getArrayDecayedType(QualType Ty) const {
3257c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner  // Get the element type with 'getAsArrayType' so that we don't lose any
3258c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner  // typedefs in the element type of the array.  This also handles propagation
3259c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner  // of type qualifiers from the array type into the element type if present
3260c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner  // (C99 6.7.3p8).
3261c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner  const ArrayType *PrettyArrayType = getAsArrayType(Ty);
3262c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner  assert(PrettyArrayType && "Not an array type!");
32631eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
3264c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner  QualType PtrTy = getPointerType(PrettyArrayType->getElementType());
3265e6327747b72bb687c948270f702ff53c30f411a6Chris Lattner
3266e6327747b72bb687c948270f702ff53c30f411a6Chris Lattner  // int x[restrict 4] ->  int *restrict
32670953e767ff7817f97b3ab20896b229891eeff45bJohn McCall  return getQualifiedType(PtrTy, PrettyArrayType->getIndexTypeQualifiers());
3268e6327747b72bb687c948270f702ff53c30f411a6Chris Lattner}
3269e6327747b72bb687c948270f702ff53c30f411a6Chris Lattner
32703b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCallQualType ASTContext::getBaseElementType(const ArrayType *array) const {
32713b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall  return getBaseElementType(array->getElementType());
32725e03f9ea8174ae588c5e69ec6b5ef4c68f8fd766Douglas Gregor}
32735e03f9ea8174ae588c5e69ec6b5ef4c68f8fd766Douglas Gregor
32743b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCallQualType ASTContext::getBaseElementType(QualType type) const {
32753b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall  Qualifiers qs;
32763b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall  while (true) {
32773b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall    SplitQualType split = type.getSplitDesugaredType();
32783b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall    const ArrayType *array = split.first->getAsArrayTypeUnsafe();
32793b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall    if (!array) break;
32801eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
32813b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall    type = array->getElementType();
32823b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall    qs.addConsistentQualifiers(split.second);
32833b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall  }
32841eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
32853b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall  return getQualifiedType(type, qs);
32866183a99b064b397d98297904fbd6cf00fe1f453dAnders Carlsson}
32876183a99b064b397d98297904fbd6cf00fe1f453dAnders Carlsson
32880de78998e7bda473b408437053e48661b510d453Fariborz Jahanian/// getConstantArrayElementCount - Returns number of constant array elements.
32891eb4433ac451dc16f4133a88af2d002ac26c58efMike Stumpuint64_t
32900de78998e7bda473b408437053e48661b510d453Fariborz JahanianASTContext::getConstantArrayElementCount(const ConstantArrayType *CA)  const {
32910de78998e7bda473b408437053e48661b510d453Fariborz Jahanian  uint64_t ElementCount = 1;
32920de78998e7bda473b408437053e48661b510d453Fariborz Jahanian  do {
32930de78998e7bda473b408437053e48661b510d453Fariborz Jahanian    ElementCount *= CA->getSize().getZExtValue();
32940de78998e7bda473b408437053e48661b510d453Fariborz Jahanian    CA = dyn_cast<ConstantArrayType>(CA->getElementType());
32950de78998e7bda473b408437053e48661b510d453Fariborz Jahanian  } while (CA);
32960de78998e7bda473b408437053e48661b510d453Fariborz Jahanian  return ElementCount;
32970de78998e7bda473b408437053e48661b510d453Fariborz Jahanian}
32980de78998e7bda473b408437053e48661b510d453Fariborz Jahanian
32995f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// getFloatingRank - Return a relative rank for floating point types.
33005f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// This routine will assert if passed a built-in type that isn't a float.
3301a75cea3f6be0daa8054d36af81a6ffda1713f82dChris Lattnerstatic FloatingRank getFloatingRank(QualType T) {
3302183700f494ec9b6701b6efe82bcb25f4c79ba561John McCall  if (const ComplexType *CT = T->getAs<ComplexType>())
33035f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    return getFloatingRank(CT->getElementType());
3304a75cea3f6be0daa8054d36af81a6ffda1713f82dChris Lattner
3305183700f494ec9b6701b6efe82bcb25f4c79ba561John McCall  assert(T->getAs<BuiltinType>() && "getFloatingRank(): not a floating type");
3306183700f494ec9b6701b6efe82bcb25f4c79ba561John McCall  switch (T->getAs<BuiltinType>()->getKind()) {
3307a75cea3f6be0daa8054d36af81a6ffda1713f82dChris Lattner  default: assert(0 && "getFloatingRank(): not a floating type");
33085f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case BuiltinType::Float:      return FloatRank;
33095f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case BuiltinType::Double:     return DoubleRank;
33105f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case BuiltinType::LongDouble: return LongDoubleRank;
33115f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
33125f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
33135f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
33141eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// getFloatingTypeOfSizeWithinDomain - Returns a real floating
33151eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// point or a complex type (based on typeDomain/typeSize).
3316716c7304ff5d27a95e1e7823acd1d09d5ec3e37fSteve Naroff/// 'typeDomain' is a real floating point or complex type.
3317716c7304ff5d27a95e1e7823acd1d09d5ec3e37fSteve Naroff/// 'typeSize' is a real floating point or complex type.
33181361b11066239ea15764a2a844405352d87296b3Chris LattnerQualType ASTContext::getFloatingTypeOfSizeWithinDomain(QualType Size,
33191361b11066239ea15764a2a844405352d87296b3Chris Lattner                                                       QualType Domain) const {
33201361b11066239ea15764a2a844405352d87296b3Chris Lattner  FloatingRank EltRank = getFloatingRank(Size);
33211361b11066239ea15764a2a844405352d87296b3Chris Lattner  if (Domain->isComplexType()) {
33221361b11066239ea15764a2a844405352d87296b3Chris Lattner    switch (EltRank) {
3323716c7304ff5d27a95e1e7823acd1d09d5ec3e37fSteve Naroff    default: assert(0 && "getFloatingRank(): illegal value for rank");
3324f1448a0e4a1e868ff873a8530a61a09cb68666ccSteve Naroff    case FloatRank:      return FloatComplexTy;
3325f1448a0e4a1e868ff873a8530a61a09cb68666ccSteve Naroff    case DoubleRank:     return DoubleComplexTy;
3326f1448a0e4a1e868ff873a8530a61a09cb68666ccSteve Naroff    case LongDoubleRank: return LongDoubleComplexTy;
3327f1448a0e4a1e868ff873a8530a61a09cb68666ccSteve Naroff    }
3328f1448a0e4a1e868ff873a8530a61a09cb68666ccSteve Naroff  }
33291361b11066239ea15764a2a844405352d87296b3Chris Lattner
33301361b11066239ea15764a2a844405352d87296b3Chris Lattner  assert(Domain->isRealFloatingType() && "Unknown domain!");
33311361b11066239ea15764a2a844405352d87296b3Chris Lattner  switch (EltRank) {
33321361b11066239ea15764a2a844405352d87296b3Chris Lattner  default: assert(0 && "getFloatingRank(): illegal value for rank");
33331361b11066239ea15764a2a844405352d87296b3Chris Lattner  case FloatRank:      return FloatTy;
33341361b11066239ea15764a2a844405352d87296b3Chris Lattner  case DoubleRank:     return DoubleTy;
33351361b11066239ea15764a2a844405352d87296b3Chris Lattner  case LongDoubleRank: return LongDoubleTy;
33365f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
33375f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
33385f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
33397cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner/// getFloatingTypeOrder - Compare the rank of the two specified floating
33407cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner/// point types, ignoring the domain of the type (i.e. 'double' ==
33417cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner/// '_Complex double').  If LHS > RHS, return 1.  If LHS == RHS, return 0. If
33421eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// LHS < RHS, return -1.
33434ba2a17694148e16eaa8d3917f657ffcd3667be4Jay Foadint ASTContext::getFloatingTypeOrder(QualType LHS, QualType RHS) const {
3344a75cea3f6be0daa8054d36af81a6ffda1713f82dChris Lattner  FloatingRank LHSR = getFloatingRank(LHS);
3345a75cea3f6be0daa8054d36af81a6ffda1713f82dChris Lattner  FloatingRank RHSR = getFloatingRank(RHS);
33461eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
3347a75cea3f6be0daa8054d36af81a6ffda1713f82dChris Lattner  if (LHSR == RHSR)
3348fb0d49669aa370b4c0993c5cee60275ef9fd6518Steve Naroff    return 0;
3349a75cea3f6be0daa8054d36af81a6ffda1713f82dChris Lattner  if (LHSR > RHSR)
3350fb0d49669aa370b4c0993c5cee60275ef9fd6518Steve Naroff    return 1;
3351fb0d49669aa370b4c0993c5cee60275ef9fd6518Steve Naroff  return -1;
33525f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
33535f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
3354f52ab250ff92bc51a9ac9a8e19bd43b63a5f844fChris Lattner/// getIntegerRank - Return an integer conversion rank (C99 6.3.1.1p1). This
3355f52ab250ff92bc51a9ac9a8e19bd43b63a5f844fChris Lattner/// routine will assert if passed a built-in type that isn't an integer or enum,
3356f52ab250ff92bc51a9ac9a8e19bd43b63a5f844fChris Lattner/// or if it is not canonicalized.
3357f4c7371fb1d3cebcfb40abad4537bb82515704eaJohn McCallunsigned ASTContext::getIntegerRank(const Type *T) const {
3358467b27b9a24bdc823218ad1ad0e37673b6cc1e83John McCall  assert(T->isCanonicalUnqualified() && "T should be canonicalized");
3359f4c7371fb1d3cebcfb40abad4537bb82515704eaJohn McCall  if (const EnumType* ET = dyn_cast<EnumType>(T))
3360842aef8d942a880eeb9535d40de31a86838264cbJohn McCall    T = ET->getDecl()->getPromotionType().getTypePtr();
3361f98aba35e6c3da5aae61843fc01334939e4e12ecEli Friedman
33623f59c975aa5d047f7edd1b900b5e885c38af0ef7Chris Lattner  if (T->isSpecificBuiltinType(BuiltinType::WChar_S) ||
33633f59c975aa5d047f7edd1b900b5e885c38af0ef7Chris Lattner      T->isSpecificBuiltinType(BuiltinType::WChar_U))
3364a34267595534a72703290153a6f7e3da1adcec59Eli Friedman    T = getFromTargetType(Target.getWCharType()).getTypePtr();
3365a34267595534a72703290153a6f7e3da1adcec59Eli Friedman
3366f5c209d23b20ada4a9b6235db50317239cbf6ae1Alisdair Meredith  if (T->isSpecificBuiltinType(BuiltinType::Char16))
3367f5c209d23b20ada4a9b6235db50317239cbf6ae1Alisdair Meredith    T = getFromTargetType(Target.getChar16Type()).getTypePtr();
3368f5c209d23b20ada4a9b6235db50317239cbf6ae1Alisdair Meredith
3369f5c209d23b20ada4a9b6235db50317239cbf6ae1Alisdair Meredith  if (T->isSpecificBuiltinType(BuiltinType::Char32))
3370f5c209d23b20ada4a9b6235db50317239cbf6ae1Alisdair Meredith    T = getFromTargetType(Target.getChar32Type()).getTypePtr();
3371f5c209d23b20ada4a9b6235db50317239cbf6ae1Alisdair Meredith
3372f52ab250ff92bc51a9ac9a8e19bd43b63a5f844fChris Lattner  switch (cast<BuiltinType>(T)->getKind()) {
33737cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner  default: assert(0 && "getIntegerRank(): not a built-in integer");
33747cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner  case BuiltinType::Bool:
3375f98aba35e6c3da5aae61843fc01334939e4e12ecEli Friedman    return 1 + (getIntWidth(BoolTy) << 3);
33767cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner  case BuiltinType::Char_S:
33777cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner  case BuiltinType::Char_U:
33787cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner  case BuiltinType::SChar:
33797cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner  case BuiltinType::UChar:
3380f98aba35e6c3da5aae61843fc01334939e4e12ecEli Friedman    return 2 + (getIntWidth(CharTy) << 3);
33817cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner  case BuiltinType::Short:
33827cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner  case BuiltinType::UShort:
3383f98aba35e6c3da5aae61843fc01334939e4e12ecEli Friedman    return 3 + (getIntWidth(ShortTy) << 3);
33847cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner  case BuiltinType::Int:
33857cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner  case BuiltinType::UInt:
3386f98aba35e6c3da5aae61843fc01334939e4e12ecEli Friedman    return 4 + (getIntWidth(IntTy) << 3);
33877cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner  case BuiltinType::Long:
33887cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner  case BuiltinType::ULong:
3389f98aba35e6c3da5aae61843fc01334939e4e12ecEli Friedman    return 5 + (getIntWidth(LongTy) << 3);
33907cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner  case BuiltinType::LongLong:
33917cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner  case BuiltinType::ULongLong:
3392f98aba35e6c3da5aae61843fc01334939e4e12ecEli Friedman    return 6 + (getIntWidth(LongLongTy) << 3);
33932df9ced9fd1e8c7d7b38443db07e0e811de22571Chris Lattner  case BuiltinType::Int128:
33942df9ced9fd1e8c7d7b38443db07e0e811de22571Chris Lattner  case BuiltinType::UInt128:
33952df9ced9fd1e8c7d7b38443db07e0e811de22571Chris Lattner    return 7 + (getIntWidth(Int128Ty) << 3);
3396f52ab250ff92bc51a9ac9a8e19bd43b63a5f844fChris Lattner  }
3397f52ab250ff92bc51a9ac9a8e19bd43b63a5f844fChris Lattner}
3398f52ab250ff92bc51a9ac9a8e19bd43b63a5f844fChris Lattner
339904e8357f6801e9ff52673e7e899a67bbabf9de93Eli Friedman/// \brief Whether this is a promotable bitfield reference according
340004e8357f6801e9ff52673e7e899a67bbabf9de93Eli Friedman/// to C99 6.3.1.1p2, bullet 2 (and GCC extensions).
340104e8357f6801e9ff52673e7e899a67bbabf9de93Eli Friedman///
340204e8357f6801e9ff52673e7e899a67bbabf9de93Eli Friedman/// \returns the type this bit-field will promote to, or NULL if no
340304e8357f6801e9ff52673e7e899a67bbabf9de93Eli Friedman/// promotion occurs.
34044ba2a17694148e16eaa8d3917f657ffcd3667be4Jay FoadQualType ASTContext::isPromotableBitField(Expr *E) const {
3405ceafbdeb93ecf323cca74e660bf54504c86f3b71Douglas Gregor  if (E->isTypeDependent() || E->isValueDependent())
3406ceafbdeb93ecf323cca74e660bf54504c86f3b71Douglas Gregor    return QualType();
3407ceafbdeb93ecf323cca74e660bf54504c86f3b71Douglas Gregor
340804e8357f6801e9ff52673e7e899a67bbabf9de93Eli Friedman  FieldDecl *Field = E->getBitField();
340904e8357f6801e9ff52673e7e899a67bbabf9de93Eli Friedman  if (!Field)
341004e8357f6801e9ff52673e7e899a67bbabf9de93Eli Friedman    return QualType();
341104e8357f6801e9ff52673e7e899a67bbabf9de93Eli Friedman
341204e8357f6801e9ff52673e7e899a67bbabf9de93Eli Friedman  QualType FT = Field->getType();
341304e8357f6801e9ff52673e7e899a67bbabf9de93Eli Friedman
341404e8357f6801e9ff52673e7e899a67bbabf9de93Eli Friedman  llvm::APSInt BitWidthAP = Field->getBitWidth()->EvaluateAsInt(*this);
341504e8357f6801e9ff52673e7e899a67bbabf9de93Eli Friedman  uint64_t BitWidth = BitWidthAP.getZExtValue();
341604e8357f6801e9ff52673e7e899a67bbabf9de93Eli Friedman  uint64_t IntSize = getTypeSize(IntTy);
341704e8357f6801e9ff52673e7e899a67bbabf9de93Eli Friedman  // GCC extension compatibility: if the bit-field size is less than or equal
341804e8357f6801e9ff52673e7e899a67bbabf9de93Eli Friedman  // to the size of int, it gets promoted no matter what its type is.
341904e8357f6801e9ff52673e7e899a67bbabf9de93Eli Friedman  // For instance, unsigned long bf : 4 gets promoted to signed int.
342004e8357f6801e9ff52673e7e899a67bbabf9de93Eli Friedman  if (BitWidth < IntSize)
342104e8357f6801e9ff52673e7e899a67bbabf9de93Eli Friedman    return IntTy;
342204e8357f6801e9ff52673e7e899a67bbabf9de93Eli Friedman
342304e8357f6801e9ff52673e7e899a67bbabf9de93Eli Friedman  if (BitWidth == IntSize)
342404e8357f6801e9ff52673e7e899a67bbabf9de93Eli Friedman    return FT->isSignedIntegerType() ? IntTy : UnsignedIntTy;
342504e8357f6801e9ff52673e7e899a67bbabf9de93Eli Friedman
342604e8357f6801e9ff52673e7e899a67bbabf9de93Eli Friedman  // Types bigger than int are not subject to promotions, and therefore act
342704e8357f6801e9ff52673e7e899a67bbabf9de93Eli Friedman  // like the base type.
342804e8357f6801e9ff52673e7e899a67bbabf9de93Eli Friedman  // FIXME: This doesn't quite match what gcc does, but what gcc does here
342904e8357f6801e9ff52673e7e899a67bbabf9de93Eli Friedman  // is ridiculous.
343004e8357f6801e9ff52673e7e899a67bbabf9de93Eli Friedman  return QualType();
343104e8357f6801e9ff52673e7e899a67bbabf9de93Eli Friedman}
343204e8357f6801e9ff52673e7e899a67bbabf9de93Eli Friedman
3433a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman/// getPromotedIntegerType - Returns the type that Promotable will
3434a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman/// promote to: C99 6.3.1.1p2, assuming that Promotable is a promotable
3435a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman/// integer type.
34364ba2a17694148e16eaa8d3917f657ffcd3667be4Jay FoadQualType ASTContext::getPromotedIntegerType(QualType Promotable) const {
3437a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman  assert(!Promotable.isNull());
3438a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman  assert(Promotable->isPromotableIntegerType());
3439842aef8d942a880eeb9535d40de31a86838264cbJohn McCall  if (const EnumType *ET = Promotable->getAs<EnumType>())
3440842aef8d942a880eeb9535d40de31a86838264cbJohn McCall    return ET->getDecl()->getPromotionType();
3441a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman  if (Promotable->isSignedIntegerType())
3442a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman    return IntTy;
3443a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman  uint64_t PromotableSize = getTypeSize(Promotable);
3444a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman  uint64_t IntSize = getTypeSize(IntTy);
3445a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman  assert(Promotable->isUnsignedIntegerType() && PromotableSize <= IntSize);
3446a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman  return (PromotableSize != IntSize) ? IntTy : UnsignedIntTy;
3447a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman}
3448a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman
34491eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// getIntegerTypeOrder - Returns the highest ranked integer type:
34507cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner/// C99 6.3.1.8p1.  If LHS > RHS, return 1.  If LHS == RHS, return 0. If
34511eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// LHS < RHS, return -1.
34524ba2a17694148e16eaa8d3917f657ffcd3667be4Jay Foadint ASTContext::getIntegerTypeOrder(QualType LHS, QualType RHS) const {
3453f4c7371fb1d3cebcfb40abad4537bb82515704eaJohn McCall  const Type *LHSC = getCanonicalType(LHS).getTypePtr();
3454f4c7371fb1d3cebcfb40abad4537bb82515704eaJohn McCall  const Type *RHSC = getCanonicalType(RHS).getTypePtr();
34557cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner  if (LHSC == RHSC) return 0;
34561eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
3457f52ab250ff92bc51a9ac9a8e19bd43b63a5f844fChris Lattner  bool LHSUnsigned = LHSC->isUnsignedIntegerType();
3458f52ab250ff92bc51a9ac9a8e19bd43b63a5f844fChris Lattner  bool RHSUnsigned = RHSC->isUnsignedIntegerType();
34591eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
34607cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner  unsigned LHSRank = getIntegerRank(LHSC);
34617cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner  unsigned RHSRank = getIntegerRank(RHSC);
34621eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
34637cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner  if (LHSUnsigned == RHSUnsigned) {  // Both signed or both unsigned.
34647cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner    if (LHSRank == RHSRank) return 0;
34657cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner    return LHSRank > RHSRank ? 1 : -1;
34667cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner  }
34671eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
34687cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner  // Otherwise, the LHS is signed and the RHS is unsigned or visa versa.
34697cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner  if (LHSUnsigned) {
34707cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner    // If the unsigned [LHS] type is larger, return it.
34717cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner    if (LHSRank >= RHSRank)
34727cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner      return 1;
34731eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
34747cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner    // If the signed type can represent all values of the unsigned type, it
34757cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner    // wins.  Because we are dealing with 2's complement and types that are
34761eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    // powers of two larger than each other, this is always safe.
34777cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner    return -1;
34787cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner  }
34797cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner
34807cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner  // If the unsigned [RHS] type is larger, return it.
34817cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner  if (RHSRank >= LHSRank)
34827cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner    return -1;
34831eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
34847cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner  // If the signed type can represent all values of the unsigned type, it
34857cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner  // wins.  Because we are dealing with 2's complement and types that are
34861eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  // powers of two larger than each other, this is always safe.
34877cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner  return 1;
34885f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
348971993dd85eed9cc42c6b2fa61ee5c53026b74817Anders Carlsson
349079cbc7dd2aacd85a28f469b5dc73c4ea296e7072Anders Carlssonstatic RecordDecl *
3491ba877adeb49ed6dc17f27fa3a3bcd0cca713fd68Abramo BagnaraCreateRecordDecl(const ASTContext &Ctx, RecordDecl::TagKind TK,
3492ba877adeb49ed6dc17f27fa3a3bcd0cca713fd68Abramo Bagnara                 DeclContext *DC, IdentifierInfo *Id) {
3493ba877adeb49ed6dc17f27fa3a3bcd0cca713fd68Abramo Bagnara  SourceLocation Loc;
349479cbc7dd2aacd85a28f469b5dc73c4ea296e7072Anders Carlsson  if (Ctx.getLangOptions().CPlusPlus)
3495ba877adeb49ed6dc17f27fa3a3bcd0cca713fd68Abramo Bagnara    return CXXRecordDecl::Create(Ctx, TK, DC, Loc, Loc, Id);
349679cbc7dd2aacd85a28f469b5dc73c4ea296e7072Anders Carlsson  else
3497ba877adeb49ed6dc17f27fa3a3bcd0cca713fd68Abramo Bagnara    return RecordDecl::Create(Ctx, TK, DC, Loc, Loc, Id);
349879cbc7dd2aacd85a28f469b5dc73c4ea296e7072Anders Carlsson}
3499ba877adeb49ed6dc17f27fa3a3bcd0cca713fd68Abramo Bagnara
35001eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump// getCFConstantStringType - Return the type used for constant CFStrings.
35014ba2a17694148e16eaa8d3917f657ffcd3667be4Jay FoadQualType ASTContext::getCFConstantStringType() const {
350271993dd85eed9cc42c6b2fa61ee5c53026b74817Anders Carlsson  if (!CFConstantStringTypeDecl) {
35031eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    CFConstantStringTypeDecl =
3504ba877adeb49ed6dc17f27fa3a3bcd0cca713fd68Abramo Bagnara      CreateRecordDecl(*this, TTK_Struct, TUDecl,
350579cbc7dd2aacd85a28f469b5dc73c4ea296e7072Anders Carlsson                       &Idents.get("NSConstantString"));
35065cfa011e61e14e6f2e1659047d809706c0e4c6a3John McCall    CFConstantStringTypeDecl->startDefinition();
350779cbc7dd2aacd85a28f469b5dc73c4ea296e7072Anders Carlsson
3508f06273f8bbacb086a46bde456429c8d08f6d07eeAnders Carlsson    QualType FieldTypes[4];
35091eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
351071993dd85eed9cc42c6b2fa61ee5c53026b74817Anders Carlsson    // const int *isa;
35110953e767ff7817f97b3ab20896b229891eeff45bJohn McCall    FieldTypes[0] = getPointerType(IntTy.withConst());
3512f06273f8bbacb086a46bde456429c8d08f6d07eeAnders Carlsson    // int flags;
3513f06273f8bbacb086a46bde456429c8d08f6d07eeAnders Carlsson    FieldTypes[1] = IntTy;
351471993dd85eed9cc42c6b2fa61ee5c53026b74817Anders Carlsson    // const char *str;
35150953e767ff7817f97b3ab20896b229891eeff45bJohn McCall    FieldTypes[2] = getPointerType(CharTy.withConst());
351671993dd85eed9cc42c6b2fa61ee5c53026b74817Anders Carlsson    // long length;
35171eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    FieldTypes[3] = LongTy;
35181eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
351944b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor    // Create fields
352044b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor    for (unsigned i = 0; i < 4; ++i) {
35211eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      FieldDecl *Field = FieldDecl::Create(*this, CFConstantStringTypeDecl,
3522ff676cb48fe8bf7be2feaa251dc7c5fb15af4730Abramo Bagnara                                           SourceLocation(),
352344b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor                                           SourceLocation(), 0,
3524a93c934af4fbf97cbe8e649d82e68ccacfe57c95John McCall                                           FieldTypes[i], /*TInfo=*/0,
35251eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump                                           /*BitWidth=*/0,
35264afa39deaa245592977136d367251ee2c173dd8dDouglas Gregor                                           /*Mutable=*/false);
35272888b65aae768f54062505330df7be230a0510c7John McCall      Field->setAccess(AS_public);
352817945a0f64fe03ff6ec0c2146005a87636e3ac12Argyrios Kyrtzidis      CFConstantStringTypeDecl->addDecl(Field);
352944b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor    }
353044b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor
3531838db383b69b9fb55f55c8e9546477df198a4faaDouglas Gregor    CFConstantStringTypeDecl->completeDefinition();
353271993dd85eed9cc42c6b2fa61ee5c53026b74817Anders Carlsson  }
35331eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
353471993dd85eed9cc42c6b2fa61ee5c53026b74817Anders Carlsson  return getTagDeclType(CFConstantStringTypeDecl);
35358467583c2704e7a9691ea56939a029015f0ade0aGabor Greif}
3536b2cf3573d7351094f6247fcca94703ce88eb9ee0Anders Carlsson
3537319ac896a0fef7365d5589b8021db7e41207fe42Douglas Gregorvoid ASTContext::setCFConstantStringType(QualType T) {
35386217b80b7a1379b74cced1c076338262c3c980b3Ted Kremenek  const RecordType *Rec = T->getAs<RecordType>();
3539319ac896a0fef7365d5589b8021db7e41207fe42Douglas Gregor  assert(Rec && "Invalid CFConstantStringType");
3540319ac896a0fef7365d5589b8021db7e41207fe42Douglas Gregor  CFConstantStringTypeDecl = Rec->getDecl();
3541319ac896a0fef7365d5589b8021db7e41207fe42Douglas Gregor}
3542319ac896a0fef7365d5589b8021db7e41207fe42Douglas Gregor
35432bb5ddaff86ee73d2cea7ec1835978afc88a83f0Fariborz Jahanian// getNSConstantStringType - Return the type used for constant NSStrings.
35444ba2a17694148e16eaa8d3917f657ffcd3667be4Jay FoadQualType ASTContext::getNSConstantStringType() const {
35452bb5ddaff86ee73d2cea7ec1835978afc88a83f0Fariborz Jahanian  if (!NSConstantStringTypeDecl) {
35462bb5ddaff86ee73d2cea7ec1835978afc88a83f0Fariborz Jahanian    NSConstantStringTypeDecl =
3547ba877adeb49ed6dc17f27fa3a3bcd0cca713fd68Abramo Bagnara    CreateRecordDecl(*this, TTK_Struct, TUDecl,
35482bb5ddaff86ee73d2cea7ec1835978afc88a83f0Fariborz Jahanian                     &Idents.get("__builtin_NSString"));
35492bb5ddaff86ee73d2cea7ec1835978afc88a83f0Fariborz Jahanian    NSConstantStringTypeDecl->startDefinition();
35502bb5ddaff86ee73d2cea7ec1835978afc88a83f0Fariborz Jahanian
35512bb5ddaff86ee73d2cea7ec1835978afc88a83f0Fariborz Jahanian    QualType FieldTypes[3];
35522bb5ddaff86ee73d2cea7ec1835978afc88a83f0Fariborz Jahanian
35532bb5ddaff86ee73d2cea7ec1835978afc88a83f0Fariborz Jahanian    // const int *isa;
35542bb5ddaff86ee73d2cea7ec1835978afc88a83f0Fariborz Jahanian    FieldTypes[0] = getPointerType(IntTy.withConst());
35552bb5ddaff86ee73d2cea7ec1835978afc88a83f0Fariborz Jahanian    // const char *str;
35562bb5ddaff86ee73d2cea7ec1835978afc88a83f0Fariborz Jahanian    FieldTypes[1] = getPointerType(CharTy.withConst());
35572bb5ddaff86ee73d2cea7ec1835978afc88a83f0Fariborz Jahanian    // unsigned int length;
35582bb5ddaff86ee73d2cea7ec1835978afc88a83f0Fariborz Jahanian    FieldTypes[2] = UnsignedIntTy;
35592bb5ddaff86ee73d2cea7ec1835978afc88a83f0Fariborz Jahanian
35602bb5ddaff86ee73d2cea7ec1835978afc88a83f0Fariborz Jahanian    // Create fields
35612bb5ddaff86ee73d2cea7ec1835978afc88a83f0Fariborz Jahanian    for (unsigned i = 0; i < 3; ++i) {
35622bb5ddaff86ee73d2cea7ec1835978afc88a83f0Fariborz Jahanian      FieldDecl *Field = FieldDecl::Create(*this, NSConstantStringTypeDecl,
3563ff676cb48fe8bf7be2feaa251dc7c5fb15af4730Abramo Bagnara                                           SourceLocation(),
35642bb5ddaff86ee73d2cea7ec1835978afc88a83f0Fariborz Jahanian                                           SourceLocation(), 0,
35652bb5ddaff86ee73d2cea7ec1835978afc88a83f0Fariborz Jahanian                                           FieldTypes[i], /*TInfo=*/0,
35662bb5ddaff86ee73d2cea7ec1835978afc88a83f0Fariborz Jahanian                                           /*BitWidth=*/0,
35672bb5ddaff86ee73d2cea7ec1835978afc88a83f0Fariborz Jahanian                                           /*Mutable=*/false);
35682888b65aae768f54062505330df7be230a0510c7John McCall      Field->setAccess(AS_public);
35692bb5ddaff86ee73d2cea7ec1835978afc88a83f0Fariborz Jahanian      NSConstantStringTypeDecl->addDecl(Field);
35702bb5ddaff86ee73d2cea7ec1835978afc88a83f0Fariborz Jahanian    }
35712bb5ddaff86ee73d2cea7ec1835978afc88a83f0Fariborz Jahanian
35722bb5ddaff86ee73d2cea7ec1835978afc88a83f0Fariborz Jahanian    NSConstantStringTypeDecl->completeDefinition();
35732bb5ddaff86ee73d2cea7ec1835978afc88a83f0Fariborz Jahanian  }
35742bb5ddaff86ee73d2cea7ec1835978afc88a83f0Fariborz Jahanian
35752bb5ddaff86ee73d2cea7ec1835978afc88a83f0Fariborz Jahanian  return getTagDeclType(NSConstantStringTypeDecl);
35762bb5ddaff86ee73d2cea7ec1835978afc88a83f0Fariborz Jahanian}
35772bb5ddaff86ee73d2cea7ec1835978afc88a83f0Fariborz Jahanian
35782bb5ddaff86ee73d2cea7ec1835978afc88a83f0Fariborz Jahanianvoid ASTContext::setNSConstantStringType(QualType T) {
35792bb5ddaff86ee73d2cea7ec1835978afc88a83f0Fariborz Jahanian  const RecordType *Rec = T->getAs<RecordType>();
35802bb5ddaff86ee73d2cea7ec1835978afc88a83f0Fariborz Jahanian  assert(Rec && "Invalid NSConstantStringType");
35812bb5ddaff86ee73d2cea7ec1835978afc88a83f0Fariborz Jahanian  NSConstantStringTypeDecl = Rec->getDecl();
35822bb5ddaff86ee73d2cea7ec1835978afc88a83f0Fariborz Jahanian}
35832bb5ddaff86ee73d2cea7ec1835978afc88a83f0Fariborz Jahanian
35844ba2a17694148e16eaa8d3917f657ffcd3667be4Jay FoadQualType ASTContext::getObjCFastEnumerationStateType() const {
3585bd4c1ada2e8668f43a865dc2c662085cf61940c4Anders Carlsson  if (!ObjCFastEnumerationStateTypeDecl) {
358644b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor    ObjCFastEnumerationStateTypeDecl =
3587ba877adeb49ed6dc17f27fa3a3bcd0cca713fd68Abramo Bagnara      CreateRecordDecl(*this, TTK_Struct, TUDecl,
358879cbc7dd2aacd85a28f469b5dc73c4ea296e7072Anders Carlsson                       &Idents.get("__objcFastEnumerationState"));
35895cfa011e61e14e6f2e1659047d809706c0e4c6a3John McCall    ObjCFastEnumerationStateTypeDecl->startDefinition();
35901eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
3591bd4c1ada2e8668f43a865dc2c662085cf61940c4Anders Carlsson    QualType FieldTypes[] = {
3592bd4c1ada2e8668f43a865dc2c662085cf61940c4Anders Carlsson      UnsignedLongTy,
3593de2e22d33afec98324a66a358dfe0951b3c7259aSteve Naroff      getPointerType(ObjCIdTypedefType),
3594bd4c1ada2e8668f43a865dc2c662085cf61940c4Anders Carlsson      getPointerType(UnsignedLongTy),
3595bd4c1ada2e8668f43a865dc2c662085cf61940c4Anders Carlsson      getConstantArrayType(UnsignedLongTy,
3596bd4c1ada2e8668f43a865dc2c662085cf61940c4Anders Carlsson                           llvm::APInt(32, 5), ArrayType::Normal, 0)
3597bd4c1ada2e8668f43a865dc2c662085cf61940c4Anders Carlsson    };
35981eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
359944b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor    for (size_t i = 0; i < 4; ++i) {
36001eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      FieldDecl *Field = FieldDecl::Create(*this,
36011eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump                                           ObjCFastEnumerationStateTypeDecl,
3602ff676cb48fe8bf7be2feaa251dc7c5fb15af4730Abramo Bagnara                                           SourceLocation(),
36031eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump                                           SourceLocation(), 0,
3604a93c934af4fbf97cbe8e649d82e68ccacfe57c95John McCall                                           FieldTypes[i], /*TInfo=*/0,
36051eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump                                           /*BitWidth=*/0,
36064afa39deaa245592977136d367251ee2c173dd8dDouglas Gregor                                           /*Mutable=*/false);
36072888b65aae768f54062505330df7be230a0510c7John McCall      Field->setAccess(AS_public);
360817945a0f64fe03ff6ec0c2146005a87636e3ac12Argyrios Kyrtzidis      ObjCFastEnumerationStateTypeDecl->addDecl(Field);
360944b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor    }
36101eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
3611838db383b69b9fb55f55c8e9546477df198a4faaDouglas Gregor    ObjCFastEnumerationStateTypeDecl->completeDefinition();
3612bd4c1ada2e8668f43a865dc2c662085cf61940c4Anders Carlsson  }
36131eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
3614bd4c1ada2e8668f43a865dc2c662085cf61940c4Anders Carlsson  return getTagDeclType(ObjCFastEnumerationStateTypeDecl);
3615bd4c1ada2e8668f43a865dc2c662085cf61940c4Anders Carlsson}
3616bd4c1ada2e8668f43a865dc2c662085cf61940c4Anders Carlsson
36174ba2a17694148e16eaa8d3917f657ffcd3667be4Jay FoadQualType ASTContext::getBlockDescriptorType() const {
3618adaaad3715c9c26cdcfdfe3401a13d7b4423ddcfMike Stump  if (BlockDescriptorType)
3619adaaad3715c9c26cdcfdfe3401a13d7b4423ddcfMike Stump    return getTagDeclType(BlockDescriptorType);
3620adaaad3715c9c26cdcfdfe3401a13d7b4423ddcfMike Stump
3621adaaad3715c9c26cdcfdfe3401a13d7b4423ddcfMike Stump  RecordDecl *T;
3622adaaad3715c9c26cdcfdfe3401a13d7b4423ddcfMike Stump  // FIXME: Needs the FlagAppleBlock bit.
3623ba877adeb49ed6dc17f27fa3a3bcd0cca713fd68Abramo Bagnara  T = CreateRecordDecl(*this, TTK_Struct, TUDecl,
362479cbc7dd2aacd85a28f469b5dc73c4ea296e7072Anders Carlsson                       &Idents.get("__block_descriptor"));
36255cfa011e61e14e6f2e1659047d809706c0e4c6a3John McCall  T->startDefinition();
3626adaaad3715c9c26cdcfdfe3401a13d7b4423ddcfMike Stump
3627adaaad3715c9c26cdcfdfe3401a13d7b4423ddcfMike Stump  QualType FieldTypes[] = {
3628adaaad3715c9c26cdcfdfe3401a13d7b4423ddcfMike Stump    UnsignedLongTy,
3629adaaad3715c9c26cdcfdfe3401a13d7b4423ddcfMike Stump    UnsignedLongTy,
3630adaaad3715c9c26cdcfdfe3401a13d7b4423ddcfMike Stump  };
3631adaaad3715c9c26cdcfdfe3401a13d7b4423ddcfMike Stump
3632adaaad3715c9c26cdcfdfe3401a13d7b4423ddcfMike Stump  const char *FieldNames[] = {
3633adaaad3715c9c26cdcfdfe3401a13d7b4423ddcfMike Stump    "reserved",
3634083c25eea14bb4cc4ecc3ec763c60e2e609e22bdMike Stump    "Size"
3635adaaad3715c9c26cdcfdfe3401a13d7b4423ddcfMike Stump  };
3636adaaad3715c9c26cdcfdfe3401a13d7b4423ddcfMike Stump
3637adaaad3715c9c26cdcfdfe3401a13d7b4423ddcfMike Stump  for (size_t i = 0; i < 2; ++i) {
3638ff676cb48fe8bf7be2feaa251dc7c5fb15af4730Abramo Bagnara    FieldDecl *Field = FieldDecl::Create(*this, T, SourceLocation(),
3639adaaad3715c9c26cdcfdfe3401a13d7b4423ddcfMike Stump                                         SourceLocation(),
3640adaaad3715c9c26cdcfdfe3401a13d7b4423ddcfMike Stump                                         &Idents.get(FieldNames[i]),
3641a93c934af4fbf97cbe8e649d82e68ccacfe57c95John McCall                                         FieldTypes[i], /*TInfo=*/0,
3642adaaad3715c9c26cdcfdfe3401a13d7b4423ddcfMike Stump                                         /*BitWidth=*/0,
3643adaaad3715c9c26cdcfdfe3401a13d7b4423ddcfMike Stump                                         /*Mutable=*/false);
36442888b65aae768f54062505330df7be230a0510c7John McCall    Field->setAccess(AS_public);
3645adaaad3715c9c26cdcfdfe3401a13d7b4423ddcfMike Stump    T->addDecl(Field);
3646adaaad3715c9c26cdcfdfe3401a13d7b4423ddcfMike Stump  }
3647adaaad3715c9c26cdcfdfe3401a13d7b4423ddcfMike Stump
3648838db383b69b9fb55f55c8e9546477df198a4faaDouglas Gregor  T->completeDefinition();
3649adaaad3715c9c26cdcfdfe3401a13d7b4423ddcfMike Stump
3650adaaad3715c9c26cdcfdfe3401a13d7b4423ddcfMike Stump  BlockDescriptorType = T;
3651adaaad3715c9c26cdcfdfe3401a13d7b4423ddcfMike Stump
3652adaaad3715c9c26cdcfdfe3401a13d7b4423ddcfMike Stump  return getTagDeclType(BlockDescriptorType);
3653adaaad3715c9c26cdcfdfe3401a13d7b4423ddcfMike Stump}
3654adaaad3715c9c26cdcfdfe3401a13d7b4423ddcfMike Stump
3655adaaad3715c9c26cdcfdfe3401a13d7b4423ddcfMike Stumpvoid ASTContext::setBlockDescriptorType(QualType T) {
3656adaaad3715c9c26cdcfdfe3401a13d7b4423ddcfMike Stump  const RecordType *Rec = T->getAs<RecordType>();
3657adaaad3715c9c26cdcfdfe3401a13d7b4423ddcfMike Stump  assert(Rec && "Invalid BlockDescriptorType");
3658adaaad3715c9c26cdcfdfe3401a13d7b4423ddcfMike Stump  BlockDescriptorType = Rec->getDecl();
3659adaaad3715c9c26cdcfdfe3401a13d7b4423ddcfMike Stump}
3660adaaad3715c9c26cdcfdfe3401a13d7b4423ddcfMike Stump
36614ba2a17694148e16eaa8d3917f657ffcd3667be4Jay FoadQualType ASTContext::getBlockDescriptorExtendedType() const {
3662083c25eea14bb4cc4ecc3ec763c60e2e609e22bdMike Stump  if (BlockDescriptorExtendedType)
3663083c25eea14bb4cc4ecc3ec763c60e2e609e22bdMike Stump    return getTagDeclType(BlockDescriptorExtendedType);
3664083c25eea14bb4cc4ecc3ec763c60e2e609e22bdMike Stump
3665083c25eea14bb4cc4ecc3ec763c60e2e609e22bdMike Stump  RecordDecl *T;
3666083c25eea14bb4cc4ecc3ec763c60e2e609e22bdMike Stump  // FIXME: Needs the FlagAppleBlock bit.
3667ba877adeb49ed6dc17f27fa3a3bcd0cca713fd68Abramo Bagnara  T = CreateRecordDecl(*this, TTK_Struct, TUDecl,
366879cbc7dd2aacd85a28f469b5dc73c4ea296e7072Anders Carlsson                       &Idents.get("__block_descriptor_withcopydispose"));
36695cfa011e61e14e6f2e1659047d809706c0e4c6a3John McCall  T->startDefinition();
3670083c25eea14bb4cc4ecc3ec763c60e2e609e22bdMike Stump
3671083c25eea14bb4cc4ecc3ec763c60e2e609e22bdMike Stump  QualType FieldTypes[] = {
3672083c25eea14bb4cc4ecc3ec763c60e2e609e22bdMike Stump    UnsignedLongTy,
3673083c25eea14bb4cc4ecc3ec763c60e2e609e22bdMike Stump    UnsignedLongTy,
3674083c25eea14bb4cc4ecc3ec763c60e2e609e22bdMike Stump    getPointerType(VoidPtrTy),
3675083c25eea14bb4cc4ecc3ec763c60e2e609e22bdMike Stump    getPointerType(VoidPtrTy)
3676083c25eea14bb4cc4ecc3ec763c60e2e609e22bdMike Stump  };
3677083c25eea14bb4cc4ecc3ec763c60e2e609e22bdMike Stump
3678083c25eea14bb4cc4ecc3ec763c60e2e609e22bdMike Stump  const char *FieldNames[] = {
3679083c25eea14bb4cc4ecc3ec763c60e2e609e22bdMike Stump    "reserved",
3680083c25eea14bb4cc4ecc3ec763c60e2e609e22bdMike Stump    "Size",
3681083c25eea14bb4cc4ecc3ec763c60e2e609e22bdMike Stump    "CopyFuncPtr",
3682083c25eea14bb4cc4ecc3ec763c60e2e609e22bdMike Stump    "DestroyFuncPtr"
3683083c25eea14bb4cc4ecc3ec763c60e2e609e22bdMike Stump  };
3684083c25eea14bb4cc4ecc3ec763c60e2e609e22bdMike Stump
3685083c25eea14bb4cc4ecc3ec763c60e2e609e22bdMike Stump  for (size_t i = 0; i < 4; ++i) {
3686ff676cb48fe8bf7be2feaa251dc7c5fb15af4730Abramo Bagnara    FieldDecl *Field = FieldDecl::Create(*this, T, SourceLocation(),
3687083c25eea14bb4cc4ecc3ec763c60e2e609e22bdMike Stump                                         SourceLocation(),
3688083c25eea14bb4cc4ecc3ec763c60e2e609e22bdMike Stump                                         &Idents.get(FieldNames[i]),
3689a93c934af4fbf97cbe8e649d82e68ccacfe57c95John McCall                                         FieldTypes[i], /*TInfo=*/0,
3690083c25eea14bb4cc4ecc3ec763c60e2e609e22bdMike Stump                                         /*BitWidth=*/0,
3691083c25eea14bb4cc4ecc3ec763c60e2e609e22bdMike Stump                                         /*Mutable=*/false);
36922888b65aae768f54062505330df7be230a0510c7John McCall    Field->setAccess(AS_public);
3693083c25eea14bb4cc4ecc3ec763c60e2e609e22bdMike Stump    T->addDecl(Field);
3694083c25eea14bb4cc4ecc3ec763c60e2e609e22bdMike Stump  }
3695083c25eea14bb4cc4ecc3ec763c60e2e609e22bdMike Stump
3696838db383b69b9fb55f55c8e9546477df198a4faaDouglas Gregor  T->completeDefinition();
3697083c25eea14bb4cc4ecc3ec763c60e2e609e22bdMike Stump
3698083c25eea14bb4cc4ecc3ec763c60e2e609e22bdMike Stump  BlockDescriptorExtendedType = T;
3699083c25eea14bb4cc4ecc3ec763c60e2e609e22bdMike Stump
3700083c25eea14bb4cc4ecc3ec763c60e2e609e22bdMike Stump  return getTagDeclType(BlockDescriptorExtendedType);
3701083c25eea14bb4cc4ecc3ec763c60e2e609e22bdMike Stump}
3702083c25eea14bb4cc4ecc3ec763c60e2e609e22bdMike Stump
3703083c25eea14bb4cc4ecc3ec763c60e2e609e22bdMike Stumpvoid ASTContext::setBlockDescriptorExtendedType(QualType T) {
3704083c25eea14bb4cc4ecc3ec763c60e2e609e22bdMike Stump  const RecordType *Rec = T->getAs<RecordType>();
3705083c25eea14bb4cc4ecc3ec763c60e2e609e22bdMike Stump  assert(Rec && "Invalid BlockDescriptorType");
3706083c25eea14bb4cc4ecc3ec763c60e2e609e22bdMike Stump  BlockDescriptorExtendedType = Rec->getDecl();
3707083c25eea14bb4cc4ecc3ec763c60e2e609e22bdMike Stump}
3708083c25eea14bb4cc4ecc3ec763c60e2e609e22bdMike Stump
37094ba2a17694148e16eaa8d3917f657ffcd3667be4Jay Foadbool ASTContext::BlockRequiresCopying(QualType Ty) const {
3710af7b44d847d3e44c43346d508b2e55a6254b6e9dMike Stump  if (Ty->isBlockPointerType())
3711af7b44d847d3e44c43346d508b2e55a6254b6e9dMike Stump    return true;
3712af7b44d847d3e44c43346d508b2e55a6254b6e9dMike Stump  if (isObjCNSObjectType(Ty))
3713af7b44d847d3e44c43346d508b2e55a6254b6e9dMike Stump    return true;
3714af7b44d847d3e44c43346d508b2e55a6254b6e9dMike Stump  if (Ty->isObjCObjectPointerType())
3715af7b44d847d3e44c43346d508b2e55a6254b6e9dMike Stump    return true;
3716e38be617437ccdcc180c5a49e447cbcd07539292Fariborz Jahanian  if (getLangOptions().CPlusPlus) {
3717e38be617437ccdcc180c5a49e447cbcd07539292Fariborz Jahanian    if (const RecordType *RT = Ty->getAs<RecordType>()) {
3718e38be617437ccdcc180c5a49e447cbcd07539292Fariborz Jahanian      CXXRecordDecl *RD = cast<CXXRecordDecl>(RT->getDecl());
3719e38be617437ccdcc180c5a49e447cbcd07539292Fariborz Jahanian      return RD->hasConstCopyConstructor(*this);
3720e38be617437ccdcc180c5a49e447cbcd07539292Fariborz Jahanian
3721e38be617437ccdcc180c5a49e447cbcd07539292Fariborz Jahanian    }
3722e38be617437ccdcc180c5a49e447cbcd07539292Fariborz Jahanian  }
3723af7b44d847d3e44c43346d508b2e55a6254b6e9dMike Stump  return false;
3724af7b44d847d3e44c43346d508b2e55a6254b6e9dMike Stump}
3725af7b44d847d3e44c43346d508b2e55a6254b6e9dMike Stump
37264ba2a17694148e16eaa8d3917f657ffcd3667be4Jay FoadQualType
37274ba2a17694148e16eaa8d3917f657ffcd3667be4Jay FoadASTContext::BuildByRefType(llvm::StringRef DeclName, QualType Ty) const {
3728af7b44d847d3e44c43346d508b2e55a6254b6e9dMike Stump  //  type = struct __Block_byref_1_X {
3729ea26cb522e88fc86b0d1cae61dcefcfe4cc20231Mike Stump  //    void *__isa;
3730af7b44d847d3e44c43346d508b2e55a6254b6e9dMike Stump  //    struct __Block_byref_1_X *__forwarding;
3731ea26cb522e88fc86b0d1cae61dcefcfe4cc20231Mike Stump  //    unsigned int __flags;
3732ea26cb522e88fc86b0d1cae61dcefcfe4cc20231Mike Stump  //    unsigned int __size;
3733a7e6845660f91ec611427e1db842780e1ec12bdbEli Friedman  //    void *__copy_helper;            // as needed
3734a7e6845660f91ec611427e1db842780e1ec12bdbEli Friedman  //    void *__destroy_help            // as needed
3735af7b44d847d3e44c43346d508b2e55a6254b6e9dMike Stump  //    int X;
3736ea26cb522e88fc86b0d1cae61dcefcfe4cc20231Mike Stump  //  } *
3737ea26cb522e88fc86b0d1cae61dcefcfe4cc20231Mike Stump
3738af7b44d847d3e44c43346d508b2e55a6254b6e9dMike Stump  bool HasCopyAndDispose = BlockRequiresCopying(Ty);
3739af7b44d847d3e44c43346d508b2e55a6254b6e9dMike Stump
3740af7b44d847d3e44c43346d508b2e55a6254b6e9dMike Stump  // FIXME: Move up
3741f5942a44880be26878592eb052b737579349411eBenjamin Kramer  llvm::SmallString<36> Name;
3742f5942a44880be26878592eb052b737579349411eBenjamin Kramer  llvm::raw_svector_ostream(Name) << "__Block_byref_" <<
3743f5942a44880be26878592eb052b737579349411eBenjamin Kramer                                  ++UniqueBlockByRefTypeID << '_' << DeclName;
3744af7b44d847d3e44c43346d508b2e55a6254b6e9dMike Stump  RecordDecl *T;
3745ba877adeb49ed6dc17f27fa3a3bcd0cca713fd68Abramo Bagnara  T = CreateRecordDecl(*this, TTK_Struct, TUDecl, &Idents.get(Name.str()));
3746af7b44d847d3e44c43346d508b2e55a6254b6e9dMike Stump  T->startDefinition();
3747af7b44d847d3e44c43346d508b2e55a6254b6e9dMike Stump  QualType Int32Ty = IntTy;
3748af7b44d847d3e44c43346d508b2e55a6254b6e9dMike Stump  assert(getIntWidth(IntTy) == 32 && "non-32bit int not supported");
3749af7b44d847d3e44c43346d508b2e55a6254b6e9dMike Stump  QualType FieldTypes[] = {
3750af7b44d847d3e44c43346d508b2e55a6254b6e9dMike Stump    getPointerType(VoidPtrTy),
3751af7b44d847d3e44c43346d508b2e55a6254b6e9dMike Stump    getPointerType(getTagDeclType(T)),
3752af7b44d847d3e44c43346d508b2e55a6254b6e9dMike Stump    Int32Ty,
3753af7b44d847d3e44c43346d508b2e55a6254b6e9dMike Stump    Int32Ty,
3754af7b44d847d3e44c43346d508b2e55a6254b6e9dMike Stump    getPointerType(VoidPtrTy),
3755af7b44d847d3e44c43346d508b2e55a6254b6e9dMike Stump    getPointerType(VoidPtrTy),
3756af7b44d847d3e44c43346d508b2e55a6254b6e9dMike Stump    Ty
3757af7b44d847d3e44c43346d508b2e55a6254b6e9dMike Stump  };
3758af7b44d847d3e44c43346d508b2e55a6254b6e9dMike Stump
37594087f27e5416c799bcb6be072f905be752acb61cDaniel Dunbar  llvm::StringRef FieldNames[] = {
3760af7b44d847d3e44c43346d508b2e55a6254b6e9dMike Stump    "__isa",
3761af7b44d847d3e44c43346d508b2e55a6254b6e9dMike Stump    "__forwarding",
3762af7b44d847d3e44c43346d508b2e55a6254b6e9dMike Stump    "__flags",
3763af7b44d847d3e44c43346d508b2e55a6254b6e9dMike Stump    "__size",
3764af7b44d847d3e44c43346d508b2e55a6254b6e9dMike Stump    "__copy_helper",
3765af7b44d847d3e44c43346d508b2e55a6254b6e9dMike Stump    "__destroy_helper",
3766af7b44d847d3e44c43346d508b2e55a6254b6e9dMike Stump    DeclName,
3767af7b44d847d3e44c43346d508b2e55a6254b6e9dMike Stump  };
3768af7b44d847d3e44c43346d508b2e55a6254b6e9dMike Stump
3769af7b44d847d3e44c43346d508b2e55a6254b6e9dMike Stump  for (size_t i = 0; i < 7; ++i) {
3770af7b44d847d3e44c43346d508b2e55a6254b6e9dMike Stump    if (!HasCopyAndDispose && i >=4 && i <= 5)
3771af7b44d847d3e44c43346d508b2e55a6254b6e9dMike Stump      continue;
3772af7b44d847d3e44c43346d508b2e55a6254b6e9dMike Stump    FieldDecl *Field = FieldDecl::Create(*this, T, SourceLocation(),
3773ff676cb48fe8bf7be2feaa251dc7c5fb15af4730Abramo Bagnara                                         SourceLocation(),
3774af7b44d847d3e44c43346d508b2e55a6254b6e9dMike Stump                                         &Idents.get(FieldNames[i]),
3775a93c934af4fbf97cbe8e649d82e68ccacfe57c95John McCall                                         FieldTypes[i], /*TInfo=*/0,
3776af7b44d847d3e44c43346d508b2e55a6254b6e9dMike Stump                                         /*BitWidth=*/0, /*Mutable=*/false);
37772888b65aae768f54062505330df7be230a0510c7John McCall    Field->setAccess(AS_public);
3778af7b44d847d3e44c43346d508b2e55a6254b6e9dMike Stump    T->addDecl(Field);
3779af7b44d847d3e44c43346d508b2e55a6254b6e9dMike Stump  }
3780af7b44d847d3e44c43346d508b2e55a6254b6e9dMike Stump
3781838db383b69b9fb55f55c8e9546477df198a4faaDouglas Gregor  T->completeDefinition();
3782af7b44d847d3e44c43346d508b2e55a6254b6e9dMike Stump
3783af7b44d847d3e44c43346d508b2e55a6254b6e9dMike Stump  return getPointerType(getTagDeclType(T));
3784ea26cb522e88fc86b0d1cae61dcefcfe4cc20231Mike Stump}
3785ea26cb522e88fc86b0d1cae61dcefcfe4cc20231Mike Stump
3786319ac896a0fef7365d5589b8021db7e41207fe42Douglas Gregorvoid ASTContext::setObjCFastEnumerationStateType(QualType T) {
37876217b80b7a1379b74cced1c076338262c3c980b3Ted Kremenek  const RecordType *Rec = T->getAs<RecordType>();
3788319ac896a0fef7365d5589b8021db7e41207fe42Douglas Gregor  assert(Rec && "Invalid ObjCFAstEnumerationStateType");
3789319ac896a0fef7365d5589b8021db7e41207fe42Douglas Gregor  ObjCFastEnumerationStateTypeDecl = Rec->getDecl();
3790319ac896a0fef7365d5589b8021db7e41207fe42Douglas Gregor}
3791319ac896a0fef7365d5589b8021db7e41207fe42Douglas Gregor
3792e8c49533521c40643653f943d47229e62d277f88Anders Carlsson// This returns true if a type has been typedefed to BOOL:
3793e8c49533521c40643653f943d47229e62d277f88Anders Carlsson// typedef <type> BOOL;
37942d99833e8c956775f2183601cd120b65b569c867Chris Lattnerstatic bool isTypeTypedefedAsBOOL(QualType T) {
3795e8c49533521c40643653f943d47229e62d277f88Anders Carlsson  if (const TypedefType *TT = dyn_cast<TypedefType>(T))
3796bb49c3ee5d270485f4b273691fd14bc97403fa5dChris Lattner    if (IdentifierInfo *II = TT->getDecl()->getIdentifier())
3797bb49c3ee5d270485f4b273691fd14bc97403fa5dChris Lattner      return II->isStr("BOOL");
37981eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
379985f9bceab1542aafff012d4d28e998f4ba16e362Anders Carlsson  return false;
380085f9bceab1542aafff012d4d28e998f4ba16e362Anders Carlsson}
380185f9bceab1542aafff012d4d28e998f4ba16e362Anders Carlsson
3802a526c5c67e5a0473c340903ee542ce570119665fTed Kremenek/// getObjCEncodingTypeSize returns size of type for objective-c encoding
380333e1d64ab5cd5d27f8530ccd056191fe2c9f3f2eFariborz Jahanian/// purpose.
38044ba2a17694148e16eaa8d3917f657ffcd3667be4Jay FoadCharUnits ASTContext::getObjCEncodingTypeSize(QualType type) const {
3805199c3d6cd16aebbb9c7f0d42af9d922c9628bf70Ken Dyck  CharUnits sz = getTypeSizeInChars(type);
38061eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
380733e1d64ab5cd5d27f8530ccd056191fe2c9f3f2eFariborz Jahanian  // Make all integer and enum types at least as large as an int
38082ade35e2cfd554e49d35a52047cea98a82787af9Douglas Gregor  if (sz.isPositive() && type->isIntegralOrEnumerationType())
3809199c3d6cd16aebbb9c7f0d42af9d922c9628bf70Ken Dyck    sz = std::max(sz, getTypeSizeInChars(IntTy));
381033e1d64ab5cd5d27f8530ccd056191fe2c9f3f2eFariborz Jahanian  // Treat arrays as pointers, since that's how they're passed in.
381133e1d64ab5cd5d27f8530ccd056191fe2c9f3f2eFariborz Jahanian  else if (type->isArrayType())
3812199c3d6cd16aebbb9c7f0d42af9d922c9628bf70Ken Dyck    sz = getTypeSizeInChars(VoidPtrTy);
3813aa8741a1db98eef05f09b1200dba94aa5dc3bc3dKen Dyck  return sz;
3814199c3d6cd16aebbb9c7f0d42af9d922c9628bf70Ken Dyck}
3815199c3d6cd16aebbb9c7f0d42af9d922c9628bf70Ken Dyck
3816199c3d6cd16aebbb9c7f0d42af9d922c9628bf70Ken Dyckstatic inline
3817199c3d6cd16aebbb9c7f0d42af9d922c9628bf70Ken Dyckstd::string charUnitsToString(const CharUnits &CU) {
3818199c3d6cd16aebbb9c7f0d42af9d922c9628bf70Ken Dyck  return llvm::itostr(CU.getQuantity());
381933e1d64ab5cd5d27f8530ccd056191fe2c9f3f2eFariborz Jahanian}
382033e1d64ab5cd5d27f8530ccd056191fe2c9f3f2eFariborz Jahanian
38216b5a61b6dc400027fd793dcadceeb9da944a37eaJohn McCall/// getObjCEncodingForBlock - Return the encoded type for this block
38225e530af5d51572a0ed5dbe50da54bd333840c63dDavid Chisnall/// declaration.
38236b5a61b6dc400027fd793dcadceeb9da944a37eaJohn McCallstd::string ASTContext::getObjCEncodingForBlock(const BlockExpr *Expr) const {
38246b5a61b6dc400027fd793dcadceeb9da944a37eaJohn McCall  std::string S;
38256b5a61b6dc400027fd793dcadceeb9da944a37eaJohn McCall
38265e530af5d51572a0ed5dbe50da54bd333840c63dDavid Chisnall  const BlockDecl *Decl = Expr->getBlockDecl();
38275e530af5d51572a0ed5dbe50da54bd333840c63dDavid Chisnall  QualType BlockTy =
38285e530af5d51572a0ed5dbe50da54bd333840c63dDavid Chisnall      Expr->getType()->getAs<BlockPointerType>()->getPointeeType();
38295e530af5d51572a0ed5dbe50da54bd333840c63dDavid Chisnall  // Encode result type.
3830c71a4915ca216847599d03cab4ed1c5086b0eb43John McCall  getObjCEncodingForType(BlockTy->getAs<FunctionType>()->getResultType(), S);
38315e530af5d51572a0ed5dbe50da54bd333840c63dDavid Chisnall  // Compute size of all parameters.
38325e530af5d51572a0ed5dbe50da54bd333840c63dDavid Chisnall  // Start with computing size of a pointer in number of bytes.
38335e530af5d51572a0ed5dbe50da54bd333840c63dDavid Chisnall  // FIXME: There might(should) be a better way of doing this computation!
38345e530af5d51572a0ed5dbe50da54bd333840c63dDavid Chisnall  SourceLocation Loc;
3835199c3d6cd16aebbb9c7f0d42af9d922c9628bf70Ken Dyck  CharUnits PtrSize = getTypeSizeInChars(VoidPtrTy);
3836199c3d6cd16aebbb9c7f0d42af9d922c9628bf70Ken Dyck  CharUnits ParmOffset = PtrSize;
38376f46c2653c1545cc3fef0c0df996d18160160ce8Fariborz Jahanian  for (BlockDecl::param_const_iterator PI = Decl->param_begin(),
38385e530af5d51572a0ed5dbe50da54bd333840c63dDavid Chisnall       E = Decl->param_end(); PI != E; ++PI) {
38395e530af5d51572a0ed5dbe50da54bd333840c63dDavid Chisnall    QualType PType = (*PI)->getType();
3840aa8741a1db98eef05f09b1200dba94aa5dc3bc3dKen Dyck    CharUnits sz = getObjCEncodingTypeSize(PType);
3841199c3d6cd16aebbb9c7f0d42af9d922c9628bf70Ken Dyck    assert (sz.isPositive() && "BlockExpr - Incomplete param type");
38425e530af5d51572a0ed5dbe50da54bd333840c63dDavid Chisnall    ParmOffset += sz;
38435e530af5d51572a0ed5dbe50da54bd333840c63dDavid Chisnall  }
38445e530af5d51572a0ed5dbe50da54bd333840c63dDavid Chisnall  // Size of the argument frame
3845199c3d6cd16aebbb9c7f0d42af9d922c9628bf70Ken Dyck  S += charUnitsToString(ParmOffset);
38465e530af5d51572a0ed5dbe50da54bd333840c63dDavid Chisnall  // Block pointer and offset.
38475e530af5d51572a0ed5dbe50da54bd333840c63dDavid Chisnall  S += "@?0";
38485e530af5d51572a0ed5dbe50da54bd333840c63dDavid Chisnall  ParmOffset = PtrSize;
38495e530af5d51572a0ed5dbe50da54bd333840c63dDavid Chisnall
38505e530af5d51572a0ed5dbe50da54bd333840c63dDavid Chisnall  // Argument types.
38515e530af5d51572a0ed5dbe50da54bd333840c63dDavid Chisnall  ParmOffset = PtrSize;
38525e530af5d51572a0ed5dbe50da54bd333840c63dDavid Chisnall  for (BlockDecl::param_const_iterator PI = Decl->param_begin(), E =
38535e530af5d51572a0ed5dbe50da54bd333840c63dDavid Chisnall       Decl->param_end(); PI != E; ++PI) {
38545e530af5d51572a0ed5dbe50da54bd333840c63dDavid Chisnall    ParmVarDecl *PVDecl = *PI;
38555e530af5d51572a0ed5dbe50da54bd333840c63dDavid Chisnall    QualType PType = PVDecl->getOriginalType();
38565e530af5d51572a0ed5dbe50da54bd333840c63dDavid Chisnall    if (const ArrayType *AT =
38575e530af5d51572a0ed5dbe50da54bd333840c63dDavid Chisnall          dyn_cast<ArrayType>(PType->getCanonicalTypeInternal())) {
38585e530af5d51572a0ed5dbe50da54bd333840c63dDavid Chisnall      // Use array's original type only if it has known number of
38595389f48b24937ad7b4093307128b3cbf25235654David Chisnall      // elements.
38605389f48b24937ad7b4093307128b3cbf25235654David Chisnall      if (!isa<ConstantArrayType>(AT))
38615389f48b24937ad7b4093307128b3cbf25235654David Chisnall        PType = PVDecl->getType();
38625389f48b24937ad7b4093307128b3cbf25235654David Chisnall    } else if (PType->isFunctionType())
38635389f48b24937ad7b4093307128b3cbf25235654David Chisnall      PType = PVDecl->getType();
38645389f48b24937ad7b4093307128b3cbf25235654David Chisnall    getObjCEncodingForType(PType, S);
38655389f48b24937ad7b4093307128b3cbf25235654David Chisnall    S += charUnitsToString(ParmOffset);
38665389f48b24937ad7b4093307128b3cbf25235654David Chisnall    ParmOffset += getObjCEncodingTypeSize(PType);
38675389f48b24937ad7b4093307128b3cbf25235654David Chisnall  }
38686b5a61b6dc400027fd793dcadceeb9da944a37eaJohn McCall
38696b5a61b6dc400027fd793dcadceeb9da944a37eaJohn McCall  return S;
38705389f48b24937ad7b4093307128b3cbf25235654David Chisnall}
38715389f48b24937ad7b4093307128b3cbf25235654David Chisnall
38725389f48b24937ad7b4093307128b3cbf25235654David Chisnallvoid ASTContext::getObjCEncodingForFunctionDecl(const FunctionDecl *Decl,
38735389f48b24937ad7b4093307128b3cbf25235654David Chisnall                                                std::string& S) {
38745389f48b24937ad7b4093307128b3cbf25235654David Chisnall  // Encode result type.
38755389f48b24937ad7b4093307128b3cbf25235654David Chisnall  getObjCEncodingForType(Decl->getResultType(), S);
38765389f48b24937ad7b4093307128b3cbf25235654David Chisnall  CharUnits ParmOffset;
38775389f48b24937ad7b4093307128b3cbf25235654David Chisnall  // Compute size of all parameters.
38785389f48b24937ad7b4093307128b3cbf25235654David Chisnall  for (FunctionDecl::param_const_iterator PI = Decl->param_begin(),
38795389f48b24937ad7b4093307128b3cbf25235654David Chisnall       E = Decl->param_end(); PI != E; ++PI) {
38805389f48b24937ad7b4093307128b3cbf25235654David Chisnall    QualType PType = (*PI)->getType();
38815389f48b24937ad7b4093307128b3cbf25235654David Chisnall    CharUnits sz = getObjCEncodingTypeSize(PType);
38825389f48b24937ad7b4093307128b3cbf25235654David Chisnall    assert (sz.isPositive() &&
38835389f48b24937ad7b4093307128b3cbf25235654David Chisnall        "getObjCEncodingForMethodDecl - Incomplete param type");
38845389f48b24937ad7b4093307128b3cbf25235654David Chisnall    ParmOffset += sz;
38855389f48b24937ad7b4093307128b3cbf25235654David Chisnall  }
38865389f48b24937ad7b4093307128b3cbf25235654David Chisnall  S += charUnitsToString(ParmOffset);
38875389f48b24937ad7b4093307128b3cbf25235654David Chisnall  ParmOffset = CharUnits::Zero();
38885389f48b24937ad7b4093307128b3cbf25235654David Chisnall
38895389f48b24937ad7b4093307128b3cbf25235654David Chisnall  // Argument types.
38905389f48b24937ad7b4093307128b3cbf25235654David Chisnall  for (FunctionDecl::param_const_iterator PI = Decl->param_begin(),
38915389f48b24937ad7b4093307128b3cbf25235654David Chisnall       E = Decl->param_end(); PI != E; ++PI) {
38925389f48b24937ad7b4093307128b3cbf25235654David Chisnall    ParmVarDecl *PVDecl = *PI;
38935389f48b24937ad7b4093307128b3cbf25235654David Chisnall    QualType PType = PVDecl->getOriginalType();
38945389f48b24937ad7b4093307128b3cbf25235654David Chisnall    if (const ArrayType *AT =
38955389f48b24937ad7b4093307128b3cbf25235654David Chisnall          dyn_cast<ArrayType>(PType->getCanonicalTypeInternal())) {
38965389f48b24937ad7b4093307128b3cbf25235654David Chisnall      // Use array's original type only if it has known number of
38975e530af5d51572a0ed5dbe50da54bd333840c63dDavid Chisnall      // elements.
38985e530af5d51572a0ed5dbe50da54bd333840c63dDavid Chisnall      if (!isa<ConstantArrayType>(AT))
38995e530af5d51572a0ed5dbe50da54bd333840c63dDavid Chisnall        PType = PVDecl->getType();
39005e530af5d51572a0ed5dbe50da54bd333840c63dDavid Chisnall    } else if (PType->isFunctionType())
39015e530af5d51572a0ed5dbe50da54bd333840c63dDavid Chisnall      PType = PVDecl->getType();
39025e530af5d51572a0ed5dbe50da54bd333840c63dDavid Chisnall    getObjCEncodingForType(PType, S);
3903199c3d6cd16aebbb9c7f0d42af9d922c9628bf70Ken Dyck    S += charUnitsToString(ParmOffset);
3904aa8741a1db98eef05f09b1200dba94aa5dc3bc3dKen Dyck    ParmOffset += getObjCEncodingTypeSize(PType);
39055e530af5d51572a0ed5dbe50da54bd333840c63dDavid Chisnall  }
39065e530af5d51572a0ed5dbe50da54bd333840c63dDavid Chisnall}
39075e530af5d51572a0ed5dbe50da54bd333840c63dDavid Chisnall
3908a526c5c67e5a0473c340903ee542ce570119665fTed Kremenek/// getObjCEncodingForMethodDecl - Return the encoded type for this method
390933e1d64ab5cd5d27f8530ccd056191fe2c9f3f2eFariborz Jahanian/// declaration.
39101eb4433ac451dc16f4133a88af2d002ac26c58efMike Stumpvoid ASTContext::getObjCEncodingForMethodDecl(const ObjCMethodDecl *Decl,
39114ba2a17694148e16eaa8d3917f657ffcd3667be4Jay Foad                                              std::string& S) const {
3912c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar  // FIXME: This is not very efficient.
3913ecb01e666665efabd2aa76a76f6080e2a78965faFariborz Jahanian  // Encode type qualifer, 'in', 'inout', etc. for the return type.
3914a526c5c67e5a0473c340903ee542ce570119665fTed Kremenek  getObjCEncodingForTypeQualifier(Decl->getObjCDeclQualifier(), S);
391533e1d64ab5cd5d27f8530ccd056191fe2c9f3f2eFariborz Jahanian  // Encode result type.
39160d504c1da852e58ff802545c823ecff3b6c654b8Daniel Dunbar  getObjCEncodingForType(Decl->getResultType(), S);
391733e1d64ab5cd5d27f8530ccd056191fe2c9f3f2eFariborz Jahanian  // Compute size of all parameters.
391833e1d64ab5cd5d27f8530ccd056191fe2c9f3f2eFariborz Jahanian  // Start with computing size of a pointer in number of bytes.
391933e1d64ab5cd5d27f8530ccd056191fe2c9f3f2eFariborz Jahanian  // FIXME: There might(should) be a better way of doing this computation!
392033e1d64ab5cd5d27f8530ccd056191fe2c9f3f2eFariborz Jahanian  SourceLocation Loc;
3921199c3d6cd16aebbb9c7f0d42af9d922c9628bf70Ken Dyck  CharUnits PtrSize = getTypeSizeInChars(VoidPtrTy);
392233e1d64ab5cd5d27f8530ccd056191fe2c9f3f2eFariborz Jahanian  // The first two arguments (self and _cmd) are pointers; account for
392333e1d64ab5cd5d27f8530ccd056191fe2c9f3f2eFariborz Jahanian  // their size.
3924199c3d6cd16aebbb9c7f0d42af9d922c9628bf70Ken Dyck  CharUnits ParmOffset = 2 * PtrSize;
392589951a86b594513c2a013532ed45d197413b1087Chris Lattner  for (ObjCMethodDecl::param_iterator PI = Decl->param_begin(),
39267732cc9c0fdc97a2f8cce4e5933d8103213d1aefFariborz Jahanian       E = Decl->sel_param_end(); PI != E; ++PI) {
392789951a86b594513c2a013532ed45d197413b1087Chris Lattner    QualType PType = (*PI)->getType();
3928aa8741a1db98eef05f09b1200dba94aa5dc3bc3dKen Dyck    CharUnits sz = getObjCEncodingTypeSize(PType);
3929199c3d6cd16aebbb9c7f0d42af9d922c9628bf70Ken Dyck    assert (sz.isPositive() &&
3930199c3d6cd16aebbb9c7f0d42af9d922c9628bf70Ken Dyck        "getObjCEncodingForMethodDecl - Incomplete param type");
393133e1d64ab5cd5d27f8530ccd056191fe2c9f3f2eFariborz Jahanian    ParmOffset += sz;
393233e1d64ab5cd5d27f8530ccd056191fe2c9f3f2eFariborz Jahanian  }
3933199c3d6cd16aebbb9c7f0d42af9d922c9628bf70Ken Dyck  S += charUnitsToString(ParmOffset);
393433e1d64ab5cd5d27f8530ccd056191fe2c9f3f2eFariborz Jahanian  S += "@0:";
3935199c3d6cd16aebbb9c7f0d42af9d922c9628bf70Ken Dyck  S += charUnitsToString(PtrSize);
39361eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
393733e1d64ab5cd5d27f8530ccd056191fe2c9f3f2eFariborz Jahanian  // Argument types.
393833e1d64ab5cd5d27f8530ccd056191fe2c9f3f2eFariborz Jahanian  ParmOffset = 2 * PtrSize;
393989951a86b594513c2a013532ed45d197413b1087Chris Lattner  for (ObjCMethodDecl::param_iterator PI = Decl->param_begin(),
39407732cc9c0fdc97a2f8cce4e5933d8103213d1aefFariborz Jahanian       E = Decl->sel_param_end(); PI != E; ++PI) {
394189951a86b594513c2a013532ed45d197413b1087Chris Lattner    ParmVarDecl *PVDecl = *PI;
39421eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    QualType PType = PVDecl->getOriginalType();
39434306d3cb9116605728252e2738df24b9f6ab53c3Fariborz Jahanian    if (const ArrayType *AT =
3944ab76d45e023fc5ae966968344e180cd09fdcc746Steve Naroff          dyn_cast<ArrayType>(PType->getCanonicalTypeInternal())) {
3945ab76d45e023fc5ae966968344e180cd09fdcc746Steve Naroff      // Use array's original type only if it has known number of
3946ab76d45e023fc5ae966968344e180cd09fdcc746Steve Naroff      // elements.
3947bb3fde337fb712c0e6da8790d431621be4793048Steve Naroff      if (!isa<ConstantArrayType>(AT))
3948ab76d45e023fc5ae966968344e180cd09fdcc746Steve Naroff        PType = PVDecl->getType();
3949ab76d45e023fc5ae966968344e180cd09fdcc746Steve Naroff    } else if (PType->isFunctionType())
3950ab76d45e023fc5ae966968344e180cd09fdcc746Steve Naroff      PType = PVDecl->getType();
3951ecb01e666665efabd2aa76a76f6080e2a78965faFariborz Jahanian    // Process argument qualifiers for user supplied arguments; such as,
395233e1d64ab5cd5d27f8530ccd056191fe2c9f3f2eFariborz Jahanian    // 'in', 'inout', etc.
39534306d3cb9116605728252e2738df24b9f6ab53c3Fariborz Jahanian    getObjCEncodingForTypeQualifier(PVDecl->getObjCDeclQualifier(), S);
39540d504c1da852e58ff802545c823ecff3b6c654b8Daniel Dunbar    getObjCEncodingForType(PType, S);
3955199c3d6cd16aebbb9c7f0d42af9d922c9628bf70Ken Dyck    S += charUnitsToString(ParmOffset);
3956aa8741a1db98eef05f09b1200dba94aa5dc3bc3dKen Dyck    ParmOffset += getObjCEncodingTypeSize(PType);
395733e1d64ab5cd5d27f8530ccd056191fe2c9f3f2eFariborz Jahanian  }
395833e1d64ab5cd5d27f8530ccd056191fe2c9f3f2eFariborz Jahanian}
395933e1d64ab5cd5d27f8530ccd056191fe2c9f3f2eFariborz Jahanian
3960c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar/// getObjCEncodingForPropertyDecl - Return the encoded type for this
396183bccb85ff4b9981c4250c45494b439df8cbf983Fariborz Jahanian/// property declaration. If non-NULL, Container must be either an
3962c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar/// ObjCCategoryImplDecl or ObjCImplementationDecl; it should only be
3963c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar/// NULL when getting encodings for protocol properties.
39641eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// Property attributes are stored as a comma-delimited C string. The simple
39651eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// attributes readonly and bycopy are encoded as single characters. The
39661eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// parametrized attributes, getter=name, setter=name, and ivar=name, are
39671eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// encoded as single characters, followed by an identifier. Property types
39681eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// are also encoded as a parametrized attribute. The characters used to encode
396983bccb85ff4b9981c4250c45494b439df8cbf983Fariborz Jahanian/// these attributes are defined by the following enumeration:
397083bccb85ff4b9981c4250c45494b439df8cbf983Fariborz Jahanian/// @code
397183bccb85ff4b9981c4250c45494b439df8cbf983Fariborz Jahanian/// enum PropertyAttributes {
397283bccb85ff4b9981c4250c45494b439df8cbf983Fariborz Jahanian/// kPropertyReadOnly = 'R',   // property is read-only.
397383bccb85ff4b9981c4250c45494b439df8cbf983Fariborz Jahanian/// kPropertyBycopy = 'C',     // property is a copy of the value last assigned
397483bccb85ff4b9981c4250c45494b439df8cbf983Fariborz Jahanian/// kPropertyByref = '&',  // property is a reference to the value last assigned
397583bccb85ff4b9981c4250c45494b439df8cbf983Fariborz Jahanian/// kPropertyDynamic = 'D',    // property is dynamic
397683bccb85ff4b9981c4250c45494b439df8cbf983Fariborz Jahanian/// kPropertyGetter = 'G',     // followed by getter selector name
397783bccb85ff4b9981c4250c45494b439df8cbf983Fariborz Jahanian/// kPropertySetter = 'S',     // followed by setter selector name
397883bccb85ff4b9981c4250c45494b439df8cbf983Fariborz Jahanian/// kPropertyInstanceVariable = 'V'  // followed by instance variable  name
397983bccb85ff4b9981c4250c45494b439df8cbf983Fariborz Jahanian/// kPropertyType = 't'              // followed by old-style type encoding.
398083bccb85ff4b9981c4250c45494b439df8cbf983Fariborz Jahanian/// kPropertyWeak = 'W'              // 'weak' property
398183bccb85ff4b9981c4250c45494b439df8cbf983Fariborz Jahanian/// kPropertyStrong = 'P'            // property GC'able
398283bccb85ff4b9981c4250c45494b439df8cbf983Fariborz Jahanian/// kPropertyNonAtomic = 'N'         // property non-atomic
398383bccb85ff4b9981c4250c45494b439df8cbf983Fariborz Jahanian/// };
398483bccb85ff4b9981c4250c45494b439df8cbf983Fariborz Jahanian/// @endcode
39851eb4433ac451dc16f4133a88af2d002ac26c58efMike Stumpvoid ASTContext::getObjCEncodingForPropertyDecl(const ObjCPropertyDecl *PD,
3986c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar                                                const Decl *Container,
39874ba2a17694148e16eaa8d3917f657ffcd3667be4Jay Foad                                                std::string& S) const {
3988c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar  // Collect information from the property implementation decl(s).
3989c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar  bool Dynamic = false;
3990c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar  ObjCPropertyImplDecl *SynthesizePID = 0;
3991c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar
3992c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar  // FIXME: Duplicated code due to poor abstraction.
3993c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar  if (Container) {
39941eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    if (const ObjCCategoryImplDecl *CID =
3995c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar        dyn_cast<ObjCCategoryImplDecl>(Container)) {
3996c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar      for (ObjCCategoryImplDecl::propimpl_iterator
399717945a0f64fe03ff6ec0c2146005a87636e3ac12Argyrios Kyrtzidis             i = CID->propimpl_begin(), e = CID->propimpl_end();
3998653f1b1bf293a9bd96fd4dd6372e779cc7af1597Douglas Gregor           i != e; ++i) {
3999c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar        ObjCPropertyImplDecl *PID = *i;
4000c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar        if (PID->getPropertyDecl() == PD) {
4001c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar          if (PID->getPropertyImplementation()==ObjCPropertyImplDecl::Dynamic) {
4002c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar            Dynamic = true;
4003c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar          } else {
4004c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar            SynthesizePID = PID;
4005c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar          }
4006c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar        }
4007c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar      }
4008c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar    } else {
400961710854be2b098428aff5316e64bd34b30fbcb7Chris Lattner      const ObjCImplementationDecl *OID=cast<ObjCImplementationDecl>(Container);
4010c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar      for (ObjCCategoryImplDecl::propimpl_iterator
401117945a0f64fe03ff6ec0c2146005a87636e3ac12Argyrios Kyrtzidis             i = OID->propimpl_begin(), e = OID->propimpl_end();
4012653f1b1bf293a9bd96fd4dd6372e779cc7af1597Douglas Gregor           i != e; ++i) {
4013c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar        ObjCPropertyImplDecl *PID = *i;
4014c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar        if (PID->getPropertyDecl() == PD) {
4015c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar          if (PID->getPropertyImplementation()==ObjCPropertyImplDecl::Dynamic) {
4016c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar            Dynamic = true;
4017c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar          } else {
4018c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar            SynthesizePID = PID;
4019c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar          }
4020c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar        }
40211eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      }
4022c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar    }
4023c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar  }
4024c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar
4025c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar  // FIXME: This is not very efficient.
4026c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar  S = "T";
4027c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar
4028c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar  // Encode result type.
4029090b3f71702c5626d8520f9608d77c6f26dcfa15Fariborz Jahanian  // GCC has some special rules regarding encoding of properties which
4030090b3f71702c5626d8520f9608d77c6f26dcfa15Fariborz Jahanian  // closely resembles encoding of ivars.
40311eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  getObjCEncodingForTypeImpl(PD->getType(), S, true, true, 0,
4032090b3f71702c5626d8520f9608d77c6f26dcfa15Fariborz Jahanian                             true /* outermost type */,
4033090b3f71702c5626d8520f9608d77c6f26dcfa15Fariborz Jahanian                             true /* encoding for property */);
4034c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar
4035c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar  if (PD->isReadOnly()) {
4036c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar    S += ",R";
4037c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar  } else {
4038c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar    switch (PD->getSetterKind()) {
4039c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar    case ObjCPropertyDecl::Assign: break;
4040c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar    case ObjCPropertyDecl::Copy:   S += ",C"; break;
40411eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    case ObjCPropertyDecl::Retain: S += ",&"; break;
4042c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar    }
4043c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar  }
4044c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar
4045c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar  // It really isn't clear at all what this means, since properties
4046c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar  // are "dynamic by default".
4047c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar  if (Dynamic)
4048c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar    S += ",D";
4049c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar
4050090b3f71702c5626d8520f9608d77c6f26dcfa15Fariborz Jahanian  if (PD->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_nonatomic)
4051090b3f71702c5626d8520f9608d77c6f26dcfa15Fariborz Jahanian    S += ",N";
40521eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
4053c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar  if (PD->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_getter) {
4054c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar    S += ",G";
4055077bf5e2f48acfa9e7d69429b6e4ba86ea14896dChris Lattner    S += PD->getGetterName().getAsString();
4056c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar  }
4057c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar
4058c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar  if (PD->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_setter) {
4059c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar    S += ",S";
4060077bf5e2f48acfa9e7d69429b6e4ba86ea14896dChris Lattner    S += PD->getSetterName().getAsString();
4061c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar  }
4062c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar
4063c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar  if (SynthesizePID) {
4064c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar    const ObjCIvarDecl *OID = SynthesizePID->getPropertyIvarDecl();
4065c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar    S += ",V";
406639f34e97d6a468f0a7dfa5664c61217cffc65b74Chris Lattner    S += OID->getNameAsString();
4067c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar  }
4068c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar
4069c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar  // FIXME: OBJCGC: weak & strong
4070c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar}
4071c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar
4072a1c033e9514865f3a7b0d8b3b20e6de926cfec6cFariborz Jahanian/// getLegacyIntegralTypeEncoding -
40731eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// Another legacy compatibility encoding: 32-bit longs are encoded as
40741eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// 'l' or 'L' , but not always.  For typedefs, we need to use
4075a1c033e9514865f3a7b0d8b3b20e6de926cfec6cFariborz Jahanian/// 'i' or 'I' instead if encoding a struct field, or a pointer!
4076a1c033e9514865f3a7b0d8b3b20e6de926cfec6cFariborz Jahanian///
4077a1c033e9514865f3a7b0d8b3b20e6de926cfec6cFariborz Jahanianvoid ASTContext::getLegacyIntegralTypeEncoding (QualType &PointeeTy) const {
40788e1fab243ab8023b7ee3899745386b3b3a4258f8Mike Stump  if (isa<TypedefType>(PointeeTy.getTypePtr())) {
4079183700f494ec9b6701b6efe82bcb25f4c79ba561John McCall    if (const BuiltinType *BT = PointeeTy->getAs<BuiltinType>()) {
40804ba2a17694148e16eaa8d3917f657ffcd3667be4Jay Foad      if (BT->getKind() == BuiltinType::ULong && getIntWidth(PointeeTy) == 32)
4081a1c033e9514865f3a7b0d8b3b20e6de926cfec6cFariborz Jahanian        PointeeTy = UnsignedIntTy;
40821eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      else
40834ba2a17694148e16eaa8d3917f657ffcd3667be4Jay Foad        if (BT->getKind() == BuiltinType::Long && getIntWidth(PointeeTy) == 32)
4084a1c033e9514865f3a7b0d8b3b20e6de926cfec6cFariborz Jahanian          PointeeTy = IntTy;
4085a1c033e9514865f3a7b0d8b3b20e6de926cfec6cFariborz Jahanian    }
4086a1c033e9514865f3a7b0d8b3b20e6de926cfec6cFariborz Jahanian  }
4087a1c033e9514865f3a7b0d8b3b20e6de926cfec6cFariborz Jahanian}
4088a1c033e9514865f3a7b0d8b3b20e6de926cfec6cFariborz Jahanian
40897d6b46d9a9d75dea8ef9f6973dd50633c1f37963Fariborz Jahanianvoid ASTContext::getObjCEncodingForType(QualType T, std::string& S,
40904ba2a17694148e16eaa8d3917f657ffcd3667be4Jay Foad                                        const FieldDecl *Field) const {
409182a6cfbc421cc99c5b7313271f399f7ef95056ecDaniel Dunbar  // We follow the behavior of gcc, expanding structures which are
409282a6cfbc421cc99c5b7313271f399f7ef95056ecDaniel Dunbar  // directly pointed to, and expanding embedded structures. Note that
409382a6cfbc421cc99c5b7313271f399f7ef95056ecDaniel Dunbar  // these rules are sufficient to prevent recursive encoding of the
409482a6cfbc421cc99c5b7313271f399f7ef95056ecDaniel Dunbar  // same type.
40951eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  getObjCEncodingForTypeImpl(T, S, true, true, Field,
40965b8c7d9fb620ba3a71e996d61e7b9bdf763b5c09Fariborz Jahanian                             true /* outermost type */);
409782a6cfbc421cc99c5b7313271f399f7ef95056ecDaniel Dunbar}
409882a6cfbc421cc99c5b7313271f399f7ef95056ecDaniel Dunbar
409964fd7e86c1a90d9ff78e4a0bd79f69499667a4e3David Chisnallstatic char ObjCEncodingForPrimitiveKind(const ASTContext *C, QualType T) {
410064fd7e86c1a90d9ff78e4a0bd79f69499667a4e3David Chisnall    switch (T->getAs<BuiltinType>()->getKind()) {
410164fd7e86c1a90d9ff78e4a0bd79f69499667a4e3David Chisnall    default: assert(0 && "Unhandled builtin type kind");
410264fd7e86c1a90d9ff78e4a0bd79f69499667a4e3David Chisnall    case BuiltinType::Void:       return 'v';
410364fd7e86c1a90d9ff78e4a0bd79f69499667a4e3David Chisnall    case BuiltinType::Bool:       return 'B';
410464fd7e86c1a90d9ff78e4a0bd79f69499667a4e3David Chisnall    case BuiltinType::Char_U:
410564fd7e86c1a90d9ff78e4a0bd79f69499667a4e3David Chisnall    case BuiltinType::UChar:      return 'C';
410664fd7e86c1a90d9ff78e4a0bd79f69499667a4e3David Chisnall    case BuiltinType::UShort:     return 'S';
410764fd7e86c1a90d9ff78e4a0bd79f69499667a4e3David Chisnall    case BuiltinType::UInt:       return 'I';
410864fd7e86c1a90d9ff78e4a0bd79f69499667a4e3David Chisnall    case BuiltinType::ULong:
41094ba2a17694148e16eaa8d3917f657ffcd3667be4Jay Foad        return C->getIntWidth(T) == 32 ? 'L' : 'Q';
411064fd7e86c1a90d9ff78e4a0bd79f69499667a4e3David Chisnall    case BuiltinType::UInt128:    return 'T';
411164fd7e86c1a90d9ff78e4a0bd79f69499667a4e3David Chisnall    case BuiltinType::ULongLong:  return 'Q';
411264fd7e86c1a90d9ff78e4a0bd79f69499667a4e3David Chisnall    case BuiltinType::Char_S:
411364fd7e86c1a90d9ff78e4a0bd79f69499667a4e3David Chisnall    case BuiltinType::SChar:      return 'c';
411464fd7e86c1a90d9ff78e4a0bd79f69499667a4e3David Chisnall    case BuiltinType::Short:      return 's';
41153f59c975aa5d047f7edd1b900b5e885c38af0ef7Chris Lattner    case BuiltinType::WChar_S:
41163f59c975aa5d047f7edd1b900b5e885c38af0ef7Chris Lattner    case BuiltinType::WChar_U:
411764fd7e86c1a90d9ff78e4a0bd79f69499667a4e3David Chisnall    case BuiltinType::Int:        return 'i';
411864fd7e86c1a90d9ff78e4a0bd79f69499667a4e3David Chisnall    case BuiltinType::Long:
41194ba2a17694148e16eaa8d3917f657ffcd3667be4Jay Foad      return C->getIntWidth(T) == 32 ? 'l' : 'q';
412064fd7e86c1a90d9ff78e4a0bd79f69499667a4e3David Chisnall    case BuiltinType::LongLong:   return 'q';
412164fd7e86c1a90d9ff78e4a0bd79f69499667a4e3David Chisnall    case BuiltinType::Int128:     return 't';
412264fd7e86c1a90d9ff78e4a0bd79f69499667a4e3David Chisnall    case BuiltinType::Float:      return 'f';
412364fd7e86c1a90d9ff78e4a0bd79f69499667a4e3David Chisnall    case BuiltinType::Double:     return 'd';
41243a0be84b2aed8563150cdbd976a98838afa261ebDaniel Dunbar    case BuiltinType::LongDouble: return 'D';
412564fd7e86c1a90d9ff78e4a0bd79f69499667a4e3David Chisnall    }
412664fd7e86c1a90d9ff78e4a0bd79f69499667a4e3David Chisnall}
412764fd7e86c1a90d9ff78e4a0bd79f69499667a4e3David Chisnall
41284ba2a17694148e16eaa8d3917f657ffcd3667be4Jay Foadstatic void EncodeBitField(const ASTContext *Ctx, std::string& S,
412964fd7e86c1a90d9ff78e4a0bd79f69499667a4e3David Chisnall                           QualType T, const FieldDecl *FD) {
41308b4bf90eb6d3d08cf3bfb86705f0fdb20b9c5875Fariborz Jahanian  const Expr *E = FD->getBitWidth();
41318b4bf90eb6d3d08cf3bfb86705f0fdb20b9c5875Fariborz Jahanian  assert(E && "bitfield width not there - getObjCEncodingForTypeImpl");
41328b4bf90eb6d3d08cf3bfb86705f0fdb20b9c5875Fariborz Jahanian  S += 'b';
413364fd7e86c1a90d9ff78e4a0bd79f69499667a4e3David Chisnall  // The NeXT runtime encodes bit fields as b followed by the number of bits.
413464fd7e86c1a90d9ff78e4a0bd79f69499667a4e3David Chisnall  // The GNU runtime requires more information; bitfields are encoded as b,
413564fd7e86c1a90d9ff78e4a0bd79f69499667a4e3David Chisnall  // then the offset (in bits) of the first element, then the type of the
413664fd7e86c1a90d9ff78e4a0bd79f69499667a4e3David Chisnall  // bitfield, then the size in bits.  For example, in this structure:
413764fd7e86c1a90d9ff78e4a0bd79f69499667a4e3David Chisnall  //
413864fd7e86c1a90d9ff78e4a0bd79f69499667a4e3David Chisnall  // struct
413964fd7e86c1a90d9ff78e4a0bd79f69499667a4e3David Chisnall  // {
414064fd7e86c1a90d9ff78e4a0bd79f69499667a4e3David Chisnall  //    int integer;
414164fd7e86c1a90d9ff78e4a0bd79f69499667a4e3David Chisnall  //    int flags:2;
414264fd7e86c1a90d9ff78e4a0bd79f69499667a4e3David Chisnall  // };
414364fd7e86c1a90d9ff78e4a0bd79f69499667a4e3David Chisnall  // On a 32-bit system, the encoding for flags would be b2 for the NeXT
414464fd7e86c1a90d9ff78e4a0bd79f69499667a4e3David Chisnall  // runtime, but b32i2 for the GNU runtime.  The reason for this extra
414564fd7e86c1a90d9ff78e4a0bd79f69499667a4e3David Chisnall  // information is not especially sensible, but we're stuck with it for
414664fd7e86c1a90d9ff78e4a0bd79f69499667a4e3David Chisnall  // compatibility with GCC, although providing it breaks anything that
414764fd7e86c1a90d9ff78e4a0bd79f69499667a4e3David Chisnall  // actually uses runtime introspection and wants to work on both runtimes...
414864fd7e86c1a90d9ff78e4a0bd79f69499667a4e3David Chisnall  if (!Ctx->getLangOptions().NeXTRuntime) {
414964fd7e86c1a90d9ff78e4a0bd79f69499667a4e3David Chisnall    const RecordDecl *RD = FD->getParent();
415064fd7e86c1a90d9ff78e4a0bd79f69499667a4e3David Chisnall    const ASTRecordLayout &RL = Ctx->getASTRecordLayout(RD);
415164fd7e86c1a90d9ff78e4a0bd79f69499667a4e3David Chisnall    // FIXME: This same linear search is also used in ExprConstant - it might
415264fd7e86c1a90d9ff78e4a0bd79f69499667a4e3David Chisnall    // be better if the FieldDecl stored its offset.  We'd be increasing the
415364fd7e86c1a90d9ff78e4a0bd79f69499667a4e3David Chisnall    // size of the object slightly, but saving some time every time it is used.
415464fd7e86c1a90d9ff78e4a0bd79f69499667a4e3David Chisnall    unsigned i = 0;
415564fd7e86c1a90d9ff78e4a0bd79f69499667a4e3David Chisnall    for (RecordDecl::field_iterator Field = RD->field_begin(),
415664fd7e86c1a90d9ff78e4a0bd79f69499667a4e3David Chisnall                                 FieldEnd = RD->field_end();
415764fd7e86c1a90d9ff78e4a0bd79f69499667a4e3David Chisnall         Field != FieldEnd; (void)++Field, ++i) {
415864fd7e86c1a90d9ff78e4a0bd79f69499667a4e3David Chisnall      if (*Field == FD)
415964fd7e86c1a90d9ff78e4a0bd79f69499667a4e3David Chisnall        break;
416064fd7e86c1a90d9ff78e4a0bd79f69499667a4e3David Chisnall    }
416164fd7e86c1a90d9ff78e4a0bd79f69499667a4e3David Chisnall    S += llvm::utostr(RL.getFieldOffset(i));
4162c7ff82c2040f45eaad2eddea0e4461dddc972cd1David Chisnall    if (T->isEnumeralType())
4163c7ff82c2040f45eaad2eddea0e4461dddc972cd1David Chisnall      S += 'i';
4164c7ff82c2040f45eaad2eddea0e4461dddc972cd1David Chisnall    else
41654ba2a17694148e16eaa8d3917f657ffcd3667be4Jay Foad      S += ObjCEncodingForPrimitiveKind(Ctx, T);
416664fd7e86c1a90d9ff78e4a0bd79f69499667a4e3David Chisnall  }
416764fd7e86c1a90d9ff78e4a0bd79f69499667a4e3David Chisnall  unsigned N = E->EvaluateAsInt(*Ctx).getZExtValue();
41688b4bf90eb6d3d08cf3bfb86705f0fdb20b9c5875Fariborz Jahanian  S += llvm::utostr(N);
41698b4bf90eb6d3d08cf3bfb86705f0fdb20b9c5875Fariborz Jahanian}
41708b4bf90eb6d3d08cf3bfb86705f0fdb20b9c5875Fariborz Jahanian
417101eb9b9683535d8a65c704ad2c545903409e2d36Daniel Dunbar// FIXME: Use SmallString for accumulating string.
417282a6cfbc421cc99c5b7313271f399f7ef95056ecDaniel Dunbarvoid ASTContext::getObjCEncodingForTypeImpl(QualType T, std::string& S,
417382a6cfbc421cc99c5b7313271f399f7ef95056ecDaniel Dunbar                                            bool ExpandPointedToStructures,
417482a6cfbc421cc99c5b7313271f399f7ef95056ecDaniel Dunbar                                            bool ExpandStructures,
4175153bfe5795e2c1a5a738e73d3784964e082237fcDaniel Dunbar                                            const FieldDecl *FD,
4176090b3f71702c5626d8520f9608d77c6f26dcfa15Fariborz Jahanian                                            bool OutermostType,
41772636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis                                            bool EncodingProperty,
41782636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis                                            bool StructField) const {
417964fd7e86c1a90d9ff78e4a0bd79f69499667a4e3David Chisnall  if (T->getAs<BuiltinType>()) {
4180ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner    if (FD && FD->isBitField())
418164fd7e86c1a90d9ff78e4a0bd79f69499667a4e3David Chisnall      return EncodeBitField(this, S, T, FD);
418264fd7e86c1a90d9ff78e4a0bd79f69499667a4e3David Chisnall    S += ObjCEncodingForPrimitiveKind(this, T);
4183ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner    return;
4184ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner  }
41851eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
4186183700f494ec9b6701b6efe82bcb25f4c79ba561John McCall  if (const ComplexType *CT = T->getAs<ComplexType>()) {
4187c612f7bc9a6379cd7e7c2dd306d05938e890051bAnders Carlsson    S += 'j';
41881eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    getObjCEncodingForTypeImpl(CT->getElementType(), S, false, false, 0, false,
4189c612f7bc9a6379cd7e7c2dd306d05938e890051bAnders Carlsson                               false);
4190ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner    return;
4191ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner  }
419260bce3ef20a4c9684e3825cdd739fefb9810327dFariborz Jahanian
4193aa1d76163e4b0b1cc54e222be67379f8c02e8ffaFariborz Jahanian  // encoding for pointer or r3eference types.
4194aa1d76163e4b0b1cc54e222be67379f8c02e8ffaFariborz Jahanian  QualType PointeeTy;
41956217b80b7a1379b74cced1c076338262c3c980b3Ted Kremenek  if (const PointerType *PT = T->getAs<PointerType>()) {
41968d2c0a9814e56c2b22e22d1045181c735aef62fdFariborz Jahanian    if (PT->isObjCSelType()) {
41978d2c0a9814e56c2b22e22d1045181c735aef62fdFariborz Jahanian      S += ':';
41988d2c0a9814e56c2b22e22d1045181c735aef62fdFariborz Jahanian      return;
41998d2c0a9814e56c2b22e22d1045181c735aef62fdFariborz Jahanian    }
4200aa1d76163e4b0b1cc54e222be67379f8c02e8ffaFariborz Jahanian    PointeeTy = PT->getPointeeType();
4201aa1d76163e4b0b1cc54e222be67379f8c02e8ffaFariborz Jahanian  }
4202aa1d76163e4b0b1cc54e222be67379f8c02e8ffaFariborz Jahanian  else if (const ReferenceType *RT = T->getAs<ReferenceType>())
4203aa1d76163e4b0b1cc54e222be67379f8c02e8ffaFariborz Jahanian    PointeeTy = RT->getPointeeType();
4204aa1d76163e4b0b1cc54e222be67379f8c02e8ffaFariborz Jahanian  if (!PointeeTy.isNull()) {
4205a1c033e9514865f3a7b0d8b3b20e6de926cfec6cFariborz Jahanian    bool isReadOnly = false;
4206a1c033e9514865f3a7b0d8b3b20e6de926cfec6cFariborz Jahanian    // For historical/compatibility reasons, the read-only qualifier of the
4207a1c033e9514865f3a7b0d8b3b20e6de926cfec6cFariborz Jahanian    // pointee gets emitted _before_ the '^'.  The read-only qualifier of
4208a1c033e9514865f3a7b0d8b3b20e6de926cfec6cFariborz Jahanian    // the pointer itself gets ignored, _unless_ we are looking at a typedef!
42091eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    // Also, do not emit the 'r' for anything but the outermost type!
42108e1fab243ab8023b7ee3899745386b3b3a4258f8Mike Stump    if (isa<TypedefType>(T.getTypePtr())) {
4211a1c033e9514865f3a7b0d8b3b20e6de926cfec6cFariborz Jahanian      if (OutermostType && T.isConstQualified()) {
4212a1c033e9514865f3a7b0d8b3b20e6de926cfec6cFariborz Jahanian        isReadOnly = true;
4213a1c033e9514865f3a7b0d8b3b20e6de926cfec6cFariborz Jahanian        S += 'r';
4214a1c033e9514865f3a7b0d8b3b20e6de926cfec6cFariborz Jahanian      }
42159fdbab3cbc2fc04bcaf5768023d83707f3151144Mike Stump    } else if (OutermostType) {
4216a1c033e9514865f3a7b0d8b3b20e6de926cfec6cFariborz Jahanian      QualType P = PointeeTy;
42176217b80b7a1379b74cced1c076338262c3c980b3Ted Kremenek      while (P->getAs<PointerType>())
42186217b80b7a1379b74cced1c076338262c3c980b3Ted Kremenek        P = P->getAs<PointerType>()->getPointeeType();
4219a1c033e9514865f3a7b0d8b3b20e6de926cfec6cFariborz Jahanian      if (P.isConstQualified()) {
4220a1c033e9514865f3a7b0d8b3b20e6de926cfec6cFariborz Jahanian        isReadOnly = true;
4221a1c033e9514865f3a7b0d8b3b20e6de926cfec6cFariborz Jahanian        S += 'r';
4222a1c033e9514865f3a7b0d8b3b20e6de926cfec6cFariborz Jahanian      }
4223a1c033e9514865f3a7b0d8b3b20e6de926cfec6cFariborz Jahanian    }
4224a1c033e9514865f3a7b0d8b3b20e6de926cfec6cFariborz Jahanian    if (isReadOnly) {
4225a1c033e9514865f3a7b0d8b3b20e6de926cfec6cFariborz Jahanian      // Another legacy compatibility encoding. Some ObjC qualifier and type
4226a1c033e9514865f3a7b0d8b3b20e6de926cfec6cFariborz Jahanian      // combinations need to be rearranged.
4227a1c033e9514865f3a7b0d8b3b20e6de926cfec6cFariborz Jahanian      // Rewrite "in const" from "nr" to "rn"
42280237941e0beb0c929934b66ad29443b484d987feBenjamin Kramer      if (llvm::StringRef(S).endswith("nr"))
42290237941e0beb0c929934b66ad29443b484d987feBenjamin Kramer        S.replace(S.end()-2, S.end(), "rn");
4230a1c033e9514865f3a7b0d8b3b20e6de926cfec6cFariborz Jahanian    }
42311eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
423285f9bceab1542aafff012d4d28e998f4ba16e362Anders Carlsson    if (PointeeTy->isCharType()) {
423385f9bceab1542aafff012d4d28e998f4ba16e362Anders Carlsson      // char pointer types should be encoded as '*' unless it is a
423485f9bceab1542aafff012d4d28e998f4ba16e362Anders Carlsson      // type that has been typedef'd to 'BOOL'.
4235e8c49533521c40643653f943d47229e62d277f88Anders Carlsson      if (!isTypeTypedefedAsBOOL(PointeeTy)) {
423685f9bceab1542aafff012d4d28e998f4ba16e362Anders Carlsson        S += '*';
423785f9bceab1542aafff012d4d28e998f4ba16e362Anders Carlsson        return;
423885f9bceab1542aafff012d4d28e998f4ba16e362Anders Carlsson      }
42396217b80b7a1379b74cced1c076338262c3c980b3Ted Kremenek    } else if (const RecordType *RTy = PointeeTy->getAs<RecordType>()) {
42409533a7fdb8397421f3be52e879442460a87389f6Steve Naroff      // GCC binary compat: Need to convert "struct objc_class *" to "#".
42419533a7fdb8397421f3be52e879442460a87389f6Steve Naroff      if (RTy->getDecl()->getIdentifier() == &Idents.get("objc_class")) {
42429533a7fdb8397421f3be52e879442460a87389f6Steve Naroff        S += '#';
42439533a7fdb8397421f3be52e879442460a87389f6Steve Naroff        return;
42449533a7fdb8397421f3be52e879442460a87389f6Steve Naroff      }
42459533a7fdb8397421f3be52e879442460a87389f6Steve Naroff      // GCC binary compat: Need to convert "struct objc_object *" to "@".
42469533a7fdb8397421f3be52e879442460a87389f6Steve Naroff      if (RTy->getDecl()->getIdentifier() == &Idents.get("objc_object")) {
42479533a7fdb8397421f3be52e879442460a87389f6Steve Naroff        S += '@';
42489533a7fdb8397421f3be52e879442460a87389f6Steve Naroff        return;
42499533a7fdb8397421f3be52e879442460a87389f6Steve Naroff      }
42509533a7fdb8397421f3be52e879442460a87389f6Steve Naroff      // fall through...
425185f9bceab1542aafff012d4d28e998f4ba16e362Anders Carlsson    }
425285f9bceab1542aafff012d4d28e998f4ba16e362Anders Carlsson    S += '^';
4253a1c033e9514865f3a7b0d8b3b20e6de926cfec6cFariborz Jahanian    getLegacyIntegralTypeEncoding(PointeeTy);
4254a1c033e9514865f3a7b0d8b3b20e6de926cfec6cFariborz Jahanian
42551eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    getObjCEncodingForTypeImpl(PointeeTy, S, false, ExpandPointedToStructures,
425643822eaeddeaa419b90f23c68af6b23c46788a58Fariborz Jahanian                               NULL);
4257ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner    return;
4258ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner  }
4259aa1d76163e4b0b1cc54e222be67379f8c02e8ffaFariborz Jahanian
4260ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner  if (const ArrayType *AT =
4261ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner      // Ignore type qualifiers etc.
4262ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner        dyn_cast<ArrayType>(T->getCanonicalTypeInternal())) {
42632636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis    if (isa<IncompleteArrayType>(AT) && !StructField) {
4264559a83330416affb0e341a2c53800cbf924a5178Anders Carlsson      // Incomplete arrays are encoded as a pointer to the array element.
4265559a83330416affb0e341a2c53800cbf924a5178Anders Carlsson      S += '^';
4266559a83330416affb0e341a2c53800cbf924a5178Anders Carlsson
42671eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      getObjCEncodingForTypeImpl(AT->getElementType(), S,
4268559a83330416affb0e341a2c53800cbf924a5178Anders Carlsson                                 false, ExpandStructures, FD);
4269559a83330416affb0e341a2c53800cbf924a5178Anders Carlsson    } else {
4270559a83330416affb0e341a2c53800cbf924a5178Anders Carlsson      S += '[';
42711eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
42722636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis      if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(AT)) {
42732636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis        if (getTypeSize(CAT->getElementType()) == 0)
42742636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis          S += '0';
42752636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis        else
42762636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis          S += llvm::utostr(CAT->getSize().getZExtValue());
42772636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis      } else {
4278559a83330416affb0e341a2c53800cbf924a5178Anders Carlsson        //Variable length arrays are encoded as a regular array with 0 elements.
42792636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis        assert((isa<VariableArrayType>(AT) || isa<IncompleteArrayType>(AT)) &&
42802636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis               "Unknown array type!");
4281559a83330416affb0e341a2c53800cbf924a5178Anders Carlsson        S += '0';
4282559a83330416affb0e341a2c53800cbf924a5178Anders Carlsson      }
42831eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
42841eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      getObjCEncodingForTypeImpl(AT->getElementType(), S,
4285559a83330416affb0e341a2c53800cbf924a5178Anders Carlsson                                 false, ExpandStructures, FD);
4286559a83330416affb0e341a2c53800cbf924a5178Anders Carlsson      S += ']';
4287559a83330416affb0e341a2c53800cbf924a5178Anders Carlsson    }
4288ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner    return;
4289ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner  }
42901eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
4291183700f494ec9b6701b6efe82bcb25f4c79ba561John McCall  if (T->getAs<FunctionType>()) {
4292c0a87b7db06643178ad2cbce0767548c139ea387Anders Carlsson    S += '?';
4293ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner    return;
4294ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner  }
42951eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
42966217b80b7a1379b74cced1c076338262c3c980b3Ted Kremenek  if (const RecordType *RTy = T->getAs<RecordType>()) {
429782a6cfbc421cc99c5b7313271f399f7ef95056ecDaniel Dunbar    RecordDecl *RDecl = RTy->getDecl();
4298d96b35bc6becf8db00d140c11e3d0e53f27567a1Daniel Dunbar    S += RDecl->isUnion() ? '(' : '{';
4299502a4a1ce4c34cf78c8182d9798da0a51d9b7302Daniel Dunbar    // Anonymous structures print as '?'
4300502a4a1ce4c34cf78c8182d9798da0a51d9b7302Daniel Dunbar    if (const IdentifierInfo *II = RDecl->getIdentifier()) {
4301502a4a1ce4c34cf78c8182d9798da0a51d9b7302Daniel Dunbar      S += II->getName();
43026fb94391dc7cb11fd4bbdb969bbab11b6b48c223Fariborz Jahanian      if (ClassTemplateSpecializationDecl *Spec
43036fb94391dc7cb11fd4bbdb969bbab11b6b48c223Fariborz Jahanian          = dyn_cast<ClassTemplateSpecializationDecl>(RDecl)) {
43046fb94391dc7cb11fd4bbdb969bbab11b6b48c223Fariborz Jahanian        const TemplateArgumentList &TemplateArgs = Spec->getTemplateArgs();
43056fb94391dc7cb11fd4bbdb969bbab11b6b48c223Fariborz Jahanian        std::string TemplateArgsStr
43066fb94391dc7cb11fd4bbdb969bbab11b6b48c223Fariborz Jahanian          = TemplateSpecializationType::PrintTemplateArgumentList(
4307910f8008fea79120489a53593fe971b0b8a4a740Douglas Gregor                                            TemplateArgs.data(),
4308910f8008fea79120489a53593fe971b0b8a4a740Douglas Gregor                                            TemplateArgs.size(),
43096fb94391dc7cb11fd4bbdb969bbab11b6b48c223Fariborz Jahanian                                            (*this).PrintingPolicy);
43106fb94391dc7cb11fd4bbdb969bbab11b6b48c223Fariborz Jahanian
43116fb94391dc7cb11fd4bbdb969bbab11b6b48c223Fariborz Jahanian        S += TemplateArgsStr;
43126fb94391dc7cb11fd4bbdb969bbab11b6b48c223Fariborz Jahanian      }
4313502a4a1ce4c34cf78c8182d9798da0a51d9b7302Daniel Dunbar    } else {
4314502a4a1ce4c34cf78c8182d9798da0a51d9b7302Daniel Dunbar      S += '?';
4315502a4a1ce4c34cf78c8182d9798da0a51d9b7302Daniel Dunbar    }
43160d504c1da852e58ff802545c823ecff3b6c654b8Daniel Dunbar    if (ExpandStructures) {
43177d6b46d9a9d75dea8ef9f6973dd50633c1f37963Fariborz Jahanian      S += '=';
43182636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis      if (!RDecl->isUnion()) {
43192636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis        getObjCEncodingForStructureImpl(RDecl, S, FD);
43202636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis      } else {
43212636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis        for (RecordDecl::field_iterator Field = RDecl->field_begin(),
43222636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis                                     FieldEnd = RDecl->field_end();
43232636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis             Field != FieldEnd; ++Field) {
43242636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis          if (FD) {
43252636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis            S += '"';
43262636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis            S += Field->getNameAsString();
43272636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis            S += '"';
43282636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis          }
43291eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
43302636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis          // Special case bit-fields.
43312636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis          if (Field->isBitField()) {
43322636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis            getObjCEncodingForTypeImpl(Field->getType(), S, false, true,
43332636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis                                       (*Field));
43342636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis          } else {
43352636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis            QualType qt = Field->getType();
43362636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis            getLegacyIntegralTypeEncoding(qt);
43372636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis            getObjCEncodingForTypeImpl(qt, S, false, true,
43382636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis                                       FD, /*OutermostType*/false,
43392636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis                                       /*EncodingProperty*/false,
43402636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis                                       /*StructField*/true);
43412636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis          }
4342d96b35bc6becf8db00d140c11e3d0e53f27567a1Daniel Dunbar        }
43437d6b46d9a9d75dea8ef9f6973dd50633c1f37963Fariborz Jahanian      }
43446de88a873a4cbe06d72602eef57d68006730a80bFariborz Jahanian    }
4345d96b35bc6becf8db00d140c11e3d0e53f27567a1Daniel Dunbar    S += RDecl->isUnion() ? ')' : '}';
4346ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner    return;
4347ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner  }
4348e6012c7ecb9d848f4091c8c48e7d9946cc36b23fFariborz Jahanian
4349ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner  if (T->isEnumeralType()) {
43508b4bf90eb6d3d08cf3bfb86705f0fdb20b9c5875Fariborz Jahanian    if (FD && FD->isBitField())
435164fd7e86c1a90d9ff78e4a0bd79f69499667a4e3David Chisnall      EncodeBitField(this, S, T, FD);
43528b4bf90eb6d3d08cf3bfb86705f0fdb20b9c5875Fariborz Jahanian    else
43538b4bf90eb6d3d08cf3bfb86705f0fdb20b9c5875Fariborz Jahanian      S += 'i';
4354ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner    return;
4355ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner  }
43561eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
4357ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner  if (T->isBlockPointerType()) {
435821a98b188857d690aa4510c52ac4317ffa0908a8Steve Naroff    S += "@?"; // Unlike a pointer-to-function, which is "^?".
4359ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner    return;
4360ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner  }
43611eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
4362c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall  // Ignore protocol qualifiers when mangling at this level.
4363c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall  if (const ObjCObjectType *OT = T->getAs<ObjCObjectType>())
4364c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall    T = OT->getBaseType();
4365c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall
43660953e767ff7817f97b3ab20896b229891eeff45bJohn McCall  if (const ObjCInterfaceType *OIT = T->getAs<ObjCInterfaceType>()) {
436743822eaeddeaa419b90f23c68af6b23c46788a58Fariborz Jahanian    // @encode(class_name)
43680953e767ff7817f97b3ab20896b229891eeff45bJohn McCall    ObjCInterfaceDecl *OI = OIT->getDecl();
436943822eaeddeaa419b90f23c68af6b23c46788a58Fariborz Jahanian    S += '{';
437043822eaeddeaa419b90f23c68af6b23c46788a58Fariborz Jahanian    const IdentifierInfo *II = OI->getIdentifier();
437143822eaeddeaa419b90f23c68af6b23c46788a58Fariborz Jahanian    S += II->getName();
437243822eaeddeaa419b90f23c68af6b23c46788a58Fariborz Jahanian    S += '=';
43732c18bb7c9fca66c30b6eabbdcbc6399d24a54fa9Fariborz Jahanian    llvm::SmallVector<ObjCIvarDecl*, 32> Ivars;
43742c18bb7c9fca66c30b6eabbdcbc6399d24a54fa9Fariborz Jahanian    DeepCollectObjCIvars(OI, true, Ivars);
43752c18bb7c9fca66c30b6eabbdcbc6399d24a54fa9Fariborz Jahanian    for (unsigned i = 0, e = Ivars.size(); i != e; ++i) {
43762c18bb7c9fca66c30b6eabbdcbc6399d24a54fa9Fariborz Jahanian      FieldDecl *Field = cast<FieldDecl>(Ivars[i]);
43772c18bb7c9fca66c30b6eabbdcbc6399d24a54fa9Fariborz Jahanian      if (Field->isBitField())
43782c18bb7c9fca66c30b6eabbdcbc6399d24a54fa9Fariborz Jahanian        getObjCEncodingForTypeImpl(Field->getType(), S, false, true, Field);
437943822eaeddeaa419b90f23c68af6b23c46788a58Fariborz Jahanian      else
43802c18bb7c9fca66c30b6eabbdcbc6399d24a54fa9Fariborz Jahanian        getObjCEncodingForTypeImpl(Field->getType(), S, false, true, FD);
438143822eaeddeaa419b90f23c68af6b23c46788a58Fariborz Jahanian    }
438243822eaeddeaa419b90f23c68af6b23c46788a58Fariborz Jahanian    S += '}';
4383ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner    return;
438443822eaeddeaa419b90f23c68af6b23c46788a58Fariborz Jahanian  }
43851eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
4386183700f494ec9b6701b6efe82bcb25f4c79ba561John McCall  if (const ObjCObjectPointerType *OPT = T->getAs<ObjCObjectPointerType>()) {
438714108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff    if (OPT->isObjCIdType()) {
438814108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff      S += '@';
438914108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff      return;
4390ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner    }
43911eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
439227d20a24a1b816cecbd142727d2c81af5a6a111bSteve Naroff    if (OPT->isObjCClassType() || OPT->isObjCQualifiedClassType()) {
439327d20a24a1b816cecbd142727d2c81af5a6a111bSteve Naroff      // FIXME: Consider if we need to output qualifiers for 'Class<p>'.
439427d20a24a1b816cecbd142727d2c81af5a6a111bSteve Naroff      // Since this is a binary compatibility issue, need to consult with runtime
439527d20a24a1b816cecbd142727d2c81af5a6a111bSteve Naroff      // folks. Fortunately, this is a *very* obsure construct.
439614108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff      S += '#';
439714108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff      return;
4398ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner    }
43991eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
4400ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner    if (OPT->isObjCQualifiedIdType()) {
44011eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      getObjCEncodingForTypeImpl(getObjCIdType(), S,
440214108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff                                 ExpandPointedToStructures,
440314108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff                                 ExpandStructures, FD);
440414108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff      if (FD || EncodingProperty) {
440514108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff        // Note that we do extended encoding of protocol qualifer list
440614108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff        // Only when doing ivar or property encoding.
440714108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff        S += '"';
440867ef8eaea8a0a2073147a8d863f0e3f30d525802Steve Naroff        for (ObjCObjectPointerType::qual_iterator I = OPT->qual_begin(),
440967ef8eaea8a0a2073147a8d863f0e3f30d525802Steve Naroff             E = OPT->qual_end(); I != E; ++I) {
441014108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff          S += '<';
441114108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff          S += (*I)->getNameAsString();
441214108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff          S += '>';
441314108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff        }
441414108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff        S += '"';
441514108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff      }
441614108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff      return;
4417ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner    }
44181eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
4419ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner    QualType PointeeTy = OPT->getPointeeType();
4420ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner    if (!EncodingProperty &&
4421ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner        isa<TypedefType>(PointeeTy.getTypePtr())) {
4422ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner      // Another historical/compatibility reason.
44231eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      // We encode the underlying type which comes out as
4424ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner      // {...};
4425ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner      S += '^';
44261eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      getObjCEncodingForTypeImpl(PointeeTy, S,
44271eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump                                 false, ExpandPointedToStructures,
4428ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner                                 NULL);
442914108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff      return;
443014108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff    }
4431ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner
4432ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner    S += '@';
443327d20a24a1b816cecbd142727d2c81af5a6a111bSteve Naroff    if (OPT->getInterfaceDecl() && (FD || EncodingProperty)) {
4434ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner      S += '"';
443527d20a24a1b816cecbd142727d2c81af5a6a111bSteve Naroff      S += OPT->getInterfaceDecl()->getIdentifier()->getName();
443667ef8eaea8a0a2073147a8d863f0e3f30d525802Steve Naroff      for (ObjCObjectPointerType::qual_iterator I = OPT->qual_begin(),
443767ef8eaea8a0a2073147a8d863f0e3f30d525802Steve Naroff           E = OPT->qual_end(); I != E; ++I) {
4438ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner        S += '<';
4439ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner        S += (*I)->getNameAsString();
4440ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner        S += '>';
44411eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      }
4442ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner      S += '"';
4443ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner    }
4444ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner    return;
4445ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner  }
44461eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
4447532ec7baf2d0791abc9551ef856a537711c5774aJohn McCall  // gcc just blithely ignores member pointers.
4448532ec7baf2d0791abc9551ef856a537711c5774aJohn McCall  // TODO: maybe there should be a mangling for these
4449532ec7baf2d0791abc9551ef856a537711c5774aJohn McCall  if (T->getAs<MemberPointerType>())
4450532ec7baf2d0791abc9551ef856a537711c5774aJohn McCall    return;
4451e6012c7ecb9d848f4091c8c48e7d9946cc36b23fFariborz Jahanian
4452e6012c7ecb9d848f4091c8c48e7d9946cc36b23fFariborz Jahanian  if (T->isVectorType()) {
4453e6012c7ecb9d848f4091c8c48e7d9946cc36b23fFariborz Jahanian    // This matches gcc's encoding, even though technically it is
4454e6012c7ecb9d848f4091c8c48e7d9946cc36b23fFariborz Jahanian    // insufficient.
4455e6012c7ecb9d848f4091c8c48e7d9946cc36b23fFariborz Jahanian    // FIXME. We should do a better job than gcc.
4456e6012c7ecb9d848f4091c8c48e7d9946cc36b23fFariborz Jahanian    return;
4457e6012c7ecb9d848f4091c8c48e7d9946cc36b23fFariborz Jahanian  }
4458e6012c7ecb9d848f4091c8c48e7d9946cc36b23fFariborz Jahanian
4459ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner  assert(0 && "@encode for type not implemented!");
446085f9bceab1542aafff012d4d28e998f4ba16e362Anders Carlsson}
446185f9bceab1542aafff012d4d28e998f4ba16e362Anders Carlsson
44622636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidisvoid ASTContext::getObjCEncodingForStructureImpl(RecordDecl *RDecl,
44632636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis                                                 std::string &S,
44642636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis                                                 const FieldDecl *FD,
44652636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis                                                 bool includeVBases) const {
44662636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis  assert(RDecl && "Expected non-null RecordDecl");
44672636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis  assert(!RDecl->isUnion() && "Should not be called for unions");
44682636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis  if (!RDecl->getDefinition())
44692636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis    return;
44702636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis
44712636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis  CXXRecordDecl *CXXRec = dyn_cast<CXXRecordDecl>(RDecl);
44722636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis  std::multimap<uint64_t, NamedDecl *> FieldOrBaseOffsets;
44732636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis  const ASTRecordLayout &layout = getASTRecordLayout(RDecl);
44742636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis
44752636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis  if (CXXRec) {
44762636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis    for (CXXRecordDecl::base_class_iterator
44772636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis           BI = CXXRec->bases_begin(),
44782636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis           BE = CXXRec->bases_end(); BI != BE; ++BI) {
44792636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis      if (!BI->isVirtual()) {
44802636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis        CXXRecordDecl *base = BI->getType()->getAsCXXRecordDecl();
44812636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis        uint64_t offs = layout.getBaseClassOffsetInBits(base);
44822636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis        FieldOrBaseOffsets.insert(FieldOrBaseOffsets.upper_bound(offs),
44832636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis                                  std::make_pair(offs, base));
44842636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis      }
44852636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis    }
44862636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis  }
44872636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis
44882636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis  unsigned i = 0;
44892636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis  for (RecordDecl::field_iterator Field = RDecl->field_begin(),
44902636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis                               FieldEnd = RDecl->field_end();
44912636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis       Field != FieldEnd; ++Field, ++i) {
44922636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis    uint64_t offs = layout.getFieldOffset(i);
44932636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis    FieldOrBaseOffsets.insert(FieldOrBaseOffsets.upper_bound(offs),
44942636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis                              std::make_pair(offs, *Field));
44952636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis  }
44962636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis
44972636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis  if (CXXRec && includeVBases) {
44982636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis    for (CXXRecordDecl::base_class_iterator
44992636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis           BI = CXXRec->vbases_begin(),
45002636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis           BE = CXXRec->vbases_end(); BI != BE; ++BI) {
45012636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis      CXXRecordDecl *base = BI->getType()->getAsCXXRecordDecl();
45022636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis      uint64_t offs = layout.getVBaseClassOffsetInBits(base);
45032636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis      FieldOrBaseOffsets.insert(FieldOrBaseOffsets.upper_bound(offs),
45042636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis                                std::make_pair(offs, base));
45052636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis    }
45062636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis  }
45072636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis
45082636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis  CharUnits size;
45092636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis  if (CXXRec) {
45102636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis    size = includeVBases ? layout.getSize() : layout.getNonVirtualSize();
45112636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis  } else {
45122636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis    size = layout.getSize();
45132636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis  }
45142636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis
45152636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis  uint64_t CurOffs = 0;
45162636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis  std::multimap<uint64_t, NamedDecl *>::iterator
45172636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis    CurLayObj = FieldOrBaseOffsets.begin();
45182636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis
45192636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis  if (CurLayObj != FieldOrBaseOffsets.end() && CurLayObj->first != 0) {
45202636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis    assert(CXXRec && CXXRec->isDynamicClass() &&
45212636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis           "Offset 0 was empty but no VTable ?");
45222636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis    if (FD) {
45232636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis      S += "\"_vptr$";
45242636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis      std::string recname = CXXRec->getNameAsString();
45252636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis      if (recname.empty()) recname = "?";
45262636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis      S += recname;
45272636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis      S += '"';
45282636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis    }
45292636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis    S += "^^?";
45302636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis    CurOffs += getTypeSize(VoidPtrTy);
45312636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis  }
45322636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis
45332636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis  if (!RDecl->hasFlexibleArrayMember()) {
45342636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis    // Mark the end of the structure.
45352636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis    uint64_t offs = toBits(size);
45362636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis    FieldOrBaseOffsets.insert(FieldOrBaseOffsets.upper_bound(offs),
45372636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis                              std::make_pair(offs, (NamedDecl*)0));
45382636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis  }
45392636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis
45402636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis  for (; CurLayObj != FieldOrBaseOffsets.end(); ++CurLayObj) {
45412636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis    assert(CurOffs <= CurLayObj->first);
45422636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis
45432636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis    if (CurOffs < CurLayObj->first) {
45442636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis      uint64_t padding = CurLayObj->first - CurOffs;
45452636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis      // FIXME: There doesn't seem to be a way to indicate in the encoding that
45462636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis      // packing/alignment of members is different that normal, in which case
45472636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis      // the encoding will be out-of-sync with the real layout.
45482636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis      // If the runtime switches to just consider the size of types without
45492636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis      // taking into account alignment, we could make padding explicit in the
45502636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis      // encoding (e.g. using arrays of chars). The encoding strings would be
45512636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis      // longer then though.
45522636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis      CurOffs += padding;
45532636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis    }
45542636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis
45552636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis    NamedDecl *dcl = CurLayObj->second;
45562636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis    if (dcl == 0)
45572636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis      break; // reached end of structure.
45582636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis
45592636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis    if (CXXRecordDecl *base = dyn_cast<CXXRecordDecl>(dcl)) {
45602636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis      // We expand the bases without their virtual bases since those are going
45612636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis      // in the initial structure. Note that this differs from gcc which
45622636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis      // expands virtual bases each time one is encountered in the hierarchy,
45632636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis      // making the encoding type bigger than it really is.
45642636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis      getObjCEncodingForStructureImpl(base, S, FD, /*includeVBases*/false);
45652636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis      if (!base->isEmpty())
45662636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis        CurOffs += toBits(getASTRecordLayout(base).getNonVirtualSize());
45672636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis    } else {
45682636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis      FieldDecl *field = cast<FieldDecl>(dcl);
45692636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis      if (FD) {
45702636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis        S += '"';
45712636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis        S += field->getNameAsString();
45722636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis        S += '"';
45732636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis      }
45742636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis
45752636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis      if (field->isBitField()) {
45762636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis        EncodeBitField(this, S, field->getType(), field);
45772636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis        CurOffs += field->getBitWidth()->EvaluateAsInt(*this).getZExtValue();
45782636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis      } else {
45792636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis        QualType qt = field->getType();
45802636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis        getLegacyIntegralTypeEncoding(qt);
45812636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis        getObjCEncodingForTypeImpl(qt, S, false, true, FD,
45822636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis                                   /*OutermostType*/false,
45832636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis                                   /*EncodingProperty*/false,
45842636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis                                   /*StructField*/true);
45852636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis        CurOffs += getTypeSize(field->getType());
45862636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis      }
45872636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis    }
45882636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis  }
45892636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis}
45902636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis
45911eb4433ac451dc16f4133a88af2d002ac26c58efMike Stumpvoid ASTContext::getObjCEncodingForTypeQualifier(Decl::ObjCDeclQualifier QT,
4592ecb01e666665efabd2aa76a76f6080e2a78965faFariborz Jahanian                                                 std::string& S) const {
4593ecb01e666665efabd2aa76a76f6080e2a78965faFariborz Jahanian  if (QT & Decl::OBJC_TQ_In)
4594ecb01e666665efabd2aa76a76f6080e2a78965faFariborz Jahanian    S += 'n';
4595ecb01e666665efabd2aa76a76f6080e2a78965faFariborz Jahanian  if (QT & Decl::OBJC_TQ_Inout)
4596ecb01e666665efabd2aa76a76f6080e2a78965faFariborz Jahanian    S += 'N';
4597ecb01e666665efabd2aa76a76f6080e2a78965faFariborz Jahanian  if (QT & Decl::OBJC_TQ_Out)
4598ecb01e666665efabd2aa76a76f6080e2a78965faFariborz Jahanian    S += 'o';
4599ecb01e666665efabd2aa76a76f6080e2a78965faFariborz Jahanian  if (QT & Decl::OBJC_TQ_Bycopy)
4600ecb01e666665efabd2aa76a76f6080e2a78965faFariborz Jahanian    S += 'O';
4601ecb01e666665efabd2aa76a76f6080e2a78965faFariborz Jahanian  if (QT & Decl::OBJC_TQ_Byref)
4602ecb01e666665efabd2aa76a76f6080e2a78965faFariborz Jahanian    S += 'R';
4603ecb01e666665efabd2aa76a76f6080e2a78965faFariborz Jahanian  if (QT & Decl::OBJC_TQ_Oneway)
4604ecb01e666665efabd2aa76a76f6080e2a78965faFariborz Jahanian    S += 'V';
4605ecb01e666665efabd2aa76a76f6080e2a78965faFariborz Jahanian}
4606ecb01e666665efabd2aa76a76f6080e2a78965faFariborz Jahanian
4607ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattnervoid ASTContext::setBuiltinVaListType(QualType T) {
4608b2cf3573d7351094f6247fcca94703ce88eb9ee0Anders Carlsson  assert(BuiltinVaListType.isNull() && "__builtin_va_list type already set!");
46091eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
4610b2cf3573d7351094f6247fcca94703ce88eb9ee0Anders Carlsson  BuiltinVaListType = T;
4611b2cf3573d7351094f6247fcca94703ce88eb9ee0Anders Carlsson}
4612b2cf3573d7351094f6247fcca94703ce88eb9ee0Anders Carlsson
4613ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattnervoid ASTContext::setObjCIdType(QualType T) {
4614de2e22d33afec98324a66a358dfe0951b3c7259aSteve Naroff  ObjCIdTypedefType = T;
46157e219e47de26346885d667131977bd9ca2d7662aSteve Naroff}
46167e219e47de26346885d667131977bd9ca2d7662aSteve Naroff
4617ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattnervoid ASTContext::setObjCSelType(QualType T) {
461813dcd00615de5c4279d97bdf63cd5f0a14fd9dccFariborz Jahanian  ObjCSelTypedefType = T;
4619b62f6813406a03bf8a371c4e46c9fad51d102121Fariborz Jahanian}
4620b62f6813406a03bf8a371c4e46c9fad51d102121Fariborz Jahanian
4621ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattnervoid ASTContext::setObjCProtoType(QualType QT) {
4622a526c5c67e5a0473c340903ee542ce570119665fTed Kremenek  ObjCProtoType = QT;
4623390d50a725497e99247dc104a7d2c2a255d3af14Fariborz Jahanian}
4624390d50a725497e99247dc104a7d2c2a255d3af14Fariborz Jahanian
4625ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattnervoid ASTContext::setObjCClassType(QualType T) {
4626de2e22d33afec98324a66a358dfe0951b3c7259aSteve Naroff  ObjCClassTypedefType = T;
46278baaca50f07d0c10bba69c8d88c1b9078c92d06dAnders Carlsson}
46288baaca50f07d0c10bba69c8d88c1b9078c92d06dAnders Carlsson
4629a526c5c67e5a0473c340903ee542ce570119665fTed Kremenekvoid ASTContext::setObjCConstantStringInterface(ObjCInterfaceDecl *Decl) {
46301eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  assert(ObjCConstantStringType.isNull() &&
46312198891824c38d45b2279de5d5e3ef9394eb457cSteve Naroff         "'NSConstantString' type already set!");
46321eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
4633a526c5c67e5a0473c340903ee542ce570119665fTed Kremenek  ObjCConstantStringType = getObjCInterfaceType(Decl);
46342198891824c38d45b2279de5d5e3ef9394eb457cSteve Naroff}
46352198891824c38d45b2279de5d5e3ef9394eb457cSteve Naroff
46360bd6feb9e9d40fc889fd47e899985125a43dfed8John McCall/// \brief Retrieve the template name that corresponds to a non-empty
46370bd6feb9e9d40fc889fd47e899985125a43dfed8John McCall/// lookup.
46384ba2a17694148e16eaa8d3917f657ffcd3667be4Jay FoadTemplateName
46394ba2a17694148e16eaa8d3917f657ffcd3667be4Jay FoadASTContext::getOverloadedTemplateName(UnresolvedSetIterator Begin,
46404ba2a17694148e16eaa8d3917f657ffcd3667be4Jay Foad                                      UnresolvedSetIterator End) const {
46410bd6feb9e9d40fc889fd47e899985125a43dfed8John McCall  unsigned size = End - Begin;
46420bd6feb9e9d40fc889fd47e899985125a43dfed8John McCall  assert(size > 1 && "set is not overloaded!");
46437532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor
46440bd6feb9e9d40fc889fd47e899985125a43dfed8John McCall  void *memory = Allocate(sizeof(OverloadedTemplateStorage) +
46450bd6feb9e9d40fc889fd47e899985125a43dfed8John McCall                          size * sizeof(FunctionTemplateDecl*));
46460bd6feb9e9d40fc889fd47e899985125a43dfed8John McCall  OverloadedTemplateStorage *OT = new(memory) OverloadedTemplateStorage(size);
46470bd6feb9e9d40fc889fd47e899985125a43dfed8John McCall
46480bd6feb9e9d40fc889fd47e899985125a43dfed8John McCall  NamedDecl **Storage = OT->getStorage();
4649eec51cf1ba5f0e62c9cdb81b5c63babdd6e649abJohn McCall  for (UnresolvedSetIterator I = Begin; I != End; ++I) {
46500bd6feb9e9d40fc889fd47e899985125a43dfed8John McCall    NamedDecl *D = *I;
46510bd6feb9e9d40fc889fd47e899985125a43dfed8John McCall    assert(isa<FunctionTemplateDecl>(D) ||
46520bd6feb9e9d40fc889fd47e899985125a43dfed8John McCall           (isa<UsingShadowDecl>(D) &&
46530bd6feb9e9d40fc889fd47e899985125a43dfed8John McCall            isa<FunctionTemplateDecl>(D->getUnderlyingDecl())));
46540bd6feb9e9d40fc889fd47e899985125a43dfed8John McCall    *Storage++ = D;
46557532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor  }
46567532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor
46570bd6feb9e9d40fc889fd47e899985125a43dfed8John McCall  return TemplateName(OT);
46587532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor}
46597532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor
4660d99cbe66403ee39c2ee58024b9582b95649a4fc5Douglas Gregor/// \brief Retrieve the template name that represents a qualified
4661d99cbe66403ee39c2ee58024b9582b95649a4fc5Douglas Gregor/// template name such as \c std::vector.
46624ba2a17694148e16eaa8d3917f657ffcd3667be4Jay FoadTemplateName
46634ba2a17694148e16eaa8d3917f657ffcd3667be4Jay FoadASTContext::getQualifiedTemplateName(NestedNameSpecifier *NNS,
46644ba2a17694148e16eaa8d3917f657ffcd3667be4Jay Foad                                     bool TemplateKeyword,
46654ba2a17694148e16eaa8d3917f657ffcd3667be4Jay Foad                                     TemplateDecl *Template) const {
46660f0ea2a96534c615ff5fdd81363989b23cf2164aDouglas Gregor  assert(NNS && "Missing nested-name-specifier in qualified template name");
46670f0ea2a96534c615ff5fdd81363989b23cf2164aDouglas Gregor
4668789b1f640205e81b5af250693246120f1ce9d147Douglas Gregor  // FIXME: Canonicalization?
4669d99cbe66403ee39c2ee58024b9582b95649a4fc5Douglas Gregor  llvm::FoldingSetNodeID ID;
4670d99cbe66403ee39c2ee58024b9582b95649a4fc5Douglas Gregor  QualifiedTemplateName::Profile(ID, NNS, TemplateKeyword, Template);
46711eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
4672d99cbe66403ee39c2ee58024b9582b95649a4fc5Douglas Gregor  void *InsertPos = 0;
4673d99cbe66403ee39c2ee58024b9582b95649a4fc5Douglas Gregor  QualifiedTemplateName *QTN =
46740bd6feb9e9d40fc889fd47e899985125a43dfed8John McCall    QualifiedTemplateNames.FindNodeOrInsertPos(ID, InsertPos);
4675d99cbe66403ee39c2ee58024b9582b95649a4fc5Douglas Gregor  if (!QTN) {
4676d99cbe66403ee39c2ee58024b9582b95649a4fc5Douglas Gregor    QTN = new (*this,4) QualifiedTemplateName(NNS, TemplateKeyword, Template);
4677d99cbe66403ee39c2ee58024b9582b95649a4fc5Douglas Gregor    QualifiedTemplateNames.InsertNode(QTN, InsertPos);
4678d99cbe66403ee39c2ee58024b9582b95649a4fc5Douglas Gregor  }
46791eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
4680d99cbe66403ee39c2ee58024b9582b95649a4fc5Douglas Gregor  return TemplateName(QTN);
4681d99cbe66403ee39c2ee58024b9582b95649a4fc5Douglas Gregor}
4682d99cbe66403ee39c2ee58024b9582b95649a4fc5Douglas Gregor
46837532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor/// \brief Retrieve the template name that represents a dependent
46847532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor/// template name such as \c MetaFun::template apply.
46854ba2a17694148e16eaa8d3917f657ffcd3667be4Jay FoadTemplateName
46864ba2a17694148e16eaa8d3917f657ffcd3667be4Jay FoadASTContext::getDependentTemplateName(NestedNameSpecifier *NNS,
46874ba2a17694148e16eaa8d3917f657ffcd3667be4Jay Foad                                     const IdentifierInfo *Name) const {
46881eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  assert((!NNS || NNS->isDependent()) &&
46893b6afbb99a1c44b4076f8e15fb7311405941b306Douglas Gregor         "Nested name specifier must be dependent");
46907532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor
46917532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor  llvm::FoldingSetNodeID ID;
46927532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor  DependentTemplateName::Profile(ID, NNS, Name);
46937532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor
46947532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor  void *InsertPos = 0;
46957532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor  DependentTemplateName *QTN =
46967532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor    DependentTemplateNames.FindNodeOrInsertPos(ID, InsertPos);
46977532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor
46987532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor  if (QTN)
46997532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor    return TemplateName(QTN);
47007532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor
47017532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor  NestedNameSpecifier *CanonNNS = getCanonicalNestedNameSpecifier(NNS);
47027532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor  if (CanonNNS == NNS) {
47037532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor    QTN = new (*this,4) DependentTemplateName(NNS, Name);
47047532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor  } else {
47057532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor    TemplateName Canon = getDependentTemplateName(CanonNNS, Name);
47067532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor    QTN = new (*this,4) DependentTemplateName(NNS, Name, Canon);
4707789b1f640205e81b5af250693246120f1ce9d147Douglas Gregor    DependentTemplateName *CheckQTN =
4708789b1f640205e81b5af250693246120f1ce9d147Douglas Gregor      DependentTemplateNames.FindNodeOrInsertPos(ID, InsertPos);
4709789b1f640205e81b5af250693246120f1ce9d147Douglas Gregor    assert(!CheckQTN && "Dependent type name canonicalization broken");
4710789b1f640205e81b5af250693246120f1ce9d147Douglas Gregor    (void)CheckQTN;
47117532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor  }
47127532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor
47137532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor  DependentTemplateNames.InsertNode(QTN, InsertPos);
47147532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor  return TemplateName(QTN);
47157532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor}
47167532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor
4717ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor/// \brief Retrieve the template name that represents a dependent
4718ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor/// template name such as \c MetaFun::template operator+.
4719ca1bdd7c269a2390d43c040a60511edd017ee130Douglas GregorTemplateName
4720ca1bdd7c269a2390d43c040a60511edd017ee130Douglas GregorASTContext::getDependentTemplateName(NestedNameSpecifier *NNS,
47214ba2a17694148e16eaa8d3917f657ffcd3667be4Jay Foad                                     OverloadedOperatorKind Operator) const {
4722ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor  assert((!NNS || NNS->isDependent()) &&
4723ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor         "Nested name specifier must be dependent");
4724ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor
4725ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor  llvm::FoldingSetNodeID ID;
4726ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor  DependentTemplateName::Profile(ID, NNS, Operator);
4727ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor
4728ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor  void *InsertPos = 0;
4729789b1f640205e81b5af250693246120f1ce9d147Douglas Gregor  DependentTemplateName *QTN
4730789b1f640205e81b5af250693246120f1ce9d147Douglas Gregor    = DependentTemplateNames.FindNodeOrInsertPos(ID, InsertPos);
4731ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor
4732ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor  if (QTN)
4733ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor    return TemplateName(QTN);
4734ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor
4735ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor  NestedNameSpecifier *CanonNNS = getCanonicalNestedNameSpecifier(NNS);
4736ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor  if (CanonNNS == NNS) {
4737ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor    QTN = new (*this,4) DependentTemplateName(NNS, Operator);
4738ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor  } else {
4739ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor    TemplateName Canon = getDependentTemplateName(CanonNNS, Operator);
4740ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor    QTN = new (*this,4) DependentTemplateName(NNS, Operator, Canon);
4741789b1f640205e81b5af250693246120f1ce9d147Douglas Gregor
4742789b1f640205e81b5af250693246120f1ce9d147Douglas Gregor    DependentTemplateName *CheckQTN
4743789b1f640205e81b5af250693246120f1ce9d147Douglas Gregor      = DependentTemplateNames.FindNodeOrInsertPos(ID, InsertPos);
4744789b1f640205e81b5af250693246120f1ce9d147Douglas Gregor    assert(!CheckQTN && "Dependent template name canonicalization broken");
4745789b1f640205e81b5af250693246120f1ce9d147Douglas Gregor    (void)CheckQTN;
4746ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor  }
4747ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor
4748ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor  DependentTemplateNames.InsertNode(QTN, InsertPos);
4749ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor  return TemplateName(QTN);
4750ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor}
4751ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor
47521aee05d08b2184acadeb36de300e216390780d6cDouglas GregorTemplateName
47531aee05d08b2184acadeb36de300e216390780d6cDouglas GregorASTContext::getSubstTemplateTemplateParmPack(TemplateTemplateParmDecl *Param,
47541aee05d08b2184acadeb36de300e216390780d6cDouglas Gregor                                       const TemplateArgument &ArgPack) const {
47551aee05d08b2184acadeb36de300e216390780d6cDouglas Gregor  ASTContext &Self = const_cast<ASTContext &>(*this);
47561aee05d08b2184acadeb36de300e216390780d6cDouglas Gregor  llvm::FoldingSetNodeID ID;
47571aee05d08b2184acadeb36de300e216390780d6cDouglas Gregor  SubstTemplateTemplateParmPackStorage::Profile(ID, Self, Param, ArgPack);
47581aee05d08b2184acadeb36de300e216390780d6cDouglas Gregor
47591aee05d08b2184acadeb36de300e216390780d6cDouglas Gregor  void *InsertPos = 0;
47601aee05d08b2184acadeb36de300e216390780d6cDouglas Gregor  SubstTemplateTemplateParmPackStorage *Subst
47611aee05d08b2184acadeb36de300e216390780d6cDouglas Gregor    = SubstTemplateTemplateParmPacks.FindNodeOrInsertPos(ID, InsertPos);
47621aee05d08b2184acadeb36de300e216390780d6cDouglas Gregor
47631aee05d08b2184acadeb36de300e216390780d6cDouglas Gregor  if (!Subst) {
47641aee05d08b2184acadeb36de300e216390780d6cDouglas Gregor    Subst = new (*this) SubstTemplateTemplateParmPackStorage(Self, Param,
47651aee05d08b2184acadeb36de300e216390780d6cDouglas Gregor                                                           ArgPack.pack_size(),
47661aee05d08b2184acadeb36de300e216390780d6cDouglas Gregor                                                         ArgPack.pack_begin());
47671aee05d08b2184acadeb36de300e216390780d6cDouglas Gregor    SubstTemplateTemplateParmPacks.InsertNode(Subst, InsertPos);
47681aee05d08b2184acadeb36de300e216390780d6cDouglas Gregor  }
47691aee05d08b2184acadeb36de300e216390780d6cDouglas Gregor
47701aee05d08b2184acadeb36de300e216390780d6cDouglas Gregor  return TemplateName(Subst);
47711aee05d08b2184acadeb36de300e216390780d6cDouglas Gregor}
47721aee05d08b2184acadeb36de300e216390780d6cDouglas Gregor
4773b4e66d5259f90e9aae4d40fc5de801e046c7df94Douglas Gregor/// getFromTargetType - Given one of the integer types provided by
4774d934112e6170b0fd940d8e40db6936cea2cdcf62Douglas Gregor/// TargetInfo, produce the corresponding type. The unsigned @p Type
4775d934112e6170b0fd940d8e40db6936cea2cdcf62Douglas Gregor/// is actually a value of type @c TargetInfo::IntType.
4776e27ec8ad56dbf1efb2de004b90fbbb86f740e3f1John McCallCanQualType ASTContext::getFromTargetType(unsigned Type) const {
4777b4e66d5259f90e9aae4d40fc5de801e046c7df94Douglas Gregor  switch (Type) {
4778e27ec8ad56dbf1efb2de004b90fbbb86f740e3f1John McCall  case TargetInfo::NoInt: return CanQualType();
4779b4e66d5259f90e9aae4d40fc5de801e046c7df94Douglas Gregor  case TargetInfo::SignedShort: return ShortTy;
4780b4e66d5259f90e9aae4d40fc5de801e046c7df94Douglas Gregor  case TargetInfo::UnsignedShort: return UnsignedShortTy;
4781b4e66d5259f90e9aae4d40fc5de801e046c7df94Douglas Gregor  case TargetInfo::SignedInt: return IntTy;
4782b4e66d5259f90e9aae4d40fc5de801e046c7df94Douglas Gregor  case TargetInfo::UnsignedInt: return UnsignedIntTy;
4783b4e66d5259f90e9aae4d40fc5de801e046c7df94Douglas Gregor  case TargetInfo::SignedLong: return LongTy;
4784b4e66d5259f90e9aae4d40fc5de801e046c7df94Douglas Gregor  case TargetInfo::UnsignedLong: return UnsignedLongTy;
4785b4e66d5259f90e9aae4d40fc5de801e046c7df94Douglas Gregor  case TargetInfo::SignedLongLong: return LongLongTy;
4786b4e66d5259f90e9aae4d40fc5de801e046c7df94Douglas Gregor  case TargetInfo::UnsignedLongLong: return UnsignedLongLongTy;
4787b4e66d5259f90e9aae4d40fc5de801e046c7df94Douglas Gregor  }
4788b4e66d5259f90e9aae4d40fc5de801e046c7df94Douglas Gregor
4789b4e66d5259f90e9aae4d40fc5de801e046c7df94Douglas Gregor  assert(false && "Unhandled TargetInfo::IntType value");
4790e27ec8ad56dbf1efb2de004b90fbbb86f740e3f1John McCall  return CanQualType();
4791b4e66d5259f90e9aae4d40fc5de801e046c7df94Douglas Gregor}
4792b6ccaac65ca72f72954eb3893bbd940bedd23f00Ted Kremenek
4793b6ccaac65ca72f72954eb3893bbd940bedd23f00Ted Kremenek//===----------------------------------------------------------------------===//
4794b6ccaac65ca72f72954eb3893bbd940bedd23f00Ted Kremenek//                        Type Predicates.
4795b6ccaac65ca72f72954eb3893bbd940bedd23f00Ted Kremenek//===----------------------------------------------------------------------===//
4796b6ccaac65ca72f72954eb3893bbd940bedd23f00Ted Kremenek
4797fa23c1d9adc99c662c1c0e192817185809d95614Fariborz Jahanian/// isObjCNSObjectType - Return true if this is an NSObject object using
4798fa23c1d9adc99c662c1c0e192817185809d95614Fariborz Jahanian/// NSObject attribute on a c-style pointer type.
4799fa23c1d9adc99c662c1c0e192817185809d95614Fariborz Jahanian/// FIXME - Make it work directly on types.
4800f49545602089be5b1f744e04326b8a566f6d8773Steve Naroff/// FIXME: Move to Type.
4801fa23c1d9adc99c662c1c0e192817185809d95614Fariborz Jahanian///
4802fa23c1d9adc99c662c1c0e192817185809d95614Fariborz Jahanianbool ASTContext::isObjCNSObjectType(QualType Ty) const {
4803f4c7371fb1d3cebcfb40abad4537bb82515704eaJohn McCall  if (const TypedefType *TDT = dyn_cast<TypedefType>(Ty)) {
4804162e1c1b487352434552147967c3dd296ebee2f7Richard Smith    if (TypedefNameDecl *TD = TDT->getDecl())
480540b598eea1310ec9ed554d56ce3e25b34c585458Argyrios Kyrtzidis      if (TD->getAttr<ObjCNSObjectAttr>())
4806fa23c1d9adc99c662c1c0e192817185809d95614Fariborz Jahanian        return true;
4807fa23c1d9adc99c662c1c0e192817185809d95614Fariborz Jahanian  }
48081eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  return false;
4809fa23c1d9adc99c662c1c0e192817185809d95614Fariborz Jahanian}
4810fa23c1d9adc99c662c1c0e192817185809d95614Fariborz Jahanian
48114fd83ea566f4a0c083001c84b75da6cc8c99c1d6Fariborz Jahanian/// getObjCGCAttr - Returns one of GCNone, Weak or Strong objc's
48124fd83ea566f4a0c083001c84b75da6cc8c99c1d6Fariborz Jahanian/// garbage collection attribute.
48134fd83ea566f4a0c083001c84b75da6cc8c99c1d6Fariborz Jahanian///
4814ae278a3a57595349a411f6474938d4dd1b263a0eJohn McCallQualifiers::GC ASTContext::getObjCGCAttrKind(QualType Ty) const {
4815ae278a3a57595349a411f6474938d4dd1b263a0eJohn McCall  if (getLangOptions().getGCMode() == LangOptions::NonGC)
4816ae278a3a57595349a411f6474938d4dd1b263a0eJohn McCall    return Qualifiers::GCNone;
4817ae278a3a57595349a411f6474938d4dd1b263a0eJohn McCall
4818ae278a3a57595349a411f6474938d4dd1b263a0eJohn McCall  assert(getLangOptions().ObjC1);
4819ae278a3a57595349a411f6474938d4dd1b263a0eJohn McCall  Qualifiers::GC GCAttrs = Ty.getObjCGCAttr();
4820ae278a3a57595349a411f6474938d4dd1b263a0eJohn McCall
4821ae278a3a57595349a411f6474938d4dd1b263a0eJohn McCall  // Default behaviour under objective-C's gc is for ObjC pointers
4822ae278a3a57595349a411f6474938d4dd1b263a0eJohn McCall  // (or pointers to them) be treated as though they were declared
4823ae278a3a57595349a411f6474938d4dd1b263a0eJohn McCall  // as __strong.
4824ae278a3a57595349a411f6474938d4dd1b263a0eJohn McCall  if (GCAttrs == Qualifiers::GCNone) {
4825ae278a3a57595349a411f6474938d4dd1b263a0eJohn McCall    if (Ty->isObjCObjectPointerType() || Ty->isBlockPointerType())
4826ae278a3a57595349a411f6474938d4dd1b263a0eJohn McCall      return Qualifiers::Strong;
4827ae278a3a57595349a411f6474938d4dd1b263a0eJohn McCall    else if (Ty->isPointerType())
4828ae278a3a57595349a411f6474938d4dd1b263a0eJohn McCall      return getObjCGCAttrKind(Ty->getAs<PointerType>()->getPointeeType());
4829ae278a3a57595349a411f6474938d4dd1b263a0eJohn McCall  } else {
4830ae278a3a57595349a411f6474938d4dd1b263a0eJohn McCall    // It's not valid to set GC attributes on anything that isn't a
4831ae278a3a57595349a411f6474938d4dd1b263a0eJohn McCall    // pointer.
4832ae278a3a57595349a411f6474938d4dd1b263a0eJohn McCall#ifndef NDEBUG
4833ae278a3a57595349a411f6474938d4dd1b263a0eJohn McCall    QualType CT = Ty->getCanonicalTypeInternal();
4834ae278a3a57595349a411f6474938d4dd1b263a0eJohn McCall    while (const ArrayType *AT = dyn_cast<ArrayType>(CT))
4835ae278a3a57595349a411f6474938d4dd1b263a0eJohn McCall      CT = AT->getElementType();
4836ae278a3a57595349a411f6474938d4dd1b263a0eJohn McCall    assert(CT->isAnyPointerType() || CT->isBlockPointerType());
4837ae278a3a57595349a411f6474938d4dd1b263a0eJohn McCall#endif
48384fd83ea566f4a0c083001c84b75da6cc8c99c1d6Fariborz Jahanian  }
4839b7d2553edd2532d29b98b9e76bcf6a62bc48b417Chris Lattner  return GCAttrs;
48404fd83ea566f4a0c083001c84b75da6cc8c99c1d6Fariborz Jahanian}
48414fd83ea566f4a0c083001c84b75da6cc8c99c1d6Fariborz Jahanian
48426ac46a4a521366d7ab36ebe2ce4e624ab96b06f9Chris Lattner//===----------------------------------------------------------------------===//
48436ac46a4a521366d7ab36ebe2ce4e624ab96b06f9Chris Lattner//                        Type Compatibility Testing
48446ac46a4a521366d7ab36ebe2ce4e624ab96b06f9Chris Lattner//===----------------------------------------------------------------------===//
4845770951b5bb6028a8d326ddb4a13cef7d4a128162Chris Lattner
48461eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// areCompatVectorTypes - Return true if the two specified vector types are
48476ac46a4a521366d7ab36ebe2ce4e624ab96b06f9Chris Lattner/// compatible.
48486ac46a4a521366d7ab36ebe2ce4e624ab96b06f9Chris Lattnerstatic bool areCompatVectorTypes(const VectorType *LHS,
48496ac46a4a521366d7ab36ebe2ce4e624ab96b06f9Chris Lattner                                 const VectorType *RHS) {
4850467b27b9a24bdc823218ad1ad0e37673b6cc1e83John McCall  assert(LHS->isCanonicalUnqualified() && RHS->isCanonicalUnqualified());
48516ac46a4a521366d7ab36ebe2ce4e624ab96b06f9Chris Lattner  return LHS->getElementType() == RHS->getElementType() &&
485261710854be2b098428aff5316e64bd34b30fbcb7Chris Lattner         LHS->getNumElements() == RHS->getNumElements();
48536ac46a4a521366d7ab36ebe2ce4e624ab96b06f9Chris Lattner}
48546ac46a4a521366d7ab36ebe2ce4e624ab96b06f9Chris Lattner
4855255210ef415b9893f0e3794e8d9a704194c12f3cDouglas Gregorbool ASTContext::areCompatibleVectorTypes(QualType FirstVec,
4856255210ef415b9893f0e3794e8d9a704194c12f3cDouglas Gregor                                          QualType SecondVec) {
4857255210ef415b9893f0e3794e8d9a704194c12f3cDouglas Gregor  assert(FirstVec->isVectorType() && "FirstVec should be a vector type");
4858255210ef415b9893f0e3794e8d9a704194c12f3cDouglas Gregor  assert(SecondVec->isVectorType() && "SecondVec should be a vector type");
4859255210ef415b9893f0e3794e8d9a704194c12f3cDouglas Gregor
4860255210ef415b9893f0e3794e8d9a704194c12f3cDouglas Gregor  if (hasSameUnqualifiedType(FirstVec, SecondVec))
4861255210ef415b9893f0e3794e8d9a704194c12f3cDouglas Gregor    return true;
4862255210ef415b9893f0e3794e8d9a704194c12f3cDouglas Gregor
4863f69eb7cf8e616b5aad7911ec6f79b24b0a009227Bob Wilson  // Treat Neon vector types and most AltiVec vector types as if they are the
4864f69eb7cf8e616b5aad7911ec6f79b24b0a009227Bob Wilson  // equivalent GCC vector types.
4865255210ef415b9893f0e3794e8d9a704194c12f3cDouglas Gregor  const VectorType *First = FirstVec->getAs<VectorType>();
4866255210ef415b9893f0e3794e8d9a704194c12f3cDouglas Gregor  const VectorType *Second = SecondVec->getAs<VectorType>();
4867f69eb7cf8e616b5aad7911ec6f79b24b0a009227Bob Wilson  if (First->getNumElements() == Second->getNumElements() &&
4868255210ef415b9893f0e3794e8d9a704194c12f3cDouglas Gregor      hasSameType(First->getElementType(), Second->getElementType()) &&
4869f69eb7cf8e616b5aad7911ec6f79b24b0a009227Bob Wilson      First->getVectorKind() != VectorType::AltiVecPixel &&
4870f69eb7cf8e616b5aad7911ec6f79b24b0a009227Bob Wilson      First->getVectorKind() != VectorType::AltiVecBool &&
4871f69eb7cf8e616b5aad7911ec6f79b24b0a009227Bob Wilson      Second->getVectorKind() != VectorType::AltiVecPixel &&
4872f69eb7cf8e616b5aad7911ec6f79b24b0a009227Bob Wilson      Second->getVectorKind() != VectorType::AltiVecBool)
4873255210ef415b9893f0e3794e8d9a704194c12f3cDouglas Gregor    return true;
4874255210ef415b9893f0e3794e8d9a704194c12f3cDouglas Gregor
4875255210ef415b9893f0e3794e8d9a704194c12f3cDouglas Gregor  return false;
4876255210ef415b9893f0e3794e8d9a704194c12f3cDouglas Gregor}
4877255210ef415b9893f0e3794e8d9a704194c12f3cDouglas Gregor
48784084c306635b70f37029dca938444e6013f08684Steve Naroff//===----------------------------------------------------------------------===//
48794084c306635b70f37029dca938444e6013f08684Steve Naroff// ObjCQualifiedIdTypesAreCompatible - Compatibility testing for qualified id's.
48804084c306635b70f37029dca938444e6013f08684Steve Naroff//===----------------------------------------------------------------------===//
48814084c306635b70f37029dca938444e6013f08684Steve Naroff
48824084c306635b70f37029dca938444e6013f08684Steve Naroff/// ProtocolCompatibleWithProtocol - return 'true' if 'lProto' is in the
48834084c306635b70f37029dca938444e6013f08684Steve Naroff/// inheritance hierarchy of 'rProto'.
48844ba2a17694148e16eaa8d3917f657ffcd3667be4Jay Foadbool
48854ba2a17694148e16eaa8d3917f657ffcd3667be4Jay FoadASTContext::ProtocolCompatibleWithProtocol(ObjCProtocolDecl *lProto,
48864ba2a17694148e16eaa8d3917f657ffcd3667be4Jay Foad                                           ObjCProtocolDecl *rProto) const {
48874084c306635b70f37029dca938444e6013f08684Steve Naroff  if (lProto == rProto)
488814108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff    return true;
48894084c306635b70f37029dca938444e6013f08684Steve Naroff  for (ObjCProtocolDecl::protocol_iterator PI = rProto->protocol_begin(),
48904084c306635b70f37029dca938444e6013f08684Steve Naroff       E = rProto->protocol_end(); PI != E; ++PI)
48914084c306635b70f37029dca938444e6013f08684Steve Naroff    if (ProtocolCompatibleWithProtocol(lProto, *PI))
48924084c306635b70f37029dca938444e6013f08684Steve Naroff      return true;
48934084c306635b70f37029dca938444e6013f08684Steve Naroff  return false;
48944084c306635b70f37029dca938444e6013f08684Steve Naroff}
489514108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff
48964084c306635b70f37029dca938444e6013f08684Steve Naroff/// QualifiedIdConformsQualifiedId - compare id<p,...> with id<p1,...>
48974084c306635b70f37029dca938444e6013f08684Steve Naroff/// return true if lhs's protocols conform to rhs's protocol; false
48984084c306635b70f37029dca938444e6013f08684Steve Naroff/// otherwise.
48994084c306635b70f37029dca938444e6013f08684Steve Naroffbool ASTContext::QualifiedIdConformsQualifiedId(QualType lhs, QualType rhs) {
49004084c306635b70f37029dca938444e6013f08684Steve Naroff  if (lhs->isObjCQualifiedIdType() && rhs->isObjCQualifiedIdType())
49014084c306635b70f37029dca938444e6013f08684Steve Naroff    return ObjCQualifiedIdTypesAreCompatible(lhs, rhs, false);
49024084c306635b70f37029dca938444e6013f08684Steve Naroff  return false;
49034084c306635b70f37029dca938444e6013f08684Steve Naroff}
49044084c306635b70f37029dca938444e6013f08684Steve Naroff
4905a8f8dac6a29f6d33474a38a32ce9dd859b696da9Fariborz Jahanian/// ObjCQualifiedClassTypesAreCompatible - compare  Class<p,...> and
4906a8f8dac6a29f6d33474a38a32ce9dd859b696da9Fariborz Jahanian/// Class<p1, ...>.
4907a8f8dac6a29f6d33474a38a32ce9dd859b696da9Fariborz Jahanianbool ASTContext::ObjCQualifiedClassTypesAreCompatible(QualType lhs,
4908a8f8dac6a29f6d33474a38a32ce9dd859b696da9Fariborz Jahanian                                                      QualType rhs) {
4909a8f8dac6a29f6d33474a38a32ce9dd859b696da9Fariborz Jahanian  const ObjCObjectPointerType *lhsQID = lhs->getAs<ObjCObjectPointerType>();
4910a8f8dac6a29f6d33474a38a32ce9dd859b696da9Fariborz Jahanian  const ObjCObjectPointerType *rhsOPT = rhs->getAs<ObjCObjectPointerType>();
4911a8f8dac6a29f6d33474a38a32ce9dd859b696da9Fariborz Jahanian  assert ((lhsQID && rhsOPT) && "ObjCQualifiedClassTypesAreCompatible");
4912a8f8dac6a29f6d33474a38a32ce9dd859b696da9Fariborz Jahanian
4913a8f8dac6a29f6d33474a38a32ce9dd859b696da9Fariborz Jahanian  for (ObjCObjectPointerType::qual_iterator I = lhsQID->qual_begin(),
4914a8f8dac6a29f6d33474a38a32ce9dd859b696da9Fariborz Jahanian       E = lhsQID->qual_end(); I != E; ++I) {
4915a8f8dac6a29f6d33474a38a32ce9dd859b696da9Fariborz Jahanian    bool match = false;
4916a8f8dac6a29f6d33474a38a32ce9dd859b696da9Fariborz Jahanian    ObjCProtocolDecl *lhsProto = *I;
4917a8f8dac6a29f6d33474a38a32ce9dd859b696da9Fariborz Jahanian    for (ObjCObjectPointerType::qual_iterator J = rhsOPT->qual_begin(),
4918a8f8dac6a29f6d33474a38a32ce9dd859b696da9Fariborz Jahanian         E = rhsOPT->qual_end(); J != E; ++J) {
4919a8f8dac6a29f6d33474a38a32ce9dd859b696da9Fariborz Jahanian      ObjCProtocolDecl *rhsProto = *J;
4920a8f8dac6a29f6d33474a38a32ce9dd859b696da9Fariborz Jahanian      if (ProtocolCompatibleWithProtocol(lhsProto, rhsProto)) {
4921a8f8dac6a29f6d33474a38a32ce9dd859b696da9Fariborz Jahanian        match = true;
4922a8f8dac6a29f6d33474a38a32ce9dd859b696da9Fariborz Jahanian        break;
4923a8f8dac6a29f6d33474a38a32ce9dd859b696da9Fariborz Jahanian      }
4924a8f8dac6a29f6d33474a38a32ce9dd859b696da9Fariborz Jahanian    }
4925a8f8dac6a29f6d33474a38a32ce9dd859b696da9Fariborz Jahanian    if (!match)
4926a8f8dac6a29f6d33474a38a32ce9dd859b696da9Fariborz Jahanian      return false;
4927a8f8dac6a29f6d33474a38a32ce9dd859b696da9Fariborz Jahanian  }
4928a8f8dac6a29f6d33474a38a32ce9dd859b696da9Fariborz Jahanian  return true;
4929a8f8dac6a29f6d33474a38a32ce9dd859b696da9Fariborz Jahanian}
4930a8f8dac6a29f6d33474a38a32ce9dd859b696da9Fariborz Jahanian
49314084c306635b70f37029dca938444e6013f08684Steve Naroff/// ObjCQualifiedIdTypesAreCompatible - We know that one of lhs/rhs is an
49324084c306635b70f37029dca938444e6013f08684Steve Naroff/// ObjCQualifiedIDType.
49334084c306635b70f37029dca938444e6013f08684Steve Naroffbool ASTContext::ObjCQualifiedIdTypesAreCompatible(QualType lhs, QualType rhs,
49344084c306635b70f37029dca938444e6013f08684Steve Naroff                                                   bool compare) {
49354084c306635b70f37029dca938444e6013f08684Steve Naroff  // Allow id<P..> and an 'id' or void* type in all cases.
49361eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  if (lhs->isVoidPointerType() ||
49374084c306635b70f37029dca938444e6013f08684Steve Naroff      lhs->isObjCIdType() || lhs->isObjCClassType())
49384084c306635b70f37029dca938444e6013f08684Steve Naroff    return true;
49391eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  else if (rhs->isVoidPointerType() ||
49404084c306635b70f37029dca938444e6013f08684Steve Naroff           rhs->isObjCIdType() || rhs->isObjCClassType())
49414084c306635b70f37029dca938444e6013f08684Steve Naroff    return true;
49424084c306635b70f37029dca938444e6013f08684Steve Naroff
49434084c306635b70f37029dca938444e6013f08684Steve Naroff  if (const ObjCObjectPointerType *lhsQID = lhs->getAsObjCQualifiedIdType()) {
4944183700f494ec9b6701b6efe82bcb25f4c79ba561John McCall    const ObjCObjectPointerType *rhsOPT = rhs->getAs<ObjCObjectPointerType>();
49451eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
49464084c306635b70f37029dca938444e6013f08684Steve Naroff    if (!rhsOPT) return false;
49471eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
49484084c306635b70f37029dca938444e6013f08684Steve Naroff    if (rhsOPT->qual_empty()) {
49491eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      // If the RHS is a unqualified interface pointer "NSString*",
49504084c306635b70f37029dca938444e6013f08684Steve Naroff      // make sure we check the class hierarchy.
49514084c306635b70f37029dca938444e6013f08684Steve Naroff      if (ObjCInterfaceDecl *rhsID = rhsOPT->getInterfaceDecl()) {
49524084c306635b70f37029dca938444e6013f08684Steve Naroff        for (ObjCObjectPointerType::qual_iterator I = lhsQID->qual_begin(),
49534084c306635b70f37029dca938444e6013f08684Steve Naroff             E = lhsQID->qual_end(); I != E; ++I) {
49544084c306635b70f37029dca938444e6013f08684Steve Naroff          // when comparing an id<P> on lhs with a static type on rhs,
49554084c306635b70f37029dca938444e6013f08684Steve Naroff          // see if static class implements all of id's protocols, directly or
49564084c306635b70f37029dca938444e6013f08684Steve Naroff          // through its super class and categories.
49570fd8904c5f71a11d29f67716c3ebdf7ad1c855fbFariborz Jahanian          if (!rhsID->ClassImplementsProtocol(*I, true))
49584084c306635b70f37029dca938444e6013f08684Steve Naroff            return false;
49594084c306635b70f37029dca938444e6013f08684Steve Naroff        }
49604084c306635b70f37029dca938444e6013f08684Steve Naroff      }
49614084c306635b70f37029dca938444e6013f08684Steve Naroff      // If there are no qualifiers and no interface, we have an 'id'.
49624084c306635b70f37029dca938444e6013f08684Steve Naroff      return true;
49634084c306635b70f37029dca938444e6013f08684Steve Naroff    }
49641eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    // Both the right and left sides have qualifiers.
49654084c306635b70f37029dca938444e6013f08684Steve Naroff    for (ObjCObjectPointerType::qual_iterator I = lhsQID->qual_begin(),
49664084c306635b70f37029dca938444e6013f08684Steve Naroff         E = lhsQID->qual_end(); I != E; ++I) {
49674084c306635b70f37029dca938444e6013f08684Steve Naroff      ObjCProtocolDecl *lhsProto = *I;
49684084c306635b70f37029dca938444e6013f08684Steve Naroff      bool match = false;
4969de2e22d33afec98324a66a358dfe0951b3c7259aSteve Naroff
4970de2e22d33afec98324a66a358dfe0951b3c7259aSteve Naroff      // when comparing an id<P> on lhs with a static type on rhs,
4971de2e22d33afec98324a66a358dfe0951b3c7259aSteve Naroff      // see if static class implements all of id's protocols, directly or
4972de2e22d33afec98324a66a358dfe0951b3c7259aSteve Naroff      // through its super class and categories.
49734084c306635b70f37029dca938444e6013f08684Steve Naroff      for (ObjCObjectPointerType::qual_iterator J = rhsOPT->qual_begin(),
49744084c306635b70f37029dca938444e6013f08684Steve Naroff           E = rhsOPT->qual_end(); J != E; ++J) {
49754084c306635b70f37029dca938444e6013f08684Steve Naroff        ObjCProtocolDecl *rhsProto = *J;
49764084c306635b70f37029dca938444e6013f08684Steve Naroff        if (ProtocolCompatibleWithProtocol(lhsProto, rhsProto) ||
49774084c306635b70f37029dca938444e6013f08684Steve Naroff            (compare && ProtocolCompatibleWithProtocol(rhsProto, lhsProto))) {
49784084c306635b70f37029dca938444e6013f08684Steve Naroff          match = true;
49798f16756441450ed9fb39316e47d107fc2a1ef35bSteve Naroff          break;
49808f16756441450ed9fb39316e47d107fc2a1ef35bSteve Naroff        }
4981de2e22d33afec98324a66a358dfe0951b3c7259aSteve Naroff      }
49821eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      // If the RHS is a qualified interface pointer "NSString<P>*",
49834084c306635b70f37029dca938444e6013f08684Steve Naroff      // make sure we check the class hierarchy.
49844084c306635b70f37029dca938444e6013f08684Steve Naroff      if (ObjCInterfaceDecl *rhsID = rhsOPT->getInterfaceDecl()) {
49854084c306635b70f37029dca938444e6013f08684Steve Naroff        for (ObjCObjectPointerType::qual_iterator I = lhsQID->qual_begin(),
49864084c306635b70f37029dca938444e6013f08684Steve Naroff             E = lhsQID->qual_end(); I != E; ++I) {
49874084c306635b70f37029dca938444e6013f08684Steve Naroff          // when comparing an id<P> on lhs with a static type on rhs,
49884084c306635b70f37029dca938444e6013f08684Steve Naroff          // see if static class implements all of id's protocols, directly or
49894084c306635b70f37029dca938444e6013f08684Steve Naroff          // through its super class and categories.
49900fd8904c5f71a11d29f67716c3ebdf7ad1c855fbFariborz Jahanian          if (rhsID->ClassImplementsProtocol(*I, true)) {
49914084c306635b70f37029dca938444e6013f08684Steve Naroff            match = true;
49924084c306635b70f37029dca938444e6013f08684Steve Naroff            break;
49934084c306635b70f37029dca938444e6013f08684Steve Naroff          }
49944084c306635b70f37029dca938444e6013f08684Steve Naroff        }
49954084c306635b70f37029dca938444e6013f08684Steve Naroff      }
49964084c306635b70f37029dca938444e6013f08684Steve Naroff      if (!match)
4997de2e22d33afec98324a66a358dfe0951b3c7259aSteve Naroff        return false;
4998de2e22d33afec98324a66a358dfe0951b3c7259aSteve Naroff    }
49991eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
5000de2e22d33afec98324a66a358dfe0951b3c7259aSteve Naroff    return true;
5001de2e22d33afec98324a66a358dfe0951b3c7259aSteve Naroff  }
50021eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
50034084c306635b70f37029dca938444e6013f08684Steve Naroff  const ObjCObjectPointerType *rhsQID = rhs->getAsObjCQualifiedIdType();
50044084c306635b70f37029dca938444e6013f08684Steve Naroff  assert(rhsQID && "One of the LHS/RHS should be id<x>");
50054084c306635b70f37029dca938444e6013f08684Steve Naroff
50061eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  if (const ObjCObjectPointerType *lhsOPT =
50074084c306635b70f37029dca938444e6013f08684Steve Naroff        lhs->getAsObjCInterfacePointerType()) {
5008de5b17ea3c74d2844ed035a1edfa6479866139b5Fariborz Jahanian    // If both the right and left sides have qualifiers.
50094084c306635b70f37029dca938444e6013f08684Steve Naroff    for (ObjCObjectPointerType::qual_iterator I = lhsOPT->qual_begin(),
50104084c306635b70f37029dca938444e6013f08684Steve Naroff         E = lhsOPT->qual_end(); I != E; ++I) {
50114084c306635b70f37029dca938444e6013f08684Steve Naroff      ObjCProtocolDecl *lhsProto = *I;
50124084c306635b70f37029dca938444e6013f08684Steve Naroff      bool match = false;
50134084c306635b70f37029dca938444e6013f08684Steve Naroff
5014de5b17ea3c74d2844ed035a1edfa6479866139b5Fariborz Jahanian      // when comparing an id<P> on rhs with a static type on lhs,
50154084c306635b70f37029dca938444e6013f08684Steve Naroff      // see if static class implements all of id's protocols, directly or
50164084c306635b70f37029dca938444e6013f08684Steve Naroff      // through its super class and categories.
5017de5b17ea3c74d2844ed035a1edfa6479866139b5Fariborz Jahanian      // First, lhs protocols in the qualifier list must be found, direct
5018de5b17ea3c74d2844ed035a1edfa6479866139b5Fariborz Jahanian      // or indirect in rhs's qualifier list or it is a mismatch.
50194084c306635b70f37029dca938444e6013f08684Steve Naroff      for (ObjCObjectPointerType::qual_iterator J = rhsQID->qual_begin(),
50204084c306635b70f37029dca938444e6013f08684Steve Naroff           E = rhsQID->qual_end(); J != E; ++J) {
50214084c306635b70f37029dca938444e6013f08684Steve Naroff        ObjCProtocolDecl *rhsProto = *J;
50224084c306635b70f37029dca938444e6013f08684Steve Naroff        if (ProtocolCompatibleWithProtocol(lhsProto, rhsProto) ||
50234084c306635b70f37029dca938444e6013f08684Steve Naroff            (compare && ProtocolCompatibleWithProtocol(rhsProto, lhsProto))) {
50244084c306635b70f37029dca938444e6013f08684Steve Naroff          match = true;
50254084c306635b70f37029dca938444e6013f08684Steve Naroff          break;
50264084c306635b70f37029dca938444e6013f08684Steve Naroff        }
50274084c306635b70f37029dca938444e6013f08684Steve Naroff      }
50284084c306635b70f37029dca938444e6013f08684Steve Naroff      if (!match)
50294084c306635b70f37029dca938444e6013f08684Steve Naroff        return false;
50304084c306635b70f37029dca938444e6013f08684Steve Naroff    }
5031de5b17ea3c74d2844ed035a1edfa6479866139b5Fariborz Jahanian
5032de5b17ea3c74d2844ed035a1edfa6479866139b5Fariborz Jahanian    // Static class's protocols, or its super class or category protocols
5033de5b17ea3c74d2844ed035a1edfa6479866139b5Fariborz Jahanian    // must be found, direct or indirect in rhs's qualifier list or it is a mismatch.
5034de5b17ea3c74d2844ed035a1edfa6479866139b5Fariborz Jahanian    if (ObjCInterfaceDecl *lhsID = lhsOPT->getInterfaceDecl()) {
5035de5b17ea3c74d2844ed035a1edfa6479866139b5Fariborz Jahanian      llvm::SmallPtrSet<ObjCProtocolDecl *, 8> LHSInheritedProtocols;
5036de5b17ea3c74d2844ed035a1edfa6479866139b5Fariborz Jahanian      CollectInheritedProtocols(lhsID, LHSInheritedProtocols);
5037de5b17ea3c74d2844ed035a1edfa6479866139b5Fariborz Jahanian      // This is rather dubious but matches gcc's behavior. If lhs has
5038de5b17ea3c74d2844ed035a1edfa6479866139b5Fariborz Jahanian      // no type qualifier and its class has no static protocol(s)
5039de5b17ea3c74d2844ed035a1edfa6479866139b5Fariborz Jahanian      // assume that it is mismatch.
5040de5b17ea3c74d2844ed035a1edfa6479866139b5Fariborz Jahanian      if (LHSInheritedProtocols.empty() && lhsOPT->qual_empty())
5041de5b17ea3c74d2844ed035a1edfa6479866139b5Fariborz Jahanian        return false;
5042de5b17ea3c74d2844ed035a1edfa6479866139b5Fariborz Jahanian      for (llvm::SmallPtrSet<ObjCProtocolDecl*,8>::iterator I =
5043de5b17ea3c74d2844ed035a1edfa6479866139b5Fariborz Jahanian           LHSInheritedProtocols.begin(),
5044de5b17ea3c74d2844ed035a1edfa6479866139b5Fariborz Jahanian           E = LHSInheritedProtocols.end(); I != E; ++I) {
5045de5b17ea3c74d2844ed035a1edfa6479866139b5Fariborz Jahanian        bool match = false;
5046de5b17ea3c74d2844ed035a1edfa6479866139b5Fariborz Jahanian        ObjCProtocolDecl *lhsProto = (*I);
5047de5b17ea3c74d2844ed035a1edfa6479866139b5Fariborz Jahanian        for (ObjCObjectPointerType::qual_iterator J = rhsQID->qual_begin(),
5048de5b17ea3c74d2844ed035a1edfa6479866139b5Fariborz Jahanian             E = rhsQID->qual_end(); J != E; ++J) {
5049de5b17ea3c74d2844ed035a1edfa6479866139b5Fariborz Jahanian          ObjCProtocolDecl *rhsProto = *J;
5050de5b17ea3c74d2844ed035a1edfa6479866139b5Fariborz Jahanian          if (ProtocolCompatibleWithProtocol(lhsProto, rhsProto) ||
5051de5b17ea3c74d2844ed035a1edfa6479866139b5Fariborz Jahanian              (compare && ProtocolCompatibleWithProtocol(rhsProto, lhsProto))) {
5052de5b17ea3c74d2844ed035a1edfa6479866139b5Fariborz Jahanian            match = true;
5053de5b17ea3c74d2844ed035a1edfa6479866139b5Fariborz Jahanian            break;
5054de5b17ea3c74d2844ed035a1edfa6479866139b5Fariborz Jahanian          }
5055de5b17ea3c74d2844ed035a1edfa6479866139b5Fariborz Jahanian        }
5056de5b17ea3c74d2844ed035a1edfa6479866139b5Fariborz Jahanian        if (!match)
5057de5b17ea3c74d2844ed035a1edfa6479866139b5Fariborz Jahanian          return false;
5058de5b17ea3c74d2844ed035a1edfa6479866139b5Fariborz Jahanian      }
5059de5b17ea3c74d2844ed035a1edfa6479866139b5Fariborz Jahanian    }
50604084c306635b70f37029dca938444e6013f08684Steve Naroff    return true;
50614084c306635b70f37029dca938444e6013f08684Steve Naroff  }
50624084c306635b70f37029dca938444e6013f08684Steve Naroff  return false;
50634084c306635b70f37029dca938444e6013f08684Steve Naroff}
50644084c306635b70f37029dca938444e6013f08684Steve Naroff
50654084c306635b70f37029dca938444e6013f08684Steve Naroff/// canAssignObjCInterfaces - Return true if the two interface types are
50664084c306635b70f37029dca938444e6013f08684Steve Naroff/// compatible for assignment from RHS to LHS.  This handles validation of any
50674084c306635b70f37029dca938444e6013f08684Steve Naroff/// protocol qualifiers on the LHS or RHS.
50684084c306635b70f37029dca938444e6013f08684Steve Naroff///
50694084c306635b70f37029dca938444e6013f08684Steve Naroffbool ASTContext::canAssignObjCInterfaces(const ObjCObjectPointerType *LHSOPT,
50704084c306635b70f37029dca938444e6013f08684Steve Naroff                                         const ObjCObjectPointerType *RHSOPT) {
5071c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall  const ObjCObjectType* LHS = LHSOPT->getObjectType();
5072c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall  const ObjCObjectType* RHS = RHSOPT->getObjectType();
5073c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall
50744084c306635b70f37029dca938444e6013f08684Steve Naroff  // If either type represents the built-in 'id' or 'Class' types, return true.
5075c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall  if (LHS->isObjCUnqualifiedIdOrClass() ||
5076c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall      RHS->isObjCUnqualifiedIdOrClass())
50774084c306635b70f37029dca938444e6013f08684Steve Naroff    return true;
50784084c306635b70f37029dca938444e6013f08684Steve Naroff
5079c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall  if (LHS->isObjCQualifiedId() || RHS->isObjCQualifiedId())
50801eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    return ObjCQualifiedIdTypesAreCompatible(QualType(LHSOPT,0),
50811eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump                                             QualType(RHSOPT,0),
50824084c306635b70f37029dca938444e6013f08684Steve Naroff                                             false);
5083a8f8dac6a29f6d33474a38a32ce9dd859b696da9Fariborz Jahanian
5084a8f8dac6a29f6d33474a38a32ce9dd859b696da9Fariborz Jahanian  if (LHS->isObjCQualifiedClass() && RHS->isObjCQualifiedClass())
5085a8f8dac6a29f6d33474a38a32ce9dd859b696da9Fariborz Jahanian    return ObjCQualifiedClassTypesAreCompatible(QualType(LHSOPT,0),
5086a8f8dac6a29f6d33474a38a32ce9dd859b696da9Fariborz Jahanian                                                QualType(RHSOPT,0));
5087a8f8dac6a29f6d33474a38a32ce9dd859b696da9Fariborz Jahanian
5088c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall  // If we have 2 user-defined types, fall into that path.
5089c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall  if (LHS->getInterface() && RHS->getInterface())
50904084c306635b70f37029dca938444e6013f08684Steve Naroff    return canAssignObjCInterfaces(LHS, RHS);
50911eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
50924084c306635b70f37029dca938444e6013f08684Steve Naroff  return false;
509314108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff}
509414108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff
5095132f2a2da34f378fc675b9e174564b0f52c31d98Fariborz Jahanian/// canAssignObjCInterfacesInBlockPointer - This routine is specifically written
5096fc8f0e14ad142ed811e90fbd9a30e419e301c717Chris Lattner/// for providing type-safety for objective-c pointers used to pass/return
5097132f2a2da34f378fc675b9e174564b0f52c31d98Fariborz Jahanian/// arguments in block literals. When passed as arguments, passing 'A*' where
5098132f2a2da34f378fc675b9e174564b0f52c31d98Fariborz Jahanian/// 'id' is expected is not OK. Passing 'Sub *" where 'Super *" is expected is
5099132f2a2da34f378fc675b9e174564b0f52c31d98Fariborz Jahanian/// not OK. For the return type, the opposite is not OK.
5100132f2a2da34f378fc675b9e174564b0f52c31d98Fariborz Jahanianbool ASTContext::canAssignObjCInterfacesInBlockPointer(
5101132f2a2da34f378fc675b9e174564b0f52c31d98Fariborz Jahanian                                         const ObjCObjectPointerType *LHSOPT,
5102a4fdbfad150ae37bddaa4094d3925a27a1a1cf3fFariborz Jahanian                                         const ObjCObjectPointerType *RHSOPT,
5103a4fdbfad150ae37bddaa4094d3925a27a1a1cf3fFariborz Jahanian                                         bool BlockReturnType) {
5104a98344869c278295f38e106583906377520079b8Fariborz Jahanian  if (RHSOPT->isObjCBuiltinType() || LHSOPT->isObjCIdType())
5105132f2a2da34f378fc675b9e174564b0f52c31d98Fariborz Jahanian    return true;
5106132f2a2da34f378fc675b9e174564b0f52c31d98Fariborz Jahanian
5107132f2a2da34f378fc675b9e174564b0f52c31d98Fariborz Jahanian  if (LHSOPT->isObjCBuiltinType()) {
5108132f2a2da34f378fc675b9e174564b0f52c31d98Fariborz Jahanian    return RHSOPT->isObjCBuiltinType() || RHSOPT->isObjCQualifiedIdType();
5109132f2a2da34f378fc675b9e174564b0f52c31d98Fariborz Jahanian  }
5110132f2a2da34f378fc675b9e174564b0f52c31d98Fariborz Jahanian
5111a98344869c278295f38e106583906377520079b8Fariborz Jahanian  if (LHSOPT->isObjCQualifiedIdType() || RHSOPT->isObjCQualifiedIdType())
5112132f2a2da34f378fc675b9e174564b0f52c31d98Fariborz Jahanian    return ObjCQualifiedIdTypesAreCompatible(QualType(LHSOPT,0),
5113132f2a2da34f378fc675b9e174564b0f52c31d98Fariborz Jahanian                                             QualType(RHSOPT,0),
5114132f2a2da34f378fc675b9e174564b0f52c31d98Fariborz Jahanian                                             false);
5115132f2a2da34f378fc675b9e174564b0f52c31d98Fariborz Jahanian
5116132f2a2da34f378fc675b9e174564b0f52c31d98Fariborz Jahanian  const ObjCInterfaceType* LHS = LHSOPT->getInterfaceType();
5117132f2a2da34f378fc675b9e174564b0f52c31d98Fariborz Jahanian  const ObjCInterfaceType* RHS = RHSOPT->getInterfaceType();
5118132f2a2da34f378fc675b9e174564b0f52c31d98Fariborz Jahanian  if (LHS && RHS)  { // We have 2 user-defined types.
5119132f2a2da34f378fc675b9e174564b0f52c31d98Fariborz Jahanian    if (LHS != RHS) {
5120132f2a2da34f378fc675b9e174564b0f52c31d98Fariborz Jahanian      if (LHS->getDecl()->isSuperClassOf(RHS->getDecl()))
5121a4fdbfad150ae37bddaa4094d3925a27a1a1cf3fFariborz Jahanian        return BlockReturnType;
5122132f2a2da34f378fc675b9e174564b0f52c31d98Fariborz Jahanian      if (RHS->getDecl()->isSuperClassOf(LHS->getDecl()))
5123a4fdbfad150ae37bddaa4094d3925a27a1a1cf3fFariborz Jahanian        return !BlockReturnType;
5124132f2a2da34f378fc675b9e174564b0f52c31d98Fariborz Jahanian    }
5125132f2a2da34f378fc675b9e174564b0f52c31d98Fariborz Jahanian    else
5126132f2a2da34f378fc675b9e174564b0f52c31d98Fariborz Jahanian      return true;
5127132f2a2da34f378fc675b9e174564b0f52c31d98Fariborz Jahanian  }
5128132f2a2da34f378fc675b9e174564b0f52c31d98Fariborz Jahanian  return false;
5129132f2a2da34f378fc675b9e174564b0f52c31d98Fariborz Jahanian}
5130132f2a2da34f378fc675b9e174564b0f52c31d98Fariborz Jahanian
5131e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanian/// getIntersectionOfProtocols - This routine finds the intersection of set
5132e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanian/// of protocols inherited from two distinct objective-c pointer objects.
5133e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanian/// It is used to build composite qualifier list of the composite type of
5134e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanian/// the conditional expression involving two objective-c pointer objects.
5135e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanianstatic
5136e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanianvoid getIntersectionOfProtocols(ASTContext &Context,
5137e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanian                                const ObjCObjectPointerType *LHSOPT,
5138e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanian                                const ObjCObjectPointerType *RHSOPT,
5139e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanian      llvm::SmallVectorImpl<ObjCProtocolDecl *> &IntersectionOfProtocols) {
5140e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanian
5141c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall  const ObjCObjectType* LHS = LHSOPT->getObjectType();
5142c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall  const ObjCObjectType* RHS = RHSOPT->getObjectType();
5143c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall  assert(LHS->getInterface() && "LHS must have an interface base");
5144c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall  assert(RHS->getInterface() && "RHS must have an interface base");
5145e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanian
5146e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanian  llvm::SmallPtrSet<ObjCProtocolDecl *, 8> InheritedProtocolSet;
5147e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanian  unsigned LHSNumProtocols = LHS->getNumProtocols();
5148e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanian  if (LHSNumProtocols > 0)
5149e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanian    InheritedProtocolSet.insert(LHS->qual_begin(), LHS->qual_end());
5150e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanian  else {
5151432a8893f7e30d141d7f279bd00b741a3cdac81fFariborz Jahanian    llvm::SmallPtrSet<ObjCProtocolDecl *, 8> LHSInheritedProtocols;
5152c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall    Context.CollectInheritedProtocols(LHS->getInterface(),
5153c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall                                      LHSInheritedProtocols);
5154e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanian    InheritedProtocolSet.insert(LHSInheritedProtocols.begin(),
5155e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanian                                LHSInheritedProtocols.end());
5156e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanian  }
5157e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanian
5158e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanian  unsigned RHSNumProtocols = RHS->getNumProtocols();
5159e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanian  if (RHSNumProtocols > 0) {
5160cb421fa690da545b58a720abe5f1c49b166dbde7Dan Gohman    ObjCProtocolDecl **RHSProtocols =
5161cb421fa690da545b58a720abe5f1c49b166dbde7Dan Gohman      const_cast<ObjCProtocolDecl **>(RHS->qual_begin());
5162e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanian    for (unsigned i = 0; i < RHSNumProtocols; ++i)
5163e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanian      if (InheritedProtocolSet.count(RHSProtocols[i]))
5164e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanian        IntersectionOfProtocols.push_back(RHSProtocols[i]);
5165e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanian  }
5166e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanian  else {
5167432a8893f7e30d141d7f279bd00b741a3cdac81fFariborz Jahanian    llvm::SmallPtrSet<ObjCProtocolDecl *, 8> RHSInheritedProtocols;
5168c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall    Context.CollectInheritedProtocols(RHS->getInterface(),
5169c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall                                      RHSInheritedProtocols);
5170432a8893f7e30d141d7f279bd00b741a3cdac81fFariborz Jahanian    for (llvm::SmallPtrSet<ObjCProtocolDecl*,8>::iterator I =
5171432a8893f7e30d141d7f279bd00b741a3cdac81fFariborz Jahanian         RHSInheritedProtocols.begin(),
5172432a8893f7e30d141d7f279bd00b741a3cdac81fFariborz Jahanian         E = RHSInheritedProtocols.end(); I != E; ++I)
5173432a8893f7e30d141d7f279bd00b741a3cdac81fFariborz Jahanian      if (InheritedProtocolSet.count((*I)))
5174432a8893f7e30d141d7f279bd00b741a3cdac81fFariborz Jahanian        IntersectionOfProtocols.push_back((*I));
5175e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanian  }
5176e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanian}
5177e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanian
5178db07b3f7cdcb505329c1280d7cf70791739a7cadFariborz Jahanian/// areCommonBaseCompatible - Returns common base class of the two classes if
5179db07b3f7cdcb505329c1280d7cf70791739a7cadFariborz Jahanian/// one found. Note that this is O'2 algorithm. But it will be called as the
5180db07b3f7cdcb505329c1280d7cf70791739a7cadFariborz Jahanian/// last type comparison in a ?-exp of ObjC pointer types before a
5181db07b3f7cdcb505329c1280d7cf70791739a7cadFariborz Jahanian/// warning is issued. So, its invokation is extremely rare.
5182db07b3f7cdcb505329c1280d7cf70791739a7cadFariborz JahanianQualType ASTContext::areCommonBaseCompatible(
5183c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall                                          const ObjCObjectPointerType *Lptr,
5184c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall                                          const ObjCObjectPointerType *Rptr) {
5185c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall  const ObjCObjectType *LHS = Lptr->getObjectType();
5186c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall  const ObjCObjectType *RHS = Rptr->getObjectType();
5187c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall  const ObjCInterfaceDecl* LDecl = LHS->getInterface();
5188c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall  const ObjCInterfaceDecl* RDecl = RHS->getInterface();
51897c2bdcb4d30f2d370b4367664e6a11b075ce2cb3Fariborz Jahanian  if (!LDecl || !RDecl || (LDecl == RDecl))
5190db07b3f7cdcb505329c1280d7cf70791739a7cadFariborz Jahanian    return QualType();
5191db07b3f7cdcb505329c1280d7cf70791739a7cadFariborz Jahanian
51927c2bdcb4d30f2d370b4367664e6a11b075ce2cb3Fariborz Jahanian  do {
5193c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall    LHS = cast<ObjCInterfaceType>(getObjCInterfaceType(LDecl));
5194e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanian    if (canAssignObjCInterfaces(LHS, RHS)) {
5195c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall      llvm::SmallVector<ObjCProtocolDecl *, 8> Protocols;
5196c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall      getIntersectionOfProtocols(*this, Lptr, Rptr, Protocols);
5197c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall
5198c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall      QualType Result = QualType(LHS, 0);
5199c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall      if (!Protocols.empty())
5200c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall        Result = getObjCObjectType(Result, Protocols.data(), Protocols.size());
5201c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall      Result = getObjCObjectPointerType(Result);
5202c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall      return Result;
5203e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanian    }
52047c2bdcb4d30f2d370b4367664e6a11b075ce2cb3Fariborz Jahanian  } while ((LDecl = LDecl->getSuperClass()));
5205db07b3f7cdcb505329c1280d7cf70791739a7cadFariborz Jahanian
5206db07b3f7cdcb505329c1280d7cf70791739a7cadFariborz Jahanian  return QualType();
5207db07b3f7cdcb505329c1280d7cf70791739a7cadFariborz Jahanian}
5208db07b3f7cdcb505329c1280d7cf70791739a7cadFariborz Jahanian
5209c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCallbool ASTContext::canAssignObjCInterfaces(const ObjCObjectType *LHS,
5210c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall                                         const ObjCObjectType *RHS) {
5211c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall  assert(LHS->getInterface() && "LHS is not an interface type");
5212c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall  assert(RHS->getInterface() && "RHS is not an interface type");
5213c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall
52146ac46a4a521366d7ab36ebe2ce4e624ab96b06f9Chris Lattner  // Verify that the base decls are compatible: the RHS must be a subclass of
52156ac46a4a521366d7ab36ebe2ce4e624ab96b06f9Chris Lattner  // the LHS.
5216c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall  if (!LHS->getInterface()->isSuperClassOf(RHS->getInterface()))
52176ac46a4a521366d7ab36ebe2ce4e624ab96b06f9Chris Lattner    return false;
52181eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
52196ac46a4a521366d7ab36ebe2ce4e624ab96b06f9Chris Lattner  // RHS must have a superset of the protocols in the LHS.  If the LHS is not
52206ac46a4a521366d7ab36ebe2ce4e624ab96b06f9Chris Lattner  // protocol qualified at all, then we are good.
5221c15cb2af27514ecc879daba9aa01389c5203685dSteve Naroff  if (LHS->getNumProtocols() == 0)
52226ac46a4a521366d7ab36ebe2ce4e624ab96b06f9Chris Lattner    return true;
52231eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
5224b7bc34a83aff8af09f2a78aa6d1dcafe18ad8619Fariborz Jahanian  // Okay, we know the LHS has protocol qualifiers.  If the RHS doesn't,
5225b7bc34a83aff8af09f2a78aa6d1dcafe18ad8619Fariborz Jahanian  // more detailed analysis is required.
5226b7bc34a83aff8af09f2a78aa6d1dcafe18ad8619Fariborz Jahanian  if (RHS->getNumProtocols() == 0) {
5227b7bc34a83aff8af09f2a78aa6d1dcafe18ad8619Fariborz Jahanian    // OK, if LHS is a superclass of RHS *and*
5228b7bc34a83aff8af09f2a78aa6d1dcafe18ad8619Fariborz Jahanian    // this superclass is assignment compatible with LHS.
5229b7bc34a83aff8af09f2a78aa6d1dcafe18ad8619Fariborz Jahanian    // false otherwise.
5230627788c29976fbeb4ad47bcfcb3576889070e357Fariborz Jahanian    bool IsSuperClass =
5231627788c29976fbeb4ad47bcfcb3576889070e357Fariborz Jahanian      LHS->getInterface()->isSuperClassOf(RHS->getInterface());
5232627788c29976fbeb4ad47bcfcb3576889070e357Fariborz Jahanian    if (IsSuperClass) {
5233b7bc34a83aff8af09f2a78aa6d1dcafe18ad8619Fariborz Jahanian      // OK if conversion of LHS to SuperClass results in narrowing of types
5234b7bc34a83aff8af09f2a78aa6d1dcafe18ad8619Fariborz Jahanian      // ; i.e., SuperClass may implement at least one of the protocols
5235b7bc34a83aff8af09f2a78aa6d1dcafe18ad8619Fariborz Jahanian      // in LHS's protocol list. Example, SuperObj<P1> = lhs<P1,P2> is ok.
5236b7bc34a83aff8af09f2a78aa6d1dcafe18ad8619Fariborz Jahanian      // But not SuperObj<P1,P2,P3> = lhs<P1,P2>.
5237b7bc34a83aff8af09f2a78aa6d1dcafe18ad8619Fariborz Jahanian      llvm::SmallPtrSet<ObjCProtocolDecl *, 8> SuperClassInheritedProtocols;
5238627788c29976fbeb4ad47bcfcb3576889070e357Fariborz Jahanian      CollectInheritedProtocols(RHS->getInterface(), SuperClassInheritedProtocols);
5239b7bc34a83aff8af09f2a78aa6d1dcafe18ad8619Fariborz Jahanian      // If super class has no protocols, it is not a match.
5240b7bc34a83aff8af09f2a78aa6d1dcafe18ad8619Fariborz Jahanian      if (SuperClassInheritedProtocols.empty())
5241b7bc34a83aff8af09f2a78aa6d1dcafe18ad8619Fariborz Jahanian        return false;
5242b7bc34a83aff8af09f2a78aa6d1dcafe18ad8619Fariborz Jahanian
5243b7bc34a83aff8af09f2a78aa6d1dcafe18ad8619Fariborz Jahanian      for (ObjCObjectType::qual_iterator LHSPI = LHS->qual_begin(),
5244b7bc34a83aff8af09f2a78aa6d1dcafe18ad8619Fariborz Jahanian           LHSPE = LHS->qual_end();
5245b7bc34a83aff8af09f2a78aa6d1dcafe18ad8619Fariborz Jahanian           LHSPI != LHSPE; LHSPI++) {
5246b7bc34a83aff8af09f2a78aa6d1dcafe18ad8619Fariborz Jahanian        bool SuperImplementsProtocol = false;
5247b7bc34a83aff8af09f2a78aa6d1dcafe18ad8619Fariborz Jahanian        ObjCProtocolDecl *LHSProto = (*LHSPI);
5248b7bc34a83aff8af09f2a78aa6d1dcafe18ad8619Fariborz Jahanian
5249b7bc34a83aff8af09f2a78aa6d1dcafe18ad8619Fariborz Jahanian        for (llvm::SmallPtrSet<ObjCProtocolDecl*,8>::iterator I =
5250b7bc34a83aff8af09f2a78aa6d1dcafe18ad8619Fariborz Jahanian             SuperClassInheritedProtocols.begin(),
5251b7bc34a83aff8af09f2a78aa6d1dcafe18ad8619Fariborz Jahanian             E = SuperClassInheritedProtocols.end(); I != E; ++I) {
5252b7bc34a83aff8af09f2a78aa6d1dcafe18ad8619Fariborz Jahanian          ObjCProtocolDecl *SuperClassProto = (*I);
5253b7bc34a83aff8af09f2a78aa6d1dcafe18ad8619Fariborz Jahanian          if (SuperClassProto->lookupProtocolNamed(LHSProto->getIdentifier())) {
5254b7bc34a83aff8af09f2a78aa6d1dcafe18ad8619Fariborz Jahanian            SuperImplementsProtocol = true;
5255b7bc34a83aff8af09f2a78aa6d1dcafe18ad8619Fariborz Jahanian            break;
5256b7bc34a83aff8af09f2a78aa6d1dcafe18ad8619Fariborz Jahanian          }
5257b7bc34a83aff8af09f2a78aa6d1dcafe18ad8619Fariborz Jahanian        }
5258b7bc34a83aff8af09f2a78aa6d1dcafe18ad8619Fariborz Jahanian        if (!SuperImplementsProtocol)
5259b7bc34a83aff8af09f2a78aa6d1dcafe18ad8619Fariborz Jahanian          return false;
5260b7bc34a83aff8af09f2a78aa6d1dcafe18ad8619Fariborz Jahanian      }
5261b7bc34a83aff8af09f2a78aa6d1dcafe18ad8619Fariborz Jahanian      return true;
5262b7bc34a83aff8af09f2a78aa6d1dcafe18ad8619Fariborz Jahanian    }
5263b7bc34a83aff8af09f2a78aa6d1dcafe18ad8619Fariborz Jahanian    return false;
5264b7bc34a83aff8af09f2a78aa6d1dcafe18ad8619Fariborz Jahanian  }
52651eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
5266c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall  for (ObjCObjectType::qual_iterator LHSPI = LHS->qual_begin(),
5267c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall                                     LHSPE = LHS->qual_end();
526891b0b0cf6b537cbcbca0038c7032f87161a41d31Steve Naroff       LHSPI != LHSPE; LHSPI++) {
526991b0b0cf6b537cbcbca0038c7032f87161a41d31Steve Naroff    bool RHSImplementsProtocol = false;
527091b0b0cf6b537cbcbca0038c7032f87161a41d31Steve Naroff
527191b0b0cf6b537cbcbca0038c7032f87161a41d31Steve Naroff    // If the RHS doesn't implement the protocol on the left, the types
527291b0b0cf6b537cbcbca0038c7032f87161a41d31Steve Naroff    // are incompatible.
5273c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall    for (ObjCObjectType::qual_iterator RHSPI = RHS->qual_begin(),
5274c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall                                       RHSPE = RHS->qual_end();
52758f16756441450ed9fb39316e47d107fc2a1ef35bSteve Naroff         RHSPI != RHSPE; RHSPI++) {
52768f16756441450ed9fb39316e47d107fc2a1ef35bSteve Naroff      if ((*RHSPI)->lookupProtocolNamed((*LHSPI)->getIdentifier())) {
527791b0b0cf6b537cbcbca0038c7032f87161a41d31Steve Naroff        RHSImplementsProtocol = true;
52788f16756441450ed9fb39316e47d107fc2a1ef35bSteve Naroff        break;
52798f16756441450ed9fb39316e47d107fc2a1ef35bSteve Naroff      }
528091b0b0cf6b537cbcbca0038c7032f87161a41d31Steve Naroff    }
528191b0b0cf6b537cbcbca0038c7032f87161a41d31Steve Naroff    // FIXME: For better diagnostics, consider passing back the protocol name.
528291b0b0cf6b537cbcbca0038c7032f87161a41d31Steve Naroff    if (!RHSImplementsProtocol)
528391b0b0cf6b537cbcbca0038c7032f87161a41d31Steve Naroff      return false;
528491b0b0cf6b537cbcbca0038c7032f87161a41d31Steve Naroff  }
528591b0b0cf6b537cbcbca0038c7032f87161a41d31Steve Naroff  // The RHS implements all protocols listed on the LHS.
528691b0b0cf6b537cbcbca0038c7032f87161a41d31Steve Naroff  return true;
52876ac46a4a521366d7ab36ebe2ce4e624ab96b06f9Chris Lattner}
52886ac46a4a521366d7ab36ebe2ce4e624ab96b06f9Chris Lattner
5289389bf46ae41241a656ed71b00ac2177d7f385651Steve Naroffbool ASTContext::areComparableObjCPointerTypes(QualType LHS, QualType RHS) {
5290389bf46ae41241a656ed71b00ac2177d7f385651Steve Naroff  // get the "pointed to" types
5291183700f494ec9b6701b6efe82bcb25f4c79ba561John McCall  const ObjCObjectPointerType *LHSOPT = LHS->getAs<ObjCObjectPointerType>();
5292183700f494ec9b6701b6efe82bcb25f4c79ba561John McCall  const ObjCObjectPointerType *RHSOPT = RHS->getAs<ObjCObjectPointerType>();
52931eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
529414108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff  if (!LHSOPT || !RHSOPT)
5295389bf46ae41241a656ed71b00ac2177d7f385651Steve Naroff    return false;
529614108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff
529714108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff  return canAssignObjCInterfaces(LHSOPT, RHSOPT) ||
529814108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff         canAssignObjCInterfaces(RHSOPT, LHSOPT);
5299389bf46ae41241a656ed71b00ac2177d7f385651Steve Naroff}
5300389bf46ae41241a656ed71b00ac2177d7f385651Steve Naroff
5301569c3166874324c24011f8ade6978421f0d39b3cDouglas Gregorbool ASTContext::canBindObjCObjectType(QualType To, QualType From) {
5302569c3166874324c24011f8ade6978421f0d39b3cDouglas Gregor  return canAssignObjCInterfaces(
5303569c3166874324c24011f8ade6978421f0d39b3cDouglas Gregor                getObjCObjectPointerType(To)->getAs<ObjCObjectPointerType>(),
5304569c3166874324c24011f8ade6978421f0d39b3cDouglas Gregor                getObjCObjectPointerType(From)->getAs<ObjCObjectPointerType>());
5305569c3166874324c24011f8ade6978421f0d39b3cDouglas Gregor}
5306569c3166874324c24011f8ade6978421f0d39b3cDouglas Gregor
53071eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// typesAreCompatible - C99 6.7.3p9: For two qualified types to be compatible,
5308ec0550fa3653d46560bf4484a2e988329c228e39Steve Naroff/// both shall have the identically qualified version of a compatible type.
53091eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// C99 6.2.7p1: Two types have compatible types if their types are the
5310ec0550fa3653d46560bf4484a2e988329c228e39Steve Naroff/// same. See 6.7.[2,3,5] for additional rules.
5311447234dd459a00a5ed9b7c3e066162cd7a75bf2dDouglas Gregorbool ASTContext::typesAreCompatible(QualType LHS, QualType RHS,
5312447234dd459a00a5ed9b7c3e066162cd7a75bf2dDouglas Gregor                                    bool CompareUnqualified) {
53130e709abafbd939326850501f795cc7a92c88a354Douglas Gregor  if (getLangOptions().CPlusPlus)
53140e709abafbd939326850501f795cc7a92c88a354Douglas Gregor    return hasSameType(LHS, RHS);
53150e709abafbd939326850501f795cc7a92c88a354Douglas Gregor
5316447234dd459a00a5ed9b7c3e066162cd7a75bf2dDouglas Gregor  return !mergeTypes(LHS, RHS, false, CompareUnqualified).isNull();
53173d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman}
53183d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman
5319132f2a2da34f378fc675b9e174564b0f52c31d98Fariborz Jahanianbool ASTContext::typesAreBlockPointerCompatible(QualType LHS, QualType RHS) {
5320132f2a2da34f378fc675b9e174564b0f52c31d98Fariborz Jahanian  return !mergeTypes(LHS, RHS, true).isNull();
5321132f2a2da34f378fc675b9e174564b0f52c31d98Fariborz Jahanian}
5322132f2a2da34f378fc675b9e174564b0f52c31d98Fariborz Jahanian
53234846675e0e42d1802b0ffd8972a45e72aeb3758dPeter Collingbourne/// mergeTransparentUnionType - if T is a transparent union type and a member
53244846675e0e42d1802b0ffd8972a45e72aeb3758dPeter Collingbourne/// of T is compatible with SubType, return the merged type, else return
53254846675e0e42d1802b0ffd8972a45e72aeb3758dPeter Collingbourne/// QualType()
53264846675e0e42d1802b0ffd8972a45e72aeb3758dPeter CollingbourneQualType ASTContext::mergeTransparentUnionType(QualType T, QualType SubType,
53274846675e0e42d1802b0ffd8972a45e72aeb3758dPeter Collingbourne                                               bool OfBlockPointer,
53284846675e0e42d1802b0ffd8972a45e72aeb3758dPeter Collingbourne                                               bool Unqualified) {
53294846675e0e42d1802b0ffd8972a45e72aeb3758dPeter Collingbourne  if (const RecordType *UT = T->getAsUnionType()) {
53304846675e0e42d1802b0ffd8972a45e72aeb3758dPeter Collingbourne    RecordDecl *UD = UT->getDecl();
53314846675e0e42d1802b0ffd8972a45e72aeb3758dPeter Collingbourne    if (UD->hasAttr<TransparentUnionAttr>()) {
53324846675e0e42d1802b0ffd8972a45e72aeb3758dPeter Collingbourne      for (RecordDecl::field_iterator it = UD->field_begin(),
53334846675e0e42d1802b0ffd8972a45e72aeb3758dPeter Collingbourne           itend = UD->field_end(); it != itend; ++it) {
5334f91d7572ee7fa6f2926c8e7e816039a1154a59f8Peter Collingbourne        QualType ET = it->getType().getUnqualifiedType();
53354846675e0e42d1802b0ffd8972a45e72aeb3758dPeter Collingbourne        QualType MT = mergeTypes(ET, SubType, OfBlockPointer, Unqualified);
53364846675e0e42d1802b0ffd8972a45e72aeb3758dPeter Collingbourne        if (!MT.isNull())
53374846675e0e42d1802b0ffd8972a45e72aeb3758dPeter Collingbourne          return MT;
53384846675e0e42d1802b0ffd8972a45e72aeb3758dPeter Collingbourne      }
53394846675e0e42d1802b0ffd8972a45e72aeb3758dPeter Collingbourne    }
53404846675e0e42d1802b0ffd8972a45e72aeb3758dPeter Collingbourne  }
53414846675e0e42d1802b0ffd8972a45e72aeb3758dPeter Collingbourne
53424846675e0e42d1802b0ffd8972a45e72aeb3758dPeter Collingbourne  return QualType();
53434846675e0e42d1802b0ffd8972a45e72aeb3758dPeter Collingbourne}
53444846675e0e42d1802b0ffd8972a45e72aeb3758dPeter Collingbourne
53454846675e0e42d1802b0ffd8972a45e72aeb3758dPeter Collingbourne/// mergeFunctionArgumentTypes - merge two types which appear as function
53464846675e0e42d1802b0ffd8972a45e72aeb3758dPeter Collingbourne/// argument types
53474846675e0e42d1802b0ffd8972a45e72aeb3758dPeter CollingbourneQualType ASTContext::mergeFunctionArgumentTypes(QualType lhs, QualType rhs,
53484846675e0e42d1802b0ffd8972a45e72aeb3758dPeter Collingbourne                                                bool OfBlockPointer,
53494846675e0e42d1802b0ffd8972a45e72aeb3758dPeter Collingbourne                                                bool Unqualified) {
53504846675e0e42d1802b0ffd8972a45e72aeb3758dPeter Collingbourne  // GNU extension: two types are compatible if they appear as a function
53514846675e0e42d1802b0ffd8972a45e72aeb3758dPeter Collingbourne  // argument, one of the types is a transparent union type and the other
53524846675e0e42d1802b0ffd8972a45e72aeb3758dPeter Collingbourne  // type is compatible with a union member
53534846675e0e42d1802b0ffd8972a45e72aeb3758dPeter Collingbourne  QualType lmerge = mergeTransparentUnionType(lhs, rhs, OfBlockPointer,
53544846675e0e42d1802b0ffd8972a45e72aeb3758dPeter Collingbourne                                              Unqualified);
53554846675e0e42d1802b0ffd8972a45e72aeb3758dPeter Collingbourne  if (!lmerge.isNull())
53564846675e0e42d1802b0ffd8972a45e72aeb3758dPeter Collingbourne    return lmerge;
53574846675e0e42d1802b0ffd8972a45e72aeb3758dPeter Collingbourne
53584846675e0e42d1802b0ffd8972a45e72aeb3758dPeter Collingbourne  QualType rmerge = mergeTransparentUnionType(rhs, lhs, OfBlockPointer,
53594846675e0e42d1802b0ffd8972a45e72aeb3758dPeter Collingbourne                                              Unqualified);
53604846675e0e42d1802b0ffd8972a45e72aeb3758dPeter Collingbourne  if (!rmerge.isNull())
53614846675e0e42d1802b0ffd8972a45e72aeb3758dPeter Collingbourne    return rmerge;
53624846675e0e42d1802b0ffd8972a45e72aeb3758dPeter Collingbourne
53634846675e0e42d1802b0ffd8972a45e72aeb3758dPeter Collingbourne  return mergeTypes(lhs, rhs, OfBlockPointer, Unqualified);
53644846675e0e42d1802b0ffd8972a45e72aeb3758dPeter Collingbourne}
53654846675e0e42d1802b0ffd8972a45e72aeb3758dPeter Collingbourne
5366132f2a2da34f378fc675b9e174564b0f52c31d98Fariborz JahanianQualType ASTContext::mergeFunctionTypes(QualType lhs, QualType rhs,
5367447234dd459a00a5ed9b7c3e066162cd7a75bf2dDouglas Gregor                                        bool OfBlockPointer,
5368447234dd459a00a5ed9b7c3e066162cd7a75bf2dDouglas Gregor                                        bool Unqualified) {
5369183700f494ec9b6701b6efe82bcb25f4c79ba561John McCall  const FunctionType *lbase = lhs->getAs<FunctionType>();
5370183700f494ec9b6701b6efe82bcb25f4c79ba561John McCall  const FunctionType *rbase = rhs->getAs<FunctionType>();
537172564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor  const FunctionProtoType *lproto = dyn_cast<FunctionProtoType>(lbase);
537272564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor  const FunctionProtoType *rproto = dyn_cast<FunctionProtoType>(rbase);
53733d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman  bool allLTypes = true;
53743d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman  bool allRTypes = true;
53753d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman
53763d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman  // Check return type
5377132f2a2da34f378fc675b9e174564b0f52c31d98Fariborz Jahanian  QualType retType;
5378d263fd1451299b1e5f5f1acb2bb13b0a4119aee8Fariborz Jahanian  if (OfBlockPointer) {
5379d263fd1451299b1e5f5f1acb2bb13b0a4119aee8Fariborz Jahanian    QualType RHS = rbase->getResultType();
5380d263fd1451299b1e5f5f1acb2bb13b0a4119aee8Fariborz Jahanian    QualType LHS = lbase->getResultType();
5381d263fd1451299b1e5f5f1acb2bb13b0a4119aee8Fariborz Jahanian    bool UnqualifiedResult = Unqualified;
5382d263fd1451299b1e5f5f1acb2bb13b0a4119aee8Fariborz Jahanian    if (!UnqualifiedResult)
5383d263fd1451299b1e5f5f1acb2bb13b0a4119aee8Fariborz Jahanian      UnqualifiedResult = (!RHS.hasQualifiers() && LHS.hasQualifiers());
5384a4fdbfad150ae37bddaa4094d3925a27a1a1cf3fFariborz Jahanian    retType = mergeTypes(LHS, RHS, true, UnqualifiedResult, true);
5385d263fd1451299b1e5f5f1acb2bb13b0a4119aee8Fariborz Jahanian  }
5386132f2a2da34f378fc675b9e174564b0f52c31d98Fariborz Jahanian  else
53878cc246c9a68c783a5b90d2e8b8927521cb3a49b7John McCall    retType = mergeTypes(lbase->getResultType(), rbase->getResultType(), false,
53888cc246c9a68c783a5b90d2e8b8927521cb3a49b7John McCall                         Unqualified);
53893d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman  if (retType.isNull()) return QualType();
5390447234dd459a00a5ed9b7c3e066162cd7a75bf2dDouglas Gregor
5391447234dd459a00a5ed9b7c3e066162cd7a75bf2dDouglas Gregor  if (Unqualified)
5392447234dd459a00a5ed9b7c3e066162cd7a75bf2dDouglas Gregor    retType = retType.getUnqualifiedType();
5393447234dd459a00a5ed9b7c3e066162cd7a75bf2dDouglas Gregor
5394447234dd459a00a5ed9b7c3e066162cd7a75bf2dDouglas Gregor  CanQualType LRetType = getCanonicalType(lbase->getResultType());
5395447234dd459a00a5ed9b7c3e066162cd7a75bf2dDouglas Gregor  CanQualType RRetType = getCanonicalType(rbase->getResultType());
5396447234dd459a00a5ed9b7c3e066162cd7a75bf2dDouglas Gregor  if (Unqualified) {
5397447234dd459a00a5ed9b7c3e066162cd7a75bf2dDouglas Gregor    LRetType = LRetType.getUnqualifiedType();
5398447234dd459a00a5ed9b7c3e066162cd7a75bf2dDouglas Gregor    RRetType = RRetType.getUnqualifiedType();
5399447234dd459a00a5ed9b7c3e066162cd7a75bf2dDouglas Gregor  }
5400447234dd459a00a5ed9b7c3e066162cd7a75bf2dDouglas Gregor
5401447234dd459a00a5ed9b7c3e066162cd7a75bf2dDouglas Gregor  if (getCanonicalType(retType) != LRetType)
540261710854be2b098428aff5316e64bd34b30fbcb7Chris Lattner    allLTypes = false;
5403447234dd459a00a5ed9b7c3e066162cd7a75bf2dDouglas Gregor  if (getCanonicalType(retType) != RRetType)
540461710854be2b098428aff5316e64bd34b30fbcb7Chris Lattner    allRTypes = false;
54058cc246c9a68c783a5b90d2e8b8927521cb3a49b7John McCall
54066a15c8586cc3e5d6e01c4c4e8b4374584569b58fDaniel Dunbar  // FIXME: double check this
54076a15c8586cc3e5d6e01c4c4e8b4374584569b58fDaniel Dunbar  // FIXME: should we error if lbase->getRegParmAttr() != 0 &&
54086a15c8586cc3e5d6e01c4c4e8b4374584569b58fDaniel Dunbar  //                           rbase->getRegParmAttr() != 0 &&
54096a15c8586cc3e5d6e01c4c4e8b4374584569b58fDaniel Dunbar  //                           lbase->getRegParmAttr() != rbase->getRegParmAttr()?
5410264ba48dc98f3f843935a485d5b086f7e0fdc4f1Rafael Espindola  FunctionType::ExtInfo lbaseInfo = lbase->getExtInfo();
5411264ba48dc98f3f843935a485d5b086f7e0fdc4f1Rafael Espindola  FunctionType::ExtInfo rbaseInfo = rbase->getExtInfo();
54128cc246c9a68c783a5b90d2e8b8927521cb3a49b7John McCall
54138cc246c9a68c783a5b90d2e8b8927521cb3a49b7John McCall  // Compatible functions must have compatible calling conventions
54148cc246c9a68c783a5b90d2e8b8927521cb3a49b7John McCall  if (!isSameCallConv(lbaseInfo.getCC(), rbaseInfo.getCC()))
54158cc246c9a68c783a5b90d2e8b8927521cb3a49b7John McCall    return QualType();
54168cc246c9a68c783a5b90d2e8b8927521cb3a49b7John McCall
54178cc246c9a68c783a5b90d2e8b8927521cb3a49b7John McCall  // Regparm is part of the calling convention.
5418a49218e17bcbb1acde0245773173e2c0c42f4f19Eli Friedman  if (lbaseInfo.getHasRegParm() != rbaseInfo.getHasRegParm())
5419a49218e17bcbb1acde0245773173e2c0c42f4f19Eli Friedman    return QualType();
54208cc246c9a68c783a5b90d2e8b8927521cb3a49b7John McCall  if (lbaseInfo.getRegParm() != rbaseInfo.getRegParm())
54218cc246c9a68c783a5b90d2e8b8927521cb3a49b7John McCall    return QualType();
54228cc246c9a68c783a5b90d2e8b8927521cb3a49b7John McCall
54238cc246c9a68c783a5b90d2e8b8927521cb3a49b7John McCall  // It's noreturn if either type is.
54248cc246c9a68c783a5b90d2e8b8927521cb3a49b7John McCall  // FIXME: some uses, e.g. conditional exprs, really want this to be 'both'.
54256a15c8586cc3e5d6e01c4c4e8b4374584569b58fDaniel Dunbar  bool NoReturn = lbaseInfo.getNoReturn() || rbaseInfo.getNoReturn();
54268cc246c9a68c783a5b90d2e8b8927521cb3a49b7John McCall  if (NoReturn != lbaseInfo.getNoReturn())
54276a15c8586cc3e5d6e01c4c4e8b4374584569b58fDaniel Dunbar    allLTypes = false;
54288cc246c9a68c783a5b90d2e8b8927521cb3a49b7John McCall  if (NoReturn != rbaseInfo.getNoReturn())
54296a15c8586cc3e5d6e01c4c4e8b4374584569b58fDaniel Dunbar    allRTypes = false;
54301eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
54318cc246c9a68c783a5b90d2e8b8927521cb3a49b7John McCall  FunctionType::ExtInfo einfo(NoReturn,
5432a49218e17bcbb1acde0245773173e2c0c42f4f19Eli Friedman                              lbaseInfo.getHasRegParm(),
54338cc246c9a68c783a5b90d2e8b8927521cb3a49b7John McCall                              lbaseInfo.getRegParm(),
54348cc246c9a68c783a5b90d2e8b8927521cb3a49b7John McCall                              lbaseInfo.getCC());
5435e23cf437fe76b1ed02d63c3f61b456fd48a915f5John McCall
54363d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman  if (lproto && rproto) { // two C99 style function prototypes
5437465226e23a3008bd68973513dda1f9e3cd27dbddSebastian Redl    assert(!lproto->hasExceptionSpec() && !rproto->hasExceptionSpec() &&
5438465226e23a3008bd68973513dda1f9e3cd27dbddSebastian Redl           "C++ shouldn't be here");
54393d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    unsigned lproto_nargs = lproto->getNumArgs();
54403d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    unsigned rproto_nargs = rproto->getNumArgs();
54413d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman
54423d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    // Compatible functions must have the same number of arguments
54433d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    if (lproto_nargs != rproto_nargs)
54443d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman      return QualType();
54453d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman
54463d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    // Variadic and non-variadic functions aren't compatible
54473d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    if (lproto->isVariadic() != rproto->isVariadic())
54483d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman      return QualType();
54493d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman
54507fb5e4888221cd36652d078c6b171ac55e7f406dArgyrios Kyrtzidis    if (lproto->getTypeQuals() != rproto->getTypeQuals())
54517fb5e4888221cd36652d078c6b171ac55e7f406dArgyrios Kyrtzidis      return QualType();
54527fb5e4888221cd36652d078c6b171ac55e7f406dArgyrios Kyrtzidis
54533d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    // Check argument compatibility
54543d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    llvm::SmallVector<QualType, 10> types;
54553d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    for (unsigned i = 0; i < lproto_nargs; i++) {
54563d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman      QualType largtype = lproto->getArgType(i).getUnqualifiedType();
54573d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman      QualType rargtype = rproto->getArgType(i).getUnqualifiedType();
54584846675e0e42d1802b0ffd8972a45e72aeb3758dPeter Collingbourne      QualType argtype = mergeFunctionArgumentTypes(largtype, rargtype,
54594846675e0e42d1802b0ffd8972a45e72aeb3758dPeter Collingbourne                                                    OfBlockPointer,
54604846675e0e42d1802b0ffd8972a45e72aeb3758dPeter Collingbourne                                                    Unqualified);
54613d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman      if (argtype.isNull()) return QualType();
5462447234dd459a00a5ed9b7c3e066162cd7a75bf2dDouglas Gregor
5463447234dd459a00a5ed9b7c3e066162cd7a75bf2dDouglas Gregor      if (Unqualified)
5464447234dd459a00a5ed9b7c3e066162cd7a75bf2dDouglas Gregor        argtype = argtype.getUnqualifiedType();
5465447234dd459a00a5ed9b7c3e066162cd7a75bf2dDouglas Gregor
54663d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman      types.push_back(argtype);
5467447234dd459a00a5ed9b7c3e066162cd7a75bf2dDouglas Gregor      if (Unqualified) {
5468447234dd459a00a5ed9b7c3e066162cd7a75bf2dDouglas Gregor        largtype = largtype.getUnqualifiedType();
5469447234dd459a00a5ed9b7c3e066162cd7a75bf2dDouglas Gregor        rargtype = rargtype.getUnqualifiedType();
5470447234dd459a00a5ed9b7c3e066162cd7a75bf2dDouglas Gregor      }
5471447234dd459a00a5ed9b7c3e066162cd7a75bf2dDouglas Gregor
547261710854be2b098428aff5316e64bd34b30fbcb7Chris Lattner      if (getCanonicalType(argtype) != getCanonicalType(largtype))
547361710854be2b098428aff5316e64bd34b30fbcb7Chris Lattner        allLTypes = false;
547461710854be2b098428aff5316e64bd34b30fbcb7Chris Lattner      if (getCanonicalType(argtype) != getCanonicalType(rargtype))
547561710854be2b098428aff5316e64bd34b30fbcb7Chris Lattner        allRTypes = false;
54763d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    }
54773d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    if (allLTypes) return lhs;
54783d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    if (allRTypes) return rhs;
5479e23cf437fe76b1ed02d63c3f61b456fd48a915f5John McCall
5480e23cf437fe76b1ed02d63c3f61b456fd48a915f5John McCall    FunctionProtoType::ExtProtoInfo EPI = lproto->getExtProtoInfo();
5481e23cf437fe76b1ed02d63c3f61b456fd48a915f5John McCall    EPI.ExtInfo = einfo;
5482e23cf437fe76b1ed02d63c3f61b456fd48a915f5John McCall    return getFunctionType(retType, types.begin(), types.size(), EPI);
54833d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman  }
54843d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman
54853d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman  if (lproto) allRTypes = false;
54863d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman  if (rproto) allLTypes = false;
54873d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman
548872564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor  const FunctionProtoType *proto = lproto ? lproto : rproto;
54893d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman  if (proto) {
5490465226e23a3008bd68973513dda1f9e3cd27dbddSebastian Redl    assert(!proto->hasExceptionSpec() && "C++ shouldn't be here");
54913d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    if (proto->isVariadic()) return QualType();
54923d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    // Check that the types are compatible with the types that
54933d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    // would result from default argument promotions (C99 6.7.5.3p15).
54943d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    // The only types actually affected are promotable integer
54953d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    // types and floats, which would be passed as a different
54963d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    // type depending on whether the prototype is visible.
54973d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    unsigned proto_nargs = proto->getNumArgs();
54983d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    for (unsigned i = 0; i < proto_nargs; ++i) {
54993d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman      QualType argTy = proto->getArgType(i);
5500b0f8eacfdcd0d43f51e669f2d723992d4af9f746Douglas Gregor
5501b0f8eacfdcd0d43f51e669f2d723992d4af9f746Douglas Gregor      // Look at the promotion type of enum types, since that is the type used
5502b0f8eacfdcd0d43f51e669f2d723992d4af9f746Douglas Gregor      // to pass enum values.
5503b0f8eacfdcd0d43f51e669f2d723992d4af9f746Douglas Gregor      if (const EnumType *Enum = argTy->getAs<EnumType>())
5504b0f8eacfdcd0d43f51e669f2d723992d4af9f746Douglas Gregor        argTy = Enum->getDecl()->getPromotionType();
5505b0f8eacfdcd0d43f51e669f2d723992d4af9f746Douglas Gregor
55063d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman      if (argTy->isPromotableIntegerType() ||
55073d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman          getCanonicalType(argTy).getUnqualifiedType() == FloatTy)
55083d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman        return QualType();
55093d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    }
55103d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman
55113d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    if (allLTypes) return lhs;
55123d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    if (allRTypes) return rhs;
5513e23cf437fe76b1ed02d63c3f61b456fd48a915f5John McCall
5514e23cf437fe76b1ed02d63c3f61b456fd48a915f5John McCall    FunctionProtoType::ExtProtoInfo EPI = proto->getExtProtoInfo();
5515e23cf437fe76b1ed02d63c3f61b456fd48a915f5John McCall    EPI.ExtInfo = einfo;
55163d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    return getFunctionType(retType, proto->arg_type_begin(),
5517e23cf437fe76b1ed02d63c3f61b456fd48a915f5John McCall                           proto->getNumArgs(), EPI);
55183d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman  }
55193d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman
55203d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman  if (allLTypes) return lhs;
55213d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman  if (allRTypes) return rhs;
55228cc246c9a68c783a5b90d2e8b8927521cb3a49b7John McCall  return getFunctionNoProtoType(retType, einfo);
55233d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman}
55243d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman
5525132f2a2da34f378fc675b9e174564b0f52c31d98Fariborz JahanianQualType ASTContext::mergeTypes(QualType LHS, QualType RHS,
5526447234dd459a00a5ed9b7c3e066162cd7a75bf2dDouglas Gregor                                bool OfBlockPointer,
5527a4fdbfad150ae37bddaa4094d3925a27a1a1cf3fFariborz Jahanian                                bool Unqualified, bool BlockReturnType) {
552843d69750e7f7b26076e7474dec8839bb777b260fBill Wendling  // C++ [expr]: If an expression initially has the type "reference to T", the
552943d69750e7f7b26076e7474dec8839bb777b260fBill Wendling  // type is adjusted to "T" prior to any further analysis, the expression
553043d69750e7f7b26076e7474dec8839bb777b260fBill Wendling  // designates the object or function denoted by the reference, and the
55317c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl  // expression is an lvalue unless the reference is an rvalue reference and
55327c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl  // the expression is a function call (possibly inside parentheses).
55330e709abafbd939326850501f795cc7a92c88a354Douglas Gregor  assert(!LHS->getAs<ReferenceType>() && "LHS is a reference type?");
55340e709abafbd939326850501f795cc7a92c88a354Douglas Gregor  assert(!RHS->getAs<ReferenceType>() && "RHS is a reference type?");
5535447234dd459a00a5ed9b7c3e066162cd7a75bf2dDouglas Gregor
5536447234dd459a00a5ed9b7c3e066162cd7a75bf2dDouglas Gregor  if (Unqualified) {
5537447234dd459a00a5ed9b7c3e066162cd7a75bf2dDouglas Gregor    LHS = LHS.getUnqualifiedType();
5538447234dd459a00a5ed9b7c3e066162cd7a75bf2dDouglas Gregor    RHS = RHS.getUnqualifiedType();
5539447234dd459a00a5ed9b7c3e066162cd7a75bf2dDouglas Gregor  }
55400e709abafbd939326850501f795cc7a92c88a354Douglas Gregor
55413d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman  QualType LHSCan = getCanonicalType(LHS),
55423d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman           RHSCan = getCanonicalType(RHS);
55433d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman
5544f3692dc4a47dc48d10cec0415c6e9e39b7a39707Chris Lattner  // If two types are identical, they are compatible.
55453d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman  if (LHSCan == RHSCan)
55463d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    return LHS;
55473d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman
55480953e767ff7817f97b3ab20896b229891eeff45bJohn McCall  // If the qualifiers are different, the types aren't compatible... mostly.
5549a4923eb7c4b04d360cb2747641a5e92818edf804Douglas Gregor  Qualifiers LQuals = LHSCan.getLocalQualifiers();
5550a4923eb7c4b04d360cb2747641a5e92818edf804Douglas Gregor  Qualifiers RQuals = RHSCan.getLocalQualifiers();
55510953e767ff7817f97b3ab20896b229891eeff45bJohn McCall  if (LQuals != RQuals) {
55520953e767ff7817f97b3ab20896b229891eeff45bJohn McCall    // If any of these qualifiers are different, we have a type
55530953e767ff7817f97b3ab20896b229891eeff45bJohn McCall    // mismatch.
55540953e767ff7817f97b3ab20896b229891eeff45bJohn McCall    if (LQuals.getCVRQualifiers() != RQuals.getCVRQualifiers() ||
55550953e767ff7817f97b3ab20896b229891eeff45bJohn McCall        LQuals.getAddressSpace() != RQuals.getAddressSpace())
55560953e767ff7817f97b3ab20896b229891eeff45bJohn McCall      return QualType();
55570953e767ff7817f97b3ab20896b229891eeff45bJohn McCall
55580953e767ff7817f97b3ab20896b229891eeff45bJohn McCall    // Exactly one GC qualifier difference is allowed: __strong is
55590953e767ff7817f97b3ab20896b229891eeff45bJohn McCall    // okay if the other type has no GC qualifier but is an Objective
55600953e767ff7817f97b3ab20896b229891eeff45bJohn McCall    // C object pointer (i.e. implicitly strong by default).  We fix
55610953e767ff7817f97b3ab20896b229891eeff45bJohn McCall    // this by pretending that the unqualified type was actually
55620953e767ff7817f97b3ab20896b229891eeff45bJohn McCall    // qualified __strong.
55630953e767ff7817f97b3ab20896b229891eeff45bJohn McCall    Qualifiers::GC GC_L = LQuals.getObjCGCAttr();
55640953e767ff7817f97b3ab20896b229891eeff45bJohn McCall    Qualifiers::GC GC_R = RQuals.getObjCGCAttr();
55650953e767ff7817f97b3ab20896b229891eeff45bJohn McCall    assert((GC_L != GC_R) && "unequal qualifier sets had only equal elements");
55660953e767ff7817f97b3ab20896b229891eeff45bJohn McCall
55670953e767ff7817f97b3ab20896b229891eeff45bJohn McCall    if (GC_L == Qualifiers::Weak || GC_R == Qualifiers::Weak)
55680953e767ff7817f97b3ab20896b229891eeff45bJohn McCall      return QualType();
55690953e767ff7817f97b3ab20896b229891eeff45bJohn McCall
55700953e767ff7817f97b3ab20896b229891eeff45bJohn McCall    if (GC_L == Qualifiers::Strong && RHSCan->isObjCObjectPointerType()) {
55710953e767ff7817f97b3ab20896b229891eeff45bJohn McCall      return mergeTypes(LHS, getObjCGCQualType(RHS, Qualifiers::Strong));
55720953e767ff7817f97b3ab20896b229891eeff45bJohn McCall    }
55730953e767ff7817f97b3ab20896b229891eeff45bJohn McCall    if (GC_R == Qualifiers::Strong && LHSCan->isObjCObjectPointerType()) {
55740953e767ff7817f97b3ab20896b229891eeff45bJohn McCall      return mergeTypes(getObjCGCQualType(LHS, Qualifiers::Strong), RHS);
55750953e767ff7817f97b3ab20896b229891eeff45bJohn McCall    }
55763d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    return QualType();
55770953e767ff7817f97b3ab20896b229891eeff45bJohn McCall  }
55780953e767ff7817f97b3ab20896b229891eeff45bJohn McCall
55790953e767ff7817f97b3ab20896b229891eeff45bJohn McCall  // Okay, qualifiers are equal.
55803d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman
5581852d63b806c5cbd730c6b9d696e2e27d02546b49Eli Friedman  Type::TypeClass LHSClass = LHSCan->getTypeClass();
5582852d63b806c5cbd730c6b9d696e2e27d02546b49Eli Friedman  Type::TypeClass RHSClass = RHSCan->getTypeClass();
5583f3692dc4a47dc48d10cec0415c6e9e39b7a39707Chris Lattner
55841adb88370beab45af2f065afe86b51ccd59ec50dChris Lattner  // We want to consider the two function types to be the same for these
55851adb88370beab45af2f065afe86b51ccd59ec50dChris Lattner  // comparisons, just force one to the other.
55861adb88370beab45af2f065afe86b51ccd59ec50dChris Lattner  if (LHSClass == Type::FunctionProto) LHSClass = Type::FunctionNoProto;
55871adb88370beab45af2f065afe86b51ccd59ec50dChris Lattner  if (RHSClass == Type::FunctionProto) RHSClass = Type::FunctionNoProto;
55884c721d381fb279899337d120edd4a24d405e56b2Eli Friedman
55894c721d381fb279899337d120edd4a24d405e56b2Eli Friedman  // Same as above for arrays
5590a36a61f218b9f7a97f2c0f511e0b29eb42e8f78bChris Lattner  if (LHSClass == Type::VariableArray || LHSClass == Type::IncompleteArray)
5591a36a61f218b9f7a97f2c0f511e0b29eb42e8f78bChris Lattner    LHSClass = Type::ConstantArray;
5592a36a61f218b9f7a97f2c0f511e0b29eb42e8f78bChris Lattner  if (RHSClass == Type::VariableArray || RHSClass == Type::IncompleteArray)
5593a36a61f218b9f7a97f2c0f511e0b29eb42e8f78bChris Lattner    RHSClass = Type::ConstantArray;
55941eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
5595c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall  // ObjCInterfaces are just specialized ObjCObjects.
5596c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall  if (LHSClass == Type::ObjCInterface) LHSClass = Type::ObjCObject;
5597c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall  if (RHSClass == Type::ObjCInterface) RHSClass = Type::ObjCObject;
5598c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall
5599213541a68a3e137d11d2cefb612c6cdb410d7e8eNate Begeman  // Canonicalize ExtVector -> Vector.
5600213541a68a3e137d11d2cefb612c6cdb410d7e8eNate Begeman  if (LHSClass == Type::ExtVector) LHSClass = Type::Vector;
5601213541a68a3e137d11d2cefb612c6cdb410d7e8eNate Begeman  if (RHSClass == Type::ExtVector) RHSClass = Type::Vector;
56021eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
56034e78fd0a960eaa7e97467f2e8f390f3a57da279bSteve Naroff  // If the canonical type classes don't match.
56044e78fd0a960eaa7e97467f2e8f390f3a57da279bSteve Naroff  if (LHSClass != RHSClass) {
56051adb88370beab45af2f065afe86b51ccd59ec50dChris Lattner    // C99 6.7.2.2p4: Each enumerated type shall be compatible with char,
56061eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    // a signed integer type, or an unsigned integer type.
5607842aef8d942a880eeb9535d40de31a86838264cbJohn McCall    // Compatibility is based on the underlying type, not the promotion
5608842aef8d942a880eeb9535d40de31a86838264cbJohn McCall    // type.
5609183700f494ec9b6701b6efe82bcb25f4c79ba561John McCall    if (const EnumType* ETy = LHS->getAs<EnumType>()) {
56103d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman      if (ETy->getDecl()->getIntegerType() == RHSCan.getUnqualifiedType())
56113d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman        return RHS;
5612bab96968886f4b77083f4e26a28986ddb1e42d67Eli Friedman    }
5613183700f494ec9b6701b6efe82bcb25f4c79ba561John McCall    if (const EnumType* ETy = RHS->getAs<EnumType>()) {
56143d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman      if (ETy->getDecl()->getIntegerType() == LHSCan.getUnqualifiedType())
56153d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman        return LHS;
5616bab96968886f4b77083f4e26a28986ddb1e42d67Eli Friedman    }
56171adb88370beab45af2f065afe86b51ccd59ec50dChris Lattner
56183d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    return QualType();
5619ec0550fa3653d46560bf4484a2e988329c228e39Steve Naroff  }
56203d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman
56214a74678ed6c3dedac05d02b1ee341f1db869f049Steve Naroff  // The canonical type classes match.
56221adb88370beab45af2f065afe86b51ccd59ec50dChris Lattner  switch (LHSClass) {
562372564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor#define TYPE(Class, Base)
562472564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor#define ABSTRACT_TYPE(Class, Base)
5625ad5e73887052193afda72db8efcb812bd083a4a8John McCall#define NON_CANONICAL_UNLESS_DEPENDENT_TYPE(Class, Base) case Type::Class:
562672564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor#define NON_CANONICAL_TYPE(Class, Base) case Type::Class:
562772564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor#define DEPENDENT_TYPE(Class, Base) case Type::Class:
562872564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor#include "clang/AST/TypeNodes.def"
562972564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor    assert(false && "Non-canonical and dependent types shouldn't get here");
563072564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor    return QualType();
563172564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor
56327c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl  case Type::LValueReference:
56337c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl  case Type::RValueReference:
563472564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor  case Type::MemberPointer:
563572564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor    assert(false && "C++ should never be in mergeTypes");
563672564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor    return QualType();
563772564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor
5638c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall  case Type::ObjCInterface:
563972564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor  case Type::IncompleteArray:
564072564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor  case Type::VariableArray:
564172564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor  case Type::FunctionProto:
564272564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor  case Type::ExtVector:
564372564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor    assert(false && "Types are eliminated above");
564472564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor    return QualType();
564572564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor
56461adb88370beab45af2f065afe86b51ccd59ec50dChris Lattner  case Type::Pointer:
56473d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman  {
56483d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    // Merge two pointer types, while trying to preserve typedef info
56496217b80b7a1379b74cced1c076338262c3c980b3Ted Kremenek    QualType LHSPointee = LHS->getAs<PointerType>()->getPointeeType();
56506217b80b7a1379b74cced1c076338262c3c980b3Ted Kremenek    QualType RHSPointee = RHS->getAs<PointerType>()->getPointeeType();
5651447234dd459a00a5ed9b7c3e066162cd7a75bf2dDouglas Gregor    if (Unqualified) {
5652447234dd459a00a5ed9b7c3e066162cd7a75bf2dDouglas Gregor      LHSPointee = LHSPointee.getUnqualifiedType();
5653447234dd459a00a5ed9b7c3e066162cd7a75bf2dDouglas Gregor      RHSPointee = RHSPointee.getUnqualifiedType();
5654447234dd459a00a5ed9b7c3e066162cd7a75bf2dDouglas Gregor    }
5655447234dd459a00a5ed9b7c3e066162cd7a75bf2dDouglas Gregor    QualType ResultType = mergeTypes(LHSPointee, RHSPointee, false,
5656447234dd459a00a5ed9b7c3e066162cd7a75bf2dDouglas Gregor                                     Unqualified);
56573d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    if (ResultType.isNull()) return QualType();
565807d258756dc856c6987c394a0972884e6ed46765Eli Friedman    if (getCanonicalType(LHSPointee) == getCanonicalType(ResultType))
565961710854be2b098428aff5316e64bd34b30fbcb7Chris Lattner      return LHS;
566007d258756dc856c6987c394a0972884e6ed46765Eli Friedman    if (getCanonicalType(RHSPointee) == getCanonicalType(ResultType))
566161710854be2b098428aff5316e64bd34b30fbcb7Chris Lattner      return RHS;
56623d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    return getPointerType(ResultType);
56633d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman  }
5664c0febd58f5cbf4a93fd12f461863564dba0af76dSteve Naroff  case Type::BlockPointer:
5665c0febd58f5cbf4a93fd12f461863564dba0af76dSteve Naroff  {
5666c0febd58f5cbf4a93fd12f461863564dba0af76dSteve Naroff    // Merge two block pointer types, while trying to preserve typedef info
56676217b80b7a1379b74cced1c076338262c3c980b3Ted Kremenek    QualType LHSPointee = LHS->getAs<BlockPointerType>()->getPointeeType();
56686217b80b7a1379b74cced1c076338262c3c980b3Ted Kremenek    QualType RHSPointee = RHS->getAs<BlockPointerType>()->getPointeeType();
5669447234dd459a00a5ed9b7c3e066162cd7a75bf2dDouglas Gregor    if (Unqualified) {
5670447234dd459a00a5ed9b7c3e066162cd7a75bf2dDouglas Gregor      LHSPointee = LHSPointee.getUnqualifiedType();
5671447234dd459a00a5ed9b7c3e066162cd7a75bf2dDouglas Gregor      RHSPointee = RHSPointee.getUnqualifiedType();
5672447234dd459a00a5ed9b7c3e066162cd7a75bf2dDouglas Gregor    }
5673447234dd459a00a5ed9b7c3e066162cd7a75bf2dDouglas Gregor    QualType ResultType = mergeTypes(LHSPointee, RHSPointee, OfBlockPointer,
5674447234dd459a00a5ed9b7c3e066162cd7a75bf2dDouglas Gregor                                     Unqualified);
5675c0febd58f5cbf4a93fd12f461863564dba0af76dSteve Naroff    if (ResultType.isNull()) return QualType();
5676c0febd58f5cbf4a93fd12f461863564dba0af76dSteve Naroff    if (getCanonicalType(LHSPointee) == getCanonicalType(ResultType))
5677c0febd58f5cbf4a93fd12f461863564dba0af76dSteve Naroff      return LHS;
5678c0febd58f5cbf4a93fd12f461863564dba0af76dSteve Naroff    if (getCanonicalType(RHSPointee) == getCanonicalType(ResultType))
5679c0febd58f5cbf4a93fd12f461863564dba0af76dSteve Naroff      return RHS;
5680c0febd58f5cbf4a93fd12f461863564dba0af76dSteve Naroff    return getBlockPointerType(ResultType);
5681c0febd58f5cbf4a93fd12f461863564dba0af76dSteve Naroff  }
56821adb88370beab45af2f065afe86b51ccd59ec50dChris Lattner  case Type::ConstantArray:
56833d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman  {
56843d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    const ConstantArrayType* LCAT = getAsConstantArrayType(LHS);
56853d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    const ConstantArrayType* RCAT = getAsConstantArrayType(RHS);
56863d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    if (LCAT && RCAT && RCAT->getSize() != LCAT->getSize())
56873d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman      return QualType();
56883d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman
56893d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    QualType LHSElem = getAsArrayType(LHS)->getElementType();
56903d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    QualType RHSElem = getAsArrayType(RHS)->getElementType();
5691447234dd459a00a5ed9b7c3e066162cd7a75bf2dDouglas Gregor    if (Unqualified) {
5692447234dd459a00a5ed9b7c3e066162cd7a75bf2dDouglas Gregor      LHSElem = LHSElem.getUnqualifiedType();
5693447234dd459a00a5ed9b7c3e066162cd7a75bf2dDouglas Gregor      RHSElem = RHSElem.getUnqualifiedType();
5694447234dd459a00a5ed9b7c3e066162cd7a75bf2dDouglas Gregor    }
5695447234dd459a00a5ed9b7c3e066162cd7a75bf2dDouglas Gregor
5696447234dd459a00a5ed9b7c3e066162cd7a75bf2dDouglas Gregor    QualType ResultType = mergeTypes(LHSElem, RHSElem, false, Unqualified);
56973d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    if (ResultType.isNull()) return QualType();
569861710854be2b098428aff5316e64bd34b30fbcb7Chris Lattner    if (LCAT && getCanonicalType(LHSElem) == getCanonicalType(ResultType))
569961710854be2b098428aff5316e64bd34b30fbcb7Chris Lattner      return LHS;
570061710854be2b098428aff5316e64bd34b30fbcb7Chris Lattner    if (RCAT && getCanonicalType(RHSElem) == getCanonicalType(ResultType))
570161710854be2b098428aff5316e64bd34b30fbcb7Chris Lattner      return RHS;
57023bc0f45a5e65814f42b22dcdf7249d1120d16f36Eli Friedman    if (LCAT) return getConstantArrayType(ResultType, LCAT->getSize(),
57033bc0f45a5e65814f42b22dcdf7249d1120d16f36Eli Friedman                                          ArrayType::ArraySizeModifier(), 0);
57043bc0f45a5e65814f42b22dcdf7249d1120d16f36Eli Friedman    if (RCAT) return getConstantArrayType(ResultType, RCAT->getSize(),
57053bc0f45a5e65814f42b22dcdf7249d1120d16f36Eli Friedman                                          ArrayType::ArraySizeModifier(), 0);
57063d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    const VariableArrayType* LVAT = getAsVariableArrayType(LHS);
57073d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    const VariableArrayType* RVAT = getAsVariableArrayType(RHS);
570861710854be2b098428aff5316e64bd34b30fbcb7Chris Lattner    if (LVAT && getCanonicalType(LHSElem) == getCanonicalType(ResultType))
570961710854be2b098428aff5316e64bd34b30fbcb7Chris Lattner      return LHS;
571061710854be2b098428aff5316e64bd34b30fbcb7Chris Lattner    if (RVAT && getCanonicalType(RHSElem) == getCanonicalType(ResultType))
571161710854be2b098428aff5316e64bd34b30fbcb7Chris Lattner      return RHS;
57123d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    if (LVAT) {
57133d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman      // FIXME: This isn't correct! But tricky to implement because
57143d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman      // the array's size has to be the size of LHS, but the type
57153d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman      // has to be different.
57163d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman      return LHS;
57173d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    }
57183d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    if (RVAT) {
57193d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman      // FIXME: This isn't correct! But tricky to implement because
57203d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman      // the array's size has to be the size of RHS, but the type
57213d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman      // has to be different.
57223d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman      return RHS;
57233d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    }
57243bc0f45a5e65814f42b22dcdf7249d1120d16f36Eli Friedman    if (getCanonicalType(LHSElem) == getCanonicalType(ResultType)) return LHS;
57253bc0f45a5e65814f42b22dcdf7249d1120d16f36Eli Friedman    if (getCanonicalType(RHSElem) == getCanonicalType(ResultType)) return RHS;
57267e7eb3da052a6d80ddf2377cab0384c798f73f75Douglas Gregor    return getIncompleteArrayType(ResultType,
57277e7eb3da052a6d80ddf2377cab0384c798f73f75Douglas Gregor                                  ArrayType::ArraySizeModifier(), 0);
57283d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman  }
57291adb88370beab45af2f065afe86b51ccd59ec50dChris Lattner  case Type::FunctionNoProto:
5730447234dd459a00a5ed9b7c3e066162cd7a75bf2dDouglas Gregor    return mergeFunctionTypes(LHS, RHS, OfBlockPointer, Unqualified);
573172564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor  case Type::Record:
573272564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor  case Type::Enum:
57333d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    return QualType();
57341adb88370beab45af2f065afe86b51ccd59ec50dChris Lattner  case Type::Builtin:
57353cc4c0c3058a788689b8fc73c0ac139544435c97Chris Lattner    // Only exactly equal builtin types are compatible, which is tested above.
57363d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    return QualType();
573764cfdb7da3cb744642fe8a99ad5c851ad3c930b2Daniel Dunbar  case Type::Complex:
573864cfdb7da3cb744642fe8a99ad5c851ad3c930b2Daniel Dunbar    // Distinct complex types are incompatible.
573964cfdb7da3cb744642fe8a99ad5c851ad3c930b2Daniel Dunbar    return QualType();
57403cc4c0c3058a788689b8fc73c0ac139544435c97Chris Lattner  case Type::Vector:
57415a61f0e5c5aaecd5713c3fa4b78be7167a7eeff2Eli Friedman    // FIXME: The merged type should be an ExtVector!
57421c471f3e1c0ec8cbc82447bb35908dfc55463e46John McCall    if (areCompatVectorTypes(LHSCan->getAs<VectorType>(),
57431c471f3e1c0ec8cbc82447bb35908dfc55463e46John McCall                             RHSCan->getAs<VectorType>()))
57443d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman      return LHS;
574561710854be2b098428aff5316e64bd34b30fbcb7Chris Lattner    return QualType();
5746c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall  case Type::ObjCObject: {
5747c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall    // Check if the types are assignment compatible.
57485a61f0e5c5aaecd5713c3fa4b78be7167a7eeff2Eli Friedman    // FIXME: This should be type compatibility, e.g. whether
57495a61f0e5c5aaecd5713c3fa4b78be7167a7eeff2Eli Friedman    // "LHS x; RHS x;" at global scope is legal.
5750c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall    const ObjCObjectType* LHSIface = LHS->getAs<ObjCObjectType>();
5751c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall    const ObjCObjectType* RHSIface = RHS->getAs<ObjCObjectType>();
5752c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall    if (canAssignObjCInterfaces(LHSIface, RHSIface))
57535fd659db11922fc12a58e478f7b745f9656b15a7Steve Naroff      return LHS;
57545fd659db11922fc12a58e478f7b745f9656b15a7Steve Naroff
57553d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    return QualType();
575661490e9a965cfee8a78c12c6802138844f04250dCedric Venet  }
575714108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff  case Type::ObjCObjectPointer: {
5758132f2a2da34f378fc675b9e174564b0f52c31d98Fariborz Jahanian    if (OfBlockPointer) {
5759132f2a2da34f378fc675b9e174564b0f52c31d98Fariborz Jahanian      if (canAssignObjCInterfacesInBlockPointer(
5760132f2a2da34f378fc675b9e174564b0f52c31d98Fariborz Jahanian                                          LHS->getAs<ObjCObjectPointerType>(),
5761a4fdbfad150ae37bddaa4094d3925a27a1a1cf3fFariborz Jahanian                                          RHS->getAs<ObjCObjectPointerType>(),
5762a4fdbfad150ae37bddaa4094d3925a27a1a1cf3fFariborz Jahanian                                          BlockReturnType))
5763132f2a2da34f378fc675b9e174564b0f52c31d98Fariborz Jahanian      return LHS;
5764132f2a2da34f378fc675b9e174564b0f52c31d98Fariborz Jahanian      return QualType();
5765132f2a2da34f378fc675b9e174564b0f52c31d98Fariborz Jahanian    }
5766183700f494ec9b6701b6efe82bcb25f4c79ba561John McCall    if (canAssignObjCInterfaces(LHS->getAs<ObjCObjectPointerType>(),
5767183700f494ec9b6701b6efe82bcb25f4c79ba561John McCall                                RHS->getAs<ObjCObjectPointerType>()))
576814108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff      return LHS;
576914108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff
5770bc76dd06eb881c70c9775b74bab8b88cd747f173Steve Naroff    return QualType();
5771132f2a2da34f378fc675b9e174564b0f52c31d98Fariborz Jahanian    }
5772ec0550fa3653d46560bf4484a2e988329c228e39Steve Naroff  }
577372564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor
577472564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor  return QualType();
5775ec0550fa3653d46560bf4484a2e988329c228e39Steve Naroff}
57767192f8e9592729882a09d84d77838db26e39ebd4Ted Kremenek
57772390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian/// mergeObjCGCQualifiers - This routine merges ObjC's GC attribute of 'LHS' and
57782390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian/// 'RHS' attributes and returns the merged version; including for function
57792390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian/// return types.
57802390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz JahanianQualType ASTContext::mergeObjCGCQualifiers(QualType LHS, QualType RHS) {
57812390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian  QualType LHSCan = getCanonicalType(LHS),
57822390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian  RHSCan = getCanonicalType(RHS);
57832390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian  // If two types are identical, they are compatible.
57842390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian  if (LHSCan == RHSCan)
57852390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian    return LHS;
57862390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian  if (RHSCan->isFunctionType()) {
57872390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian    if (!LHSCan->isFunctionType())
57882390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian      return QualType();
57892390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian    QualType OldReturnType =
57902390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian      cast<FunctionType>(RHSCan.getTypePtr())->getResultType();
57912390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian    QualType NewReturnType =
57922390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian      cast<FunctionType>(LHSCan.getTypePtr())->getResultType();
57932390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian    QualType ResReturnType =
57942390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian      mergeObjCGCQualifiers(NewReturnType, OldReturnType);
57952390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian    if (ResReturnType.isNull())
57962390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian      return QualType();
57972390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian    if (ResReturnType == NewReturnType || ResReturnType == OldReturnType) {
57982390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian      // id foo(); ... __strong id foo(); or: __strong id foo(); ... id foo();
57992390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian      // In either case, use OldReturnType to build the new function type.
58002390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian      const FunctionType *F = LHS->getAs<FunctionType>();
58012390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian      if (const FunctionProtoType *FPT = cast<FunctionProtoType>(F)) {
5802e23cf437fe76b1ed02d63c3f61b456fd48a915f5John McCall        FunctionProtoType::ExtProtoInfo EPI = FPT->getExtProtoInfo();
5803e23cf437fe76b1ed02d63c3f61b456fd48a915f5John McCall        EPI.ExtInfo = getFunctionExtInfo(LHS);
58042390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian        QualType ResultType
58052390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian          = getFunctionType(OldReturnType, FPT->arg_type_begin(),
5806e23cf437fe76b1ed02d63c3f61b456fd48a915f5John McCall                            FPT->getNumArgs(), EPI);
58072390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian        return ResultType;
58082390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian      }
58092390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian    }
58102390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian    return QualType();
58112390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian  }
58122390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian
58132390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian  // If the qualifiers are different, the types can still be merged.
58142390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian  Qualifiers LQuals = LHSCan.getLocalQualifiers();
58152390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian  Qualifiers RQuals = RHSCan.getLocalQualifiers();
58162390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian  if (LQuals != RQuals) {
58172390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian    // If any of these qualifiers are different, we have a type mismatch.
58182390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian    if (LQuals.getCVRQualifiers() != RQuals.getCVRQualifiers() ||
58192390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian        LQuals.getAddressSpace() != RQuals.getAddressSpace())
58202390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian      return QualType();
58212390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian
58222390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian    // Exactly one GC qualifier difference is allowed: __strong is
58232390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian    // okay if the other type has no GC qualifier but is an Objective
58242390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian    // C object pointer (i.e. implicitly strong by default).  We fix
58252390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian    // this by pretending that the unqualified type was actually
58262390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian    // qualified __strong.
58272390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian    Qualifiers::GC GC_L = LQuals.getObjCGCAttr();
58282390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian    Qualifiers::GC GC_R = RQuals.getObjCGCAttr();
58292390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian    assert((GC_L != GC_R) && "unequal qualifier sets had only equal elements");
58302390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian
58312390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian    if (GC_L == Qualifiers::Weak || GC_R == Qualifiers::Weak)
58322390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian      return QualType();
58332390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian
58342390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian    if (GC_L == Qualifiers::Strong)
58352390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian      return LHS;
58362390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian    if (GC_R == Qualifiers::Strong)
58372390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian      return RHS;
58382390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian    return QualType();
58392390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian  }
58402390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian
58412390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian  if (LHSCan->isObjCObjectPointerType() && RHSCan->isObjCObjectPointerType()) {
58422390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian    QualType LHSBaseQT = LHS->getAs<ObjCObjectPointerType>()->getPointeeType();
58432390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian    QualType RHSBaseQT = RHS->getAs<ObjCObjectPointerType>()->getPointeeType();
58442390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian    QualType ResQT = mergeObjCGCQualifiers(LHSBaseQT, RHSBaseQT);
58452390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian    if (ResQT == LHSBaseQT)
58462390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian      return LHS;
58472390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian    if (ResQT == RHSBaseQT)
58482390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian      return RHS;
58492390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian  }
58502390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian  return QualType();
58512390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian}
58522390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian
58535426bf6456a5aeac416a9150de157904d101c819Chris Lattner//===----------------------------------------------------------------------===//
5854ad74a758189180b8ab8faea648e4766c3bfd7fcbEli Friedman//                         Integer Predicates
5855ad74a758189180b8ab8faea648e4766c3bfd7fcbEli Friedman//===----------------------------------------------------------------------===//
585688054dee0402e4d3c1f64e6b697acc47195c0d72Chris Lattner
58574ba2a17694148e16eaa8d3917f657ffcd3667be4Jay Foadunsigned ASTContext::getIntWidth(QualType T) const {
5858f4c7371fb1d3cebcfb40abad4537bb82515704eaJohn McCall  if (const EnumType *ET = dyn_cast<EnumType>(T))
585929a7f3342c3c6dd15d914c61ae22246c36d51ce7Eli Friedman    T = ET->getDecl()->getIntegerType();
58601274ccd90aec0b205fc838c3d504821ccfb55482Douglas Gregor  if (T->isBooleanType())
58611274ccd90aec0b205fc838c3d504821ccfb55482Douglas Gregor    return 1;
5862f98aba35e6c3da5aae61843fc01334939e4e12ecEli Friedman  // For builtin types, just use the standard type sizing method
5863ad74a758189180b8ab8faea648e4766c3bfd7fcbEli Friedman  return (unsigned)getTypeSize(T);
5864ad74a758189180b8ab8faea648e4766c3bfd7fcbEli Friedman}
5865ad74a758189180b8ab8faea648e4766c3bfd7fcbEli Friedman
5866ad74a758189180b8ab8faea648e4766c3bfd7fcbEli FriedmanQualType ASTContext::getCorrespondingUnsignedType(QualType T) {
5867f60946222721d9ba3c059563935c17b84703187aDouglas Gregor  assert(T->hasSignedIntegerRepresentation() && "Unexpected type");
58686a2b9261bf9c973c7122d9d1febce24a38fa862dChris Lattner
58696a2b9261bf9c973c7122d9d1febce24a38fa862dChris Lattner  // Turn <4 x signed int> -> <4 x unsigned int>
58706a2b9261bf9c973c7122d9d1febce24a38fa862dChris Lattner  if (const VectorType *VTy = T->getAs<VectorType>())
58716a2b9261bf9c973c7122d9d1febce24a38fa862dChris Lattner    return getVectorType(getCorrespondingUnsignedType(VTy->getElementType()),
5872e86d78cf4754a6aef2cf9a33d847aa15338e276fBob Wilson                         VTy->getNumElements(), VTy->getVectorKind());
58736a2b9261bf9c973c7122d9d1febce24a38fa862dChris Lattner
58746a2b9261bf9c973c7122d9d1febce24a38fa862dChris Lattner  // For enums, we return the unsigned version of the base type.
58756a2b9261bf9c973c7122d9d1febce24a38fa862dChris Lattner  if (const EnumType *ETy = T->getAs<EnumType>())
5876ad74a758189180b8ab8faea648e4766c3bfd7fcbEli Friedman    T = ETy->getDecl()->getIntegerType();
58776a2b9261bf9c973c7122d9d1febce24a38fa862dChris Lattner
58786a2b9261bf9c973c7122d9d1febce24a38fa862dChris Lattner  const BuiltinType *BTy = T->getAs<BuiltinType>();
58796a2b9261bf9c973c7122d9d1febce24a38fa862dChris Lattner  assert(BTy && "Unexpected signed integer type");
5880ad74a758189180b8ab8faea648e4766c3bfd7fcbEli Friedman  switch (BTy->getKind()) {
5881ad74a758189180b8ab8faea648e4766c3bfd7fcbEli Friedman  case BuiltinType::Char_S:
5882ad74a758189180b8ab8faea648e4766c3bfd7fcbEli Friedman  case BuiltinType::SChar:
5883ad74a758189180b8ab8faea648e4766c3bfd7fcbEli Friedman    return UnsignedCharTy;
5884ad74a758189180b8ab8faea648e4766c3bfd7fcbEli Friedman  case BuiltinType::Short:
5885ad74a758189180b8ab8faea648e4766c3bfd7fcbEli Friedman    return UnsignedShortTy;
5886ad74a758189180b8ab8faea648e4766c3bfd7fcbEli Friedman  case BuiltinType::Int:
5887ad74a758189180b8ab8faea648e4766c3bfd7fcbEli Friedman    return UnsignedIntTy;
5888ad74a758189180b8ab8faea648e4766c3bfd7fcbEli Friedman  case BuiltinType::Long:
5889ad74a758189180b8ab8faea648e4766c3bfd7fcbEli Friedman    return UnsignedLongTy;
5890ad74a758189180b8ab8faea648e4766c3bfd7fcbEli Friedman  case BuiltinType::LongLong:
5891ad74a758189180b8ab8faea648e4766c3bfd7fcbEli Friedman    return UnsignedLongLongTy;
58922df9ced9fd1e8c7d7b38443db07e0e811de22571Chris Lattner  case BuiltinType::Int128:
58932df9ced9fd1e8c7d7b38443db07e0e811de22571Chris Lattner    return UnsignedInt128Ty;
5894ad74a758189180b8ab8faea648e4766c3bfd7fcbEli Friedman  default:
5895ad74a758189180b8ab8faea648e4766c3bfd7fcbEli Friedman    assert(0 && "Unexpected signed integer type");
5896ad74a758189180b8ab8faea648e4766c3bfd7fcbEli Friedman    return QualType();
5897ad74a758189180b8ab8faea648e4766c3bfd7fcbEli Friedman  }
5898ad74a758189180b8ab8faea648e4766c3bfd7fcbEli Friedman}
5899ad74a758189180b8ab8faea648e4766c3bfd7fcbEli Friedman
59007b90340c9c7d07aef4e301e72b5e8a30d5f4f0c8Argyrios KyrtzidisASTMutationListener::~ASTMutationListener() { }
59017b90340c9c7d07aef4e301e72b5e8a30d5f4f0c8Argyrios Kyrtzidis
590286df27bbdbb98c39ec2184695c0561209f91beddChris Lattner
590386df27bbdbb98c39ec2184695c0561209f91beddChris Lattner//===----------------------------------------------------------------------===//
590486df27bbdbb98c39ec2184695c0561209f91beddChris Lattner//                          Builtin Type Computation
590586df27bbdbb98c39ec2184695c0561209f91beddChris Lattner//===----------------------------------------------------------------------===//
590686df27bbdbb98c39ec2184695c0561209f91beddChris Lattner
590786df27bbdbb98c39ec2184695c0561209f91beddChris Lattner/// DecodeTypeFromStr - This decodes one type descriptor from Str, advancing the
590833daae66462e8f51ee153463b32bdefd60c801aaChris Lattner/// pointer over the consumed characters.  This returns the resultant type.  If
590933daae66462e8f51ee153463b32bdefd60c801aaChris Lattner/// AllowTypeModifiers is false then modifier like * are not parsed, just basic
591033daae66462e8f51ee153463b32bdefd60c801aaChris Lattner/// types.  This allows "v2i*" to be parsed as a pointer to a v2i instead of
591133daae66462e8f51ee153463b32bdefd60c801aaChris Lattner/// a vector of "i*".
591214e0e7436cf6650a72052baea1f8ebe644cef489Chris Lattner///
591314e0e7436cf6650a72052baea1f8ebe644cef489Chris Lattner/// RequiresICE is filled in on return to indicate whether the value is required
591414e0e7436cf6650a72052baea1f8ebe644cef489Chris Lattner/// to be an Integer Constant Expression.
59154ba2a17694148e16eaa8d3917f657ffcd3667be4Jay Foadstatic QualType DecodeTypeFromStr(const char *&Str, const ASTContext &Context,
591686df27bbdbb98c39ec2184695c0561209f91beddChris Lattner                                  ASTContext::GetBuiltinTypeError &Error,
591714e0e7436cf6650a72052baea1f8ebe644cef489Chris Lattner                                  bool &RequiresICE,
591833daae66462e8f51ee153463b32bdefd60c801aaChris Lattner                                  bool AllowTypeModifiers) {
591986df27bbdbb98c39ec2184695c0561209f91beddChris Lattner  // Modifiers.
592086df27bbdbb98c39ec2184695c0561209f91beddChris Lattner  int HowLong = 0;
592186df27bbdbb98c39ec2184695c0561209f91beddChris Lattner  bool Signed = false, Unsigned = false;
592214e0e7436cf6650a72052baea1f8ebe644cef489Chris Lattner  RequiresICE = false;
5923393bd8e185692a451b2ba16bdfc9e7d3543b4217Chris Lattner
592433daae66462e8f51ee153463b32bdefd60c801aaChris Lattner  // Read the prefixed modifiers first.
592586df27bbdbb98c39ec2184695c0561209f91beddChris Lattner  bool Done = false;
592686df27bbdbb98c39ec2184695c0561209f91beddChris Lattner  while (!Done) {
592786df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    switch (*Str++) {
59281eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    default: Done = true; --Str; break;
5929393bd8e185692a451b2ba16bdfc9e7d3543b4217Chris Lattner    case 'I':
593014e0e7436cf6650a72052baea1f8ebe644cef489Chris Lattner      RequiresICE = true;
5931393bd8e185692a451b2ba16bdfc9e7d3543b4217Chris Lattner      break;
593286df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    case 'S':
593386df27bbdbb98c39ec2184695c0561209f91beddChris Lattner      assert(!Unsigned && "Can't use both 'S' and 'U' modifiers!");
593486df27bbdbb98c39ec2184695c0561209f91beddChris Lattner      assert(!Signed && "Can't use 'S' modifier multiple times!");
593586df27bbdbb98c39ec2184695c0561209f91beddChris Lattner      Signed = true;
593686df27bbdbb98c39ec2184695c0561209f91beddChris Lattner      break;
593786df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    case 'U':
593886df27bbdbb98c39ec2184695c0561209f91beddChris Lattner      assert(!Signed && "Can't use both 'S' and 'U' modifiers!");
593986df27bbdbb98c39ec2184695c0561209f91beddChris Lattner      assert(!Unsigned && "Can't use 'S' modifier multiple times!");
594086df27bbdbb98c39ec2184695c0561209f91beddChris Lattner      Unsigned = true;
594186df27bbdbb98c39ec2184695c0561209f91beddChris Lattner      break;
594286df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    case 'L':
594386df27bbdbb98c39ec2184695c0561209f91beddChris Lattner      assert(HowLong <= 2 && "Can't have LLLL modifier");
594486df27bbdbb98c39ec2184695c0561209f91beddChris Lattner      ++HowLong;
594586df27bbdbb98c39ec2184695c0561209f91beddChris Lattner      break;
594686df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    }
594786df27bbdbb98c39ec2184695c0561209f91beddChris Lattner  }
594886df27bbdbb98c39ec2184695c0561209f91beddChris Lattner
594986df27bbdbb98c39ec2184695c0561209f91beddChris Lattner  QualType Type;
59501eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
595186df27bbdbb98c39ec2184695c0561209f91beddChris Lattner  // Read the base type.
595286df27bbdbb98c39ec2184695c0561209f91beddChris Lattner  switch (*Str++) {
595386df27bbdbb98c39ec2184695c0561209f91beddChris Lattner  default: assert(0 && "Unknown builtin type letter!");
595486df27bbdbb98c39ec2184695c0561209f91beddChris Lattner  case 'v':
595586df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    assert(HowLong == 0 && !Signed && !Unsigned &&
595686df27bbdbb98c39ec2184695c0561209f91beddChris Lattner           "Bad modifiers used with 'v'!");
595786df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    Type = Context.VoidTy;
595886df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    break;
595986df27bbdbb98c39ec2184695c0561209f91beddChris Lattner  case 'f':
596086df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    assert(HowLong == 0 && !Signed && !Unsigned &&
596186df27bbdbb98c39ec2184695c0561209f91beddChris Lattner           "Bad modifiers used with 'f'!");
596286df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    Type = Context.FloatTy;
596386df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    break;
596486df27bbdbb98c39ec2184695c0561209f91beddChris Lattner  case 'd':
596586df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    assert(HowLong < 2 && !Signed && !Unsigned &&
596686df27bbdbb98c39ec2184695c0561209f91beddChris Lattner           "Bad modifiers used with 'd'!");
596786df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    if (HowLong)
596886df27bbdbb98c39ec2184695c0561209f91beddChris Lattner      Type = Context.LongDoubleTy;
596986df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    else
597086df27bbdbb98c39ec2184695c0561209f91beddChris Lattner      Type = Context.DoubleTy;
597186df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    break;
597286df27bbdbb98c39ec2184695c0561209f91beddChris Lattner  case 's':
597386df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    assert(HowLong == 0 && "Bad modifiers used with 's'!");
597486df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    if (Unsigned)
597586df27bbdbb98c39ec2184695c0561209f91beddChris Lattner      Type = Context.UnsignedShortTy;
597686df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    else
597786df27bbdbb98c39ec2184695c0561209f91beddChris Lattner      Type = Context.ShortTy;
597886df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    break;
597986df27bbdbb98c39ec2184695c0561209f91beddChris Lattner  case 'i':
598086df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    if (HowLong == 3)
598186df27bbdbb98c39ec2184695c0561209f91beddChris Lattner      Type = Unsigned ? Context.UnsignedInt128Ty : Context.Int128Ty;
598286df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    else if (HowLong == 2)
598386df27bbdbb98c39ec2184695c0561209f91beddChris Lattner      Type = Unsigned ? Context.UnsignedLongLongTy : Context.LongLongTy;
598486df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    else if (HowLong == 1)
598586df27bbdbb98c39ec2184695c0561209f91beddChris Lattner      Type = Unsigned ? Context.UnsignedLongTy : Context.LongTy;
598686df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    else
598786df27bbdbb98c39ec2184695c0561209f91beddChris Lattner      Type = Unsigned ? Context.UnsignedIntTy : Context.IntTy;
598886df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    break;
598986df27bbdbb98c39ec2184695c0561209f91beddChris Lattner  case 'c':
599086df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    assert(HowLong == 0 && "Bad modifiers used with 'c'!");
599186df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    if (Signed)
599286df27bbdbb98c39ec2184695c0561209f91beddChris Lattner      Type = Context.SignedCharTy;
599386df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    else if (Unsigned)
599486df27bbdbb98c39ec2184695c0561209f91beddChris Lattner      Type = Context.UnsignedCharTy;
599586df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    else
599686df27bbdbb98c39ec2184695c0561209f91beddChris Lattner      Type = Context.CharTy;
599786df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    break;
599886df27bbdbb98c39ec2184695c0561209f91beddChris Lattner  case 'b': // boolean
599986df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    assert(HowLong == 0 && !Signed && !Unsigned && "Bad modifiers for 'b'!");
600086df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    Type = Context.BoolTy;
600186df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    break;
600286df27bbdbb98c39ec2184695c0561209f91beddChris Lattner  case 'z':  // size_t.
600386df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    assert(HowLong == 0 && !Signed && !Unsigned && "Bad modifiers for 'z'!");
600486df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    Type = Context.getSizeType();
600586df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    break;
600686df27bbdbb98c39ec2184695c0561209f91beddChris Lattner  case 'F':
600786df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    Type = Context.getCFConstantStringType();
600886df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    break;
6009ba8bda05fefd3bb2f1ef201784b685f715bdde29Fariborz Jahanian  case 'G':
6010ba8bda05fefd3bb2f1ef201784b685f715bdde29Fariborz Jahanian    Type = Context.getObjCIdType();
6011ba8bda05fefd3bb2f1ef201784b685f715bdde29Fariborz Jahanian    break;
6012ba8bda05fefd3bb2f1ef201784b685f715bdde29Fariborz Jahanian  case 'H':
6013ba8bda05fefd3bb2f1ef201784b685f715bdde29Fariborz Jahanian    Type = Context.getObjCSelType();
6014ba8bda05fefd3bb2f1ef201784b685f715bdde29Fariborz Jahanian    break;
601586df27bbdbb98c39ec2184695c0561209f91beddChris Lattner  case 'a':
601686df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    Type = Context.getBuiltinVaListType();
601786df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    assert(!Type.isNull() && "builtin va list type not initialized!");
601886df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    break;
601986df27bbdbb98c39ec2184695c0561209f91beddChris Lattner  case 'A':
602086df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    // This is a "reference" to a va_list; however, what exactly
602186df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    // this means depends on how va_list is defined. There are two
602286df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    // different kinds of va_list: ones passed by value, and ones
602386df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    // passed by reference.  An example of a by-value va_list is
602486df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    // x86, where va_list is a char*. An example of by-ref va_list
602586df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    // is x86-64, where va_list is a __va_list_tag[1]. For x86,
602686df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    // we want this argument to be a char*&; for x86-64, we want
602786df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    // it to be a __va_list_tag*.
602886df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    Type = Context.getBuiltinVaListType();
602986df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    assert(!Type.isNull() && "builtin va list type not initialized!");
603014e0e7436cf6650a72052baea1f8ebe644cef489Chris Lattner    if (Type->isArrayType())
603186df27bbdbb98c39ec2184695c0561209f91beddChris Lattner      Type = Context.getArrayDecayedType(Type);
603214e0e7436cf6650a72052baea1f8ebe644cef489Chris Lattner    else
603386df27bbdbb98c39ec2184695c0561209f91beddChris Lattner      Type = Context.getLValueReferenceType(Type);
603486df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    break;
603586df27bbdbb98c39ec2184695c0561209f91beddChris Lattner  case 'V': {
603686df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    char *End;
603786df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    unsigned NumElements = strtoul(Str, &End, 10);
603886df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    assert(End != Str && "Missing vector size");
603986df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    Str = End;
60401eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
604114e0e7436cf6650a72052baea1f8ebe644cef489Chris Lattner    QualType ElementType = DecodeTypeFromStr(Str, Context, Error,
604214e0e7436cf6650a72052baea1f8ebe644cef489Chris Lattner                                             RequiresICE, false);
604314e0e7436cf6650a72052baea1f8ebe644cef489Chris Lattner    assert(!RequiresICE && "Can't require vector ICE");
604433daae66462e8f51ee153463b32bdefd60c801aaChris Lattner
604533daae66462e8f51ee153463b32bdefd60c801aaChris Lattner    // TODO: No way to make AltiVec vectors in builtins yet.
6046788b0fd67e1992f23555454efcdb16a19dfefac3Chris Lattner    Type = Context.getVectorType(ElementType, NumElements,
6047e86d78cf4754a6aef2cf9a33d847aa15338e276fBob Wilson                                 VectorType::GenericVector);
604886df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    break;
604986df27bbdbb98c39ec2184695c0561209f91beddChris Lattner  }
6050d3a23b238a2b2c0f11e6ac4951c7410a8c5717bfDouglas Gregor  case 'X': {
605114e0e7436cf6650a72052baea1f8ebe644cef489Chris Lattner    QualType ElementType = DecodeTypeFromStr(Str, Context, Error, RequiresICE,
605214e0e7436cf6650a72052baea1f8ebe644cef489Chris Lattner                                             false);
605314e0e7436cf6650a72052baea1f8ebe644cef489Chris Lattner    assert(!RequiresICE && "Can't require complex ICE");
6054d3a23b238a2b2c0f11e6ac4951c7410a8c5717bfDouglas Gregor    Type = Context.getComplexType(ElementType);
6055d3a23b238a2b2c0f11e6ac4951c7410a8c5717bfDouglas Gregor    break;
6056d3a23b238a2b2c0f11e6ac4951c7410a8c5717bfDouglas Gregor  }
60579a5a7e7351f78345a72c4956af25590f6d40ebcdChris Lattner  case 'P':
6058c29f77b769bcc5b6dc85e72c8e3cc2e348e5cf25Douglas Gregor    Type = Context.getFILEType();
6059c29f77b769bcc5b6dc85e72c8e3cc2e348e5cf25Douglas Gregor    if (Type.isNull()) {
6060f711c41dd9412a8182793259d355c4f6979ed5edMike Stump      Error = ASTContext::GE_Missing_stdio;
606186df27bbdbb98c39ec2184695c0561209f91beddChris Lattner      return QualType();
606286df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    }
6063fd612dbb23cd31c03c898ae53ff18d0dfd8488f9Mike Stump    break;
60649a5a7e7351f78345a72c4956af25590f6d40ebcdChris Lattner  case 'J':
6065f711c41dd9412a8182793259d355c4f6979ed5edMike Stump    if (Signed)
6066782fa308a765aeac2acb39c4e697c937ec21185bMike Stump      Type = Context.getsigjmp_bufType();
6067f711c41dd9412a8182793259d355c4f6979ed5edMike Stump    else
6068f711c41dd9412a8182793259d355c4f6979ed5edMike Stump      Type = Context.getjmp_bufType();
6069f711c41dd9412a8182793259d355c4f6979ed5edMike Stump
6070fd612dbb23cd31c03c898ae53ff18d0dfd8488f9Mike Stump    if (Type.isNull()) {
6071f711c41dd9412a8182793259d355c4f6979ed5edMike Stump      Error = ASTContext::GE_Missing_setjmp;
6072fd612dbb23cd31c03c898ae53ff18d0dfd8488f9Mike Stump      return QualType();
6073fd612dbb23cd31c03c898ae53ff18d0dfd8488f9Mike Stump    }
6074fd612dbb23cd31c03c898ae53ff18d0dfd8488f9Mike Stump    break;
6075782fa308a765aeac2acb39c4e697c937ec21185bMike Stump  }
60761eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
607733daae66462e8f51ee153463b32bdefd60c801aaChris Lattner  // If there are modifiers and if we're allowed to parse them, go for it.
607833daae66462e8f51ee153463b32bdefd60c801aaChris Lattner  Done = !AllowTypeModifiers;
607986df27bbdbb98c39ec2184695c0561209f91beddChris Lattner  while (!Done) {
6080187ab37a05b8f7015b9f39cc8cd9129a0c6d0b48John McCall    switch (char c = *Str++) {
608133daae66462e8f51ee153463b32bdefd60c801aaChris Lattner    default: Done = true; --Str; break;
608233daae66462e8f51ee153463b32bdefd60c801aaChris Lattner    case '*':
608333daae66462e8f51ee153463b32bdefd60c801aaChris Lattner    case '&': {
608433daae66462e8f51ee153463b32bdefd60c801aaChris Lattner      // Both pointers and references can have their pointee types
608533daae66462e8f51ee153463b32bdefd60c801aaChris Lattner      // qualified with an address space.
608633daae66462e8f51ee153463b32bdefd60c801aaChris Lattner      char *End;
608733daae66462e8f51ee153463b32bdefd60c801aaChris Lattner      unsigned AddrSpace = strtoul(Str, &End, 10);
608833daae66462e8f51ee153463b32bdefd60c801aaChris Lattner      if (End != Str && AddrSpace != 0) {
608933daae66462e8f51ee153463b32bdefd60c801aaChris Lattner        Type = Context.getAddrSpaceQualType(Type, AddrSpace);
609033daae66462e8f51ee153463b32bdefd60c801aaChris Lattner        Str = End;
609133daae66462e8f51ee153463b32bdefd60c801aaChris Lattner      }
609233daae66462e8f51ee153463b32bdefd60c801aaChris Lattner      if (c == '*')
609333daae66462e8f51ee153463b32bdefd60c801aaChris Lattner        Type = Context.getPointerType(Type);
609433daae66462e8f51ee153463b32bdefd60c801aaChris Lattner      else
609533daae66462e8f51ee153463b32bdefd60c801aaChris Lattner        Type = Context.getLValueReferenceType(Type);
609633daae66462e8f51ee153463b32bdefd60c801aaChris Lattner      break;
609733daae66462e8f51ee153463b32bdefd60c801aaChris Lattner    }
609833daae66462e8f51ee153463b32bdefd60c801aaChris Lattner    // FIXME: There's no way to have a built-in with an rvalue ref arg.
609933daae66462e8f51ee153463b32bdefd60c801aaChris Lattner    case 'C':
610033daae66462e8f51ee153463b32bdefd60c801aaChris Lattner      Type = Type.withConst();
610133daae66462e8f51ee153463b32bdefd60c801aaChris Lattner      break;
610233daae66462e8f51ee153463b32bdefd60c801aaChris Lattner    case 'D':
610333daae66462e8f51ee153463b32bdefd60c801aaChris Lattner      Type = Context.getVolatileType(Type);
610433daae66462e8f51ee153463b32bdefd60c801aaChris Lattner      break;
610586df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    }
610686df27bbdbb98c39ec2184695c0561209f91beddChris Lattner  }
6107393bd8e185692a451b2ba16bdfc9e7d3543b4217Chris Lattner
610814e0e7436cf6650a72052baea1f8ebe644cef489Chris Lattner  assert((!RequiresICE || Type->isIntegralOrEnumerationType()) &&
6109393bd8e185692a451b2ba16bdfc9e7d3543b4217Chris Lattner         "Integer constant 'I' type must be an integer");
61101eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
611186df27bbdbb98c39ec2184695c0561209f91beddChris Lattner  return Type;
611286df27bbdbb98c39ec2184695c0561209f91beddChris Lattner}
611386df27bbdbb98c39ec2184695c0561209f91beddChris Lattner
611486df27bbdbb98c39ec2184695c0561209f91beddChris Lattner/// GetBuiltinType - Return the type for the specified builtin.
611533daae66462e8f51ee153463b32bdefd60c801aaChris LattnerQualType ASTContext::GetBuiltinType(unsigned Id,
611614e0e7436cf6650a72052baea1f8ebe644cef489Chris Lattner                                    GetBuiltinTypeError &Error,
61174ba2a17694148e16eaa8d3917f657ffcd3667be4Jay Foad                                    unsigned *IntegerConstantArgs) const {
611833daae66462e8f51ee153463b32bdefd60c801aaChris Lattner  const char *TypeStr = BuiltinInfo.GetTypeString(Id);
61191eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
612086df27bbdbb98c39ec2184695c0561209f91beddChris Lattner  llvm::SmallVector<QualType, 8> ArgTypes;
61211eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
612214e0e7436cf6650a72052baea1f8ebe644cef489Chris Lattner  bool RequiresICE = false;
612386df27bbdbb98c39ec2184695c0561209f91beddChris Lattner  Error = GE_None;
612414e0e7436cf6650a72052baea1f8ebe644cef489Chris Lattner  QualType ResType = DecodeTypeFromStr(TypeStr, *this, Error,
612514e0e7436cf6650a72052baea1f8ebe644cef489Chris Lattner                                       RequiresICE, true);
612686df27bbdbb98c39ec2184695c0561209f91beddChris Lattner  if (Error != GE_None)
612786df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    return QualType();
612814e0e7436cf6650a72052baea1f8ebe644cef489Chris Lattner
612914e0e7436cf6650a72052baea1f8ebe644cef489Chris Lattner  assert(!RequiresICE && "Result of intrinsic cannot be required to be an ICE");
613014e0e7436cf6650a72052baea1f8ebe644cef489Chris Lattner
613186df27bbdbb98c39ec2184695c0561209f91beddChris Lattner  while (TypeStr[0] && TypeStr[0] != '.') {
613214e0e7436cf6650a72052baea1f8ebe644cef489Chris Lattner    QualType Ty = DecodeTypeFromStr(TypeStr, *this, Error, RequiresICE, true);
613386df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    if (Error != GE_None)
613486df27bbdbb98c39ec2184695c0561209f91beddChris Lattner      return QualType();
613586df27bbdbb98c39ec2184695c0561209f91beddChris Lattner
613614e0e7436cf6650a72052baea1f8ebe644cef489Chris Lattner    // If this argument is required to be an IntegerConstantExpression and the
613714e0e7436cf6650a72052baea1f8ebe644cef489Chris Lattner    // caller cares, fill in the bitmask we return.
613814e0e7436cf6650a72052baea1f8ebe644cef489Chris Lattner    if (RequiresICE && IntegerConstantArgs)
613914e0e7436cf6650a72052baea1f8ebe644cef489Chris Lattner      *IntegerConstantArgs |= 1 << ArgTypes.size();
614014e0e7436cf6650a72052baea1f8ebe644cef489Chris Lattner
614186df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    // Do array -> pointer decay.  The builtin should use the decayed type.
614286df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    if (Ty->isArrayType())
614386df27bbdbb98c39ec2184695c0561209f91beddChris Lattner      Ty = getArrayDecayedType(Ty);
61441eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
614586df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    ArgTypes.push_back(Ty);
614686df27bbdbb98c39ec2184695c0561209f91beddChris Lattner  }
614786df27bbdbb98c39ec2184695c0561209f91beddChris Lattner
614886df27bbdbb98c39ec2184695c0561209f91beddChris Lattner  assert((TypeStr[0] != '.' || TypeStr[1] == 0) &&
614986df27bbdbb98c39ec2184695c0561209f91beddChris Lattner         "'.' should only occur at end of builtin type list!");
615086df27bbdbb98c39ec2184695c0561209f91beddChris Lattner
615100ccbefcffeb88ea3e2e6323e594fa968753ad14John McCall  FunctionType::ExtInfo EI;
615200ccbefcffeb88ea3e2e6323e594fa968753ad14John McCall  if (BuiltinInfo.isNoReturn(Id)) EI = EI.withNoReturn(true);
615300ccbefcffeb88ea3e2e6323e594fa968753ad14John McCall
615400ccbefcffeb88ea3e2e6323e594fa968753ad14John McCall  bool Variadic = (TypeStr[0] == '.');
615500ccbefcffeb88ea3e2e6323e594fa968753ad14John McCall
615600ccbefcffeb88ea3e2e6323e594fa968753ad14John McCall  // We really shouldn't be making a no-proto type here, especially in C++.
615700ccbefcffeb88ea3e2e6323e594fa968753ad14John McCall  if (ArgTypes.empty() && Variadic)
615800ccbefcffeb88ea3e2e6323e594fa968753ad14John McCall    return getFunctionNoProtoType(ResType, EI);
6159ce056bcaa1c97b89a4b2de2112c62d060863be2bDouglas Gregor
6160e23cf437fe76b1ed02d63c3f61b456fd48a915f5John McCall  FunctionProtoType::ExtProtoInfo EPI;
616100ccbefcffeb88ea3e2e6323e594fa968753ad14John McCall  EPI.ExtInfo = EI;
616200ccbefcffeb88ea3e2e6323e594fa968753ad14John McCall  EPI.Variadic = Variadic;
6163e23cf437fe76b1ed02d63c3f61b456fd48a915f5John McCall
6164e23cf437fe76b1ed02d63c3f61b456fd48a915f5John McCall  return getFunctionType(ResType, ArgTypes.data(), ArgTypes.size(), EPI);
616586df27bbdbb98c39ec2184695c0561209f91beddChris Lattner}
6166a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman
616790e99a84ddd020e8fda79643748243725a2ed071Argyrios KyrtzidisGVALinkage ASTContext::GetGVALinkageForFunction(const FunctionDecl *FD) {
616890e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis  GVALinkage External = GVA_StrongExternal;
616990e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis
617090e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis  Linkage L = FD->getLinkage();
617190e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis  switch (L) {
617290e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis  case NoLinkage:
617390e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis  case InternalLinkage:
617490e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis  case UniqueExternalLinkage:
617590e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis    return GVA_Internal;
617690e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis
617790e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis  case ExternalLinkage:
617890e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis    switch (FD->getTemplateSpecializationKind()) {
617990e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis    case TSK_Undeclared:
618090e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis    case TSK_ExplicitSpecialization:
618190e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis      External = GVA_StrongExternal;
618290e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis      break;
618390e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis
618490e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis    case TSK_ExplicitInstantiationDefinition:
618590e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis      return GVA_ExplicitTemplateInstantiation;
618690e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis
618790e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis    case TSK_ExplicitInstantiationDeclaration:
618890e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis    case TSK_ImplicitInstantiation:
618990e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis      External = GVA_TemplateInstantiation;
619090e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis      break;
619190e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis    }
619290e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis  }
619390e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis
619490e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis  if (!FD->isInlined())
619590e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis    return External;
619690e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis
619790e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis  if (!getLangOptions().CPlusPlus || FD->hasAttr<GNUInlineAttr>()) {
619890e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis    // GNU or C99 inline semantics. Determine whether this symbol should be
619990e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis    // externally visible.
620090e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis    if (FD->isInlineDefinitionExternallyVisible())
620190e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis      return External;
620290e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis
620390e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis    // C99 inline semantics, where the symbol is not externally visible.
620490e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis    return GVA_C99Inline;
620590e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis  }
620690e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis
620790e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis  // C++0x [temp.explicit]p9:
620890e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis  //   [ Note: The intent is that an inline function that is the subject of
620990e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis  //   an explicit instantiation declaration will still be implicitly
621090e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis  //   instantiated when used so that the body can be considered for
621190e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis  //   inlining, but that no out-of-line copy of the inline function would be
621290e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis  //   generated in the translation unit. -- end note ]
621390e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis  if (FD->getTemplateSpecializationKind()
621490e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis                                       == TSK_ExplicitInstantiationDeclaration)
621590e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis    return GVA_C99Inline;
621690e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis
621790e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis  return GVA_CXXInline;
621890e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis}
621990e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis
622090e99a84ddd020e8fda79643748243725a2ed071Argyrios KyrtzidisGVALinkage ASTContext::GetGVALinkageForVariable(const VarDecl *VD) {
622190e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis  // If this is a static data member, compute the kind of template
622290e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis  // specialization. Otherwise, this variable is not part of a
622390e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis  // template.
622490e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis  TemplateSpecializationKind TSK = TSK_Undeclared;
622590e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis  if (VD->isStaticDataMember())
622690e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis    TSK = VD->getTemplateSpecializationKind();
622790e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis
622890e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis  Linkage L = VD->getLinkage();
622990e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis  if (L == ExternalLinkage && getLangOptions().CPlusPlus &&
623090e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis      VD->getType()->getLinkage() == UniqueExternalLinkage)
623190e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis    L = UniqueExternalLinkage;
623290e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis
623390e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis  switch (L) {
623490e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis  case NoLinkage:
623590e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis  case InternalLinkage:
623690e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis  case UniqueExternalLinkage:
623790e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis    return GVA_Internal;
623890e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis
623990e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis  case ExternalLinkage:
624090e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis    switch (TSK) {
624190e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis    case TSK_Undeclared:
624290e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis    case TSK_ExplicitSpecialization:
624390e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis      return GVA_StrongExternal;
624490e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis
624590e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis    case TSK_ExplicitInstantiationDeclaration:
624690e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis      llvm_unreachable("Variable should not be instantiated");
624790e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis      // Fall through to treat this like any other instantiation.
624890e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis
624990e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis    case TSK_ExplicitInstantiationDefinition:
625090e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis      return GVA_ExplicitTemplateInstantiation;
625190e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis
625290e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis    case TSK_ImplicitInstantiation:
625390e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis      return GVA_TemplateInstantiation;
625490e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis    }
625590e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis  }
625690e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis
625790e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis  return GVA_StrongExternal;
625890e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis}
625990e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis
62604ac7c0bb39696e92fd220118fedc484c09a69870Argyrios Kyrtzidisbool ASTContext::DeclMustBeEmitted(const Decl *D) {
626190e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis  if (const VarDecl *VD = dyn_cast<VarDecl>(D)) {
626290e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis    if (!VD->isFileVarDecl())
626390e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis      return false;
626490e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis  } else if (!isa<FunctionDecl>(D))
626590e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis    return false;
626690e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis
6267ab411c8c2efed8f2403bf8596e780c0f2f905a19Argyrios Kyrtzidis  // Weak references don't produce any output by themselves.
6268ab411c8c2efed8f2403bf8596e780c0f2f905a19Argyrios Kyrtzidis  if (D->hasAttr<WeakRefAttr>())
6269ab411c8c2efed8f2403bf8596e780c0f2f905a19Argyrios Kyrtzidis    return false;
6270ab411c8c2efed8f2403bf8596e780c0f2f905a19Argyrios Kyrtzidis
627190e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis  // Aliases and used decls are required.
627290e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis  if (D->hasAttr<AliasAttr>() || D->hasAttr<UsedAttr>())
627390e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis    return true;
627490e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis
627590e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis  if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
627690e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis    // Forward declarations aren't required.
627710620eb5164e31208fcbf0437cd79ae535ed0559Sean Hunt    if (!FD->doesThisDeclarationHaveABody())
627890e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis      return false;
627990e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis
628090e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis    // Constructors and destructors are required.
628190e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis    if (FD->hasAttr<ConstructorAttr>() || FD->hasAttr<DestructorAttr>())
628290e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis      return true;
628390e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis
628490e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis    // The key function for a class is required.
628590e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis    if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(FD)) {
628690e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis      const CXXRecordDecl *RD = MD->getParent();
628790e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis      if (MD->isOutOfLine() && RD->isDynamicClass()) {
628890e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis        const CXXMethodDecl *KeyFunc = getKeyFunction(RD);
628990e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis        if (KeyFunc && KeyFunc->getCanonicalDecl() == MD->getCanonicalDecl())
629090e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis          return true;
629190e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis      }
629290e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis    }
629390e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis
629490e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis    GVALinkage Linkage = GetGVALinkageForFunction(FD);
629590e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis
629690e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis    // static, static inline, always_inline, and extern inline functions can
629790e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis    // always be deferred.  Normal inline functions can be deferred in C99/C++.
629890e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis    // Implicit template instantiations can also be deferred in C++.
629990e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis    if (Linkage == GVA_Internal  || Linkage == GVA_C99Inline ||
630090e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis        Linkage == GVA_CXXInline || Linkage == GVA_TemplateInstantiation)
630190e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis      return false;
630290e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis    return true;
630390e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis  }
630490e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis
630590e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis  const VarDecl *VD = cast<VarDecl>(D);
630690e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis  assert(VD->isFileVarDecl() && "Expected file scoped var");
630790e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis
6308ab411c8c2efed8f2403bf8596e780c0f2f905a19Argyrios Kyrtzidis  if (VD->isThisDeclarationADefinition() == VarDecl::DeclarationOnly)
6309ab411c8c2efed8f2403bf8596e780c0f2f905a19Argyrios Kyrtzidis    return false;
6310ab411c8c2efed8f2403bf8596e780c0f2f905a19Argyrios Kyrtzidis
631190e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis  // Structs that have non-trivial constructors or destructors are required.
631290e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis
631390e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis  // FIXME: Handle references.
6314023df37c27ee8035664fb62f206ca58f4e2a169dSean Hunt  // FIXME: Be more selective about which constructors we care about.
631590e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis  if (const RecordType *RT = VD->getType()->getAs<RecordType>()) {
631690e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis    if (const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(RT->getDecl())) {
6317023df37c27ee8035664fb62f206ca58f4e2a169dSean Hunt      if (RD->hasDefinition() && !(RD->hasTrivialDefaultConstructor() &&
6318023df37c27ee8035664fb62f206ca58f4e2a169dSean Hunt                                   RD->hasTrivialCopyConstructor() &&
6319023df37c27ee8035664fb62f206ca58f4e2a169dSean Hunt                                   RD->hasTrivialMoveConstructor() &&
6320023df37c27ee8035664fb62f206ca58f4e2a169dSean Hunt                                   RD->hasTrivialDestructor()))
632190e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis        return true;
632290e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis    }
632390e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis  }
632490e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis
632590e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis  GVALinkage L = GetGVALinkageForVariable(VD);
632690e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis  if (L == GVA_Internal || L == GVA_TemplateInstantiation) {
632790e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis    if (!(VD->getInit() && VD->getInit()->HasSideEffects(*this)))
632890e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis      return false;
632990e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis  }
633090e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis
633190e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis  return true;
633290e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis}
6333071cc7deffad608165b1ddd5263e8bf181861520Charles Davis
6334ee743f903858e337434ac0335f147f4de4ecae05Charles DavisCallingConv ASTContext::getDefaultMethodCallConv() {
6335ee743f903858e337434ac0335f147f4de4ecae05Charles Davis  // Pass through to the C++ ABI object
6336ee743f903858e337434ac0335f147f4de4ecae05Charles Davis  return ABI->getDefaultMethodCallConv();
6337ee743f903858e337434ac0335f147f4de4ecae05Charles Davis}
6338ee743f903858e337434ac0335f147f4de4ecae05Charles Davis
63394ba2a17694148e16eaa8d3917f657ffcd3667be4Jay Foadbool ASTContext::isNearlyEmpty(const CXXRecordDecl *RD) const {
6340dae0cb52e4e3d46bbfc9a4510909522197a92e54Anders Carlsson  // Pass through to the C++ ABI object
6341dae0cb52e4e3d46bbfc9a4510909522197a92e54Anders Carlsson  return ABI->isNearlyEmpty(RD);
6342dae0cb52e4e3d46bbfc9a4510909522197a92e54Anders Carlsson}
6343dae0cb52e4e3d46bbfc9a4510909522197a92e54Anders Carlsson
634414110477887e3dc168ffc6c191e72d705051f99ePeter CollingbourneMangleContext *ASTContext::createMangleContext() {
634514110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne  switch (Target.getCXXABI()) {
634614110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne  case CXXABI_ARM:
634714110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne  case CXXABI_Itanium:
634814110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne    return createItaniumMangleContext(*this, getDiagnostics());
634914110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne  case CXXABI_Microsoft:
635014110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne    return createMicrosoftMangleContext(*this, getDiagnostics());
635114110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne  }
635214110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne  assert(0 && "Unsupported ABI");
635314110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne  return 0;
635414110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne}
635514110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne
6356071cc7deffad608165b1ddd5263e8bf181861520Charles DavisCXXABI::~CXXABI() {}
6357ba29bd25515fbd99e98ba0fedb9d93617b27609eTed Kremenek
6358ba29bd25515fbd99e98ba0fedb9d93617b27609eTed Kremeneksize_t ASTContext::getSideTableAllocatedMemory() const {
6359ba29bd25515fbd99e98ba0fedb9d93617b27609eTed Kremenek  size_t bytes = 0;
6360ba29bd25515fbd99e98ba0fedb9d93617b27609eTed Kremenek  bytes += ASTRecordLayouts.getMemorySize();
6361ba29bd25515fbd99e98ba0fedb9d93617b27609eTed Kremenek  bytes += ObjCLayouts.getMemorySize();
6362ba29bd25515fbd99e98ba0fedb9d93617b27609eTed Kremenek  bytes += KeyFunctions.getMemorySize();
6363ba29bd25515fbd99e98ba0fedb9d93617b27609eTed Kremenek  bytes += ObjCImpls.getMemorySize();
6364ba29bd25515fbd99e98ba0fedb9d93617b27609eTed Kremenek  bytes += BlockVarCopyInits.getMemorySize();
6365ba29bd25515fbd99e98ba0fedb9d93617b27609eTed Kremenek  bytes += DeclAttrs.getMemorySize();
6366ba29bd25515fbd99e98ba0fedb9d93617b27609eTed Kremenek  bytes += InstantiatedFromStaticDataMember.getMemorySize();
6367ba29bd25515fbd99e98ba0fedb9d93617b27609eTed Kremenek  bytes += InstantiatedFromUsingDecl.getMemorySize();
6368ba29bd25515fbd99e98ba0fedb9d93617b27609eTed Kremenek  bytes += InstantiatedFromUsingShadowDecl.getMemorySize();
6369ba29bd25515fbd99e98ba0fedb9d93617b27609eTed Kremenek  bytes += InstantiatedFromUnnamedFieldDecl.getMemorySize();
6370ba29bd25515fbd99e98ba0fedb9d93617b27609eTed Kremenek  return bytes;
6371ba29bd25515fbd99e98ba0fedb9d93617b27609eTed Kremenek}
6372ba29bd25515fbd99e98ba0fedb9d93617b27609eTed Kremenek
6373