ASTContext.cpp revision af0f4d0b2e38c810effc8b024ad2fb6604eec5d3
15f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//===--- ASTContext.cpp - Context to hold long-lived AST nodes ------------===//
25f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//
35f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//                     The LLVM Compiler Infrastructure
45f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//
50bc735ffcfb223c0186419547abaa5c84482663eChris Lattner// This file is distributed under the University of Illinois Open Source
60bc735ffcfb223c0186419547abaa5c84482663eChris Lattner// License. See LICENSE.TXT for details.
75f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//
85f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//===----------------------------------------------------------------------===//
95f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//
105f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//  This file implements the ASTContext interface.
115f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//
125f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//===----------------------------------------------------------------------===//
135f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
145f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer#include "clang/AST/ASTContext.h"
15bdc601b196c48d4cd56a5ceb45d41ae4e87371abKen Dyck#include "clang/AST/CharUnits.h"
1649aa7ff1245abd03e6e998e01302df31e4c6f8f6Argyrios Kyrtzidis#include "clang/AST/DeclCXX.h"
17980e508ca70d6de75d2abfd96b4681fc98bb2698Steve Naroff#include "clang/AST/DeclObjC.h"
18aaba5e346dffdbad5d1c42765a89e4a7afb0da67Douglas Gregor#include "clang/AST/DeclTemplate.h"
19b17166c8077cd900cca83a895c43b30ea6660598Argyrios Kyrtzidis#include "clang/AST/TypeLoc.h"
20e91593ef084479340582b2ba177b44be50a717b7Daniel Dunbar#include "clang/AST/Expr.h"
21ea1471e0e967548c596a71469702f8846dbaf3c0John McCall#include "clang/AST/ExprCXX.h"
222cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor#include "clang/AST/ExternalASTSource.h"
237b90340c9c7d07aef4e301e72b5e8a30d5f4f0c8Argyrios Kyrtzidis#include "clang/AST/ASTMutationListener.h"
2419cc4abea06a9b49e0e16a50d335c064cd723572Anders Carlsson#include "clang/AST/RecordLayout.h"
2514110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne#include "clang/AST/Mangle.h"
261b63e4f732dbc73d90abf886b4d21f8e3a165f6dChris Lattner#include "clang/Basic/Builtins.h"
27a9376d470ccb0eac74fe09a6b2a18a890f1d17c4Chris Lattner#include "clang/Basic/SourceManager.h"
285f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer#include "clang/Basic/TargetInfo.h"
29f5942a44880be26878592eb052b737579349411eBenjamin Kramer#include "llvm/ADT/SmallString.h"
3085f9bceab1542aafff012d4d28e998f4ba16e362Anders Carlsson#include "llvm/ADT/StringExtras.h"
316fe7c8aa8c7546743ecd0ac0138c2cf5d8155386Nate Begeman#include "llvm/Support/MathExtras.h"
32f5942a44880be26878592eb052b737579349411eBenjamin Kramer#include "llvm/Support/raw_ostream.h"
330c8cd1ac55720276ad41fa7b4f8785bead5afd32Ted Kremenek#include "llvm/Support/Capacity.h"
34071cc7deffad608165b1ddd5263e8bf181861520Charles Davis#include "CXXABI.h"
352636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis#include <map>
3629445a0542d128cd7ee587ee52229670b9b54a12Anders Carlsson
375f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencerusing namespace clang;
385f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
391827403a7138946305c0058f262e02b595cf882fDouglas Gregorunsigned ASTContext::NumImplicitDefaultConstructors;
401827403a7138946305c0058f262e02b595cf882fDouglas Gregorunsigned ASTContext::NumImplicitDefaultConstructorsDeclared;
41225843186e3972ce798d3de00f86da9008b15a0eDouglas Gregorunsigned ASTContext::NumImplicitCopyConstructors;
42225843186e3972ce798d3de00f86da9008b15a0eDouglas Gregorunsigned ASTContext::NumImplicitCopyConstructorsDeclared;
43ffe37fdda5b4b4f162a45155c30d9f60ce110c12Sean Huntunsigned ASTContext::NumImplicitMoveConstructors;
44ffe37fdda5b4b4f162a45155c30d9f60ce110c12Sean Huntunsigned ASTContext::NumImplicitMoveConstructorsDeclared;
45a376d10acfacf19d6dfa41069f7929739a18dd7aDouglas Gregorunsigned ASTContext::NumImplicitCopyAssignmentOperators;
46a376d10acfacf19d6dfa41069f7929739a18dd7aDouglas Gregorunsigned ASTContext::NumImplicitCopyAssignmentOperatorsDeclared;
47ffe37fdda5b4b4f162a45155c30d9f60ce110c12Sean Huntunsigned ASTContext::NumImplicitMoveAssignmentOperators;
48ffe37fdda5b4b4f162a45155c30d9f60ce110c12Sean Huntunsigned ASTContext::NumImplicitMoveAssignmentOperatorsDeclared;
494923aa25eb39d64106a5817c02d560a3aecf8b2cDouglas Gregorunsigned ASTContext::NumImplicitDestructors;
504923aa25eb39d64106a5817c02d560a3aecf8b2cDouglas Gregorunsigned ASTContext::NumImplicitDestructorsDeclared;
514923aa25eb39d64106a5817c02d560a3aecf8b2cDouglas Gregor
525f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencerenum FloatingRank {
535f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  FloatRank, DoubleRank, LongDoubleRank
545f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer};
555f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
563e1274f2b99cb99c03cc8e2c6517c37d330b597aDouglas Gregorvoid
573e1274f2b99cb99c03cc8e2c6517c37d330b597aDouglas GregorASTContext::CanonicalTemplateTemplateParm::Profile(llvm::FoldingSetNodeID &ID,
583e1274f2b99cb99c03cc8e2c6517c37d330b597aDouglas Gregor                                               TemplateTemplateParmDecl *Parm) {
593e1274f2b99cb99c03cc8e2c6517c37d330b597aDouglas Gregor  ID.AddInteger(Parm->getDepth());
603e1274f2b99cb99c03cc8e2c6517c37d330b597aDouglas Gregor  ID.AddInteger(Parm->getPosition());
6161c4d28e36cd3f1be392cb77f07436d1fa6b0f9fDouglas Gregor  ID.AddBoolean(Parm->isParameterPack());
623e1274f2b99cb99c03cc8e2c6517c37d330b597aDouglas Gregor
633e1274f2b99cb99c03cc8e2c6517c37d330b597aDouglas Gregor  TemplateParameterList *Params = Parm->getTemplateParameters();
643e1274f2b99cb99c03cc8e2c6517c37d330b597aDouglas Gregor  ID.AddInteger(Params->size());
653e1274f2b99cb99c03cc8e2c6517c37d330b597aDouglas Gregor  for (TemplateParameterList::const_iterator P = Params->begin(),
663e1274f2b99cb99c03cc8e2c6517c37d330b597aDouglas Gregor                                          PEnd = Params->end();
673e1274f2b99cb99c03cc8e2c6517c37d330b597aDouglas Gregor       P != PEnd; ++P) {
683e1274f2b99cb99c03cc8e2c6517c37d330b597aDouglas Gregor    if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(*P)) {
693e1274f2b99cb99c03cc8e2c6517c37d330b597aDouglas Gregor      ID.AddInteger(0);
703e1274f2b99cb99c03cc8e2c6517c37d330b597aDouglas Gregor      ID.AddBoolean(TTP->isParameterPack());
713e1274f2b99cb99c03cc8e2c6517c37d330b597aDouglas Gregor      continue;
723e1274f2b99cb99c03cc8e2c6517c37d330b597aDouglas Gregor    }
733e1274f2b99cb99c03cc8e2c6517c37d330b597aDouglas Gregor
743e1274f2b99cb99c03cc8e2c6517c37d330b597aDouglas Gregor    if (NonTypeTemplateParmDecl *NTTP = dyn_cast<NonTypeTemplateParmDecl>(*P)) {
753e1274f2b99cb99c03cc8e2c6517c37d330b597aDouglas Gregor      ID.AddInteger(1);
7661c4d28e36cd3f1be392cb77f07436d1fa6b0f9fDouglas Gregor      ID.AddBoolean(NTTP->isParameterPack());
773e1274f2b99cb99c03cc8e2c6517c37d330b597aDouglas Gregor      ID.AddPointer(NTTP->getType().getAsOpaquePtr());
786952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor      if (NTTP->isExpandedParameterPack()) {
796952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor        ID.AddBoolean(true);
806952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor        ID.AddInteger(NTTP->getNumExpansionTypes());
816952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor        for (unsigned I = 0, N = NTTP->getNumExpansionTypes(); I != N; ++I)
826952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor          ID.AddPointer(NTTP->getExpansionType(I).getAsOpaquePtr());
836952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor      } else
846952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor        ID.AddBoolean(false);
853e1274f2b99cb99c03cc8e2c6517c37d330b597aDouglas Gregor      continue;
863e1274f2b99cb99c03cc8e2c6517c37d330b597aDouglas Gregor    }
873e1274f2b99cb99c03cc8e2c6517c37d330b597aDouglas Gregor
883e1274f2b99cb99c03cc8e2c6517c37d330b597aDouglas Gregor    TemplateTemplateParmDecl *TTP = cast<TemplateTemplateParmDecl>(*P);
893e1274f2b99cb99c03cc8e2c6517c37d330b597aDouglas Gregor    ID.AddInteger(2);
903e1274f2b99cb99c03cc8e2c6517c37d330b597aDouglas Gregor    Profile(ID, TTP);
913e1274f2b99cb99c03cc8e2c6517c37d330b597aDouglas Gregor  }
923e1274f2b99cb99c03cc8e2c6517c37d330b597aDouglas Gregor}
933e1274f2b99cb99c03cc8e2c6517c37d330b597aDouglas Gregor
943e1274f2b99cb99c03cc8e2c6517c37d330b597aDouglas GregorTemplateTemplateParmDecl *
953e1274f2b99cb99c03cc8e2c6517c37d330b597aDouglas GregorASTContext::getCanonicalTemplateTemplateParmDecl(
964ba2a17694148e16eaa8d3917f657ffcd3667be4Jay Foad                                          TemplateTemplateParmDecl *TTP) const {
973e1274f2b99cb99c03cc8e2c6517c37d330b597aDouglas Gregor  // Check if we already have a canonical template template parameter.
983e1274f2b99cb99c03cc8e2c6517c37d330b597aDouglas Gregor  llvm::FoldingSetNodeID ID;
993e1274f2b99cb99c03cc8e2c6517c37d330b597aDouglas Gregor  CanonicalTemplateTemplateParm::Profile(ID, TTP);
1003e1274f2b99cb99c03cc8e2c6517c37d330b597aDouglas Gregor  void *InsertPos = 0;
1013e1274f2b99cb99c03cc8e2c6517c37d330b597aDouglas Gregor  CanonicalTemplateTemplateParm *Canonical
1023e1274f2b99cb99c03cc8e2c6517c37d330b597aDouglas Gregor    = CanonTemplateTemplateParms.FindNodeOrInsertPos(ID, InsertPos);
1033e1274f2b99cb99c03cc8e2c6517c37d330b597aDouglas Gregor  if (Canonical)
1043e1274f2b99cb99c03cc8e2c6517c37d330b597aDouglas Gregor    return Canonical->getParam();
1053e1274f2b99cb99c03cc8e2c6517c37d330b597aDouglas Gregor
1063e1274f2b99cb99c03cc8e2c6517c37d330b597aDouglas Gregor  // Build a canonical template parameter list.
1073e1274f2b99cb99c03cc8e2c6517c37d330b597aDouglas Gregor  TemplateParameterList *Params = TTP->getTemplateParameters();
1085f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner  SmallVector<NamedDecl *, 4> CanonParams;
1093e1274f2b99cb99c03cc8e2c6517c37d330b597aDouglas Gregor  CanonParams.reserve(Params->size());
1103e1274f2b99cb99c03cc8e2c6517c37d330b597aDouglas Gregor  for (TemplateParameterList::const_iterator P = Params->begin(),
1113e1274f2b99cb99c03cc8e2c6517c37d330b597aDouglas Gregor                                          PEnd = Params->end();
1123e1274f2b99cb99c03cc8e2c6517c37d330b597aDouglas Gregor       P != PEnd; ++P) {
1133e1274f2b99cb99c03cc8e2c6517c37d330b597aDouglas Gregor    if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(*P))
1143e1274f2b99cb99c03cc8e2c6517c37d330b597aDouglas Gregor      CanonParams.push_back(
1153e1274f2b99cb99c03cc8e2c6517c37d330b597aDouglas Gregor                  TemplateTypeParmDecl::Create(*this, getTranslationUnitDecl(),
116344577e6b58f42d18dc8118c8903b49a85dc005eAbramo Bagnara                                               SourceLocation(),
117344577e6b58f42d18dc8118c8903b49a85dc005eAbramo Bagnara                                               SourceLocation(),
118344577e6b58f42d18dc8118c8903b49a85dc005eAbramo Bagnara                                               TTP->getDepth(),
1193e1274f2b99cb99c03cc8e2c6517c37d330b597aDouglas Gregor                                               TTP->getIndex(), 0, false,
1203e1274f2b99cb99c03cc8e2c6517c37d330b597aDouglas Gregor                                               TTP->isParameterPack()));
1213e1274f2b99cb99c03cc8e2c6517c37d330b597aDouglas Gregor    else if (NonTypeTemplateParmDecl *NTTP
1226952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor             = dyn_cast<NonTypeTemplateParmDecl>(*P)) {
1236952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor      QualType T = getCanonicalType(NTTP->getType());
1246952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor      TypeSourceInfo *TInfo = getTrivialTypeSourceInfo(T);
1256952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor      NonTypeTemplateParmDecl *Param;
1266952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor      if (NTTP->isExpandedParameterPack()) {
1275f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner        SmallVector<QualType, 2> ExpandedTypes;
1285f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner        SmallVector<TypeSourceInfo *, 2> ExpandedTInfos;
1296952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor        for (unsigned I = 0, N = NTTP->getNumExpansionTypes(); I != N; ++I) {
1306952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor          ExpandedTypes.push_back(getCanonicalType(NTTP->getExpansionType(I)));
1316952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor          ExpandedTInfos.push_back(
1326952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor                                getTrivialTypeSourceInfo(ExpandedTypes.back()));
1336952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor        }
1346952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor
1356952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor        Param = NonTypeTemplateParmDecl::Create(*this, getTranslationUnitDecl(),
136ff676cb48fe8bf7be2feaa251dc7c5fb15af4730Abramo Bagnara                                                SourceLocation(),
137ff676cb48fe8bf7be2feaa251dc7c5fb15af4730Abramo Bagnara                                                SourceLocation(),
1386952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor                                                NTTP->getDepth(),
1396952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor                                                NTTP->getPosition(), 0,
1406952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor                                                T,
1416952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor                                                TInfo,
1426952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor                                                ExpandedTypes.data(),
1436952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor                                                ExpandedTypes.size(),
1446952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor                                                ExpandedTInfos.data());
1456952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor      } else {
1466952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor        Param = NonTypeTemplateParmDecl::Create(*this, getTranslationUnitDecl(),
147ff676cb48fe8bf7be2feaa251dc7c5fb15af4730Abramo Bagnara                                                SourceLocation(),
148ff676cb48fe8bf7be2feaa251dc7c5fb15af4730Abramo Bagnara                                                SourceLocation(),
1496952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor                                                NTTP->getDepth(),
1506952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor                                                NTTP->getPosition(), 0,
1516952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor                                                T,
1526952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor                                                NTTP->isParameterPack(),
1536952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor                                                TInfo);
1546952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor      }
1556952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor      CanonParams.push_back(Param);
1566952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor
1576952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor    } else
1583e1274f2b99cb99c03cc8e2c6517c37d330b597aDouglas Gregor      CanonParams.push_back(getCanonicalTemplateTemplateParmDecl(
1593e1274f2b99cb99c03cc8e2c6517c37d330b597aDouglas Gregor                                           cast<TemplateTemplateParmDecl>(*P)));
1603e1274f2b99cb99c03cc8e2c6517c37d330b597aDouglas Gregor  }
1613e1274f2b99cb99c03cc8e2c6517c37d330b597aDouglas Gregor
1623e1274f2b99cb99c03cc8e2c6517c37d330b597aDouglas Gregor  TemplateTemplateParmDecl *CanonTTP
1633e1274f2b99cb99c03cc8e2c6517c37d330b597aDouglas Gregor    = TemplateTemplateParmDecl::Create(*this, getTranslationUnitDecl(),
1643e1274f2b99cb99c03cc8e2c6517c37d330b597aDouglas Gregor                                       SourceLocation(), TTP->getDepth(),
16561c4d28e36cd3f1be392cb77f07436d1fa6b0f9fDouglas Gregor                                       TTP->getPosition(),
16661c4d28e36cd3f1be392cb77f07436d1fa6b0f9fDouglas Gregor                                       TTP->isParameterPack(),
16761c4d28e36cd3f1be392cb77f07436d1fa6b0f9fDouglas Gregor                                       0,
1683e1274f2b99cb99c03cc8e2c6517c37d330b597aDouglas Gregor                         TemplateParameterList::Create(*this, SourceLocation(),
1693e1274f2b99cb99c03cc8e2c6517c37d330b597aDouglas Gregor                                                       SourceLocation(),
1703e1274f2b99cb99c03cc8e2c6517c37d330b597aDouglas Gregor                                                       CanonParams.data(),
1713e1274f2b99cb99c03cc8e2c6517c37d330b597aDouglas Gregor                                                       CanonParams.size(),
1723e1274f2b99cb99c03cc8e2c6517c37d330b597aDouglas Gregor                                                       SourceLocation()));
1733e1274f2b99cb99c03cc8e2c6517c37d330b597aDouglas Gregor
1743e1274f2b99cb99c03cc8e2c6517c37d330b597aDouglas Gregor  // Get the new insert position for the node we care about.
1753e1274f2b99cb99c03cc8e2c6517c37d330b597aDouglas Gregor  Canonical = CanonTemplateTemplateParms.FindNodeOrInsertPos(ID, InsertPos);
1763e1274f2b99cb99c03cc8e2c6517c37d330b597aDouglas Gregor  assert(Canonical == 0 && "Shouldn't be in the map!");
1773e1274f2b99cb99c03cc8e2c6517c37d330b597aDouglas Gregor  (void)Canonical;
1783e1274f2b99cb99c03cc8e2c6517c37d330b597aDouglas Gregor
1793e1274f2b99cb99c03cc8e2c6517c37d330b597aDouglas Gregor  // Create the canonical template template parameter entry.
1803e1274f2b99cb99c03cc8e2c6517c37d330b597aDouglas Gregor  Canonical = new (*this) CanonicalTemplateTemplateParm(CanonTTP);
1813e1274f2b99cb99c03cc8e2c6517c37d330b597aDouglas Gregor  CanonTemplateTemplateParms.InsertNode(Canonical, InsertPos);
1823e1274f2b99cb99c03cc8e2c6517c37d330b597aDouglas Gregor  return CanonTTP;
1833e1274f2b99cb99c03cc8e2c6517c37d330b597aDouglas Gregor}
1843e1274f2b99cb99c03cc8e2c6517c37d330b597aDouglas Gregor
185071cc7deffad608165b1ddd5263e8bf181861520Charles DavisCXXABI *ASTContext::createCXXABI(const TargetInfo &T) {
186ee79a4c30e5d1c5285551c9a25b8ec6d45d46aa7John McCall  if (!LangOpts.CPlusPlus) return 0;
187ee79a4c30e5d1c5285551c9a25b8ec6d45d46aa7John McCall
18820cf717034ba1f20fc47c025ecb72ed9b631ad13Charles Davis  switch (T.getCXXABI()) {
189ee79a4c30e5d1c5285551c9a25b8ec6d45d46aa7John McCall  case CXXABI_ARM:
190ee79a4c30e5d1c5285551c9a25b8ec6d45d46aa7John McCall    return CreateARMCXXABI(*this);
191ee79a4c30e5d1c5285551c9a25b8ec6d45d46aa7John McCall  case CXXABI_Itanium:
192071cc7deffad608165b1ddd5263e8bf181861520Charles Davis    return CreateItaniumCXXABI(*this);
19320cf717034ba1f20fc47c025ecb72ed9b631ad13Charles Davis  case CXXABI_Microsoft:
19420cf717034ba1f20fc47c025ecb72ed9b631ad13Charles Davis    return CreateMicrosoftCXXABI(*this);
19520cf717034ba1f20fc47c025ecb72ed9b631ad13Charles Davis  }
196ee79a4c30e5d1c5285551c9a25b8ec6d45d46aa7John McCall  return 0;
197071cc7deffad608165b1ddd5263e8bf181861520Charles Davis}
198071cc7deffad608165b1ddd5263e8bf181861520Charles Davis
199207f4d8543529221932af82836016a2ef066c917Peter Collingbournestatic const LangAS::Map &getAddressSpaceMap(const TargetInfo &T,
200207f4d8543529221932af82836016a2ef066c917Peter Collingbourne                                             const LangOptions &LOpts) {
201207f4d8543529221932af82836016a2ef066c917Peter Collingbourne  if (LOpts.FakeAddressSpaceMap) {
202207f4d8543529221932af82836016a2ef066c917Peter Collingbourne    // The fake address space map must have a distinct entry for each
203207f4d8543529221932af82836016a2ef066c917Peter Collingbourne    // language-specific address space.
204207f4d8543529221932af82836016a2ef066c917Peter Collingbourne    static const unsigned FakeAddrSpaceMap[] = {
205207f4d8543529221932af82836016a2ef066c917Peter Collingbourne      1, // opencl_global
206207f4d8543529221932af82836016a2ef066c917Peter Collingbourne      2, // opencl_local
207207f4d8543529221932af82836016a2ef066c917Peter Collingbourne      3  // opencl_constant
208207f4d8543529221932af82836016a2ef066c917Peter Collingbourne    };
209207f4d8543529221932af82836016a2ef066c917Peter Collingbourne    return FakeAddrSpaceMap;
210207f4d8543529221932af82836016a2ef066c917Peter Collingbourne  } else {
211207f4d8543529221932af82836016a2ef066c917Peter Collingbourne    return T.getAddressSpaceMap();
212207f4d8543529221932af82836016a2ef066c917Peter Collingbourne  }
213207f4d8543529221932af82836016a2ef066c917Peter Collingbourne}
214207f4d8543529221932af82836016a2ef066c917Peter Collingbourne
21561710854be2b098428aff5316e64bd34b30fbcb7Chris LattnerASTContext::ASTContext(const LangOptions& LOpts, SourceManager &SM,
216444be7366d0a1e172c0290a1ea54c1cb16b5947cDaniel Dunbar                       const TargetInfo &t,
217e91593ef084479340582b2ba177b44be50a717b7Daniel Dunbar                       IdentifierTable &idents, SelectorTable &sels,
2181b63e4f732dbc73d90abf886b4d21f8e3a165f6dChris Lattner                       Builtin::Context &builtins,
21963fe86bee66fc145942c56b2cc564ea0b9b9ea12Douglas Gregor                       unsigned size_reserve) :
2208026f6d82f7fa544bc0453714fe94bca62a1196eSebastian Redl  FunctionProtoTypes(this_()),
221ef99001908e799c388f1363b1e607dad5f5b57d3John McCall  TemplateSpecializationTypes(this_()),
222ef99001908e799c388f1363b1e607dad5f5b57d3John McCall  DependentTemplateSpecializationTypes(this_()),
223146060435c3efce95c95a092c7a1eb651cfb9ae0John McCall  SubstTemplateTemplateParmPacks(this_()),
224772eeaefef2c883aabe35caf4543e7e32d290183Douglas Gregor  GlobalNestedNameSpecifier(0),
225772eeaefef2c883aabe35caf4543e7e32d290183Douglas Gregor  Int128Decl(0), UInt128Decl(0),
2267a27ea52b7bd635c89bec5a9c521a3bf7d204238Douglas Gregor  ObjCIdDecl(0), ObjCSelDecl(0), ObjCClassDecl(0),
22779d6726921897811232554ed94c5d77b5b7b3fc0Douglas Gregor  CFConstantStringTypeDecl(0),
2280815b579b31cb3129f732bb7ea36fd6ba6949e98Douglas Gregor  FILEDecl(0),
229f85e193739c953358c865005855253af4f68a497John McCall  jmp_bufDecl(0), sigjmp_bufDecl(0), BlockDescriptorType(0),
230f85e193739c953358c865005855253af4f68a497John McCall  BlockDescriptorExtendedType(0), cudaConfigureCallDecl(0),
231bf1a028246d884a540aeafa38e89be59a269b072John McCall  NullTypeSourceInfo(QualType()),
232207f4d8543529221932af82836016a2ef066c917Peter Collingbourne  SourceMgr(SM), LangOpts(LOpts), ABI(createCXXABI(t)),
233207f4d8543529221932af82836016a2ef066c917Peter Collingbourne  AddrSpaceMap(getAddressSpaceMap(t, LOpts)), Target(t),
2342e22253e03e175144aeb9d13350a12fd83f858beDouglas Gregor  Idents(idents), Selectors(sels),
235ac9590effa90406767a544005ed1de52e258306bTed Kremenek  BuiltinInfo(builtins),
236ac9590effa90406767a544005ed1de52e258306bTed Kremenek  DeclarationNames(*this),
2379a44b5f5593bd8a385d06b07c564795f643ec8daArgyrios Kyrtzidis  ExternalSource(0), Listener(0), PrintingPolicy(LOpts),
238f057bf78a69d37ee511430c88852a85eea19ad15Ted Kremenek  LastSDM(0, 0),
2398178df3b39ab923ff5d24538812628abee33df79John McCall  UniqueBlockByRefTypeID(0) {
2401eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  if (size_reserve > 0) Types.reserve(size_reserve);
241e91593ef084479340582b2ba177b44be50a717b7Daniel Dunbar  TUDecl = TranslationUnitDecl::Create(*this);
24214108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff  InitBuiltinTypes();
243e91593ef084479340582b2ba177b44be50a717b7Daniel Dunbar}
244e91593ef084479340582b2ba177b44be50a717b7Daniel Dunbar
2455f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid SpencerASTContext::~ASTContext() {
2463478eb6872d836600caf45b0f81c2065d685d6e0Ted Kremenek  // Release the DenseMaps associated with DeclContext objects.
2473478eb6872d836600caf45b0f81c2065d685d6e0Ted Kremenek  // FIXME: Is this the ideal solution?
2483478eb6872d836600caf45b0f81c2065d685d6e0Ted Kremenek  ReleaseDeclContextMaps();
2497d10b7eb670b821741b4c96f6cf7afbc3bb39abeDouglas Gregor
25063fe86bee66fc145942c56b2cc564ea0b9b9ea12Douglas Gregor  // Call all of the deallocation functions.
25163fe86bee66fc145942c56b2cc564ea0b9b9ea12Douglas Gregor  for (unsigned I = 0, N = Deallocations.size(); I != N; ++I)
25263fe86bee66fc145942c56b2cc564ea0b9b9ea12Douglas Gregor    Deallocations[I].first(Deallocations[I].second);
2530054531488928a424666ac11fcdc6bcc5112de52Douglas Gregor
2547d10b7eb670b821741b4c96f6cf7afbc3bb39abeDouglas Gregor  // Release all of the memory associated with overridden C++ methods.
2557d10b7eb670b821741b4c96f6cf7afbc3bb39abeDouglas Gregor  for (llvm::DenseMap<const CXXMethodDecl *, CXXMethodVector>::iterator
2567d10b7eb670b821741b4c96f6cf7afbc3bb39abeDouglas Gregor         OM = OverriddenMethods.begin(), OMEnd = OverriddenMethods.end();
2577d10b7eb670b821741b4c96f6cf7afbc3bb39abeDouglas Gregor       OM != OMEnd; ++OM)
2587d10b7eb670b821741b4c96f6cf7afbc3bb39abeDouglas Gregor    OM->second.Destroy();
2593478eb6872d836600caf45b0f81c2065d685d6e0Ted Kremenek
260dcfcfbec478f7ed96cd8d92f30c29bd4e30d5b9cTed Kremenek  // ASTRecordLayout objects in ASTRecordLayouts must always be destroyed
26163fe86bee66fc145942c56b2cc564ea0b9b9ea12Douglas Gregor  // because they can contain DenseMaps.
26263fe86bee66fc145942c56b2cc564ea0b9b9ea12Douglas Gregor  for (llvm::DenseMap<const ObjCContainerDecl*,
26363fe86bee66fc145942c56b2cc564ea0b9b9ea12Douglas Gregor       const ASTRecordLayout*>::iterator
26463fe86bee66fc145942c56b2cc564ea0b9b9ea12Douglas Gregor       I = ObjCLayouts.begin(), E = ObjCLayouts.end(); I != E; )
26563fe86bee66fc145942c56b2cc564ea0b9b9ea12Douglas Gregor    // Increment in loop to prevent using deallocated memory.
26663fe86bee66fc145942c56b2cc564ea0b9b9ea12Douglas Gregor    if (ASTRecordLayout *R = const_cast<ASTRecordLayout*>((I++)->second))
26763fe86bee66fc145942c56b2cc564ea0b9b9ea12Douglas Gregor      R->Destroy(*this);
26863fe86bee66fc145942c56b2cc564ea0b9b9ea12Douglas Gregor
269dcfcfbec478f7ed96cd8d92f30c29bd4e30d5b9cTed Kremenek  for (llvm::DenseMap<const RecordDecl*, const ASTRecordLayout*>::iterator
270dcfcfbec478f7ed96cd8d92f30c29bd4e30d5b9cTed Kremenek       I = ASTRecordLayouts.begin(), E = ASTRecordLayouts.end(); I != E; ) {
271dcfcfbec478f7ed96cd8d92f30c29bd4e30d5b9cTed Kremenek    // Increment in loop to prevent using deallocated memory.
272dcfcfbec478f7ed96cd8d92f30c29bd4e30d5b9cTed Kremenek    if (ASTRecordLayout *R = const_cast<ASTRecordLayout*>((I++)->second))
273dcfcfbec478f7ed96cd8d92f30c29bd4e30d5b9cTed Kremenek      R->Destroy(*this);
274dcfcfbec478f7ed96cd8d92f30c29bd4e30d5b9cTed Kremenek  }
2756320064d0c60fa8683f5623881c9394fd4aa7689Douglas Gregor
2766320064d0c60fa8683f5623881c9394fd4aa7689Douglas Gregor  for (llvm::DenseMap<const Decl*, AttrVec*>::iterator A = DeclAttrs.begin(),
2776320064d0c60fa8683f5623881c9394fd4aa7689Douglas Gregor                                                    AEnd = DeclAttrs.end();
2786320064d0c60fa8683f5623881c9394fd4aa7689Douglas Gregor       A != AEnd; ++A)
2796320064d0c60fa8683f5623881c9394fd4aa7689Douglas Gregor    A->second->~AttrVec();
2806320064d0c60fa8683f5623881c9394fd4aa7689Douglas Gregor}
281ab452ba8323d1985e08bade2bced588cddf2cc28Douglas Gregor
2820054531488928a424666ac11fcdc6bcc5112de52Douglas Gregorvoid ASTContext::AddDeallocation(void (*Callback)(void*), void *Data) {
2830054531488928a424666ac11fcdc6bcc5112de52Douglas Gregor  Deallocations.push_back(std::make_pair(Callback, Data));
2840054531488928a424666ac11fcdc6bcc5112de52Douglas Gregor}
2850054531488928a424666ac11fcdc6bcc5112de52Douglas Gregor
2861eb4433ac451dc16f4133a88af2d002ac26c58efMike Stumpvoid
2872cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas GregorASTContext::setExternalSource(llvm::OwningPtr<ExternalASTSource> &Source) {
2882cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor  ExternalSource.reset(Source.take());
2892cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor}
2902cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor
2915f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencervoid ASTContext::PrintStats() const {
292cd92a65edc7cbbbb7e3aee8d31008594de90fa51Chandler Carruth  llvm::errs() << "\n*** AST Context Stats:\n";
293cd92a65edc7cbbbb7e3aee8d31008594de90fa51Chandler Carruth  llvm::errs() << "  " << Types.size() << " types total.\n";
2947c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl
295dbe833da54e1b6192991b64fc453cd50b5ee7909Douglas Gregor  unsigned counts[] = {
2961eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump#define TYPE(Name, Parent) 0,
297dbe833da54e1b6192991b64fc453cd50b5ee7909Douglas Gregor#define ABSTRACT_TYPE(Name, Parent)
298dbe833da54e1b6192991b64fc453cd50b5ee7909Douglas Gregor#include "clang/AST/TypeNodes.def"
299dbe833da54e1b6192991b64fc453cd50b5ee7909Douglas Gregor    0 // Extra
300dbe833da54e1b6192991b64fc453cd50b5ee7909Douglas Gregor  };
301c2ee10d79f70036af652a395ac1f8273f3d04e12Douglas Gregor
3025f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  for (unsigned i = 0, e = Types.size(); i != e; ++i) {
3035f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    Type *T = Types[i];
304dbe833da54e1b6192991b64fc453cd50b5ee7909Douglas Gregor    counts[(unsigned)T->getTypeClass()]++;
3055f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
3065f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
307dbe833da54e1b6192991b64fc453cd50b5ee7909Douglas Gregor  unsigned Idx = 0;
308dbe833da54e1b6192991b64fc453cd50b5ee7909Douglas Gregor  unsigned TotalBytes = 0;
309dbe833da54e1b6192991b64fc453cd50b5ee7909Douglas Gregor#define TYPE(Name, Parent)                                              \
310dbe833da54e1b6192991b64fc453cd50b5ee7909Douglas Gregor  if (counts[Idx])                                                      \
311cd92a65edc7cbbbb7e3aee8d31008594de90fa51Chandler Carruth    llvm::errs() << "    " << counts[Idx] << " " << #Name               \
312cd92a65edc7cbbbb7e3aee8d31008594de90fa51Chandler Carruth                 << " types\n";                                         \
313dbe833da54e1b6192991b64fc453cd50b5ee7909Douglas Gregor  TotalBytes += counts[Idx] * sizeof(Name##Type);                       \
314dbe833da54e1b6192991b64fc453cd50b5ee7909Douglas Gregor  ++Idx;
315dbe833da54e1b6192991b64fc453cd50b5ee7909Douglas Gregor#define ABSTRACT_TYPE(Name, Parent)
316dbe833da54e1b6192991b64fc453cd50b5ee7909Douglas Gregor#include "clang/AST/TypeNodes.def"
3171eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
318cd92a65edc7cbbbb7e3aee8d31008594de90fa51Chandler Carruth  llvm::errs() << "Total bytes = " << TotalBytes << "\n";
319cd92a65edc7cbbbb7e3aee8d31008594de90fa51Chandler Carruth
3204923aa25eb39d64106a5817c02d560a3aecf8b2cDouglas Gregor  // Implicit special member functions.
321cd92a65edc7cbbbb7e3aee8d31008594de90fa51Chandler Carruth  llvm::errs() << NumImplicitDefaultConstructorsDeclared << "/"
322cd92a65edc7cbbbb7e3aee8d31008594de90fa51Chandler Carruth               << NumImplicitDefaultConstructors
323cd92a65edc7cbbbb7e3aee8d31008594de90fa51Chandler Carruth               << " implicit default constructors created\n";
324cd92a65edc7cbbbb7e3aee8d31008594de90fa51Chandler Carruth  llvm::errs() << NumImplicitCopyConstructorsDeclared << "/"
325cd92a65edc7cbbbb7e3aee8d31008594de90fa51Chandler Carruth               << NumImplicitCopyConstructors
326cd92a65edc7cbbbb7e3aee8d31008594de90fa51Chandler Carruth               << " implicit copy constructors created\n";
327ffe37fdda5b4b4f162a45155c30d9f60ce110c12Sean Hunt  if (getLangOptions().CPlusPlus)
328cd92a65edc7cbbbb7e3aee8d31008594de90fa51Chandler Carruth    llvm::errs() << NumImplicitMoveConstructorsDeclared << "/"
329cd92a65edc7cbbbb7e3aee8d31008594de90fa51Chandler Carruth                 << NumImplicitMoveConstructors
330cd92a65edc7cbbbb7e3aee8d31008594de90fa51Chandler Carruth                 << " implicit move constructors created\n";
331cd92a65edc7cbbbb7e3aee8d31008594de90fa51Chandler Carruth  llvm::errs() << NumImplicitCopyAssignmentOperatorsDeclared << "/"
332cd92a65edc7cbbbb7e3aee8d31008594de90fa51Chandler Carruth               << NumImplicitCopyAssignmentOperators
333cd92a65edc7cbbbb7e3aee8d31008594de90fa51Chandler Carruth               << " implicit copy assignment operators created\n";
334ffe37fdda5b4b4f162a45155c30d9f60ce110c12Sean Hunt  if (getLangOptions().CPlusPlus)
335cd92a65edc7cbbbb7e3aee8d31008594de90fa51Chandler Carruth    llvm::errs() << NumImplicitMoveAssignmentOperatorsDeclared << "/"
336cd92a65edc7cbbbb7e3aee8d31008594de90fa51Chandler Carruth                 << NumImplicitMoveAssignmentOperators
337cd92a65edc7cbbbb7e3aee8d31008594de90fa51Chandler Carruth                 << " implicit move assignment operators created\n";
338cd92a65edc7cbbbb7e3aee8d31008594de90fa51Chandler Carruth  llvm::errs() << NumImplicitDestructorsDeclared << "/"
339cd92a65edc7cbbbb7e3aee8d31008594de90fa51Chandler Carruth               << NumImplicitDestructors
340cd92a65edc7cbbbb7e3aee8d31008594de90fa51Chandler Carruth               << " implicit destructors created\n";
341cd92a65edc7cbbbb7e3aee8d31008594de90fa51Chandler Carruth
3422cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor  if (ExternalSource.get()) {
343cd92a65edc7cbbbb7e3aee8d31008594de90fa51Chandler Carruth    llvm::errs() << "\n";
3442cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor    ExternalSource->PrintStats();
3452cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor  }
346cd92a65edc7cbbbb7e3aee8d31008594de90fa51Chandler Carruth
34763fe86bee66fc145942c56b2cc564ea0b9b9ea12Douglas Gregor  BumpAlloc.PrintStats();
3485f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
3495f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
350772eeaefef2c883aabe35caf4543e7e32d290183Douglas GregorTypedefDecl *ASTContext::getInt128Decl() const {
351772eeaefef2c883aabe35caf4543e7e32d290183Douglas Gregor  if (!Int128Decl) {
352772eeaefef2c883aabe35caf4543e7e32d290183Douglas Gregor    TypeSourceInfo *TInfo = getTrivialTypeSourceInfo(Int128Ty);
353772eeaefef2c883aabe35caf4543e7e32d290183Douglas Gregor    Int128Decl = TypedefDecl::Create(const_cast<ASTContext &>(*this),
354772eeaefef2c883aabe35caf4543e7e32d290183Douglas Gregor                                     getTranslationUnitDecl(),
355772eeaefef2c883aabe35caf4543e7e32d290183Douglas Gregor                                     SourceLocation(),
356772eeaefef2c883aabe35caf4543e7e32d290183Douglas Gregor                                     SourceLocation(),
357772eeaefef2c883aabe35caf4543e7e32d290183Douglas Gregor                                     &Idents.get("__int128_t"),
358772eeaefef2c883aabe35caf4543e7e32d290183Douglas Gregor                                     TInfo);
359772eeaefef2c883aabe35caf4543e7e32d290183Douglas Gregor  }
360772eeaefef2c883aabe35caf4543e7e32d290183Douglas Gregor
361772eeaefef2c883aabe35caf4543e7e32d290183Douglas Gregor  return Int128Decl;
362772eeaefef2c883aabe35caf4543e7e32d290183Douglas Gregor}
363772eeaefef2c883aabe35caf4543e7e32d290183Douglas Gregor
364772eeaefef2c883aabe35caf4543e7e32d290183Douglas GregorTypedefDecl *ASTContext::getUInt128Decl() const {
365772eeaefef2c883aabe35caf4543e7e32d290183Douglas Gregor  if (!UInt128Decl) {
366772eeaefef2c883aabe35caf4543e7e32d290183Douglas Gregor    TypeSourceInfo *TInfo = getTrivialTypeSourceInfo(UnsignedInt128Ty);
367772eeaefef2c883aabe35caf4543e7e32d290183Douglas Gregor    UInt128Decl = TypedefDecl::Create(const_cast<ASTContext &>(*this),
368772eeaefef2c883aabe35caf4543e7e32d290183Douglas Gregor                                     getTranslationUnitDecl(),
369772eeaefef2c883aabe35caf4543e7e32d290183Douglas Gregor                                     SourceLocation(),
370772eeaefef2c883aabe35caf4543e7e32d290183Douglas Gregor                                     SourceLocation(),
371772eeaefef2c883aabe35caf4543e7e32d290183Douglas Gregor                                     &Idents.get("__uint128_t"),
372772eeaefef2c883aabe35caf4543e7e32d290183Douglas Gregor                                     TInfo);
373772eeaefef2c883aabe35caf4543e7e32d290183Douglas Gregor  }
374772eeaefef2c883aabe35caf4543e7e32d290183Douglas Gregor
375772eeaefef2c883aabe35caf4543e7e32d290183Douglas Gregor  return UInt128Decl;
376772eeaefef2c883aabe35caf4543e7e32d290183Douglas Gregor}
3775f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
378e27ec8ad56dbf1efb2de004b90fbbb86f740e3f1John McCallvoid ASTContext::InitBuiltinType(CanQualType &R, BuiltinType::Kind K) {
3796b304a0254a13f42390b865ff5ba668a49cc58aeJohn McCall  BuiltinType *Ty = new (*this, TypeAlignment) BuiltinType(K);
380e27ec8ad56dbf1efb2de004b90fbbb86f740e3f1John McCall  R = CanQualType::CreateUnsafe(QualType(Ty, 0));
3816b304a0254a13f42390b865ff5ba668a49cc58aeJohn McCall  Types.push_back(Ty);
3825f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
3835f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
3845f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencervoid ASTContext::InitBuiltinTypes() {
3855f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  assert(VoidTy.isNull() && "Context reinitialized?");
3861eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
3875f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // C99 6.2.5p19.
3885f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  InitBuiltinType(VoidTy,              BuiltinType::Void);
3891eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
3905f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // C99 6.2.5p2.
3915f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  InitBuiltinType(BoolTy,              BuiltinType::Bool);
3925f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // C99 6.2.5p3.
39315b91764d08e886391c865c4a444d7b51141c284Eli Friedman  if (LangOpts.CharIsSigned)
3945f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    InitBuiltinType(CharTy,            BuiltinType::Char_S);
3955f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  else
3965f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    InitBuiltinType(CharTy,            BuiltinType::Char_U);
3975f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // C99 6.2.5p4.
3985f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  InitBuiltinType(SignedCharTy,        BuiltinType::SChar);
3995f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  InitBuiltinType(ShortTy,             BuiltinType::Short);
4005f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  InitBuiltinType(IntTy,               BuiltinType::Int);
4015f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  InitBuiltinType(LongTy,              BuiltinType::Long);
4025f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  InitBuiltinType(LongLongTy,          BuiltinType::LongLong);
4031eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
4045f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // C99 6.2.5p6.
4055f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  InitBuiltinType(UnsignedCharTy,      BuiltinType::UChar);
4065f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  InitBuiltinType(UnsignedShortTy,     BuiltinType::UShort);
4075f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  InitBuiltinType(UnsignedIntTy,       BuiltinType::UInt);
4085f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  InitBuiltinType(UnsignedLongTy,      BuiltinType::ULong);
4095f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  InitBuiltinType(UnsignedLongLongTy,  BuiltinType::ULongLong);
4101eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
4115f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // C99 6.2.5p10.
4125f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  InitBuiltinType(FloatTy,             BuiltinType::Float);
4135f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  InitBuiltinType(DoubleTy,            BuiltinType::Double);
4145f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  InitBuiltinType(LongDoubleTy,        BuiltinType::LongDouble);
41564c438a4be2a871fa43c78264663ba1e9788b94dArgyrios Kyrtzidis
4162df9ced9fd1e8c7d7b38443db07e0e811de22571Chris Lattner  // GNU extension, 128-bit integers.
4172df9ced9fd1e8c7d7b38443db07e0e811de22571Chris Lattner  InitBuiltinType(Int128Ty,            BuiltinType::Int128);
4182df9ced9fd1e8c7d7b38443db07e0e811de22571Chris Lattner  InitBuiltinType(UnsignedInt128Ty,    BuiltinType::UInt128);
4192df9ced9fd1e8c7d7b38443db07e0e811de22571Chris Lattner
4203f59c975aa5d047f7edd1b900b5e885c38af0ef7Chris Lattner  if (LangOpts.CPlusPlus) { // C++ 3.9.1p5
421d3d77cd138f8e830f6547b6f83fcd5721ccf5f5dEli Friedman    if (TargetInfo::isTypeSigned(Target.getWCharType()))
4223f59c975aa5d047f7edd1b900b5e885c38af0ef7Chris Lattner      InitBuiltinType(WCharTy,           BuiltinType::WChar_S);
4233f59c975aa5d047f7edd1b900b5e885c38af0ef7Chris Lattner    else  // -fshort-wchar makes wchar_t be unsigned.
4243f59c975aa5d047f7edd1b900b5e885c38af0ef7Chris Lattner      InitBuiltinType(WCharTy,           BuiltinType::WChar_U);
4253f59c975aa5d047f7edd1b900b5e885c38af0ef7Chris Lattner  } else // C99
4263a2503227c3db04a3619735127483263c1075ef7Chris Lattner    WCharTy = getFromTargetType(Target.getWCharType());
42764c438a4be2a871fa43c78264663ba1e9788b94dArgyrios Kyrtzidis
428f5c209d23b20ada4a9b6235db50317239cbf6ae1Alisdair Meredith  if (LangOpts.CPlusPlus) // C++0x 3.9.1p5, extension for C++
429f5c209d23b20ada4a9b6235db50317239cbf6ae1Alisdair Meredith    InitBuiltinType(Char16Ty,           BuiltinType::Char16);
430f5c209d23b20ada4a9b6235db50317239cbf6ae1Alisdair Meredith  else // C99
431f5c209d23b20ada4a9b6235db50317239cbf6ae1Alisdair Meredith    Char16Ty = getFromTargetType(Target.getChar16Type());
432f5c209d23b20ada4a9b6235db50317239cbf6ae1Alisdair Meredith
433f5c209d23b20ada4a9b6235db50317239cbf6ae1Alisdair Meredith  if (LangOpts.CPlusPlus) // C++0x 3.9.1p5, extension for C++
434f5c209d23b20ada4a9b6235db50317239cbf6ae1Alisdair Meredith    InitBuiltinType(Char32Ty,           BuiltinType::Char32);
435f5c209d23b20ada4a9b6235db50317239cbf6ae1Alisdair Meredith  else // C99
436f5c209d23b20ada4a9b6235db50317239cbf6ae1Alisdair Meredith    Char32Ty = getFromTargetType(Target.getChar32Type());
437f5c209d23b20ada4a9b6235db50317239cbf6ae1Alisdair Meredith
438898574e7496ba8fd76290079d3a9d06954992734Douglas Gregor  // Placeholder type for type-dependent expressions whose type is
439898574e7496ba8fd76290079d3a9d06954992734Douglas Gregor  // completely unknown. No code should ever check a type against
440898574e7496ba8fd76290079d3a9d06954992734Douglas Gregor  // DependentTy and users should never see it; however, it is here to
441898574e7496ba8fd76290079d3a9d06954992734Douglas Gregor  // help diagnose failures to properly check for type-dependent
442898574e7496ba8fd76290079d3a9d06954992734Douglas Gregor  // expressions.
443898574e7496ba8fd76290079d3a9d06954992734Douglas Gregor  InitBuiltinType(DependentTy,         BuiltinType::Dependent);
4448e9bebdea69c590dedfbf27374114cb76fe12fbdDouglas Gregor
4452a984cad5ac3fdceeff2bd99daa7b90979313475John McCall  // Placeholder type for functions.
4462a984cad5ac3fdceeff2bd99daa7b90979313475John McCall  InitBuiltinType(OverloadTy,          BuiltinType::Overload);
4472a984cad5ac3fdceeff2bd99daa7b90979313475John McCall
448864c041e118155c2b1ce0ba36942a3da5a4a055eJohn McCall  // Placeholder type for bound members.
449864c041e118155c2b1ce0ba36942a3da5a4a055eJohn McCall  InitBuiltinType(BoundMemberTy,       BuiltinType::BoundMember);
450864c041e118155c2b1ce0ba36942a3da5a4a055eJohn McCall
4511de4d4e8cb2e9c88809fea8092bc6e835a5473d2John McCall  // "any" type; useful for debugger-like clients.
4521de4d4e8cb2e9c88809fea8092bc6e835a5473d2John McCall  InitBuiltinType(UnknownAnyTy,        BuiltinType::UnknownAny);
4531de4d4e8cb2e9c88809fea8092bc6e835a5473d2John McCall
4545f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // C99 6.2.5p11.
4555f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  FloatComplexTy      = getComplexType(FloatTy);
4565f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  DoubleComplexTy     = getComplexType(DoubleTy);
4575f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  LongDoubleComplexTy = getComplexType(LongDoubleTy);
4588e9bebdea69c590dedfbf27374114cb76fe12fbdDouglas Gregor
4597e219e47de26346885d667131977bd9ca2d7662aSteve Naroff  BuiltinVaListType = QualType();
4601eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
46113dcd00615de5c4279d97bdf63cd5f0a14fd9dccFariborz Jahanian  // Builtin types for 'id', 'Class', and 'SEL'.
462de2e22d33afec98324a66a358dfe0951b3c7259aSteve Naroff  InitBuiltinType(ObjCBuiltinIdTy, BuiltinType::ObjCId);
463de2e22d33afec98324a66a358dfe0951b3c7259aSteve Naroff  InitBuiltinType(ObjCBuiltinClassTy, BuiltinType::ObjCClass);
46413dcd00615de5c4279d97bdf63cd5f0a14fd9dccFariborz Jahanian  InitBuiltinType(ObjCBuiltinSelTy, BuiltinType::ObjCSel);
46514108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff
466a526c5c67e5a0473c340903ee542ce570119665fTed Kremenek  ObjCConstantStringType = QualType();
4671eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
46833e1d64ab5cd5d27f8530ccd056191fe2c9f3f2eFariborz Jahanian  // void * type
46933e1d64ab5cd5d27f8530ccd056191fe2c9f3f2eFariborz Jahanian  VoidPtrTy = getPointerType(VoidTy);
4706e8ed16ffef02b82995a90bdcf10ffff7d63839aSebastian Redl
4716e8ed16ffef02b82995a90bdcf10ffff7d63839aSebastian Redl  // nullptr type (C++0x 2.14.7)
4726e8ed16ffef02b82995a90bdcf10ffff7d63839aSebastian Redl  InitBuiltinType(NullPtrTy,           BuiltinType::NullPtr);
4735f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
4745f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
47578a916ec5ff5b66adec3c499e1b9af7b87668309Argyrios KyrtzidisDiagnostic &ASTContext::getDiagnostics() const {
47678a916ec5ff5b66adec3c499e1b9af7b87668309Argyrios Kyrtzidis  return SourceMgr.getDiagnostics();
47778a916ec5ff5b66adec3c499e1b9af7b87668309Argyrios Kyrtzidis}
47878a916ec5ff5b66adec3c499e1b9af7b87668309Argyrios Kyrtzidis
4796320064d0c60fa8683f5623881c9394fd4aa7689Douglas GregorAttrVec& ASTContext::getDeclAttrs(const Decl *D) {
4806320064d0c60fa8683f5623881c9394fd4aa7689Douglas Gregor  AttrVec *&Result = DeclAttrs[D];
4816320064d0c60fa8683f5623881c9394fd4aa7689Douglas Gregor  if (!Result) {
4826320064d0c60fa8683f5623881c9394fd4aa7689Douglas Gregor    void *Mem = Allocate(sizeof(AttrVec));
4836320064d0c60fa8683f5623881c9394fd4aa7689Douglas Gregor    Result = new (Mem) AttrVec;
4846320064d0c60fa8683f5623881c9394fd4aa7689Douglas Gregor  }
4856320064d0c60fa8683f5623881c9394fd4aa7689Douglas Gregor
4866320064d0c60fa8683f5623881c9394fd4aa7689Douglas Gregor  return *Result;
4876320064d0c60fa8683f5623881c9394fd4aa7689Douglas Gregor}
4886320064d0c60fa8683f5623881c9394fd4aa7689Douglas Gregor
4896320064d0c60fa8683f5623881c9394fd4aa7689Douglas Gregor/// \brief Erase the attributes corresponding to the given declaration.
4906320064d0c60fa8683f5623881c9394fd4aa7689Douglas Gregorvoid ASTContext::eraseDeclAttrs(const Decl *D) {
4916320064d0c60fa8683f5623881c9394fd4aa7689Douglas Gregor  llvm::DenseMap<const Decl*, AttrVec*>::iterator Pos = DeclAttrs.find(D);
4926320064d0c60fa8683f5623881c9394fd4aa7689Douglas Gregor  if (Pos != DeclAttrs.end()) {
4936320064d0c60fa8683f5623881c9394fd4aa7689Douglas Gregor    Pos->second->~AttrVec();
4946320064d0c60fa8683f5623881c9394fd4aa7689Douglas Gregor    DeclAttrs.erase(Pos);
4956320064d0c60fa8683f5623881c9394fd4aa7689Douglas Gregor  }
4966320064d0c60fa8683f5623881c9394fd4aa7689Douglas Gregor}
4976320064d0c60fa8683f5623881c9394fd4aa7689Douglas Gregor
498251b4ff2578e26959a4c036140ccd61c5e9292f2Douglas GregorMemberSpecializationInfo *
499663b5a0be7261c29bc4c526a71cffcfa02d4153eDouglas GregorASTContext::getInstantiatedFromStaticDataMember(const VarDecl *Var) {
5007caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  assert(Var->isStaticDataMember() && "Not a static data member");
501663b5a0be7261c29bc4c526a71cffcfa02d4153eDouglas Gregor  llvm::DenseMap<const VarDecl *, MemberSpecializationInfo *>::iterator Pos
5027caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor    = InstantiatedFromStaticDataMember.find(Var);
5037caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  if (Pos == InstantiatedFromStaticDataMember.end())
5047caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor    return 0;
5051eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
5067caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  return Pos->second;
5077caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor}
5087caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor
5091eb4433ac451dc16f4133a88af2d002ac26c58efMike Stumpvoid
510251b4ff2578e26959a4c036140ccd61c5e9292f2Douglas GregorASTContext::setInstantiatedFromStaticDataMember(VarDecl *Inst, VarDecl *Tmpl,
5119421adc43891e272156fab640e5d5ee5054b779cArgyrios Kyrtzidis                                                TemplateSpecializationKind TSK,
5129421adc43891e272156fab640e5d5ee5054b779cArgyrios Kyrtzidis                                          SourceLocation PointOfInstantiation) {
5137caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  assert(Inst->isStaticDataMember() && "Not a static data member");
5147caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  assert(Tmpl->isStaticDataMember() && "Not a static data member");
5157caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  assert(!InstantiatedFromStaticDataMember[Inst] &&
5167caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor         "Already noted what static data member was instantiated from");
517251b4ff2578e26959a4c036140ccd61c5e9292f2Douglas Gregor  InstantiatedFromStaticDataMember[Inst]
5189421adc43891e272156fab640e5d5ee5054b779cArgyrios Kyrtzidis    = new (*this) MemberSpecializationInfo(Tmpl, TSK, PointOfInstantiation);
5197caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor}
5207caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor
521af0f4d0b2e38c810effc8b024ad2fb6604eec5d3Francois PichetFunctionDecl *ASTContext::getClassScopeSpecializationPattern(
522af0f4d0b2e38c810effc8b024ad2fb6604eec5d3Francois Pichet                                                     const FunctionDecl *FD){
523af0f4d0b2e38c810effc8b024ad2fb6604eec5d3Francois Pichet  assert(FD && "Specialization is 0");
524af0f4d0b2e38c810effc8b024ad2fb6604eec5d3Francois Pichet  llvm::DenseMap<const FunctionDecl*, FunctionDecl *>::const_iterator Pos
525af0f4d0b2e38c810effc8b024ad2fb6604eec5d3Francois Pichet    = ClassScopeSpecilizationPattern.find(FD);
526af0f4d0b2e38c810effc8b024ad2fb6604eec5d3Francois Pichet  if (Pos == ClassScopeSpecilizationPattern.end())
527af0f4d0b2e38c810effc8b024ad2fb6604eec5d3Francois Pichet    return 0;
528af0f4d0b2e38c810effc8b024ad2fb6604eec5d3Francois Pichet
529af0f4d0b2e38c810effc8b024ad2fb6604eec5d3Francois Pichet  return Pos->second;
530af0f4d0b2e38c810effc8b024ad2fb6604eec5d3Francois Pichet}
531af0f4d0b2e38c810effc8b024ad2fb6604eec5d3Francois Pichet
532af0f4d0b2e38c810effc8b024ad2fb6604eec5d3Francois Pichetvoid ASTContext::setClassScopeSpecializationPattern(FunctionDecl *FD,
533af0f4d0b2e38c810effc8b024ad2fb6604eec5d3Francois Pichet                                        FunctionDecl *Pattern) {
534af0f4d0b2e38c810effc8b024ad2fb6604eec5d3Francois Pichet  assert(FD && "Specialization is 0");
535af0f4d0b2e38c810effc8b024ad2fb6604eec5d3Francois Pichet  assert(Pattern && "Class scope specialization pattern is 0");
536af0f4d0b2e38c810effc8b024ad2fb6604eec5d3Francois Pichet  ClassScopeSpecilizationPattern[FD] = Pattern;
537af0f4d0b2e38c810effc8b024ad2fb6604eec5d3Francois Pichet}
538af0f4d0b2e38c810effc8b024ad2fb6604eec5d3Francois Pichet
5397ba107a1863ddfa1664555854f0d7bdb3c491c92John McCallNamedDecl *
540ed97649e9574b9d854fa4d6109c9333ae0993554John McCallASTContext::getInstantiatedFromUsingDecl(UsingDecl *UUD) {
5417ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall  llvm::DenseMap<UsingDecl *, NamedDecl *>::const_iterator Pos
542ed97649e9574b9d854fa4d6109c9333ae0993554John McCall    = InstantiatedFromUsingDecl.find(UUD);
543ed97649e9574b9d854fa4d6109c9333ae0993554John McCall  if (Pos == InstantiatedFromUsingDecl.end())
5440d8df780aef1acda5962347a32591efc629b6748Anders Carlsson    return 0;
5451eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
5460d8df780aef1acda5962347a32591efc629b6748Anders Carlsson  return Pos->second;
5470d8df780aef1acda5962347a32591efc629b6748Anders Carlsson}
5480d8df780aef1acda5962347a32591efc629b6748Anders Carlsson
5490d8df780aef1acda5962347a32591efc629b6748Anders Carlssonvoid
550ed97649e9574b9d854fa4d6109c9333ae0993554John McCallASTContext::setInstantiatedFromUsingDecl(UsingDecl *Inst, NamedDecl *Pattern) {
551ed97649e9574b9d854fa4d6109c9333ae0993554John McCall  assert((isa<UsingDecl>(Pattern) ||
552ed97649e9574b9d854fa4d6109c9333ae0993554John McCall          isa<UnresolvedUsingValueDecl>(Pattern) ||
553ed97649e9574b9d854fa4d6109c9333ae0993554John McCall          isa<UnresolvedUsingTypenameDecl>(Pattern)) &&
554ed97649e9574b9d854fa4d6109c9333ae0993554John McCall         "pattern decl is not a using decl");
555ed97649e9574b9d854fa4d6109c9333ae0993554John McCall  assert(!InstantiatedFromUsingDecl[Inst] && "pattern already exists");
556ed97649e9574b9d854fa4d6109c9333ae0993554John McCall  InstantiatedFromUsingDecl[Inst] = Pattern;
557ed97649e9574b9d854fa4d6109c9333ae0993554John McCall}
558ed97649e9574b9d854fa4d6109c9333ae0993554John McCall
559ed97649e9574b9d854fa4d6109c9333ae0993554John McCallUsingShadowDecl *
560ed97649e9574b9d854fa4d6109c9333ae0993554John McCallASTContext::getInstantiatedFromUsingShadowDecl(UsingShadowDecl *Inst) {
561ed97649e9574b9d854fa4d6109c9333ae0993554John McCall  llvm::DenseMap<UsingShadowDecl*, UsingShadowDecl*>::const_iterator Pos
562ed97649e9574b9d854fa4d6109c9333ae0993554John McCall    = InstantiatedFromUsingShadowDecl.find(Inst);
563ed97649e9574b9d854fa4d6109c9333ae0993554John McCall  if (Pos == InstantiatedFromUsingShadowDecl.end())
564ed97649e9574b9d854fa4d6109c9333ae0993554John McCall    return 0;
565ed97649e9574b9d854fa4d6109c9333ae0993554John McCall
566ed97649e9574b9d854fa4d6109c9333ae0993554John McCall  return Pos->second;
567ed97649e9574b9d854fa4d6109c9333ae0993554John McCall}
568ed97649e9574b9d854fa4d6109c9333ae0993554John McCall
569ed97649e9574b9d854fa4d6109c9333ae0993554John McCallvoid
570ed97649e9574b9d854fa4d6109c9333ae0993554John McCallASTContext::setInstantiatedFromUsingShadowDecl(UsingShadowDecl *Inst,
571ed97649e9574b9d854fa4d6109c9333ae0993554John McCall                                               UsingShadowDecl *Pattern) {
572ed97649e9574b9d854fa4d6109c9333ae0993554John McCall  assert(!InstantiatedFromUsingShadowDecl[Inst] && "pattern already exists");
573ed97649e9574b9d854fa4d6109c9333ae0993554John McCall  InstantiatedFromUsingShadowDecl[Inst] = Pattern;
5740d8df780aef1acda5962347a32591efc629b6748Anders Carlsson}
5750d8df780aef1acda5962347a32591efc629b6748Anders Carlsson
576d8b285fee4471f393da8ee30f552ceacdc362afaAnders CarlssonFieldDecl *ASTContext::getInstantiatedFromUnnamedFieldDecl(FieldDecl *Field) {
577d8b285fee4471f393da8ee30f552ceacdc362afaAnders Carlsson  llvm::DenseMap<FieldDecl *, FieldDecl *>::iterator Pos
578d8b285fee4471f393da8ee30f552ceacdc362afaAnders Carlsson    = InstantiatedFromUnnamedFieldDecl.find(Field);
579d8b285fee4471f393da8ee30f552ceacdc362afaAnders Carlsson  if (Pos == InstantiatedFromUnnamedFieldDecl.end())
580d8b285fee4471f393da8ee30f552ceacdc362afaAnders Carlsson    return 0;
5811eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
582d8b285fee4471f393da8ee30f552ceacdc362afaAnders Carlsson  return Pos->second;
583d8b285fee4471f393da8ee30f552ceacdc362afaAnders Carlsson}
584d8b285fee4471f393da8ee30f552ceacdc362afaAnders Carlsson
585d8b285fee4471f393da8ee30f552ceacdc362afaAnders Carlssonvoid ASTContext::setInstantiatedFromUnnamedFieldDecl(FieldDecl *Inst,
586d8b285fee4471f393da8ee30f552ceacdc362afaAnders Carlsson                                                     FieldDecl *Tmpl) {
587d8b285fee4471f393da8ee30f552ceacdc362afaAnders Carlsson  assert(!Inst->getDeclName() && "Instantiated field decl is not unnamed");
588d8b285fee4471f393da8ee30f552ceacdc362afaAnders Carlsson  assert(!Tmpl->getDeclName() && "Template field decl is not unnamed");
589d8b285fee4471f393da8ee30f552ceacdc362afaAnders Carlsson  assert(!InstantiatedFromUnnamedFieldDecl[Inst] &&
590d8b285fee4471f393da8ee30f552ceacdc362afaAnders Carlsson         "Already noted what unnamed field was instantiated from");
5911eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
592d8b285fee4471f393da8ee30f552ceacdc362afaAnders Carlsson  InstantiatedFromUnnamedFieldDecl[Inst] = Tmpl;
593d8b285fee4471f393da8ee30f552ceacdc362afaAnders Carlsson}
594d8b285fee4471f393da8ee30f552ceacdc362afaAnders Carlsson
59514d56ef43ff4921c6749f7340212fbb743fdbb9bFariborz Jahanianbool ASTContext::ZeroBitfieldFollowsNonBitfield(const FieldDecl *FD,
59614d56ef43ff4921c6749f7340212fbb743fdbb9bFariborz Jahanian                                    const FieldDecl *LastFD) const {
59714d56ef43ff4921c6749f7340212fbb743fdbb9bFariborz Jahanian  return (FD->isBitField() && LastFD && !LastFD->isBitField() &&
598682b7f8506b70024d3ae88b53a07332bd2cca55eChad Rosier          FD->getBitWidth()->EvaluateAsInt(*this).getZExtValue() == 0);
59914d56ef43ff4921c6749f7340212fbb743fdbb9bFariborz Jahanian}
60014d56ef43ff4921c6749f7340212fbb743fdbb9bFariborz Jahanian
601340fa242130c2d8d74c83edca0952e771aebe0e6Fariborz Jahanianbool ASTContext::ZeroBitfieldFollowsBitfield(const FieldDecl *FD,
602340fa242130c2d8d74c83edca0952e771aebe0e6Fariborz Jahanian                                             const FieldDecl *LastFD) const {
603340fa242130c2d8d74c83edca0952e771aebe0e6Fariborz Jahanian  return (FD->isBitField() && LastFD && LastFD->isBitField() &&
604682b7f8506b70024d3ae88b53a07332bd2cca55eChad Rosier          FD->getBitWidth()->EvaluateAsInt(*this).getZExtValue() == 0 &&
605682b7f8506b70024d3ae88b53a07332bd2cca55eChad Rosier          LastFD->getBitWidth()->EvaluateAsInt(*this).getZExtValue() != 0);
606340fa242130c2d8d74c83edca0952e771aebe0e6Fariborz Jahanian}
607340fa242130c2d8d74c83edca0952e771aebe0e6Fariborz Jahanian
6089b3acaa32548d0ce78b9c39a3911397f6738a47cFariborz Jahanianbool ASTContext::BitfieldFollowsBitfield(const FieldDecl *FD,
6099b3acaa32548d0ce78b9c39a3911397f6738a47cFariborz Jahanian                                         const FieldDecl *LastFD) const {
6109b3acaa32548d0ce78b9c39a3911397f6738a47cFariborz Jahanian  return (FD->isBitField() && LastFD && LastFD->isBitField() &&
611682b7f8506b70024d3ae88b53a07332bd2cca55eChad Rosier          FD->getBitWidth()->EvaluateAsInt(*this).getZExtValue() &&
612682b7f8506b70024d3ae88b53a07332bd2cca55eChad Rosier          LastFD->getBitWidth()->EvaluateAsInt(*this).getZExtValue());
6139b3acaa32548d0ce78b9c39a3911397f6738a47cFariborz Jahanian}
6149b3acaa32548d0ce78b9c39a3911397f6738a47cFariborz Jahanian
615dd7fddb5b6883326e52b278a9b7e9cefea29aae0Chad Rosierbool ASTContext::NonBitfieldFollowsBitfield(const FieldDecl *FD,
61652bbe7a1133c3cb57e9246f1b96c12940ea3821aFariborz Jahanian                                         const FieldDecl *LastFD) const {
61752bbe7a1133c3cb57e9246f1b96c12940ea3821aFariborz Jahanian  return (!FD->isBitField() && LastFD && LastFD->isBitField() &&
618682b7f8506b70024d3ae88b53a07332bd2cca55eChad Rosier          LastFD->getBitWidth()->EvaluateAsInt(*this).getZExtValue());
61952bbe7a1133c3cb57e9246f1b96c12940ea3821aFariborz Jahanian}
62052bbe7a1133c3cb57e9246f1b96c12940ea3821aFariborz Jahanian
621dd7fddb5b6883326e52b278a9b7e9cefea29aae0Chad Rosierbool ASTContext::BitfieldFollowsNonBitfield(const FieldDecl *FD,
62252bbe7a1133c3cb57e9246f1b96c12940ea3821aFariborz Jahanian                                             const FieldDecl *LastFD) const {
62352bbe7a1133c3cb57e9246f1b96c12940ea3821aFariborz Jahanian  return (FD->isBitField() && LastFD && !LastFD->isBitField() &&
624682b7f8506b70024d3ae88b53a07332bd2cca55eChad Rosier          FD->getBitWidth()->EvaluateAsInt(*this).getZExtValue());
62552bbe7a1133c3cb57e9246f1b96c12940ea3821aFariborz Jahanian}
62652bbe7a1133c3cb57e9246f1b96c12940ea3821aFariborz Jahanian
6277d10b7eb670b821741b4c96f6cf7afbc3bb39abeDouglas GregorASTContext::overridden_cxx_method_iterator
6287d10b7eb670b821741b4c96f6cf7afbc3bb39abeDouglas GregorASTContext::overridden_methods_begin(const CXXMethodDecl *Method) const {
6297d10b7eb670b821741b4c96f6cf7afbc3bb39abeDouglas Gregor  llvm::DenseMap<const CXXMethodDecl *, CXXMethodVector>::const_iterator Pos
6307d10b7eb670b821741b4c96f6cf7afbc3bb39abeDouglas Gregor    = OverriddenMethods.find(Method);
6317d10b7eb670b821741b4c96f6cf7afbc3bb39abeDouglas Gregor  if (Pos == OverriddenMethods.end())
6327d10b7eb670b821741b4c96f6cf7afbc3bb39abeDouglas Gregor    return 0;
6337d10b7eb670b821741b4c96f6cf7afbc3bb39abeDouglas Gregor
6347d10b7eb670b821741b4c96f6cf7afbc3bb39abeDouglas Gregor  return Pos->second.begin();
6357d10b7eb670b821741b4c96f6cf7afbc3bb39abeDouglas Gregor}
6367d10b7eb670b821741b4c96f6cf7afbc3bb39abeDouglas Gregor
6377d10b7eb670b821741b4c96f6cf7afbc3bb39abeDouglas GregorASTContext::overridden_cxx_method_iterator
6387d10b7eb670b821741b4c96f6cf7afbc3bb39abeDouglas GregorASTContext::overridden_methods_end(const CXXMethodDecl *Method) const {
6397d10b7eb670b821741b4c96f6cf7afbc3bb39abeDouglas Gregor  llvm::DenseMap<const CXXMethodDecl *, CXXMethodVector>::const_iterator Pos
6407d10b7eb670b821741b4c96f6cf7afbc3bb39abeDouglas Gregor    = OverriddenMethods.find(Method);
6417d10b7eb670b821741b4c96f6cf7afbc3bb39abeDouglas Gregor  if (Pos == OverriddenMethods.end())
6427d10b7eb670b821741b4c96f6cf7afbc3bb39abeDouglas Gregor    return 0;
6437d10b7eb670b821741b4c96f6cf7afbc3bb39abeDouglas Gregor
6447d10b7eb670b821741b4c96f6cf7afbc3bb39abeDouglas Gregor  return Pos->second.end();
6457d10b7eb670b821741b4c96f6cf7afbc3bb39abeDouglas Gregor}
6467d10b7eb670b821741b4c96f6cf7afbc3bb39abeDouglas Gregor
647c91e9f439ae85d5f79a6b65672f1d7d1b55ccda0Argyrios Kyrtzidisunsigned
648c91e9f439ae85d5f79a6b65672f1d7d1b55ccda0Argyrios KyrtzidisASTContext::overridden_methods_size(const CXXMethodDecl *Method) const {
649c91e9f439ae85d5f79a6b65672f1d7d1b55ccda0Argyrios Kyrtzidis  llvm::DenseMap<const CXXMethodDecl *, CXXMethodVector>::const_iterator Pos
650c91e9f439ae85d5f79a6b65672f1d7d1b55ccda0Argyrios Kyrtzidis    = OverriddenMethods.find(Method);
651c91e9f439ae85d5f79a6b65672f1d7d1b55ccda0Argyrios Kyrtzidis  if (Pos == OverriddenMethods.end())
652c91e9f439ae85d5f79a6b65672f1d7d1b55ccda0Argyrios Kyrtzidis    return 0;
653c91e9f439ae85d5f79a6b65672f1d7d1b55ccda0Argyrios Kyrtzidis
654c91e9f439ae85d5f79a6b65672f1d7d1b55ccda0Argyrios Kyrtzidis  return Pos->second.size();
655c91e9f439ae85d5f79a6b65672f1d7d1b55ccda0Argyrios Kyrtzidis}
656c91e9f439ae85d5f79a6b65672f1d7d1b55ccda0Argyrios Kyrtzidis
6577d10b7eb670b821741b4c96f6cf7afbc3bb39abeDouglas Gregorvoid ASTContext::addOverriddenMethod(const CXXMethodDecl *Method,
6587d10b7eb670b821741b4c96f6cf7afbc3bb39abeDouglas Gregor                                     const CXXMethodDecl *Overridden) {
6597d10b7eb670b821741b4c96f6cf7afbc3bb39abeDouglas Gregor  OverriddenMethods[Method].push_back(Overridden);
6607d10b7eb670b821741b4c96f6cf7afbc3bb39abeDouglas Gregor}
6617d10b7eb670b821741b4c96f6cf7afbc3bb39abeDouglas Gregor
662464175bba1318bef7905122e9fda20cff926df78Chris Lattner//===----------------------------------------------------------------------===//
663464175bba1318bef7905122e9fda20cff926df78Chris Lattner//                         Type Sizing and Analysis
664464175bba1318bef7905122e9fda20cff926df78Chris Lattner//===----------------------------------------------------------------------===//
665a7674d8a9a69f3f6fe16e70cf2a3b2b15fb7c43dChris Lattner
666b7cfe88e88cb4f46308de89cf3f0c81bfe624128Chris Lattner/// getFloatTypeSemantics - Return the APFloat 'semantics' for the specified
667b7cfe88e88cb4f46308de89cf3f0c81bfe624128Chris Lattner/// scalar floating point type.
668b7cfe88e88cb4f46308de89cf3f0c81bfe624128Chris Lattnerconst llvm::fltSemantics &ASTContext::getFloatTypeSemantics(QualType T) const {
669183700f494ec9b6701b6efe82bcb25f4c79ba561John McCall  const BuiltinType *BT = T->getAs<BuiltinType>();
670b7cfe88e88cb4f46308de89cf3f0c81bfe624128Chris Lattner  assert(BT && "Not a floating point type!");
671b7cfe88e88cb4f46308de89cf3f0c81bfe624128Chris Lattner  switch (BT->getKind()) {
672b7cfe88e88cb4f46308de89cf3f0c81bfe624128Chris Lattner  default: assert(0 && "Not a floating point type!");
673b7cfe88e88cb4f46308de89cf3f0c81bfe624128Chris Lattner  case BuiltinType::Float:      return Target.getFloatFormat();
674b7cfe88e88cb4f46308de89cf3f0c81bfe624128Chris Lattner  case BuiltinType::Double:     return Target.getDoubleFormat();
675b7cfe88e88cb4f46308de89cf3f0c81bfe624128Chris Lattner  case BuiltinType::LongDouble: return Target.getLongDoubleFormat();
676b7cfe88e88cb4f46308de89cf3f0c81bfe624128Chris Lattner  }
677b7cfe88e88cb4f46308de89cf3f0c81bfe624128Chris Lattner}
678b7cfe88e88cb4f46308de89cf3f0c81bfe624128Chris Lattner
6798b752f10c394b140f9ef89e049cbad1a7676fc25Ken Dyck/// getDeclAlign - Return a conservative estimate of the alignment of the
680af707ab8fbb9451e8febb8d766f6c043628125c4Chris Lattner/// specified decl.  Note that bitfields do not have a valid alignment, so
681af707ab8fbb9451e8febb8d766f6c043628125c4Chris Lattner/// this method will assert on them.
6825d484e8cf710207010720589d89602233de61d01Sebastian Redl/// If @p RefAsPointee, references are treated like their underlying type
6835d484e8cf710207010720589d89602233de61d01Sebastian Redl/// (for alignof), else they're treated like pointers (for CodeGen).
6844ba2a17694148e16eaa8d3917f657ffcd3667be4Jay FoadCharUnits ASTContext::getDeclAlign(const Decl *D, bool RefAsPointee) const {
685dcdafb6a701aa9d81edcb088915f58933315dc05Eli Friedman  unsigned Align = Target.getCharWidth();
686dcdafb6a701aa9d81edcb088915f58933315dc05Eli Friedman
6874081a5c5f1381c4ec77f8ab3866693917e4329c4John McCall  bool UseAlignAttrOnly = false;
6884081a5c5f1381c4ec77f8ab3866693917e4329c4John McCall  if (unsigned AlignFromAttr = D->getMaxAlignment()) {
6894081a5c5f1381c4ec77f8ab3866693917e4329c4John McCall    Align = AlignFromAttr;
6904081a5c5f1381c4ec77f8ab3866693917e4329c4John McCall
6914081a5c5f1381c4ec77f8ab3866693917e4329c4John McCall    // __attribute__((aligned)) can increase or decrease alignment
6924081a5c5f1381c4ec77f8ab3866693917e4329c4John McCall    // *except* on a struct or struct member, where it only increases
6934081a5c5f1381c4ec77f8ab3866693917e4329c4John McCall    // alignment unless 'packed' is also specified.
6944081a5c5f1381c4ec77f8ab3866693917e4329c4John McCall    //
6954081a5c5f1381c4ec77f8ab3866693917e4329c4John McCall    // It is an error for [[align]] to decrease alignment, so we can
6964081a5c5f1381c4ec77f8ab3866693917e4329c4John McCall    // ignore that possibility;  Sema should diagnose it.
6974081a5c5f1381c4ec77f8ab3866693917e4329c4John McCall    if (isa<FieldDecl>(D)) {
6984081a5c5f1381c4ec77f8ab3866693917e4329c4John McCall      UseAlignAttrOnly = D->hasAttr<PackedAttr>() ||
6994081a5c5f1381c4ec77f8ab3866693917e4329c4John McCall        cast<FieldDecl>(D)->getParent()->hasAttr<PackedAttr>();
7004081a5c5f1381c4ec77f8ab3866693917e4329c4John McCall    } else {
7014081a5c5f1381c4ec77f8ab3866693917e4329c4John McCall      UseAlignAttrOnly = true;
7024081a5c5f1381c4ec77f8ab3866693917e4329c4John McCall    }
7034081a5c5f1381c4ec77f8ab3866693917e4329c4John McCall  }
70478a7d7d79964119a3f35b262eb154b5cbf1001edFariborz Jahanian  else if (isa<FieldDecl>(D))
70578a7d7d79964119a3f35b262eb154b5cbf1001edFariborz Jahanian      UseAlignAttrOnly =
70678a7d7d79964119a3f35b262eb154b5cbf1001edFariborz Jahanian        D->hasAttr<PackedAttr>() ||
70778a7d7d79964119a3f35b262eb154b5cbf1001edFariborz Jahanian        cast<FieldDecl>(D)->getParent()->hasAttr<PackedAttr>();
708dcdafb6a701aa9d81edcb088915f58933315dc05Eli Friedman
709ba4f5d5754c8291690d01ca9581926673d69b24cJohn McCall  // If we're using the align attribute only, just ignore everything
710ba4f5d5754c8291690d01ca9581926673d69b24cJohn McCall  // else about the declaration and its type.
7114081a5c5f1381c4ec77f8ab3866693917e4329c4John McCall  if (UseAlignAttrOnly) {
712ba4f5d5754c8291690d01ca9581926673d69b24cJohn McCall    // do nothing
713ba4f5d5754c8291690d01ca9581926673d69b24cJohn McCall
7144081a5c5f1381c4ec77f8ab3866693917e4329c4John McCall  } else if (const ValueDecl *VD = dyn_cast<ValueDecl>(D)) {
715af707ab8fbb9451e8febb8d766f6c043628125c4Chris Lattner    QualType T = VD->getType();
7166217b80b7a1379b74cced1c076338262c3c980b3Ted Kremenek    if (const ReferenceType* RT = T->getAs<ReferenceType>()) {
7175d484e8cf710207010720589d89602233de61d01Sebastian Redl      if (RefAsPointee)
7185d484e8cf710207010720589d89602233de61d01Sebastian Redl        T = RT->getPointeeType();
7195d484e8cf710207010720589d89602233de61d01Sebastian Redl      else
7205d484e8cf710207010720589d89602233de61d01Sebastian Redl        T = getPointerType(RT->getPointeeType());
7215d484e8cf710207010720589d89602233de61d01Sebastian Redl    }
7225d484e8cf710207010720589d89602233de61d01Sebastian Redl    if (!T->isIncompleteType() && !T->isFunctionType()) {
7233b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall      // Adjust alignments of declarations with array type by the
7243b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall      // large-array alignment on the target.
7256deecb0d46bcfd048e651d2db7c4fb0d6407da96Rafael Espindola      unsigned MinWidth = Target.getLargeArrayMinWidth();
7263b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall      const ArrayType *arrayType;
7273b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall      if (MinWidth && (arrayType = getAsArrayType(T))) {
7283b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall        if (isa<VariableArrayType>(arrayType))
7293b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall          Align = std::max(Align, Target.getLargeArrayAlign());
7303b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall        else if (isa<ConstantArrayType>(arrayType) &&
7313b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall                 MinWidth <= getTypeSize(cast<ConstantArrayType>(arrayType)))
7323b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall          Align = std::max(Align, Target.getLargeArrayAlign());
7333b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall
7343b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall        // Walk through any array types while we're at it.
7353b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall        T = getBaseElementType(arrayType);
7366deecb0d46bcfd048e651d2db7c4fb0d6407da96Rafael Espindola      }
7379f1210c3280104417a4ad30f0a00825ac8fa718aChad Rosier      Align = std::max(Align, getPreferredTypeAlign(T.getTypePtr()));
738dcdafb6a701aa9d81edcb088915f58933315dc05Eli Friedman    }
739ba4f5d5754c8291690d01ca9581926673d69b24cJohn McCall
740ba4f5d5754c8291690d01ca9581926673d69b24cJohn McCall    // Fields can be subject to extra alignment constraints, like if
741ba4f5d5754c8291690d01ca9581926673d69b24cJohn McCall    // the field is packed, the struct is packed, or the struct has a
742ba4f5d5754c8291690d01ca9581926673d69b24cJohn McCall    // a max-field-alignment constraint (#pragma pack).  So calculate
743ba4f5d5754c8291690d01ca9581926673d69b24cJohn McCall    // the actual alignment of the field within the struct, and then
744ba4f5d5754c8291690d01ca9581926673d69b24cJohn McCall    // (as we're expected to) constrain that by the alignment of the type.
745ba4f5d5754c8291690d01ca9581926673d69b24cJohn McCall    if (const FieldDecl *field = dyn_cast<FieldDecl>(VD)) {
746ba4f5d5754c8291690d01ca9581926673d69b24cJohn McCall      // So calculate the alignment of the field.
747ba4f5d5754c8291690d01ca9581926673d69b24cJohn McCall      const ASTRecordLayout &layout = getASTRecordLayout(field->getParent());
748ba4f5d5754c8291690d01ca9581926673d69b24cJohn McCall
749ba4f5d5754c8291690d01ca9581926673d69b24cJohn McCall      // Start with the record's overall alignment.
750dac54c124e302d6f028ea5723c425b7f66fc7c71Ken Dyck      unsigned fieldAlign = toBits(layout.getAlignment());
751ba4f5d5754c8291690d01ca9581926673d69b24cJohn McCall
752ba4f5d5754c8291690d01ca9581926673d69b24cJohn McCall      // Use the GCD of that and the offset within the record.
753ba4f5d5754c8291690d01ca9581926673d69b24cJohn McCall      uint64_t offset = layout.getFieldOffset(field->getFieldIndex());
754ba4f5d5754c8291690d01ca9581926673d69b24cJohn McCall      if (offset > 0) {
755ba4f5d5754c8291690d01ca9581926673d69b24cJohn McCall        // Alignment is always a power of 2, so the GCD will be a power of 2,
756ba4f5d5754c8291690d01ca9581926673d69b24cJohn McCall        // which means we get to do this crazy thing instead of Euclid's.
757ba4f5d5754c8291690d01ca9581926673d69b24cJohn McCall        uint64_t lowBitOfOffset = offset & (~offset + 1);
758ba4f5d5754c8291690d01ca9581926673d69b24cJohn McCall        if (lowBitOfOffset < fieldAlign)
759ba4f5d5754c8291690d01ca9581926673d69b24cJohn McCall          fieldAlign = static_cast<unsigned>(lowBitOfOffset);
760ba4f5d5754c8291690d01ca9581926673d69b24cJohn McCall      }
761ba4f5d5754c8291690d01ca9581926673d69b24cJohn McCall
762ba4f5d5754c8291690d01ca9581926673d69b24cJohn McCall      Align = std::min(Align, fieldAlign);
76305f62474dd2b0f1cb69adbe0787f2868788aa949Charles Davis    }
764af707ab8fbb9451e8febb8d766f6c043628125c4Chris Lattner  }
765dcdafb6a701aa9d81edcb088915f58933315dc05Eli Friedman
766eb6f5dc86531f794ba7746a2da4d28e37cf5da7eKen Dyck  return toCharUnitsFromBits(Align);
767af707ab8fbb9451e8febb8d766f6c043628125c4Chris Lattner}
768b7cfe88e88cb4f46308de89cf3f0c81bfe624128Chris Lattner
769ea1471e0e967548c596a71469702f8846dbaf3c0John McCallstd::pair<CharUnits, CharUnits>
770bee5a79fc95e3003d755031e3d2bb4410a71e1c1Ken DyckASTContext::getTypeInfoInChars(const Type *T) const {
771ea1471e0e967548c596a71469702f8846dbaf3c0John McCall  std::pair<uint64_t, unsigned> Info = getTypeInfo(T);
772eb6f5dc86531f794ba7746a2da4d28e37cf5da7eKen Dyck  return std::make_pair(toCharUnitsFromBits(Info.first),
773eb6f5dc86531f794ba7746a2da4d28e37cf5da7eKen Dyck                        toCharUnitsFromBits(Info.second));
774ea1471e0e967548c596a71469702f8846dbaf3c0John McCall}
775ea1471e0e967548c596a71469702f8846dbaf3c0John McCall
776ea1471e0e967548c596a71469702f8846dbaf3c0John McCallstd::pair<CharUnits, CharUnits>
777bee5a79fc95e3003d755031e3d2bb4410a71e1c1Ken DyckASTContext::getTypeInfoInChars(QualType T) const {
778ea1471e0e967548c596a71469702f8846dbaf3c0John McCall  return getTypeInfoInChars(T.getTypePtr());
779ea1471e0e967548c596a71469702f8846dbaf3c0John McCall}
780ea1471e0e967548c596a71469702f8846dbaf3c0John McCall
781a7674d8a9a69f3f6fe16e70cf2a3b2b15fb7c43dChris Lattner/// getTypeSize - Return the size of the specified type, in bits.  This method
782a7674d8a9a69f3f6fe16e70cf2a3b2b15fb7c43dChris Lattner/// does not work on incomplete types.
7830953e767ff7817f97b3ab20896b229891eeff45bJohn McCall///
7840953e767ff7817f97b3ab20896b229891eeff45bJohn McCall/// FIXME: Pointers into different addr spaces could have different sizes and
7850953e767ff7817f97b3ab20896b229891eeff45bJohn McCall/// alignment requirements: getPointerInfo should take an AddrSpace, this
7860953e767ff7817f97b3ab20896b229891eeff45bJohn McCall/// should take a QualType, &c.
787d2d2a11a91d7ddf468bfb70f66362d24806ed601Chris Lattnerstd::pair<uint64_t, unsigned>
7884ba2a17694148e16eaa8d3917f657ffcd3667be4Jay FoadASTContext::getTypeInfo(const Type *T) const {
7895e301007e31e14c8ff647288e1b8bd8dbf8a5fe4Mike Stump  uint64_t Width=0;
7905e301007e31e14c8ff647288e1b8bd8dbf8a5fe4Mike Stump  unsigned Align=8;
791a7674d8a9a69f3f6fe16e70cf2a3b2b15fb7c43dChris Lattner  switch (T->getTypeClass()) {
79272564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor#define TYPE(Class, Base)
79372564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor#define ABSTRACT_TYPE(Class, Base)
79418857644059c45da6776f1a288eec7b4cf3a844aDouglas Gregor#define NON_CANONICAL_TYPE(Class, Base)
79572564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor#define DEPENDENT_TYPE(Class, Base) case Type::Class:
79672564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor#include "clang/AST/TypeNodes.def"
797d3d49bb27c7ffd9accc0a6c00e887111c0348845John McCall    llvm_unreachable("Should not see dependent types");
79872564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor    break;
79972564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor
8005d2a6303467184b1f159bb6556efc434e50e3c28Chris Lattner  case Type::FunctionNoProto:
8015d2a6303467184b1f159bb6556efc434e50e3c28Chris Lattner  case Type::FunctionProto:
80218857644059c45da6776f1a288eec7b4cf3a844aDouglas Gregor    // GCC extension: alignof(function) = 32 bits
80318857644059c45da6776f1a288eec7b4cf3a844aDouglas Gregor    Width = 0;
80418857644059c45da6776f1a288eec7b4cf3a844aDouglas Gregor    Align = 32;
80518857644059c45da6776f1a288eec7b4cf3a844aDouglas Gregor    break;
80618857644059c45da6776f1a288eec7b4cf3a844aDouglas Gregor
80772564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor  case Type::IncompleteArray:
808fb22d96692c5240fb8d611290dbf7eeed3759c73Steve Naroff  case Type::VariableArray:
80918857644059c45da6776f1a288eec7b4cf3a844aDouglas Gregor    Width = 0;
81018857644059c45da6776f1a288eec7b4cf3a844aDouglas Gregor    Align = getTypeAlign(cast<ArrayType>(T)->getElementType());
81118857644059c45da6776f1a288eec7b4cf3a844aDouglas Gregor    break;
81218857644059c45da6776f1a288eec7b4cf3a844aDouglas Gregor
813fb22d96692c5240fb8d611290dbf7eeed3759c73Steve Naroff  case Type::ConstantArray: {
8141d75118af76cae2bfc06389cde410e14bd0a19fcDaniel Dunbar    const ConstantArrayType *CAT = cast<ConstantArrayType>(T);
8151eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
81698be4943e8dc4f3905629a7102668960873cf863Chris Lattner    std::pair<uint64_t, unsigned> EltInfo = getTypeInfo(CAT->getElementType());
8179e9b6dc3fd413f5341fab54b681420eeb21cd169Chris Lattner    Width = EltInfo.first*CAT->getSize().getZExtValue();
818030d8846c7e520330007087e949f621989876e3aChris Lattner    Align = EltInfo.second;
819cd88b4171753dcb2bc0a21d78f1597c796bb8a20Argyrios Kyrtzidis    Width = llvm::RoundUpToAlignment(Width, Align);
820030d8846c7e520330007087e949f621989876e3aChris Lattner    break;
8215c09a02a5db85e08a432b6eeced9aa656349710dChristopher Lamb  }
822213541a68a3e137d11d2cefb612c6cdb410d7e8eNate Begeman  case Type::ExtVector:
823030d8846c7e520330007087e949f621989876e3aChris Lattner  case Type::Vector: {
8249fcfe926432f3c3f7e9a61219e55c352fd358e45Chris Lattner    const VectorType *VT = cast<VectorType>(T);
8259fcfe926432f3c3f7e9a61219e55c352fd358e45Chris Lattner    std::pair<uint64_t, unsigned> EltInfo = getTypeInfo(VT->getElementType());
8269fcfe926432f3c3f7e9a61219e55c352fd358e45Chris Lattner    Width = EltInfo.first*VT->getNumElements();
8274bd998bbc228915d2b9cae5b67879de48940d05eEli Friedman    Align = Width;
8286fe7c8aa8c7546743ecd0ac0138c2cf5d8155386Nate Begeman    // If the alignment is not a power of 2, round up to the next power of 2.
8296fe7c8aa8c7546743ecd0ac0138c2cf5d8155386Nate Begeman    // This happens for non-power-of-2 length vectors.
8308eefcd353c1d06a10104f69e5079ebab3183f9a3Dan Gohman    if (Align & (Align-1)) {
8319fcfe926432f3c3f7e9a61219e55c352fd358e45Chris Lattner      Align = llvm::NextPowerOf2(Align);
8329fcfe926432f3c3f7e9a61219e55c352fd358e45Chris Lattner      Width = llvm::RoundUpToAlignment(Width, Align);
8339fcfe926432f3c3f7e9a61219e55c352fd358e45Chris Lattner    }
834030d8846c7e520330007087e949f621989876e3aChris Lattner    break;
835030d8846c7e520330007087e949f621989876e3aChris Lattner  }
8365d2a6303467184b1f159bb6556efc434e50e3c28Chris Lattner
8379e9b6dc3fd413f5341fab54b681420eeb21cd169Chris Lattner  case Type::Builtin:
838a7674d8a9a69f3f6fe16e70cf2a3b2b15fb7c43dChris Lattner    switch (cast<BuiltinType>(T)->getKind()) {
839692233e90a99c3a81dd04879d36eb9688f137c44Chris Lattner    default: assert(0 && "Unknown builtin type!");
840d2d2a11a91d7ddf468bfb70f66362d24806ed601Chris Lattner    case BuiltinType::Void:
84118857644059c45da6776f1a288eec7b4cf3a844aDouglas Gregor      // GCC extension: alignof(void) = 8 bits.
84218857644059c45da6776f1a288eec7b4cf3a844aDouglas Gregor      Width = 0;
84318857644059c45da6776f1a288eec7b4cf3a844aDouglas Gregor      Align = 8;
84418857644059c45da6776f1a288eec7b4cf3a844aDouglas Gregor      break;
84518857644059c45da6776f1a288eec7b4cf3a844aDouglas Gregor
8466f62c2abd8077bf70d2166d37e8caa426b34d8e4Chris Lattner    case BuiltinType::Bool:
8479e9b6dc3fd413f5341fab54b681420eeb21cd169Chris Lattner      Width = Target.getBoolWidth();
8489e9b6dc3fd413f5341fab54b681420eeb21cd169Chris Lattner      Align = Target.getBoolAlign();
8496f62c2abd8077bf70d2166d37e8caa426b34d8e4Chris Lattner      break;
850692233e90a99c3a81dd04879d36eb9688f137c44Chris Lattner    case BuiltinType::Char_S:
851692233e90a99c3a81dd04879d36eb9688f137c44Chris Lattner    case BuiltinType::Char_U:
852692233e90a99c3a81dd04879d36eb9688f137c44Chris Lattner    case BuiltinType::UChar:
8536f62c2abd8077bf70d2166d37e8caa426b34d8e4Chris Lattner    case BuiltinType::SChar:
8549e9b6dc3fd413f5341fab54b681420eeb21cd169Chris Lattner      Width = Target.getCharWidth();
8559e9b6dc3fd413f5341fab54b681420eeb21cd169Chris Lattner      Align = Target.getCharAlign();
8566f62c2abd8077bf70d2166d37e8caa426b34d8e4Chris Lattner      break;
8573f59c975aa5d047f7edd1b900b5e885c38af0ef7Chris Lattner    case BuiltinType::WChar_S:
8583f59c975aa5d047f7edd1b900b5e885c38af0ef7Chris Lattner    case BuiltinType::WChar_U:
85964c438a4be2a871fa43c78264663ba1e9788b94dArgyrios Kyrtzidis      Width = Target.getWCharWidth();
86064c438a4be2a871fa43c78264663ba1e9788b94dArgyrios Kyrtzidis      Align = Target.getWCharAlign();
86164c438a4be2a871fa43c78264663ba1e9788b94dArgyrios Kyrtzidis      break;
862f5c209d23b20ada4a9b6235db50317239cbf6ae1Alisdair Meredith    case BuiltinType::Char16:
863f5c209d23b20ada4a9b6235db50317239cbf6ae1Alisdair Meredith      Width = Target.getChar16Width();
864f5c209d23b20ada4a9b6235db50317239cbf6ae1Alisdair Meredith      Align = Target.getChar16Align();
865f5c209d23b20ada4a9b6235db50317239cbf6ae1Alisdair Meredith      break;
866f5c209d23b20ada4a9b6235db50317239cbf6ae1Alisdair Meredith    case BuiltinType::Char32:
867f5c209d23b20ada4a9b6235db50317239cbf6ae1Alisdair Meredith      Width = Target.getChar32Width();
868f5c209d23b20ada4a9b6235db50317239cbf6ae1Alisdair Meredith      Align = Target.getChar32Align();
869f5c209d23b20ada4a9b6235db50317239cbf6ae1Alisdair Meredith      break;
870692233e90a99c3a81dd04879d36eb9688f137c44Chris Lattner    case BuiltinType::UShort:
8716f62c2abd8077bf70d2166d37e8caa426b34d8e4Chris Lattner    case BuiltinType::Short:
8729e9b6dc3fd413f5341fab54b681420eeb21cd169Chris Lattner      Width = Target.getShortWidth();
8739e9b6dc3fd413f5341fab54b681420eeb21cd169Chris Lattner      Align = Target.getShortAlign();
8746f62c2abd8077bf70d2166d37e8caa426b34d8e4Chris Lattner      break;
875692233e90a99c3a81dd04879d36eb9688f137c44Chris Lattner    case BuiltinType::UInt:
8766f62c2abd8077bf70d2166d37e8caa426b34d8e4Chris Lattner    case BuiltinType::Int:
8779e9b6dc3fd413f5341fab54b681420eeb21cd169Chris Lattner      Width = Target.getIntWidth();
8789e9b6dc3fd413f5341fab54b681420eeb21cd169Chris Lattner      Align = Target.getIntAlign();
8796f62c2abd8077bf70d2166d37e8caa426b34d8e4Chris Lattner      break;
880692233e90a99c3a81dd04879d36eb9688f137c44Chris Lattner    case BuiltinType::ULong:
8816f62c2abd8077bf70d2166d37e8caa426b34d8e4Chris Lattner    case BuiltinType::Long:
8829e9b6dc3fd413f5341fab54b681420eeb21cd169Chris Lattner      Width = Target.getLongWidth();
8839e9b6dc3fd413f5341fab54b681420eeb21cd169Chris Lattner      Align = Target.getLongAlign();
8846f62c2abd8077bf70d2166d37e8caa426b34d8e4Chris Lattner      break;
885692233e90a99c3a81dd04879d36eb9688f137c44Chris Lattner    case BuiltinType::ULongLong:
8866f62c2abd8077bf70d2166d37e8caa426b34d8e4Chris Lattner    case BuiltinType::LongLong:
8879e9b6dc3fd413f5341fab54b681420eeb21cd169Chris Lattner      Width = Target.getLongLongWidth();
8889e9b6dc3fd413f5341fab54b681420eeb21cd169Chris Lattner      Align = Target.getLongLongAlign();
8896f62c2abd8077bf70d2166d37e8caa426b34d8e4Chris Lattner      break;
890ec16cb9b5a481d62a73ad47fa59034ced4d62022Chris Lattner    case BuiltinType::Int128:
891ec16cb9b5a481d62a73ad47fa59034ced4d62022Chris Lattner    case BuiltinType::UInt128:
892ec16cb9b5a481d62a73ad47fa59034ced4d62022Chris Lattner      Width = 128;
893ec16cb9b5a481d62a73ad47fa59034ced4d62022Chris Lattner      Align = 128; // int128_t is 128-bit aligned on all targets.
894ec16cb9b5a481d62a73ad47fa59034ced4d62022Chris Lattner      break;
8956f62c2abd8077bf70d2166d37e8caa426b34d8e4Chris Lattner    case BuiltinType::Float:
8969e9b6dc3fd413f5341fab54b681420eeb21cd169Chris Lattner      Width = Target.getFloatWidth();
8979e9b6dc3fd413f5341fab54b681420eeb21cd169Chris Lattner      Align = Target.getFloatAlign();
8986f62c2abd8077bf70d2166d37e8caa426b34d8e4Chris Lattner      break;
8996f62c2abd8077bf70d2166d37e8caa426b34d8e4Chris Lattner    case BuiltinType::Double:
9005426bf6456a5aeac416a9150de157904d101c819Chris Lattner      Width = Target.getDoubleWidth();
9015426bf6456a5aeac416a9150de157904d101c819Chris Lattner      Align = Target.getDoubleAlign();
9026f62c2abd8077bf70d2166d37e8caa426b34d8e4Chris Lattner      break;
9036f62c2abd8077bf70d2166d37e8caa426b34d8e4Chris Lattner    case BuiltinType::LongDouble:
9049e9b6dc3fd413f5341fab54b681420eeb21cd169Chris Lattner      Width = Target.getLongDoubleWidth();
9059e9b6dc3fd413f5341fab54b681420eeb21cd169Chris Lattner      Align = Target.getLongDoubleAlign();
9066f62c2abd8077bf70d2166d37e8caa426b34d8e4Chris Lattner      break;
9076e8ed16ffef02b82995a90bdcf10ffff7d63839aSebastian Redl    case BuiltinType::NullPtr:
9086e8ed16ffef02b82995a90bdcf10ffff7d63839aSebastian Redl      Width = Target.getPointerWidth(0); // C++ 3.9.1p11: sizeof(nullptr_t)
9096e8ed16ffef02b82995a90bdcf10ffff7d63839aSebastian Redl      Align = Target.getPointerAlign(0); //   == sizeof(void*)
9101590d9c0fec4c710c2962e4bb71f76979b5163d3Sebastian Redl      break;
911e04f5fc25cf49e8a5b836459d836c20dc3229a95Fariborz Jahanian    case BuiltinType::ObjCId:
912e04f5fc25cf49e8a5b836459d836c20dc3229a95Fariborz Jahanian    case BuiltinType::ObjCClass:
913e04f5fc25cf49e8a5b836459d836c20dc3229a95Fariborz Jahanian    case BuiltinType::ObjCSel:
914e04f5fc25cf49e8a5b836459d836c20dc3229a95Fariborz Jahanian      Width = Target.getPointerWidth(0);
915e04f5fc25cf49e8a5b836459d836c20dc3229a95Fariborz Jahanian      Align = Target.getPointerAlign(0);
916e04f5fc25cf49e8a5b836459d836c20dc3229a95Fariborz Jahanian      break;
917a7674d8a9a69f3f6fe16e70cf2a3b2b15fb7c43dChris Lattner    }
918bfef6d7c67831a135d6ab79931f010f750a730adChris Lattner    break;
919d1b3c2dd5bc1f3103bee6137957aa7c5f8f2f0bcSteve Naroff  case Type::ObjCObjectPointer:
9205426bf6456a5aeac416a9150de157904d101c819Chris Lattner    Width = Target.getPointerWidth(0);
921f72a44330b9d9a4b2d93e9b91cfb8ab7bd4a0643Chris Lattner    Align = Target.getPointerAlign(0);
9226f62c2abd8077bf70d2166d37e8caa426b34d8e4Chris Lattner    break;
923485eeff9ba73376c8e01179bf1a501b1723446cbSteve Naroff  case Type::BlockPointer: {
924207f4d8543529221932af82836016a2ef066c917Peter Collingbourne    unsigned AS = getTargetAddressSpace(
925207f4d8543529221932af82836016a2ef066c917Peter Collingbourne        cast<BlockPointerType>(T)->getPointeeType());
926485eeff9ba73376c8e01179bf1a501b1723446cbSteve Naroff    Width = Target.getPointerWidth(AS);
927485eeff9ba73376c8e01179bf1a501b1723446cbSteve Naroff    Align = Target.getPointerAlign(AS);
928485eeff9ba73376c8e01179bf1a501b1723446cbSteve Naroff    break;
929485eeff9ba73376c8e01179bf1a501b1723446cbSteve Naroff  }
9305d484e8cf710207010720589d89602233de61d01Sebastian Redl  case Type::LValueReference:
9315d484e8cf710207010720589d89602233de61d01Sebastian Redl  case Type::RValueReference: {
9325d484e8cf710207010720589d89602233de61d01Sebastian Redl    // alignof and sizeof should never enter this code path here, so we go
9335d484e8cf710207010720589d89602233de61d01Sebastian Redl    // the pointer route.
934207f4d8543529221932af82836016a2ef066c917Peter Collingbourne    unsigned AS = getTargetAddressSpace(
935207f4d8543529221932af82836016a2ef066c917Peter Collingbourne        cast<ReferenceType>(T)->getPointeeType());
9365d484e8cf710207010720589d89602233de61d01Sebastian Redl    Width = Target.getPointerWidth(AS);
9375d484e8cf710207010720589d89602233de61d01Sebastian Redl    Align = Target.getPointerAlign(AS);
9385d484e8cf710207010720589d89602233de61d01Sebastian Redl    break;
9395d484e8cf710207010720589d89602233de61d01Sebastian Redl  }
940f72a44330b9d9a4b2d93e9b91cfb8ab7bd4a0643Chris Lattner  case Type::Pointer: {
941207f4d8543529221932af82836016a2ef066c917Peter Collingbourne    unsigned AS = getTargetAddressSpace(cast<PointerType>(T)->getPointeeType());
9425426bf6456a5aeac416a9150de157904d101c819Chris Lattner    Width = Target.getPointerWidth(AS);
943f72a44330b9d9a4b2d93e9b91cfb8ab7bd4a0643Chris Lattner    Align = Target.getPointerAlign(AS);
944f72a44330b9d9a4b2d93e9b91cfb8ab7bd4a0643Chris Lattner    break;
945f72a44330b9d9a4b2d93e9b91cfb8ab7bd4a0643Chris Lattner  }
946f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl  case Type::MemberPointer: {
947071cc7deffad608165b1ddd5263e8bf181861520Charles Davis    const MemberPointerType *MPT = cast<MemberPointerType>(T);
9481eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    std::pair<uint64_t, unsigned> PtrDiffInfo =
9491cca74ef3627a3a0ab14501d23e336548f6611b2Anders Carlsson      getTypeInfo(getPointerDiffType());
950071cc7deffad608165b1ddd5263e8bf181861520Charles Davis    Width = PtrDiffInfo.first * ABI->getMemberPointerSize(MPT);
9511cca74ef3627a3a0ab14501d23e336548f6611b2Anders Carlsson    Align = PtrDiffInfo.second;
9521cca74ef3627a3a0ab14501d23e336548f6611b2Anders Carlsson    break;
953f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl  }
9545d2a6303467184b1f159bb6556efc434e50e3c28Chris Lattner  case Type::Complex: {
9555d2a6303467184b1f159bb6556efc434e50e3c28Chris Lattner    // Complex types have the same alignment as their elements, but twice the
9565d2a6303467184b1f159bb6556efc434e50e3c28Chris Lattner    // size.
9571eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    std::pair<uint64_t, unsigned> EltInfo =
95898be4943e8dc4f3905629a7102668960873cf863Chris Lattner      getTypeInfo(cast<ComplexType>(T)->getElementType());
9599e9b6dc3fd413f5341fab54b681420eeb21cd169Chris Lattner    Width = EltInfo.first*2;
9605d2a6303467184b1f159bb6556efc434e50e3c28Chris Lattner    Align = EltInfo.second;
9615d2a6303467184b1f159bb6556efc434e50e3c28Chris Lattner    break;
9625d2a6303467184b1f159bb6556efc434e50e3c28Chris Lattner  }
963c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall  case Type::ObjCObject:
964c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall    return getTypeInfo(cast<ObjCObjectType>(T)->getBaseType().getTypePtr());
96544a3dded8080c5c9cfdad208ade8f8f7850d9a4fDevang Patel  case Type::ObjCInterface: {
9661d75118af76cae2bfc06389cde410e14bd0a19fcDaniel Dunbar    const ObjCInterfaceType *ObjCI = cast<ObjCInterfaceType>(T);
96744a3dded8080c5c9cfdad208ade8f8f7850d9a4fDevang Patel    const ASTRecordLayout &Layout = getASTObjCInterfaceLayout(ObjCI->getDecl());
968dd76a9ab9ea675671200f94b18ce95766841952bKen Dyck    Width = toBits(Layout.getSize());
969dac54c124e302d6f028ea5723c425b7f66fc7c71Ken Dyck    Align = toBits(Layout.getAlignment());
97044a3dded8080c5c9cfdad208ade8f8f7850d9a4fDevang Patel    break;
97144a3dded8080c5c9cfdad208ade8f8f7850d9a4fDevang Patel  }
97272564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor  case Type::Record:
97372564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor  case Type::Enum: {
9741d75118af76cae2bfc06389cde410e14bd0a19fcDaniel Dunbar    const TagType *TT = cast<TagType>(T);
9751d75118af76cae2bfc06389cde410e14bd0a19fcDaniel Dunbar
9761d75118af76cae2bfc06389cde410e14bd0a19fcDaniel Dunbar    if (TT->getDecl()->isInvalidDecl()) {
97722ce41d9fc8509da65858c75bf5b3c4dae2d8c04Douglas Gregor      Width = 8;
97822ce41d9fc8509da65858c75bf5b3c4dae2d8c04Douglas Gregor      Align = 8;
9798389eab190afef3462f6418b8d8fb70fb01c4005Chris Lattner      break;
9808389eab190afef3462f6418b8d8fb70fb01c4005Chris Lattner    }
9811eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
9821d75118af76cae2bfc06389cde410e14bd0a19fcDaniel Dunbar    if (const EnumType *ET = dyn_cast<EnumType>(TT))
9837176331b0f5cfaaa2b5aa487a6660e859e371119Chris Lattner      return getTypeInfo(ET->getDecl()->getIntegerType());
9847176331b0f5cfaaa2b5aa487a6660e859e371119Chris Lattner
9851d75118af76cae2bfc06389cde410e14bd0a19fcDaniel Dunbar    const RecordType *RT = cast<RecordType>(TT);
9867176331b0f5cfaaa2b5aa487a6660e859e371119Chris Lattner    const ASTRecordLayout &Layout = getASTRecordLayout(RT->getDecl());
987dd76a9ab9ea675671200f94b18ce95766841952bKen Dyck    Width = toBits(Layout.getSize());
988dac54c124e302d6f028ea5723c425b7f66fc7c71Ken Dyck    Align = toBits(Layout.getAlignment());
989dc0d73e6495404418acf8548875aeaff07791a74Chris Lattner    break;
990a7674d8a9a69f3f6fe16e70cf2a3b2b15fb7c43dChris Lattner  }
9917532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor
9929fcfe926432f3c3f7e9a61219e55c352fd358e45Chris Lattner  case Type::SubstTemplateTypeParm:
99349a832bd499d6f61c23655f1fac99f0dd229756eJohn McCall    return getTypeInfo(cast<SubstTemplateTypeParmType>(T)->
99449a832bd499d6f61c23655f1fac99f0dd229756eJohn McCall                       getReplacementType().getTypePtr());
99549a832bd499d6f61c23655f1fac99f0dd229756eJohn McCall
99634b41d939a1328f484511c6002ba2456db879a29Richard Smith  case Type::Auto: {
99734b41d939a1328f484511c6002ba2456db879a29Richard Smith    const AutoType *A = cast<AutoType>(T);
99834b41d939a1328f484511c6002ba2456db879a29Richard Smith    assert(A->isDeduced() && "Cannot request the size of a dependent type");
999dc856aff4428380baa9afb5577ea04f8fb6beb13Matt Beaumont-Gay    return getTypeInfo(A->getDeducedType().getTypePtr());
100034b41d939a1328f484511c6002ba2456db879a29Richard Smith  }
100134b41d939a1328f484511c6002ba2456db879a29Richard Smith
1002075f8f1b6bed4d1b224c74f87508534cc6392ce6Abramo Bagnara  case Type::Paren:
1003075f8f1b6bed4d1b224c74f87508534cc6392ce6Abramo Bagnara    return getTypeInfo(cast<ParenType>(T)->getInnerType().getTypePtr());
1004075f8f1b6bed4d1b224c74f87508534cc6392ce6Abramo Bagnara
100518857644059c45da6776f1a288eec7b4cf3a844aDouglas Gregor  case Type::Typedef: {
1006162e1c1b487352434552147967c3dd296ebee2f7Richard Smith    const TypedefNameDecl *Typedef = cast<TypedefType>(T)->getDecl();
1007df1367af26cb2959775e9511108f12dcd2370a27Douglas Gregor    std::pair<uint64_t, unsigned> Info
1008df1367af26cb2959775e9511108f12dcd2370a27Douglas Gregor      = getTypeInfo(Typedef->getUnderlyingType().getTypePtr());
1009c1de52de64725945e5ae87e6f99ddedf161856e5Chris Lattner    // If the typedef has an aligned attribute on it, it overrides any computed
1010c1de52de64725945e5ae87e6f99ddedf161856e5Chris Lattner    // alignment we have.  This violates the GCC documentation (which says that
1011c1de52de64725945e5ae87e6f99ddedf161856e5Chris Lattner    // attribute(aligned) can only round up) but matches its implementation.
1012c1de52de64725945e5ae87e6f99ddedf161856e5Chris Lattner    if (unsigned AttrAlign = Typedef->getMaxAlignment())
1013c1de52de64725945e5ae87e6f99ddedf161856e5Chris Lattner      Align = AttrAlign;
1014c1de52de64725945e5ae87e6f99ddedf161856e5Chris Lattner    else
1015c1de52de64725945e5ae87e6f99ddedf161856e5Chris Lattner      Align = Info.second;
1016df1367af26cb2959775e9511108f12dcd2370a27Douglas Gregor    Width = Info.first;
10177532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor    break;
10187176331b0f5cfaaa2b5aa487a6660e859e371119Chris Lattner  }
101918857644059c45da6776f1a288eec7b4cf3a844aDouglas Gregor
102018857644059c45da6776f1a288eec7b4cf3a844aDouglas Gregor  case Type::TypeOfExpr:
102118857644059c45da6776f1a288eec7b4cf3a844aDouglas Gregor    return getTypeInfo(cast<TypeOfExprType>(T)->getUnderlyingExpr()->getType()
102218857644059c45da6776f1a288eec7b4cf3a844aDouglas Gregor                         .getTypePtr());
102318857644059c45da6776f1a288eec7b4cf3a844aDouglas Gregor
102418857644059c45da6776f1a288eec7b4cf3a844aDouglas Gregor  case Type::TypeOf:
102518857644059c45da6776f1a288eec7b4cf3a844aDouglas Gregor    return getTypeInfo(cast<TypeOfType>(T)->getUnderlyingType().getTypePtr());
102618857644059c45da6776f1a288eec7b4cf3a844aDouglas Gregor
1027395b475a4474f1c7574d927ad142ca0c7997cbcaAnders Carlsson  case Type::Decltype:
1028395b475a4474f1c7574d927ad142ca0c7997cbcaAnders Carlsson    return getTypeInfo(cast<DecltypeType>(T)->getUnderlyingExpr()->getType()
1029395b475a4474f1c7574d927ad142ca0c7997cbcaAnders Carlsson                        .getTypePtr());
1030395b475a4474f1c7574d927ad142ca0c7997cbcaAnders Carlsson
1031ca63c200346c0ca9e00194ec6e34a5a7b0ed9321Sean Hunt  case Type::UnaryTransform:
1032ca63c200346c0ca9e00194ec6e34a5a7b0ed9321Sean Hunt    return getTypeInfo(cast<UnaryTransformType>(T)->getUnderlyingType());
1033ca63c200346c0ca9e00194ec6e34a5a7b0ed9321Sean Hunt
1034465d41b92b2c862f3062c412a0538db65c6a2661Abramo Bagnara  case Type::Elaborated:
1035465d41b92b2c862f3062c412a0538db65c6a2661Abramo Bagnara    return getTypeInfo(cast<ElaboratedType>(T)->getNamedType().getTypePtr());
10361eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
10379d156a7b1b2771e191f2f5a45a7b7a694129463bJohn McCall  case Type::Attributed:
10389d156a7b1b2771e191f2f5a45a7b7a694129463bJohn McCall    return getTypeInfo(
10399d156a7b1b2771e191f2f5a45a7b7a694129463bJohn McCall                  cast<AttributedType>(T)->getEquivalentType().getTypePtr());
10409d156a7b1b2771e191f2f5a45a7b7a694129463bJohn McCall
10413e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith  case Type::TemplateSpecialization: {
10421eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    assert(getCanonicalType(T) != T &&
104318857644059c45da6776f1a288eec7b4cf3a844aDouglas Gregor           "Cannot request the size of a dependent type");
10443e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith    const TemplateSpecializationType *TST = cast<TemplateSpecializationType>(T);
10453e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith    // A type alias template specialization may refer to a typedef with the
10463e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith    // aligned attribute on it.
10473e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith    if (TST->isTypeAlias())
10483e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith      return getTypeInfo(TST->getAliasedType().getTypePtr());
10493e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith    else
10503e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith      return getTypeInfo(getCanonicalType(T));
10513e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith  }
10523e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith
105318857644059c45da6776f1a288eec7b4cf3a844aDouglas Gregor  }
10541eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1055464175bba1318bef7905122e9fda20cff926df78Chris Lattner  assert(Align && (Align & (Align-1)) == 0 && "Alignment must be power of 2");
10569e9b6dc3fd413f5341fab54b681420eeb21cd169Chris Lattner  return std::make_pair(Width, Align);
1057a7674d8a9a69f3f6fe16e70cf2a3b2b15fb7c43dChris Lattner}
1058a7674d8a9a69f3f6fe16e70cf2a3b2b15fb7c43dChris Lattner
1059eb6f5dc86531f794ba7746a2da4d28e37cf5da7eKen Dyck/// toCharUnitsFromBits - Convert a size in bits to a size in characters.
1060eb6f5dc86531f794ba7746a2da4d28e37cf5da7eKen DyckCharUnits ASTContext::toCharUnitsFromBits(int64_t BitSize) const {
1061eb6f5dc86531f794ba7746a2da4d28e37cf5da7eKen Dyck  return CharUnits::fromQuantity(BitSize / getCharWidth());
1062eb6f5dc86531f794ba7746a2da4d28e37cf5da7eKen Dyck}
1063eb6f5dc86531f794ba7746a2da4d28e37cf5da7eKen Dyck
1064dd76a9ab9ea675671200f94b18ce95766841952bKen Dyck/// toBits - Convert a size in characters to a size in characters.
1065dd76a9ab9ea675671200f94b18ce95766841952bKen Dyckint64_t ASTContext::toBits(CharUnits CharSize) const {
1066dd76a9ab9ea675671200f94b18ce95766841952bKen Dyck  return CharSize.getQuantity() * getCharWidth();
1067dd76a9ab9ea675671200f94b18ce95766841952bKen Dyck}
1068dd76a9ab9ea675671200f94b18ce95766841952bKen Dyck
1069bdc601b196c48d4cd56a5ceb45d41ae4e87371abKen Dyck/// getTypeSizeInChars - Return the size of the specified type, in characters.
1070bdc601b196c48d4cd56a5ceb45d41ae4e87371abKen Dyck/// This method does not work on incomplete types.
10714ba2a17694148e16eaa8d3917f657ffcd3667be4Jay FoadCharUnits ASTContext::getTypeSizeInChars(QualType T) const {
1072eb6f5dc86531f794ba7746a2da4d28e37cf5da7eKen Dyck  return toCharUnitsFromBits(getTypeSize(T));
1073bdc601b196c48d4cd56a5ceb45d41ae4e87371abKen Dyck}
10744ba2a17694148e16eaa8d3917f657ffcd3667be4Jay FoadCharUnits ASTContext::getTypeSizeInChars(const Type *T) const {
1075eb6f5dc86531f794ba7746a2da4d28e37cf5da7eKen Dyck  return toCharUnitsFromBits(getTypeSize(T));
1076bdc601b196c48d4cd56a5ceb45d41ae4e87371abKen Dyck}
1077bdc601b196c48d4cd56a5ceb45d41ae4e87371abKen Dyck
107816e20cce43385001f33f8e3f90ee345609c805d1Ken Dyck/// getTypeAlignInChars - Return the ABI-specified alignment of a type, in
107986fa4311c8a330957ff5b765fbb0a7750ecd38c9Ken Dyck/// characters. This method does not work on incomplete types.
10804ba2a17694148e16eaa8d3917f657ffcd3667be4Jay FoadCharUnits ASTContext::getTypeAlignInChars(QualType T) const {
1081eb6f5dc86531f794ba7746a2da4d28e37cf5da7eKen Dyck  return toCharUnitsFromBits(getTypeAlign(T));
108286fa4311c8a330957ff5b765fbb0a7750ecd38c9Ken Dyck}
10834ba2a17694148e16eaa8d3917f657ffcd3667be4Jay FoadCharUnits ASTContext::getTypeAlignInChars(const Type *T) const {
1084eb6f5dc86531f794ba7746a2da4d28e37cf5da7eKen Dyck  return toCharUnitsFromBits(getTypeAlign(T));
108586fa4311c8a330957ff5b765fbb0a7750ecd38c9Ken Dyck}
108686fa4311c8a330957ff5b765fbb0a7750ecd38c9Ken Dyck
108734ebde404dc17d89487b07e6daaf1b47d5dfee39Chris Lattner/// getPreferredTypeAlign - Return the "preferred" alignment of the specified
108834ebde404dc17d89487b07e6daaf1b47d5dfee39Chris Lattner/// type for the current target in bits.  This can be different than the ABI
108934ebde404dc17d89487b07e6daaf1b47d5dfee39Chris Lattner/// alignment in cases where it is beneficial for performance to overalign
109034ebde404dc17d89487b07e6daaf1b47d5dfee39Chris Lattner/// a data type.
10914ba2a17694148e16eaa8d3917f657ffcd3667be4Jay Foadunsigned ASTContext::getPreferredTypeAlign(const Type *T) const {
109234ebde404dc17d89487b07e6daaf1b47d5dfee39Chris Lattner  unsigned ABIAlign = getTypeAlign(T);
10931eed60297ef4701b899c6a3b9680bf08f3403422Eli Friedman
10941eed60297ef4701b899c6a3b9680bf08f3403422Eli Friedman  // Double and long long should be naturally aligned if possible.
1095183700f494ec9b6701b6efe82bcb25f4c79ba561John McCall  if (const ComplexType* CT = T->getAs<ComplexType>())
10961eed60297ef4701b899c6a3b9680bf08f3403422Eli Friedman    T = CT->getElementType().getTypePtr();
10971eed60297ef4701b899c6a3b9680bf08f3403422Eli Friedman  if (T->isSpecificBuiltinType(BuiltinType::Double) ||
10981eed60297ef4701b899c6a3b9680bf08f3403422Eli Friedman      T->isSpecificBuiltinType(BuiltinType::LongLong))
10991eed60297ef4701b899c6a3b9680bf08f3403422Eli Friedman    return std::max(ABIAlign, (unsigned)getTypeSize(T));
11001eed60297ef4701b899c6a3b9680bf08f3403422Eli Friedman
110134ebde404dc17d89487b07e6daaf1b47d5dfee39Chris Lattner  return ABIAlign;
110234ebde404dc17d89487b07e6daaf1b47d5dfee39Chris Lattner}
110334ebde404dc17d89487b07e6daaf1b47d5dfee39Chris Lattner
11042c18bb7c9fca66c30b6eabbdcbc6399d24a54fa9Fariborz Jahanian/// DeepCollectObjCIvars -
11052c18bb7c9fca66c30b6eabbdcbc6399d24a54fa9Fariborz Jahanian/// This routine first collects all declared, but not synthesized, ivars in
11062c18bb7c9fca66c30b6eabbdcbc6399d24a54fa9Fariborz Jahanian/// super class and then collects all ivars, including those synthesized for
11072c18bb7c9fca66c30b6eabbdcbc6399d24a54fa9Fariborz Jahanian/// current class. This routine is used for implementation of current class
11082c18bb7c9fca66c30b6eabbdcbc6399d24a54fa9Fariborz Jahanian/// when all ivars, declared and synthesized are known.
11099820074dd47d37681085e964cd3392ac0b3e67b9Fariborz Jahanian///
11102c18bb7c9fca66c30b6eabbdcbc6399d24a54fa9Fariborz Jahanianvoid ASTContext::DeepCollectObjCIvars(const ObjCInterfaceDecl *OI,
11112c18bb7c9fca66c30b6eabbdcbc6399d24a54fa9Fariborz Jahanian                                      bool leafClass,
1112db8264e4c5ffd7af6fbad4ca4306bd382bb02691Jordy Rose                            SmallVectorImpl<const ObjCIvarDecl*> &Ivars) const {
11132c18bb7c9fca66c30b6eabbdcbc6399d24a54fa9Fariborz Jahanian  if (const ObjCInterfaceDecl *SuperClass = OI->getSuperClass())
11142c18bb7c9fca66c30b6eabbdcbc6399d24a54fa9Fariborz Jahanian    DeepCollectObjCIvars(SuperClass, false, Ivars);
11152c18bb7c9fca66c30b6eabbdcbc6399d24a54fa9Fariborz Jahanian  if (!leafClass) {
11162c18bb7c9fca66c30b6eabbdcbc6399d24a54fa9Fariborz Jahanian    for (ObjCInterfaceDecl::ivar_iterator I = OI->ivar_begin(),
11172c18bb7c9fca66c30b6eabbdcbc6399d24a54fa9Fariborz Jahanian         E = OI->ivar_end(); I != E; ++I)
111811062e11236b7bc689dad150e8b490fd6b063ec3Fariborz Jahanian      Ivars.push_back(*I);
111911062e11236b7bc689dad150e8b490fd6b063ec3Fariborz Jahanian  }
1120bf9eb88792e022e54a658657bf22e1925948e384Fariborz Jahanian  else {
1121bf9eb88792e022e54a658657bf22e1925948e384Fariborz Jahanian    ObjCInterfaceDecl *IDecl = const_cast<ObjCInterfaceDecl *>(OI);
1122db8264e4c5ffd7af6fbad4ca4306bd382bb02691Jordy Rose    for (const ObjCIvarDecl *Iv = IDecl->all_declared_ivar_begin(); Iv;
1123bf9eb88792e022e54a658657bf22e1925948e384Fariborz Jahanian         Iv= Iv->getNextIvar())
1124bf9eb88792e022e54a658657bf22e1925948e384Fariborz Jahanian      Ivars.push_back(Iv);
1125bf9eb88792e022e54a658657bf22e1925948e384Fariborz Jahanian  }
11269820074dd47d37681085e964cd3392ac0b3e67b9Fariborz Jahanian}
11279820074dd47d37681085e964cd3392ac0b3e67b9Fariborz Jahanian
1128e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanian/// CollectInheritedProtocols - Collect all protocols in current class and
1129e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanian/// those inherited by it.
1130e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanianvoid ASTContext::CollectInheritedProtocols(const Decl *CDecl,
1131432a8893f7e30d141d7f279bd00b741a3cdac81fFariborz Jahanian                          llvm::SmallPtrSet<ObjCProtocolDecl*, 8> &Protocols) {
1132e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanian  if (const ObjCInterfaceDecl *OI = dyn_cast<ObjCInterfaceDecl>(CDecl)) {
113353b9441b5a81a24fa1f66f3f6416f1e36baa9c2fTed Kremenek    // We can use protocol_iterator here instead of
113453b9441b5a81a24fa1f66f3f6416f1e36baa9c2fTed Kremenek    // all_referenced_protocol_iterator since we are walking all categories.
113553b9441b5a81a24fa1f66f3f6416f1e36baa9c2fTed Kremenek    for (ObjCInterfaceDecl::all_protocol_iterator P = OI->all_referenced_protocol_begin(),
113653b9441b5a81a24fa1f66f3f6416f1e36baa9c2fTed Kremenek         PE = OI->all_referenced_protocol_end(); P != PE; ++P) {
1137e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanian      ObjCProtocolDecl *Proto = (*P);
1138432a8893f7e30d141d7f279bd00b741a3cdac81fFariborz Jahanian      Protocols.insert(Proto);
1139e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanian      for (ObjCProtocolDecl::protocol_iterator P = Proto->protocol_begin(),
1140b2f812165676230bce5d0215e49a4749c451ca9cFariborz Jahanian           PE = Proto->protocol_end(); P != PE; ++P) {
1141b2f812165676230bce5d0215e49a4749c451ca9cFariborz Jahanian        Protocols.insert(*P);
1142e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanian        CollectInheritedProtocols(*P, Protocols);
1143e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanian      }
1144b2f812165676230bce5d0215e49a4749c451ca9cFariborz Jahanian    }
1145e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanian
1146e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanian    // Categories of this Interface.
1147e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanian    for (const ObjCCategoryDecl *CDeclChain = OI->getCategoryList();
1148e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanian         CDeclChain; CDeclChain = CDeclChain->getNextClassCategory())
1149e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanian      CollectInheritedProtocols(CDeclChain, Protocols);
1150e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanian    if (ObjCInterfaceDecl *SD = OI->getSuperClass())
1151e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanian      while (SD) {
1152e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanian        CollectInheritedProtocols(SD, Protocols);
1153e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanian        SD = SD->getSuperClass();
1154e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanian      }
1155b170ca5f4a8397c10e52050ff5df6885a3e6eca9Benjamin Kramer  } else if (const ObjCCategoryDecl *OC = dyn_cast<ObjCCategoryDecl>(CDecl)) {
115653b9441b5a81a24fa1f66f3f6416f1e36baa9c2fTed Kremenek    for (ObjCCategoryDecl::protocol_iterator P = OC->protocol_begin(),
1157e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanian         PE = OC->protocol_end(); P != PE; ++P) {
1158e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanian      ObjCProtocolDecl *Proto = (*P);
1159432a8893f7e30d141d7f279bd00b741a3cdac81fFariborz Jahanian      Protocols.insert(Proto);
1160e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanian      for (ObjCProtocolDecl::protocol_iterator P = Proto->protocol_begin(),
1161e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanian           PE = Proto->protocol_end(); P != PE; ++P)
1162e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanian        CollectInheritedProtocols(*P, Protocols);
1163e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanian    }
1164b170ca5f4a8397c10e52050ff5df6885a3e6eca9Benjamin Kramer  } else if (const ObjCProtocolDecl *OP = dyn_cast<ObjCProtocolDecl>(CDecl)) {
1165e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanian    for (ObjCProtocolDecl::protocol_iterator P = OP->protocol_begin(),
1166e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanian         PE = OP->protocol_end(); P != PE; ++P) {
1167e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanian      ObjCProtocolDecl *Proto = (*P);
1168432a8893f7e30d141d7f279bd00b741a3cdac81fFariborz Jahanian      Protocols.insert(Proto);
1169e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanian      for (ObjCProtocolDecl::protocol_iterator P = Proto->protocol_begin(),
1170e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanian           PE = Proto->protocol_end(); P != PE; ++P)
1171e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanian        CollectInheritedProtocols(*P, Protocols);
1172e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanian    }
1173e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanian  }
1174e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanian}
1175e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanian
11764ba2a17694148e16eaa8d3917f657ffcd3667be4Jay Foadunsigned ASTContext::CountNonClassIvars(const ObjCInterfaceDecl *OI) const {
11773bfacdf6f0706987c1b33c625cd68fb880881c22Fariborz Jahanian  unsigned count = 0;
11783bfacdf6f0706987c1b33c625cd68fb880881c22Fariborz Jahanian  // Count ivars declared in class extension.
117980aa1cd7973561889e51c1c152c8990a8de9c953Fariborz Jahanian  for (const ObjCCategoryDecl *CDecl = OI->getFirstClassExtension(); CDecl;
118080aa1cd7973561889e51c1c152c8990a8de9c953Fariborz Jahanian       CDecl = CDecl->getNextClassExtension())
1181b170ca5f4a8397c10e52050ff5df6885a3e6eca9Benjamin Kramer    count += CDecl->ivar_size();
1182b170ca5f4a8397c10e52050ff5df6885a3e6eca9Benjamin Kramer
11833bfacdf6f0706987c1b33c625cd68fb880881c22Fariborz Jahanian  // Count ivar defined in this class's implementation.  This
11843bfacdf6f0706987c1b33c625cd68fb880881c22Fariborz Jahanian  // includes synthesized ivars.
11853bfacdf6f0706987c1b33c625cd68fb880881c22Fariborz Jahanian  if (ObjCImplementationDecl *ImplDecl = OI->getImplementation())
1186b170ca5f4a8397c10e52050ff5df6885a3e6eca9Benjamin Kramer    count += ImplDecl->ivar_size();
1187b170ca5f4a8397c10e52050ff5df6885a3e6eca9Benjamin Kramer
11888e6ac1d80055fa37b9b84029c7e751624ba7f84cFariborz Jahanian  return count;
11898e6ac1d80055fa37b9b84029c7e751624ba7f84cFariborz Jahanian}
11908e6ac1d80055fa37b9b84029c7e751624ba7f84cFariborz Jahanian
11918a1d722f13df383600f36d77f842957c8adb5f1bArgyrios Kyrtzidis/// \brief Get the implementation of ObjCInterfaceDecl,or NULL if none exists.
11928a1d722f13df383600f36d77f842957c8adb5f1bArgyrios KyrtzidisObjCImplementationDecl *ASTContext::getObjCImplementation(ObjCInterfaceDecl *D) {
11938a1d722f13df383600f36d77f842957c8adb5f1bArgyrios Kyrtzidis  llvm::DenseMap<ObjCContainerDecl*, ObjCImplDecl*>::iterator
11948a1d722f13df383600f36d77f842957c8adb5f1bArgyrios Kyrtzidis    I = ObjCImpls.find(D);
11958a1d722f13df383600f36d77f842957c8adb5f1bArgyrios Kyrtzidis  if (I != ObjCImpls.end())
11968a1d722f13df383600f36d77f842957c8adb5f1bArgyrios Kyrtzidis    return cast<ObjCImplementationDecl>(I->second);
11978a1d722f13df383600f36d77f842957c8adb5f1bArgyrios Kyrtzidis  return 0;
11988a1d722f13df383600f36d77f842957c8adb5f1bArgyrios Kyrtzidis}
11998a1d722f13df383600f36d77f842957c8adb5f1bArgyrios Kyrtzidis/// \brief Get the implementation of ObjCCategoryDecl, or NULL if none exists.
12008a1d722f13df383600f36d77f842957c8adb5f1bArgyrios KyrtzidisObjCCategoryImplDecl *ASTContext::getObjCImplementation(ObjCCategoryDecl *D) {
12018a1d722f13df383600f36d77f842957c8adb5f1bArgyrios Kyrtzidis  llvm::DenseMap<ObjCContainerDecl*, ObjCImplDecl*>::iterator
12028a1d722f13df383600f36d77f842957c8adb5f1bArgyrios Kyrtzidis    I = ObjCImpls.find(D);
12038a1d722f13df383600f36d77f842957c8adb5f1bArgyrios Kyrtzidis  if (I != ObjCImpls.end())
12048a1d722f13df383600f36d77f842957c8adb5f1bArgyrios Kyrtzidis    return cast<ObjCCategoryImplDecl>(I->second);
12058a1d722f13df383600f36d77f842957c8adb5f1bArgyrios Kyrtzidis  return 0;
12068a1d722f13df383600f36d77f842957c8adb5f1bArgyrios Kyrtzidis}
12078a1d722f13df383600f36d77f842957c8adb5f1bArgyrios Kyrtzidis
12088a1d722f13df383600f36d77f842957c8adb5f1bArgyrios Kyrtzidis/// \brief Set the implementation of ObjCInterfaceDecl.
12098a1d722f13df383600f36d77f842957c8adb5f1bArgyrios Kyrtzidisvoid ASTContext::setObjCImplementation(ObjCInterfaceDecl *IFaceD,
12108a1d722f13df383600f36d77f842957c8adb5f1bArgyrios Kyrtzidis                           ObjCImplementationDecl *ImplD) {
12118a1d722f13df383600f36d77f842957c8adb5f1bArgyrios Kyrtzidis  assert(IFaceD && ImplD && "Passed null params");
12128a1d722f13df383600f36d77f842957c8adb5f1bArgyrios Kyrtzidis  ObjCImpls[IFaceD] = ImplD;
12138a1d722f13df383600f36d77f842957c8adb5f1bArgyrios Kyrtzidis}
12148a1d722f13df383600f36d77f842957c8adb5f1bArgyrios Kyrtzidis/// \brief Set the implementation of ObjCCategoryDecl.
12158a1d722f13df383600f36d77f842957c8adb5f1bArgyrios Kyrtzidisvoid ASTContext::setObjCImplementation(ObjCCategoryDecl *CatD,
12168a1d722f13df383600f36d77f842957c8adb5f1bArgyrios Kyrtzidis                           ObjCCategoryImplDecl *ImplD) {
12178a1d722f13df383600f36d77f842957c8adb5f1bArgyrios Kyrtzidis  assert(CatD && ImplD && "Passed null params");
12188a1d722f13df383600f36d77f842957c8adb5f1bArgyrios Kyrtzidis  ObjCImpls[CatD] = ImplD;
12198a1d722f13df383600f36d77f842957c8adb5f1bArgyrios Kyrtzidis}
12208a1d722f13df383600f36d77f842957c8adb5f1bArgyrios Kyrtzidis
12211ceee5c42d5c410217f67d384eecc6ea4a2bba9bFariborz Jahanian/// \brief Get the copy initialization expression of VarDecl,or NULL if
12221ceee5c42d5c410217f67d384eecc6ea4a2bba9bFariborz Jahanian/// none exists.
1223830937bc1100fba7682f7c32c40512085870f50cFariborz JahanianExpr *ASTContext::getBlockVarCopyInits(const VarDecl*VD) {
1224d016ec204c54d766c4561d43548551ffde0e6b43Fariborz Jahanian  assert(VD && "Passed null params");
1225d016ec204c54d766c4561d43548551ffde0e6b43Fariborz Jahanian  assert(VD->hasAttr<BlocksAttr>() &&
1226d016ec204c54d766c4561d43548551ffde0e6b43Fariborz Jahanian         "getBlockVarCopyInits - not __block var");
1227830937bc1100fba7682f7c32c40512085870f50cFariborz Jahanian  llvm::DenseMap<const VarDecl*, Expr*>::iterator
1228d016ec204c54d766c4561d43548551ffde0e6b43Fariborz Jahanian    I = BlockVarCopyInits.find(VD);
12291ceee5c42d5c410217f67d384eecc6ea4a2bba9bFariborz Jahanian  return (I != BlockVarCopyInits.end()) ? cast<Expr>(I->second) : 0;
12301ceee5c42d5c410217f67d384eecc6ea4a2bba9bFariborz Jahanian}
12311ceee5c42d5c410217f67d384eecc6ea4a2bba9bFariborz Jahanian
12321ceee5c42d5c410217f67d384eecc6ea4a2bba9bFariborz Jahanian/// \brief Set the copy inialization expression of a block var decl.
12331ceee5c42d5c410217f67d384eecc6ea4a2bba9bFariborz Jahanianvoid ASTContext::setBlockVarCopyInits(VarDecl*VD, Expr* Init) {
12341ceee5c42d5c410217f67d384eecc6ea4a2bba9bFariborz Jahanian  assert(VD && Init && "Passed null params");
1235d016ec204c54d766c4561d43548551ffde0e6b43Fariborz Jahanian  assert(VD->hasAttr<BlocksAttr>() &&
1236d016ec204c54d766c4561d43548551ffde0e6b43Fariborz Jahanian         "setBlockVarCopyInits - not __block var");
12371ceee5c42d5c410217f67d384eecc6ea4a2bba9bFariborz Jahanian  BlockVarCopyInits[VD] = Init;
12381ceee5c42d5c410217f67d384eecc6ea4a2bba9bFariborz Jahanian}
12391ceee5c42d5c410217f67d384eecc6ea4a2bba9bFariborz Jahanian
1240a93c934af4fbf97cbe8e649d82e68ccacfe57c95John McCall/// \brief Allocate an uninitialized TypeSourceInfo.
1241b17166c8077cd900cca83a895c43b30ea6660598Argyrios Kyrtzidis///
1242a93c934af4fbf97cbe8e649d82e68ccacfe57c95John McCall/// The caller should initialize the memory held by TypeSourceInfo using
1243b17166c8077cd900cca83a895c43b30ea6660598Argyrios Kyrtzidis/// the TypeLoc wrappers.
1244b17166c8077cd900cca83a895c43b30ea6660598Argyrios Kyrtzidis///
1245b17166c8077cd900cca83a895c43b30ea6660598Argyrios Kyrtzidis/// \param T the type that will be the basis for type source info. This type
1246b17166c8077cd900cca83a895c43b30ea6660598Argyrios Kyrtzidis/// should refer to how the declarator was written in source code, not to
1247b17166c8077cd900cca83a895c43b30ea6660598Argyrios Kyrtzidis/// what type semantic analysis resolved the declarator to.
1248a93c934af4fbf97cbe8e649d82e68ccacfe57c95John McCallTypeSourceInfo *ASTContext::CreateTypeSourceInfo(QualType T,
12494ba2a17694148e16eaa8d3917f657ffcd3667be4Jay Foad                                                 unsigned DataSize) const {
1250109de5ead1dfcb3bc985cddb8cb3ed5bcecad88dJohn McCall  if (!DataSize)
1251109de5ead1dfcb3bc985cddb8cb3ed5bcecad88dJohn McCall    DataSize = TypeLoc::getFullDataSizeForType(T);
1252109de5ead1dfcb3bc985cddb8cb3ed5bcecad88dJohn McCall  else
1253109de5ead1dfcb3bc985cddb8cb3ed5bcecad88dJohn McCall    assert(DataSize == TypeLoc::getFullDataSizeForType(T) &&
1254a93c934af4fbf97cbe8e649d82e68ccacfe57c95John McCall           "incorrect data size provided to CreateTypeSourceInfo!");
1255109de5ead1dfcb3bc985cddb8cb3ed5bcecad88dJohn McCall
1256a93c934af4fbf97cbe8e649d82e68ccacfe57c95John McCall  TypeSourceInfo *TInfo =
1257a93c934af4fbf97cbe8e649d82e68ccacfe57c95John McCall    (TypeSourceInfo*)BumpAlloc.Allocate(sizeof(TypeSourceInfo) + DataSize, 8);
1258a93c934af4fbf97cbe8e649d82e68ccacfe57c95John McCall  new (TInfo) TypeSourceInfo(T);
1259a93c934af4fbf97cbe8e649d82e68ccacfe57c95John McCall  return TInfo;
1260b17166c8077cd900cca83a895c43b30ea6660598Argyrios Kyrtzidis}
1261b17166c8077cd900cca83a895c43b30ea6660598Argyrios Kyrtzidis
1262a93c934af4fbf97cbe8e649d82e68ccacfe57c95John McCallTypeSourceInfo *ASTContext::getTrivialTypeSourceInfo(QualType T,
12636952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor                                                     SourceLocation L) const {
1264a93c934af4fbf97cbe8e649d82e68ccacfe57c95John McCall  TypeSourceInfo *DI = CreateTypeSourceInfo(T);
1265c21c7e9c2cded68f91be15be6847c9649242dc17Douglas Gregor  DI->getTypeLoc().initialize(const_cast<ASTContext &>(*this), L);
1266a4eb74d4dfe126c686dc708fec444c85ffb73b47John McCall  return DI;
1267a4eb74d4dfe126c686dc708fec444c85ffb73b47John McCall}
1268a4eb74d4dfe126c686dc708fec444c85ffb73b47John McCall
1269b2dbbb99e12806eaaf53b7ccabc32f42b5719443Daniel Dunbarconst ASTRecordLayout &
12704ba2a17694148e16eaa8d3917f657ffcd3667be4Jay FoadASTContext::getASTObjCInterfaceLayout(const ObjCInterfaceDecl *D) const {
1271b2dbbb99e12806eaaf53b7ccabc32f42b5719443Daniel Dunbar  return getObjCLayout(D, 0);
1272b2dbbb99e12806eaaf53b7ccabc32f42b5719443Daniel Dunbar}
1273b2dbbb99e12806eaaf53b7ccabc32f42b5719443Daniel Dunbar
1274b2dbbb99e12806eaaf53b7ccabc32f42b5719443Daniel Dunbarconst ASTRecordLayout &
12754ba2a17694148e16eaa8d3917f657ffcd3667be4Jay FoadASTContext::getASTObjCImplementationLayout(
12764ba2a17694148e16eaa8d3917f657ffcd3667be4Jay Foad                                        const ObjCImplementationDecl *D) const {
1277b2dbbb99e12806eaaf53b7ccabc32f42b5719443Daniel Dunbar  return getObjCLayout(D->getClassInterface(), D);
1278b2dbbb99e12806eaaf53b7ccabc32f42b5719443Daniel Dunbar}
1279b2dbbb99e12806eaaf53b7ccabc32f42b5719443Daniel Dunbar
1280a7674d8a9a69f3f6fe16e70cf2a3b2b15fb7c43dChris Lattner//===----------------------------------------------------------------------===//
1281a7674d8a9a69f3f6fe16e70cf2a3b2b15fb7c43dChris Lattner//                   Type creation/memoization methods
1282a7674d8a9a69f3f6fe16e70cf2a3b2b15fb7c43dChris Lattner//===----------------------------------------------------------------------===//
1283a7674d8a9a69f3f6fe16e70cf2a3b2b15fb7c43dChris Lattner
12844ba2a17694148e16eaa8d3917f657ffcd3667be4Jay FoadQualType
12853b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCallASTContext::getExtQualType(const Type *baseType, Qualifiers quals) const {
12863b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall  unsigned fastQuals = quals.getFastQualifiers();
12873b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall  quals.removeFastQualifiers();
12880953e767ff7817f97b3ab20896b229891eeff45bJohn McCall
12890953e767ff7817f97b3ab20896b229891eeff45bJohn McCall  // Check if we've already instantiated this type.
12900953e767ff7817f97b3ab20896b229891eeff45bJohn McCall  llvm::FoldingSetNodeID ID;
12913b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall  ExtQuals::Profile(ID, baseType, quals);
12923b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall  void *insertPos = 0;
12933b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall  if (ExtQuals *eq = ExtQualNodes.FindNodeOrInsertPos(ID, insertPos)) {
12943b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall    assert(eq->getQualifiers() == quals);
12953b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall    return QualType(eq, fastQuals);
12960953e767ff7817f97b3ab20896b229891eeff45bJohn McCall  }
12970953e767ff7817f97b3ab20896b229891eeff45bJohn McCall
12983b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall  // If the base type is not canonical, make the appropriate canonical type.
12993b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall  QualType canon;
13003b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall  if (!baseType->isCanonicalUnqualified()) {
13013b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall    SplitQualType canonSplit = baseType->getCanonicalTypeInternal().split();
13023b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall    canonSplit.second.addConsistentQualifiers(quals);
13033b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall    canon = getExtQualType(canonSplit.first, canonSplit.second);
13043b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall
13053b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall    // Re-find the insert position.
13063b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall    (void) ExtQualNodes.FindNodeOrInsertPos(ID, insertPos);
13073b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall  }
13083b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall
13093b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall  ExtQuals *eq = new (*this, TypeAlignment) ExtQuals(baseType, canon, quals);
13103b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall  ExtQualNodes.InsertNode(eq, insertPos);
13113b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall  return QualType(eq, fastQuals);
13120953e767ff7817f97b3ab20896b229891eeff45bJohn McCall}
13130953e767ff7817f97b3ab20896b229891eeff45bJohn McCall
13144ba2a17694148e16eaa8d3917f657ffcd3667be4Jay FoadQualType
13154ba2a17694148e16eaa8d3917f657ffcd3667be4Jay FoadASTContext::getAddrSpaceQualType(QualType T, unsigned AddressSpace) const {
1316f52ab250ff92bc51a9ac9a8e19bd43b63a5f844fChris Lattner  QualType CanT = getCanonicalType(T);
1317f52ab250ff92bc51a9ac9a8e19bd43b63a5f844fChris Lattner  if (CanT.getAddressSpace() == AddressSpace)
1318f46699ce225811d8d9dbab9d00189a0e54469457Chris Lattner    return T;
1319b7d2553edd2532d29b98b9e76bcf6a62bc48b417Chris Lattner
13200953e767ff7817f97b3ab20896b229891eeff45bJohn McCall  // If we are composing extended qualifiers together, merge together
13210953e767ff7817f97b3ab20896b229891eeff45bJohn McCall  // into one ExtQuals node.
13220953e767ff7817f97b3ab20896b229891eeff45bJohn McCall  QualifierCollector Quals;
13230953e767ff7817f97b3ab20896b229891eeff45bJohn McCall  const Type *TypeNode = Quals.strip(T);
13241eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
13250953e767ff7817f97b3ab20896b229891eeff45bJohn McCall  // If this type already has an address space specified, it cannot get
13260953e767ff7817f97b3ab20896b229891eeff45bJohn McCall  // another one.
13270953e767ff7817f97b3ab20896b229891eeff45bJohn McCall  assert(!Quals.hasAddressSpace() &&
13280953e767ff7817f97b3ab20896b229891eeff45bJohn McCall         "Type cannot be in multiple addr spaces!");
13290953e767ff7817f97b3ab20896b229891eeff45bJohn McCall  Quals.addAddressSpace(AddressSpace);
13301eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
13310953e767ff7817f97b3ab20896b229891eeff45bJohn McCall  return getExtQualType(TypeNode, Quals);
1332ebb97e98c03f8d7034bd3748a10e35f39a95c289Christopher Lamb}
1333ebb97e98c03f8d7034bd3748a10e35f39a95c289Christopher Lamb
1334b7d2553edd2532d29b98b9e76bcf6a62bc48b417Chris LattnerQualType ASTContext::getObjCGCQualType(QualType T,
13354ba2a17694148e16eaa8d3917f657ffcd3667be4Jay Foad                                       Qualifiers::GC GCAttr) const {
1336d33d9c0cc0cfdcd0b10f35a6acdfb25da4a64f19Fariborz Jahanian  QualType CanT = getCanonicalType(T);
1337b7d2553edd2532d29b98b9e76bcf6a62bc48b417Chris Lattner  if (CanT.getObjCGCAttr() == GCAttr)
1338d33d9c0cc0cfdcd0b10f35a6acdfb25da4a64f19Fariborz Jahanian    return T;
13391eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
13407f040a9d817cd1c72b565e92abff473510bf9e1dJohn McCall  if (const PointerType *ptr = T->getAs<PointerType>()) {
13417f040a9d817cd1c72b565e92abff473510bf9e1dJohn McCall    QualType Pointee = ptr->getPointeeType();
134258f9f2c884af6b72d036b746a016d8031d31cb7aSteve Naroff    if (Pointee->isAnyPointerType()) {
13434027cd1b924e29784a49085b1717f35cdd719146Fariborz Jahanian      QualType ResultType = getObjCGCQualType(Pointee, GCAttr);
13444027cd1b924e29784a49085b1717f35cdd719146Fariborz Jahanian      return getPointerType(ResultType);
13454027cd1b924e29784a49085b1717f35cdd719146Fariborz Jahanian    }
13464027cd1b924e29784a49085b1717f35cdd719146Fariborz Jahanian  }
13471eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
13480953e767ff7817f97b3ab20896b229891eeff45bJohn McCall  // If we are composing extended qualifiers together, merge together
13490953e767ff7817f97b3ab20896b229891eeff45bJohn McCall  // into one ExtQuals node.
13500953e767ff7817f97b3ab20896b229891eeff45bJohn McCall  QualifierCollector Quals;
13510953e767ff7817f97b3ab20896b229891eeff45bJohn McCall  const Type *TypeNode = Quals.strip(T);
13521eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
13530953e767ff7817f97b3ab20896b229891eeff45bJohn McCall  // If this type already has an ObjCGC specified, it cannot get
13540953e767ff7817f97b3ab20896b229891eeff45bJohn McCall  // another one.
13550953e767ff7817f97b3ab20896b229891eeff45bJohn McCall  assert(!Quals.hasObjCGCAttr() &&
13560953e767ff7817f97b3ab20896b229891eeff45bJohn McCall         "Type cannot have multiple ObjCGCs!");
13570953e767ff7817f97b3ab20896b229891eeff45bJohn McCall  Quals.addObjCGCAttr(GCAttr);
13581eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
13590953e767ff7817f97b3ab20896b229891eeff45bJohn McCall  return getExtQualType(TypeNode, Quals);
1360d33d9c0cc0cfdcd0b10f35a6acdfb25da4a64f19Fariborz Jahanian}
1361a7674d8a9a69f3f6fe16e70cf2a3b2b15fb7c43dChris Lattner
1362e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCallconst FunctionType *ASTContext::adjustFunctionType(const FunctionType *T,
1363e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall                                                   FunctionType::ExtInfo Info) {
1364e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall  if (T->getExtInfo() == Info)
1365e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall    return T;
1366e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall
1367e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall  QualType Result;
1368e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall  if (const FunctionNoProtoType *FNPT = dyn_cast<FunctionNoProtoType>(T)) {
1369e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall    Result = getFunctionNoProtoType(FNPT->getResultType(), Info);
1370e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall  } else {
1371e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall    const FunctionProtoType *FPT = cast<FunctionProtoType>(T);
1372e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall    FunctionProtoType::ExtProtoInfo EPI = FPT->getExtProtoInfo();
1373e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall    EPI.ExtInfo = Info;
1374e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall    Result = getFunctionType(FPT->getResultType(), FPT->arg_type_begin(),
1375e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall                             FPT->getNumArgs(), EPI);
1376e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall  }
1377e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall
1378e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall  return cast<FunctionType>(Result.getTypePtr());
1379e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall}
1380e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall
13815f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// getComplexType - Return the uniqued reference to the type for a complex
13825f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// number with the specified element type.
13834ba2a17694148e16eaa8d3917f657ffcd3667be4Jay FoadQualType ASTContext::getComplexType(QualType T) const {
13845f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // Unique pointers, to guarantee there is only one pointer of a particular
13855f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // structure.
13865f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  llvm::FoldingSetNodeID ID;
13875f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  ComplexType::Profile(ID, T);
13881eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
13895f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  void *InsertPos = 0;
13905f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  if (ComplexType *CT = ComplexTypes.FindNodeOrInsertPos(ID, InsertPos))
13915f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    return QualType(CT, 0);
13921eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
13935f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // If the pointee type isn't canonical, this won't be a canonical type either,
13945f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // so fill in the canonical type field.
13955f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  QualType Canonical;
1396467b27b9a24bdc823218ad1ad0e37673b6cc1e83John McCall  if (!T.isCanonical()) {
1397f52ab250ff92bc51a9ac9a8e19bd43b63a5f844fChris Lattner    Canonical = getComplexType(getCanonicalType(T));
13981eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
13995f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    // Get the new insert position for the node we care about.
14005f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    ComplexType *NewIP = ComplexTypes.FindNodeOrInsertPos(ID, InsertPos);
1401c6ed729f669044f5072a49d79041f455d971ece3Jeffrey Yasskin    assert(NewIP == 0 && "Shouldn't be in the map!"); (void)NewIP;
14025f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
14036b304a0254a13f42390b865ff5ba668a49cc58aeJohn McCall  ComplexType *New = new (*this, TypeAlignment) ComplexType(T, Canonical);
14045f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  Types.push_back(New);
14055f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  ComplexTypes.InsertNode(New, InsertPos);
14065f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  return QualType(New, 0);
14075f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
14085f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
14095f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// getPointerType - Return the uniqued reference to the type for a pointer to
14105f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// the specified type.
14114ba2a17694148e16eaa8d3917f657ffcd3667be4Jay FoadQualType ASTContext::getPointerType(QualType T) const {
14125f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // Unique pointers, to guarantee there is only one pointer of a particular
14135f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // structure.
14145f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  llvm::FoldingSetNodeID ID;
14155f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  PointerType::Profile(ID, T);
14161eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
14175f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  void *InsertPos = 0;
14185f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  if (PointerType *PT = PointerTypes.FindNodeOrInsertPos(ID, InsertPos))
14195f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    return QualType(PT, 0);
14201eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
14215f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // If the pointee type isn't canonical, this won't be a canonical type either,
14225f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // so fill in the canonical type field.
14235f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  QualType Canonical;
1424467b27b9a24bdc823218ad1ad0e37673b6cc1e83John McCall  if (!T.isCanonical()) {
1425f52ab250ff92bc51a9ac9a8e19bd43b63a5f844fChris Lattner    Canonical = getPointerType(getCanonicalType(T));
14261eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
14275f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    // Get the new insert position for the node we care about.
14285f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    PointerType *NewIP = PointerTypes.FindNodeOrInsertPos(ID, InsertPos);
1429c6ed729f669044f5072a49d79041f455d971ece3Jeffrey Yasskin    assert(NewIP == 0 && "Shouldn't be in the map!"); (void)NewIP;
14305f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
14316b304a0254a13f42390b865ff5ba668a49cc58aeJohn McCall  PointerType *New = new (*this, TypeAlignment) PointerType(T, Canonical);
14325f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  Types.push_back(New);
14335f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  PointerTypes.InsertNode(New, InsertPos);
14345f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  return QualType(New, 0);
14355f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
14365f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
14371eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// getBlockPointerType - Return the uniqued reference to the type for
14385618bd4a52c45fbbb605e3ba885663b2164db8a3Steve Naroff/// a pointer to the specified block.
14394ba2a17694148e16eaa8d3917f657ffcd3667be4Jay FoadQualType ASTContext::getBlockPointerType(QualType T) const {
1440296e8d5fdcf9946f51e866adc8d281379e51efe9Steve Naroff  assert(T->isFunctionType() && "block of function types only");
1441296e8d5fdcf9946f51e866adc8d281379e51efe9Steve Naroff  // Unique pointers, to guarantee there is only one block of a particular
14425618bd4a52c45fbbb605e3ba885663b2164db8a3Steve Naroff  // structure.
14435618bd4a52c45fbbb605e3ba885663b2164db8a3Steve Naroff  llvm::FoldingSetNodeID ID;
14445618bd4a52c45fbbb605e3ba885663b2164db8a3Steve Naroff  BlockPointerType::Profile(ID, T);
14451eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
14465618bd4a52c45fbbb605e3ba885663b2164db8a3Steve Naroff  void *InsertPos = 0;
14475618bd4a52c45fbbb605e3ba885663b2164db8a3Steve Naroff  if (BlockPointerType *PT =
14485618bd4a52c45fbbb605e3ba885663b2164db8a3Steve Naroff        BlockPointerTypes.FindNodeOrInsertPos(ID, InsertPos))
14495618bd4a52c45fbbb605e3ba885663b2164db8a3Steve Naroff    return QualType(PT, 0);
14501eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
14511eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  // If the block pointee type isn't canonical, this won't be a canonical
14525618bd4a52c45fbbb605e3ba885663b2164db8a3Steve Naroff  // type either so fill in the canonical type field.
14535618bd4a52c45fbbb605e3ba885663b2164db8a3Steve Naroff  QualType Canonical;
1454467b27b9a24bdc823218ad1ad0e37673b6cc1e83John McCall  if (!T.isCanonical()) {
14555618bd4a52c45fbbb605e3ba885663b2164db8a3Steve Naroff    Canonical = getBlockPointerType(getCanonicalType(T));
14561eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
14575618bd4a52c45fbbb605e3ba885663b2164db8a3Steve Naroff    // Get the new insert position for the node we care about.
14585618bd4a52c45fbbb605e3ba885663b2164db8a3Steve Naroff    BlockPointerType *NewIP =
14595618bd4a52c45fbbb605e3ba885663b2164db8a3Steve Naroff      BlockPointerTypes.FindNodeOrInsertPos(ID, InsertPos);
1460c6ed729f669044f5072a49d79041f455d971ece3Jeffrey Yasskin    assert(NewIP == 0 && "Shouldn't be in the map!"); (void)NewIP;
14615618bd4a52c45fbbb605e3ba885663b2164db8a3Steve Naroff  }
14626b304a0254a13f42390b865ff5ba668a49cc58aeJohn McCall  BlockPointerType *New
14636b304a0254a13f42390b865ff5ba668a49cc58aeJohn McCall    = new (*this, TypeAlignment) BlockPointerType(T, Canonical);
14645618bd4a52c45fbbb605e3ba885663b2164db8a3Steve Naroff  Types.push_back(New);
14655618bd4a52c45fbbb605e3ba885663b2164db8a3Steve Naroff  BlockPointerTypes.InsertNode(New, InsertPos);
14665618bd4a52c45fbbb605e3ba885663b2164db8a3Steve Naroff  return QualType(New, 0);
14675618bd4a52c45fbbb605e3ba885663b2164db8a3Steve Naroff}
14685618bd4a52c45fbbb605e3ba885663b2164db8a3Steve Naroff
14697c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl/// getLValueReferenceType - Return the uniqued reference to the type for an
14707c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl/// lvalue reference to the specified type.
14714ba2a17694148e16eaa8d3917f657ffcd3667be4Jay FoadQualType
14724ba2a17694148e16eaa8d3917f657ffcd3667be4Jay FoadASTContext::getLValueReferenceType(QualType T, bool SpelledAsLValue) const {
14739625e44c0252485277a340746ed8ac950686156fDouglas Gregor  assert(getCanonicalType(T) != OverloadTy &&
14749625e44c0252485277a340746ed8ac950686156fDouglas Gregor         "Unresolved overloaded function type");
14759625e44c0252485277a340746ed8ac950686156fDouglas Gregor
14765f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // Unique pointers, to guarantee there is only one pointer of a particular
14775f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // structure.
14785f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  llvm::FoldingSetNodeID ID;
147954e14c4db764c0636160d26c5bbf491637c83a76John McCall  ReferenceType::Profile(ID, T, SpelledAsLValue);
14805f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
14815f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  void *InsertPos = 0;
14827c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl  if (LValueReferenceType *RT =
14837c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl        LValueReferenceTypes.FindNodeOrInsertPos(ID, InsertPos))
14845f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    return QualType(RT, 0);
14857c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl
148654e14c4db764c0636160d26c5bbf491637c83a76John McCall  const ReferenceType *InnerRef = T->getAs<ReferenceType>();
148754e14c4db764c0636160d26c5bbf491637c83a76John McCall
14885f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // If the referencee type isn't canonical, this won't be a canonical type
14895f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // either, so fill in the canonical type field.
14905f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  QualType Canonical;
149154e14c4db764c0636160d26c5bbf491637c83a76John McCall  if (!SpelledAsLValue || InnerRef || !T.isCanonical()) {
149254e14c4db764c0636160d26c5bbf491637c83a76John McCall    QualType PointeeType = (InnerRef ? InnerRef->getPointeeType() : T);
149354e14c4db764c0636160d26c5bbf491637c83a76John McCall    Canonical = getLValueReferenceType(getCanonicalType(PointeeType));
14947c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl
14957c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl    // Get the new insert position for the node we care about.
14967c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl    LValueReferenceType *NewIP =
14977c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl      LValueReferenceTypes.FindNodeOrInsertPos(ID, InsertPos);
1498c6ed729f669044f5072a49d79041f455d971ece3Jeffrey Yasskin    assert(NewIP == 0 && "Shouldn't be in the map!"); (void)NewIP;
14997c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl  }
15007c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl
15016b304a0254a13f42390b865ff5ba668a49cc58aeJohn McCall  LValueReferenceType *New
150254e14c4db764c0636160d26c5bbf491637c83a76John McCall    = new (*this, TypeAlignment) LValueReferenceType(T, Canonical,
150354e14c4db764c0636160d26c5bbf491637c83a76John McCall                                                     SpelledAsLValue);
15047c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl  Types.push_back(New);
15057c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl  LValueReferenceTypes.InsertNode(New, InsertPos);
150654e14c4db764c0636160d26c5bbf491637c83a76John McCall
15077c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl  return QualType(New, 0);
15087c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl}
15097c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl
15107c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl/// getRValueReferenceType - Return the uniqued reference to the type for an
15117c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl/// rvalue reference to the specified type.
15124ba2a17694148e16eaa8d3917f657ffcd3667be4Jay FoadQualType ASTContext::getRValueReferenceType(QualType T) const {
15137c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl  // Unique pointers, to guarantee there is only one pointer of a particular
15147c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl  // structure.
15157c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl  llvm::FoldingSetNodeID ID;
151654e14c4db764c0636160d26c5bbf491637c83a76John McCall  ReferenceType::Profile(ID, T, false);
15177c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl
15187c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl  void *InsertPos = 0;
15197c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl  if (RValueReferenceType *RT =
15207c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl        RValueReferenceTypes.FindNodeOrInsertPos(ID, InsertPos))
15217c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl    return QualType(RT, 0);
15227c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl
152354e14c4db764c0636160d26c5bbf491637c83a76John McCall  const ReferenceType *InnerRef = T->getAs<ReferenceType>();
152454e14c4db764c0636160d26c5bbf491637c83a76John McCall
15257c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl  // If the referencee type isn't canonical, this won't be a canonical type
15267c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl  // either, so fill in the canonical type field.
15277c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl  QualType Canonical;
152854e14c4db764c0636160d26c5bbf491637c83a76John McCall  if (InnerRef || !T.isCanonical()) {
152954e14c4db764c0636160d26c5bbf491637c83a76John McCall    QualType PointeeType = (InnerRef ? InnerRef->getPointeeType() : T);
153054e14c4db764c0636160d26c5bbf491637c83a76John McCall    Canonical = getRValueReferenceType(getCanonicalType(PointeeType));
15317c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl
15325f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    // Get the new insert position for the node we care about.
15337c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl    RValueReferenceType *NewIP =
15347c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl      RValueReferenceTypes.FindNodeOrInsertPos(ID, InsertPos);
1535c6ed729f669044f5072a49d79041f455d971ece3Jeffrey Yasskin    assert(NewIP == 0 && "Shouldn't be in the map!"); (void)NewIP;
15365f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
15375f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
15386b304a0254a13f42390b865ff5ba668a49cc58aeJohn McCall  RValueReferenceType *New
15396b304a0254a13f42390b865ff5ba668a49cc58aeJohn McCall    = new (*this, TypeAlignment) RValueReferenceType(T, Canonical);
15405f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  Types.push_back(New);
15417c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl  RValueReferenceTypes.InsertNode(New, InsertPos);
15425f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  return QualType(New, 0);
15435f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
15445f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
1545f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl/// getMemberPointerType - Return the uniqued reference to the type for a
1546f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl/// member pointer to the specified type, in the specified class.
15474ba2a17694148e16eaa8d3917f657ffcd3667be4Jay FoadQualType ASTContext::getMemberPointerType(QualType T, const Type *Cls) const {
1548f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl  // Unique pointers, to guarantee there is only one pointer of a particular
1549f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl  // structure.
1550f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl  llvm::FoldingSetNodeID ID;
1551f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl  MemberPointerType::Profile(ID, T, Cls);
1552f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl
1553f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl  void *InsertPos = 0;
1554f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl  if (MemberPointerType *PT =
1555f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl      MemberPointerTypes.FindNodeOrInsertPos(ID, InsertPos))
1556f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl    return QualType(PT, 0);
1557f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl
1558f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl  // If the pointee or class type isn't canonical, this won't be a canonical
1559f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl  // type either, so fill in the canonical type field.
1560f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl  QualType Canonical;
156187c12c4a4667279dacb3d4a93c64b49148a0ff79Douglas Gregor  if (!T.isCanonical() || !Cls->isCanonicalUnqualified()) {
1562f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl    Canonical = getMemberPointerType(getCanonicalType(T),getCanonicalType(Cls));
1563f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl
1564f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl    // Get the new insert position for the node we care about.
1565f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl    MemberPointerType *NewIP =
1566f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl      MemberPointerTypes.FindNodeOrInsertPos(ID, InsertPos);
1567c6ed729f669044f5072a49d79041f455d971ece3Jeffrey Yasskin    assert(NewIP == 0 && "Shouldn't be in the map!"); (void)NewIP;
1568f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl  }
15696b304a0254a13f42390b865ff5ba668a49cc58aeJohn McCall  MemberPointerType *New
15706b304a0254a13f42390b865ff5ba668a49cc58aeJohn McCall    = new (*this, TypeAlignment) MemberPointerType(T, Cls, Canonical);
1571f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl  Types.push_back(New);
1572f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl  MemberPointerTypes.InsertNode(New, InsertPos);
1573f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl  return QualType(New, 0);
1574f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl}
1575f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl
15761eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// getConstantArrayType - Return the unique reference to the type for an
1577fb22d96692c5240fb8d611290dbf7eeed3759c73Steve Naroff/// array of the specified element type.
15781eb4433ac451dc16f4133a88af2d002ac26c58efMike StumpQualType ASTContext::getConstantArrayType(QualType EltTy,
157938aeec7299c48cb79523f7f89776fb258c84aeeaChris Lattner                                          const llvm::APInt &ArySizeIn,
1580c9406125e2cac9208098655ac8058c095c2c3a65Steve Naroff                                          ArrayType::ArraySizeModifier ASM,
158163e7d25d2e6036616b42f744fd4a39cd5f911960Abramo Bagnara                                          unsigned IndexTypeQuals) const {
1582923d56d436f750bc1f29db50e641078725558a1bSebastian Redl  assert((EltTy->isDependentType() ||
1583923d56d436f750bc1f29db50e641078725558a1bSebastian Redl          EltTy->isIncompleteType() || EltTy->isConstantSizeType()) &&
1584587cbdfd95f4b0aaccc14b31f5debe85d5daf7edEli Friedman         "Constant array of VLAs is illegal!");
1585587cbdfd95f4b0aaccc14b31f5debe85d5daf7edEli Friedman
158638aeec7299c48cb79523f7f89776fb258c84aeeaChris Lattner  // Convert the array size into a canonical width matching the pointer size for
158738aeec7299c48cb79523f7f89776fb258c84aeeaChris Lattner  // the target.
158838aeec7299c48cb79523f7f89776fb258c84aeeaChris Lattner  llvm::APInt ArySize(ArySizeIn);
15899f71a8f4c7a182a5236da9e747d57cc1d1bd24c2Jay Foad  ArySize =
1590207f4d8543529221932af82836016a2ef066c917Peter Collingbourne    ArySize.zextOrTrunc(Target.getPointerWidth(getTargetAddressSpace(EltTy)));
15911eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
15925f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  llvm::FoldingSetNodeID ID;
159363e7d25d2e6036616b42f744fd4a39cd5f911960Abramo Bagnara  ConstantArrayType::Profile(ID, EltTy, ArySize, ASM, IndexTypeQuals);
15941eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
15955f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  void *InsertPos = 0;
15961eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  if (ConstantArrayType *ATP =
15977192f8e9592729882a09d84d77838db26e39ebd4Ted Kremenek      ConstantArrayTypes.FindNodeOrInsertPos(ID, InsertPos))
15985f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    return QualType(ATP, 0);
15991eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
16003b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall  // If the element type isn't canonical or has qualifiers, this won't
16013b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall  // be a canonical type either, so fill in the canonical type field.
16023b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall  QualType Canon;
16033b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall  if (!EltTy.isCanonical() || EltTy.hasLocalQualifiers()) {
16043b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall    SplitQualType canonSplit = getCanonicalType(EltTy).split();
16053b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall    Canon = getConstantArrayType(QualType(canonSplit.first, 0), ArySize,
160663e7d25d2e6036616b42f744fd4a39cd5f911960Abramo Bagnara                                 ASM, IndexTypeQuals);
16073b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall    Canon = getQualifiedType(Canon, canonSplit.second);
16083b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall
16095f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    // Get the new insert position for the node we care about.
16101eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    ConstantArrayType *NewIP =
16117192f8e9592729882a09d84d77838db26e39ebd4Ted Kremenek      ConstantArrayTypes.FindNodeOrInsertPos(ID, InsertPos);
1612c6ed729f669044f5072a49d79041f455d971ece3Jeffrey Yasskin    assert(NewIP == 0 && "Shouldn't be in the map!"); (void)NewIP;
16135f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
16141eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
16156b304a0254a13f42390b865ff5ba668a49cc58aeJohn McCall  ConstantArrayType *New = new(*this,TypeAlignment)
161663e7d25d2e6036616b42f744fd4a39cd5f911960Abramo Bagnara    ConstantArrayType(EltTy, Canon, ArySize, ASM, IndexTypeQuals);
16177192f8e9592729882a09d84d77838db26e39ebd4Ted Kremenek  ConstantArrayTypes.InsertNode(New, InsertPos);
16185f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  Types.push_back(New);
16195f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  return QualType(New, 0);
16205f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
16215f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
1622ce8890371fcdb983ae487c87fa40606a34896ff7John McCall/// getVariableArrayDecayedType - Turns the given type, which may be
1623ce8890371fcdb983ae487c87fa40606a34896ff7John McCall/// variably-modified, into the corresponding type with all the known
1624ce8890371fcdb983ae487c87fa40606a34896ff7John McCall/// sizes replaced with [*].
1625ce8890371fcdb983ae487c87fa40606a34896ff7John McCallQualType ASTContext::getVariableArrayDecayedType(QualType type) const {
1626ce8890371fcdb983ae487c87fa40606a34896ff7John McCall  // Vastly most common case.
1627ce8890371fcdb983ae487c87fa40606a34896ff7John McCall  if (!type->isVariablyModifiedType()) return type;
1628ce8890371fcdb983ae487c87fa40606a34896ff7John McCall
1629ce8890371fcdb983ae487c87fa40606a34896ff7John McCall  QualType result;
1630ce8890371fcdb983ae487c87fa40606a34896ff7John McCall
1631ce8890371fcdb983ae487c87fa40606a34896ff7John McCall  SplitQualType split = type.getSplitDesugaredType();
1632ce8890371fcdb983ae487c87fa40606a34896ff7John McCall  const Type *ty = split.first;
1633ce8890371fcdb983ae487c87fa40606a34896ff7John McCall  switch (ty->getTypeClass()) {
1634ce8890371fcdb983ae487c87fa40606a34896ff7John McCall#define TYPE(Class, Base)
1635ce8890371fcdb983ae487c87fa40606a34896ff7John McCall#define ABSTRACT_TYPE(Class, Base)
1636ce8890371fcdb983ae487c87fa40606a34896ff7John McCall#define NON_CANONICAL_TYPE(Class, Base) case Type::Class:
1637ce8890371fcdb983ae487c87fa40606a34896ff7John McCall#include "clang/AST/TypeNodes.def"
1638ce8890371fcdb983ae487c87fa40606a34896ff7John McCall    llvm_unreachable("didn't desugar past all non-canonical types?");
1639ce8890371fcdb983ae487c87fa40606a34896ff7John McCall
1640ce8890371fcdb983ae487c87fa40606a34896ff7John McCall  // These types should never be variably-modified.
1641ce8890371fcdb983ae487c87fa40606a34896ff7John McCall  case Type::Builtin:
1642ce8890371fcdb983ae487c87fa40606a34896ff7John McCall  case Type::Complex:
1643ce8890371fcdb983ae487c87fa40606a34896ff7John McCall  case Type::Vector:
1644ce8890371fcdb983ae487c87fa40606a34896ff7John McCall  case Type::ExtVector:
1645ce8890371fcdb983ae487c87fa40606a34896ff7John McCall  case Type::DependentSizedExtVector:
1646ce8890371fcdb983ae487c87fa40606a34896ff7John McCall  case Type::ObjCObject:
1647ce8890371fcdb983ae487c87fa40606a34896ff7John McCall  case Type::ObjCInterface:
1648ce8890371fcdb983ae487c87fa40606a34896ff7John McCall  case Type::ObjCObjectPointer:
1649ce8890371fcdb983ae487c87fa40606a34896ff7John McCall  case Type::Record:
1650ce8890371fcdb983ae487c87fa40606a34896ff7John McCall  case Type::Enum:
1651ce8890371fcdb983ae487c87fa40606a34896ff7John McCall  case Type::UnresolvedUsing:
1652ce8890371fcdb983ae487c87fa40606a34896ff7John McCall  case Type::TypeOfExpr:
1653ce8890371fcdb983ae487c87fa40606a34896ff7John McCall  case Type::TypeOf:
1654ce8890371fcdb983ae487c87fa40606a34896ff7John McCall  case Type::Decltype:
1655ca63c200346c0ca9e00194ec6e34a5a7b0ed9321Sean Hunt  case Type::UnaryTransform:
1656ce8890371fcdb983ae487c87fa40606a34896ff7John McCall  case Type::DependentName:
1657ce8890371fcdb983ae487c87fa40606a34896ff7John McCall  case Type::InjectedClassName:
1658ce8890371fcdb983ae487c87fa40606a34896ff7John McCall  case Type::TemplateSpecialization:
1659ce8890371fcdb983ae487c87fa40606a34896ff7John McCall  case Type::DependentTemplateSpecialization:
1660ce8890371fcdb983ae487c87fa40606a34896ff7John McCall  case Type::TemplateTypeParm:
1661ce8890371fcdb983ae487c87fa40606a34896ff7John McCall  case Type::SubstTemplateTypeParmPack:
166234b41d939a1328f484511c6002ba2456db879a29Richard Smith  case Type::Auto:
1663ce8890371fcdb983ae487c87fa40606a34896ff7John McCall  case Type::PackExpansion:
1664ce8890371fcdb983ae487c87fa40606a34896ff7John McCall    llvm_unreachable("type should never be variably-modified");
1665ce8890371fcdb983ae487c87fa40606a34896ff7John McCall
1666ce8890371fcdb983ae487c87fa40606a34896ff7John McCall  // These types can be variably-modified but should never need to
1667ce8890371fcdb983ae487c87fa40606a34896ff7John McCall  // further decay.
1668ce8890371fcdb983ae487c87fa40606a34896ff7John McCall  case Type::FunctionNoProto:
1669ce8890371fcdb983ae487c87fa40606a34896ff7John McCall  case Type::FunctionProto:
1670ce8890371fcdb983ae487c87fa40606a34896ff7John McCall  case Type::BlockPointer:
1671ce8890371fcdb983ae487c87fa40606a34896ff7John McCall  case Type::MemberPointer:
1672ce8890371fcdb983ae487c87fa40606a34896ff7John McCall    return type;
1673ce8890371fcdb983ae487c87fa40606a34896ff7John McCall
1674ce8890371fcdb983ae487c87fa40606a34896ff7John McCall  // These types can be variably-modified.  All these modifications
1675ce8890371fcdb983ae487c87fa40606a34896ff7John McCall  // preserve structure except as noted by comments.
1676ce8890371fcdb983ae487c87fa40606a34896ff7John McCall  // TODO: if we ever care about optimizing VLAs, there are no-op
1677ce8890371fcdb983ae487c87fa40606a34896ff7John McCall  // optimizations available here.
1678ce8890371fcdb983ae487c87fa40606a34896ff7John McCall  case Type::Pointer:
1679ce8890371fcdb983ae487c87fa40606a34896ff7John McCall    result = getPointerType(getVariableArrayDecayedType(
1680ce8890371fcdb983ae487c87fa40606a34896ff7John McCall                              cast<PointerType>(ty)->getPointeeType()));
1681ce8890371fcdb983ae487c87fa40606a34896ff7John McCall    break;
1682ce8890371fcdb983ae487c87fa40606a34896ff7John McCall
1683ce8890371fcdb983ae487c87fa40606a34896ff7John McCall  case Type::LValueReference: {
1684ce8890371fcdb983ae487c87fa40606a34896ff7John McCall    const LValueReferenceType *lv = cast<LValueReferenceType>(ty);
1685ce8890371fcdb983ae487c87fa40606a34896ff7John McCall    result = getLValueReferenceType(
1686ce8890371fcdb983ae487c87fa40606a34896ff7John McCall                 getVariableArrayDecayedType(lv->getPointeeType()),
1687ce8890371fcdb983ae487c87fa40606a34896ff7John McCall                                    lv->isSpelledAsLValue());
1688ce8890371fcdb983ae487c87fa40606a34896ff7John McCall    break;
1689745da3a5bb4ea35f93f50301e7fbbb7d78d3b6bbFariborz Jahanian  }
1690ce8890371fcdb983ae487c87fa40606a34896ff7John McCall
1691ce8890371fcdb983ae487c87fa40606a34896ff7John McCall  case Type::RValueReference: {
1692ce8890371fcdb983ae487c87fa40606a34896ff7John McCall    const RValueReferenceType *lv = cast<RValueReferenceType>(ty);
1693ce8890371fcdb983ae487c87fa40606a34896ff7John McCall    result = getRValueReferenceType(
1694ce8890371fcdb983ae487c87fa40606a34896ff7John McCall                 getVariableArrayDecayedType(lv->getPointeeType()));
1695ce8890371fcdb983ae487c87fa40606a34896ff7John McCall    break;
1696745da3a5bb4ea35f93f50301e7fbbb7d78d3b6bbFariborz Jahanian  }
1697745da3a5bb4ea35f93f50301e7fbbb7d78d3b6bbFariborz Jahanian
1698ce8890371fcdb983ae487c87fa40606a34896ff7John McCall  case Type::ConstantArray: {
1699ce8890371fcdb983ae487c87fa40606a34896ff7John McCall    const ConstantArrayType *cat = cast<ConstantArrayType>(ty);
1700ce8890371fcdb983ae487c87fa40606a34896ff7John McCall    result = getConstantArrayType(
1701ce8890371fcdb983ae487c87fa40606a34896ff7John McCall                 getVariableArrayDecayedType(cat->getElementType()),
1702ce8890371fcdb983ae487c87fa40606a34896ff7John McCall                                  cat->getSize(),
1703ce8890371fcdb983ae487c87fa40606a34896ff7John McCall                                  cat->getSizeModifier(),
1704ce8890371fcdb983ae487c87fa40606a34896ff7John McCall                                  cat->getIndexTypeCVRQualifiers());
1705ce8890371fcdb983ae487c87fa40606a34896ff7John McCall    break;
1706745da3a5bb4ea35f93f50301e7fbbb7d78d3b6bbFariborz Jahanian  }
1707745da3a5bb4ea35f93f50301e7fbbb7d78d3b6bbFariborz Jahanian
1708ce8890371fcdb983ae487c87fa40606a34896ff7John McCall  case Type::DependentSizedArray: {
1709ce8890371fcdb983ae487c87fa40606a34896ff7John McCall    const DependentSizedArrayType *dat = cast<DependentSizedArrayType>(ty);
1710ce8890371fcdb983ae487c87fa40606a34896ff7John McCall    result = getDependentSizedArrayType(
1711ce8890371fcdb983ae487c87fa40606a34896ff7John McCall                 getVariableArrayDecayedType(dat->getElementType()),
1712ce8890371fcdb983ae487c87fa40606a34896ff7John McCall                                        dat->getSizeExpr(),
1713ce8890371fcdb983ae487c87fa40606a34896ff7John McCall                                        dat->getSizeModifier(),
1714ce8890371fcdb983ae487c87fa40606a34896ff7John McCall                                        dat->getIndexTypeCVRQualifiers(),
1715ce8890371fcdb983ae487c87fa40606a34896ff7John McCall                                        dat->getBracketsRange());
1716ce8890371fcdb983ae487c87fa40606a34896ff7John McCall    break;
1717ce8890371fcdb983ae487c87fa40606a34896ff7John McCall  }
1718ce8890371fcdb983ae487c87fa40606a34896ff7John McCall
1719ce8890371fcdb983ae487c87fa40606a34896ff7John McCall  // Turn incomplete types into [*] types.
1720ce8890371fcdb983ae487c87fa40606a34896ff7John McCall  case Type::IncompleteArray: {
1721ce8890371fcdb983ae487c87fa40606a34896ff7John McCall    const IncompleteArrayType *iat = cast<IncompleteArrayType>(ty);
1722ce8890371fcdb983ae487c87fa40606a34896ff7John McCall    result = getVariableArrayType(
1723ce8890371fcdb983ae487c87fa40606a34896ff7John McCall                 getVariableArrayDecayedType(iat->getElementType()),
1724ce8890371fcdb983ae487c87fa40606a34896ff7John McCall                                  /*size*/ 0,
1725ce8890371fcdb983ae487c87fa40606a34896ff7John McCall                                  ArrayType::Normal,
1726ce8890371fcdb983ae487c87fa40606a34896ff7John McCall                                  iat->getIndexTypeCVRQualifiers(),
1727ce8890371fcdb983ae487c87fa40606a34896ff7John McCall                                  SourceRange());
1728ce8890371fcdb983ae487c87fa40606a34896ff7John McCall    break;
1729ce8890371fcdb983ae487c87fa40606a34896ff7John McCall  }
1730ce8890371fcdb983ae487c87fa40606a34896ff7John McCall
1731ce8890371fcdb983ae487c87fa40606a34896ff7John McCall  // Turn VLA types into [*] types.
1732ce8890371fcdb983ae487c87fa40606a34896ff7John McCall  case Type::VariableArray: {
1733ce8890371fcdb983ae487c87fa40606a34896ff7John McCall    const VariableArrayType *vat = cast<VariableArrayType>(ty);
1734ce8890371fcdb983ae487c87fa40606a34896ff7John McCall    result = getVariableArrayType(
1735ce8890371fcdb983ae487c87fa40606a34896ff7John McCall                 getVariableArrayDecayedType(vat->getElementType()),
1736ce8890371fcdb983ae487c87fa40606a34896ff7John McCall                                  /*size*/ 0,
1737ce8890371fcdb983ae487c87fa40606a34896ff7John McCall                                  ArrayType::Star,
1738ce8890371fcdb983ae487c87fa40606a34896ff7John McCall                                  vat->getIndexTypeCVRQualifiers(),
1739ce8890371fcdb983ae487c87fa40606a34896ff7John McCall                                  vat->getBracketsRange());
1740ce8890371fcdb983ae487c87fa40606a34896ff7John McCall    break;
1741ce8890371fcdb983ae487c87fa40606a34896ff7John McCall  }
1742ce8890371fcdb983ae487c87fa40606a34896ff7John McCall  }
1743ce8890371fcdb983ae487c87fa40606a34896ff7John McCall
1744ce8890371fcdb983ae487c87fa40606a34896ff7John McCall  // Apply the top-level qualifiers from the original.
1745ce8890371fcdb983ae487c87fa40606a34896ff7John McCall  return getQualifiedType(result, split.second);
1746ce8890371fcdb983ae487c87fa40606a34896ff7John McCall}
1747745da3a5bb4ea35f93f50301e7fbbb7d78d3b6bbFariborz Jahanian
1748bdbf7b030a3e0ddb95240076683830e6f78c79a5Steve Naroff/// getVariableArrayType - Returns a non-unique reference to the type for a
1749bdbf7b030a3e0ddb95240076683830e6f78c79a5Steve Naroff/// variable array of the specified element type.
17507e7eb3da052a6d80ddf2377cab0384c798f73f75Douglas GregorQualType ASTContext::getVariableArrayType(QualType EltTy,
17517e7eb3da052a6d80ddf2377cab0384c798f73f75Douglas Gregor                                          Expr *NumElts,
1752c9406125e2cac9208098655ac8058c095c2c3a65Steve Naroff                                          ArrayType::ArraySizeModifier ASM,
175363e7d25d2e6036616b42f744fd4a39cd5f911960Abramo Bagnara                                          unsigned IndexTypeQuals,
17544ba2a17694148e16eaa8d3917f657ffcd3667be4Jay Foad                                          SourceRange Brackets) const {
1755c5773c4b8ce1ed6ed5c7112c9020c954a47dce96Eli Friedman  // Since we don't unique expressions, it isn't possible to unique VLA's
1756c5773c4b8ce1ed6ed5c7112c9020c954a47dce96Eli Friedman  // that have an expression provided for their size.
17573b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall  QualType Canon;
1758715e9c8a39437347e838aa108df443fe1086d359Douglas Gregor
17593b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall  // Be sure to pull qualifiers off the element type.
17603b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall  if (!EltTy.isCanonical() || EltTy.hasLocalQualifiers()) {
17613b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall    SplitQualType canonSplit = getCanonicalType(EltTy).split();
17623b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall    Canon = getVariableArrayType(QualType(canonSplit.first, 0), NumElts, ASM,
176363e7d25d2e6036616b42f744fd4a39cd5f911960Abramo Bagnara                                 IndexTypeQuals, Brackets);
17643b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall    Canon = getQualifiedType(Canon, canonSplit.second);
1765715e9c8a39437347e838aa108df443fe1086d359Douglas Gregor  }
1766715e9c8a39437347e838aa108df443fe1086d359Douglas Gregor
17676b304a0254a13f42390b865ff5ba668a49cc58aeJohn McCall  VariableArrayType *New = new(*this, TypeAlignment)
176863e7d25d2e6036616b42f744fd4a39cd5f911960Abramo Bagnara    VariableArrayType(EltTy, Canon, NumElts, ASM, IndexTypeQuals, Brackets);
1769c5773c4b8ce1ed6ed5c7112c9020c954a47dce96Eli Friedman
1770c5773c4b8ce1ed6ed5c7112c9020c954a47dce96Eli Friedman  VariableArrayTypes.push_back(New);
1771c5773c4b8ce1ed6ed5c7112c9020c954a47dce96Eli Friedman  Types.push_back(New);
1772c5773c4b8ce1ed6ed5c7112c9020c954a47dce96Eli Friedman  return QualType(New, 0);
1773c5773c4b8ce1ed6ed5c7112c9020c954a47dce96Eli Friedman}
1774c5773c4b8ce1ed6ed5c7112c9020c954a47dce96Eli Friedman
1775898574e7496ba8fd76290079d3a9d06954992734Douglas Gregor/// getDependentSizedArrayType - Returns a non-unique reference to
1776898574e7496ba8fd76290079d3a9d06954992734Douglas Gregor/// the type for a dependently-sized array of the specified element
177704d4beee4b86af20a9e4457023d3925cab8f9908Douglas Gregor/// type.
17783b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCallQualType ASTContext::getDependentSizedArrayType(QualType elementType,
17793b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall                                                Expr *numElements,
1780898574e7496ba8fd76290079d3a9d06954992734Douglas Gregor                                                ArrayType::ArraySizeModifier ASM,
17813b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall                                                unsigned elementTypeQuals,
17823b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall                                                SourceRange brackets) const {
17833b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall  assert((!numElements || numElements->isTypeDependent() ||
17843b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall          numElements->isValueDependent()) &&
1785898574e7496ba8fd76290079d3a9d06954992734Douglas Gregor         "Size must be type- or value-dependent!");
1786898574e7496ba8fd76290079d3a9d06954992734Douglas Gregor
17873b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall  // Dependently-sized array types that do not have a specified number
17883b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall  // of elements will have their sizes deduced from a dependent
17893b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall  // initializer.  We do no canonicalization here at all, which is okay
17903b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall  // because they can't be used in most locations.
17913b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall  if (!numElements) {
17923b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall    DependentSizedArrayType *newType
17933b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall      = new (*this, TypeAlignment)
17943b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall          DependentSizedArrayType(*this, elementType, QualType(),
17953b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall                                  numElements, ASM, elementTypeQuals,
17963b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall                                  brackets);
17973b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall    Types.push_back(newType);
17983b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall    return QualType(newType, 0);
1799cb78d8852a454684c987220132cdb5e54dd00121Douglas Gregor  }
1800cb78d8852a454684c987220132cdb5e54dd00121Douglas Gregor
18013b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall  // Otherwise, we actually build a new type every time, but we
18023b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall  // also build a canonical type.
18031eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
18043b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall  SplitQualType canonElementType = getCanonicalType(elementType).split();
1805898574e7496ba8fd76290079d3a9d06954992734Douglas Gregor
18063b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall  void *insertPos = 0;
18073b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall  llvm::FoldingSetNodeID ID;
18083b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall  DependentSizedArrayType::Profile(ID, *this,
18093b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall                                   QualType(canonElementType.first, 0),
18103b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall                                   ASM, elementTypeQuals, numElements);
18113b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall
18123b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall  // Look for an existing type with these properties.
18133b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall  DependentSizedArrayType *canonTy =
18143b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall    DependentSizedArrayTypes.FindNodeOrInsertPos(ID, insertPos);
18153b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall
18163b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall  // If we don't have one, build one.
18173b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall  if (!canonTy) {
18183b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall    canonTy = new (*this, TypeAlignment)
18193b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall      DependentSizedArrayType(*this, QualType(canonElementType.first, 0),
18203b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall                              QualType(), numElements, ASM, elementTypeQuals,
18213b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall                              brackets);
18223b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall    DependentSizedArrayTypes.InsertNode(canonTy, insertPos);
18233b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall    Types.push_back(canonTy);
18243b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall  }
18253b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall
18263b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall  // Apply qualifiers from the element type to the array.
18273b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall  QualType canon = getQualifiedType(QualType(canonTy,0),
18283b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall                                    canonElementType.second);
18293b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall
18303b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall  // If we didn't need extra canonicalization for the element type,
18313b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall  // then just use that as our result.
18323b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall  if (QualType(canonElementType.first, 0) == elementType)
18333b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall    return canon;
18343b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall
18353b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall  // Otherwise, we need to build a type which follows the spelling
18363b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall  // of the element type.
18373b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall  DependentSizedArrayType *sugaredType
18383b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall    = new (*this, TypeAlignment)
18393b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall        DependentSizedArrayType(*this, elementType, canon, numElements,
18403b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall                                ASM, elementTypeQuals, brackets);
18413b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall  Types.push_back(sugaredType);
18423b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall  return QualType(sugaredType, 0);
18433b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall}
18443b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall
18453b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCallQualType ASTContext::getIncompleteArrayType(QualType elementType,
1846c5773c4b8ce1ed6ed5c7112c9020c954a47dce96Eli Friedman                                            ArrayType::ArraySizeModifier ASM,
18473b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall                                            unsigned elementTypeQuals) const {
1848c5773c4b8ce1ed6ed5c7112c9020c954a47dce96Eli Friedman  llvm::FoldingSetNodeID ID;
18493b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall  IncompleteArrayType::Profile(ID, elementType, ASM, elementTypeQuals);
1850c5773c4b8ce1ed6ed5c7112c9020c954a47dce96Eli Friedman
18513b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall  void *insertPos = 0;
18523b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall  if (IncompleteArrayType *iat =
18533b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall       IncompleteArrayTypes.FindNodeOrInsertPos(ID, insertPos))
18543b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall    return QualType(iat, 0);
1855c5773c4b8ce1ed6ed5c7112c9020c954a47dce96Eli Friedman
1856c5773c4b8ce1ed6ed5c7112c9020c954a47dce96Eli Friedman  // If the element type isn't canonical, this won't be a canonical type
18573b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall  // either, so fill in the canonical type field.  We also have to pull
18583b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall  // qualifiers off the element type.
18593b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall  QualType canon;
1860c5773c4b8ce1ed6ed5c7112c9020c954a47dce96Eli Friedman
18613b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall  if (!elementType.isCanonical() || elementType.hasLocalQualifiers()) {
18623b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall    SplitQualType canonSplit = getCanonicalType(elementType).split();
18633b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall    canon = getIncompleteArrayType(QualType(canonSplit.first, 0),
18643b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall                                   ASM, elementTypeQuals);
18653b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall    canon = getQualifiedType(canon, canonSplit.second);
1866c5773c4b8ce1ed6ed5c7112c9020c954a47dce96Eli Friedman
1867c5773c4b8ce1ed6ed5c7112c9020c954a47dce96Eli Friedman    // Get the new insert position for the node we care about.
18683b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall    IncompleteArrayType *existing =
18693b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall      IncompleteArrayTypes.FindNodeOrInsertPos(ID, insertPos);
18703b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall    assert(!existing && "Shouldn't be in the map!"); (void) existing;
18712bd24ba6d10f8c811c8e2a57c8397e07082ba497Ted Kremenek  }
1872c5773c4b8ce1ed6ed5c7112c9020c954a47dce96Eli Friedman
18733b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall  IncompleteArrayType *newType = new (*this, TypeAlignment)
18743b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall    IncompleteArrayType(elementType, canon, ASM, elementTypeQuals);
1875c5773c4b8ce1ed6ed5c7112c9020c954a47dce96Eli Friedman
18763b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall  IncompleteArrayTypes.InsertNode(newType, insertPos);
18773b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall  Types.push_back(newType);
18783b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall  return QualType(newType, 0);
1879fb22d96692c5240fb8d611290dbf7eeed3759c73Steve Naroff}
1880fb22d96692c5240fb8d611290dbf7eeed3759c73Steve Naroff
188173322924127c873c13101b705dd823f5539ffa5fSteve Naroff/// getVectorType - Return the unique reference to a vector type of
188273322924127c873c13101b705dd823f5539ffa5fSteve Naroff/// the specified element type and size. VectorType must be a built-in type.
188382287d19ded35248c4ce6a425ce74116a13ce44eJohn ThompsonQualType ASTContext::getVectorType(QualType vecType, unsigned NumElts,
18844ba2a17694148e16eaa8d3917f657ffcd3667be4Jay Foad                                   VectorType::VectorKind VecKind) const {
18853b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall  assert(vecType->isBuiltinType());
18861eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
18875f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // Check if we've already instantiated a vector of this type.
18885f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  llvm::FoldingSetNodeID ID;
1889e86d78cf4754a6aef2cf9a33d847aa15338e276fBob Wilson  VectorType::Profile(ID, vecType, NumElts, Type::Vector, VecKind);
1890788b0fd67e1992f23555454efcdb16a19dfefac3Chris Lattner
18915f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  void *InsertPos = 0;
18925f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  if (VectorType *VTP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos))
18935f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    return QualType(VTP, 0);
18945f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
18955f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // If the element type isn't canonical, this won't be a canonical type either,
18965f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // so fill in the canonical type field.
18975f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  QualType Canonical;
1898255210ef415b9893f0e3794e8d9a704194c12f3cDouglas Gregor  if (!vecType.isCanonical()) {
1899231da7eb3dd13007e5e40fffe48998e5ef284e06Bob Wilson    Canonical = getVectorType(getCanonicalType(vecType), NumElts, VecKind);
19001eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
19015f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    // Get the new insert position for the node we care about.
19025f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    VectorType *NewIP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos);
1903c6ed729f669044f5072a49d79041f455d971ece3Jeffrey Yasskin    assert(NewIP == 0 && "Shouldn't be in the map!"); (void)NewIP;
19045f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
19056b304a0254a13f42390b865ff5ba668a49cc58aeJohn McCall  VectorType *New = new (*this, TypeAlignment)
1906e86d78cf4754a6aef2cf9a33d847aa15338e276fBob Wilson    VectorType(vecType, NumElts, Canonical, VecKind);
19075f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  VectorTypes.InsertNode(New, InsertPos);
19085f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  Types.push_back(New);
19095f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  return QualType(New, 0);
19105f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
19115f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
1912213541a68a3e137d11d2cefb612c6cdb410d7e8eNate Begeman/// getExtVectorType - Return the unique reference to an extended vector type of
191373322924127c873c13101b705dd823f5539ffa5fSteve Naroff/// the specified element type and size. VectorType must be a built-in type.
19144ba2a17694148e16eaa8d3917f657ffcd3667be4Jay FoadQualType
19154ba2a17694148e16eaa8d3917f657ffcd3667be4Jay FoadASTContext::getExtVectorType(QualType vecType, unsigned NumElts) const {
19164ac01401b1ec602a1f58c217544d3dcb5fcbd7f1Douglas Gregor  assert(vecType->isBuiltinType() || vecType->isDependentType());
19171eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
191873322924127c873c13101b705dd823f5539ffa5fSteve Naroff  // Check if we've already instantiated a vector of this type.
191973322924127c873c13101b705dd823f5539ffa5fSteve Naroff  llvm::FoldingSetNodeID ID;
1920788b0fd67e1992f23555454efcdb16a19dfefac3Chris Lattner  VectorType::Profile(ID, vecType, NumElts, Type::ExtVector,
1921e86d78cf4754a6aef2cf9a33d847aa15338e276fBob Wilson                      VectorType::GenericVector);
192273322924127c873c13101b705dd823f5539ffa5fSteve Naroff  void *InsertPos = 0;
192373322924127c873c13101b705dd823f5539ffa5fSteve Naroff  if (VectorType *VTP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos))
192473322924127c873c13101b705dd823f5539ffa5fSteve Naroff    return QualType(VTP, 0);
192573322924127c873c13101b705dd823f5539ffa5fSteve Naroff
192673322924127c873c13101b705dd823f5539ffa5fSteve Naroff  // If the element type isn't canonical, this won't be a canonical type either,
192773322924127c873c13101b705dd823f5539ffa5fSteve Naroff  // so fill in the canonical type field.
192873322924127c873c13101b705dd823f5539ffa5fSteve Naroff  QualType Canonical;
1929467b27b9a24bdc823218ad1ad0e37673b6cc1e83John McCall  if (!vecType.isCanonical()) {
1930213541a68a3e137d11d2cefb612c6cdb410d7e8eNate Begeman    Canonical = getExtVectorType(getCanonicalType(vecType), NumElts);
19311eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
193273322924127c873c13101b705dd823f5539ffa5fSteve Naroff    // Get the new insert position for the node we care about.
193373322924127c873c13101b705dd823f5539ffa5fSteve Naroff    VectorType *NewIP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos);
1934c6ed729f669044f5072a49d79041f455d971ece3Jeffrey Yasskin    assert(NewIP == 0 && "Shouldn't be in the map!"); (void)NewIP;
193573322924127c873c13101b705dd823f5539ffa5fSteve Naroff  }
19366b304a0254a13f42390b865ff5ba668a49cc58aeJohn McCall  ExtVectorType *New = new (*this, TypeAlignment)
19376b304a0254a13f42390b865ff5ba668a49cc58aeJohn McCall    ExtVectorType(vecType, NumElts, Canonical);
193873322924127c873c13101b705dd823f5539ffa5fSteve Naroff  VectorTypes.InsertNode(New, InsertPos);
193973322924127c873c13101b705dd823f5539ffa5fSteve Naroff  Types.push_back(New);
194073322924127c873c13101b705dd823f5539ffa5fSteve Naroff  return QualType(New, 0);
194173322924127c873c13101b705dd823f5539ffa5fSteve Naroff}
194273322924127c873c13101b705dd823f5539ffa5fSteve Naroff
19434ba2a17694148e16eaa8d3917f657ffcd3667be4Jay FoadQualType
19444ba2a17694148e16eaa8d3917f657ffcd3667be4Jay FoadASTContext::getDependentSizedExtVectorType(QualType vecType,
19454ba2a17694148e16eaa8d3917f657ffcd3667be4Jay Foad                                           Expr *SizeExpr,
19464ba2a17694148e16eaa8d3917f657ffcd3667be4Jay Foad                                           SourceLocation AttrLoc) const {
19472ec09f1dc123e1942ed756e8ee4fef86451eac9eDouglas Gregor  llvm::FoldingSetNodeID ID;
19481eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  DependentSizedExtVectorType::Profile(ID, *this, getCanonicalType(vecType),
19492ec09f1dc123e1942ed756e8ee4fef86451eac9eDouglas Gregor                                       SizeExpr);
19501eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
19512ec09f1dc123e1942ed756e8ee4fef86451eac9eDouglas Gregor  void *InsertPos = 0;
19522ec09f1dc123e1942ed756e8ee4fef86451eac9eDouglas Gregor  DependentSizedExtVectorType *Canon
19532ec09f1dc123e1942ed756e8ee4fef86451eac9eDouglas Gregor    = DependentSizedExtVectorTypes.FindNodeOrInsertPos(ID, InsertPos);
19542ec09f1dc123e1942ed756e8ee4fef86451eac9eDouglas Gregor  DependentSizedExtVectorType *New;
19552ec09f1dc123e1942ed756e8ee4fef86451eac9eDouglas Gregor  if (Canon) {
19562ec09f1dc123e1942ed756e8ee4fef86451eac9eDouglas Gregor    // We already have a canonical version of this array type; use it as
19572ec09f1dc123e1942ed756e8ee4fef86451eac9eDouglas Gregor    // the canonical type for a newly-built type.
19586b304a0254a13f42390b865ff5ba668a49cc58aeJohn McCall    New = new (*this, TypeAlignment)
19596b304a0254a13f42390b865ff5ba668a49cc58aeJohn McCall      DependentSizedExtVectorType(*this, vecType, QualType(Canon, 0),
19606b304a0254a13f42390b865ff5ba668a49cc58aeJohn McCall                                  SizeExpr, AttrLoc);
19612ec09f1dc123e1942ed756e8ee4fef86451eac9eDouglas Gregor  } else {
19622ec09f1dc123e1942ed756e8ee4fef86451eac9eDouglas Gregor    QualType CanonVecTy = getCanonicalType(vecType);
19632ec09f1dc123e1942ed756e8ee4fef86451eac9eDouglas Gregor    if (CanonVecTy == vecType) {
19646b304a0254a13f42390b865ff5ba668a49cc58aeJohn McCall      New = new (*this, TypeAlignment)
19656b304a0254a13f42390b865ff5ba668a49cc58aeJohn McCall        DependentSizedExtVectorType(*this, vecType, QualType(), SizeExpr,
19666b304a0254a13f42390b865ff5ba668a49cc58aeJohn McCall                                    AttrLoc);
1967789b1f640205e81b5af250693246120f1ce9d147Douglas Gregor
1968789b1f640205e81b5af250693246120f1ce9d147Douglas Gregor      DependentSizedExtVectorType *CanonCheck
1969789b1f640205e81b5af250693246120f1ce9d147Douglas Gregor        = DependentSizedExtVectorTypes.FindNodeOrInsertPos(ID, InsertPos);
1970789b1f640205e81b5af250693246120f1ce9d147Douglas Gregor      assert(!CanonCheck && "Dependent-sized ext_vector canonical type broken");
1971789b1f640205e81b5af250693246120f1ce9d147Douglas Gregor      (void)CanonCheck;
19722ec09f1dc123e1942ed756e8ee4fef86451eac9eDouglas Gregor      DependentSizedExtVectorTypes.InsertNode(New, InsertPos);
19732ec09f1dc123e1942ed756e8ee4fef86451eac9eDouglas Gregor    } else {
19742ec09f1dc123e1942ed756e8ee4fef86451eac9eDouglas Gregor      QualType Canon = getDependentSizedExtVectorType(CanonVecTy, SizeExpr,
19752ec09f1dc123e1942ed756e8ee4fef86451eac9eDouglas Gregor                                                      SourceLocation());
19766b304a0254a13f42390b865ff5ba668a49cc58aeJohn McCall      New = new (*this, TypeAlignment)
19776b304a0254a13f42390b865ff5ba668a49cc58aeJohn McCall        DependentSizedExtVectorType(*this, vecType, Canon, SizeExpr, AttrLoc);
19782ec09f1dc123e1942ed756e8ee4fef86451eac9eDouglas Gregor    }
19792ec09f1dc123e1942ed756e8ee4fef86451eac9eDouglas Gregor  }
19801eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
19819cdda0cf8528e3d595be9bfa002f0450074beb4dDouglas Gregor  Types.push_back(New);
19829cdda0cf8528e3d595be9bfa002f0450074beb4dDouglas Gregor  return QualType(New, 0);
19839cdda0cf8528e3d595be9bfa002f0450074beb4dDouglas Gregor}
19849cdda0cf8528e3d595be9bfa002f0450074beb4dDouglas Gregor
198572564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor/// getFunctionNoProtoType - Return a K&R style C function type like 'int()'.
19865f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer///
19874ba2a17694148e16eaa8d3917f657ffcd3667be4Jay FoadQualType
19884ba2a17694148e16eaa8d3917f657ffcd3667be4Jay FoadASTContext::getFunctionNoProtoType(QualType ResultTy,
19894ba2a17694148e16eaa8d3917f657ffcd3667be4Jay Foad                                   const FunctionType::ExtInfo &Info) const {
1990cfe9af250f466e7e38becea4428990448ae07737Roman Divacky  const CallingConv DefaultCC = Info.getCC();
1991cfe9af250f466e7e38becea4428990448ae07737Roman Divacky  const CallingConv CallConv = (LangOpts.MRTD && DefaultCC == CC_Default) ?
1992cfe9af250f466e7e38becea4428990448ae07737Roman Divacky                               CC_X86StdCall : DefaultCC;
19935f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // Unique functions, to guarantee there is only one function of a particular
19945f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // structure.
19955f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  llvm::FoldingSetNodeID ID;
1996264ba48dc98f3f843935a485d5b086f7e0fdc4f1Rafael Espindola  FunctionNoProtoType::Profile(ID, ResultTy, Info);
19971eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
19985f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  void *InsertPos = 0;
19991eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  if (FunctionNoProtoType *FT =
200072564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor        FunctionNoProtoTypes.FindNodeOrInsertPos(ID, InsertPos))
20015f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    return QualType(FT, 0);
20021eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
20035f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  QualType Canonical;
2004ab8bbf4ebd3e3e6eab913cb044772a62b7581941Douglas Gregor  if (!ResultTy.isCanonical() ||
200504a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall      getCanonicalCallConv(CallConv) != CallConv) {
2006264ba48dc98f3f843935a485d5b086f7e0fdc4f1Rafael Espindola    Canonical =
2007264ba48dc98f3f843935a485d5b086f7e0fdc4f1Rafael Espindola      getFunctionNoProtoType(getCanonicalType(ResultTy),
2008264ba48dc98f3f843935a485d5b086f7e0fdc4f1Rafael Espindola                     Info.withCallingConv(getCanonicalCallConv(CallConv)));
20091eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
20105f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    // Get the new insert position for the node we care about.
201172564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor    FunctionNoProtoType *NewIP =
201272564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor      FunctionNoProtoTypes.FindNodeOrInsertPos(ID, InsertPos);
2013c6ed729f669044f5072a49d79041f455d971ece3Jeffrey Yasskin    assert(NewIP == 0 && "Shouldn't be in the map!"); (void)NewIP;
20145f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
20151eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
2016cfe9af250f466e7e38becea4428990448ae07737Roman Divacky  FunctionProtoType::ExtInfo newInfo = Info.withCallingConv(CallConv);
20176b304a0254a13f42390b865ff5ba668a49cc58aeJohn McCall  FunctionNoProtoType *New = new (*this, TypeAlignment)
2018cfe9af250f466e7e38becea4428990448ae07737Roman Divacky    FunctionNoProtoType(ResultTy, Canonical, newInfo);
20195f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  Types.push_back(New);
202072564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor  FunctionNoProtoTypes.InsertNode(New, InsertPos);
20215f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  return QualType(New, 0);
20225f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
20235f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
20245f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// getFunctionType - Return a normal function type with a typed argument
20255f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// list.  isVariadic indicates whether the argument list includes '...'.
20264ba2a17694148e16eaa8d3917f657ffcd3667be4Jay FoadQualType
20274ba2a17694148e16eaa8d3917f657ffcd3667be4Jay FoadASTContext::getFunctionType(QualType ResultTy,
20284ba2a17694148e16eaa8d3917f657ffcd3667be4Jay Foad                            const QualType *ArgArray, unsigned NumArgs,
20294ba2a17694148e16eaa8d3917f657ffcd3667be4Jay Foad                            const FunctionProtoType::ExtProtoInfo &EPI) const {
20305f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // Unique functions, to guarantee there is only one function of a particular
20315f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // structure.
20325f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  llvm::FoldingSetNodeID ID;
20338026f6d82f7fa544bc0453714fe94bca62a1196eSebastian Redl  FunctionProtoType::Profile(ID, ResultTy, ArgArray, NumArgs, EPI, *this);
20345f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
20355f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  void *InsertPos = 0;
20361eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  if (FunctionProtoType *FTP =
203772564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor        FunctionProtoTypes.FindNodeOrInsertPos(ID, InsertPos))
20385f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    return QualType(FTP, 0);
2039465226e23a3008bd68973513dda1f9e3cd27dbddSebastian Redl
2040465226e23a3008bd68973513dda1f9e3cd27dbddSebastian Redl  // Determine whether the type being created is already canonical or not.
20418b5b4099c61a136e9a1714c4d8a593febe942268Sebastian Redl  bool isCanonical= EPI.ExceptionSpecType == EST_None && ResultTy.isCanonical();
20425f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  for (unsigned i = 0; i != NumArgs && isCanonical; ++i)
204354e14c4db764c0636160d26c5bbf491637c83a76John McCall    if (!ArgArray[i].isCanonicalAsParam())
20445f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      isCanonical = false;
20455f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
2046cfe9af250f466e7e38becea4428990448ae07737Roman Divacky  const CallingConv DefaultCC = EPI.ExtInfo.getCC();
2047cfe9af250f466e7e38becea4428990448ae07737Roman Divacky  const CallingConv CallConv = (LangOpts.MRTD && DefaultCC == CC_Default) ?
2048cfe9af250f466e7e38becea4428990448ae07737Roman Divacky                               CC_X86StdCall : DefaultCC;
2049e23cf437fe76b1ed02d63c3f61b456fd48a915f5John McCall
20505f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // If this type isn't canonical, get the canonical version of it.
2051465226e23a3008bd68973513dda1f9e3cd27dbddSebastian Redl  // The exception spec is not part of the canonical type.
20525f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  QualType Canonical;
205304a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall  if (!isCanonical || getCanonicalCallConv(CallConv) != CallConv) {
20545f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner    SmallVector<QualType, 16> CanonicalArgs;
20555f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    CanonicalArgs.reserve(NumArgs);
20565f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    for (unsigned i = 0; i != NumArgs; ++i)
205754e14c4db764c0636160d26c5bbf491637c83a76John McCall      CanonicalArgs.push_back(getCanonicalParamType(ArgArray[i]));
2058465226e23a3008bd68973513dda1f9e3cd27dbddSebastian Redl
2059e23cf437fe76b1ed02d63c3f61b456fd48a915f5John McCall    FunctionProtoType::ExtProtoInfo CanonicalEPI = EPI;
20608b5b4099c61a136e9a1714c4d8a593febe942268Sebastian Redl    CanonicalEPI.ExceptionSpecType = EST_None;
20618b5b4099c61a136e9a1714c4d8a593febe942268Sebastian Redl    CanonicalEPI.NumExceptions = 0;
2062e23cf437fe76b1ed02d63c3f61b456fd48a915f5John McCall    CanonicalEPI.ExtInfo
2063e23cf437fe76b1ed02d63c3f61b456fd48a915f5John McCall      = CanonicalEPI.ExtInfo.withCallingConv(getCanonicalCallConv(CallConv));
2064e23cf437fe76b1ed02d63c3f61b456fd48a915f5John McCall
2065f52ab250ff92bc51a9ac9a8e19bd43b63a5f844fChris Lattner    Canonical = getFunctionType(getCanonicalType(ResultTy),
2066beaaccd8e2a8748f77b66e2b330fb9136937e14cJay Foad                                CanonicalArgs.data(), NumArgs,
2067e23cf437fe76b1ed02d63c3f61b456fd48a915f5John McCall                                CanonicalEPI);
2068465226e23a3008bd68973513dda1f9e3cd27dbddSebastian Redl
20695f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    // Get the new insert position for the node we care about.
207072564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor    FunctionProtoType *NewIP =
207172564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor      FunctionProtoTypes.FindNodeOrInsertPos(ID, InsertPos);
2072c6ed729f669044f5072a49d79041f455d971ece3Jeffrey Yasskin    assert(NewIP == 0 && "Shouldn't be in the map!"); (void)NewIP;
20735f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
2074465226e23a3008bd68973513dda1f9e3cd27dbddSebastian Redl
2075f85e193739c953358c865005855253af4f68a497John McCall  // FunctionProtoType objects are allocated with extra bytes after
2076f85e193739c953358c865005855253af4f68a497John McCall  // them for three variable size arrays at the end:
2077f85e193739c953358c865005855253af4f68a497John McCall  //  - parameter types
2078f85e193739c953358c865005855253af4f68a497John McCall  //  - exception types
2079f85e193739c953358c865005855253af4f68a497John McCall  //  - consumed-arguments flags
2080f85e193739c953358c865005855253af4f68a497John McCall  // Instead of the exception types, there could be a noexcept
2081f85e193739c953358c865005855253af4f68a497John McCall  // expression.
2082e23cf437fe76b1ed02d63c3f61b456fd48a915f5John McCall  size_t Size = sizeof(FunctionProtoType) +
208360618fa7f88d5162bb5b40988b6b38d4d75d6fc6Sebastian Redl                NumArgs * sizeof(QualType);
208460618fa7f88d5162bb5b40988b6b38d4d75d6fc6Sebastian Redl  if (EPI.ExceptionSpecType == EST_Dynamic)
208560618fa7f88d5162bb5b40988b6b38d4d75d6fc6Sebastian Redl    Size += EPI.NumExceptions * sizeof(QualType);
208660618fa7f88d5162bb5b40988b6b38d4d75d6fc6Sebastian Redl  else if (EPI.ExceptionSpecType == EST_ComputedNoexcept) {
20878026f6d82f7fa544bc0453714fe94bca62a1196eSebastian Redl    Size += sizeof(Expr*);
208860618fa7f88d5162bb5b40988b6b38d4d75d6fc6Sebastian Redl  }
2089f85e193739c953358c865005855253af4f68a497John McCall  if (EPI.ConsumedArguments)
2090f85e193739c953358c865005855253af4f68a497John McCall    Size += NumArgs * sizeof(bool);
2091f85e193739c953358c865005855253af4f68a497John McCall
2092e23cf437fe76b1ed02d63c3f61b456fd48a915f5John McCall  FunctionProtoType *FTP = (FunctionProtoType*) Allocate(Size, TypeAlignment);
2093cfe9af250f466e7e38becea4428990448ae07737Roman Divacky  FunctionProtoType::ExtProtoInfo newEPI = EPI;
2094cfe9af250f466e7e38becea4428990448ae07737Roman Divacky  newEPI.ExtInfo = EPI.ExtInfo.withCallingConv(CallConv);
20958026f6d82f7fa544bc0453714fe94bca62a1196eSebastian Redl  new (FTP) FunctionProtoType(ResultTy, ArgArray, NumArgs, Canonical, newEPI);
20965f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  Types.push_back(FTP);
209772564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor  FunctionProtoTypes.InsertNode(FTP, InsertPos);
20985f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  return QualType(FTP, 0);
20995f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
21005f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
21013cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall#ifndef NDEBUG
21023cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCallstatic bool NeedsInjectedClassNameType(const RecordDecl *D) {
21033cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall  if (!isa<CXXRecordDecl>(D)) return false;
21043cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall  const CXXRecordDecl *RD = cast<CXXRecordDecl>(D);
21053cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall  if (isa<ClassTemplatePartialSpecializationDecl>(RD))
21063cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall    return true;
21073cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall  if (RD->getDescribedClassTemplate() &&
21083cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall      !isa<ClassTemplateSpecializationDecl>(RD))
21093cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall    return true;
21103cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall  return false;
21113cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall}
21123cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall#endif
21133cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall
21143cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall/// getInjectedClassNameType - Return the unique reference to the
21153cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall/// injected class name type for the specified templated declaration.
21163cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCallQualType ASTContext::getInjectedClassNameType(CXXRecordDecl *Decl,
21174ba2a17694148e16eaa8d3917f657ffcd3667be4Jay Foad                                              QualType TST) const {
21183cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall  assert(NeedsInjectedClassNameType(Decl));
21193cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall  if (Decl->TypeForDecl) {
21203cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall    assert(isa<InjectedClassNameType>(Decl->TypeForDecl));
212137ffed3b7f229844cae2463ff82b527506c86c74Argyrios Kyrtzidis  } else if (CXXRecordDecl *PrevDecl = Decl->getPreviousDeclaration()) {
21223cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall    assert(PrevDecl->TypeForDecl && "previous declaration has no type");
21233cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall    Decl->TypeForDecl = PrevDecl->TypeForDecl;
21243cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall    assert(isa<InjectedClassNameType>(Decl->TypeForDecl));
21253cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall  } else {
2126f4c7371fb1d3cebcfb40abad4537bb82515704eaJohn McCall    Type *newType =
212731f17ecbef57b5679c017c375db330546b7b5145John McCall      new (*this, TypeAlignment) InjectedClassNameType(Decl, TST);
2128f4c7371fb1d3cebcfb40abad4537bb82515704eaJohn McCall    Decl->TypeForDecl = newType;
2129f4c7371fb1d3cebcfb40abad4537bb82515704eaJohn McCall    Types.push_back(newType);
21303cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall  }
21313cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall  return QualType(Decl->TypeForDecl, 0);
21323cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall}
21333cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall
21342ce52f3fb95bf544db6bd3d91a72bce7d9cceb6cDouglas Gregor/// getTypeDeclType - Return the unique reference to the type for the
21352ce52f3fb95bf544db6bd3d91a72bce7d9cceb6cDouglas Gregor/// specified type declaration.
21364ba2a17694148e16eaa8d3917f657ffcd3667be4Jay FoadQualType ASTContext::getTypeDeclTypeSlow(const TypeDecl *Decl) const {
21371e6759e9e33dcaa73ce14c8a908ac9f87ac16463Argyrios Kyrtzidis  assert(Decl && "Passed null for Decl param");
2138becb8d5a6ab5103393eac5344ae69bcb860601ddJohn McCall  assert(!Decl->TypeForDecl && "TypeForDecl present in slow case");
21391eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
2140162e1c1b487352434552147967c3dd296ebee2f7Richard Smith  if (const TypedefNameDecl *Typedef = dyn_cast<TypedefNameDecl>(Decl))
21412ce52f3fb95bf544db6bd3d91a72bce7d9cceb6cDouglas Gregor    return getTypedefType(Typedef);
2142becb8d5a6ab5103393eac5344ae69bcb860601ddJohn McCall
2143becb8d5a6ab5103393eac5344ae69bcb860601ddJohn McCall  assert(!isa<TemplateTypeParmDecl>(Decl) &&
2144becb8d5a6ab5103393eac5344ae69bcb860601ddJohn McCall         "Template type parameter types are always available.");
2145becb8d5a6ab5103393eac5344ae69bcb860601ddJohn McCall
214619c8576b7328f4dc2d07682f5da552875c1912efJohn McCall  if (const RecordDecl *Record = dyn_cast<RecordDecl>(Decl)) {
2147becb8d5a6ab5103393eac5344ae69bcb860601ddJohn McCall    assert(!Record->getPreviousDeclaration() &&
2148becb8d5a6ab5103393eac5344ae69bcb860601ddJohn McCall           "struct/union has previous declaration");
2149becb8d5a6ab5103393eac5344ae69bcb860601ddJohn McCall    assert(!NeedsInjectedClassNameType(Record));
2150400f5125e2432d648f2c8a31b36a7f318a880c47Argyrios Kyrtzidis    return getRecordType(Record);
215119c8576b7328f4dc2d07682f5da552875c1912efJohn McCall  } else if (const EnumDecl *Enum = dyn_cast<EnumDecl>(Decl)) {
2152becb8d5a6ab5103393eac5344ae69bcb860601ddJohn McCall    assert(!Enum->getPreviousDeclaration() &&
2153becb8d5a6ab5103393eac5344ae69bcb860601ddJohn McCall           "enum has previous declaration");
2154400f5125e2432d648f2c8a31b36a7f318a880c47Argyrios Kyrtzidis    return getEnumType(Enum);
215519c8576b7328f4dc2d07682f5da552875c1912efJohn McCall  } else if (const UnresolvedUsingTypenameDecl *Using =
2156ed97649e9574b9d854fa4d6109c9333ae0993554John McCall               dyn_cast<UnresolvedUsingTypenameDecl>(Decl)) {
2157f4c7371fb1d3cebcfb40abad4537bb82515704eaJohn McCall    Type *newType = new (*this, TypeAlignment) UnresolvedUsingType(Using);
2158f4c7371fb1d3cebcfb40abad4537bb82515704eaJohn McCall    Decl->TypeForDecl = newType;
2159f4c7371fb1d3cebcfb40abad4537bb82515704eaJohn McCall    Types.push_back(newType);
21609fdbab3cbc2fc04bcaf5768023d83707f3151144Mike Stump  } else
2161becb8d5a6ab5103393eac5344ae69bcb860601ddJohn McCall    llvm_unreachable("TypeDecl without a type?");
216249aa7ff1245abd03e6e998e01302df31e4c6f8f6Argyrios Kyrtzidis
216349aa7ff1245abd03e6e998e01302df31e4c6f8f6Argyrios Kyrtzidis  return QualType(Decl->TypeForDecl, 0);
21642ce52f3fb95bf544db6bd3d91a72bce7d9cceb6cDouglas Gregor}
21652ce52f3fb95bf544db6bd3d91a72bce7d9cceb6cDouglas Gregor
21665f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// getTypedefType - Return the unique reference to the type for the
2167162e1c1b487352434552147967c3dd296ebee2f7Richard Smith/// specified typedef name decl.
21689763e221e16026ddf487d2564ed349d2c874a1a1Argyrios KyrtzidisQualType
2169162e1c1b487352434552147967c3dd296ebee2f7Richard SmithASTContext::getTypedefType(const TypedefNameDecl *Decl,
2170162e1c1b487352434552147967c3dd296ebee2f7Richard Smith                           QualType Canonical) const {
21715f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  if (Decl->TypeForDecl) return QualType(Decl->TypeForDecl, 0);
21721eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
21739763e221e16026ddf487d2564ed349d2c874a1a1Argyrios Kyrtzidis  if (Canonical.isNull())
21749763e221e16026ddf487d2564ed349d2c874a1a1Argyrios Kyrtzidis    Canonical = getCanonicalType(Decl->getUnderlyingType());
2175f4c7371fb1d3cebcfb40abad4537bb82515704eaJohn McCall  TypedefType *newType = new(*this, TypeAlignment)
21766b304a0254a13f42390b865ff5ba668a49cc58aeJohn McCall    TypedefType(Type::Typedef, Decl, Canonical);
2177f4c7371fb1d3cebcfb40abad4537bb82515704eaJohn McCall  Decl->TypeForDecl = newType;
2178f4c7371fb1d3cebcfb40abad4537bb82515704eaJohn McCall  Types.push_back(newType);
2179f4c7371fb1d3cebcfb40abad4537bb82515704eaJohn McCall  return QualType(newType, 0);
21805f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
21815f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
21824ba2a17694148e16eaa8d3917f657ffcd3667be4Jay FoadQualType ASTContext::getRecordType(const RecordDecl *Decl) const {
2183400f5125e2432d648f2c8a31b36a7f318a880c47Argyrios Kyrtzidis  if (Decl->TypeForDecl) return QualType(Decl->TypeForDecl, 0);
2184400f5125e2432d648f2c8a31b36a7f318a880c47Argyrios Kyrtzidis
2185400f5125e2432d648f2c8a31b36a7f318a880c47Argyrios Kyrtzidis  if (const RecordDecl *PrevDecl = Decl->getPreviousDeclaration())
2186400f5125e2432d648f2c8a31b36a7f318a880c47Argyrios Kyrtzidis    if (PrevDecl->TypeForDecl)
2187400f5125e2432d648f2c8a31b36a7f318a880c47Argyrios Kyrtzidis      return QualType(Decl->TypeForDecl = PrevDecl->TypeForDecl, 0);
2188400f5125e2432d648f2c8a31b36a7f318a880c47Argyrios Kyrtzidis
2189f4c7371fb1d3cebcfb40abad4537bb82515704eaJohn McCall  RecordType *newType = new (*this, TypeAlignment) RecordType(Decl);
2190f4c7371fb1d3cebcfb40abad4537bb82515704eaJohn McCall  Decl->TypeForDecl = newType;
2191f4c7371fb1d3cebcfb40abad4537bb82515704eaJohn McCall  Types.push_back(newType);
2192f4c7371fb1d3cebcfb40abad4537bb82515704eaJohn McCall  return QualType(newType, 0);
2193400f5125e2432d648f2c8a31b36a7f318a880c47Argyrios Kyrtzidis}
2194400f5125e2432d648f2c8a31b36a7f318a880c47Argyrios Kyrtzidis
21954ba2a17694148e16eaa8d3917f657ffcd3667be4Jay FoadQualType ASTContext::getEnumType(const EnumDecl *Decl) const {
2196400f5125e2432d648f2c8a31b36a7f318a880c47Argyrios Kyrtzidis  if (Decl->TypeForDecl) return QualType(Decl->TypeForDecl, 0);
2197400f5125e2432d648f2c8a31b36a7f318a880c47Argyrios Kyrtzidis
2198400f5125e2432d648f2c8a31b36a7f318a880c47Argyrios Kyrtzidis  if (const EnumDecl *PrevDecl = Decl->getPreviousDeclaration())
2199400f5125e2432d648f2c8a31b36a7f318a880c47Argyrios Kyrtzidis    if (PrevDecl->TypeForDecl)
2200400f5125e2432d648f2c8a31b36a7f318a880c47Argyrios Kyrtzidis      return QualType(Decl->TypeForDecl = PrevDecl->TypeForDecl, 0);
2201400f5125e2432d648f2c8a31b36a7f318a880c47Argyrios Kyrtzidis
2202f4c7371fb1d3cebcfb40abad4537bb82515704eaJohn McCall  EnumType *newType = new (*this, TypeAlignment) EnumType(Decl);
2203f4c7371fb1d3cebcfb40abad4537bb82515704eaJohn McCall  Decl->TypeForDecl = newType;
2204f4c7371fb1d3cebcfb40abad4537bb82515704eaJohn McCall  Types.push_back(newType);
2205f4c7371fb1d3cebcfb40abad4537bb82515704eaJohn McCall  return QualType(newType, 0);
2206400f5125e2432d648f2c8a31b36a7f318a880c47Argyrios Kyrtzidis}
2207400f5125e2432d648f2c8a31b36a7f318a880c47Argyrios Kyrtzidis
22089d156a7b1b2771e191f2f5a45a7b7a694129463bJohn McCallQualType ASTContext::getAttributedType(AttributedType::Kind attrKind,
22099d156a7b1b2771e191f2f5a45a7b7a694129463bJohn McCall                                       QualType modifiedType,
22109d156a7b1b2771e191f2f5a45a7b7a694129463bJohn McCall                                       QualType equivalentType) {
22119d156a7b1b2771e191f2f5a45a7b7a694129463bJohn McCall  llvm::FoldingSetNodeID id;
22129d156a7b1b2771e191f2f5a45a7b7a694129463bJohn McCall  AttributedType::Profile(id, attrKind, modifiedType, equivalentType);
22139d156a7b1b2771e191f2f5a45a7b7a694129463bJohn McCall
22149d156a7b1b2771e191f2f5a45a7b7a694129463bJohn McCall  void *insertPos = 0;
22159d156a7b1b2771e191f2f5a45a7b7a694129463bJohn McCall  AttributedType *type = AttributedTypes.FindNodeOrInsertPos(id, insertPos);
22169d156a7b1b2771e191f2f5a45a7b7a694129463bJohn McCall  if (type) return QualType(type, 0);
22179d156a7b1b2771e191f2f5a45a7b7a694129463bJohn McCall
22189d156a7b1b2771e191f2f5a45a7b7a694129463bJohn McCall  QualType canon = getCanonicalType(equivalentType);
22199d156a7b1b2771e191f2f5a45a7b7a694129463bJohn McCall  type = new (*this, TypeAlignment)
22209d156a7b1b2771e191f2f5a45a7b7a694129463bJohn McCall           AttributedType(canon, attrKind, modifiedType, equivalentType);
22219d156a7b1b2771e191f2f5a45a7b7a694129463bJohn McCall
22229d156a7b1b2771e191f2f5a45a7b7a694129463bJohn McCall  Types.push_back(type);
22239d156a7b1b2771e191f2f5a45a7b7a694129463bJohn McCall  AttributedTypes.InsertNode(type, insertPos);
22249d156a7b1b2771e191f2f5a45a7b7a694129463bJohn McCall
22259d156a7b1b2771e191f2f5a45a7b7a694129463bJohn McCall  return QualType(type, 0);
22269d156a7b1b2771e191f2f5a45a7b7a694129463bJohn McCall}
22279d156a7b1b2771e191f2f5a45a7b7a694129463bJohn McCall
22289d156a7b1b2771e191f2f5a45a7b7a694129463bJohn McCall
222949a832bd499d6f61c23655f1fac99f0dd229756eJohn McCall/// \brief Retrieve a substitution-result type.
223049a832bd499d6f61c23655f1fac99f0dd229756eJohn McCallQualType
223149a832bd499d6f61c23655f1fac99f0dd229756eJohn McCallASTContext::getSubstTemplateTypeParmType(const TemplateTypeParmType *Parm,
22324ba2a17694148e16eaa8d3917f657ffcd3667be4Jay Foad                                         QualType Replacement) const {
2233467b27b9a24bdc823218ad1ad0e37673b6cc1e83John McCall  assert(Replacement.isCanonical()
223449a832bd499d6f61c23655f1fac99f0dd229756eJohn McCall         && "replacement types must always be canonical");
223549a832bd499d6f61c23655f1fac99f0dd229756eJohn McCall
223649a832bd499d6f61c23655f1fac99f0dd229756eJohn McCall  llvm::FoldingSetNodeID ID;
223749a832bd499d6f61c23655f1fac99f0dd229756eJohn McCall  SubstTemplateTypeParmType::Profile(ID, Parm, Replacement);
223849a832bd499d6f61c23655f1fac99f0dd229756eJohn McCall  void *InsertPos = 0;
223949a832bd499d6f61c23655f1fac99f0dd229756eJohn McCall  SubstTemplateTypeParmType *SubstParm
224049a832bd499d6f61c23655f1fac99f0dd229756eJohn McCall    = SubstTemplateTypeParmTypes.FindNodeOrInsertPos(ID, InsertPos);
224149a832bd499d6f61c23655f1fac99f0dd229756eJohn McCall
224249a832bd499d6f61c23655f1fac99f0dd229756eJohn McCall  if (!SubstParm) {
224349a832bd499d6f61c23655f1fac99f0dd229756eJohn McCall    SubstParm = new (*this, TypeAlignment)
224449a832bd499d6f61c23655f1fac99f0dd229756eJohn McCall      SubstTemplateTypeParmType(Parm, Replacement);
224549a832bd499d6f61c23655f1fac99f0dd229756eJohn McCall    Types.push_back(SubstParm);
224649a832bd499d6f61c23655f1fac99f0dd229756eJohn McCall    SubstTemplateTypeParmTypes.InsertNode(SubstParm, InsertPos);
224749a832bd499d6f61c23655f1fac99f0dd229756eJohn McCall  }
224849a832bd499d6f61c23655f1fac99f0dd229756eJohn McCall
224949a832bd499d6f61c23655f1fac99f0dd229756eJohn McCall  return QualType(SubstParm, 0);
225049a832bd499d6f61c23655f1fac99f0dd229756eJohn McCall}
225149a832bd499d6f61c23655f1fac99f0dd229756eJohn McCall
2252c3069d618f4661d923cb1b5c4525b082fce73b04Douglas Gregor/// \brief Retrieve a
2253c3069d618f4661d923cb1b5c4525b082fce73b04Douglas GregorQualType ASTContext::getSubstTemplateTypeParmPackType(
2254c3069d618f4661d923cb1b5c4525b082fce73b04Douglas Gregor                                          const TemplateTypeParmType *Parm,
2255c3069d618f4661d923cb1b5c4525b082fce73b04Douglas Gregor                                              const TemplateArgument &ArgPack) {
2256c3069d618f4661d923cb1b5c4525b082fce73b04Douglas Gregor#ifndef NDEBUG
2257c3069d618f4661d923cb1b5c4525b082fce73b04Douglas Gregor  for (TemplateArgument::pack_iterator P = ArgPack.pack_begin(),
2258c3069d618f4661d923cb1b5c4525b082fce73b04Douglas Gregor                                    PEnd = ArgPack.pack_end();
2259c3069d618f4661d923cb1b5c4525b082fce73b04Douglas Gregor       P != PEnd; ++P) {
2260c3069d618f4661d923cb1b5c4525b082fce73b04Douglas Gregor    assert(P->getKind() == TemplateArgument::Type &&"Pack contains a non-type");
2261c3069d618f4661d923cb1b5c4525b082fce73b04Douglas Gregor    assert(P->getAsType().isCanonical() && "Pack contains non-canonical type");
2262c3069d618f4661d923cb1b5c4525b082fce73b04Douglas Gregor  }
2263c3069d618f4661d923cb1b5c4525b082fce73b04Douglas Gregor#endif
2264c3069d618f4661d923cb1b5c4525b082fce73b04Douglas Gregor
2265c3069d618f4661d923cb1b5c4525b082fce73b04Douglas Gregor  llvm::FoldingSetNodeID ID;
2266c3069d618f4661d923cb1b5c4525b082fce73b04Douglas Gregor  SubstTemplateTypeParmPackType::Profile(ID, Parm, ArgPack);
2267c3069d618f4661d923cb1b5c4525b082fce73b04Douglas Gregor  void *InsertPos = 0;
2268c3069d618f4661d923cb1b5c4525b082fce73b04Douglas Gregor  if (SubstTemplateTypeParmPackType *SubstParm
2269c3069d618f4661d923cb1b5c4525b082fce73b04Douglas Gregor        = SubstTemplateTypeParmPackTypes.FindNodeOrInsertPos(ID, InsertPos))
2270c3069d618f4661d923cb1b5c4525b082fce73b04Douglas Gregor    return QualType(SubstParm, 0);
2271c3069d618f4661d923cb1b5c4525b082fce73b04Douglas Gregor
2272c3069d618f4661d923cb1b5c4525b082fce73b04Douglas Gregor  QualType Canon;
2273c3069d618f4661d923cb1b5c4525b082fce73b04Douglas Gregor  if (!Parm->isCanonicalUnqualified()) {
2274c3069d618f4661d923cb1b5c4525b082fce73b04Douglas Gregor    Canon = getCanonicalType(QualType(Parm, 0));
2275c3069d618f4661d923cb1b5c4525b082fce73b04Douglas Gregor    Canon = getSubstTemplateTypeParmPackType(cast<TemplateTypeParmType>(Canon),
2276c3069d618f4661d923cb1b5c4525b082fce73b04Douglas Gregor                                             ArgPack);
2277c3069d618f4661d923cb1b5c4525b082fce73b04Douglas Gregor    SubstTemplateTypeParmPackTypes.FindNodeOrInsertPos(ID, InsertPos);
2278c3069d618f4661d923cb1b5c4525b082fce73b04Douglas Gregor  }
2279c3069d618f4661d923cb1b5c4525b082fce73b04Douglas Gregor
2280c3069d618f4661d923cb1b5c4525b082fce73b04Douglas Gregor  SubstTemplateTypeParmPackType *SubstParm
2281c3069d618f4661d923cb1b5c4525b082fce73b04Douglas Gregor    = new (*this, TypeAlignment) SubstTemplateTypeParmPackType(Parm, Canon,
2282c3069d618f4661d923cb1b5c4525b082fce73b04Douglas Gregor                                                               ArgPack);
2283c3069d618f4661d923cb1b5c4525b082fce73b04Douglas Gregor  Types.push_back(SubstParm);
2284c3069d618f4661d923cb1b5c4525b082fce73b04Douglas Gregor  SubstTemplateTypeParmTypes.InsertNode(SubstParm, InsertPos);
2285c3069d618f4661d923cb1b5c4525b082fce73b04Douglas Gregor  return QualType(SubstParm, 0);
2286c3069d618f4661d923cb1b5c4525b082fce73b04Douglas Gregor}
2287c3069d618f4661d923cb1b5c4525b082fce73b04Douglas Gregor
2288fab9d67cebb87be968e7ae31a3b549a5279b5d51Douglas Gregor/// \brief Retrieve the template type parameter type for a template
22891eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// parameter or parameter pack with the given depth, index, and (optionally)
229076e4ce42a30cee4dc40ce7c6014874fbc4f9baa7Anders Carlsson/// name.
22911eb4433ac451dc16f4133a88af2d002ac26c58efMike StumpQualType ASTContext::getTemplateTypeParmType(unsigned Depth, unsigned Index,
229276e4ce42a30cee4dc40ce7c6014874fbc4f9baa7Anders Carlsson                                             bool ParameterPack,
22934fb86f8c4585e53c21c847ad3de9e3b2de123cd9Chandler Carruth                                             TemplateTypeParmDecl *TTPDecl) const {
2294fab9d67cebb87be968e7ae31a3b549a5279b5d51Douglas Gregor  llvm::FoldingSetNodeID ID;
22954fb86f8c4585e53c21c847ad3de9e3b2de123cd9Chandler Carruth  TemplateTypeParmType::Profile(ID, Depth, Index, ParameterPack, TTPDecl);
2296fab9d67cebb87be968e7ae31a3b549a5279b5d51Douglas Gregor  void *InsertPos = 0;
22971eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  TemplateTypeParmType *TypeParm
2298fab9d67cebb87be968e7ae31a3b549a5279b5d51Douglas Gregor    = TemplateTypeParmTypes.FindNodeOrInsertPos(ID, InsertPos);
2299fab9d67cebb87be968e7ae31a3b549a5279b5d51Douglas Gregor
2300fab9d67cebb87be968e7ae31a3b549a5279b5d51Douglas Gregor  if (TypeParm)
2301fab9d67cebb87be968e7ae31a3b549a5279b5d51Douglas Gregor    return QualType(TypeParm, 0);
23021eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
23034fb86f8c4585e53c21c847ad3de9e3b2de123cd9Chandler Carruth  if (TTPDecl) {
230476e4ce42a30cee4dc40ce7c6014874fbc4f9baa7Anders Carlsson    QualType Canon = getTemplateTypeParmType(Depth, Index, ParameterPack);
23054fb86f8c4585e53c21c847ad3de9e3b2de123cd9Chandler Carruth    TypeParm = new (*this, TypeAlignment) TemplateTypeParmType(TTPDecl, Canon);
2306789b1f640205e81b5af250693246120f1ce9d147Douglas Gregor
2307789b1f640205e81b5af250693246120f1ce9d147Douglas Gregor    TemplateTypeParmType *TypeCheck
2308789b1f640205e81b5af250693246120f1ce9d147Douglas Gregor      = TemplateTypeParmTypes.FindNodeOrInsertPos(ID, InsertPos);
2309789b1f640205e81b5af250693246120f1ce9d147Douglas Gregor    assert(!TypeCheck && "Template type parameter canonical type broken");
2310789b1f640205e81b5af250693246120f1ce9d147Douglas Gregor    (void)TypeCheck;
231176e4ce42a30cee4dc40ce7c6014874fbc4f9baa7Anders Carlsson  } else
23126b304a0254a13f42390b865ff5ba668a49cc58aeJohn McCall    TypeParm = new (*this, TypeAlignment)
23136b304a0254a13f42390b865ff5ba668a49cc58aeJohn McCall      TemplateTypeParmType(Depth, Index, ParameterPack);
2314fab9d67cebb87be968e7ae31a3b549a5279b5d51Douglas Gregor
2315fab9d67cebb87be968e7ae31a3b549a5279b5d51Douglas Gregor  Types.push_back(TypeParm);
2316fab9d67cebb87be968e7ae31a3b549a5279b5d51Douglas Gregor  TemplateTypeParmTypes.InsertNode(TypeParm, InsertPos);
2317fab9d67cebb87be968e7ae31a3b549a5279b5d51Douglas Gregor
2318fab9d67cebb87be968e7ae31a3b549a5279b5d51Douglas Gregor  return QualType(TypeParm, 0);
2319fab9d67cebb87be968e7ae31a3b549a5279b5d51Douglas Gregor}
2320fab9d67cebb87be968e7ae31a3b549a5279b5d51Douglas Gregor
23213cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCallTypeSourceInfo *
23223cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCallASTContext::getTemplateSpecializationTypeInfo(TemplateName Name,
23233cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall                                              SourceLocation NameLoc,
23243cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall                                        const TemplateArgumentListInfo &Args,
23253e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith                                              QualType Underlying) const {
23267c3179cf463c3b3b8c21dbb955f933ba50b74f28Douglas Gregor  assert(!Name.getAsDependentTemplateName() &&
23277c3179cf463c3b3b8c21dbb955f933ba50b74f28Douglas Gregor         "No dependent template names here!");
23283e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith  QualType TST = getTemplateSpecializationType(Name, Args, Underlying);
23293cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall
23303cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall  TypeSourceInfo *DI = CreateTypeSourceInfo(TST);
23313cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall  TemplateSpecializationTypeLoc TL
23323cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall    = cast<TemplateSpecializationTypeLoc>(DI->getTypeLoc());
23333cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall  TL.setTemplateNameLoc(NameLoc);
23343cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall  TL.setLAngleLoc(Args.getLAngleLoc());
23353cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall  TL.setRAngleLoc(Args.getRAngleLoc());
23363cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall  for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i)
23373cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall    TL.setArgLocInfo(i, Args[i].getLocInfo());
23383cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall  return DI;
23393cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall}
23403cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall
23411eb4433ac451dc16f4133a88af2d002ac26c58efMike StumpQualType
23427532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas GregorASTContext::getTemplateSpecializationType(TemplateName Template,
2343d5532b6cfff2977e0c59fa6ead7f7973984a620dJohn McCall                                          const TemplateArgumentListInfo &Args,
23443e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith                                          QualType Underlying) const {
23457c3179cf463c3b3b8c21dbb955f933ba50b74f28Douglas Gregor  assert(!Template.getAsDependentTemplateName() &&
23467c3179cf463c3b3b8c21dbb955f933ba50b74f28Douglas Gregor         "No dependent template names here!");
23477c3179cf463c3b3b8c21dbb955f933ba50b74f28Douglas Gregor
2348d5532b6cfff2977e0c59fa6ead7f7973984a620dJohn McCall  unsigned NumArgs = Args.size();
2349d5532b6cfff2977e0c59fa6ead7f7973984a620dJohn McCall
23505f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner  SmallVector<TemplateArgument, 4> ArgVec;
2351833ca991c1bfc967f0995974ca86f66ba1f666b5John McCall  ArgVec.reserve(NumArgs);
2352833ca991c1bfc967f0995974ca86f66ba1f666b5John McCall  for (unsigned i = 0; i != NumArgs; ++i)
2353833ca991c1bfc967f0995974ca86f66ba1f666b5John McCall    ArgVec.push_back(Args[i].getArgument());
2354833ca991c1bfc967f0995974ca86f66ba1f666b5John McCall
235531f17ecbef57b5679c017c375db330546b7b5145John McCall  return getTemplateSpecializationType(Template, ArgVec.data(), NumArgs,
23563e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith                                       Underlying);
2357833ca991c1bfc967f0995974ca86f66ba1f666b5John McCall}
2358833ca991c1bfc967f0995974ca86f66ba1f666b5John McCall
2359833ca991c1bfc967f0995974ca86f66ba1f666b5John McCallQualType
2360833ca991c1bfc967f0995974ca86f66ba1f666b5John McCallASTContext::getTemplateSpecializationType(TemplateName Template,
23617532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor                                          const TemplateArgument *Args,
23627532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor                                          unsigned NumArgs,
23633e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith                                          QualType Underlying) const {
23647c3179cf463c3b3b8c21dbb955f933ba50b74f28Douglas Gregor  assert(!Template.getAsDependentTemplateName() &&
23657c3179cf463c3b3b8c21dbb955f933ba50b74f28Douglas Gregor         "No dependent template names here!");
23660f0ea2a96534c615ff5fdd81363989b23cf2164aDouglas Gregor  // Look through qualified template names.
23670f0ea2a96534c615ff5fdd81363989b23cf2164aDouglas Gregor  if (QualifiedTemplateName *QTN = Template.getAsQualifiedTemplateName())
23680f0ea2a96534c615ff5fdd81363989b23cf2164aDouglas Gregor    Template = TemplateName(QTN->getTemplateDecl());
23697c3179cf463c3b3b8c21dbb955f933ba50b74f28Douglas Gregor
23703e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith  bool isTypeAlias =
23713e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith    Template.getAsTemplateDecl() &&
23723e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith    isa<TypeAliasTemplateDecl>(Template.getAsTemplateDecl());
23733e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith
23743e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith  QualType CanonType;
23753e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith  if (!Underlying.isNull())
23763e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith    CanonType = getCanonicalType(Underlying);
23773e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith  else {
23783e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith    assert(!isTypeAlias &&
23793e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith           "Underlying type for template alias must be computed by caller");
23803e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith    CanonType = getCanonicalTemplateSpecializationType(Template, Args,
23813e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith                                                       NumArgs);
23823e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith  }
2383fc705b84347e6fb4746a1a7e26949f64c2f2f358Douglas Gregor
23841275ae098acda31fe0e434510c729fcfed0458a1Douglas Gregor  // Allocate the (non-canonical) template specialization type, but don't
23851275ae098acda31fe0e434510c729fcfed0458a1Douglas Gregor  // try to unique it: these types typically have location information that
23861275ae098acda31fe0e434510c729fcfed0458a1Douglas Gregor  // we don't unique and don't want to lose.
23873e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith  void *Mem = Allocate(sizeof(TemplateSpecializationType) +
23883e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith                       sizeof(TemplateArgument) * NumArgs +
23893e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith                       (isTypeAlias ? sizeof(QualType) : 0),
23906b304a0254a13f42390b865ff5ba668a49cc58aeJohn McCall                       TypeAlignment);
23911eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  TemplateSpecializationType *Spec
2392ef99001908e799c388f1363b1e607dad5f5b57d3John McCall    = new (Mem) TemplateSpecializationType(Template,
239331f17ecbef57b5679c017c375db330546b7b5145John McCall                                           Args, NumArgs,
23943e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith                                           CanonType,
23953e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith                                         isTypeAlias ? Underlying : QualType());
23961eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
239755f6b14230c94272efbbcdd89a92224c8db9f225Douglas Gregor  Types.push_back(Spec);
23981eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  return QualType(Spec, 0);
239955f6b14230c94272efbbcdd89a92224c8db9f225Douglas Gregor}
240055f6b14230c94272efbbcdd89a92224c8db9f225Douglas Gregor
24011eb4433ac451dc16f4133a88af2d002ac26c58efMike StumpQualType
24029763e221e16026ddf487d2564ed349d2c874a1a1Argyrios KyrtzidisASTContext::getCanonicalTemplateSpecializationType(TemplateName Template,
24039763e221e16026ddf487d2564ed349d2c874a1a1Argyrios Kyrtzidis                                                   const TemplateArgument *Args,
24044ba2a17694148e16eaa8d3917f657ffcd3667be4Jay Foad                                                   unsigned NumArgs) const {
24057c3179cf463c3b3b8c21dbb955f933ba50b74f28Douglas Gregor  assert(!Template.getAsDependentTemplateName() &&
24067c3179cf463c3b3b8c21dbb955f933ba50b74f28Douglas Gregor         "No dependent template names here!");
24073e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith  assert((!Template.getAsTemplateDecl() ||
24083e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith          !isa<TypeAliasTemplateDecl>(Template.getAsTemplateDecl())) &&
24093e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith         "Underlying type for template alias must be computed by caller");
24103e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith
24110f0ea2a96534c615ff5fdd81363989b23cf2164aDouglas Gregor  // Look through qualified template names.
24120f0ea2a96534c615ff5fdd81363989b23cf2164aDouglas Gregor  if (QualifiedTemplateName *QTN = Template.getAsQualifiedTemplateName())
24130f0ea2a96534c615ff5fdd81363989b23cf2164aDouglas Gregor    Template = TemplateName(QTN->getTemplateDecl());
24147c3179cf463c3b3b8c21dbb955f933ba50b74f28Douglas Gregor
24159763e221e16026ddf487d2564ed349d2c874a1a1Argyrios Kyrtzidis  // Build the canonical template specialization type.
24169763e221e16026ddf487d2564ed349d2c874a1a1Argyrios Kyrtzidis  TemplateName CanonTemplate = getCanonicalTemplateName(Template);
24175f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner  SmallVector<TemplateArgument, 4> CanonArgs;
24189763e221e16026ddf487d2564ed349d2c874a1a1Argyrios Kyrtzidis  CanonArgs.reserve(NumArgs);
24199763e221e16026ddf487d2564ed349d2c874a1a1Argyrios Kyrtzidis  for (unsigned I = 0; I != NumArgs; ++I)
24209763e221e16026ddf487d2564ed349d2c874a1a1Argyrios Kyrtzidis    CanonArgs.push_back(getCanonicalTemplateArgument(Args[I]));
24219763e221e16026ddf487d2564ed349d2c874a1a1Argyrios Kyrtzidis
24229763e221e16026ddf487d2564ed349d2c874a1a1Argyrios Kyrtzidis  // Determine whether this canonical template specialization type already
24239763e221e16026ddf487d2564ed349d2c874a1a1Argyrios Kyrtzidis  // exists.
24249763e221e16026ddf487d2564ed349d2c874a1a1Argyrios Kyrtzidis  llvm::FoldingSetNodeID ID;
24259763e221e16026ddf487d2564ed349d2c874a1a1Argyrios Kyrtzidis  TemplateSpecializationType::Profile(ID, CanonTemplate,
24269763e221e16026ddf487d2564ed349d2c874a1a1Argyrios Kyrtzidis                                      CanonArgs.data(), NumArgs, *this);
24279763e221e16026ddf487d2564ed349d2c874a1a1Argyrios Kyrtzidis
24289763e221e16026ddf487d2564ed349d2c874a1a1Argyrios Kyrtzidis  void *InsertPos = 0;
24299763e221e16026ddf487d2564ed349d2c874a1a1Argyrios Kyrtzidis  TemplateSpecializationType *Spec
24309763e221e16026ddf487d2564ed349d2c874a1a1Argyrios Kyrtzidis    = TemplateSpecializationTypes.FindNodeOrInsertPos(ID, InsertPos);
24319763e221e16026ddf487d2564ed349d2c874a1a1Argyrios Kyrtzidis
24329763e221e16026ddf487d2564ed349d2c874a1a1Argyrios Kyrtzidis  if (!Spec) {
24339763e221e16026ddf487d2564ed349d2c874a1a1Argyrios Kyrtzidis    // Allocate a new canonical template specialization type.
24349763e221e16026ddf487d2564ed349d2c874a1a1Argyrios Kyrtzidis    void *Mem = Allocate((sizeof(TemplateSpecializationType) +
24359763e221e16026ddf487d2564ed349d2c874a1a1Argyrios Kyrtzidis                          sizeof(TemplateArgument) * NumArgs),
24369763e221e16026ddf487d2564ed349d2c874a1a1Argyrios Kyrtzidis                         TypeAlignment);
24379763e221e16026ddf487d2564ed349d2c874a1a1Argyrios Kyrtzidis    Spec = new (Mem) TemplateSpecializationType(CanonTemplate,
24389763e221e16026ddf487d2564ed349d2c874a1a1Argyrios Kyrtzidis                                                CanonArgs.data(), NumArgs,
24393e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith                                                QualType(), QualType());
24409763e221e16026ddf487d2564ed349d2c874a1a1Argyrios Kyrtzidis    Types.push_back(Spec);
24419763e221e16026ddf487d2564ed349d2c874a1a1Argyrios Kyrtzidis    TemplateSpecializationTypes.InsertNode(Spec, InsertPos);
24429763e221e16026ddf487d2564ed349d2c874a1a1Argyrios Kyrtzidis  }
24439763e221e16026ddf487d2564ed349d2c874a1a1Argyrios Kyrtzidis
24449763e221e16026ddf487d2564ed349d2c874a1a1Argyrios Kyrtzidis  assert(Spec->isDependentType() &&
24459763e221e16026ddf487d2564ed349d2c874a1a1Argyrios Kyrtzidis         "Non-dependent template-id type must have a canonical type");
24469763e221e16026ddf487d2564ed349d2c874a1a1Argyrios Kyrtzidis  return QualType(Spec, 0);
24479763e221e16026ddf487d2564ed349d2c874a1a1Argyrios Kyrtzidis}
24489763e221e16026ddf487d2564ed349d2c874a1a1Argyrios Kyrtzidis
24499763e221e16026ddf487d2564ed349d2c874a1a1Argyrios KyrtzidisQualType
2450465d41b92b2c862f3062c412a0538db65c6a2661Abramo BagnaraASTContext::getElaboratedType(ElaboratedTypeKeyword Keyword,
2451465d41b92b2c862f3062c412a0538db65c6a2661Abramo Bagnara                              NestedNameSpecifier *NNS,
24524ba2a17694148e16eaa8d3917f657ffcd3667be4Jay Foad                              QualType NamedType) const {
2453e4e5b054b4917f0ee493bb2fda5b1ec749bfb9a1Douglas Gregor  llvm::FoldingSetNodeID ID;
2454465d41b92b2c862f3062c412a0538db65c6a2661Abramo Bagnara  ElaboratedType::Profile(ID, Keyword, NNS, NamedType);
2455e4e5b054b4917f0ee493bb2fda5b1ec749bfb9a1Douglas Gregor
2456e4e5b054b4917f0ee493bb2fda5b1ec749bfb9a1Douglas Gregor  void *InsertPos = 0;
2457465d41b92b2c862f3062c412a0538db65c6a2661Abramo Bagnara  ElaboratedType *T = ElaboratedTypes.FindNodeOrInsertPos(ID, InsertPos);
2458e4e5b054b4917f0ee493bb2fda5b1ec749bfb9a1Douglas Gregor  if (T)
2459e4e5b054b4917f0ee493bb2fda5b1ec749bfb9a1Douglas Gregor    return QualType(T, 0);
2460e4e5b054b4917f0ee493bb2fda5b1ec749bfb9a1Douglas Gregor
2461789b1f640205e81b5af250693246120f1ce9d147Douglas Gregor  QualType Canon = NamedType;
2462789b1f640205e81b5af250693246120f1ce9d147Douglas Gregor  if (!Canon.isCanonical()) {
2463789b1f640205e81b5af250693246120f1ce9d147Douglas Gregor    Canon = getCanonicalType(NamedType);
2464465d41b92b2c862f3062c412a0538db65c6a2661Abramo Bagnara    ElaboratedType *CheckT = ElaboratedTypes.FindNodeOrInsertPos(ID, InsertPos);
2465465d41b92b2c862f3062c412a0538db65c6a2661Abramo Bagnara    assert(!CheckT && "Elaborated canonical type broken");
2466789b1f640205e81b5af250693246120f1ce9d147Douglas Gregor    (void)CheckT;
2467789b1f640205e81b5af250693246120f1ce9d147Douglas Gregor  }
2468789b1f640205e81b5af250693246120f1ce9d147Douglas Gregor
2469465d41b92b2c862f3062c412a0538db65c6a2661Abramo Bagnara  T = new (*this) ElaboratedType(Keyword, NNS, NamedType, Canon);
2470e4e5b054b4917f0ee493bb2fda5b1ec749bfb9a1Douglas Gregor  Types.push_back(T);
2471465d41b92b2c862f3062c412a0538db65c6a2661Abramo Bagnara  ElaboratedTypes.InsertNode(T, InsertPos);
2472e4e5b054b4917f0ee493bb2fda5b1ec749bfb9a1Douglas Gregor  return QualType(T, 0);
2473e4e5b054b4917f0ee493bb2fda5b1ec749bfb9a1Douglas Gregor}
2474e4e5b054b4917f0ee493bb2fda5b1ec749bfb9a1Douglas Gregor
2475075f8f1b6bed4d1b224c74f87508534cc6392ce6Abramo BagnaraQualType
24764ba2a17694148e16eaa8d3917f657ffcd3667be4Jay FoadASTContext::getParenType(QualType InnerType) const {
2477075f8f1b6bed4d1b224c74f87508534cc6392ce6Abramo Bagnara  llvm::FoldingSetNodeID ID;
2478075f8f1b6bed4d1b224c74f87508534cc6392ce6Abramo Bagnara  ParenType::Profile(ID, InnerType);
2479075f8f1b6bed4d1b224c74f87508534cc6392ce6Abramo Bagnara
2480075f8f1b6bed4d1b224c74f87508534cc6392ce6Abramo Bagnara  void *InsertPos = 0;
2481075f8f1b6bed4d1b224c74f87508534cc6392ce6Abramo Bagnara  ParenType *T = ParenTypes.FindNodeOrInsertPos(ID, InsertPos);
2482075f8f1b6bed4d1b224c74f87508534cc6392ce6Abramo Bagnara  if (T)
2483075f8f1b6bed4d1b224c74f87508534cc6392ce6Abramo Bagnara    return QualType(T, 0);
2484075f8f1b6bed4d1b224c74f87508534cc6392ce6Abramo Bagnara
2485075f8f1b6bed4d1b224c74f87508534cc6392ce6Abramo Bagnara  QualType Canon = InnerType;
2486075f8f1b6bed4d1b224c74f87508534cc6392ce6Abramo Bagnara  if (!Canon.isCanonical()) {
2487075f8f1b6bed4d1b224c74f87508534cc6392ce6Abramo Bagnara    Canon = getCanonicalType(InnerType);
2488075f8f1b6bed4d1b224c74f87508534cc6392ce6Abramo Bagnara    ParenType *CheckT = ParenTypes.FindNodeOrInsertPos(ID, InsertPos);
2489075f8f1b6bed4d1b224c74f87508534cc6392ce6Abramo Bagnara    assert(!CheckT && "Paren canonical type broken");
2490075f8f1b6bed4d1b224c74f87508534cc6392ce6Abramo Bagnara    (void)CheckT;
2491075f8f1b6bed4d1b224c74f87508534cc6392ce6Abramo Bagnara  }
2492075f8f1b6bed4d1b224c74f87508534cc6392ce6Abramo Bagnara
2493075f8f1b6bed4d1b224c74f87508534cc6392ce6Abramo Bagnara  T = new (*this) ParenType(InnerType, Canon);
2494075f8f1b6bed4d1b224c74f87508534cc6392ce6Abramo Bagnara  Types.push_back(T);
2495075f8f1b6bed4d1b224c74f87508534cc6392ce6Abramo Bagnara  ParenTypes.InsertNode(T, InsertPos);
2496075f8f1b6bed4d1b224c74f87508534cc6392ce6Abramo Bagnara  return QualType(T, 0);
2497075f8f1b6bed4d1b224c74f87508534cc6392ce6Abramo Bagnara}
2498075f8f1b6bed4d1b224c74f87508534cc6392ce6Abramo Bagnara
24994a2023f5014e82389d5980d307b89c545dbbac81Douglas GregorQualType ASTContext::getDependentNameType(ElaboratedTypeKeyword Keyword,
25004a2023f5014e82389d5980d307b89c545dbbac81Douglas Gregor                                          NestedNameSpecifier *NNS,
25014a2023f5014e82389d5980d307b89c545dbbac81Douglas Gregor                                          const IdentifierInfo *Name,
25024ba2a17694148e16eaa8d3917f657ffcd3667be4Jay Foad                                          QualType Canon) const {
2503d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor  assert(NNS->isDependent() && "nested-name-specifier must be dependent");
2504d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor
2505d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor  if (Canon.isNull()) {
2506d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor    NestedNameSpecifier *CanonNNS = getCanonicalNestedNameSpecifier(NNS);
25074a2023f5014e82389d5980d307b89c545dbbac81Douglas Gregor    ElaboratedTypeKeyword CanonKeyword = Keyword;
25084a2023f5014e82389d5980d307b89c545dbbac81Douglas Gregor    if (Keyword == ETK_None)
25094a2023f5014e82389d5980d307b89c545dbbac81Douglas Gregor      CanonKeyword = ETK_Typename;
25104a2023f5014e82389d5980d307b89c545dbbac81Douglas Gregor
25114a2023f5014e82389d5980d307b89c545dbbac81Douglas Gregor    if (CanonNNS != NNS || CanonKeyword != Keyword)
25124a2023f5014e82389d5980d307b89c545dbbac81Douglas Gregor      Canon = getDependentNameType(CanonKeyword, CanonNNS, Name);
2513d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor  }
2514d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor
2515d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor  llvm::FoldingSetNodeID ID;
25164a2023f5014e82389d5980d307b89c545dbbac81Douglas Gregor  DependentNameType::Profile(ID, Keyword, NNS, Name);
2517d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor
2518d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor  void *InsertPos = 0;
25194714c12a1ab759156b78be8f109ea4c12213af57Douglas Gregor  DependentNameType *T
25204714c12a1ab759156b78be8f109ea4c12213af57Douglas Gregor    = DependentNameTypes.FindNodeOrInsertPos(ID, InsertPos);
2521d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor  if (T)
2522d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor    return QualType(T, 0);
2523d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor
25244a2023f5014e82389d5980d307b89c545dbbac81Douglas Gregor  T = new (*this) DependentNameType(Keyword, NNS, Name, Canon);
2525d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor  Types.push_back(T);
25264714c12a1ab759156b78be8f109ea4c12213af57Douglas Gregor  DependentNameTypes.InsertNode(T, InsertPos);
25271eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  return QualType(T, 0);
2528d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor}
2529d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor
25301eb4433ac451dc16f4133a88af2d002ac26c58efMike StumpQualType
253133500955d731c73717af52088b7fc0e7a85681e7John McCallASTContext::getDependentTemplateSpecializationType(
253233500955d731c73717af52088b7fc0e7a85681e7John McCall                                 ElaboratedTypeKeyword Keyword,
25334a2023f5014e82389d5980d307b89c545dbbac81Douglas Gregor                                 NestedNameSpecifier *NNS,
253433500955d731c73717af52088b7fc0e7a85681e7John McCall                                 const IdentifierInfo *Name,
25354ba2a17694148e16eaa8d3917f657ffcd3667be4Jay Foad                                 const TemplateArgumentListInfo &Args) const {
253633500955d731c73717af52088b7fc0e7a85681e7John McCall  // TODO: avoid this copy
25375f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner  SmallVector<TemplateArgument, 16> ArgCopy;
253833500955d731c73717af52088b7fc0e7a85681e7John McCall  for (unsigned I = 0, E = Args.size(); I != E; ++I)
253933500955d731c73717af52088b7fc0e7a85681e7John McCall    ArgCopy.push_back(Args[I].getArgument());
254033500955d731c73717af52088b7fc0e7a85681e7John McCall  return getDependentTemplateSpecializationType(Keyword, NNS, Name,
254133500955d731c73717af52088b7fc0e7a85681e7John McCall                                                ArgCopy.size(),
254233500955d731c73717af52088b7fc0e7a85681e7John McCall                                                ArgCopy.data());
254333500955d731c73717af52088b7fc0e7a85681e7John McCall}
254433500955d731c73717af52088b7fc0e7a85681e7John McCall
254533500955d731c73717af52088b7fc0e7a85681e7John McCallQualType
254633500955d731c73717af52088b7fc0e7a85681e7John McCallASTContext::getDependentTemplateSpecializationType(
254733500955d731c73717af52088b7fc0e7a85681e7John McCall                                 ElaboratedTypeKeyword Keyword,
254833500955d731c73717af52088b7fc0e7a85681e7John McCall                                 NestedNameSpecifier *NNS,
254933500955d731c73717af52088b7fc0e7a85681e7John McCall                                 const IdentifierInfo *Name,
255033500955d731c73717af52088b7fc0e7a85681e7John McCall                                 unsigned NumArgs,
25514ba2a17694148e16eaa8d3917f657ffcd3667be4Jay Foad                                 const TemplateArgument *Args) const {
2552aa2187de137e5b809dcbbe14f3b61ae907a3d8aaDouglas Gregor  assert((!NNS || NNS->isDependent()) &&
2553aa2187de137e5b809dcbbe14f3b61ae907a3d8aaDouglas Gregor         "nested-name-specifier must be dependent");
25541734317845d60307d474b5da8a8d33adbaf5e723Douglas Gregor
2555789b1f640205e81b5af250693246120f1ce9d147Douglas Gregor  llvm::FoldingSetNodeID ID;
255633500955d731c73717af52088b7fc0e7a85681e7John McCall  DependentTemplateSpecializationType::Profile(ID, *this, Keyword, NNS,
255733500955d731c73717af52088b7fc0e7a85681e7John McCall                                               Name, NumArgs, Args);
2558789b1f640205e81b5af250693246120f1ce9d147Douglas Gregor
2559789b1f640205e81b5af250693246120f1ce9d147Douglas Gregor  void *InsertPos = 0;
256033500955d731c73717af52088b7fc0e7a85681e7John McCall  DependentTemplateSpecializationType *T
256133500955d731c73717af52088b7fc0e7a85681e7John McCall    = DependentTemplateSpecializationTypes.FindNodeOrInsertPos(ID, InsertPos);
2562789b1f640205e81b5af250693246120f1ce9d147Douglas Gregor  if (T)
2563789b1f640205e81b5af250693246120f1ce9d147Douglas Gregor    return QualType(T, 0);
2564789b1f640205e81b5af250693246120f1ce9d147Douglas Gregor
256533500955d731c73717af52088b7fc0e7a85681e7John McCall  NestedNameSpecifier *CanonNNS = getCanonicalNestedNameSpecifier(NNS);
256633500955d731c73717af52088b7fc0e7a85681e7John McCall
256733500955d731c73717af52088b7fc0e7a85681e7John McCall  ElaboratedTypeKeyword CanonKeyword = Keyword;
256833500955d731c73717af52088b7fc0e7a85681e7John McCall  if (Keyword == ETK_None) CanonKeyword = ETK_Typename;
25691734317845d60307d474b5da8a8d33adbaf5e723Douglas Gregor
257033500955d731c73717af52088b7fc0e7a85681e7John McCall  bool AnyNonCanonArgs = false;
25715f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner  SmallVector<TemplateArgument, 16> CanonArgs(NumArgs);
257233500955d731c73717af52088b7fc0e7a85681e7John McCall  for (unsigned I = 0; I != NumArgs; ++I) {
257333500955d731c73717af52088b7fc0e7a85681e7John McCall    CanonArgs[I] = getCanonicalTemplateArgument(Args[I]);
257433500955d731c73717af52088b7fc0e7a85681e7John McCall    if (!CanonArgs[I].structurallyEquals(Args[I]))
257533500955d731c73717af52088b7fc0e7a85681e7John McCall      AnyNonCanonArgs = true;
2576789b1f640205e81b5af250693246120f1ce9d147Douglas Gregor  }
25771734317845d60307d474b5da8a8d33adbaf5e723Douglas Gregor
257833500955d731c73717af52088b7fc0e7a85681e7John McCall  QualType Canon;
257933500955d731c73717af52088b7fc0e7a85681e7John McCall  if (AnyNonCanonArgs || CanonNNS != NNS || CanonKeyword != Keyword) {
258033500955d731c73717af52088b7fc0e7a85681e7John McCall    Canon = getDependentTemplateSpecializationType(CanonKeyword, CanonNNS,
258133500955d731c73717af52088b7fc0e7a85681e7John McCall                                                   Name, NumArgs,
258233500955d731c73717af52088b7fc0e7a85681e7John McCall                                                   CanonArgs.data());
258333500955d731c73717af52088b7fc0e7a85681e7John McCall
258433500955d731c73717af52088b7fc0e7a85681e7John McCall    // Find the insert position again.
258533500955d731c73717af52088b7fc0e7a85681e7John McCall    DependentTemplateSpecializationTypes.FindNodeOrInsertPos(ID, InsertPos);
258633500955d731c73717af52088b7fc0e7a85681e7John McCall  }
258733500955d731c73717af52088b7fc0e7a85681e7John McCall
258833500955d731c73717af52088b7fc0e7a85681e7John McCall  void *Mem = Allocate((sizeof(DependentTemplateSpecializationType) +
258933500955d731c73717af52088b7fc0e7a85681e7John McCall                        sizeof(TemplateArgument) * NumArgs),
259033500955d731c73717af52088b7fc0e7a85681e7John McCall                       TypeAlignment);
2591ef99001908e799c388f1363b1e607dad5f5b57d3John McCall  T = new (Mem) DependentTemplateSpecializationType(Keyword, NNS,
259233500955d731c73717af52088b7fc0e7a85681e7John McCall                                                    Name, NumArgs, Args, Canon);
25931734317845d60307d474b5da8a8d33adbaf5e723Douglas Gregor  Types.push_back(T);
259433500955d731c73717af52088b7fc0e7a85681e7John McCall  DependentTemplateSpecializationTypes.InsertNode(T, InsertPos);
25951eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  return QualType(T, 0);
25961734317845d60307d474b5da8a8d33adbaf5e723Douglas Gregor}
25971734317845d60307d474b5da8a8d33adbaf5e723Douglas Gregor
2598cded4f649cd4b7ba7d461c25c6482ef52b8d3a2aDouglas GregorQualType ASTContext::getPackExpansionType(QualType Pattern,
2599cded4f649cd4b7ba7d461c25c6482ef52b8d3a2aDouglas Gregor                                      llvm::Optional<unsigned> NumExpansions) {
26007536dd5e6c99584481b7dab68b7e7d8df9c54054Douglas Gregor  llvm::FoldingSetNodeID ID;
2601cded4f649cd4b7ba7d461c25c6482ef52b8d3a2aDouglas Gregor  PackExpansionType::Profile(ID, Pattern, NumExpansions);
26027536dd5e6c99584481b7dab68b7e7d8df9c54054Douglas Gregor
26037536dd5e6c99584481b7dab68b7e7d8df9c54054Douglas Gregor  assert(Pattern->containsUnexpandedParameterPack() &&
26047536dd5e6c99584481b7dab68b7e7d8df9c54054Douglas Gregor         "Pack expansions must expand one or more parameter packs");
26057536dd5e6c99584481b7dab68b7e7d8df9c54054Douglas Gregor  void *InsertPos = 0;
26067536dd5e6c99584481b7dab68b7e7d8df9c54054Douglas Gregor  PackExpansionType *T
26077536dd5e6c99584481b7dab68b7e7d8df9c54054Douglas Gregor    = PackExpansionTypes.FindNodeOrInsertPos(ID, InsertPos);
26087536dd5e6c99584481b7dab68b7e7d8df9c54054Douglas Gregor  if (T)
26097536dd5e6c99584481b7dab68b7e7d8df9c54054Douglas Gregor    return QualType(T, 0);
26107536dd5e6c99584481b7dab68b7e7d8df9c54054Douglas Gregor
26117536dd5e6c99584481b7dab68b7e7d8df9c54054Douglas Gregor  QualType Canon;
26127536dd5e6c99584481b7dab68b7e7d8df9c54054Douglas Gregor  if (!Pattern.isCanonical()) {
2613cded4f649cd4b7ba7d461c25c6482ef52b8d3a2aDouglas Gregor    Canon = getPackExpansionType(getCanonicalType(Pattern), NumExpansions);
26147536dd5e6c99584481b7dab68b7e7d8df9c54054Douglas Gregor
26157536dd5e6c99584481b7dab68b7e7d8df9c54054Douglas Gregor    // Find the insert position again.
26167536dd5e6c99584481b7dab68b7e7d8df9c54054Douglas Gregor    PackExpansionTypes.FindNodeOrInsertPos(ID, InsertPos);
26177536dd5e6c99584481b7dab68b7e7d8df9c54054Douglas Gregor  }
26187536dd5e6c99584481b7dab68b7e7d8df9c54054Douglas Gregor
2619cded4f649cd4b7ba7d461c25c6482ef52b8d3a2aDouglas Gregor  T = new (*this) PackExpansionType(Pattern, Canon, NumExpansions);
26207536dd5e6c99584481b7dab68b7e7d8df9c54054Douglas Gregor  Types.push_back(T);
26217536dd5e6c99584481b7dab68b7e7d8df9c54054Douglas Gregor  PackExpansionTypes.InsertNode(T, InsertPos);
26227536dd5e6c99584481b7dab68b7e7d8df9c54054Douglas Gregor  return QualType(T, 0);
26237536dd5e6c99584481b7dab68b7e7d8df9c54054Douglas Gregor}
26247536dd5e6c99584481b7dab68b7e7d8df9c54054Douglas Gregor
262588cb27a160adc305783a44f922ee4b216006ebf9Chris Lattner/// CmpProtocolNames - Comparison predicate for sorting protocols
262688cb27a160adc305783a44f922ee4b216006ebf9Chris Lattner/// alphabetically.
262788cb27a160adc305783a44f922ee4b216006ebf9Chris Lattnerstatic bool CmpProtocolNames(const ObjCProtocolDecl *LHS,
262888cb27a160adc305783a44f922ee4b216006ebf9Chris Lattner                            const ObjCProtocolDecl *RHS) {
26292e1cd4264d363ca869bf37ef160902f211d21b8cDouglas Gregor  return LHS->getDeclName() < RHS->getDeclName();
263088cb27a160adc305783a44f922ee4b216006ebf9Chris Lattner}
263188cb27a160adc305783a44f922ee4b216006ebf9Chris Lattner
2632c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCallstatic bool areSortedAndUniqued(ObjCProtocolDecl * const *Protocols,
263354e14c4db764c0636160d26c5bbf491637c83a76John McCall                                unsigned NumProtocols) {
263454e14c4db764c0636160d26c5bbf491637c83a76John McCall  if (NumProtocols == 0) return true;
263554e14c4db764c0636160d26c5bbf491637c83a76John McCall
263654e14c4db764c0636160d26c5bbf491637c83a76John McCall  for (unsigned i = 1; i != NumProtocols; ++i)
263754e14c4db764c0636160d26c5bbf491637c83a76John McCall    if (!CmpProtocolNames(Protocols[i-1], Protocols[i]))
263854e14c4db764c0636160d26c5bbf491637c83a76John McCall      return false;
263954e14c4db764c0636160d26c5bbf491637c83a76John McCall  return true;
264054e14c4db764c0636160d26c5bbf491637c83a76John McCall}
264154e14c4db764c0636160d26c5bbf491637c83a76John McCall
264254e14c4db764c0636160d26c5bbf491637c83a76John McCallstatic void SortAndUniqueProtocols(ObjCProtocolDecl **Protocols,
264388cb27a160adc305783a44f922ee4b216006ebf9Chris Lattner                                   unsigned &NumProtocols) {
264488cb27a160adc305783a44f922ee4b216006ebf9Chris Lattner  ObjCProtocolDecl **ProtocolsEnd = Protocols+NumProtocols;
26451eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
264688cb27a160adc305783a44f922ee4b216006ebf9Chris Lattner  // Sort protocols, keyed by name.
264788cb27a160adc305783a44f922ee4b216006ebf9Chris Lattner  std::sort(Protocols, Protocols+NumProtocols, CmpProtocolNames);
264888cb27a160adc305783a44f922ee4b216006ebf9Chris Lattner
264988cb27a160adc305783a44f922ee4b216006ebf9Chris Lattner  // Remove duplicates.
265088cb27a160adc305783a44f922ee4b216006ebf9Chris Lattner  ProtocolsEnd = std::unique(Protocols, ProtocolsEnd);
265188cb27a160adc305783a44f922ee4b216006ebf9Chris Lattner  NumProtocols = ProtocolsEnd-Protocols;
265288cb27a160adc305783a44f922ee4b216006ebf9Chris Lattner}
265388cb27a160adc305783a44f922ee4b216006ebf9Chris Lattner
2654c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCallQualType ASTContext::getObjCObjectType(QualType BaseType,
2655c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall                                       ObjCProtocolDecl * const *Protocols,
26564ba2a17694148e16eaa8d3917f657ffcd3667be4Jay Foad                                       unsigned NumProtocols) const {
2657c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall  // If the base type is an interface and there aren't any protocols
2658c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall  // to add, then the interface type will do just fine.
2659c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall  if (!NumProtocols && isa<ObjCInterfaceType>(BaseType))
2660c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall    return BaseType;
2661d1b3c2dd5bc1f3103bee6137957aa7c5f8f2f0bcSteve Naroff
2662c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall  // Look in the folding set for an existing type.
2663c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall  llvm::FoldingSetNodeID ID;
2664c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall  ObjCObjectTypeImpl::Profile(ID, BaseType, Protocols, NumProtocols);
2665d1b3c2dd5bc1f3103bee6137957aa7c5f8f2f0bcSteve Naroff  void *InsertPos = 0;
2666c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall  if (ObjCObjectType *QT = ObjCObjectTypes.FindNodeOrInsertPos(ID, InsertPos))
2667c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall    return QualType(QT, 0);
2668d1b3c2dd5bc1f3103bee6137957aa7c5f8f2f0bcSteve Naroff
2669c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall  // Build the canonical type, which has the canonical base type and
2670c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall  // a sorted-and-uniqued list of protocols.
267154e14c4db764c0636160d26c5bbf491637c83a76John McCall  QualType Canonical;
2672c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall  bool ProtocolsSorted = areSortedAndUniqued(Protocols, NumProtocols);
2673c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall  if (!ProtocolsSorted || !BaseType.isCanonical()) {
2674c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall    if (!ProtocolsSorted) {
26755f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner      SmallVector<ObjCProtocolDecl*, 8> Sorted(Protocols,
26760237941e0beb0c929934b66ad29443b484d987feBenjamin Kramer                                                     Protocols + NumProtocols);
267754e14c4db764c0636160d26c5bbf491637c83a76John McCall      unsigned UniqueCount = NumProtocols;
267854e14c4db764c0636160d26c5bbf491637c83a76John McCall
267954e14c4db764c0636160d26c5bbf491637c83a76John McCall      SortAndUniqueProtocols(&Sorted[0], UniqueCount);
2680c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall      Canonical = getObjCObjectType(getCanonicalType(BaseType),
2681c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall                                    &Sorted[0], UniqueCount);
268254e14c4db764c0636160d26c5bbf491637c83a76John McCall    } else {
2683c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall      Canonical = getObjCObjectType(getCanonicalType(BaseType),
2684c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall                                    Protocols, NumProtocols);
268554e14c4db764c0636160d26c5bbf491637c83a76John McCall    }
268654e14c4db764c0636160d26c5bbf491637c83a76John McCall
268754e14c4db764c0636160d26c5bbf491637c83a76John McCall    // Regenerate InsertPos.
2688c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall    ObjCObjectTypes.FindNodeOrInsertPos(ID, InsertPos);
268954e14c4db764c0636160d26c5bbf491637c83a76John McCall  }
269054e14c4db764c0636160d26c5bbf491637c83a76John McCall
2691c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall  unsigned Size = sizeof(ObjCObjectTypeImpl);
2692c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall  Size += NumProtocols * sizeof(ObjCProtocolDecl *);
2693fd6a0887a099256c35a5b23e9afd517ffe95fa0aDouglas Gregor  void *Mem = Allocate(Size, TypeAlignment);
2694c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall  ObjCObjectTypeImpl *T =
2695c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall    new (Mem) ObjCObjectTypeImpl(Canonical, BaseType, Protocols, NumProtocols);
26961eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
2697c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall  Types.push_back(T);
2698c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall  ObjCObjectTypes.InsertNode(T, InsertPos);
2699c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall  return QualType(T, 0);
2700d1b3c2dd5bc1f3103bee6137957aa7c5f8f2f0bcSteve Naroff}
270188cb27a160adc305783a44f922ee4b216006ebf9Chris Lattner
2702c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall/// getObjCObjectPointerType - Return a ObjCObjectPointerType type for
2703c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall/// the given object type.
27044ba2a17694148e16eaa8d3917f657ffcd3667be4Jay FoadQualType ASTContext::getObjCObjectPointerType(QualType ObjectT) const {
27054b6c9051c6522894978c9ba6a819a659d102db36Fariborz Jahanian  llvm::FoldingSetNodeID ID;
2706c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall  ObjCObjectPointerType::Profile(ID, ObjectT);
27071eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
27084b6c9051c6522894978c9ba6a819a659d102db36Fariborz Jahanian  void *InsertPos = 0;
2709c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall  if (ObjCObjectPointerType *QT =
2710c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall              ObjCObjectPointerTypes.FindNodeOrInsertPos(ID, InsertPos))
27114b6c9051c6522894978c9ba6a819a659d102db36Fariborz Jahanian    return QualType(QT, 0);
27121eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
2713c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall  // Find the canonical object type.
271454e14c4db764c0636160d26c5bbf491637c83a76John McCall  QualType Canonical;
2715c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall  if (!ObjectT.isCanonical()) {
2716c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall    Canonical = getObjCObjectPointerType(getCanonicalType(ObjectT));
271754e14c4db764c0636160d26c5bbf491637c83a76John McCall
2718c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall    // Regenerate InsertPos.
2719c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall    ObjCObjectPointerTypes.FindNodeOrInsertPos(ID, InsertPos);
272054e14c4db764c0636160d26c5bbf491637c83a76John McCall  }
272154e14c4db764c0636160d26c5bbf491637c83a76John McCall
2722c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall  // No match.
2723c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall  void *Mem = Allocate(sizeof(ObjCObjectPointerType), TypeAlignment);
2724c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall  ObjCObjectPointerType *QType =
2725c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall    new (Mem) ObjCObjectPointerType(Canonical, ObjectT);
272624fab41057e4b67ed69a6b4027d5ae0f2f6934dcArgyrios Kyrtzidis
272724fab41057e4b67ed69a6b4027d5ae0f2f6934dcArgyrios Kyrtzidis  Types.push_back(QType);
2728c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall  ObjCObjectPointerTypes.InsertNode(QType, InsertPos);
272924fab41057e4b67ed69a6b4027d5ae0f2f6934dcArgyrios Kyrtzidis  return QualType(QType, 0);
273024fab41057e4b67ed69a6b4027d5ae0f2f6934dcArgyrios Kyrtzidis}
273124fab41057e4b67ed69a6b4027d5ae0f2f6934dcArgyrios Kyrtzidis
2732deacbdca554298ccdf636f19c6094a8825ec6b34Douglas Gregor/// getObjCInterfaceType - Return the unique reference to the type for the
2733deacbdca554298ccdf636f19c6094a8825ec6b34Douglas Gregor/// specified ObjC interface decl. The list of protocols is optional.
27344ba2a17694148e16eaa8d3917f657ffcd3667be4Jay FoadQualType ASTContext::getObjCInterfaceType(const ObjCInterfaceDecl *Decl) const {
2735deacbdca554298ccdf636f19c6094a8825ec6b34Douglas Gregor  if (Decl->TypeForDecl)
2736deacbdca554298ccdf636f19c6094a8825ec6b34Douglas Gregor    return QualType(Decl->TypeForDecl, 0);
273774c730ad1f6818b676b0bad46d806a9176950328Sebastian Redl
2738deacbdca554298ccdf636f19c6094a8825ec6b34Douglas Gregor  // FIXME: redeclarations?
2739deacbdca554298ccdf636f19c6094a8825ec6b34Douglas Gregor  void *Mem = Allocate(sizeof(ObjCInterfaceType), TypeAlignment);
2740deacbdca554298ccdf636f19c6094a8825ec6b34Douglas Gregor  ObjCInterfaceType *T = new (Mem) ObjCInterfaceType(Decl);
2741deacbdca554298ccdf636f19c6094a8825ec6b34Douglas Gregor  Decl->TypeForDecl = T;
2742deacbdca554298ccdf636f19c6094a8825ec6b34Douglas Gregor  Types.push_back(T);
2743deacbdca554298ccdf636f19c6094a8825ec6b34Douglas Gregor  return QualType(T, 0);
2744c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall}
2745c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall
274672564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor/// getTypeOfExprType - Unlike many "get<Type>" functions, we can't unique
274772564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor/// TypeOfExprType AST's (since expression's are never shared). For example,
27489752f25748d954df99087d741ea35db37ff16beaSteve Naroff/// multiple declarations that refer to "typeof(x)" all contain different
27491eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// DeclRefExpr's. This doesn't effect the type checker, since it operates
27509752f25748d954df99087d741ea35db37ff16beaSteve Naroff/// on canonical type's (which are always unique).
27514ba2a17694148e16eaa8d3917f657ffcd3667be4Jay FoadQualType ASTContext::getTypeOfExprType(Expr *tofExpr) const {
2752dd0257c77719a13d4acd513df40b04300cbfc871Douglas Gregor  TypeOfExprType *toe;
2753b197572cf1cd70a817a1f546478cb2cb9112c48eDouglas Gregor  if (tofExpr->isTypeDependent()) {
2754b197572cf1cd70a817a1f546478cb2cb9112c48eDouglas Gregor    llvm::FoldingSetNodeID ID;
2755b197572cf1cd70a817a1f546478cb2cb9112c48eDouglas Gregor    DependentTypeOfExprType::Profile(ID, *this, tofExpr);
27561eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
2757b197572cf1cd70a817a1f546478cb2cb9112c48eDouglas Gregor    void *InsertPos = 0;
2758b197572cf1cd70a817a1f546478cb2cb9112c48eDouglas Gregor    DependentTypeOfExprType *Canon
2759b197572cf1cd70a817a1f546478cb2cb9112c48eDouglas Gregor      = DependentTypeOfExprTypes.FindNodeOrInsertPos(ID, InsertPos);
2760b197572cf1cd70a817a1f546478cb2cb9112c48eDouglas Gregor    if (Canon) {
2761b197572cf1cd70a817a1f546478cb2cb9112c48eDouglas Gregor      // We already have a "canonical" version of an identical, dependent
2762b197572cf1cd70a817a1f546478cb2cb9112c48eDouglas Gregor      // typeof(expr) type. Use that as our canonical type.
27636b304a0254a13f42390b865ff5ba668a49cc58aeJohn McCall      toe = new (*this, TypeAlignment) TypeOfExprType(tofExpr,
2764b197572cf1cd70a817a1f546478cb2cb9112c48eDouglas Gregor                                          QualType((TypeOfExprType*)Canon, 0));
2765b197572cf1cd70a817a1f546478cb2cb9112c48eDouglas Gregor    }
2766b197572cf1cd70a817a1f546478cb2cb9112c48eDouglas Gregor    else {
2767b197572cf1cd70a817a1f546478cb2cb9112c48eDouglas Gregor      // Build a new, canonical typeof(expr) type.
27686b304a0254a13f42390b865ff5ba668a49cc58aeJohn McCall      Canon
27696b304a0254a13f42390b865ff5ba668a49cc58aeJohn McCall        = new (*this, TypeAlignment) DependentTypeOfExprType(*this, tofExpr);
2770b197572cf1cd70a817a1f546478cb2cb9112c48eDouglas Gregor      DependentTypeOfExprTypes.InsertNode(Canon, InsertPos);
2771b197572cf1cd70a817a1f546478cb2cb9112c48eDouglas Gregor      toe = Canon;
2772b197572cf1cd70a817a1f546478cb2cb9112c48eDouglas Gregor    }
2773b197572cf1cd70a817a1f546478cb2cb9112c48eDouglas Gregor  } else {
2774dd0257c77719a13d4acd513df40b04300cbfc871Douglas Gregor    QualType Canonical = getCanonicalType(tofExpr->getType());
27756b304a0254a13f42390b865ff5ba668a49cc58aeJohn McCall    toe = new (*this, TypeAlignment) TypeOfExprType(tofExpr, Canonical);
2776dd0257c77719a13d4acd513df40b04300cbfc871Douglas Gregor  }
27779752f25748d954df99087d741ea35db37ff16beaSteve Naroff  Types.push_back(toe);
27789752f25748d954df99087d741ea35db37ff16beaSteve Naroff  return QualType(toe, 0);
2779d1861fd633d5096a00777c918eb8575ea7162fe7Steve Naroff}
2780d1861fd633d5096a00777c918eb8575ea7162fe7Steve Naroff
27819752f25748d954df99087d741ea35db37ff16beaSteve Naroff/// getTypeOfType -  Unlike many "get<Type>" functions, we don't unique
27829752f25748d954df99087d741ea35db37ff16beaSteve Naroff/// TypeOfType AST's. The only motivation to unique these nodes would be
27839752f25748d954df99087d741ea35db37ff16beaSteve Naroff/// memory savings. Since typeof(t) is fairly uncommon, space shouldn't be
27841eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// an issue. This doesn't effect the type checker, since it operates
27859752f25748d954df99087d741ea35db37ff16beaSteve Naroff/// on canonical type's (which are always unique).
27864ba2a17694148e16eaa8d3917f657ffcd3667be4Jay FoadQualType ASTContext::getTypeOfType(QualType tofType) const {
2787f52ab250ff92bc51a9ac9a8e19bd43b63a5f844fChris Lattner  QualType Canonical = getCanonicalType(tofType);
27886b304a0254a13f42390b865ff5ba668a49cc58aeJohn McCall  TypeOfType *tot = new (*this, TypeAlignment) TypeOfType(tofType, Canonical);
27899752f25748d954df99087d741ea35db37ff16beaSteve Naroff  Types.push_back(tot);
27909752f25748d954df99087d741ea35db37ff16beaSteve Naroff  return QualType(tot, 0);
2791d1861fd633d5096a00777c918eb8575ea7162fe7Steve Naroff}
2792d1861fd633d5096a00777c918eb8575ea7162fe7Steve Naroff
279360a9a2a404a4cf259d39133383e922aa00ca9043Anders Carlsson/// getDecltypeForExpr - Given an expr, will return the decltype for that
279460a9a2a404a4cf259d39133383e922aa00ca9043Anders Carlsson/// expression, according to the rules in C++0x [dcl.type.simple]p4
27954ba2a17694148e16eaa8d3917f657ffcd3667be4Jay Foadstatic QualType getDecltypeForExpr(const Expr *e, const ASTContext &Context) {
2796a07c33e64e1169e4261f7748c7f9191091a3ad2eAnders Carlsson  if (e->isTypeDependent())
2797a07c33e64e1169e4261f7748c7f9191091a3ad2eAnders Carlsson    return Context.DependentTy;
27981eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
279960a9a2a404a4cf259d39133383e922aa00ca9043Anders Carlsson  // If e is an id expression or a class member access, decltype(e) is defined
280060a9a2a404a4cf259d39133383e922aa00ca9043Anders Carlsson  // as the type of the entity named by e.
280160a9a2a404a4cf259d39133383e922aa00ca9043Anders Carlsson  if (const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(e)) {
280260a9a2a404a4cf259d39133383e922aa00ca9043Anders Carlsson    if (const ValueDecl *VD = dyn_cast<ValueDecl>(DRE->getDecl()))
280360a9a2a404a4cf259d39133383e922aa00ca9043Anders Carlsson      return VD->getType();
280460a9a2a404a4cf259d39133383e922aa00ca9043Anders Carlsson  }
280560a9a2a404a4cf259d39133383e922aa00ca9043Anders Carlsson  if (const MemberExpr *ME = dyn_cast<MemberExpr>(e)) {
280660a9a2a404a4cf259d39133383e922aa00ca9043Anders Carlsson    if (const FieldDecl *FD = dyn_cast<FieldDecl>(ME->getMemberDecl()))
280760a9a2a404a4cf259d39133383e922aa00ca9043Anders Carlsson      return FD->getType();
280860a9a2a404a4cf259d39133383e922aa00ca9043Anders Carlsson  }
280960a9a2a404a4cf259d39133383e922aa00ca9043Anders Carlsson  // If e is a function call or an invocation of an overloaded operator,
281060a9a2a404a4cf259d39133383e922aa00ca9043Anders Carlsson  // (parentheses around e are ignored), decltype(e) is defined as the
281160a9a2a404a4cf259d39133383e922aa00ca9043Anders Carlsson  // return type of that function.
281260a9a2a404a4cf259d39133383e922aa00ca9043Anders Carlsson  if (const CallExpr *CE = dyn_cast<CallExpr>(e->IgnoreParens()))
281360a9a2a404a4cf259d39133383e922aa00ca9043Anders Carlsson    return CE->getCallReturnType();
28141eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
281560a9a2a404a4cf259d39133383e922aa00ca9043Anders Carlsson  QualType T = e->getType();
28161eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
28171eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  // Otherwise, where T is the type of e, if e is an lvalue, decltype(e) is
281860a9a2a404a4cf259d39133383e922aa00ca9043Anders Carlsson  // defined as T&, otherwise decltype(e) is defined as T.
28197eb0a9eb0cde8444b97f9c5b713d9be7a6f1e607John McCall  if (e->isLValue())
282060a9a2a404a4cf259d39133383e922aa00ca9043Anders Carlsson    T = Context.getLValueReferenceType(T);
28211eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
282260a9a2a404a4cf259d39133383e922aa00ca9043Anders Carlsson  return T;
282360a9a2a404a4cf259d39133383e922aa00ca9043Anders Carlsson}
282460a9a2a404a4cf259d39133383e922aa00ca9043Anders Carlsson
2825395b475a4474f1c7574d927ad142ca0c7997cbcaAnders Carlsson/// getDecltypeType -  Unlike many "get<Type>" functions, we don't unique
2826395b475a4474f1c7574d927ad142ca0c7997cbcaAnders Carlsson/// DecltypeType AST's. The only motivation to unique these nodes would be
2827395b475a4474f1c7574d927ad142ca0c7997cbcaAnders Carlsson/// memory savings. Since decltype(t) is fairly uncommon, space shouldn't be
28281eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// an issue. This doesn't effect the type checker, since it operates
2829395b475a4474f1c7574d927ad142ca0c7997cbcaAnders Carlsson/// on canonical type's (which are always unique).
28304ba2a17694148e16eaa8d3917f657ffcd3667be4Jay FoadQualType ASTContext::getDecltypeType(Expr *e) const {
2831dd0257c77719a13d4acd513df40b04300cbfc871Douglas Gregor  DecltypeType *dt;
2832561f81243f665cf2001caadc45df505f826b72d6Douglas Gregor
2833561f81243f665cf2001caadc45df505f826b72d6Douglas Gregor  // C++0x [temp.type]p2:
2834561f81243f665cf2001caadc45df505f826b72d6Douglas Gregor  //   If an expression e involves a template parameter, decltype(e) denotes a
2835561f81243f665cf2001caadc45df505f826b72d6Douglas Gregor  //   unique dependent type. Two such decltype-specifiers refer to the same
2836561f81243f665cf2001caadc45df505f826b72d6Douglas Gregor  //   type only if their expressions are equivalent (14.5.6.1).
2837561f81243f665cf2001caadc45df505f826b72d6Douglas Gregor  if (e->isInstantiationDependent()) {
28389d702ae1cd5cfa19d884cbef77e1df99395138bbDouglas Gregor    llvm::FoldingSetNodeID ID;
28399d702ae1cd5cfa19d884cbef77e1df99395138bbDouglas Gregor    DependentDecltypeType::Profile(ID, *this, e);
28401eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
28419d702ae1cd5cfa19d884cbef77e1df99395138bbDouglas Gregor    void *InsertPos = 0;
28429d702ae1cd5cfa19d884cbef77e1df99395138bbDouglas Gregor    DependentDecltypeType *Canon
28439d702ae1cd5cfa19d884cbef77e1df99395138bbDouglas Gregor      = DependentDecltypeTypes.FindNodeOrInsertPos(ID, InsertPos);
28449d702ae1cd5cfa19d884cbef77e1df99395138bbDouglas Gregor    if (Canon) {
28459d702ae1cd5cfa19d884cbef77e1df99395138bbDouglas Gregor      // We already have a "canonical" version of an equivalent, dependent
28469d702ae1cd5cfa19d884cbef77e1df99395138bbDouglas Gregor      // decltype type. Use that as our canonical type.
28476b304a0254a13f42390b865ff5ba668a49cc58aeJohn McCall      dt = new (*this, TypeAlignment) DecltypeType(e, DependentTy,
28489d702ae1cd5cfa19d884cbef77e1df99395138bbDouglas Gregor                                       QualType((DecltypeType*)Canon, 0));
28499d702ae1cd5cfa19d884cbef77e1df99395138bbDouglas Gregor    }
28509d702ae1cd5cfa19d884cbef77e1df99395138bbDouglas Gregor    else {
28519d702ae1cd5cfa19d884cbef77e1df99395138bbDouglas Gregor      // Build a new, canonical typeof(expr) type.
28526b304a0254a13f42390b865ff5ba668a49cc58aeJohn McCall      Canon = new (*this, TypeAlignment) DependentDecltypeType(*this, e);
28539d702ae1cd5cfa19d884cbef77e1df99395138bbDouglas Gregor      DependentDecltypeTypes.InsertNode(Canon, InsertPos);
28549d702ae1cd5cfa19d884cbef77e1df99395138bbDouglas Gregor      dt = Canon;
28559d702ae1cd5cfa19d884cbef77e1df99395138bbDouglas Gregor    }
28569d702ae1cd5cfa19d884cbef77e1df99395138bbDouglas Gregor  } else {
2857dd0257c77719a13d4acd513df40b04300cbfc871Douglas Gregor    QualType T = getDecltypeForExpr(e, *this);
28586b304a0254a13f42390b865ff5ba668a49cc58aeJohn McCall    dt = new (*this, TypeAlignment) DecltypeType(e, T, getCanonicalType(T));
2859dd0257c77719a13d4acd513df40b04300cbfc871Douglas Gregor  }
2860395b475a4474f1c7574d927ad142ca0c7997cbcaAnders Carlsson  Types.push_back(dt);
2861395b475a4474f1c7574d927ad142ca0c7997cbcaAnders Carlsson  return QualType(dt, 0);
2862395b475a4474f1c7574d927ad142ca0c7997cbcaAnders Carlsson}
2863395b475a4474f1c7574d927ad142ca0c7997cbcaAnders Carlsson
2864ca63c200346c0ca9e00194ec6e34a5a7b0ed9321Sean Hunt/// getUnaryTransformationType - We don't unique these, since the memory
2865ca63c200346c0ca9e00194ec6e34a5a7b0ed9321Sean Hunt/// savings are minimal and these are rare.
2866ca63c200346c0ca9e00194ec6e34a5a7b0ed9321Sean HuntQualType ASTContext::getUnaryTransformType(QualType BaseType,
2867ca63c200346c0ca9e00194ec6e34a5a7b0ed9321Sean Hunt                                           QualType UnderlyingType,
2868ca63c200346c0ca9e00194ec6e34a5a7b0ed9321Sean Hunt                                           UnaryTransformType::UTTKind Kind)
2869ca63c200346c0ca9e00194ec6e34a5a7b0ed9321Sean Hunt    const {
2870ca63c200346c0ca9e00194ec6e34a5a7b0ed9321Sean Hunt  UnaryTransformType *Ty =
287169d9775da47a4b9f165dbc33277f02982928a94eDouglas Gregor    new (*this, TypeAlignment) UnaryTransformType (BaseType, UnderlyingType,
287269d9775da47a4b9f165dbc33277f02982928a94eDouglas Gregor                                                   Kind,
287369d9775da47a4b9f165dbc33277f02982928a94eDouglas Gregor                                 UnderlyingType->isDependentType() ?
287469d9775da47a4b9f165dbc33277f02982928a94eDouglas Gregor                                    QualType() : UnderlyingType);
2875ca63c200346c0ca9e00194ec6e34a5a7b0ed9321Sean Hunt  Types.push_back(Ty);
2876ca63c200346c0ca9e00194ec6e34a5a7b0ed9321Sean Hunt  return QualType(Ty, 0);
2877ca63c200346c0ca9e00194ec6e34a5a7b0ed9321Sean Hunt}
2878ca63c200346c0ca9e00194ec6e34a5a7b0ed9321Sean Hunt
2879483b9f3bc05c5409e2c6643f1c9d91e21c8ff9d2Richard Smith/// getAutoType - We only unique auto types after they've been deduced.
288034b41d939a1328f484511c6002ba2456db879a29Richard SmithQualType ASTContext::getAutoType(QualType DeducedType) const {
2881483b9f3bc05c5409e2c6643f1c9d91e21c8ff9d2Richard Smith  void *InsertPos = 0;
2882483b9f3bc05c5409e2c6643f1c9d91e21c8ff9d2Richard Smith  if (!DeducedType.isNull()) {
2883483b9f3bc05c5409e2c6643f1c9d91e21c8ff9d2Richard Smith    // Look in the folding set for an existing type.
2884483b9f3bc05c5409e2c6643f1c9d91e21c8ff9d2Richard Smith    llvm::FoldingSetNodeID ID;
2885483b9f3bc05c5409e2c6643f1c9d91e21c8ff9d2Richard Smith    AutoType::Profile(ID, DeducedType);
2886483b9f3bc05c5409e2c6643f1c9d91e21c8ff9d2Richard Smith    if (AutoType *AT = AutoTypes.FindNodeOrInsertPos(ID, InsertPos))
2887483b9f3bc05c5409e2c6643f1c9d91e21c8ff9d2Richard Smith      return QualType(AT, 0);
2888483b9f3bc05c5409e2c6643f1c9d91e21c8ff9d2Richard Smith  }
2889483b9f3bc05c5409e2c6643f1c9d91e21c8ff9d2Richard Smith
2890483b9f3bc05c5409e2c6643f1c9d91e21c8ff9d2Richard Smith  AutoType *AT = new (*this, TypeAlignment) AutoType(DeducedType);
2891483b9f3bc05c5409e2c6643f1c9d91e21c8ff9d2Richard Smith  Types.push_back(AT);
2892483b9f3bc05c5409e2c6643f1c9d91e21c8ff9d2Richard Smith  if (InsertPos)
2893483b9f3bc05c5409e2c6643f1c9d91e21c8ff9d2Richard Smith    AutoTypes.InsertNode(AT, InsertPos);
2894483b9f3bc05c5409e2c6643f1c9d91e21c8ff9d2Richard Smith  return QualType(AT, 0);
289534b41d939a1328f484511c6002ba2456db879a29Richard Smith}
289634b41d939a1328f484511c6002ba2456db879a29Richard Smith
2897ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith/// getAutoDeductType - Get type pattern for deducing against 'auto'.
2898ad762fcdc16b9e4705b12b09d92b8c026212b906Richard SmithQualType ASTContext::getAutoDeductType() const {
2899ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith  if (AutoDeductTy.isNull())
2900ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith    AutoDeductTy = getAutoType(QualType());
2901ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith  assert(!AutoDeductTy.isNull() && "can't build 'auto' pattern");
2902ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith  return AutoDeductTy;
2903ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith}
2904ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith
2905ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith/// getAutoRRefDeductType - Get type pattern for deducing against 'auto &&'.
2906ad762fcdc16b9e4705b12b09d92b8c026212b906Richard SmithQualType ASTContext::getAutoRRefDeductType() const {
2907ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith  if (AutoRRefDeductTy.isNull())
2908ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith    AutoRRefDeductTy = getRValueReferenceType(getAutoDeductType());
2909ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith  assert(!AutoRRefDeductTy.isNull() && "can't build 'auto &&' pattern");
2910ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith  return AutoRRefDeductTy;
2911ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith}
2912ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith
29135f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// getTagDeclType - Return the unique reference to the type for the
29145f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// specified TagDecl (struct/union/class/enum) decl.
29154ba2a17694148e16eaa8d3917f657ffcd3667be4Jay FoadQualType ASTContext::getTagDeclType(const TagDecl *Decl) const {
2916d778f88d32b96a74c9edb7342c81357606a7cdc0Ted Kremenek  assert (Decl);
2917e607ed068334bacb8d7b093996b4671c6ca79e25Mike Stump  // FIXME: What is the design on getTagDeclType when it requires casting
2918e607ed068334bacb8d7b093996b4671c6ca79e25Mike Stump  // away const?  mutable?
2919e607ed068334bacb8d7b093996b4671c6ca79e25Mike Stump  return getTypeDeclType(const_cast<TagDecl*>(Decl));
29205f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
29215f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
29221eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// getSizeType - Return the unique type for "size_t" (C99 7.17), the result
29231eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// of the sizeof operator (C99 6.5.3.4p4). The value is target dependent and
29241eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// needs to agree with the definition in <stddef.h>.
2925a3ccda58913cc1a4b8564e349448b12acc462da7Anders CarlssonCanQualType ASTContext::getSizeType() const {
2926b4e66d5259f90e9aae4d40fc5de801e046c7df94Douglas Gregor  return getFromTargetType(Target.getSizeType());
29275f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
29285f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
292964c438a4be2a871fa43c78264663ba1e9788b94dArgyrios Kyrtzidis/// getSignedWCharType - Return the type of "signed wchar_t".
293064c438a4be2a871fa43c78264663ba1e9788b94dArgyrios Kyrtzidis/// Used when in C++, as a GCC extension.
293164c438a4be2a871fa43c78264663ba1e9788b94dArgyrios KyrtzidisQualType ASTContext::getSignedWCharType() const {
293264c438a4be2a871fa43c78264663ba1e9788b94dArgyrios Kyrtzidis  // FIXME: derive from "Target" ?
293364c438a4be2a871fa43c78264663ba1e9788b94dArgyrios Kyrtzidis  return WCharTy;
293464c438a4be2a871fa43c78264663ba1e9788b94dArgyrios Kyrtzidis}
293564c438a4be2a871fa43c78264663ba1e9788b94dArgyrios Kyrtzidis
293664c438a4be2a871fa43c78264663ba1e9788b94dArgyrios Kyrtzidis/// getUnsignedWCharType - Return the type of "unsigned wchar_t".
293764c438a4be2a871fa43c78264663ba1e9788b94dArgyrios Kyrtzidis/// Used when in C++, as a GCC extension.
293864c438a4be2a871fa43c78264663ba1e9788b94dArgyrios KyrtzidisQualType ASTContext::getUnsignedWCharType() const {
293964c438a4be2a871fa43c78264663ba1e9788b94dArgyrios Kyrtzidis  // FIXME: derive from "Target" ?
294064c438a4be2a871fa43c78264663ba1e9788b94dArgyrios Kyrtzidis  return UnsignedIntTy;
294164c438a4be2a871fa43c78264663ba1e9788b94dArgyrios Kyrtzidis}
294264c438a4be2a871fa43c78264663ba1e9788b94dArgyrios Kyrtzidis
29438b9023ba35a86838789e2c9034a6128728c547aaChris Lattner/// getPointerDiffType - Return the unique type for "ptrdiff_t" (ref?)
29448b9023ba35a86838789e2c9034a6128728c547aaChris Lattner/// defined in <stddef.h>. Pointer - pointer requires this (C99 6.5.6p9).
29458b9023ba35a86838789e2c9034a6128728c547aaChris LattnerQualType ASTContext::getPointerDiffType() const {
2946b4e66d5259f90e9aae4d40fc5de801e046c7df94Douglas Gregor  return getFromTargetType(Target.getPtrDiffType(0));
29478b9023ba35a86838789e2c9034a6128728c547aaChris Lattner}
29488b9023ba35a86838789e2c9034a6128728c547aaChris Lattner
2949e6327747b72bb687c948270f702ff53c30f411a6Chris Lattner//===----------------------------------------------------------------------===//
2950e6327747b72bb687c948270f702ff53c30f411a6Chris Lattner//                              Type Operators
2951e6327747b72bb687c948270f702ff53c30f411a6Chris Lattner//===----------------------------------------------------------------------===//
2952e6327747b72bb687c948270f702ff53c30f411a6Chris Lattner
29534ba2a17694148e16eaa8d3917f657ffcd3667be4Jay FoadCanQualType ASTContext::getCanonicalParamType(QualType T) const {
295454e14c4db764c0636160d26c5bbf491637c83a76John McCall  // Push qualifiers into arrays, and then discard any remaining
295554e14c4db764c0636160d26c5bbf491637c83a76John McCall  // qualifiers.
295654e14c4db764c0636160d26c5bbf491637c83a76John McCall  T = getCanonicalType(T);
2957745da3a5bb4ea35f93f50301e7fbbb7d78d3b6bbFariborz Jahanian  T = getVariableArrayDecayedType(T);
295854e14c4db764c0636160d26c5bbf491637c83a76John McCall  const Type *Ty = T.getTypePtr();
295954e14c4db764c0636160d26c5bbf491637c83a76John McCall  QualType Result;
296054e14c4db764c0636160d26c5bbf491637c83a76John McCall  if (isa<ArrayType>(Ty)) {
296154e14c4db764c0636160d26c5bbf491637c83a76John McCall    Result = getArrayDecayedType(QualType(Ty,0));
296254e14c4db764c0636160d26c5bbf491637c83a76John McCall  } else if (isa<FunctionType>(Ty)) {
296354e14c4db764c0636160d26c5bbf491637c83a76John McCall    Result = getPointerType(QualType(Ty, 0));
296454e14c4db764c0636160d26c5bbf491637c83a76John McCall  } else {
296554e14c4db764c0636160d26c5bbf491637c83a76John McCall    Result = QualType(Ty, 0);
296654e14c4db764c0636160d26c5bbf491637c83a76John McCall  }
296754e14c4db764c0636160d26c5bbf491637c83a76John McCall
296854e14c4db764c0636160d26c5bbf491637c83a76John McCall  return CanQualType::CreateUnsafe(Result);
296954e14c4db764c0636160d26c5bbf491637c83a76John McCall}
297054e14c4db764c0636160d26c5bbf491637c83a76John McCall
297162c28c831bbf207cc36e683e7c321fc33bf8928cJohn McCallQualType ASTContext::getUnqualifiedArrayType(QualType type,
297262c28c831bbf207cc36e683e7c321fc33bf8928cJohn McCall                                             Qualifiers &quals) {
297362c28c831bbf207cc36e683e7c321fc33bf8928cJohn McCall  SplitQualType splitType = type.getSplitUnqualifiedType();
297462c28c831bbf207cc36e683e7c321fc33bf8928cJohn McCall
297562c28c831bbf207cc36e683e7c321fc33bf8928cJohn McCall  // FIXME: getSplitUnqualifiedType() actually walks all the way to
297662c28c831bbf207cc36e683e7c321fc33bf8928cJohn McCall  // the unqualified desugared type and then drops it on the floor.
297762c28c831bbf207cc36e683e7c321fc33bf8928cJohn McCall  // We then have to strip that sugar back off with
297862c28c831bbf207cc36e683e7c321fc33bf8928cJohn McCall  // getUnqualifiedDesugaredType(), which is silly.
297962c28c831bbf207cc36e683e7c321fc33bf8928cJohn McCall  const ArrayType *AT =
298062c28c831bbf207cc36e683e7c321fc33bf8928cJohn McCall    dyn_cast<ArrayType>(splitType.first->getUnqualifiedDesugaredType());
298162c28c831bbf207cc36e683e7c321fc33bf8928cJohn McCall
298262c28c831bbf207cc36e683e7c321fc33bf8928cJohn McCall  // If we don't have an array, just use the results in splitType.
29839dadd94e1c17fa030d1f88d8f2113ff59ccc6714Douglas Gregor  if (!AT) {
298462c28c831bbf207cc36e683e7c321fc33bf8928cJohn McCall    quals = splitType.second;
298562c28c831bbf207cc36e683e7c321fc33bf8928cJohn McCall    return QualType(splitType.first, 0);
298628e318cc6008c2bc008f0caee70dc736a03d6289Chandler Carruth  }
298728e318cc6008c2bc008f0caee70dc736a03d6289Chandler Carruth
298862c28c831bbf207cc36e683e7c321fc33bf8928cJohn McCall  // Otherwise, recurse on the array's element type.
298962c28c831bbf207cc36e683e7c321fc33bf8928cJohn McCall  QualType elementType = AT->getElementType();
299062c28c831bbf207cc36e683e7c321fc33bf8928cJohn McCall  QualType unqualElementType = getUnqualifiedArrayType(elementType, quals);
299162c28c831bbf207cc36e683e7c321fc33bf8928cJohn McCall
299262c28c831bbf207cc36e683e7c321fc33bf8928cJohn McCall  // If that didn't change the element type, AT has no qualifiers, so we
299362c28c831bbf207cc36e683e7c321fc33bf8928cJohn McCall  // can just use the results in splitType.
299462c28c831bbf207cc36e683e7c321fc33bf8928cJohn McCall  if (elementType == unqualElementType) {
299562c28c831bbf207cc36e683e7c321fc33bf8928cJohn McCall    assert(quals.empty()); // from the recursive call
299662c28c831bbf207cc36e683e7c321fc33bf8928cJohn McCall    quals = splitType.second;
299762c28c831bbf207cc36e683e7c321fc33bf8928cJohn McCall    return QualType(splitType.first, 0);
299862c28c831bbf207cc36e683e7c321fc33bf8928cJohn McCall  }
299962c28c831bbf207cc36e683e7c321fc33bf8928cJohn McCall
300062c28c831bbf207cc36e683e7c321fc33bf8928cJohn McCall  // Otherwise, add in the qualifiers from the outermost type, then
300162c28c831bbf207cc36e683e7c321fc33bf8928cJohn McCall  // build the type back up.
300262c28c831bbf207cc36e683e7c321fc33bf8928cJohn McCall  quals.addConsistentQualifiers(splitType.second);
300328e318cc6008c2bc008f0caee70dc736a03d6289Chandler Carruth
30049dadd94e1c17fa030d1f88d8f2113ff59ccc6714Douglas Gregor  if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(AT)) {
300562c28c831bbf207cc36e683e7c321fc33bf8928cJohn McCall    return getConstantArrayType(unqualElementType, CAT->getSize(),
300628e318cc6008c2bc008f0caee70dc736a03d6289Chandler Carruth                                CAT->getSizeModifier(), 0);
300728e318cc6008c2bc008f0caee70dc736a03d6289Chandler Carruth  }
300828e318cc6008c2bc008f0caee70dc736a03d6289Chandler Carruth
30099dadd94e1c17fa030d1f88d8f2113ff59ccc6714Douglas Gregor  if (const IncompleteArrayType *IAT = dyn_cast<IncompleteArrayType>(AT)) {
301062c28c831bbf207cc36e683e7c321fc33bf8928cJohn McCall    return getIncompleteArrayType(unqualElementType, IAT->getSizeModifier(), 0);
301128e318cc6008c2bc008f0caee70dc736a03d6289Chandler Carruth  }
301228e318cc6008c2bc008f0caee70dc736a03d6289Chandler Carruth
30139dadd94e1c17fa030d1f88d8f2113ff59ccc6714Douglas Gregor  if (const VariableArrayType *VAT = dyn_cast<VariableArrayType>(AT)) {
301462c28c831bbf207cc36e683e7c321fc33bf8928cJohn McCall    return getVariableArrayType(unqualElementType,
30153fa5cae9b3812cab9fab6c042c3329bb70a3d046John McCall                                VAT->getSizeExpr(),
30169dadd94e1c17fa030d1f88d8f2113ff59ccc6714Douglas Gregor                                VAT->getSizeModifier(),
30179dadd94e1c17fa030d1f88d8f2113ff59ccc6714Douglas Gregor                                VAT->getIndexTypeCVRQualifiers(),
30189dadd94e1c17fa030d1f88d8f2113ff59ccc6714Douglas Gregor                                VAT->getBracketsRange());
30199dadd94e1c17fa030d1f88d8f2113ff59ccc6714Douglas Gregor  }
30209dadd94e1c17fa030d1f88d8f2113ff59ccc6714Douglas Gregor
30219dadd94e1c17fa030d1f88d8f2113ff59ccc6714Douglas Gregor  const DependentSizedArrayType *DSAT = cast<DependentSizedArrayType>(AT);
302262c28c831bbf207cc36e683e7c321fc33bf8928cJohn McCall  return getDependentSizedArrayType(unqualElementType, DSAT->getSizeExpr(),
302328e318cc6008c2bc008f0caee70dc736a03d6289Chandler Carruth                                    DSAT->getSizeModifier(), 0,
302428e318cc6008c2bc008f0caee70dc736a03d6289Chandler Carruth                                    SourceRange());
302528e318cc6008c2bc008f0caee70dc736a03d6289Chandler Carruth}
302628e318cc6008c2bc008f0caee70dc736a03d6289Chandler Carruth
30275a57efd7bf88a4a13018e0471ded8063a4abe8afDouglas Gregor/// UnwrapSimilarPointerTypes - If T1 and T2 are pointer types  that
30285a57efd7bf88a4a13018e0471ded8063a4abe8afDouglas Gregor/// may be similar (C++ 4.4), replaces T1 and T2 with the type that
30295a57efd7bf88a4a13018e0471ded8063a4abe8afDouglas Gregor/// they point to and return true. If T1 and T2 aren't pointer types
30305a57efd7bf88a4a13018e0471ded8063a4abe8afDouglas Gregor/// or pointer-to-member types, or if they are not similar at this
30315a57efd7bf88a4a13018e0471ded8063a4abe8afDouglas Gregor/// level, returns false and leaves T1 and T2 unchanged. Top-level
30325a57efd7bf88a4a13018e0471ded8063a4abe8afDouglas Gregor/// qualifiers on T1 and T2 are ignored. This function will typically
30335a57efd7bf88a4a13018e0471ded8063a4abe8afDouglas Gregor/// be called in a loop that successively "unwraps" pointer and
30345a57efd7bf88a4a13018e0471ded8063a4abe8afDouglas Gregor/// pointer-to-member types to compare them at each level.
30355a57efd7bf88a4a13018e0471ded8063a4abe8afDouglas Gregorbool ASTContext::UnwrapSimilarPointerTypes(QualType &T1, QualType &T2) {
30365a57efd7bf88a4a13018e0471ded8063a4abe8afDouglas Gregor  const PointerType *T1PtrType = T1->getAs<PointerType>(),
30375a57efd7bf88a4a13018e0471ded8063a4abe8afDouglas Gregor                    *T2PtrType = T2->getAs<PointerType>();
30385a57efd7bf88a4a13018e0471ded8063a4abe8afDouglas Gregor  if (T1PtrType && T2PtrType) {
30395a57efd7bf88a4a13018e0471ded8063a4abe8afDouglas Gregor    T1 = T1PtrType->getPointeeType();
30405a57efd7bf88a4a13018e0471ded8063a4abe8afDouglas Gregor    T2 = T2PtrType->getPointeeType();
30415a57efd7bf88a4a13018e0471ded8063a4abe8afDouglas Gregor    return true;
30425a57efd7bf88a4a13018e0471ded8063a4abe8afDouglas Gregor  }
30435a57efd7bf88a4a13018e0471ded8063a4abe8afDouglas Gregor
30445a57efd7bf88a4a13018e0471ded8063a4abe8afDouglas Gregor  const MemberPointerType *T1MPType = T1->getAs<MemberPointerType>(),
30455a57efd7bf88a4a13018e0471ded8063a4abe8afDouglas Gregor                          *T2MPType = T2->getAs<MemberPointerType>();
30465a57efd7bf88a4a13018e0471ded8063a4abe8afDouglas Gregor  if (T1MPType && T2MPType &&
30475a57efd7bf88a4a13018e0471ded8063a4abe8afDouglas Gregor      hasSameUnqualifiedType(QualType(T1MPType->getClass(), 0),
30485a57efd7bf88a4a13018e0471ded8063a4abe8afDouglas Gregor                             QualType(T2MPType->getClass(), 0))) {
30495a57efd7bf88a4a13018e0471ded8063a4abe8afDouglas Gregor    T1 = T1MPType->getPointeeType();
30505a57efd7bf88a4a13018e0471ded8063a4abe8afDouglas Gregor    T2 = T2MPType->getPointeeType();
30515a57efd7bf88a4a13018e0471ded8063a4abe8afDouglas Gregor    return true;
30525a57efd7bf88a4a13018e0471ded8063a4abe8afDouglas Gregor  }
30535a57efd7bf88a4a13018e0471ded8063a4abe8afDouglas Gregor
30545a57efd7bf88a4a13018e0471ded8063a4abe8afDouglas Gregor  if (getLangOptions().ObjC1) {
30555a57efd7bf88a4a13018e0471ded8063a4abe8afDouglas Gregor    const ObjCObjectPointerType *T1OPType = T1->getAs<ObjCObjectPointerType>(),
30565a57efd7bf88a4a13018e0471ded8063a4abe8afDouglas Gregor                                *T2OPType = T2->getAs<ObjCObjectPointerType>();
30575a57efd7bf88a4a13018e0471ded8063a4abe8afDouglas Gregor    if (T1OPType && T2OPType) {
30585a57efd7bf88a4a13018e0471ded8063a4abe8afDouglas Gregor      T1 = T1OPType->getPointeeType();
30595a57efd7bf88a4a13018e0471ded8063a4abe8afDouglas Gregor      T2 = T2OPType->getPointeeType();
30605a57efd7bf88a4a13018e0471ded8063a4abe8afDouglas Gregor      return true;
30615a57efd7bf88a4a13018e0471ded8063a4abe8afDouglas Gregor    }
30625a57efd7bf88a4a13018e0471ded8063a4abe8afDouglas Gregor  }
30635a57efd7bf88a4a13018e0471ded8063a4abe8afDouglas Gregor
30645a57efd7bf88a4a13018e0471ded8063a4abe8afDouglas Gregor  // FIXME: Block pointers, too?
30655a57efd7bf88a4a13018e0471ded8063a4abe8afDouglas Gregor
30665a57efd7bf88a4a13018e0471ded8063a4abe8afDouglas Gregor  return false;
30675a57efd7bf88a4a13018e0471ded8063a4abe8afDouglas Gregor}
30685a57efd7bf88a4a13018e0471ded8063a4abe8afDouglas Gregor
30694ba2a17694148e16eaa8d3917f657ffcd3667be4Jay FoadDeclarationNameInfo
30704ba2a17694148e16eaa8d3917f657ffcd3667be4Jay FoadASTContext::getNameForTemplate(TemplateName Name,
30714ba2a17694148e16eaa8d3917f657ffcd3667be4Jay Foad                               SourceLocation NameLoc) const {
3072146060435c3efce95c95a092c7a1eb651cfb9ae0John McCall  switch (Name.getKind()) {
3073146060435c3efce95c95a092c7a1eb651cfb9ae0John McCall  case TemplateName::QualifiedTemplate:
3074146060435c3efce95c95a092c7a1eb651cfb9ae0John McCall  case TemplateName::Template:
30752577743c5650c646fb705df01403707e94f2df04Abramo Bagnara    // DNInfo work in progress: CHECKME: what about DNLoc?
3076146060435c3efce95c95a092c7a1eb651cfb9ae0John McCall    return DeclarationNameInfo(Name.getAsTemplateDecl()->getDeclName(),
3077146060435c3efce95c95a092c7a1eb651cfb9ae0John McCall                               NameLoc);
30782577743c5650c646fb705df01403707e94f2df04Abramo Bagnara
3079146060435c3efce95c95a092c7a1eb651cfb9ae0John McCall  case TemplateName::OverloadedTemplate: {
3080146060435c3efce95c95a092c7a1eb651cfb9ae0John McCall    OverloadedTemplateStorage *Storage = Name.getAsOverloadedTemplate();
3081146060435c3efce95c95a092c7a1eb651cfb9ae0John McCall    // DNInfo work in progress: CHECKME: what about DNLoc?
3082146060435c3efce95c95a092c7a1eb651cfb9ae0John McCall    return DeclarationNameInfo((*Storage->begin())->getDeclName(), NameLoc);
3083146060435c3efce95c95a092c7a1eb651cfb9ae0John McCall  }
3084146060435c3efce95c95a092c7a1eb651cfb9ae0John McCall
3085146060435c3efce95c95a092c7a1eb651cfb9ae0John McCall  case TemplateName::DependentTemplate: {
3086146060435c3efce95c95a092c7a1eb651cfb9ae0John McCall    DependentTemplateName *DTN = Name.getAsDependentTemplateName();
30872577743c5650c646fb705df01403707e94f2df04Abramo Bagnara    DeclarationName DName;
308880ad16f4b2b350ddbaae21a52975e63df5aafc2cJohn McCall    if (DTN->isIdentifier()) {
30892577743c5650c646fb705df01403707e94f2df04Abramo Bagnara      DName = DeclarationNames.getIdentifier(DTN->getIdentifier());
30902577743c5650c646fb705df01403707e94f2df04Abramo Bagnara      return DeclarationNameInfo(DName, NameLoc);
309180ad16f4b2b350ddbaae21a52975e63df5aafc2cJohn McCall    } else {
30922577743c5650c646fb705df01403707e94f2df04Abramo Bagnara      DName = DeclarationNames.getCXXOperatorName(DTN->getOperator());
30932577743c5650c646fb705df01403707e94f2df04Abramo Bagnara      // DNInfo work in progress: FIXME: source locations?
30942577743c5650c646fb705df01403707e94f2df04Abramo Bagnara      DeclarationNameLoc DNLoc;
30952577743c5650c646fb705df01403707e94f2df04Abramo Bagnara      DNLoc.CXXOperatorName.BeginOpNameLoc = SourceLocation().getRawEncoding();
30962577743c5650c646fb705df01403707e94f2df04Abramo Bagnara      DNLoc.CXXOperatorName.EndOpNameLoc = SourceLocation().getRawEncoding();
30972577743c5650c646fb705df01403707e94f2df04Abramo Bagnara      return DeclarationNameInfo(DName, NameLoc, DNLoc);
309880ad16f4b2b350ddbaae21a52975e63df5aafc2cJohn McCall    }
309980ad16f4b2b350ddbaae21a52975e63df5aafc2cJohn McCall  }
310080ad16f4b2b350ddbaae21a52975e63df5aafc2cJohn McCall
3101146060435c3efce95c95a092c7a1eb651cfb9ae0John McCall  case TemplateName::SubstTemplateTemplateParm: {
3102146060435c3efce95c95a092c7a1eb651cfb9ae0John McCall    SubstTemplateTemplateParmStorage *subst
3103146060435c3efce95c95a092c7a1eb651cfb9ae0John McCall      = Name.getAsSubstTemplateTemplateParm();
3104146060435c3efce95c95a092c7a1eb651cfb9ae0John McCall    return DeclarationNameInfo(subst->getParameter()->getDeclName(),
3105146060435c3efce95c95a092c7a1eb651cfb9ae0John McCall                               NameLoc);
3106146060435c3efce95c95a092c7a1eb651cfb9ae0John McCall  }
3107146060435c3efce95c95a092c7a1eb651cfb9ae0John McCall
3108146060435c3efce95c95a092c7a1eb651cfb9ae0John McCall  case TemplateName::SubstTemplateTemplateParmPack: {
3109146060435c3efce95c95a092c7a1eb651cfb9ae0John McCall    SubstTemplateTemplateParmPackStorage *subst
3110146060435c3efce95c95a092c7a1eb651cfb9ae0John McCall      = Name.getAsSubstTemplateTemplateParmPack();
3111146060435c3efce95c95a092c7a1eb651cfb9ae0John McCall    return DeclarationNameInfo(subst->getParameterPack()->getDeclName(),
3112146060435c3efce95c95a092c7a1eb651cfb9ae0John McCall                               NameLoc);
3113146060435c3efce95c95a092c7a1eb651cfb9ae0John McCall  }
3114146060435c3efce95c95a092c7a1eb651cfb9ae0John McCall  }
3115146060435c3efce95c95a092c7a1eb651cfb9ae0John McCall
3116146060435c3efce95c95a092c7a1eb651cfb9ae0John McCall  llvm_unreachable("bad template name kind!");
311780ad16f4b2b350ddbaae21a52975e63df5aafc2cJohn McCall}
311880ad16f4b2b350ddbaae21a52975e63df5aafc2cJohn McCall
31194ba2a17694148e16eaa8d3917f657ffcd3667be4Jay FoadTemplateName ASTContext::getCanonicalTemplateName(TemplateName Name) const {
3120146060435c3efce95c95a092c7a1eb651cfb9ae0John McCall  switch (Name.getKind()) {
3121146060435c3efce95c95a092c7a1eb651cfb9ae0John McCall  case TemplateName::QualifiedTemplate:
3122146060435c3efce95c95a092c7a1eb651cfb9ae0John McCall  case TemplateName::Template: {
3123146060435c3efce95c95a092c7a1eb651cfb9ae0John McCall    TemplateDecl *Template = Name.getAsTemplateDecl();
31243e1274f2b99cb99c03cc8e2c6517c37d330b597aDouglas Gregor    if (TemplateTemplateParmDecl *TTP
3125146060435c3efce95c95a092c7a1eb651cfb9ae0John McCall          = dyn_cast<TemplateTemplateParmDecl>(Template))
31263e1274f2b99cb99c03cc8e2c6517c37d330b597aDouglas Gregor      Template = getCanonicalTemplateTemplateParmDecl(TTP);
31273e1274f2b99cb99c03cc8e2c6517c37d330b597aDouglas Gregor
31283e1274f2b99cb99c03cc8e2c6517c37d330b597aDouglas Gregor    // The canonical template name is the canonical template declaration.
312997fbaa2a38804268a024f1a104b43fcf8b4411b0Argyrios Kyrtzidis    return TemplateName(cast<TemplateDecl>(Template->getCanonicalDecl()));
31303e1274f2b99cb99c03cc8e2c6517c37d330b597aDouglas Gregor  }
313125a3ef7cc5fd55dc8cc67c6e6770c8595657e082Douglas Gregor
3132146060435c3efce95c95a092c7a1eb651cfb9ae0John McCall  case TemplateName::OverloadedTemplate:
3133146060435c3efce95c95a092c7a1eb651cfb9ae0John McCall    llvm_unreachable("cannot canonicalize overloaded template");
3134146060435c3efce95c95a092c7a1eb651cfb9ae0John McCall
3135146060435c3efce95c95a092c7a1eb651cfb9ae0John McCall  case TemplateName::DependentTemplate: {
3136146060435c3efce95c95a092c7a1eb651cfb9ae0John McCall    DependentTemplateName *DTN = Name.getAsDependentTemplateName();
3137146060435c3efce95c95a092c7a1eb651cfb9ae0John McCall    assert(DTN && "Non-dependent template names must refer to template decls.");
3138146060435c3efce95c95a092c7a1eb651cfb9ae0John McCall    return DTN->CanonicalTemplateName;
3139146060435c3efce95c95a092c7a1eb651cfb9ae0John McCall  }
3140146060435c3efce95c95a092c7a1eb651cfb9ae0John McCall
3141146060435c3efce95c95a092c7a1eb651cfb9ae0John McCall  case TemplateName::SubstTemplateTemplateParm: {
3142146060435c3efce95c95a092c7a1eb651cfb9ae0John McCall    SubstTemplateTemplateParmStorage *subst
3143146060435c3efce95c95a092c7a1eb651cfb9ae0John McCall      = Name.getAsSubstTemplateTemplateParm();
3144146060435c3efce95c95a092c7a1eb651cfb9ae0John McCall    return getCanonicalTemplateName(subst->getReplacement());
31451aee05d08b2184acadeb36de300e216390780d6cDouglas Gregor  }
31461eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
3147146060435c3efce95c95a092c7a1eb651cfb9ae0John McCall  case TemplateName::SubstTemplateTemplateParmPack: {
3148146060435c3efce95c95a092c7a1eb651cfb9ae0John McCall    SubstTemplateTemplateParmPackStorage *subst
3149146060435c3efce95c95a092c7a1eb651cfb9ae0John McCall                                  = Name.getAsSubstTemplateTemplateParmPack();
3150146060435c3efce95c95a092c7a1eb651cfb9ae0John McCall    TemplateTemplateParmDecl *canonParameter
3151146060435c3efce95c95a092c7a1eb651cfb9ae0John McCall      = getCanonicalTemplateTemplateParmDecl(subst->getParameterPack());
3152146060435c3efce95c95a092c7a1eb651cfb9ae0John McCall    TemplateArgument canonArgPack
3153146060435c3efce95c95a092c7a1eb651cfb9ae0John McCall      = getCanonicalTemplateArgument(subst->getArgumentPack());
3154146060435c3efce95c95a092c7a1eb651cfb9ae0John McCall    return getSubstTemplateTemplateParmPack(canonParameter, canonArgPack);
3155146060435c3efce95c95a092c7a1eb651cfb9ae0John McCall  }
3156146060435c3efce95c95a092c7a1eb651cfb9ae0John McCall  }
3157146060435c3efce95c95a092c7a1eb651cfb9ae0John McCall
3158146060435c3efce95c95a092c7a1eb651cfb9ae0John McCall  llvm_unreachable("bad template name!");
315925a3ef7cc5fd55dc8cc67c6e6770c8595657e082Douglas Gregor}
316025a3ef7cc5fd55dc8cc67c6e6770c8595657e082Douglas Gregor
3161db0d4b751e83b8841b8f48f913f17e50467f13d4Douglas Gregorbool ASTContext::hasSameTemplateName(TemplateName X, TemplateName Y) {
3162db0d4b751e83b8841b8f48f913f17e50467f13d4Douglas Gregor  X = getCanonicalTemplateName(X);
3163db0d4b751e83b8841b8f48f913f17e50467f13d4Douglas Gregor  Y = getCanonicalTemplateName(Y);
3164db0d4b751e83b8841b8f48f913f17e50467f13d4Douglas Gregor  return X.getAsVoidPointer() == Y.getAsVoidPointer();
3165db0d4b751e83b8841b8f48f913f17e50467f13d4Douglas Gregor}
3166db0d4b751e83b8841b8f48f913f17e50467f13d4Douglas Gregor
31671eb4433ac451dc16f4133a88af2d002ac26c58efMike StumpTemplateArgument
31684ba2a17694148e16eaa8d3917f657ffcd3667be4Jay FoadASTContext::getCanonicalTemplateArgument(const TemplateArgument &Arg) const {
31691275ae098acda31fe0e434510c729fcfed0458a1Douglas Gregor  switch (Arg.getKind()) {
31701275ae098acda31fe0e434510c729fcfed0458a1Douglas Gregor    case TemplateArgument::Null:
31711275ae098acda31fe0e434510c729fcfed0458a1Douglas Gregor      return Arg;
31721eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
31731275ae098acda31fe0e434510c729fcfed0458a1Douglas Gregor    case TemplateArgument::Expression:
31741275ae098acda31fe0e434510c729fcfed0458a1Douglas Gregor      return Arg;
31751eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
31761275ae098acda31fe0e434510c729fcfed0458a1Douglas Gregor    case TemplateArgument::Declaration:
3177833ca991c1bfc967f0995974ca86f66ba1f666b5John McCall      return TemplateArgument(Arg.getAsDecl()->getCanonicalDecl());
31781eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
3179788cd06cf8e868a67158aafec5de3a1f408d14f3Douglas Gregor    case TemplateArgument::Template:
3180788cd06cf8e868a67158aafec5de3a1f408d14f3Douglas Gregor      return TemplateArgument(getCanonicalTemplateName(Arg.getAsTemplate()));
3181a7fc901a2e39bfe55bfcff5934b2d9fdf9656491Douglas Gregor
3182a7fc901a2e39bfe55bfcff5934b2d9fdf9656491Douglas Gregor    case TemplateArgument::TemplateExpansion:
3183a7fc901a2e39bfe55bfcff5934b2d9fdf9656491Douglas Gregor      return TemplateArgument(getCanonicalTemplateName(
3184a7fc901a2e39bfe55bfcff5934b2d9fdf9656491Douglas Gregor                                         Arg.getAsTemplateOrTemplatePattern()),
31852be29f423acad3bbe39099a78db2805acb5bdf17Douglas Gregor                              Arg.getNumTemplateExpansions());
3186a7fc901a2e39bfe55bfcff5934b2d9fdf9656491Douglas Gregor
31871275ae098acda31fe0e434510c729fcfed0458a1Douglas Gregor    case TemplateArgument::Integral:
3188833ca991c1bfc967f0995974ca86f66ba1f666b5John McCall      return TemplateArgument(*Arg.getAsIntegral(),
31891275ae098acda31fe0e434510c729fcfed0458a1Douglas Gregor                              getCanonicalType(Arg.getIntegralType()));
31901eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
31911275ae098acda31fe0e434510c729fcfed0458a1Douglas Gregor    case TemplateArgument::Type:
3192833ca991c1bfc967f0995974ca86f66ba1f666b5John McCall      return TemplateArgument(getCanonicalType(Arg.getAsType()));
31931eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
31941275ae098acda31fe0e434510c729fcfed0458a1Douglas Gregor    case TemplateArgument::Pack: {
319587dd697dcc8ecb64df73ae64d61b8c80ff0c157cDouglas Gregor      if (Arg.pack_size() == 0)
319687dd697dcc8ecb64df73ae64d61b8c80ff0c157cDouglas Gregor        return Arg;
319787dd697dcc8ecb64df73ae64d61b8c80ff0c157cDouglas Gregor
3198910f8008fea79120489a53593fe971b0b8a4a740Douglas Gregor      TemplateArgument *CanonArgs
3199910f8008fea79120489a53593fe971b0b8a4a740Douglas Gregor        = new (*this) TemplateArgument[Arg.pack_size()];
32001275ae098acda31fe0e434510c729fcfed0458a1Douglas Gregor      unsigned Idx = 0;
32011eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      for (TemplateArgument::pack_iterator A = Arg.pack_begin(),
32021275ae098acda31fe0e434510c729fcfed0458a1Douglas Gregor                                        AEnd = Arg.pack_end();
32031275ae098acda31fe0e434510c729fcfed0458a1Douglas Gregor           A != AEnd; (void)++A, ++Idx)
32041275ae098acda31fe0e434510c729fcfed0458a1Douglas Gregor        CanonArgs[Idx] = getCanonicalTemplateArgument(*A);
32051eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
3206910f8008fea79120489a53593fe971b0b8a4a740Douglas Gregor      return TemplateArgument(CanonArgs, Arg.pack_size());
32071275ae098acda31fe0e434510c729fcfed0458a1Douglas Gregor    }
32081275ae098acda31fe0e434510c729fcfed0458a1Douglas Gregor  }
32091275ae098acda31fe0e434510c729fcfed0458a1Douglas Gregor
32101275ae098acda31fe0e434510c729fcfed0458a1Douglas Gregor  // Silence GCC warning
32111275ae098acda31fe0e434510c729fcfed0458a1Douglas Gregor  assert(false && "Unhandled template argument kind");
32121275ae098acda31fe0e434510c729fcfed0458a1Douglas Gregor  return TemplateArgument();
32131275ae098acda31fe0e434510c729fcfed0458a1Douglas Gregor}
32141275ae098acda31fe0e434510c729fcfed0458a1Douglas Gregor
3215d57959af02b4af695276f4204443afe6e5d86bd8Douglas GregorNestedNameSpecifier *
32164ba2a17694148e16eaa8d3917f657ffcd3667be4Jay FoadASTContext::getCanonicalNestedNameSpecifier(NestedNameSpecifier *NNS) const {
32171eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  if (!NNS)
3218d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor    return 0;
3219d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor
3220d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor  switch (NNS->getKind()) {
3221d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor  case NestedNameSpecifier::Identifier:
3222d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor    // Canonicalize the prefix but keep the identifier the same.
32231eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    return NestedNameSpecifier::Create(*this,
3224d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor                         getCanonicalNestedNameSpecifier(NNS->getPrefix()),
3225d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor                                       NNS->getAsIdentifier());
3226d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor
3227d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor  case NestedNameSpecifier::Namespace:
3228d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor    // A namespace is canonical; build a nested-name-specifier with
3229d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor    // this namespace and no prefix.
323014aba76042e041b2c5e439bf4ae353a0a3c7fd73Douglas Gregor    return NestedNameSpecifier::Create(*this, 0,
323114aba76042e041b2c5e439bf4ae353a0a3c7fd73Douglas Gregor                                 NNS->getAsNamespace()->getOriginalNamespace());
323214aba76042e041b2c5e439bf4ae353a0a3c7fd73Douglas Gregor
323314aba76042e041b2c5e439bf4ae353a0a3c7fd73Douglas Gregor  case NestedNameSpecifier::NamespaceAlias:
323414aba76042e041b2c5e439bf4ae353a0a3c7fd73Douglas Gregor    // A namespace is canonical; build a nested-name-specifier with
323514aba76042e041b2c5e439bf4ae353a0a3c7fd73Douglas Gregor    // this namespace and no prefix.
323614aba76042e041b2c5e439bf4ae353a0a3c7fd73Douglas Gregor    return NestedNameSpecifier::Create(*this, 0,
323714aba76042e041b2c5e439bf4ae353a0a3c7fd73Douglas Gregor                                    NNS->getAsNamespaceAlias()->getNamespace()
323814aba76042e041b2c5e439bf4ae353a0a3c7fd73Douglas Gregor                                                      ->getOriginalNamespace());
3239d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor
3240d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor  case NestedNameSpecifier::TypeSpec:
3241d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor  case NestedNameSpecifier::TypeSpecWithTemplate: {
3242d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor    QualType T = getCanonicalType(QualType(NNS->getAsType(), 0));
3243264bf66d55563dd86a3d7e06738aa427de512d2cDouglas Gregor
3244264bf66d55563dd86a3d7e06738aa427de512d2cDouglas Gregor    // If we have some kind of dependent-named type (e.g., "typename T::type"),
3245264bf66d55563dd86a3d7e06738aa427de512d2cDouglas Gregor    // break it apart into its prefix and identifier, then reconsititute those
3246264bf66d55563dd86a3d7e06738aa427de512d2cDouglas Gregor    // as the canonical nested-name-specifier. This is required to canonicalize
3247264bf66d55563dd86a3d7e06738aa427de512d2cDouglas Gregor    // a dependent nested-name-specifier involving typedefs of dependent-name
3248264bf66d55563dd86a3d7e06738aa427de512d2cDouglas Gregor    // types, e.g.,
3249264bf66d55563dd86a3d7e06738aa427de512d2cDouglas Gregor    //   typedef typename T::type T1;
3250264bf66d55563dd86a3d7e06738aa427de512d2cDouglas Gregor    //   typedef typename T1::type T2;
3251264bf66d55563dd86a3d7e06738aa427de512d2cDouglas Gregor    if (const DependentNameType *DNT = T->getAs<DependentNameType>()) {
3252264bf66d55563dd86a3d7e06738aa427de512d2cDouglas Gregor      NestedNameSpecifier *Prefix
3253264bf66d55563dd86a3d7e06738aa427de512d2cDouglas Gregor        = getCanonicalNestedNameSpecifier(DNT->getQualifier());
3254264bf66d55563dd86a3d7e06738aa427de512d2cDouglas Gregor      return NestedNameSpecifier::Create(*this, Prefix,
3255264bf66d55563dd86a3d7e06738aa427de512d2cDouglas Gregor                           const_cast<IdentifierInfo *>(DNT->getIdentifier()));
3256264bf66d55563dd86a3d7e06738aa427de512d2cDouglas Gregor    }
3257264bf66d55563dd86a3d7e06738aa427de512d2cDouglas Gregor
3258643f84353b3b7bbf9b73bcbc7da0ef8a39a5ca99Douglas Gregor    // Do the same thing as above, but with dependent-named specializations.
3259264bf66d55563dd86a3d7e06738aa427de512d2cDouglas Gregor    if (const DependentTemplateSpecializationType *DTST
3260264bf66d55563dd86a3d7e06738aa427de512d2cDouglas Gregor          = T->getAs<DependentTemplateSpecializationType>()) {
3261264bf66d55563dd86a3d7e06738aa427de512d2cDouglas Gregor      NestedNameSpecifier *Prefix
3262264bf66d55563dd86a3d7e06738aa427de512d2cDouglas Gregor        = getCanonicalNestedNameSpecifier(DTST->getQualifier());
3263aa2187de137e5b809dcbbe14f3b61ae907a3d8aaDouglas Gregor
3264aa2187de137e5b809dcbbe14f3b61ae907a3d8aaDouglas Gregor      T = getDependentTemplateSpecializationType(DTST->getKeyword(),
3265aa2187de137e5b809dcbbe14f3b61ae907a3d8aaDouglas Gregor                                                 Prefix, DTST->getIdentifier(),
3266aa2187de137e5b809dcbbe14f3b61ae907a3d8aaDouglas Gregor                                                 DTST->getNumArgs(),
3267aa2187de137e5b809dcbbe14f3b61ae907a3d8aaDouglas Gregor                                                 DTST->getArgs());
3268264bf66d55563dd86a3d7e06738aa427de512d2cDouglas Gregor      T = getCanonicalType(T);
3269264bf66d55563dd86a3d7e06738aa427de512d2cDouglas Gregor    }
3270264bf66d55563dd86a3d7e06738aa427de512d2cDouglas Gregor
32713b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall    return NestedNameSpecifier::Create(*this, 0, false,
32723b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall                                       const_cast<Type*>(T.getTypePtr()));
3273d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor  }
3274d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor
3275d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor  case NestedNameSpecifier::Global:
3276d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor    // The global specifier is canonical and unique.
3277d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor    return NNS;
3278d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor  }
3279d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor
3280d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor  // Required to silence a GCC warning
3281d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor  return 0;
3282d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor}
3283d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor
3284c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner
32854ba2a17694148e16eaa8d3917f657ffcd3667be4Jay Foadconst ArrayType *ASTContext::getAsArrayType(QualType T) const {
3286c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner  // Handle the non-qualified case efficiently.
3287a4923eb7c4b04d360cb2747641a5e92818edf804Douglas Gregor  if (!T.hasLocalQualifiers()) {
3288c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner    // Handle the common positive case fast.
3289c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner    if (const ArrayType *AT = dyn_cast<ArrayType>(T))
3290c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner      return AT;
3291c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner  }
32921eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
32930953e767ff7817f97b3ab20896b229891eeff45bJohn McCall  // Handle the common negative case fast.
32943b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall  if (!isa<ArrayType>(T.getCanonicalType()))
3295c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner    return 0;
32961eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
32970953e767ff7817f97b3ab20896b229891eeff45bJohn McCall  // Apply any qualifiers from the array type to the element type.  This
3298c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner  // implements C99 6.7.3p8: "If the specification of an array type includes
3299c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner  // any type qualifiers, the element type is so qualified, not the array type."
33001eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
3301c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner  // If we get here, we either have type qualifiers on the type, or we have
3302c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner  // sugar such as a typedef in the way.  If we have type qualifiers on the type
330350d62d1b4a98adbc83de8f8cd1379ea1c25656f7Douglas Gregor  // we must propagate them down into the element type.
33040953e767ff7817f97b3ab20896b229891eeff45bJohn McCall
33053b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall  SplitQualType split = T.getSplitDesugaredType();
33063b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall  Qualifiers qs = split.second;
33071eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
3308c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner  // If we have a simple case, just return now.
33093b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall  const ArrayType *ATy = dyn_cast<ArrayType>(split.first);
33103b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall  if (ATy == 0 || qs.empty())
3311c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner    return ATy;
33121eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
3313c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner  // Otherwise, we have an array and we have qualifiers on it.  Push the
3314c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner  // qualifiers into the array element type and return a new array type.
33153b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall  QualType NewEltTy = getQualifiedType(ATy->getElementType(), qs);
33161eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
3317c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner  if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(ATy))
3318c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner    return cast<ArrayType>(getConstantArrayType(NewEltTy, CAT->getSize(),
3319c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner                                                CAT->getSizeModifier(),
33200953e767ff7817f97b3ab20896b229891eeff45bJohn McCall                                           CAT->getIndexTypeCVRQualifiers()));
3321c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner  if (const IncompleteArrayType *IAT = dyn_cast<IncompleteArrayType>(ATy))
3322c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner    return cast<ArrayType>(getIncompleteArrayType(NewEltTy,
3323c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner                                                  IAT->getSizeModifier(),
33240953e767ff7817f97b3ab20896b229891eeff45bJohn McCall                                           IAT->getIndexTypeCVRQualifiers()));
3325898574e7496ba8fd76290079d3a9d06954992734Douglas Gregor
33261eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  if (const DependentSizedArrayType *DSAT
3327898574e7496ba8fd76290079d3a9d06954992734Douglas Gregor        = dyn_cast<DependentSizedArrayType>(ATy))
3328898574e7496ba8fd76290079d3a9d06954992734Douglas Gregor    return cast<ArrayType>(
33291eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump                     getDependentSizedArrayType(NewEltTy,
33303fa5cae9b3812cab9fab6c042c3329bb70a3d046John McCall                                                DSAT->getSizeExpr(),
3331898574e7496ba8fd76290079d3a9d06954992734Douglas Gregor                                                DSAT->getSizeModifier(),
33320953e767ff7817f97b3ab20896b229891eeff45bJohn McCall                                              DSAT->getIndexTypeCVRQualifiers(),
33337e7eb3da052a6d80ddf2377cab0384c798f73f75Douglas Gregor                                                DSAT->getBracketsRange()));
33341eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
3335c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner  const VariableArrayType *VAT = cast<VariableArrayType>(ATy);
33367e7eb3da052a6d80ddf2377cab0384c798f73f75Douglas Gregor  return cast<ArrayType>(getVariableArrayType(NewEltTy,
33373fa5cae9b3812cab9fab6c042c3329bb70a3d046John McCall                                              VAT->getSizeExpr(),
3338c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner                                              VAT->getSizeModifier(),
33390953e767ff7817f97b3ab20896b229891eeff45bJohn McCall                                              VAT->getIndexTypeCVRQualifiers(),
33407e7eb3da052a6d80ddf2377cab0384c798f73f75Douglas Gregor                                              VAT->getBracketsRange()));
334177c9647cae939104c6cb2b6a4dd8ca859d2e5770Chris Lattner}
334277c9647cae939104c6cb2b6a4dd8ca859d2e5770Chris Lattner
334379e6bd379773447a74cc3e579d9081e4c5cb6d63Douglas GregorQualType ASTContext::getAdjustedParameterType(QualType T) {
334479e6bd379773447a74cc3e579d9081e4c5cb6d63Douglas Gregor  // C99 6.7.5.3p7:
334579e6bd379773447a74cc3e579d9081e4c5cb6d63Douglas Gregor  //   A declaration of a parameter as "array of type" shall be
334679e6bd379773447a74cc3e579d9081e4c5cb6d63Douglas Gregor  //   adjusted to "qualified pointer to type", where the type
334779e6bd379773447a74cc3e579d9081e4c5cb6d63Douglas Gregor  //   qualifiers (if any) are those specified within the [ and ] of
334879e6bd379773447a74cc3e579d9081e4c5cb6d63Douglas Gregor  //   the array type derivation.
334979e6bd379773447a74cc3e579d9081e4c5cb6d63Douglas Gregor  if (T->isArrayType())
335079e6bd379773447a74cc3e579d9081e4c5cb6d63Douglas Gregor    return getArrayDecayedType(T);
335179e6bd379773447a74cc3e579d9081e4c5cb6d63Douglas Gregor
335279e6bd379773447a74cc3e579d9081e4c5cb6d63Douglas Gregor  // C99 6.7.5.3p8:
335379e6bd379773447a74cc3e579d9081e4c5cb6d63Douglas Gregor  //   A declaration of a parameter as "function returning type"
335479e6bd379773447a74cc3e579d9081e4c5cb6d63Douglas Gregor  //   shall be adjusted to "pointer to function returning type", as
335579e6bd379773447a74cc3e579d9081e4c5cb6d63Douglas Gregor  //   in 6.3.2.1.
335679e6bd379773447a74cc3e579d9081e4c5cb6d63Douglas Gregor  if (T->isFunctionType())
335779e6bd379773447a74cc3e579d9081e4c5cb6d63Douglas Gregor    return getPointerType(T);
335879e6bd379773447a74cc3e579d9081e4c5cb6d63Douglas Gregor
335979e6bd379773447a74cc3e579d9081e4c5cb6d63Douglas Gregor  return T;
336079e6bd379773447a74cc3e579d9081e4c5cb6d63Douglas Gregor}
336179e6bd379773447a74cc3e579d9081e4c5cb6d63Douglas Gregor
336279e6bd379773447a74cc3e579d9081e4c5cb6d63Douglas GregorQualType ASTContext::getSignatureParameterType(QualType T) {
336379e6bd379773447a74cc3e579d9081e4c5cb6d63Douglas Gregor  T = getVariableArrayDecayedType(T);
336479e6bd379773447a74cc3e579d9081e4c5cb6d63Douglas Gregor  T = getAdjustedParameterType(T);
336579e6bd379773447a74cc3e579d9081e4c5cb6d63Douglas Gregor  return T.getUnqualifiedType();
336679e6bd379773447a74cc3e579d9081e4c5cb6d63Douglas Gregor}
336779e6bd379773447a74cc3e579d9081e4c5cb6d63Douglas Gregor
3368e6327747b72bb687c948270f702ff53c30f411a6Chris Lattner/// getArrayDecayedType - Return the properly qualified result of decaying the
3369e6327747b72bb687c948270f702ff53c30f411a6Chris Lattner/// specified array type to a pointer.  This operation is non-trivial when
3370e6327747b72bb687c948270f702ff53c30f411a6Chris Lattner/// handling typedefs etc.  The canonical type of "T" must be an array type,
3371e6327747b72bb687c948270f702ff53c30f411a6Chris Lattner/// this returns a pointer to a properly qualified element of the array.
3372e6327747b72bb687c948270f702ff53c30f411a6Chris Lattner///
3373e6327747b72bb687c948270f702ff53c30f411a6Chris Lattner/// See C99 6.7.5.3p7 and C99 6.3.2.1p3.
33744ba2a17694148e16eaa8d3917f657ffcd3667be4Jay FoadQualType ASTContext::getArrayDecayedType(QualType Ty) const {
3375c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner  // Get the element type with 'getAsArrayType' so that we don't lose any
3376c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner  // typedefs in the element type of the array.  This also handles propagation
3377c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner  // of type qualifiers from the array type into the element type if present
3378c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner  // (C99 6.7.3p8).
3379c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner  const ArrayType *PrettyArrayType = getAsArrayType(Ty);
3380c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner  assert(PrettyArrayType && "Not an array type!");
33811eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
3382c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner  QualType PtrTy = getPointerType(PrettyArrayType->getElementType());
3383e6327747b72bb687c948270f702ff53c30f411a6Chris Lattner
3384e6327747b72bb687c948270f702ff53c30f411a6Chris Lattner  // int x[restrict 4] ->  int *restrict
33850953e767ff7817f97b3ab20896b229891eeff45bJohn McCall  return getQualifiedType(PtrTy, PrettyArrayType->getIndexTypeQualifiers());
3386e6327747b72bb687c948270f702ff53c30f411a6Chris Lattner}
3387e6327747b72bb687c948270f702ff53c30f411a6Chris Lattner
33883b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCallQualType ASTContext::getBaseElementType(const ArrayType *array) const {
33893b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall  return getBaseElementType(array->getElementType());
33905e03f9ea8174ae588c5e69ec6b5ef4c68f8fd766Douglas Gregor}
33915e03f9ea8174ae588c5e69ec6b5ef4c68f8fd766Douglas Gregor
33923b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCallQualType ASTContext::getBaseElementType(QualType type) const {
33933b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall  Qualifiers qs;
33943b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall  while (true) {
33953b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall    SplitQualType split = type.getSplitDesugaredType();
33963b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall    const ArrayType *array = split.first->getAsArrayTypeUnsafe();
33973b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall    if (!array) break;
33981eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
33993b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall    type = array->getElementType();
34003b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall    qs.addConsistentQualifiers(split.second);
34013b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall  }
34021eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
34033b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall  return getQualifiedType(type, qs);
34046183a99b064b397d98297904fbd6cf00fe1f453dAnders Carlsson}
34056183a99b064b397d98297904fbd6cf00fe1f453dAnders Carlsson
34060de78998e7bda473b408437053e48661b510d453Fariborz Jahanian/// getConstantArrayElementCount - Returns number of constant array elements.
34071eb4433ac451dc16f4133a88af2d002ac26c58efMike Stumpuint64_t
34080de78998e7bda473b408437053e48661b510d453Fariborz JahanianASTContext::getConstantArrayElementCount(const ConstantArrayType *CA)  const {
34090de78998e7bda473b408437053e48661b510d453Fariborz Jahanian  uint64_t ElementCount = 1;
34100de78998e7bda473b408437053e48661b510d453Fariborz Jahanian  do {
34110de78998e7bda473b408437053e48661b510d453Fariborz Jahanian    ElementCount *= CA->getSize().getZExtValue();
34120de78998e7bda473b408437053e48661b510d453Fariborz Jahanian    CA = dyn_cast<ConstantArrayType>(CA->getElementType());
34130de78998e7bda473b408437053e48661b510d453Fariborz Jahanian  } while (CA);
34140de78998e7bda473b408437053e48661b510d453Fariborz Jahanian  return ElementCount;
34150de78998e7bda473b408437053e48661b510d453Fariborz Jahanian}
34160de78998e7bda473b408437053e48661b510d453Fariborz Jahanian
34175f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// getFloatingRank - Return a relative rank for floating point types.
34185f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// This routine will assert if passed a built-in type that isn't a float.
3419a75cea3f6be0daa8054d36af81a6ffda1713f82dChris Lattnerstatic FloatingRank getFloatingRank(QualType T) {
3420183700f494ec9b6701b6efe82bcb25f4c79ba561John McCall  if (const ComplexType *CT = T->getAs<ComplexType>())
34215f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    return getFloatingRank(CT->getElementType());
3422a75cea3f6be0daa8054d36af81a6ffda1713f82dChris Lattner
3423183700f494ec9b6701b6efe82bcb25f4c79ba561John McCall  assert(T->getAs<BuiltinType>() && "getFloatingRank(): not a floating type");
3424183700f494ec9b6701b6efe82bcb25f4c79ba561John McCall  switch (T->getAs<BuiltinType>()->getKind()) {
3425a75cea3f6be0daa8054d36af81a6ffda1713f82dChris Lattner  default: assert(0 && "getFloatingRank(): not a floating type");
34265f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case BuiltinType::Float:      return FloatRank;
34275f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case BuiltinType::Double:     return DoubleRank;
34285f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case BuiltinType::LongDouble: return LongDoubleRank;
34295f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
34305f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
34315f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
34321eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// getFloatingTypeOfSizeWithinDomain - Returns a real floating
34331eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// point or a complex type (based on typeDomain/typeSize).
3434716c7304ff5d27a95e1e7823acd1d09d5ec3e37fSteve Naroff/// 'typeDomain' is a real floating point or complex type.
3435716c7304ff5d27a95e1e7823acd1d09d5ec3e37fSteve Naroff/// 'typeSize' is a real floating point or complex type.
34361361b11066239ea15764a2a844405352d87296b3Chris LattnerQualType ASTContext::getFloatingTypeOfSizeWithinDomain(QualType Size,
34371361b11066239ea15764a2a844405352d87296b3Chris Lattner                                                       QualType Domain) const {
34381361b11066239ea15764a2a844405352d87296b3Chris Lattner  FloatingRank EltRank = getFloatingRank(Size);
34391361b11066239ea15764a2a844405352d87296b3Chris Lattner  if (Domain->isComplexType()) {
34401361b11066239ea15764a2a844405352d87296b3Chris Lattner    switch (EltRank) {
3441716c7304ff5d27a95e1e7823acd1d09d5ec3e37fSteve Naroff    default: assert(0 && "getFloatingRank(): illegal value for rank");
3442f1448a0e4a1e868ff873a8530a61a09cb68666ccSteve Naroff    case FloatRank:      return FloatComplexTy;
3443f1448a0e4a1e868ff873a8530a61a09cb68666ccSteve Naroff    case DoubleRank:     return DoubleComplexTy;
3444f1448a0e4a1e868ff873a8530a61a09cb68666ccSteve Naroff    case LongDoubleRank: return LongDoubleComplexTy;
3445f1448a0e4a1e868ff873a8530a61a09cb68666ccSteve Naroff    }
3446f1448a0e4a1e868ff873a8530a61a09cb68666ccSteve Naroff  }
34471361b11066239ea15764a2a844405352d87296b3Chris Lattner
34481361b11066239ea15764a2a844405352d87296b3Chris Lattner  assert(Domain->isRealFloatingType() && "Unknown domain!");
34491361b11066239ea15764a2a844405352d87296b3Chris Lattner  switch (EltRank) {
34501361b11066239ea15764a2a844405352d87296b3Chris Lattner  default: assert(0 && "getFloatingRank(): illegal value for rank");
34511361b11066239ea15764a2a844405352d87296b3Chris Lattner  case FloatRank:      return FloatTy;
34521361b11066239ea15764a2a844405352d87296b3Chris Lattner  case DoubleRank:     return DoubleTy;
34531361b11066239ea15764a2a844405352d87296b3Chris Lattner  case LongDoubleRank: return LongDoubleTy;
34545f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
34555f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
34565f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
34577cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner/// getFloatingTypeOrder - Compare the rank of the two specified floating
34587cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner/// point types, ignoring the domain of the type (i.e. 'double' ==
34597cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner/// '_Complex double').  If LHS > RHS, return 1.  If LHS == RHS, return 0. If
34601eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// LHS < RHS, return -1.
34614ba2a17694148e16eaa8d3917f657ffcd3667be4Jay Foadint ASTContext::getFloatingTypeOrder(QualType LHS, QualType RHS) const {
3462a75cea3f6be0daa8054d36af81a6ffda1713f82dChris Lattner  FloatingRank LHSR = getFloatingRank(LHS);
3463a75cea3f6be0daa8054d36af81a6ffda1713f82dChris Lattner  FloatingRank RHSR = getFloatingRank(RHS);
34641eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
3465a75cea3f6be0daa8054d36af81a6ffda1713f82dChris Lattner  if (LHSR == RHSR)
3466fb0d49669aa370b4c0993c5cee60275ef9fd6518Steve Naroff    return 0;
3467a75cea3f6be0daa8054d36af81a6ffda1713f82dChris Lattner  if (LHSR > RHSR)
3468fb0d49669aa370b4c0993c5cee60275ef9fd6518Steve Naroff    return 1;
3469fb0d49669aa370b4c0993c5cee60275ef9fd6518Steve Naroff  return -1;
34705f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
34715f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
3472f52ab250ff92bc51a9ac9a8e19bd43b63a5f844fChris Lattner/// getIntegerRank - Return an integer conversion rank (C99 6.3.1.1p1). This
3473f52ab250ff92bc51a9ac9a8e19bd43b63a5f844fChris Lattner/// routine will assert if passed a built-in type that isn't an integer or enum,
3474f52ab250ff92bc51a9ac9a8e19bd43b63a5f844fChris Lattner/// or if it is not canonicalized.
3475f4c7371fb1d3cebcfb40abad4537bb82515704eaJohn McCallunsigned ASTContext::getIntegerRank(const Type *T) const {
3476467b27b9a24bdc823218ad1ad0e37673b6cc1e83John McCall  assert(T->isCanonicalUnqualified() && "T should be canonicalized");
3477f4c7371fb1d3cebcfb40abad4537bb82515704eaJohn McCall  if (const EnumType* ET = dyn_cast<EnumType>(T))
3478842aef8d942a880eeb9535d40de31a86838264cbJohn McCall    T = ET->getDecl()->getPromotionType().getTypePtr();
3479f98aba35e6c3da5aae61843fc01334939e4e12ecEli Friedman
34803f59c975aa5d047f7edd1b900b5e885c38af0ef7Chris Lattner  if (T->isSpecificBuiltinType(BuiltinType::WChar_S) ||
34813f59c975aa5d047f7edd1b900b5e885c38af0ef7Chris Lattner      T->isSpecificBuiltinType(BuiltinType::WChar_U))
3482a34267595534a72703290153a6f7e3da1adcec59Eli Friedman    T = getFromTargetType(Target.getWCharType()).getTypePtr();
3483a34267595534a72703290153a6f7e3da1adcec59Eli Friedman
3484f5c209d23b20ada4a9b6235db50317239cbf6ae1Alisdair Meredith  if (T->isSpecificBuiltinType(BuiltinType::Char16))
3485f5c209d23b20ada4a9b6235db50317239cbf6ae1Alisdair Meredith    T = getFromTargetType(Target.getChar16Type()).getTypePtr();
3486f5c209d23b20ada4a9b6235db50317239cbf6ae1Alisdair Meredith
3487f5c209d23b20ada4a9b6235db50317239cbf6ae1Alisdair Meredith  if (T->isSpecificBuiltinType(BuiltinType::Char32))
3488f5c209d23b20ada4a9b6235db50317239cbf6ae1Alisdair Meredith    T = getFromTargetType(Target.getChar32Type()).getTypePtr();
3489f5c209d23b20ada4a9b6235db50317239cbf6ae1Alisdair Meredith
3490f52ab250ff92bc51a9ac9a8e19bd43b63a5f844fChris Lattner  switch (cast<BuiltinType>(T)->getKind()) {
34917cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner  default: assert(0 && "getIntegerRank(): not a built-in integer");
34927cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner  case BuiltinType::Bool:
3493f98aba35e6c3da5aae61843fc01334939e4e12ecEli Friedman    return 1 + (getIntWidth(BoolTy) << 3);
34947cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner  case BuiltinType::Char_S:
34957cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner  case BuiltinType::Char_U:
34967cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner  case BuiltinType::SChar:
34977cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner  case BuiltinType::UChar:
3498f98aba35e6c3da5aae61843fc01334939e4e12ecEli Friedman    return 2 + (getIntWidth(CharTy) << 3);
34997cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner  case BuiltinType::Short:
35007cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner  case BuiltinType::UShort:
3501f98aba35e6c3da5aae61843fc01334939e4e12ecEli Friedman    return 3 + (getIntWidth(ShortTy) << 3);
35027cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner  case BuiltinType::Int:
35037cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner  case BuiltinType::UInt:
3504f98aba35e6c3da5aae61843fc01334939e4e12ecEli Friedman    return 4 + (getIntWidth(IntTy) << 3);
35057cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner  case BuiltinType::Long:
35067cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner  case BuiltinType::ULong:
3507f98aba35e6c3da5aae61843fc01334939e4e12ecEli Friedman    return 5 + (getIntWidth(LongTy) << 3);
35087cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner  case BuiltinType::LongLong:
35097cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner  case BuiltinType::ULongLong:
3510f98aba35e6c3da5aae61843fc01334939e4e12ecEli Friedman    return 6 + (getIntWidth(LongLongTy) << 3);
35112df9ced9fd1e8c7d7b38443db07e0e811de22571Chris Lattner  case BuiltinType::Int128:
35122df9ced9fd1e8c7d7b38443db07e0e811de22571Chris Lattner  case BuiltinType::UInt128:
35132df9ced9fd1e8c7d7b38443db07e0e811de22571Chris Lattner    return 7 + (getIntWidth(Int128Ty) << 3);
3514f52ab250ff92bc51a9ac9a8e19bd43b63a5f844fChris Lattner  }
3515f52ab250ff92bc51a9ac9a8e19bd43b63a5f844fChris Lattner}
3516f52ab250ff92bc51a9ac9a8e19bd43b63a5f844fChris Lattner
351704e8357f6801e9ff52673e7e899a67bbabf9de93Eli Friedman/// \brief Whether this is a promotable bitfield reference according
351804e8357f6801e9ff52673e7e899a67bbabf9de93Eli Friedman/// to C99 6.3.1.1p2, bullet 2 (and GCC extensions).
351904e8357f6801e9ff52673e7e899a67bbabf9de93Eli Friedman///
352004e8357f6801e9ff52673e7e899a67bbabf9de93Eli Friedman/// \returns the type this bit-field will promote to, or NULL if no
352104e8357f6801e9ff52673e7e899a67bbabf9de93Eli Friedman/// promotion occurs.
35224ba2a17694148e16eaa8d3917f657ffcd3667be4Jay FoadQualType ASTContext::isPromotableBitField(Expr *E) const {
3523ceafbdeb93ecf323cca74e660bf54504c86f3b71Douglas Gregor  if (E->isTypeDependent() || E->isValueDependent())
3524ceafbdeb93ecf323cca74e660bf54504c86f3b71Douglas Gregor    return QualType();
3525ceafbdeb93ecf323cca74e660bf54504c86f3b71Douglas Gregor
352604e8357f6801e9ff52673e7e899a67bbabf9de93Eli Friedman  FieldDecl *Field = E->getBitField();
352704e8357f6801e9ff52673e7e899a67bbabf9de93Eli Friedman  if (!Field)
352804e8357f6801e9ff52673e7e899a67bbabf9de93Eli Friedman    return QualType();
352904e8357f6801e9ff52673e7e899a67bbabf9de93Eli Friedman
353004e8357f6801e9ff52673e7e899a67bbabf9de93Eli Friedman  QualType FT = Field->getType();
353104e8357f6801e9ff52673e7e899a67bbabf9de93Eli Friedman
353204e8357f6801e9ff52673e7e899a67bbabf9de93Eli Friedman  llvm::APSInt BitWidthAP = Field->getBitWidth()->EvaluateAsInt(*this);
353304e8357f6801e9ff52673e7e899a67bbabf9de93Eli Friedman  uint64_t BitWidth = BitWidthAP.getZExtValue();
353404e8357f6801e9ff52673e7e899a67bbabf9de93Eli Friedman  uint64_t IntSize = getTypeSize(IntTy);
353504e8357f6801e9ff52673e7e899a67bbabf9de93Eli Friedman  // GCC extension compatibility: if the bit-field size is less than or equal
353604e8357f6801e9ff52673e7e899a67bbabf9de93Eli Friedman  // to the size of int, it gets promoted no matter what its type is.
353704e8357f6801e9ff52673e7e899a67bbabf9de93Eli Friedman  // For instance, unsigned long bf : 4 gets promoted to signed int.
353804e8357f6801e9ff52673e7e899a67bbabf9de93Eli Friedman  if (BitWidth < IntSize)
353904e8357f6801e9ff52673e7e899a67bbabf9de93Eli Friedman    return IntTy;
354004e8357f6801e9ff52673e7e899a67bbabf9de93Eli Friedman
354104e8357f6801e9ff52673e7e899a67bbabf9de93Eli Friedman  if (BitWidth == IntSize)
354204e8357f6801e9ff52673e7e899a67bbabf9de93Eli Friedman    return FT->isSignedIntegerType() ? IntTy : UnsignedIntTy;
354304e8357f6801e9ff52673e7e899a67bbabf9de93Eli Friedman
354404e8357f6801e9ff52673e7e899a67bbabf9de93Eli Friedman  // Types bigger than int are not subject to promotions, and therefore act
354504e8357f6801e9ff52673e7e899a67bbabf9de93Eli Friedman  // like the base type.
354604e8357f6801e9ff52673e7e899a67bbabf9de93Eli Friedman  // FIXME: This doesn't quite match what gcc does, but what gcc does here
354704e8357f6801e9ff52673e7e899a67bbabf9de93Eli Friedman  // is ridiculous.
354804e8357f6801e9ff52673e7e899a67bbabf9de93Eli Friedman  return QualType();
354904e8357f6801e9ff52673e7e899a67bbabf9de93Eli Friedman}
355004e8357f6801e9ff52673e7e899a67bbabf9de93Eli Friedman
3551a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman/// getPromotedIntegerType - Returns the type that Promotable will
3552a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman/// promote to: C99 6.3.1.1p2, assuming that Promotable is a promotable
3553a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman/// integer type.
35544ba2a17694148e16eaa8d3917f657ffcd3667be4Jay FoadQualType ASTContext::getPromotedIntegerType(QualType Promotable) const {
3555a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman  assert(!Promotable.isNull());
3556a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman  assert(Promotable->isPromotableIntegerType());
3557842aef8d942a880eeb9535d40de31a86838264cbJohn McCall  if (const EnumType *ET = Promotable->getAs<EnumType>())
3558842aef8d942a880eeb9535d40de31a86838264cbJohn McCall    return ET->getDecl()->getPromotionType();
3559a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman  if (Promotable->isSignedIntegerType())
3560a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman    return IntTy;
3561a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman  uint64_t PromotableSize = getTypeSize(Promotable);
3562a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman  uint64_t IntSize = getTypeSize(IntTy);
3563a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman  assert(Promotable->isUnsignedIntegerType() && PromotableSize <= IntSize);
3564a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman  return (PromotableSize != IntSize) ? IntTy : UnsignedIntTy;
3565a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman}
3566a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman
356731862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis/// \brief Recurses in pointer/array types until it finds an objc retainable
356831862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis/// type and returns its ownership.
356931862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios KyrtzidisQualifiers::ObjCLifetime ASTContext::getInnerObjCOwnership(QualType T) const {
357031862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis  while (!T.isNull()) {
357131862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis    if (T.getObjCLifetime() != Qualifiers::OCL_None)
357231862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis      return T.getObjCLifetime();
357331862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis    if (T->isArrayType())
357431862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis      T = getBaseElementType(T);
357531862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis    else if (const PointerType *PT = T->getAs<PointerType>())
357631862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis      T = PT->getPointeeType();
357731862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis    else if (const ReferenceType *RT = T->getAs<ReferenceType>())
357828445f0b62f6aed851ff87ce64d9b19200d3211fArgyrios Kyrtzidis      T = RT->getPointeeType();
357931862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis    else
358031862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis      break;
358131862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis  }
358231862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis
358331862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis  return Qualifiers::OCL_None;
358431862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis}
358531862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis
35861eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// getIntegerTypeOrder - Returns the highest ranked integer type:
35877cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner/// C99 6.3.1.8p1.  If LHS > RHS, return 1.  If LHS == RHS, return 0. If
35881eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// LHS < RHS, return -1.
35894ba2a17694148e16eaa8d3917f657ffcd3667be4Jay Foadint ASTContext::getIntegerTypeOrder(QualType LHS, QualType RHS) const {
3590f4c7371fb1d3cebcfb40abad4537bb82515704eaJohn McCall  const Type *LHSC = getCanonicalType(LHS).getTypePtr();
3591f4c7371fb1d3cebcfb40abad4537bb82515704eaJohn McCall  const Type *RHSC = getCanonicalType(RHS).getTypePtr();
35927cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner  if (LHSC == RHSC) return 0;
35931eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
3594f52ab250ff92bc51a9ac9a8e19bd43b63a5f844fChris Lattner  bool LHSUnsigned = LHSC->isUnsignedIntegerType();
3595f52ab250ff92bc51a9ac9a8e19bd43b63a5f844fChris Lattner  bool RHSUnsigned = RHSC->isUnsignedIntegerType();
35961eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
35977cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner  unsigned LHSRank = getIntegerRank(LHSC);
35987cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner  unsigned RHSRank = getIntegerRank(RHSC);
35991eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
36007cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner  if (LHSUnsigned == RHSUnsigned) {  // Both signed or both unsigned.
36017cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner    if (LHSRank == RHSRank) return 0;
36027cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner    return LHSRank > RHSRank ? 1 : -1;
36037cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner  }
36041eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
36057cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner  // Otherwise, the LHS is signed and the RHS is unsigned or visa versa.
36067cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner  if (LHSUnsigned) {
36077cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner    // If the unsigned [LHS] type is larger, return it.
36087cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner    if (LHSRank >= RHSRank)
36097cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner      return 1;
36101eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
36117cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner    // If the signed type can represent all values of the unsigned type, it
36127cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner    // wins.  Because we are dealing with 2's complement and types that are
36131eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    // powers of two larger than each other, this is always safe.
36147cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner    return -1;
36157cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner  }
36167cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner
36177cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner  // If the unsigned [RHS] type is larger, return it.
36187cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner  if (RHSRank >= LHSRank)
36197cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner    return -1;
36201eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
36217cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner  // If the signed type can represent all values of the unsigned type, it
36227cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner  // wins.  Because we are dealing with 2's complement and types that are
36231eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  // powers of two larger than each other, this is always safe.
36247cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner  return 1;
36255f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
362671993dd85eed9cc42c6b2fa61ee5c53026b74817Anders Carlsson
362779cbc7dd2aacd85a28f469b5dc73c4ea296e7072Anders Carlssonstatic RecordDecl *
3628ba877adeb49ed6dc17f27fa3a3bcd0cca713fd68Abramo BagnaraCreateRecordDecl(const ASTContext &Ctx, RecordDecl::TagKind TK,
3629ba877adeb49ed6dc17f27fa3a3bcd0cca713fd68Abramo Bagnara                 DeclContext *DC, IdentifierInfo *Id) {
3630ba877adeb49ed6dc17f27fa3a3bcd0cca713fd68Abramo Bagnara  SourceLocation Loc;
363179cbc7dd2aacd85a28f469b5dc73c4ea296e7072Anders Carlsson  if (Ctx.getLangOptions().CPlusPlus)
3632ba877adeb49ed6dc17f27fa3a3bcd0cca713fd68Abramo Bagnara    return CXXRecordDecl::Create(Ctx, TK, DC, Loc, Loc, Id);
363379cbc7dd2aacd85a28f469b5dc73c4ea296e7072Anders Carlsson  else
3634ba877adeb49ed6dc17f27fa3a3bcd0cca713fd68Abramo Bagnara    return RecordDecl::Create(Ctx, TK, DC, Loc, Loc, Id);
363579cbc7dd2aacd85a28f469b5dc73c4ea296e7072Anders Carlsson}
3636ba877adeb49ed6dc17f27fa3a3bcd0cca713fd68Abramo Bagnara
36371eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump// getCFConstantStringType - Return the type used for constant CFStrings.
36384ba2a17694148e16eaa8d3917f657ffcd3667be4Jay FoadQualType ASTContext::getCFConstantStringType() const {
363971993dd85eed9cc42c6b2fa61ee5c53026b74817Anders Carlsson  if (!CFConstantStringTypeDecl) {
36401eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    CFConstantStringTypeDecl =
3641ba877adeb49ed6dc17f27fa3a3bcd0cca713fd68Abramo Bagnara      CreateRecordDecl(*this, TTK_Struct, TUDecl,
364279cbc7dd2aacd85a28f469b5dc73c4ea296e7072Anders Carlsson                       &Idents.get("NSConstantString"));
36435cfa011e61e14e6f2e1659047d809706c0e4c6a3John McCall    CFConstantStringTypeDecl->startDefinition();
364479cbc7dd2aacd85a28f469b5dc73c4ea296e7072Anders Carlsson
3645f06273f8bbacb086a46bde456429c8d08f6d07eeAnders Carlsson    QualType FieldTypes[4];
36461eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
364771993dd85eed9cc42c6b2fa61ee5c53026b74817Anders Carlsson    // const int *isa;
36480953e767ff7817f97b3ab20896b229891eeff45bJohn McCall    FieldTypes[0] = getPointerType(IntTy.withConst());
3649f06273f8bbacb086a46bde456429c8d08f6d07eeAnders Carlsson    // int flags;
3650f06273f8bbacb086a46bde456429c8d08f6d07eeAnders Carlsson    FieldTypes[1] = IntTy;
365171993dd85eed9cc42c6b2fa61ee5c53026b74817Anders Carlsson    // const char *str;
36520953e767ff7817f97b3ab20896b229891eeff45bJohn McCall    FieldTypes[2] = getPointerType(CharTy.withConst());
365371993dd85eed9cc42c6b2fa61ee5c53026b74817Anders Carlsson    // long length;
36541eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    FieldTypes[3] = LongTy;
36551eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
365644b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor    // Create fields
365744b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor    for (unsigned i = 0; i < 4; ++i) {
36581eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      FieldDecl *Field = FieldDecl::Create(*this, CFConstantStringTypeDecl,
3659ff676cb48fe8bf7be2feaa251dc7c5fb15af4730Abramo Bagnara                                           SourceLocation(),
366044b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor                                           SourceLocation(), 0,
3661a93c934af4fbf97cbe8e649d82e68ccacfe57c95John McCall                                           FieldTypes[i], /*TInfo=*/0,
36621eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump                                           /*BitWidth=*/0,
36637a614d8380297fcd2bc23986241905d97222948cRichard Smith                                           /*Mutable=*/false,
36647a614d8380297fcd2bc23986241905d97222948cRichard Smith                                           /*HasInit=*/false);
36652888b65aae768f54062505330df7be230a0510c7John McCall      Field->setAccess(AS_public);
366617945a0f64fe03ff6ec0c2146005a87636e3ac12Argyrios Kyrtzidis      CFConstantStringTypeDecl->addDecl(Field);
366744b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor    }
366844b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor
3669838db383b69b9fb55f55c8e9546477df198a4faaDouglas Gregor    CFConstantStringTypeDecl->completeDefinition();
367071993dd85eed9cc42c6b2fa61ee5c53026b74817Anders Carlsson  }
36711eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
367271993dd85eed9cc42c6b2fa61ee5c53026b74817Anders Carlsson  return getTagDeclType(CFConstantStringTypeDecl);
36738467583c2704e7a9691ea56939a029015f0ade0aGabor Greif}
3674b2cf3573d7351094f6247fcca94703ce88eb9ee0Anders Carlsson
3675319ac896a0fef7365d5589b8021db7e41207fe42Douglas Gregorvoid ASTContext::setCFConstantStringType(QualType T) {
36766217b80b7a1379b74cced1c076338262c3c980b3Ted Kremenek  const RecordType *Rec = T->getAs<RecordType>();
3677319ac896a0fef7365d5589b8021db7e41207fe42Douglas Gregor  assert(Rec && "Invalid CFConstantStringType");
3678319ac896a0fef7365d5589b8021db7e41207fe42Douglas Gregor  CFConstantStringTypeDecl = Rec->getDecl();
3679319ac896a0fef7365d5589b8021db7e41207fe42Douglas Gregor}
3680319ac896a0fef7365d5589b8021db7e41207fe42Douglas Gregor
36814ba2a17694148e16eaa8d3917f657ffcd3667be4Jay FoadQualType ASTContext::getBlockDescriptorType() const {
3682adaaad3715c9c26cdcfdfe3401a13d7b4423ddcfMike Stump  if (BlockDescriptorType)
3683adaaad3715c9c26cdcfdfe3401a13d7b4423ddcfMike Stump    return getTagDeclType(BlockDescriptorType);
3684adaaad3715c9c26cdcfdfe3401a13d7b4423ddcfMike Stump
3685adaaad3715c9c26cdcfdfe3401a13d7b4423ddcfMike Stump  RecordDecl *T;
3686adaaad3715c9c26cdcfdfe3401a13d7b4423ddcfMike Stump  // FIXME: Needs the FlagAppleBlock bit.
3687ba877adeb49ed6dc17f27fa3a3bcd0cca713fd68Abramo Bagnara  T = CreateRecordDecl(*this, TTK_Struct, TUDecl,
368879cbc7dd2aacd85a28f469b5dc73c4ea296e7072Anders Carlsson                       &Idents.get("__block_descriptor"));
36895cfa011e61e14e6f2e1659047d809706c0e4c6a3John McCall  T->startDefinition();
3690adaaad3715c9c26cdcfdfe3401a13d7b4423ddcfMike Stump
3691adaaad3715c9c26cdcfdfe3401a13d7b4423ddcfMike Stump  QualType FieldTypes[] = {
3692adaaad3715c9c26cdcfdfe3401a13d7b4423ddcfMike Stump    UnsignedLongTy,
3693adaaad3715c9c26cdcfdfe3401a13d7b4423ddcfMike Stump    UnsignedLongTy,
3694adaaad3715c9c26cdcfdfe3401a13d7b4423ddcfMike Stump  };
3695adaaad3715c9c26cdcfdfe3401a13d7b4423ddcfMike Stump
3696adaaad3715c9c26cdcfdfe3401a13d7b4423ddcfMike Stump  const char *FieldNames[] = {
3697adaaad3715c9c26cdcfdfe3401a13d7b4423ddcfMike Stump    "reserved",
3698083c25eea14bb4cc4ecc3ec763c60e2e609e22bdMike Stump    "Size"
3699adaaad3715c9c26cdcfdfe3401a13d7b4423ddcfMike Stump  };
3700adaaad3715c9c26cdcfdfe3401a13d7b4423ddcfMike Stump
3701adaaad3715c9c26cdcfdfe3401a13d7b4423ddcfMike Stump  for (size_t i = 0; i < 2; ++i) {
3702ff676cb48fe8bf7be2feaa251dc7c5fb15af4730Abramo Bagnara    FieldDecl *Field = FieldDecl::Create(*this, T, SourceLocation(),
3703adaaad3715c9c26cdcfdfe3401a13d7b4423ddcfMike Stump                                         SourceLocation(),
3704adaaad3715c9c26cdcfdfe3401a13d7b4423ddcfMike Stump                                         &Idents.get(FieldNames[i]),
3705a93c934af4fbf97cbe8e649d82e68ccacfe57c95John McCall                                         FieldTypes[i], /*TInfo=*/0,
3706adaaad3715c9c26cdcfdfe3401a13d7b4423ddcfMike Stump                                         /*BitWidth=*/0,
37077a614d8380297fcd2bc23986241905d97222948cRichard Smith                                         /*Mutable=*/false,
37087a614d8380297fcd2bc23986241905d97222948cRichard Smith                                         /*HasInit=*/false);
37092888b65aae768f54062505330df7be230a0510c7John McCall    Field->setAccess(AS_public);
3710adaaad3715c9c26cdcfdfe3401a13d7b4423ddcfMike Stump    T->addDecl(Field);
3711adaaad3715c9c26cdcfdfe3401a13d7b4423ddcfMike Stump  }
3712adaaad3715c9c26cdcfdfe3401a13d7b4423ddcfMike Stump
3713838db383b69b9fb55f55c8e9546477df198a4faaDouglas Gregor  T->completeDefinition();
3714adaaad3715c9c26cdcfdfe3401a13d7b4423ddcfMike Stump
3715adaaad3715c9c26cdcfdfe3401a13d7b4423ddcfMike Stump  BlockDescriptorType = T;
3716adaaad3715c9c26cdcfdfe3401a13d7b4423ddcfMike Stump
3717adaaad3715c9c26cdcfdfe3401a13d7b4423ddcfMike Stump  return getTagDeclType(BlockDescriptorType);
3718adaaad3715c9c26cdcfdfe3401a13d7b4423ddcfMike Stump}
3719adaaad3715c9c26cdcfdfe3401a13d7b4423ddcfMike Stump
37204ba2a17694148e16eaa8d3917f657ffcd3667be4Jay FoadQualType ASTContext::getBlockDescriptorExtendedType() const {
3721083c25eea14bb4cc4ecc3ec763c60e2e609e22bdMike Stump  if (BlockDescriptorExtendedType)
3722083c25eea14bb4cc4ecc3ec763c60e2e609e22bdMike Stump    return getTagDeclType(BlockDescriptorExtendedType);
3723083c25eea14bb4cc4ecc3ec763c60e2e609e22bdMike Stump
3724083c25eea14bb4cc4ecc3ec763c60e2e609e22bdMike Stump  RecordDecl *T;
3725083c25eea14bb4cc4ecc3ec763c60e2e609e22bdMike Stump  // FIXME: Needs the FlagAppleBlock bit.
3726ba877adeb49ed6dc17f27fa3a3bcd0cca713fd68Abramo Bagnara  T = CreateRecordDecl(*this, TTK_Struct, TUDecl,
372779cbc7dd2aacd85a28f469b5dc73c4ea296e7072Anders Carlsson                       &Idents.get("__block_descriptor_withcopydispose"));
37285cfa011e61e14e6f2e1659047d809706c0e4c6a3John McCall  T->startDefinition();
3729083c25eea14bb4cc4ecc3ec763c60e2e609e22bdMike Stump
3730083c25eea14bb4cc4ecc3ec763c60e2e609e22bdMike Stump  QualType FieldTypes[] = {
3731083c25eea14bb4cc4ecc3ec763c60e2e609e22bdMike Stump    UnsignedLongTy,
3732083c25eea14bb4cc4ecc3ec763c60e2e609e22bdMike Stump    UnsignedLongTy,
3733083c25eea14bb4cc4ecc3ec763c60e2e609e22bdMike Stump    getPointerType(VoidPtrTy),
3734083c25eea14bb4cc4ecc3ec763c60e2e609e22bdMike Stump    getPointerType(VoidPtrTy)
3735083c25eea14bb4cc4ecc3ec763c60e2e609e22bdMike Stump  };
3736083c25eea14bb4cc4ecc3ec763c60e2e609e22bdMike Stump
3737083c25eea14bb4cc4ecc3ec763c60e2e609e22bdMike Stump  const char *FieldNames[] = {
3738083c25eea14bb4cc4ecc3ec763c60e2e609e22bdMike Stump    "reserved",
3739083c25eea14bb4cc4ecc3ec763c60e2e609e22bdMike Stump    "Size",
3740083c25eea14bb4cc4ecc3ec763c60e2e609e22bdMike Stump    "CopyFuncPtr",
3741083c25eea14bb4cc4ecc3ec763c60e2e609e22bdMike Stump    "DestroyFuncPtr"
3742083c25eea14bb4cc4ecc3ec763c60e2e609e22bdMike Stump  };
3743083c25eea14bb4cc4ecc3ec763c60e2e609e22bdMike Stump
3744083c25eea14bb4cc4ecc3ec763c60e2e609e22bdMike Stump  for (size_t i = 0; i < 4; ++i) {
3745ff676cb48fe8bf7be2feaa251dc7c5fb15af4730Abramo Bagnara    FieldDecl *Field = FieldDecl::Create(*this, T, SourceLocation(),
3746083c25eea14bb4cc4ecc3ec763c60e2e609e22bdMike Stump                                         SourceLocation(),
3747083c25eea14bb4cc4ecc3ec763c60e2e609e22bdMike Stump                                         &Idents.get(FieldNames[i]),
3748a93c934af4fbf97cbe8e649d82e68ccacfe57c95John McCall                                         FieldTypes[i], /*TInfo=*/0,
3749083c25eea14bb4cc4ecc3ec763c60e2e609e22bdMike Stump                                         /*BitWidth=*/0,
37507a614d8380297fcd2bc23986241905d97222948cRichard Smith                                         /*Mutable=*/false,
37517a614d8380297fcd2bc23986241905d97222948cRichard Smith                                         /*HasInit=*/false);
37522888b65aae768f54062505330df7be230a0510c7John McCall    Field->setAccess(AS_public);
3753083c25eea14bb4cc4ecc3ec763c60e2e609e22bdMike Stump    T->addDecl(Field);
3754083c25eea14bb4cc4ecc3ec763c60e2e609e22bdMike Stump  }
3755083c25eea14bb4cc4ecc3ec763c60e2e609e22bdMike Stump
3756838db383b69b9fb55f55c8e9546477df198a4faaDouglas Gregor  T->completeDefinition();
3757083c25eea14bb4cc4ecc3ec763c60e2e609e22bdMike Stump
3758083c25eea14bb4cc4ecc3ec763c60e2e609e22bdMike Stump  BlockDescriptorExtendedType = T;
3759083c25eea14bb4cc4ecc3ec763c60e2e609e22bdMike Stump
3760083c25eea14bb4cc4ecc3ec763c60e2e609e22bdMike Stump  return getTagDeclType(BlockDescriptorExtendedType);
3761083c25eea14bb4cc4ecc3ec763c60e2e609e22bdMike Stump}
3762083c25eea14bb4cc4ecc3ec763c60e2e609e22bdMike Stump
37634ba2a17694148e16eaa8d3917f657ffcd3667be4Jay Foadbool ASTContext::BlockRequiresCopying(QualType Ty) const {
3764f85e193739c953358c865005855253af4f68a497John McCall  if (Ty->isObjCRetainableType())
3765af7b44d847d3e44c43346d508b2e55a6254b6e9dMike Stump    return true;
3766e38be617437ccdcc180c5a49e447cbcd07539292Fariborz Jahanian  if (getLangOptions().CPlusPlus) {
3767e38be617437ccdcc180c5a49e447cbcd07539292Fariborz Jahanian    if (const RecordType *RT = Ty->getAs<RecordType>()) {
3768e38be617437ccdcc180c5a49e447cbcd07539292Fariborz Jahanian      CXXRecordDecl *RD = cast<CXXRecordDecl>(RT->getDecl());
3769ffe37fdda5b4b4f162a45155c30d9f60ce110c12Sean Hunt      return RD->hasConstCopyConstructor();
3770e38be617437ccdcc180c5a49e447cbcd07539292Fariborz Jahanian
3771e38be617437ccdcc180c5a49e447cbcd07539292Fariborz Jahanian    }
3772e38be617437ccdcc180c5a49e447cbcd07539292Fariborz Jahanian  }
3773af7b44d847d3e44c43346d508b2e55a6254b6e9dMike Stump  return false;
3774af7b44d847d3e44c43346d508b2e55a6254b6e9dMike Stump}
3775af7b44d847d3e44c43346d508b2e55a6254b6e9dMike Stump
37764ba2a17694148e16eaa8d3917f657ffcd3667be4Jay FoadQualType
37775f9e272e632e951b1efe824cd16acb4d96077930Chris LattnerASTContext::BuildByRefType(StringRef DeclName, QualType Ty) const {
3778af7b44d847d3e44c43346d508b2e55a6254b6e9dMike Stump  //  type = struct __Block_byref_1_X {
3779ea26cb522e88fc86b0d1cae61dcefcfe4cc20231Mike Stump  //    void *__isa;
3780af7b44d847d3e44c43346d508b2e55a6254b6e9dMike Stump  //    struct __Block_byref_1_X *__forwarding;
3781ea26cb522e88fc86b0d1cae61dcefcfe4cc20231Mike Stump  //    unsigned int __flags;
3782ea26cb522e88fc86b0d1cae61dcefcfe4cc20231Mike Stump  //    unsigned int __size;
3783a7e6845660f91ec611427e1db842780e1ec12bdbEli Friedman  //    void *__copy_helper;            // as needed
3784a7e6845660f91ec611427e1db842780e1ec12bdbEli Friedman  //    void *__destroy_help            // as needed
3785af7b44d847d3e44c43346d508b2e55a6254b6e9dMike Stump  //    int X;
3786ea26cb522e88fc86b0d1cae61dcefcfe4cc20231Mike Stump  //  } *
3787ea26cb522e88fc86b0d1cae61dcefcfe4cc20231Mike Stump
3788af7b44d847d3e44c43346d508b2e55a6254b6e9dMike Stump  bool HasCopyAndDispose = BlockRequiresCopying(Ty);
3789af7b44d847d3e44c43346d508b2e55a6254b6e9dMike Stump
3790af7b44d847d3e44c43346d508b2e55a6254b6e9dMike Stump  // FIXME: Move up
3791f5942a44880be26878592eb052b737579349411eBenjamin Kramer  llvm::SmallString<36> Name;
3792f5942a44880be26878592eb052b737579349411eBenjamin Kramer  llvm::raw_svector_ostream(Name) << "__Block_byref_" <<
3793f5942a44880be26878592eb052b737579349411eBenjamin Kramer                                  ++UniqueBlockByRefTypeID << '_' << DeclName;
3794af7b44d847d3e44c43346d508b2e55a6254b6e9dMike Stump  RecordDecl *T;
3795ba877adeb49ed6dc17f27fa3a3bcd0cca713fd68Abramo Bagnara  T = CreateRecordDecl(*this, TTK_Struct, TUDecl, &Idents.get(Name.str()));
3796af7b44d847d3e44c43346d508b2e55a6254b6e9dMike Stump  T->startDefinition();
3797af7b44d847d3e44c43346d508b2e55a6254b6e9dMike Stump  QualType Int32Ty = IntTy;
3798af7b44d847d3e44c43346d508b2e55a6254b6e9dMike Stump  assert(getIntWidth(IntTy) == 32 && "non-32bit int not supported");
3799af7b44d847d3e44c43346d508b2e55a6254b6e9dMike Stump  QualType FieldTypes[] = {
3800af7b44d847d3e44c43346d508b2e55a6254b6e9dMike Stump    getPointerType(VoidPtrTy),
3801af7b44d847d3e44c43346d508b2e55a6254b6e9dMike Stump    getPointerType(getTagDeclType(T)),
3802af7b44d847d3e44c43346d508b2e55a6254b6e9dMike Stump    Int32Ty,
3803af7b44d847d3e44c43346d508b2e55a6254b6e9dMike Stump    Int32Ty,
3804af7b44d847d3e44c43346d508b2e55a6254b6e9dMike Stump    getPointerType(VoidPtrTy),
3805af7b44d847d3e44c43346d508b2e55a6254b6e9dMike Stump    getPointerType(VoidPtrTy),
3806af7b44d847d3e44c43346d508b2e55a6254b6e9dMike Stump    Ty
3807af7b44d847d3e44c43346d508b2e55a6254b6e9dMike Stump  };
3808af7b44d847d3e44c43346d508b2e55a6254b6e9dMike Stump
38095f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner  StringRef FieldNames[] = {
3810af7b44d847d3e44c43346d508b2e55a6254b6e9dMike Stump    "__isa",
3811af7b44d847d3e44c43346d508b2e55a6254b6e9dMike Stump    "__forwarding",
3812af7b44d847d3e44c43346d508b2e55a6254b6e9dMike Stump    "__flags",
3813af7b44d847d3e44c43346d508b2e55a6254b6e9dMike Stump    "__size",
3814af7b44d847d3e44c43346d508b2e55a6254b6e9dMike Stump    "__copy_helper",
3815af7b44d847d3e44c43346d508b2e55a6254b6e9dMike Stump    "__destroy_helper",
3816af7b44d847d3e44c43346d508b2e55a6254b6e9dMike Stump    DeclName,
3817af7b44d847d3e44c43346d508b2e55a6254b6e9dMike Stump  };
3818af7b44d847d3e44c43346d508b2e55a6254b6e9dMike Stump
3819af7b44d847d3e44c43346d508b2e55a6254b6e9dMike Stump  for (size_t i = 0; i < 7; ++i) {
3820af7b44d847d3e44c43346d508b2e55a6254b6e9dMike Stump    if (!HasCopyAndDispose && i >=4 && i <= 5)
3821af7b44d847d3e44c43346d508b2e55a6254b6e9dMike Stump      continue;
3822af7b44d847d3e44c43346d508b2e55a6254b6e9dMike Stump    FieldDecl *Field = FieldDecl::Create(*this, T, SourceLocation(),
3823ff676cb48fe8bf7be2feaa251dc7c5fb15af4730Abramo Bagnara                                         SourceLocation(),
3824af7b44d847d3e44c43346d508b2e55a6254b6e9dMike Stump                                         &Idents.get(FieldNames[i]),
3825a93c934af4fbf97cbe8e649d82e68ccacfe57c95John McCall                                         FieldTypes[i], /*TInfo=*/0,
38267a614d8380297fcd2bc23986241905d97222948cRichard Smith                                         /*BitWidth=*/0, /*Mutable=*/false,
38277a614d8380297fcd2bc23986241905d97222948cRichard Smith                                         /*HasInit=*/false);
38282888b65aae768f54062505330df7be230a0510c7John McCall    Field->setAccess(AS_public);
3829af7b44d847d3e44c43346d508b2e55a6254b6e9dMike Stump    T->addDecl(Field);
3830af7b44d847d3e44c43346d508b2e55a6254b6e9dMike Stump  }
3831af7b44d847d3e44c43346d508b2e55a6254b6e9dMike Stump
3832838db383b69b9fb55f55c8e9546477df198a4faaDouglas Gregor  T->completeDefinition();
3833af7b44d847d3e44c43346d508b2e55a6254b6e9dMike Stump
3834af7b44d847d3e44c43346d508b2e55a6254b6e9dMike Stump  return getPointerType(getTagDeclType(T));
3835ea26cb522e88fc86b0d1cae61dcefcfe4cc20231Mike Stump}
3836ea26cb522e88fc86b0d1cae61dcefcfe4cc20231Mike Stump
3837e8c49533521c40643653f943d47229e62d277f88Anders Carlsson// This returns true if a type has been typedefed to BOOL:
3838e8c49533521c40643653f943d47229e62d277f88Anders Carlsson// typedef <type> BOOL;
38392d99833e8c956775f2183601cd120b65b569c867Chris Lattnerstatic bool isTypeTypedefedAsBOOL(QualType T) {
3840e8c49533521c40643653f943d47229e62d277f88Anders Carlsson  if (const TypedefType *TT = dyn_cast<TypedefType>(T))
3841bb49c3ee5d270485f4b273691fd14bc97403fa5dChris Lattner    if (IdentifierInfo *II = TT->getDecl()->getIdentifier())
3842bb49c3ee5d270485f4b273691fd14bc97403fa5dChris Lattner      return II->isStr("BOOL");
38431eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
384485f9bceab1542aafff012d4d28e998f4ba16e362Anders Carlsson  return false;
384585f9bceab1542aafff012d4d28e998f4ba16e362Anders Carlsson}
384685f9bceab1542aafff012d4d28e998f4ba16e362Anders Carlsson
3847a526c5c67e5a0473c340903ee542ce570119665fTed Kremenek/// getObjCEncodingTypeSize returns size of type for objective-c encoding
384833e1d64ab5cd5d27f8530ccd056191fe2c9f3f2eFariborz Jahanian/// purpose.
38494ba2a17694148e16eaa8d3917f657ffcd3667be4Jay FoadCharUnits ASTContext::getObjCEncodingTypeSize(QualType type) const {
3850f968d8374791c37bc464efd9168c2d33dd73605fDouglas Gregor  if (!type->isIncompleteArrayType() && type->isIncompleteType())
3851f968d8374791c37bc464efd9168c2d33dd73605fDouglas Gregor    return CharUnits::Zero();
3852f968d8374791c37bc464efd9168c2d33dd73605fDouglas Gregor
3853199c3d6cd16aebbb9c7f0d42af9d922c9628bf70Ken Dyck  CharUnits sz = getTypeSizeInChars(type);
38541eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
385533e1d64ab5cd5d27f8530ccd056191fe2c9f3f2eFariborz Jahanian  // Make all integer and enum types at least as large as an int
38562ade35e2cfd554e49d35a52047cea98a82787af9Douglas Gregor  if (sz.isPositive() && type->isIntegralOrEnumerationType())
3857199c3d6cd16aebbb9c7f0d42af9d922c9628bf70Ken Dyck    sz = std::max(sz, getTypeSizeInChars(IntTy));
385833e1d64ab5cd5d27f8530ccd056191fe2c9f3f2eFariborz Jahanian  // Treat arrays as pointers, since that's how they're passed in.
385933e1d64ab5cd5d27f8530ccd056191fe2c9f3f2eFariborz Jahanian  else if (type->isArrayType())
3860199c3d6cd16aebbb9c7f0d42af9d922c9628bf70Ken Dyck    sz = getTypeSizeInChars(VoidPtrTy);
3861aa8741a1db98eef05f09b1200dba94aa5dc3bc3dKen Dyck  return sz;
3862199c3d6cd16aebbb9c7f0d42af9d922c9628bf70Ken Dyck}
3863199c3d6cd16aebbb9c7f0d42af9d922c9628bf70Ken Dyck
3864199c3d6cd16aebbb9c7f0d42af9d922c9628bf70Ken Dyckstatic inline
3865199c3d6cd16aebbb9c7f0d42af9d922c9628bf70Ken Dyckstd::string charUnitsToString(const CharUnits &CU) {
3866199c3d6cd16aebbb9c7f0d42af9d922c9628bf70Ken Dyck  return llvm::itostr(CU.getQuantity());
386733e1d64ab5cd5d27f8530ccd056191fe2c9f3f2eFariborz Jahanian}
386833e1d64ab5cd5d27f8530ccd056191fe2c9f3f2eFariborz Jahanian
38696b5a61b6dc400027fd793dcadceeb9da944a37eaJohn McCall/// getObjCEncodingForBlock - Return the encoded type for this block
38705e530af5d51572a0ed5dbe50da54bd333840c63dDavid Chisnall/// declaration.
38716b5a61b6dc400027fd793dcadceeb9da944a37eaJohn McCallstd::string ASTContext::getObjCEncodingForBlock(const BlockExpr *Expr) const {
38726b5a61b6dc400027fd793dcadceeb9da944a37eaJohn McCall  std::string S;
38736b5a61b6dc400027fd793dcadceeb9da944a37eaJohn McCall
38745e530af5d51572a0ed5dbe50da54bd333840c63dDavid Chisnall  const BlockDecl *Decl = Expr->getBlockDecl();
38755e530af5d51572a0ed5dbe50da54bd333840c63dDavid Chisnall  QualType BlockTy =
38765e530af5d51572a0ed5dbe50da54bd333840c63dDavid Chisnall      Expr->getType()->getAs<BlockPointerType>()->getPointeeType();
38775e530af5d51572a0ed5dbe50da54bd333840c63dDavid Chisnall  // Encode result type.
3878c71a4915ca216847599d03cab4ed1c5086b0eb43John McCall  getObjCEncodingForType(BlockTy->getAs<FunctionType>()->getResultType(), S);
38795e530af5d51572a0ed5dbe50da54bd333840c63dDavid Chisnall  // Compute size of all parameters.
38805e530af5d51572a0ed5dbe50da54bd333840c63dDavid Chisnall  // Start with computing size of a pointer in number of bytes.
38815e530af5d51572a0ed5dbe50da54bd333840c63dDavid Chisnall  // FIXME: There might(should) be a better way of doing this computation!
38825e530af5d51572a0ed5dbe50da54bd333840c63dDavid Chisnall  SourceLocation Loc;
3883199c3d6cd16aebbb9c7f0d42af9d922c9628bf70Ken Dyck  CharUnits PtrSize = getTypeSizeInChars(VoidPtrTy);
3884199c3d6cd16aebbb9c7f0d42af9d922c9628bf70Ken Dyck  CharUnits ParmOffset = PtrSize;
38856f46c2653c1545cc3fef0c0df996d18160160ce8Fariborz Jahanian  for (BlockDecl::param_const_iterator PI = Decl->param_begin(),
38865e530af5d51572a0ed5dbe50da54bd333840c63dDavid Chisnall       E = Decl->param_end(); PI != E; ++PI) {
38875e530af5d51572a0ed5dbe50da54bd333840c63dDavid Chisnall    QualType PType = (*PI)->getType();
3888aa8741a1db98eef05f09b1200dba94aa5dc3bc3dKen Dyck    CharUnits sz = getObjCEncodingTypeSize(PType);
3889199c3d6cd16aebbb9c7f0d42af9d922c9628bf70Ken Dyck    assert (sz.isPositive() && "BlockExpr - Incomplete param type");
38905e530af5d51572a0ed5dbe50da54bd333840c63dDavid Chisnall    ParmOffset += sz;
38915e530af5d51572a0ed5dbe50da54bd333840c63dDavid Chisnall  }
38925e530af5d51572a0ed5dbe50da54bd333840c63dDavid Chisnall  // Size of the argument frame
3893199c3d6cd16aebbb9c7f0d42af9d922c9628bf70Ken Dyck  S += charUnitsToString(ParmOffset);
38945e530af5d51572a0ed5dbe50da54bd333840c63dDavid Chisnall  // Block pointer and offset.
38955e530af5d51572a0ed5dbe50da54bd333840c63dDavid Chisnall  S += "@?0";
38965e530af5d51572a0ed5dbe50da54bd333840c63dDavid Chisnall
38975e530af5d51572a0ed5dbe50da54bd333840c63dDavid Chisnall  // Argument types.
38985e530af5d51572a0ed5dbe50da54bd333840c63dDavid Chisnall  ParmOffset = PtrSize;
38995e530af5d51572a0ed5dbe50da54bd333840c63dDavid Chisnall  for (BlockDecl::param_const_iterator PI = Decl->param_begin(), E =
39005e530af5d51572a0ed5dbe50da54bd333840c63dDavid Chisnall       Decl->param_end(); PI != E; ++PI) {
39015e530af5d51572a0ed5dbe50da54bd333840c63dDavid Chisnall    ParmVarDecl *PVDecl = *PI;
39025e530af5d51572a0ed5dbe50da54bd333840c63dDavid Chisnall    QualType PType = PVDecl->getOriginalType();
39035e530af5d51572a0ed5dbe50da54bd333840c63dDavid Chisnall    if (const ArrayType *AT =
39045e530af5d51572a0ed5dbe50da54bd333840c63dDavid Chisnall          dyn_cast<ArrayType>(PType->getCanonicalTypeInternal())) {
39055e530af5d51572a0ed5dbe50da54bd333840c63dDavid Chisnall      // Use array's original type only if it has known number of
39065389f48b24937ad7b4093307128b3cbf25235654David Chisnall      // elements.
39075389f48b24937ad7b4093307128b3cbf25235654David Chisnall      if (!isa<ConstantArrayType>(AT))
39085389f48b24937ad7b4093307128b3cbf25235654David Chisnall        PType = PVDecl->getType();
39095389f48b24937ad7b4093307128b3cbf25235654David Chisnall    } else if (PType->isFunctionType())
39105389f48b24937ad7b4093307128b3cbf25235654David Chisnall      PType = PVDecl->getType();
39115389f48b24937ad7b4093307128b3cbf25235654David Chisnall    getObjCEncodingForType(PType, S);
39125389f48b24937ad7b4093307128b3cbf25235654David Chisnall    S += charUnitsToString(ParmOffset);
39135389f48b24937ad7b4093307128b3cbf25235654David Chisnall    ParmOffset += getObjCEncodingTypeSize(PType);
39145389f48b24937ad7b4093307128b3cbf25235654David Chisnall  }
39156b5a61b6dc400027fd793dcadceeb9da944a37eaJohn McCall
39166b5a61b6dc400027fd793dcadceeb9da944a37eaJohn McCall  return S;
39175389f48b24937ad7b4093307128b3cbf25235654David Chisnall}
39185389f48b24937ad7b4093307128b3cbf25235654David Chisnall
3919f968d8374791c37bc464efd9168c2d33dd73605fDouglas Gregorbool ASTContext::getObjCEncodingForFunctionDecl(const FunctionDecl *Decl,
39205389f48b24937ad7b4093307128b3cbf25235654David Chisnall                                                std::string& S) {
39215389f48b24937ad7b4093307128b3cbf25235654David Chisnall  // Encode result type.
39225389f48b24937ad7b4093307128b3cbf25235654David Chisnall  getObjCEncodingForType(Decl->getResultType(), S);
39235389f48b24937ad7b4093307128b3cbf25235654David Chisnall  CharUnits ParmOffset;
39245389f48b24937ad7b4093307128b3cbf25235654David Chisnall  // Compute size of all parameters.
39255389f48b24937ad7b4093307128b3cbf25235654David Chisnall  for (FunctionDecl::param_const_iterator PI = Decl->param_begin(),
39265389f48b24937ad7b4093307128b3cbf25235654David Chisnall       E = Decl->param_end(); PI != E; ++PI) {
39275389f48b24937ad7b4093307128b3cbf25235654David Chisnall    QualType PType = (*PI)->getType();
39285389f48b24937ad7b4093307128b3cbf25235654David Chisnall    CharUnits sz = getObjCEncodingTypeSize(PType);
3929f968d8374791c37bc464efd9168c2d33dd73605fDouglas Gregor    if (sz.isZero())
3930f968d8374791c37bc464efd9168c2d33dd73605fDouglas Gregor      return true;
3931f968d8374791c37bc464efd9168c2d33dd73605fDouglas Gregor
39325389f48b24937ad7b4093307128b3cbf25235654David Chisnall    assert (sz.isPositive() &&
3933f968d8374791c37bc464efd9168c2d33dd73605fDouglas Gregor        "getObjCEncodingForFunctionDecl - Incomplete param type");
39345389f48b24937ad7b4093307128b3cbf25235654David Chisnall    ParmOffset += sz;
39355389f48b24937ad7b4093307128b3cbf25235654David Chisnall  }
39365389f48b24937ad7b4093307128b3cbf25235654David Chisnall  S += charUnitsToString(ParmOffset);
39375389f48b24937ad7b4093307128b3cbf25235654David Chisnall  ParmOffset = CharUnits::Zero();
39385389f48b24937ad7b4093307128b3cbf25235654David Chisnall
39395389f48b24937ad7b4093307128b3cbf25235654David Chisnall  // Argument types.
39405389f48b24937ad7b4093307128b3cbf25235654David Chisnall  for (FunctionDecl::param_const_iterator PI = Decl->param_begin(),
39415389f48b24937ad7b4093307128b3cbf25235654David Chisnall       E = Decl->param_end(); PI != E; ++PI) {
39425389f48b24937ad7b4093307128b3cbf25235654David Chisnall    ParmVarDecl *PVDecl = *PI;
39435389f48b24937ad7b4093307128b3cbf25235654David Chisnall    QualType PType = PVDecl->getOriginalType();
39445389f48b24937ad7b4093307128b3cbf25235654David Chisnall    if (const ArrayType *AT =
39455389f48b24937ad7b4093307128b3cbf25235654David Chisnall          dyn_cast<ArrayType>(PType->getCanonicalTypeInternal())) {
39465389f48b24937ad7b4093307128b3cbf25235654David Chisnall      // Use array's original type only if it has known number of
39475e530af5d51572a0ed5dbe50da54bd333840c63dDavid Chisnall      // elements.
39485e530af5d51572a0ed5dbe50da54bd333840c63dDavid Chisnall      if (!isa<ConstantArrayType>(AT))
39495e530af5d51572a0ed5dbe50da54bd333840c63dDavid Chisnall        PType = PVDecl->getType();
39505e530af5d51572a0ed5dbe50da54bd333840c63dDavid Chisnall    } else if (PType->isFunctionType())
39515e530af5d51572a0ed5dbe50da54bd333840c63dDavid Chisnall      PType = PVDecl->getType();
39525e530af5d51572a0ed5dbe50da54bd333840c63dDavid Chisnall    getObjCEncodingForType(PType, S);
3953199c3d6cd16aebbb9c7f0d42af9d922c9628bf70Ken Dyck    S += charUnitsToString(ParmOffset);
3954aa8741a1db98eef05f09b1200dba94aa5dc3bc3dKen Dyck    ParmOffset += getObjCEncodingTypeSize(PType);
39555e530af5d51572a0ed5dbe50da54bd333840c63dDavid Chisnall  }
3956f968d8374791c37bc464efd9168c2d33dd73605fDouglas Gregor
3957f968d8374791c37bc464efd9168c2d33dd73605fDouglas Gregor  return false;
39585e530af5d51572a0ed5dbe50da54bd333840c63dDavid Chisnall}
39595e530af5d51572a0ed5dbe50da54bd333840c63dDavid Chisnall
3960a526c5c67e5a0473c340903ee542ce570119665fTed Kremenek/// getObjCEncodingForMethodDecl - Return the encoded type for this method
396133e1d64ab5cd5d27f8530ccd056191fe2c9f3f2eFariborz Jahanian/// declaration.
3962f968d8374791c37bc464efd9168c2d33dd73605fDouglas Gregorbool ASTContext::getObjCEncodingForMethodDecl(const ObjCMethodDecl *Decl,
39634ba2a17694148e16eaa8d3917f657ffcd3667be4Jay Foad                                              std::string& S) const {
3964c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar  // FIXME: This is not very efficient.
3965ecb01e666665efabd2aa76a76f6080e2a78965faFariborz Jahanian  // Encode type qualifer, 'in', 'inout', etc. for the return type.
3966a526c5c67e5a0473c340903ee542ce570119665fTed Kremenek  getObjCEncodingForTypeQualifier(Decl->getObjCDeclQualifier(), S);
396733e1d64ab5cd5d27f8530ccd056191fe2c9f3f2eFariborz Jahanian  // Encode result type.
39680d504c1da852e58ff802545c823ecff3b6c654b8Daniel Dunbar  getObjCEncodingForType(Decl->getResultType(), S);
396933e1d64ab5cd5d27f8530ccd056191fe2c9f3f2eFariborz Jahanian  // Compute size of all parameters.
397033e1d64ab5cd5d27f8530ccd056191fe2c9f3f2eFariborz Jahanian  // Start with computing size of a pointer in number of bytes.
397133e1d64ab5cd5d27f8530ccd056191fe2c9f3f2eFariborz Jahanian  // FIXME: There might(should) be a better way of doing this computation!
397233e1d64ab5cd5d27f8530ccd056191fe2c9f3f2eFariborz Jahanian  SourceLocation Loc;
3973199c3d6cd16aebbb9c7f0d42af9d922c9628bf70Ken Dyck  CharUnits PtrSize = getTypeSizeInChars(VoidPtrTy);
397433e1d64ab5cd5d27f8530ccd056191fe2c9f3f2eFariborz Jahanian  // The first two arguments (self and _cmd) are pointers; account for
397533e1d64ab5cd5d27f8530ccd056191fe2c9f3f2eFariborz Jahanian  // their size.
3976199c3d6cd16aebbb9c7f0d42af9d922c9628bf70Ken Dyck  CharUnits ParmOffset = 2 * PtrSize;
397789951a86b594513c2a013532ed45d197413b1087Chris Lattner  for (ObjCMethodDecl::param_iterator PI = Decl->param_begin(),
39787732cc9c0fdc97a2f8cce4e5933d8103213d1aefFariborz Jahanian       E = Decl->sel_param_end(); PI != E; ++PI) {
397989951a86b594513c2a013532ed45d197413b1087Chris Lattner    QualType PType = (*PI)->getType();
3980aa8741a1db98eef05f09b1200dba94aa5dc3bc3dKen Dyck    CharUnits sz = getObjCEncodingTypeSize(PType);
3981f968d8374791c37bc464efd9168c2d33dd73605fDouglas Gregor    if (sz.isZero())
3982f968d8374791c37bc464efd9168c2d33dd73605fDouglas Gregor      return true;
3983f968d8374791c37bc464efd9168c2d33dd73605fDouglas Gregor
3984199c3d6cd16aebbb9c7f0d42af9d922c9628bf70Ken Dyck    assert (sz.isPositive() &&
3985199c3d6cd16aebbb9c7f0d42af9d922c9628bf70Ken Dyck        "getObjCEncodingForMethodDecl - Incomplete param type");
398633e1d64ab5cd5d27f8530ccd056191fe2c9f3f2eFariborz Jahanian    ParmOffset += sz;
398733e1d64ab5cd5d27f8530ccd056191fe2c9f3f2eFariborz Jahanian  }
3988199c3d6cd16aebbb9c7f0d42af9d922c9628bf70Ken Dyck  S += charUnitsToString(ParmOffset);
398933e1d64ab5cd5d27f8530ccd056191fe2c9f3f2eFariborz Jahanian  S += "@0:";
3990199c3d6cd16aebbb9c7f0d42af9d922c9628bf70Ken Dyck  S += charUnitsToString(PtrSize);
39911eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
399233e1d64ab5cd5d27f8530ccd056191fe2c9f3f2eFariborz Jahanian  // Argument types.
399333e1d64ab5cd5d27f8530ccd056191fe2c9f3f2eFariborz Jahanian  ParmOffset = 2 * PtrSize;
399489951a86b594513c2a013532ed45d197413b1087Chris Lattner  for (ObjCMethodDecl::param_iterator PI = Decl->param_begin(),
39957732cc9c0fdc97a2f8cce4e5933d8103213d1aefFariborz Jahanian       E = Decl->sel_param_end(); PI != E; ++PI) {
399689951a86b594513c2a013532ed45d197413b1087Chris Lattner    ParmVarDecl *PVDecl = *PI;
39971eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    QualType PType = PVDecl->getOriginalType();
39984306d3cb9116605728252e2738df24b9f6ab53c3Fariborz Jahanian    if (const ArrayType *AT =
3999ab76d45e023fc5ae966968344e180cd09fdcc746Steve Naroff          dyn_cast<ArrayType>(PType->getCanonicalTypeInternal())) {
4000ab76d45e023fc5ae966968344e180cd09fdcc746Steve Naroff      // Use array's original type only if it has known number of
4001ab76d45e023fc5ae966968344e180cd09fdcc746Steve Naroff      // elements.
4002bb3fde337fb712c0e6da8790d431621be4793048Steve Naroff      if (!isa<ConstantArrayType>(AT))
4003ab76d45e023fc5ae966968344e180cd09fdcc746Steve Naroff        PType = PVDecl->getType();
4004ab76d45e023fc5ae966968344e180cd09fdcc746Steve Naroff    } else if (PType->isFunctionType())
4005ab76d45e023fc5ae966968344e180cd09fdcc746Steve Naroff      PType = PVDecl->getType();
4006ecb01e666665efabd2aa76a76f6080e2a78965faFariborz Jahanian    // Process argument qualifiers for user supplied arguments; such as,
400733e1d64ab5cd5d27f8530ccd056191fe2c9f3f2eFariborz Jahanian    // 'in', 'inout', etc.
40084306d3cb9116605728252e2738df24b9f6ab53c3Fariborz Jahanian    getObjCEncodingForTypeQualifier(PVDecl->getObjCDeclQualifier(), S);
40090d504c1da852e58ff802545c823ecff3b6c654b8Daniel Dunbar    getObjCEncodingForType(PType, S);
4010199c3d6cd16aebbb9c7f0d42af9d922c9628bf70Ken Dyck    S += charUnitsToString(ParmOffset);
4011aa8741a1db98eef05f09b1200dba94aa5dc3bc3dKen Dyck    ParmOffset += getObjCEncodingTypeSize(PType);
401233e1d64ab5cd5d27f8530ccd056191fe2c9f3f2eFariborz Jahanian  }
4013f968d8374791c37bc464efd9168c2d33dd73605fDouglas Gregor
4014f968d8374791c37bc464efd9168c2d33dd73605fDouglas Gregor  return false;
401533e1d64ab5cd5d27f8530ccd056191fe2c9f3f2eFariborz Jahanian}
401633e1d64ab5cd5d27f8530ccd056191fe2c9f3f2eFariborz Jahanian
4017c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar/// getObjCEncodingForPropertyDecl - Return the encoded type for this
401883bccb85ff4b9981c4250c45494b439df8cbf983Fariborz Jahanian/// property declaration. If non-NULL, Container must be either an
4019c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar/// ObjCCategoryImplDecl or ObjCImplementationDecl; it should only be
4020c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar/// NULL when getting encodings for protocol properties.
40211eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// Property attributes are stored as a comma-delimited C string. The simple
40221eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// attributes readonly and bycopy are encoded as single characters. The
40231eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// parametrized attributes, getter=name, setter=name, and ivar=name, are
40241eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// encoded as single characters, followed by an identifier. Property types
40251eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// are also encoded as a parametrized attribute. The characters used to encode
402683bccb85ff4b9981c4250c45494b439df8cbf983Fariborz Jahanian/// these attributes are defined by the following enumeration:
402783bccb85ff4b9981c4250c45494b439df8cbf983Fariborz Jahanian/// @code
402883bccb85ff4b9981c4250c45494b439df8cbf983Fariborz Jahanian/// enum PropertyAttributes {
402983bccb85ff4b9981c4250c45494b439df8cbf983Fariborz Jahanian/// kPropertyReadOnly = 'R',   // property is read-only.
403083bccb85ff4b9981c4250c45494b439df8cbf983Fariborz Jahanian/// kPropertyBycopy = 'C',     // property is a copy of the value last assigned
403183bccb85ff4b9981c4250c45494b439df8cbf983Fariborz Jahanian/// kPropertyByref = '&',  // property is a reference to the value last assigned
403283bccb85ff4b9981c4250c45494b439df8cbf983Fariborz Jahanian/// kPropertyDynamic = 'D',    // property is dynamic
403383bccb85ff4b9981c4250c45494b439df8cbf983Fariborz Jahanian/// kPropertyGetter = 'G',     // followed by getter selector name
403483bccb85ff4b9981c4250c45494b439df8cbf983Fariborz Jahanian/// kPropertySetter = 'S',     // followed by setter selector name
403583bccb85ff4b9981c4250c45494b439df8cbf983Fariborz Jahanian/// kPropertyInstanceVariable = 'V'  // followed by instance variable  name
403683bccb85ff4b9981c4250c45494b439df8cbf983Fariborz Jahanian/// kPropertyType = 't'              // followed by old-style type encoding.
403783bccb85ff4b9981c4250c45494b439df8cbf983Fariborz Jahanian/// kPropertyWeak = 'W'              // 'weak' property
403883bccb85ff4b9981c4250c45494b439df8cbf983Fariborz Jahanian/// kPropertyStrong = 'P'            // property GC'able
403983bccb85ff4b9981c4250c45494b439df8cbf983Fariborz Jahanian/// kPropertyNonAtomic = 'N'         // property non-atomic
404083bccb85ff4b9981c4250c45494b439df8cbf983Fariborz Jahanian/// };
404183bccb85ff4b9981c4250c45494b439df8cbf983Fariborz Jahanian/// @endcode
40421eb4433ac451dc16f4133a88af2d002ac26c58efMike Stumpvoid ASTContext::getObjCEncodingForPropertyDecl(const ObjCPropertyDecl *PD,
4043c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar                                                const Decl *Container,
40444ba2a17694148e16eaa8d3917f657ffcd3667be4Jay Foad                                                std::string& S) const {
4045c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar  // Collect information from the property implementation decl(s).
4046c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar  bool Dynamic = false;
4047c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar  ObjCPropertyImplDecl *SynthesizePID = 0;
4048c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar
4049c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar  // FIXME: Duplicated code due to poor abstraction.
4050c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar  if (Container) {
40511eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    if (const ObjCCategoryImplDecl *CID =
4052c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar        dyn_cast<ObjCCategoryImplDecl>(Container)) {
4053c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar      for (ObjCCategoryImplDecl::propimpl_iterator
405417945a0f64fe03ff6ec0c2146005a87636e3ac12Argyrios Kyrtzidis             i = CID->propimpl_begin(), e = CID->propimpl_end();
4055653f1b1bf293a9bd96fd4dd6372e779cc7af1597Douglas Gregor           i != e; ++i) {
4056c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar        ObjCPropertyImplDecl *PID = *i;
4057c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar        if (PID->getPropertyDecl() == PD) {
4058c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar          if (PID->getPropertyImplementation()==ObjCPropertyImplDecl::Dynamic) {
4059c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar            Dynamic = true;
4060c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar          } else {
4061c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar            SynthesizePID = PID;
4062c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar          }
4063c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar        }
4064c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar      }
4065c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar    } else {
406661710854be2b098428aff5316e64bd34b30fbcb7Chris Lattner      const ObjCImplementationDecl *OID=cast<ObjCImplementationDecl>(Container);
4067c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar      for (ObjCCategoryImplDecl::propimpl_iterator
406817945a0f64fe03ff6ec0c2146005a87636e3ac12Argyrios Kyrtzidis             i = OID->propimpl_begin(), e = OID->propimpl_end();
4069653f1b1bf293a9bd96fd4dd6372e779cc7af1597Douglas Gregor           i != e; ++i) {
4070c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar        ObjCPropertyImplDecl *PID = *i;
4071c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar        if (PID->getPropertyDecl() == PD) {
4072c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar          if (PID->getPropertyImplementation()==ObjCPropertyImplDecl::Dynamic) {
4073c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar            Dynamic = true;
4074c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar          } else {
4075c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar            SynthesizePID = PID;
4076c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar          }
4077c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar        }
40781eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      }
4079c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar    }
4080c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar  }
4081c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar
4082c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar  // FIXME: This is not very efficient.
4083c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar  S = "T";
4084c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar
4085c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar  // Encode result type.
4086090b3f71702c5626d8520f9608d77c6f26dcfa15Fariborz Jahanian  // GCC has some special rules regarding encoding of properties which
4087090b3f71702c5626d8520f9608d77c6f26dcfa15Fariborz Jahanian  // closely resembles encoding of ivars.
40881eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  getObjCEncodingForTypeImpl(PD->getType(), S, true, true, 0,
4089090b3f71702c5626d8520f9608d77c6f26dcfa15Fariborz Jahanian                             true /* outermost type */,
4090090b3f71702c5626d8520f9608d77c6f26dcfa15Fariborz Jahanian                             true /* encoding for property */);
4091c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar
4092c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar  if (PD->isReadOnly()) {
4093c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar    S += ",R";
4094c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar  } else {
4095c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar    switch (PD->getSetterKind()) {
4096c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar    case ObjCPropertyDecl::Assign: break;
4097c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar    case ObjCPropertyDecl::Copy:   S += ",C"; break;
40981eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    case ObjCPropertyDecl::Retain: S += ",&"; break;
40993a02b44e3948f7762dbfba94b7961281ca29d022Fariborz Jahanian    case ObjCPropertyDecl::Weak:   S += ",W"; break;
4100c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar    }
4101c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar  }
4102c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar
4103c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar  // It really isn't clear at all what this means, since properties
4104c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar  // are "dynamic by default".
4105c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar  if (Dynamic)
4106c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar    S += ",D";
4107c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar
4108090b3f71702c5626d8520f9608d77c6f26dcfa15Fariborz Jahanian  if (PD->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_nonatomic)
4109090b3f71702c5626d8520f9608d77c6f26dcfa15Fariborz Jahanian    S += ",N";
41101eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
4111c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar  if (PD->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_getter) {
4112c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar    S += ",G";
4113077bf5e2f48acfa9e7d69429b6e4ba86ea14896dChris Lattner    S += PD->getGetterName().getAsString();
4114c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar  }
4115c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar
4116c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar  if (PD->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_setter) {
4117c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar    S += ",S";
4118077bf5e2f48acfa9e7d69429b6e4ba86ea14896dChris Lattner    S += PD->getSetterName().getAsString();
4119c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar  }
4120c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar
4121c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar  if (SynthesizePID) {
4122c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar    const ObjCIvarDecl *OID = SynthesizePID->getPropertyIvarDecl();
4123c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar    S += ",V";
412439f34e97d6a468f0a7dfa5664c61217cffc65b74Chris Lattner    S += OID->getNameAsString();
4125c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar  }
4126c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar
4127c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar  // FIXME: OBJCGC: weak & strong
4128c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar}
4129c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar
4130a1c033e9514865f3a7b0d8b3b20e6de926cfec6cFariborz Jahanian/// getLegacyIntegralTypeEncoding -
41311eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// Another legacy compatibility encoding: 32-bit longs are encoded as
41321eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// 'l' or 'L' , but not always.  For typedefs, we need to use
4133a1c033e9514865f3a7b0d8b3b20e6de926cfec6cFariborz Jahanian/// 'i' or 'I' instead if encoding a struct field, or a pointer!
4134a1c033e9514865f3a7b0d8b3b20e6de926cfec6cFariborz Jahanian///
4135a1c033e9514865f3a7b0d8b3b20e6de926cfec6cFariborz Jahanianvoid ASTContext::getLegacyIntegralTypeEncoding (QualType &PointeeTy) const {
41368e1fab243ab8023b7ee3899745386b3b3a4258f8Mike Stump  if (isa<TypedefType>(PointeeTy.getTypePtr())) {
4137183700f494ec9b6701b6efe82bcb25f4c79ba561John McCall    if (const BuiltinType *BT = PointeeTy->getAs<BuiltinType>()) {
41384ba2a17694148e16eaa8d3917f657ffcd3667be4Jay Foad      if (BT->getKind() == BuiltinType::ULong && getIntWidth(PointeeTy) == 32)
4139a1c033e9514865f3a7b0d8b3b20e6de926cfec6cFariborz Jahanian        PointeeTy = UnsignedIntTy;
41401eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      else
41414ba2a17694148e16eaa8d3917f657ffcd3667be4Jay Foad        if (BT->getKind() == BuiltinType::Long && getIntWidth(PointeeTy) == 32)
4142a1c033e9514865f3a7b0d8b3b20e6de926cfec6cFariborz Jahanian          PointeeTy = IntTy;
4143a1c033e9514865f3a7b0d8b3b20e6de926cfec6cFariborz Jahanian    }
4144a1c033e9514865f3a7b0d8b3b20e6de926cfec6cFariborz Jahanian  }
4145a1c033e9514865f3a7b0d8b3b20e6de926cfec6cFariborz Jahanian}
4146a1c033e9514865f3a7b0d8b3b20e6de926cfec6cFariborz Jahanian
41477d6b46d9a9d75dea8ef9f6973dd50633c1f37963Fariborz Jahanianvoid ASTContext::getObjCEncodingForType(QualType T, std::string& S,
41484ba2a17694148e16eaa8d3917f657ffcd3667be4Jay Foad                                        const FieldDecl *Field) const {
414982a6cfbc421cc99c5b7313271f399f7ef95056ecDaniel Dunbar  // We follow the behavior of gcc, expanding structures which are
415082a6cfbc421cc99c5b7313271f399f7ef95056ecDaniel Dunbar  // directly pointed to, and expanding embedded structures. Note that
415182a6cfbc421cc99c5b7313271f399f7ef95056ecDaniel Dunbar  // these rules are sufficient to prevent recursive encoding of the
415282a6cfbc421cc99c5b7313271f399f7ef95056ecDaniel Dunbar  // same type.
41531eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  getObjCEncodingForTypeImpl(T, S, true, true, Field,
41545b8c7d9fb620ba3a71e996d61e7b9bdf763b5c09Fariborz Jahanian                             true /* outermost type */);
415582a6cfbc421cc99c5b7313271f399f7ef95056ecDaniel Dunbar}
415682a6cfbc421cc99c5b7313271f399f7ef95056ecDaniel Dunbar
415764fd7e86c1a90d9ff78e4a0bd79f69499667a4e3David Chisnallstatic char ObjCEncodingForPrimitiveKind(const ASTContext *C, QualType T) {
415864fd7e86c1a90d9ff78e4a0bd79f69499667a4e3David Chisnall    switch (T->getAs<BuiltinType>()->getKind()) {
415964fd7e86c1a90d9ff78e4a0bd79f69499667a4e3David Chisnall    default: assert(0 && "Unhandled builtin type kind");
416064fd7e86c1a90d9ff78e4a0bd79f69499667a4e3David Chisnall    case BuiltinType::Void:       return 'v';
416164fd7e86c1a90d9ff78e4a0bd79f69499667a4e3David Chisnall    case BuiltinType::Bool:       return 'B';
416264fd7e86c1a90d9ff78e4a0bd79f69499667a4e3David Chisnall    case BuiltinType::Char_U:
416364fd7e86c1a90d9ff78e4a0bd79f69499667a4e3David Chisnall    case BuiltinType::UChar:      return 'C';
416464fd7e86c1a90d9ff78e4a0bd79f69499667a4e3David Chisnall    case BuiltinType::UShort:     return 'S';
416564fd7e86c1a90d9ff78e4a0bd79f69499667a4e3David Chisnall    case BuiltinType::UInt:       return 'I';
416664fd7e86c1a90d9ff78e4a0bd79f69499667a4e3David Chisnall    case BuiltinType::ULong:
41674ba2a17694148e16eaa8d3917f657ffcd3667be4Jay Foad        return C->getIntWidth(T) == 32 ? 'L' : 'Q';
416864fd7e86c1a90d9ff78e4a0bd79f69499667a4e3David Chisnall    case BuiltinType::UInt128:    return 'T';
416964fd7e86c1a90d9ff78e4a0bd79f69499667a4e3David Chisnall    case BuiltinType::ULongLong:  return 'Q';
417064fd7e86c1a90d9ff78e4a0bd79f69499667a4e3David Chisnall    case BuiltinType::Char_S:
417164fd7e86c1a90d9ff78e4a0bd79f69499667a4e3David Chisnall    case BuiltinType::SChar:      return 'c';
417264fd7e86c1a90d9ff78e4a0bd79f69499667a4e3David Chisnall    case BuiltinType::Short:      return 's';
41733f59c975aa5d047f7edd1b900b5e885c38af0ef7Chris Lattner    case BuiltinType::WChar_S:
41743f59c975aa5d047f7edd1b900b5e885c38af0ef7Chris Lattner    case BuiltinType::WChar_U:
417564fd7e86c1a90d9ff78e4a0bd79f69499667a4e3David Chisnall    case BuiltinType::Int:        return 'i';
417664fd7e86c1a90d9ff78e4a0bd79f69499667a4e3David Chisnall    case BuiltinType::Long:
41774ba2a17694148e16eaa8d3917f657ffcd3667be4Jay Foad      return C->getIntWidth(T) == 32 ? 'l' : 'q';
417864fd7e86c1a90d9ff78e4a0bd79f69499667a4e3David Chisnall    case BuiltinType::LongLong:   return 'q';
417964fd7e86c1a90d9ff78e4a0bd79f69499667a4e3David Chisnall    case BuiltinType::Int128:     return 't';
418064fd7e86c1a90d9ff78e4a0bd79f69499667a4e3David Chisnall    case BuiltinType::Float:      return 'f';
418164fd7e86c1a90d9ff78e4a0bd79f69499667a4e3David Chisnall    case BuiltinType::Double:     return 'd';
41823a0be84b2aed8563150cdbd976a98838afa261ebDaniel Dunbar    case BuiltinType::LongDouble: return 'D';
418364fd7e86c1a90d9ff78e4a0bd79f69499667a4e3David Chisnall    }
418464fd7e86c1a90d9ff78e4a0bd79f69499667a4e3David Chisnall}
418564fd7e86c1a90d9ff78e4a0bd79f69499667a4e3David Chisnall
41864ba2a17694148e16eaa8d3917f657ffcd3667be4Jay Foadstatic void EncodeBitField(const ASTContext *Ctx, std::string& S,
418764fd7e86c1a90d9ff78e4a0bd79f69499667a4e3David Chisnall                           QualType T, const FieldDecl *FD) {
41888b4bf90eb6d3d08cf3bfb86705f0fdb20b9c5875Fariborz Jahanian  const Expr *E = FD->getBitWidth();
41898b4bf90eb6d3d08cf3bfb86705f0fdb20b9c5875Fariborz Jahanian  assert(E && "bitfield width not there - getObjCEncodingForTypeImpl");
41908b4bf90eb6d3d08cf3bfb86705f0fdb20b9c5875Fariborz Jahanian  S += 'b';
419164fd7e86c1a90d9ff78e4a0bd79f69499667a4e3David Chisnall  // The NeXT runtime encodes bit fields as b followed by the number of bits.
419264fd7e86c1a90d9ff78e4a0bd79f69499667a4e3David Chisnall  // The GNU runtime requires more information; bitfields are encoded as b,
419364fd7e86c1a90d9ff78e4a0bd79f69499667a4e3David Chisnall  // then the offset (in bits) of the first element, then the type of the
419464fd7e86c1a90d9ff78e4a0bd79f69499667a4e3David Chisnall  // bitfield, then the size in bits.  For example, in this structure:
419564fd7e86c1a90d9ff78e4a0bd79f69499667a4e3David Chisnall  //
419664fd7e86c1a90d9ff78e4a0bd79f69499667a4e3David Chisnall  // struct
419764fd7e86c1a90d9ff78e4a0bd79f69499667a4e3David Chisnall  // {
419864fd7e86c1a90d9ff78e4a0bd79f69499667a4e3David Chisnall  //    int integer;
419964fd7e86c1a90d9ff78e4a0bd79f69499667a4e3David Chisnall  //    int flags:2;
420064fd7e86c1a90d9ff78e4a0bd79f69499667a4e3David Chisnall  // };
420164fd7e86c1a90d9ff78e4a0bd79f69499667a4e3David Chisnall  // On a 32-bit system, the encoding for flags would be b2 for the NeXT
420264fd7e86c1a90d9ff78e4a0bd79f69499667a4e3David Chisnall  // runtime, but b32i2 for the GNU runtime.  The reason for this extra
420364fd7e86c1a90d9ff78e4a0bd79f69499667a4e3David Chisnall  // information is not especially sensible, but we're stuck with it for
420464fd7e86c1a90d9ff78e4a0bd79f69499667a4e3David Chisnall  // compatibility with GCC, although providing it breaks anything that
420564fd7e86c1a90d9ff78e4a0bd79f69499667a4e3David Chisnall  // actually uses runtime introspection and wants to work on both runtimes...
420664fd7e86c1a90d9ff78e4a0bd79f69499667a4e3David Chisnall  if (!Ctx->getLangOptions().NeXTRuntime) {
420764fd7e86c1a90d9ff78e4a0bd79f69499667a4e3David Chisnall    const RecordDecl *RD = FD->getParent();
420864fd7e86c1a90d9ff78e4a0bd79f69499667a4e3David Chisnall    const ASTRecordLayout &RL = Ctx->getASTRecordLayout(RD);
420982905749d5c8d8b4edec11de754a73349cb96603Eli Friedman    S += llvm::utostr(RL.getFieldOffset(FD->getFieldIndex()));
4210c7ff82c2040f45eaad2eddea0e4461dddc972cd1David Chisnall    if (T->isEnumeralType())
4211c7ff82c2040f45eaad2eddea0e4461dddc972cd1David Chisnall      S += 'i';
4212c7ff82c2040f45eaad2eddea0e4461dddc972cd1David Chisnall    else
42134ba2a17694148e16eaa8d3917f657ffcd3667be4Jay Foad      S += ObjCEncodingForPrimitiveKind(Ctx, T);
421464fd7e86c1a90d9ff78e4a0bd79f69499667a4e3David Chisnall  }
421564fd7e86c1a90d9ff78e4a0bd79f69499667a4e3David Chisnall  unsigned N = E->EvaluateAsInt(*Ctx).getZExtValue();
42168b4bf90eb6d3d08cf3bfb86705f0fdb20b9c5875Fariborz Jahanian  S += llvm::utostr(N);
42178b4bf90eb6d3d08cf3bfb86705f0fdb20b9c5875Fariborz Jahanian}
42188b4bf90eb6d3d08cf3bfb86705f0fdb20b9c5875Fariborz Jahanian
421901eb9b9683535d8a65c704ad2c545903409e2d36Daniel Dunbar// FIXME: Use SmallString for accumulating string.
422082a6cfbc421cc99c5b7313271f399f7ef95056ecDaniel Dunbarvoid ASTContext::getObjCEncodingForTypeImpl(QualType T, std::string& S,
422182a6cfbc421cc99c5b7313271f399f7ef95056ecDaniel Dunbar                                            bool ExpandPointedToStructures,
422282a6cfbc421cc99c5b7313271f399f7ef95056ecDaniel Dunbar                                            bool ExpandStructures,
4223153bfe5795e2c1a5a738e73d3784964e082237fcDaniel Dunbar                                            const FieldDecl *FD,
4224090b3f71702c5626d8520f9608d77c6f26dcfa15Fariborz Jahanian                                            bool OutermostType,
42252636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis                                            bool EncodingProperty,
42262636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis                                            bool StructField) const {
422764fd7e86c1a90d9ff78e4a0bd79f69499667a4e3David Chisnall  if (T->getAs<BuiltinType>()) {
4228ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner    if (FD && FD->isBitField())
422964fd7e86c1a90d9ff78e4a0bd79f69499667a4e3David Chisnall      return EncodeBitField(this, S, T, FD);
423064fd7e86c1a90d9ff78e4a0bd79f69499667a4e3David Chisnall    S += ObjCEncodingForPrimitiveKind(this, T);
4231ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner    return;
4232ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner  }
42331eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
4234183700f494ec9b6701b6efe82bcb25f4c79ba561John McCall  if (const ComplexType *CT = T->getAs<ComplexType>()) {
4235c612f7bc9a6379cd7e7c2dd306d05938e890051bAnders Carlsson    S += 'j';
42361eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    getObjCEncodingForTypeImpl(CT->getElementType(), S, false, false, 0, false,
4237c612f7bc9a6379cd7e7c2dd306d05938e890051bAnders Carlsson                               false);
4238ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner    return;
4239ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner  }
424060bce3ef20a4c9684e3825cdd739fefb9810327dFariborz Jahanian
4241aa1d76163e4b0b1cc54e222be67379f8c02e8ffaFariborz Jahanian  // encoding for pointer or r3eference types.
4242aa1d76163e4b0b1cc54e222be67379f8c02e8ffaFariborz Jahanian  QualType PointeeTy;
42436217b80b7a1379b74cced1c076338262c3c980b3Ted Kremenek  if (const PointerType *PT = T->getAs<PointerType>()) {
42448d2c0a9814e56c2b22e22d1045181c735aef62fdFariborz Jahanian    if (PT->isObjCSelType()) {
42458d2c0a9814e56c2b22e22d1045181c735aef62fdFariborz Jahanian      S += ':';
42468d2c0a9814e56c2b22e22d1045181c735aef62fdFariborz Jahanian      return;
42478d2c0a9814e56c2b22e22d1045181c735aef62fdFariborz Jahanian    }
4248aa1d76163e4b0b1cc54e222be67379f8c02e8ffaFariborz Jahanian    PointeeTy = PT->getPointeeType();
4249aa1d76163e4b0b1cc54e222be67379f8c02e8ffaFariborz Jahanian  }
4250aa1d76163e4b0b1cc54e222be67379f8c02e8ffaFariborz Jahanian  else if (const ReferenceType *RT = T->getAs<ReferenceType>())
4251aa1d76163e4b0b1cc54e222be67379f8c02e8ffaFariborz Jahanian    PointeeTy = RT->getPointeeType();
4252aa1d76163e4b0b1cc54e222be67379f8c02e8ffaFariborz Jahanian  if (!PointeeTy.isNull()) {
4253a1c033e9514865f3a7b0d8b3b20e6de926cfec6cFariborz Jahanian    bool isReadOnly = false;
4254a1c033e9514865f3a7b0d8b3b20e6de926cfec6cFariborz Jahanian    // For historical/compatibility reasons, the read-only qualifier of the
4255a1c033e9514865f3a7b0d8b3b20e6de926cfec6cFariborz Jahanian    // pointee gets emitted _before_ the '^'.  The read-only qualifier of
4256a1c033e9514865f3a7b0d8b3b20e6de926cfec6cFariborz Jahanian    // the pointer itself gets ignored, _unless_ we are looking at a typedef!
42571eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    // Also, do not emit the 'r' for anything but the outermost type!
42588e1fab243ab8023b7ee3899745386b3b3a4258f8Mike Stump    if (isa<TypedefType>(T.getTypePtr())) {
4259a1c033e9514865f3a7b0d8b3b20e6de926cfec6cFariborz Jahanian      if (OutermostType && T.isConstQualified()) {
4260a1c033e9514865f3a7b0d8b3b20e6de926cfec6cFariborz Jahanian        isReadOnly = true;
4261a1c033e9514865f3a7b0d8b3b20e6de926cfec6cFariborz Jahanian        S += 'r';
4262a1c033e9514865f3a7b0d8b3b20e6de926cfec6cFariborz Jahanian      }
42639fdbab3cbc2fc04bcaf5768023d83707f3151144Mike Stump    } else if (OutermostType) {
4264a1c033e9514865f3a7b0d8b3b20e6de926cfec6cFariborz Jahanian      QualType P = PointeeTy;
42656217b80b7a1379b74cced1c076338262c3c980b3Ted Kremenek      while (P->getAs<PointerType>())
42666217b80b7a1379b74cced1c076338262c3c980b3Ted Kremenek        P = P->getAs<PointerType>()->getPointeeType();
4267a1c033e9514865f3a7b0d8b3b20e6de926cfec6cFariborz Jahanian      if (P.isConstQualified()) {
4268a1c033e9514865f3a7b0d8b3b20e6de926cfec6cFariborz Jahanian        isReadOnly = true;
4269a1c033e9514865f3a7b0d8b3b20e6de926cfec6cFariborz Jahanian        S += 'r';
4270a1c033e9514865f3a7b0d8b3b20e6de926cfec6cFariborz Jahanian      }
4271a1c033e9514865f3a7b0d8b3b20e6de926cfec6cFariborz Jahanian    }
4272a1c033e9514865f3a7b0d8b3b20e6de926cfec6cFariborz Jahanian    if (isReadOnly) {
4273a1c033e9514865f3a7b0d8b3b20e6de926cfec6cFariborz Jahanian      // Another legacy compatibility encoding. Some ObjC qualifier and type
4274a1c033e9514865f3a7b0d8b3b20e6de926cfec6cFariborz Jahanian      // combinations need to be rearranged.
4275a1c033e9514865f3a7b0d8b3b20e6de926cfec6cFariborz Jahanian      // Rewrite "in const" from "nr" to "rn"
42765f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner      if (StringRef(S).endswith("nr"))
42770237941e0beb0c929934b66ad29443b484d987feBenjamin Kramer        S.replace(S.end()-2, S.end(), "rn");
4278a1c033e9514865f3a7b0d8b3b20e6de926cfec6cFariborz Jahanian    }
42791eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
428085f9bceab1542aafff012d4d28e998f4ba16e362Anders Carlsson    if (PointeeTy->isCharType()) {
428185f9bceab1542aafff012d4d28e998f4ba16e362Anders Carlsson      // char pointer types should be encoded as '*' unless it is a
428285f9bceab1542aafff012d4d28e998f4ba16e362Anders Carlsson      // type that has been typedef'd to 'BOOL'.
4283e8c49533521c40643653f943d47229e62d277f88Anders Carlsson      if (!isTypeTypedefedAsBOOL(PointeeTy)) {
428485f9bceab1542aafff012d4d28e998f4ba16e362Anders Carlsson        S += '*';
428585f9bceab1542aafff012d4d28e998f4ba16e362Anders Carlsson        return;
428685f9bceab1542aafff012d4d28e998f4ba16e362Anders Carlsson      }
42876217b80b7a1379b74cced1c076338262c3c980b3Ted Kremenek    } else if (const RecordType *RTy = PointeeTy->getAs<RecordType>()) {
42889533a7fdb8397421f3be52e879442460a87389f6Steve Naroff      // GCC binary compat: Need to convert "struct objc_class *" to "#".
42899533a7fdb8397421f3be52e879442460a87389f6Steve Naroff      if (RTy->getDecl()->getIdentifier() == &Idents.get("objc_class")) {
42909533a7fdb8397421f3be52e879442460a87389f6Steve Naroff        S += '#';
42919533a7fdb8397421f3be52e879442460a87389f6Steve Naroff        return;
42929533a7fdb8397421f3be52e879442460a87389f6Steve Naroff      }
42939533a7fdb8397421f3be52e879442460a87389f6Steve Naroff      // GCC binary compat: Need to convert "struct objc_object *" to "@".
42949533a7fdb8397421f3be52e879442460a87389f6Steve Naroff      if (RTy->getDecl()->getIdentifier() == &Idents.get("objc_object")) {
42959533a7fdb8397421f3be52e879442460a87389f6Steve Naroff        S += '@';
42969533a7fdb8397421f3be52e879442460a87389f6Steve Naroff        return;
42979533a7fdb8397421f3be52e879442460a87389f6Steve Naroff      }
42989533a7fdb8397421f3be52e879442460a87389f6Steve Naroff      // fall through...
429985f9bceab1542aafff012d4d28e998f4ba16e362Anders Carlsson    }
430085f9bceab1542aafff012d4d28e998f4ba16e362Anders Carlsson    S += '^';
4301a1c033e9514865f3a7b0d8b3b20e6de926cfec6cFariborz Jahanian    getLegacyIntegralTypeEncoding(PointeeTy);
4302a1c033e9514865f3a7b0d8b3b20e6de926cfec6cFariborz Jahanian
43031eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    getObjCEncodingForTypeImpl(PointeeTy, S, false, ExpandPointedToStructures,
430443822eaeddeaa419b90f23c68af6b23c46788a58Fariborz Jahanian                               NULL);
4305ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner    return;
4306ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner  }
4307aa1d76163e4b0b1cc54e222be67379f8c02e8ffaFariborz Jahanian
4308ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner  if (const ArrayType *AT =
4309ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner      // Ignore type qualifiers etc.
4310ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner        dyn_cast<ArrayType>(T->getCanonicalTypeInternal())) {
43112636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis    if (isa<IncompleteArrayType>(AT) && !StructField) {
4312559a83330416affb0e341a2c53800cbf924a5178Anders Carlsson      // Incomplete arrays are encoded as a pointer to the array element.
4313559a83330416affb0e341a2c53800cbf924a5178Anders Carlsson      S += '^';
4314559a83330416affb0e341a2c53800cbf924a5178Anders Carlsson
43151eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      getObjCEncodingForTypeImpl(AT->getElementType(), S,
4316559a83330416affb0e341a2c53800cbf924a5178Anders Carlsson                                 false, ExpandStructures, FD);
4317559a83330416affb0e341a2c53800cbf924a5178Anders Carlsson    } else {
4318559a83330416affb0e341a2c53800cbf924a5178Anders Carlsson      S += '[';
43191eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
43202636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis      if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(AT)) {
43212636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis        if (getTypeSize(CAT->getElementType()) == 0)
43222636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis          S += '0';
43232636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis        else
43242636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis          S += llvm::utostr(CAT->getSize().getZExtValue());
43252636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis      } else {
4326559a83330416affb0e341a2c53800cbf924a5178Anders Carlsson        //Variable length arrays are encoded as a regular array with 0 elements.
43272636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis        assert((isa<VariableArrayType>(AT) || isa<IncompleteArrayType>(AT)) &&
43282636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis               "Unknown array type!");
4329559a83330416affb0e341a2c53800cbf924a5178Anders Carlsson        S += '0';
4330559a83330416affb0e341a2c53800cbf924a5178Anders Carlsson      }
43311eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
43321eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      getObjCEncodingForTypeImpl(AT->getElementType(), S,
4333559a83330416affb0e341a2c53800cbf924a5178Anders Carlsson                                 false, ExpandStructures, FD);
4334559a83330416affb0e341a2c53800cbf924a5178Anders Carlsson      S += ']';
4335559a83330416affb0e341a2c53800cbf924a5178Anders Carlsson    }
4336ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner    return;
4337ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner  }
43381eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
4339183700f494ec9b6701b6efe82bcb25f4c79ba561John McCall  if (T->getAs<FunctionType>()) {
4340c0a87b7db06643178ad2cbce0767548c139ea387Anders Carlsson    S += '?';
4341ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner    return;
4342ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner  }
43431eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
43446217b80b7a1379b74cced1c076338262c3c980b3Ted Kremenek  if (const RecordType *RTy = T->getAs<RecordType>()) {
434582a6cfbc421cc99c5b7313271f399f7ef95056ecDaniel Dunbar    RecordDecl *RDecl = RTy->getDecl();
4346d96b35bc6becf8db00d140c11e3d0e53f27567a1Daniel Dunbar    S += RDecl->isUnion() ? '(' : '{';
4347502a4a1ce4c34cf78c8182d9798da0a51d9b7302Daniel Dunbar    // Anonymous structures print as '?'
4348502a4a1ce4c34cf78c8182d9798da0a51d9b7302Daniel Dunbar    if (const IdentifierInfo *II = RDecl->getIdentifier()) {
4349502a4a1ce4c34cf78c8182d9798da0a51d9b7302Daniel Dunbar      S += II->getName();
43506fb94391dc7cb11fd4bbdb969bbab11b6b48c223Fariborz Jahanian      if (ClassTemplateSpecializationDecl *Spec
43516fb94391dc7cb11fd4bbdb969bbab11b6b48c223Fariborz Jahanian          = dyn_cast<ClassTemplateSpecializationDecl>(RDecl)) {
43526fb94391dc7cb11fd4bbdb969bbab11b6b48c223Fariborz Jahanian        const TemplateArgumentList &TemplateArgs = Spec->getTemplateArgs();
43536fb94391dc7cb11fd4bbdb969bbab11b6b48c223Fariborz Jahanian        std::string TemplateArgsStr
43546fb94391dc7cb11fd4bbdb969bbab11b6b48c223Fariborz Jahanian          = TemplateSpecializationType::PrintTemplateArgumentList(
4355910f8008fea79120489a53593fe971b0b8a4a740Douglas Gregor                                            TemplateArgs.data(),
4356910f8008fea79120489a53593fe971b0b8a4a740Douglas Gregor                                            TemplateArgs.size(),
43576fb94391dc7cb11fd4bbdb969bbab11b6b48c223Fariborz Jahanian                                            (*this).PrintingPolicy);
43586fb94391dc7cb11fd4bbdb969bbab11b6b48c223Fariborz Jahanian
43596fb94391dc7cb11fd4bbdb969bbab11b6b48c223Fariborz Jahanian        S += TemplateArgsStr;
43606fb94391dc7cb11fd4bbdb969bbab11b6b48c223Fariborz Jahanian      }
4361502a4a1ce4c34cf78c8182d9798da0a51d9b7302Daniel Dunbar    } else {
4362502a4a1ce4c34cf78c8182d9798da0a51d9b7302Daniel Dunbar      S += '?';
4363502a4a1ce4c34cf78c8182d9798da0a51d9b7302Daniel Dunbar    }
43640d504c1da852e58ff802545c823ecff3b6c654b8Daniel Dunbar    if (ExpandStructures) {
43657d6b46d9a9d75dea8ef9f6973dd50633c1f37963Fariborz Jahanian      S += '=';
43662636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis      if (!RDecl->isUnion()) {
43672636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis        getObjCEncodingForStructureImpl(RDecl, S, FD);
43682636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis      } else {
43692636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis        for (RecordDecl::field_iterator Field = RDecl->field_begin(),
43702636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis                                     FieldEnd = RDecl->field_end();
43712636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis             Field != FieldEnd; ++Field) {
43722636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis          if (FD) {
43732636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis            S += '"';
43742636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis            S += Field->getNameAsString();
43752636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis            S += '"';
43762636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis          }
43771eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
43782636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis          // Special case bit-fields.
43792636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis          if (Field->isBitField()) {
43802636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis            getObjCEncodingForTypeImpl(Field->getType(), S, false, true,
43812636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis                                       (*Field));
43822636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis          } else {
43832636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis            QualType qt = Field->getType();
43842636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis            getLegacyIntegralTypeEncoding(qt);
43852636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis            getObjCEncodingForTypeImpl(qt, S, false, true,
43862636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis                                       FD, /*OutermostType*/false,
43872636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis                                       /*EncodingProperty*/false,
43882636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis                                       /*StructField*/true);
43892636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis          }
4390d96b35bc6becf8db00d140c11e3d0e53f27567a1Daniel Dunbar        }
43917d6b46d9a9d75dea8ef9f6973dd50633c1f37963Fariborz Jahanian      }
43926de88a873a4cbe06d72602eef57d68006730a80bFariborz Jahanian    }
4393d96b35bc6becf8db00d140c11e3d0e53f27567a1Daniel Dunbar    S += RDecl->isUnion() ? ')' : '}';
4394ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner    return;
4395ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner  }
4396e6012c7ecb9d848f4091c8c48e7d9946cc36b23fFariborz Jahanian
4397ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner  if (T->isEnumeralType()) {
43988b4bf90eb6d3d08cf3bfb86705f0fdb20b9c5875Fariborz Jahanian    if (FD && FD->isBitField())
439964fd7e86c1a90d9ff78e4a0bd79f69499667a4e3David Chisnall      EncodeBitField(this, S, T, FD);
44008b4bf90eb6d3d08cf3bfb86705f0fdb20b9c5875Fariborz Jahanian    else
44018b4bf90eb6d3d08cf3bfb86705f0fdb20b9c5875Fariborz Jahanian      S += 'i';
4402ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner    return;
4403ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner  }
44041eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
4405ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner  if (T->isBlockPointerType()) {
440621a98b188857d690aa4510c52ac4317ffa0908a8Steve Naroff    S += "@?"; // Unlike a pointer-to-function, which is "^?".
4407ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner    return;
4408ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner  }
44091eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
4410c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall  // Ignore protocol qualifiers when mangling at this level.
4411c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall  if (const ObjCObjectType *OT = T->getAs<ObjCObjectType>())
4412c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall    T = OT->getBaseType();
4413c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall
44140953e767ff7817f97b3ab20896b229891eeff45bJohn McCall  if (const ObjCInterfaceType *OIT = T->getAs<ObjCInterfaceType>()) {
441543822eaeddeaa419b90f23c68af6b23c46788a58Fariborz Jahanian    // @encode(class_name)
44160953e767ff7817f97b3ab20896b229891eeff45bJohn McCall    ObjCInterfaceDecl *OI = OIT->getDecl();
441743822eaeddeaa419b90f23c68af6b23c46788a58Fariborz Jahanian    S += '{';
441843822eaeddeaa419b90f23c68af6b23c46788a58Fariborz Jahanian    const IdentifierInfo *II = OI->getIdentifier();
441943822eaeddeaa419b90f23c68af6b23c46788a58Fariborz Jahanian    S += II->getName();
442043822eaeddeaa419b90f23c68af6b23c46788a58Fariborz Jahanian    S += '=';
4421db8264e4c5ffd7af6fbad4ca4306bd382bb02691Jordy Rose    SmallVector<const ObjCIvarDecl*, 32> Ivars;
44222c18bb7c9fca66c30b6eabbdcbc6399d24a54fa9Fariborz Jahanian    DeepCollectObjCIvars(OI, true, Ivars);
44232c18bb7c9fca66c30b6eabbdcbc6399d24a54fa9Fariborz Jahanian    for (unsigned i = 0, e = Ivars.size(); i != e; ++i) {
4424db8264e4c5ffd7af6fbad4ca4306bd382bb02691Jordy Rose      const FieldDecl *Field = cast<FieldDecl>(Ivars[i]);
44252c18bb7c9fca66c30b6eabbdcbc6399d24a54fa9Fariborz Jahanian      if (Field->isBitField())
44262c18bb7c9fca66c30b6eabbdcbc6399d24a54fa9Fariborz Jahanian        getObjCEncodingForTypeImpl(Field->getType(), S, false, true, Field);
442743822eaeddeaa419b90f23c68af6b23c46788a58Fariborz Jahanian      else
44282c18bb7c9fca66c30b6eabbdcbc6399d24a54fa9Fariborz Jahanian        getObjCEncodingForTypeImpl(Field->getType(), S, false, true, FD);
442943822eaeddeaa419b90f23c68af6b23c46788a58Fariborz Jahanian    }
443043822eaeddeaa419b90f23c68af6b23c46788a58Fariborz Jahanian    S += '}';
4431ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner    return;
443243822eaeddeaa419b90f23c68af6b23c46788a58Fariborz Jahanian  }
44331eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
4434183700f494ec9b6701b6efe82bcb25f4c79ba561John McCall  if (const ObjCObjectPointerType *OPT = T->getAs<ObjCObjectPointerType>()) {
443514108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff    if (OPT->isObjCIdType()) {
443614108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff      S += '@';
443714108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff      return;
4438ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner    }
44391eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
444027d20a24a1b816cecbd142727d2c81af5a6a111bSteve Naroff    if (OPT->isObjCClassType() || OPT->isObjCQualifiedClassType()) {
444127d20a24a1b816cecbd142727d2c81af5a6a111bSteve Naroff      // FIXME: Consider if we need to output qualifiers for 'Class<p>'.
444227d20a24a1b816cecbd142727d2c81af5a6a111bSteve Naroff      // Since this is a binary compatibility issue, need to consult with runtime
444327d20a24a1b816cecbd142727d2c81af5a6a111bSteve Naroff      // folks. Fortunately, this is a *very* obsure construct.
444414108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff      S += '#';
444514108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff      return;
4446ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner    }
44471eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
4448ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner    if (OPT->isObjCQualifiedIdType()) {
44491eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      getObjCEncodingForTypeImpl(getObjCIdType(), S,
445014108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff                                 ExpandPointedToStructures,
445114108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff                                 ExpandStructures, FD);
445214108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff      if (FD || EncodingProperty) {
445314108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff        // Note that we do extended encoding of protocol qualifer list
445414108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff        // Only when doing ivar or property encoding.
445514108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff        S += '"';
445667ef8eaea8a0a2073147a8d863f0e3f30d525802Steve Naroff        for (ObjCObjectPointerType::qual_iterator I = OPT->qual_begin(),
445767ef8eaea8a0a2073147a8d863f0e3f30d525802Steve Naroff             E = OPT->qual_end(); I != E; ++I) {
445814108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff          S += '<';
445914108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff          S += (*I)->getNameAsString();
446014108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff          S += '>';
446114108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff        }
446214108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff        S += '"';
446314108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff      }
446414108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff      return;
4465ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner    }
44661eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
4467ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner    QualType PointeeTy = OPT->getPointeeType();
4468ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner    if (!EncodingProperty &&
4469ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner        isa<TypedefType>(PointeeTy.getTypePtr())) {
4470ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner      // Another historical/compatibility reason.
44711eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      // We encode the underlying type which comes out as
4472ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner      // {...};
4473ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner      S += '^';
44741eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      getObjCEncodingForTypeImpl(PointeeTy, S,
44751eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump                                 false, ExpandPointedToStructures,
4476ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner                                 NULL);
447714108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff      return;
447814108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff    }
4479ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner
4480ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner    S += '@';
448127d20a24a1b816cecbd142727d2c81af5a6a111bSteve Naroff    if (OPT->getInterfaceDecl() && (FD || EncodingProperty)) {
4482ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner      S += '"';
448327d20a24a1b816cecbd142727d2c81af5a6a111bSteve Naroff      S += OPT->getInterfaceDecl()->getIdentifier()->getName();
448467ef8eaea8a0a2073147a8d863f0e3f30d525802Steve Naroff      for (ObjCObjectPointerType::qual_iterator I = OPT->qual_begin(),
448567ef8eaea8a0a2073147a8d863f0e3f30d525802Steve Naroff           E = OPT->qual_end(); I != E; ++I) {
4486ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner        S += '<';
4487ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner        S += (*I)->getNameAsString();
4488ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner        S += '>';
44891eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      }
4490ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner      S += '"';
4491ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner    }
4492ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner    return;
4493ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner  }
44941eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
4495532ec7baf2d0791abc9551ef856a537711c5774aJohn McCall  // gcc just blithely ignores member pointers.
4496532ec7baf2d0791abc9551ef856a537711c5774aJohn McCall  // TODO: maybe there should be a mangling for these
4497532ec7baf2d0791abc9551ef856a537711c5774aJohn McCall  if (T->getAs<MemberPointerType>())
4498532ec7baf2d0791abc9551ef856a537711c5774aJohn McCall    return;
4499e6012c7ecb9d848f4091c8c48e7d9946cc36b23fFariborz Jahanian
4500e6012c7ecb9d848f4091c8c48e7d9946cc36b23fFariborz Jahanian  if (T->isVectorType()) {
4501e6012c7ecb9d848f4091c8c48e7d9946cc36b23fFariborz Jahanian    // This matches gcc's encoding, even though technically it is
4502e6012c7ecb9d848f4091c8c48e7d9946cc36b23fFariborz Jahanian    // insufficient.
4503e6012c7ecb9d848f4091c8c48e7d9946cc36b23fFariborz Jahanian    // FIXME. We should do a better job than gcc.
4504e6012c7ecb9d848f4091c8c48e7d9946cc36b23fFariborz Jahanian    return;
4505e6012c7ecb9d848f4091c8c48e7d9946cc36b23fFariborz Jahanian  }
4506e6012c7ecb9d848f4091c8c48e7d9946cc36b23fFariborz Jahanian
4507ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner  assert(0 && "@encode for type not implemented!");
450885f9bceab1542aafff012d4d28e998f4ba16e362Anders Carlsson}
450985f9bceab1542aafff012d4d28e998f4ba16e362Anders Carlsson
45102636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidisvoid ASTContext::getObjCEncodingForStructureImpl(RecordDecl *RDecl,
45112636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis                                                 std::string &S,
45122636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis                                                 const FieldDecl *FD,
45132636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis                                                 bool includeVBases) const {
45142636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis  assert(RDecl && "Expected non-null RecordDecl");
45152636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis  assert(!RDecl->isUnion() && "Should not be called for unions");
45162636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis  if (!RDecl->getDefinition())
45172636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis    return;
45182636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis
45192636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis  CXXRecordDecl *CXXRec = dyn_cast<CXXRecordDecl>(RDecl);
45202636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis  std::multimap<uint64_t, NamedDecl *> FieldOrBaseOffsets;
45212636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis  const ASTRecordLayout &layout = getASTRecordLayout(RDecl);
45222636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis
45232636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis  if (CXXRec) {
45242636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis    for (CXXRecordDecl::base_class_iterator
45252636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis           BI = CXXRec->bases_begin(),
45262636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis           BE = CXXRec->bases_end(); BI != BE; ++BI) {
45272636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis      if (!BI->isVirtual()) {
45282636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis        CXXRecordDecl *base = BI->getType()->getAsCXXRecordDecl();
4529829f20097e030229fd4c234b84948f53312eaf55Argyrios Kyrtzidis        if (base->isEmpty())
4530829f20097e030229fd4c234b84948f53312eaf55Argyrios Kyrtzidis          continue;
45312636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis        uint64_t offs = layout.getBaseClassOffsetInBits(base);
45322636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis        FieldOrBaseOffsets.insert(FieldOrBaseOffsets.upper_bound(offs),
45332636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis                                  std::make_pair(offs, base));
45342636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis      }
45352636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis    }
45362636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis  }
45372636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis
45382636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis  unsigned i = 0;
45392636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis  for (RecordDecl::field_iterator Field = RDecl->field_begin(),
45402636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis                               FieldEnd = RDecl->field_end();
45412636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis       Field != FieldEnd; ++Field, ++i) {
45422636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis    uint64_t offs = layout.getFieldOffset(i);
45432636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis    FieldOrBaseOffsets.insert(FieldOrBaseOffsets.upper_bound(offs),
45442636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis                              std::make_pair(offs, *Field));
45452636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis  }
45462636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis
45472636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis  if (CXXRec && includeVBases) {
45482636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis    for (CXXRecordDecl::base_class_iterator
45492636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis           BI = CXXRec->vbases_begin(),
45502636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis           BE = CXXRec->vbases_end(); BI != BE; ++BI) {
45512636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis      CXXRecordDecl *base = BI->getType()->getAsCXXRecordDecl();
4552829f20097e030229fd4c234b84948f53312eaf55Argyrios Kyrtzidis      if (base->isEmpty())
4553829f20097e030229fd4c234b84948f53312eaf55Argyrios Kyrtzidis        continue;
45542636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis      uint64_t offs = layout.getVBaseClassOffsetInBits(base);
45552636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis      FieldOrBaseOffsets.insert(FieldOrBaseOffsets.upper_bound(offs),
45562636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis                                std::make_pair(offs, base));
45572636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis    }
45582636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis  }
45592636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis
45602636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis  CharUnits size;
45612636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis  if (CXXRec) {
45622636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis    size = includeVBases ? layout.getSize() : layout.getNonVirtualSize();
45632636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis  } else {
45642636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis    size = layout.getSize();
45652636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis  }
45662636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis
45672636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis  uint64_t CurOffs = 0;
45682636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis  std::multimap<uint64_t, NamedDecl *>::iterator
45692636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis    CurLayObj = FieldOrBaseOffsets.begin();
45702636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis
45712636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis  if (CurLayObj != FieldOrBaseOffsets.end() && CurLayObj->first != 0) {
45722636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis    assert(CXXRec && CXXRec->isDynamicClass() &&
45732636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis           "Offset 0 was empty but no VTable ?");
45742636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis    if (FD) {
45752636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis      S += "\"_vptr$";
45762636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis      std::string recname = CXXRec->getNameAsString();
45772636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis      if (recname.empty()) recname = "?";
45782636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis      S += recname;
45792636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis      S += '"';
45802636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis    }
45812636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis    S += "^^?";
45822636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis    CurOffs += getTypeSize(VoidPtrTy);
45832636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis  }
45842636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis
45852636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis  if (!RDecl->hasFlexibleArrayMember()) {
45862636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis    // Mark the end of the structure.
45872636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis    uint64_t offs = toBits(size);
45882636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis    FieldOrBaseOffsets.insert(FieldOrBaseOffsets.upper_bound(offs),
45892636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis                              std::make_pair(offs, (NamedDecl*)0));
45902636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis  }
45912636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis
45922636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis  for (; CurLayObj != FieldOrBaseOffsets.end(); ++CurLayObj) {
45932636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis    assert(CurOffs <= CurLayObj->first);
45942636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis
45952636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis    if (CurOffs < CurLayObj->first) {
45962636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis      uint64_t padding = CurLayObj->first - CurOffs;
45972636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis      // FIXME: There doesn't seem to be a way to indicate in the encoding that
45982636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis      // packing/alignment of members is different that normal, in which case
45992636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis      // the encoding will be out-of-sync with the real layout.
46002636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis      // If the runtime switches to just consider the size of types without
46012636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis      // taking into account alignment, we could make padding explicit in the
46022636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis      // encoding (e.g. using arrays of chars). The encoding strings would be
46032636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis      // longer then though.
46042636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis      CurOffs += padding;
46052636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis    }
46062636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis
46072636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis    NamedDecl *dcl = CurLayObj->second;
46082636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis    if (dcl == 0)
46092636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis      break; // reached end of structure.
46102636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis
46112636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis    if (CXXRecordDecl *base = dyn_cast<CXXRecordDecl>(dcl)) {
46122636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis      // We expand the bases without their virtual bases since those are going
46132636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis      // in the initial structure. Note that this differs from gcc which
46142636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis      // expands virtual bases each time one is encountered in the hierarchy,
46152636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis      // making the encoding type bigger than it really is.
46162636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis      getObjCEncodingForStructureImpl(base, S, FD, /*includeVBases*/false);
4617829f20097e030229fd4c234b84948f53312eaf55Argyrios Kyrtzidis      assert(!base->isEmpty());
4618829f20097e030229fd4c234b84948f53312eaf55Argyrios Kyrtzidis      CurOffs += toBits(getASTRecordLayout(base).getNonVirtualSize());
46192636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis    } else {
46202636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis      FieldDecl *field = cast<FieldDecl>(dcl);
46212636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis      if (FD) {
46222636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis        S += '"';
46232636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis        S += field->getNameAsString();
46242636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis        S += '"';
46252636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis      }
46262636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis
46272636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis      if (field->isBitField()) {
46282636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis        EncodeBitField(this, S, field->getType(), field);
46292636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis        CurOffs += field->getBitWidth()->EvaluateAsInt(*this).getZExtValue();
46302636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis      } else {
46312636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis        QualType qt = field->getType();
46322636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis        getLegacyIntegralTypeEncoding(qt);
46332636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis        getObjCEncodingForTypeImpl(qt, S, false, true, FD,
46342636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis                                   /*OutermostType*/false,
46352636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis                                   /*EncodingProperty*/false,
46362636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis                                   /*StructField*/true);
46372636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis        CurOffs += getTypeSize(field->getType());
46382636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis      }
46392636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis    }
46402636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis  }
46412636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis}
46422636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis
46431eb4433ac451dc16f4133a88af2d002ac26c58efMike Stumpvoid ASTContext::getObjCEncodingForTypeQualifier(Decl::ObjCDeclQualifier QT,
4644ecb01e666665efabd2aa76a76f6080e2a78965faFariborz Jahanian                                                 std::string& S) const {
4645ecb01e666665efabd2aa76a76f6080e2a78965faFariborz Jahanian  if (QT & Decl::OBJC_TQ_In)
4646ecb01e666665efabd2aa76a76f6080e2a78965faFariborz Jahanian    S += 'n';
4647ecb01e666665efabd2aa76a76f6080e2a78965faFariborz Jahanian  if (QT & Decl::OBJC_TQ_Inout)
4648ecb01e666665efabd2aa76a76f6080e2a78965faFariborz Jahanian    S += 'N';
4649ecb01e666665efabd2aa76a76f6080e2a78965faFariborz Jahanian  if (QT & Decl::OBJC_TQ_Out)
4650ecb01e666665efabd2aa76a76f6080e2a78965faFariborz Jahanian    S += 'o';
4651ecb01e666665efabd2aa76a76f6080e2a78965faFariborz Jahanian  if (QT & Decl::OBJC_TQ_Bycopy)
4652ecb01e666665efabd2aa76a76f6080e2a78965faFariborz Jahanian    S += 'O';
4653ecb01e666665efabd2aa76a76f6080e2a78965faFariborz Jahanian  if (QT & Decl::OBJC_TQ_Byref)
4654ecb01e666665efabd2aa76a76f6080e2a78965faFariborz Jahanian    S += 'R';
4655ecb01e666665efabd2aa76a76f6080e2a78965faFariborz Jahanian  if (QT & Decl::OBJC_TQ_Oneway)
4656ecb01e666665efabd2aa76a76f6080e2a78965faFariborz Jahanian    S += 'V';
4657ecb01e666665efabd2aa76a76f6080e2a78965faFariborz Jahanian}
4658ecb01e666665efabd2aa76a76f6080e2a78965faFariborz Jahanian
4659ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattnervoid ASTContext::setBuiltinVaListType(QualType T) {
4660b2cf3573d7351094f6247fcca94703ce88eb9ee0Anders Carlsson  assert(BuiltinVaListType.isNull() && "__builtin_va_list type already set!");
46611eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
4662b2cf3573d7351094f6247fcca94703ce88eb9ee0Anders Carlsson  BuiltinVaListType = T;
4663b2cf3573d7351094f6247fcca94703ce88eb9ee0Anders Carlsson}
4664b2cf3573d7351094f6247fcca94703ce88eb9ee0Anders Carlsson
46654dfd02a17c6d604c72e6936527c5e1c56d3ecb7aDouglas GregorTypedefDecl *ASTContext::getObjCIdDecl() const {
46664dfd02a17c6d604c72e6936527c5e1c56d3ecb7aDouglas Gregor  if (!ObjCIdDecl) {
46674dfd02a17c6d604c72e6936527c5e1c56d3ecb7aDouglas Gregor    QualType T = getObjCObjectType(ObjCBuiltinIdTy, 0, 0);
46684dfd02a17c6d604c72e6936527c5e1c56d3ecb7aDouglas Gregor    T = getObjCObjectPointerType(T);
46694dfd02a17c6d604c72e6936527c5e1c56d3ecb7aDouglas Gregor    TypeSourceInfo *IdInfo = getTrivialTypeSourceInfo(T);
46704dfd02a17c6d604c72e6936527c5e1c56d3ecb7aDouglas Gregor    ObjCIdDecl = TypedefDecl::Create(const_cast<ASTContext &>(*this),
46714dfd02a17c6d604c72e6936527c5e1c56d3ecb7aDouglas Gregor                                     getTranslationUnitDecl(),
46724dfd02a17c6d604c72e6936527c5e1c56d3ecb7aDouglas Gregor                                     SourceLocation(), SourceLocation(),
46734dfd02a17c6d604c72e6936527c5e1c56d3ecb7aDouglas Gregor                                     &Idents.get("id"), IdInfo);
46744dfd02a17c6d604c72e6936527c5e1c56d3ecb7aDouglas Gregor  }
46754dfd02a17c6d604c72e6936527c5e1c56d3ecb7aDouglas Gregor
46764dfd02a17c6d604c72e6936527c5e1c56d3ecb7aDouglas Gregor  return ObjCIdDecl;
46777e219e47de26346885d667131977bd9ca2d7662aSteve Naroff}
46787e219e47de26346885d667131977bd9ca2d7662aSteve Naroff
46797a27ea52b7bd635c89bec5a9c521a3bf7d204238Douglas GregorTypedefDecl *ASTContext::getObjCSelDecl() const {
46807a27ea52b7bd635c89bec5a9c521a3bf7d204238Douglas Gregor  if (!ObjCSelDecl) {
46817a27ea52b7bd635c89bec5a9c521a3bf7d204238Douglas Gregor    QualType SelT = getPointerType(ObjCBuiltinSelTy);
46827a27ea52b7bd635c89bec5a9c521a3bf7d204238Douglas Gregor    TypeSourceInfo *SelInfo = getTrivialTypeSourceInfo(SelT);
46837a27ea52b7bd635c89bec5a9c521a3bf7d204238Douglas Gregor    ObjCSelDecl = TypedefDecl::Create(const_cast<ASTContext &>(*this),
46847a27ea52b7bd635c89bec5a9c521a3bf7d204238Douglas Gregor                                      getTranslationUnitDecl(),
46857a27ea52b7bd635c89bec5a9c521a3bf7d204238Douglas Gregor                                      SourceLocation(), SourceLocation(),
46867a27ea52b7bd635c89bec5a9c521a3bf7d204238Douglas Gregor                                      &Idents.get("SEL"), SelInfo);
46877a27ea52b7bd635c89bec5a9c521a3bf7d204238Douglas Gregor  }
46887a27ea52b7bd635c89bec5a9c521a3bf7d204238Douglas Gregor  return ObjCSelDecl;
4689b62f6813406a03bf8a371c4e46c9fad51d102121Fariborz Jahanian}
4690b62f6813406a03bf8a371c4e46c9fad51d102121Fariborz Jahanian
4691ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattnervoid ASTContext::setObjCProtoType(QualType QT) {
4692a526c5c67e5a0473c340903ee542ce570119665fTed Kremenek  ObjCProtoType = QT;
4693390d50a725497e99247dc104a7d2c2a255d3af14Fariborz Jahanian}
4694390d50a725497e99247dc104a7d2c2a255d3af14Fariborz Jahanian
469579d6726921897811232554ed94c5d77b5b7b3fc0Douglas GregorTypedefDecl *ASTContext::getObjCClassDecl() const {
469679d6726921897811232554ed94c5d77b5b7b3fc0Douglas Gregor  if (!ObjCClassDecl) {
469779d6726921897811232554ed94c5d77b5b7b3fc0Douglas Gregor    QualType T = getObjCObjectType(ObjCBuiltinClassTy, 0, 0);
469879d6726921897811232554ed94c5d77b5b7b3fc0Douglas Gregor    T = getObjCObjectPointerType(T);
469979d6726921897811232554ed94c5d77b5b7b3fc0Douglas Gregor    TypeSourceInfo *ClassInfo = getTrivialTypeSourceInfo(T);
470079d6726921897811232554ed94c5d77b5b7b3fc0Douglas Gregor    ObjCClassDecl = TypedefDecl::Create(const_cast<ASTContext &>(*this),
470179d6726921897811232554ed94c5d77b5b7b3fc0Douglas Gregor                                        getTranslationUnitDecl(),
470279d6726921897811232554ed94c5d77b5b7b3fc0Douglas Gregor                                        SourceLocation(), SourceLocation(),
470379d6726921897811232554ed94c5d77b5b7b3fc0Douglas Gregor                                        &Idents.get("Class"), ClassInfo);
470479d6726921897811232554ed94c5d77b5b7b3fc0Douglas Gregor  }
470579d6726921897811232554ed94c5d77b5b7b3fc0Douglas Gregor
470679d6726921897811232554ed94c5d77b5b7b3fc0Douglas Gregor  return ObjCClassDecl;
47078baaca50f07d0c10bba69c8d88c1b9078c92d06dAnders Carlsson}
47088baaca50f07d0c10bba69c8d88c1b9078c92d06dAnders Carlsson
4709a526c5c67e5a0473c340903ee542ce570119665fTed Kremenekvoid ASTContext::setObjCConstantStringInterface(ObjCInterfaceDecl *Decl) {
47101eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  assert(ObjCConstantStringType.isNull() &&
47112198891824c38d45b2279de5d5e3ef9394eb457cSteve Naroff         "'NSConstantString' type already set!");
47121eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
4713a526c5c67e5a0473c340903ee542ce570119665fTed Kremenek  ObjCConstantStringType = getObjCInterfaceType(Decl);
47142198891824c38d45b2279de5d5e3ef9394eb457cSteve Naroff}
47152198891824c38d45b2279de5d5e3ef9394eb457cSteve Naroff
47160bd6feb9e9d40fc889fd47e899985125a43dfed8John McCall/// \brief Retrieve the template name that corresponds to a non-empty
47170bd6feb9e9d40fc889fd47e899985125a43dfed8John McCall/// lookup.
47184ba2a17694148e16eaa8d3917f657ffcd3667be4Jay FoadTemplateName
47194ba2a17694148e16eaa8d3917f657ffcd3667be4Jay FoadASTContext::getOverloadedTemplateName(UnresolvedSetIterator Begin,
47204ba2a17694148e16eaa8d3917f657ffcd3667be4Jay Foad                                      UnresolvedSetIterator End) const {
47210bd6feb9e9d40fc889fd47e899985125a43dfed8John McCall  unsigned size = End - Begin;
47220bd6feb9e9d40fc889fd47e899985125a43dfed8John McCall  assert(size > 1 && "set is not overloaded!");
47237532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor
47240bd6feb9e9d40fc889fd47e899985125a43dfed8John McCall  void *memory = Allocate(sizeof(OverloadedTemplateStorage) +
47250bd6feb9e9d40fc889fd47e899985125a43dfed8John McCall                          size * sizeof(FunctionTemplateDecl*));
47260bd6feb9e9d40fc889fd47e899985125a43dfed8John McCall  OverloadedTemplateStorage *OT = new(memory) OverloadedTemplateStorage(size);
47270bd6feb9e9d40fc889fd47e899985125a43dfed8John McCall
47280bd6feb9e9d40fc889fd47e899985125a43dfed8John McCall  NamedDecl **Storage = OT->getStorage();
4729eec51cf1ba5f0e62c9cdb81b5c63babdd6e649abJohn McCall  for (UnresolvedSetIterator I = Begin; I != End; ++I) {
47300bd6feb9e9d40fc889fd47e899985125a43dfed8John McCall    NamedDecl *D = *I;
47310bd6feb9e9d40fc889fd47e899985125a43dfed8John McCall    assert(isa<FunctionTemplateDecl>(D) ||
47320bd6feb9e9d40fc889fd47e899985125a43dfed8John McCall           (isa<UsingShadowDecl>(D) &&
47330bd6feb9e9d40fc889fd47e899985125a43dfed8John McCall            isa<FunctionTemplateDecl>(D->getUnderlyingDecl())));
47340bd6feb9e9d40fc889fd47e899985125a43dfed8John McCall    *Storage++ = D;
47357532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor  }
47367532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor
47370bd6feb9e9d40fc889fd47e899985125a43dfed8John McCall  return TemplateName(OT);
47387532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor}
47397532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor
4740d99cbe66403ee39c2ee58024b9582b95649a4fc5Douglas Gregor/// \brief Retrieve the template name that represents a qualified
4741d99cbe66403ee39c2ee58024b9582b95649a4fc5Douglas Gregor/// template name such as \c std::vector.
47424ba2a17694148e16eaa8d3917f657ffcd3667be4Jay FoadTemplateName
47434ba2a17694148e16eaa8d3917f657ffcd3667be4Jay FoadASTContext::getQualifiedTemplateName(NestedNameSpecifier *NNS,
47444ba2a17694148e16eaa8d3917f657ffcd3667be4Jay Foad                                     bool TemplateKeyword,
47454ba2a17694148e16eaa8d3917f657ffcd3667be4Jay Foad                                     TemplateDecl *Template) const {
47460f0ea2a96534c615ff5fdd81363989b23cf2164aDouglas Gregor  assert(NNS && "Missing nested-name-specifier in qualified template name");
47470f0ea2a96534c615ff5fdd81363989b23cf2164aDouglas Gregor
4748789b1f640205e81b5af250693246120f1ce9d147Douglas Gregor  // FIXME: Canonicalization?
4749d99cbe66403ee39c2ee58024b9582b95649a4fc5Douglas Gregor  llvm::FoldingSetNodeID ID;
4750d99cbe66403ee39c2ee58024b9582b95649a4fc5Douglas Gregor  QualifiedTemplateName::Profile(ID, NNS, TemplateKeyword, Template);
47511eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
4752d99cbe66403ee39c2ee58024b9582b95649a4fc5Douglas Gregor  void *InsertPos = 0;
4753d99cbe66403ee39c2ee58024b9582b95649a4fc5Douglas Gregor  QualifiedTemplateName *QTN =
47540bd6feb9e9d40fc889fd47e899985125a43dfed8John McCall    QualifiedTemplateNames.FindNodeOrInsertPos(ID, InsertPos);
4755d99cbe66403ee39c2ee58024b9582b95649a4fc5Douglas Gregor  if (!QTN) {
4756d99cbe66403ee39c2ee58024b9582b95649a4fc5Douglas Gregor    QTN = new (*this,4) QualifiedTemplateName(NNS, TemplateKeyword, Template);
4757d99cbe66403ee39c2ee58024b9582b95649a4fc5Douglas Gregor    QualifiedTemplateNames.InsertNode(QTN, InsertPos);
4758d99cbe66403ee39c2ee58024b9582b95649a4fc5Douglas Gregor  }
47591eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
4760d99cbe66403ee39c2ee58024b9582b95649a4fc5Douglas Gregor  return TemplateName(QTN);
4761d99cbe66403ee39c2ee58024b9582b95649a4fc5Douglas Gregor}
4762d99cbe66403ee39c2ee58024b9582b95649a4fc5Douglas Gregor
47637532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor/// \brief Retrieve the template name that represents a dependent
47647532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor/// template name such as \c MetaFun::template apply.
47654ba2a17694148e16eaa8d3917f657ffcd3667be4Jay FoadTemplateName
47664ba2a17694148e16eaa8d3917f657ffcd3667be4Jay FoadASTContext::getDependentTemplateName(NestedNameSpecifier *NNS,
47674ba2a17694148e16eaa8d3917f657ffcd3667be4Jay Foad                                     const IdentifierInfo *Name) const {
47681eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  assert((!NNS || NNS->isDependent()) &&
47693b6afbb99a1c44b4076f8e15fb7311405941b306Douglas Gregor         "Nested name specifier must be dependent");
47707532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor
47717532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor  llvm::FoldingSetNodeID ID;
47727532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor  DependentTemplateName::Profile(ID, NNS, Name);
47737532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor
47747532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor  void *InsertPos = 0;
47757532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor  DependentTemplateName *QTN =
47767532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor    DependentTemplateNames.FindNodeOrInsertPos(ID, InsertPos);
47777532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor
47787532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor  if (QTN)
47797532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor    return TemplateName(QTN);
47807532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor
47817532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor  NestedNameSpecifier *CanonNNS = getCanonicalNestedNameSpecifier(NNS);
47827532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor  if (CanonNNS == NNS) {
47837532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor    QTN = new (*this,4) DependentTemplateName(NNS, Name);
47847532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor  } else {
47857532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor    TemplateName Canon = getDependentTemplateName(CanonNNS, Name);
47867532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor    QTN = new (*this,4) DependentTemplateName(NNS, Name, Canon);
4787789b1f640205e81b5af250693246120f1ce9d147Douglas Gregor    DependentTemplateName *CheckQTN =
4788789b1f640205e81b5af250693246120f1ce9d147Douglas Gregor      DependentTemplateNames.FindNodeOrInsertPos(ID, InsertPos);
4789789b1f640205e81b5af250693246120f1ce9d147Douglas Gregor    assert(!CheckQTN && "Dependent type name canonicalization broken");
4790789b1f640205e81b5af250693246120f1ce9d147Douglas Gregor    (void)CheckQTN;
47917532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor  }
47927532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor
47937532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor  DependentTemplateNames.InsertNode(QTN, InsertPos);
47947532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor  return TemplateName(QTN);
47957532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor}
47967532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor
4797ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor/// \brief Retrieve the template name that represents a dependent
4798ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor/// template name such as \c MetaFun::template operator+.
4799ca1bdd7c269a2390d43c040a60511edd017ee130Douglas GregorTemplateName
4800ca1bdd7c269a2390d43c040a60511edd017ee130Douglas GregorASTContext::getDependentTemplateName(NestedNameSpecifier *NNS,
48014ba2a17694148e16eaa8d3917f657ffcd3667be4Jay Foad                                     OverloadedOperatorKind Operator) const {
4802ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor  assert((!NNS || NNS->isDependent()) &&
4803ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor         "Nested name specifier must be dependent");
4804ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor
4805ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor  llvm::FoldingSetNodeID ID;
4806ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor  DependentTemplateName::Profile(ID, NNS, Operator);
4807ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor
4808ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor  void *InsertPos = 0;
4809789b1f640205e81b5af250693246120f1ce9d147Douglas Gregor  DependentTemplateName *QTN
4810789b1f640205e81b5af250693246120f1ce9d147Douglas Gregor    = DependentTemplateNames.FindNodeOrInsertPos(ID, InsertPos);
4811ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor
4812ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor  if (QTN)
4813ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor    return TemplateName(QTN);
4814ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor
4815ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor  NestedNameSpecifier *CanonNNS = getCanonicalNestedNameSpecifier(NNS);
4816ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor  if (CanonNNS == NNS) {
4817ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor    QTN = new (*this,4) DependentTemplateName(NNS, Operator);
4818ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor  } else {
4819ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor    TemplateName Canon = getDependentTemplateName(CanonNNS, Operator);
4820ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor    QTN = new (*this,4) DependentTemplateName(NNS, Operator, Canon);
4821789b1f640205e81b5af250693246120f1ce9d147Douglas Gregor
4822789b1f640205e81b5af250693246120f1ce9d147Douglas Gregor    DependentTemplateName *CheckQTN
4823789b1f640205e81b5af250693246120f1ce9d147Douglas Gregor      = DependentTemplateNames.FindNodeOrInsertPos(ID, InsertPos);
4824789b1f640205e81b5af250693246120f1ce9d147Douglas Gregor    assert(!CheckQTN && "Dependent template name canonicalization broken");
4825789b1f640205e81b5af250693246120f1ce9d147Douglas Gregor    (void)CheckQTN;
4826ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor  }
4827ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor
4828ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor  DependentTemplateNames.InsertNode(QTN, InsertPos);
4829ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor  return TemplateName(QTN);
4830ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor}
4831ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor
48321aee05d08b2184acadeb36de300e216390780d6cDouglas GregorTemplateName
4833146060435c3efce95c95a092c7a1eb651cfb9ae0John McCallASTContext::getSubstTemplateTemplateParm(TemplateTemplateParmDecl *param,
4834146060435c3efce95c95a092c7a1eb651cfb9ae0John McCall                                         TemplateName replacement) const {
4835146060435c3efce95c95a092c7a1eb651cfb9ae0John McCall  llvm::FoldingSetNodeID ID;
4836146060435c3efce95c95a092c7a1eb651cfb9ae0John McCall  SubstTemplateTemplateParmStorage::Profile(ID, param, replacement);
4837146060435c3efce95c95a092c7a1eb651cfb9ae0John McCall
4838146060435c3efce95c95a092c7a1eb651cfb9ae0John McCall  void *insertPos = 0;
4839146060435c3efce95c95a092c7a1eb651cfb9ae0John McCall  SubstTemplateTemplateParmStorage *subst
4840146060435c3efce95c95a092c7a1eb651cfb9ae0John McCall    = SubstTemplateTemplateParms.FindNodeOrInsertPos(ID, insertPos);
4841146060435c3efce95c95a092c7a1eb651cfb9ae0John McCall
4842146060435c3efce95c95a092c7a1eb651cfb9ae0John McCall  if (!subst) {
4843146060435c3efce95c95a092c7a1eb651cfb9ae0John McCall    subst = new (*this) SubstTemplateTemplateParmStorage(param, replacement);
4844146060435c3efce95c95a092c7a1eb651cfb9ae0John McCall    SubstTemplateTemplateParms.InsertNode(subst, insertPos);
4845146060435c3efce95c95a092c7a1eb651cfb9ae0John McCall  }
4846146060435c3efce95c95a092c7a1eb651cfb9ae0John McCall
4847146060435c3efce95c95a092c7a1eb651cfb9ae0John McCall  return TemplateName(subst);
4848146060435c3efce95c95a092c7a1eb651cfb9ae0John McCall}
4849146060435c3efce95c95a092c7a1eb651cfb9ae0John McCall
4850146060435c3efce95c95a092c7a1eb651cfb9ae0John McCallTemplateName
48511aee05d08b2184acadeb36de300e216390780d6cDouglas GregorASTContext::getSubstTemplateTemplateParmPack(TemplateTemplateParmDecl *Param,
48521aee05d08b2184acadeb36de300e216390780d6cDouglas Gregor                                       const TemplateArgument &ArgPack) const {
48531aee05d08b2184acadeb36de300e216390780d6cDouglas Gregor  ASTContext &Self = const_cast<ASTContext &>(*this);
48541aee05d08b2184acadeb36de300e216390780d6cDouglas Gregor  llvm::FoldingSetNodeID ID;
48551aee05d08b2184acadeb36de300e216390780d6cDouglas Gregor  SubstTemplateTemplateParmPackStorage::Profile(ID, Self, Param, ArgPack);
48561aee05d08b2184acadeb36de300e216390780d6cDouglas Gregor
48571aee05d08b2184acadeb36de300e216390780d6cDouglas Gregor  void *InsertPos = 0;
48581aee05d08b2184acadeb36de300e216390780d6cDouglas Gregor  SubstTemplateTemplateParmPackStorage *Subst
48591aee05d08b2184acadeb36de300e216390780d6cDouglas Gregor    = SubstTemplateTemplateParmPacks.FindNodeOrInsertPos(ID, InsertPos);
48601aee05d08b2184acadeb36de300e216390780d6cDouglas Gregor
48611aee05d08b2184acadeb36de300e216390780d6cDouglas Gregor  if (!Subst) {
4862146060435c3efce95c95a092c7a1eb651cfb9ae0John McCall    Subst = new (*this) SubstTemplateTemplateParmPackStorage(Param,
48631aee05d08b2184acadeb36de300e216390780d6cDouglas Gregor                                                           ArgPack.pack_size(),
48641aee05d08b2184acadeb36de300e216390780d6cDouglas Gregor                                                         ArgPack.pack_begin());
48651aee05d08b2184acadeb36de300e216390780d6cDouglas Gregor    SubstTemplateTemplateParmPacks.InsertNode(Subst, InsertPos);
48661aee05d08b2184acadeb36de300e216390780d6cDouglas Gregor  }
48671aee05d08b2184acadeb36de300e216390780d6cDouglas Gregor
48681aee05d08b2184acadeb36de300e216390780d6cDouglas Gregor  return TemplateName(Subst);
48691aee05d08b2184acadeb36de300e216390780d6cDouglas Gregor}
48701aee05d08b2184acadeb36de300e216390780d6cDouglas Gregor
4871b4e66d5259f90e9aae4d40fc5de801e046c7df94Douglas Gregor/// getFromTargetType - Given one of the integer types provided by
4872d934112e6170b0fd940d8e40db6936cea2cdcf62Douglas Gregor/// TargetInfo, produce the corresponding type. The unsigned @p Type
4873d934112e6170b0fd940d8e40db6936cea2cdcf62Douglas Gregor/// is actually a value of type @c TargetInfo::IntType.
4874e27ec8ad56dbf1efb2de004b90fbbb86f740e3f1John McCallCanQualType ASTContext::getFromTargetType(unsigned Type) const {
4875b4e66d5259f90e9aae4d40fc5de801e046c7df94Douglas Gregor  switch (Type) {
4876e27ec8ad56dbf1efb2de004b90fbbb86f740e3f1John McCall  case TargetInfo::NoInt: return CanQualType();
4877b4e66d5259f90e9aae4d40fc5de801e046c7df94Douglas Gregor  case TargetInfo::SignedShort: return ShortTy;
4878b4e66d5259f90e9aae4d40fc5de801e046c7df94Douglas Gregor  case TargetInfo::UnsignedShort: return UnsignedShortTy;
4879b4e66d5259f90e9aae4d40fc5de801e046c7df94Douglas Gregor  case TargetInfo::SignedInt: return IntTy;
4880b4e66d5259f90e9aae4d40fc5de801e046c7df94Douglas Gregor  case TargetInfo::UnsignedInt: return UnsignedIntTy;
4881b4e66d5259f90e9aae4d40fc5de801e046c7df94Douglas Gregor  case TargetInfo::SignedLong: return LongTy;
4882b4e66d5259f90e9aae4d40fc5de801e046c7df94Douglas Gregor  case TargetInfo::UnsignedLong: return UnsignedLongTy;
4883b4e66d5259f90e9aae4d40fc5de801e046c7df94Douglas Gregor  case TargetInfo::SignedLongLong: return LongLongTy;
4884b4e66d5259f90e9aae4d40fc5de801e046c7df94Douglas Gregor  case TargetInfo::UnsignedLongLong: return UnsignedLongLongTy;
4885b4e66d5259f90e9aae4d40fc5de801e046c7df94Douglas Gregor  }
4886b4e66d5259f90e9aae4d40fc5de801e046c7df94Douglas Gregor
4887b4e66d5259f90e9aae4d40fc5de801e046c7df94Douglas Gregor  assert(false && "Unhandled TargetInfo::IntType value");
4888e27ec8ad56dbf1efb2de004b90fbbb86f740e3f1John McCall  return CanQualType();
4889b4e66d5259f90e9aae4d40fc5de801e046c7df94Douglas Gregor}
4890b6ccaac65ca72f72954eb3893bbd940bedd23f00Ted Kremenek
4891b6ccaac65ca72f72954eb3893bbd940bedd23f00Ted Kremenek//===----------------------------------------------------------------------===//
4892b6ccaac65ca72f72954eb3893bbd940bedd23f00Ted Kremenek//                        Type Predicates.
4893b6ccaac65ca72f72954eb3893bbd940bedd23f00Ted Kremenek//===----------------------------------------------------------------------===//
4894b6ccaac65ca72f72954eb3893bbd940bedd23f00Ted Kremenek
48954fd83ea566f4a0c083001c84b75da6cc8c99c1d6Fariborz Jahanian/// getObjCGCAttr - Returns one of GCNone, Weak or Strong objc's
48964fd83ea566f4a0c083001c84b75da6cc8c99c1d6Fariborz Jahanian/// garbage collection attribute.
48974fd83ea566f4a0c083001c84b75da6cc8c99c1d6Fariborz Jahanian///
4898ae278a3a57595349a411f6474938d4dd1b263a0eJohn McCallQualifiers::GC ASTContext::getObjCGCAttrKind(QualType Ty) const {
4899ae278a3a57595349a411f6474938d4dd1b263a0eJohn McCall  if (getLangOptions().getGCMode() == LangOptions::NonGC)
4900ae278a3a57595349a411f6474938d4dd1b263a0eJohn McCall    return Qualifiers::GCNone;
4901ae278a3a57595349a411f6474938d4dd1b263a0eJohn McCall
4902ae278a3a57595349a411f6474938d4dd1b263a0eJohn McCall  assert(getLangOptions().ObjC1);
4903ae278a3a57595349a411f6474938d4dd1b263a0eJohn McCall  Qualifiers::GC GCAttrs = Ty.getObjCGCAttr();
4904ae278a3a57595349a411f6474938d4dd1b263a0eJohn McCall
4905ae278a3a57595349a411f6474938d4dd1b263a0eJohn McCall  // Default behaviour under objective-C's gc is for ObjC pointers
4906ae278a3a57595349a411f6474938d4dd1b263a0eJohn McCall  // (or pointers to them) be treated as though they were declared
4907ae278a3a57595349a411f6474938d4dd1b263a0eJohn McCall  // as __strong.
4908ae278a3a57595349a411f6474938d4dd1b263a0eJohn McCall  if (GCAttrs == Qualifiers::GCNone) {
4909ae278a3a57595349a411f6474938d4dd1b263a0eJohn McCall    if (Ty->isObjCObjectPointerType() || Ty->isBlockPointerType())
4910ae278a3a57595349a411f6474938d4dd1b263a0eJohn McCall      return Qualifiers::Strong;
4911ae278a3a57595349a411f6474938d4dd1b263a0eJohn McCall    else if (Ty->isPointerType())
4912ae278a3a57595349a411f6474938d4dd1b263a0eJohn McCall      return getObjCGCAttrKind(Ty->getAs<PointerType>()->getPointeeType());
4913ae278a3a57595349a411f6474938d4dd1b263a0eJohn McCall  } else {
4914ae278a3a57595349a411f6474938d4dd1b263a0eJohn McCall    // It's not valid to set GC attributes on anything that isn't a
4915ae278a3a57595349a411f6474938d4dd1b263a0eJohn McCall    // pointer.
4916ae278a3a57595349a411f6474938d4dd1b263a0eJohn McCall#ifndef NDEBUG
4917ae278a3a57595349a411f6474938d4dd1b263a0eJohn McCall    QualType CT = Ty->getCanonicalTypeInternal();
4918ae278a3a57595349a411f6474938d4dd1b263a0eJohn McCall    while (const ArrayType *AT = dyn_cast<ArrayType>(CT))
4919ae278a3a57595349a411f6474938d4dd1b263a0eJohn McCall      CT = AT->getElementType();
4920ae278a3a57595349a411f6474938d4dd1b263a0eJohn McCall    assert(CT->isAnyPointerType() || CT->isBlockPointerType());
4921ae278a3a57595349a411f6474938d4dd1b263a0eJohn McCall#endif
49224fd83ea566f4a0c083001c84b75da6cc8c99c1d6Fariborz Jahanian  }
4923b7d2553edd2532d29b98b9e76bcf6a62bc48b417Chris Lattner  return GCAttrs;
49244fd83ea566f4a0c083001c84b75da6cc8c99c1d6Fariborz Jahanian}
49254fd83ea566f4a0c083001c84b75da6cc8c99c1d6Fariborz Jahanian
49266ac46a4a521366d7ab36ebe2ce4e624ab96b06f9Chris Lattner//===----------------------------------------------------------------------===//
49276ac46a4a521366d7ab36ebe2ce4e624ab96b06f9Chris Lattner//                        Type Compatibility Testing
49286ac46a4a521366d7ab36ebe2ce4e624ab96b06f9Chris Lattner//===----------------------------------------------------------------------===//
4929770951b5bb6028a8d326ddb4a13cef7d4a128162Chris Lattner
49301eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// areCompatVectorTypes - Return true if the two specified vector types are
49316ac46a4a521366d7ab36ebe2ce4e624ab96b06f9Chris Lattner/// compatible.
49326ac46a4a521366d7ab36ebe2ce4e624ab96b06f9Chris Lattnerstatic bool areCompatVectorTypes(const VectorType *LHS,
49336ac46a4a521366d7ab36ebe2ce4e624ab96b06f9Chris Lattner                                 const VectorType *RHS) {
4934467b27b9a24bdc823218ad1ad0e37673b6cc1e83John McCall  assert(LHS->isCanonicalUnqualified() && RHS->isCanonicalUnqualified());
49356ac46a4a521366d7ab36ebe2ce4e624ab96b06f9Chris Lattner  return LHS->getElementType() == RHS->getElementType() &&
493661710854be2b098428aff5316e64bd34b30fbcb7Chris Lattner         LHS->getNumElements() == RHS->getNumElements();
49376ac46a4a521366d7ab36ebe2ce4e624ab96b06f9Chris Lattner}
49386ac46a4a521366d7ab36ebe2ce4e624ab96b06f9Chris Lattner
4939255210ef415b9893f0e3794e8d9a704194c12f3cDouglas Gregorbool ASTContext::areCompatibleVectorTypes(QualType FirstVec,
4940255210ef415b9893f0e3794e8d9a704194c12f3cDouglas Gregor                                          QualType SecondVec) {
4941255210ef415b9893f0e3794e8d9a704194c12f3cDouglas Gregor  assert(FirstVec->isVectorType() && "FirstVec should be a vector type");
4942255210ef415b9893f0e3794e8d9a704194c12f3cDouglas Gregor  assert(SecondVec->isVectorType() && "SecondVec should be a vector type");
4943255210ef415b9893f0e3794e8d9a704194c12f3cDouglas Gregor
4944255210ef415b9893f0e3794e8d9a704194c12f3cDouglas Gregor  if (hasSameUnqualifiedType(FirstVec, SecondVec))
4945255210ef415b9893f0e3794e8d9a704194c12f3cDouglas Gregor    return true;
4946255210ef415b9893f0e3794e8d9a704194c12f3cDouglas Gregor
4947f69eb7cf8e616b5aad7911ec6f79b24b0a009227Bob Wilson  // Treat Neon vector types and most AltiVec vector types as if they are the
4948f69eb7cf8e616b5aad7911ec6f79b24b0a009227Bob Wilson  // equivalent GCC vector types.
4949255210ef415b9893f0e3794e8d9a704194c12f3cDouglas Gregor  const VectorType *First = FirstVec->getAs<VectorType>();
4950255210ef415b9893f0e3794e8d9a704194c12f3cDouglas Gregor  const VectorType *Second = SecondVec->getAs<VectorType>();
4951f69eb7cf8e616b5aad7911ec6f79b24b0a009227Bob Wilson  if (First->getNumElements() == Second->getNumElements() &&
4952255210ef415b9893f0e3794e8d9a704194c12f3cDouglas Gregor      hasSameType(First->getElementType(), Second->getElementType()) &&
4953f69eb7cf8e616b5aad7911ec6f79b24b0a009227Bob Wilson      First->getVectorKind() != VectorType::AltiVecPixel &&
4954f69eb7cf8e616b5aad7911ec6f79b24b0a009227Bob Wilson      First->getVectorKind() != VectorType::AltiVecBool &&
4955f69eb7cf8e616b5aad7911ec6f79b24b0a009227Bob Wilson      Second->getVectorKind() != VectorType::AltiVecPixel &&
4956f69eb7cf8e616b5aad7911ec6f79b24b0a009227Bob Wilson      Second->getVectorKind() != VectorType::AltiVecBool)
4957255210ef415b9893f0e3794e8d9a704194c12f3cDouglas Gregor    return true;
4958255210ef415b9893f0e3794e8d9a704194c12f3cDouglas Gregor
4959255210ef415b9893f0e3794e8d9a704194c12f3cDouglas Gregor  return false;
4960255210ef415b9893f0e3794e8d9a704194c12f3cDouglas Gregor}
4961255210ef415b9893f0e3794e8d9a704194c12f3cDouglas Gregor
49624084c306635b70f37029dca938444e6013f08684Steve Naroff//===----------------------------------------------------------------------===//
49634084c306635b70f37029dca938444e6013f08684Steve Naroff// ObjCQualifiedIdTypesAreCompatible - Compatibility testing for qualified id's.
49644084c306635b70f37029dca938444e6013f08684Steve Naroff//===----------------------------------------------------------------------===//
49654084c306635b70f37029dca938444e6013f08684Steve Naroff
49664084c306635b70f37029dca938444e6013f08684Steve Naroff/// ProtocolCompatibleWithProtocol - return 'true' if 'lProto' is in the
49674084c306635b70f37029dca938444e6013f08684Steve Naroff/// inheritance hierarchy of 'rProto'.
49684ba2a17694148e16eaa8d3917f657ffcd3667be4Jay Foadbool
49694ba2a17694148e16eaa8d3917f657ffcd3667be4Jay FoadASTContext::ProtocolCompatibleWithProtocol(ObjCProtocolDecl *lProto,
49704ba2a17694148e16eaa8d3917f657ffcd3667be4Jay Foad                                           ObjCProtocolDecl *rProto) const {
49714084c306635b70f37029dca938444e6013f08684Steve Naroff  if (lProto == rProto)
497214108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff    return true;
49734084c306635b70f37029dca938444e6013f08684Steve Naroff  for (ObjCProtocolDecl::protocol_iterator PI = rProto->protocol_begin(),
49744084c306635b70f37029dca938444e6013f08684Steve Naroff       E = rProto->protocol_end(); PI != E; ++PI)
49754084c306635b70f37029dca938444e6013f08684Steve Naroff    if (ProtocolCompatibleWithProtocol(lProto, *PI))
49764084c306635b70f37029dca938444e6013f08684Steve Naroff      return true;
49774084c306635b70f37029dca938444e6013f08684Steve Naroff  return false;
49784084c306635b70f37029dca938444e6013f08684Steve Naroff}
497914108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff
49804084c306635b70f37029dca938444e6013f08684Steve Naroff/// QualifiedIdConformsQualifiedId - compare id<p,...> with id<p1,...>
49814084c306635b70f37029dca938444e6013f08684Steve Naroff/// return true if lhs's protocols conform to rhs's protocol; false
49824084c306635b70f37029dca938444e6013f08684Steve Naroff/// otherwise.
49834084c306635b70f37029dca938444e6013f08684Steve Naroffbool ASTContext::QualifiedIdConformsQualifiedId(QualType lhs, QualType rhs) {
49844084c306635b70f37029dca938444e6013f08684Steve Naroff  if (lhs->isObjCQualifiedIdType() && rhs->isObjCQualifiedIdType())
49854084c306635b70f37029dca938444e6013f08684Steve Naroff    return ObjCQualifiedIdTypesAreCompatible(lhs, rhs, false);
49864084c306635b70f37029dca938444e6013f08684Steve Naroff  return false;
49874084c306635b70f37029dca938444e6013f08684Steve Naroff}
49884084c306635b70f37029dca938444e6013f08684Steve Naroff
4989a8f8dac6a29f6d33474a38a32ce9dd859b696da9Fariborz Jahanian/// ObjCQualifiedClassTypesAreCompatible - compare  Class<p,...> and
4990a8f8dac6a29f6d33474a38a32ce9dd859b696da9Fariborz Jahanian/// Class<p1, ...>.
4991a8f8dac6a29f6d33474a38a32ce9dd859b696da9Fariborz Jahanianbool ASTContext::ObjCQualifiedClassTypesAreCompatible(QualType lhs,
4992a8f8dac6a29f6d33474a38a32ce9dd859b696da9Fariborz Jahanian                                                      QualType rhs) {
4993a8f8dac6a29f6d33474a38a32ce9dd859b696da9Fariborz Jahanian  const ObjCObjectPointerType *lhsQID = lhs->getAs<ObjCObjectPointerType>();
4994a8f8dac6a29f6d33474a38a32ce9dd859b696da9Fariborz Jahanian  const ObjCObjectPointerType *rhsOPT = rhs->getAs<ObjCObjectPointerType>();
4995a8f8dac6a29f6d33474a38a32ce9dd859b696da9Fariborz Jahanian  assert ((lhsQID && rhsOPT) && "ObjCQualifiedClassTypesAreCompatible");
4996a8f8dac6a29f6d33474a38a32ce9dd859b696da9Fariborz Jahanian
4997a8f8dac6a29f6d33474a38a32ce9dd859b696da9Fariborz Jahanian  for (ObjCObjectPointerType::qual_iterator I = lhsQID->qual_begin(),
4998a8f8dac6a29f6d33474a38a32ce9dd859b696da9Fariborz Jahanian       E = lhsQID->qual_end(); I != E; ++I) {
4999a8f8dac6a29f6d33474a38a32ce9dd859b696da9Fariborz Jahanian    bool match = false;
5000a8f8dac6a29f6d33474a38a32ce9dd859b696da9Fariborz Jahanian    ObjCProtocolDecl *lhsProto = *I;
5001a8f8dac6a29f6d33474a38a32ce9dd859b696da9Fariborz Jahanian    for (ObjCObjectPointerType::qual_iterator J = rhsOPT->qual_begin(),
5002a8f8dac6a29f6d33474a38a32ce9dd859b696da9Fariborz Jahanian         E = rhsOPT->qual_end(); J != E; ++J) {
5003a8f8dac6a29f6d33474a38a32ce9dd859b696da9Fariborz Jahanian      ObjCProtocolDecl *rhsProto = *J;
5004a8f8dac6a29f6d33474a38a32ce9dd859b696da9Fariborz Jahanian      if (ProtocolCompatibleWithProtocol(lhsProto, rhsProto)) {
5005a8f8dac6a29f6d33474a38a32ce9dd859b696da9Fariborz Jahanian        match = true;
5006a8f8dac6a29f6d33474a38a32ce9dd859b696da9Fariborz Jahanian        break;
5007a8f8dac6a29f6d33474a38a32ce9dd859b696da9Fariborz Jahanian      }
5008a8f8dac6a29f6d33474a38a32ce9dd859b696da9Fariborz Jahanian    }
5009a8f8dac6a29f6d33474a38a32ce9dd859b696da9Fariborz Jahanian    if (!match)
5010a8f8dac6a29f6d33474a38a32ce9dd859b696da9Fariborz Jahanian      return false;
5011a8f8dac6a29f6d33474a38a32ce9dd859b696da9Fariborz Jahanian  }
5012a8f8dac6a29f6d33474a38a32ce9dd859b696da9Fariborz Jahanian  return true;
5013a8f8dac6a29f6d33474a38a32ce9dd859b696da9Fariborz Jahanian}
5014a8f8dac6a29f6d33474a38a32ce9dd859b696da9Fariborz Jahanian
50154084c306635b70f37029dca938444e6013f08684Steve Naroff/// ObjCQualifiedIdTypesAreCompatible - We know that one of lhs/rhs is an
50164084c306635b70f37029dca938444e6013f08684Steve Naroff/// ObjCQualifiedIDType.
50174084c306635b70f37029dca938444e6013f08684Steve Naroffbool ASTContext::ObjCQualifiedIdTypesAreCompatible(QualType lhs, QualType rhs,
50184084c306635b70f37029dca938444e6013f08684Steve Naroff                                                   bool compare) {
50194084c306635b70f37029dca938444e6013f08684Steve Naroff  // Allow id<P..> and an 'id' or void* type in all cases.
50201eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  if (lhs->isVoidPointerType() ||
50214084c306635b70f37029dca938444e6013f08684Steve Naroff      lhs->isObjCIdType() || lhs->isObjCClassType())
50224084c306635b70f37029dca938444e6013f08684Steve Naroff    return true;
50231eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  else if (rhs->isVoidPointerType() ||
50244084c306635b70f37029dca938444e6013f08684Steve Naroff           rhs->isObjCIdType() || rhs->isObjCClassType())
50254084c306635b70f37029dca938444e6013f08684Steve Naroff    return true;
50264084c306635b70f37029dca938444e6013f08684Steve Naroff
50274084c306635b70f37029dca938444e6013f08684Steve Naroff  if (const ObjCObjectPointerType *lhsQID = lhs->getAsObjCQualifiedIdType()) {
5028183700f494ec9b6701b6efe82bcb25f4c79ba561John McCall    const ObjCObjectPointerType *rhsOPT = rhs->getAs<ObjCObjectPointerType>();
50291eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
50304084c306635b70f37029dca938444e6013f08684Steve Naroff    if (!rhsOPT) return false;
50311eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
50324084c306635b70f37029dca938444e6013f08684Steve Naroff    if (rhsOPT->qual_empty()) {
50331eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      // If the RHS is a unqualified interface pointer "NSString*",
50344084c306635b70f37029dca938444e6013f08684Steve Naroff      // make sure we check the class hierarchy.
50354084c306635b70f37029dca938444e6013f08684Steve Naroff      if (ObjCInterfaceDecl *rhsID = rhsOPT->getInterfaceDecl()) {
50364084c306635b70f37029dca938444e6013f08684Steve Naroff        for (ObjCObjectPointerType::qual_iterator I = lhsQID->qual_begin(),
50374084c306635b70f37029dca938444e6013f08684Steve Naroff             E = lhsQID->qual_end(); I != E; ++I) {
50384084c306635b70f37029dca938444e6013f08684Steve Naroff          // when comparing an id<P> on lhs with a static type on rhs,
50394084c306635b70f37029dca938444e6013f08684Steve Naroff          // see if static class implements all of id's protocols, directly or
50404084c306635b70f37029dca938444e6013f08684Steve Naroff          // through its super class and categories.
50410fd8904c5f71a11d29f67716c3ebdf7ad1c855fbFariborz Jahanian          if (!rhsID->ClassImplementsProtocol(*I, true))
50424084c306635b70f37029dca938444e6013f08684Steve Naroff            return false;
50434084c306635b70f37029dca938444e6013f08684Steve Naroff        }
50444084c306635b70f37029dca938444e6013f08684Steve Naroff      }
50454084c306635b70f37029dca938444e6013f08684Steve Naroff      // If there are no qualifiers and no interface, we have an 'id'.
50464084c306635b70f37029dca938444e6013f08684Steve Naroff      return true;
50474084c306635b70f37029dca938444e6013f08684Steve Naroff    }
50481eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    // Both the right and left sides have qualifiers.
50494084c306635b70f37029dca938444e6013f08684Steve Naroff    for (ObjCObjectPointerType::qual_iterator I = lhsQID->qual_begin(),
50504084c306635b70f37029dca938444e6013f08684Steve Naroff         E = lhsQID->qual_end(); I != E; ++I) {
50514084c306635b70f37029dca938444e6013f08684Steve Naroff      ObjCProtocolDecl *lhsProto = *I;
50524084c306635b70f37029dca938444e6013f08684Steve Naroff      bool match = false;
5053de2e22d33afec98324a66a358dfe0951b3c7259aSteve Naroff
5054de2e22d33afec98324a66a358dfe0951b3c7259aSteve Naroff      // when comparing an id<P> on lhs with a static type on rhs,
5055de2e22d33afec98324a66a358dfe0951b3c7259aSteve Naroff      // see if static class implements all of id's protocols, directly or
5056de2e22d33afec98324a66a358dfe0951b3c7259aSteve Naroff      // through its super class and categories.
50574084c306635b70f37029dca938444e6013f08684Steve Naroff      for (ObjCObjectPointerType::qual_iterator J = rhsOPT->qual_begin(),
50584084c306635b70f37029dca938444e6013f08684Steve Naroff           E = rhsOPT->qual_end(); J != E; ++J) {
50594084c306635b70f37029dca938444e6013f08684Steve Naroff        ObjCProtocolDecl *rhsProto = *J;
50604084c306635b70f37029dca938444e6013f08684Steve Naroff        if (ProtocolCompatibleWithProtocol(lhsProto, rhsProto) ||
50614084c306635b70f37029dca938444e6013f08684Steve Naroff            (compare && ProtocolCompatibleWithProtocol(rhsProto, lhsProto))) {
50624084c306635b70f37029dca938444e6013f08684Steve Naroff          match = true;
50638f16756441450ed9fb39316e47d107fc2a1ef35bSteve Naroff          break;
50648f16756441450ed9fb39316e47d107fc2a1ef35bSteve Naroff        }
5065de2e22d33afec98324a66a358dfe0951b3c7259aSteve Naroff      }
50661eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      // If the RHS is a qualified interface pointer "NSString<P>*",
50674084c306635b70f37029dca938444e6013f08684Steve Naroff      // make sure we check the class hierarchy.
50684084c306635b70f37029dca938444e6013f08684Steve Naroff      if (ObjCInterfaceDecl *rhsID = rhsOPT->getInterfaceDecl()) {
50694084c306635b70f37029dca938444e6013f08684Steve Naroff        for (ObjCObjectPointerType::qual_iterator I = lhsQID->qual_begin(),
50704084c306635b70f37029dca938444e6013f08684Steve Naroff             E = lhsQID->qual_end(); I != E; ++I) {
50714084c306635b70f37029dca938444e6013f08684Steve Naroff          // when comparing an id<P> on lhs with a static type on rhs,
50724084c306635b70f37029dca938444e6013f08684Steve Naroff          // see if static class implements all of id's protocols, directly or
50734084c306635b70f37029dca938444e6013f08684Steve Naroff          // through its super class and categories.
50740fd8904c5f71a11d29f67716c3ebdf7ad1c855fbFariborz Jahanian          if (rhsID->ClassImplementsProtocol(*I, true)) {
50754084c306635b70f37029dca938444e6013f08684Steve Naroff            match = true;
50764084c306635b70f37029dca938444e6013f08684Steve Naroff            break;
50774084c306635b70f37029dca938444e6013f08684Steve Naroff          }
50784084c306635b70f37029dca938444e6013f08684Steve Naroff        }
50794084c306635b70f37029dca938444e6013f08684Steve Naroff      }
50804084c306635b70f37029dca938444e6013f08684Steve Naroff      if (!match)
5081de2e22d33afec98324a66a358dfe0951b3c7259aSteve Naroff        return false;
5082de2e22d33afec98324a66a358dfe0951b3c7259aSteve Naroff    }
50831eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
5084de2e22d33afec98324a66a358dfe0951b3c7259aSteve Naroff    return true;
5085de2e22d33afec98324a66a358dfe0951b3c7259aSteve Naroff  }
50861eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
50874084c306635b70f37029dca938444e6013f08684Steve Naroff  const ObjCObjectPointerType *rhsQID = rhs->getAsObjCQualifiedIdType();
50884084c306635b70f37029dca938444e6013f08684Steve Naroff  assert(rhsQID && "One of the LHS/RHS should be id<x>");
50894084c306635b70f37029dca938444e6013f08684Steve Naroff
50901eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  if (const ObjCObjectPointerType *lhsOPT =
50914084c306635b70f37029dca938444e6013f08684Steve Naroff        lhs->getAsObjCInterfacePointerType()) {
5092de5b17ea3c74d2844ed035a1edfa6479866139b5Fariborz Jahanian    // If both the right and left sides have qualifiers.
50934084c306635b70f37029dca938444e6013f08684Steve Naroff    for (ObjCObjectPointerType::qual_iterator I = lhsOPT->qual_begin(),
50944084c306635b70f37029dca938444e6013f08684Steve Naroff         E = lhsOPT->qual_end(); I != E; ++I) {
50954084c306635b70f37029dca938444e6013f08684Steve Naroff      ObjCProtocolDecl *lhsProto = *I;
50964084c306635b70f37029dca938444e6013f08684Steve Naroff      bool match = false;
50974084c306635b70f37029dca938444e6013f08684Steve Naroff
5098de5b17ea3c74d2844ed035a1edfa6479866139b5Fariborz Jahanian      // when comparing an id<P> on rhs with a static type on lhs,
50994084c306635b70f37029dca938444e6013f08684Steve Naroff      // see if static class implements all of id's protocols, directly or
51004084c306635b70f37029dca938444e6013f08684Steve Naroff      // through its super class and categories.
5101de5b17ea3c74d2844ed035a1edfa6479866139b5Fariborz Jahanian      // First, lhs protocols in the qualifier list must be found, direct
5102de5b17ea3c74d2844ed035a1edfa6479866139b5Fariborz Jahanian      // or indirect in rhs's qualifier list or it is a mismatch.
51034084c306635b70f37029dca938444e6013f08684Steve Naroff      for (ObjCObjectPointerType::qual_iterator J = rhsQID->qual_begin(),
51044084c306635b70f37029dca938444e6013f08684Steve Naroff           E = rhsQID->qual_end(); J != E; ++J) {
51054084c306635b70f37029dca938444e6013f08684Steve Naroff        ObjCProtocolDecl *rhsProto = *J;
51064084c306635b70f37029dca938444e6013f08684Steve Naroff        if (ProtocolCompatibleWithProtocol(lhsProto, rhsProto) ||
51074084c306635b70f37029dca938444e6013f08684Steve Naroff            (compare && ProtocolCompatibleWithProtocol(rhsProto, lhsProto))) {
51084084c306635b70f37029dca938444e6013f08684Steve Naroff          match = true;
51094084c306635b70f37029dca938444e6013f08684Steve Naroff          break;
51104084c306635b70f37029dca938444e6013f08684Steve Naroff        }
51114084c306635b70f37029dca938444e6013f08684Steve Naroff      }
51124084c306635b70f37029dca938444e6013f08684Steve Naroff      if (!match)
51134084c306635b70f37029dca938444e6013f08684Steve Naroff        return false;
51144084c306635b70f37029dca938444e6013f08684Steve Naroff    }
5115de5b17ea3c74d2844ed035a1edfa6479866139b5Fariborz Jahanian
5116de5b17ea3c74d2844ed035a1edfa6479866139b5Fariborz Jahanian    // Static class's protocols, or its super class or category protocols
5117de5b17ea3c74d2844ed035a1edfa6479866139b5Fariborz Jahanian    // must be found, direct or indirect in rhs's qualifier list or it is a mismatch.
5118de5b17ea3c74d2844ed035a1edfa6479866139b5Fariborz Jahanian    if (ObjCInterfaceDecl *lhsID = lhsOPT->getInterfaceDecl()) {
5119de5b17ea3c74d2844ed035a1edfa6479866139b5Fariborz Jahanian      llvm::SmallPtrSet<ObjCProtocolDecl *, 8> LHSInheritedProtocols;
5120de5b17ea3c74d2844ed035a1edfa6479866139b5Fariborz Jahanian      CollectInheritedProtocols(lhsID, LHSInheritedProtocols);
5121de5b17ea3c74d2844ed035a1edfa6479866139b5Fariborz Jahanian      // This is rather dubious but matches gcc's behavior. If lhs has
5122de5b17ea3c74d2844ed035a1edfa6479866139b5Fariborz Jahanian      // no type qualifier and its class has no static protocol(s)
5123de5b17ea3c74d2844ed035a1edfa6479866139b5Fariborz Jahanian      // assume that it is mismatch.
5124de5b17ea3c74d2844ed035a1edfa6479866139b5Fariborz Jahanian      if (LHSInheritedProtocols.empty() && lhsOPT->qual_empty())
5125de5b17ea3c74d2844ed035a1edfa6479866139b5Fariborz Jahanian        return false;
5126de5b17ea3c74d2844ed035a1edfa6479866139b5Fariborz Jahanian      for (llvm::SmallPtrSet<ObjCProtocolDecl*,8>::iterator I =
5127de5b17ea3c74d2844ed035a1edfa6479866139b5Fariborz Jahanian           LHSInheritedProtocols.begin(),
5128de5b17ea3c74d2844ed035a1edfa6479866139b5Fariborz Jahanian           E = LHSInheritedProtocols.end(); I != E; ++I) {
5129de5b17ea3c74d2844ed035a1edfa6479866139b5Fariborz Jahanian        bool match = false;
5130de5b17ea3c74d2844ed035a1edfa6479866139b5Fariborz Jahanian        ObjCProtocolDecl *lhsProto = (*I);
5131de5b17ea3c74d2844ed035a1edfa6479866139b5Fariborz Jahanian        for (ObjCObjectPointerType::qual_iterator J = rhsQID->qual_begin(),
5132de5b17ea3c74d2844ed035a1edfa6479866139b5Fariborz Jahanian             E = rhsQID->qual_end(); J != E; ++J) {
5133de5b17ea3c74d2844ed035a1edfa6479866139b5Fariborz Jahanian          ObjCProtocolDecl *rhsProto = *J;
5134de5b17ea3c74d2844ed035a1edfa6479866139b5Fariborz Jahanian          if (ProtocolCompatibleWithProtocol(lhsProto, rhsProto) ||
5135de5b17ea3c74d2844ed035a1edfa6479866139b5Fariborz Jahanian              (compare && ProtocolCompatibleWithProtocol(rhsProto, lhsProto))) {
5136de5b17ea3c74d2844ed035a1edfa6479866139b5Fariborz Jahanian            match = true;
5137de5b17ea3c74d2844ed035a1edfa6479866139b5Fariborz Jahanian            break;
5138de5b17ea3c74d2844ed035a1edfa6479866139b5Fariborz Jahanian          }
5139de5b17ea3c74d2844ed035a1edfa6479866139b5Fariborz Jahanian        }
5140de5b17ea3c74d2844ed035a1edfa6479866139b5Fariborz Jahanian        if (!match)
5141de5b17ea3c74d2844ed035a1edfa6479866139b5Fariborz Jahanian          return false;
5142de5b17ea3c74d2844ed035a1edfa6479866139b5Fariborz Jahanian      }
5143de5b17ea3c74d2844ed035a1edfa6479866139b5Fariborz Jahanian    }
51444084c306635b70f37029dca938444e6013f08684Steve Naroff    return true;
51454084c306635b70f37029dca938444e6013f08684Steve Naroff  }
51464084c306635b70f37029dca938444e6013f08684Steve Naroff  return false;
51474084c306635b70f37029dca938444e6013f08684Steve Naroff}
51484084c306635b70f37029dca938444e6013f08684Steve Naroff
51494084c306635b70f37029dca938444e6013f08684Steve Naroff/// canAssignObjCInterfaces - Return true if the two interface types are
51504084c306635b70f37029dca938444e6013f08684Steve Naroff/// compatible for assignment from RHS to LHS.  This handles validation of any
51514084c306635b70f37029dca938444e6013f08684Steve Naroff/// protocol qualifiers on the LHS or RHS.
51524084c306635b70f37029dca938444e6013f08684Steve Naroff///
51534084c306635b70f37029dca938444e6013f08684Steve Naroffbool ASTContext::canAssignObjCInterfaces(const ObjCObjectPointerType *LHSOPT,
51544084c306635b70f37029dca938444e6013f08684Steve Naroff                                         const ObjCObjectPointerType *RHSOPT) {
5155c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall  const ObjCObjectType* LHS = LHSOPT->getObjectType();
5156c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall  const ObjCObjectType* RHS = RHSOPT->getObjectType();
5157c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall
51584084c306635b70f37029dca938444e6013f08684Steve Naroff  // If either type represents the built-in 'id' or 'Class' types, return true.
5159c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall  if (LHS->isObjCUnqualifiedIdOrClass() ||
5160c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall      RHS->isObjCUnqualifiedIdOrClass())
51614084c306635b70f37029dca938444e6013f08684Steve Naroff    return true;
51624084c306635b70f37029dca938444e6013f08684Steve Naroff
5163c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall  if (LHS->isObjCQualifiedId() || RHS->isObjCQualifiedId())
51641eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    return ObjCQualifiedIdTypesAreCompatible(QualType(LHSOPT,0),
51651eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump                                             QualType(RHSOPT,0),
51664084c306635b70f37029dca938444e6013f08684Steve Naroff                                             false);
5167a8f8dac6a29f6d33474a38a32ce9dd859b696da9Fariborz Jahanian
5168a8f8dac6a29f6d33474a38a32ce9dd859b696da9Fariborz Jahanian  if (LHS->isObjCQualifiedClass() && RHS->isObjCQualifiedClass())
5169a8f8dac6a29f6d33474a38a32ce9dd859b696da9Fariborz Jahanian    return ObjCQualifiedClassTypesAreCompatible(QualType(LHSOPT,0),
5170a8f8dac6a29f6d33474a38a32ce9dd859b696da9Fariborz Jahanian                                                QualType(RHSOPT,0));
5171a8f8dac6a29f6d33474a38a32ce9dd859b696da9Fariborz Jahanian
5172c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall  // If we have 2 user-defined types, fall into that path.
5173c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall  if (LHS->getInterface() && RHS->getInterface())
51744084c306635b70f37029dca938444e6013f08684Steve Naroff    return canAssignObjCInterfaces(LHS, RHS);
51751eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
51764084c306635b70f37029dca938444e6013f08684Steve Naroff  return false;
517714108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff}
517814108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff
5179132f2a2da34f378fc675b9e174564b0f52c31d98Fariborz Jahanian/// canAssignObjCInterfacesInBlockPointer - This routine is specifically written
5180fc8f0e14ad142ed811e90fbd9a30e419e301c717Chris Lattner/// for providing type-safety for objective-c pointers used to pass/return
5181132f2a2da34f378fc675b9e174564b0f52c31d98Fariborz Jahanian/// arguments in block literals. When passed as arguments, passing 'A*' where
5182132f2a2da34f378fc675b9e174564b0f52c31d98Fariborz Jahanian/// 'id' is expected is not OK. Passing 'Sub *" where 'Super *" is expected is
5183132f2a2da34f378fc675b9e174564b0f52c31d98Fariborz Jahanian/// not OK. For the return type, the opposite is not OK.
5184132f2a2da34f378fc675b9e174564b0f52c31d98Fariborz Jahanianbool ASTContext::canAssignObjCInterfacesInBlockPointer(
5185132f2a2da34f378fc675b9e174564b0f52c31d98Fariborz Jahanian                                         const ObjCObjectPointerType *LHSOPT,
5186a4fdbfad150ae37bddaa4094d3925a27a1a1cf3fFariborz Jahanian                                         const ObjCObjectPointerType *RHSOPT,
5187a4fdbfad150ae37bddaa4094d3925a27a1a1cf3fFariborz Jahanian                                         bool BlockReturnType) {
5188a98344869c278295f38e106583906377520079b8Fariborz Jahanian  if (RHSOPT->isObjCBuiltinType() || LHSOPT->isObjCIdType())
5189132f2a2da34f378fc675b9e174564b0f52c31d98Fariborz Jahanian    return true;
5190132f2a2da34f378fc675b9e174564b0f52c31d98Fariborz Jahanian
5191132f2a2da34f378fc675b9e174564b0f52c31d98Fariborz Jahanian  if (LHSOPT->isObjCBuiltinType()) {
5192132f2a2da34f378fc675b9e174564b0f52c31d98Fariborz Jahanian    return RHSOPT->isObjCBuiltinType() || RHSOPT->isObjCQualifiedIdType();
5193132f2a2da34f378fc675b9e174564b0f52c31d98Fariborz Jahanian  }
5194132f2a2da34f378fc675b9e174564b0f52c31d98Fariborz Jahanian
5195a98344869c278295f38e106583906377520079b8Fariborz Jahanian  if (LHSOPT->isObjCQualifiedIdType() || RHSOPT->isObjCQualifiedIdType())
5196132f2a2da34f378fc675b9e174564b0f52c31d98Fariborz Jahanian    return ObjCQualifiedIdTypesAreCompatible(QualType(LHSOPT,0),
5197132f2a2da34f378fc675b9e174564b0f52c31d98Fariborz Jahanian                                             QualType(RHSOPT,0),
5198132f2a2da34f378fc675b9e174564b0f52c31d98Fariborz Jahanian                                             false);
5199132f2a2da34f378fc675b9e174564b0f52c31d98Fariborz Jahanian
5200132f2a2da34f378fc675b9e174564b0f52c31d98Fariborz Jahanian  const ObjCInterfaceType* LHS = LHSOPT->getInterfaceType();
5201132f2a2da34f378fc675b9e174564b0f52c31d98Fariborz Jahanian  const ObjCInterfaceType* RHS = RHSOPT->getInterfaceType();
5202132f2a2da34f378fc675b9e174564b0f52c31d98Fariborz Jahanian  if (LHS && RHS)  { // We have 2 user-defined types.
5203132f2a2da34f378fc675b9e174564b0f52c31d98Fariborz Jahanian    if (LHS != RHS) {
5204132f2a2da34f378fc675b9e174564b0f52c31d98Fariborz Jahanian      if (LHS->getDecl()->isSuperClassOf(RHS->getDecl()))
5205a4fdbfad150ae37bddaa4094d3925a27a1a1cf3fFariborz Jahanian        return BlockReturnType;
5206132f2a2da34f378fc675b9e174564b0f52c31d98Fariborz Jahanian      if (RHS->getDecl()->isSuperClassOf(LHS->getDecl()))
5207a4fdbfad150ae37bddaa4094d3925a27a1a1cf3fFariborz Jahanian        return !BlockReturnType;
5208132f2a2da34f378fc675b9e174564b0f52c31d98Fariborz Jahanian    }
5209132f2a2da34f378fc675b9e174564b0f52c31d98Fariborz Jahanian    else
5210132f2a2da34f378fc675b9e174564b0f52c31d98Fariborz Jahanian      return true;
5211132f2a2da34f378fc675b9e174564b0f52c31d98Fariborz Jahanian  }
5212132f2a2da34f378fc675b9e174564b0f52c31d98Fariborz Jahanian  return false;
5213132f2a2da34f378fc675b9e174564b0f52c31d98Fariborz Jahanian}
5214132f2a2da34f378fc675b9e174564b0f52c31d98Fariborz Jahanian
5215e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanian/// getIntersectionOfProtocols - This routine finds the intersection of set
5216e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanian/// of protocols inherited from two distinct objective-c pointer objects.
5217e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanian/// It is used to build composite qualifier list of the composite type of
5218e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanian/// the conditional expression involving two objective-c pointer objects.
5219e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanianstatic
5220e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanianvoid getIntersectionOfProtocols(ASTContext &Context,
5221e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanian                                const ObjCObjectPointerType *LHSOPT,
5222e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanian                                const ObjCObjectPointerType *RHSOPT,
52235f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner      SmallVectorImpl<ObjCProtocolDecl *> &IntersectionOfProtocols) {
5224e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanian
5225c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall  const ObjCObjectType* LHS = LHSOPT->getObjectType();
5226c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall  const ObjCObjectType* RHS = RHSOPT->getObjectType();
5227c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall  assert(LHS->getInterface() && "LHS must have an interface base");
5228c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall  assert(RHS->getInterface() && "RHS must have an interface base");
5229e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanian
5230e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanian  llvm::SmallPtrSet<ObjCProtocolDecl *, 8> InheritedProtocolSet;
5231e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanian  unsigned LHSNumProtocols = LHS->getNumProtocols();
5232e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanian  if (LHSNumProtocols > 0)
5233e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanian    InheritedProtocolSet.insert(LHS->qual_begin(), LHS->qual_end());
5234e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanian  else {
5235432a8893f7e30d141d7f279bd00b741a3cdac81fFariborz Jahanian    llvm::SmallPtrSet<ObjCProtocolDecl *, 8> LHSInheritedProtocols;
5236c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall    Context.CollectInheritedProtocols(LHS->getInterface(),
5237c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall                                      LHSInheritedProtocols);
5238e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanian    InheritedProtocolSet.insert(LHSInheritedProtocols.begin(),
5239e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanian                                LHSInheritedProtocols.end());
5240e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanian  }
5241e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanian
5242e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanian  unsigned RHSNumProtocols = RHS->getNumProtocols();
5243e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanian  if (RHSNumProtocols > 0) {
5244cb421fa690da545b58a720abe5f1c49b166dbde7Dan Gohman    ObjCProtocolDecl **RHSProtocols =
5245cb421fa690da545b58a720abe5f1c49b166dbde7Dan Gohman      const_cast<ObjCProtocolDecl **>(RHS->qual_begin());
5246e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanian    for (unsigned i = 0; i < RHSNumProtocols; ++i)
5247e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanian      if (InheritedProtocolSet.count(RHSProtocols[i]))
5248e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanian        IntersectionOfProtocols.push_back(RHSProtocols[i]);
5249e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanian  }
5250e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanian  else {
5251432a8893f7e30d141d7f279bd00b741a3cdac81fFariborz Jahanian    llvm::SmallPtrSet<ObjCProtocolDecl *, 8> RHSInheritedProtocols;
5252c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall    Context.CollectInheritedProtocols(RHS->getInterface(),
5253c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall                                      RHSInheritedProtocols);
5254432a8893f7e30d141d7f279bd00b741a3cdac81fFariborz Jahanian    for (llvm::SmallPtrSet<ObjCProtocolDecl*,8>::iterator I =
5255432a8893f7e30d141d7f279bd00b741a3cdac81fFariborz Jahanian         RHSInheritedProtocols.begin(),
5256432a8893f7e30d141d7f279bd00b741a3cdac81fFariborz Jahanian         E = RHSInheritedProtocols.end(); I != E; ++I)
5257432a8893f7e30d141d7f279bd00b741a3cdac81fFariborz Jahanian      if (InheritedProtocolSet.count((*I)))
5258432a8893f7e30d141d7f279bd00b741a3cdac81fFariborz Jahanian        IntersectionOfProtocols.push_back((*I));
5259e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanian  }
5260e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanian}
5261e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanian
5262db07b3f7cdcb505329c1280d7cf70791739a7cadFariborz Jahanian/// areCommonBaseCompatible - Returns common base class of the two classes if
5263db07b3f7cdcb505329c1280d7cf70791739a7cadFariborz Jahanian/// one found. Note that this is O'2 algorithm. But it will be called as the
5264db07b3f7cdcb505329c1280d7cf70791739a7cadFariborz Jahanian/// last type comparison in a ?-exp of ObjC pointer types before a
5265db07b3f7cdcb505329c1280d7cf70791739a7cadFariborz Jahanian/// warning is issued. So, its invokation is extremely rare.
5266db07b3f7cdcb505329c1280d7cf70791739a7cadFariborz JahanianQualType ASTContext::areCommonBaseCompatible(
5267c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall                                          const ObjCObjectPointerType *Lptr,
5268c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall                                          const ObjCObjectPointerType *Rptr) {
5269c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall  const ObjCObjectType *LHS = Lptr->getObjectType();
5270c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall  const ObjCObjectType *RHS = Rptr->getObjectType();
5271c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall  const ObjCInterfaceDecl* LDecl = LHS->getInterface();
5272c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall  const ObjCInterfaceDecl* RDecl = RHS->getInterface();
52737c2bdcb4d30f2d370b4367664e6a11b075ce2cb3Fariborz Jahanian  if (!LDecl || !RDecl || (LDecl == RDecl))
5274db07b3f7cdcb505329c1280d7cf70791739a7cadFariborz Jahanian    return QualType();
5275db07b3f7cdcb505329c1280d7cf70791739a7cadFariborz Jahanian
52767c2bdcb4d30f2d370b4367664e6a11b075ce2cb3Fariborz Jahanian  do {
5277c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall    LHS = cast<ObjCInterfaceType>(getObjCInterfaceType(LDecl));
5278e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanian    if (canAssignObjCInterfaces(LHS, RHS)) {
52795f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner      SmallVector<ObjCProtocolDecl *, 8> Protocols;
5280c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall      getIntersectionOfProtocols(*this, Lptr, Rptr, Protocols);
5281c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall
5282c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall      QualType Result = QualType(LHS, 0);
5283c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall      if (!Protocols.empty())
5284c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall        Result = getObjCObjectType(Result, Protocols.data(), Protocols.size());
5285c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall      Result = getObjCObjectPointerType(Result);
5286c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall      return Result;
5287e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanian    }
52887c2bdcb4d30f2d370b4367664e6a11b075ce2cb3Fariborz Jahanian  } while ((LDecl = LDecl->getSuperClass()));
5289db07b3f7cdcb505329c1280d7cf70791739a7cadFariborz Jahanian
5290db07b3f7cdcb505329c1280d7cf70791739a7cadFariborz Jahanian  return QualType();
5291db07b3f7cdcb505329c1280d7cf70791739a7cadFariborz Jahanian}
5292db07b3f7cdcb505329c1280d7cf70791739a7cadFariborz Jahanian
5293c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCallbool ASTContext::canAssignObjCInterfaces(const ObjCObjectType *LHS,
5294c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall                                         const ObjCObjectType *RHS) {
5295c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall  assert(LHS->getInterface() && "LHS is not an interface type");
5296c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall  assert(RHS->getInterface() && "RHS is not an interface type");
5297c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall
52986ac46a4a521366d7ab36ebe2ce4e624ab96b06f9Chris Lattner  // Verify that the base decls are compatible: the RHS must be a subclass of
52996ac46a4a521366d7ab36ebe2ce4e624ab96b06f9Chris Lattner  // the LHS.
5300c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall  if (!LHS->getInterface()->isSuperClassOf(RHS->getInterface()))
53016ac46a4a521366d7ab36ebe2ce4e624ab96b06f9Chris Lattner    return false;
53021eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
53036ac46a4a521366d7ab36ebe2ce4e624ab96b06f9Chris Lattner  // RHS must have a superset of the protocols in the LHS.  If the LHS is not
53046ac46a4a521366d7ab36ebe2ce4e624ab96b06f9Chris Lattner  // protocol qualified at all, then we are good.
5305c15cb2af27514ecc879daba9aa01389c5203685dSteve Naroff  if (LHS->getNumProtocols() == 0)
53066ac46a4a521366d7ab36ebe2ce4e624ab96b06f9Chris Lattner    return true;
53071eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
5308b7bc34a83aff8af09f2a78aa6d1dcafe18ad8619Fariborz Jahanian  // Okay, we know the LHS has protocol qualifiers.  If the RHS doesn't,
5309b7bc34a83aff8af09f2a78aa6d1dcafe18ad8619Fariborz Jahanian  // more detailed analysis is required.
5310b7bc34a83aff8af09f2a78aa6d1dcafe18ad8619Fariborz Jahanian  if (RHS->getNumProtocols() == 0) {
5311b7bc34a83aff8af09f2a78aa6d1dcafe18ad8619Fariborz Jahanian    // OK, if LHS is a superclass of RHS *and*
5312b7bc34a83aff8af09f2a78aa6d1dcafe18ad8619Fariborz Jahanian    // this superclass is assignment compatible with LHS.
5313b7bc34a83aff8af09f2a78aa6d1dcafe18ad8619Fariborz Jahanian    // false otherwise.
5314627788c29976fbeb4ad47bcfcb3576889070e357Fariborz Jahanian    bool IsSuperClass =
5315627788c29976fbeb4ad47bcfcb3576889070e357Fariborz Jahanian      LHS->getInterface()->isSuperClassOf(RHS->getInterface());
5316627788c29976fbeb4ad47bcfcb3576889070e357Fariborz Jahanian    if (IsSuperClass) {
5317b7bc34a83aff8af09f2a78aa6d1dcafe18ad8619Fariborz Jahanian      // OK if conversion of LHS to SuperClass results in narrowing of types
5318b7bc34a83aff8af09f2a78aa6d1dcafe18ad8619Fariborz Jahanian      // ; i.e., SuperClass may implement at least one of the protocols
5319b7bc34a83aff8af09f2a78aa6d1dcafe18ad8619Fariborz Jahanian      // in LHS's protocol list. Example, SuperObj<P1> = lhs<P1,P2> is ok.
5320b7bc34a83aff8af09f2a78aa6d1dcafe18ad8619Fariborz Jahanian      // But not SuperObj<P1,P2,P3> = lhs<P1,P2>.
5321b7bc34a83aff8af09f2a78aa6d1dcafe18ad8619Fariborz Jahanian      llvm::SmallPtrSet<ObjCProtocolDecl *, 8> SuperClassInheritedProtocols;
5322627788c29976fbeb4ad47bcfcb3576889070e357Fariborz Jahanian      CollectInheritedProtocols(RHS->getInterface(), SuperClassInheritedProtocols);
5323b7bc34a83aff8af09f2a78aa6d1dcafe18ad8619Fariborz Jahanian      // If super class has no protocols, it is not a match.
5324b7bc34a83aff8af09f2a78aa6d1dcafe18ad8619Fariborz Jahanian      if (SuperClassInheritedProtocols.empty())
5325b7bc34a83aff8af09f2a78aa6d1dcafe18ad8619Fariborz Jahanian        return false;
5326b7bc34a83aff8af09f2a78aa6d1dcafe18ad8619Fariborz Jahanian
5327b7bc34a83aff8af09f2a78aa6d1dcafe18ad8619Fariborz Jahanian      for (ObjCObjectType::qual_iterator LHSPI = LHS->qual_begin(),
5328b7bc34a83aff8af09f2a78aa6d1dcafe18ad8619Fariborz Jahanian           LHSPE = LHS->qual_end();
5329b7bc34a83aff8af09f2a78aa6d1dcafe18ad8619Fariborz Jahanian           LHSPI != LHSPE; LHSPI++) {
5330b7bc34a83aff8af09f2a78aa6d1dcafe18ad8619Fariborz Jahanian        bool SuperImplementsProtocol = false;
5331b7bc34a83aff8af09f2a78aa6d1dcafe18ad8619Fariborz Jahanian        ObjCProtocolDecl *LHSProto = (*LHSPI);
5332b7bc34a83aff8af09f2a78aa6d1dcafe18ad8619Fariborz Jahanian
5333b7bc34a83aff8af09f2a78aa6d1dcafe18ad8619Fariborz Jahanian        for (llvm::SmallPtrSet<ObjCProtocolDecl*,8>::iterator I =
5334b7bc34a83aff8af09f2a78aa6d1dcafe18ad8619Fariborz Jahanian             SuperClassInheritedProtocols.begin(),
5335b7bc34a83aff8af09f2a78aa6d1dcafe18ad8619Fariborz Jahanian             E = SuperClassInheritedProtocols.end(); I != E; ++I) {
5336b7bc34a83aff8af09f2a78aa6d1dcafe18ad8619Fariborz Jahanian          ObjCProtocolDecl *SuperClassProto = (*I);
5337b7bc34a83aff8af09f2a78aa6d1dcafe18ad8619Fariborz Jahanian          if (SuperClassProto->lookupProtocolNamed(LHSProto->getIdentifier())) {
5338b7bc34a83aff8af09f2a78aa6d1dcafe18ad8619Fariborz Jahanian            SuperImplementsProtocol = true;
5339b7bc34a83aff8af09f2a78aa6d1dcafe18ad8619Fariborz Jahanian            break;
5340b7bc34a83aff8af09f2a78aa6d1dcafe18ad8619Fariborz Jahanian          }
5341b7bc34a83aff8af09f2a78aa6d1dcafe18ad8619Fariborz Jahanian        }
5342b7bc34a83aff8af09f2a78aa6d1dcafe18ad8619Fariborz Jahanian        if (!SuperImplementsProtocol)
5343b7bc34a83aff8af09f2a78aa6d1dcafe18ad8619Fariborz Jahanian          return false;
5344b7bc34a83aff8af09f2a78aa6d1dcafe18ad8619Fariborz Jahanian      }
5345b7bc34a83aff8af09f2a78aa6d1dcafe18ad8619Fariborz Jahanian      return true;
5346b7bc34a83aff8af09f2a78aa6d1dcafe18ad8619Fariborz Jahanian    }
5347b7bc34a83aff8af09f2a78aa6d1dcafe18ad8619Fariborz Jahanian    return false;
5348b7bc34a83aff8af09f2a78aa6d1dcafe18ad8619Fariborz Jahanian  }
53491eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
5350c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall  for (ObjCObjectType::qual_iterator LHSPI = LHS->qual_begin(),
5351c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall                                     LHSPE = LHS->qual_end();
535291b0b0cf6b537cbcbca0038c7032f87161a41d31Steve Naroff       LHSPI != LHSPE; LHSPI++) {
535391b0b0cf6b537cbcbca0038c7032f87161a41d31Steve Naroff    bool RHSImplementsProtocol = false;
535491b0b0cf6b537cbcbca0038c7032f87161a41d31Steve Naroff
535591b0b0cf6b537cbcbca0038c7032f87161a41d31Steve Naroff    // If the RHS doesn't implement the protocol on the left, the types
535691b0b0cf6b537cbcbca0038c7032f87161a41d31Steve Naroff    // are incompatible.
5357c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall    for (ObjCObjectType::qual_iterator RHSPI = RHS->qual_begin(),
5358c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall                                       RHSPE = RHS->qual_end();
53598f16756441450ed9fb39316e47d107fc2a1ef35bSteve Naroff         RHSPI != RHSPE; RHSPI++) {
53608f16756441450ed9fb39316e47d107fc2a1ef35bSteve Naroff      if ((*RHSPI)->lookupProtocolNamed((*LHSPI)->getIdentifier())) {
536191b0b0cf6b537cbcbca0038c7032f87161a41d31Steve Naroff        RHSImplementsProtocol = true;
53628f16756441450ed9fb39316e47d107fc2a1ef35bSteve Naroff        break;
53638f16756441450ed9fb39316e47d107fc2a1ef35bSteve Naroff      }
536491b0b0cf6b537cbcbca0038c7032f87161a41d31Steve Naroff    }
536591b0b0cf6b537cbcbca0038c7032f87161a41d31Steve Naroff    // FIXME: For better diagnostics, consider passing back the protocol name.
536691b0b0cf6b537cbcbca0038c7032f87161a41d31Steve Naroff    if (!RHSImplementsProtocol)
536791b0b0cf6b537cbcbca0038c7032f87161a41d31Steve Naroff      return false;
536891b0b0cf6b537cbcbca0038c7032f87161a41d31Steve Naroff  }
536991b0b0cf6b537cbcbca0038c7032f87161a41d31Steve Naroff  // The RHS implements all protocols listed on the LHS.
537091b0b0cf6b537cbcbca0038c7032f87161a41d31Steve Naroff  return true;
53716ac46a4a521366d7ab36ebe2ce4e624ab96b06f9Chris Lattner}
53726ac46a4a521366d7ab36ebe2ce4e624ab96b06f9Chris Lattner
5373389bf46ae41241a656ed71b00ac2177d7f385651Steve Naroffbool ASTContext::areComparableObjCPointerTypes(QualType LHS, QualType RHS) {
5374389bf46ae41241a656ed71b00ac2177d7f385651Steve Naroff  // get the "pointed to" types
5375183700f494ec9b6701b6efe82bcb25f4c79ba561John McCall  const ObjCObjectPointerType *LHSOPT = LHS->getAs<ObjCObjectPointerType>();
5376183700f494ec9b6701b6efe82bcb25f4c79ba561John McCall  const ObjCObjectPointerType *RHSOPT = RHS->getAs<ObjCObjectPointerType>();
53771eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
537814108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff  if (!LHSOPT || !RHSOPT)
5379389bf46ae41241a656ed71b00ac2177d7f385651Steve Naroff    return false;
538014108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff
538114108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff  return canAssignObjCInterfaces(LHSOPT, RHSOPT) ||
538214108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff         canAssignObjCInterfaces(RHSOPT, LHSOPT);
5383389bf46ae41241a656ed71b00ac2177d7f385651Steve Naroff}
5384389bf46ae41241a656ed71b00ac2177d7f385651Steve Naroff
5385569c3166874324c24011f8ade6978421f0d39b3cDouglas Gregorbool ASTContext::canBindObjCObjectType(QualType To, QualType From) {
5386569c3166874324c24011f8ade6978421f0d39b3cDouglas Gregor  return canAssignObjCInterfaces(
5387569c3166874324c24011f8ade6978421f0d39b3cDouglas Gregor                getObjCObjectPointerType(To)->getAs<ObjCObjectPointerType>(),
5388569c3166874324c24011f8ade6978421f0d39b3cDouglas Gregor                getObjCObjectPointerType(From)->getAs<ObjCObjectPointerType>());
5389569c3166874324c24011f8ade6978421f0d39b3cDouglas Gregor}
5390569c3166874324c24011f8ade6978421f0d39b3cDouglas Gregor
53911eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// typesAreCompatible - C99 6.7.3p9: For two qualified types to be compatible,
5392ec0550fa3653d46560bf4484a2e988329c228e39Steve Naroff/// both shall have the identically qualified version of a compatible type.
53931eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// C99 6.2.7p1: Two types have compatible types if their types are the
5394ec0550fa3653d46560bf4484a2e988329c228e39Steve Naroff/// same. See 6.7.[2,3,5] for additional rules.
5395447234dd459a00a5ed9b7c3e066162cd7a75bf2dDouglas Gregorbool ASTContext::typesAreCompatible(QualType LHS, QualType RHS,
5396447234dd459a00a5ed9b7c3e066162cd7a75bf2dDouglas Gregor                                    bool CompareUnqualified) {
53970e709abafbd939326850501f795cc7a92c88a354Douglas Gregor  if (getLangOptions().CPlusPlus)
53980e709abafbd939326850501f795cc7a92c88a354Douglas Gregor    return hasSameType(LHS, RHS);
53990e709abafbd939326850501f795cc7a92c88a354Douglas Gregor
5400447234dd459a00a5ed9b7c3e066162cd7a75bf2dDouglas Gregor  return !mergeTypes(LHS, RHS, false, CompareUnqualified).isNull();
54013d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman}
54023d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman
5403c286f3835eb6001c61664cef5d610dfaf80a6e9bFariborz Jahanianbool ASTContext::propertyTypesAreCompatible(QualType LHS, QualType RHS) {
540482378391dfc3b2fc22f63dbf1552bd9aed39dc42Fariborz Jahanian  return typesAreCompatible(LHS, RHS);
5405c286f3835eb6001c61664cef5d610dfaf80a6e9bFariborz Jahanian}
5406c286f3835eb6001c61664cef5d610dfaf80a6e9bFariborz Jahanian
5407132f2a2da34f378fc675b9e174564b0f52c31d98Fariborz Jahanianbool ASTContext::typesAreBlockPointerCompatible(QualType LHS, QualType RHS) {
5408132f2a2da34f378fc675b9e174564b0f52c31d98Fariborz Jahanian  return !mergeTypes(LHS, RHS, true).isNull();
5409132f2a2da34f378fc675b9e174564b0f52c31d98Fariborz Jahanian}
5410132f2a2da34f378fc675b9e174564b0f52c31d98Fariborz Jahanian
54114846675e0e42d1802b0ffd8972a45e72aeb3758dPeter Collingbourne/// mergeTransparentUnionType - if T is a transparent union type and a member
54124846675e0e42d1802b0ffd8972a45e72aeb3758dPeter Collingbourne/// of T is compatible with SubType, return the merged type, else return
54134846675e0e42d1802b0ffd8972a45e72aeb3758dPeter Collingbourne/// QualType()
54144846675e0e42d1802b0ffd8972a45e72aeb3758dPeter CollingbourneQualType ASTContext::mergeTransparentUnionType(QualType T, QualType SubType,
54154846675e0e42d1802b0ffd8972a45e72aeb3758dPeter Collingbourne                                               bool OfBlockPointer,
54164846675e0e42d1802b0ffd8972a45e72aeb3758dPeter Collingbourne                                               bool Unqualified) {
54174846675e0e42d1802b0ffd8972a45e72aeb3758dPeter Collingbourne  if (const RecordType *UT = T->getAsUnionType()) {
54184846675e0e42d1802b0ffd8972a45e72aeb3758dPeter Collingbourne    RecordDecl *UD = UT->getDecl();
54194846675e0e42d1802b0ffd8972a45e72aeb3758dPeter Collingbourne    if (UD->hasAttr<TransparentUnionAttr>()) {
54204846675e0e42d1802b0ffd8972a45e72aeb3758dPeter Collingbourne      for (RecordDecl::field_iterator it = UD->field_begin(),
54214846675e0e42d1802b0ffd8972a45e72aeb3758dPeter Collingbourne           itend = UD->field_end(); it != itend; ++it) {
5422f91d7572ee7fa6f2926c8e7e816039a1154a59f8Peter Collingbourne        QualType ET = it->getType().getUnqualifiedType();
54234846675e0e42d1802b0ffd8972a45e72aeb3758dPeter Collingbourne        QualType MT = mergeTypes(ET, SubType, OfBlockPointer, Unqualified);
54244846675e0e42d1802b0ffd8972a45e72aeb3758dPeter Collingbourne        if (!MT.isNull())
54254846675e0e42d1802b0ffd8972a45e72aeb3758dPeter Collingbourne          return MT;
54264846675e0e42d1802b0ffd8972a45e72aeb3758dPeter Collingbourne      }
54274846675e0e42d1802b0ffd8972a45e72aeb3758dPeter Collingbourne    }
54284846675e0e42d1802b0ffd8972a45e72aeb3758dPeter Collingbourne  }
54294846675e0e42d1802b0ffd8972a45e72aeb3758dPeter Collingbourne
54304846675e0e42d1802b0ffd8972a45e72aeb3758dPeter Collingbourne  return QualType();
54314846675e0e42d1802b0ffd8972a45e72aeb3758dPeter Collingbourne}
54324846675e0e42d1802b0ffd8972a45e72aeb3758dPeter Collingbourne
54334846675e0e42d1802b0ffd8972a45e72aeb3758dPeter Collingbourne/// mergeFunctionArgumentTypes - merge two types which appear as function
54344846675e0e42d1802b0ffd8972a45e72aeb3758dPeter Collingbourne/// argument types
54354846675e0e42d1802b0ffd8972a45e72aeb3758dPeter CollingbourneQualType ASTContext::mergeFunctionArgumentTypes(QualType lhs, QualType rhs,
54364846675e0e42d1802b0ffd8972a45e72aeb3758dPeter Collingbourne                                                bool OfBlockPointer,
54374846675e0e42d1802b0ffd8972a45e72aeb3758dPeter Collingbourne                                                bool Unqualified) {
54384846675e0e42d1802b0ffd8972a45e72aeb3758dPeter Collingbourne  // GNU extension: two types are compatible if they appear as a function
54394846675e0e42d1802b0ffd8972a45e72aeb3758dPeter Collingbourne  // argument, one of the types is a transparent union type and the other
54404846675e0e42d1802b0ffd8972a45e72aeb3758dPeter Collingbourne  // type is compatible with a union member
54414846675e0e42d1802b0ffd8972a45e72aeb3758dPeter Collingbourne  QualType lmerge = mergeTransparentUnionType(lhs, rhs, OfBlockPointer,
54424846675e0e42d1802b0ffd8972a45e72aeb3758dPeter Collingbourne                                              Unqualified);
54434846675e0e42d1802b0ffd8972a45e72aeb3758dPeter Collingbourne  if (!lmerge.isNull())
54444846675e0e42d1802b0ffd8972a45e72aeb3758dPeter Collingbourne    return lmerge;
54454846675e0e42d1802b0ffd8972a45e72aeb3758dPeter Collingbourne
54464846675e0e42d1802b0ffd8972a45e72aeb3758dPeter Collingbourne  QualType rmerge = mergeTransparentUnionType(rhs, lhs, OfBlockPointer,
54474846675e0e42d1802b0ffd8972a45e72aeb3758dPeter Collingbourne                                              Unqualified);
54484846675e0e42d1802b0ffd8972a45e72aeb3758dPeter Collingbourne  if (!rmerge.isNull())
54494846675e0e42d1802b0ffd8972a45e72aeb3758dPeter Collingbourne    return rmerge;
54504846675e0e42d1802b0ffd8972a45e72aeb3758dPeter Collingbourne
54514846675e0e42d1802b0ffd8972a45e72aeb3758dPeter Collingbourne  return mergeTypes(lhs, rhs, OfBlockPointer, Unqualified);
54524846675e0e42d1802b0ffd8972a45e72aeb3758dPeter Collingbourne}
54534846675e0e42d1802b0ffd8972a45e72aeb3758dPeter Collingbourne
5454132f2a2da34f378fc675b9e174564b0f52c31d98Fariborz JahanianQualType ASTContext::mergeFunctionTypes(QualType lhs, QualType rhs,
5455447234dd459a00a5ed9b7c3e066162cd7a75bf2dDouglas Gregor                                        bool OfBlockPointer,
5456447234dd459a00a5ed9b7c3e066162cd7a75bf2dDouglas Gregor                                        bool Unqualified) {
5457183700f494ec9b6701b6efe82bcb25f4c79ba561John McCall  const FunctionType *lbase = lhs->getAs<FunctionType>();
5458183700f494ec9b6701b6efe82bcb25f4c79ba561John McCall  const FunctionType *rbase = rhs->getAs<FunctionType>();
545972564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor  const FunctionProtoType *lproto = dyn_cast<FunctionProtoType>(lbase);
546072564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor  const FunctionProtoType *rproto = dyn_cast<FunctionProtoType>(rbase);
54613d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman  bool allLTypes = true;
54623d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman  bool allRTypes = true;
54633d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman
54643d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman  // Check return type
5465132f2a2da34f378fc675b9e174564b0f52c31d98Fariborz Jahanian  QualType retType;
5466d263fd1451299b1e5f5f1acb2bb13b0a4119aee8Fariborz Jahanian  if (OfBlockPointer) {
5467d263fd1451299b1e5f5f1acb2bb13b0a4119aee8Fariborz Jahanian    QualType RHS = rbase->getResultType();
5468d263fd1451299b1e5f5f1acb2bb13b0a4119aee8Fariborz Jahanian    QualType LHS = lbase->getResultType();
5469d263fd1451299b1e5f5f1acb2bb13b0a4119aee8Fariborz Jahanian    bool UnqualifiedResult = Unqualified;
5470d263fd1451299b1e5f5f1acb2bb13b0a4119aee8Fariborz Jahanian    if (!UnqualifiedResult)
5471d263fd1451299b1e5f5f1acb2bb13b0a4119aee8Fariborz Jahanian      UnqualifiedResult = (!RHS.hasQualifiers() && LHS.hasQualifiers());
5472a4fdbfad150ae37bddaa4094d3925a27a1a1cf3fFariborz Jahanian    retType = mergeTypes(LHS, RHS, true, UnqualifiedResult, true);
5473d263fd1451299b1e5f5f1acb2bb13b0a4119aee8Fariborz Jahanian  }
5474132f2a2da34f378fc675b9e174564b0f52c31d98Fariborz Jahanian  else
54758cc246c9a68c783a5b90d2e8b8927521cb3a49b7John McCall    retType = mergeTypes(lbase->getResultType(), rbase->getResultType(), false,
54768cc246c9a68c783a5b90d2e8b8927521cb3a49b7John McCall                         Unqualified);
54773d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman  if (retType.isNull()) return QualType();
5478447234dd459a00a5ed9b7c3e066162cd7a75bf2dDouglas Gregor
5479447234dd459a00a5ed9b7c3e066162cd7a75bf2dDouglas Gregor  if (Unqualified)
5480447234dd459a00a5ed9b7c3e066162cd7a75bf2dDouglas Gregor    retType = retType.getUnqualifiedType();
5481447234dd459a00a5ed9b7c3e066162cd7a75bf2dDouglas Gregor
5482447234dd459a00a5ed9b7c3e066162cd7a75bf2dDouglas Gregor  CanQualType LRetType = getCanonicalType(lbase->getResultType());
5483447234dd459a00a5ed9b7c3e066162cd7a75bf2dDouglas Gregor  CanQualType RRetType = getCanonicalType(rbase->getResultType());
5484447234dd459a00a5ed9b7c3e066162cd7a75bf2dDouglas Gregor  if (Unqualified) {
5485447234dd459a00a5ed9b7c3e066162cd7a75bf2dDouglas Gregor    LRetType = LRetType.getUnqualifiedType();
5486447234dd459a00a5ed9b7c3e066162cd7a75bf2dDouglas Gregor    RRetType = RRetType.getUnqualifiedType();
5487447234dd459a00a5ed9b7c3e066162cd7a75bf2dDouglas Gregor  }
5488447234dd459a00a5ed9b7c3e066162cd7a75bf2dDouglas Gregor
5489447234dd459a00a5ed9b7c3e066162cd7a75bf2dDouglas Gregor  if (getCanonicalType(retType) != LRetType)
549061710854be2b098428aff5316e64bd34b30fbcb7Chris Lattner    allLTypes = false;
5491447234dd459a00a5ed9b7c3e066162cd7a75bf2dDouglas Gregor  if (getCanonicalType(retType) != RRetType)
549261710854be2b098428aff5316e64bd34b30fbcb7Chris Lattner    allRTypes = false;
54938cc246c9a68c783a5b90d2e8b8927521cb3a49b7John McCall
54946a15c8586cc3e5d6e01c4c4e8b4374584569b58fDaniel Dunbar  // FIXME: double check this
54956a15c8586cc3e5d6e01c4c4e8b4374584569b58fDaniel Dunbar  // FIXME: should we error if lbase->getRegParmAttr() != 0 &&
54966a15c8586cc3e5d6e01c4c4e8b4374584569b58fDaniel Dunbar  //                           rbase->getRegParmAttr() != 0 &&
54976a15c8586cc3e5d6e01c4c4e8b4374584569b58fDaniel Dunbar  //                           lbase->getRegParmAttr() != rbase->getRegParmAttr()?
5498264ba48dc98f3f843935a485d5b086f7e0fdc4f1Rafael Espindola  FunctionType::ExtInfo lbaseInfo = lbase->getExtInfo();
5499264ba48dc98f3f843935a485d5b086f7e0fdc4f1Rafael Espindola  FunctionType::ExtInfo rbaseInfo = rbase->getExtInfo();
55008cc246c9a68c783a5b90d2e8b8927521cb3a49b7John McCall
55018cc246c9a68c783a5b90d2e8b8927521cb3a49b7John McCall  // Compatible functions must have compatible calling conventions
55028cc246c9a68c783a5b90d2e8b8927521cb3a49b7John McCall  if (!isSameCallConv(lbaseInfo.getCC(), rbaseInfo.getCC()))
55038cc246c9a68c783a5b90d2e8b8927521cb3a49b7John McCall    return QualType();
55048cc246c9a68c783a5b90d2e8b8927521cb3a49b7John McCall
55058cc246c9a68c783a5b90d2e8b8927521cb3a49b7John McCall  // Regparm is part of the calling convention.
5506a49218e17bcbb1acde0245773173e2c0c42f4f19Eli Friedman  if (lbaseInfo.getHasRegParm() != rbaseInfo.getHasRegParm())
5507a49218e17bcbb1acde0245773173e2c0c42f4f19Eli Friedman    return QualType();
55088cc246c9a68c783a5b90d2e8b8927521cb3a49b7John McCall  if (lbaseInfo.getRegParm() != rbaseInfo.getRegParm())
55098cc246c9a68c783a5b90d2e8b8927521cb3a49b7John McCall    return QualType();
55108cc246c9a68c783a5b90d2e8b8927521cb3a49b7John McCall
5511f85e193739c953358c865005855253af4f68a497John McCall  if (lbaseInfo.getProducesResult() != rbaseInfo.getProducesResult())
5512f85e193739c953358c865005855253af4f68a497John McCall    return QualType();
5513f85e193739c953358c865005855253af4f68a497John McCall
55148cc246c9a68c783a5b90d2e8b8927521cb3a49b7John McCall  // It's noreturn if either type is.
55158cc246c9a68c783a5b90d2e8b8927521cb3a49b7John McCall  // FIXME: some uses, e.g. conditional exprs, really want this to be 'both'.
55166a15c8586cc3e5d6e01c4c4e8b4374584569b58fDaniel Dunbar  bool NoReturn = lbaseInfo.getNoReturn() || rbaseInfo.getNoReturn();
55178cc246c9a68c783a5b90d2e8b8927521cb3a49b7John McCall  if (NoReturn != lbaseInfo.getNoReturn())
55186a15c8586cc3e5d6e01c4c4e8b4374584569b58fDaniel Dunbar    allLTypes = false;
55198cc246c9a68c783a5b90d2e8b8927521cb3a49b7John McCall  if (NoReturn != rbaseInfo.getNoReturn())
55206a15c8586cc3e5d6e01c4c4e8b4374584569b58fDaniel Dunbar    allRTypes = false;
55211eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
5522f85e193739c953358c865005855253af4f68a497John McCall  FunctionType::ExtInfo einfo = lbaseInfo.withNoReturn(NoReturn);
5523e23cf437fe76b1ed02d63c3f61b456fd48a915f5John McCall
55243d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman  if (lproto && rproto) { // two C99 style function prototypes
5525465226e23a3008bd68973513dda1f9e3cd27dbddSebastian Redl    assert(!lproto->hasExceptionSpec() && !rproto->hasExceptionSpec() &&
5526465226e23a3008bd68973513dda1f9e3cd27dbddSebastian Redl           "C++ shouldn't be here");
55273d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    unsigned lproto_nargs = lproto->getNumArgs();
55283d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    unsigned rproto_nargs = rproto->getNumArgs();
55293d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman
55303d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    // Compatible functions must have the same number of arguments
55313d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    if (lproto_nargs != rproto_nargs)
55323d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman      return QualType();
55333d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman
55343d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    // Variadic and non-variadic functions aren't compatible
55353d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    if (lproto->isVariadic() != rproto->isVariadic())
55363d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman      return QualType();
55373d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman
55387fb5e4888221cd36652d078c6b171ac55e7f406dArgyrios Kyrtzidis    if (lproto->getTypeQuals() != rproto->getTypeQuals())
55397fb5e4888221cd36652d078c6b171ac55e7f406dArgyrios Kyrtzidis      return QualType();
55407fb5e4888221cd36652d078c6b171ac55e7f406dArgyrios Kyrtzidis
55413d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    // Check argument compatibility
55425f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner    SmallVector<QualType, 10> types;
55433d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    for (unsigned i = 0; i < lproto_nargs; i++) {
55443d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman      QualType largtype = lproto->getArgType(i).getUnqualifiedType();
55453d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman      QualType rargtype = rproto->getArgType(i).getUnqualifiedType();
55464846675e0e42d1802b0ffd8972a45e72aeb3758dPeter Collingbourne      QualType argtype = mergeFunctionArgumentTypes(largtype, rargtype,
55474846675e0e42d1802b0ffd8972a45e72aeb3758dPeter Collingbourne                                                    OfBlockPointer,
55484846675e0e42d1802b0ffd8972a45e72aeb3758dPeter Collingbourne                                                    Unqualified);
55493d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman      if (argtype.isNull()) return QualType();
5550447234dd459a00a5ed9b7c3e066162cd7a75bf2dDouglas Gregor
5551447234dd459a00a5ed9b7c3e066162cd7a75bf2dDouglas Gregor      if (Unqualified)
5552447234dd459a00a5ed9b7c3e066162cd7a75bf2dDouglas Gregor        argtype = argtype.getUnqualifiedType();
5553447234dd459a00a5ed9b7c3e066162cd7a75bf2dDouglas Gregor
55543d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman      types.push_back(argtype);
5555447234dd459a00a5ed9b7c3e066162cd7a75bf2dDouglas Gregor      if (Unqualified) {
5556447234dd459a00a5ed9b7c3e066162cd7a75bf2dDouglas Gregor        largtype = largtype.getUnqualifiedType();
5557447234dd459a00a5ed9b7c3e066162cd7a75bf2dDouglas Gregor        rargtype = rargtype.getUnqualifiedType();
5558447234dd459a00a5ed9b7c3e066162cd7a75bf2dDouglas Gregor      }
5559447234dd459a00a5ed9b7c3e066162cd7a75bf2dDouglas Gregor
556061710854be2b098428aff5316e64bd34b30fbcb7Chris Lattner      if (getCanonicalType(argtype) != getCanonicalType(largtype))
556161710854be2b098428aff5316e64bd34b30fbcb7Chris Lattner        allLTypes = false;
556261710854be2b098428aff5316e64bd34b30fbcb7Chris Lattner      if (getCanonicalType(argtype) != getCanonicalType(rargtype))
556361710854be2b098428aff5316e64bd34b30fbcb7Chris Lattner        allRTypes = false;
55643d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    }
55653d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    if (allLTypes) return lhs;
55663d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    if (allRTypes) return rhs;
5567e23cf437fe76b1ed02d63c3f61b456fd48a915f5John McCall
5568e23cf437fe76b1ed02d63c3f61b456fd48a915f5John McCall    FunctionProtoType::ExtProtoInfo EPI = lproto->getExtProtoInfo();
5569e23cf437fe76b1ed02d63c3f61b456fd48a915f5John McCall    EPI.ExtInfo = einfo;
5570e23cf437fe76b1ed02d63c3f61b456fd48a915f5John McCall    return getFunctionType(retType, types.begin(), types.size(), EPI);
55713d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman  }
55723d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman
55733d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman  if (lproto) allRTypes = false;
55743d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman  if (rproto) allLTypes = false;
55753d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman
557672564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor  const FunctionProtoType *proto = lproto ? lproto : rproto;
55773d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman  if (proto) {
5578465226e23a3008bd68973513dda1f9e3cd27dbddSebastian Redl    assert(!proto->hasExceptionSpec() && "C++ shouldn't be here");
55793d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    if (proto->isVariadic()) return QualType();
55803d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    // Check that the types are compatible with the types that
55813d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    // would result from default argument promotions (C99 6.7.5.3p15).
55823d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    // The only types actually affected are promotable integer
55833d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    // types and floats, which would be passed as a different
55843d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    // type depending on whether the prototype is visible.
55853d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    unsigned proto_nargs = proto->getNumArgs();
55863d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    for (unsigned i = 0; i < proto_nargs; ++i) {
55873d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman      QualType argTy = proto->getArgType(i);
5588b0f8eacfdcd0d43f51e669f2d723992d4af9f746Douglas Gregor
5589b0f8eacfdcd0d43f51e669f2d723992d4af9f746Douglas Gregor      // Look at the promotion type of enum types, since that is the type used
5590b0f8eacfdcd0d43f51e669f2d723992d4af9f746Douglas Gregor      // to pass enum values.
5591b0f8eacfdcd0d43f51e669f2d723992d4af9f746Douglas Gregor      if (const EnumType *Enum = argTy->getAs<EnumType>())
5592b0f8eacfdcd0d43f51e669f2d723992d4af9f746Douglas Gregor        argTy = Enum->getDecl()->getPromotionType();
5593b0f8eacfdcd0d43f51e669f2d723992d4af9f746Douglas Gregor
55943d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman      if (argTy->isPromotableIntegerType() ||
55953d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman          getCanonicalType(argTy).getUnqualifiedType() == FloatTy)
55963d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman        return QualType();
55973d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    }
55983d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman
55993d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    if (allLTypes) return lhs;
56003d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    if (allRTypes) return rhs;
5601e23cf437fe76b1ed02d63c3f61b456fd48a915f5John McCall
5602e23cf437fe76b1ed02d63c3f61b456fd48a915f5John McCall    FunctionProtoType::ExtProtoInfo EPI = proto->getExtProtoInfo();
5603e23cf437fe76b1ed02d63c3f61b456fd48a915f5John McCall    EPI.ExtInfo = einfo;
56043d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    return getFunctionType(retType, proto->arg_type_begin(),
5605e23cf437fe76b1ed02d63c3f61b456fd48a915f5John McCall                           proto->getNumArgs(), EPI);
56063d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman  }
56073d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman
56083d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman  if (allLTypes) return lhs;
56093d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman  if (allRTypes) return rhs;
56108cc246c9a68c783a5b90d2e8b8927521cb3a49b7John McCall  return getFunctionNoProtoType(retType, einfo);
56113d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman}
56123d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman
5613132f2a2da34f378fc675b9e174564b0f52c31d98Fariborz JahanianQualType ASTContext::mergeTypes(QualType LHS, QualType RHS,
5614447234dd459a00a5ed9b7c3e066162cd7a75bf2dDouglas Gregor                                bool OfBlockPointer,
5615a4fdbfad150ae37bddaa4094d3925a27a1a1cf3fFariborz Jahanian                                bool Unqualified, bool BlockReturnType) {
561643d69750e7f7b26076e7474dec8839bb777b260fBill Wendling  // C++ [expr]: If an expression initially has the type "reference to T", the
561743d69750e7f7b26076e7474dec8839bb777b260fBill Wendling  // type is adjusted to "T" prior to any further analysis, the expression
561843d69750e7f7b26076e7474dec8839bb777b260fBill Wendling  // designates the object or function denoted by the reference, and the
56197c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl  // expression is an lvalue unless the reference is an rvalue reference and
56207c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl  // the expression is a function call (possibly inside parentheses).
56210e709abafbd939326850501f795cc7a92c88a354Douglas Gregor  assert(!LHS->getAs<ReferenceType>() && "LHS is a reference type?");
56220e709abafbd939326850501f795cc7a92c88a354Douglas Gregor  assert(!RHS->getAs<ReferenceType>() && "RHS is a reference type?");
5623447234dd459a00a5ed9b7c3e066162cd7a75bf2dDouglas Gregor
5624447234dd459a00a5ed9b7c3e066162cd7a75bf2dDouglas Gregor  if (Unqualified) {
5625447234dd459a00a5ed9b7c3e066162cd7a75bf2dDouglas Gregor    LHS = LHS.getUnqualifiedType();
5626447234dd459a00a5ed9b7c3e066162cd7a75bf2dDouglas Gregor    RHS = RHS.getUnqualifiedType();
5627447234dd459a00a5ed9b7c3e066162cd7a75bf2dDouglas Gregor  }
56280e709abafbd939326850501f795cc7a92c88a354Douglas Gregor
56293d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman  QualType LHSCan = getCanonicalType(LHS),
56303d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman           RHSCan = getCanonicalType(RHS);
56313d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman
5632f3692dc4a47dc48d10cec0415c6e9e39b7a39707Chris Lattner  // If two types are identical, they are compatible.
56333d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman  if (LHSCan == RHSCan)
56343d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    return LHS;
56353d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman
56360953e767ff7817f97b3ab20896b229891eeff45bJohn McCall  // If the qualifiers are different, the types aren't compatible... mostly.
5637a4923eb7c4b04d360cb2747641a5e92818edf804Douglas Gregor  Qualifiers LQuals = LHSCan.getLocalQualifiers();
5638a4923eb7c4b04d360cb2747641a5e92818edf804Douglas Gregor  Qualifiers RQuals = RHSCan.getLocalQualifiers();
56390953e767ff7817f97b3ab20896b229891eeff45bJohn McCall  if (LQuals != RQuals) {
56400953e767ff7817f97b3ab20896b229891eeff45bJohn McCall    // If any of these qualifiers are different, we have a type
56410953e767ff7817f97b3ab20896b229891eeff45bJohn McCall    // mismatch.
56420953e767ff7817f97b3ab20896b229891eeff45bJohn McCall    if (LQuals.getCVRQualifiers() != RQuals.getCVRQualifiers() ||
5643f85e193739c953358c865005855253af4f68a497John McCall        LQuals.getAddressSpace() != RQuals.getAddressSpace() ||
5644f85e193739c953358c865005855253af4f68a497John McCall        LQuals.getObjCLifetime() != RQuals.getObjCLifetime())
56450953e767ff7817f97b3ab20896b229891eeff45bJohn McCall      return QualType();
56460953e767ff7817f97b3ab20896b229891eeff45bJohn McCall
56470953e767ff7817f97b3ab20896b229891eeff45bJohn McCall    // Exactly one GC qualifier difference is allowed: __strong is
56480953e767ff7817f97b3ab20896b229891eeff45bJohn McCall    // okay if the other type has no GC qualifier but is an Objective
56490953e767ff7817f97b3ab20896b229891eeff45bJohn McCall    // C object pointer (i.e. implicitly strong by default).  We fix
56500953e767ff7817f97b3ab20896b229891eeff45bJohn McCall    // this by pretending that the unqualified type was actually
56510953e767ff7817f97b3ab20896b229891eeff45bJohn McCall    // qualified __strong.
56520953e767ff7817f97b3ab20896b229891eeff45bJohn McCall    Qualifiers::GC GC_L = LQuals.getObjCGCAttr();
56530953e767ff7817f97b3ab20896b229891eeff45bJohn McCall    Qualifiers::GC GC_R = RQuals.getObjCGCAttr();
56540953e767ff7817f97b3ab20896b229891eeff45bJohn McCall    assert((GC_L != GC_R) && "unequal qualifier sets had only equal elements");
56550953e767ff7817f97b3ab20896b229891eeff45bJohn McCall
56560953e767ff7817f97b3ab20896b229891eeff45bJohn McCall    if (GC_L == Qualifiers::Weak || GC_R == Qualifiers::Weak)
56570953e767ff7817f97b3ab20896b229891eeff45bJohn McCall      return QualType();
56580953e767ff7817f97b3ab20896b229891eeff45bJohn McCall
56590953e767ff7817f97b3ab20896b229891eeff45bJohn McCall    if (GC_L == Qualifiers::Strong && RHSCan->isObjCObjectPointerType()) {
56600953e767ff7817f97b3ab20896b229891eeff45bJohn McCall      return mergeTypes(LHS, getObjCGCQualType(RHS, Qualifiers::Strong));
56610953e767ff7817f97b3ab20896b229891eeff45bJohn McCall    }
56620953e767ff7817f97b3ab20896b229891eeff45bJohn McCall    if (GC_R == Qualifiers::Strong && LHSCan->isObjCObjectPointerType()) {
56630953e767ff7817f97b3ab20896b229891eeff45bJohn McCall      return mergeTypes(getObjCGCQualType(LHS, Qualifiers::Strong), RHS);
56640953e767ff7817f97b3ab20896b229891eeff45bJohn McCall    }
56653d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    return QualType();
56660953e767ff7817f97b3ab20896b229891eeff45bJohn McCall  }
56670953e767ff7817f97b3ab20896b229891eeff45bJohn McCall
56680953e767ff7817f97b3ab20896b229891eeff45bJohn McCall  // Okay, qualifiers are equal.
56693d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman
5670852d63b806c5cbd730c6b9d696e2e27d02546b49Eli Friedman  Type::TypeClass LHSClass = LHSCan->getTypeClass();
5671852d63b806c5cbd730c6b9d696e2e27d02546b49Eli Friedman  Type::TypeClass RHSClass = RHSCan->getTypeClass();
5672f3692dc4a47dc48d10cec0415c6e9e39b7a39707Chris Lattner
56731adb88370beab45af2f065afe86b51ccd59ec50dChris Lattner  // We want to consider the two function types to be the same for these
56741adb88370beab45af2f065afe86b51ccd59ec50dChris Lattner  // comparisons, just force one to the other.
56751adb88370beab45af2f065afe86b51ccd59ec50dChris Lattner  if (LHSClass == Type::FunctionProto) LHSClass = Type::FunctionNoProto;
56761adb88370beab45af2f065afe86b51ccd59ec50dChris Lattner  if (RHSClass == Type::FunctionProto) RHSClass = Type::FunctionNoProto;
56774c721d381fb279899337d120edd4a24d405e56b2Eli Friedman
56784c721d381fb279899337d120edd4a24d405e56b2Eli Friedman  // Same as above for arrays
5679a36a61f218b9f7a97f2c0f511e0b29eb42e8f78bChris Lattner  if (LHSClass == Type::VariableArray || LHSClass == Type::IncompleteArray)
5680a36a61f218b9f7a97f2c0f511e0b29eb42e8f78bChris Lattner    LHSClass = Type::ConstantArray;
5681a36a61f218b9f7a97f2c0f511e0b29eb42e8f78bChris Lattner  if (RHSClass == Type::VariableArray || RHSClass == Type::IncompleteArray)
5682a36a61f218b9f7a97f2c0f511e0b29eb42e8f78bChris Lattner    RHSClass = Type::ConstantArray;
56831eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
5684c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall  // ObjCInterfaces are just specialized ObjCObjects.
5685c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall  if (LHSClass == Type::ObjCInterface) LHSClass = Type::ObjCObject;
5686c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall  if (RHSClass == Type::ObjCInterface) RHSClass = Type::ObjCObject;
5687c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall
5688213541a68a3e137d11d2cefb612c6cdb410d7e8eNate Begeman  // Canonicalize ExtVector -> Vector.
5689213541a68a3e137d11d2cefb612c6cdb410d7e8eNate Begeman  if (LHSClass == Type::ExtVector) LHSClass = Type::Vector;
5690213541a68a3e137d11d2cefb612c6cdb410d7e8eNate Begeman  if (RHSClass == Type::ExtVector) RHSClass = Type::Vector;
56911eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
56924e78fd0a960eaa7e97467f2e8f390f3a57da279bSteve Naroff  // If the canonical type classes don't match.
56934e78fd0a960eaa7e97467f2e8f390f3a57da279bSteve Naroff  if (LHSClass != RHSClass) {
56941adb88370beab45af2f065afe86b51ccd59ec50dChris Lattner    // C99 6.7.2.2p4: Each enumerated type shall be compatible with char,
56951eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    // a signed integer type, or an unsigned integer type.
5696842aef8d942a880eeb9535d40de31a86838264cbJohn McCall    // Compatibility is based on the underlying type, not the promotion
5697842aef8d942a880eeb9535d40de31a86838264cbJohn McCall    // type.
5698183700f494ec9b6701b6efe82bcb25f4c79ba561John McCall    if (const EnumType* ETy = LHS->getAs<EnumType>()) {
56993d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman      if (ETy->getDecl()->getIntegerType() == RHSCan.getUnqualifiedType())
57003d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman        return RHS;
5701bab96968886f4b77083f4e26a28986ddb1e42d67Eli Friedman    }
5702183700f494ec9b6701b6efe82bcb25f4c79ba561John McCall    if (const EnumType* ETy = RHS->getAs<EnumType>()) {
57033d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman      if (ETy->getDecl()->getIntegerType() == LHSCan.getUnqualifiedType())
57043d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman        return LHS;
5705bab96968886f4b77083f4e26a28986ddb1e42d67Eli Friedman    }
57061adb88370beab45af2f065afe86b51ccd59ec50dChris Lattner
57073d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    return QualType();
5708ec0550fa3653d46560bf4484a2e988329c228e39Steve Naroff  }
57093d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman
57104a74678ed6c3dedac05d02b1ee341f1db869f049Steve Naroff  // The canonical type classes match.
57111adb88370beab45af2f065afe86b51ccd59ec50dChris Lattner  switch (LHSClass) {
571272564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor#define TYPE(Class, Base)
571372564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor#define ABSTRACT_TYPE(Class, Base)
5714ad5e73887052193afda72db8efcb812bd083a4a8John McCall#define NON_CANONICAL_UNLESS_DEPENDENT_TYPE(Class, Base) case Type::Class:
571572564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor#define NON_CANONICAL_TYPE(Class, Base) case Type::Class:
571672564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor#define DEPENDENT_TYPE(Class, Base) case Type::Class:
571772564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor#include "clang/AST/TypeNodes.def"
571872564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor    assert(false && "Non-canonical and dependent types shouldn't get here");
571972564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor    return QualType();
572072564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor
57217c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl  case Type::LValueReference:
57227c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl  case Type::RValueReference:
572372564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor  case Type::MemberPointer:
572472564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor    assert(false && "C++ should never be in mergeTypes");
572572564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor    return QualType();
572672564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor
5727c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall  case Type::ObjCInterface:
572872564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor  case Type::IncompleteArray:
572972564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor  case Type::VariableArray:
573072564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor  case Type::FunctionProto:
573172564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor  case Type::ExtVector:
573272564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor    assert(false && "Types are eliminated above");
573372564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor    return QualType();
573472564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor
57351adb88370beab45af2f065afe86b51ccd59ec50dChris Lattner  case Type::Pointer:
57363d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman  {
57373d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    // Merge two pointer types, while trying to preserve typedef info
57386217b80b7a1379b74cced1c076338262c3c980b3Ted Kremenek    QualType LHSPointee = LHS->getAs<PointerType>()->getPointeeType();
57396217b80b7a1379b74cced1c076338262c3c980b3Ted Kremenek    QualType RHSPointee = RHS->getAs<PointerType>()->getPointeeType();
5740447234dd459a00a5ed9b7c3e066162cd7a75bf2dDouglas Gregor    if (Unqualified) {
5741447234dd459a00a5ed9b7c3e066162cd7a75bf2dDouglas Gregor      LHSPointee = LHSPointee.getUnqualifiedType();
5742447234dd459a00a5ed9b7c3e066162cd7a75bf2dDouglas Gregor      RHSPointee = RHSPointee.getUnqualifiedType();
5743447234dd459a00a5ed9b7c3e066162cd7a75bf2dDouglas Gregor    }
5744447234dd459a00a5ed9b7c3e066162cd7a75bf2dDouglas Gregor    QualType ResultType = mergeTypes(LHSPointee, RHSPointee, false,
5745447234dd459a00a5ed9b7c3e066162cd7a75bf2dDouglas Gregor                                     Unqualified);
57463d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    if (ResultType.isNull()) return QualType();
574707d258756dc856c6987c394a0972884e6ed46765Eli Friedman    if (getCanonicalType(LHSPointee) == getCanonicalType(ResultType))
574861710854be2b098428aff5316e64bd34b30fbcb7Chris Lattner      return LHS;
574907d258756dc856c6987c394a0972884e6ed46765Eli Friedman    if (getCanonicalType(RHSPointee) == getCanonicalType(ResultType))
575061710854be2b098428aff5316e64bd34b30fbcb7Chris Lattner      return RHS;
57513d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    return getPointerType(ResultType);
57523d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman  }
5753c0febd58f5cbf4a93fd12f461863564dba0af76dSteve Naroff  case Type::BlockPointer:
5754c0febd58f5cbf4a93fd12f461863564dba0af76dSteve Naroff  {
5755c0febd58f5cbf4a93fd12f461863564dba0af76dSteve Naroff    // Merge two block pointer types, while trying to preserve typedef info
57566217b80b7a1379b74cced1c076338262c3c980b3Ted Kremenek    QualType LHSPointee = LHS->getAs<BlockPointerType>()->getPointeeType();
57576217b80b7a1379b74cced1c076338262c3c980b3Ted Kremenek    QualType RHSPointee = RHS->getAs<BlockPointerType>()->getPointeeType();
5758447234dd459a00a5ed9b7c3e066162cd7a75bf2dDouglas Gregor    if (Unqualified) {
5759447234dd459a00a5ed9b7c3e066162cd7a75bf2dDouglas Gregor      LHSPointee = LHSPointee.getUnqualifiedType();
5760447234dd459a00a5ed9b7c3e066162cd7a75bf2dDouglas Gregor      RHSPointee = RHSPointee.getUnqualifiedType();
5761447234dd459a00a5ed9b7c3e066162cd7a75bf2dDouglas Gregor    }
5762447234dd459a00a5ed9b7c3e066162cd7a75bf2dDouglas Gregor    QualType ResultType = mergeTypes(LHSPointee, RHSPointee, OfBlockPointer,
5763447234dd459a00a5ed9b7c3e066162cd7a75bf2dDouglas Gregor                                     Unqualified);
5764c0febd58f5cbf4a93fd12f461863564dba0af76dSteve Naroff    if (ResultType.isNull()) return QualType();
5765c0febd58f5cbf4a93fd12f461863564dba0af76dSteve Naroff    if (getCanonicalType(LHSPointee) == getCanonicalType(ResultType))
5766c0febd58f5cbf4a93fd12f461863564dba0af76dSteve Naroff      return LHS;
5767c0febd58f5cbf4a93fd12f461863564dba0af76dSteve Naroff    if (getCanonicalType(RHSPointee) == getCanonicalType(ResultType))
5768c0febd58f5cbf4a93fd12f461863564dba0af76dSteve Naroff      return RHS;
5769c0febd58f5cbf4a93fd12f461863564dba0af76dSteve Naroff    return getBlockPointerType(ResultType);
5770c0febd58f5cbf4a93fd12f461863564dba0af76dSteve Naroff  }
57711adb88370beab45af2f065afe86b51ccd59ec50dChris Lattner  case Type::ConstantArray:
57723d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman  {
57733d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    const ConstantArrayType* LCAT = getAsConstantArrayType(LHS);
57743d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    const ConstantArrayType* RCAT = getAsConstantArrayType(RHS);
57753d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    if (LCAT && RCAT && RCAT->getSize() != LCAT->getSize())
57763d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman      return QualType();
57773d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman
57783d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    QualType LHSElem = getAsArrayType(LHS)->getElementType();
57793d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    QualType RHSElem = getAsArrayType(RHS)->getElementType();
5780447234dd459a00a5ed9b7c3e066162cd7a75bf2dDouglas Gregor    if (Unqualified) {
5781447234dd459a00a5ed9b7c3e066162cd7a75bf2dDouglas Gregor      LHSElem = LHSElem.getUnqualifiedType();
5782447234dd459a00a5ed9b7c3e066162cd7a75bf2dDouglas Gregor      RHSElem = RHSElem.getUnqualifiedType();
5783447234dd459a00a5ed9b7c3e066162cd7a75bf2dDouglas Gregor    }
5784447234dd459a00a5ed9b7c3e066162cd7a75bf2dDouglas Gregor
5785447234dd459a00a5ed9b7c3e066162cd7a75bf2dDouglas Gregor    QualType ResultType = mergeTypes(LHSElem, RHSElem, false, Unqualified);
57863d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    if (ResultType.isNull()) return QualType();
578761710854be2b098428aff5316e64bd34b30fbcb7Chris Lattner    if (LCAT && getCanonicalType(LHSElem) == getCanonicalType(ResultType))
578861710854be2b098428aff5316e64bd34b30fbcb7Chris Lattner      return LHS;
578961710854be2b098428aff5316e64bd34b30fbcb7Chris Lattner    if (RCAT && getCanonicalType(RHSElem) == getCanonicalType(ResultType))
579061710854be2b098428aff5316e64bd34b30fbcb7Chris Lattner      return RHS;
57913bc0f45a5e65814f42b22dcdf7249d1120d16f36Eli Friedman    if (LCAT) return getConstantArrayType(ResultType, LCAT->getSize(),
57923bc0f45a5e65814f42b22dcdf7249d1120d16f36Eli Friedman                                          ArrayType::ArraySizeModifier(), 0);
57933bc0f45a5e65814f42b22dcdf7249d1120d16f36Eli Friedman    if (RCAT) return getConstantArrayType(ResultType, RCAT->getSize(),
57943bc0f45a5e65814f42b22dcdf7249d1120d16f36Eli Friedman                                          ArrayType::ArraySizeModifier(), 0);
57953d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    const VariableArrayType* LVAT = getAsVariableArrayType(LHS);
57963d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    const VariableArrayType* RVAT = getAsVariableArrayType(RHS);
579761710854be2b098428aff5316e64bd34b30fbcb7Chris Lattner    if (LVAT && getCanonicalType(LHSElem) == getCanonicalType(ResultType))
579861710854be2b098428aff5316e64bd34b30fbcb7Chris Lattner      return LHS;
579961710854be2b098428aff5316e64bd34b30fbcb7Chris Lattner    if (RVAT && getCanonicalType(RHSElem) == getCanonicalType(ResultType))
580061710854be2b098428aff5316e64bd34b30fbcb7Chris Lattner      return RHS;
58013d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    if (LVAT) {
58023d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman      // FIXME: This isn't correct! But tricky to implement because
58033d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman      // the array's size has to be the size of LHS, but the type
58043d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman      // has to be different.
58053d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman      return LHS;
58063d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    }
58073d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    if (RVAT) {
58083d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman      // FIXME: This isn't correct! But tricky to implement because
58093d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman      // the array's size has to be the size of RHS, but the type
58103d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman      // has to be different.
58113d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman      return RHS;
58123d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    }
58133bc0f45a5e65814f42b22dcdf7249d1120d16f36Eli Friedman    if (getCanonicalType(LHSElem) == getCanonicalType(ResultType)) return LHS;
58143bc0f45a5e65814f42b22dcdf7249d1120d16f36Eli Friedman    if (getCanonicalType(RHSElem) == getCanonicalType(ResultType)) return RHS;
58157e7eb3da052a6d80ddf2377cab0384c798f73f75Douglas Gregor    return getIncompleteArrayType(ResultType,
58167e7eb3da052a6d80ddf2377cab0384c798f73f75Douglas Gregor                                  ArrayType::ArraySizeModifier(), 0);
58173d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman  }
58181adb88370beab45af2f065afe86b51ccd59ec50dChris Lattner  case Type::FunctionNoProto:
5819447234dd459a00a5ed9b7c3e066162cd7a75bf2dDouglas Gregor    return mergeFunctionTypes(LHS, RHS, OfBlockPointer, Unqualified);
582072564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor  case Type::Record:
582172564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor  case Type::Enum:
58223d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    return QualType();
58231adb88370beab45af2f065afe86b51ccd59ec50dChris Lattner  case Type::Builtin:
58243cc4c0c3058a788689b8fc73c0ac139544435c97Chris Lattner    // Only exactly equal builtin types are compatible, which is tested above.
58253d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    return QualType();
582664cfdb7da3cb744642fe8a99ad5c851ad3c930b2Daniel Dunbar  case Type::Complex:
582764cfdb7da3cb744642fe8a99ad5c851ad3c930b2Daniel Dunbar    // Distinct complex types are incompatible.
582864cfdb7da3cb744642fe8a99ad5c851ad3c930b2Daniel Dunbar    return QualType();
58293cc4c0c3058a788689b8fc73c0ac139544435c97Chris Lattner  case Type::Vector:
58305a61f0e5c5aaecd5713c3fa4b78be7167a7eeff2Eli Friedman    // FIXME: The merged type should be an ExtVector!
58311c471f3e1c0ec8cbc82447bb35908dfc55463e46John McCall    if (areCompatVectorTypes(LHSCan->getAs<VectorType>(),
58321c471f3e1c0ec8cbc82447bb35908dfc55463e46John McCall                             RHSCan->getAs<VectorType>()))
58333d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman      return LHS;
583461710854be2b098428aff5316e64bd34b30fbcb7Chris Lattner    return QualType();
5835c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall  case Type::ObjCObject: {
5836c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall    // Check if the types are assignment compatible.
58375a61f0e5c5aaecd5713c3fa4b78be7167a7eeff2Eli Friedman    // FIXME: This should be type compatibility, e.g. whether
58385a61f0e5c5aaecd5713c3fa4b78be7167a7eeff2Eli Friedman    // "LHS x; RHS x;" at global scope is legal.
5839c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall    const ObjCObjectType* LHSIface = LHS->getAs<ObjCObjectType>();
5840c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall    const ObjCObjectType* RHSIface = RHS->getAs<ObjCObjectType>();
5841c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall    if (canAssignObjCInterfaces(LHSIface, RHSIface))
58425fd659db11922fc12a58e478f7b745f9656b15a7Steve Naroff      return LHS;
58435fd659db11922fc12a58e478f7b745f9656b15a7Steve Naroff
58443d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    return QualType();
584561490e9a965cfee8a78c12c6802138844f04250dCedric Venet  }
584614108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff  case Type::ObjCObjectPointer: {
5847132f2a2da34f378fc675b9e174564b0f52c31d98Fariborz Jahanian    if (OfBlockPointer) {
5848132f2a2da34f378fc675b9e174564b0f52c31d98Fariborz Jahanian      if (canAssignObjCInterfacesInBlockPointer(
5849132f2a2da34f378fc675b9e174564b0f52c31d98Fariborz Jahanian                                          LHS->getAs<ObjCObjectPointerType>(),
5850a4fdbfad150ae37bddaa4094d3925a27a1a1cf3fFariborz Jahanian                                          RHS->getAs<ObjCObjectPointerType>(),
5851a4fdbfad150ae37bddaa4094d3925a27a1a1cf3fFariborz Jahanian                                          BlockReturnType))
5852132f2a2da34f378fc675b9e174564b0f52c31d98Fariborz Jahanian      return LHS;
5853132f2a2da34f378fc675b9e174564b0f52c31d98Fariborz Jahanian      return QualType();
5854132f2a2da34f378fc675b9e174564b0f52c31d98Fariborz Jahanian    }
5855183700f494ec9b6701b6efe82bcb25f4c79ba561John McCall    if (canAssignObjCInterfaces(LHS->getAs<ObjCObjectPointerType>(),
5856183700f494ec9b6701b6efe82bcb25f4c79ba561John McCall                                RHS->getAs<ObjCObjectPointerType>()))
585714108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff      return LHS;
585814108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff
5859bc76dd06eb881c70c9775b74bab8b88cd747f173Steve Naroff    return QualType();
5860132f2a2da34f378fc675b9e174564b0f52c31d98Fariborz Jahanian    }
5861ec0550fa3653d46560bf4484a2e988329c228e39Steve Naroff  }
586272564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor
586372564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor  return QualType();
5864ec0550fa3653d46560bf4484a2e988329c228e39Steve Naroff}
58657192f8e9592729882a09d84d77838db26e39ebd4Ted Kremenek
58662390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian/// mergeObjCGCQualifiers - This routine merges ObjC's GC attribute of 'LHS' and
58672390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian/// 'RHS' attributes and returns the merged version; including for function
58682390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian/// return types.
58692390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz JahanianQualType ASTContext::mergeObjCGCQualifiers(QualType LHS, QualType RHS) {
58702390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian  QualType LHSCan = getCanonicalType(LHS),
58712390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian  RHSCan = getCanonicalType(RHS);
58722390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian  // If two types are identical, they are compatible.
58732390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian  if (LHSCan == RHSCan)
58742390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian    return LHS;
58752390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian  if (RHSCan->isFunctionType()) {
58762390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian    if (!LHSCan->isFunctionType())
58772390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian      return QualType();
58782390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian    QualType OldReturnType =
58792390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian      cast<FunctionType>(RHSCan.getTypePtr())->getResultType();
58802390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian    QualType NewReturnType =
58812390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian      cast<FunctionType>(LHSCan.getTypePtr())->getResultType();
58822390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian    QualType ResReturnType =
58832390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian      mergeObjCGCQualifiers(NewReturnType, OldReturnType);
58842390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian    if (ResReturnType.isNull())
58852390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian      return QualType();
58862390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian    if (ResReturnType == NewReturnType || ResReturnType == OldReturnType) {
58872390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian      // id foo(); ... __strong id foo(); or: __strong id foo(); ... id foo();
58882390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian      // In either case, use OldReturnType to build the new function type.
58892390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian      const FunctionType *F = LHS->getAs<FunctionType>();
58902390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian      if (const FunctionProtoType *FPT = cast<FunctionProtoType>(F)) {
5891e23cf437fe76b1ed02d63c3f61b456fd48a915f5John McCall        FunctionProtoType::ExtProtoInfo EPI = FPT->getExtProtoInfo();
5892e23cf437fe76b1ed02d63c3f61b456fd48a915f5John McCall        EPI.ExtInfo = getFunctionExtInfo(LHS);
58932390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian        QualType ResultType
58942390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian          = getFunctionType(OldReturnType, FPT->arg_type_begin(),
5895e23cf437fe76b1ed02d63c3f61b456fd48a915f5John McCall                            FPT->getNumArgs(), EPI);
58962390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian        return ResultType;
58972390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian      }
58982390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian    }
58992390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian    return QualType();
59002390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian  }
59012390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian
59022390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian  // If the qualifiers are different, the types can still be merged.
59032390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian  Qualifiers LQuals = LHSCan.getLocalQualifiers();
59042390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian  Qualifiers RQuals = RHSCan.getLocalQualifiers();
59052390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian  if (LQuals != RQuals) {
59062390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian    // If any of these qualifiers are different, we have a type mismatch.
59072390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian    if (LQuals.getCVRQualifiers() != RQuals.getCVRQualifiers() ||
59082390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian        LQuals.getAddressSpace() != RQuals.getAddressSpace())
59092390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian      return QualType();
59102390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian
59112390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian    // Exactly one GC qualifier difference is allowed: __strong is
59122390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian    // okay if the other type has no GC qualifier but is an Objective
59132390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian    // C object pointer (i.e. implicitly strong by default).  We fix
59142390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian    // this by pretending that the unqualified type was actually
59152390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian    // qualified __strong.
59162390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian    Qualifiers::GC GC_L = LQuals.getObjCGCAttr();
59172390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian    Qualifiers::GC GC_R = RQuals.getObjCGCAttr();
59182390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian    assert((GC_L != GC_R) && "unequal qualifier sets had only equal elements");
59192390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian
59202390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian    if (GC_L == Qualifiers::Weak || GC_R == Qualifiers::Weak)
59212390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian      return QualType();
59222390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian
59232390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian    if (GC_L == Qualifiers::Strong)
59242390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian      return LHS;
59252390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian    if (GC_R == Qualifiers::Strong)
59262390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian      return RHS;
59272390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian    return QualType();
59282390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian  }
59292390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian
59302390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian  if (LHSCan->isObjCObjectPointerType() && RHSCan->isObjCObjectPointerType()) {
59312390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian    QualType LHSBaseQT = LHS->getAs<ObjCObjectPointerType>()->getPointeeType();
59322390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian    QualType RHSBaseQT = RHS->getAs<ObjCObjectPointerType>()->getPointeeType();
59332390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian    QualType ResQT = mergeObjCGCQualifiers(LHSBaseQT, RHSBaseQT);
59342390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian    if (ResQT == LHSBaseQT)
59352390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian      return LHS;
59362390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian    if (ResQT == RHSBaseQT)
59372390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian      return RHS;
59382390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian  }
59392390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian  return QualType();
59402390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian}
59412390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian
59425426bf6456a5aeac416a9150de157904d101c819Chris Lattner//===----------------------------------------------------------------------===//
5943ad74a758189180b8ab8faea648e4766c3bfd7fcbEli Friedman//                         Integer Predicates
5944ad74a758189180b8ab8faea648e4766c3bfd7fcbEli Friedman//===----------------------------------------------------------------------===//
594588054dee0402e4d3c1f64e6b697acc47195c0d72Chris Lattner
59464ba2a17694148e16eaa8d3917f657ffcd3667be4Jay Foadunsigned ASTContext::getIntWidth(QualType T) const {
5947f4c7371fb1d3cebcfb40abad4537bb82515704eaJohn McCall  if (const EnumType *ET = dyn_cast<EnumType>(T))
594829a7f3342c3c6dd15d914c61ae22246c36d51ce7Eli Friedman    T = ET->getDecl()->getIntegerType();
59491274ccd90aec0b205fc838c3d504821ccfb55482Douglas Gregor  if (T->isBooleanType())
59501274ccd90aec0b205fc838c3d504821ccfb55482Douglas Gregor    return 1;
5951f98aba35e6c3da5aae61843fc01334939e4e12ecEli Friedman  // For builtin types, just use the standard type sizing method
5952ad74a758189180b8ab8faea648e4766c3bfd7fcbEli Friedman  return (unsigned)getTypeSize(T);
5953ad74a758189180b8ab8faea648e4766c3bfd7fcbEli Friedman}
5954ad74a758189180b8ab8faea648e4766c3bfd7fcbEli Friedman
5955ad74a758189180b8ab8faea648e4766c3bfd7fcbEli FriedmanQualType ASTContext::getCorrespondingUnsignedType(QualType T) {
5956f60946222721d9ba3c059563935c17b84703187aDouglas Gregor  assert(T->hasSignedIntegerRepresentation() && "Unexpected type");
59576a2b9261bf9c973c7122d9d1febce24a38fa862dChris Lattner
59586a2b9261bf9c973c7122d9d1febce24a38fa862dChris Lattner  // Turn <4 x signed int> -> <4 x unsigned int>
59596a2b9261bf9c973c7122d9d1febce24a38fa862dChris Lattner  if (const VectorType *VTy = T->getAs<VectorType>())
59606a2b9261bf9c973c7122d9d1febce24a38fa862dChris Lattner    return getVectorType(getCorrespondingUnsignedType(VTy->getElementType()),
5961e86d78cf4754a6aef2cf9a33d847aa15338e276fBob Wilson                         VTy->getNumElements(), VTy->getVectorKind());
59626a2b9261bf9c973c7122d9d1febce24a38fa862dChris Lattner
59636a2b9261bf9c973c7122d9d1febce24a38fa862dChris Lattner  // For enums, we return the unsigned version of the base type.
59646a2b9261bf9c973c7122d9d1febce24a38fa862dChris Lattner  if (const EnumType *ETy = T->getAs<EnumType>())
5965ad74a758189180b8ab8faea648e4766c3bfd7fcbEli Friedman    T = ETy->getDecl()->getIntegerType();
59666a2b9261bf9c973c7122d9d1febce24a38fa862dChris Lattner
59676a2b9261bf9c973c7122d9d1febce24a38fa862dChris Lattner  const BuiltinType *BTy = T->getAs<BuiltinType>();
59686a2b9261bf9c973c7122d9d1febce24a38fa862dChris Lattner  assert(BTy && "Unexpected signed integer type");
5969ad74a758189180b8ab8faea648e4766c3bfd7fcbEli Friedman  switch (BTy->getKind()) {
5970ad74a758189180b8ab8faea648e4766c3bfd7fcbEli Friedman  case BuiltinType::Char_S:
5971ad74a758189180b8ab8faea648e4766c3bfd7fcbEli Friedman  case BuiltinType::SChar:
5972ad74a758189180b8ab8faea648e4766c3bfd7fcbEli Friedman    return UnsignedCharTy;
5973ad74a758189180b8ab8faea648e4766c3bfd7fcbEli Friedman  case BuiltinType::Short:
5974ad74a758189180b8ab8faea648e4766c3bfd7fcbEli Friedman    return UnsignedShortTy;
5975ad74a758189180b8ab8faea648e4766c3bfd7fcbEli Friedman  case BuiltinType::Int:
5976ad74a758189180b8ab8faea648e4766c3bfd7fcbEli Friedman    return UnsignedIntTy;
5977ad74a758189180b8ab8faea648e4766c3bfd7fcbEli Friedman  case BuiltinType::Long:
5978ad74a758189180b8ab8faea648e4766c3bfd7fcbEli Friedman    return UnsignedLongTy;
5979ad74a758189180b8ab8faea648e4766c3bfd7fcbEli Friedman  case BuiltinType::LongLong:
5980ad74a758189180b8ab8faea648e4766c3bfd7fcbEli Friedman    return UnsignedLongLongTy;
59812df9ced9fd1e8c7d7b38443db07e0e811de22571Chris Lattner  case BuiltinType::Int128:
59822df9ced9fd1e8c7d7b38443db07e0e811de22571Chris Lattner    return UnsignedInt128Ty;
5983ad74a758189180b8ab8faea648e4766c3bfd7fcbEli Friedman  default:
5984ad74a758189180b8ab8faea648e4766c3bfd7fcbEli Friedman    assert(0 && "Unexpected signed integer type");
5985ad74a758189180b8ab8faea648e4766c3bfd7fcbEli Friedman    return QualType();
5986ad74a758189180b8ab8faea648e4766c3bfd7fcbEli Friedman  }
5987ad74a758189180b8ab8faea648e4766c3bfd7fcbEli Friedman}
5988ad74a758189180b8ab8faea648e4766c3bfd7fcbEli Friedman
59897b90340c9c7d07aef4e301e72b5e8a30d5f4f0c8Argyrios KyrtzidisASTMutationListener::~ASTMutationListener() { }
59907b90340c9c7d07aef4e301e72b5e8a30d5f4f0c8Argyrios Kyrtzidis
599186df27bbdbb98c39ec2184695c0561209f91beddChris Lattner
599286df27bbdbb98c39ec2184695c0561209f91beddChris Lattner//===----------------------------------------------------------------------===//
599386df27bbdbb98c39ec2184695c0561209f91beddChris Lattner//                          Builtin Type Computation
599486df27bbdbb98c39ec2184695c0561209f91beddChris Lattner//===----------------------------------------------------------------------===//
599586df27bbdbb98c39ec2184695c0561209f91beddChris Lattner
599686df27bbdbb98c39ec2184695c0561209f91beddChris Lattner/// DecodeTypeFromStr - This decodes one type descriptor from Str, advancing the
599733daae66462e8f51ee153463b32bdefd60c801aaChris Lattner/// pointer over the consumed characters.  This returns the resultant type.  If
599833daae66462e8f51ee153463b32bdefd60c801aaChris Lattner/// AllowTypeModifiers is false then modifier like * are not parsed, just basic
599933daae66462e8f51ee153463b32bdefd60c801aaChris Lattner/// types.  This allows "v2i*" to be parsed as a pointer to a v2i instead of
600033daae66462e8f51ee153463b32bdefd60c801aaChris Lattner/// a vector of "i*".
600114e0e7436cf6650a72052baea1f8ebe644cef489Chris Lattner///
600214e0e7436cf6650a72052baea1f8ebe644cef489Chris Lattner/// RequiresICE is filled in on return to indicate whether the value is required
600314e0e7436cf6650a72052baea1f8ebe644cef489Chris Lattner/// to be an Integer Constant Expression.
60044ba2a17694148e16eaa8d3917f657ffcd3667be4Jay Foadstatic QualType DecodeTypeFromStr(const char *&Str, const ASTContext &Context,
600586df27bbdbb98c39ec2184695c0561209f91beddChris Lattner                                  ASTContext::GetBuiltinTypeError &Error,
600614e0e7436cf6650a72052baea1f8ebe644cef489Chris Lattner                                  bool &RequiresICE,
600733daae66462e8f51ee153463b32bdefd60c801aaChris Lattner                                  bool AllowTypeModifiers) {
600886df27bbdbb98c39ec2184695c0561209f91beddChris Lattner  // Modifiers.
600986df27bbdbb98c39ec2184695c0561209f91beddChris Lattner  int HowLong = 0;
601086df27bbdbb98c39ec2184695c0561209f91beddChris Lattner  bool Signed = false, Unsigned = false;
601114e0e7436cf6650a72052baea1f8ebe644cef489Chris Lattner  RequiresICE = false;
6012393bd8e185692a451b2ba16bdfc9e7d3543b4217Chris Lattner
601333daae66462e8f51ee153463b32bdefd60c801aaChris Lattner  // Read the prefixed modifiers first.
601486df27bbdbb98c39ec2184695c0561209f91beddChris Lattner  bool Done = false;
601586df27bbdbb98c39ec2184695c0561209f91beddChris Lattner  while (!Done) {
601686df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    switch (*Str++) {
60171eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    default: Done = true; --Str; break;
6018393bd8e185692a451b2ba16bdfc9e7d3543b4217Chris Lattner    case 'I':
601914e0e7436cf6650a72052baea1f8ebe644cef489Chris Lattner      RequiresICE = true;
6020393bd8e185692a451b2ba16bdfc9e7d3543b4217Chris Lattner      break;
602186df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    case 'S':
602286df27bbdbb98c39ec2184695c0561209f91beddChris Lattner      assert(!Unsigned && "Can't use both 'S' and 'U' modifiers!");
602386df27bbdbb98c39ec2184695c0561209f91beddChris Lattner      assert(!Signed && "Can't use 'S' modifier multiple times!");
602486df27bbdbb98c39ec2184695c0561209f91beddChris Lattner      Signed = true;
602586df27bbdbb98c39ec2184695c0561209f91beddChris Lattner      break;
602686df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    case 'U':
602786df27bbdbb98c39ec2184695c0561209f91beddChris Lattner      assert(!Signed && "Can't use both 'S' and 'U' modifiers!");
602886df27bbdbb98c39ec2184695c0561209f91beddChris Lattner      assert(!Unsigned && "Can't use 'S' modifier multiple times!");
602986df27bbdbb98c39ec2184695c0561209f91beddChris Lattner      Unsigned = true;
603086df27bbdbb98c39ec2184695c0561209f91beddChris Lattner      break;
603186df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    case 'L':
603286df27bbdbb98c39ec2184695c0561209f91beddChris Lattner      assert(HowLong <= 2 && "Can't have LLLL modifier");
603386df27bbdbb98c39ec2184695c0561209f91beddChris Lattner      ++HowLong;
603486df27bbdbb98c39ec2184695c0561209f91beddChris Lattner      break;
603586df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    }
603686df27bbdbb98c39ec2184695c0561209f91beddChris Lattner  }
603786df27bbdbb98c39ec2184695c0561209f91beddChris Lattner
603886df27bbdbb98c39ec2184695c0561209f91beddChris Lattner  QualType Type;
60391eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
604086df27bbdbb98c39ec2184695c0561209f91beddChris Lattner  // Read the base type.
604186df27bbdbb98c39ec2184695c0561209f91beddChris Lattner  switch (*Str++) {
604286df27bbdbb98c39ec2184695c0561209f91beddChris Lattner  default: assert(0 && "Unknown builtin type letter!");
604386df27bbdbb98c39ec2184695c0561209f91beddChris Lattner  case 'v':
604486df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    assert(HowLong == 0 && !Signed && !Unsigned &&
604586df27bbdbb98c39ec2184695c0561209f91beddChris Lattner           "Bad modifiers used with 'v'!");
604686df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    Type = Context.VoidTy;
604786df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    break;
604886df27bbdbb98c39ec2184695c0561209f91beddChris Lattner  case 'f':
604986df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    assert(HowLong == 0 && !Signed && !Unsigned &&
605086df27bbdbb98c39ec2184695c0561209f91beddChris Lattner           "Bad modifiers used with 'f'!");
605186df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    Type = Context.FloatTy;
605286df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    break;
605386df27bbdbb98c39ec2184695c0561209f91beddChris Lattner  case 'd':
605486df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    assert(HowLong < 2 && !Signed && !Unsigned &&
605586df27bbdbb98c39ec2184695c0561209f91beddChris Lattner           "Bad modifiers used with 'd'!");
605686df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    if (HowLong)
605786df27bbdbb98c39ec2184695c0561209f91beddChris Lattner      Type = Context.LongDoubleTy;
605886df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    else
605986df27bbdbb98c39ec2184695c0561209f91beddChris Lattner      Type = Context.DoubleTy;
606086df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    break;
606186df27bbdbb98c39ec2184695c0561209f91beddChris Lattner  case 's':
606286df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    assert(HowLong == 0 && "Bad modifiers used with 's'!");
606386df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    if (Unsigned)
606486df27bbdbb98c39ec2184695c0561209f91beddChris Lattner      Type = Context.UnsignedShortTy;
606586df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    else
606686df27bbdbb98c39ec2184695c0561209f91beddChris Lattner      Type = Context.ShortTy;
606786df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    break;
606886df27bbdbb98c39ec2184695c0561209f91beddChris Lattner  case 'i':
606986df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    if (HowLong == 3)
607086df27bbdbb98c39ec2184695c0561209f91beddChris Lattner      Type = Unsigned ? Context.UnsignedInt128Ty : Context.Int128Ty;
607186df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    else if (HowLong == 2)
607286df27bbdbb98c39ec2184695c0561209f91beddChris Lattner      Type = Unsigned ? Context.UnsignedLongLongTy : Context.LongLongTy;
607386df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    else if (HowLong == 1)
607486df27bbdbb98c39ec2184695c0561209f91beddChris Lattner      Type = Unsigned ? Context.UnsignedLongTy : Context.LongTy;
607586df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    else
607686df27bbdbb98c39ec2184695c0561209f91beddChris Lattner      Type = Unsigned ? Context.UnsignedIntTy : Context.IntTy;
607786df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    break;
607886df27bbdbb98c39ec2184695c0561209f91beddChris Lattner  case 'c':
607986df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    assert(HowLong == 0 && "Bad modifiers used with 'c'!");
608086df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    if (Signed)
608186df27bbdbb98c39ec2184695c0561209f91beddChris Lattner      Type = Context.SignedCharTy;
608286df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    else if (Unsigned)
608386df27bbdbb98c39ec2184695c0561209f91beddChris Lattner      Type = Context.UnsignedCharTy;
608486df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    else
608586df27bbdbb98c39ec2184695c0561209f91beddChris Lattner      Type = Context.CharTy;
608686df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    break;
608786df27bbdbb98c39ec2184695c0561209f91beddChris Lattner  case 'b': // boolean
608886df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    assert(HowLong == 0 && !Signed && !Unsigned && "Bad modifiers for 'b'!");
608986df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    Type = Context.BoolTy;
609086df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    break;
609186df27bbdbb98c39ec2184695c0561209f91beddChris Lattner  case 'z':  // size_t.
609286df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    assert(HowLong == 0 && !Signed && !Unsigned && "Bad modifiers for 'z'!");
609386df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    Type = Context.getSizeType();
609486df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    break;
609586df27bbdbb98c39ec2184695c0561209f91beddChris Lattner  case 'F':
609686df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    Type = Context.getCFConstantStringType();
609786df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    break;
6098ba8bda05fefd3bb2f1ef201784b685f715bdde29Fariborz Jahanian  case 'G':
6099ba8bda05fefd3bb2f1ef201784b685f715bdde29Fariborz Jahanian    Type = Context.getObjCIdType();
6100ba8bda05fefd3bb2f1ef201784b685f715bdde29Fariborz Jahanian    break;
6101ba8bda05fefd3bb2f1ef201784b685f715bdde29Fariborz Jahanian  case 'H':
6102ba8bda05fefd3bb2f1ef201784b685f715bdde29Fariborz Jahanian    Type = Context.getObjCSelType();
6103ba8bda05fefd3bb2f1ef201784b685f715bdde29Fariborz Jahanian    break;
610486df27bbdbb98c39ec2184695c0561209f91beddChris Lattner  case 'a':
610586df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    Type = Context.getBuiltinVaListType();
610686df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    assert(!Type.isNull() && "builtin va list type not initialized!");
610786df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    break;
610886df27bbdbb98c39ec2184695c0561209f91beddChris Lattner  case 'A':
610986df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    // This is a "reference" to a va_list; however, what exactly
611086df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    // this means depends on how va_list is defined. There are two
611186df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    // different kinds of va_list: ones passed by value, and ones
611286df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    // passed by reference.  An example of a by-value va_list is
611386df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    // x86, where va_list is a char*. An example of by-ref va_list
611486df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    // is x86-64, where va_list is a __va_list_tag[1]. For x86,
611586df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    // we want this argument to be a char*&; for x86-64, we want
611686df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    // it to be a __va_list_tag*.
611786df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    Type = Context.getBuiltinVaListType();
611886df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    assert(!Type.isNull() && "builtin va list type not initialized!");
611914e0e7436cf6650a72052baea1f8ebe644cef489Chris Lattner    if (Type->isArrayType())
612086df27bbdbb98c39ec2184695c0561209f91beddChris Lattner      Type = Context.getArrayDecayedType(Type);
612114e0e7436cf6650a72052baea1f8ebe644cef489Chris Lattner    else
612286df27bbdbb98c39ec2184695c0561209f91beddChris Lattner      Type = Context.getLValueReferenceType(Type);
612386df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    break;
612486df27bbdbb98c39ec2184695c0561209f91beddChris Lattner  case 'V': {
612586df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    char *End;
612686df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    unsigned NumElements = strtoul(Str, &End, 10);
612786df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    assert(End != Str && "Missing vector size");
612886df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    Str = End;
61291eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
613014e0e7436cf6650a72052baea1f8ebe644cef489Chris Lattner    QualType ElementType = DecodeTypeFromStr(Str, Context, Error,
613114e0e7436cf6650a72052baea1f8ebe644cef489Chris Lattner                                             RequiresICE, false);
613214e0e7436cf6650a72052baea1f8ebe644cef489Chris Lattner    assert(!RequiresICE && "Can't require vector ICE");
613333daae66462e8f51ee153463b32bdefd60c801aaChris Lattner
613433daae66462e8f51ee153463b32bdefd60c801aaChris Lattner    // TODO: No way to make AltiVec vectors in builtins yet.
6135788b0fd67e1992f23555454efcdb16a19dfefac3Chris Lattner    Type = Context.getVectorType(ElementType, NumElements,
6136e86d78cf4754a6aef2cf9a33d847aa15338e276fBob Wilson                                 VectorType::GenericVector);
613786df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    break;
613886df27bbdbb98c39ec2184695c0561209f91beddChris Lattner  }
6139d3a23b238a2b2c0f11e6ac4951c7410a8c5717bfDouglas Gregor  case 'X': {
614014e0e7436cf6650a72052baea1f8ebe644cef489Chris Lattner    QualType ElementType = DecodeTypeFromStr(Str, Context, Error, RequiresICE,
614114e0e7436cf6650a72052baea1f8ebe644cef489Chris Lattner                                             false);
614214e0e7436cf6650a72052baea1f8ebe644cef489Chris Lattner    assert(!RequiresICE && "Can't require complex ICE");
6143d3a23b238a2b2c0f11e6ac4951c7410a8c5717bfDouglas Gregor    Type = Context.getComplexType(ElementType);
6144d3a23b238a2b2c0f11e6ac4951c7410a8c5717bfDouglas Gregor    break;
6145d3a23b238a2b2c0f11e6ac4951c7410a8c5717bfDouglas Gregor  }
61469a5a7e7351f78345a72c4956af25590f6d40ebcdChris Lattner  case 'P':
6147c29f77b769bcc5b6dc85e72c8e3cc2e348e5cf25Douglas Gregor    Type = Context.getFILEType();
6148c29f77b769bcc5b6dc85e72c8e3cc2e348e5cf25Douglas Gregor    if (Type.isNull()) {
6149f711c41dd9412a8182793259d355c4f6979ed5edMike Stump      Error = ASTContext::GE_Missing_stdio;
615086df27bbdbb98c39ec2184695c0561209f91beddChris Lattner      return QualType();
615186df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    }
6152fd612dbb23cd31c03c898ae53ff18d0dfd8488f9Mike Stump    break;
61539a5a7e7351f78345a72c4956af25590f6d40ebcdChris Lattner  case 'J':
6154f711c41dd9412a8182793259d355c4f6979ed5edMike Stump    if (Signed)
6155782fa308a765aeac2acb39c4e697c937ec21185bMike Stump      Type = Context.getsigjmp_bufType();
6156f711c41dd9412a8182793259d355c4f6979ed5edMike Stump    else
6157f711c41dd9412a8182793259d355c4f6979ed5edMike Stump      Type = Context.getjmp_bufType();
6158f711c41dd9412a8182793259d355c4f6979ed5edMike Stump
6159fd612dbb23cd31c03c898ae53ff18d0dfd8488f9Mike Stump    if (Type.isNull()) {
6160f711c41dd9412a8182793259d355c4f6979ed5edMike Stump      Error = ASTContext::GE_Missing_setjmp;
6161fd612dbb23cd31c03c898ae53ff18d0dfd8488f9Mike Stump      return QualType();
6162fd612dbb23cd31c03c898ae53ff18d0dfd8488f9Mike Stump    }
6163fd612dbb23cd31c03c898ae53ff18d0dfd8488f9Mike Stump    break;
6164782fa308a765aeac2acb39c4e697c937ec21185bMike Stump  }
61651eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
616633daae66462e8f51ee153463b32bdefd60c801aaChris Lattner  // If there are modifiers and if we're allowed to parse them, go for it.
616733daae66462e8f51ee153463b32bdefd60c801aaChris Lattner  Done = !AllowTypeModifiers;
616886df27bbdbb98c39ec2184695c0561209f91beddChris Lattner  while (!Done) {
6169187ab37a05b8f7015b9f39cc8cd9129a0c6d0b48John McCall    switch (char c = *Str++) {
617033daae66462e8f51ee153463b32bdefd60c801aaChris Lattner    default: Done = true; --Str; break;
617133daae66462e8f51ee153463b32bdefd60c801aaChris Lattner    case '*':
617233daae66462e8f51ee153463b32bdefd60c801aaChris Lattner    case '&': {
617333daae66462e8f51ee153463b32bdefd60c801aaChris Lattner      // Both pointers and references can have their pointee types
617433daae66462e8f51ee153463b32bdefd60c801aaChris Lattner      // qualified with an address space.
617533daae66462e8f51ee153463b32bdefd60c801aaChris Lattner      char *End;
617633daae66462e8f51ee153463b32bdefd60c801aaChris Lattner      unsigned AddrSpace = strtoul(Str, &End, 10);
617733daae66462e8f51ee153463b32bdefd60c801aaChris Lattner      if (End != Str && AddrSpace != 0) {
617833daae66462e8f51ee153463b32bdefd60c801aaChris Lattner        Type = Context.getAddrSpaceQualType(Type, AddrSpace);
617933daae66462e8f51ee153463b32bdefd60c801aaChris Lattner        Str = End;
618033daae66462e8f51ee153463b32bdefd60c801aaChris Lattner      }
618133daae66462e8f51ee153463b32bdefd60c801aaChris Lattner      if (c == '*')
618233daae66462e8f51ee153463b32bdefd60c801aaChris Lattner        Type = Context.getPointerType(Type);
618333daae66462e8f51ee153463b32bdefd60c801aaChris Lattner      else
618433daae66462e8f51ee153463b32bdefd60c801aaChris Lattner        Type = Context.getLValueReferenceType(Type);
618533daae66462e8f51ee153463b32bdefd60c801aaChris Lattner      break;
618633daae66462e8f51ee153463b32bdefd60c801aaChris Lattner    }
618733daae66462e8f51ee153463b32bdefd60c801aaChris Lattner    // FIXME: There's no way to have a built-in with an rvalue ref arg.
618833daae66462e8f51ee153463b32bdefd60c801aaChris Lattner    case 'C':
618933daae66462e8f51ee153463b32bdefd60c801aaChris Lattner      Type = Type.withConst();
619033daae66462e8f51ee153463b32bdefd60c801aaChris Lattner      break;
619133daae66462e8f51ee153463b32bdefd60c801aaChris Lattner    case 'D':
619233daae66462e8f51ee153463b32bdefd60c801aaChris Lattner      Type = Context.getVolatileType(Type);
619333daae66462e8f51ee153463b32bdefd60c801aaChris Lattner      break;
619486df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    }
619586df27bbdbb98c39ec2184695c0561209f91beddChris Lattner  }
6196393bd8e185692a451b2ba16bdfc9e7d3543b4217Chris Lattner
619714e0e7436cf6650a72052baea1f8ebe644cef489Chris Lattner  assert((!RequiresICE || Type->isIntegralOrEnumerationType()) &&
6198393bd8e185692a451b2ba16bdfc9e7d3543b4217Chris Lattner         "Integer constant 'I' type must be an integer");
61991eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
620086df27bbdbb98c39ec2184695c0561209f91beddChris Lattner  return Type;
620186df27bbdbb98c39ec2184695c0561209f91beddChris Lattner}
620286df27bbdbb98c39ec2184695c0561209f91beddChris Lattner
620386df27bbdbb98c39ec2184695c0561209f91beddChris Lattner/// GetBuiltinType - Return the type for the specified builtin.
620433daae66462e8f51ee153463b32bdefd60c801aaChris LattnerQualType ASTContext::GetBuiltinType(unsigned Id,
620514e0e7436cf6650a72052baea1f8ebe644cef489Chris Lattner                                    GetBuiltinTypeError &Error,
62064ba2a17694148e16eaa8d3917f657ffcd3667be4Jay Foad                                    unsigned *IntegerConstantArgs) const {
620733daae66462e8f51ee153463b32bdefd60c801aaChris Lattner  const char *TypeStr = BuiltinInfo.GetTypeString(Id);
62081eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
62095f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner  SmallVector<QualType, 8> ArgTypes;
62101eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
621114e0e7436cf6650a72052baea1f8ebe644cef489Chris Lattner  bool RequiresICE = false;
621286df27bbdbb98c39ec2184695c0561209f91beddChris Lattner  Error = GE_None;
621314e0e7436cf6650a72052baea1f8ebe644cef489Chris Lattner  QualType ResType = DecodeTypeFromStr(TypeStr, *this, Error,
621414e0e7436cf6650a72052baea1f8ebe644cef489Chris Lattner                                       RequiresICE, true);
621586df27bbdbb98c39ec2184695c0561209f91beddChris Lattner  if (Error != GE_None)
621686df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    return QualType();
621714e0e7436cf6650a72052baea1f8ebe644cef489Chris Lattner
621814e0e7436cf6650a72052baea1f8ebe644cef489Chris Lattner  assert(!RequiresICE && "Result of intrinsic cannot be required to be an ICE");
621914e0e7436cf6650a72052baea1f8ebe644cef489Chris Lattner
622086df27bbdbb98c39ec2184695c0561209f91beddChris Lattner  while (TypeStr[0] && TypeStr[0] != '.') {
622114e0e7436cf6650a72052baea1f8ebe644cef489Chris Lattner    QualType Ty = DecodeTypeFromStr(TypeStr, *this, Error, RequiresICE, true);
622286df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    if (Error != GE_None)
622386df27bbdbb98c39ec2184695c0561209f91beddChris Lattner      return QualType();
622486df27bbdbb98c39ec2184695c0561209f91beddChris Lattner
622514e0e7436cf6650a72052baea1f8ebe644cef489Chris Lattner    // If this argument is required to be an IntegerConstantExpression and the
622614e0e7436cf6650a72052baea1f8ebe644cef489Chris Lattner    // caller cares, fill in the bitmask we return.
622714e0e7436cf6650a72052baea1f8ebe644cef489Chris Lattner    if (RequiresICE && IntegerConstantArgs)
622814e0e7436cf6650a72052baea1f8ebe644cef489Chris Lattner      *IntegerConstantArgs |= 1 << ArgTypes.size();
622914e0e7436cf6650a72052baea1f8ebe644cef489Chris Lattner
623086df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    // Do array -> pointer decay.  The builtin should use the decayed type.
623186df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    if (Ty->isArrayType())
623286df27bbdbb98c39ec2184695c0561209f91beddChris Lattner      Ty = getArrayDecayedType(Ty);
62331eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
623486df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    ArgTypes.push_back(Ty);
623586df27bbdbb98c39ec2184695c0561209f91beddChris Lattner  }
623686df27bbdbb98c39ec2184695c0561209f91beddChris Lattner
623786df27bbdbb98c39ec2184695c0561209f91beddChris Lattner  assert((TypeStr[0] != '.' || TypeStr[1] == 0) &&
623886df27bbdbb98c39ec2184695c0561209f91beddChris Lattner         "'.' should only occur at end of builtin type list!");
623986df27bbdbb98c39ec2184695c0561209f91beddChris Lattner
624000ccbefcffeb88ea3e2e6323e594fa968753ad14John McCall  FunctionType::ExtInfo EI;
624100ccbefcffeb88ea3e2e6323e594fa968753ad14John McCall  if (BuiltinInfo.isNoReturn(Id)) EI = EI.withNoReturn(true);
624200ccbefcffeb88ea3e2e6323e594fa968753ad14John McCall
624300ccbefcffeb88ea3e2e6323e594fa968753ad14John McCall  bool Variadic = (TypeStr[0] == '.');
624400ccbefcffeb88ea3e2e6323e594fa968753ad14John McCall
624500ccbefcffeb88ea3e2e6323e594fa968753ad14John McCall  // We really shouldn't be making a no-proto type here, especially in C++.
624600ccbefcffeb88ea3e2e6323e594fa968753ad14John McCall  if (ArgTypes.empty() && Variadic)
624700ccbefcffeb88ea3e2e6323e594fa968753ad14John McCall    return getFunctionNoProtoType(ResType, EI);
6248ce056bcaa1c97b89a4b2de2112c62d060863be2bDouglas Gregor
6249e23cf437fe76b1ed02d63c3f61b456fd48a915f5John McCall  FunctionProtoType::ExtProtoInfo EPI;
625000ccbefcffeb88ea3e2e6323e594fa968753ad14John McCall  EPI.ExtInfo = EI;
625100ccbefcffeb88ea3e2e6323e594fa968753ad14John McCall  EPI.Variadic = Variadic;
6252e23cf437fe76b1ed02d63c3f61b456fd48a915f5John McCall
6253e23cf437fe76b1ed02d63c3f61b456fd48a915f5John McCall  return getFunctionType(ResType, ArgTypes.data(), ArgTypes.size(), EPI);
625486df27bbdbb98c39ec2184695c0561209f91beddChris Lattner}
6255a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman
625690e99a84ddd020e8fda79643748243725a2ed071Argyrios KyrtzidisGVALinkage ASTContext::GetGVALinkageForFunction(const FunctionDecl *FD) {
625790e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis  GVALinkage External = GVA_StrongExternal;
625890e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis
625990e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis  Linkage L = FD->getLinkage();
626090e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis  switch (L) {
626190e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis  case NoLinkage:
626290e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis  case InternalLinkage:
626390e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis  case UniqueExternalLinkage:
626490e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis    return GVA_Internal;
626590e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis
626690e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis  case ExternalLinkage:
626790e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis    switch (FD->getTemplateSpecializationKind()) {
626890e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis    case TSK_Undeclared:
626990e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis    case TSK_ExplicitSpecialization:
627090e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis      External = GVA_StrongExternal;
627190e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis      break;
627290e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis
627390e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis    case TSK_ExplicitInstantiationDefinition:
627490e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis      return GVA_ExplicitTemplateInstantiation;
627590e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis
627690e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis    case TSK_ExplicitInstantiationDeclaration:
627790e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis    case TSK_ImplicitInstantiation:
627890e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis      External = GVA_TemplateInstantiation;
627990e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis      break;
628090e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis    }
628190e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis  }
628290e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis
628390e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis  if (!FD->isInlined())
628490e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis    return External;
628590e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis
628690e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis  if (!getLangOptions().CPlusPlus || FD->hasAttr<GNUInlineAttr>()) {
628790e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis    // GNU or C99 inline semantics. Determine whether this symbol should be
628890e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis    // externally visible.
628990e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis    if (FD->isInlineDefinitionExternallyVisible())
629090e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis      return External;
629190e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis
629290e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis    // C99 inline semantics, where the symbol is not externally visible.
629390e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis    return GVA_C99Inline;
629490e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis  }
629590e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis
629690e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis  // C++0x [temp.explicit]p9:
629790e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis  //   [ Note: The intent is that an inline function that is the subject of
629890e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis  //   an explicit instantiation declaration will still be implicitly
629990e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis  //   instantiated when used so that the body can be considered for
630090e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis  //   inlining, but that no out-of-line copy of the inline function would be
630190e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis  //   generated in the translation unit. -- end note ]
630290e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis  if (FD->getTemplateSpecializationKind()
630390e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis                                       == TSK_ExplicitInstantiationDeclaration)
630490e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis    return GVA_C99Inline;
630590e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis
630690e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis  return GVA_CXXInline;
630790e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis}
630890e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis
630990e99a84ddd020e8fda79643748243725a2ed071Argyrios KyrtzidisGVALinkage ASTContext::GetGVALinkageForVariable(const VarDecl *VD) {
631090e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis  // If this is a static data member, compute the kind of template
631190e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis  // specialization. Otherwise, this variable is not part of a
631290e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis  // template.
631390e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis  TemplateSpecializationKind TSK = TSK_Undeclared;
631490e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis  if (VD->isStaticDataMember())
631590e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis    TSK = VD->getTemplateSpecializationKind();
631690e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis
631790e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis  Linkage L = VD->getLinkage();
631890e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis  if (L == ExternalLinkage && getLangOptions().CPlusPlus &&
631990e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis      VD->getType()->getLinkage() == UniqueExternalLinkage)
632090e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis    L = UniqueExternalLinkage;
632190e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis
632290e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis  switch (L) {
632390e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis  case NoLinkage:
632490e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis  case InternalLinkage:
632590e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis  case UniqueExternalLinkage:
632690e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis    return GVA_Internal;
632790e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis
632890e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis  case ExternalLinkage:
632990e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis    switch (TSK) {
633090e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis    case TSK_Undeclared:
633190e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis    case TSK_ExplicitSpecialization:
633290e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis      return GVA_StrongExternal;
633390e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis
633490e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis    case TSK_ExplicitInstantiationDeclaration:
633590e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis      llvm_unreachable("Variable should not be instantiated");
633690e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis      // Fall through to treat this like any other instantiation.
633790e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis
633890e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis    case TSK_ExplicitInstantiationDefinition:
633990e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis      return GVA_ExplicitTemplateInstantiation;
634090e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis
634190e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis    case TSK_ImplicitInstantiation:
634290e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis      return GVA_TemplateInstantiation;
634390e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis    }
634490e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis  }
634590e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis
634690e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis  return GVA_StrongExternal;
634790e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis}
634890e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis
63494ac7c0bb39696e92fd220118fedc484c09a69870Argyrios Kyrtzidisbool ASTContext::DeclMustBeEmitted(const Decl *D) {
635090e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis  if (const VarDecl *VD = dyn_cast<VarDecl>(D)) {
635190e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis    if (!VD->isFileVarDecl())
635290e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis      return false;
635390e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis  } else if (!isa<FunctionDecl>(D))
635490e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis    return false;
635590e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis
6356ab411c8c2efed8f2403bf8596e780c0f2f905a19Argyrios Kyrtzidis  // Weak references don't produce any output by themselves.
6357ab411c8c2efed8f2403bf8596e780c0f2f905a19Argyrios Kyrtzidis  if (D->hasAttr<WeakRefAttr>())
6358ab411c8c2efed8f2403bf8596e780c0f2f905a19Argyrios Kyrtzidis    return false;
6359ab411c8c2efed8f2403bf8596e780c0f2f905a19Argyrios Kyrtzidis
636090e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis  // Aliases and used decls are required.
636190e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis  if (D->hasAttr<AliasAttr>() || D->hasAttr<UsedAttr>())
636290e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis    return true;
636390e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis
636490e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis  if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
636590e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis    // Forward declarations aren't required.
636610620eb5164e31208fcbf0437cd79ae535ed0559Sean Hunt    if (!FD->doesThisDeclarationHaveABody())
6367dce67a70a86db8758c926a76fdd980f5369d5746Nick Lewycky      return FD->doesDeclarationForceExternallyVisibleDefinition();
636890e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis
636990e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis    // Constructors and destructors are required.
637090e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis    if (FD->hasAttr<ConstructorAttr>() || FD->hasAttr<DestructorAttr>())
637190e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis      return true;
637290e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis
637390e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis    // The key function for a class is required.
637490e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis    if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(FD)) {
637590e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis      const CXXRecordDecl *RD = MD->getParent();
637690e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis      if (MD->isOutOfLine() && RD->isDynamicClass()) {
637790e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis        const CXXMethodDecl *KeyFunc = getKeyFunction(RD);
637890e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis        if (KeyFunc && KeyFunc->getCanonicalDecl() == MD->getCanonicalDecl())
637990e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis          return true;
638090e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis      }
638190e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis    }
638290e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis
638390e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis    GVALinkage Linkage = GetGVALinkageForFunction(FD);
638490e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis
638590e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis    // static, static inline, always_inline, and extern inline functions can
638690e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis    // always be deferred.  Normal inline functions can be deferred in C99/C++.
638790e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis    // Implicit template instantiations can also be deferred in C++.
638890e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis    if (Linkage == GVA_Internal  || Linkage == GVA_C99Inline ||
638990e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis        Linkage == GVA_CXXInline || Linkage == GVA_TemplateInstantiation)
639090e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis      return false;
639190e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis    return true;
639290e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis  }
639390e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis
639490e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis  const VarDecl *VD = cast<VarDecl>(D);
639590e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis  assert(VD->isFileVarDecl() && "Expected file scoped var");
639690e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis
6397ab411c8c2efed8f2403bf8596e780c0f2f905a19Argyrios Kyrtzidis  if (VD->isThisDeclarationADefinition() == VarDecl::DeclarationOnly)
6398ab411c8c2efed8f2403bf8596e780c0f2f905a19Argyrios Kyrtzidis    return false;
6399ab411c8c2efed8f2403bf8596e780c0f2f905a19Argyrios Kyrtzidis
640090e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis  // Structs that have non-trivial constructors or destructors are required.
640190e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis
640290e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis  // FIXME: Handle references.
6403023df37c27ee8035664fb62f206ca58f4e2a169dSean Hunt  // FIXME: Be more selective about which constructors we care about.
640490e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis  if (const RecordType *RT = VD->getType()->getAs<RecordType>()) {
640590e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis    if (const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(RT->getDecl())) {
6406023df37c27ee8035664fb62f206ca58f4e2a169dSean Hunt      if (RD->hasDefinition() && !(RD->hasTrivialDefaultConstructor() &&
6407023df37c27ee8035664fb62f206ca58f4e2a169dSean Hunt                                   RD->hasTrivialCopyConstructor() &&
6408023df37c27ee8035664fb62f206ca58f4e2a169dSean Hunt                                   RD->hasTrivialMoveConstructor() &&
6409023df37c27ee8035664fb62f206ca58f4e2a169dSean Hunt                                   RD->hasTrivialDestructor()))
641090e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis        return true;
641190e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis    }
641290e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis  }
641390e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis
641490e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis  GVALinkage L = GetGVALinkageForVariable(VD);
641590e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis  if (L == GVA_Internal || L == GVA_TemplateInstantiation) {
641690e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis    if (!(VD->getInit() && VD->getInit()->HasSideEffects(*this)))
641790e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis      return false;
641890e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis  }
641990e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis
642090e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis  return true;
642190e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis}
6422071cc7deffad608165b1ddd5263e8bf181861520Charles Davis
6423ee743f903858e337434ac0335f147f4de4ecae05Charles DavisCallingConv ASTContext::getDefaultMethodCallConv() {
6424ee743f903858e337434ac0335f147f4de4ecae05Charles Davis  // Pass through to the C++ ABI object
6425ee743f903858e337434ac0335f147f4de4ecae05Charles Davis  return ABI->getDefaultMethodCallConv();
6426ee743f903858e337434ac0335f147f4de4ecae05Charles Davis}
6427ee743f903858e337434ac0335f147f4de4ecae05Charles Davis
64284ba2a17694148e16eaa8d3917f657ffcd3667be4Jay Foadbool ASTContext::isNearlyEmpty(const CXXRecordDecl *RD) const {
6429dae0cb52e4e3d46bbfc9a4510909522197a92e54Anders Carlsson  // Pass through to the C++ ABI object
6430dae0cb52e4e3d46bbfc9a4510909522197a92e54Anders Carlsson  return ABI->isNearlyEmpty(RD);
6431dae0cb52e4e3d46bbfc9a4510909522197a92e54Anders Carlsson}
6432dae0cb52e4e3d46bbfc9a4510909522197a92e54Anders Carlsson
643314110477887e3dc168ffc6c191e72d705051f99ePeter CollingbourneMangleContext *ASTContext::createMangleContext() {
643414110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne  switch (Target.getCXXABI()) {
643514110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne  case CXXABI_ARM:
643614110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne  case CXXABI_Itanium:
643714110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne    return createItaniumMangleContext(*this, getDiagnostics());
643814110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne  case CXXABI_Microsoft:
643914110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne    return createMicrosoftMangleContext(*this, getDiagnostics());
644014110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne  }
644114110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne  assert(0 && "Unsupported ABI");
644214110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne  return 0;
644314110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne}
644414110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne
6445071cc7deffad608165b1ddd5263e8bf181861520Charles DavisCXXABI::~CXXABI() {}
6446ba29bd25515fbd99e98ba0fedb9d93617b27609eTed Kremenek
6447ba29bd25515fbd99e98ba0fedb9d93617b27609eTed Kremeneksize_t ASTContext::getSideTableAllocatedMemory() const {
64480c8cd1ac55720276ad41fa7b4f8785bead5afd32Ted Kremenek  return ASTRecordLayouts.getMemorySize()
64490c8cd1ac55720276ad41fa7b4f8785bead5afd32Ted Kremenek    + llvm::capacity_in_bytes(ObjCLayouts)
64500c8cd1ac55720276ad41fa7b4f8785bead5afd32Ted Kremenek    + llvm::capacity_in_bytes(KeyFunctions)
64510c8cd1ac55720276ad41fa7b4f8785bead5afd32Ted Kremenek    + llvm::capacity_in_bytes(ObjCImpls)
64520c8cd1ac55720276ad41fa7b4f8785bead5afd32Ted Kremenek    + llvm::capacity_in_bytes(BlockVarCopyInits)
64530c8cd1ac55720276ad41fa7b4f8785bead5afd32Ted Kremenek    + llvm::capacity_in_bytes(DeclAttrs)
64540c8cd1ac55720276ad41fa7b4f8785bead5afd32Ted Kremenek    + llvm::capacity_in_bytes(InstantiatedFromStaticDataMember)
64550c8cd1ac55720276ad41fa7b4f8785bead5afd32Ted Kremenek    + llvm::capacity_in_bytes(InstantiatedFromUsingDecl)
64560c8cd1ac55720276ad41fa7b4f8785bead5afd32Ted Kremenek    + llvm::capacity_in_bytes(InstantiatedFromUsingShadowDecl)
64570c8cd1ac55720276ad41fa7b4f8785bead5afd32Ted Kremenek    + llvm::capacity_in_bytes(InstantiatedFromUnnamedFieldDecl)
64580c8cd1ac55720276ad41fa7b4f8785bead5afd32Ted Kremenek    + llvm::capacity_in_bytes(OverriddenMethods)
64590c8cd1ac55720276ad41fa7b4f8785bead5afd32Ted Kremenek    + llvm::capacity_in_bytes(Types)
6460af0f4d0b2e38c810effc8b024ad2fb6604eec5d3Francois Pichet    + llvm::capacity_in_bytes(VariableArrayTypes)
6461af0f4d0b2e38c810effc8b024ad2fb6604eec5d3Francois Pichet    + llvm::capacity_in_bytes(ClassScopeSpecilizationPattern);
6462ba29bd25515fbd99e98ba0fedb9d93617b27609eTed Kremenek}
6463