ASTContext.cpp revision 8bdb58a7835a9a90dd9b9791fccf269cbc1dcef3
15f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//===--- ASTContext.cpp - Context to hold long-lived AST nodes ------------===//
25f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//
35f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//                     The LLVM Compiler Infrastructure
45f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//
50bc735ffcfb223c0186419547abaa5c84482663eChris Lattner// This file is distributed under the University of Illinois Open Source
60bc735ffcfb223c0186419547abaa5c84482663eChris Lattner// License. See LICENSE.TXT for details.
75f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//
85f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//===----------------------------------------------------------------------===//
95f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//
105f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//  This file implements the ASTContext interface.
115f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//
125f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//===----------------------------------------------------------------------===//
135f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
145f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer#include "clang/AST/ASTContext.h"
15bdc601b196c48d4cd56a5ceb45d41ae4e87371abKen Dyck#include "clang/AST/CharUnits.h"
1649aa7ff1245abd03e6e998e01302df31e4c6f8f6Argyrios Kyrtzidis#include "clang/AST/DeclCXX.h"
17980e508ca70d6de75d2abfd96b4681fc98bb2698Steve Naroff#include "clang/AST/DeclObjC.h"
18aaba5e346dffdbad5d1c42765a89e4a7afb0da67Douglas Gregor#include "clang/AST/DeclTemplate.h"
19b17166c8077cd900cca83a895c43b30ea6660598Argyrios Kyrtzidis#include "clang/AST/TypeLoc.h"
20e91593ef084479340582b2ba177b44be50a717b7Daniel Dunbar#include "clang/AST/Expr.h"
21ea1471e0e967548c596a71469702f8846dbaf3c0John McCall#include "clang/AST/ExprCXX.h"
222cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor#include "clang/AST/ExternalASTSource.h"
237b90340c9c7d07aef4e301e72b5e8a30d5f4f0c8Argyrios Kyrtzidis#include "clang/AST/ASTMutationListener.h"
2419cc4abea06a9b49e0e16a50d335c064cd723572Anders Carlsson#include "clang/AST/RecordLayout.h"
2514110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne#include "clang/AST/Mangle.h"
261b63e4f732dbc73d90abf886b4d21f8e3a165f6dChris Lattner#include "clang/Basic/Builtins.h"
27a9376d470ccb0eac74fe09a6b2a18a890f1d17c4Chris Lattner#include "clang/Basic/SourceManager.h"
285f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer#include "clang/Basic/TargetInfo.h"
29f5942a44880be26878592eb052b737579349411eBenjamin Kramer#include "llvm/ADT/SmallString.h"
3085f9bceab1542aafff012d4d28e998f4ba16e362Anders Carlsson#include "llvm/ADT/StringExtras.h"
316fe7c8aa8c7546743ecd0ac0138c2cf5d8155386Nate Begeman#include "llvm/Support/MathExtras.h"
32f5942a44880be26878592eb052b737579349411eBenjamin Kramer#include "llvm/Support/raw_ostream.h"
330c8cd1ac55720276ad41fa7b4f8785bead5afd32Ted Kremenek#include "llvm/Support/Capacity.h"
34071cc7deffad608165b1ddd5263e8bf181861520Charles Davis#include "CXXABI.h"
352636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis#include <map>
3629445a0542d128cd7ee587ee52229670b9b54a12Anders Carlsson
375f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencerusing namespace clang;
385f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
391827403a7138946305c0058f262e02b595cf882fDouglas Gregorunsigned ASTContext::NumImplicitDefaultConstructors;
401827403a7138946305c0058f262e02b595cf882fDouglas Gregorunsigned ASTContext::NumImplicitDefaultConstructorsDeclared;
41225843186e3972ce798d3de00f86da9008b15a0eDouglas Gregorunsigned ASTContext::NumImplicitCopyConstructors;
42225843186e3972ce798d3de00f86da9008b15a0eDouglas Gregorunsigned ASTContext::NumImplicitCopyConstructorsDeclared;
43ffe37fdda5b4b4f162a45155c30d9f60ce110c12Sean Huntunsigned ASTContext::NumImplicitMoveConstructors;
44ffe37fdda5b4b4f162a45155c30d9f60ce110c12Sean Huntunsigned ASTContext::NumImplicitMoveConstructorsDeclared;
45a376d10acfacf19d6dfa41069f7929739a18dd7aDouglas Gregorunsigned ASTContext::NumImplicitCopyAssignmentOperators;
46a376d10acfacf19d6dfa41069f7929739a18dd7aDouglas Gregorunsigned ASTContext::NumImplicitCopyAssignmentOperatorsDeclared;
47ffe37fdda5b4b4f162a45155c30d9f60ce110c12Sean Huntunsigned ASTContext::NumImplicitMoveAssignmentOperators;
48ffe37fdda5b4b4f162a45155c30d9f60ce110c12Sean Huntunsigned ASTContext::NumImplicitMoveAssignmentOperatorsDeclared;
494923aa25eb39d64106a5817c02d560a3aecf8b2cDouglas Gregorunsigned ASTContext::NumImplicitDestructors;
504923aa25eb39d64106a5817c02d560a3aecf8b2cDouglas Gregorunsigned ASTContext::NumImplicitDestructorsDeclared;
514923aa25eb39d64106a5817c02d560a3aecf8b2cDouglas Gregor
525f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencerenum FloatingRank {
53aa4a99b4a62615db243f7a5c433169f2fc704420Anton Korobeynikov  HalfRank, FloatRank, DoubleRank, LongDoubleRank
545f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer};
555f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
56aa0cd85838f2a024e589ea4e8c2094130065af21Dmitri Gribenkoconst RawComment *ASTContext::getRawCommentForDeclNoCache(const Decl *D) const {
57aa0cd85838f2a024e589ea4e8c2094130065af21Dmitri Gribenko  if (!CommentsLoaded && ExternalSource) {
58aa0cd85838f2a024e589ea4e8c2094130065af21Dmitri Gribenko    ExternalSource->ReadComments();
59aa0cd85838f2a024e589ea4e8c2094130065af21Dmitri Gribenko    CommentsLoaded = true;
60aa0cd85838f2a024e589ea4e8c2094130065af21Dmitri Gribenko  }
61aa0cd85838f2a024e589ea4e8c2094130065af21Dmitri Gribenko
62aa0cd85838f2a024e589ea4e8c2094130065af21Dmitri Gribenko  assert(D);
63aa0cd85838f2a024e589ea4e8c2094130065af21Dmitri Gribenko
64aa0cd85838f2a024e589ea4e8c2094130065af21Dmitri Gribenko  // TODO: handle comments for function parameters properly.
65aa0cd85838f2a024e589ea4e8c2094130065af21Dmitri Gribenko  if (isa<ParmVarDecl>(D))
66aa0cd85838f2a024e589ea4e8c2094130065af21Dmitri Gribenko    return NULL;
67aa0cd85838f2a024e589ea4e8c2094130065af21Dmitri Gribenko
68aa0cd85838f2a024e589ea4e8c2094130065af21Dmitri Gribenko  ArrayRef<RawComment> RawComments = Comments.getComments();
69aa0cd85838f2a024e589ea4e8c2094130065af21Dmitri Gribenko
70aa0cd85838f2a024e589ea4e8c2094130065af21Dmitri Gribenko  // If there are no comments anywhere, we won't find anything.
71aa0cd85838f2a024e589ea4e8c2094130065af21Dmitri Gribenko  if (RawComments.empty())
72aa0cd85838f2a024e589ea4e8c2094130065af21Dmitri Gribenko    return NULL;
73aa0cd85838f2a024e589ea4e8c2094130065af21Dmitri Gribenko
74aa0cd85838f2a024e589ea4e8c2094130065af21Dmitri Gribenko  // If the declaration doesn't map directly to a location in a file, we
75aa0cd85838f2a024e589ea4e8c2094130065af21Dmitri Gribenko  // can't find the comment.
76aa0cd85838f2a024e589ea4e8c2094130065af21Dmitri Gribenko  SourceLocation DeclLoc = D->getLocation();
77aa0cd85838f2a024e589ea4e8c2094130065af21Dmitri Gribenko  if (DeclLoc.isInvalid() || !DeclLoc.isFileID())
78aa0cd85838f2a024e589ea4e8c2094130065af21Dmitri Gribenko    return NULL;
79aa0cd85838f2a024e589ea4e8c2094130065af21Dmitri Gribenko
80aa0cd85838f2a024e589ea4e8c2094130065af21Dmitri Gribenko  // Find the comment that occurs just after this declaration.
81aa0cd85838f2a024e589ea4e8c2094130065af21Dmitri Gribenko  ArrayRef<RawComment>::iterator Comment
82aa0cd85838f2a024e589ea4e8c2094130065af21Dmitri Gribenko      = std::lower_bound(RawComments.begin(),
83aa0cd85838f2a024e589ea4e8c2094130065af21Dmitri Gribenko                         RawComments.end(),
849530a8bf275f91f95147e1fb205dc85bea1ae45cDmitri Gribenko                         RawComment(SourceMgr, SourceRange(DeclLoc)),
85aa0cd85838f2a024e589ea4e8c2094130065af21Dmitri Gribenko                         BeforeThanCompare<RawComment>(SourceMgr));
86aa0cd85838f2a024e589ea4e8c2094130065af21Dmitri Gribenko
87aa0cd85838f2a024e589ea4e8c2094130065af21Dmitri Gribenko  // Decompose the location for the declaration and find the beginning of the
88aa0cd85838f2a024e589ea4e8c2094130065af21Dmitri Gribenko  // file buffer.
89aa0cd85838f2a024e589ea4e8c2094130065af21Dmitri Gribenko  std::pair<FileID, unsigned> DeclLocDecomp = SourceMgr.getDecomposedLoc(DeclLoc);
90aa0cd85838f2a024e589ea4e8c2094130065af21Dmitri Gribenko
91aa0cd85838f2a024e589ea4e8c2094130065af21Dmitri Gribenko  // First check whether we have a trailing comment.
92aa0cd85838f2a024e589ea4e8c2094130065af21Dmitri Gribenko  if (Comment != RawComments.end() &&
932d44d77fed3200e2eff289f55493317e90d3398cDmitri Gribenko      Comment->isDocumentation() && Comment->isTrailingComment() &&
94aa0cd85838f2a024e589ea4e8c2094130065af21Dmitri Gribenko      !isa<TagDecl>(D) && !isa<NamespaceDecl>(D)) {
95aa0cd85838f2a024e589ea4e8c2094130065af21Dmitri Gribenko    std::pair<FileID, unsigned> CommentBeginDecomp
96aa0cd85838f2a024e589ea4e8c2094130065af21Dmitri Gribenko      = SourceMgr.getDecomposedLoc(Comment->getSourceRange().getBegin());
97aa0cd85838f2a024e589ea4e8c2094130065af21Dmitri Gribenko    // Check that Doxygen trailing comment comes after the declaration, starts
98aa0cd85838f2a024e589ea4e8c2094130065af21Dmitri Gribenko    // on the same line and in the same file as the declaration.
99aa0cd85838f2a024e589ea4e8c2094130065af21Dmitri Gribenko    if (DeclLocDecomp.first == CommentBeginDecomp.first &&
100aa0cd85838f2a024e589ea4e8c2094130065af21Dmitri Gribenko        SourceMgr.getLineNumber(DeclLocDecomp.first, DeclLocDecomp.second)
101aa0cd85838f2a024e589ea4e8c2094130065af21Dmitri Gribenko          == SourceMgr.getLineNumber(CommentBeginDecomp.first,
102aa0cd85838f2a024e589ea4e8c2094130065af21Dmitri Gribenko                                     CommentBeginDecomp.second)) {
103aa0cd85838f2a024e589ea4e8c2094130065af21Dmitri Gribenko      return &*Comment;
104aa0cd85838f2a024e589ea4e8c2094130065af21Dmitri Gribenko    }
105aa0cd85838f2a024e589ea4e8c2094130065af21Dmitri Gribenko  }
106aa0cd85838f2a024e589ea4e8c2094130065af21Dmitri Gribenko
107aa0cd85838f2a024e589ea4e8c2094130065af21Dmitri Gribenko  // The comment just after the declaration was not a trailing comment.
108aa0cd85838f2a024e589ea4e8c2094130065af21Dmitri Gribenko  // Let's look at the previous comment.
109aa0cd85838f2a024e589ea4e8c2094130065af21Dmitri Gribenko  if (Comment == RawComments.begin())
110aa0cd85838f2a024e589ea4e8c2094130065af21Dmitri Gribenko    return NULL;
111aa0cd85838f2a024e589ea4e8c2094130065af21Dmitri Gribenko  --Comment;
112aa0cd85838f2a024e589ea4e8c2094130065af21Dmitri Gribenko
113aa0cd85838f2a024e589ea4e8c2094130065af21Dmitri Gribenko  // Check that we actually have a non-member Doxygen comment.
1142d44d77fed3200e2eff289f55493317e90d3398cDmitri Gribenko  if (!Comment->isDocumentation() || Comment->isTrailingComment())
115aa0cd85838f2a024e589ea4e8c2094130065af21Dmitri Gribenko    return NULL;
116aa0cd85838f2a024e589ea4e8c2094130065af21Dmitri Gribenko
117aa0cd85838f2a024e589ea4e8c2094130065af21Dmitri Gribenko  // Decompose the end of the comment.
118aa0cd85838f2a024e589ea4e8c2094130065af21Dmitri Gribenko  std::pair<FileID, unsigned> CommentEndDecomp
119aa0cd85838f2a024e589ea4e8c2094130065af21Dmitri Gribenko    = SourceMgr.getDecomposedLoc(Comment->getSourceRange().getEnd());
120aa0cd85838f2a024e589ea4e8c2094130065af21Dmitri Gribenko
121aa0cd85838f2a024e589ea4e8c2094130065af21Dmitri Gribenko  // If the comment and the declaration aren't in the same file, then they
122aa0cd85838f2a024e589ea4e8c2094130065af21Dmitri Gribenko  // aren't related.
123aa0cd85838f2a024e589ea4e8c2094130065af21Dmitri Gribenko  if (DeclLocDecomp.first != CommentEndDecomp.first)
124aa0cd85838f2a024e589ea4e8c2094130065af21Dmitri Gribenko    return NULL;
125aa0cd85838f2a024e589ea4e8c2094130065af21Dmitri Gribenko
126aa0cd85838f2a024e589ea4e8c2094130065af21Dmitri Gribenko  // Get the corresponding buffer.
127aa0cd85838f2a024e589ea4e8c2094130065af21Dmitri Gribenko  bool Invalid = false;
128aa0cd85838f2a024e589ea4e8c2094130065af21Dmitri Gribenko  const char *Buffer = SourceMgr.getBufferData(DeclLocDecomp.first,
129aa0cd85838f2a024e589ea4e8c2094130065af21Dmitri Gribenko                                               &Invalid).data();
130aa0cd85838f2a024e589ea4e8c2094130065af21Dmitri Gribenko  if (Invalid)
131aa0cd85838f2a024e589ea4e8c2094130065af21Dmitri Gribenko    return NULL;
132aa0cd85838f2a024e589ea4e8c2094130065af21Dmitri Gribenko
133aa0cd85838f2a024e589ea4e8c2094130065af21Dmitri Gribenko  // Extract text between the comment and declaration.
134aa0cd85838f2a024e589ea4e8c2094130065af21Dmitri Gribenko  StringRef Text(Buffer + CommentEndDecomp.second,
135aa0cd85838f2a024e589ea4e8c2094130065af21Dmitri Gribenko                 DeclLocDecomp.second - CommentEndDecomp.second);
136aa0cd85838f2a024e589ea4e8c2094130065af21Dmitri Gribenko
1378bdb58a7835a9a90dd9b9791fccf269cbc1dcef3Dmitri Gribenko  // There should be no other declarations or preprocessor directives between
1388bdb58a7835a9a90dd9b9791fccf269cbc1dcef3Dmitri Gribenko  // comment and declaration.
1398bdb58a7835a9a90dd9b9791fccf269cbc1dcef3Dmitri Gribenko  if (Text.find_first_of(",;{}#") != StringRef::npos)
140aa0cd85838f2a024e589ea4e8c2094130065af21Dmitri Gribenko    return NULL;
141aa0cd85838f2a024e589ea4e8c2094130065af21Dmitri Gribenko
142aa0cd85838f2a024e589ea4e8c2094130065af21Dmitri Gribenko  return &*Comment;
143aa0cd85838f2a024e589ea4e8c2094130065af21Dmitri Gribenko}
144aa0cd85838f2a024e589ea4e8c2094130065af21Dmitri Gribenko
145aa0cd85838f2a024e589ea4e8c2094130065af21Dmitri Gribenkoconst RawComment *ASTContext::getRawCommentForDecl(const Decl *D) const {
146aa0cd85838f2a024e589ea4e8c2094130065af21Dmitri Gribenko  // Check whether we have cached a comment string for this declaration
147aa0cd85838f2a024e589ea4e8c2094130065af21Dmitri Gribenko  // already.
148aa0cd85838f2a024e589ea4e8c2094130065af21Dmitri Gribenko  llvm::DenseMap<const Decl *, const RawComment *>::iterator Pos
149aa0cd85838f2a024e589ea4e8c2094130065af21Dmitri Gribenko      = DeclComments.find(D);
150aa0cd85838f2a024e589ea4e8c2094130065af21Dmitri Gribenko  if (Pos != DeclComments.end())
151aa0cd85838f2a024e589ea4e8c2094130065af21Dmitri Gribenko      return Pos->second;
152aa0cd85838f2a024e589ea4e8c2094130065af21Dmitri Gribenko
153aa0cd85838f2a024e589ea4e8c2094130065af21Dmitri Gribenko  const RawComment *RC = getRawCommentForDeclNoCache(D);
154aa0cd85838f2a024e589ea4e8c2094130065af21Dmitri Gribenko  DeclComments[D] = RC;
155aa0cd85838f2a024e589ea4e8c2094130065af21Dmitri Gribenko  return RC;
156aa0cd85838f2a024e589ea4e8c2094130065af21Dmitri Gribenko}
157aa0cd85838f2a024e589ea4e8c2094130065af21Dmitri Gribenko
1583e1274f2b99cb99c03cc8e2c6517c37d330b597aDouglas Gregorvoid
1593e1274f2b99cb99c03cc8e2c6517c37d330b597aDouglas GregorASTContext::CanonicalTemplateTemplateParm::Profile(llvm::FoldingSetNodeID &ID,
1603e1274f2b99cb99c03cc8e2c6517c37d330b597aDouglas Gregor                                               TemplateTemplateParmDecl *Parm) {
1613e1274f2b99cb99c03cc8e2c6517c37d330b597aDouglas Gregor  ID.AddInteger(Parm->getDepth());
1623e1274f2b99cb99c03cc8e2c6517c37d330b597aDouglas Gregor  ID.AddInteger(Parm->getPosition());
16361c4d28e36cd3f1be392cb77f07436d1fa6b0f9fDouglas Gregor  ID.AddBoolean(Parm->isParameterPack());
1643e1274f2b99cb99c03cc8e2c6517c37d330b597aDouglas Gregor
1653e1274f2b99cb99c03cc8e2c6517c37d330b597aDouglas Gregor  TemplateParameterList *Params = Parm->getTemplateParameters();
1663e1274f2b99cb99c03cc8e2c6517c37d330b597aDouglas Gregor  ID.AddInteger(Params->size());
1673e1274f2b99cb99c03cc8e2c6517c37d330b597aDouglas Gregor  for (TemplateParameterList::const_iterator P = Params->begin(),
1683e1274f2b99cb99c03cc8e2c6517c37d330b597aDouglas Gregor                                          PEnd = Params->end();
1693e1274f2b99cb99c03cc8e2c6517c37d330b597aDouglas Gregor       P != PEnd; ++P) {
1703e1274f2b99cb99c03cc8e2c6517c37d330b597aDouglas Gregor    if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(*P)) {
1713e1274f2b99cb99c03cc8e2c6517c37d330b597aDouglas Gregor      ID.AddInteger(0);
1723e1274f2b99cb99c03cc8e2c6517c37d330b597aDouglas Gregor      ID.AddBoolean(TTP->isParameterPack());
1733e1274f2b99cb99c03cc8e2c6517c37d330b597aDouglas Gregor      continue;
1743e1274f2b99cb99c03cc8e2c6517c37d330b597aDouglas Gregor    }
1753e1274f2b99cb99c03cc8e2c6517c37d330b597aDouglas Gregor
1763e1274f2b99cb99c03cc8e2c6517c37d330b597aDouglas Gregor    if (NonTypeTemplateParmDecl *NTTP = dyn_cast<NonTypeTemplateParmDecl>(*P)) {
1773e1274f2b99cb99c03cc8e2c6517c37d330b597aDouglas Gregor      ID.AddInteger(1);
17861c4d28e36cd3f1be392cb77f07436d1fa6b0f9fDouglas Gregor      ID.AddBoolean(NTTP->isParameterPack());
1799e9c454b12671a624f666fc6fbf132fdf183effcEli Friedman      ID.AddPointer(NTTP->getType().getCanonicalType().getAsOpaquePtr());
1806952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor      if (NTTP->isExpandedParameterPack()) {
1816952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor        ID.AddBoolean(true);
1826952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor        ID.AddInteger(NTTP->getNumExpansionTypes());
1839e9c454b12671a624f666fc6fbf132fdf183effcEli Friedman        for (unsigned I = 0, N = NTTP->getNumExpansionTypes(); I != N; ++I) {
1849e9c454b12671a624f666fc6fbf132fdf183effcEli Friedman          QualType T = NTTP->getExpansionType(I);
1859e9c454b12671a624f666fc6fbf132fdf183effcEli Friedman          ID.AddPointer(T.getCanonicalType().getAsOpaquePtr());
1869e9c454b12671a624f666fc6fbf132fdf183effcEli Friedman        }
1876952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor      } else
1886952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor        ID.AddBoolean(false);
1893e1274f2b99cb99c03cc8e2c6517c37d330b597aDouglas Gregor      continue;
1903e1274f2b99cb99c03cc8e2c6517c37d330b597aDouglas Gregor    }
1913e1274f2b99cb99c03cc8e2c6517c37d330b597aDouglas Gregor
1923e1274f2b99cb99c03cc8e2c6517c37d330b597aDouglas Gregor    TemplateTemplateParmDecl *TTP = cast<TemplateTemplateParmDecl>(*P);
1933e1274f2b99cb99c03cc8e2c6517c37d330b597aDouglas Gregor    ID.AddInteger(2);
1943e1274f2b99cb99c03cc8e2c6517c37d330b597aDouglas Gregor    Profile(ID, TTP);
1953e1274f2b99cb99c03cc8e2c6517c37d330b597aDouglas Gregor  }
1963e1274f2b99cb99c03cc8e2c6517c37d330b597aDouglas Gregor}
1973e1274f2b99cb99c03cc8e2c6517c37d330b597aDouglas Gregor
1983e1274f2b99cb99c03cc8e2c6517c37d330b597aDouglas GregorTemplateTemplateParmDecl *
1993e1274f2b99cb99c03cc8e2c6517c37d330b597aDouglas GregorASTContext::getCanonicalTemplateTemplateParmDecl(
2004ba2a17694148e16eaa8d3917f657ffcd3667be4Jay Foad                                          TemplateTemplateParmDecl *TTP) const {
2013e1274f2b99cb99c03cc8e2c6517c37d330b597aDouglas Gregor  // Check if we already have a canonical template template parameter.
2023e1274f2b99cb99c03cc8e2c6517c37d330b597aDouglas Gregor  llvm::FoldingSetNodeID ID;
2033e1274f2b99cb99c03cc8e2c6517c37d330b597aDouglas Gregor  CanonicalTemplateTemplateParm::Profile(ID, TTP);
2043e1274f2b99cb99c03cc8e2c6517c37d330b597aDouglas Gregor  void *InsertPos = 0;
2053e1274f2b99cb99c03cc8e2c6517c37d330b597aDouglas Gregor  CanonicalTemplateTemplateParm *Canonical
2063e1274f2b99cb99c03cc8e2c6517c37d330b597aDouglas Gregor    = CanonTemplateTemplateParms.FindNodeOrInsertPos(ID, InsertPos);
2073e1274f2b99cb99c03cc8e2c6517c37d330b597aDouglas Gregor  if (Canonical)
2083e1274f2b99cb99c03cc8e2c6517c37d330b597aDouglas Gregor    return Canonical->getParam();
2093e1274f2b99cb99c03cc8e2c6517c37d330b597aDouglas Gregor
2103e1274f2b99cb99c03cc8e2c6517c37d330b597aDouglas Gregor  // Build a canonical template parameter list.
2113e1274f2b99cb99c03cc8e2c6517c37d330b597aDouglas Gregor  TemplateParameterList *Params = TTP->getTemplateParameters();
2125f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner  SmallVector<NamedDecl *, 4> CanonParams;
2133e1274f2b99cb99c03cc8e2c6517c37d330b597aDouglas Gregor  CanonParams.reserve(Params->size());
2143e1274f2b99cb99c03cc8e2c6517c37d330b597aDouglas Gregor  for (TemplateParameterList::const_iterator P = Params->begin(),
2153e1274f2b99cb99c03cc8e2c6517c37d330b597aDouglas Gregor                                          PEnd = Params->end();
2163e1274f2b99cb99c03cc8e2c6517c37d330b597aDouglas Gregor       P != PEnd; ++P) {
2173e1274f2b99cb99c03cc8e2c6517c37d330b597aDouglas Gregor    if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(*P))
2183e1274f2b99cb99c03cc8e2c6517c37d330b597aDouglas Gregor      CanonParams.push_back(
2193e1274f2b99cb99c03cc8e2c6517c37d330b597aDouglas Gregor                  TemplateTypeParmDecl::Create(*this, getTranslationUnitDecl(),
220344577e6b58f42d18dc8118c8903b49a85dc005eAbramo Bagnara                                               SourceLocation(),
221344577e6b58f42d18dc8118c8903b49a85dc005eAbramo Bagnara                                               SourceLocation(),
222344577e6b58f42d18dc8118c8903b49a85dc005eAbramo Bagnara                                               TTP->getDepth(),
2233e1274f2b99cb99c03cc8e2c6517c37d330b597aDouglas Gregor                                               TTP->getIndex(), 0, false,
2243e1274f2b99cb99c03cc8e2c6517c37d330b597aDouglas Gregor                                               TTP->isParameterPack()));
2253e1274f2b99cb99c03cc8e2c6517c37d330b597aDouglas Gregor    else if (NonTypeTemplateParmDecl *NTTP
2266952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor             = dyn_cast<NonTypeTemplateParmDecl>(*P)) {
2276952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor      QualType T = getCanonicalType(NTTP->getType());
2286952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor      TypeSourceInfo *TInfo = getTrivialTypeSourceInfo(T);
2296952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor      NonTypeTemplateParmDecl *Param;
2306952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor      if (NTTP->isExpandedParameterPack()) {
2315f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner        SmallVector<QualType, 2> ExpandedTypes;
2325f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner        SmallVector<TypeSourceInfo *, 2> ExpandedTInfos;
2336952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor        for (unsigned I = 0, N = NTTP->getNumExpansionTypes(); I != N; ++I) {
2346952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor          ExpandedTypes.push_back(getCanonicalType(NTTP->getExpansionType(I)));
2356952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor          ExpandedTInfos.push_back(
2366952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor                                getTrivialTypeSourceInfo(ExpandedTypes.back()));
2376952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor        }
2386952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor
2396952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor        Param = NonTypeTemplateParmDecl::Create(*this, getTranslationUnitDecl(),
240ff676cb48fe8bf7be2feaa251dc7c5fb15af4730Abramo Bagnara                                                SourceLocation(),
241ff676cb48fe8bf7be2feaa251dc7c5fb15af4730Abramo Bagnara                                                SourceLocation(),
2426952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor                                                NTTP->getDepth(),
2436952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor                                                NTTP->getPosition(), 0,
2446952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor                                                T,
2456952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor                                                TInfo,
2466952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor                                                ExpandedTypes.data(),
2476952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor                                                ExpandedTypes.size(),
2486952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor                                                ExpandedTInfos.data());
2496952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor      } else {
2506952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor        Param = NonTypeTemplateParmDecl::Create(*this, getTranslationUnitDecl(),
251ff676cb48fe8bf7be2feaa251dc7c5fb15af4730Abramo Bagnara                                                SourceLocation(),
252ff676cb48fe8bf7be2feaa251dc7c5fb15af4730Abramo Bagnara                                                SourceLocation(),
2536952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor                                                NTTP->getDepth(),
2546952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor                                                NTTP->getPosition(), 0,
2556952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor                                                T,
2566952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor                                                NTTP->isParameterPack(),
2576952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor                                                TInfo);
2586952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor      }
2596952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor      CanonParams.push_back(Param);
2606952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor
2616952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor    } else
2623e1274f2b99cb99c03cc8e2c6517c37d330b597aDouglas Gregor      CanonParams.push_back(getCanonicalTemplateTemplateParmDecl(
2633e1274f2b99cb99c03cc8e2c6517c37d330b597aDouglas Gregor                                           cast<TemplateTemplateParmDecl>(*P)));
2643e1274f2b99cb99c03cc8e2c6517c37d330b597aDouglas Gregor  }
2653e1274f2b99cb99c03cc8e2c6517c37d330b597aDouglas Gregor
2663e1274f2b99cb99c03cc8e2c6517c37d330b597aDouglas Gregor  TemplateTemplateParmDecl *CanonTTP
2673e1274f2b99cb99c03cc8e2c6517c37d330b597aDouglas Gregor    = TemplateTemplateParmDecl::Create(*this, getTranslationUnitDecl(),
2683e1274f2b99cb99c03cc8e2c6517c37d330b597aDouglas Gregor                                       SourceLocation(), TTP->getDepth(),
26961c4d28e36cd3f1be392cb77f07436d1fa6b0f9fDouglas Gregor                                       TTP->getPosition(),
27061c4d28e36cd3f1be392cb77f07436d1fa6b0f9fDouglas Gregor                                       TTP->isParameterPack(),
27161c4d28e36cd3f1be392cb77f07436d1fa6b0f9fDouglas Gregor                                       0,
2723e1274f2b99cb99c03cc8e2c6517c37d330b597aDouglas Gregor                         TemplateParameterList::Create(*this, SourceLocation(),
2733e1274f2b99cb99c03cc8e2c6517c37d330b597aDouglas Gregor                                                       SourceLocation(),
2743e1274f2b99cb99c03cc8e2c6517c37d330b597aDouglas Gregor                                                       CanonParams.data(),
2753e1274f2b99cb99c03cc8e2c6517c37d330b597aDouglas Gregor                                                       CanonParams.size(),
2763e1274f2b99cb99c03cc8e2c6517c37d330b597aDouglas Gregor                                                       SourceLocation()));
2773e1274f2b99cb99c03cc8e2c6517c37d330b597aDouglas Gregor
2783e1274f2b99cb99c03cc8e2c6517c37d330b597aDouglas Gregor  // Get the new insert position for the node we care about.
2793e1274f2b99cb99c03cc8e2c6517c37d330b597aDouglas Gregor  Canonical = CanonTemplateTemplateParms.FindNodeOrInsertPos(ID, InsertPos);
2803e1274f2b99cb99c03cc8e2c6517c37d330b597aDouglas Gregor  assert(Canonical == 0 && "Shouldn't be in the map!");
2813e1274f2b99cb99c03cc8e2c6517c37d330b597aDouglas Gregor  (void)Canonical;
2823e1274f2b99cb99c03cc8e2c6517c37d330b597aDouglas Gregor
2833e1274f2b99cb99c03cc8e2c6517c37d330b597aDouglas Gregor  // Create the canonical template template parameter entry.
2843e1274f2b99cb99c03cc8e2c6517c37d330b597aDouglas Gregor  Canonical = new (*this) CanonicalTemplateTemplateParm(CanonTTP);
2853e1274f2b99cb99c03cc8e2c6517c37d330b597aDouglas Gregor  CanonTemplateTemplateParms.InsertNode(Canonical, InsertPos);
2863e1274f2b99cb99c03cc8e2c6517c37d330b597aDouglas Gregor  return CanonTTP;
2873e1274f2b99cb99c03cc8e2c6517c37d330b597aDouglas Gregor}
2883e1274f2b99cb99c03cc8e2c6517c37d330b597aDouglas Gregor
289071cc7deffad608165b1ddd5263e8bf181861520Charles DavisCXXABI *ASTContext::createCXXABI(const TargetInfo &T) {
290ee79a4c30e5d1c5285551c9a25b8ec6d45d46aa7John McCall  if (!LangOpts.CPlusPlus) return 0;
291ee79a4c30e5d1c5285551c9a25b8ec6d45d46aa7John McCall
29220cf717034ba1f20fc47c025ecb72ed9b631ad13Charles Davis  switch (T.getCXXABI()) {
293ee79a4c30e5d1c5285551c9a25b8ec6d45d46aa7John McCall  case CXXABI_ARM:
294ee79a4c30e5d1c5285551c9a25b8ec6d45d46aa7John McCall    return CreateARMCXXABI(*this);
295ee79a4c30e5d1c5285551c9a25b8ec6d45d46aa7John McCall  case CXXABI_Itanium:
296071cc7deffad608165b1ddd5263e8bf181861520Charles Davis    return CreateItaniumCXXABI(*this);
29720cf717034ba1f20fc47c025ecb72ed9b631ad13Charles Davis  case CXXABI_Microsoft:
29820cf717034ba1f20fc47c025ecb72ed9b631ad13Charles Davis    return CreateMicrosoftCXXABI(*this);
29920cf717034ba1f20fc47c025ecb72ed9b631ad13Charles Davis  }
3007530c034c0c71a64c5a9173206d9742ae847af8bDavid Blaikie  llvm_unreachable("Invalid CXXABI type!");
301071cc7deffad608165b1ddd5263e8bf181861520Charles Davis}
302071cc7deffad608165b1ddd5263e8bf181861520Charles Davis
303bcfd1f55bfbb3e5944cd5e03d07b343e280838c4Douglas Gregorstatic const LangAS::Map *getAddressSpaceMap(const TargetInfo &T,
304207f4d8543529221932af82836016a2ef066c917Peter Collingbourne                                             const LangOptions &LOpts) {
305207f4d8543529221932af82836016a2ef066c917Peter Collingbourne  if (LOpts.FakeAddressSpaceMap) {
306207f4d8543529221932af82836016a2ef066c917Peter Collingbourne    // The fake address space map must have a distinct entry for each
307207f4d8543529221932af82836016a2ef066c917Peter Collingbourne    // language-specific address space.
308207f4d8543529221932af82836016a2ef066c917Peter Collingbourne    static const unsigned FakeAddrSpaceMap[] = {
309207f4d8543529221932af82836016a2ef066c917Peter Collingbourne      1, // opencl_global
310207f4d8543529221932af82836016a2ef066c917Peter Collingbourne      2, // opencl_local
3114dc34ebf2a0716bf77ba110dab6777a3fc4397ddPeter Collingbourne      3, // opencl_constant
3124dc34ebf2a0716bf77ba110dab6777a3fc4397ddPeter Collingbourne      4, // cuda_device
3134dc34ebf2a0716bf77ba110dab6777a3fc4397ddPeter Collingbourne      5, // cuda_constant
3144dc34ebf2a0716bf77ba110dab6777a3fc4397ddPeter Collingbourne      6  // cuda_shared
315207f4d8543529221932af82836016a2ef066c917Peter Collingbourne    };
316bcfd1f55bfbb3e5944cd5e03d07b343e280838c4Douglas Gregor    return &FakeAddrSpaceMap;
317207f4d8543529221932af82836016a2ef066c917Peter Collingbourne  } else {
318bcfd1f55bfbb3e5944cd5e03d07b343e280838c4Douglas Gregor    return &T.getAddressSpaceMap();
319207f4d8543529221932af82836016a2ef066c917Peter Collingbourne  }
320207f4d8543529221932af82836016a2ef066c917Peter Collingbourne}
321207f4d8543529221932af82836016a2ef066c917Peter Collingbourne
3223e3cd93b2fd9644e970c389e715c13883faf68b6Douglas GregorASTContext::ASTContext(LangOptions& LOpts, SourceManager &SM,
323bcfd1f55bfbb3e5944cd5e03d07b343e280838c4Douglas Gregor                       const TargetInfo *t,
324e91593ef084479340582b2ba177b44be50a717b7Daniel Dunbar                       IdentifierTable &idents, SelectorTable &sels,
3251b63e4f732dbc73d90abf886b4d21f8e3a165f6dChris Lattner                       Builtin::Context &builtins,
326bcfd1f55bfbb3e5944cd5e03d07b343e280838c4Douglas Gregor                       unsigned size_reserve,
327bcfd1f55bfbb3e5944cd5e03d07b343e280838c4Douglas Gregor                       bool DelayInitialization)
328bcfd1f55bfbb3e5944cd5e03d07b343e280838c4Douglas Gregor  : FunctionProtoTypes(this_()),
329bcfd1f55bfbb3e5944cd5e03d07b343e280838c4Douglas Gregor    TemplateSpecializationTypes(this_()),
330bcfd1f55bfbb3e5944cd5e03d07b343e280838c4Douglas Gregor    DependentTemplateSpecializationTypes(this_()),
331bcfd1f55bfbb3e5944cd5e03d07b343e280838c4Douglas Gregor    SubstTemplateTemplateParmPacks(this_()),
332bcfd1f55bfbb3e5944cd5e03d07b343e280838c4Douglas Gregor    GlobalNestedNameSpecifier(0),
333bcfd1f55bfbb3e5944cd5e03d07b343e280838c4Douglas Gregor    Int128Decl(0), UInt128Decl(0),
334c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge    BuiltinVaListDecl(0),
335a6ea10e22b600d92e084f6b11b9b9a92d0eb2412Douglas Gregor    ObjCIdDecl(0), ObjCSelDecl(0), ObjCClassDecl(0), ObjCProtocolClassDecl(0),
336e97179c675b341927807c718be215c8d1aab8acbDouglas Gregor    CFConstantStringTypeDecl(0), ObjCInstanceTypeDecl(0),
337bcfd1f55bfbb3e5944cd5e03d07b343e280838c4Douglas Gregor    FILEDecl(0),
338e2d4f4ed44a32f179c6d48cd1dba8346ab2129d9Rafael Espindola    jmp_bufDecl(0), sigjmp_bufDecl(0), ucontext_tDecl(0),
339e2d4f4ed44a32f179c6d48cd1dba8346ab2129d9Rafael Espindola    BlockDescriptorType(0), BlockDescriptorExtendedType(0),
340e2d4f4ed44a32f179c6d48cd1dba8346ab2129d9Rafael Espindola    cudaConfigureCallDecl(0),
341e664977aca2a05a77abab5a06dc0fb69e870cfb9Douglas Gregor    NullTypeSourceInfo(QualType()),
342e664977aca2a05a77abab5a06dc0fb69e870cfb9Douglas Gregor    FirstLocalImport(), LastLocalImport(),
343bcfd1f55bfbb3e5944cd5e03d07b343e280838c4Douglas Gregor    SourceMgr(SM), LangOpts(LOpts),
34430c42404202d2e2512e51efc6066bd614cfdb5a4Douglas Gregor    AddrSpaceMap(0), Target(t), PrintingPolicy(LOpts),
345bcfd1f55bfbb3e5944cd5e03d07b343e280838c4Douglas Gregor    Idents(idents), Selectors(sels),
346bcfd1f55bfbb3e5944cd5e03d07b343e280838c4Douglas Gregor    BuiltinInfo(builtins),
347bcfd1f55bfbb3e5944cd5e03d07b343e280838c4Douglas Gregor    DeclarationNames(*this),
34830c42404202d2e2512e51efc6066bd614cfdb5a4Douglas Gregor    ExternalSource(0), Listener(0),
349aa0cd85838f2a024e589ea4e8c2094130065af21Dmitri Gribenko    Comments(SM), CommentsLoaded(false),
350bcfd1f55bfbb3e5944cd5e03d07b343e280838c4Douglas Gregor    LastSDM(0, 0),
351bcfd1f55bfbb3e5944cd5e03d07b343e280838c4Douglas Gregor    UniqueBlockByRefTypeID(0)
352bcfd1f55bfbb3e5944cd5e03d07b343e280838c4Douglas Gregor{
3531eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  if (size_reserve > 0) Types.reserve(size_reserve);
354e91593ef084479340582b2ba177b44be50a717b7Daniel Dunbar  TUDecl = TranslationUnitDecl::Create(*this);
355bcfd1f55bfbb3e5944cd5e03d07b343e280838c4Douglas Gregor
356bcfd1f55bfbb3e5944cd5e03d07b343e280838c4Douglas Gregor  if (!DelayInitialization) {
357bcfd1f55bfbb3e5944cd5e03d07b343e280838c4Douglas Gregor    assert(t && "No target supplied for ASTContext initialization");
358bcfd1f55bfbb3e5944cd5e03d07b343e280838c4Douglas Gregor    InitBuiltinTypes(*t);
359bcfd1f55bfbb3e5944cd5e03d07b343e280838c4Douglas Gregor  }
360e91593ef084479340582b2ba177b44be50a717b7Daniel Dunbar}
361e91593ef084479340582b2ba177b44be50a717b7Daniel Dunbar
3625f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid SpencerASTContext::~ASTContext() {
3633478eb6872d836600caf45b0f81c2065d685d6e0Ted Kremenek  // Release the DenseMaps associated with DeclContext objects.
3643478eb6872d836600caf45b0f81c2065d685d6e0Ted Kremenek  // FIXME: Is this the ideal solution?
3653478eb6872d836600caf45b0f81c2065d685d6e0Ted Kremenek  ReleaseDeclContextMaps();
3667d10b7eb670b821741b4c96f6cf7afbc3bb39abeDouglas Gregor
36763fe86bee66fc145942c56b2cc564ea0b9b9ea12Douglas Gregor  // Call all of the deallocation functions.
36863fe86bee66fc145942c56b2cc564ea0b9b9ea12Douglas Gregor  for (unsigned I = 0, N = Deallocations.size(); I != N; ++I)
36963fe86bee66fc145942c56b2cc564ea0b9b9ea12Douglas Gregor    Deallocations[I].first(Deallocations[I].second);
3700054531488928a424666ac11fcdc6bcc5112de52Douglas Gregor
371dcfcfbec478f7ed96cd8d92f30c29bd4e30d5b9cTed Kremenek  // ASTRecordLayout objects in ASTRecordLayouts must always be destroyed
37263fe86bee66fc145942c56b2cc564ea0b9b9ea12Douglas Gregor  // because they can contain DenseMaps.
37363fe86bee66fc145942c56b2cc564ea0b9b9ea12Douglas Gregor  for (llvm::DenseMap<const ObjCContainerDecl*,
37463fe86bee66fc145942c56b2cc564ea0b9b9ea12Douglas Gregor       const ASTRecordLayout*>::iterator
37563fe86bee66fc145942c56b2cc564ea0b9b9ea12Douglas Gregor       I = ObjCLayouts.begin(), E = ObjCLayouts.end(); I != E; )
37663fe86bee66fc145942c56b2cc564ea0b9b9ea12Douglas Gregor    // Increment in loop to prevent using deallocated memory.
37763fe86bee66fc145942c56b2cc564ea0b9b9ea12Douglas Gregor    if (ASTRecordLayout *R = const_cast<ASTRecordLayout*>((I++)->second))
37863fe86bee66fc145942c56b2cc564ea0b9b9ea12Douglas Gregor      R->Destroy(*this);
37963fe86bee66fc145942c56b2cc564ea0b9b9ea12Douglas Gregor
380dcfcfbec478f7ed96cd8d92f30c29bd4e30d5b9cTed Kremenek  for (llvm::DenseMap<const RecordDecl*, const ASTRecordLayout*>::iterator
381dcfcfbec478f7ed96cd8d92f30c29bd4e30d5b9cTed Kremenek       I = ASTRecordLayouts.begin(), E = ASTRecordLayouts.end(); I != E; ) {
382dcfcfbec478f7ed96cd8d92f30c29bd4e30d5b9cTed Kremenek    // Increment in loop to prevent using deallocated memory.
383dcfcfbec478f7ed96cd8d92f30c29bd4e30d5b9cTed Kremenek    if (ASTRecordLayout *R = const_cast<ASTRecordLayout*>((I++)->second))
384dcfcfbec478f7ed96cd8d92f30c29bd4e30d5b9cTed Kremenek      R->Destroy(*this);
385dcfcfbec478f7ed96cd8d92f30c29bd4e30d5b9cTed Kremenek  }
3866320064d0c60fa8683f5623881c9394fd4aa7689Douglas Gregor
3876320064d0c60fa8683f5623881c9394fd4aa7689Douglas Gregor  for (llvm::DenseMap<const Decl*, AttrVec*>::iterator A = DeclAttrs.begin(),
3886320064d0c60fa8683f5623881c9394fd4aa7689Douglas Gregor                                                    AEnd = DeclAttrs.end();
3896320064d0c60fa8683f5623881c9394fd4aa7689Douglas Gregor       A != AEnd; ++A)
3906320064d0c60fa8683f5623881c9394fd4aa7689Douglas Gregor    A->second->~AttrVec();
3916320064d0c60fa8683f5623881c9394fd4aa7689Douglas Gregor}
392ab452ba8323d1985e08bade2bced588cddf2cc28Douglas Gregor
3930054531488928a424666ac11fcdc6bcc5112de52Douglas Gregorvoid ASTContext::AddDeallocation(void (*Callback)(void*), void *Data) {
3940054531488928a424666ac11fcdc6bcc5112de52Douglas Gregor  Deallocations.push_back(std::make_pair(Callback, Data));
3950054531488928a424666ac11fcdc6bcc5112de52Douglas Gregor}
3960054531488928a424666ac11fcdc6bcc5112de52Douglas Gregor
3971eb4433ac451dc16f4133a88af2d002ac26c58efMike Stumpvoid
3986f42b62b6194f53bcbc349f5d17388e1936535d7Dylan NoblesmithASTContext::setExternalSource(OwningPtr<ExternalASTSource> &Source) {
3992cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor  ExternalSource.reset(Source.take());
4002cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor}
4012cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor
4025f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencervoid ASTContext::PrintStats() const {
403cd92a65edc7cbbbb7e3aee8d31008594de90fa51Chandler Carruth  llvm::errs() << "\n*** AST Context Stats:\n";
404cd92a65edc7cbbbb7e3aee8d31008594de90fa51Chandler Carruth  llvm::errs() << "  " << Types.size() << " types total.\n";
4057c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl
406dbe833da54e1b6192991b64fc453cd50b5ee7909Douglas Gregor  unsigned counts[] = {
4071eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump#define TYPE(Name, Parent) 0,
408dbe833da54e1b6192991b64fc453cd50b5ee7909Douglas Gregor#define ABSTRACT_TYPE(Name, Parent)
409dbe833da54e1b6192991b64fc453cd50b5ee7909Douglas Gregor#include "clang/AST/TypeNodes.def"
410dbe833da54e1b6192991b64fc453cd50b5ee7909Douglas Gregor    0 // Extra
411dbe833da54e1b6192991b64fc453cd50b5ee7909Douglas Gregor  };
412c2ee10d79f70036af652a395ac1f8273f3d04e12Douglas Gregor
4135f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  for (unsigned i = 0, e = Types.size(); i != e; ++i) {
4145f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    Type *T = Types[i];
415dbe833da54e1b6192991b64fc453cd50b5ee7909Douglas Gregor    counts[(unsigned)T->getTypeClass()]++;
4165f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
4175f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
418dbe833da54e1b6192991b64fc453cd50b5ee7909Douglas Gregor  unsigned Idx = 0;
419dbe833da54e1b6192991b64fc453cd50b5ee7909Douglas Gregor  unsigned TotalBytes = 0;
420dbe833da54e1b6192991b64fc453cd50b5ee7909Douglas Gregor#define TYPE(Name, Parent)                                              \
421dbe833da54e1b6192991b64fc453cd50b5ee7909Douglas Gregor  if (counts[Idx])                                                      \
422cd92a65edc7cbbbb7e3aee8d31008594de90fa51Chandler Carruth    llvm::errs() << "    " << counts[Idx] << " " << #Name               \
423cd92a65edc7cbbbb7e3aee8d31008594de90fa51Chandler Carruth                 << " types\n";                                         \
424dbe833da54e1b6192991b64fc453cd50b5ee7909Douglas Gregor  TotalBytes += counts[Idx] * sizeof(Name##Type);                       \
425dbe833da54e1b6192991b64fc453cd50b5ee7909Douglas Gregor  ++Idx;
426dbe833da54e1b6192991b64fc453cd50b5ee7909Douglas Gregor#define ABSTRACT_TYPE(Name, Parent)
427dbe833da54e1b6192991b64fc453cd50b5ee7909Douglas Gregor#include "clang/AST/TypeNodes.def"
4281eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
429cd92a65edc7cbbbb7e3aee8d31008594de90fa51Chandler Carruth  llvm::errs() << "Total bytes = " << TotalBytes << "\n";
430cd92a65edc7cbbbb7e3aee8d31008594de90fa51Chandler Carruth
4314923aa25eb39d64106a5817c02d560a3aecf8b2cDouglas Gregor  // Implicit special member functions.
432cd92a65edc7cbbbb7e3aee8d31008594de90fa51Chandler Carruth  llvm::errs() << NumImplicitDefaultConstructorsDeclared << "/"
433cd92a65edc7cbbbb7e3aee8d31008594de90fa51Chandler Carruth               << NumImplicitDefaultConstructors
434cd92a65edc7cbbbb7e3aee8d31008594de90fa51Chandler Carruth               << " implicit default constructors created\n";
435cd92a65edc7cbbbb7e3aee8d31008594de90fa51Chandler Carruth  llvm::errs() << NumImplicitCopyConstructorsDeclared << "/"
436cd92a65edc7cbbbb7e3aee8d31008594de90fa51Chandler Carruth               << NumImplicitCopyConstructors
437cd92a65edc7cbbbb7e3aee8d31008594de90fa51Chandler Carruth               << " implicit copy constructors created\n";
4384e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie  if (getLangOpts().CPlusPlus)
439cd92a65edc7cbbbb7e3aee8d31008594de90fa51Chandler Carruth    llvm::errs() << NumImplicitMoveConstructorsDeclared << "/"
440cd92a65edc7cbbbb7e3aee8d31008594de90fa51Chandler Carruth                 << NumImplicitMoveConstructors
441cd92a65edc7cbbbb7e3aee8d31008594de90fa51Chandler Carruth                 << " implicit move constructors created\n";
442cd92a65edc7cbbbb7e3aee8d31008594de90fa51Chandler Carruth  llvm::errs() << NumImplicitCopyAssignmentOperatorsDeclared << "/"
443cd92a65edc7cbbbb7e3aee8d31008594de90fa51Chandler Carruth               << NumImplicitCopyAssignmentOperators
444cd92a65edc7cbbbb7e3aee8d31008594de90fa51Chandler Carruth               << " implicit copy assignment operators created\n";
4454e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie  if (getLangOpts().CPlusPlus)
446cd92a65edc7cbbbb7e3aee8d31008594de90fa51Chandler Carruth    llvm::errs() << NumImplicitMoveAssignmentOperatorsDeclared << "/"
447cd92a65edc7cbbbb7e3aee8d31008594de90fa51Chandler Carruth                 << NumImplicitMoveAssignmentOperators
448cd92a65edc7cbbbb7e3aee8d31008594de90fa51Chandler Carruth                 << " implicit move assignment operators created\n";
449cd92a65edc7cbbbb7e3aee8d31008594de90fa51Chandler Carruth  llvm::errs() << NumImplicitDestructorsDeclared << "/"
450cd92a65edc7cbbbb7e3aee8d31008594de90fa51Chandler Carruth               << NumImplicitDestructors
451cd92a65edc7cbbbb7e3aee8d31008594de90fa51Chandler Carruth               << " implicit destructors created\n";
452cd92a65edc7cbbbb7e3aee8d31008594de90fa51Chandler Carruth
4532cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor  if (ExternalSource.get()) {
454cd92a65edc7cbbbb7e3aee8d31008594de90fa51Chandler Carruth    llvm::errs() << "\n";
4552cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor    ExternalSource->PrintStats();
4562cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor  }
457cd92a65edc7cbbbb7e3aee8d31008594de90fa51Chandler Carruth
45863fe86bee66fc145942c56b2cc564ea0b9b9ea12Douglas Gregor  BumpAlloc.PrintStats();
4595f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
4605f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
461772eeaefef2c883aabe35caf4543e7e32d290183Douglas GregorTypedefDecl *ASTContext::getInt128Decl() const {
462772eeaefef2c883aabe35caf4543e7e32d290183Douglas Gregor  if (!Int128Decl) {
463772eeaefef2c883aabe35caf4543e7e32d290183Douglas Gregor    TypeSourceInfo *TInfo = getTrivialTypeSourceInfo(Int128Ty);
464772eeaefef2c883aabe35caf4543e7e32d290183Douglas Gregor    Int128Decl = TypedefDecl::Create(const_cast<ASTContext &>(*this),
465772eeaefef2c883aabe35caf4543e7e32d290183Douglas Gregor                                     getTranslationUnitDecl(),
466772eeaefef2c883aabe35caf4543e7e32d290183Douglas Gregor                                     SourceLocation(),
467772eeaefef2c883aabe35caf4543e7e32d290183Douglas Gregor                                     SourceLocation(),
468772eeaefef2c883aabe35caf4543e7e32d290183Douglas Gregor                                     &Idents.get("__int128_t"),
469772eeaefef2c883aabe35caf4543e7e32d290183Douglas Gregor                                     TInfo);
470772eeaefef2c883aabe35caf4543e7e32d290183Douglas Gregor  }
471772eeaefef2c883aabe35caf4543e7e32d290183Douglas Gregor
472772eeaefef2c883aabe35caf4543e7e32d290183Douglas Gregor  return Int128Decl;
473772eeaefef2c883aabe35caf4543e7e32d290183Douglas Gregor}
474772eeaefef2c883aabe35caf4543e7e32d290183Douglas Gregor
475772eeaefef2c883aabe35caf4543e7e32d290183Douglas GregorTypedefDecl *ASTContext::getUInt128Decl() const {
476772eeaefef2c883aabe35caf4543e7e32d290183Douglas Gregor  if (!UInt128Decl) {
477772eeaefef2c883aabe35caf4543e7e32d290183Douglas Gregor    TypeSourceInfo *TInfo = getTrivialTypeSourceInfo(UnsignedInt128Ty);
478772eeaefef2c883aabe35caf4543e7e32d290183Douglas Gregor    UInt128Decl = TypedefDecl::Create(const_cast<ASTContext &>(*this),
479772eeaefef2c883aabe35caf4543e7e32d290183Douglas Gregor                                     getTranslationUnitDecl(),
480772eeaefef2c883aabe35caf4543e7e32d290183Douglas Gregor                                     SourceLocation(),
481772eeaefef2c883aabe35caf4543e7e32d290183Douglas Gregor                                     SourceLocation(),
482772eeaefef2c883aabe35caf4543e7e32d290183Douglas Gregor                                     &Idents.get("__uint128_t"),
483772eeaefef2c883aabe35caf4543e7e32d290183Douglas Gregor                                     TInfo);
484772eeaefef2c883aabe35caf4543e7e32d290183Douglas Gregor  }
485772eeaefef2c883aabe35caf4543e7e32d290183Douglas Gregor
486772eeaefef2c883aabe35caf4543e7e32d290183Douglas Gregor  return UInt128Decl;
487772eeaefef2c883aabe35caf4543e7e32d290183Douglas Gregor}
4885f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
489e27ec8ad56dbf1efb2de004b90fbbb86f740e3f1John McCallvoid ASTContext::InitBuiltinType(CanQualType &R, BuiltinType::Kind K) {
4906b304a0254a13f42390b865ff5ba668a49cc58aeJohn McCall  BuiltinType *Ty = new (*this, TypeAlignment) BuiltinType(K);
491e27ec8ad56dbf1efb2de004b90fbbb86f740e3f1John McCall  R = CanQualType::CreateUnsafe(QualType(Ty, 0));
4926b304a0254a13f42390b865ff5ba668a49cc58aeJohn McCall  Types.push_back(Ty);
4935f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
4945f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
495bcfd1f55bfbb3e5944cd5e03d07b343e280838c4Douglas Gregorvoid ASTContext::InitBuiltinTypes(const TargetInfo &Target) {
496bcfd1f55bfbb3e5944cd5e03d07b343e280838c4Douglas Gregor  assert((!this->Target || this->Target == &Target) &&
497bcfd1f55bfbb3e5944cd5e03d07b343e280838c4Douglas Gregor         "Incorrect target reinitialization");
4985f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  assert(VoidTy.isNull() && "Context reinitialized?");
4991eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
500bcfd1f55bfbb3e5944cd5e03d07b343e280838c4Douglas Gregor  this->Target = &Target;
501bcfd1f55bfbb3e5944cd5e03d07b343e280838c4Douglas Gregor
502bcfd1f55bfbb3e5944cd5e03d07b343e280838c4Douglas Gregor  ABI.reset(createCXXABI(Target));
503bcfd1f55bfbb3e5944cd5e03d07b343e280838c4Douglas Gregor  AddrSpaceMap = getAddressSpaceMap(Target, LangOpts);
504bcfd1f55bfbb3e5944cd5e03d07b343e280838c4Douglas Gregor
5055f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // C99 6.2.5p19.
5065f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  InitBuiltinType(VoidTy,              BuiltinType::Void);
5071eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
5085f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // C99 6.2.5p2.
5095f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  InitBuiltinType(BoolTy,              BuiltinType::Bool);
5105f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // C99 6.2.5p3.
51115b91764d08e886391c865c4a444d7b51141c284Eli Friedman  if (LangOpts.CharIsSigned)
5125f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    InitBuiltinType(CharTy,            BuiltinType::Char_S);
5135f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  else
5145f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    InitBuiltinType(CharTy,            BuiltinType::Char_U);
5155f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // C99 6.2.5p4.
5165f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  InitBuiltinType(SignedCharTy,        BuiltinType::SChar);
5175f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  InitBuiltinType(ShortTy,             BuiltinType::Short);
5185f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  InitBuiltinType(IntTy,               BuiltinType::Int);
5195f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  InitBuiltinType(LongTy,              BuiltinType::Long);
5205f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  InitBuiltinType(LongLongTy,          BuiltinType::LongLong);
5211eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
5225f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // C99 6.2.5p6.
5235f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  InitBuiltinType(UnsignedCharTy,      BuiltinType::UChar);
5245f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  InitBuiltinType(UnsignedShortTy,     BuiltinType::UShort);
5255f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  InitBuiltinType(UnsignedIntTy,       BuiltinType::UInt);
5265f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  InitBuiltinType(UnsignedLongTy,      BuiltinType::ULong);
5275f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  InitBuiltinType(UnsignedLongLongTy,  BuiltinType::ULongLong);
5281eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
5295f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // C99 6.2.5p10.
5305f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  InitBuiltinType(FloatTy,             BuiltinType::Float);
5315f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  InitBuiltinType(DoubleTy,            BuiltinType::Double);
5325f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  InitBuiltinType(LongDoubleTy,        BuiltinType::LongDouble);
53364c438a4be2a871fa43c78264663ba1e9788b94dArgyrios Kyrtzidis
5342df9ced9fd1e8c7d7b38443db07e0e811de22571Chris Lattner  // GNU extension, 128-bit integers.
5352df9ced9fd1e8c7d7b38443db07e0e811de22571Chris Lattner  InitBuiltinType(Int128Ty,            BuiltinType::Int128);
5362df9ced9fd1e8c7d7b38443db07e0e811de22571Chris Lattner  InitBuiltinType(UnsignedInt128Ty,    BuiltinType::UInt128);
5372df9ced9fd1e8c7d7b38443db07e0e811de22571Chris Lattner
5383f59c975aa5d047f7edd1b900b5e885c38af0ef7Chris Lattner  if (LangOpts.CPlusPlus) { // C++ 3.9.1p5
539d3d77cd138f8e830f6547b6f83fcd5721ccf5f5dEli Friedman    if (TargetInfo::isTypeSigned(Target.getWCharType()))
5403f59c975aa5d047f7edd1b900b5e885c38af0ef7Chris Lattner      InitBuiltinType(WCharTy,           BuiltinType::WChar_S);
5413f59c975aa5d047f7edd1b900b5e885c38af0ef7Chris Lattner    else  // -fshort-wchar makes wchar_t be unsigned.
5423f59c975aa5d047f7edd1b900b5e885c38af0ef7Chris Lattner      InitBuiltinType(WCharTy,           BuiltinType::WChar_U);
5433f59c975aa5d047f7edd1b900b5e885c38af0ef7Chris Lattner  } else // C99
5443a2503227c3db04a3619735127483263c1075ef7Chris Lattner    WCharTy = getFromTargetType(Target.getWCharType());
54564c438a4be2a871fa43c78264663ba1e9788b94dArgyrios Kyrtzidis
546392da48160bd92ceb486792780467cbfdb2d0e8cJames Molloy  WIntTy = getFromTargetType(Target.getWIntType());
547392da48160bd92ceb486792780467cbfdb2d0e8cJames Molloy
548f5c209d23b20ada4a9b6235db50317239cbf6ae1Alisdair Meredith  if (LangOpts.CPlusPlus) // C++0x 3.9.1p5, extension for C++
549f5c209d23b20ada4a9b6235db50317239cbf6ae1Alisdair Meredith    InitBuiltinType(Char16Ty,           BuiltinType::Char16);
550f5c209d23b20ada4a9b6235db50317239cbf6ae1Alisdair Meredith  else // C99
551f5c209d23b20ada4a9b6235db50317239cbf6ae1Alisdair Meredith    Char16Ty = getFromTargetType(Target.getChar16Type());
552f5c209d23b20ada4a9b6235db50317239cbf6ae1Alisdair Meredith
553f5c209d23b20ada4a9b6235db50317239cbf6ae1Alisdair Meredith  if (LangOpts.CPlusPlus) // C++0x 3.9.1p5, extension for C++
554f5c209d23b20ada4a9b6235db50317239cbf6ae1Alisdair Meredith    InitBuiltinType(Char32Ty,           BuiltinType::Char32);
555f5c209d23b20ada4a9b6235db50317239cbf6ae1Alisdair Meredith  else // C99
556f5c209d23b20ada4a9b6235db50317239cbf6ae1Alisdair Meredith    Char32Ty = getFromTargetType(Target.getChar32Type());
557f5c209d23b20ada4a9b6235db50317239cbf6ae1Alisdair Meredith
558898574e7496ba8fd76290079d3a9d06954992734Douglas Gregor  // Placeholder type for type-dependent expressions whose type is
559898574e7496ba8fd76290079d3a9d06954992734Douglas Gregor  // completely unknown. No code should ever check a type against
560898574e7496ba8fd76290079d3a9d06954992734Douglas Gregor  // DependentTy and users should never see it; however, it is here to
561898574e7496ba8fd76290079d3a9d06954992734Douglas Gregor  // help diagnose failures to properly check for type-dependent
562898574e7496ba8fd76290079d3a9d06954992734Douglas Gregor  // expressions.
563898574e7496ba8fd76290079d3a9d06954992734Douglas Gregor  InitBuiltinType(DependentTy,         BuiltinType::Dependent);
5648e9bebdea69c590dedfbf27374114cb76fe12fbdDouglas Gregor
5652a984cad5ac3fdceeff2bd99daa7b90979313475John McCall  // Placeholder type for functions.
5662a984cad5ac3fdceeff2bd99daa7b90979313475John McCall  InitBuiltinType(OverloadTy,          BuiltinType::Overload);
5672a984cad5ac3fdceeff2bd99daa7b90979313475John McCall
568864c041e118155c2b1ce0ba36942a3da5a4a055eJohn McCall  // Placeholder type for bound members.
569864c041e118155c2b1ce0ba36942a3da5a4a055eJohn McCall  InitBuiltinType(BoundMemberTy,       BuiltinType::BoundMember);
570864c041e118155c2b1ce0ba36942a3da5a4a055eJohn McCall
5713c3b7f90a863af43fa63043d396553ecf205351cJohn McCall  // Placeholder type for pseudo-objects.
5723c3b7f90a863af43fa63043d396553ecf205351cJohn McCall  InitBuiltinType(PseudoObjectTy,      BuiltinType::PseudoObject);
5733c3b7f90a863af43fa63043d396553ecf205351cJohn McCall
5741de4d4e8cb2e9c88809fea8092bc6e835a5473d2John McCall  // "any" type; useful for debugger-like clients.
5751de4d4e8cb2e9c88809fea8092bc6e835a5473d2John McCall  InitBuiltinType(UnknownAnyTy,        BuiltinType::UnknownAny);
5761de4d4e8cb2e9c88809fea8092bc6e835a5473d2John McCall
5770ddaeb9b031070ec64afe92d9892875ac44df427John McCall  // Placeholder type for unbridged ARC casts.
5780ddaeb9b031070ec64afe92d9892875ac44df427John McCall  InitBuiltinType(ARCUnbridgedCastTy,  BuiltinType::ARCUnbridgedCast);
5790ddaeb9b031070ec64afe92d9892875ac44df427John McCall
5805f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // C99 6.2.5p11.
5815f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  FloatComplexTy      = getComplexType(FloatTy);
5825f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  DoubleComplexTy     = getComplexType(DoubleTy);
5835f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  LongDoubleComplexTy = getComplexType(LongDoubleTy);
5848e9bebdea69c590dedfbf27374114cb76fe12fbdDouglas Gregor
58513dcd00615de5c4279d97bdf63cd5f0a14fd9dccFariborz Jahanian  // Builtin types for 'id', 'Class', and 'SEL'.
586de2e22d33afec98324a66a358dfe0951b3c7259aSteve Naroff  InitBuiltinType(ObjCBuiltinIdTy, BuiltinType::ObjCId);
587de2e22d33afec98324a66a358dfe0951b3c7259aSteve Naroff  InitBuiltinType(ObjCBuiltinClassTy, BuiltinType::ObjCClass);
58813dcd00615de5c4279d97bdf63cd5f0a14fd9dccFariborz Jahanian  InitBuiltinType(ObjCBuiltinSelTy, BuiltinType::ObjCSel);
589ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek
590ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek  // Builtin type for __objc_yes and __objc_no
59193a49944e0e68e32bc22d45d44ee136b34beffb3Fariborz Jahanian  ObjCBuiltinBoolTy = (Target.useSignedCharForObjCBool() ?
59293a49944e0e68e32bc22d45d44ee136b34beffb3Fariborz Jahanian                       SignedCharTy : BoolTy);
593ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek
594a526c5c67e5a0473c340903ee542ce570119665fTed Kremenek  ObjCConstantStringType = QualType();
5951eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
59633e1d64ab5cd5d27f8530ccd056191fe2c9f3f2eFariborz Jahanian  // void * type
59733e1d64ab5cd5d27f8530ccd056191fe2c9f3f2eFariborz Jahanian  VoidPtrTy = getPointerType(VoidTy);
5986e8ed16ffef02b82995a90bdcf10ffff7d63839aSebastian Redl
5996e8ed16ffef02b82995a90bdcf10ffff7d63839aSebastian Redl  // nullptr type (C++0x 2.14.7)
6006e8ed16ffef02b82995a90bdcf10ffff7d63839aSebastian Redl  InitBuiltinType(NullPtrTy,           BuiltinType::NullPtr);
601aa4a99b4a62615db243f7a5c433169f2fc704420Anton Korobeynikov
602aa4a99b4a62615db243f7a5c433169f2fc704420Anton Korobeynikov  // half type (OpenCL 6.1.1.1) / ARM NEON __fp16
603aa4a99b4a62615db243f7a5c433169f2fc704420Anton Korobeynikov  InitBuiltinType(HalfTy, BuiltinType::Half);
6045f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
6055f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
606d6471f7c1921c7802804ce3ff6fe9768310f72b9David BlaikieDiagnosticsEngine &ASTContext::getDiagnostics() const {
60778a916ec5ff5b66adec3c499e1b9af7b87668309Argyrios Kyrtzidis  return SourceMgr.getDiagnostics();
60878a916ec5ff5b66adec3c499e1b9af7b87668309Argyrios Kyrtzidis}
60978a916ec5ff5b66adec3c499e1b9af7b87668309Argyrios Kyrtzidis
6106320064d0c60fa8683f5623881c9394fd4aa7689Douglas GregorAttrVec& ASTContext::getDeclAttrs(const Decl *D) {
6116320064d0c60fa8683f5623881c9394fd4aa7689Douglas Gregor  AttrVec *&Result = DeclAttrs[D];
6126320064d0c60fa8683f5623881c9394fd4aa7689Douglas Gregor  if (!Result) {
6136320064d0c60fa8683f5623881c9394fd4aa7689Douglas Gregor    void *Mem = Allocate(sizeof(AttrVec));
6146320064d0c60fa8683f5623881c9394fd4aa7689Douglas Gregor    Result = new (Mem) AttrVec;
6156320064d0c60fa8683f5623881c9394fd4aa7689Douglas Gregor  }
6166320064d0c60fa8683f5623881c9394fd4aa7689Douglas Gregor
6176320064d0c60fa8683f5623881c9394fd4aa7689Douglas Gregor  return *Result;
6186320064d0c60fa8683f5623881c9394fd4aa7689Douglas Gregor}
6196320064d0c60fa8683f5623881c9394fd4aa7689Douglas Gregor
6206320064d0c60fa8683f5623881c9394fd4aa7689Douglas Gregor/// \brief Erase the attributes corresponding to the given declaration.
6216320064d0c60fa8683f5623881c9394fd4aa7689Douglas Gregorvoid ASTContext::eraseDeclAttrs(const Decl *D) {
6226320064d0c60fa8683f5623881c9394fd4aa7689Douglas Gregor  llvm::DenseMap<const Decl*, AttrVec*>::iterator Pos = DeclAttrs.find(D);
6236320064d0c60fa8683f5623881c9394fd4aa7689Douglas Gregor  if (Pos != DeclAttrs.end()) {
6246320064d0c60fa8683f5623881c9394fd4aa7689Douglas Gregor    Pos->second->~AttrVec();
6256320064d0c60fa8683f5623881c9394fd4aa7689Douglas Gregor    DeclAttrs.erase(Pos);
6266320064d0c60fa8683f5623881c9394fd4aa7689Douglas Gregor  }
6276320064d0c60fa8683f5623881c9394fd4aa7689Douglas Gregor}
6286320064d0c60fa8683f5623881c9394fd4aa7689Douglas Gregor
629251b4ff2578e26959a4c036140ccd61c5e9292f2Douglas GregorMemberSpecializationInfo *
630663b5a0be7261c29bc4c526a71cffcfa02d4153eDouglas GregorASTContext::getInstantiatedFromStaticDataMember(const VarDecl *Var) {
6317caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  assert(Var->isStaticDataMember() && "Not a static data member");
632663b5a0be7261c29bc4c526a71cffcfa02d4153eDouglas Gregor  llvm::DenseMap<const VarDecl *, MemberSpecializationInfo *>::iterator Pos
6337caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor    = InstantiatedFromStaticDataMember.find(Var);
6347caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  if (Pos == InstantiatedFromStaticDataMember.end())
6357caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor    return 0;
6361eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
6377caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  return Pos->second;
6387caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor}
6397caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor
6401eb4433ac451dc16f4133a88af2d002ac26c58efMike Stumpvoid
641251b4ff2578e26959a4c036140ccd61c5e9292f2Douglas GregorASTContext::setInstantiatedFromStaticDataMember(VarDecl *Inst, VarDecl *Tmpl,
6429421adc43891e272156fab640e5d5ee5054b779cArgyrios Kyrtzidis                                                TemplateSpecializationKind TSK,
6439421adc43891e272156fab640e5d5ee5054b779cArgyrios Kyrtzidis                                          SourceLocation PointOfInstantiation) {
6447caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  assert(Inst->isStaticDataMember() && "Not a static data member");
6457caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  assert(Tmpl->isStaticDataMember() && "Not a static data member");
6467caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  assert(!InstantiatedFromStaticDataMember[Inst] &&
6477caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor         "Already noted what static data member was instantiated from");
648251b4ff2578e26959a4c036140ccd61c5e9292f2Douglas Gregor  InstantiatedFromStaticDataMember[Inst]
6499421adc43891e272156fab640e5d5ee5054b779cArgyrios Kyrtzidis    = new (*this) MemberSpecializationInfo(Tmpl, TSK, PointOfInstantiation);
6507caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor}
6517caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor
652af0f4d0b2e38c810effc8b024ad2fb6604eec5d3Francois PichetFunctionDecl *ASTContext::getClassScopeSpecializationPattern(
653af0f4d0b2e38c810effc8b024ad2fb6604eec5d3Francois Pichet                                                     const FunctionDecl *FD){
654af0f4d0b2e38c810effc8b024ad2fb6604eec5d3Francois Pichet  assert(FD && "Specialization is 0");
655af0f4d0b2e38c810effc8b024ad2fb6604eec5d3Francois Pichet  llvm::DenseMap<const FunctionDecl*, FunctionDecl *>::const_iterator Pos
6560d95f0d7b81110f77e99e833f766d19be7b7e072Francois Pichet    = ClassScopeSpecializationPattern.find(FD);
6570d95f0d7b81110f77e99e833f766d19be7b7e072Francois Pichet  if (Pos == ClassScopeSpecializationPattern.end())
658af0f4d0b2e38c810effc8b024ad2fb6604eec5d3Francois Pichet    return 0;
659af0f4d0b2e38c810effc8b024ad2fb6604eec5d3Francois Pichet
660af0f4d0b2e38c810effc8b024ad2fb6604eec5d3Francois Pichet  return Pos->second;
661af0f4d0b2e38c810effc8b024ad2fb6604eec5d3Francois Pichet}
662af0f4d0b2e38c810effc8b024ad2fb6604eec5d3Francois Pichet
663af0f4d0b2e38c810effc8b024ad2fb6604eec5d3Francois Pichetvoid ASTContext::setClassScopeSpecializationPattern(FunctionDecl *FD,
664af0f4d0b2e38c810effc8b024ad2fb6604eec5d3Francois Pichet                                        FunctionDecl *Pattern) {
665af0f4d0b2e38c810effc8b024ad2fb6604eec5d3Francois Pichet  assert(FD && "Specialization is 0");
666af0f4d0b2e38c810effc8b024ad2fb6604eec5d3Francois Pichet  assert(Pattern && "Class scope specialization pattern is 0");
6670d95f0d7b81110f77e99e833f766d19be7b7e072Francois Pichet  ClassScopeSpecializationPattern[FD] = Pattern;
668af0f4d0b2e38c810effc8b024ad2fb6604eec5d3Francois Pichet}
669af0f4d0b2e38c810effc8b024ad2fb6604eec5d3Francois Pichet
6707ba107a1863ddfa1664555854f0d7bdb3c491c92John McCallNamedDecl *
671ed97649e9574b9d854fa4d6109c9333ae0993554John McCallASTContext::getInstantiatedFromUsingDecl(UsingDecl *UUD) {
6727ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall  llvm::DenseMap<UsingDecl *, NamedDecl *>::const_iterator Pos
673ed97649e9574b9d854fa4d6109c9333ae0993554John McCall    = InstantiatedFromUsingDecl.find(UUD);
674ed97649e9574b9d854fa4d6109c9333ae0993554John McCall  if (Pos == InstantiatedFromUsingDecl.end())
6750d8df780aef1acda5962347a32591efc629b6748Anders Carlsson    return 0;
6761eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
6770d8df780aef1acda5962347a32591efc629b6748Anders Carlsson  return Pos->second;
6780d8df780aef1acda5962347a32591efc629b6748Anders Carlsson}
6790d8df780aef1acda5962347a32591efc629b6748Anders Carlsson
6800d8df780aef1acda5962347a32591efc629b6748Anders Carlssonvoid
681ed97649e9574b9d854fa4d6109c9333ae0993554John McCallASTContext::setInstantiatedFromUsingDecl(UsingDecl *Inst, NamedDecl *Pattern) {
682ed97649e9574b9d854fa4d6109c9333ae0993554John McCall  assert((isa<UsingDecl>(Pattern) ||
683ed97649e9574b9d854fa4d6109c9333ae0993554John McCall          isa<UnresolvedUsingValueDecl>(Pattern) ||
684ed97649e9574b9d854fa4d6109c9333ae0993554John McCall          isa<UnresolvedUsingTypenameDecl>(Pattern)) &&
685ed97649e9574b9d854fa4d6109c9333ae0993554John McCall         "pattern decl is not a using decl");
686ed97649e9574b9d854fa4d6109c9333ae0993554John McCall  assert(!InstantiatedFromUsingDecl[Inst] && "pattern already exists");
687ed97649e9574b9d854fa4d6109c9333ae0993554John McCall  InstantiatedFromUsingDecl[Inst] = Pattern;
688ed97649e9574b9d854fa4d6109c9333ae0993554John McCall}
689ed97649e9574b9d854fa4d6109c9333ae0993554John McCall
690ed97649e9574b9d854fa4d6109c9333ae0993554John McCallUsingShadowDecl *
691ed97649e9574b9d854fa4d6109c9333ae0993554John McCallASTContext::getInstantiatedFromUsingShadowDecl(UsingShadowDecl *Inst) {
692ed97649e9574b9d854fa4d6109c9333ae0993554John McCall  llvm::DenseMap<UsingShadowDecl*, UsingShadowDecl*>::const_iterator Pos
693ed97649e9574b9d854fa4d6109c9333ae0993554John McCall    = InstantiatedFromUsingShadowDecl.find(Inst);
694ed97649e9574b9d854fa4d6109c9333ae0993554John McCall  if (Pos == InstantiatedFromUsingShadowDecl.end())
695ed97649e9574b9d854fa4d6109c9333ae0993554John McCall    return 0;
696ed97649e9574b9d854fa4d6109c9333ae0993554John McCall
697ed97649e9574b9d854fa4d6109c9333ae0993554John McCall  return Pos->second;
698ed97649e9574b9d854fa4d6109c9333ae0993554John McCall}
699ed97649e9574b9d854fa4d6109c9333ae0993554John McCall
700ed97649e9574b9d854fa4d6109c9333ae0993554John McCallvoid
701ed97649e9574b9d854fa4d6109c9333ae0993554John McCallASTContext::setInstantiatedFromUsingShadowDecl(UsingShadowDecl *Inst,
702ed97649e9574b9d854fa4d6109c9333ae0993554John McCall                                               UsingShadowDecl *Pattern) {
703ed97649e9574b9d854fa4d6109c9333ae0993554John McCall  assert(!InstantiatedFromUsingShadowDecl[Inst] && "pattern already exists");
704ed97649e9574b9d854fa4d6109c9333ae0993554John McCall  InstantiatedFromUsingShadowDecl[Inst] = Pattern;
7050d8df780aef1acda5962347a32591efc629b6748Anders Carlsson}
7060d8df780aef1acda5962347a32591efc629b6748Anders Carlsson
707d8b285fee4471f393da8ee30f552ceacdc362afaAnders CarlssonFieldDecl *ASTContext::getInstantiatedFromUnnamedFieldDecl(FieldDecl *Field) {
708d8b285fee4471f393da8ee30f552ceacdc362afaAnders Carlsson  llvm::DenseMap<FieldDecl *, FieldDecl *>::iterator Pos
709d8b285fee4471f393da8ee30f552ceacdc362afaAnders Carlsson    = InstantiatedFromUnnamedFieldDecl.find(Field);
710d8b285fee4471f393da8ee30f552ceacdc362afaAnders Carlsson  if (Pos == InstantiatedFromUnnamedFieldDecl.end())
711d8b285fee4471f393da8ee30f552ceacdc362afaAnders Carlsson    return 0;
7121eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
713d8b285fee4471f393da8ee30f552ceacdc362afaAnders Carlsson  return Pos->second;
714d8b285fee4471f393da8ee30f552ceacdc362afaAnders Carlsson}
715d8b285fee4471f393da8ee30f552ceacdc362afaAnders Carlsson
716d8b285fee4471f393da8ee30f552ceacdc362afaAnders Carlssonvoid ASTContext::setInstantiatedFromUnnamedFieldDecl(FieldDecl *Inst,
717d8b285fee4471f393da8ee30f552ceacdc362afaAnders Carlsson                                                     FieldDecl *Tmpl) {
718d8b285fee4471f393da8ee30f552ceacdc362afaAnders Carlsson  assert(!Inst->getDeclName() && "Instantiated field decl is not unnamed");
719d8b285fee4471f393da8ee30f552ceacdc362afaAnders Carlsson  assert(!Tmpl->getDeclName() && "Template field decl is not unnamed");
720d8b285fee4471f393da8ee30f552ceacdc362afaAnders Carlsson  assert(!InstantiatedFromUnnamedFieldDecl[Inst] &&
721d8b285fee4471f393da8ee30f552ceacdc362afaAnders Carlsson         "Already noted what unnamed field was instantiated from");
7221eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
723d8b285fee4471f393da8ee30f552ceacdc362afaAnders Carlsson  InstantiatedFromUnnamedFieldDecl[Inst] = Tmpl;
724d8b285fee4471f393da8ee30f552ceacdc362afaAnders Carlsson}
725d8b285fee4471f393da8ee30f552ceacdc362afaAnders Carlsson
72614d56ef43ff4921c6749f7340212fbb743fdbb9bFariborz Jahanianbool ASTContext::ZeroBitfieldFollowsNonBitfield(const FieldDecl *FD,
72714d56ef43ff4921c6749f7340212fbb743fdbb9bFariborz Jahanian                                    const FieldDecl *LastFD) const {
72814d56ef43ff4921c6749f7340212fbb743fdbb9bFariborz Jahanian  return (FD->isBitField() && LastFD && !LastFD->isBitField() &&
729a6b8b2c09610b8bc4330e948ece8b940c2386406Richard Smith          FD->getBitWidthValue(*this) == 0);
73014d56ef43ff4921c6749f7340212fbb743fdbb9bFariborz Jahanian}
73114d56ef43ff4921c6749f7340212fbb743fdbb9bFariborz Jahanian
732340fa242130c2d8d74c83edca0952e771aebe0e6Fariborz Jahanianbool ASTContext::ZeroBitfieldFollowsBitfield(const FieldDecl *FD,
733340fa242130c2d8d74c83edca0952e771aebe0e6Fariborz Jahanian                                             const FieldDecl *LastFD) const {
734340fa242130c2d8d74c83edca0952e771aebe0e6Fariborz Jahanian  return (FD->isBitField() && LastFD && LastFD->isBitField() &&
735a6b8b2c09610b8bc4330e948ece8b940c2386406Richard Smith          FD->getBitWidthValue(*this) == 0 &&
736a6b8b2c09610b8bc4330e948ece8b940c2386406Richard Smith          LastFD->getBitWidthValue(*this) != 0);
737340fa242130c2d8d74c83edca0952e771aebe0e6Fariborz Jahanian}
738340fa242130c2d8d74c83edca0952e771aebe0e6Fariborz Jahanian
7399b3acaa32548d0ce78b9c39a3911397f6738a47cFariborz Jahanianbool ASTContext::BitfieldFollowsBitfield(const FieldDecl *FD,
7409b3acaa32548d0ce78b9c39a3911397f6738a47cFariborz Jahanian                                         const FieldDecl *LastFD) const {
7419b3acaa32548d0ce78b9c39a3911397f6738a47cFariborz Jahanian  return (FD->isBitField() && LastFD && LastFD->isBitField() &&
742a6b8b2c09610b8bc4330e948ece8b940c2386406Richard Smith          FD->getBitWidthValue(*this) &&
743a6b8b2c09610b8bc4330e948ece8b940c2386406Richard Smith          LastFD->getBitWidthValue(*this));
7449b3acaa32548d0ce78b9c39a3911397f6738a47cFariborz Jahanian}
7459b3acaa32548d0ce78b9c39a3911397f6738a47cFariborz Jahanian
746dd7fddb5b6883326e52b278a9b7e9cefea29aae0Chad Rosierbool ASTContext::NonBitfieldFollowsBitfield(const FieldDecl *FD,
74752bbe7a1133c3cb57e9246f1b96c12940ea3821aFariborz Jahanian                                         const FieldDecl *LastFD) const {
74852bbe7a1133c3cb57e9246f1b96c12940ea3821aFariborz Jahanian  return (!FD->isBitField() && LastFD && LastFD->isBitField() &&
749a6b8b2c09610b8bc4330e948ece8b940c2386406Richard Smith          LastFD->getBitWidthValue(*this));
75052bbe7a1133c3cb57e9246f1b96c12940ea3821aFariborz Jahanian}
75152bbe7a1133c3cb57e9246f1b96c12940ea3821aFariborz Jahanian
752dd7fddb5b6883326e52b278a9b7e9cefea29aae0Chad Rosierbool ASTContext::BitfieldFollowsNonBitfield(const FieldDecl *FD,
75352bbe7a1133c3cb57e9246f1b96c12940ea3821aFariborz Jahanian                                             const FieldDecl *LastFD) const {
75452bbe7a1133c3cb57e9246f1b96c12940ea3821aFariborz Jahanian  return (FD->isBitField() && LastFD && !LastFD->isBitField() &&
755a6b8b2c09610b8bc4330e948ece8b940c2386406Richard Smith          FD->getBitWidthValue(*this));
75652bbe7a1133c3cb57e9246f1b96c12940ea3821aFariborz Jahanian}
75752bbe7a1133c3cb57e9246f1b96c12940ea3821aFariborz Jahanian
7587d10b7eb670b821741b4c96f6cf7afbc3bb39abeDouglas GregorASTContext::overridden_cxx_method_iterator
7597d10b7eb670b821741b4c96f6cf7afbc3bb39abeDouglas GregorASTContext::overridden_methods_begin(const CXXMethodDecl *Method) const {
7607d10b7eb670b821741b4c96f6cf7afbc3bb39abeDouglas Gregor  llvm::DenseMap<const CXXMethodDecl *, CXXMethodVector>::const_iterator Pos
7617d10b7eb670b821741b4c96f6cf7afbc3bb39abeDouglas Gregor    = OverriddenMethods.find(Method);
7627d10b7eb670b821741b4c96f6cf7afbc3bb39abeDouglas Gregor  if (Pos == OverriddenMethods.end())
7637d10b7eb670b821741b4c96f6cf7afbc3bb39abeDouglas Gregor    return 0;
7647d10b7eb670b821741b4c96f6cf7afbc3bb39abeDouglas Gregor
7657d10b7eb670b821741b4c96f6cf7afbc3bb39abeDouglas Gregor  return Pos->second.begin();
7667d10b7eb670b821741b4c96f6cf7afbc3bb39abeDouglas Gregor}
7677d10b7eb670b821741b4c96f6cf7afbc3bb39abeDouglas Gregor
7687d10b7eb670b821741b4c96f6cf7afbc3bb39abeDouglas GregorASTContext::overridden_cxx_method_iterator
7697d10b7eb670b821741b4c96f6cf7afbc3bb39abeDouglas GregorASTContext::overridden_methods_end(const CXXMethodDecl *Method) const {
7707d10b7eb670b821741b4c96f6cf7afbc3bb39abeDouglas Gregor  llvm::DenseMap<const CXXMethodDecl *, CXXMethodVector>::const_iterator Pos
7717d10b7eb670b821741b4c96f6cf7afbc3bb39abeDouglas Gregor    = OverriddenMethods.find(Method);
7727d10b7eb670b821741b4c96f6cf7afbc3bb39abeDouglas Gregor  if (Pos == OverriddenMethods.end())
7737d10b7eb670b821741b4c96f6cf7afbc3bb39abeDouglas Gregor    return 0;
7747d10b7eb670b821741b4c96f6cf7afbc3bb39abeDouglas Gregor
7757d10b7eb670b821741b4c96f6cf7afbc3bb39abeDouglas Gregor  return Pos->second.end();
7767d10b7eb670b821741b4c96f6cf7afbc3bb39abeDouglas Gregor}
7777d10b7eb670b821741b4c96f6cf7afbc3bb39abeDouglas Gregor
778c91e9f439ae85d5f79a6b65672f1d7d1b55ccda0Argyrios Kyrtzidisunsigned
779c91e9f439ae85d5f79a6b65672f1d7d1b55ccda0Argyrios KyrtzidisASTContext::overridden_methods_size(const CXXMethodDecl *Method) const {
780c91e9f439ae85d5f79a6b65672f1d7d1b55ccda0Argyrios Kyrtzidis  llvm::DenseMap<const CXXMethodDecl *, CXXMethodVector>::const_iterator Pos
781c91e9f439ae85d5f79a6b65672f1d7d1b55ccda0Argyrios Kyrtzidis    = OverriddenMethods.find(Method);
782c91e9f439ae85d5f79a6b65672f1d7d1b55ccda0Argyrios Kyrtzidis  if (Pos == OverriddenMethods.end())
783c91e9f439ae85d5f79a6b65672f1d7d1b55ccda0Argyrios Kyrtzidis    return 0;
784c91e9f439ae85d5f79a6b65672f1d7d1b55ccda0Argyrios Kyrtzidis
785c91e9f439ae85d5f79a6b65672f1d7d1b55ccda0Argyrios Kyrtzidis  return Pos->second.size();
786c91e9f439ae85d5f79a6b65672f1d7d1b55ccda0Argyrios Kyrtzidis}
787c91e9f439ae85d5f79a6b65672f1d7d1b55ccda0Argyrios Kyrtzidis
7887d10b7eb670b821741b4c96f6cf7afbc3bb39abeDouglas Gregorvoid ASTContext::addOverriddenMethod(const CXXMethodDecl *Method,
7897d10b7eb670b821741b4c96f6cf7afbc3bb39abeDouglas Gregor                                     const CXXMethodDecl *Overridden) {
7907d10b7eb670b821741b4c96f6cf7afbc3bb39abeDouglas Gregor  OverriddenMethods[Method].push_back(Overridden);
7917d10b7eb670b821741b4c96f6cf7afbc3bb39abeDouglas Gregor}
7927d10b7eb670b821741b4c96f6cf7afbc3bb39abeDouglas Gregor
793e664977aca2a05a77abab5a06dc0fb69e870cfb9Douglas Gregorvoid ASTContext::addedLocalImportDecl(ImportDecl *Import) {
794e664977aca2a05a77abab5a06dc0fb69e870cfb9Douglas Gregor  assert(!Import->NextLocalImport && "Import declaration already in the chain");
795e664977aca2a05a77abab5a06dc0fb69e870cfb9Douglas Gregor  assert(!Import->isFromASTFile() && "Non-local import declaration");
796e664977aca2a05a77abab5a06dc0fb69e870cfb9Douglas Gregor  if (!FirstLocalImport) {
797e664977aca2a05a77abab5a06dc0fb69e870cfb9Douglas Gregor    FirstLocalImport = Import;
798e664977aca2a05a77abab5a06dc0fb69e870cfb9Douglas Gregor    LastLocalImport = Import;
799e664977aca2a05a77abab5a06dc0fb69e870cfb9Douglas Gregor    return;
800e664977aca2a05a77abab5a06dc0fb69e870cfb9Douglas Gregor  }
801e664977aca2a05a77abab5a06dc0fb69e870cfb9Douglas Gregor
802e664977aca2a05a77abab5a06dc0fb69e870cfb9Douglas Gregor  LastLocalImport->NextLocalImport = Import;
803e664977aca2a05a77abab5a06dc0fb69e870cfb9Douglas Gregor  LastLocalImport = Import;
804e664977aca2a05a77abab5a06dc0fb69e870cfb9Douglas Gregor}
805e664977aca2a05a77abab5a06dc0fb69e870cfb9Douglas Gregor
806464175bba1318bef7905122e9fda20cff926df78Chris Lattner//===----------------------------------------------------------------------===//
807464175bba1318bef7905122e9fda20cff926df78Chris Lattner//                         Type Sizing and Analysis
808464175bba1318bef7905122e9fda20cff926df78Chris Lattner//===----------------------------------------------------------------------===//
809a7674d8a9a69f3f6fe16e70cf2a3b2b15fb7c43dChris Lattner
810b7cfe88e88cb4f46308de89cf3f0c81bfe624128Chris Lattner/// getFloatTypeSemantics - Return the APFloat 'semantics' for the specified
811b7cfe88e88cb4f46308de89cf3f0c81bfe624128Chris Lattner/// scalar floating point type.
812b7cfe88e88cb4f46308de89cf3f0c81bfe624128Chris Lattnerconst llvm::fltSemantics &ASTContext::getFloatTypeSemantics(QualType T) const {
813183700f494ec9b6701b6efe82bcb25f4c79ba561John McCall  const BuiltinType *BT = T->getAs<BuiltinType>();
814b7cfe88e88cb4f46308de89cf3f0c81bfe624128Chris Lattner  assert(BT && "Not a floating point type!");
815b7cfe88e88cb4f46308de89cf3f0c81bfe624128Chris Lattner  switch (BT->getKind()) {
816b219cfc4d75f0a03630b7c4509ef791b7e97b2c8David Blaikie  default: llvm_unreachable("Not a floating point type!");
817aa4a99b4a62615db243f7a5c433169f2fc704420Anton Korobeynikov  case BuiltinType::Half:       return Target->getHalfFormat();
818bcfd1f55bfbb3e5944cd5e03d07b343e280838c4Douglas Gregor  case BuiltinType::Float:      return Target->getFloatFormat();
819bcfd1f55bfbb3e5944cd5e03d07b343e280838c4Douglas Gregor  case BuiltinType::Double:     return Target->getDoubleFormat();
820bcfd1f55bfbb3e5944cd5e03d07b343e280838c4Douglas Gregor  case BuiltinType::LongDouble: return Target->getLongDoubleFormat();
821b7cfe88e88cb4f46308de89cf3f0c81bfe624128Chris Lattner  }
822b7cfe88e88cb4f46308de89cf3f0c81bfe624128Chris Lattner}
823b7cfe88e88cb4f46308de89cf3f0c81bfe624128Chris Lattner
8248b752f10c394b140f9ef89e049cbad1a7676fc25Ken Dyck/// getDeclAlign - Return a conservative estimate of the alignment of the
825af707ab8fbb9451e8febb8d766f6c043628125c4Chris Lattner/// specified decl.  Note that bitfields do not have a valid alignment, so
826af707ab8fbb9451e8febb8d766f6c043628125c4Chris Lattner/// this method will assert on them.
8275d484e8cf710207010720589d89602233de61d01Sebastian Redl/// If @p RefAsPointee, references are treated like their underlying type
8285d484e8cf710207010720589d89602233de61d01Sebastian Redl/// (for alignof), else they're treated like pointers (for CodeGen).
8294ba2a17694148e16eaa8d3917f657ffcd3667be4Jay FoadCharUnits ASTContext::getDeclAlign(const Decl *D, bool RefAsPointee) const {
830bcfd1f55bfbb3e5944cd5e03d07b343e280838c4Douglas Gregor  unsigned Align = Target->getCharWidth();
831dcdafb6a701aa9d81edcb088915f58933315dc05Eli Friedman
8324081a5c5f1381c4ec77f8ab3866693917e4329c4John McCall  bool UseAlignAttrOnly = false;
8334081a5c5f1381c4ec77f8ab3866693917e4329c4John McCall  if (unsigned AlignFromAttr = D->getMaxAlignment()) {
8344081a5c5f1381c4ec77f8ab3866693917e4329c4John McCall    Align = AlignFromAttr;
8354081a5c5f1381c4ec77f8ab3866693917e4329c4John McCall
8364081a5c5f1381c4ec77f8ab3866693917e4329c4John McCall    // __attribute__((aligned)) can increase or decrease alignment
8374081a5c5f1381c4ec77f8ab3866693917e4329c4John McCall    // *except* on a struct or struct member, where it only increases
8384081a5c5f1381c4ec77f8ab3866693917e4329c4John McCall    // alignment unless 'packed' is also specified.
8394081a5c5f1381c4ec77f8ab3866693917e4329c4John McCall    //
84082d0b0aab9088e977c2a44c4a5a90479c63149fePeter Collingbourne    // It is an error for alignas to decrease alignment, so we can
8414081a5c5f1381c4ec77f8ab3866693917e4329c4John McCall    // ignore that possibility;  Sema should diagnose it.
8424081a5c5f1381c4ec77f8ab3866693917e4329c4John McCall    if (isa<FieldDecl>(D)) {
8434081a5c5f1381c4ec77f8ab3866693917e4329c4John McCall      UseAlignAttrOnly = D->hasAttr<PackedAttr>() ||
8444081a5c5f1381c4ec77f8ab3866693917e4329c4John McCall        cast<FieldDecl>(D)->getParent()->hasAttr<PackedAttr>();
8454081a5c5f1381c4ec77f8ab3866693917e4329c4John McCall    } else {
8464081a5c5f1381c4ec77f8ab3866693917e4329c4John McCall      UseAlignAttrOnly = true;
8474081a5c5f1381c4ec77f8ab3866693917e4329c4John McCall    }
8484081a5c5f1381c4ec77f8ab3866693917e4329c4John McCall  }
84978a7d7d79964119a3f35b262eb154b5cbf1001edFariborz Jahanian  else if (isa<FieldDecl>(D))
85078a7d7d79964119a3f35b262eb154b5cbf1001edFariborz Jahanian      UseAlignAttrOnly =
85178a7d7d79964119a3f35b262eb154b5cbf1001edFariborz Jahanian        D->hasAttr<PackedAttr>() ||
85278a7d7d79964119a3f35b262eb154b5cbf1001edFariborz Jahanian        cast<FieldDecl>(D)->getParent()->hasAttr<PackedAttr>();
853dcdafb6a701aa9d81edcb088915f58933315dc05Eli Friedman
854ba4f5d5754c8291690d01ca9581926673d69b24cJohn McCall  // If we're using the align attribute only, just ignore everything
855ba4f5d5754c8291690d01ca9581926673d69b24cJohn McCall  // else about the declaration and its type.
8564081a5c5f1381c4ec77f8ab3866693917e4329c4John McCall  if (UseAlignAttrOnly) {
857ba4f5d5754c8291690d01ca9581926673d69b24cJohn McCall    // do nothing
858ba4f5d5754c8291690d01ca9581926673d69b24cJohn McCall
8594081a5c5f1381c4ec77f8ab3866693917e4329c4John McCall  } else if (const ValueDecl *VD = dyn_cast<ValueDecl>(D)) {
860af707ab8fbb9451e8febb8d766f6c043628125c4Chris Lattner    QualType T = VD->getType();
8616217b80b7a1379b74cced1c076338262c3c980b3Ted Kremenek    if (const ReferenceType* RT = T->getAs<ReferenceType>()) {
8625d484e8cf710207010720589d89602233de61d01Sebastian Redl      if (RefAsPointee)
8635d484e8cf710207010720589d89602233de61d01Sebastian Redl        T = RT->getPointeeType();
8645d484e8cf710207010720589d89602233de61d01Sebastian Redl      else
8655d484e8cf710207010720589d89602233de61d01Sebastian Redl        T = getPointerType(RT->getPointeeType());
8665d484e8cf710207010720589d89602233de61d01Sebastian Redl    }
8675d484e8cf710207010720589d89602233de61d01Sebastian Redl    if (!T->isIncompleteType() && !T->isFunctionType()) {
8683b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall      // Adjust alignments of declarations with array type by the
8693b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall      // large-array alignment on the target.
870bcfd1f55bfbb3e5944cd5e03d07b343e280838c4Douglas Gregor      unsigned MinWidth = Target->getLargeArrayMinWidth();
8713b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall      const ArrayType *arrayType;
8723b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall      if (MinWidth && (arrayType = getAsArrayType(T))) {
8733b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall        if (isa<VariableArrayType>(arrayType))
874bcfd1f55bfbb3e5944cd5e03d07b343e280838c4Douglas Gregor          Align = std::max(Align, Target->getLargeArrayAlign());
8753b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall        else if (isa<ConstantArrayType>(arrayType) &&
8763b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall                 MinWidth <= getTypeSize(cast<ConstantArrayType>(arrayType)))
877bcfd1f55bfbb3e5944cd5e03d07b343e280838c4Douglas Gregor          Align = std::max(Align, Target->getLargeArrayAlign());
8783b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall
8793b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall        // Walk through any array types while we're at it.
8803b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall        T = getBaseElementType(arrayType);
8816deecb0d46bcfd048e651d2db7c4fb0d6407da96Rafael Espindola      }
8829f1210c3280104417a4ad30f0a00825ac8fa718aChad Rosier      Align = std::max(Align, getPreferredTypeAlign(T.getTypePtr()));
883dcdafb6a701aa9d81edcb088915f58933315dc05Eli Friedman    }
884ba4f5d5754c8291690d01ca9581926673d69b24cJohn McCall
885ba4f5d5754c8291690d01ca9581926673d69b24cJohn McCall    // Fields can be subject to extra alignment constraints, like if
886ba4f5d5754c8291690d01ca9581926673d69b24cJohn McCall    // the field is packed, the struct is packed, or the struct has a
887ba4f5d5754c8291690d01ca9581926673d69b24cJohn McCall    // a max-field-alignment constraint (#pragma pack).  So calculate
888ba4f5d5754c8291690d01ca9581926673d69b24cJohn McCall    // the actual alignment of the field within the struct, and then
889ba4f5d5754c8291690d01ca9581926673d69b24cJohn McCall    // (as we're expected to) constrain that by the alignment of the type.
890ba4f5d5754c8291690d01ca9581926673d69b24cJohn McCall    if (const FieldDecl *field = dyn_cast<FieldDecl>(VD)) {
891ba4f5d5754c8291690d01ca9581926673d69b24cJohn McCall      // So calculate the alignment of the field.
892ba4f5d5754c8291690d01ca9581926673d69b24cJohn McCall      const ASTRecordLayout &layout = getASTRecordLayout(field->getParent());
893ba4f5d5754c8291690d01ca9581926673d69b24cJohn McCall
894ba4f5d5754c8291690d01ca9581926673d69b24cJohn McCall      // Start with the record's overall alignment.
895dac54c124e302d6f028ea5723c425b7f66fc7c71Ken Dyck      unsigned fieldAlign = toBits(layout.getAlignment());
896ba4f5d5754c8291690d01ca9581926673d69b24cJohn McCall
897ba4f5d5754c8291690d01ca9581926673d69b24cJohn McCall      // Use the GCD of that and the offset within the record.
898ba4f5d5754c8291690d01ca9581926673d69b24cJohn McCall      uint64_t offset = layout.getFieldOffset(field->getFieldIndex());
899ba4f5d5754c8291690d01ca9581926673d69b24cJohn McCall      if (offset > 0) {
900ba4f5d5754c8291690d01ca9581926673d69b24cJohn McCall        // Alignment is always a power of 2, so the GCD will be a power of 2,
901ba4f5d5754c8291690d01ca9581926673d69b24cJohn McCall        // which means we get to do this crazy thing instead of Euclid's.
902ba4f5d5754c8291690d01ca9581926673d69b24cJohn McCall        uint64_t lowBitOfOffset = offset & (~offset + 1);
903ba4f5d5754c8291690d01ca9581926673d69b24cJohn McCall        if (lowBitOfOffset < fieldAlign)
904ba4f5d5754c8291690d01ca9581926673d69b24cJohn McCall          fieldAlign = static_cast<unsigned>(lowBitOfOffset);
905ba4f5d5754c8291690d01ca9581926673d69b24cJohn McCall      }
906ba4f5d5754c8291690d01ca9581926673d69b24cJohn McCall
907ba4f5d5754c8291690d01ca9581926673d69b24cJohn McCall      Align = std::min(Align, fieldAlign);
90805f62474dd2b0f1cb69adbe0787f2868788aa949Charles Davis    }
909af707ab8fbb9451e8febb8d766f6c043628125c4Chris Lattner  }
910dcdafb6a701aa9d81edcb088915f58933315dc05Eli Friedman
911eb6f5dc86531f794ba7746a2da4d28e37cf5da7eKen Dyck  return toCharUnitsFromBits(Align);
912af707ab8fbb9451e8febb8d766f6c043628125c4Chris Lattner}
913b7cfe88e88cb4f46308de89cf3f0c81bfe624128Chris Lattner
914ea1471e0e967548c596a71469702f8846dbaf3c0John McCallstd::pair<CharUnits, CharUnits>
915bee5a79fc95e3003d755031e3d2bb4410a71e1c1Ken DyckASTContext::getTypeInfoInChars(const Type *T) const {
916ea1471e0e967548c596a71469702f8846dbaf3c0John McCall  std::pair<uint64_t, unsigned> Info = getTypeInfo(T);
917eb6f5dc86531f794ba7746a2da4d28e37cf5da7eKen Dyck  return std::make_pair(toCharUnitsFromBits(Info.first),
918eb6f5dc86531f794ba7746a2da4d28e37cf5da7eKen Dyck                        toCharUnitsFromBits(Info.second));
919ea1471e0e967548c596a71469702f8846dbaf3c0John McCall}
920ea1471e0e967548c596a71469702f8846dbaf3c0John McCall
921ea1471e0e967548c596a71469702f8846dbaf3c0John McCallstd::pair<CharUnits, CharUnits>
922bee5a79fc95e3003d755031e3d2bb4410a71e1c1Ken DyckASTContext::getTypeInfoInChars(QualType T) const {
923ea1471e0e967548c596a71469702f8846dbaf3c0John McCall  return getTypeInfoInChars(T.getTypePtr());
924ea1471e0e967548c596a71469702f8846dbaf3c0John McCall}
925ea1471e0e967548c596a71469702f8846dbaf3c0John McCall
926bc5419a2edc4030d1a623576fe339fbd3eed17a6Daniel Dunbarstd::pair<uint64_t, unsigned> ASTContext::getTypeInfo(const Type *T) const {
927bc5419a2edc4030d1a623576fe339fbd3eed17a6Daniel Dunbar  TypeInfoMap::iterator it = MemoizedTypeInfo.find(T);
928bc5419a2edc4030d1a623576fe339fbd3eed17a6Daniel Dunbar  if (it != MemoizedTypeInfo.end())
929bc5419a2edc4030d1a623576fe339fbd3eed17a6Daniel Dunbar    return it->second;
930bc5419a2edc4030d1a623576fe339fbd3eed17a6Daniel Dunbar
931bc5419a2edc4030d1a623576fe339fbd3eed17a6Daniel Dunbar  std::pair<uint64_t, unsigned> Info = getTypeInfoImpl(T);
932bc5419a2edc4030d1a623576fe339fbd3eed17a6Daniel Dunbar  MemoizedTypeInfo.insert(std::make_pair(T, Info));
933bc5419a2edc4030d1a623576fe339fbd3eed17a6Daniel Dunbar  return Info;
934bc5419a2edc4030d1a623576fe339fbd3eed17a6Daniel Dunbar}
935bc5419a2edc4030d1a623576fe339fbd3eed17a6Daniel Dunbar
936bc5419a2edc4030d1a623576fe339fbd3eed17a6Daniel Dunbar/// getTypeInfoImpl - Return the size of the specified type, in bits.  This
937bc5419a2edc4030d1a623576fe339fbd3eed17a6Daniel Dunbar/// method does not work on incomplete types.
9380953e767ff7817f97b3ab20896b229891eeff45bJohn McCall///
9390953e767ff7817f97b3ab20896b229891eeff45bJohn McCall/// FIXME: Pointers into different addr spaces could have different sizes and
9400953e767ff7817f97b3ab20896b229891eeff45bJohn McCall/// alignment requirements: getPointerInfo should take an AddrSpace, this
9410953e767ff7817f97b3ab20896b229891eeff45bJohn McCall/// should take a QualType, &c.
942d2d2a11a91d7ddf468bfb70f66362d24806ed601Chris Lattnerstd::pair<uint64_t, unsigned>
943bc5419a2edc4030d1a623576fe339fbd3eed17a6Daniel DunbarASTContext::getTypeInfoImpl(const Type *T) const {
9445e301007e31e14c8ff647288e1b8bd8dbf8a5fe4Mike Stump  uint64_t Width=0;
9455e301007e31e14c8ff647288e1b8bd8dbf8a5fe4Mike Stump  unsigned Align=8;
946a7674d8a9a69f3f6fe16e70cf2a3b2b15fb7c43dChris Lattner  switch (T->getTypeClass()) {
94772564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor#define TYPE(Class, Base)
94872564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor#define ABSTRACT_TYPE(Class, Base)
94918857644059c45da6776f1a288eec7b4cf3a844aDouglas Gregor#define NON_CANONICAL_TYPE(Class, Base)
95072564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor#define DEPENDENT_TYPE(Class, Base) case Type::Class:
95172564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor#include "clang/AST/TypeNodes.def"
952d3d49bb27c7ffd9accc0a6c00e887111c0348845John McCall    llvm_unreachable("Should not see dependent types");
95372564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor
9545d2a6303467184b1f159bb6556efc434e50e3c28Chris Lattner  case Type::FunctionNoProto:
9555d2a6303467184b1f159bb6556efc434e50e3c28Chris Lattner  case Type::FunctionProto:
95618857644059c45da6776f1a288eec7b4cf3a844aDouglas Gregor    // GCC extension: alignof(function) = 32 bits
95718857644059c45da6776f1a288eec7b4cf3a844aDouglas Gregor    Width = 0;
95818857644059c45da6776f1a288eec7b4cf3a844aDouglas Gregor    Align = 32;
95918857644059c45da6776f1a288eec7b4cf3a844aDouglas Gregor    break;
96018857644059c45da6776f1a288eec7b4cf3a844aDouglas Gregor
96172564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor  case Type::IncompleteArray:
962fb22d96692c5240fb8d611290dbf7eeed3759c73Steve Naroff  case Type::VariableArray:
96318857644059c45da6776f1a288eec7b4cf3a844aDouglas Gregor    Width = 0;
96418857644059c45da6776f1a288eec7b4cf3a844aDouglas Gregor    Align = getTypeAlign(cast<ArrayType>(T)->getElementType());
96518857644059c45da6776f1a288eec7b4cf3a844aDouglas Gregor    break;
96618857644059c45da6776f1a288eec7b4cf3a844aDouglas Gregor
967fb22d96692c5240fb8d611290dbf7eeed3759c73Steve Naroff  case Type::ConstantArray: {
9681d75118af76cae2bfc06389cde410e14bd0a19fcDaniel Dunbar    const ConstantArrayType *CAT = cast<ConstantArrayType>(T);
9691eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
97098be4943e8dc4f3905629a7102668960873cf863Chris Lattner    std::pair<uint64_t, unsigned> EltInfo = getTypeInfo(CAT->getElementType());
971fea966a4103ed9c018d1494b95e9d09b161f5a70Abramo Bagnara    uint64_t Size = CAT->getSize().getZExtValue();
972bc5419a2edc4030d1a623576fe339fbd3eed17a6Daniel Dunbar    assert((Size == 0 || EltInfo.first <= (uint64_t)(-1)/Size) &&
973bc5419a2edc4030d1a623576fe339fbd3eed17a6Daniel Dunbar           "Overflow in array type bit size evaluation");
974fea966a4103ed9c018d1494b95e9d09b161f5a70Abramo Bagnara    Width = EltInfo.first*Size;
975030d8846c7e520330007087e949f621989876e3aChris Lattner    Align = EltInfo.second;
976cd88b4171753dcb2bc0a21d78f1597c796bb8a20Argyrios Kyrtzidis    Width = llvm::RoundUpToAlignment(Width, Align);
977030d8846c7e520330007087e949f621989876e3aChris Lattner    break;
9785c09a02a5db85e08a432b6eeced9aa656349710dChristopher Lamb  }
979213541a68a3e137d11d2cefb612c6cdb410d7e8eNate Begeman  case Type::ExtVector:
980030d8846c7e520330007087e949f621989876e3aChris Lattner  case Type::Vector: {
9819fcfe926432f3c3f7e9a61219e55c352fd358e45Chris Lattner    const VectorType *VT = cast<VectorType>(T);
9829fcfe926432f3c3f7e9a61219e55c352fd358e45Chris Lattner    std::pair<uint64_t, unsigned> EltInfo = getTypeInfo(VT->getElementType());
9839fcfe926432f3c3f7e9a61219e55c352fd358e45Chris Lattner    Width = EltInfo.first*VT->getNumElements();
9844bd998bbc228915d2b9cae5b67879de48940d05eEli Friedman    Align = Width;
9856fe7c8aa8c7546743ecd0ac0138c2cf5d8155386Nate Begeman    // If the alignment is not a power of 2, round up to the next power of 2.
9866fe7c8aa8c7546743ecd0ac0138c2cf5d8155386Nate Begeman    // This happens for non-power-of-2 length vectors.
9878eefcd353c1d06a10104f69e5079ebab3183f9a3Dan Gohman    if (Align & (Align-1)) {
9889fcfe926432f3c3f7e9a61219e55c352fd358e45Chris Lattner      Align = llvm::NextPowerOf2(Align);
9899fcfe926432f3c3f7e9a61219e55c352fd358e45Chris Lattner      Width = llvm::RoundUpToAlignment(Width, Align);
9909fcfe926432f3c3f7e9a61219e55c352fd358e45Chris Lattner    }
991030d8846c7e520330007087e949f621989876e3aChris Lattner    break;
992030d8846c7e520330007087e949f621989876e3aChris Lattner  }
9935d2a6303467184b1f159bb6556efc434e50e3c28Chris Lattner
9949e9b6dc3fd413f5341fab54b681420eeb21cd169Chris Lattner  case Type::Builtin:
995a7674d8a9a69f3f6fe16e70cf2a3b2b15fb7c43dChris Lattner    switch (cast<BuiltinType>(T)->getKind()) {
996b219cfc4d75f0a03630b7c4509ef791b7e97b2c8David Blaikie    default: llvm_unreachable("Unknown builtin type!");
997d2d2a11a91d7ddf468bfb70f66362d24806ed601Chris Lattner    case BuiltinType::Void:
99818857644059c45da6776f1a288eec7b4cf3a844aDouglas Gregor      // GCC extension: alignof(void) = 8 bits.
99918857644059c45da6776f1a288eec7b4cf3a844aDouglas Gregor      Width = 0;
100018857644059c45da6776f1a288eec7b4cf3a844aDouglas Gregor      Align = 8;
100118857644059c45da6776f1a288eec7b4cf3a844aDouglas Gregor      break;
100218857644059c45da6776f1a288eec7b4cf3a844aDouglas Gregor
10036f62c2abd8077bf70d2166d37e8caa426b34d8e4Chris Lattner    case BuiltinType::Bool:
1004bcfd1f55bfbb3e5944cd5e03d07b343e280838c4Douglas Gregor      Width = Target->getBoolWidth();
1005bcfd1f55bfbb3e5944cd5e03d07b343e280838c4Douglas Gregor      Align = Target->getBoolAlign();
10066f62c2abd8077bf70d2166d37e8caa426b34d8e4Chris Lattner      break;
1007692233e90a99c3a81dd04879d36eb9688f137c44Chris Lattner    case BuiltinType::Char_S:
1008692233e90a99c3a81dd04879d36eb9688f137c44Chris Lattner    case BuiltinType::Char_U:
1009692233e90a99c3a81dd04879d36eb9688f137c44Chris Lattner    case BuiltinType::UChar:
10106f62c2abd8077bf70d2166d37e8caa426b34d8e4Chris Lattner    case BuiltinType::SChar:
1011bcfd1f55bfbb3e5944cd5e03d07b343e280838c4Douglas Gregor      Width = Target->getCharWidth();
1012bcfd1f55bfbb3e5944cd5e03d07b343e280838c4Douglas Gregor      Align = Target->getCharAlign();
10136f62c2abd8077bf70d2166d37e8caa426b34d8e4Chris Lattner      break;
10143f59c975aa5d047f7edd1b900b5e885c38af0ef7Chris Lattner    case BuiltinType::WChar_S:
10153f59c975aa5d047f7edd1b900b5e885c38af0ef7Chris Lattner    case BuiltinType::WChar_U:
1016bcfd1f55bfbb3e5944cd5e03d07b343e280838c4Douglas Gregor      Width = Target->getWCharWidth();
1017bcfd1f55bfbb3e5944cd5e03d07b343e280838c4Douglas Gregor      Align = Target->getWCharAlign();
101864c438a4be2a871fa43c78264663ba1e9788b94dArgyrios Kyrtzidis      break;
1019f5c209d23b20ada4a9b6235db50317239cbf6ae1Alisdair Meredith    case BuiltinType::Char16:
1020bcfd1f55bfbb3e5944cd5e03d07b343e280838c4Douglas Gregor      Width = Target->getChar16Width();
1021bcfd1f55bfbb3e5944cd5e03d07b343e280838c4Douglas Gregor      Align = Target->getChar16Align();
1022f5c209d23b20ada4a9b6235db50317239cbf6ae1Alisdair Meredith      break;
1023f5c209d23b20ada4a9b6235db50317239cbf6ae1Alisdair Meredith    case BuiltinType::Char32:
1024bcfd1f55bfbb3e5944cd5e03d07b343e280838c4Douglas Gregor      Width = Target->getChar32Width();
1025bcfd1f55bfbb3e5944cd5e03d07b343e280838c4Douglas Gregor      Align = Target->getChar32Align();
1026f5c209d23b20ada4a9b6235db50317239cbf6ae1Alisdair Meredith      break;
1027692233e90a99c3a81dd04879d36eb9688f137c44Chris Lattner    case BuiltinType::UShort:
10286f62c2abd8077bf70d2166d37e8caa426b34d8e4Chris Lattner    case BuiltinType::Short:
1029bcfd1f55bfbb3e5944cd5e03d07b343e280838c4Douglas Gregor      Width = Target->getShortWidth();
1030bcfd1f55bfbb3e5944cd5e03d07b343e280838c4Douglas Gregor      Align = Target->getShortAlign();
10316f62c2abd8077bf70d2166d37e8caa426b34d8e4Chris Lattner      break;
1032692233e90a99c3a81dd04879d36eb9688f137c44Chris Lattner    case BuiltinType::UInt:
10336f62c2abd8077bf70d2166d37e8caa426b34d8e4Chris Lattner    case BuiltinType::Int:
1034bcfd1f55bfbb3e5944cd5e03d07b343e280838c4Douglas Gregor      Width = Target->getIntWidth();
1035bcfd1f55bfbb3e5944cd5e03d07b343e280838c4Douglas Gregor      Align = Target->getIntAlign();
10366f62c2abd8077bf70d2166d37e8caa426b34d8e4Chris Lattner      break;
1037692233e90a99c3a81dd04879d36eb9688f137c44Chris Lattner    case BuiltinType::ULong:
10386f62c2abd8077bf70d2166d37e8caa426b34d8e4Chris Lattner    case BuiltinType::Long:
1039bcfd1f55bfbb3e5944cd5e03d07b343e280838c4Douglas Gregor      Width = Target->getLongWidth();
1040bcfd1f55bfbb3e5944cd5e03d07b343e280838c4Douglas Gregor      Align = Target->getLongAlign();
10416f62c2abd8077bf70d2166d37e8caa426b34d8e4Chris Lattner      break;
1042692233e90a99c3a81dd04879d36eb9688f137c44Chris Lattner    case BuiltinType::ULongLong:
10436f62c2abd8077bf70d2166d37e8caa426b34d8e4Chris Lattner    case BuiltinType::LongLong:
1044bcfd1f55bfbb3e5944cd5e03d07b343e280838c4Douglas Gregor      Width = Target->getLongLongWidth();
1045bcfd1f55bfbb3e5944cd5e03d07b343e280838c4Douglas Gregor      Align = Target->getLongLongAlign();
10466f62c2abd8077bf70d2166d37e8caa426b34d8e4Chris Lattner      break;
1047ec16cb9b5a481d62a73ad47fa59034ced4d62022Chris Lattner    case BuiltinType::Int128:
1048ec16cb9b5a481d62a73ad47fa59034ced4d62022Chris Lattner    case BuiltinType::UInt128:
1049ec16cb9b5a481d62a73ad47fa59034ced4d62022Chris Lattner      Width = 128;
1050ec16cb9b5a481d62a73ad47fa59034ced4d62022Chris Lattner      Align = 128; // int128_t is 128-bit aligned on all targets.
1051ec16cb9b5a481d62a73ad47fa59034ced4d62022Chris Lattner      break;
1052aa4a99b4a62615db243f7a5c433169f2fc704420Anton Korobeynikov    case BuiltinType::Half:
1053aa4a99b4a62615db243f7a5c433169f2fc704420Anton Korobeynikov      Width = Target->getHalfWidth();
1054aa4a99b4a62615db243f7a5c433169f2fc704420Anton Korobeynikov      Align = Target->getHalfAlign();
1055aa4a99b4a62615db243f7a5c433169f2fc704420Anton Korobeynikov      break;
10566f62c2abd8077bf70d2166d37e8caa426b34d8e4Chris Lattner    case BuiltinType::Float:
1057bcfd1f55bfbb3e5944cd5e03d07b343e280838c4Douglas Gregor      Width = Target->getFloatWidth();
1058bcfd1f55bfbb3e5944cd5e03d07b343e280838c4Douglas Gregor      Align = Target->getFloatAlign();
10596f62c2abd8077bf70d2166d37e8caa426b34d8e4Chris Lattner      break;
10606f62c2abd8077bf70d2166d37e8caa426b34d8e4Chris Lattner    case BuiltinType::Double:
1061bcfd1f55bfbb3e5944cd5e03d07b343e280838c4Douglas Gregor      Width = Target->getDoubleWidth();
1062bcfd1f55bfbb3e5944cd5e03d07b343e280838c4Douglas Gregor      Align = Target->getDoubleAlign();
10636f62c2abd8077bf70d2166d37e8caa426b34d8e4Chris Lattner      break;
10646f62c2abd8077bf70d2166d37e8caa426b34d8e4Chris Lattner    case BuiltinType::LongDouble:
1065bcfd1f55bfbb3e5944cd5e03d07b343e280838c4Douglas Gregor      Width = Target->getLongDoubleWidth();
1066bcfd1f55bfbb3e5944cd5e03d07b343e280838c4Douglas Gregor      Align = Target->getLongDoubleAlign();
10676f62c2abd8077bf70d2166d37e8caa426b34d8e4Chris Lattner      break;
10686e8ed16ffef02b82995a90bdcf10ffff7d63839aSebastian Redl    case BuiltinType::NullPtr:
1069bcfd1f55bfbb3e5944cd5e03d07b343e280838c4Douglas Gregor      Width = Target->getPointerWidth(0); // C++ 3.9.1p11: sizeof(nullptr_t)
1070bcfd1f55bfbb3e5944cd5e03d07b343e280838c4Douglas Gregor      Align = Target->getPointerAlign(0); //   == sizeof(void*)
10711590d9c0fec4c710c2962e4bb71f76979b5163d3Sebastian Redl      break;
1072e04f5fc25cf49e8a5b836459d836c20dc3229a95Fariborz Jahanian    case BuiltinType::ObjCId:
1073e04f5fc25cf49e8a5b836459d836c20dc3229a95Fariborz Jahanian    case BuiltinType::ObjCClass:
1074e04f5fc25cf49e8a5b836459d836c20dc3229a95Fariborz Jahanian    case BuiltinType::ObjCSel:
1075bcfd1f55bfbb3e5944cd5e03d07b343e280838c4Douglas Gregor      Width = Target->getPointerWidth(0);
1076bcfd1f55bfbb3e5944cd5e03d07b343e280838c4Douglas Gregor      Align = Target->getPointerAlign(0);
1077e04f5fc25cf49e8a5b836459d836c20dc3229a95Fariborz Jahanian      break;
1078a7674d8a9a69f3f6fe16e70cf2a3b2b15fb7c43dChris Lattner    }
1079bfef6d7c67831a135d6ab79931f010f750a730adChris Lattner    break;
1080d1b3c2dd5bc1f3103bee6137957aa7c5f8f2f0bcSteve Naroff  case Type::ObjCObjectPointer:
1081bcfd1f55bfbb3e5944cd5e03d07b343e280838c4Douglas Gregor    Width = Target->getPointerWidth(0);
1082bcfd1f55bfbb3e5944cd5e03d07b343e280838c4Douglas Gregor    Align = Target->getPointerAlign(0);
10836f62c2abd8077bf70d2166d37e8caa426b34d8e4Chris Lattner    break;
1084485eeff9ba73376c8e01179bf1a501b1723446cbSteve Naroff  case Type::BlockPointer: {
1085207f4d8543529221932af82836016a2ef066c917Peter Collingbourne    unsigned AS = getTargetAddressSpace(
1086207f4d8543529221932af82836016a2ef066c917Peter Collingbourne        cast<BlockPointerType>(T)->getPointeeType());
1087bcfd1f55bfbb3e5944cd5e03d07b343e280838c4Douglas Gregor    Width = Target->getPointerWidth(AS);
1088bcfd1f55bfbb3e5944cd5e03d07b343e280838c4Douglas Gregor    Align = Target->getPointerAlign(AS);
1089485eeff9ba73376c8e01179bf1a501b1723446cbSteve Naroff    break;
1090485eeff9ba73376c8e01179bf1a501b1723446cbSteve Naroff  }
10915d484e8cf710207010720589d89602233de61d01Sebastian Redl  case Type::LValueReference:
10925d484e8cf710207010720589d89602233de61d01Sebastian Redl  case Type::RValueReference: {
10935d484e8cf710207010720589d89602233de61d01Sebastian Redl    // alignof and sizeof should never enter this code path here, so we go
10945d484e8cf710207010720589d89602233de61d01Sebastian Redl    // the pointer route.
1095207f4d8543529221932af82836016a2ef066c917Peter Collingbourne    unsigned AS = getTargetAddressSpace(
1096207f4d8543529221932af82836016a2ef066c917Peter Collingbourne        cast<ReferenceType>(T)->getPointeeType());
1097bcfd1f55bfbb3e5944cd5e03d07b343e280838c4Douglas Gregor    Width = Target->getPointerWidth(AS);
1098bcfd1f55bfbb3e5944cd5e03d07b343e280838c4Douglas Gregor    Align = Target->getPointerAlign(AS);
10995d484e8cf710207010720589d89602233de61d01Sebastian Redl    break;
11005d484e8cf710207010720589d89602233de61d01Sebastian Redl  }
1101f72a44330b9d9a4b2d93e9b91cfb8ab7bd4a0643Chris Lattner  case Type::Pointer: {
1102207f4d8543529221932af82836016a2ef066c917Peter Collingbourne    unsigned AS = getTargetAddressSpace(cast<PointerType>(T)->getPointeeType());
1103bcfd1f55bfbb3e5944cd5e03d07b343e280838c4Douglas Gregor    Width = Target->getPointerWidth(AS);
1104bcfd1f55bfbb3e5944cd5e03d07b343e280838c4Douglas Gregor    Align = Target->getPointerAlign(AS);
1105f72a44330b9d9a4b2d93e9b91cfb8ab7bd4a0643Chris Lattner    break;
1106f72a44330b9d9a4b2d93e9b91cfb8ab7bd4a0643Chris Lattner  }
1107f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl  case Type::MemberPointer: {
1108071cc7deffad608165b1ddd5263e8bf181861520Charles Davis    const MemberPointerType *MPT = cast<MemberPointerType>(T);
11091eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    std::pair<uint64_t, unsigned> PtrDiffInfo =
11101cca74ef3627a3a0ab14501d23e336548f6611b2Anders Carlsson      getTypeInfo(getPointerDiffType());
1111071cc7deffad608165b1ddd5263e8bf181861520Charles Davis    Width = PtrDiffInfo.first * ABI->getMemberPointerSize(MPT);
11121cca74ef3627a3a0ab14501d23e336548f6611b2Anders Carlsson    Align = PtrDiffInfo.second;
11131cca74ef3627a3a0ab14501d23e336548f6611b2Anders Carlsson    break;
1114f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl  }
11155d2a6303467184b1f159bb6556efc434e50e3c28Chris Lattner  case Type::Complex: {
11165d2a6303467184b1f159bb6556efc434e50e3c28Chris Lattner    // Complex types have the same alignment as their elements, but twice the
11175d2a6303467184b1f159bb6556efc434e50e3c28Chris Lattner    // size.
11181eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    std::pair<uint64_t, unsigned> EltInfo =
111998be4943e8dc4f3905629a7102668960873cf863Chris Lattner      getTypeInfo(cast<ComplexType>(T)->getElementType());
11209e9b6dc3fd413f5341fab54b681420eeb21cd169Chris Lattner    Width = EltInfo.first*2;
11215d2a6303467184b1f159bb6556efc434e50e3c28Chris Lattner    Align = EltInfo.second;
11225d2a6303467184b1f159bb6556efc434e50e3c28Chris Lattner    break;
11235d2a6303467184b1f159bb6556efc434e50e3c28Chris Lattner  }
1124c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall  case Type::ObjCObject:
1125c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall    return getTypeInfo(cast<ObjCObjectType>(T)->getBaseType().getTypePtr());
112644a3dded8080c5c9cfdad208ade8f8f7850d9a4fDevang Patel  case Type::ObjCInterface: {
11271d75118af76cae2bfc06389cde410e14bd0a19fcDaniel Dunbar    const ObjCInterfaceType *ObjCI = cast<ObjCInterfaceType>(T);
112844a3dded8080c5c9cfdad208ade8f8f7850d9a4fDevang Patel    const ASTRecordLayout &Layout = getASTObjCInterfaceLayout(ObjCI->getDecl());
1129dd76a9ab9ea675671200f94b18ce95766841952bKen Dyck    Width = toBits(Layout.getSize());
1130dac54c124e302d6f028ea5723c425b7f66fc7c71Ken Dyck    Align = toBits(Layout.getAlignment());
113144a3dded8080c5c9cfdad208ade8f8f7850d9a4fDevang Patel    break;
113244a3dded8080c5c9cfdad208ade8f8f7850d9a4fDevang Patel  }
113372564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor  case Type::Record:
113472564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor  case Type::Enum: {
11351d75118af76cae2bfc06389cde410e14bd0a19fcDaniel Dunbar    const TagType *TT = cast<TagType>(T);
11361d75118af76cae2bfc06389cde410e14bd0a19fcDaniel Dunbar
11371d75118af76cae2bfc06389cde410e14bd0a19fcDaniel Dunbar    if (TT->getDecl()->isInvalidDecl()) {
113822ce41d9fc8509da65858c75bf5b3c4dae2d8c04Douglas Gregor      Width = 8;
113922ce41d9fc8509da65858c75bf5b3c4dae2d8c04Douglas Gregor      Align = 8;
11408389eab190afef3462f6418b8d8fb70fb01c4005Chris Lattner      break;
11418389eab190afef3462f6418b8d8fb70fb01c4005Chris Lattner    }
11421eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
11431d75118af76cae2bfc06389cde410e14bd0a19fcDaniel Dunbar    if (const EnumType *ET = dyn_cast<EnumType>(TT))
11447176331b0f5cfaaa2b5aa487a6660e859e371119Chris Lattner      return getTypeInfo(ET->getDecl()->getIntegerType());
11457176331b0f5cfaaa2b5aa487a6660e859e371119Chris Lattner
11461d75118af76cae2bfc06389cde410e14bd0a19fcDaniel Dunbar    const RecordType *RT = cast<RecordType>(TT);
11477176331b0f5cfaaa2b5aa487a6660e859e371119Chris Lattner    const ASTRecordLayout &Layout = getASTRecordLayout(RT->getDecl());
1148dd76a9ab9ea675671200f94b18ce95766841952bKen Dyck    Width = toBits(Layout.getSize());
1149dac54c124e302d6f028ea5723c425b7f66fc7c71Ken Dyck    Align = toBits(Layout.getAlignment());
1150dc0d73e6495404418acf8548875aeaff07791a74Chris Lattner    break;
1151a7674d8a9a69f3f6fe16e70cf2a3b2b15fb7c43dChris Lattner  }
11527532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor
11539fcfe926432f3c3f7e9a61219e55c352fd358e45Chris Lattner  case Type::SubstTemplateTypeParm:
115449a832bd499d6f61c23655f1fac99f0dd229756eJohn McCall    return getTypeInfo(cast<SubstTemplateTypeParmType>(T)->
115549a832bd499d6f61c23655f1fac99f0dd229756eJohn McCall                       getReplacementType().getTypePtr());
115649a832bd499d6f61c23655f1fac99f0dd229756eJohn McCall
115734b41d939a1328f484511c6002ba2456db879a29Richard Smith  case Type::Auto: {
115834b41d939a1328f484511c6002ba2456db879a29Richard Smith    const AutoType *A = cast<AutoType>(T);
115934b41d939a1328f484511c6002ba2456db879a29Richard Smith    assert(A->isDeduced() && "Cannot request the size of a dependent type");
1160dc856aff4428380baa9afb5577ea04f8fb6beb13Matt Beaumont-Gay    return getTypeInfo(A->getDeducedType().getTypePtr());
116134b41d939a1328f484511c6002ba2456db879a29Richard Smith  }
116234b41d939a1328f484511c6002ba2456db879a29Richard Smith
1163075f8f1b6bed4d1b224c74f87508534cc6392ce6Abramo Bagnara  case Type::Paren:
1164075f8f1b6bed4d1b224c74f87508534cc6392ce6Abramo Bagnara    return getTypeInfo(cast<ParenType>(T)->getInnerType().getTypePtr());
1165075f8f1b6bed4d1b224c74f87508534cc6392ce6Abramo Bagnara
116618857644059c45da6776f1a288eec7b4cf3a844aDouglas Gregor  case Type::Typedef: {
1167162e1c1b487352434552147967c3dd296ebee2f7Richard Smith    const TypedefNameDecl *Typedef = cast<TypedefType>(T)->getDecl();
1168df1367af26cb2959775e9511108f12dcd2370a27Douglas Gregor    std::pair<uint64_t, unsigned> Info
1169df1367af26cb2959775e9511108f12dcd2370a27Douglas Gregor      = getTypeInfo(Typedef->getUnderlyingType().getTypePtr());
1170c1de52de64725945e5ae87e6f99ddedf161856e5Chris Lattner    // If the typedef has an aligned attribute on it, it overrides any computed
1171c1de52de64725945e5ae87e6f99ddedf161856e5Chris Lattner    // alignment we have.  This violates the GCC documentation (which says that
1172c1de52de64725945e5ae87e6f99ddedf161856e5Chris Lattner    // attribute(aligned) can only round up) but matches its implementation.
1173c1de52de64725945e5ae87e6f99ddedf161856e5Chris Lattner    if (unsigned AttrAlign = Typedef->getMaxAlignment())
1174c1de52de64725945e5ae87e6f99ddedf161856e5Chris Lattner      Align = AttrAlign;
1175c1de52de64725945e5ae87e6f99ddedf161856e5Chris Lattner    else
1176c1de52de64725945e5ae87e6f99ddedf161856e5Chris Lattner      Align = Info.second;
1177df1367af26cb2959775e9511108f12dcd2370a27Douglas Gregor    Width = Info.first;
11787532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor    break;
11797176331b0f5cfaaa2b5aa487a6660e859e371119Chris Lattner  }
118018857644059c45da6776f1a288eec7b4cf3a844aDouglas Gregor
118118857644059c45da6776f1a288eec7b4cf3a844aDouglas Gregor  case Type::TypeOfExpr:
118218857644059c45da6776f1a288eec7b4cf3a844aDouglas Gregor    return getTypeInfo(cast<TypeOfExprType>(T)->getUnderlyingExpr()->getType()
118318857644059c45da6776f1a288eec7b4cf3a844aDouglas Gregor                         .getTypePtr());
118418857644059c45da6776f1a288eec7b4cf3a844aDouglas Gregor
118518857644059c45da6776f1a288eec7b4cf3a844aDouglas Gregor  case Type::TypeOf:
118618857644059c45da6776f1a288eec7b4cf3a844aDouglas Gregor    return getTypeInfo(cast<TypeOfType>(T)->getUnderlyingType().getTypePtr());
118718857644059c45da6776f1a288eec7b4cf3a844aDouglas Gregor
1188395b475a4474f1c7574d927ad142ca0c7997cbcaAnders Carlsson  case Type::Decltype:
1189395b475a4474f1c7574d927ad142ca0c7997cbcaAnders Carlsson    return getTypeInfo(cast<DecltypeType>(T)->getUnderlyingExpr()->getType()
1190395b475a4474f1c7574d927ad142ca0c7997cbcaAnders Carlsson                        .getTypePtr());
1191395b475a4474f1c7574d927ad142ca0c7997cbcaAnders Carlsson
1192ca63c200346c0ca9e00194ec6e34a5a7b0ed9321Sean Hunt  case Type::UnaryTransform:
1193ca63c200346c0ca9e00194ec6e34a5a7b0ed9321Sean Hunt    return getTypeInfo(cast<UnaryTransformType>(T)->getUnderlyingType());
1194ca63c200346c0ca9e00194ec6e34a5a7b0ed9321Sean Hunt
1195465d41b92b2c862f3062c412a0538db65c6a2661Abramo Bagnara  case Type::Elaborated:
1196465d41b92b2c862f3062c412a0538db65c6a2661Abramo Bagnara    return getTypeInfo(cast<ElaboratedType>(T)->getNamedType().getTypePtr());
11971eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
11989d156a7b1b2771e191f2f5a45a7b7a694129463bJohn McCall  case Type::Attributed:
11999d156a7b1b2771e191f2f5a45a7b7a694129463bJohn McCall    return getTypeInfo(
12009d156a7b1b2771e191f2f5a45a7b7a694129463bJohn McCall                  cast<AttributedType>(T)->getEquivalentType().getTypePtr());
12019d156a7b1b2771e191f2f5a45a7b7a694129463bJohn McCall
12023e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith  case Type::TemplateSpecialization: {
12031eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    assert(getCanonicalType(T) != T &&
120418857644059c45da6776f1a288eec7b4cf3a844aDouglas Gregor           "Cannot request the size of a dependent type");
12053e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith    const TemplateSpecializationType *TST = cast<TemplateSpecializationType>(T);
12063e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith    // A type alias template specialization may refer to a typedef with the
12073e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith    // aligned attribute on it.
12083e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith    if (TST->isTypeAlias())
12093e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith      return getTypeInfo(TST->getAliasedType().getTypePtr());
12103e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith    else
12113e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith      return getTypeInfo(getCanonicalType(T));
12123e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith  }
12133e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith
1214b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman  case Type::Atomic: {
12152be460723940f8184ec36529b6f6ddf59c04e411Eli Friedman    std::pair<uint64_t, unsigned> Info
12162be460723940f8184ec36529b6f6ddf59c04e411Eli Friedman      = getTypeInfo(cast<AtomicType>(T)->getValueType());
12172be460723940f8184ec36529b6f6ddf59c04e411Eli Friedman    Width = Info.first;
12182be460723940f8184ec36529b6f6ddf59c04e411Eli Friedman    Align = Info.second;
12192be460723940f8184ec36529b6f6ddf59c04e411Eli Friedman    if (Width != 0 && Width <= Target->getMaxAtomicPromoteWidth() &&
12202be460723940f8184ec36529b6f6ddf59c04e411Eli Friedman        llvm::isPowerOf2_64(Width)) {
12212be460723940f8184ec36529b6f6ddf59c04e411Eli Friedman      // We can potentially perform lock-free atomic operations for this
12222be460723940f8184ec36529b6f6ddf59c04e411Eli Friedman      // type; promote the alignment appropriately.
12232be460723940f8184ec36529b6f6ddf59c04e411Eli Friedman      // FIXME: We could potentially promote the width here as well...
12242be460723940f8184ec36529b6f6ddf59c04e411Eli Friedman      // is that worthwhile?  (Non-struct atomic types generally have
12252be460723940f8184ec36529b6f6ddf59c04e411Eli Friedman      // power-of-two size anyway, but structs might not.  Requires a bit
12262be460723940f8184ec36529b6f6ddf59c04e411Eli Friedman      // of implementation work to make sure we zero out the extra bits.)
12272be460723940f8184ec36529b6f6ddf59c04e411Eli Friedman      Align = static_cast<unsigned>(Width);
12282be460723940f8184ec36529b6f6ddf59c04e411Eli Friedman    }
1229b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman  }
1230b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman
123118857644059c45da6776f1a288eec7b4cf3a844aDouglas Gregor  }
12321eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
12332be460723940f8184ec36529b6f6ddf59c04e411Eli Friedman  assert(llvm::isPowerOf2_32(Align) && "Alignment must be power of 2");
12349e9b6dc3fd413f5341fab54b681420eeb21cd169Chris Lattner  return std::make_pair(Width, Align);
1235a7674d8a9a69f3f6fe16e70cf2a3b2b15fb7c43dChris Lattner}
1236a7674d8a9a69f3f6fe16e70cf2a3b2b15fb7c43dChris Lattner
1237eb6f5dc86531f794ba7746a2da4d28e37cf5da7eKen Dyck/// toCharUnitsFromBits - Convert a size in bits to a size in characters.
1238eb6f5dc86531f794ba7746a2da4d28e37cf5da7eKen DyckCharUnits ASTContext::toCharUnitsFromBits(int64_t BitSize) const {
1239eb6f5dc86531f794ba7746a2da4d28e37cf5da7eKen Dyck  return CharUnits::fromQuantity(BitSize / getCharWidth());
1240eb6f5dc86531f794ba7746a2da4d28e37cf5da7eKen Dyck}
1241eb6f5dc86531f794ba7746a2da4d28e37cf5da7eKen Dyck
1242dd76a9ab9ea675671200f94b18ce95766841952bKen Dyck/// toBits - Convert a size in characters to a size in characters.
1243dd76a9ab9ea675671200f94b18ce95766841952bKen Dyckint64_t ASTContext::toBits(CharUnits CharSize) const {
1244dd76a9ab9ea675671200f94b18ce95766841952bKen Dyck  return CharSize.getQuantity() * getCharWidth();
1245dd76a9ab9ea675671200f94b18ce95766841952bKen Dyck}
1246dd76a9ab9ea675671200f94b18ce95766841952bKen Dyck
1247bdc601b196c48d4cd56a5ceb45d41ae4e87371abKen Dyck/// getTypeSizeInChars - Return the size of the specified type, in characters.
1248bdc601b196c48d4cd56a5ceb45d41ae4e87371abKen Dyck/// This method does not work on incomplete types.
12494ba2a17694148e16eaa8d3917f657ffcd3667be4Jay FoadCharUnits ASTContext::getTypeSizeInChars(QualType T) const {
1250eb6f5dc86531f794ba7746a2da4d28e37cf5da7eKen Dyck  return toCharUnitsFromBits(getTypeSize(T));
1251bdc601b196c48d4cd56a5ceb45d41ae4e87371abKen Dyck}
12524ba2a17694148e16eaa8d3917f657ffcd3667be4Jay FoadCharUnits ASTContext::getTypeSizeInChars(const Type *T) const {
1253eb6f5dc86531f794ba7746a2da4d28e37cf5da7eKen Dyck  return toCharUnitsFromBits(getTypeSize(T));
1254bdc601b196c48d4cd56a5ceb45d41ae4e87371abKen Dyck}
1255bdc601b196c48d4cd56a5ceb45d41ae4e87371abKen Dyck
125616e20cce43385001f33f8e3f90ee345609c805d1Ken Dyck/// getTypeAlignInChars - Return the ABI-specified alignment of a type, in
125786fa4311c8a330957ff5b765fbb0a7750ecd38c9Ken Dyck/// characters. This method does not work on incomplete types.
12584ba2a17694148e16eaa8d3917f657ffcd3667be4Jay FoadCharUnits ASTContext::getTypeAlignInChars(QualType T) const {
1259eb6f5dc86531f794ba7746a2da4d28e37cf5da7eKen Dyck  return toCharUnitsFromBits(getTypeAlign(T));
126086fa4311c8a330957ff5b765fbb0a7750ecd38c9Ken Dyck}
12614ba2a17694148e16eaa8d3917f657ffcd3667be4Jay FoadCharUnits ASTContext::getTypeAlignInChars(const Type *T) const {
1262eb6f5dc86531f794ba7746a2da4d28e37cf5da7eKen Dyck  return toCharUnitsFromBits(getTypeAlign(T));
126386fa4311c8a330957ff5b765fbb0a7750ecd38c9Ken Dyck}
126486fa4311c8a330957ff5b765fbb0a7750ecd38c9Ken Dyck
126534ebde404dc17d89487b07e6daaf1b47d5dfee39Chris Lattner/// getPreferredTypeAlign - Return the "preferred" alignment of the specified
126634ebde404dc17d89487b07e6daaf1b47d5dfee39Chris Lattner/// type for the current target in bits.  This can be different than the ABI
126734ebde404dc17d89487b07e6daaf1b47d5dfee39Chris Lattner/// alignment in cases where it is beneficial for performance to overalign
126834ebde404dc17d89487b07e6daaf1b47d5dfee39Chris Lattner/// a data type.
12694ba2a17694148e16eaa8d3917f657ffcd3667be4Jay Foadunsigned ASTContext::getPreferredTypeAlign(const Type *T) const {
127034ebde404dc17d89487b07e6daaf1b47d5dfee39Chris Lattner  unsigned ABIAlign = getTypeAlign(T);
12711eed60297ef4701b899c6a3b9680bf08f3403422Eli Friedman
12721eed60297ef4701b899c6a3b9680bf08f3403422Eli Friedman  // Double and long long should be naturally aligned if possible.
1273183700f494ec9b6701b6efe82bcb25f4c79ba561John McCall  if (const ComplexType* CT = T->getAs<ComplexType>())
12741eed60297ef4701b899c6a3b9680bf08f3403422Eli Friedman    T = CT->getElementType().getTypePtr();
12751eed60297ef4701b899c6a3b9680bf08f3403422Eli Friedman  if (T->isSpecificBuiltinType(BuiltinType::Double) ||
1276cde7a1dc68af2eb063a039b5a31c3b7dd92b1aa9Chad Rosier      T->isSpecificBuiltinType(BuiltinType::LongLong) ||
1277cde7a1dc68af2eb063a039b5a31c3b7dd92b1aa9Chad Rosier      T->isSpecificBuiltinType(BuiltinType::ULongLong))
12781eed60297ef4701b899c6a3b9680bf08f3403422Eli Friedman    return std::max(ABIAlign, (unsigned)getTypeSize(T));
12791eed60297ef4701b899c6a3b9680bf08f3403422Eli Friedman
128034ebde404dc17d89487b07e6daaf1b47d5dfee39Chris Lattner  return ABIAlign;
128134ebde404dc17d89487b07e6daaf1b47d5dfee39Chris Lattner}
128234ebde404dc17d89487b07e6daaf1b47d5dfee39Chris Lattner
12832c18bb7c9fca66c30b6eabbdcbc6399d24a54fa9Fariborz Jahanian/// DeepCollectObjCIvars -
12842c18bb7c9fca66c30b6eabbdcbc6399d24a54fa9Fariborz Jahanian/// This routine first collects all declared, but not synthesized, ivars in
12852c18bb7c9fca66c30b6eabbdcbc6399d24a54fa9Fariborz Jahanian/// super class and then collects all ivars, including those synthesized for
12862c18bb7c9fca66c30b6eabbdcbc6399d24a54fa9Fariborz Jahanian/// current class. This routine is used for implementation of current class
12872c18bb7c9fca66c30b6eabbdcbc6399d24a54fa9Fariborz Jahanian/// when all ivars, declared and synthesized are known.
12889820074dd47d37681085e964cd3392ac0b3e67b9Fariborz Jahanian///
12892c18bb7c9fca66c30b6eabbdcbc6399d24a54fa9Fariborz Jahanianvoid ASTContext::DeepCollectObjCIvars(const ObjCInterfaceDecl *OI,
12902c18bb7c9fca66c30b6eabbdcbc6399d24a54fa9Fariborz Jahanian                                      bool leafClass,
1291db8264e4c5ffd7af6fbad4ca4306bd382bb02691Jordy Rose                            SmallVectorImpl<const ObjCIvarDecl*> &Ivars) const {
12922c18bb7c9fca66c30b6eabbdcbc6399d24a54fa9Fariborz Jahanian  if (const ObjCInterfaceDecl *SuperClass = OI->getSuperClass())
12932c18bb7c9fca66c30b6eabbdcbc6399d24a54fa9Fariborz Jahanian    DeepCollectObjCIvars(SuperClass, false, Ivars);
12942c18bb7c9fca66c30b6eabbdcbc6399d24a54fa9Fariborz Jahanian  if (!leafClass) {
12952c18bb7c9fca66c30b6eabbdcbc6399d24a54fa9Fariborz Jahanian    for (ObjCInterfaceDecl::ivar_iterator I = OI->ivar_begin(),
12962c18bb7c9fca66c30b6eabbdcbc6399d24a54fa9Fariborz Jahanian         E = OI->ivar_end(); I != E; ++I)
1297581deb3da481053c4993c7600f97acf7768caac5David Blaikie      Ivars.push_back(*I);
12983060178ad9df29789505c1e6debcfc80a3a13587Chad Rosier  } else {
1299bf9eb88792e022e54a658657bf22e1925948e384Fariborz Jahanian    ObjCInterfaceDecl *IDecl = const_cast<ObjCInterfaceDecl *>(OI);
1300db8264e4c5ffd7af6fbad4ca4306bd382bb02691Jordy Rose    for (const ObjCIvarDecl *Iv = IDecl->all_declared_ivar_begin(); Iv;
1301bf9eb88792e022e54a658657bf22e1925948e384Fariborz Jahanian         Iv= Iv->getNextIvar())
1302bf9eb88792e022e54a658657bf22e1925948e384Fariborz Jahanian      Ivars.push_back(Iv);
1303bf9eb88792e022e54a658657bf22e1925948e384Fariborz Jahanian  }
13049820074dd47d37681085e964cd3392ac0b3e67b9Fariborz Jahanian}
13059820074dd47d37681085e964cd3392ac0b3e67b9Fariborz Jahanian
1306e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanian/// CollectInheritedProtocols - Collect all protocols in current class and
1307e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanian/// those inherited by it.
1308e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanianvoid ASTContext::CollectInheritedProtocols(const Decl *CDecl,
1309432a8893f7e30d141d7f279bd00b741a3cdac81fFariborz Jahanian                          llvm::SmallPtrSet<ObjCProtocolDecl*, 8> &Protocols) {
1310e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanian  if (const ObjCInterfaceDecl *OI = dyn_cast<ObjCInterfaceDecl>(CDecl)) {
131153b9441b5a81a24fa1f66f3f6416f1e36baa9c2fTed Kremenek    // We can use protocol_iterator here instead of
131253b9441b5a81a24fa1f66f3f6416f1e36baa9c2fTed Kremenek    // all_referenced_protocol_iterator since we are walking all categories.
131353b9441b5a81a24fa1f66f3f6416f1e36baa9c2fTed Kremenek    for (ObjCInterfaceDecl::all_protocol_iterator P = OI->all_referenced_protocol_begin(),
131453b9441b5a81a24fa1f66f3f6416f1e36baa9c2fTed Kremenek         PE = OI->all_referenced_protocol_end(); P != PE; ++P) {
1315e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanian      ObjCProtocolDecl *Proto = (*P);
13163fc73ee0c613715ebce78e30b4d050ea715a007dDouglas Gregor      Protocols.insert(Proto->getCanonicalDecl());
1317e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanian      for (ObjCProtocolDecl::protocol_iterator P = Proto->protocol_begin(),
1318b2f812165676230bce5d0215e49a4749c451ca9cFariborz Jahanian           PE = Proto->protocol_end(); P != PE; ++P) {
13193fc73ee0c613715ebce78e30b4d050ea715a007dDouglas Gregor        Protocols.insert((*P)->getCanonicalDecl());
1320e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanian        CollectInheritedProtocols(*P, Protocols);
1321e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanian      }
1322b2f812165676230bce5d0215e49a4749c451ca9cFariborz Jahanian    }
1323e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanian
1324e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanian    // Categories of this Interface.
1325e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanian    for (const ObjCCategoryDecl *CDeclChain = OI->getCategoryList();
1326e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanian         CDeclChain; CDeclChain = CDeclChain->getNextClassCategory())
1327e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanian      CollectInheritedProtocols(CDeclChain, Protocols);
1328e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanian    if (ObjCInterfaceDecl *SD = OI->getSuperClass())
1329e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanian      while (SD) {
1330e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanian        CollectInheritedProtocols(SD, Protocols);
1331e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanian        SD = SD->getSuperClass();
1332e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanian      }
1333b170ca5f4a8397c10e52050ff5df6885a3e6eca9Benjamin Kramer  } else if (const ObjCCategoryDecl *OC = dyn_cast<ObjCCategoryDecl>(CDecl)) {
133453b9441b5a81a24fa1f66f3f6416f1e36baa9c2fTed Kremenek    for (ObjCCategoryDecl::protocol_iterator P = OC->protocol_begin(),
1335e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanian         PE = OC->protocol_end(); P != PE; ++P) {
1336e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanian      ObjCProtocolDecl *Proto = (*P);
13373fc73ee0c613715ebce78e30b4d050ea715a007dDouglas Gregor      Protocols.insert(Proto->getCanonicalDecl());
1338e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanian      for (ObjCProtocolDecl::protocol_iterator P = Proto->protocol_begin(),
1339e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanian           PE = Proto->protocol_end(); P != PE; ++P)
1340e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanian        CollectInheritedProtocols(*P, Protocols);
1341e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanian    }
1342b170ca5f4a8397c10e52050ff5df6885a3e6eca9Benjamin Kramer  } else if (const ObjCProtocolDecl *OP = dyn_cast<ObjCProtocolDecl>(CDecl)) {
1343e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanian    for (ObjCProtocolDecl::protocol_iterator P = OP->protocol_begin(),
1344e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanian         PE = OP->protocol_end(); P != PE; ++P) {
1345e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanian      ObjCProtocolDecl *Proto = (*P);
13463fc73ee0c613715ebce78e30b4d050ea715a007dDouglas Gregor      Protocols.insert(Proto->getCanonicalDecl());
1347e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanian      for (ObjCProtocolDecl::protocol_iterator P = Proto->protocol_begin(),
1348e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanian           PE = Proto->protocol_end(); P != PE; ++P)
1349e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanian        CollectInheritedProtocols(*P, Protocols);
1350e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanian    }
1351e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanian  }
1352e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanian}
1353e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanian
13544ba2a17694148e16eaa8d3917f657ffcd3667be4Jay Foadunsigned ASTContext::CountNonClassIvars(const ObjCInterfaceDecl *OI) const {
13553bfacdf6f0706987c1b33c625cd68fb880881c22Fariborz Jahanian  unsigned count = 0;
13563bfacdf6f0706987c1b33c625cd68fb880881c22Fariborz Jahanian  // Count ivars declared in class extension.
135780aa1cd7973561889e51c1c152c8990a8de9c953Fariborz Jahanian  for (const ObjCCategoryDecl *CDecl = OI->getFirstClassExtension(); CDecl;
135880aa1cd7973561889e51c1c152c8990a8de9c953Fariborz Jahanian       CDecl = CDecl->getNextClassExtension())
1359b170ca5f4a8397c10e52050ff5df6885a3e6eca9Benjamin Kramer    count += CDecl->ivar_size();
1360b170ca5f4a8397c10e52050ff5df6885a3e6eca9Benjamin Kramer
13613bfacdf6f0706987c1b33c625cd68fb880881c22Fariborz Jahanian  // Count ivar defined in this class's implementation.  This
13623bfacdf6f0706987c1b33c625cd68fb880881c22Fariborz Jahanian  // includes synthesized ivars.
13633bfacdf6f0706987c1b33c625cd68fb880881c22Fariborz Jahanian  if (ObjCImplementationDecl *ImplDecl = OI->getImplementation())
1364b170ca5f4a8397c10e52050ff5df6885a3e6eca9Benjamin Kramer    count += ImplDecl->ivar_size();
1365b170ca5f4a8397c10e52050ff5df6885a3e6eca9Benjamin Kramer
13668e6ac1d80055fa37b9b84029c7e751624ba7f84cFariborz Jahanian  return count;
13678e6ac1d80055fa37b9b84029c7e751624ba7f84cFariborz Jahanian}
13688e6ac1d80055fa37b9b84029c7e751624ba7f84cFariborz Jahanian
13698deabc133c121f6c5561d0b2171a41cb2c29b2ceArgyrios Kyrtzidisbool ASTContext::isSentinelNullExpr(const Expr *E) {
13708deabc133c121f6c5561d0b2171a41cb2c29b2ceArgyrios Kyrtzidis  if (!E)
13718deabc133c121f6c5561d0b2171a41cb2c29b2ceArgyrios Kyrtzidis    return false;
13728deabc133c121f6c5561d0b2171a41cb2c29b2ceArgyrios Kyrtzidis
13738deabc133c121f6c5561d0b2171a41cb2c29b2ceArgyrios Kyrtzidis  // nullptr_t is always treated as null.
13748deabc133c121f6c5561d0b2171a41cb2c29b2ceArgyrios Kyrtzidis  if (E->getType()->isNullPtrType()) return true;
13758deabc133c121f6c5561d0b2171a41cb2c29b2ceArgyrios Kyrtzidis
13768deabc133c121f6c5561d0b2171a41cb2c29b2ceArgyrios Kyrtzidis  if (E->getType()->isAnyPointerType() &&
13778deabc133c121f6c5561d0b2171a41cb2c29b2ceArgyrios Kyrtzidis      E->IgnoreParenCasts()->isNullPointerConstant(*this,
13788deabc133c121f6c5561d0b2171a41cb2c29b2ceArgyrios Kyrtzidis                                                Expr::NPC_ValueDependentIsNull))
13798deabc133c121f6c5561d0b2171a41cb2c29b2ceArgyrios Kyrtzidis    return true;
13808deabc133c121f6c5561d0b2171a41cb2c29b2ceArgyrios Kyrtzidis
13818deabc133c121f6c5561d0b2171a41cb2c29b2ceArgyrios Kyrtzidis  // Unfortunately, __null has type 'int'.
13828deabc133c121f6c5561d0b2171a41cb2c29b2ceArgyrios Kyrtzidis  if (isa<GNUNullExpr>(E)) return true;
13838deabc133c121f6c5561d0b2171a41cb2c29b2ceArgyrios Kyrtzidis
13848deabc133c121f6c5561d0b2171a41cb2c29b2ceArgyrios Kyrtzidis  return false;
13858deabc133c121f6c5561d0b2171a41cb2c29b2ceArgyrios Kyrtzidis}
13868deabc133c121f6c5561d0b2171a41cb2c29b2ceArgyrios Kyrtzidis
13878a1d722f13df383600f36d77f842957c8adb5f1bArgyrios Kyrtzidis/// \brief Get the implementation of ObjCInterfaceDecl,or NULL if none exists.
13888a1d722f13df383600f36d77f842957c8adb5f1bArgyrios KyrtzidisObjCImplementationDecl *ASTContext::getObjCImplementation(ObjCInterfaceDecl *D) {
13898a1d722f13df383600f36d77f842957c8adb5f1bArgyrios Kyrtzidis  llvm::DenseMap<ObjCContainerDecl*, ObjCImplDecl*>::iterator
13908a1d722f13df383600f36d77f842957c8adb5f1bArgyrios Kyrtzidis    I = ObjCImpls.find(D);
13918a1d722f13df383600f36d77f842957c8adb5f1bArgyrios Kyrtzidis  if (I != ObjCImpls.end())
13928a1d722f13df383600f36d77f842957c8adb5f1bArgyrios Kyrtzidis    return cast<ObjCImplementationDecl>(I->second);
13938a1d722f13df383600f36d77f842957c8adb5f1bArgyrios Kyrtzidis  return 0;
13948a1d722f13df383600f36d77f842957c8adb5f1bArgyrios Kyrtzidis}
13958a1d722f13df383600f36d77f842957c8adb5f1bArgyrios Kyrtzidis/// \brief Get the implementation of ObjCCategoryDecl, or NULL if none exists.
13968a1d722f13df383600f36d77f842957c8adb5f1bArgyrios KyrtzidisObjCCategoryImplDecl *ASTContext::getObjCImplementation(ObjCCategoryDecl *D) {
13978a1d722f13df383600f36d77f842957c8adb5f1bArgyrios Kyrtzidis  llvm::DenseMap<ObjCContainerDecl*, ObjCImplDecl*>::iterator
13988a1d722f13df383600f36d77f842957c8adb5f1bArgyrios Kyrtzidis    I = ObjCImpls.find(D);
13998a1d722f13df383600f36d77f842957c8adb5f1bArgyrios Kyrtzidis  if (I != ObjCImpls.end())
14008a1d722f13df383600f36d77f842957c8adb5f1bArgyrios Kyrtzidis    return cast<ObjCCategoryImplDecl>(I->second);
14018a1d722f13df383600f36d77f842957c8adb5f1bArgyrios Kyrtzidis  return 0;
14028a1d722f13df383600f36d77f842957c8adb5f1bArgyrios Kyrtzidis}
14038a1d722f13df383600f36d77f842957c8adb5f1bArgyrios Kyrtzidis
14048a1d722f13df383600f36d77f842957c8adb5f1bArgyrios Kyrtzidis/// \brief Set the implementation of ObjCInterfaceDecl.
14058a1d722f13df383600f36d77f842957c8adb5f1bArgyrios Kyrtzidisvoid ASTContext::setObjCImplementation(ObjCInterfaceDecl *IFaceD,
14068a1d722f13df383600f36d77f842957c8adb5f1bArgyrios Kyrtzidis                           ObjCImplementationDecl *ImplD) {
14078a1d722f13df383600f36d77f842957c8adb5f1bArgyrios Kyrtzidis  assert(IFaceD && ImplD && "Passed null params");
14088a1d722f13df383600f36d77f842957c8adb5f1bArgyrios Kyrtzidis  ObjCImpls[IFaceD] = ImplD;
14098a1d722f13df383600f36d77f842957c8adb5f1bArgyrios Kyrtzidis}
14108a1d722f13df383600f36d77f842957c8adb5f1bArgyrios Kyrtzidis/// \brief Set the implementation of ObjCCategoryDecl.
14118a1d722f13df383600f36d77f842957c8adb5f1bArgyrios Kyrtzidisvoid ASTContext::setObjCImplementation(ObjCCategoryDecl *CatD,
14128a1d722f13df383600f36d77f842957c8adb5f1bArgyrios Kyrtzidis                           ObjCCategoryImplDecl *ImplD) {
14138a1d722f13df383600f36d77f842957c8adb5f1bArgyrios Kyrtzidis  assert(CatD && ImplD && "Passed null params");
14148a1d722f13df383600f36d77f842957c8adb5f1bArgyrios Kyrtzidis  ObjCImpls[CatD] = ImplD;
14158a1d722f13df383600f36d77f842957c8adb5f1bArgyrios Kyrtzidis}
14168a1d722f13df383600f36d77f842957c8adb5f1bArgyrios Kyrtzidis
141787ec9c257c99b4136af6c7f5be5a2d486906ba84Argyrios KyrtzidisObjCInterfaceDecl *ASTContext::getObjContainingInterface(NamedDecl *ND) const {
141887ec9c257c99b4136af6c7f5be5a2d486906ba84Argyrios Kyrtzidis  if (ObjCInterfaceDecl *ID = dyn_cast<ObjCInterfaceDecl>(ND->getDeclContext()))
141987ec9c257c99b4136af6c7f5be5a2d486906ba84Argyrios Kyrtzidis    return ID;
142087ec9c257c99b4136af6c7f5be5a2d486906ba84Argyrios Kyrtzidis  if (ObjCCategoryDecl *CD = dyn_cast<ObjCCategoryDecl>(ND->getDeclContext()))
142187ec9c257c99b4136af6c7f5be5a2d486906ba84Argyrios Kyrtzidis    return CD->getClassInterface();
142287ec9c257c99b4136af6c7f5be5a2d486906ba84Argyrios Kyrtzidis  if (ObjCImplDecl *IMD = dyn_cast<ObjCImplDecl>(ND->getDeclContext()))
142387ec9c257c99b4136af6c7f5be5a2d486906ba84Argyrios Kyrtzidis    return IMD->getClassInterface();
142487ec9c257c99b4136af6c7f5be5a2d486906ba84Argyrios Kyrtzidis
142587ec9c257c99b4136af6c7f5be5a2d486906ba84Argyrios Kyrtzidis  return 0;
142687ec9c257c99b4136af6c7f5be5a2d486906ba84Argyrios Kyrtzidis}
142787ec9c257c99b4136af6c7f5be5a2d486906ba84Argyrios Kyrtzidis
14281ceee5c42d5c410217f67d384eecc6ea4a2bba9bFariborz Jahanian/// \brief Get the copy initialization expression of VarDecl,or NULL if
14291ceee5c42d5c410217f67d384eecc6ea4a2bba9bFariborz Jahanian/// none exists.
1430830937bc1100fba7682f7c32c40512085870f50cFariborz JahanianExpr *ASTContext::getBlockVarCopyInits(const VarDecl*VD) {
1431d016ec204c54d766c4561d43548551ffde0e6b43Fariborz Jahanian  assert(VD && "Passed null params");
1432d016ec204c54d766c4561d43548551ffde0e6b43Fariborz Jahanian  assert(VD->hasAttr<BlocksAttr>() &&
1433d016ec204c54d766c4561d43548551ffde0e6b43Fariborz Jahanian         "getBlockVarCopyInits - not __block var");
1434830937bc1100fba7682f7c32c40512085870f50cFariborz Jahanian  llvm::DenseMap<const VarDecl*, Expr*>::iterator
1435d016ec204c54d766c4561d43548551ffde0e6b43Fariborz Jahanian    I = BlockVarCopyInits.find(VD);
14361ceee5c42d5c410217f67d384eecc6ea4a2bba9bFariborz Jahanian  return (I != BlockVarCopyInits.end()) ? cast<Expr>(I->second) : 0;
14371ceee5c42d5c410217f67d384eecc6ea4a2bba9bFariborz Jahanian}
14381ceee5c42d5c410217f67d384eecc6ea4a2bba9bFariborz Jahanian
14391ceee5c42d5c410217f67d384eecc6ea4a2bba9bFariborz Jahanian/// \brief Set the copy inialization expression of a block var decl.
14401ceee5c42d5c410217f67d384eecc6ea4a2bba9bFariborz Jahanianvoid ASTContext::setBlockVarCopyInits(VarDecl*VD, Expr* Init) {
14411ceee5c42d5c410217f67d384eecc6ea4a2bba9bFariborz Jahanian  assert(VD && Init && "Passed null params");
1442d016ec204c54d766c4561d43548551ffde0e6b43Fariborz Jahanian  assert(VD->hasAttr<BlocksAttr>() &&
1443d016ec204c54d766c4561d43548551ffde0e6b43Fariborz Jahanian         "setBlockVarCopyInits - not __block var");
14441ceee5c42d5c410217f67d384eecc6ea4a2bba9bFariborz Jahanian  BlockVarCopyInits[VD] = Init;
14451ceee5c42d5c410217f67d384eecc6ea4a2bba9bFariborz Jahanian}
14461ceee5c42d5c410217f67d384eecc6ea4a2bba9bFariborz Jahanian
1447a93c934af4fbf97cbe8e649d82e68ccacfe57c95John McCallTypeSourceInfo *ASTContext::CreateTypeSourceInfo(QualType T,
14484ba2a17694148e16eaa8d3917f657ffcd3667be4Jay Foad                                                 unsigned DataSize) const {
1449109de5ead1dfcb3bc985cddb8cb3ed5bcecad88dJohn McCall  if (!DataSize)
1450109de5ead1dfcb3bc985cddb8cb3ed5bcecad88dJohn McCall    DataSize = TypeLoc::getFullDataSizeForType(T);
1451109de5ead1dfcb3bc985cddb8cb3ed5bcecad88dJohn McCall  else
1452109de5ead1dfcb3bc985cddb8cb3ed5bcecad88dJohn McCall    assert(DataSize == TypeLoc::getFullDataSizeForType(T) &&
1453a93c934af4fbf97cbe8e649d82e68ccacfe57c95John McCall           "incorrect data size provided to CreateTypeSourceInfo!");
1454109de5ead1dfcb3bc985cddb8cb3ed5bcecad88dJohn McCall
1455a93c934af4fbf97cbe8e649d82e68ccacfe57c95John McCall  TypeSourceInfo *TInfo =
1456a93c934af4fbf97cbe8e649d82e68ccacfe57c95John McCall    (TypeSourceInfo*)BumpAlloc.Allocate(sizeof(TypeSourceInfo) + DataSize, 8);
1457a93c934af4fbf97cbe8e649d82e68ccacfe57c95John McCall  new (TInfo) TypeSourceInfo(T);
1458a93c934af4fbf97cbe8e649d82e68ccacfe57c95John McCall  return TInfo;
1459b17166c8077cd900cca83a895c43b30ea6660598Argyrios Kyrtzidis}
1460b17166c8077cd900cca83a895c43b30ea6660598Argyrios Kyrtzidis
1461a93c934af4fbf97cbe8e649d82e68ccacfe57c95John McCallTypeSourceInfo *ASTContext::getTrivialTypeSourceInfo(QualType T,
14626952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor                                                     SourceLocation L) const {
1463a93c934af4fbf97cbe8e649d82e68ccacfe57c95John McCall  TypeSourceInfo *DI = CreateTypeSourceInfo(T);
1464c21c7e9c2cded68f91be15be6847c9649242dc17Douglas Gregor  DI->getTypeLoc().initialize(const_cast<ASTContext &>(*this), L);
1465a4eb74d4dfe126c686dc708fec444c85ffb73b47John McCall  return DI;
1466a4eb74d4dfe126c686dc708fec444c85ffb73b47John McCall}
1467a4eb74d4dfe126c686dc708fec444c85ffb73b47John McCall
1468b2dbbb99e12806eaaf53b7ccabc32f42b5719443Daniel Dunbarconst ASTRecordLayout &
14694ba2a17694148e16eaa8d3917f657ffcd3667be4Jay FoadASTContext::getASTObjCInterfaceLayout(const ObjCInterfaceDecl *D) const {
1470b2dbbb99e12806eaaf53b7ccabc32f42b5719443Daniel Dunbar  return getObjCLayout(D, 0);
1471b2dbbb99e12806eaaf53b7ccabc32f42b5719443Daniel Dunbar}
1472b2dbbb99e12806eaaf53b7ccabc32f42b5719443Daniel Dunbar
1473b2dbbb99e12806eaaf53b7ccabc32f42b5719443Daniel Dunbarconst ASTRecordLayout &
14744ba2a17694148e16eaa8d3917f657ffcd3667be4Jay FoadASTContext::getASTObjCImplementationLayout(
14754ba2a17694148e16eaa8d3917f657ffcd3667be4Jay Foad                                        const ObjCImplementationDecl *D) const {
1476b2dbbb99e12806eaaf53b7ccabc32f42b5719443Daniel Dunbar  return getObjCLayout(D->getClassInterface(), D);
1477b2dbbb99e12806eaaf53b7ccabc32f42b5719443Daniel Dunbar}
1478b2dbbb99e12806eaaf53b7ccabc32f42b5719443Daniel Dunbar
1479a7674d8a9a69f3f6fe16e70cf2a3b2b15fb7c43dChris Lattner//===----------------------------------------------------------------------===//
1480a7674d8a9a69f3f6fe16e70cf2a3b2b15fb7c43dChris Lattner//                   Type creation/memoization methods
1481a7674d8a9a69f3f6fe16e70cf2a3b2b15fb7c43dChris Lattner//===----------------------------------------------------------------------===//
1482a7674d8a9a69f3f6fe16e70cf2a3b2b15fb7c43dChris Lattner
14834ba2a17694148e16eaa8d3917f657ffcd3667be4Jay FoadQualType
14843b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCallASTContext::getExtQualType(const Type *baseType, Qualifiers quals) const {
14853b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall  unsigned fastQuals = quals.getFastQualifiers();
14863b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall  quals.removeFastQualifiers();
14870953e767ff7817f97b3ab20896b229891eeff45bJohn McCall
14880953e767ff7817f97b3ab20896b229891eeff45bJohn McCall  // Check if we've already instantiated this type.
14890953e767ff7817f97b3ab20896b229891eeff45bJohn McCall  llvm::FoldingSetNodeID ID;
14903b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall  ExtQuals::Profile(ID, baseType, quals);
14913b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall  void *insertPos = 0;
14923b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall  if (ExtQuals *eq = ExtQualNodes.FindNodeOrInsertPos(ID, insertPos)) {
14933b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall    assert(eq->getQualifiers() == quals);
14943b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall    return QualType(eq, fastQuals);
14950953e767ff7817f97b3ab20896b229891eeff45bJohn McCall  }
14960953e767ff7817f97b3ab20896b229891eeff45bJohn McCall
14973b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall  // If the base type is not canonical, make the appropriate canonical type.
14983b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall  QualType canon;
14993b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall  if (!baseType->isCanonicalUnqualified()) {
15003b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall    SplitQualType canonSplit = baseType->getCanonicalTypeInternal().split();
1501200fa53fd420aa8369586f569dbece04930ad6a3John McCall    canonSplit.Quals.addConsistentQualifiers(quals);
1502200fa53fd420aa8369586f569dbece04930ad6a3John McCall    canon = getExtQualType(canonSplit.Ty, canonSplit.Quals);
15033b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall
15043b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall    // Re-find the insert position.
15053b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall    (void) ExtQualNodes.FindNodeOrInsertPos(ID, insertPos);
15063b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall  }
15073b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall
15083b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall  ExtQuals *eq = new (*this, TypeAlignment) ExtQuals(baseType, canon, quals);
15093b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall  ExtQualNodes.InsertNode(eq, insertPos);
15103b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall  return QualType(eq, fastQuals);
15110953e767ff7817f97b3ab20896b229891eeff45bJohn McCall}
15120953e767ff7817f97b3ab20896b229891eeff45bJohn McCall
15134ba2a17694148e16eaa8d3917f657ffcd3667be4Jay FoadQualType
15144ba2a17694148e16eaa8d3917f657ffcd3667be4Jay FoadASTContext::getAddrSpaceQualType(QualType T, unsigned AddressSpace) const {
1515f52ab250ff92bc51a9ac9a8e19bd43b63a5f844fChris Lattner  QualType CanT = getCanonicalType(T);
1516f52ab250ff92bc51a9ac9a8e19bd43b63a5f844fChris Lattner  if (CanT.getAddressSpace() == AddressSpace)
1517f46699ce225811d8d9dbab9d00189a0e54469457Chris Lattner    return T;
1518b7d2553edd2532d29b98b9e76bcf6a62bc48b417Chris Lattner
15190953e767ff7817f97b3ab20896b229891eeff45bJohn McCall  // If we are composing extended qualifiers together, merge together
15200953e767ff7817f97b3ab20896b229891eeff45bJohn McCall  // into one ExtQuals node.
15210953e767ff7817f97b3ab20896b229891eeff45bJohn McCall  QualifierCollector Quals;
15220953e767ff7817f97b3ab20896b229891eeff45bJohn McCall  const Type *TypeNode = Quals.strip(T);
15231eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
15240953e767ff7817f97b3ab20896b229891eeff45bJohn McCall  // If this type already has an address space specified, it cannot get
15250953e767ff7817f97b3ab20896b229891eeff45bJohn McCall  // another one.
15260953e767ff7817f97b3ab20896b229891eeff45bJohn McCall  assert(!Quals.hasAddressSpace() &&
15270953e767ff7817f97b3ab20896b229891eeff45bJohn McCall         "Type cannot be in multiple addr spaces!");
15280953e767ff7817f97b3ab20896b229891eeff45bJohn McCall  Quals.addAddressSpace(AddressSpace);
15291eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
15300953e767ff7817f97b3ab20896b229891eeff45bJohn McCall  return getExtQualType(TypeNode, Quals);
1531ebb97e98c03f8d7034bd3748a10e35f39a95c289Christopher Lamb}
1532ebb97e98c03f8d7034bd3748a10e35f39a95c289Christopher Lamb
1533b7d2553edd2532d29b98b9e76bcf6a62bc48b417Chris LattnerQualType ASTContext::getObjCGCQualType(QualType T,
15344ba2a17694148e16eaa8d3917f657ffcd3667be4Jay Foad                                       Qualifiers::GC GCAttr) const {
1535d33d9c0cc0cfdcd0b10f35a6acdfb25da4a64f19Fariborz Jahanian  QualType CanT = getCanonicalType(T);
1536b7d2553edd2532d29b98b9e76bcf6a62bc48b417Chris Lattner  if (CanT.getObjCGCAttr() == GCAttr)
1537d33d9c0cc0cfdcd0b10f35a6acdfb25da4a64f19Fariborz Jahanian    return T;
15381eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
15397f040a9d817cd1c72b565e92abff473510bf9e1dJohn McCall  if (const PointerType *ptr = T->getAs<PointerType>()) {
15407f040a9d817cd1c72b565e92abff473510bf9e1dJohn McCall    QualType Pointee = ptr->getPointeeType();
154158f9f2c884af6b72d036b746a016d8031d31cb7aSteve Naroff    if (Pointee->isAnyPointerType()) {
15424027cd1b924e29784a49085b1717f35cdd719146Fariborz Jahanian      QualType ResultType = getObjCGCQualType(Pointee, GCAttr);
15434027cd1b924e29784a49085b1717f35cdd719146Fariborz Jahanian      return getPointerType(ResultType);
15444027cd1b924e29784a49085b1717f35cdd719146Fariborz Jahanian    }
15454027cd1b924e29784a49085b1717f35cdd719146Fariborz Jahanian  }
15461eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
15470953e767ff7817f97b3ab20896b229891eeff45bJohn McCall  // If we are composing extended qualifiers together, merge together
15480953e767ff7817f97b3ab20896b229891eeff45bJohn McCall  // into one ExtQuals node.
15490953e767ff7817f97b3ab20896b229891eeff45bJohn McCall  QualifierCollector Quals;
15500953e767ff7817f97b3ab20896b229891eeff45bJohn McCall  const Type *TypeNode = Quals.strip(T);
15511eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
15520953e767ff7817f97b3ab20896b229891eeff45bJohn McCall  // If this type already has an ObjCGC specified, it cannot get
15530953e767ff7817f97b3ab20896b229891eeff45bJohn McCall  // another one.
15540953e767ff7817f97b3ab20896b229891eeff45bJohn McCall  assert(!Quals.hasObjCGCAttr() &&
15550953e767ff7817f97b3ab20896b229891eeff45bJohn McCall         "Type cannot have multiple ObjCGCs!");
15560953e767ff7817f97b3ab20896b229891eeff45bJohn McCall  Quals.addObjCGCAttr(GCAttr);
15571eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
15580953e767ff7817f97b3ab20896b229891eeff45bJohn McCall  return getExtQualType(TypeNode, Quals);
1559d33d9c0cc0cfdcd0b10f35a6acdfb25da4a64f19Fariborz Jahanian}
1560a7674d8a9a69f3f6fe16e70cf2a3b2b15fb7c43dChris Lattner
1561e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCallconst FunctionType *ASTContext::adjustFunctionType(const FunctionType *T,
1562e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall                                                   FunctionType::ExtInfo Info) {
1563e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall  if (T->getExtInfo() == Info)
1564e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall    return T;
1565e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall
1566e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall  QualType Result;
1567e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall  if (const FunctionNoProtoType *FNPT = dyn_cast<FunctionNoProtoType>(T)) {
1568e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall    Result = getFunctionNoProtoType(FNPT->getResultType(), Info);
1569e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall  } else {
1570e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall    const FunctionProtoType *FPT = cast<FunctionProtoType>(T);
1571e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall    FunctionProtoType::ExtProtoInfo EPI = FPT->getExtProtoInfo();
1572e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall    EPI.ExtInfo = Info;
1573e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall    Result = getFunctionType(FPT->getResultType(), FPT->arg_type_begin(),
1574e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall                             FPT->getNumArgs(), EPI);
1575e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall  }
1576e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall
1577e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall  return cast<FunctionType>(Result.getTypePtr());
1578e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall}
1579e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall
15805f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// getComplexType - Return the uniqued reference to the type for a complex
15815f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// number with the specified element type.
15824ba2a17694148e16eaa8d3917f657ffcd3667be4Jay FoadQualType ASTContext::getComplexType(QualType T) const {
15835f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // Unique pointers, to guarantee there is only one pointer of a particular
15845f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // structure.
15855f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  llvm::FoldingSetNodeID ID;
15865f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  ComplexType::Profile(ID, T);
15871eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
15885f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  void *InsertPos = 0;
15895f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  if (ComplexType *CT = ComplexTypes.FindNodeOrInsertPos(ID, InsertPos))
15905f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    return QualType(CT, 0);
15911eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
15925f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // If the pointee type isn't canonical, this won't be a canonical type either,
15935f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // so fill in the canonical type field.
15945f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  QualType Canonical;
1595467b27b9a24bdc823218ad1ad0e37673b6cc1e83John McCall  if (!T.isCanonical()) {
1596f52ab250ff92bc51a9ac9a8e19bd43b63a5f844fChris Lattner    Canonical = getComplexType(getCanonicalType(T));
15971eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
15985f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    // Get the new insert position for the node we care about.
15995f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    ComplexType *NewIP = ComplexTypes.FindNodeOrInsertPos(ID, InsertPos);
1600c6ed729f669044f5072a49d79041f455d971ece3Jeffrey Yasskin    assert(NewIP == 0 && "Shouldn't be in the map!"); (void)NewIP;
16015f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
16026b304a0254a13f42390b865ff5ba668a49cc58aeJohn McCall  ComplexType *New = new (*this, TypeAlignment) ComplexType(T, Canonical);
16035f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  Types.push_back(New);
16045f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  ComplexTypes.InsertNode(New, InsertPos);
16055f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  return QualType(New, 0);
16065f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
16075f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
16085f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// getPointerType - Return the uniqued reference to the type for a pointer to
16095f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// the specified type.
16104ba2a17694148e16eaa8d3917f657ffcd3667be4Jay FoadQualType ASTContext::getPointerType(QualType T) const {
16115f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // Unique pointers, to guarantee there is only one pointer of a particular
16125f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // structure.
16135f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  llvm::FoldingSetNodeID ID;
16145f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  PointerType::Profile(ID, T);
16151eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
16165f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  void *InsertPos = 0;
16175f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  if (PointerType *PT = PointerTypes.FindNodeOrInsertPos(ID, InsertPos))
16185f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    return QualType(PT, 0);
16191eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
16205f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // If the pointee type isn't canonical, this won't be a canonical type either,
16215f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // so fill in the canonical type field.
16225f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  QualType Canonical;
1623467b27b9a24bdc823218ad1ad0e37673b6cc1e83John McCall  if (!T.isCanonical()) {
1624f52ab250ff92bc51a9ac9a8e19bd43b63a5f844fChris Lattner    Canonical = getPointerType(getCanonicalType(T));
16251eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
16265f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    // Get the new insert position for the node we care about.
16275f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    PointerType *NewIP = PointerTypes.FindNodeOrInsertPos(ID, InsertPos);
1628c6ed729f669044f5072a49d79041f455d971ece3Jeffrey Yasskin    assert(NewIP == 0 && "Shouldn't be in the map!"); (void)NewIP;
16295f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
16306b304a0254a13f42390b865ff5ba668a49cc58aeJohn McCall  PointerType *New = new (*this, TypeAlignment) PointerType(T, Canonical);
16315f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  Types.push_back(New);
16325f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  PointerTypes.InsertNode(New, InsertPos);
16335f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  return QualType(New, 0);
16345f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
16355f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
16361eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// getBlockPointerType - Return the uniqued reference to the type for
16375618bd4a52c45fbbb605e3ba885663b2164db8a3Steve Naroff/// a pointer to the specified block.
16384ba2a17694148e16eaa8d3917f657ffcd3667be4Jay FoadQualType ASTContext::getBlockPointerType(QualType T) const {
1639296e8d5fdcf9946f51e866adc8d281379e51efe9Steve Naroff  assert(T->isFunctionType() && "block of function types only");
1640296e8d5fdcf9946f51e866adc8d281379e51efe9Steve Naroff  // Unique pointers, to guarantee there is only one block of a particular
16415618bd4a52c45fbbb605e3ba885663b2164db8a3Steve Naroff  // structure.
16425618bd4a52c45fbbb605e3ba885663b2164db8a3Steve Naroff  llvm::FoldingSetNodeID ID;
16435618bd4a52c45fbbb605e3ba885663b2164db8a3Steve Naroff  BlockPointerType::Profile(ID, T);
16441eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
16455618bd4a52c45fbbb605e3ba885663b2164db8a3Steve Naroff  void *InsertPos = 0;
16465618bd4a52c45fbbb605e3ba885663b2164db8a3Steve Naroff  if (BlockPointerType *PT =
16475618bd4a52c45fbbb605e3ba885663b2164db8a3Steve Naroff        BlockPointerTypes.FindNodeOrInsertPos(ID, InsertPos))
16485618bd4a52c45fbbb605e3ba885663b2164db8a3Steve Naroff    return QualType(PT, 0);
16491eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
16501eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  // If the block pointee type isn't canonical, this won't be a canonical
16515618bd4a52c45fbbb605e3ba885663b2164db8a3Steve Naroff  // type either so fill in the canonical type field.
16525618bd4a52c45fbbb605e3ba885663b2164db8a3Steve Naroff  QualType Canonical;
1653467b27b9a24bdc823218ad1ad0e37673b6cc1e83John McCall  if (!T.isCanonical()) {
16545618bd4a52c45fbbb605e3ba885663b2164db8a3Steve Naroff    Canonical = getBlockPointerType(getCanonicalType(T));
16551eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
16565618bd4a52c45fbbb605e3ba885663b2164db8a3Steve Naroff    // Get the new insert position for the node we care about.
16575618bd4a52c45fbbb605e3ba885663b2164db8a3Steve Naroff    BlockPointerType *NewIP =
16585618bd4a52c45fbbb605e3ba885663b2164db8a3Steve Naroff      BlockPointerTypes.FindNodeOrInsertPos(ID, InsertPos);
1659c6ed729f669044f5072a49d79041f455d971ece3Jeffrey Yasskin    assert(NewIP == 0 && "Shouldn't be in the map!"); (void)NewIP;
16605618bd4a52c45fbbb605e3ba885663b2164db8a3Steve Naroff  }
16616b304a0254a13f42390b865ff5ba668a49cc58aeJohn McCall  BlockPointerType *New
16626b304a0254a13f42390b865ff5ba668a49cc58aeJohn McCall    = new (*this, TypeAlignment) BlockPointerType(T, Canonical);
16635618bd4a52c45fbbb605e3ba885663b2164db8a3Steve Naroff  Types.push_back(New);
16645618bd4a52c45fbbb605e3ba885663b2164db8a3Steve Naroff  BlockPointerTypes.InsertNode(New, InsertPos);
16655618bd4a52c45fbbb605e3ba885663b2164db8a3Steve Naroff  return QualType(New, 0);
16665618bd4a52c45fbbb605e3ba885663b2164db8a3Steve Naroff}
16675618bd4a52c45fbbb605e3ba885663b2164db8a3Steve Naroff
16687c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl/// getLValueReferenceType - Return the uniqued reference to the type for an
16697c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl/// lvalue reference to the specified type.
16704ba2a17694148e16eaa8d3917f657ffcd3667be4Jay FoadQualType
16714ba2a17694148e16eaa8d3917f657ffcd3667be4Jay FoadASTContext::getLValueReferenceType(QualType T, bool SpelledAsLValue) const {
16729625e44c0252485277a340746ed8ac950686156fDouglas Gregor  assert(getCanonicalType(T) != OverloadTy &&
16739625e44c0252485277a340746ed8ac950686156fDouglas Gregor         "Unresolved overloaded function type");
16749625e44c0252485277a340746ed8ac950686156fDouglas Gregor
16755f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // Unique pointers, to guarantee there is only one pointer of a particular
16765f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // structure.
16775f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  llvm::FoldingSetNodeID ID;
167854e14c4db764c0636160d26c5bbf491637c83a76John McCall  ReferenceType::Profile(ID, T, SpelledAsLValue);
16795f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
16805f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  void *InsertPos = 0;
16817c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl  if (LValueReferenceType *RT =
16827c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl        LValueReferenceTypes.FindNodeOrInsertPos(ID, InsertPos))
16835f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    return QualType(RT, 0);
16847c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl
168554e14c4db764c0636160d26c5bbf491637c83a76John McCall  const ReferenceType *InnerRef = T->getAs<ReferenceType>();
168654e14c4db764c0636160d26c5bbf491637c83a76John McCall
16875f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // If the referencee type isn't canonical, this won't be a canonical type
16885f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // either, so fill in the canonical type field.
16895f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  QualType Canonical;
169054e14c4db764c0636160d26c5bbf491637c83a76John McCall  if (!SpelledAsLValue || InnerRef || !T.isCanonical()) {
169154e14c4db764c0636160d26c5bbf491637c83a76John McCall    QualType PointeeType = (InnerRef ? InnerRef->getPointeeType() : T);
169254e14c4db764c0636160d26c5bbf491637c83a76John McCall    Canonical = getLValueReferenceType(getCanonicalType(PointeeType));
16937c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl
16947c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl    // Get the new insert position for the node we care about.
16957c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl    LValueReferenceType *NewIP =
16967c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl      LValueReferenceTypes.FindNodeOrInsertPos(ID, InsertPos);
1697c6ed729f669044f5072a49d79041f455d971ece3Jeffrey Yasskin    assert(NewIP == 0 && "Shouldn't be in the map!"); (void)NewIP;
16987c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl  }
16997c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl
17006b304a0254a13f42390b865ff5ba668a49cc58aeJohn McCall  LValueReferenceType *New
170154e14c4db764c0636160d26c5bbf491637c83a76John McCall    = new (*this, TypeAlignment) LValueReferenceType(T, Canonical,
170254e14c4db764c0636160d26c5bbf491637c83a76John McCall                                                     SpelledAsLValue);
17037c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl  Types.push_back(New);
17047c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl  LValueReferenceTypes.InsertNode(New, InsertPos);
170554e14c4db764c0636160d26c5bbf491637c83a76John McCall
17067c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl  return QualType(New, 0);
17077c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl}
17087c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl
17097c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl/// getRValueReferenceType - Return the uniqued reference to the type for an
17107c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl/// rvalue reference to the specified type.
17114ba2a17694148e16eaa8d3917f657ffcd3667be4Jay FoadQualType ASTContext::getRValueReferenceType(QualType T) const {
17127c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl  // Unique pointers, to guarantee there is only one pointer of a particular
17137c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl  // structure.
17147c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl  llvm::FoldingSetNodeID ID;
171554e14c4db764c0636160d26c5bbf491637c83a76John McCall  ReferenceType::Profile(ID, T, false);
17167c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl
17177c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl  void *InsertPos = 0;
17187c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl  if (RValueReferenceType *RT =
17197c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl        RValueReferenceTypes.FindNodeOrInsertPos(ID, InsertPos))
17207c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl    return QualType(RT, 0);
17217c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl
172254e14c4db764c0636160d26c5bbf491637c83a76John McCall  const ReferenceType *InnerRef = T->getAs<ReferenceType>();
172354e14c4db764c0636160d26c5bbf491637c83a76John McCall
17247c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl  // If the referencee type isn't canonical, this won't be a canonical type
17257c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl  // either, so fill in the canonical type field.
17267c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl  QualType Canonical;
172754e14c4db764c0636160d26c5bbf491637c83a76John McCall  if (InnerRef || !T.isCanonical()) {
172854e14c4db764c0636160d26c5bbf491637c83a76John McCall    QualType PointeeType = (InnerRef ? InnerRef->getPointeeType() : T);
172954e14c4db764c0636160d26c5bbf491637c83a76John McCall    Canonical = getRValueReferenceType(getCanonicalType(PointeeType));
17307c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl
17315f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    // Get the new insert position for the node we care about.
17327c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl    RValueReferenceType *NewIP =
17337c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl      RValueReferenceTypes.FindNodeOrInsertPos(ID, InsertPos);
1734c6ed729f669044f5072a49d79041f455d971ece3Jeffrey Yasskin    assert(NewIP == 0 && "Shouldn't be in the map!"); (void)NewIP;
17355f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
17365f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
17376b304a0254a13f42390b865ff5ba668a49cc58aeJohn McCall  RValueReferenceType *New
17386b304a0254a13f42390b865ff5ba668a49cc58aeJohn McCall    = new (*this, TypeAlignment) RValueReferenceType(T, Canonical);
17395f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  Types.push_back(New);
17407c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl  RValueReferenceTypes.InsertNode(New, InsertPos);
17415f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  return QualType(New, 0);
17425f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
17435f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
1744f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl/// getMemberPointerType - Return the uniqued reference to the type for a
1745f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl/// member pointer to the specified type, in the specified class.
17464ba2a17694148e16eaa8d3917f657ffcd3667be4Jay FoadQualType ASTContext::getMemberPointerType(QualType T, const Type *Cls) const {
1747f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl  // Unique pointers, to guarantee there is only one pointer of a particular
1748f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl  // structure.
1749f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl  llvm::FoldingSetNodeID ID;
1750f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl  MemberPointerType::Profile(ID, T, Cls);
1751f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl
1752f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl  void *InsertPos = 0;
1753f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl  if (MemberPointerType *PT =
1754f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl      MemberPointerTypes.FindNodeOrInsertPos(ID, InsertPos))
1755f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl    return QualType(PT, 0);
1756f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl
1757f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl  // If the pointee or class type isn't canonical, this won't be a canonical
1758f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl  // type either, so fill in the canonical type field.
1759f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl  QualType Canonical;
176087c12c4a4667279dacb3d4a93c64b49148a0ff79Douglas Gregor  if (!T.isCanonical() || !Cls->isCanonicalUnqualified()) {
1761f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl    Canonical = getMemberPointerType(getCanonicalType(T),getCanonicalType(Cls));
1762f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl
1763f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl    // Get the new insert position for the node we care about.
1764f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl    MemberPointerType *NewIP =
1765f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl      MemberPointerTypes.FindNodeOrInsertPos(ID, InsertPos);
1766c6ed729f669044f5072a49d79041f455d971ece3Jeffrey Yasskin    assert(NewIP == 0 && "Shouldn't be in the map!"); (void)NewIP;
1767f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl  }
17686b304a0254a13f42390b865ff5ba668a49cc58aeJohn McCall  MemberPointerType *New
17696b304a0254a13f42390b865ff5ba668a49cc58aeJohn McCall    = new (*this, TypeAlignment) MemberPointerType(T, Cls, Canonical);
1770f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl  Types.push_back(New);
1771f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl  MemberPointerTypes.InsertNode(New, InsertPos);
1772f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl  return QualType(New, 0);
1773f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl}
1774f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl
17751eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// getConstantArrayType - Return the unique reference to the type for an
1776fb22d96692c5240fb8d611290dbf7eeed3759c73Steve Naroff/// array of the specified element type.
17771eb4433ac451dc16f4133a88af2d002ac26c58efMike StumpQualType ASTContext::getConstantArrayType(QualType EltTy,
177838aeec7299c48cb79523f7f89776fb258c84aeeaChris Lattner                                          const llvm::APInt &ArySizeIn,
1779c9406125e2cac9208098655ac8058c095c2c3a65Steve Naroff                                          ArrayType::ArraySizeModifier ASM,
178063e7d25d2e6036616b42f744fd4a39cd5f911960Abramo Bagnara                                          unsigned IndexTypeQuals) const {
1781923d56d436f750bc1f29db50e641078725558a1bSebastian Redl  assert((EltTy->isDependentType() ||
1782923d56d436f750bc1f29db50e641078725558a1bSebastian Redl          EltTy->isIncompleteType() || EltTy->isConstantSizeType()) &&
1783587cbdfd95f4b0aaccc14b31f5debe85d5daf7edEli Friedman         "Constant array of VLAs is illegal!");
1784587cbdfd95f4b0aaccc14b31f5debe85d5daf7edEli Friedman
178538aeec7299c48cb79523f7f89776fb258c84aeeaChris Lattner  // Convert the array size into a canonical width matching the pointer size for
178638aeec7299c48cb79523f7f89776fb258c84aeeaChris Lattner  // the target.
178738aeec7299c48cb79523f7f89776fb258c84aeeaChris Lattner  llvm::APInt ArySize(ArySizeIn);
17889f71a8f4c7a182a5236da9e747d57cc1d1bd24c2Jay Foad  ArySize =
1789bcfd1f55bfbb3e5944cd5e03d07b343e280838c4Douglas Gregor    ArySize.zextOrTrunc(Target->getPointerWidth(getTargetAddressSpace(EltTy)));
17901eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
17915f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  llvm::FoldingSetNodeID ID;
179263e7d25d2e6036616b42f744fd4a39cd5f911960Abramo Bagnara  ConstantArrayType::Profile(ID, EltTy, ArySize, ASM, IndexTypeQuals);
17931eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
17945f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  void *InsertPos = 0;
17951eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  if (ConstantArrayType *ATP =
17967192f8e9592729882a09d84d77838db26e39ebd4Ted Kremenek      ConstantArrayTypes.FindNodeOrInsertPos(ID, InsertPos))
17975f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    return QualType(ATP, 0);
17981eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
17993b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall  // If the element type isn't canonical or has qualifiers, this won't
18003b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall  // be a canonical type either, so fill in the canonical type field.
18013b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall  QualType Canon;
18023b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall  if (!EltTy.isCanonical() || EltTy.hasLocalQualifiers()) {
18033b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall    SplitQualType canonSplit = getCanonicalType(EltTy).split();
1804200fa53fd420aa8369586f569dbece04930ad6a3John McCall    Canon = getConstantArrayType(QualType(canonSplit.Ty, 0), ArySize,
180563e7d25d2e6036616b42f744fd4a39cd5f911960Abramo Bagnara                                 ASM, IndexTypeQuals);
1806200fa53fd420aa8369586f569dbece04930ad6a3John McCall    Canon = getQualifiedType(Canon, canonSplit.Quals);
18073b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall
18085f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    // Get the new insert position for the node we care about.
18091eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    ConstantArrayType *NewIP =
18107192f8e9592729882a09d84d77838db26e39ebd4Ted Kremenek      ConstantArrayTypes.FindNodeOrInsertPos(ID, InsertPos);
1811c6ed729f669044f5072a49d79041f455d971ece3Jeffrey Yasskin    assert(NewIP == 0 && "Shouldn't be in the map!"); (void)NewIP;
18125f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
18131eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
18146b304a0254a13f42390b865ff5ba668a49cc58aeJohn McCall  ConstantArrayType *New = new(*this,TypeAlignment)
181563e7d25d2e6036616b42f744fd4a39cd5f911960Abramo Bagnara    ConstantArrayType(EltTy, Canon, ArySize, ASM, IndexTypeQuals);
18167192f8e9592729882a09d84d77838db26e39ebd4Ted Kremenek  ConstantArrayTypes.InsertNode(New, InsertPos);
18175f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  Types.push_back(New);
18185f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  return QualType(New, 0);
18195f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
18205f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
1821ce8890371fcdb983ae487c87fa40606a34896ff7John McCall/// getVariableArrayDecayedType - Turns the given type, which may be
1822ce8890371fcdb983ae487c87fa40606a34896ff7John McCall/// variably-modified, into the corresponding type with all the known
1823ce8890371fcdb983ae487c87fa40606a34896ff7John McCall/// sizes replaced with [*].
1824ce8890371fcdb983ae487c87fa40606a34896ff7John McCallQualType ASTContext::getVariableArrayDecayedType(QualType type) const {
1825ce8890371fcdb983ae487c87fa40606a34896ff7John McCall  // Vastly most common case.
1826ce8890371fcdb983ae487c87fa40606a34896ff7John McCall  if (!type->isVariablyModifiedType()) return type;
1827ce8890371fcdb983ae487c87fa40606a34896ff7John McCall
1828ce8890371fcdb983ae487c87fa40606a34896ff7John McCall  QualType result;
1829ce8890371fcdb983ae487c87fa40606a34896ff7John McCall
1830ce8890371fcdb983ae487c87fa40606a34896ff7John McCall  SplitQualType split = type.getSplitDesugaredType();
1831200fa53fd420aa8369586f569dbece04930ad6a3John McCall  const Type *ty = split.Ty;
1832ce8890371fcdb983ae487c87fa40606a34896ff7John McCall  switch (ty->getTypeClass()) {
1833ce8890371fcdb983ae487c87fa40606a34896ff7John McCall#define TYPE(Class, Base)
1834ce8890371fcdb983ae487c87fa40606a34896ff7John McCall#define ABSTRACT_TYPE(Class, Base)
1835ce8890371fcdb983ae487c87fa40606a34896ff7John McCall#define NON_CANONICAL_TYPE(Class, Base) case Type::Class:
1836ce8890371fcdb983ae487c87fa40606a34896ff7John McCall#include "clang/AST/TypeNodes.def"
1837ce8890371fcdb983ae487c87fa40606a34896ff7John McCall    llvm_unreachable("didn't desugar past all non-canonical types?");
1838ce8890371fcdb983ae487c87fa40606a34896ff7John McCall
1839ce8890371fcdb983ae487c87fa40606a34896ff7John McCall  // These types should never be variably-modified.
1840ce8890371fcdb983ae487c87fa40606a34896ff7John McCall  case Type::Builtin:
1841ce8890371fcdb983ae487c87fa40606a34896ff7John McCall  case Type::Complex:
1842ce8890371fcdb983ae487c87fa40606a34896ff7John McCall  case Type::Vector:
1843ce8890371fcdb983ae487c87fa40606a34896ff7John McCall  case Type::ExtVector:
1844ce8890371fcdb983ae487c87fa40606a34896ff7John McCall  case Type::DependentSizedExtVector:
1845ce8890371fcdb983ae487c87fa40606a34896ff7John McCall  case Type::ObjCObject:
1846ce8890371fcdb983ae487c87fa40606a34896ff7John McCall  case Type::ObjCInterface:
1847ce8890371fcdb983ae487c87fa40606a34896ff7John McCall  case Type::ObjCObjectPointer:
1848ce8890371fcdb983ae487c87fa40606a34896ff7John McCall  case Type::Record:
1849ce8890371fcdb983ae487c87fa40606a34896ff7John McCall  case Type::Enum:
1850ce8890371fcdb983ae487c87fa40606a34896ff7John McCall  case Type::UnresolvedUsing:
1851ce8890371fcdb983ae487c87fa40606a34896ff7John McCall  case Type::TypeOfExpr:
1852ce8890371fcdb983ae487c87fa40606a34896ff7John McCall  case Type::TypeOf:
1853ce8890371fcdb983ae487c87fa40606a34896ff7John McCall  case Type::Decltype:
1854ca63c200346c0ca9e00194ec6e34a5a7b0ed9321Sean Hunt  case Type::UnaryTransform:
1855ce8890371fcdb983ae487c87fa40606a34896ff7John McCall  case Type::DependentName:
1856ce8890371fcdb983ae487c87fa40606a34896ff7John McCall  case Type::InjectedClassName:
1857ce8890371fcdb983ae487c87fa40606a34896ff7John McCall  case Type::TemplateSpecialization:
1858ce8890371fcdb983ae487c87fa40606a34896ff7John McCall  case Type::DependentTemplateSpecialization:
1859ce8890371fcdb983ae487c87fa40606a34896ff7John McCall  case Type::TemplateTypeParm:
1860ce8890371fcdb983ae487c87fa40606a34896ff7John McCall  case Type::SubstTemplateTypeParmPack:
186134b41d939a1328f484511c6002ba2456db879a29Richard Smith  case Type::Auto:
1862ce8890371fcdb983ae487c87fa40606a34896ff7John McCall  case Type::PackExpansion:
1863ce8890371fcdb983ae487c87fa40606a34896ff7John McCall    llvm_unreachable("type should never be variably-modified");
1864ce8890371fcdb983ae487c87fa40606a34896ff7John McCall
1865ce8890371fcdb983ae487c87fa40606a34896ff7John McCall  // These types can be variably-modified but should never need to
1866ce8890371fcdb983ae487c87fa40606a34896ff7John McCall  // further decay.
1867ce8890371fcdb983ae487c87fa40606a34896ff7John McCall  case Type::FunctionNoProto:
1868ce8890371fcdb983ae487c87fa40606a34896ff7John McCall  case Type::FunctionProto:
1869ce8890371fcdb983ae487c87fa40606a34896ff7John McCall  case Type::BlockPointer:
1870ce8890371fcdb983ae487c87fa40606a34896ff7John McCall  case Type::MemberPointer:
1871ce8890371fcdb983ae487c87fa40606a34896ff7John McCall    return type;
1872ce8890371fcdb983ae487c87fa40606a34896ff7John McCall
1873ce8890371fcdb983ae487c87fa40606a34896ff7John McCall  // These types can be variably-modified.  All these modifications
1874ce8890371fcdb983ae487c87fa40606a34896ff7John McCall  // preserve structure except as noted by comments.
1875ce8890371fcdb983ae487c87fa40606a34896ff7John McCall  // TODO: if we ever care about optimizing VLAs, there are no-op
1876ce8890371fcdb983ae487c87fa40606a34896ff7John McCall  // optimizations available here.
1877ce8890371fcdb983ae487c87fa40606a34896ff7John McCall  case Type::Pointer:
1878ce8890371fcdb983ae487c87fa40606a34896ff7John McCall    result = getPointerType(getVariableArrayDecayedType(
1879ce8890371fcdb983ae487c87fa40606a34896ff7John McCall                              cast<PointerType>(ty)->getPointeeType()));
1880ce8890371fcdb983ae487c87fa40606a34896ff7John McCall    break;
1881ce8890371fcdb983ae487c87fa40606a34896ff7John McCall
1882ce8890371fcdb983ae487c87fa40606a34896ff7John McCall  case Type::LValueReference: {
1883ce8890371fcdb983ae487c87fa40606a34896ff7John McCall    const LValueReferenceType *lv = cast<LValueReferenceType>(ty);
1884ce8890371fcdb983ae487c87fa40606a34896ff7John McCall    result = getLValueReferenceType(
1885ce8890371fcdb983ae487c87fa40606a34896ff7John McCall                 getVariableArrayDecayedType(lv->getPointeeType()),
1886ce8890371fcdb983ae487c87fa40606a34896ff7John McCall                                    lv->isSpelledAsLValue());
1887ce8890371fcdb983ae487c87fa40606a34896ff7John McCall    break;
1888745da3a5bb4ea35f93f50301e7fbbb7d78d3b6bbFariborz Jahanian  }
1889ce8890371fcdb983ae487c87fa40606a34896ff7John McCall
1890ce8890371fcdb983ae487c87fa40606a34896ff7John McCall  case Type::RValueReference: {
1891ce8890371fcdb983ae487c87fa40606a34896ff7John McCall    const RValueReferenceType *lv = cast<RValueReferenceType>(ty);
1892ce8890371fcdb983ae487c87fa40606a34896ff7John McCall    result = getRValueReferenceType(
1893ce8890371fcdb983ae487c87fa40606a34896ff7John McCall                 getVariableArrayDecayedType(lv->getPointeeType()));
1894ce8890371fcdb983ae487c87fa40606a34896ff7John McCall    break;
1895745da3a5bb4ea35f93f50301e7fbbb7d78d3b6bbFariborz Jahanian  }
1896745da3a5bb4ea35f93f50301e7fbbb7d78d3b6bbFariborz Jahanian
1897b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman  case Type::Atomic: {
1898b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman    const AtomicType *at = cast<AtomicType>(ty);
1899b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman    result = getAtomicType(getVariableArrayDecayedType(at->getValueType()));
1900b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman    break;
1901b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman  }
1902b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman
1903ce8890371fcdb983ae487c87fa40606a34896ff7John McCall  case Type::ConstantArray: {
1904ce8890371fcdb983ae487c87fa40606a34896ff7John McCall    const ConstantArrayType *cat = cast<ConstantArrayType>(ty);
1905ce8890371fcdb983ae487c87fa40606a34896ff7John McCall    result = getConstantArrayType(
1906ce8890371fcdb983ae487c87fa40606a34896ff7John McCall                 getVariableArrayDecayedType(cat->getElementType()),
1907ce8890371fcdb983ae487c87fa40606a34896ff7John McCall                                  cat->getSize(),
1908ce8890371fcdb983ae487c87fa40606a34896ff7John McCall                                  cat->getSizeModifier(),
1909ce8890371fcdb983ae487c87fa40606a34896ff7John McCall                                  cat->getIndexTypeCVRQualifiers());
1910ce8890371fcdb983ae487c87fa40606a34896ff7John McCall    break;
1911745da3a5bb4ea35f93f50301e7fbbb7d78d3b6bbFariborz Jahanian  }
1912745da3a5bb4ea35f93f50301e7fbbb7d78d3b6bbFariborz Jahanian
1913ce8890371fcdb983ae487c87fa40606a34896ff7John McCall  case Type::DependentSizedArray: {
1914ce8890371fcdb983ae487c87fa40606a34896ff7John McCall    const DependentSizedArrayType *dat = cast<DependentSizedArrayType>(ty);
1915ce8890371fcdb983ae487c87fa40606a34896ff7John McCall    result = getDependentSizedArrayType(
1916ce8890371fcdb983ae487c87fa40606a34896ff7John McCall                 getVariableArrayDecayedType(dat->getElementType()),
1917ce8890371fcdb983ae487c87fa40606a34896ff7John McCall                                        dat->getSizeExpr(),
1918ce8890371fcdb983ae487c87fa40606a34896ff7John McCall                                        dat->getSizeModifier(),
1919ce8890371fcdb983ae487c87fa40606a34896ff7John McCall                                        dat->getIndexTypeCVRQualifiers(),
1920ce8890371fcdb983ae487c87fa40606a34896ff7John McCall                                        dat->getBracketsRange());
1921ce8890371fcdb983ae487c87fa40606a34896ff7John McCall    break;
1922ce8890371fcdb983ae487c87fa40606a34896ff7John McCall  }
1923ce8890371fcdb983ae487c87fa40606a34896ff7John McCall
1924ce8890371fcdb983ae487c87fa40606a34896ff7John McCall  // Turn incomplete types into [*] types.
1925ce8890371fcdb983ae487c87fa40606a34896ff7John McCall  case Type::IncompleteArray: {
1926ce8890371fcdb983ae487c87fa40606a34896ff7John McCall    const IncompleteArrayType *iat = cast<IncompleteArrayType>(ty);
1927ce8890371fcdb983ae487c87fa40606a34896ff7John McCall    result = getVariableArrayType(
1928ce8890371fcdb983ae487c87fa40606a34896ff7John McCall                 getVariableArrayDecayedType(iat->getElementType()),
1929ce8890371fcdb983ae487c87fa40606a34896ff7John McCall                                  /*size*/ 0,
1930ce8890371fcdb983ae487c87fa40606a34896ff7John McCall                                  ArrayType::Normal,
1931ce8890371fcdb983ae487c87fa40606a34896ff7John McCall                                  iat->getIndexTypeCVRQualifiers(),
1932ce8890371fcdb983ae487c87fa40606a34896ff7John McCall                                  SourceRange());
1933ce8890371fcdb983ae487c87fa40606a34896ff7John McCall    break;
1934ce8890371fcdb983ae487c87fa40606a34896ff7John McCall  }
1935ce8890371fcdb983ae487c87fa40606a34896ff7John McCall
1936ce8890371fcdb983ae487c87fa40606a34896ff7John McCall  // Turn VLA types into [*] types.
1937ce8890371fcdb983ae487c87fa40606a34896ff7John McCall  case Type::VariableArray: {
1938ce8890371fcdb983ae487c87fa40606a34896ff7John McCall    const VariableArrayType *vat = cast<VariableArrayType>(ty);
1939ce8890371fcdb983ae487c87fa40606a34896ff7John McCall    result = getVariableArrayType(
1940ce8890371fcdb983ae487c87fa40606a34896ff7John McCall                 getVariableArrayDecayedType(vat->getElementType()),
1941ce8890371fcdb983ae487c87fa40606a34896ff7John McCall                                  /*size*/ 0,
1942ce8890371fcdb983ae487c87fa40606a34896ff7John McCall                                  ArrayType::Star,
1943ce8890371fcdb983ae487c87fa40606a34896ff7John McCall                                  vat->getIndexTypeCVRQualifiers(),
1944ce8890371fcdb983ae487c87fa40606a34896ff7John McCall                                  vat->getBracketsRange());
1945ce8890371fcdb983ae487c87fa40606a34896ff7John McCall    break;
1946ce8890371fcdb983ae487c87fa40606a34896ff7John McCall  }
1947ce8890371fcdb983ae487c87fa40606a34896ff7John McCall  }
1948ce8890371fcdb983ae487c87fa40606a34896ff7John McCall
1949ce8890371fcdb983ae487c87fa40606a34896ff7John McCall  // Apply the top-level qualifiers from the original.
1950200fa53fd420aa8369586f569dbece04930ad6a3John McCall  return getQualifiedType(result, split.Quals);
1951ce8890371fcdb983ae487c87fa40606a34896ff7John McCall}
1952745da3a5bb4ea35f93f50301e7fbbb7d78d3b6bbFariborz Jahanian
1953bdbf7b030a3e0ddb95240076683830e6f78c79a5Steve Naroff/// getVariableArrayType - Returns a non-unique reference to the type for a
1954bdbf7b030a3e0ddb95240076683830e6f78c79a5Steve Naroff/// variable array of the specified element type.
19557e7eb3da052a6d80ddf2377cab0384c798f73f75Douglas GregorQualType ASTContext::getVariableArrayType(QualType EltTy,
19567e7eb3da052a6d80ddf2377cab0384c798f73f75Douglas Gregor                                          Expr *NumElts,
1957c9406125e2cac9208098655ac8058c095c2c3a65Steve Naroff                                          ArrayType::ArraySizeModifier ASM,
195863e7d25d2e6036616b42f744fd4a39cd5f911960Abramo Bagnara                                          unsigned IndexTypeQuals,
19594ba2a17694148e16eaa8d3917f657ffcd3667be4Jay Foad                                          SourceRange Brackets) const {
1960c5773c4b8ce1ed6ed5c7112c9020c954a47dce96Eli Friedman  // Since we don't unique expressions, it isn't possible to unique VLA's
1961c5773c4b8ce1ed6ed5c7112c9020c954a47dce96Eli Friedman  // that have an expression provided for their size.
19623b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall  QualType Canon;
1963715e9c8a39437347e838aa108df443fe1086d359Douglas Gregor
19643b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall  // Be sure to pull qualifiers off the element type.
19653b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall  if (!EltTy.isCanonical() || EltTy.hasLocalQualifiers()) {
19663b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall    SplitQualType canonSplit = getCanonicalType(EltTy).split();
1967200fa53fd420aa8369586f569dbece04930ad6a3John McCall    Canon = getVariableArrayType(QualType(canonSplit.Ty, 0), NumElts, ASM,
196863e7d25d2e6036616b42f744fd4a39cd5f911960Abramo Bagnara                                 IndexTypeQuals, Brackets);
1969200fa53fd420aa8369586f569dbece04930ad6a3John McCall    Canon = getQualifiedType(Canon, canonSplit.Quals);
1970715e9c8a39437347e838aa108df443fe1086d359Douglas Gregor  }
1971715e9c8a39437347e838aa108df443fe1086d359Douglas Gregor
19726b304a0254a13f42390b865ff5ba668a49cc58aeJohn McCall  VariableArrayType *New = new(*this, TypeAlignment)
197363e7d25d2e6036616b42f744fd4a39cd5f911960Abramo Bagnara    VariableArrayType(EltTy, Canon, NumElts, ASM, IndexTypeQuals, Brackets);
1974c5773c4b8ce1ed6ed5c7112c9020c954a47dce96Eli Friedman
1975c5773c4b8ce1ed6ed5c7112c9020c954a47dce96Eli Friedman  VariableArrayTypes.push_back(New);
1976c5773c4b8ce1ed6ed5c7112c9020c954a47dce96Eli Friedman  Types.push_back(New);
1977c5773c4b8ce1ed6ed5c7112c9020c954a47dce96Eli Friedman  return QualType(New, 0);
1978c5773c4b8ce1ed6ed5c7112c9020c954a47dce96Eli Friedman}
1979c5773c4b8ce1ed6ed5c7112c9020c954a47dce96Eli Friedman
1980898574e7496ba8fd76290079d3a9d06954992734Douglas Gregor/// getDependentSizedArrayType - Returns a non-unique reference to
1981898574e7496ba8fd76290079d3a9d06954992734Douglas Gregor/// the type for a dependently-sized array of the specified element
198204d4beee4b86af20a9e4457023d3925cab8f9908Douglas Gregor/// type.
19833b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCallQualType ASTContext::getDependentSizedArrayType(QualType elementType,
19843b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall                                                Expr *numElements,
1985898574e7496ba8fd76290079d3a9d06954992734Douglas Gregor                                                ArrayType::ArraySizeModifier ASM,
19863b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall                                                unsigned elementTypeQuals,
19873b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall                                                SourceRange brackets) const {
19883b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall  assert((!numElements || numElements->isTypeDependent() ||
19893b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall          numElements->isValueDependent()) &&
1990898574e7496ba8fd76290079d3a9d06954992734Douglas Gregor         "Size must be type- or value-dependent!");
1991898574e7496ba8fd76290079d3a9d06954992734Douglas Gregor
19923b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall  // Dependently-sized array types that do not have a specified number
19933b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall  // of elements will have their sizes deduced from a dependent
19943b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall  // initializer.  We do no canonicalization here at all, which is okay
19953b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall  // because they can't be used in most locations.
19963b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall  if (!numElements) {
19973b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall    DependentSizedArrayType *newType
19983b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall      = new (*this, TypeAlignment)
19993b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall          DependentSizedArrayType(*this, elementType, QualType(),
20003b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall                                  numElements, ASM, elementTypeQuals,
20013b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall                                  brackets);
20023b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall    Types.push_back(newType);
20033b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall    return QualType(newType, 0);
2004cb78d8852a454684c987220132cdb5e54dd00121Douglas Gregor  }
2005cb78d8852a454684c987220132cdb5e54dd00121Douglas Gregor
20063b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall  // Otherwise, we actually build a new type every time, but we
20073b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall  // also build a canonical type.
20081eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
20093b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall  SplitQualType canonElementType = getCanonicalType(elementType).split();
2010898574e7496ba8fd76290079d3a9d06954992734Douglas Gregor
20113b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall  void *insertPos = 0;
20123b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall  llvm::FoldingSetNodeID ID;
20133b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall  DependentSizedArrayType::Profile(ID, *this,
2014200fa53fd420aa8369586f569dbece04930ad6a3John McCall                                   QualType(canonElementType.Ty, 0),
20153b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall                                   ASM, elementTypeQuals, numElements);
20163b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall
20173b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall  // Look for an existing type with these properties.
20183b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall  DependentSizedArrayType *canonTy =
20193b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall    DependentSizedArrayTypes.FindNodeOrInsertPos(ID, insertPos);
20203b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall
20213b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall  // If we don't have one, build one.
20223b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall  if (!canonTy) {
20233b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall    canonTy = new (*this, TypeAlignment)
2024200fa53fd420aa8369586f569dbece04930ad6a3John McCall      DependentSizedArrayType(*this, QualType(canonElementType.Ty, 0),
20253b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall                              QualType(), numElements, ASM, elementTypeQuals,
20263b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall                              brackets);
20273b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall    DependentSizedArrayTypes.InsertNode(canonTy, insertPos);
20283b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall    Types.push_back(canonTy);
20293b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall  }
20303b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall
20313b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall  // Apply qualifiers from the element type to the array.
20323b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall  QualType canon = getQualifiedType(QualType(canonTy,0),
2033200fa53fd420aa8369586f569dbece04930ad6a3John McCall                                    canonElementType.Quals);
20343b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall
20353b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall  // If we didn't need extra canonicalization for the element type,
20363b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall  // then just use that as our result.
2037200fa53fd420aa8369586f569dbece04930ad6a3John McCall  if (QualType(canonElementType.Ty, 0) == elementType)
20383b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall    return canon;
20393b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall
20403b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall  // Otherwise, we need to build a type which follows the spelling
20413b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall  // of the element type.
20423b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall  DependentSizedArrayType *sugaredType
20433b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall    = new (*this, TypeAlignment)
20443b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall        DependentSizedArrayType(*this, elementType, canon, numElements,
20453b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall                                ASM, elementTypeQuals, brackets);
20463b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall  Types.push_back(sugaredType);
20473b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall  return QualType(sugaredType, 0);
20483b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall}
20493b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall
20503b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCallQualType ASTContext::getIncompleteArrayType(QualType elementType,
2051c5773c4b8ce1ed6ed5c7112c9020c954a47dce96Eli Friedman                                            ArrayType::ArraySizeModifier ASM,
20523b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall                                            unsigned elementTypeQuals) const {
2053c5773c4b8ce1ed6ed5c7112c9020c954a47dce96Eli Friedman  llvm::FoldingSetNodeID ID;
20543b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall  IncompleteArrayType::Profile(ID, elementType, ASM, elementTypeQuals);
2055c5773c4b8ce1ed6ed5c7112c9020c954a47dce96Eli Friedman
20563b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall  void *insertPos = 0;
20573b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall  if (IncompleteArrayType *iat =
20583b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall       IncompleteArrayTypes.FindNodeOrInsertPos(ID, insertPos))
20593b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall    return QualType(iat, 0);
2060c5773c4b8ce1ed6ed5c7112c9020c954a47dce96Eli Friedman
2061c5773c4b8ce1ed6ed5c7112c9020c954a47dce96Eli Friedman  // If the element type isn't canonical, this won't be a canonical type
20623b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall  // either, so fill in the canonical type field.  We also have to pull
20633b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall  // qualifiers off the element type.
20643b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall  QualType canon;
2065c5773c4b8ce1ed6ed5c7112c9020c954a47dce96Eli Friedman
20663b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall  if (!elementType.isCanonical() || elementType.hasLocalQualifiers()) {
20673b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall    SplitQualType canonSplit = getCanonicalType(elementType).split();
2068200fa53fd420aa8369586f569dbece04930ad6a3John McCall    canon = getIncompleteArrayType(QualType(canonSplit.Ty, 0),
20693b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall                                   ASM, elementTypeQuals);
2070200fa53fd420aa8369586f569dbece04930ad6a3John McCall    canon = getQualifiedType(canon, canonSplit.Quals);
2071c5773c4b8ce1ed6ed5c7112c9020c954a47dce96Eli Friedman
2072c5773c4b8ce1ed6ed5c7112c9020c954a47dce96Eli Friedman    // Get the new insert position for the node we care about.
20733b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall    IncompleteArrayType *existing =
20743b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall      IncompleteArrayTypes.FindNodeOrInsertPos(ID, insertPos);
20753b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall    assert(!existing && "Shouldn't be in the map!"); (void) existing;
20762bd24ba6d10f8c811c8e2a57c8397e07082ba497Ted Kremenek  }
2077c5773c4b8ce1ed6ed5c7112c9020c954a47dce96Eli Friedman
20783b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall  IncompleteArrayType *newType = new (*this, TypeAlignment)
20793b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall    IncompleteArrayType(elementType, canon, ASM, elementTypeQuals);
2080c5773c4b8ce1ed6ed5c7112c9020c954a47dce96Eli Friedman
20813b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall  IncompleteArrayTypes.InsertNode(newType, insertPos);
20823b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall  Types.push_back(newType);
20833b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall  return QualType(newType, 0);
2084fb22d96692c5240fb8d611290dbf7eeed3759c73Steve Naroff}
2085fb22d96692c5240fb8d611290dbf7eeed3759c73Steve Naroff
208673322924127c873c13101b705dd823f5539ffa5fSteve Naroff/// getVectorType - Return the unique reference to a vector type of
208773322924127c873c13101b705dd823f5539ffa5fSteve Naroff/// the specified element type and size. VectorType must be a built-in type.
208882287d19ded35248c4ce6a425ce74116a13ce44eJohn ThompsonQualType ASTContext::getVectorType(QualType vecType, unsigned NumElts,
20894ba2a17694148e16eaa8d3917f657ffcd3667be4Jay Foad                                   VectorType::VectorKind VecKind) const {
20903b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall  assert(vecType->isBuiltinType());
20911eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
20925f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // Check if we've already instantiated a vector of this type.
20935f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  llvm::FoldingSetNodeID ID;
2094e86d78cf4754a6aef2cf9a33d847aa15338e276fBob Wilson  VectorType::Profile(ID, vecType, NumElts, Type::Vector, VecKind);
2095788b0fd67e1992f23555454efcdb16a19dfefac3Chris Lattner
20965f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  void *InsertPos = 0;
20975f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  if (VectorType *VTP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos))
20985f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    return QualType(VTP, 0);
20995f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
21005f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // If the element type isn't canonical, this won't be a canonical type either,
21015f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // so fill in the canonical type field.
21025f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  QualType Canonical;
2103255210ef415b9893f0e3794e8d9a704194c12f3cDouglas Gregor  if (!vecType.isCanonical()) {
2104231da7eb3dd13007e5e40fffe48998e5ef284e06Bob Wilson    Canonical = getVectorType(getCanonicalType(vecType), NumElts, VecKind);
21051eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
21065f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    // Get the new insert position for the node we care about.
21075f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    VectorType *NewIP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos);
2108c6ed729f669044f5072a49d79041f455d971ece3Jeffrey Yasskin    assert(NewIP == 0 && "Shouldn't be in the map!"); (void)NewIP;
21095f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
21106b304a0254a13f42390b865ff5ba668a49cc58aeJohn McCall  VectorType *New = new (*this, TypeAlignment)
2111e86d78cf4754a6aef2cf9a33d847aa15338e276fBob Wilson    VectorType(vecType, NumElts, Canonical, VecKind);
21125f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  VectorTypes.InsertNode(New, InsertPos);
21135f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  Types.push_back(New);
21145f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  return QualType(New, 0);
21155f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
21165f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
2117213541a68a3e137d11d2cefb612c6cdb410d7e8eNate Begeman/// getExtVectorType - Return the unique reference to an extended vector type of
211873322924127c873c13101b705dd823f5539ffa5fSteve Naroff/// the specified element type and size. VectorType must be a built-in type.
21194ba2a17694148e16eaa8d3917f657ffcd3667be4Jay FoadQualType
21204ba2a17694148e16eaa8d3917f657ffcd3667be4Jay FoadASTContext::getExtVectorType(QualType vecType, unsigned NumElts) const {
21214ac01401b1ec602a1f58c217544d3dcb5fcbd7f1Douglas Gregor  assert(vecType->isBuiltinType() || vecType->isDependentType());
21221eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
212373322924127c873c13101b705dd823f5539ffa5fSteve Naroff  // Check if we've already instantiated a vector of this type.
212473322924127c873c13101b705dd823f5539ffa5fSteve Naroff  llvm::FoldingSetNodeID ID;
2125788b0fd67e1992f23555454efcdb16a19dfefac3Chris Lattner  VectorType::Profile(ID, vecType, NumElts, Type::ExtVector,
2126e86d78cf4754a6aef2cf9a33d847aa15338e276fBob Wilson                      VectorType::GenericVector);
212773322924127c873c13101b705dd823f5539ffa5fSteve Naroff  void *InsertPos = 0;
212873322924127c873c13101b705dd823f5539ffa5fSteve Naroff  if (VectorType *VTP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos))
212973322924127c873c13101b705dd823f5539ffa5fSteve Naroff    return QualType(VTP, 0);
213073322924127c873c13101b705dd823f5539ffa5fSteve Naroff
213173322924127c873c13101b705dd823f5539ffa5fSteve Naroff  // If the element type isn't canonical, this won't be a canonical type either,
213273322924127c873c13101b705dd823f5539ffa5fSteve Naroff  // so fill in the canonical type field.
213373322924127c873c13101b705dd823f5539ffa5fSteve Naroff  QualType Canonical;
2134467b27b9a24bdc823218ad1ad0e37673b6cc1e83John McCall  if (!vecType.isCanonical()) {
2135213541a68a3e137d11d2cefb612c6cdb410d7e8eNate Begeman    Canonical = getExtVectorType(getCanonicalType(vecType), NumElts);
21361eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
213773322924127c873c13101b705dd823f5539ffa5fSteve Naroff    // Get the new insert position for the node we care about.
213873322924127c873c13101b705dd823f5539ffa5fSteve Naroff    VectorType *NewIP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos);
2139c6ed729f669044f5072a49d79041f455d971ece3Jeffrey Yasskin    assert(NewIP == 0 && "Shouldn't be in the map!"); (void)NewIP;
214073322924127c873c13101b705dd823f5539ffa5fSteve Naroff  }
21416b304a0254a13f42390b865ff5ba668a49cc58aeJohn McCall  ExtVectorType *New = new (*this, TypeAlignment)
21426b304a0254a13f42390b865ff5ba668a49cc58aeJohn McCall    ExtVectorType(vecType, NumElts, Canonical);
214373322924127c873c13101b705dd823f5539ffa5fSteve Naroff  VectorTypes.InsertNode(New, InsertPos);
214473322924127c873c13101b705dd823f5539ffa5fSteve Naroff  Types.push_back(New);
214573322924127c873c13101b705dd823f5539ffa5fSteve Naroff  return QualType(New, 0);
214673322924127c873c13101b705dd823f5539ffa5fSteve Naroff}
214773322924127c873c13101b705dd823f5539ffa5fSteve Naroff
21484ba2a17694148e16eaa8d3917f657ffcd3667be4Jay FoadQualType
21494ba2a17694148e16eaa8d3917f657ffcd3667be4Jay FoadASTContext::getDependentSizedExtVectorType(QualType vecType,
21504ba2a17694148e16eaa8d3917f657ffcd3667be4Jay Foad                                           Expr *SizeExpr,
21514ba2a17694148e16eaa8d3917f657ffcd3667be4Jay Foad                                           SourceLocation AttrLoc) const {
21522ec09f1dc123e1942ed756e8ee4fef86451eac9eDouglas Gregor  llvm::FoldingSetNodeID ID;
21531eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  DependentSizedExtVectorType::Profile(ID, *this, getCanonicalType(vecType),
21542ec09f1dc123e1942ed756e8ee4fef86451eac9eDouglas Gregor                                       SizeExpr);
21551eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
21562ec09f1dc123e1942ed756e8ee4fef86451eac9eDouglas Gregor  void *InsertPos = 0;
21572ec09f1dc123e1942ed756e8ee4fef86451eac9eDouglas Gregor  DependentSizedExtVectorType *Canon
21582ec09f1dc123e1942ed756e8ee4fef86451eac9eDouglas Gregor    = DependentSizedExtVectorTypes.FindNodeOrInsertPos(ID, InsertPos);
21592ec09f1dc123e1942ed756e8ee4fef86451eac9eDouglas Gregor  DependentSizedExtVectorType *New;
21602ec09f1dc123e1942ed756e8ee4fef86451eac9eDouglas Gregor  if (Canon) {
21612ec09f1dc123e1942ed756e8ee4fef86451eac9eDouglas Gregor    // We already have a canonical version of this array type; use it as
21622ec09f1dc123e1942ed756e8ee4fef86451eac9eDouglas Gregor    // the canonical type for a newly-built type.
21636b304a0254a13f42390b865ff5ba668a49cc58aeJohn McCall    New = new (*this, TypeAlignment)
21646b304a0254a13f42390b865ff5ba668a49cc58aeJohn McCall      DependentSizedExtVectorType(*this, vecType, QualType(Canon, 0),
21656b304a0254a13f42390b865ff5ba668a49cc58aeJohn McCall                                  SizeExpr, AttrLoc);
21662ec09f1dc123e1942ed756e8ee4fef86451eac9eDouglas Gregor  } else {
21672ec09f1dc123e1942ed756e8ee4fef86451eac9eDouglas Gregor    QualType CanonVecTy = getCanonicalType(vecType);
21682ec09f1dc123e1942ed756e8ee4fef86451eac9eDouglas Gregor    if (CanonVecTy == vecType) {
21696b304a0254a13f42390b865ff5ba668a49cc58aeJohn McCall      New = new (*this, TypeAlignment)
21706b304a0254a13f42390b865ff5ba668a49cc58aeJohn McCall        DependentSizedExtVectorType(*this, vecType, QualType(), SizeExpr,
21716b304a0254a13f42390b865ff5ba668a49cc58aeJohn McCall                                    AttrLoc);
2172789b1f640205e81b5af250693246120f1ce9d147Douglas Gregor
2173789b1f640205e81b5af250693246120f1ce9d147Douglas Gregor      DependentSizedExtVectorType *CanonCheck
2174789b1f640205e81b5af250693246120f1ce9d147Douglas Gregor        = DependentSizedExtVectorTypes.FindNodeOrInsertPos(ID, InsertPos);
2175789b1f640205e81b5af250693246120f1ce9d147Douglas Gregor      assert(!CanonCheck && "Dependent-sized ext_vector canonical type broken");
2176789b1f640205e81b5af250693246120f1ce9d147Douglas Gregor      (void)CanonCheck;
21772ec09f1dc123e1942ed756e8ee4fef86451eac9eDouglas Gregor      DependentSizedExtVectorTypes.InsertNode(New, InsertPos);
21782ec09f1dc123e1942ed756e8ee4fef86451eac9eDouglas Gregor    } else {
21792ec09f1dc123e1942ed756e8ee4fef86451eac9eDouglas Gregor      QualType Canon = getDependentSizedExtVectorType(CanonVecTy, SizeExpr,
21802ec09f1dc123e1942ed756e8ee4fef86451eac9eDouglas Gregor                                                      SourceLocation());
21816b304a0254a13f42390b865ff5ba668a49cc58aeJohn McCall      New = new (*this, TypeAlignment)
21826b304a0254a13f42390b865ff5ba668a49cc58aeJohn McCall        DependentSizedExtVectorType(*this, vecType, Canon, SizeExpr, AttrLoc);
21832ec09f1dc123e1942ed756e8ee4fef86451eac9eDouglas Gregor    }
21842ec09f1dc123e1942ed756e8ee4fef86451eac9eDouglas Gregor  }
21851eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
21869cdda0cf8528e3d595be9bfa002f0450074beb4dDouglas Gregor  Types.push_back(New);
21879cdda0cf8528e3d595be9bfa002f0450074beb4dDouglas Gregor  return QualType(New, 0);
21889cdda0cf8528e3d595be9bfa002f0450074beb4dDouglas Gregor}
21899cdda0cf8528e3d595be9bfa002f0450074beb4dDouglas Gregor
219072564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor/// getFunctionNoProtoType - Return a K&R style C function type like 'int()'.
21915f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer///
21924ba2a17694148e16eaa8d3917f657ffcd3667be4Jay FoadQualType
21934ba2a17694148e16eaa8d3917f657ffcd3667be4Jay FoadASTContext::getFunctionNoProtoType(QualType ResultTy,
21944ba2a17694148e16eaa8d3917f657ffcd3667be4Jay Foad                                   const FunctionType::ExtInfo &Info) const {
2195cfe9af250f466e7e38becea4428990448ae07737Roman Divacky  const CallingConv DefaultCC = Info.getCC();
2196cfe9af250f466e7e38becea4428990448ae07737Roman Divacky  const CallingConv CallConv = (LangOpts.MRTD && DefaultCC == CC_Default) ?
2197cfe9af250f466e7e38becea4428990448ae07737Roman Divacky                               CC_X86StdCall : DefaultCC;
21985f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // Unique functions, to guarantee there is only one function of a particular
21995f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // structure.
22005f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  llvm::FoldingSetNodeID ID;
2201264ba48dc98f3f843935a485d5b086f7e0fdc4f1Rafael Espindola  FunctionNoProtoType::Profile(ID, ResultTy, Info);
22021eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
22035f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  void *InsertPos = 0;
22041eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  if (FunctionNoProtoType *FT =
220572564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor        FunctionNoProtoTypes.FindNodeOrInsertPos(ID, InsertPos))
22065f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    return QualType(FT, 0);
22071eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
22085f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  QualType Canonical;
2209ab8bbf4ebd3e3e6eab913cb044772a62b7581941Douglas Gregor  if (!ResultTy.isCanonical() ||
221004a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall      getCanonicalCallConv(CallConv) != CallConv) {
2211264ba48dc98f3f843935a485d5b086f7e0fdc4f1Rafael Espindola    Canonical =
2212264ba48dc98f3f843935a485d5b086f7e0fdc4f1Rafael Espindola      getFunctionNoProtoType(getCanonicalType(ResultTy),
2213264ba48dc98f3f843935a485d5b086f7e0fdc4f1Rafael Espindola                     Info.withCallingConv(getCanonicalCallConv(CallConv)));
22141eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
22155f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    // Get the new insert position for the node we care about.
221672564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor    FunctionNoProtoType *NewIP =
221772564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor      FunctionNoProtoTypes.FindNodeOrInsertPos(ID, InsertPos);
2218c6ed729f669044f5072a49d79041f455d971ece3Jeffrey Yasskin    assert(NewIP == 0 && "Shouldn't be in the map!"); (void)NewIP;
22195f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
22201eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
2221cfe9af250f466e7e38becea4428990448ae07737Roman Divacky  FunctionProtoType::ExtInfo newInfo = Info.withCallingConv(CallConv);
22226b304a0254a13f42390b865ff5ba668a49cc58aeJohn McCall  FunctionNoProtoType *New = new (*this, TypeAlignment)
2223cfe9af250f466e7e38becea4428990448ae07737Roman Divacky    FunctionNoProtoType(ResultTy, Canonical, newInfo);
22245f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  Types.push_back(New);
222572564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor  FunctionNoProtoTypes.InsertNode(New, InsertPos);
22265f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  return QualType(New, 0);
22275f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
22285f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
22295f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// getFunctionType - Return a normal function type with a typed argument
22305f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// list.  isVariadic indicates whether the argument list includes '...'.
22314ba2a17694148e16eaa8d3917f657ffcd3667be4Jay FoadQualType
22324ba2a17694148e16eaa8d3917f657ffcd3667be4Jay FoadASTContext::getFunctionType(QualType ResultTy,
22334ba2a17694148e16eaa8d3917f657ffcd3667be4Jay Foad                            const QualType *ArgArray, unsigned NumArgs,
22344ba2a17694148e16eaa8d3917f657ffcd3667be4Jay Foad                            const FunctionProtoType::ExtProtoInfo &EPI) const {
22355f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // Unique functions, to guarantee there is only one function of a particular
22365f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // structure.
22375f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  llvm::FoldingSetNodeID ID;
22388026f6d82f7fa544bc0453714fe94bca62a1196eSebastian Redl  FunctionProtoType::Profile(ID, ResultTy, ArgArray, NumArgs, EPI, *this);
22395f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
22405f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  void *InsertPos = 0;
22411eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  if (FunctionProtoType *FTP =
224272564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor        FunctionProtoTypes.FindNodeOrInsertPos(ID, InsertPos))
22435f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    return QualType(FTP, 0);
2244465226e23a3008bd68973513dda1f9e3cd27dbddSebastian Redl
2245465226e23a3008bd68973513dda1f9e3cd27dbddSebastian Redl  // Determine whether the type being created is already canonical or not.
2246eefb3d5b49c844347f212073a7e975b8118fe8e9Richard Smith  bool isCanonical =
2247eefb3d5b49c844347f212073a7e975b8118fe8e9Richard Smith    EPI.ExceptionSpecType == EST_None && ResultTy.isCanonical() &&
2248eefb3d5b49c844347f212073a7e975b8118fe8e9Richard Smith    !EPI.HasTrailingReturn;
22495f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  for (unsigned i = 0; i != NumArgs && isCanonical; ++i)
225054e14c4db764c0636160d26c5bbf491637c83a76John McCall    if (!ArgArray[i].isCanonicalAsParam())
22515f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      isCanonical = false;
22525f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
2253cfe9af250f466e7e38becea4428990448ae07737Roman Divacky  const CallingConv DefaultCC = EPI.ExtInfo.getCC();
2254cfe9af250f466e7e38becea4428990448ae07737Roman Divacky  const CallingConv CallConv = (LangOpts.MRTD && DefaultCC == CC_Default) ?
2255cfe9af250f466e7e38becea4428990448ae07737Roman Divacky                               CC_X86StdCall : DefaultCC;
2256e23cf437fe76b1ed02d63c3f61b456fd48a915f5John McCall
22575f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // If this type isn't canonical, get the canonical version of it.
2258465226e23a3008bd68973513dda1f9e3cd27dbddSebastian Redl  // The exception spec is not part of the canonical type.
22595f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  QualType Canonical;
226004a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall  if (!isCanonical || getCanonicalCallConv(CallConv) != CallConv) {
22615f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner    SmallVector<QualType, 16> CanonicalArgs;
22625f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    CanonicalArgs.reserve(NumArgs);
22635f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    for (unsigned i = 0; i != NumArgs; ++i)
226454e14c4db764c0636160d26c5bbf491637c83a76John McCall      CanonicalArgs.push_back(getCanonicalParamType(ArgArray[i]));
2265465226e23a3008bd68973513dda1f9e3cd27dbddSebastian Redl
2266e23cf437fe76b1ed02d63c3f61b456fd48a915f5John McCall    FunctionProtoType::ExtProtoInfo CanonicalEPI = EPI;
2267eefb3d5b49c844347f212073a7e975b8118fe8e9Richard Smith    CanonicalEPI.HasTrailingReturn = false;
22688b5b4099c61a136e9a1714c4d8a593febe942268Sebastian Redl    CanonicalEPI.ExceptionSpecType = EST_None;
22698b5b4099c61a136e9a1714c4d8a593febe942268Sebastian Redl    CanonicalEPI.NumExceptions = 0;
2270e23cf437fe76b1ed02d63c3f61b456fd48a915f5John McCall    CanonicalEPI.ExtInfo
2271e23cf437fe76b1ed02d63c3f61b456fd48a915f5John McCall      = CanonicalEPI.ExtInfo.withCallingConv(getCanonicalCallConv(CallConv));
2272e23cf437fe76b1ed02d63c3f61b456fd48a915f5John McCall
2273f52ab250ff92bc51a9ac9a8e19bd43b63a5f844fChris Lattner    Canonical = getFunctionType(getCanonicalType(ResultTy),
2274beaaccd8e2a8748f77b66e2b330fb9136937e14cJay Foad                                CanonicalArgs.data(), NumArgs,
2275e23cf437fe76b1ed02d63c3f61b456fd48a915f5John McCall                                CanonicalEPI);
2276465226e23a3008bd68973513dda1f9e3cd27dbddSebastian Redl
22775f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    // Get the new insert position for the node we care about.
227872564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor    FunctionProtoType *NewIP =
227972564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor      FunctionProtoTypes.FindNodeOrInsertPos(ID, InsertPos);
2280c6ed729f669044f5072a49d79041f455d971ece3Jeffrey Yasskin    assert(NewIP == 0 && "Shouldn't be in the map!"); (void)NewIP;
22815f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
2282465226e23a3008bd68973513dda1f9e3cd27dbddSebastian Redl
2283f85e193739c953358c865005855253af4f68a497John McCall  // FunctionProtoType objects are allocated with extra bytes after
2284f85e193739c953358c865005855253af4f68a497John McCall  // them for three variable size arrays at the end:
2285f85e193739c953358c865005855253af4f68a497John McCall  //  - parameter types
2286f85e193739c953358c865005855253af4f68a497John McCall  //  - exception types
2287f85e193739c953358c865005855253af4f68a497John McCall  //  - consumed-arguments flags
2288f85e193739c953358c865005855253af4f68a497John McCall  // Instead of the exception types, there could be a noexcept
2289f85e193739c953358c865005855253af4f68a497John McCall  // expression.
2290e23cf437fe76b1ed02d63c3f61b456fd48a915f5John McCall  size_t Size = sizeof(FunctionProtoType) +
229160618fa7f88d5162bb5b40988b6b38d4d75d6fc6Sebastian Redl                NumArgs * sizeof(QualType);
229260618fa7f88d5162bb5b40988b6b38d4d75d6fc6Sebastian Redl  if (EPI.ExceptionSpecType == EST_Dynamic)
229360618fa7f88d5162bb5b40988b6b38d4d75d6fc6Sebastian Redl    Size += EPI.NumExceptions * sizeof(QualType);
229460618fa7f88d5162bb5b40988b6b38d4d75d6fc6Sebastian Redl  else if (EPI.ExceptionSpecType == EST_ComputedNoexcept) {
22958026f6d82f7fa544bc0453714fe94bca62a1196eSebastian Redl    Size += sizeof(Expr*);
2296e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith  } else if (EPI.ExceptionSpecType == EST_Uninstantiated) {
229713bffc532bafd45d4a77867993c1afb83c7661beRichard Smith    Size += 2 * sizeof(FunctionDecl*);
229860618fa7f88d5162bb5b40988b6b38d4d75d6fc6Sebastian Redl  }
2299f85e193739c953358c865005855253af4f68a497John McCall  if (EPI.ConsumedArguments)
2300f85e193739c953358c865005855253af4f68a497John McCall    Size += NumArgs * sizeof(bool);
2301f85e193739c953358c865005855253af4f68a497John McCall
2302e23cf437fe76b1ed02d63c3f61b456fd48a915f5John McCall  FunctionProtoType *FTP = (FunctionProtoType*) Allocate(Size, TypeAlignment);
2303cfe9af250f466e7e38becea4428990448ae07737Roman Divacky  FunctionProtoType::ExtProtoInfo newEPI = EPI;
2304cfe9af250f466e7e38becea4428990448ae07737Roman Divacky  newEPI.ExtInfo = EPI.ExtInfo.withCallingConv(CallConv);
23058026f6d82f7fa544bc0453714fe94bca62a1196eSebastian Redl  new (FTP) FunctionProtoType(ResultTy, ArgArray, NumArgs, Canonical, newEPI);
23065f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  Types.push_back(FTP);
230772564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor  FunctionProtoTypes.InsertNode(FTP, InsertPos);
23085f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  return QualType(FTP, 0);
23095f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
23105f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
23113cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall#ifndef NDEBUG
23123cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCallstatic bool NeedsInjectedClassNameType(const RecordDecl *D) {
23133cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall  if (!isa<CXXRecordDecl>(D)) return false;
23143cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall  const CXXRecordDecl *RD = cast<CXXRecordDecl>(D);
23153cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall  if (isa<ClassTemplatePartialSpecializationDecl>(RD))
23163cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall    return true;
23173cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall  if (RD->getDescribedClassTemplate() &&
23183cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall      !isa<ClassTemplateSpecializationDecl>(RD))
23193cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall    return true;
23203cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall  return false;
23213cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall}
23223cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall#endif
23233cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall
23243cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall/// getInjectedClassNameType - Return the unique reference to the
23253cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall/// injected class name type for the specified templated declaration.
23263cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCallQualType ASTContext::getInjectedClassNameType(CXXRecordDecl *Decl,
23274ba2a17694148e16eaa8d3917f657ffcd3667be4Jay Foad                                              QualType TST) const {
23283cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall  assert(NeedsInjectedClassNameType(Decl));
23293cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall  if (Decl->TypeForDecl) {
23303cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall    assert(isa<InjectedClassNameType>(Decl->TypeForDecl));
2331ef96ee0be5f100789f451641542a69cd719144d2Douglas Gregor  } else if (CXXRecordDecl *PrevDecl = Decl->getPreviousDecl()) {
23323cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall    assert(PrevDecl->TypeForDecl && "previous declaration has no type");
23333cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall    Decl->TypeForDecl = PrevDecl->TypeForDecl;
23343cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall    assert(isa<InjectedClassNameType>(Decl->TypeForDecl));
23353cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall  } else {
2336f4c7371fb1d3cebcfb40abad4537bb82515704eaJohn McCall    Type *newType =
233731f17ecbef57b5679c017c375db330546b7b5145John McCall      new (*this, TypeAlignment) InjectedClassNameType(Decl, TST);
2338f4c7371fb1d3cebcfb40abad4537bb82515704eaJohn McCall    Decl->TypeForDecl = newType;
2339f4c7371fb1d3cebcfb40abad4537bb82515704eaJohn McCall    Types.push_back(newType);
23403cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall  }
23413cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall  return QualType(Decl->TypeForDecl, 0);
23423cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall}
23433cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall
23442ce52f3fb95bf544db6bd3d91a72bce7d9cceb6cDouglas Gregor/// getTypeDeclType - Return the unique reference to the type for the
23452ce52f3fb95bf544db6bd3d91a72bce7d9cceb6cDouglas Gregor/// specified type declaration.
23464ba2a17694148e16eaa8d3917f657ffcd3667be4Jay FoadQualType ASTContext::getTypeDeclTypeSlow(const TypeDecl *Decl) const {
23471e6759e9e33dcaa73ce14c8a908ac9f87ac16463Argyrios Kyrtzidis  assert(Decl && "Passed null for Decl param");
2348becb8d5a6ab5103393eac5344ae69bcb860601ddJohn McCall  assert(!Decl->TypeForDecl && "TypeForDecl present in slow case");
23491eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
2350162e1c1b487352434552147967c3dd296ebee2f7Richard Smith  if (const TypedefNameDecl *Typedef = dyn_cast<TypedefNameDecl>(Decl))
23512ce52f3fb95bf544db6bd3d91a72bce7d9cceb6cDouglas Gregor    return getTypedefType(Typedef);
2352becb8d5a6ab5103393eac5344ae69bcb860601ddJohn McCall
2353becb8d5a6ab5103393eac5344ae69bcb860601ddJohn McCall  assert(!isa<TemplateTypeParmDecl>(Decl) &&
2354becb8d5a6ab5103393eac5344ae69bcb860601ddJohn McCall         "Template type parameter types are always available.");
2355becb8d5a6ab5103393eac5344ae69bcb860601ddJohn McCall
235619c8576b7328f4dc2d07682f5da552875c1912efJohn McCall  if (const RecordDecl *Record = dyn_cast<RecordDecl>(Decl)) {
2357ef96ee0be5f100789f451641542a69cd719144d2Douglas Gregor    assert(!Record->getPreviousDecl() &&
2358becb8d5a6ab5103393eac5344ae69bcb860601ddJohn McCall           "struct/union has previous declaration");
2359becb8d5a6ab5103393eac5344ae69bcb860601ddJohn McCall    assert(!NeedsInjectedClassNameType(Record));
2360400f5125e2432d648f2c8a31b36a7f318a880c47Argyrios Kyrtzidis    return getRecordType(Record);
236119c8576b7328f4dc2d07682f5da552875c1912efJohn McCall  } else if (const EnumDecl *Enum = dyn_cast<EnumDecl>(Decl)) {
2362ef96ee0be5f100789f451641542a69cd719144d2Douglas Gregor    assert(!Enum->getPreviousDecl() &&
2363becb8d5a6ab5103393eac5344ae69bcb860601ddJohn McCall           "enum has previous declaration");
2364400f5125e2432d648f2c8a31b36a7f318a880c47Argyrios Kyrtzidis    return getEnumType(Enum);
236519c8576b7328f4dc2d07682f5da552875c1912efJohn McCall  } else if (const UnresolvedUsingTypenameDecl *Using =
2366ed97649e9574b9d854fa4d6109c9333ae0993554John McCall               dyn_cast<UnresolvedUsingTypenameDecl>(Decl)) {
2367f4c7371fb1d3cebcfb40abad4537bb82515704eaJohn McCall    Type *newType = new (*this, TypeAlignment) UnresolvedUsingType(Using);
2368f4c7371fb1d3cebcfb40abad4537bb82515704eaJohn McCall    Decl->TypeForDecl = newType;
2369f4c7371fb1d3cebcfb40abad4537bb82515704eaJohn McCall    Types.push_back(newType);
23709fdbab3cbc2fc04bcaf5768023d83707f3151144Mike Stump  } else
2371becb8d5a6ab5103393eac5344ae69bcb860601ddJohn McCall    llvm_unreachable("TypeDecl without a type?");
237249aa7ff1245abd03e6e998e01302df31e4c6f8f6Argyrios Kyrtzidis
237349aa7ff1245abd03e6e998e01302df31e4c6f8f6Argyrios Kyrtzidis  return QualType(Decl->TypeForDecl, 0);
23742ce52f3fb95bf544db6bd3d91a72bce7d9cceb6cDouglas Gregor}
23752ce52f3fb95bf544db6bd3d91a72bce7d9cceb6cDouglas Gregor
23765f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// getTypedefType - Return the unique reference to the type for the
2377162e1c1b487352434552147967c3dd296ebee2f7Richard Smith/// specified typedef name decl.
23789763e221e16026ddf487d2564ed349d2c874a1a1Argyrios KyrtzidisQualType
2379162e1c1b487352434552147967c3dd296ebee2f7Richard SmithASTContext::getTypedefType(const TypedefNameDecl *Decl,
2380162e1c1b487352434552147967c3dd296ebee2f7Richard Smith                           QualType Canonical) const {
23815f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  if (Decl->TypeForDecl) return QualType(Decl->TypeForDecl, 0);
23821eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
23839763e221e16026ddf487d2564ed349d2c874a1a1Argyrios Kyrtzidis  if (Canonical.isNull())
23849763e221e16026ddf487d2564ed349d2c874a1a1Argyrios Kyrtzidis    Canonical = getCanonicalType(Decl->getUnderlyingType());
2385f4c7371fb1d3cebcfb40abad4537bb82515704eaJohn McCall  TypedefType *newType = new(*this, TypeAlignment)
23866b304a0254a13f42390b865ff5ba668a49cc58aeJohn McCall    TypedefType(Type::Typedef, Decl, Canonical);
2387f4c7371fb1d3cebcfb40abad4537bb82515704eaJohn McCall  Decl->TypeForDecl = newType;
2388f4c7371fb1d3cebcfb40abad4537bb82515704eaJohn McCall  Types.push_back(newType);
2389f4c7371fb1d3cebcfb40abad4537bb82515704eaJohn McCall  return QualType(newType, 0);
23905f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
23915f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
23924ba2a17694148e16eaa8d3917f657ffcd3667be4Jay FoadQualType ASTContext::getRecordType(const RecordDecl *Decl) const {
2393400f5125e2432d648f2c8a31b36a7f318a880c47Argyrios Kyrtzidis  if (Decl->TypeForDecl) return QualType(Decl->TypeForDecl, 0);
2394400f5125e2432d648f2c8a31b36a7f318a880c47Argyrios Kyrtzidis
2395ef96ee0be5f100789f451641542a69cd719144d2Douglas Gregor  if (const RecordDecl *PrevDecl = Decl->getPreviousDecl())
2396400f5125e2432d648f2c8a31b36a7f318a880c47Argyrios Kyrtzidis    if (PrevDecl->TypeForDecl)
2397400f5125e2432d648f2c8a31b36a7f318a880c47Argyrios Kyrtzidis      return QualType(Decl->TypeForDecl = PrevDecl->TypeForDecl, 0);
2398400f5125e2432d648f2c8a31b36a7f318a880c47Argyrios Kyrtzidis
2399f4c7371fb1d3cebcfb40abad4537bb82515704eaJohn McCall  RecordType *newType = new (*this, TypeAlignment) RecordType(Decl);
2400f4c7371fb1d3cebcfb40abad4537bb82515704eaJohn McCall  Decl->TypeForDecl = newType;
2401f4c7371fb1d3cebcfb40abad4537bb82515704eaJohn McCall  Types.push_back(newType);
2402f4c7371fb1d3cebcfb40abad4537bb82515704eaJohn McCall  return QualType(newType, 0);
2403400f5125e2432d648f2c8a31b36a7f318a880c47Argyrios Kyrtzidis}
2404400f5125e2432d648f2c8a31b36a7f318a880c47Argyrios Kyrtzidis
24054ba2a17694148e16eaa8d3917f657ffcd3667be4Jay FoadQualType ASTContext::getEnumType(const EnumDecl *Decl) const {
2406400f5125e2432d648f2c8a31b36a7f318a880c47Argyrios Kyrtzidis  if (Decl->TypeForDecl) return QualType(Decl->TypeForDecl, 0);
2407400f5125e2432d648f2c8a31b36a7f318a880c47Argyrios Kyrtzidis
2408ef96ee0be5f100789f451641542a69cd719144d2Douglas Gregor  if (const EnumDecl *PrevDecl = Decl->getPreviousDecl())
2409400f5125e2432d648f2c8a31b36a7f318a880c47Argyrios Kyrtzidis    if (PrevDecl->TypeForDecl)
2410400f5125e2432d648f2c8a31b36a7f318a880c47Argyrios Kyrtzidis      return QualType(Decl->TypeForDecl = PrevDecl->TypeForDecl, 0);
2411400f5125e2432d648f2c8a31b36a7f318a880c47Argyrios Kyrtzidis
2412f4c7371fb1d3cebcfb40abad4537bb82515704eaJohn McCall  EnumType *newType = new (*this, TypeAlignment) EnumType(Decl);
2413f4c7371fb1d3cebcfb40abad4537bb82515704eaJohn McCall  Decl->TypeForDecl = newType;
2414f4c7371fb1d3cebcfb40abad4537bb82515704eaJohn McCall  Types.push_back(newType);
2415f4c7371fb1d3cebcfb40abad4537bb82515704eaJohn McCall  return QualType(newType, 0);
2416400f5125e2432d648f2c8a31b36a7f318a880c47Argyrios Kyrtzidis}
2417400f5125e2432d648f2c8a31b36a7f318a880c47Argyrios Kyrtzidis
24189d156a7b1b2771e191f2f5a45a7b7a694129463bJohn McCallQualType ASTContext::getAttributedType(AttributedType::Kind attrKind,
24199d156a7b1b2771e191f2f5a45a7b7a694129463bJohn McCall                                       QualType modifiedType,
24209d156a7b1b2771e191f2f5a45a7b7a694129463bJohn McCall                                       QualType equivalentType) {
24219d156a7b1b2771e191f2f5a45a7b7a694129463bJohn McCall  llvm::FoldingSetNodeID id;
24229d156a7b1b2771e191f2f5a45a7b7a694129463bJohn McCall  AttributedType::Profile(id, attrKind, modifiedType, equivalentType);
24239d156a7b1b2771e191f2f5a45a7b7a694129463bJohn McCall
24249d156a7b1b2771e191f2f5a45a7b7a694129463bJohn McCall  void *insertPos = 0;
24259d156a7b1b2771e191f2f5a45a7b7a694129463bJohn McCall  AttributedType *type = AttributedTypes.FindNodeOrInsertPos(id, insertPos);
24269d156a7b1b2771e191f2f5a45a7b7a694129463bJohn McCall  if (type) return QualType(type, 0);
24279d156a7b1b2771e191f2f5a45a7b7a694129463bJohn McCall
24289d156a7b1b2771e191f2f5a45a7b7a694129463bJohn McCall  QualType canon = getCanonicalType(equivalentType);
24299d156a7b1b2771e191f2f5a45a7b7a694129463bJohn McCall  type = new (*this, TypeAlignment)
24309d156a7b1b2771e191f2f5a45a7b7a694129463bJohn McCall           AttributedType(canon, attrKind, modifiedType, equivalentType);
24319d156a7b1b2771e191f2f5a45a7b7a694129463bJohn McCall
24329d156a7b1b2771e191f2f5a45a7b7a694129463bJohn McCall  Types.push_back(type);
24339d156a7b1b2771e191f2f5a45a7b7a694129463bJohn McCall  AttributedTypes.InsertNode(type, insertPos);
24349d156a7b1b2771e191f2f5a45a7b7a694129463bJohn McCall
24359d156a7b1b2771e191f2f5a45a7b7a694129463bJohn McCall  return QualType(type, 0);
24369d156a7b1b2771e191f2f5a45a7b7a694129463bJohn McCall}
24379d156a7b1b2771e191f2f5a45a7b7a694129463bJohn McCall
24389d156a7b1b2771e191f2f5a45a7b7a694129463bJohn McCall
243949a832bd499d6f61c23655f1fac99f0dd229756eJohn McCall/// \brief Retrieve a substitution-result type.
244049a832bd499d6f61c23655f1fac99f0dd229756eJohn McCallQualType
244149a832bd499d6f61c23655f1fac99f0dd229756eJohn McCallASTContext::getSubstTemplateTypeParmType(const TemplateTypeParmType *Parm,
24424ba2a17694148e16eaa8d3917f657ffcd3667be4Jay Foad                                         QualType Replacement) const {
2443467b27b9a24bdc823218ad1ad0e37673b6cc1e83John McCall  assert(Replacement.isCanonical()
244449a832bd499d6f61c23655f1fac99f0dd229756eJohn McCall         && "replacement types must always be canonical");
244549a832bd499d6f61c23655f1fac99f0dd229756eJohn McCall
244649a832bd499d6f61c23655f1fac99f0dd229756eJohn McCall  llvm::FoldingSetNodeID ID;
244749a832bd499d6f61c23655f1fac99f0dd229756eJohn McCall  SubstTemplateTypeParmType::Profile(ID, Parm, Replacement);
244849a832bd499d6f61c23655f1fac99f0dd229756eJohn McCall  void *InsertPos = 0;
244949a832bd499d6f61c23655f1fac99f0dd229756eJohn McCall  SubstTemplateTypeParmType *SubstParm
245049a832bd499d6f61c23655f1fac99f0dd229756eJohn McCall    = SubstTemplateTypeParmTypes.FindNodeOrInsertPos(ID, InsertPos);
245149a832bd499d6f61c23655f1fac99f0dd229756eJohn McCall
245249a832bd499d6f61c23655f1fac99f0dd229756eJohn McCall  if (!SubstParm) {
245349a832bd499d6f61c23655f1fac99f0dd229756eJohn McCall    SubstParm = new (*this, TypeAlignment)
245449a832bd499d6f61c23655f1fac99f0dd229756eJohn McCall      SubstTemplateTypeParmType(Parm, Replacement);
245549a832bd499d6f61c23655f1fac99f0dd229756eJohn McCall    Types.push_back(SubstParm);
245649a832bd499d6f61c23655f1fac99f0dd229756eJohn McCall    SubstTemplateTypeParmTypes.InsertNode(SubstParm, InsertPos);
245749a832bd499d6f61c23655f1fac99f0dd229756eJohn McCall  }
245849a832bd499d6f61c23655f1fac99f0dd229756eJohn McCall
245949a832bd499d6f61c23655f1fac99f0dd229756eJohn McCall  return QualType(SubstParm, 0);
246049a832bd499d6f61c23655f1fac99f0dd229756eJohn McCall}
246149a832bd499d6f61c23655f1fac99f0dd229756eJohn McCall
2462c3069d618f4661d923cb1b5c4525b082fce73b04Douglas Gregor/// \brief Retrieve a
2463c3069d618f4661d923cb1b5c4525b082fce73b04Douglas GregorQualType ASTContext::getSubstTemplateTypeParmPackType(
2464c3069d618f4661d923cb1b5c4525b082fce73b04Douglas Gregor                                          const TemplateTypeParmType *Parm,
2465c3069d618f4661d923cb1b5c4525b082fce73b04Douglas Gregor                                              const TemplateArgument &ArgPack) {
2466c3069d618f4661d923cb1b5c4525b082fce73b04Douglas Gregor#ifndef NDEBUG
2467c3069d618f4661d923cb1b5c4525b082fce73b04Douglas Gregor  for (TemplateArgument::pack_iterator P = ArgPack.pack_begin(),
2468c3069d618f4661d923cb1b5c4525b082fce73b04Douglas Gregor                                    PEnd = ArgPack.pack_end();
2469c3069d618f4661d923cb1b5c4525b082fce73b04Douglas Gregor       P != PEnd; ++P) {
2470c3069d618f4661d923cb1b5c4525b082fce73b04Douglas Gregor    assert(P->getKind() == TemplateArgument::Type &&"Pack contains a non-type");
2471c3069d618f4661d923cb1b5c4525b082fce73b04Douglas Gregor    assert(P->getAsType().isCanonical() && "Pack contains non-canonical type");
2472c3069d618f4661d923cb1b5c4525b082fce73b04Douglas Gregor  }
2473c3069d618f4661d923cb1b5c4525b082fce73b04Douglas Gregor#endif
2474c3069d618f4661d923cb1b5c4525b082fce73b04Douglas Gregor
2475c3069d618f4661d923cb1b5c4525b082fce73b04Douglas Gregor  llvm::FoldingSetNodeID ID;
2476c3069d618f4661d923cb1b5c4525b082fce73b04Douglas Gregor  SubstTemplateTypeParmPackType::Profile(ID, Parm, ArgPack);
2477c3069d618f4661d923cb1b5c4525b082fce73b04Douglas Gregor  void *InsertPos = 0;
2478c3069d618f4661d923cb1b5c4525b082fce73b04Douglas Gregor  if (SubstTemplateTypeParmPackType *SubstParm
2479c3069d618f4661d923cb1b5c4525b082fce73b04Douglas Gregor        = SubstTemplateTypeParmPackTypes.FindNodeOrInsertPos(ID, InsertPos))
2480c3069d618f4661d923cb1b5c4525b082fce73b04Douglas Gregor    return QualType(SubstParm, 0);
2481c3069d618f4661d923cb1b5c4525b082fce73b04Douglas Gregor
2482c3069d618f4661d923cb1b5c4525b082fce73b04Douglas Gregor  QualType Canon;
2483c3069d618f4661d923cb1b5c4525b082fce73b04Douglas Gregor  if (!Parm->isCanonicalUnqualified()) {
2484c3069d618f4661d923cb1b5c4525b082fce73b04Douglas Gregor    Canon = getCanonicalType(QualType(Parm, 0));
2485c3069d618f4661d923cb1b5c4525b082fce73b04Douglas Gregor    Canon = getSubstTemplateTypeParmPackType(cast<TemplateTypeParmType>(Canon),
2486c3069d618f4661d923cb1b5c4525b082fce73b04Douglas Gregor                                             ArgPack);
2487c3069d618f4661d923cb1b5c4525b082fce73b04Douglas Gregor    SubstTemplateTypeParmPackTypes.FindNodeOrInsertPos(ID, InsertPos);
2488c3069d618f4661d923cb1b5c4525b082fce73b04Douglas Gregor  }
2489c3069d618f4661d923cb1b5c4525b082fce73b04Douglas Gregor
2490c3069d618f4661d923cb1b5c4525b082fce73b04Douglas Gregor  SubstTemplateTypeParmPackType *SubstParm
2491c3069d618f4661d923cb1b5c4525b082fce73b04Douglas Gregor    = new (*this, TypeAlignment) SubstTemplateTypeParmPackType(Parm, Canon,
2492c3069d618f4661d923cb1b5c4525b082fce73b04Douglas Gregor                                                               ArgPack);
2493c3069d618f4661d923cb1b5c4525b082fce73b04Douglas Gregor  Types.push_back(SubstParm);
2494c3069d618f4661d923cb1b5c4525b082fce73b04Douglas Gregor  SubstTemplateTypeParmTypes.InsertNode(SubstParm, InsertPos);
2495c3069d618f4661d923cb1b5c4525b082fce73b04Douglas Gregor  return QualType(SubstParm, 0);
2496c3069d618f4661d923cb1b5c4525b082fce73b04Douglas Gregor}
2497c3069d618f4661d923cb1b5c4525b082fce73b04Douglas Gregor
2498fab9d67cebb87be968e7ae31a3b549a5279b5d51Douglas Gregor/// \brief Retrieve the template type parameter type for a template
24991eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// parameter or parameter pack with the given depth, index, and (optionally)
250076e4ce42a30cee4dc40ce7c6014874fbc4f9baa7Anders Carlsson/// name.
25011eb4433ac451dc16f4133a88af2d002ac26c58efMike StumpQualType ASTContext::getTemplateTypeParmType(unsigned Depth, unsigned Index,
250276e4ce42a30cee4dc40ce7c6014874fbc4f9baa7Anders Carlsson                                             bool ParameterPack,
25034fb86f8c4585e53c21c847ad3de9e3b2de123cd9Chandler Carruth                                             TemplateTypeParmDecl *TTPDecl) const {
2504fab9d67cebb87be968e7ae31a3b549a5279b5d51Douglas Gregor  llvm::FoldingSetNodeID ID;
25054fb86f8c4585e53c21c847ad3de9e3b2de123cd9Chandler Carruth  TemplateTypeParmType::Profile(ID, Depth, Index, ParameterPack, TTPDecl);
2506fab9d67cebb87be968e7ae31a3b549a5279b5d51Douglas Gregor  void *InsertPos = 0;
25071eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  TemplateTypeParmType *TypeParm
2508fab9d67cebb87be968e7ae31a3b549a5279b5d51Douglas Gregor    = TemplateTypeParmTypes.FindNodeOrInsertPos(ID, InsertPos);
2509fab9d67cebb87be968e7ae31a3b549a5279b5d51Douglas Gregor
2510fab9d67cebb87be968e7ae31a3b549a5279b5d51Douglas Gregor  if (TypeParm)
2511fab9d67cebb87be968e7ae31a3b549a5279b5d51Douglas Gregor    return QualType(TypeParm, 0);
25121eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
25134fb86f8c4585e53c21c847ad3de9e3b2de123cd9Chandler Carruth  if (TTPDecl) {
251476e4ce42a30cee4dc40ce7c6014874fbc4f9baa7Anders Carlsson    QualType Canon = getTemplateTypeParmType(Depth, Index, ParameterPack);
25154fb86f8c4585e53c21c847ad3de9e3b2de123cd9Chandler Carruth    TypeParm = new (*this, TypeAlignment) TemplateTypeParmType(TTPDecl, Canon);
2516789b1f640205e81b5af250693246120f1ce9d147Douglas Gregor
2517789b1f640205e81b5af250693246120f1ce9d147Douglas Gregor    TemplateTypeParmType *TypeCheck
2518789b1f640205e81b5af250693246120f1ce9d147Douglas Gregor      = TemplateTypeParmTypes.FindNodeOrInsertPos(ID, InsertPos);
2519789b1f640205e81b5af250693246120f1ce9d147Douglas Gregor    assert(!TypeCheck && "Template type parameter canonical type broken");
2520789b1f640205e81b5af250693246120f1ce9d147Douglas Gregor    (void)TypeCheck;
252176e4ce42a30cee4dc40ce7c6014874fbc4f9baa7Anders Carlsson  } else
25226b304a0254a13f42390b865ff5ba668a49cc58aeJohn McCall    TypeParm = new (*this, TypeAlignment)
25236b304a0254a13f42390b865ff5ba668a49cc58aeJohn McCall      TemplateTypeParmType(Depth, Index, ParameterPack);
2524fab9d67cebb87be968e7ae31a3b549a5279b5d51Douglas Gregor
2525fab9d67cebb87be968e7ae31a3b549a5279b5d51Douglas Gregor  Types.push_back(TypeParm);
2526fab9d67cebb87be968e7ae31a3b549a5279b5d51Douglas Gregor  TemplateTypeParmTypes.InsertNode(TypeParm, InsertPos);
2527fab9d67cebb87be968e7ae31a3b549a5279b5d51Douglas Gregor
2528fab9d67cebb87be968e7ae31a3b549a5279b5d51Douglas Gregor  return QualType(TypeParm, 0);
2529fab9d67cebb87be968e7ae31a3b549a5279b5d51Douglas Gregor}
2530fab9d67cebb87be968e7ae31a3b549a5279b5d51Douglas Gregor
25313cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCallTypeSourceInfo *
25323cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCallASTContext::getTemplateSpecializationTypeInfo(TemplateName Name,
25333cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall                                              SourceLocation NameLoc,
25343cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall                                        const TemplateArgumentListInfo &Args,
25353e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith                                              QualType Underlying) const {
25367c3179cf463c3b3b8c21dbb955f933ba50b74f28Douglas Gregor  assert(!Name.getAsDependentTemplateName() &&
25377c3179cf463c3b3b8c21dbb955f933ba50b74f28Douglas Gregor         "No dependent template names here!");
25383e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith  QualType TST = getTemplateSpecializationType(Name, Args, Underlying);
25393cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall
25403cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall  TypeSourceInfo *DI = CreateTypeSourceInfo(TST);
25413cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall  TemplateSpecializationTypeLoc TL
25423cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall    = cast<TemplateSpecializationTypeLoc>(DI->getTypeLoc());
254355d23c925b058be29b792008ddb7d68f6c4fa9a0Abramo Bagnara  TL.setTemplateKeywordLoc(SourceLocation());
25443cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall  TL.setTemplateNameLoc(NameLoc);
25453cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall  TL.setLAngleLoc(Args.getLAngleLoc());
25463cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall  TL.setRAngleLoc(Args.getRAngleLoc());
25473cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall  for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i)
25483cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall    TL.setArgLocInfo(i, Args[i].getLocInfo());
25493cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall  return DI;
25503cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall}
25513cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall
25521eb4433ac451dc16f4133a88af2d002ac26c58efMike StumpQualType
25537532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas GregorASTContext::getTemplateSpecializationType(TemplateName Template,
2554d5532b6cfff2977e0c59fa6ead7f7973984a620dJohn McCall                                          const TemplateArgumentListInfo &Args,
25553e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith                                          QualType Underlying) const {
25567c3179cf463c3b3b8c21dbb955f933ba50b74f28Douglas Gregor  assert(!Template.getAsDependentTemplateName() &&
25577c3179cf463c3b3b8c21dbb955f933ba50b74f28Douglas Gregor         "No dependent template names here!");
25587c3179cf463c3b3b8c21dbb955f933ba50b74f28Douglas Gregor
2559d5532b6cfff2977e0c59fa6ead7f7973984a620dJohn McCall  unsigned NumArgs = Args.size();
2560d5532b6cfff2977e0c59fa6ead7f7973984a620dJohn McCall
25615f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner  SmallVector<TemplateArgument, 4> ArgVec;
2562833ca991c1bfc967f0995974ca86f66ba1f666b5John McCall  ArgVec.reserve(NumArgs);
2563833ca991c1bfc967f0995974ca86f66ba1f666b5John McCall  for (unsigned i = 0; i != NumArgs; ++i)
2564833ca991c1bfc967f0995974ca86f66ba1f666b5John McCall    ArgVec.push_back(Args[i].getArgument());
2565833ca991c1bfc967f0995974ca86f66ba1f666b5John McCall
256631f17ecbef57b5679c017c375db330546b7b5145John McCall  return getTemplateSpecializationType(Template, ArgVec.data(), NumArgs,
25673e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith                                       Underlying);
2568833ca991c1bfc967f0995974ca86f66ba1f666b5John McCall}
2569833ca991c1bfc967f0995974ca86f66ba1f666b5John McCall
2570b70126a328f89937f46db42f9e3cba1592887c91Douglas Gregor#ifndef NDEBUG
2571b70126a328f89937f46db42f9e3cba1592887c91Douglas Gregorstatic bool hasAnyPackExpansions(const TemplateArgument *Args,
2572b70126a328f89937f46db42f9e3cba1592887c91Douglas Gregor                                 unsigned NumArgs) {
2573b70126a328f89937f46db42f9e3cba1592887c91Douglas Gregor  for (unsigned I = 0; I != NumArgs; ++I)
2574b70126a328f89937f46db42f9e3cba1592887c91Douglas Gregor    if (Args[I].isPackExpansion())
2575b70126a328f89937f46db42f9e3cba1592887c91Douglas Gregor      return true;
2576b70126a328f89937f46db42f9e3cba1592887c91Douglas Gregor
2577b70126a328f89937f46db42f9e3cba1592887c91Douglas Gregor  return true;
2578b70126a328f89937f46db42f9e3cba1592887c91Douglas Gregor}
2579b70126a328f89937f46db42f9e3cba1592887c91Douglas Gregor#endif
2580b70126a328f89937f46db42f9e3cba1592887c91Douglas Gregor
2581833ca991c1bfc967f0995974ca86f66ba1f666b5John McCallQualType
2582833ca991c1bfc967f0995974ca86f66ba1f666b5John McCallASTContext::getTemplateSpecializationType(TemplateName Template,
25837532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor                                          const TemplateArgument *Args,
25847532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor                                          unsigned NumArgs,
25853e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith                                          QualType Underlying) const {
25867c3179cf463c3b3b8c21dbb955f933ba50b74f28Douglas Gregor  assert(!Template.getAsDependentTemplateName() &&
25877c3179cf463c3b3b8c21dbb955f933ba50b74f28Douglas Gregor         "No dependent template names here!");
25880f0ea2a96534c615ff5fdd81363989b23cf2164aDouglas Gregor  // Look through qualified template names.
25890f0ea2a96534c615ff5fdd81363989b23cf2164aDouglas Gregor  if (QualifiedTemplateName *QTN = Template.getAsQualifiedTemplateName())
25900f0ea2a96534c615ff5fdd81363989b23cf2164aDouglas Gregor    Template = TemplateName(QTN->getTemplateDecl());
25917c3179cf463c3b3b8c21dbb955f933ba50b74f28Douglas Gregor
2592b70126a328f89937f46db42f9e3cba1592887c91Douglas Gregor  bool IsTypeAlias =
25933e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith    Template.getAsTemplateDecl() &&
25943e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith    isa<TypeAliasTemplateDecl>(Template.getAsTemplateDecl());
25953e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith  QualType CanonType;
25963e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith  if (!Underlying.isNull())
25973e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith    CanonType = getCanonicalType(Underlying);
25983e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith  else {
2599b70126a328f89937f46db42f9e3cba1592887c91Douglas Gregor    // We can get here with an alias template when the specialization contains
2600b70126a328f89937f46db42f9e3cba1592887c91Douglas Gregor    // a pack expansion that does not match up with a parameter pack.
2601b70126a328f89937f46db42f9e3cba1592887c91Douglas Gregor    assert((!IsTypeAlias || hasAnyPackExpansions(Args, NumArgs)) &&
2602b70126a328f89937f46db42f9e3cba1592887c91Douglas Gregor           "Caller must compute aliased type");
2603b70126a328f89937f46db42f9e3cba1592887c91Douglas Gregor    IsTypeAlias = false;
26043e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith    CanonType = getCanonicalTemplateSpecializationType(Template, Args,
26053e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith                                                       NumArgs);
26063e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith  }
2607fc705b84347e6fb4746a1a7e26949f64c2f2f358Douglas Gregor
26081275ae098acda31fe0e434510c729fcfed0458a1Douglas Gregor  // Allocate the (non-canonical) template specialization type, but don't
26091275ae098acda31fe0e434510c729fcfed0458a1Douglas Gregor  // try to unique it: these types typically have location information that
26101275ae098acda31fe0e434510c729fcfed0458a1Douglas Gregor  // we don't unique and don't want to lose.
26113e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith  void *Mem = Allocate(sizeof(TemplateSpecializationType) +
26123e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith                       sizeof(TemplateArgument) * NumArgs +
2613b70126a328f89937f46db42f9e3cba1592887c91Douglas Gregor                       (IsTypeAlias? sizeof(QualType) : 0),
26146b304a0254a13f42390b865ff5ba668a49cc58aeJohn McCall                       TypeAlignment);
26151eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  TemplateSpecializationType *Spec
2616b70126a328f89937f46db42f9e3cba1592887c91Douglas Gregor    = new (Mem) TemplateSpecializationType(Template, Args, NumArgs, CanonType,
2617b70126a328f89937f46db42f9e3cba1592887c91Douglas Gregor                                         IsTypeAlias ? Underlying : QualType());
26181eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
261955f6b14230c94272efbbcdd89a92224c8db9f225Douglas Gregor  Types.push_back(Spec);
26201eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  return QualType(Spec, 0);
262155f6b14230c94272efbbcdd89a92224c8db9f225Douglas Gregor}
262255f6b14230c94272efbbcdd89a92224c8db9f225Douglas Gregor
26231eb4433ac451dc16f4133a88af2d002ac26c58efMike StumpQualType
26249763e221e16026ddf487d2564ed349d2c874a1a1Argyrios KyrtzidisASTContext::getCanonicalTemplateSpecializationType(TemplateName Template,
26259763e221e16026ddf487d2564ed349d2c874a1a1Argyrios Kyrtzidis                                                   const TemplateArgument *Args,
26264ba2a17694148e16eaa8d3917f657ffcd3667be4Jay Foad                                                   unsigned NumArgs) const {
26277c3179cf463c3b3b8c21dbb955f933ba50b74f28Douglas Gregor  assert(!Template.getAsDependentTemplateName() &&
26287c3179cf463c3b3b8c21dbb955f933ba50b74f28Douglas Gregor         "No dependent template names here!");
26293e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith
26300f0ea2a96534c615ff5fdd81363989b23cf2164aDouglas Gregor  // Look through qualified template names.
26310f0ea2a96534c615ff5fdd81363989b23cf2164aDouglas Gregor  if (QualifiedTemplateName *QTN = Template.getAsQualifiedTemplateName())
26320f0ea2a96534c615ff5fdd81363989b23cf2164aDouglas Gregor    Template = TemplateName(QTN->getTemplateDecl());
26337c3179cf463c3b3b8c21dbb955f933ba50b74f28Douglas Gregor
26349763e221e16026ddf487d2564ed349d2c874a1a1Argyrios Kyrtzidis  // Build the canonical template specialization type.
26359763e221e16026ddf487d2564ed349d2c874a1a1Argyrios Kyrtzidis  TemplateName CanonTemplate = getCanonicalTemplateName(Template);
26365f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner  SmallVector<TemplateArgument, 4> CanonArgs;
26379763e221e16026ddf487d2564ed349d2c874a1a1Argyrios Kyrtzidis  CanonArgs.reserve(NumArgs);
26389763e221e16026ddf487d2564ed349d2c874a1a1Argyrios Kyrtzidis  for (unsigned I = 0; I != NumArgs; ++I)
26399763e221e16026ddf487d2564ed349d2c874a1a1Argyrios Kyrtzidis    CanonArgs.push_back(getCanonicalTemplateArgument(Args[I]));
26409763e221e16026ddf487d2564ed349d2c874a1a1Argyrios Kyrtzidis
26419763e221e16026ddf487d2564ed349d2c874a1a1Argyrios Kyrtzidis  // Determine whether this canonical template specialization type already
26429763e221e16026ddf487d2564ed349d2c874a1a1Argyrios Kyrtzidis  // exists.
26439763e221e16026ddf487d2564ed349d2c874a1a1Argyrios Kyrtzidis  llvm::FoldingSetNodeID ID;
26449763e221e16026ddf487d2564ed349d2c874a1a1Argyrios Kyrtzidis  TemplateSpecializationType::Profile(ID, CanonTemplate,
26459763e221e16026ddf487d2564ed349d2c874a1a1Argyrios Kyrtzidis                                      CanonArgs.data(), NumArgs, *this);
26469763e221e16026ddf487d2564ed349d2c874a1a1Argyrios Kyrtzidis
26479763e221e16026ddf487d2564ed349d2c874a1a1Argyrios Kyrtzidis  void *InsertPos = 0;
26489763e221e16026ddf487d2564ed349d2c874a1a1Argyrios Kyrtzidis  TemplateSpecializationType *Spec
26499763e221e16026ddf487d2564ed349d2c874a1a1Argyrios Kyrtzidis    = TemplateSpecializationTypes.FindNodeOrInsertPos(ID, InsertPos);
26509763e221e16026ddf487d2564ed349d2c874a1a1Argyrios Kyrtzidis
26519763e221e16026ddf487d2564ed349d2c874a1a1Argyrios Kyrtzidis  if (!Spec) {
26529763e221e16026ddf487d2564ed349d2c874a1a1Argyrios Kyrtzidis    // Allocate a new canonical template specialization type.
26539763e221e16026ddf487d2564ed349d2c874a1a1Argyrios Kyrtzidis    void *Mem = Allocate((sizeof(TemplateSpecializationType) +
26549763e221e16026ddf487d2564ed349d2c874a1a1Argyrios Kyrtzidis                          sizeof(TemplateArgument) * NumArgs),
26559763e221e16026ddf487d2564ed349d2c874a1a1Argyrios Kyrtzidis                         TypeAlignment);
26569763e221e16026ddf487d2564ed349d2c874a1a1Argyrios Kyrtzidis    Spec = new (Mem) TemplateSpecializationType(CanonTemplate,
26579763e221e16026ddf487d2564ed349d2c874a1a1Argyrios Kyrtzidis                                                CanonArgs.data(), NumArgs,
26583e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith                                                QualType(), QualType());
26599763e221e16026ddf487d2564ed349d2c874a1a1Argyrios Kyrtzidis    Types.push_back(Spec);
26609763e221e16026ddf487d2564ed349d2c874a1a1Argyrios Kyrtzidis    TemplateSpecializationTypes.InsertNode(Spec, InsertPos);
26619763e221e16026ddf487d2564ed349d2c874a1a1Argyrios Kyrtzidis  }
26629763e221e16026ddf487d2564ed349d2c874a1a1Argyrios Kyrtzidis
26639763e221e16026ddf487d2564ed349d2c874a1a1Argyrios Kyrtzidis  assert(Spec->isDependentType() &&
26649763e221e16026ddf487d2564ed349d2c874a1a1Argyrios Kyrtzidis         "Non-dependent template-id type must have a canonical type");
26659763e221e16026ddf487d2564ed349d2c874a1a1Argyrios Kyrtzidis  return QualType(Spec, 0);
26669763e221e16026ddf487d2564ed349d2c874a1a1Argyrios Kyrtzidis}
26679763e221e16026ddf487d2564ed349d2c874a1a1Argyrios Kyrtzidis
26689763e221e16026ddf487d2564ed349d2c874a1a1Argyrios KyrtzidisQualType
2669465d41b92b2c862f3062c412a0538db65c6a2661Abramo BagnaraASTContext::getElaboratedType(ElaboratedTypeKeyword Keyword,
2670465d41b92b2c862f3062c412a0538db65c6a2661Abramo Bagnara                              NestedNameSpecifier *NNS,
26714ba2a17694148e16eaa8d3917f657ffcd3667be4Jay Foad                              QualType NamedType) const {
2672e4e5b054b4917f0ee493bb2fda5b1ec749bfb9a1Douglas Gregor  llvm::FoldingSetNodeID ID;
2673465d41b92b2c862f3062c412a0538db65c6a2661Abramo Bagnara  ElaboratedType::Profile(ID, Keyword, NNS, NamedType);
2674e4e5b054b4917f0ee493bb2fda5b1ec749bfb9a1Douglas Gregor
2675e4e5b054b4917f0ee493bb2fda5b1ec749bfb9a1Douglas Gregor  void *InsertPos = 0;
2676465d41b92b2c862f3062c412a0538db65c6a2661Abramo Bagnara  ElaboratedType *T = ElaboratedTypes.FindNodeOrInsertPos(ID, InsertPos);
2677e4e5b054b4917f0ee493bb2fda5b1ec749bfb9a1Douglas Gregor  if (T)
2678e4e5b054b4917f0ee493bb2fda5b1ec749bfb9a1Douglas Gregor    return QualType(T, 0);
2679e4e5b054b4917f0ee493bb2fda5b1ec749bfb9a1Douglas Gregor
2680789b1f640205e81b5af250693246120f1ce9d147Douglas Gregor  QualType Canon = NamedType;
2681789b1f640205e81b5af250693246120f1ce9d147Douglas Gregor  if (!Canon.isCanonical()) {
2682789b1f640205e81b5af250693246120f1ce9d147Douglas Gregor    Canon = getCanonicalType(NamedType);
2683465d41b92b2c862f3062c412a0538db65c6a2661Abramo Bagnara    ElaboratedType *CheckT = ElaboratedTypes.FindNodeOrInsertPos(ID, InsertPos);
2684465d41b92b2c862f3062c412a0538db65c6a2661Abramo Bagnara    assert(!CheckT && "Elaborated canonical type broken");
2685789b1f640205e81b5af250693246120f1ce9d147Douglas Gregor    (void)CheckT;
2686789b1f640205e81b5af250693246120f1ce9d147Douglas Gregor  }
2687789b1f640205e81b5af250693246120f1ce9d147Douglas Gregor
2688465d41b92b2c862f3062c412a0538db65c6a2661Abramo Bagnara  T = new (*this) ElaboratedType(Keyword, NNS, NamedType, Canon);
2689e4e5b054b4917f0ee493bb2fda5b1ec749bfb9a1Douglas Gregor  Types.push_back(T);
2690465d41b92b2c862f3062c412a0538db65c6a2661Abramo Bagnara  ElaboratedTypes.InsertNode(T, InsertPos);
2691e4e5b054b4917f0ee493bb2fda5b1ec749bfb9a1Douglas Gregor  return QualType(T, 0);
2692e4e5b054b4917f0ee493bb2fda5b1ec749bfb9a1Douglas Gregor}
2693e4e5b054b4917f0ee493bb2fda5b1ec749bfb9a1Douglas Gregor
2694075f8f1b6bed4d1b224c74f87508534cc6392ce6Abramo BagnaraQualType
26954ba2a17694148e16eaa8d3917f657ffcd3667be4Jay FoadASTContext::getParenType(QualType InnerType) const {
2696075f8f1b6bed4d1b224c74f87508534cc6392ce6Abramo Bagnara  llvm::FoldingSetNodeID ID;
2697075f8f1b6bed4d1b224c74f87508534cc6392ce6Abramo Bagnara  ParenType::Profile(ID, InnerType);
2698075f8f1b6bed4d1b224c74f87508534cc6392ce6Abramo Bagnara
2699075f8f1b6bed4d1b224c74f87508534cc6392ce6Abramo Bagnara  void *InsertPos = 0;
2700075f8f1b6bed4d1b224c74f87508534cc6392ce6Abramo Bagnara  ParenType *T = ParenTypes.FindNodeOrInsertPos(ID, InsertPos);
2701075f8f1b6bed4d1b224c74f87508534cc6392ce6Abramo Bagnara  if (T)
2702075f8f1b6bed4d1b224c74f87508534cc6392ce6Abramo Bagnara    return QualType(T, 0);
2703075f8f1b6bed4d1b224c74f87508534cc6392ce6Abramo Bagnara
2704075f8f1b6bed4d1b224c74f87508534cc6392ce6Abramo Bagnara  QualType Canon = InnerType;
2705075f8f1b6bed4d1b224c74f87508534cc6392ce6Abramo Bagnara  if (!Canon.isCanonical()) {
2706075f8f1b6bed4d1b224c74f87508534cc6392ce6Abramo Bagnara    Canon = getCanonicalType(InnerType);
2707075f8f1b6bed4d1b224c74f87508534cc6392ce6Abramo Bagnara    ParenType *CheckT = ParenTypes.FindNodeOrInsertPos(ID, InsertPos);
2708075f8f1b6bed4d1b224c74f87508534cc6392ce6Abramo Bagnara    assert(!CheckT && "Paren canonical type broken");
2709075f8f1b6bed4d1b224c74f87508534cc6392ce6Abramo Bagnara    (void)CheckT;
2710075f8f1b6bed4d1b224c74f87508534cc6392ce6Abramo Bagnara  }
2711075f8f1b6bed4d1b224c74f87508534cc6392ce6Abramo Bagnara
2712075f8f1b6bed4d1b224c74f87508534cc6392ce6Abramo Bagnara  T = new (*this) ParenType(InnerType, Canon);
2713075f8f1b6bed4d1b224c74f87508534cc6392ce6Abramo Bagnara  Types.push_back(T);
2714075f8f1b6bed4d1b224c74f87508534cc6392ce6Abramo Bagnara  ParenTypes.InsertNode(T, InsertPos);
2715075f8f1b6bed4d1b224c74f87508534cc6392ce6Abramo Bagnara  return QualType(T, 0);
2716075f8f1b6bed4d1b224c74f87508534cc6392ce6Abramo Bagnara}
2717075f8f1b6bed4d1b224c74f87508534cc6392ce6Abramo Bagnara
27184a2023f5014e82389d5980d307b89c545dbbac81Douglas GregorQualType ASTContext::getDependentNameType(ElaboratedTypeKeyword Keyword,
27194a2023f5014e82389d5980d307b89c545dbbac81Douglas Gregor                                          NestedNameSpecifier *NNS,
27204a2023f5014e82389d5980d307b89c545dbbac81Douglas Gregor                                          const IdentifierInfo *Name,
27214ba2a17694148e16eaa8d3917f657ffcd3667be4Jay Foad                                          QualType Canon) const {
2722d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor  assert(NNS->isDependent() && "nested-name-specifier must be dependent");
2723d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor
2724d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor  if (Canon.isNull()) {
2725d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor    NestedNameSpecifier *CanonNNS = getCanonicalNestedNameSpecifier(NNS);
27264a2023f5014e82389d5980d307b89c545dbbac81Douglas Gregor    ElaboratedTypeKeyword CanonKeyword = Keyword;
27274a2023f5014e82389d5980d307b89c545dbbac81Douglas Gregor    if (Keyword == ETK_None)
27284a2023f5014e82389d5980d307b89c545dbbac81Douglas Gregor      CanonKeyword = ETK_Typename;
27294a2023f5014e82389d5980d307b89c545dbbac81Douglas Gregor
27304a2023f5014e82389d5980d307b89c545dbbac81Douglas Gregor    if (CanonNNS != NNS || CanonKeyword != Keyword)
27314a2023f5014e82389d5980d307b89c545dbbac81Douglas Gregor      Canon = getDependentNameType(CanonKeyword, CanonNNS, Name);
2732d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor  }
2733d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor
2734d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor  llvm::FoldingSetNodeID ID;
27354a2023f5014e82389d5980d307b89c545dbbac81Douglas Gregor  DependentNameType::Profile(ID, Keyword, NNS, Name);
2736d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor
2737d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor  void *InsertPos = 0;
27384714c12a1ab759156b78be8f109ea4c12213af57Douglas Gregor  DependentNameType *T
27394714c12a1ab759156b78be8f109ea4c12213af57Douglas Gregor    = DependentNameTypes.FindNodeOrInsertPos(ID, InsertPos);
2740d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor  if (T)
2741d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor    return QualType(T, 0);
2742d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor
27434a2023f5014e82389d5980d307b89c545dbbac81Douglas Gregor  T = new (*this) DependentNameType(Keyword, NNS, Name, Canon);
2744d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor  Types.push_back(T);
27454714c12a1ab759156b78be8f109ea4c12213af57Douglas Gregor  DependentNameTypes.InsertNode(T, InsertPos);
27461eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  return QualType(T, 0);
2747d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor}
2748d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor
27491eb4433ac451dc16f4133a88af2d002ac26c58efMike StumpQualType
275033500955d731c73717af52088b7fc0e7a85681e7John McCallASTContext::getDependentTemplateSpecializationType(
275133500955d731c73717af52088b7fc0e7a85681e7John McCall                                 ElaboratedTypeKeyword Keyword,
27524a2023f5014e82389d5980d307b89c545dbbac81Douglas Gregor                                 NestedNameSpecifier *NNS,
275333500955d731c73717af52088b7fc0e7a85681e7John McCall                                 const IdentifierInfo *Name,
27544ba2a17694148e16eaa8d3917f657ffcd3667be4Jay Foad                                 const TemplateArgumentListInfo &Args) const {
275533500955d731c73717af52088b7fc0e7a85681e7John McCall  // TODO: avoid this copy
27565f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner  SmallVector<TemplateArgument, 16> ArgCopy;
275733500955d731c73717af52088b7fc0e7a85681e7John McCall  for (unsigned I = 0, E = Args.size(); I != E; ++I)
275833500955d731c73717af52088b7fc0e7a85681e7John McCall    ArgCopy.push_back(Args[I].getArgument());
275933500955d731c73717af52088b7fc0e7a85681e7John McCall  return getDependentTemplateSpecializationType(Keyword, NNS, Name,
276033500955d731c73717af52088b7fc0e7a85681e7John McCall                                                ArgCopy.size(),
276133500955d731c73717af52088b7fc0e7a85681e7John McCall                                                ArgCopy.data());
276233500955d731c73717af52088b7fc0e7a85681e7John McCall}
276333500955d731c73717af52088b7fc0e7a85681e7John McCall
276433500955d731c73717af52088b7fc0e7a85681e7John McCallQualType
276533500955d731c73717af52088b7fc0e7a85681e7John McCallASTContext::getDependentTemplateSpecializationType(
276633500955d731c73717af52088b7fc0e7a85681e7John McCall                                 ElaboratedTypeKeyword Keyword,
276733500955d731c73717af52088b7fc0e7a85681e7John McCall                                 NestedNameSpecifier *NNS,
276833500955d731c73717af52088b7fc0e7a85681e7John McCall                                 const IdentifierInfo *Name,
276933500955d731c73717af52088b7fc0e7a85681e7John McCall                                 unsigned NumArgs,
27704ba2a17694148e16eaa8d3917f657ffcd3667be4Jay Foad                                 const TemplateArgument *Args) const {
2771aa2187de137e5b809dcbbe14f3b61ae907a3d8aaDouglas Gregor  assert((!NNS || NNS->isDependent()) &&
2772aa2187de137e5b809dcbbe14f3b61ae907a3d8aaDouglas Gregor         "nested-name-specifier must be dependent");
27731734317845d60307d474b5da8a8d33adbaf5e723Douglas Gregor
2774789b1f640205e81b5af250693246120f1ce9d147Douglas Gregor  llvm::FoldingSetNodeID ID;
277533500955d731c73717af52088b7fc0e7a85681e7John McCall  DependentTemplateSpecializationType::Profile(ID, *this, Keyword, NNS,
277633500955d731c73717af52088b7fc0e7a85681e7John McCall                                               Name, NumArgs, Args);
2777789b1f640205e81b5af250693246120f1ce9d147Douglas Gregor
2778789b1f640205e81b5af250693246120f1ce9d147Douglas Gregor  void *InsertPos = 0;
277933500955d731c73717af52088b7fc0e7a85681e7John McCall  DependentTemplateSpecializationType *T
278033500955d731c73717af52088b7fc0e7a85681e7John McCall    = DependentTemplateSpecializationTypes.FindNodeOrInsertPos(ID, InsertPos);
2781789b1f640205e81b5af250693246120f1ce9d147Douglas Gregor  if (T)
2782789b1f640205e81b5af250693246120f1ce9d147Douglas Gregor    return QualType(T, 0);
2783789b1f640205e81b5af250693246120f1ce9d147Douglas Gregor
278433500955d731c73717af52088b7fc0e7a85681e7John McCall  NestedNameSpecifier *CanonNNS = getCanonicalNestedNameSpecifier(NNS);
278533500955d731c73717af52088b7fc0e7a85681e7John McCall
278633500955d731c73717af52088b7fc0e7a85681e7John McCall  ElaboratedTypeKeyword CanonKeyword = Keyword;
278733500955d731c73717af52088b7fc0e7a85681e7John McCall  if (Keyword == ETK_None) CanonKeyword = ETK_Typename;
27881734317845d60307d474b5da8a8d33adbaf5e723Douglas Gregor
278933500955d731c73717af52088b7fc0e7a85681e7John McCall  bool AnyNonCanonArgs = false;
27905f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner  SmallVector<TemplateArgument, 16> CanonArgs(NumArgs);
279133500955d731c73717af52088b7fc0e7a85681e7John McCall  for (unsigned I = 0; I != NumArgs; ++I) {
279233500955d731c73717af52088b7fc0e7a85681e7John McCall    CanonArgs[I] = getCanonicalTemplateArgument(Args[I]);
279333500955d731c73717af52088b7fc0e7a85681e7John McCall    if (!CanonArgs[I].structurallyEquals(Args[I]))
279433500955d731c73717af52088b7fc0e7a85681e7John McCall      AnyNonCanonArgs = true;
2795789b1f640205e81b5af250693246120f1ce9d147Douglas Gregor  }
27961734317845d60307d474b5da8a8d33adbaf5e723Douglas Gregor
279733500955d731c73717af52088b7fc0e7a85681e7John McCall  QualType Canon;
279833500955d731c73717af52088b7fc0e7a85681e7John McCall  if (AnyNonCanonArgs || CanonNNS != NNS || CanonKeyword != Keyword) {
279933500955d731c73717af52088b7fc0e7a85681e7John McCall    Canon = getDependentTemplateSpecializationType(CanonKeyword, CanonNNS,
280033500955d731c73717af52088b7fc0e7a85681e7John McCall                                                   Name, NumArgs,
280133500955d731c73717af52088b7fc0e7a85681e7John McCall                                                   CanonArgs.data());
280233500955d731c73717af52088b7fc0e7a85681e7John McCall
280333500955d731c73717af52088b7fc0e7a85681e7John McCall    // Find the insert position again.
280433500955d731c73717af52088b7fc0e7a85681e7John McCall    DependentTemplateSpecializationTypes.FindNodeOrInsertPos(ID, InsertPos);
280533500955d731c73717af52088b7fc0e7a85681e7John McCall  }
280633500955d731c73717af52088b7fc0e7a85681e7John McCall
280733500955d731c73717af52088b7fc0e7a85681e7John McCall  void *Mem = Allocate((sizeof(DependentTemplateSpecializationType) +
280833500955d731c73717af52088b7fc0e7a85681e7John McCall                        sizeof(TemplateArgument) * NumArgs),
280933500955d731c73717af52088b7fc0e7a85681e7John McCall                       TypeAlignment);
2810ef99001908e799c388f1363b1e607dad5f5b57d3John McCall  T = new (Mem) DependentTemplateSpecializationType(Keyword, NNS,
281133500955d731c73717af52088b7fc0e7a85681e7John McCall                                                    Name, NumArgs, Args, Canon);
28121734317845d60307d474b5da8a8d33adbaf5e723Douglas Gregor  Types.push_back(T);
281333500955d731c73717af52088b7fc0e7a85681e7John McCall  DependentTemplateSpecializationTypes.InsertNode(T, InsertPos);
28141eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  return QualType(T, 0);
28151734317845d60307d474b5da8a8d33adbaf5e723Douglas Gregor}
28161734317845d60307d474b5da8a8d33adbaf5e723Douglas Gregor
2817cded4f649cd4b7ba7d461c25c6482ef52b8d3a2aDouglas GregorQualType ASTContext::getPackExpansionType(QualType Pattern,
2818cded4f649cd4b7ba7d461c25c6482ef52b8d3a2aDouglas Gregor                                      llvm::Optional<unsigned> NumExpansions) {
28197536dd5e6c99584481b7dab68b7e7d8df9c54054Douglas Gregor  llvm::FoldingSetNodeID ID;
2820cded4f649cd4b7ba7d461c25c6482ef52b8d3a2aDouglas Gregor  PackExpansionType::Profile(ID, Pattern, NumExpansions);
28217536dd5e6c99584481b7dab68b7e7d8df9c54054Douglas Gregor
28227536dd5e6c99584481b7dab68b7e7d8df9c54054Douglas Gregor  assert(Pattern->containsUnexpandedParameterPack() &&
28237536dd5e6c99584481b7dab68b7e7d8df9c54054Douglas Gregor         "Pack expansions must expand one or more parameter packs");
28247536dd5e6c99584481b7dab68b7e7d8df9c54054Douglas Gregor  void *InsertPos = 0;
28257536dd5e6c99584481b7dab68b7e7d8df9c54054Douglas Gregor  PackExpansionType *T
28267536dd5e6c99584481b7dab68b7e7d8df9c54054Douglas Gregor    = PackExpansionTypes.FindNodeOrInsertPos(ID, InsertPos);
28277536dd5e6c99584481b7dab68b7e7d8df9c54054Douglas Gregor  if (T)
28287536dd5e6c99584481b7dab68b7e7d8df9c54054Douglas Gregor    return QualType(T, 0);
28297536dd5e6c99584481b7dab68b7e7d8df9c54054Douglas Gregor
28307536dd5e6c99584481b7dab68b7e7d8df9c54054Douglas Gregor  QualType Canon;
28317536dd5e6c99584481b7dab68b7e7d8df9c54054Douglas Gregor  if (!Pattern.isCanonical()) {
2832cded4f649cd4b7ba7d461c25c6482ef52b8d3a2aDouglas Gregor    Canon = getPackExpansionType(getCanonicalType(Pattern), NumExpansions);
28337536dd5e6c99584481b7dab68b7e7d8df9c54054Douglas Gregor
28347536dd5e6c99584481b7dab68b7e7d8df9c54054Douglas Gregor    // Find the insert position again.
28357536dd5e6c99584481b7dab68b7e7d8df9c54054Douglas Gregor    PackExpansionTypes.FindNodeOrInsertPos(ID, InsertPos);
28367536dd5e6c99584481b7dab68b7e7d8df9c54054Douglas Gregor  }
28377536dd5e6c99584481b7dab68b7e7d8df9c54054Douglas Gregor
2838cded4f649cd4b7ba7d461c25c6482ef52b8d3a2aDouglas Gregor  T = new (*this) PackExpansionType(Pattern, Canon, NumExpansions);
28397536dd5e6c99584481b7dab68b7e7d8df9c54054Douglas Gregor  Types.push_back(T);
28407536dd5e6c99584481b7dab68b7e7d8df9c54054Douglas Gregor  PackExpansionTypes.InsertNode(T, InsertPos);
28417536dd5e6c99584481b7dab68b7e7d8df9c54054Douglas Gregor  return QualType(T, 0);
28427536dd5e6c99584481b7dab68b7e7d8df9c54054Douglas Gregor}
28437536dd5e6c99584481b7dab68b7e7d8df9c54054Douglas Gregor
284488cb27a160adc305783a44f922ee4b216006ebf9Chris Lattner/// CmpProtocolNames - Comparison predicate for sorting protocols
284588cb27a160adc305783a44f922ee4b216006ebf9Chris Lattner/// alphabetically.
284688cb27a160adc305783a44f922ee4b216006ebf9Chris Lattnerstatic bool CmpProtocolNames(const ObjCProtocolDecl *LHS,
284788cb27a160adc305783a44f922ee4b216006ebf9Chris Lattner                            const ObjCProtocolDecl *RHS) {
28482e1cd4264d363ca869bf37ef160902f211d21b8cDouglas Gregor  return LHS->getDeclName() < RHS->getDeclName();
284988cb27a160adc305783a44f922ee4b216006ebf9Chris Lattner}
285088cb27a160adc305783a44f922ee4b216006ebf9Chris Lattner
2851c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCallstatic bool areSortedAndUniqued(ObjCProtocolDecl * const *Protocols,
285254e14c4db764c0636160d26c5bbf491637c83a76John McCall                                unsigned NumProtocols) {
285354e14c4db764c0636160d26c5bbf491637c83a76John McCall  if (NumProtocols == 0) return true;
285454e14c4db764c0636160d26c5bbf491637c83a76John McCall
285561cc296de6c1f82fa84c0abb3ecd142a584838efDouglas Gregor  if (Protocols[0]->getCanonicalDecl() != Protocols[0])
285661cc296de6c1f82fa84c0abb3ecd142a584838efDouglas Gregor    return false;
285761cc296de6c1f82fa84c0abb3ecd142a584838efDouglas Gregor
285854e14c4db764c0636160d26c5bbf491637c83a76John McCall  for (unsigned i = 1; i != NumProtocols; ++i)
285961cc296de6c1f82fa84c0abb3ecd142a584838efDouglas Gregor    if (!CmpProtocolNames(Protocols[i-1], Protocols[i]) ||
286061cc296de6c1f82fa84c0abb3ecd142a584838efDouglas Gregor        Protocols[i]->getCanonicalDecl() != Protocols[i])
286154e14c4db764c0636160d26c5bbf491637c83a76John McCall      return false;
286254e14c4db764c0636160d26c5bbf491637c83a76John McCall  return true;
286354e14c4db764c0636160d26c5bbf491637c83a76John McCall}
286454e14c4db764c0636160d26c5bbf491637c83a76John McCall
286554e14c4db764c0636160d26c5bbf491637c83a76John McCallstatic void SortAndUniqueProtocols(ObjCProtocolDecl **Protocols,
286688cb27a160adc305783a44f922ee4b216006ebf9Chris Lattner                                   unsigned &NumProtocols) {
286788cb27a160adc305783a44f922ee4b216006ebf9Chris Lattner  ObjCProtocolDecl **ProtocolsEnd = Protocols+NumProtocols;
28681eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
286988cb27a160adc305783a44f922ee4b216006ebf9Chris Lattner  // Sort protocols, keyed by name.
287088cb27a160adc305783a44f922ee4b216006ebf9Chris Lattner  std::sort(Protocols, Protocols+NumProtocols, CmpProtocolNames);
287188cb27a160adc305783a44f922ee4b216006ebf9Chris Lattner
287261cc296de6c1f82fa84c0abb3ecd142a584838efDouglas Gregor  // Canonicalize.
287361cc296de6c1f82fa84c0abb3ecd142a584838efDouglas Gregor  for (unsigned I = 0, N = NumProtocols; I != N; ++I)
287461cc296de6c1f82fa84c0abb3ecd142a584838efDouglas Gregor    Protocols[I] = Protocols[I]->getCanonicalDecl();
287561cc296de6c1f82fa84c0abb3ecd142a584838efDouglas Gregor
287688cb27a160adc305783a44f922ee4b216006ebf9Chris Lattner  // Remove duplicates.
287788cb27a160adc305783a44f922ee4b216006ebf9Chris Lattner  ProtocolsEnd = std::unique(Protocols, ProtocolsEnd);
287888cb27a160adc305783a44f922ee4b216006ebf9Chris Lattner  NumProtocols = ProtocolsEnd-Protocols;
287988cb27a160adc305783a44f922ee4b216006ebf9Chris Lattner}
288088cb27a160adc305783a44f922ee4b216006ebf9Chris Lattner
2881c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCallQualType ASTContext::getObjCObjectType(QualType BaseType,
2882c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall                                       ObjCProtocolDecl * const *Protocols,
28834ba2a17694148e16eaa8d3917f657ffcd3667be4Jay Foad                                       unsigned NumProtocols) const {
2884c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall  // If the base type is an interface and there aren't any protocols
2885c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall  // to add, then the interface type will do just fine.
2886c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall  if (!NumProtocols && isa<ObjCInterfaceType>(BaseType))
2887c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall    return BaseType;
2888d1b3c2dd5bc1f3103bee6137957aa7c5f8f2f0bcSteve Naroff
2889c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall  // Look in the folding set for an existing type.
2890c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall  llvm::FoldingSetNodeID ID;
2891c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall  ObjCObjectTypeImpl::Profile(ID, BaseType, Protocols, NumProtocols);
2892d1b3c2dd5bc1f3103bee6137957aa7c5f8f2f0bcSteve Naroff  void *InsertPos = 0;
2893c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall  if (ObjCObjectType *QT = ObjCObjectTypes.FindNodeOrInsertPos(ID, InsertPos))
2894c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall    return QualType(QT, 0);
2895d1b3c2dd5bc1f3103bee6137957aa7c5f8f2f0bcSteve Naroff
2896c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall  // Build the canonical type, which has the canonical base type and
2897c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall  // a sorted-and-uniqued list of protocols.
289854e14c4db764c0636160d26c5bbf491637c83a76John McCall  QualType Canonical;
2899c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall  bool ProtocolsSorted = areSortedAndUniqued(Protocols, NumProtocols);
2900c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall  if (!ProtocolsSorted || !BaseType.isCanonical()) {
2901c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall    if (!ProtocolsSorted) {
29025f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner      SmallVector<ObjCProtocolDecl*, 8> Sorted(Protocols,
29030237941e0beb0c929934b66ad29443b484d987feBenjamin Kramer                                                     Protocols + NumProtocols);
290454e14c4db764c0636160d26c5bbf491637c83a76John McCall      unsigned UniqueCount = NumProtocols;
290554e14c4db764c0636160d26c5bbf491637c83a76John McCall
290654e14c4db764c0636160d26c5bbf491637c83a76John McCall      SortAndUniqueProtocols(&Sorted[0], UniqueCount);
2907c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall      Canonical = getObjCObjectType(getCanonicalType(BaseType),
2908c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall                                    &Sorted[0], UniqueCount);
290954e14c4db764c0636160d26c5bbf491637c83a76John McCall    } else {
2910c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall      Canonical = getObjCObjectType(getCanonicalType(BaseType),
2911c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall                                    Protocols, NumProtocols);
291254e14c4db764c0636160d26c5bbf491637c83a76John McCall    }
291354e14c4db764c0636160d26c5bbf491637c83a76John McCall
291454e14c4db764c0636160d26c5bbf491637c83a76John McCall    // Regenerate InsertPos.
2915c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall    ObjCObjectTypes.FindNodeOrInsertPos(ID, InsertPos);
291654e14c4db764c0636160d26c5bbf491637c83a76John McCall  }
291754e14c4db764c0636160d26c5bbf491637c83a76John McCall
2918c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall  unsigned Size = sizeof(ObjCObjectTypeImpl);
2919c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall  Size += NumProtocols * sizeof(ObjCProtocolDecl *);
2920fd6a0887a099256c35a5b23e9afd517ffe95fa0aDouglas Gregor  void *Mem = Allocate(Size, TypeAlignment);
2921c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall  ObjCObjectTypeImpl *T =
2922c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall    new (Mem) ObjCObjectTypeImpl(Canonical, BaseType, Protocols, NumProtocols);
29231eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
2924c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall  Types.push_back(T);
2925c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall  ObjCObjectTypes.InsertNode(T, InsertPos);
2926c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall  return QualType(T, 0);
2927d1b3c2dd5bc1f3103bee6137957aa7c5f8f2f0bcSteve Naroff}
292888cb27a160adc305783a44f922ee4b216006ebf9Chris Lattner
2929c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall/// getObjCObjectPointerType - Return a ObjCObjectPointerType type for
2930c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall/// the given object type.
29314ba2a17694148e16eaa8d3917f657ffcd3667be4Jay FoadQualType ASTContext::getObjCObjectPointerType(QualType ObjectT) const {
29324b6c9051c6522894978c9ba6a819a659d102db36Fariborz Jahanian  llvm::FoldingSetNodeID ID;
2933c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall  ObjCObjectPointerType::Profile(ID, ObjectT);
29341eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
29354b6c9051c6522894978c9ba6a819a659d102db36Fariborz Jahanian  void *InsertPos = 0;
2936c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall  if (ObjCObjectPointerType *QT =
2937c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall              ObjCObjectPointerTypes.FindNodeOrInsertPos(ID, InsertPos))
29384b6c9051c6522894978c9ba6a819a659d102db36Fariborz Jahanian    return QualType(QT, 0);
29391eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
2940c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall  // Find the canonical object type.
294154e14c4db764c0636160d26c5bbf491637c83a76John McCall  QualType Canonical;
2942c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall  if (!ObjectT.isCanonical()) {
2943c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall    Canonical = getObjCObjectPointerType(getCanonicalType(ObjectT));
294454e14c4db764c0636160d26c5bbf491637c83a76John McCall
2945c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall    // Regenerate InsertPos.
2946c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall    ObjCObjectPointerTypes.FindNodeOrInsertPos(ID, InsertPos);
294754e14c4db764c0636160d26c5bbf491637c83a76John McCall  }
294854e14c4db764c0636160d26c5bbf491637c83a76John McCall
2949c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall  // No match.
2950c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall  void *Mem = Allocate(sizeof(ObjCObjectPointerType), TypeAlignment);
2951c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall  ObjCObjectPointerType *QType =
2952c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall    new (Mem) ObjCObjectPointerType(Canonical, ObjectT);
295324fab41057e4b67ed69a6b4027d5ae0f2f6934dcArgyrios Kyrtzidis
295424fab41057e4b67ed69a6b4027d5ae0f2f6934dcArgyrios Kyrtzidis  Types.push_back(QType);
2955c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall  ObjCObjectPointerTypes.InsertNode(QType, InsertPos);
295624fab41057e4b67ed69a6b4027d5ae0f2f6934dcArgyrios Kyrtzidis  return QualType(QType, 0);
295724fab41057e4b67ed69a6b4027d5ae0f2f6934dcArgyrios Kyrtzidis}
295824fab41057e4b67ed69a6b4027d5ae0f2f6934dcArgyrios Kyrtzidis
2959deacbdca554298ccdf636f19c6094a8825ec6b34Douglas Gregor/// getObjCInterfaceType - Return the unique reference to the type for the
2960deacbdca554298ccdf636f19c6094a8825ec6b34Douglas Gregor/// specified ObjC interface decl. The list of protocols is optional.
29610af550115df1f57f17a4f125ff0e8b34820c65d1Douglas GregorQualType ASTContext::getObjCInterfaceType(const ObjCInterfaceDecl *Decl,
29620af550115df1f57f17a4f125ff0e8b34820c65d1Douglas Gregor                                          ObjCInterfaceDecl *PrevDecl) const {
2963deacbdca554298ccdf636f19c6094a8825ec6b34Douglas Gregor  if (Decl->TypeForDecl)
2964deacbdca554298ccdf636f19c6094a8825ec6b34Douglas Gregor    return QualType(Decl->TypeForDecl, 0);
296574c730ad1f6818b676b0bad46d806a9176950328Sebastian Redl
29660af550115df1f57f17a4f125ff0e8b34820c65d1Douglas Gregor  if (PrevDecl) {
29670af550115df1f57f17a4f125ff0e8b34820c65d1Douglas Gregor    assert(PrevDecl->TypeForDecl && "previous decl has no TypeForDecl");
29680af550115df1f57f17a4f125ff0e8b34820c65d1Douglas Gregor    Decl->TypeForDecl = PrevDecl->TypeForDecl;
29690af550115df1f57f17a4f125ff0e8b34820c65d1Douglas Gregor    return QualType(PrevDecl->TypeForDecl, 0);
29700af550115df1f57f17a4f125ff0e8b34820c65d1Douglas Gregor  }
29710af550115df1f57f17a4f125ff0e8b34820c65d1Douglas Gregor
29728d2dbbf9ddfd9d762a341d83f83d840ff68ce03dDouglas Gregor  // Prefer the definition, if there is one.
29738d2dbbf9ddfd9d762a341d83f83d840ff68ce03dDouglas Gregor  if (const ObjCInterfaceDecl *Def = Decl->getDefinition())
29748d2dbbf9ddfd9d762a341d83f83d840ff68ce03dDouglas Gregor    Decl = Def;
29758d2dbbf9ddfd9d762a341d83f83d840ff68ce03dDouglas Gregor
2976deacbdca554298ccdf636f19c6094a8825ec6b34Douglas Gregor  void *Mem = Allocate(sizeof(ObjCInterfaceType), TypeAlignment);
2977deacbdca554298ccdf636f19c6094a8825ec6b34Douglas Gregor  ObjCInterfaceType *T = new (Mem) ObjCInterfaceType(Decl);
2978deacbdca554298ccdf636f19c6094a8825ec6b34Douglas Gregor  Decl->TypeForDecl = T;
2979deacbdca554298ccdf636f19c6094a8825ec6b34Douglas Gregor  Types.push_back(T);
2980deacbdca554298ccdf636f19c6094a8825ec6b34Douglas Gregor  return QualType(T, 0);
2981c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall}
2982c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall
298372564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor/// getTypeOfExprType - Unlike many "get<Type>" functions, we can't unique
298472564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor/// TypeOfExprType AST's (since expression's are never shared). For example,
29859752f25748d954df99087d741ea35db37ff16beaSteve Naroff/// multiple declarations that refer to "typeof(x)" all contain different
29861eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// DeclRefExpr's. This doesn't effect the type checker, since it operates
29879752f25748d954df99087d741ea35db37ff16beaSteve Naroff/// on canonical type's (which are always unique).
29884ba2a17694148e16eaa8d3917f657ffcd3667be4Jay FoadQualType ASTContext::getTypeOfExprType(Expr *tofExpr) const {
2989dd0257c77719a13d4acd513df40b04300cbfc871Douglas Gregor  TypeOfExprType *toe;
2990b197572cf1cd70a817a1f546478cb2cb9112c48eDouglas Gregor  if (tofExpr->isTypeDependent()) {
2991b197572cf1cd70a817a1f546478cb2cb9112c48eDouglas Gregor    llvm::FoldingSetNodeID ID;
2992b197572cf1cd70a817a1f546478cb2cb9112c48eDouglas Gregor    DependentTypeOfExprType::Profile(ID, *this, tofExpr);
29931eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
2994b197572cf1cd70a817a1f546478cb2cb9112c48eDouglas Gregor    void *InsertPos = 0;
2995b197572cf1cd70a817a1f546478cb2cb9112c48eDouglas Gregor    DependentTypeOfExprType *Canon
2996b197572cf1cd70a817a1f546478cb2cb9112c48eDouglas Gregor      = DependentTypeOfExprTypes.FindNodeOrInsertPos(ID, InsertPos);
2997b197572cf1cd70a817a1f546478cb2cb9112c48eDouglas Gregor    if (Canon) {
2998b197572cf1cd70a817a1f546478cb2cb9112c48eDouglas Gregor      // We already have a "canonical" version of an identical, dependent
2999b197572cf1cd70a817a1f546478cb2cb9112c48eDouglas Gregor      // typeof(expr) type. Use that as our canonical type.
30006b304a0254a13f42390b865ff5ba668a49cc58aeJohn McCall      toe = new (*this, TypeAlignment) TypeOfExprType(tofExpr,
3001b197572cf1cd70a817a1f546478cb2cb9112c48eDouglas Gregor                                          QualType((TypeOfExprType*)Canon, 0));
30023060178ad9df29789505c1e6debcfc80a3a13587Chad Rosier    } else {
3003b197572cf1cd70a817a1f546478cb2cb9112c48eDouglas Gregor      // Build a new, canonical typeof(expr) type.
30046b304a0254a13f42390b865ff5ba668a49cc58aeJohn McCall      Canon
30056b304a0254a13f42390b865ff5ba668a49cc58aeJohn McCall        = new (*this, TypeAlignment) DependentTypeOfExprType(*this, tofExpr);
3006b197572cf1cd70a817a1f546478cb2cb9112c48eDouglas Gregor      DependentTypeOfExprTypes.InsertNode(Canon, InsertPos);
3007b197572cf1cd70a817a1f546478cb2cb9112c48eDouglas Gregor      toe = Canon;
3008b197572cf1cd70a817a1f546478cb2cb9112c48eDouglas Gregor    }
3009b197572cf1cd70a817a1f546478cb2cb9112c48eDouglas Gregor  } else {
3010dd0257c77719a13d4acd513df40b04300cbfc871Douglas Gregor    QualType Canonical = getCanonicalType(tofExpr->getType());
30116b304a0254a13f42390b865ff5ba668a49cc58aeJohn McCall    toe = new (*this, TypeAlignment) TypeOfExprType(tofExpr, Canonical);
3012dd0257c77719a13d4acd513df40b04300cbfc871Douglas Gregor  }
30139752f25748d954df99087d741ea35db37ff16beaSteve Naroff  Types.push_back(toe);
30149752f25748d954df99087d741ea35db37ff16beaSteve Naroff  return QualType(toe, 0);
3015d1861fd633d5096a00777c918eb8575ea7162fe7Steve Naroff}
3016d1861fd633d5096a00777c918eb8575ea7162fe7Steve Naroff
30179752f25748d954df99087d741ea35db37ff16beaSteve Naroff/// getTypeOfType -  Unlike many "get<Type>" functions, we don't unique
30189752f25748d954df99087d741ea35db37ff16beaSteve Naroff/// TypeOfType AST's. The only motivation to unique these nodes would be
30199752f25748d954df99087d741ea35db37ff16beaSteve Naroff/// memory savings. Since typeof(t) is fairly uncommon, space shouldn't be
30201eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// an issue. This doesn't effect the type checker, since it operates
30219752f25748d954df99087d741ea35db37ff16beaSteve Naroff/// on canonical type's (which are always unique).
30224ba2a17694148e16eaa8d3917f657ffcd3667be4Jay FoadQualType ASTContext::getTypeOfType(QualType tofType) const {
3023f52ab250ff92bc51a9ac9a8e19bd43b63a5f844fChris Lattner  QualType Canonical = getCanonicalType(tofType);
30246b304a0254a13f42390b865ff5ba668a49cc58aeJohn McCall  TypeOfType *tot = new (*this, TypeAlignment) TypeOfType(tofType, Canonical);
30259752f25748d954df99087d741ea35db37ff16beaSteve Naroff  Types.push_back(tot);
30269752f25748d954df99087d741ea35db37ff16beaSteve Naroff  return QualType(tot, 0);
3027d1861fd633d5096a00777c918eb8575ea7162fe7Steve Naroff}
3028d1861fd633d5096a00777c918eb8575ea7162fe7Steve Naroff
302960a9a2a404a4cf259d39133383e922aa00ca9043Anders Carlsson
3030395b475a4474f1c7574d927ad142ca0c7997cbcaAnders Carlsson/// getDecltypeType -  Unlike many "get<Type>" functions, we don't unique
3031395b475a4474f1c7574d927ad142ca0c7997cbcaAnders Carlsson/// DecltypeType AST's. The only motivation to unique these nodes would be
3032395b475a4474f1c7574d927ad142ca0c7997cbcaAnders Carlsson/// memory savings. Since decltype(t) is fairly uncommon, space shouldn't be
30331eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// an issue. This doesn't effect the type checker, since it operates
303439e02032b01874a0d02ba85a4cd3922adda81376David Blaikie/// on canonical types (which are always unique).
3035f8af98286022f72157d84951b48fde5fb369ab29Douglas GregorQualType ASTContext::getDecltypeType(Expr *e, QualType UnderlyingType) const {
3036dd0257c77719a13d4acd513df40b04300cbfc871Douglas Gregor  DecltypeType *dt;
3037561f81243f665cf2001caadc45df505f826b72d6Douglas Gregor
3038561f81243f665cf2001caadc45df505f826b72d6Douglas Gregor  // C++0x [temp.type]p2:
3039561f81243f665cf2001caadc45df505f826b72d6Douglas Gregor  //   If an expression e involves a template parameter, decltype(e) denotes a
3040561f81243f665cf2001caadc45df505f826b72d6Douglas Gregor  //   unique dependent type. Two such decltype-specifiers refer to the same
3041561f81243f665cf2001caadc45df505f826b72d6Douglas Gregor  //   type only if their expressions are equivalent (14.5.6.1).
3042561f81243f665cf2001caadc45df505f826b72d6Douglas Gregor  if (e->isInstantiationDependent()) {
30439d702ae1cd5cfa19d884cbef77e1df99395138bbDouglas Gregor    llvm::FoldingSetNodeID ID;
30449d702ae1cd5cfa19d884cbef77e1df99395138bbDouglas Gregor    DependentDecltypeType::Profile(ID, *this, e);
30451eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
30469d702ae1cd5cfa19d884cbef77e1df99395138bbDouglas Gregor    void *InsertPos = 0;
30479d702ae1cd5cfa19d884cbef77e1df99395138bbDouglas Gregor    DependentDecltypeType *Canon
30489d702ae1cd5cfa19d884cbef77e1df99395138bbDouglas Gregor      = DependentDecltypeTypes.FindNodeOrInsertPos(ID, InsertPos);
30499d702ae1cd5cfa19d884cbef77e1df99395138bbDouglas Gregor    if (Canon) {
30509d702ae1cd5cfa19d884cbef77e1df99395138bbDouglas Gregor      // We already have a "canonical" version of an equivalent, dependent
30519d702ae1cd5cfa19d884cbef77e1df99395138bbDouglas Gregor      // decltype type. Use that as our canonical type.
30526b304a0254a13f42390b865ff5ba668a49cc58aeJohn McCall      dt = new (*this, TypeAlignment) DecltypeType(e, DependentTy,
30539d702ae1cd5cfa19d884cbef77e1df99395138bbDouglas Gregor                                       QualType((DecltypeType*)Canon, 0));
30543060178ad9df29789505c1e6debcfc80a3a13587Chad Rosier    } else {
30559d702ae1cd5cfa19d884cbef77e1df99395138bbDouglas Gregor      // Build a new, canonical typeof(expr) type.
30566b304a0254a13f42390b865ff5ba668a49cc58aeJohn McCall      Canon = new (*this, TypeAlignment) DependentDecltypeType(*this, e);
30579d702ae1cd5cfa19d884cbef77e1df99395138bbDouglas Gregor      DependentDecltypeTypes.InsertNode(Canon, InsertPos);
30589d702ae1cd5cfa19d884cbef77e1df99395138bbDouglas Gregor      dt = Canon;
30599d702ae1cd5cfa19d884cbef77e1df99395138bbDouglas Gregor    }
30609d702ae1cd5cfa19d884cbef77e1df99395138bbDouglas Gregor  } else {
3061f8af98286022f72157d84951b48fde5fb369ab29Douglas Gregor    dt = new (*this, TypeAlignment) DecltypeType(e, UnderlyingType,
3062f8af98286022f72157d84951b48fde5fb369ab29Douglas Gregor                                      getCanonicalType(UnderlyingType));
3063dd0257c77719a13d4acd513df40b04300cbfc871Douglas Gregor  }
3064395b475a4474f1c7574d927ad142ca0c7997cbcaAnders Carlsson  Types.push_back(dt);
3065395b475a4474f1c7574d927ad142ca0c7997cbcaAnders Carlsson  return QualType(dt, 0);
3066395b475a4474f1c7574d927ad142ca0c7997cbcaAnders Carlsson}
3067395b475a4474f1c7574d927ad142ca0c7997cbcaAnders Carlsson
3068ca63c200346c0ca9e00194ec6e34a5a7b0ed9321Sean Hunt/// getUnaryTransformationType - We don't unique these, since the memory
3069ca63c200346c0ca9e00194ec6e34a5a7b0ed9321Sean Hunt/// savings are minimal and these are rare.
3070ca63c200346c0ca9e00194ec6e34a5a7b0ed9321Sean HuntQualType ASTContext::getUnaryTransformType(QualType BaseType,
3071ca63c200346c0ca9e00194ec6e34a5a7b0ed9321Sean Hunt                                           QualType UnderlyingType,
3072ca63c200346c0ca9e00194ec6e34a5a7b0ed9321Sean Hunt                                           UnaryTransformType::UTTKind Kind)
3073ca63c200346c0ca9e00194ec6e34a5a7b0ed9321Sean Hunt    const {
3074ca63c200346c0ca9e00194ec6e34a5a7b0ed9321Sean Hunt  UnaryTransformType *Ty =
307569d9775da47a4b9f165dbc33277f02982928a94eDouglas Gregor    new (*this, TypeAlignment) UnaryTransformType (BaseType, UnderlyingType,
307669d9775da47a4b9f165dbc33277f02982928a94eDouglas Gregor                                                   Kind,
307769d9775da47a4b9f165dbc33277f02982928a94eDouglas Gregor                                 UnderlyingType->isDependentType() ?
307812fc4b0624706b474fa10308631fa8daf92f340fPeter Collingbourne                                 QualType() : getCanonicalType(UnderlyingType));
3079ca63c200346c0ca9e00194ec6e34a5a7b0ed9321Sean Hunt  Types.push_back(Ty);
3080ca63c200346c0ca9e00194ec6e34a5a7b0ed9321Sean Hunt  return QualType(Ty, 0);
3081ca63c200346c0ca9e00194ec6e34a5a7b0ed9321Sean Hunt}
3082ca63c200346c0ca9e00194ec6e34a5a7b0ed9321Sean Hunt
3083483b9f3bc05c5409e2c6643f1c9d91e21c8ff9d2Richard Smith/// getAutoType - We only unique auto types after they've been deduced.
308434b41d939a1328f484511c6002ba2456db879a29Richard SmithQualType ASTContext::getAutoType(QualType DeducedType) const {
3085483b9f3bc05c5409e2c6643f1c9d91e21c8ff9d2Richard Smith  void *InsertPos = 0;
3086483b9f3bc05c5409e2c6643f1c9d91e21c8ff9d2Richard Smith  if (!DeducedType.isNull()) {
3087483b9f3bc05c5409e2c6643f1c9d91e21c8ff9d2Richard Smith    // Look in the folding set for an existing type.
3088483b9f3bc05c5409e2c6643f1c9d91e21c8ff9d2Richard Smith    llvm::FoldingSetNodeID ID;
3089483b9f3bc05c5409e2c6643f1c9d91e21c8ff9d2Richard Smith    AutoType::Profile(ID, DeducedType);
3090483b9f3bc05c5409e2c6643f1c9d91e21c8ff9d2Richard Smith    if (AutoType *AT = AutoTypes.FindNodeOrInsertPos(ID, InsertPos))
3091483b9f3bc05c5409e2c6643f1c9d91e21c8ff9d2Richard Smith      return QualType(AT, 0);
3092483b9f3bc05c5409e2c6643f1c9d91e21c8ff9d2Richard Smith  }
3093483b9f3bc05c5409e2c6643f1c9d91e21c8ff9d2Richard Smith
3094483b9f3bc05c5409e2c6643f1c9d91e21c8ff9d2Richard Smith  AutoType *AT = new (*this, TypeAlignment) AutoType(DeducedType);
3095483b9f3bc05c5409e2c6643f1c9d91e21c8ff9d2Richard Smith  Types.push_back(AT);
3096483b9f3bc05c5409e2c6643f1c9d91e21c8ff9d2Richard Smith  if (InsertPos)
3097483b9f3bc05c5409e2c6643f1c9d91e21c8ff9d2Richard Smith    AutoTypes.InsertNode(AT, InsertPos);
3098483b9f3bc05c5409e2c6643f1c9d91e21c8ff9d2Richard Smith  return QualType(AT, 0);
309934b41d939a1328f484511c6002ba2456db879a29Richard Smith}
310034b41d939a1328f484511c6002ba2456db879a29Richard Smith
3101b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman/// getAtomicType - Return the uniqued reference to the atomic type for
3102b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman/// the given value type.
3103b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli FriedmanQualType ASTContext::getAtomicType(QualType T) const {
3104b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman  // Unique pointers, to guarantee there is only one pointer of a particular
3105b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman  // structure.
3106b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman  llvm::FoldingSetNodeID ID;
3107b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman  AtomicType::Profile(ID, T);
3108b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman
3109b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman  void *InsertPos = 0;
3110b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman  if (AtomicType *AT = AtomicTypes.FindNodeOrInsertPos(ID, InsertPos))
3111b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman    return QualType(AT, 0);
3112b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman
3113b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman  // If the atomic value type isn't canonical, this won't be a canonical type
3114b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman  // either, so fill in the canonical type field.
3115b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman  QualType Canonical;
3116b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman  if (!T.isCanonical()) {
3117b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman    Canonical = getAtomicType(getCanonicalType(T));
3118b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman
3119b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman    // Get the new insert position for the node we care about.
3120b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman    AtomicType *NewIP = AtomicTypes.FindNodeOrInsertPos(ID, InsertPos);
3121b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman    assert(NewIP == 0 && "Shouldn't be in the map!"); (void)NewIP;
3122b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman  }
3123b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman  AtomicType *New = new (*this, TypeAlignment) AtomicType(T, Canonical);
3124b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman  Types.push_back(New);
3125b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman  AtomicTypes.InsertNode(New, InsertPos);
3126b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman  return QualType(New, 0);
3127b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman}
3128b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman
3129ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith/// getAutoDeductType - Get type pattern for deducing against 'auto'.
3130ad762fcdc16b9e4705b12b09d92b8c026212b906Richard SmithQualType ASTContext::getAutoDeductType() const {
3131ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith  if (AutoDeductTy.isNull())
3132ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith    AutoDeductTy = getAutoType(QualType());
3133ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith  assert(!AutoDeductTy.isNull() && "can't build 'auto' pattern");
3134ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith  return AutoDeductTy;
3135ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith}
3136ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith
3137ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith/// getAutoRRefDeductType - Get type pattern for deducing against 'auto &&'.
3138ad762fcdc16b9e4705b12b09d92b8c026212b906Richard SmithQualType ASTContext::getAutoRRefDeductType() const {
3139ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith  if (AutoRRefDeductTy.isNull())
3140ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith    AutoRRefDeductTy = getRValueReferenceType(getAutoDeductType());
3141ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith  assert(!AutoRRefDeductTy.isNull() && "can't build 'auto &&' pattern");
3142ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith  return AutoRRefDeductTy;
3143ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith}
3144ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith
31455f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// getTagDeclType - Return the unique reference to the type for the
31465f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// specified TagDecl (struct/union/class/enum) decl.
31474ba2a17694148e16eaa8d3917f657ffcd3667be4Jay FoadQualType ASTContext::getTagDeclType(const TagDecl *Decl) const {
3148d778f88d32b96a74c9edb7342c81357606a7cdc0Ted Kremenek  assert (Decl);
3149e607ed068334bacb8d7b093996b4671c6ca79e25Mike Stump  // FIXME: What is the design on getTagDeclType when it requires casting
3150e607ed068334bacb8d7b093996b4671c6ca79e25Mike Stump  // away const?  mutable?
3151e607ed068334bacb8d7b093996b4671c6ca79e25Mike Stump  return getTypeDeclType(const_cast<TagDecl*>(Decl));
31525f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
31535f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
31541eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// getSizeType - Return the unique type for "size_t" (C99 7.17), the result
31551eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// of the sizeof operator (C99 6.5.3.4p4). The value is target dependent and
31561eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// needs to agree with the definition in <stddef.h>.
3157a3ccda58913cc1a4b8564e349448b12acc462da7Anders CarlssonCanQualType ASTContext::getSizeType() const {
3158bcfd1f55bfbb3e5944cd5e03d07b343e280838c4Douglas Gregor  return getFromTargetType(Target->getSizeType());
31595f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
31605f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
316129e97cb35fab314388f62b68fefa78947e93c1dcHans Wennborg/// getIntMaxType - Return the unique type for "intmax_t" (C99 7.18.1.5).
316229e97cb35fab314388f62b68fefa78947e93c1dcHans WennborgCanQualType ASTContext::getIntMaxType() const {
316329e97cb35fab314388f62b68fefa78947e93c1dcHans Wennborg  return getFromTargetType(Target->getIntMaxType());
316429e97cb35fab314388f62b68fefa78947e93c1dcHans Wennborg}
316529e97cb35fab314388f62b68fefa78947e93c1dcHans Wennborg
316629e97cb35fab314388f62b68fefa78947e93c1dcHans Wennborg/// getUIntMaxType - Return the unique type for "uintmax_t" (C99 7.18.1.5).
316729e97cb35fab314388f62b68fefa78947e93c1dcHans WennborgCanQualType ASTContext::getUIntMaxType() const {
316829e97cb35fab314388f62b68fefa78947e93c1dcHans Wennborg  return getFromTargetType(Target->getUIntMaxType());
316929e97cb35fab314388f62b68fefa78947e93c1dcHans Wennborg}
317029e97cb35fab314388f62b68fefa78947e93c1dcHans Wennborg
317164c438a4be2a871fa43c78264663ba1e9788b94dArgyrios Kyrtzidis/// getSignedWCharType - Return the type of "signed wchar_t".
317264c438a4be2a871fa43c78264663ba1e9788b94dArgyrios Kyrtzidis/// Used when in C++, as a GCC extension.
317364c438a4be2a871fa43c78264663ba1e9788b94dArgyrios KyrtzidisQualType ASTContext::getSignedWCharType() const {
317464c438a4be2a871fa43c78264663ba1e9788b94dArgyrios Kyrtzidis  // FIXME: derive from "Target" ?
317564c438a4be2a871fa43c78264663ba1e9788b94dArgyrios Kyrtzidis  return WCharTy;
317664c438a4be2a871fa43c78264663ba1e9788b94dArgyrios Kyrtzidis}
317764c438a4be2a871fa43c78264663ba1e9788b94dArgyrios Kyrtzidis
317864c438a4be2a871fa43c78264663ba1e9788b94dArgyrios Kyrtzidis/// getUnsignedWCharType - Return the type of "unsigned wchar_t".
317964c438a4be2a871fa43c78264663ba1e9788b94dArgyrios Kyrtzidis/// Used when in C++, as a GCC extension.
318064c438a4be2a871fa43c78264663ba1e9788b94dArgyrios KyrtzidisQualType ASTContext::getUnsignedWCharType() const {
318164c438a4be2a871fa43c78264663ba1e9788b94dArgyrios Kyrtzidis  // FIXME: derive from "Target" ?
318264c438a4be2a871fa43c78264663ba1e9788b94dArgyrios Kyrtzidis  return UnsignedIntTy;
318364c438a4be2a871fa43c78264663ba1e9788b94dArgyrios Kyrtzidis}
318464c438a4be2a871fa43c78264663ba1e9788b94dArgyrios Kyrtzidis
318529e97cb35fab314388f62b68fefa78947e93c1dcHans Wennborg/// getPointerDiffType - Return the unique type for "ptrdiff_t" (C99 7.17)
31868b9023ba35a86838789e2c9034a6128728c547aaChris Lattner/// defined in <stddef.h>. Pointer - pointer requires this (C99 6.5.6p9).
31878b9023ba35a86838789e2c9034a6128728c547aaChris LattnerQualType ASTContext::getPointerDiffType() const {
3188bcfd1f55bfbb3e5944cd5e03d07b343e280838c4Douglas Gregor  return getFromTargetType(Target->getPtrDiffType(0));
31898b9023ba35a86838789e2c9034a6128728c547aaChris Lattner}
31908b9023ba35a86838789e2c9034a6128728c547aaChris Lattner
3191e6327747b72bb687c948270f702ff53c30f411a6Chris Lattner//===----------------------------------------------------------------------===//
3192e6327747b72bb687c948270f702ff53c30f411a6Chris Lattner//                              Type Operators
3193e6327747b72bb687c948270f702ff53c30f411a6Chris Lattner//===----------------------------------------------------------------------===//
3194e6327747b72bb687c948270f702ff53c30f411a6Chris Lattner
31954ba2a17694148e16eaa8d3917f657ffcd3667be4Jay FoadCanQualType ASTContext::getCanonicalParamType(QualType T) const {
319654e14c4db764c0636160d26c5bbf491637c83a76John McCall  // Push qualifiers into arrays, and then discard any remaining
319754e14c4db764c0636160d26c5bbf491637c83a76John McCall  // qualifiers.
319854e14c4db764c0636160d26c5bbf491637c83a76John McCall  T = getCanonicalType(T);
3199745da3a5bb4ea35f93f50301e7fbbb7d78d3b6bbFariborz Jahanian  T = getVariableArrayDecayedType(T);
320054e14c4db764c0636160d26c5bbf491637c83a76John McCall  const Type *Ty = T.getTypePtr();
320154e14c4db764c0636160d26c5bbf491637c83a76John McCall  QualType Result;
320254e14c4db764c0636160d26c5bbf491637c83a76John McCall  if (isa<ArrayType>(Ty)) {
320354e14c4db764c0636160d26c5bbf491637c83a76John McCall    Result = getArrayDecayedType(QualType(Ty,0));
320454e14c4db764c0636160d26c5bbf491637c83a76John McCall  } else if (isa<FunctionType>(Ty)) {
320554e14c4db764c0636160d26c5bbf491637c83a76John McCall    Result = getPointerType(QualType(Ty, 0));
320654e14c4db764c0636160d26c5bbf491637c83a76John McCall  } else {
320754e14c4db764c0636160d26c5bbf491637c83a76John McCall    Result = QualType(Ty, 0);
320854e14c4db764c0636160d26c5bbf491637c83a76John McCall  }
320954e14c4db764c0636160d26c5bbf491637c83a76John McCall
321054e14c4db764c0636160d26c5bbf491637c83a76John McCall  return CanQualType::CreateUnsafe(Result);
321154e14c4db764c0636160d26c5bbf491637c83a76John McCall}
321254e14c4db764c0636160d26c5bbf491637c83a76John McCall
321362c28c831bbf207cc36e683e7c321fc33bf8928cJohn McCallQualType ASTContext::getUnqualifiedArrayType(QualType type,
321462c28c831bbf207cc36e683e7c321fc33bf8928cJohn McCall                                             Qualifiers &quals) {
321562c28c831bbf207cc36e683e7c321fc33bf8928cJohn McCall  SplitQualType splitType = type.getSplitUnqualifiedType();
321662c28c831bbf207cc36e683e7c321fc33bf8928cJohn McCall
321762c28c831bbf207cc36e683e7c321fc33bf8928cJohn McCall  // FIXME: getSplitUnqualifiedType() actually walks all the way to
321862c28c831bbf207cc36e683e7c321fc33bf8928cJohn McCall  // the unqualified desugared type and then drops it on the floor.
321962c28c831bbf207cc36e683e7c321fc33bf8928cJohn McCall  // We then have to strip that sugar back off with
322062c28c831bbf207cc36e683e7c321fc33bf8928cJohn McCall  // getUnqualifiedDesugaredType(), which is silly.
322162c28c831bbf207cc36e683e7c321fc33bf8928cJohn McCall  const ArrayType *AT =
3222200fa53fd420aa8369586f569dbece04930ad6a3John McCall    dyn_cast<ArrayType>(splitType.Ty->getUnqualifiedDesugaredType());
322362c28c831bbf207cc36e683e7c321fc33bf8928cJohn McCall
322462c28c831bbf207cc36e683e7c321fc33bf8928cJohn McCall  // If we don't have an array, just use the results in splitType.
32259dadd94e1c17fa030d1f88d8f2113ff59ccc6714Douglas Gregor  if (!AT) {
3226200fa53fd420aa8369586f569dbece04930ad6a3John McCall    quals = splitType.Quals;
3227200fa53fd420aa8369586f569dbece04930ad6a3John McCall    return QualType(splitType.Ty, 0);
322828e318cc6008c2bc008f0caee70dc736a03d6289Chandler Carruth  }
322928e318cc6008c2bc008f0caee70dc736a03d6289Chandler Carruth
323062c28c831bbf207cc36e683e7c321fc33bf8928cJohn McCall  // Otherwise, recurse on the array's element type.
323162c28c831bbf207cc36e683e7c321fc33bf8928cJohn McCall  QualType elementType = AT->getElementType();
323262c28c831bbf207cc36e683e7c321fc33bf8928cJohn McCall  QualType unqualElementType = getUnqualifiedArrayType(elementType, quals);
323362c28c831bbf207cc36e683e7c321fc33bf8928cJohn McCall
323462c28c831bbf207cc36e683e7c321fc33bf8928cJohn McCall  // If that didn't change the element type, AT has no qualifiers, so we
323562c28c831bbf207cc36e683e7c321fc33bf8928cJohn McCall  // can just use the results in splitType.
323662c28c831bbf207cc36e683e7c321fc33bf8928cJohn McCall  if (elementType == unqualElementType) {
323762c28c831bbf207cc36e683e7c321fc33bf8928cJohn McCall    assert(quals.empty()); // from the recursive call
3238200fa53fd420aa8369586f569dbece04930ad6a3John McCall    quals = splitType.Quals;
3239200fa53fd420aa8369586f569dbece04930ad6a3John McCall    return QualType(splitType.Ty, 0);
324062c28c831bbf207cc36e683e7c321fc33bf8928cJohn McCall  }
324162c28c831bbf207cc36e683e7c321fc33bf8928cJohn McCall
324262c28c831bbf207cc36e683e7c321fc33bf8928cJohn McCall  // Otherwise, add in the qualifiers from the outermost type, then
324362c28c831bbf207cc36e683e7c321fc33bf8928cJohn McCall  // build the type back up.
3244200fa53fd420aa8369586f569dbece04930ad6a3John McCall  quals.addConsistentQualifiers(splitType.Quals);
324528e318cc6008c2bc008f0caee70dc736a03d6289Chandler Carruth
32469dadd94e1c17fa030d1f88d8f2113ff59ccc6714Douglas Gregor  if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(AT)) {
324762c28c831bbf207cc36e683e7c321fc33bf8928cJohn McCall    return getConstantArrayType(unqualElementType, CAT->getSize(),
324828e318cc6008c2bc008f0caee70dc736a03d6289Chandler Carruth                                CAT->getSizeModifier(), 0);
324928e318cc6008c2bc008f0caee70dc736a03d6289Chandler Carruth  }
325028e318cc6008c2bc008f0caee70dc736a03d6289Chandler Carruth
32519dadd94e1c17fa030d1f88d8f2113ff59ccc6714Douglas Gregor  if (const IncompleteArrayType *IAT = dyn_cast<IncompleteArrayType>(AT)) {
325262c28c831bbf207cc36e683e7c321fc33bf8928cJohn McCall    return getIncompleteArrayType(unqualElementType, IAT->getSizeModifier(), 0);
325328e318cc6008c2bc008f0caee70dc736a03d6289Chandler Carruth  }
325428e318cc6008c2bc008f0caee70dc736a03d6289Chandler Carruth
32559dadd94e1c17fa030d1f88d8f2113ff59ccc6714Douglas Gregor  if (const VariableArrayType *VAT = dyn_cast<VariableArrayType>(AT)) {
325662c28c831bbf207cc36e683e7c321fc33bf8928cJohn McCall    return getVariableArrayType(unqualElementType,
32573fa5cae9b3812cab9fab6c042c3329bb70a3d046John McCall                                VAT->getSizeExpr(),
32589dadd94e1c17fa030d1f88d8f2113ff59ccc6714Douglas Gregor                                VAT->getSizeModifier(),
32599dadd94e1c17fa030d1f88d8f2113ff59ccc6714Douglas Gregor                                VAT->getIndexTypeCVRQualifiers(),
32609dadd94e1c17fa030d1f88d8f2113ff59ccc6714Douglas Gregor                                VAT->getBracketsRange());
32619dadd94e1c17fa030d1f88d8f2113ff59ccc6714Douglas Gregor  }
32629dadd94e1c17fa030d1f88d8f2113ff59ccc6714Douglas Gregor
32639dadd94e1c17fa030d1f88d8f2113ff59ccc6714Douglas Gregor  const DependentSizedArrayType *DSAT = cast<DependentSizedArrayType>(AT);
326462c28c831bbf207cc36e683e7c321fc33bf8928cJohn McCall  return getDependentSizedArrayType(unqualElementType, DSAT->getSizeExpr(),
326528e318cc6008c2bc008f0caee70dc736a03d6289Chandler Carruth                                    DSAT->getSizeModifier(), 0,
326628e318cc6008c2bc008f0caee70dc736a03d6289Chandler Carruth                                    SourceRange());
326728e318cc6008c2bc008f0caee70dc736a03d6289Chandler Carruth}
326828e318cc6008c2bc008f0caee70dc736a03d6289Chandler Carruth
32695a57efd7bf88a4a13018e0471ded8063a4abe8afDouglas Gregor/// UnwrapSimilarPointerTypes - If T1 and T2 are pointer types  that
32705a57efd7bf88a4a13018e0471ded8063a4abe8afDouglas Gregor/// may be similar (C++ 4.4), replaces T1 and T2 with the type that
32715a57efd7bf88a4a13018e0471ded8063a4abe8afDouglas Gregor/// they point to and return true. If T1 and T2 aren't pointer types
32725a57efd7bf88a4a13018e0471ded8063a4abe8afDouglas Gregor/// or pointer-to-member types, or if they are not similar at this
32735a57efd7bf88a4a13018e0471ded8063a4abe8afDouglas Gregor/// level, returns false and leaves T1 and T2 unchanged. Top-level
32745a57efd7bf88a4a13018e0471ded8063a4abe8afDouglas Gregor/// qualifiers on T1 and T2 are ignored. This function will typically
32755a57efd7bf88a4a13018e0471ded8063a4abe8afDouglas Gregor/// be called in a loop that successively "unwraps" pointer and
32765a57efd7bf88a4a13018e0471ded8063a4abe8afDouglas Gregor/// pointer-to-member types to compare them at each level.
32775a57efd7bf88a4a13018e0471ded8063a4abe8afDouglas Gregorbool ASTContext::UnwrapSimilarPointerTypes(QualType &T1, QualType &T2) {
32785a57efd7bf88a4a13018e0471ded8063a4abe8afDouglas Gregor  const PointerType *T1PtrType = T1->getAs<PointerType>(),
32795a57efd7bf88a4a13018e0471ded8063a4abe8afDouglas Gregor                    *T2PtrType = T2->getAs<PointerType>();
32805a57efd7bf88a4a13018e0471ded8063a4abe8afDouglas Gregor  if (T1PtrType && T2PtrType) {
32815a57efd7bf88a4a13018e0471ded8063a4abe8afDouglas Gregor    T1 = T1PtrType->getPointeeType();
32825a57efd7bf88a4a13018e0471ded8063a4abe8afDouglas Gregor    T2 = T2PtrType->getPointeeType();
32835a57efd7bf88a4a13018e0471ded8063a4abe8afDouglas Gregor    return true;
32845a57efd7bf88a4a13018e0471ded8063a4abe8afDouglas Gregor  }
32855a57efd7bf88a4a13018e0471ded8063a4abe8afDouglas Gregor
32865a57efd7bf88a4a13018e0471ded8063a4abe8afDouglas Gregor  const MemberPointerType *T1MPType = T1->getAs<MemberPointerType>(),
32875a57efd7bf88a4a13018e0471ded8063a4abe8afDouglas Gregor                          *T2MPType = T2->getAs<MemberPointerType>();
32885a57efd7bf88a4a13018e0471ded8063a4abe8afDouglas Gregor  if (T1MPType && T2MPType &&
32895a57efd7bf88a4a13018e0471ded8063a4abe8afDouglas Gregor      hasSameUnqualifiedType(QualType(T1MPType->getClass(), 0),
32905a57efd7bf88a4a13018e0471ded8063a4abe8afDouglas Gregor                             QualType(T2MPType->getClass(), 0))) {
32915a57efd7bf88a4a13018e0471ded8063a4abe8afDouglas Gregor    T1 = T1MPType->getPointeeType();
32925a57efd7bf88a4a13018e0471ded8063a4abe8afDouglas Gregor    T2 = T2MPType->getPointeeType();
32935a57efd7bf88a4a13018e0471ded8063a4abe8afDouglas Gregor    return true;
32945a57efd7bf88a4a13018e0471ded8063a4abe8afDouglas Gregor  }
32955a57efd7bf88a4a13018e0471ded8063a4abe8afDouglas Gregor
32964e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie  if (getLangOpts().ObjC1) {
32975a57efd7bf88a4a13018e0471ded8063a4abe8afDouglas Gregor    const ObjCObjectPointerType *T1OPType = T1->getAs<ObjCObjectPointerType>(),
32985a57efd7bf88a4a13018e0471ded8063a4abe8afDouglas Gregor                                *T2OPType = T2->getAs<ObjCObjectPointerType>();
32995a57efd7bf88a4a13018e0471ded8063a4abe8afDouglas Gregor    if (T1OPType && T2OPType) {
33005a57efd7bf88a4a13018e0471ded8063a4abe8afDouglas Gregor      T1 = T1OPType->getPointeeType();
33015a57efd7bf88a4a13018e0471ded8063a4abe8afDouglas Gregor      T2 = T2OPType->getPointeeType();
33025a57efd7bf88a4a13018e0471ded8063a4abe8afDouglas Gregor      return true;
33035a57efd7bf88a4a13018e0471ded8063a4abe8afDouglas Gregor    }
33045a57efd7bf88a4a13018e0471ded8063a4abe8afDouglas Gregor  }
33055a57efd7bf88a4a13018e0471ded8063a4abe8afDouglas Gregor
33065a57efd7bf88a4a13018e0471ded8063a4abe8afDouglas Gregor  // FIXME: Block pointers, too?
33075a57efd7bf88a4a13018e0471ded8063a4abe8afDouglas Gregor
33085a57efd7bf88a4a13018e0471ded8063a4abe8afDouglas Gregor  return false;
33095a57efd7bf88a4a13018e0471ded8063a4abe8afDouglas Gregor}
33105a57efd7bf88a4a13018e0471ded8063a4abe8afDouglas Gregor
33114ba2a17694148e16eaa8d3917f657ffcd3667be4Jay FoadDeclarationNameInfo
33124ba2a17694148e16eaa8d3917f657ffcd3667be4Jay FoadASTContext::getNameForTemplate(TemplateName Name,
33134ba2a17694148e16eaa8d3917f657ffcd3667be4Jay Foad                               SourceLocation NameLoc) const {
3314146060435c3efce95c95a092c7a1eb651cfb9ae0John McCall  switch (Name.getKind()) {
3315146060435c3efce95c95a092c7a1eb651cfb9ae0John McCall  case TemplateName::QualifiedTemplate:
3316146060435c3efce95c95a092c7a1eb651cfb9ae0John McCall  case TemplateName::Template:
33172577743c5650c646fb705df01403707e94f2df04Abramo Bagnara    // DNInfo work in progress: CHECKME: what about DNLoc?
3318146060435c3efce95c95a092c7a1eb651cfb9ae0John McCall    return DeclarationNameInfo(Name.getAsTemplateDecl()->getDeclName(),
3319146060435c3efce95c95a092c7a1eb651cfb9ae0John McCall                               NameLoc);
33202577743c5650c646fb705df01403707e94f2df04Abramo Bagnara
3321146060435c3efce95c95a092c7a1eb651cfb9ae0John McCall  case TemplateName::OverloadedTemplate: {
3322146060435c3efce95c95a092c7a1eb651cfb9ae0John McCall    OverloadedTemplateStorage *Storage = Name.getAsOverloadedTemplate();
3323146060435c3efce95c95a092c7a1eb651cfb9ae0John McCall    // DNInfo work in progress: CHECKME: what about DNLoc?
3324146060435c3efce95c95a092c7a1eb651cfb9ae0John McCall    return DeclarationNameInfo((*Storage->begin())->getDeclName(), NameLoc);
3325146060435c3efce95c95a092c7a1eb651cfb9ae0John McCall  }
3326146060435c3efce95c95a092c7a1eb651cfb9ae0John McCall
3327146060435c3efce95c95a092c7a1eb651cfb9ae0John McCall  case TemplateName::DependentTemplate: {
3328146060435c3efce95c95a092c7a1eb651cfb9ae0John McCall    DependentTemplateName *DTN = Name.getAsDependentTemplateName();
33292577743c5650c646fb705df01403707e94f2df04Abramo Bagnara    DeclarationName DName;
333080ad16f4b2b350ddbaae21a52975e63df5aafc2cJohn McCall    if (DTN->isIdentifier()) {
33312577743c5650c646fb705df01403707e94f2df04Abramo Bagnara      DName = DeclarationNames.getIdentifier(DTN->getIdentifier());
33322577743c5650c646fb705df01403707e94f2df04Abramo Bagnara      return DeclarationNameInfo(DName, NameLoc);
333380ad16f4b2b350ddbaae21a52975e63df5aafc2cJohn McCall    } else {
33342577743c5650c646fb705df01403707e94f2df04Abramo Bagnara      DName = DeclarationNames.getCXXOperatorName(DTN->getOperator());
33352577743c5650c646fb705df01403707e94f2df04Abramo Bagnara      // DNInfo work in progress: FIXME: source locations?
33362577743c5650c646fb705df01403707e94f2df04Abramo Bagnara      DeclarationNameLoc DNLoc;
33372577743c5650c646fb705df01403707e94f2df04Abramo Bagnara      DNLoc.CXXOperatorName.BeginOpNameLoc = SourceLocation().getRawEncoding();
33382577743c5650c646fb705df01403707e94f2df04Abramo Bagnara      DNLoc.CXXOperatorName.EndOpNameLoc = SourceLocation().getRawEncoding();
33392577743c5650c646fb705df01403707e94f2df04Abramo Bagnara      return DeclarationNameInfo(DName, NameLoc, DNLoc);
334080ad16f4b2b350ddbaae21a52975e63df5aafc2cJohn McCall    }
334180ad16f4b2b350ddbaae21a52975e63df5aafc2cJohn McCall  }
334280ad16f4b2b350ddbaae21a52975e63df5aafc2cJohn McCall
3343146060435c3efce95c95a092c7a1eb651cfb9ae0John McCall  case TemplateName::SubstTemplateTemplateParm: {
3344146060435c3efce95c95a092c7a1eb651cfb9ae0John McCall    SubstTemplateTemplateParmStorage *subst
3345146060435c3efce95c95a092c7a1eb651cfb9ae0John McCall      = Name.getAsSubstTemplateTemplateParm();
3346146060435c3efce95c95a092c7a1eb651cfb9ae0John McCall    return DeclarationNameInfo(subst->getParameter()->getDeclName(),
3347146060435c3efce95c95a092c7a1eb651cfb9ae0John McCall                               NameLoc);
3348146060435c3efce95c95a092c7a1eb651cfb9ae0John McCall  }
3349146060435c3efce95c95a092c7a1eb651cfb9ae0John McCall
3350146060435c3efce95c95a092c7a1eb651cfb9ae0John McCall  case TemplateName::SubstTemplateTemplateParmPack: {
3351146060435c3efce95c95a092c7a1eb651cfb9ae0John McCall    SubstTemplateTemplateParmPackStorage *subst
3352146060435c3efce95c95a092c7a1eb651cfb9ae0John McCall      = Name.getAsSubstTemplateTemplateParmPack();
3353146060435c3efce95c95a092c7a1eb651cfb9ae0John McCall    return DeclarationNameInfo(subst->getParameterPack()->getDeclName(),
3354146060435c3efce95c95a092c7a1eb651cfb9ae0John McCall                               NameLoc);
3355146060435c3efce95c95a092c7a1eb651cfb9ae0John McCall  }
3356146060435c3efce95c95a092c7a1eb651cfb9ae0John McCall  }
3357146060435c3efce95c95a092c7a1eb651cfb9ae0John McCall
3358146060435c3efce95c95a092c7a1eb651cfb9ae0John McCall  llvm_unreachable("bad template name kind!");
335980ad16f4b2b350ddbaae21a52975e63df5aafc2cJohn McCall}
336080ad16f4b2b350ddbaae21a52975e63df5aafc2cJohn McCall
33614ba2a17694148e16eaa8d3917f657ffcd3667be4Jay FoadTemplateName ASTContext::getCanonicalTemplateName(TemplateName Name) const {
3362146060435c3efce95c95a092c7a1eb651cfb9ae0John McCall  switch (Name.getKind()) {
3363146060435c3efce95c95a092c7a1eb651cfb9ae0John McCall  case TemplateName::QualifiedTemplate:
3364146060435c3efce95c95a092c7a1eb651cfb9ae0John McCall  case TemplateName::Template: {
3365146060435c3efce95c95a092c7a1eb651cfb9ae0John McCall    TemplateDecl *Template = Name.getAsTemplateDecl();
33663e1274f2b99cb99c03cc8e2c6517c37d330b597aDouglas Gregor    if (TemplateTemplateParmDecl *TTP
3367146060435c3efce95c95a092c7a1eb651cfb9ae0John McCall          = dyn_cast<TemplateTemplateParmDecl>(Template))
33683e1274f2b99cb99c03cc8e2c6517c37d330b597aDouglas Gregor      Template = getCanonicalTemplateTemplateParmDecl(TTP);
33693e1274f2b99cb99c03cc8e2c6517c37d330b597aDouglas Gregor
33703e1274f2b99cb99c03cc8e2c6517c37d330b597aDouglas Gregor    // The canonical template name is the canonical template declaration.
337197fbaa2a38804268a024f1a104b43fcf8b4411b0Argyrios Kyrtzidis    return TemplateName(cast<TemplateDecl>(Template->getCanonicalDecl()));
33723e1274f2b99cb99c03cc8e2c6517c37d330b597aDouglas Gregor  }
337325a3ef7cc5fd55dc8cc67c6e6770c8595657e082Douglas Gregor
3374146060435c3efce95c95a092c7a1eb651cfb9ae0John McCall  case TemplateName::OverloadedTemplate:
3375146060435c3efce95c95a092c7a1eb651cfb9ae0John McCall    llvm_unreachable("cannot canonicalize overloaded template");
3376146060435c3efce95c95a092c7a1eb651cfb9ae0John McCall
3377146060435c3efce95c95a092c7a1eb651cfb9ae0John McCall  case TemplateName::DependentTemplate: {
3378146060435c3efce95c95a092c7a1eb651cfb9ae0John McCall    DependentTemplateName *DTN = Name.getAsDependentTemplateName();
3379146060435c3efce95c95a092c7a1eb651cfb9ae0John McCall    assert(DTN && "Non-dependent template names must refer to template decls.");
3380146060435c3efce95c95a092c7a1eb651cfb9ae0John McCall    return DTN->CanonicalTemplateName;
3381146060435c3efce95c95a092c7a1eb651cfb9ae0John McCall  }
3382146060435c3efce95c95a092c7a1eb651cfb9ae0John McCall
3383146060435c3efce95c95a092c7a1eb651cfb9ae0John McCall  case TemplateName::SubstTemplateTemplateParm: {
3384146060435c3efce95c95a092c7a1eb651cfb9ae0John McCall    SubstTemplateTemplateParmStorage *subst
3385146060435c3efce95c95a092c7a1eb651cfb9ae0John McCall      = Name.getAsSubstTemplateTemplateParm();
3386146060435c3efce95c95a092c7a1eb651cfb9ae0John McCall    return getCanonicalTemplateName(subst->getReplacement());
33871aee05d08b2184acadeb36de300e216390780d6cDouglas Gregor  }
33881eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
3389146060435c3efce95c95a092c7a1eb651cfb9ae0John McCall  case TemplateName::SubstTemplateTemplateParmPack: {
3390146060435c3efce95c95a092c7a1eb651cfb9ae0John McCall    SubstTemplateTemplateParmPackStorage *subst
3391146060435c3efce95c95a092c7a1eb651cfb9ae0John McCall                                  = Name.getAsSubstTemplateTemplateParmPack();
3392146060435c3efce95c95a092c7a1eb651cfb9ae0John McCall    TemplateTemplateParmDecl *canonParameter
3393146060435c3efce95c95a092c7a1eb651cfb9ae0John McCall      = getCanonicalTemplateTemplateParmDecl(subst->getParameterPack());
3394146060435c3efce95c95a092c7a1eb651cfb9ae0John McCall    TemplateArgument canonArgPack
3395146060435c3efce95c95a092c7a1eb651cfb9ae0John McCall      = getCanonicalTemplateArgument(subst->getArgumentPack());
3396146060435c3efce95c95a092c7a1eb651cfb9ae0John McCall    return getSubstTemplateTemplateParmPack(canonParameter, canonArgPack);
3397146060435c3efce95c95a092c7a1eb651cfb9ae0John McCall  }
3398146060435c3efce95c95a092c7a1eb651cfb9ae0John McCall  }
3399146060435c3efce95c95a092c7a1eb651cfb9ae0John McCall
3400146060435c3efce95c95a092c7a1eb651cfb9ae0John McCall  llvm_unreachable("bad template name!");
340125a3ef7cc5fd55dc8cc67c6e6770c8595657e082Douglas Gregor}
340225a3ef7cc5fd55dc8cc67c6e6770c8595657e082Douglas Gregor
3403db0d4b751e83b8841b8f48f913f17e50467f13d4Douglas Gregorbool ASTContext::hasSameTemplateName(TemplateName X, TemplateName Y) {
3404db0d4b751e83b8841b8f48f913f17e50467f13d4Douglas Gregor  X = getCanonicalTemplateName(X);
3405db0d4b751e83b8841b8f48f913f17e50467f13d4Douglas Gregor  Y = getCanonicalTemplateName(Y);
3406db0d4b751e83b8841b8f48f913f17e50467f13d4Douglas Gregor  return X.getAsVoidPointer() == Y.getAsVoidPointer();
3407db0d4b751e83b8841b8f48f913f17e50467f13d4Douglas Gregor}
3408db0d4b751e83b8841b8f48f913f17e50467f13d4Douglas Gregor
34091eb4433ac451dc16f4133a88af2d002ac26c58efMike StumpTemplateArgument
34104ba2a17694148e16eaa8d3917f657ffcd3667be4Jay FoadASTContext::getCanonicalTemplateArgument(const TemplateArgument &Arg) const {
34111275ae098acda31fe0e434510c729fcfed0458a1Douglas Gregor  switch (Arg.getKind()) {
34121275ae098acda31fe0e434510c729fcfed0458a1Douglas Gregor    case TemplateArgument::Null:
34131275ae098acda31fe0e434510c729fcfed0458a1Douglas Gregor      return Arg;
34141eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
34151275ae098acda31fe0e434510c729fcfed0458a1Douglas Gregor    case TemplateArgument::Expression:
34161275ae098acda31fe0e434510c729fcfed0458a1Douglas Gregor      return Arg;
34171eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
3418d2008e2c80d6c9282044ec873a937a17a0f33579Douglas Gregor    case TemplateArgument::Declaration: {
3419d2008e2c80d6c9282044ec873a937a17a0f33579Douglas Gregor      if (Decl *D = Arg.getAsDecl())
3420d2008e2c80d6c9282044ec873a937a17a0f33579Douglas Gregor          return TemplateArgument(D->getCanonicalDecl());
3421d2008e2c80d6c9282044ec873a937a17a0f33579Douglas Gregor      return TemplateArgument((Decl*)0);
3422d2008e2c80d6c9282044ec873a937a17a0f33579Douglas Gregor    }
34231eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
3424788cd06cf8e868a67158aafec5de3a1f408d14f3Douglas Gregor    case TemplateArgument::Template:
3425788cd06cf8e868a67158aafec5de3a1f408d14f3Douglas Gregor      return TemplateArgument(getCanonicalTemplateName(Arg.getAsTemplate()));
3426a7fc901a2e39bfe55bfcff5934b2d9fdf9656491Douglas Gregor
3427a7fc901a2e39bfe55bfcff5934b2d9fdf9656491Douglas Gregor    case TemplateArgument::TemplateExpansion:
3428a7fc901a2e39bfe55bfcff5934b2d9fdf9656491Douglas Gregor      return TemplateArgument(getCanonicalTemplateName(
3429a7fc901a2e39bfe55bfcff5934b2d9fdf9656491Douglas Gregor                                         Arg.getAsTemplateOrTemplatePattern()),
34302be29f423acad3bbe39099a78db2805acb5bdf17Douglas Gregor                              Arg.getNumTemplateExpansions());
3431a7fc901a2e39bfe55bfcff5934b2d9fdf9656491Douglas Gregor
34321275ae098acda31fe0e434510c729fcfed0458a1Douglas Gregor    case TemplateArgument::Integral:
3433855243789cb44799c03f4c7216d3d6308805f549Benjamin Kramer      return TemplateArgument(Arg, getCanonicalType(Arg.getIntegralType()));
34341eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
34351275ae098acda31fe0e434510c729fcfed0458a1Douglas Gregor    case TemplateArgument::Type:
3436833ca991c1bfc967f0995974ca86f66ba1f666b5John McCall      return TemplateArgument(getCanonicalType(Arg.getAsType()));
34371eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
34381275ae098acda31fe0e434510c729fcfed0458a1Douglas Gregor    case TemplateArgument::Pack: {
343987dd697dcc8ecb64df73ae64d61b8c80ff0c157cDouglas Gregor      if (Arg.pack_size() == 0)
344087dd697dcc8ecb64df73ae64d61b8c80ff0c157cDouglas Gregor        return Arg;
344187dd697dcc8ecb64df73ae64d61b8c80ff0c157cDouglas Gregor
3442910f8008fea79120489a53593fe971b0b8a4a740Douglas Gregor      TemplateArgument *CanonArgs
3443910f8008fea79120489a53593fe971b0b8a4a740Douglas Gregor        = new (*this) TemplateArgument[Arg.pack_size()];
34441275ae098acda31fe0e434510c729fcfed0458a1Douglas Gregor      unsigned Idx = 0;
34451eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      for (TemplateArgument::pack_iterator A = Arg.pack_begin(),
34461275ae098acda31fe0e434510c729fcfed0458a1Douglas Gregor                                        AEnd = Arg.pack_end();
34471275ae098acda31fe0e434510c729fcfed0458a1Douglas Gregor           A != AEnd; (void)++A, ++Idx)
34481275ae098acda31fe0e434510c729fcfed0458a1Douglas Gregor        CanonArgs[Idx] = getCanonicalTemplateArgument(*A);
34491eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
3450910f8008fea79120489a53593fe971b0b8a4a740Douglas Gregor      return TemplateArgument(CanonArgs, Arg.pack_size());
34511275ae098acda31fe0e434510c729fcfed0458a1Douglas Gregor    }
34521275ae098acda31fe0e434510c729fcfed0458a1Douglas Gregor  }
34531275ae098acda31fe0e434510c729fcfed0458a1Douglas Gregor
34541275ae098acda31fe0e434510c729fcfed0458a1Douglas Gregor  // Silence GCC warning
3455b219cfc4d75f0a03630b7c4509ef791b7e97b2c8David Blaikie  llvm_unreachable("Unhandled template argument kind");
34561275ae098acda31fe0e434510c729fcfed0458a1Douglas Gregor}
34571275ae098acda31fe0e434510c729fcfed0458a1Douglas Gregor
3458d57959af02b4af695276f4204443afe6e5d86bd8Douglas GregorNestedNameSpecifier *
34594ba2a17694148e16eaa8d3917f657ffcd3667be4Jay FoadASTContext::getCanonicalNestedNameSpecifier(NestedNameSpecifier *NNS) const {
34601eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  if (!NNS)
3461d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor    return 0;
3462d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor
3463d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor  switch (NNS->getKind()) {
3464d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor  case NestedNameSpecifier::Identifier:
3465d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor    // Canonicalize the prefix but keep the identifier the same.
34661eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    return NestedNameSpecifier::Create(*this,
3467d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor                         getCanonicalNestedNameSpecifier(NNS->getPrefix()),
3468d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor                                       NNS->getAsIdentifier());
3469d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor
3470d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor  case NestedNameSpecifier::Namespace:
3471d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor    // A namespace is canonical; build a nested-name-specifier with
3472d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor    // this namespace and no prefix.
347314aba76042e041b2c5e439bf4ae353a0a3c7fd73Douglas Gregor    return NestedNameSpecifier::Create(*this, 0,
347414aba76042e041b2c5e439bf4ae353a0a3c7fd73Douglas Gregor                                 NNS->getAsNamespace()->getOriginalNamespace());
347514aba76042e041b2c5e439bf4ae353a0a3c7fd73Douglas Gregor
347614aba76042e041b2c5e439bf4ae353a0a3c7fd73Douglas Gregor  case NestedNameSpecifier::NamespaceAlias:
347714aba76042e041b2c5e439bf4ae353a0a3c7fd73Douglas Gregor    // A namespace is canonical; build a nested-name-specifier with
347814aba76042e041b2c5e439bf4ae353a0a3c7fd73Douglas Gregor    // this namespace and no prefix.
347914aba76042e041b2c5e439bf4ae353a0a3c7fd73Douglas Gregor    return NestedNameSpecifier::Create(*this, 0,
348014aba76042e041b2c5e439bf4ae353a0a3c7fd73Douglas Gregor                                    NNS->getAsNamespaceAlias()->getNamespace()
348114aba76042e041b2c5e439bf4ae353a0a3c7fd73Douglas Gregor                                                      ->getOriginalNamespace());
3482d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor
3483d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor  case NestedNameSpecifier::TypeSpec:
3484d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor  case NestedNameSpecifier::TypeSpecWithTemplate: {
3485d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor    QualType T = getCanonicalType(QualType(NNS->getAsType(), 0));
3486264bf66d55563dd86a3d7e06738aa427de512d2cDouglas Gregor
3487264bf66d55563dd86a3d7e06738aa427de512d2cDouglas Gregor    // If we have some kind of dependent-named type (e.g., "typename T::type"),
3488264bf66d55563dd86a3d7e06738aa427de512d2cDouglas Gregor    // break it apart into its prefix and identifier, then reconsititute those
3489264bf66d55563dd86a3d7e06738aa427de512d2cDouglas Gregor    // as the canonical nested-name-specifier. This is required to canonicalize
3490264bf66d55563dd86a3d7e06738aa427de512d2cDouglas Gregor    // a dependent nested-name-specifier involving typedefs of dependent-name
3491264bf66d55563dd86a3d7e06738aa427de512d2cDouglas Gregor    // types, e.g.,
3492264bf66d55563dd86a3d7e06738aa427de512d2cDouglas Gregor    //   typedef typename T::type T1;
3493264bf66d55563dd86a3d7e06738aa427de512d2cDouglas Gregor    //   typedef typename T1::type T2;
349416412ef25a2203b7066d0db2b41f944631e5cf79Eli Friedman    if (const DependentNameType *DNT = T->getAs<DependentNameType>())
349516412ef25a2203b7066d0db2b41f944631e5cf79Eli Friedman      return NestedNameSpecifier::Create(*this, DNT->getQualifier(),
3496264bf66d55563dd86a3d7e06738aa427de512d2cDouglas Gregor                           const_cast<IdentifierInfo *>(DNT->getIdentifier()));
3497264bf66d55563dd86a3d7e06738aa427de512d2cDouglas Gregor
349816412ef25a2203b7066d0db2b41f944631e5cf79Eli Friedman    // Otherwise, just canonicalize the type, and force it to be a TypeSpec.
349916412ef25a2203b7066d0db2b41f944631e5cf79Eli Friedman    // FIXME: Why are TypeSpec and TypeSpecWithTemplate distinct in the
350016412ef25a2203b7066d0db2b41f944631e5cf79Eli Friedman    // first place?
35013b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall    return NestedNameSpecifier::Create(*this, 0, false,
35023b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall                                       const_cast<Type*>(T.getTypePtr()));
3503d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor  }
3504d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor
3505d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor  case NestedNameSpecifier::Global:
3506d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor    // The global specifier is canonical and unique.
3507d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor    return NNS;
3508d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor  }
3509d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor
35107530c034c0c71a64c5a9173206d9742ae847af8bDavid Blaikie  llvm_unreachable("Invalid NestedNameSpecifier::Kind!");
3511d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor}
3512d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor
3513c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner
35144ba2a17694148e16eaa8d3917f657ffcd3667be4Jay Foadconst ArrayType *ASTContext::getAsArrayType(QualType T) const {
3515c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner  // Handle the non-qualified case efficiently.
3516a4923eb7c4b04d360cb2747641a5e92818edf804Douglas Gregor  if (!T.hasLocalQualifiers()) {
3517c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner    // Handle the common positive case fast.
3518c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner    if (const ArrayType *AT = dyn_cast<ArrayType>(T))
3519c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner      return AT;
3520c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner  }
35211eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
35220953e767ff7817f97b3ab20896b229891eeff45bJohn McCall  // Handle the common negative case fast.
35233b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall  if (!isa<ArrayType>(T.getCanonicalType()))
3524c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner    return 0;
35251eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
35260953e767ff7817f97b3ab20896b229891eeff45bJohn McCall  // Apply any qualifiers from the array type to the element type.  This
3527c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner  // implements C99 6.7.3p8: "If the specification of an array type includes
3528c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner  // any type qualifiers, the element type is so qualified, not the array type."
35291eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
3530c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner  // If we get here, we either have type qualifiers on the type, or we have
3531c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner  // sugar such as a typedef in the way.  If we have type qualifiers on the type
353250d62d1b4a98adbc83de8f8cd1379ea1c25656f7Douglas Gregor  // we must propagate them down into the element type.
35330953e767ff7817f97b3ab20896b229891eeff45bJohn McCall
35343b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall  SplitQualType split = T.getSplitDesugaredType();
3535200fa53fd420aa8369586f569dbece04930ad6a3John McCall  Qualifiers qs = split.Quals;
35361eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
3537c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner  // If we have a simple case, just return now.
3538200fa53fd420aa8369586f569dbece04930ad6a3John McCall  const ArrayType *ATy = dyn_cast<ArrayType>(split.Ty);
35393b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall  if (ATy == 0 || qs.empty())
3540c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner    return ATy;
35411eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
3542c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner  // Otherwise, we have an array and we have qualifiers on it.  Push the
3543c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner  // qualifiers into the array element type and return a new array type.
35443b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall  QualType NewEltTy = getQualifiedType(ATy->getElementType(), qs);
35451eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
3546c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner  if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(ATy))
3547c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner    return cast<ArrayType>(getConstantArrayType(NewEltTy, CAT->getSize(),
3548c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner                                                CAT->getSizeModifier(),
35490953e767ff7817f97b3ab20896b229891eeff45bJohn McCall                                           CAT->getIndexTypeCVRQualifiers()));
3550c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner  if (const IncompleteArrayType *IAT = dyn_cast<IncompleteArrayType>(ATy))
3551c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner    return cast<ArrayType>(getIncompleteArrayType(NewEltTy,
3552c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner                                                  IAT->getSizeModifier(),
35530953e767ff7817f97b3ab20896b229891eeff45bJohn McCall                                           IAT->getIndexTypeCVRQualifiers()));
3554898574e7496ba8fd76290079d3a9d06954992734Douglas Gregor
35551eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  if (const DependentSizedArrayType *DSAT
3556898574e7496ba8fd76290079d3a9d06954992734Douglas Gregor        = dyn_cast<DependentSizedArrayType>(ATy))
3557898574e7496ba8fd76290079d3a9d06954992734Douglas Gregor    return cast<ArrayType>(
35581eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump                     getDependentSizedArrayType(NewEltTy,
35593fa5cae9b3812cab9fab6c042c3329bb70a3d046John McCall                                                DSAT->getSizeExpr(),
3560898574e7496ba8fd76290079d3a9d06954992734Douglas Gregor                                                DSAT->getSizeModifier(),
35610953e767ff7817f97b3ab20896b229891eeff45bJohn McCall                                              DSAT->getIndexTypeCVRQualifiers(),
35627e7eb3da052a6d80ddf2377cab0384c798f73f75Douglas Gregor                                                DSAT->getBracketsRange()));
35631eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
3564c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner  const VariableArrayType *VAT = cast<VariableArrayType>(ATy);
35657e7eb3da052a6d80ddf2377cab0384c798f73f75Douglas Gregor  return cast<ArrayType>(getVariableArrayType(NewEltTy,
35663fa5cae9b3812cab9fab6c042c3329bb70a3d046John McCall                                              VAT->getSizeExpr(),
3567c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner                                              VAT->getSizeModifier(),
35680953e767ff7817f97b3ab20896b229891eeff45bJohn McCall                                              VAT->getIndexTypeCVRQualifiers(),
35697e7eb3da052a6d80ddf2377cab0384c798f73f75Douglas Gregor                                              VAT->getBracketsRange()));
357077c9647cae939104c6cb2b6a4dd8ca859d2e5770Chris Lattner}
357177c9647cae939104c6cb2b6a4dd8ca859d2e5770Chris Lattner
3572ad9689f3531c49e4bff467d9469993606800068cAbramo BagnaraQualType ASTContext::getAdjustedParameterType(QualType T) const {
357379e6bd379773447a74cc3e579d9081e4c5cb6d63Douglas Gregor  // C99 6.7.5.3p7:
357479e6bd379773447a74cc3e579d9081e4c5cb6d63Douglas Gregor  //   A declaration of a parameter as "array of type" shall be
357579e6bd379773447a74cc3e579d9081e4c5cb6d63Douglas Gregor  //   adjusted to "qualified pointer to type", where the type
357679e6bd379773447a74cc3e579d9081e4c5cb6d63Douglas Gregor  //   qualifiers (if any) are those specified within the [ and ] of
357779e6bd379773447a74cc3e579d9081e4c5cb6d63Douglas Gregor  //   the array type derivation.
357879e6bd379773447a74cc3e579d9081e4c5cb6d63Douglas Gregor  if (T->isArrayType())
357979e6bd379773447a74cc3e579d9081e4c5cb6d63Douglas Gregor    return getArrayDecayedType(T);
358079e6bd379773447a74cc3e579d9081e4c5cb6d63Douglas Gregor
358179e6bd379773447a74cc3e579d9081e4c5cb6d63Douglas Gregor  // C99 6.7.5.3p8:
358279e6bd379773447a74cc3e579d9081e4c5cb6d63Douglas Gregor  //   A declaration of a parameter as "function returning type"
358379e6bd379773447a74cc3e579d9081e4c5cb6d63Douglas Gregor  //   shall be adjusted to "pointer to function returning type", as
358479e6bd379773447a74cc3e579d9081e4c5cb6d63Douglas Gregor  //   in 6.3.2.1.
358579e6bd379773447a74cc3e579d9081e4c5cb6d63Douglas Gregor  if (T->isFunctionType())
358679e6bd379773447a74cc3e579d9081e4c5cb6d63Douglas Gregor    return getPointerType(T);
358779e6bd379773447a74cc3e579d9081e4c5cb6d63Douglas Gregor
358879e6bd379773447a74cc3e579d9081e4c5cb6d63Douglas Gregor  return T;
358979e6bd379773447a74cc3e579d9081e4c5cb6d63Douglas Gregor}
359079e6bd379773447a74cc3e579d9081e4c5cb6d63Douglas Gregor
3591ad9689f3531c49e4bff467d9469993606800068cAbramo BagnaraQualType ASTContext::getSignatureParameterType(QualType T) const {
359279e6bd379773447a74cc3e579d9081e4c5cb6d63Douglas Gregor  T = getVariableArrayDecayedType(T);
359379e6bd379773447a74cc3e579d9081e4c5cb6d63Douglas Gregor  T = getAdjustedParameterType(T);
359479e6bd379773447a74cc3e579d9081e4c5cb6d63Douglas Gregor  return T.getUnqualifiedType();
359579e6bd379773447a74cc3e579d9081e4c5cb6d63Douglas Gregor}
359679e6bd379773447a74cc3e579d9081e4c5cb6d63Douglas Gregor
3597e6327747b72bb687c948270f702ff53c30f411a6Chris Lattner/// getArrayDecayedType - Return the properly qualified result of decaying the
3598e6327747b72bb687c948270f702ff53c30f411a6Chris Lattner/// specified array type to a pointer.  This operation is non-trivial when
3599e6327747b72bb687c948270f702ff53c30f411a6Chris Lattner/// handling typedefs etc.  The canonical type of "T" must be an array type,
3600e6327747b72bb687c948270f702ff53c30f411a6Chris Lattner/// this returns a pointer to a properly qualified element of the array.
3601e6327747b72bb687c948270f702ff53c30f411a6Chris Lattner///
3602e6327747b72bb687c948270f702ff53c30f411a6Chris Lattner/// See C99 6.7.5.3p7 and C99 6.3.2.1p3.
36034ba2a17694148e16eaa8d3917f657ffcd3667be4Jay FoadQualType ASTContext::getArrayDecayedType(QualType Ty) const {
3604c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner  // Get the element type with 'getAsArrayType' so that we don't lose any
3605c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner  // typedefs in the element type of the array.  This also handles propagation
3606c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner  // of type qualifiers from the array type into the element type if present
3607c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner  // (C99 6.7.3p8).
3608c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner  const ArrayType *PrettyArrayType = getAsArrayType(Ty);
3609c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner  assert(PrettyArrayType && "Not an array type!");
36101eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
3611c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner  QualType PtrTy = getPointerType(PrettyArrayType->getElementType());
3612e6327747b72bb687c948270f702ff53c30f411a6Chris Lattner
3613e6327747b72bb687c948270f702ff53c30f411a6Chris Lattner  // int x[restrict 4] ->  int *restrict
36140953e767ff7817f97b3ab20896b229891eeff45bJohn McCall  return getQualifiedType(PtrTy, PrettyArrayType->getIndexTypeQualifiers());
3615e6327747b72bb687c948270f702ff53c30f411a6Chris Lattner}
3616e6327747b72bb687c948270f702ff53c30f411a6Chris Lattner
36173b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCallQualType ASTContext::getBaseElementType(const ArrayType *array) const {
36183b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall  return getBaseElementType(array->getElementType());
36195e03f9ea8174ae588c5e69ec6b5ef4c68f8fd766Douglas Gregor}
36205e03f9ea8174ae588c5e69ec6b5ef4c68f8fd766Douglas Gregor
36213b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCallQualType ASTContext::getBaseElementType(QualType type) const {
36223b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall  Qualifiers qs;
36233b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall  while (true) {
36243b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall    SplitQualType split = type.getSplitDesugaredType();
3625200fa53fd420aa8369586f569dbece04930ad6a3John McCall    const ArrayType *array = split.Ty->getAsArrayTypeUnsafe();
36263b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall    if (!array) break;
36271eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
36283b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall    type = array->getElementType();
3629200fa53fd420aa8369586f569dbece04930ad6a3John McCall    qs.addConsistentQualifiers(split.Quals);
36303b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall  }
36311eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
36323b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall  return getQualifiedType(type, qs);
36336183a99b064b397d98297904fbd6cf00fe1f453dAnders Carlsson}
36346183a99b064b397d98297904fbd6cf00fe1f453dAnders Carlsson
36350de78998e7bda473b408437053e48661b510d453Fariborz Jahanian/// getConstantArrayElementCount - Returns number of constant array elements.
36361eb4433ac451dc16f4133a88af2d002ac26c58efMike Stumpuint64_t
36370de78998e7bda473b408437053e48661b510d453Fariborz JahanianASTContext::getConstantArrayElementCount(const ConstantArrayType *CA)  const {
36380de78998e7bda473b408437053e48661b510d453Fariborz Jahanian  uint64_t ElementCount = 1;
36390de78998e7bda473b408437053e48661b510d453Fariborz Jahanian  do {
36400de78998e7bda473b408437053e48661b510d453Fariborz Jahanian    ElementCount *= CA->getSize().getZExtValue();
36410de78998e7bda473b408437053e48661b510d453Fariborz Jahanian    CA = dyn_cast<ConstantArrayType>(CA->getElementType());
36420de78998e7bda473b408437053e48661b510d453Fariborz Jahanian  } while (CA);
36430de78998e7bda473b408437053e48661b510d453Fariborz Jahanian  return ElementCount;
36440de78998e7bda473b408437053e48661b510d453Fariborz Jahanian}
36450de78998e7bda473b408437053e48661b510d453Fariborz Jahanian
36465f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// getFloatingRank - Return a relative rank for floating point types.
36475f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// This routine will assert if passed a built-in type that isn't a float.
3648a75cea3f6be0daa8054d36af81a6ffda1713f82dChris Lattnerstatic FloatingRank getFloatingRank(QualType T) {
3649183700f494ec9b6701b6efe82bcb25f4c79ba561John McCall  if (const ComplexType *CT = T->getAs<ComplexType>())
36505f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    return getFloatingRank(CT->getElementType());
3651a75cea3f6be0daa8054d36af81a6ffda1713f82dChris Lattner
3652183700f494ec9b6701b6efe82bcb25f4c79ba561John McCall  assert(T->getAs<BuiltinType>() && "getFloatingRank(): not a floating type");
3653183700f494ec9b6701b6efe82bcb25f4c79ba561John McCall  switch (T->getAs<BuiltinType>()->getKind()) {
3654b219cfc4d75f0a03630b7c4509ef791b7e97b2c8David Blaikie  default: llvm_unreachable("getFloatingRank(): not a floating type");
3655aa4a99b4a62615db243f7a5c433169f2fc704420Anton Korobeynikov  case BuiltinType::Half:       return HalfRank;
36565f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case BuiltinType::Float:      return FloatRank;
36575f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case BuiltinType::Double:     return DoubleRank;
36585f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case BuiltinType::LongDouble: return LongDoubleRank;
36595f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
36605f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
36615f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
36621eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// getFloatingTypeOfSizeWithinDomain - Returns a real floating
36631eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// point or a complex type (based on typeDomain/typeSize).
3664716c7304ff5d27a95e1e7823acd1d09d5ec3e37fSteve Naroff/// 'typeDomain' is a real floating point or complex type.
3665716c7304ff5d27a95e1e7823acd1d09d5ec3e37fSteve Naroff/// 'typeSize' is a real floating point or complex type.
36661361b11066239ea15764a2a844405352d87296b3Chris LattnerQualType ASTContext::getFloatingTypeOfSizeWithinDomain(QualType Size,
36671361b11066239ea15764a2a844405352d87296b3Chris Lattner                                                       QualType Domain) const {
36681361b11066239ea15764a2a844405352d87296b3Chris Lattner  FloatingRank EltRank = getFloatingRank(Size);
36691361b11066239ea15764a2a844405352d87296b3Chris Lattner  if (Domain->isComplexType()) {
36701361b11066239ea15764a2a844405352d87296b3Chris Lattner    switch (EltRank) {
3671561d3abc881033776ece385a01a510e1cbc1fa92David Blaikie    case HalfRank: llvm_unreachable("Complex half is not supported");
3672f1448a0e4a1e868ff873a8530a61a09cb68666ccSteve Naroff    case FloatRank:      return FloatComplexTy;
3673f1448a0e4a1e868ff873a8530a61a09cb68666ccSteve Naroff    case DoubleRank:     return DoubleComplexTy;
3674f1448a0e4a1e868ff873a8530a61a09cb68666ccSteve Naroff    case LongDoubleRank: return LongDoubleComplexTy;
3675f1448a0e4a1e868ff873a8530a61a09cb68666ccSteve Naroff    }
3676f1448a0e4a1e868ff873a8530a61a09cb68666ccSteve Naroff  }
36771361b11066239ea15764a2a844405352d87296b3Chris Lattner
36781361b11066239ea15764a2a844405352d87296b3Chris Lattner  assert(Domain->isRealFloatingType() && "Unknown domain!");
36791361b11066239ea15764a2a844405352d87296b3Chris Lattner  switch (EltRank) {
3680561d3abc881033776ece385a01a510e1cbc1fa92David Blaikie  case HalfRank: llvm_unreachable("Half ranks are not valid here");
36811361b11066239ea15764a2a844405352d87296b3Chris Lattner  case FloatRank:      return FloatTy;
36821361b11066239ea15764a2a844405352d87296b3Chris Lattner  case DoubleRank:     return DoubleTy;
36831361b11066239ea15764a2a844405352d87296b3Chris Lattner  case LongDoubleRank: return LongDoubleTy;
36845f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
3685561d3abc881033776ece385a01a510e1cbc1fa92David Blaikie  llvm_unreachable("getFloatingRank(): illegal value for rank");
36865f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
36875f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
36887cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner/// getFloatingTypeOrder - Compare the rank of the two specified floating
36897cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner/// point types, ignoring the domain of the type (i.e. 'double' ==
36907cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner/// '_Complex double').  If LHS > RHS, return 1.  If LHS == RHS, return 0. If
36911eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// LHS < RHS, return -1.
36924ba2a17694148e16eaa8d3917f657ffcd3667be4Jay Foadint ASTContext::getFloatingTypeOrder(QualType LHS, QualType RHS) const {
3693a75cea3f6be0daa8054d36af81a6ffda1713f82dChris Lattner  FloatingRank LHSR = getFloatingRank(LHS);
3694a75cea3f6be0daa8054d36af81a6ffda1713f82dChris Lattner  FloatingRank RHSR = getFloatingRank(RHS);
36951eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
3696a75cea3f6be0daa8054d36af81a6ffda1713f82dChris Lattner  if (LHSR == RHSR)
3697fb0d49669aa370b4c0993c5cee60275ef9fd6518Steve Naroff    return 0;
3698a75cea3f6be0daa8054d36af81a6ffda1713f82dChris Lattner  if (LHSR > RHSR)
3699fb0d49669aa370b4c0993c5cee60275ef9fd6518Steve Naroff    return 1;
3700fb0d49669aa370b4c0993c5cee60275ef9fd6518Steve Naroff  return -1;
37015f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
37025f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
3703f52ab250ff92bc51a9ac9a8e19bd43b63a5f844fChris Lattner/// getIntegerRank - Return an integer conversion rank (C99 6.3.1.1p1). This
3704f52ab250ff92bc51a9ac9a8e19bd43b63a5f844fChris Lattner/// routine will assert if passed a built-in type that isn't an integer or enum,
3705f52ab250ff92bc51a9ac9a8e19bd43b63a5f844fChris Lattner/// or if it is not canonicalized.
3706f4c7371fb1d3cebcfb40abad4537bb82515704eaJohn McCallunsigned ASTContext::getIntegerRank(const Type *T) const {
3707467b27b9a24bdc823218ad1ad0e37673b6cc1e83John McCall  assert(T->isCanonicalUnqualified() && "T should be canonicalized");
3708f5c209d23b20ada4a9b6235db50317239cbf6ae1Alisdair Meredith
3709f52ab250ff92bc51a9ac9a8e19bd43b63a5f844fChris Lattner  switch (cast<BuiltinType>(T)->getKind()) {
3710b219cfc4d75f0a03630b7c4509ef791b7e97b2c8David Blaikie  default: llvm_unreachable("getIntegerRank(): not a built-in integer");
37117cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner  case BuiltinType::Bool:
3712f98aba35e6c3da5aae61843fc01334939e4e12ecEli Friedman    return 1 + (getIntWidth(BoolTy) << 3);
37137cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner  case BuiltinType::Char_S:
37147cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner  case BuiltinType::Char_U:
37157cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner  case BuiltinType::SChar:
37167cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner  case BuiltinType::UChar:
3717f98aba35e6c3da5aae61843fc01334939e4e12ecEli Friedman    return 2 + (getIntWidth(CharTy) << 3);
37187cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner  case BuiltinType::Short:
37197cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner  case BuiltinType::UShort:
3720f98aba35e6c3da5aae61843fc01334939e4e12ecEli Friedman    return 3 + (getIntWidth(ShortTy) << 3);
37217cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner  case BuiltinType::Int:
37227cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner  case BuiltinType::UInt:
3723f98aba35e6c3da5aae61843fc01334939e4e12ecEli Friedman    return 4 + (getIntWidth(IntTy) << 3);
37247cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner  case BuiltinType::Long:
37257cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner  case BuiltinType::ULong:
3726f98aba35e6c3da5aae61843fc01334939e4e12ecEli Friedman    return 5 + (getIntWidth(LongTy) << 3);
37277cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner  case BuiltinType::LongLong:
37287cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner  case BuiltinType::ULongLong:
3729f98aba35e6c3da5aae61843fc01334939e4e12ecEli Friedman    return 6 + (getIntWidth(LongLongTy) << 3);
37302df9ced9fd1e8c7d7b38443db07e0e811de22571Chris Lattner  case BuiltinType::Int128:
37312df9ced9fd1e8c7d7b38443db07e0e811de22571Chris Lattner  case BuiltinType::UInt128:
37322df9ced9fd1e8c7d7b38443db07e0e811de22571Chris Lattner    return 7 + (getIntWidth(Int128Ty) << 3);
3733f52ab250ff92bc51a9ac9a8e19bd43b63a5f844fChris Lattner  }
3734f52ab250ff92bc51a9ac9a8e19bd43b63a5f844fChris Lattner}
3735f52ab250ff92bc51a9ac9a8e19bd43b63a5f844fChris Lattner
373604e8357f6801e9ff52673e7e899a67bbabf9de93Eli Friedman/// \brief Whether this is a promotable bitfield reference according
373704e8357f6801e9ff52673e7e899a67bbabf9de93Eli Friedman/// to C99 6.3.1.1p2, bullet 2 (and GCC extensions).
373804e8357f6801e9ff52673e7e899a67bbabf9de93Eli Friedman///
373904e8357f6801e9ff52673e7e899a67bbabf9de93Eli Friedman/// \returns the type this bit-field will promote to, or NULL if no
374004e8357f6801e9ff52673e7e899a67bbabf9de93Eli Friedman/// promotion occurs.
37414ba2a17694148e16eaa8d3917f657ffcd3667be4Jay FoadQualType ASTContext::isPromotableBitField(Expr *E) const {
3742ceafbdeb93ecf323cca74e660bf54504c86f3b71Douglas Gregor  if (E->isTypeDependent() || E->isValueDependent())
3743ceafbdeb93ecf323cca74e660bf54504c86f3b71Douglas Gregor    return QualType();
3744ceafbdeb93ecf323cca74e660bf54504c86f3b71Douglas Gregor
374504e8357f6801e9ff52673e7e899a67bbabf9de93Eli Friedman  FieldDecl *Field = E->getBitField();
374604e8357f6801e9ff52673e7e899a67bbabf9de93Eli Friedman  if (!Field)
374704e8357f6801e9ff52673e7e899a67bbabf9de93Eli Friedman    return QualType();
374804e8357f6801e9ff52673e7e899a67bbabf9de93Eli Friedman
374904e8357f6801e9ff52673e7e899a67bbabf9de93Eli Friedman  QualType FT = Field->getType();
375004e8357f6801e9ff52673e7e899a67bbabf9de93Eli Friedman
3751a6b8b2c09610b8bc4330e948ece8b940c2386406Richard Smith  uint64_t BitWidth = Field->getBitWidthValue(*this);
375204e8357f6801e9ff52673e7e899a67bbabf9de93Eli Friedman  uint64_t IntSize = getTypeSize(IntTy);
375304e8357f6801e9ff52673e7e899a67bbabf9de93Eli Friedman  // GCC extension compatibility: if the bit-field size is less than or equal
375404e8357f6801e9ff52673e7e899a67bbabf9de93Eli Friedman  // to the size of int, it gets promoted no matter what its type is.
375504e8357f6801e9ff52673e7e899a67bbabf9de93Eli Friedman  // For instance, unsigned long bf : 4 gets promoted to signed int.
375604e8357f6801e9ff52673e7e899a67bbabf9de93Eli Friedman  if (BitWidth < IntSize)
375704e8357f6801e9ff52673e7e899a67bbabf9de93Eli Friedman    return IntTy;
375804e8357f6801e9ff52673e7e899a67bbabf9de93Eli Friedman
375904e8357f6801e9ff52673e7e899a67bbabf9de93Eli Friedman  if (BitWidth == IntSize)
376004e8357f6801e9ff52673e7e899a67bbabf9de93Eli Friedman    return FT->isSignedIntegerType() ? IntTy : UnsignedIntTy;
376104e8357f6801e9ff52673e7e899a67bbabf9de93Eli Friedman
376204e8357f6801e9ff52673e7e899a67bbabf9de93Eli Friedman  // Types bigger than int are not subject to promotions, and therefore act
376304e8357f6801e9ff52673e7e899a67bbabf9de93Eli Friedman  // like the base type.
376404e8357f6801e9ff52673e7e899a67bbabf9de93Eli Friedman  // FIXME: This doesn't quite match what gcc does, but what gcc does here
376504e8357f6801e9ff52673e7e899a67bbabf9de93Eli Friedman  // is ridiculous.
376604e8357f6801e9ff52673e7e899a67bbabf9de93Eli Friedman  return QualType();
376704e8357f6801e9ff52673e7e899a67bbabf9de93Eli Friedman}
376804e8357f6801e9ff52673e7e899a67bbabf9de93Eli Friedman
3769a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman/// getPromotedIntegerType - Returns the type that Promotable will
3770a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman/// promote to: C99 6.3.1.1p2, assuming that Promotable is a promotable
3771a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman/// integer type.
37724ba2a17694148e16eaa8d3917f657ffcd3667be4Jay FoadQualType ASTContext::getPromotedIntegerType(QualType Promotable) const {
3773a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman  assert(!Promotable.isNull());
3774a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman  assert(Promotable->isPromotableIntegerType());
3775842aef8d942a880eeb9535d40de31a86838264cbJohn McCall  if (const EnumType *ET = Promotable->getAs<EnumType>())
3776842aef8d942a880eeb9535d40de31a86838264cbJohn McCall    return ET->getDecl()->getPromotionType();
377768a2dc446fe6d32d5da3557902100ed06b21b12bEli Friedman
377868a2dc446fe6d32d5da3557902100ed06b21b12bEli Friedman  if (const BuiltinType *BT = Promotable->getAs<BuiltinType>()) {
377968a2dc446fe6d32d5da3557902100ed06b21b12bEli Friedman    // C++ [conv.prom]: A prvalue of type char16_t, char32_t, or wchar_t
378068a2dc446fe6d32d5da3557902100ed06b21b12bEli Friedman    // (3.9.1) can be converted to a prvalue of the first of the following
378168a2dc446fe6d32d5da3557902100ed06b21b12bEli Friedman    // types that can represent all the values of its underlying type:
378268a2dc446fe6d32d5da3557902100ed06b21b12bEli Friedman    // int, unsigned int, long int, unsigned long int, long long int, or
378368a2dc446fe6d32d5da3557902100ed06b21b12bEli Friedman    // unsigned long long int [...]
378468a2dc446fe6d32d5da3557902100ed06b21b12bEli Friedman    // FIXME: Is there some better way to compute this?
378568a2dc446fe6d32d5da3557902100ed06b21b12bEli Friedman    if (BT->getKind() == BuiltinType::WChar_S ||
378668a2dc446fe6d32d5da3557902100ed06b21b12bEli Friedman        BT->getKind() == BuiltinType::WChar_U ||
378768a2dc446fe6d32d5da3557902100ed06b21b12bEli Friedman        BT->getKind() == BuiltinType::Char16 ||
378868a2dc446fe6d32d5da3557902100ed06b21b12bEli Friedman        BT->getKind() == BuiltinType::Char32) {
378968a2dc446fe6d32d5da3557902100ed06b21b12bEli Friedman      bool FromIsSigned = BT->getKind() == BuiltinType::WChar_S;
379068a2dc446fe6d32d5da3557902100ed06b21b12bEli Friedman      uint64_t FromSize = getTypeSize(BT);
379168a2dc446fe6d32d5da3557902100ed06b21b12bEli Friedman      QualType PromoteTypes[] = { IntTy, UnsignedIntTy, LongTy, UnsignedLongTy,
379268a2dc446fe6d32d5da3557902100ed06b21b12bEli Friedman                                  LongLongTy, UnsignedLongLongTy };
379368a2dc446fe6d32d5da3557902100ed06b21b12bEli Friedman      for (size_t Idx = 0; Idx < llvm::array_lengthof(PromoteTypes); ++Idx) {
379468a2dc446fe6d32d5da3557902100ed06b21b12bEli Friedman        uint64_t ToSize = getTypeSize(PromoteTypes[Idx]);
379568a2dc446fe6d32d5da3557902100ed06b21b12bEli Friedman        if (FromSize < ToSize ||
379668a2dc446fe6d32d5da3557902100ed06b21b12bEli Friedman            (FromSize == ToSize &&
379768a2dc446fe6d32d5da3557902100ed06b21b12bEli Friedman             FromIsSigned == PromoteTypes[Idx]->isSignedIntegerType()))
379868a2dc446fe6d32d5da3557902100ed06b21b12bEli Friedman          return PromoteTypes[Idx];
379968a2dc446fe6d32d5da3557902100ed06b21b12bEli Friedman      }
380068a2dc446fe6d32d5da3557902100ed06b21b12bEli Friedman      llvm_unreachable("char type should fit into long long");
380168a2dc446fe6d32d5da3557902100ed06b21b12bEli Friedman    }
380268a2dc446fe6d32d5da3557902100ed06b21b12bEli Friedman  }
380368a2dc446fe6d32d5da3557902100ed06b21b12bEli Friedman
380468a2dc446fe6d32d5da3557902100ed06b21b12bEli Friedman  // At this point, we should have a signed or unsigned integer type.
3805a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman  if (Promotable->isSignedIntegerType())
3806a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman    return IntTy;
3807a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman  uint64_t PromotableSize = getTypeSize(Promotable);
3808a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman  uint64_t IntSize = getTypeSize(IntTy);
3809a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman  assert(Promotable->isUnsignedIntegerType() && PromotableSize <= IntSize);
3810a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman  return (PromotableSize != IntSize) ? IntTy : UnsignedIntTy;
3811a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman}
3812a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman
381331862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis/// \brief Recurses in pointer/array types until it finds an objc retainable
381431862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis/// type and returns its ownership.
381531862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios KyrtzidisQualifiers::ObjCLifetime ASTContext::getInnerObjCOwnership(QualType T) const {
381631862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis  while (!T.isNull()) {
381731862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis    if (T.getObjCLifetime() != Qualifiers::OCL_None)
381831862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis      return T.getObjCLifetime();
381931862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis    if (T->isArrayType())
382031862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis      T = getBaseElementType(T);
382131862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis    else if (const PointerType *PT = T->getAs<PointerType>())
382231862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis      T = PT->getPointeeType();
382331862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis    else if (const ReferenceType *RT = T->getAs<ReferenceType>())
382428445f0b62f6aed851ff87ce64d9b19200d3211fArgyrios Kyrtzidis      T = RT->getPointeeType();
382531862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis    else
382631862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis      break;
382731862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis  }
382831862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis
382931862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis  return Qualifiers::OCL_None;
383031862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis}
383131862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis
38321eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// getIntegerTypeOrder - Returns the highest ranked integer type:
38337cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner/// C99 6.3.1.8p1.  If LHS > RHS, return 1.  If LHS == RHS, return 0. If
38341eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// LHS < RHS, return -1.
38354ba2a17694148e16eaa8d3917f657ffcd3667be4Jay Foadint ASTContext::getIntegerTypeOrder(QualType LHS, QualType RHS) const {
3836f4c7371fb1d3cebcfb40abad4537bb82515704eaJohn McCall  const Type *LHSC = getCanonicalType(LHS).getTypePtr();
3837f4c7371fb1d3cebcfb40abad4537bb82515704eaJohn McCall  const Type *RHSC = getCanonicalType(RHS).getTypePtr();
38387cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner  if (LHSC == RHSC) return 0;
38391eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
3840f52ab250ff92bc51a9ac9a8e19bd43b63a5f844fChris Lattner  bool LHSUnsigned = LHSC->isUnsignedIntegerType();
3841f52ab250ff92bc51a9ac9a8e19bd43b63a5f844fChris Lattner  bool RHSUnsigned = RHSC->isUnsignedIntegerType();
38421eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
38437cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner  unsigned LHSRank = getIntegerRank(LHSC);
38447cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner  unsigned RHSRank = getIntegerRank(RHSC);
38451eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
38467cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner  if (LHSUnsigned == RHSUnsigned) {  // Both signed or both unsigned.
38477cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner    if (LHSRank == RHSRank) return 0;
38487cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner    return LHSRank > RHSRank ? 1 : -1;
38497cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner  }
38501eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
38517cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner  // Otherwise, the LHS is signed and the RHS is unsigned or visa versa.
38527cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner  if (LHSUnsigned) {
38537cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner    // If the unsigned [LHS] type is larger, return it.
38547cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner    if (LHSRank >= RHSRank)
38557cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner      return 1;
38561eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
38577cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner    // If the signed type can represent all values of the unsigned type, it
38587cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner    // wins.  Because we are dealing with 2's complement and types that are
38591eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    // powers of two larger than each other, this is always safe.
38607cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner    return -1;
38617cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner  }
38627cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner
38637cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner  // If the unsigned [RHS] type is larger, return it.
38647cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner  if (RHSRank >= LHSRank)
38657cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner    return -1;
38661eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
38677cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner  // If the signed type can represent all values of the unsigned type, it
38687cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner  // wins.  Because we are dealing with 2's complement and types that are
38691eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  // powers of two larger than each other, this is always safe.
38707cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner  return 1;
38715f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
387271993dd85eed9cc42c6b2fa61ee5c53026b74817Anders Carlsson
387379cbc7dd2aacd85a28f469b5dc73c4ea296e7072Anders Carlssonstatic RecordDecl *
3874ba877adeb49ed6dc17f27fa3a3bcd0cca713fd68Abramo BagnaraCreateRecordDecl(const ASTContext &Ctx, RecordDecl::TagKind TK,
3875ba877adeb49ed6dc17f27fa3a3bcd0cca713fd68Abramo Bagnara                 DeclContext *DC, IdentifierInfo *Id) {
3876ba877adeb49ed6dc17f27fa3a3bcd0cca713fd68Abramo Bagnara  SourceLocation Loc;
38774e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie  if (Ctx.getLangOpts().CPlusPlus)
3878ba877adeb49ed6dc17f27fa3a3bcd0cca713fd68Abramo Bagnara    return CXXRecordDecl::Create(Ctx, TK, DC, Loc, Loc, Id);
387979cbc7dd2aacd85a28f469b5dc73c4ea296e7072Anders Carlsson  else
3880ba877adeb49ed6dc17f27fa3a3bcd0cca713fd68Abramo Bagnara    return RecordDecl::Create(Ctx, TK, DC, Loc, Loc, Id);
388179cbc7dd2aacd85a28f469b5dc73c4ea296e7072Anders Carlsson}
3882ba877adeb49ed6dc17f27fa3a3bcd0cca713fd68Abramo Bagnara
38831eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump// getCFConstantStringType - Return the type used for constant CFStrings.
38844ba2a17694148e16eaa8d3917f657ffcd3667be4Jay FoadQualType ASTContext::getCFConstantStringType() const {
388571993dd85eed9cc42c6b2fa61ee5c53026b74817Anders Carlsson  if (!CFConstantStringTypeDecl) {
38861eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    CFConstantStringTypeDecl =
3887ba877adeb49ed6dc17f27fa3a3bcd0cca713fd68Abramo Bagnara      CreateRecordDecl(*this, TTK_Struct, TUDecl,
388879cbc7dd2aacd85a28f469b5dc73c4ea296e7072Anders Carlsson                       &Idents.get("NSConstantString"));
38895cfa011e61e14e6f2e1659047d809706c0e4c6a3John McCall    CFConstantStringTypeDecl->startDefinition();
389079cbc7dd2aacd85a28f469b5dc73c4ea296e7072Anders Carlsson
3891f06273f8bbacb086a46bde456429c8d08f6d07eeAnders Carlsson    QualType FieldTypes[4];
38921eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
389371993dd85eed9cc42c6b2fa61ee5c53026b74817Anders Carlsson    // const int *isa;
38940953e767ff7817f97b3ab20896b229891eeff45bJohn McCall    FieldTypes[0] = getPointerType(IntTy.withConst());
3895f06273f8bbacb086a46bde456429c8d08f6d07eeAnders Carlsson    // int flags;
3896f06273f8bbacb086a46bde456429c8d08f6d07eeAnders Carlsson    FieldTypes[1] = IntTy;
389771993dd85eed9cc42c6b2fa61ee5c53026b74817Anders Carlsson    // const char *str;
38980953e767ff7817f97b3ab20896b229891eeff45bJohn McCall    FieldTypes[2] = getPointerType(CharTy.withConst());
389971993dd85eed9cc42c6b2fa61ee5c53026b74817Anders Carlsson    // long length;
39001eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    FieldTypes[3] = LongTy;
39011eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
390244b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor    // Create fields
390344b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor    for (unsigned i = 0; i < 4; ++i) {
39041eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      FieldDecl *Field = FieldDecl::Create(*this, CFConstantStringTypeDecl,
3905ff676cb48fe8bf7be2feaa251dc7c5fb15af4730Abramo Bagnara                                           SourceLocation(),
390644b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor                                           SourceLocation(), 0,
3907a93c934af4fbf97cbe8e649d82e68ccacfe57c95John McCall                                           FieldTypes[i], /*TInfo=*/0,
39081eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump                                           /*BitWidth=*/0,
39097a614d8380297fcd2bc23986241905d97222948cRichard Smith                                           /*Mutable=*/false,
3910ca5233044ef679840d1ad1c46a36b16e2ee8a6e1Richard Smith                                           ICIS_NoInit);
39112888b65aae768f54062505330df7be230a0510c7John McCall      Field->setAccess(AS_public);
391217945a0f64fe03ff6ec0c2146005a87636e3ac12Argyrios Kyrtzidis      CFConstantStringTypeDecl->addDecl(Field);
391344b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor    }
391444b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor
3915838db383b69b9fb55f55c8e9546477df198a4faaDouglas Gregor    CFConstantStringTypeDecl->completeDefinition();
391671993dd85eed9cc42c6b2fa61ee5c53026b74817Anders Carlsson  }
39171eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
391871993dd85eed9cc42c6b2fa61ee5c53026b74817Anders Carlsson  return getTagDeclType(CFConstantStringTypeDecl);
39198467583c2704e7a9691ea56939a029015f0ade0aGabor Greif}
3920b2cf3573d7351094f6247fcca94703ce88eb9ee0Anders Carlsson
3921319ac896a0fef7365d5589b8021db7e41207fe42Douglas Gregorvoid ASTContext::setCFConstantStringType(QualType T) {
39226217b80b7a1379b74cced1c076338262c3c980b3Ted Kremenek  const RecordType *Rec = T->getAs<RecordType>();
3923319ac896a0fef7365d5589b8021db7e41207fe42Douglas Gregor  assert(Rec && "Invalid CFConstantStringType");
3924319ac896a0fef7365d5589b8021db7e41207fe42Douglas Gregor  CFConstantStringTypeDecl = Rec->getDecl();
3925319ac896a0fef7365d5589b8021db7e41207fe42Douglas Gregor}
3926319ac896a0fef7365d5589b8021db7e41207fe42Douglas Gregor
39274ba2a17694148e16eaa8d3917f657ffcd3667be4Jay FoadQualType ASTContext::getBlockDescriptorType() const {
3928adaaad3715c9c26cdcfdfe3401a13d7b4423ddcfMike Stump  if (BlockDescriptorType)
3929adaaad3715c9c26cdcfdfe3401a13d7b4423ddcfMike Stump    return getTagDeclType(BlockDescriptorType);
3930adaaad3715c9c26cdcfdfe3401a13d7b4423ddcfMike Stump
3931adaaad3715c9c26cdcfdfe3401a13d7b4423ddcfMike Stump  RecordDecl *T;
3932adaaad3715c9c26cdcfdfe3401a13d7b4423ddcfMike Stump  // FIXME: Needs the FlagAppleBlock bit.
3933ba877adeb49ed6dc17f27fa3a3bcd0cca713fd68Abramo Bagnara  T = CreateRecordDecl(*this, TTK_Struct, TUDecl,
393479cbc7dd2aacd85a28f469b5dc73c4ea296e7072Anders Carlsson                       &Idents.get("__block_descriptor"));
39355cfa011e61e14e6f2e1659047d809706c0e4c6a3John McCall  T->startDefinition();
3936adaaad3715c9c26cdcfdfe3401a13d7b4423ddcfMike Stump
3937adaaad3715c9c26cdcfdfe3401a13d7b4423ddcfMike Stump  QualType FieldTypes[] = {
3938adaaad3715c9c26cdcfdfe3401a13d7b4423ddcfMike Stump    UnsignedLongTy,
3939adaaad3715c9c26cdcfdfe3401a13d7b4423ddcfMike Stump    UnsignedLongTy,
3940adaaad3715c9c26cdcfdfe3401a13d7b4423ddcfMike Stump  };
3941adaaad3715c9c26cdcfdfe3401a13d7b4423ddcfMike Stump
3942adaaad3715c9c26cdcfdfe3401a13d7b4423ddcfMike Stump  const char *FieldNames[] = {
3943adaaad3715c9c26cdcfdfe3401a13d7b4423ddcfMike Stump    "reserved",
3944083c25eea14bb4cc4ecc3ec763c60e2e609e22bdMike Stump    "Size"
3945adaaad3715c9c26cdcfdfe3401a13d7b4423ddcfMike Stump  };
3946adaaad3715c9c26cdcfdfe3401a13d7b4423ddcfMike Stump
3947adaaad3715c9c26cdcfdfe3401a13d7b4423ddcfMike Stump  for (size_t i = 0; i < 2; ++i) {
3948ff676cb48fe8bf7be2feaa251dc7c5fb15af4730Abramo Bagnara    FieldDecl *Field = FieldDecl::Create(*this, T, SourceLocation(),
3949adaaad3715c9c26cdcfdfe3401a13d7b4423ddcfMike Stump                                         SourceLocation(),
3950adaaad3715c9c26cdcfdfe3401a13d7b4423ddcfMike Stump                                         &Idents.get(FieldNames[i]),
3951a93c934af4fbf97cbe8e649d82e68ccacfe57c95John McCall                                         FieldTypes[i], /*TInfo=*/0,
3952adaaad3715c9c26cdcfdfe3401a13d7b4423ddcfMike Stump                                         /*BitWidth=*/0,
39537a614d8380297fcd2bc23986241905d97222948cRichard Smith                                         /*Mutable=*/false,
3954ca5233044ef679840d1ad1c46a36b16e2ee8a6e1Richard Smith                                         ICIS_NoInit);
39552888b65aae768f54062505330df7be230a0510c7John McCall    Field->setAccess(AS_public);
3956adaaad3715c9c26cdcfdfe3401a13d7b4423ddcfMike Stump    T->addDecl(Field);
3957adaaad3715c9c26cdcfdfe3401a13d7b4423ddcfMike Stump  }
3958adaaad3715c9c26cdcfdfe3401a13d7b4423ddcfMike Stump
3959838db383b69b9fb55f55c8e9546477df198a4faaDouglas Gregor  T->completeDefinition();
3960adaaad3715c9c26cdcfdfe3401a13d7b4423ddcfMike Stump
3961adaaad3715c9c26cdcfdfe3401a13d7b4423ddcfMike Stump  BlockDescriptorType = T;
3962adaaad3715c9c26cdcfdfe3401a13d7b4423ddcfMike Stump
3963adaaad3715c9c26cdcfdfe3401a13d7b4423ddcfMike Stump  return getTagDeclType(BlockDescriptorType);
3964adaaad3715c9c26cdcfdfe3401a13d7b4423ddcfMike Stump}
3965adaaad3715c9c26cdcfdfe3401a13d7b4423ddcfMike Stump
39664ba2a17694148e16eaa8d3917f657ffcd3667be4Jay FoadQualType ASTContext::getBlockDescriptorExtendedType() const {
3967083c25eea14bb4cc4ecc3ec763c60e2e609e22bdMike Stump  if (BlockDescriptorExtendedType)
3968083c25eea14bb4cc4ecc3ec763c60e2e609e22bdMike Stump    return getTagDeclType(BlockDescriptorExtendedType);
3969083c25eea14bb4cc4ecc3ec763c60e2e609e22bdMike Stump
3970083c25eea14bb4cc4ecc3ec763c60e2e609e22bdMike Stump  RecordDecl *T;
3971083c25eea14bb4cc4ecc3ec763c60e2e609e22bdMike Stump  // FIXME: Needs the FlagAppleBlock bit.
3972ba877adeb49ed6dc17f27fa3a3bcd0cca713fd68Abramo Bagnara  T = CreateRecordDecl(*this, TTK_Struct, TUDecl,
397379cbc7dd2aacd85a28f469b5dc73c4ea296e7072Anders Carlsson                       &Idents.get("__block_descriptor_withcopydispose"));
39745cfa011e61e14e6f2e1659047d809706c0e4c6a3John McCall  T->startDefinition();
3975083c25eea14bb4cc4ecc3ec763c60e2e609e22bdMike Stump
3976083c25eea14bb4cc4ecc3ec763c60e2e609e22bdMike Stump  QualType FieldTypes[] = {
3977083c25eea14bb4cc4ecc3ec763c60e2e609e22bdMike Stump    UnsignedLongTy,
3978083c25eea14bb4cc4ecc3ec763c60e2e609e22bdMike Stump    UnsignedLongTy,
3979083c25eea14bb4cc4ecc3ec763c60e2e609e22bdMike Stump    getPointerType(VoidPtrTy),
3980083c25eea14bb4cc4ecc3ec763c60e2e609e22bdMike Stump    getPointerType(VoidPtrTy)
3981083c25eea14bb4cc4ecc3ec763c60e2e609e22bdMike Stump  };
3982083c25eea14bb4cc4ecc3ec763c60e2e609e22bdMike Stump
3983083c25eea14bb4cc4ecc3ec763c60e2e609e22bdMike Stump  const char *FieldNames[] = {
3984083c25eea14bb4cc4ecc3ec763c60e2e609e22bdMike Stump    "reserved",
3985083c25eea14bb4cc4ecc3ec763c60e2e609e22bdMike Stump    "Size",
3986083c25eea14bb4cc4ecc3ec763c60e2e609e22bdMike Stump    "CopyFuncPtr",
3987083c25eea14bb4cc4ecc3ec763c60e2e609e22bdMike Stump    "DestroyFuncPtr"
3988083c25eea14bb4cc4ecc3ec763c60e2e609e22bdMike Stump  };
3989083c25eea14bb4cc4ecc3ec763c60e2e609e22bdMike Stump
3990083c25eea14bb4cc4ecc3ec763c60e2e609e22bdMike Stump  for (size_t i = 0; i < 4; ++i) {
3991ff676cb48fe8bf7be2feaa251dc7c5fb15af4730Abramo Bagnara    FieldDecl *Field = FieldDecl::Create(*this, T, SourceLocation(),
3992083c25eea14bb4cc4ecc3ec763c60e2e609e22bdMike Stump                                         SourceLocation(),
3993083c25eea14bb4cc4ecc3ec763c60e2e609e22bdMike Stump                                         &Idents.get(FieldNames[i]),
3994a93c934af4fbf97cbe8e649d82e68ccacfe57c95John McCall                                         FieldTypes[i], /*TInfo=*/0,
3995083c25eea14bb4cc4ecc3ec763c60e2e609e22bdMike Stump                                         /*BitWidth=*/0,
39967a614d8380297fcd2bc23986241905d97222948cRichard Smith                                         /*Mutable=*/false,
3997ca5233044ef679840d1ad1c46a36b16e2ee8a6e1Richard Smith                                         ICIS_NoInit);
39982888b65aae768f54062505330df7be230a0510c7John McCall    Field->setAccess(AS_public);
3999083c25eea14bb4cc4ecc3ec763c60e2e609e22bdMike Stump    T->addDecl(Field);
4000083c25eea14bb4cc4ecc3ec763c60e2e609e22bdMike Stump  }
4001083c25eea14bb4cc4ecc3ec763c60e2e609e22bdMike Stump
4002838db383b69b9fb55f55c8e9546477df198a4faaDouglas Gregor  T->completeDefinition();
4003083c25eea14bb4cc4ecc3ec763c60e2e609e22bdMike Stump
4004083c25eea14bb4cc4ecc3ec763c60e2e609e22bdMike Stump  BlockDescriptorExtendedType = T;
4005083c25eea14bb4cc4ecc3ec763c60e2e609e22bdMike Stump
4006083c25eea14bb4cc4ecc3ec763c60e2e609e22bdMike Stump  return getTagDeclType(BlockDescriptorExtendedType);
4007083c25eea14bb4cc4ecc3ec763c60e2e609e22bdMike Stump}
4008083c25eea14bb4cc4ecc3ec763c60e2e609e22bdMike Stump
40094ba2a17694148e16eaa8d3917f657ffcd3667be4Jay Foadbool ASTContext::BlockRequiresCopying(QualType Ty) const {
4010f85e193739c953358c865005855253af4f68a497John McCall  if (Ty->isObjCRetainableType())
4011af7b44d847d3e44c43346d508b2e55a6254b6e9dMike Stump    return true;
40124e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie  if (getLangOpts().CPlusPlus) {
4013e38be617437ccdcc180c5a49e447cbcd07539292Fariborz Jahanian    if (const RecordType *RT = Ty->getAs<RecordType>()) {
4014e38be617437ccdcc180c5a49e447cbcd07539292Fariborz Jahanian      CXXRecordDecl *RD = cast<CXXRecordDecl>(RT->getDecl());
4015ffe37fdda5b4b4f162a45155c30d9f60ce110c12Sean Hunt      return RD->hasConstCopyConstructor();
4016e38be617437ccdcc180c5a49e447cbcd07539292Fariborz Jahanian
4017e38be617437ccdcc180c5a49e447cbcd07539292Fariborz Jahanian    }
4018e38be617437ccdcc180c5a49e447cbcd07539292Fariborz Jahanian  }
4019af7b44d847d3e44c43346d508b2e55a6254b6e9dMike Stump  return false;
4020af7b44d847d3e44c43346d508b2e55a6254b6e9dMike Stump}
4021af7b44d847d3e44c43346d508b2e55a6254b6e9dMike Stump
40224ba2a17694148e16eaa8d3917f657ffcd3667be4Jay FoadQualType
40235f9e272e632e951b1efe824cd16acb4d96077930Chris LattnerASTContext::BuildByRefType(StringRef DeclName, QualType Ty) const {
4024af7b44d847d3e44c43346d508b2e55a6254b6e9dMike Stump  //  type = struct __Block_byref_1_X {
4025ea26cb522e88fc86b0d1cae61dcefcfe4cc20231Mike Stump  //    void *__isa;
4026af7b44d847d3e44c43346d508b2e55a6254b6e9dMike Stump  //    struct __Block_byref_1_X *__forwarding;
4027ea26cb522e88fc86b0d1cae61dcefcfe4cc20231Mike Stump  //    unsigned int __flags;
4028ea26cb522e88fc86b0d1cae61dcefcfe4cc20231Mike Stump  //    unsigned int __size;
4029a7e6845660f91ec611427e1db842780e1ec12bdbEli Friedman  //    void *__copy_helper;            // as needed
4030a7e6845660f91ec611427e1db842780e1ec12bdbEli Friedman  //    void *__destroy_help            // as needed
4031af7b44d847d3e44c43346d508b2e55a6254b6e9dMike Stump  //    int X;
4032ea26cb522e88fc86b0d1cae61dcefcfe4cc20231Mike Stump  //  } *
4033ea26cb522e88fc86b0d1cae61dcefcfe4cc20231Mike Stump
4034af7b44d847d3e44c43346d508b2e55a6254b6e9dMike Stump  bool HasCopyAndDispose = BlockRequiresCopying(Ty);
4035af7b44d847d3e44c43346d508b2e55a6254b6e9dMike Stump
4036af7b44d847d3e44c43346d508b2e55a6254b6e9dMike Stump  // FIXME: Move up
4037f7ccbad5d9949e7ddd1cbef43d482553b811e026Dylan Noblesmith  SmallString<36> Name;
4038f5942a44880be26878592eb052b737579349411eBenjamin Kramer  llvm::raw_svector_ostream(Name) << "__Block_byref_" <<
4039f5942a44880be26878592eb052b737579349411eBenjamin Kramer                                  ++UniqueBlockByRefTypeID << '_' << DeclName;
4040af7b44d847d3e44c43346d508b2e55a6254b6e9dMike Stump  RecordDecl *T;
4041ba877adeb49ed6dc17f27fa3a3bcd0cca713fd68Abramo Bagnara  T = CreateRecordDecl(*this, TTK_Struct, TUDecl, &Idents.get(Name.str()));
4042af7b44d847d3e44c43346d508b2e55a6254b6e9dMike Stump  T->startDefinition();
4043af7b44d847d3e44c43346d508b2e55a6254b6e9dMike Stump  QualType Int32Ty = IntTy;
4044af7b44d847d3e44c43346d508b2e55a6254b6e9dMike Stump  assert(getIntWidth(IntTy) == 32 && "non-32bit int not supported");
4045af7b44d847d3e44c43346d508b2e55a6254b6e9dMike Stump  QualType FieldTypes[] = {
4046af7b44d847d3e44c43346d508b2e55a6254b6e9dMike Stump    getPointerType(VoidPtrTy),
4047af7b44d847d3e44c43346d508b2e55a6254b6e9dMike Stump    getPointerType(getTagDeclType(T)),
4048af7b44d847d3e44c43346d508b2e55a6254b6e9dMike Stump    Int32Ty,
4049af7b44d847d3e44c43346d508b2e55a6254b6e9dMike Stump    Int32Ty,
4050af7b44d847d3e44c43346d508b2e55a6254b6e9dMike Stump    getPointerType(VoidPtrTy),
4051af7b44d847d3e44c43346d508b2e55a6254b6e9dMike Stump    getPointerType(VoidPtrTy),
4052af7b44d847d3e44c43346d508b2e55a6254b6e9dMike Stump    Ty
4053af7b44d847d3e44c43346d508b2e55a6254b6e9dMike Stump  };
4054af7b44d847d3e44c43346d508b2e55a6254b6e9dMike Stump
40555f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner  StringRef FieldNames[] = {
4056af7b44d847d3e44c43346d508b2e55a6254b6e9dMike Stump    "__isa",
4057af7b44d847d3e44c43346d508b2e55a6254b6e9dMike Stump    "__forwarding",
4058af7b44d847d3e44c43346d508b2e55a6254b6e9dMike Stump    "__flags",
4059af7b44d847d3e44c43346d508b2e55a6254b6e9dMike Stump    "__size",
4060af7b44d847d3e44c43346d508b2e55a6254b6e9dMike Stump    "__copy_helper",
4061af7b44d847d3e44c43346d508b2e55a6254b6e9dMike Stump    "__destroy_helper",
4062af7b44d847d3e44c43346d508b2e55a6254b6e9dMike Stump    DeclName,
4063af7b44d847d3e44c43346d508b2e55a6254b6e9dMike Stump  };
4064af7b44d847d3e44c43346d508b2e55a6254b6e9dMike Stump
4065af7b44d847d3e44c43346d508b2e55a6254b6e9dMike Stump  for (size_t i = 0; i < 7; ++i) {
4066af7b44d847d3e44c43346d508b2e55a6254b6e9dMike Stump    if (!HasCopyAndDispose && i >=4 && i <= 5)
4067af7b44d847d3e44c43346d508b2e55a6254b6e9dMike Stump      continue;
4068af7b44d847d3e44c43346d508b2e55a6254b6e9dMike Stump    FieldDecl *Field = FieldDecl::Create(*this, T, SourceLocation(),
4069ff676cb48fe8bf7be2feaa251dc7c5fb15af4730Abramo Bagnara                                         SourceLocation(),
4070af7b44d847d3e44c43346d508b2e55a6254b6e9dMike Stump                                         &Idents.get(FieldNames[i]),
4071a93c934af4fbf97cbe8e649d82e68ccacfe57c95John McCall                                         FieldTypes[i], /*TInfo=*/0,
40727a614d8380297fcd2bc23986241905d97222948cRichard Smith                                         /*BitWidth=*/0, /*Mutable=*/false,
4073ca5233044ef679840d1ad1c46a36b16e2ee8a6e1Richard Smith                                         ICIS_NoInit);
40742888b65aae768f54062505330df7be230a0510c7John McCall    Field->setAccess(AS_public);
4075af7b44d847d3e44c43346d508b2e55a6254b6e9dMike Stump    T->addDecl(Field);
4076af7b44d847d3e44c43346d508b2e55a6254b6e9dMike Stump  }
4077af7b44d847d3e44c43346d508b2e55a6254b6e9dMike Stump
4078838db383b69b9fb55f55c8e9546477df198a4faaDouglas Gregor  T->completeDefinition();
4079af7b44d847d3e44c43346d508b2e55a6254b6e9dMike Stump
4080af7b44d847d3e44c43346d508b2e55a6254b6e9dMike Stump  return getPointerType(getTagDeclType(T));
4081ea26cb522e88fc86b0d1cae61dcefcfe4cc20231Mike Stump}
4082ea26cb522e88fc86b0d1cae61dcefcfe4cc20231Mike Stump
4083e97179c675b341927807c718be215c8d1aab8acbDouglas GregorTypedefDecl *ASTContext::getObjCInstanceTypeDecl() {
4084e97179c675b341927807c718be215c8d1aab8acbDouglas Gregor  if (!ObjCInstanceTypeDecl)
4085e97179c675b341927807c718be215c8d1aab8acbDouglas Gregor    ObjCInstanceTypeDecl = TypedefDecl::Create(*this,
4086e97179c675b341927807c718be215c8d1aab8acbDouglas Gregor                                               getTranslationUnitDecl(),
4087e97179c675b341927807c718be215c8d1aab8acbDouglas Gregor                                               SourceLocation(),
4088e97179c675b341927807c718be215c8d1aab8acbDouglas Gregor                                               SourceLocation(),
4089e97179c675b341927807c718be215c8d1aab8acbDouglas Gregor                                               &Idents.get("instancetype"),
4090e97179c675b341927807c718be215c8d1aab8acbDouglas Gregor                                     getTrivialTypeSourceInfo(getObjCIdType()));
4091e97179c675b341927807c718be215c8d1aab8acbDouglas Gregor  return ObjCInstanceTypeDecl;
4092e97179c675b341927807c718be215c8d1aab8acbDouglas Gregor}
4093e97179c675b341927807c718be215c8d1aab8acbDouglas Gregor
4094e8c49533521c40643653f943d47229e62d277f88Anders Carlsson// This returns true if a type has been typedefed to BOOL:
4095e8c49533521c40643653f943d47229e62d277f88Anders Carlsson// typedef <type> BOOL;
40962d99833e8c956775f2183601cd120b65b569c867Chris Lattnerstatic bool isTypeTypedefedAsBOOL(QualType T) {
4097e8c49533521c40643653f943d47229e62d277f88Anders Carlsson  if (const TypedefType *TT = dyn_cast<TypedefType>(T))
4098bb49c3ee5d270485f4b273691fd14bc97403fa5dChris Lattner    if (IdentifierInfo *II = TT->getDecl()->getIdentifier())
4099bb49c3ee5d270485f4b273691fd14bc97403fa5dChris Lattner      return II->isStr("BOOL");
41001eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
410185f9bceab1542aafff012d4d28e998f4ba16e362Anders Carlsson  return false;
410285f9bceab1542aafff012d4d28e998f4ba16e362Anders Carlsson}
410385f9bceab1542aafff012d4d28e998f4ba16e362Anders Carlsson
4104a526c5c67e5a0473c340903ee542ce570119665fTed Kremenek/// getObjCEncodingTypeSize returns size of type for objective-c encoding
410533e1d64ab5cd5d27f8530ccd056191fe2c9f3f2eFariborz Jahanian/// purpose.
41064ba2a17694148e16eaa8d3917f657ffcd3667be4Jay FoadCharUnits ASTContext::getObjCEncodingTypeSize(QualType type) const {
4107f968d8374791c37bc464efd9168c2d33dd73605fDouglas Gregor  if (!type->isIncompleteArrayType() && type->isIncompleteType())
4108f968d8374791c37bc464efd9168c2d33dd73605fDouglas Gregor    return CharUnits::Zero();
4109f968d8374791c37bc464efd9168c2d33dd73605fDouglas Gregor
4110199c3d6cd16aebbb9c7f0d42af9d922c9628bf70Ken Dyck  CharUnits sz = getTypeSizeInChars(type);
41111eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
411233e1d64ab5cd5d27f8530ccd056191fe2c9f3f2eFariborz Jahanian  // Make all integer and enum types at least as large as an int
41132ade35e2cfd554e49d35a52047cea98a82787af9Douglas Gregor  if (sz.isPositive() && type->isIntegralOrEnumerationType())
4114199c3d6cd16aebbb9c7f0d42af9d922c9628bf70Ken Dyck    sz = std::max(sz, getTypeSizeInChars(IntTy));
411533e1d64ab5cd5d27f8530ccd056191fe2c9f3f2eFariborz Jahanian  // Treat arrays as pointers, since that's how they're passed in.
411633e1d64ab5cd5d27f8530ccd056191fe2c9f3f2eFariborz Jahanian  else if (type->isArrayType())
4117199c3d6cd16aebbb9c7f0d42af9d922c9628bf70Ken Dyck    sz = getTypeSizeInChars(VoidPtrTy);
4118aa8741a1db98eef05f09b1200dba94aa5dc3bc3dKen Dyck  return sz;
4119199c3d6cd16aebbb9c7f0d42af9d922c9628bf70Ken Dyck}
4120199c3d6cd16aebbb9c7f0d42af9d922c9628bf70Ken Dyck
4121199c3d6cd16aebbb9c7f0d42af9d922c9628bf70Ken Dyckstatic inline
4122199c3d6cd16aebbb9c7f0d42af9d922c9628bf70Ken Dyckstd::string charUnitsToString(const CharUnits &CU) {
4123199c3d6cd16aebbb9c7f0d42af9d922c9628bf70Ken Dyck  return llvm::itostr(CU.getQuantity());
412433e1d64ab5cd5d27f8530ccd056191fe2c9f3f2eFariborz Jahanian}
412533e1d64ab5cd5d27f8530ccd056191fe2c9f3f2eFariborz Jahanian
41266b5a61b6dc400027fd793dcadceeb9da944a37eaJohn McCall/// getObjCEncodingForBlock - Return the encoded type for this block
41275e530af5d51572a0ed5dbe50da54bd333840c63dDavid Chisnall/// declaration.
41286b5a61b6dc400027fd793dcadceeb9da944a37eaJohn McCallstd::string ASTContext::getObjCEncodingForBlock(const BlockExpr *Expr) const {
41296b5a61b6dc400027fd793dcadceeb9da944a37eaJohn McCall  std::string S;
41306b5a61b6dc400027fd793dcadceeb9da944a37eaJohn McCall
41315e530af5d51572a0ed5dbe50da54bd333840c63dDavid Chisnall  const BlockDecl *Decl = Expr->getBlockDecl();
41325e530af5d51572a0ed5dbe50da54bd333840c63dDavid Chisnall  QualType BlockTy =
41335e530af5d51572a0ed5dbe50da54bd333840c63dDavid Chisnall      Expr->getType()->getAs<BlockPointerType>()->getPointeeType();
41345e530af5d51572a0ed5dbe50da54bd333840c63dDavid Chisnall  // Encode result type.
4135c71a4915ca216847599d03cab4ed1c5086b0eb43John McCall  getObjCEncodingForType(BlockTy->getAs<FunctionType>()->getResultType(), S);
41365e530af5d51572a0ed5dbe50da54bd333840c63dDavid Chisnall  // Compute size of all parameters.
41375e530af5d51572a0ed5dbe50da54bd333840c63dDavid Chisnall  // Start with computing size of a pointer in number of bytes.
41385e530af5d51572a0ed5dbe50da54bd333840c63dDavid Chisnall  // FIXME: There might(should) be a better way of doing this computation!
41395e530af5d51572a0ed5dbe50da54bd333840c63dDavid Chisnall  SourceLocation Loc;
4140199c3d6cd16aebbb9c7f0d42af9d922c9628bf70Ken Dyck  CharUnits PtrSize = getTypeSizeInChars(VoidPtrTy);
4141199c3d6cd16aebbb9c7f0d42af9d922c9628bf70Ken Dyck  CharUnits ParmOffset = PtrSize;
41426f46c2653c1545cc3fef0c0df996d18160160ce8Fariborz Jahanian  for (BlockDecl::param_const_iterator PI = Decl->param_begin(),
41435e530af5d51572a0ed5dbe50da54bd333840c63dDavid Chisnall       E = Decl->param_end(); PI != E; ++PI) {
41445e530af5d51572a0ed5dbe50da54bd333840c63dDavid Chisnall    QualType PType = (*PI)->getType();
4145aa8741a1db98eef05f09b1200dba94aa5dc3bc3dKen Dyck    CharUnits sz = getObjCEncodingTypeSize(PType);
4146199c3d6cd16aebbb9c7f0d42af9d922c9628bf70Ken Dyck    assert (sz.isPositive() && "BlockExpr - Incomplete param type");
41475e530af5d51572a0ed5dbe50da54bd333840c63dDavid Chisnall    ParmOffset += sz;
41485e530af5d51572a0ed5dbe50da54bd333840c63dDavid Chisnall  }
41495e530af5d51572a0ed5dbe50da54bd333840c63dDavid Chisnall  // Size of the argument frame
4150199c3d6cd16aebbb9c7f0d42af9d922c9628bf70Ken Dyck  S += charUnitsToString(ParmOffset);
41515e530af5d51572a0ed5dbe50da54bd333840c63dDavid Chisnall  // Block pointer and offset.
41525e530af5d51572a0ed5dbe50da54bd333840c63dDavid Chisnall  S += "@?0";
41535e530af5d51572a0ed5dbe50da54bd333840c63dDavid Chisnall
41545e530af5d51572a0ed5dbe50da54bd333840c63dDavid Chisnall  // Argument types.
41555e530af5d51572a0ed5dbe50da54bd333840c63dDavid Chisnall  ParmOffset = PtrSize;
41565e530af5d51572a0ed5dbe50da54bd333840c63dDavid Chisnall  for (BlockDecl::param_const_iterator PI = Decl->param_begin(), E =
41575e530af5d51572a0ed5dbe50da54bd333840c63dDavid Chisnall       Decl->param_end(); PI != E; ++PI) {
41585e530af5d51572a0ed5dbe50da54bd333840c63dDavid Chisnall    ParmVarDecl *PVDecl = *PI;
41595e530af5d51572a0ed5dbe50da54bd333840c63dDavid Chisnall    QualType PType = PVDecl->getOriginalType();
41605e530af5d51572a0ed5dbe50da54bd333840c63dDavid Chisnall    if (const ArrayType *AT =
41615e530af5d51572a0ed5dbe50da54bd333840c63dDavid Chisnall          dyn_cast<ArrayType>(PType->getCanonicalTypeInternal())) {
41625e530af5d51572a0ed5dbe50da54bd333840c63dDavid Chisnall      // Use array's original type only if it has known number of
41635389f48b24937ad7b4093307128b3cbf25235654David Chisnall      // elements.
41645389f48b24937ad7b4093307128b3cbf25235654David Chisnall      if (!isa<ConstantArrayType>(AT))
41655389f48b24937ad7b4093307128b3cbf25235654David Chisnall        PType = PVDecl->getType();
41665389f48b24937ad7b4093307128b3cbf25235654David Chisnall    } else if (PType->isFunctionType())
41675389f48b24937ad7b4093307128b3cbf25235654David Chisnall      PType = PVDecl->getType();
41685389f48b24937ad7b4093307128b3cbf25235654David Chisnall    getObjCEncodingForType(PType, S);
41695389f48b24937ad7b4093307128b3cbf25235654David Chisnall    S += charUnitsToString(ParmOffset);
41705389f48b24937ad7b4093307128b3cbf25235654David Chisnall    ParmOffset += getObjCEncodingTypeSize(PType);
41715389f48b24937ad7b4093307128b3cbf25235654David Chisnall  }
41726b5a61b6dc400027fd793dcadceeb9da944a37eaJohn McCall
41736b5a61b6dc400027fd793dcadceeb9da944a37eaJohn McCall  return S;
41745389f48b24937ad7b4093307128b3cbf25235654David Chisnall}
41755389f48b24937ad7b4093307128b3cbf25235654David Chisnall
4176f968d8374791c37bc464efd9168c2d33dd73605fDouglas Gregorbool ASTContext::getObjCEncodingForFunctionDecl(const FunctionDecl *Decl,
41775389f48b24937ad7b4093307128b3cbf25235654David Chisnall                                                std::string& S) {
41785389f48b24937ad7b4093307128b3cbf25235654David Chisnall  // Encode result type.
41795389f48b24937ad7b4093307128b3cbf25235654David Chisnall  getObjCEncodingForType(Decl->getResultType(), S);
41805389f48b24937ad7b4093307128b3cbf25235654David Chisnall  CharUnits ParmOffset;
41815389f48b24937ad7b4093307128b3cbf25235654David Chisnall  // Compute size of all parameters.
41825389f48b24937ad7b4093307128b3cbf25235654David Chisnall  for (FunctionDecl::param_const_iterator PI = Decl->param_begin(),
41835389f48b24937ad7b4093307128b3cbf25235654David Chisnall       E = Decl->param_end(); PI != E; ++PI) {
41845389f48b24937ad7b4093307128b3cbf25235654David Chisnall    QualType PType = (*PI)->getType();
41855389f48b24937ad7b4093307128b3cbf25235654David Chisnall    CharUnits sz = getObjCEncodingTypeSize(PType);
4186f968d8374791c37bc464efd9168c2d33dd73605fDouglas Gregor    if (sz.isZero())
4187f968d8374791c37bc464efd9168c2d33dd73605fDouglas Gregor      return true;
4188f968d8374791c37bc464efd9168c2d33dd73605fDouglas Gregor
41895389f48b24937ad7b4093307128b3cbf25235654David Chisnall    assert (sz.isPositive() &&
4190f968d8374791c37bc464efd9168c2d33dd73605fDouglas Gregor        "getObjCEncodingForFunctionDecl - Incomplete param type");
41915389f48b24937ad7b4093307128b3cbf25235654David Chisnall    ParmOffset += sz;
41925389f48b24937ad7b4093307128b3cbf25235654David Chisnall  }
41935389f48b24937ad7b4093307128b3cbf25235654David Chisnall  S += charUnitsToString(ParmOffset);
41945389f48b24937ad7b4093307128b3cbf25235654David Chisnall  ParmOffset = CharUnits::Zero();
41955389f48b24937ad7b4093307128b3cbf25235654David Chisnall
41965389f48b24937ad7b4093307128b3cbf25235654David Chisnall  // Argument types.
41975389f48b24937ad7b4093307128b3cbf25235654David Chisnall  for (FunctionDecl::param_const_iterator PI = Decl->param_begin(),
41985389f48b24937ad7b4093307128b3cbf25235654David Chisnall       E = Decl->param_end(); PI != E; ++PI) {
41995389f48b24937ad7b4093307128b3cbf25235654David Chisnall    ParmVarDecl *PVDecl = *PI;
42005389f48b24937ad7b4093307128b3cbf25235654David Chisnall    QualType PType = PVDecl->getOriginalType();
42015389f48b24937ad7b4093307128b3cbf25235654David Chisnall    if (const ArrayType *AT =
42025389f48b24937ad7b4093307128b3cbf25235654David Chisnall          dyn_cast<ArrayType>(PType->getCanonicalTypeInternal())) {
42035389f48b24937ad7b4093307128b3cbf25235654David Chisnall      // Use array's original type only if it has known number of
42045e530af5d51572a0ed5dbe50da54bd333840c63dDavid Chisnall      // elements.
42055e530af5d51572a0ed5dbe50da54bd333840c63dDavid Chisnall      if (!isa<ConstantArrayType>(AT))
42065e530af5d51572a0ed5dbe50da54bd333840c63dDavid Chisnall        PType = PVDecl->getType();
42075e530af5d51572a0ed5dbe50da54bd333840c63dDavid Chisnall    } else if (PType->isFunctionType())
42085e530af5d51572a0ed5dbe50da54bd333840c63dDavid Chisnall      PType = PVDecl->getType();
42095e530af5d51572a0ed5dbe50da54bd333840c63dDavid Chisnall    getObjCEncodingForType(PType, S);
4210199c3d6cd16aebbb9c7f0d42af9d922c9628bf70Ken Dyck    S += charUnitsToString(ParmOffset);
4211aa8741a1db98eef05f09b1200dba94aa5dc3bc3dKen Dyck    ParmOffset += getObjCEncodingTypeSize(PType);
42125e530af5d51572a0ed5dbe50da54bd333840c63dDavid Chisnall  }
4213f968d8374791c37bc464efd9168c2d33dd73605fDouglas Gregor
4214f968d8374791c37bc464efd9168c2d33dd73605fDouglas Gregor  return false;
42155e530af5d51572a0ed5dbe50da54bd333840c63dDavid Chisnall}
42165e530af5d51572a0ed5dbe50da54bd333840c63dDavid Chisnall
4217dc8dab6fabf4bfd4f4b94bf572ac3342a5bbfcd7Bob Wilson/// getObjCEncodingForMethodParameter - Return the encoded type for a single
4218dc8dab6fabf4bfd4f4b94bf572ac3342a5bbfcd7Bob Wilson/// method parameter or return type. If Extended, include class names and
4219dc8dab6fabf4bfd4f4b94bf572ac3342a5bbfcd7Bob Wilson/// block object types.
4220dc8dab6fabf4bfd4f4b94bf572ac3342a5bbfcd7Bob Wilsonvoid ASTContext::getObjCEncodingForMethodParameter(Decl::ObjCDeclQualifier QT,
4221dc8dab6fabf4bfd4f4b94bf572ac3342a5bbfcd7Bob Wilson                                                   QualType T, std::string& S,
4222dc8dab6fabf4bfd4f4b94bf572ac3342a5bbfcd7Bob Wilson                                                   bool Extended) const {
4223dc8dab6fabf4bfd4f4b94bf572ac3342a5bbfcd7Bob Wilson  // Encode type qualifer, 'in', 'inout', etc. for the parameter.
4224dc8dab6fabf4bfd4f4b94bf572ac3342a5bbfcd7Bob Wilson  getObjCEncodingForTypeQualifier(QT, S);
4225dc8dab6fabf4bfd4f4b94bf572ac3342a5bbfcd7Bob Wilson  // Encode parameter type.
4226dc8dab6fabf4bfd4f4b94bf572ac3342a5bbfcd7Bob Wilson  getObjCEncodingForTypeImpl(T, S, true, true, 0,
4227dc8dab6fabf4bfd4f4b94bf572ac3342a5bbfcd7Bob Wilson                             true     /*OutermostType*/,
4228dc8dab6fabf4bfd4f4b94bf572ac3342a5bbfcd7Bob Wilson                             false    /*EncodingProperty*/,
4229dc8dab6fabf4bfd4f4b94bf572ac3342a5bbfcd7Bob Wilson                             false    /*StructField*/,
4230dc8dab6fabf4bfd4f4b94bf572ac3342a5bbfcd7Bob Wilson                             Extended /*EncodeBlockParameters*/,
4231dc8dab6fabf4bfd4f4b94bf572ac3342a5bbfcd7Bob Wilson                             Extended /*EncodeClassNames*/);
4232dc8dab6fabf4bfd4f4b94bf572ac3342a5bbfcd7Bob Wilson}
4233dc8dab6fabf4bfd4f4b94bf572ac3342a5bbfcd7Bob Wilson
4234a526c5c67e5a0473c340903ee542ce570119665fTed Kremenek/// getObjCEncodingForMethodDecl - Return the encoded type for this method
423533e1d64ab5cd5d27f8530ccd056191fe2c9f3f2eFariborz Jahanian/// declaration.
4236f968d8374791c37bc464efd9168c2d33dd73605fDouglas Gregorbool ASTContext::getObjCEncodingForMethodDecl(const ObjCMethodDecl *Decl,
4237dc8dab6fabf4bfd4f4b94bf572ac3342a5bbfcd7Bob Wilson                                              std::string& S,
4238dc8dab6fabf4bfd4f4b94bf572ac3342a5bbfcd7Bob Wilson                                              bool Extended) const {
4239c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar  // FIXME: This is not very efficient.
4240dc8dab6fabf4bfd4f4b94bf572ac3342a5bbfcd7Bob Wilson  // Encode return type.
4241dc8dab6fabf4bfd4f4b94bf572ac3342a5bbfcd7Bob Wilson  getObjCEncodingForMethodParameter(Decl->getObjCDeclQualifier(),
4242dc8dab6fabf4bfd4f4b94bf572ac3342a5bbfcd7Bob Wilson                                    Decl->getResultType(), S, Extended);
424333e1d64ab5cd5d27f8530ccd056191fe2c9f3f2eFariborz Jahanian  // Compute size of all parameters.
424433e1d64ab5cd5d27f8530ccd056191fe2c9f3f2eFariborz Jahanian  // Start with computing size of a pointer in number of bytes.
424533e1d64ab5cd5d27f8530ccd056191fe2c9f3f2eFariborz Jahanian  // FIXME: There might(should) be a better way of doing this computation!
424633e1d64ab5cd5d27f8530ccd056191fe2c9f3f2eFariborz Jahanian  SourceLocation Loc;
4247199c3d6cd16aebbb9c7f0d42af9d922c9628bf70Ken Dyck  CharUnits PtrSize = getTypeSizeInChars(VoidPtrTy);
424833e1d64ab5cd5d27f8530ccd056191fe2c9f3f2eFariborz Jahanian  // The first two arguments (self and _cmd) are pointers; account for
424933e1d64ab5cd5d27f8530ccd056191fe2c9f3f2eFariborz Jahanian  // their size.
4250199c3d6cd16aebbb9c7f0d42af9d922c9628bf70Ken Dyck  CharUnits ParmOffset = 2 * PtrSize;
4251491306a83c4f0f49f95a3bcbca8580cb98a91c7aArgyrios Kyrtzidis  for (ObjCMethodDecl::param_const_iterator PI = Decl->param_begin(),
42527732cc9c0fdc97a2f8cce4e5933d8103213d1aefFariborz Jahanian       E = Decl->sel_param_end(); PI != E; ++PI) {
425389951a86b594513c2a013532ed45d197413b1087Chris Lattner    QualType PType = (*PI)->getType();
4254aa8741a1db98eef05f09b1200dba94aa5dc3bc3dKen Dyck    CharUnits sz = getObjCEncodingTypeSize(PType);
4255f968d8374791c37bc464efd9168c2d33dd73605fDouglas Gregor    if (sz.isZero())
4256f968d8374791c37bc464efd9168c2d33dd73605fDouglas Gregor      return true;
4257f968d8374791c37bc464efd9168c2d33dd73605fDouglas Gregor
4258199c3d6cd16aebbb9c7f0d42af9d922c9628bf70Ken Dyck    assert (sz.isPositive() &&
4259199c3d6cd16aebbb9c7f0d42af9d922c9628bf70Ken Dyck        "getObjCEncodingForMethodDecl - Incomplete param type");
426033e1d64ab5cd5d27f8530ccd056191fe2c9f3f2eFariborz Jahanian    ParmOffset += sz;
426133e1d64ab5cd5d27f8530ccd056191fe2c9f3f2eFariborz Jahanian  }
4262199c3d6cd16aebbb9c7f0d42af9d922c9628bf70Ken Dyck  S += charUnitsToString(ParmOffset);
426333e1d64ab5cd5d27f8530ccd056191fe2c9f3f2eFariborz Jahanian  S += "@0:";
4264199c3d6cd16aebbb9c7f0d42af9d922c9628bf70Ken Dyck  S += charUnitsToString(PtrSize);
42651eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
426633e1d64ab5cd5d27f8530ccd056191fe2c9f3f2eFariborz Jahanian  // Argument types.
426733e1d64ab5cd5d27f8530ccd056191fe2c9f3f2eFariborz Jahanian  ParmOffset = 2 * PtrSize;
4268491306a83c4f0f49f95a3bcbca8580cb98a91c7aArgyrios Kyrtzidis  for (ObjCMethodDecl::param_const_iterator PI = Decl->param_begin(),
42697732cc9c0fdc97a2f8cce4e5933d8103213d1aefFariborz Jahanian       E = Decl->sel_param_end(); PI != E; ++PI) {
4270491306a83c4f0f49f95a3bcbca8580cb98a91c7aArgyrios Kyrtzidis    const ParmVarDecl *PVDecl = *PI;
42711eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    QualType PType = PVDecl->getOriginalType();
42724306d3cb9116605728252e2738df24b9f6ab53c3Fariborz Jahanian    if (const ArrayType *AT =
4273ab76d45e023fc5ae966968344e180cd09fdcc746Steve Naroff          dyn_cast<ArrayType>(PType->getCanonicalTypeInternal())) {
4274ab76d45e023fc5ae966968344e180cd09fdcc746Steve Naroff      // Use array's original type only if it has known number of
4275ab76d45e023fc5ae966968344e180cd09fdcc746Steve Naroff      // elements.
4276bb3fde337fb712c0e6da8790d431621be4793048Steve Naroff      if (!isa<ConstantArrayType>(AT))
4277ab76d45e023fc5ae966968344e180cd09fdcc746Steve Naroff        PType = PVDecl->getType();
4278ab76d45e023fc5ae966968344e180cd09fdcc746Steve Naroff    } else if (PType->isFunctionType())
4279ab76d45e023fc5ae966968344e180cd09fdcc746Steve Naroff      PType = PVDecl->getType();
4280dc8dab6fabf4bfd4f4b94bf572ac3342a5bbfcd7Bob Wilson    getObjCEncodingForMethodParameter(PVDecl->getObjCDeclQualifier(),
4281dc8dab6fabf4bfd4f4b94bf572ac3342a5bbfcd7Bob Wilson                                      PType, S, Extended);
4282199c3d6cd16aebbb9c7f0d42af9d922c9628bf70Ken Dyck    S += charUnitsToString(ParmOffset);
4283aa8741a1db98eef05f09b1200dba94aa5dc3bc3dKen Dyck    ParmOffset += getObjCEncodingTypeSize(PType);
428433e1d64ab5cd5d27f8530ccd056191fe2c9f3f2eFariborz Jahanian  }
4285f968d8374791c37bc464efd9168c2d33dd73605fDouglas Gregor
4286f968d8374791c37bc464efd9168c2d33dd73605fDouglas Gregor  return false;
428733e1d64ab5cd5d27f8530ccd056191fe2c9f3f2eFariborz Jahanian}
428833e1d64ab5cd5d27f8530ccd056191fe2c9f3f2eFariborz Jahanian
4289c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar/// getObjCEncodingForPropertyDecl - Return the encoded type for this
429083bccb85ff4b9981c4250c45494b439df8cbf983Fariborz Jahanian/// property declaration. If non-NULL, Container must be either an
4291c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar/// ObjCCategoryImplDecl or ObjCImplementationDecl; it should only be
4292c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar/// NULL when getting encodings for protocol properties.
42931eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// Property attributes are stored as a comma-delimited C string. The simple
42941eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// attributes readonly and bycopy are encoded as single characters. The
42951eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// parametrized attributes, getter=name, setter=name, and ivar=name, are
42961eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// encoded as single characters, followed by an identifier. Property types
42971eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// are also encoded as a parametrized attribute. The characters used to encode
429883bccb85ff4b9981c4250c45494b439df8cbf983Fariborz Jahanian/// these attributes are defined by the following enumeration:
429983bccb85ff4b9981c4250c45494b439df8cbf983Fariborz Jahanian/// @code
430083bccb85ff4b9981c4250c45494b439df8cbf983Fariborz Jahanian/// enum PropertyAttributes {
430183bccb85ff4b9981c4250c45494b439df8cbf983Fariborz Jahanian/// kPropertyReadOnly = 'R',   // property is read-only.
430283bccb85ff4b9981c4250c45494b439df8cbf983Fariborz Jahanian/// kPropertyBycopy = 'C',     // property is a copy of the value last assigned
430383bccb85ff4b9981c4250c45494b439df8cbf983Fariborz Jahanian/// kPropertyByref = '&',  // property is a reference to the value last assigned
430483bccb85ff4b9981c4250c45494b439df8cbf983Fariborz Jahanian/// kPropertyDynamic = 'D',    // property is dynamic
430583bccb85ff4b9981c4250c45494b439df8cbf983Fariborz Jahanian/// kPropertyGetter = 'G',     // followed by getter selector name
430683bccb85ff4b9981c4250c45494b439df8cbf983Fariborz Jahanian/// kPropertySetter = 'S',     // followed by setter selector name
430783bccb85ff4b9981c4250c45494b439df8cbf983Fariborz Jahanian/// kPropertyInstanceVariable = 'V'  // followed by instance variable  name
43080d4cb85130d91da61c45aecb9fd31c7097a7cfccBob Wilson/// kPropertyType = 'T'              // followed by old-style type encoding.
430983bccb85ff4b9981c4250c45494b439df8cbf983Fariborz Jahanian/// kPropertyWeak = 'W'              // 'weak' property
431083bccb85ff4b9981c4250c45494b439df8cbf983Fariborz Jahanian/// kPropertyStrong = 'P'            // property GC'able
431183bccb85ff4b9981c4250c45494b439df8cbf983Fariborz Jahanian/// kPropertyNonAtomic = 'N'         // property non-atomic
431283bccb85ff4b9981c4250c45494b439df8cbf983Fariborz Jahanian/// };
431383bccb85ff4b9981c4250c45494b439df8cbf983Fariborz Jahanian/// @endcode
43141eb4433ac451dc16f4133a88af2d002ac26c58efMike Stumpvoid ASTContext::getObjCEncodingForPropertyDecl(const ObjCPropertyDecl *PD,
4315c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar                                                const Decl *Container,
43164ba2a17694148e16eaa8d3917f657ffcd3667be4Jay Foad                                                std::string& S) const {
4317c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar  // Collect information from the property implementation decl(s).
4318c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar  bool Dynamic = false;
4319c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar  ObjCPropertyImplDecl *SynthesizePID = 0;
4320c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar
4321c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar  // FIXME: Duplicated code due to poor abstraction.
4322c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar  if (Container) {
43231eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    if (const ObjCCategoryImplDecl *CID =
4324c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar        dyn_cast<ObjCCategoryImplDecl>(Container)) {
4325c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar      for (ObjCCategoryImplDecl::propimpl_iterator
432617945a0f64fe03ff6ec0c2146005a87636e3ac12Argyrios Kyrtzidis             i = CID->propimpl_begin(), e = CID->propimpl_end();
4327653f1b1bf293a9bd96fd4dd6372e779cc7af1597Douglas Gregor           i != e; ++i) {
4328581deb3da481053c4993c7600f97acf7768caac5David Blaikie        ObjCPropertyImplDecl *PID = *i;
4329c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar        if (PID->getPropertyDecl() == PD) {
4330c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar          if (PID->getPropertyImplementation()==ObjCPropertyImplDecl::Dynamic) {
4331c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar            Dynamic = true;
4332c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar          } else {
4333c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar            SynthesizePID = PID;
4334c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar          }
4335c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar        }
4336c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar      }
4337c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar    } else {
433861710854be2b098428aff5316e64bd34b30fbcb7Chris Lattner      const ObjCImplementationDecl *OID=cast<ObjCImplementationDecl>(Container);
4339c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar      for (ObjCCategoryImplDecl::propimpl_iterator
434017945a0f64fe03ff6ec0c2146005a87636e3ac12Argyrios Kyrtzidis             i = OID->propimpl_begin(), e = OID->propimpl_end();
4341653f1b1bf293a9bd96fd4dd6372e779cc7af1597Douglas Gregor           i != e; ++i) {
4342581deb3da481053c4993c7600f97acf7768caac5David Blaikie        ObjCPropertyImplDecl *PID = *i;
4343c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar        if (PID->getPropertyDecl() == PD) {
4344c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar          if (PID->getPropertyImplementation()==ObjCPropertyImplDecl::Dynamic) {
4345c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar            Dynamic = true;
4346c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar          } else {
4347c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar            SynthesizePID = PID;
4348c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar          }
4349c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar        }
43501eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      }
4351c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar    }
4352c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar  }
4353c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar
4354c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar  // FIXME: This is not very efficient.
4355c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar  S = "T";
4356c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar
4357c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar  // Encode result type.
4358090b3f71702c5626d8520f9608d77c6f26dcfa15Fariborz Jahanian  // GCC has some special rules regarding encoding of properties which
4359090b3f71702c5626d8520f9608d77c6f26dcfa15Fariborz Jahanian  // closely resembles encoding of ivars.
43601eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  getObjCEncodingForTypeImpl(PD->getType(), S, true, true, 0,
4361090b3f71702c5626d8520f9608d77c6f26dcfa15Fariborz Jahanian                             true /* outermost type */,
4362090b3f71702c5626d8520f9608d77c6f26dcfa15Fariborz Jahanian                             true /* encoding for property */);
4363c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar
4364c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar  if (PD->isReadOnly()) {
4365c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar    S += ",R";
4366c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar  } else {
4367c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar    switch (PD->getSetterKind()) {
4368c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar    case ObjCPropertyDecl::Assign: break;
4369c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar    case ObjCPropertyDecl::Copy:   S += ",C"; break;
43701eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    case ObjCPropertyDecl::Retain: S += ",&"; break;
43713a02b44e3948f7762dbfba94b7961281ca29d022Fariborz Jahanian    case ObjCPropertyDecl::Weak:   S += ",W"; break;
4372c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar    }
4373c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar  }
4374c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar
4375c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar  // It really isn't clear at all what this means, since properties
4376c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar  // are "dynamic by default".
4377c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar  if (Dynamic)
4378c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar    S += ",D";
4379c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar
4380090b3f71702c5626d8520f9608d77c6f26dcfa15Fariborz Jahanian  if (PD->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_nonatomic)
4381090b3f71702c5626d8520f9608d77c6f26dcfa15Fariborz Jahanian    S += ",N";
43821eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
4383c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar  if (PD->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_getter) {
4384c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar    S += ",G";
4385077bf5e2f48acfa9e7d69429b6e4ba86ea14896dChris Lattner    S += PD->getGetterName().getAsString();
4386c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar  }
4387c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar
4388c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar  if (PD->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_setter) {
4389c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar    S += ",S";
4390077bf5e2f48acfa9e7d69429b6e4ba86ea14896dChris Lattner    S += PD->getSetterName().getAsString();
4391c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar  }
4392c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar
4393c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar  if (SynthesizePID) {
4394c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar    const ObjCIvarDecl *OID = SynthesizePID->getPropertyIvarDecl();
4395c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar    S += ",V";
439639f34e97d6a468f0a7dfa5664c61217cffc65b74Chris Lattner    S += OID->getNameAsString();
4397c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar  }
4398c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar
4399c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar  // FIXME: OBJCGC: weak & strong
4400c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar}
4401c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar
4402a1c033e9514865f3a7b0d8b3b20e6de926cfec6cFariborz Jahanian/// getLegacyIntegralTypeEncoding -
44031eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// Another legacy compatibility encoding: 32-bit longs are encoded as
44041eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// 'l' or 'L' , but not always.  For typedefs, we need to use
4405a1c033e9514865f3a7b0d8b3b20e6de926cfec6cFariborz Jahanian/// 'i' or 'I' instead if encoding a struct field, or a pointer!
4406a1c033e9514865f3a7b0d8b3b20e6de926cfec6cFariborz Jahanian///
4407a1c033e9514865f3a7b0d8b3b20e6de926cfec6cFariborz Jahanianvoid ASTContext::getLegacyIntegralTypeEncoding (QualType &PointeeTy) const {
44088e1fab243ab8023b7ee3899745386b3b3a4258f8Mike Stump  if (isa<TypedefType>(PointeeTy.getTypePtr())) {
4409183700f494ec9b6701b6efe82bcb25f4c79ba561John McCall    if (const BuiltinType *BT = PointeeTy->getAs<BuiltinType>()) {
44104ba2a17694148e16eaa8d3917f657ffcd3667be4Jay Foad      if (BT->getKind() == BuiltinType::ULong && getIntWidth(PointeeTy) == 32)
4411a1c033e9514865f3a7b0d8b3b20e6de926cfec6cFariborz Jahanian        PointeeTy = UnsignedIntTy;
44121eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      else
44134ba2a17694148e16eaa8d3917f657ffcd3667be4Jay Foad        if (BT->getKind() == BuiltinType::Long && getIntWidth(PointeeTy) == 32)
4414a1c033e9514865f3a7b0d8b3b20e6de926cfec6cFariborz Jahanian          PointeeTy = IntTy;
4415a1c033e9514865f3a7b0d8b3b20e6de926cfec6cFariborz Jahanian    }
4416a1c033e9514865f3a7b0d8b3b20e6de926cfec6cFariborz Jahanian  }
4417a1c033e9514865f3a7b0d8b3b20e6de926cfec6cFariborz Jahanian}
4418a1c033e9514865f3a7b0d8b3b20e6de926cfec6cFariborz Jahanian
44197d6b46d9a9d75dea8ef9f6973dd50633c1f37963Fariborz Jahanianvoid ASTContext::getObjCEncodingForType(QualType T, std::string& S,
44204ba2a17694148e16eaa8d3917f657ffcd3667be4Jay Foad                                        const FieldDecl *Field) const {
442182a6cfbc421cc99c5b7313271f399f7ef95056ecDaniel Dunbar  // We follow the behavior of gcc, expanding structures which are
442282a6cfbc421cc99c5b7313271f399f7ef95056ecDaniel Dunbar  // directly pointed to, and expanding embedded structures. Note that
442382a6cfbc421cc99c5b7313271f399f7ef95056ecDaniel Dunbar  // these rules are sufficient to prevent recursive encoding of the
442482a6cfbc421cc99c5b7313271f399f7ef95056ecDaniel Dunbar  // same type.
44251eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  getObjCEncodingForTypeImpl(T, S, true, true, Field,
44265b8c7d9fb620ba3a71e996d61e7b9bdf763b5c09Fariborz Jahanian                             true /* outermost type */);
442782a6cfbc421cc99c5b7313271f399f7ef95056ecDaniel Dunbar}
442882a6cfbc421cc99c5b7313271f399f7ef95056ecDaniel Dunbar
442964fd7e86c1a90d9ff78e4a0bd79f69499667a4e3David Chisnallstatic char ObjCEncodingForPrimitiveKind(const ASTContext *C, QualType T) {
443064fd7e86c1a90d9ff78e4a0bd79f69499667a4e3David Chisnall    switch (T->getAs<BuiltinType>()->getKind()) {
4431b219cfc4d75f0a03630b7c4509ef791b7e97b2c8David Blaikie    default: llvm_unreachable("Unhandled builtin type kind");
443264fd7e86c1a90d9ff78e4a0bd79f69499667a4e3David Chisnall    case BuiltinType::Void:       return 'v';
443364fd7e86c1a90d9ff78e4a0bd79f69499667a4e3David Chisnall    case BuiltinType::Bool:       return 'B';
443464fd7e86c1a90d9ff78e4a0bd79f69499667a4e3David Chisnall    case BuiltinType::Char_U:
443564fd7e86c1a90d9ff78e4a0bd79f69499667a4e3David Chisnall    case BuiltinType::UChar:      return 'C';
443664fd7e86c1a90d9ff78e4a0bd79f69499667a4e3David Chisnall    case BuiltinType::UShort:     return 'S';
443764fd7e86c1a90d9ff78e4a0bd79f69499667a4e3David Chisnall    case BuiltinType::UInt:       return 'I';
443864fd7e86c1a90d9ff78e4a0bd79f69499667a4e3David Chisnall    case BuiltinType::ULong:
44394ba2a17694148e16eaa8d3917f657ffcd3667be4Jay Foad        return C->getIntWidth(T) == 32 ? 'L' : 'Q';
444064fd7e86c1a90d9ff78e4a0bd79f69499667a4e3David Chisnall    case BuiltinType::UInt128:    return 'T';
444164fd7e86c1a90d9ff78e4a0bd79f69499667a4e3David Chisnall    case BuiltinType::ULongLong:  return 'Q';
444264fd7e86c1a90d9ff78e4a0bd79f69499667a4e3David Chisnall    case BuiltinType::Char_S:
444364fd7e86c1a90d9ff78e4a0bd79f69499667a4e3David Chisnall    case BuiltinType::SChar:      return 'c';
444464fd7e86c1a90d9ff78e4a0bd79f69499667a4e3David Chisnall    case BuiltinType::Short:      return 's';
44453f59c975aa5d047f7edd1b900b5e885c38af0ef7Chris Lattner    case BuiltinType::WChar_S:
44463f59c975aa5d047f7edd1b900b5e885c38af0ef7Chris Lattner    case BuiltinType::WChar_U:
444764fd7e86c1a90d9ff78e4a0bd79f69499667a4e3David Chisnall    case BuiltinType::Int:        return 'i';
444864fd7e86c1a90d9ff78e4a0bd79f69499667a4e3David Chisnall    case BuiltinType::Long:
44494ba2a17694148e16eaa8d3917f657ffcd3667be4Jay Foad      return C->getIntWidth(T) == 32 ? 'l' : 'q';
445064fd7e86c1a90d9ff78e4a0bd79f69499667a4e3David Chisnall    case BuiltinType::LongLong:   return 'q';
445164fd7e86c1a90d9ff78e4a0bd79f69499667a4e3David Chisnall    case BuiltinType::Int128:     return 't';
445264fd7e86c1a90d9ff78e4a0bd79f69499667a4e3David Chisnall    case BuiltinType::Float:      return 'f';
445364fd7e86c1a90d9ff78e4a0bd79f69499667a4e3David Chisnall    case BuiltinType::Double:     return 'd';
44543a0be84b2aed8563150cdbd976a98838afa261ebDaniel Dunbar    case BuiltinType::LongDouble: return 'D';
445564fd7e86c1a90d9ff78e4a0bd79f69499667a4e3David Chisnall    }
445664fd7e86c1a90d9ff78e4a0bd79f69499667a4e3David Chisnall}
445764fd7e86c1a90d9ff78e4a0bd79f69499667a4e3David Chisnall
44585471bc85b69912e3b448de004498a80c0de32296Douglas Gregorstatic char ObjCEncodingForEnumType(const ASTContext *C, const EnumType *ET) {
44595471bc85b69912e3b448de004498a80c0de32296Douglas Gregor  EnumDecl *Enum = ET->getDecl();
44605471bc85b69912e3b448de004498a80c0de32296Douglas Gregor
44615471bc85b69912e3b448de004498a80c0de32296Douglas Gregor  // The encoding of an non-fixed enum type is always 'i', regardless of size.
44625471bc85b69912e3b448de004498a80c0de32296Douglas Gregor  if (!Enum->isFixed())
44635471bc85b69912e3b448de004498a80c0de32296Douglas Gregor    return 'i';
44645471bc85b69912e3b448de004498a80c0de32296Douglas Gregor
44655471bc85b69912e3b448de004498a80c0de32296Douglas Gregor  // The encoding of a fixed enum type matches its fixed underlying type.
44665471bc85b69912e3b448de004498a80c0de32296Douglas Gregor  return ObjCEncodingForPrimitiveKind(C, Enum->getIntegerType());
44675471bc85b69912e3b448de004498a80c0de32296Douglas Gregor}
44685471bc85b69912e3b448de004498a80c0de32296Douglas Gregor
44694ba2a17694148e16eaa8d3917f657ffcd3667be4Jay Foadstatic void EncodeBitField(const ASTContext *Ctx, std::string& S,
447064fd7e86c1a90d9ff78e4a0bd79f69499667a4e3David Chisnall                           QualType T, const FieldDecl *FD) {
4471a6b8b2c09610b8bc4330e948ece8b940c2386406Richard Smith  assert(FD->isBitField() && "not a bitfield - getObjCEncodingForTypeImpl");
44728b4bf90eb6d3d08cf3bfb86705f0fdb20b9c5875Fariborz Jahanian  S += 'b';
447364fd7e86c1a90d9ff78e4a0bd79f69499667a4e3David Chisnall  // The NeXT runtime encodes bit fields as b followed by the number of bits.
447464fd7e86c1a90d9ff78e4a0bd79f69499667a4e3David Chisnall  // The GNU runtime requires more information; bitfields are encoded as b,
447564fd7e86c1a90d9ff78e4a0bd79f69499667a4e3David Chisnall  // then the offset (in bits) of the first element, then the type of the
447664fd7e86c1a90d9ff78e4a0bd79f69499667a4e3David Chisnall  // bitfield, then the size in bits.  For example, in this structure:
447764fd7e86c1a90d9ff78e4a0bd79f69499667a4e3David Chisnall  //
447864fd7e86c1a90d9ff78e4a0bd79f69499667a4e3David Chisnall  // struct
447964fd7e86c1a90d9ff78e4a0bd79f69499667a4e3David Chisnall  // {
448064fd7e86c1a90d9ff78e4a0bd79f69499667a4e3David Chisnall  //    int integer;
448164fd7e86c1a90d9ff78e4a0bd79f69499667a4e3David Chisnall  //    int flags:2;
448264fd7e86c1a90d9ff78e4a0bd79f69499667a4e3David Chisnall  // };
448364fd7e86c1a90d9ff78e4a0bd79f69499667a4e3David Chisnall  // On a 32-bit system, the encoding for flags would be b2 for the NeXT
448464fd7e86c1a90d9ff78e4a0bd79f69499667a4e3David Chisnall  // runtime, but b32i2 for the GNU runtime.  The reason for this extra
448564fd7e86c1a90d9ff78e4a0bd79f69499667a4e3David Chisnall  // information is not especially sensible, but we're stuck with it for
448664fd7e86c1a90d9ff78e4a0bd79f69499667a4e3David Chisnall  // compatibility with GCC, although providing it breaks anything that
448764fd7e86c1a90d9ff78e4a0bd79f69499667a4e3David Chisnall  // actually uses runtime introspection and wants to work on both runtimes...
4488260611a32535c851237926bfcf78869b13c07d5bJohn McCall  if (Ctx->getLangOpts().ObjCRuntime.isGNUFamily()) {
448964fd7e86c1a90d9ff78e4a0bd79f69499667a4e3David Chisnall    const RecordDecl *RD = FD->getParent();
449064fd7e86c1a90d9ff78e4a0bd79f69499667a4e3David Chisnall    const ASTRecordLayout &RL = Ctx->getASTRecordLayout(RD);
449182905749d5c8d8b4edec11de754a73349cb96603Eli Friedman    S += llvm::utostr(RL.getFieldOffset(FD->getFieldIndex()));
44925471bc85b69912e3b448de004498a80c0de32296Douglas Gregor    if (const EnumType *ET = T->getAs<EnumType>())
44935471bc85b69912e3b448de004498a80c0de32296Douglas Gregor      S += ObjCEncodingForEnumType(Ctx, ET);
4494c7ff82c2040f45eaad2eddea0e4461dddc972cd1David Chisnall    else
44954ba2a17694148e16eaa8d3917f657ffcd3667be4Jay Foad      S += ObjCEncodingForPrimitiveKind(Ctx, T);
449664fd7e86c1a90d9ff78e4a0bd79f69499667a4e3David Chisnall  }
4497a6b8b2c09610b8bc4330e948ece8b940c2386406Richard Smith  S += llvm::utostr(FD->getBitWidthValue(*Ctx));
44988b4bf90eb6d3d08cf3bfb86705f0fdb20b9c5875Fariborz Jahanian}
44998b4bf90eb6d3d08cf3bfb86705f0fdb20b9c5875Fariborz Jahanian
450001eb9b9683535d8a65c704ad2c545903409e2d36Daniel Dunbar// FIXME: Use SmallString for accumulating string.
450182a6cfbc421cc99c5b7313271f399f7ef95056ecDaniel Dunbarvoid ASTContext::getObjCEncodingForTypeImpl(QualType T, std::string& S,
450282a6cfbc421cc99c5b7313271f399f7ef95056ecDaniel Dunbar                                            bool ExpandPointedToStructures,
450382a6cfbc421cc99c5b7313271f399f7ef95056ecDaniel Dunbar                                            bool ExpandStructures,
4504153bfe5795e2c1a5a738e73d3784964e082237fcDaniel Dunbar                                            const FieldDecl *FD,
4505090b3f71702c5626d8520f9608d77c6f26dcfa15Fariborz Jahanian                                            bool OutermostType,
45062636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis                                            bool EncodingProperty,
4507dc8dab6fabf4bfd4f4b94bf572ac3342a5bbfcd7Bob Wilson                                            bool StructField,
4508dc8dab6fabf4bfd4f4b94bf572ac3342a5bbfcd7Bob Wilson                                            bool EncodeBlockParameters,
4509dc8dab6fabf4bfd4f4b94bf572ac3342a5bbfcd7Bob Wilson                                            bool EncodeClassNames) const {
451064fd7e86c1a90d9ff78e4a0bd79f69499667a4e3David Chisnall  if (T->getAs<BuiltinType>()) {
4511ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner    if (FD && FD->isBitField())
451264fd7e86c1a90d9ff78e4a0bd79f69499667a4e3David Chisnall      return EncodeBitField(this, S, T, FD);
451364fd7e86c1a90d9ff78e4a0bd79f69499667a4e3David Chisnall    S += ObjCEncodingForPrimitiveKind(this, T);
4514ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner    return;
4515ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner  }
45161eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
4517183700f494ec9b6701b6efe82bcb25f4c79ba561John McCall  if (const ComplexType *CT = T->getAs<ComplexType>()) {
4518c612f7bc9a6379cd7e7c2dd306d05938e890051bAnders Carlsson    S += 'j';
45191eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    getObjCEncodingForTypeImpl(CT->getElementType(), S, false, false, 0, false,
4520c612f7bc9a6379cd7e7c2dd306d05938e890051bAnders Carlsson                               false);
4521ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner    return;
4522ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner  }
452360bce3ef20a4c9684e3825cdd739fefb9810327dFariborz Jahanian
4524aa1d76163e4b0b1cc54e222be67379f8c02e8ffaFariborz Jahanian  // encoding for pointer or r3eference types.
4525aa1d76163e4b0b1cc54e222be67379f8c02e8ffaFariborz Jahanian  QualType PointeeTy;
45266217b80b7a1379b74cced1c076338262c3c980b3Ted Kremenek  if (const PointerType *PT = T->getAs<PointerType>()) {
45278d2c0a9814e56c2b22e22d1045181c735aef62fdFariborz Jahanian    if (PT->isObjCSelType()) {
45288d2c0a9814e56c2b22e22d1045181c735aef62fdFariborz Jahanian      S += ':';
45298d2c0a9814e56c2b22e22d1045181c735aef62fdFariborz Jahanian      return;
45308d2c0a9814e56c2b22e22d1045181c735aef62fdFariborz Jahanian    }
4531aa1d76163e4b0b1cc54e222be67379f8c02e8ffaFariborz Jahanian    PointeeTy = PT->getPointeeType();
4532aa1d76163e4b0b1cc54e222be67379f8c02e8ffaFariborz Jahanian  }
4533aa1d76163e4b0b1cc54e222be67379f8c02e8ffaFariborz Jahanian  else if (const ReferenceType *RT = T->getAs<ReferenceType>())
4534aa1d76163e4b0b1cc54e222be67379f8c02e8ffaFariborz Jahanian    PointeeTy = RT->getPointeeType();
4535aa1d76163e4b0b1cc54e222be67379f8c02e8ffaFariborz Jahanian  if (!PointeeTy.isNull()) {
4536a1c033e9514865f3a7b0d8b3b20e6de926cfec6cFariborz Jahanian    bool isReadOnly = false;
4537a1c033e9514865f3a7b0d8b3b20e6de926cfec6cFariborz Jahanian    // For historical/compatibility reasons, the read-only qualifier of the
4538a1c033e9514865f3a7b0d8b3b20e6de926cfec6cFariborz Jahanian    // pointee gets emitted _before_ the '^'.  The read-only qualifier of
4539a1c033e9514865f3a7b0d8b3b20e6de926cfec6cFariborz Jahanian    // the pointer itself gets ignored, _unless_ we are looking at a typedef!
45401eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    // Also, do not emit the 'r' for anything but the outermost type!
45418e1fab243ab8023b7ee3899745386b3b3a4258f8Mike Stump    if (isa<TypedefType>(T.getTypePtr())) {
4542a1c033e9514865f3a7b0d8b3b20e6de926cfec6cFariborz Jahanian      if (OutermostType && T.isConstQualified()) {
4543a1c033e9514865f3a7b0d8b3b20e6de926cfec6cFariborz Jahanian        isReadOnly = true;
4544a1c033e9514865f3a7b0d8b3b20e6de926cfec6cFariborz Jahanian        S += 'r';
4545a1c033e9514865f3a7b0d8b3b20e6de926cfec6cFariborz Jahanian      }
45469fdbab3cbc2fc04bcaf5768023d83707f3151144Mike Stump    } else if (OutermostType) {
4547a1c033e9514865f3a7b0d8b3b20e6de926cfec6cFariborz Jahanian      QualType P = PointeeTy;
45486217b80b7a1379b74cced1c076338262c3c980b3Ted Kremenek      while (P->getAs<PointerType>())
45496217b80b7a1379b74cced1c076338262c3c980b3Ted Kremenek        P = P->getAs<PointerType>()->getPointeeType();
4550a1c033e9514865f3a7b0d8b3b20e6de926cfec6cFariborz Jahanian      if (P.isConstQualified()) {
4551a1c033e9514865f3a7b0d8b3b20e6de926cfec6cFariborz Jahanian        isReadOnly = true;
4552a1c033e9514865f3a7b0d8b3b20e6de926cfec6cFariborz Jahanian        S += 'r';
4553a1c033e9514865f3a7b0d8b3b20e6de926cfec6cFariborz Jahanian      }
4554a1c033e9514865f3a7b0d8b3b20e6de926cfec6cFariborz Jahanian    }
4555a1c033e9514865f3a7b0d8b3b20e6de926cfec6cFariborz Jahanian    if (isReadOnly) {
4556a1c033e9514865f3a7b0d8b3b20e6de926cfec6cFariborz Jahanian      // Another legacy compatibility encoding. Some ObjC qualifier and type
4557a1c033e9514865f3a7b0d8b3b20e6de926cfec6cFariborz Jahanian      // combinations need to be rearranged.
4558a1c033e9514865f3a7b0d8b3b20e6de926cfec6cFariborz Jahanian      // Rewrite "in const" from "nr" to "rn"
45595f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner      if (StringRef(S).endswith("nr"))
45600237941e0beb0c929934b66ad29443b484d987feBenjamin Kramer        S.replace(S.end()-2, S.end(), "rn");
4561a1c033e9514865f3a7b0d8b3b20e6de926cfec6cFariborz Jahanian    }
45621eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
456385f9bceab1542aafff012d4d28e998f4ba16e362Anders Carlsson    if (PointeeTy->isCharType()) {
456485f9bceab1542aafff012d4d28e998f4ba16e362Anders Carlsson      // char pointer types should be encoded as '*' unless it is a
456585f9bceab1542aafff012d4d28e998f4ba16e362Anders Carlsson      // type that has been typedef'd to 'BOOL'.
4566e8c49533521c40643653f943d47229e62d277f88Anders Carlsson      if (!isTypeTypedefedAsBOOL(PointeeTy)) {
456785f9bceab1542aafff012d4d28e998f4ba16e362Anders Carlsson        S += '*';
456885f9bceab1542aafff012d4d28e998f4ba16e362Anders Carlsson        return;
456985f9bceab1542aafff012d4d28e998f4ba16e362Anders Carlsson      }
45706217b80b7a1379b74cced1c076338262c3c980b3Ted Kremenek    } else if (const RecordType *RTy = PointeeTy->getAs<RecordType>()) {
45719533a7fdb8397421f3be52e879442460a87389f6Steve Naroff      // GCC binary compat: Need to convert "struct objc_class *" to "#".
45729533a7fdb8397421f3be52e879442460a87389f6Steve Naroff      if (RTy->getDecl()->getIdentifier() == &Idents.get("objc_class")) {
45739533a7fdb8397421f3be52e879442460a87389f6Steve Naroff        S += '#';
45749533a7fdb8397421f3be52e879442460a87389f6Steve Naroff        return;
45759533a7fdb8397421f3be52e879442460a87389f6Steve Naroff      }
45769533a7fdb8397421f3be52e879442460a87389f6Steve Naroff      // GCC binary compat: Need to convert "struct objc_object *" to "@".
45779533a7fdb8397421f3be52e879442460a87389f6Steve Naroff      if (RTy->getDecl()->getIdentifier() == &Idents.get("objc_object")) {
45789533a7fdb8397421f3be52e879442460a87389f6Steve Naroff        S += '@';
45799533a7fdb8397421f3be52e879442460a87389f6Steve Naroff        return;
45809533a7fdb8397421f3be52e879442460a87389f6Steve Naroff      }
45819533a7fdb8397421f3be52e879442460a87389f6Steve Naroff      // fall through...
458285f9bceab1542aafff012d4d28e998f4ba16e362Anders Carlsson    }
458385f9bceab1542aafff012d4d28e998f4ba16e362Anders Carlsson    S += '^';
4584a1c033e9514865f3a7b0d8b3b20e6de926cfec6cFariborz Jahanian    getLegacyIntegralTypeEncoding(PointeeTy);
4585a1c033e9514865f3a7b0d8b3b20e6de926cfec6cFariborz Jahanian
45861eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    getObjCEncodingForTypeImpl(PointeeTy, S, false, ExpandPointedToStructures,
458743822eaeddeaa419b90f23c68af6b23c46788a58Fariborz Jahanian                               NULL);
4588ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner    return;
4589ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner  }
4590aa1d76163e4b0b1cc54e222be67379f8c02e8ffaFariborz Jahanian
4591ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner  if (const ArrayType *AT =
4592ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner      // Ignore type qualifiers etc.
4593ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner        dyn_cast<ArrayType>(T->getCanonicalTypeInternal())) {
45942636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis    if (isa<IncompleteArrayType>(AT) && !StructField) {
4595559a83330416affb0e341a2c53800cbf924a5178Anders Carlsson      // Incomplete arrays are encoded as a pointer to the array element.
4596559a83330416affb0e341a2c53800cbf924a5178Anders Carlsson      S += '^';
4597559a83330416affb0e341a2c53800cbf924a5178Anders Carlsson
45981eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      getObjCEncodingForTypeImpl(AT->getElementType(), S,
4599559a83330416affb0e341a2c53800cbf924a5178Anders Carlsson                                 false, ExpandStructures, FD);
4600559a83330416affb0e341a2c53800cbf924a5178Anders Carlsson    } else {
4601559a83330416affb0e341a2c53800cbf924a5178Anders Carlsson      S += '[';
46021eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
46032636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis      if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(AT)) {
46042636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis        if (getTypeSize(CAT->getElementType()) == 0)
46052636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis          S += '0';
46062636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis        else
46072636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis          S += llvm::utostr(CAT->getSize().getZExtValue());
46082636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis      } else {
4609559a83330416affb0e341a2c53800cbf924a5178Anders Carlsson        //Variable length arrays are encoded as a regular array with 0 elements.
46102636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis        assert((isa<VariableArrayType>(AT) || isa<IncompleteArrayType>(AT)) &&
46112636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis               "Unknown array type!");
4612559a83330416affb0e341a2c53800cbf924a5178Anders Carlsson        S += '0';
4613559a83330416affb0e341a2c53800cbf924a5178Anders Carlsson      }
46141eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
46151eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      getObjCEncodingForTypeImpl(AT->getElementType(), S,
4616559a83330416affb0e341a2c53800cbf924a5178Anders Carlsson                                 false, ExpandStructures, FD);
4617559a83330416affb0e341a2c53800cbf924a5178Anders Carlsson      S += ']';
4618559a83330416affb0e341a2c53800cbf924a5178Anders Carlsson    }
4619ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner    return;
4620ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner  }
46211eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
4622183700f494ec9b6701b6efe82bcb25f4c79ba561John McCall  if (T->getAs<FunctionType>()) {
4623c0a87b7db06643178ad2cbce0767548c139ea387Anders Carlsson    S += '?';
4624ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner    return;
4625ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner  }
46261eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
46276217b80b7a1379b74cced1c076338262c3c980b3Ted Kremenek  if (const RecordType *RTy = T->getAs<RecordType>()) {
462882a6cfbc421cc99c5b7313271f399f7ef95056ecDaniel Dunbar    RecordDecl *RDecl = RTy->getDecl();
4629d96b35bc6becf8db00d140c11e3d0e53f27567a1Daniel Dunbar    S += RDecl->isUnion() ? '(' : '{';
4630502a4a1ce4c34cf78c8182d9798da0a51d9b7302Daniel Dunbar    // Anonymous structures print as '?'
4631502a4a1ce4c34cf78c8182d9798da0a51d9b7302Daniel Dunbar    if (const IdentifierInfo *II = RDecl->getIdentifier()) {
4632502a4a1ce4c34cf78c8182d9798da0a51d9b7302Daniel Dunbar      S += II->getName();
46336fb94391dc7cb11fd4bbdb969bbab11b6b48c223Fariborz Jahanian      if (ClassTemplateSpecializationDecl *Spec
46346fb94391dc7cb11fd4bbdb969bbab11b6b48c223Fariborz Jahanian          = dyn_cast<ClassTemplateSpecializationDecl>(RDecl)) {
46356fb94391dc7cb11fd4bbdb969bbab11b6b48c223Fariborz Jahanian        const TemplateArgumentList &TemplateArgs = Spec->getTemplateArgs();
46366fb94391dc7cb11fd4bbdb969bbab11b6b48c223Fariborz Jahanian        std::string TemplateArgsStr
46376fb94391dc7cb11fd4bbdb969bbab11b6b48c223Fariborz Jahanian          = TemplateSpecializationType::PrintTemplateArgumentList(
4638910f8008fea79120489a53593fe971b0b8a4a740Douglas Gregor                                            TemplateArgs.data(),
4639910f8008fea79120489a53593fe971b0b8a4a740Douglas Gregor                                            TemplateArgs.size(),
464030c42404202d2e2512e51efc6066bd614cfdb5a4Douglas Gregor                                            (*this).getPrintingPolicy());
46416fb94391dc7cb11fd4bbdb969bbab11b6b48c223Fariborz Jahanian
46426fb94391dc7cb11fd4bbdb969bbab11b6b48c223Fariborz Jahanian        S += TemplateArgsStr;
46436fb94391dc7cb11fd4bbdb969bbab11b6b48c223Fariborz Jahanian      }
4644502a4a1ce4c34cf78c8182d9798da0a51d9b7302Daniel Dunbar    } else {
4645502a4a1ce4c34cf78c8182d9798da0a51d9b7302Daniel Dunbar      S += '?';
4646502a4a1ce4c34cf78c8182d9798da0a51d9b7302Daniel Dunbar    }
46470d504c1da852e58ff802545c823ecff3b6c654b8Daniel Dunbar    if (ExpandStructures) {
46487d6b46d9a9d75dea8ef9f6973dd50633c1f37963Fariborz Jahanian      S += '=';
46492636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis      if (!RDecl->isUnion()) {
46502636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis        getObjCEncodingForStructureImpl(RDecl, S, FD);
46512636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis      } else {
46522636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis        for (RecordDecl::field_iterator Field = RDecl->field_begin(),
46532636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis                                     FieldEnd = RDecl->field_end();
46542636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis             Field != FieldEnd; ++Field) {
46552636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis          if (FD) {
46562636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis            S += '"';
46572636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis            S += Field->getNameAsString();
46582636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis            S += '"';
46592636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis          }
46601eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
46612636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis          // Special case bit-fields.
46622636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis          if (Field->isBitField()) {
46632636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis            getObjCEncodingForTypeImpl(Field->getType(), S, false, true,
4664581deb3da481053c4993c7600f97acf7768caac5David Blaikie                                       *Field);
46652636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis          } else {
46662636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis            QualType qt = Field->getType();
46672636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis            getLegacyIntegralTypeEncoding(qt);
46682636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis            getObjCEncodingForTypeImpl(qt, S, false, true,
46692636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis                                       FD, /*OutermostType*/false,
46702636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis                                       /*EncodingProperty*/false,
46712636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis                                       /*StructField*/true);
46722636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis          }
4673d96b35bc6becf8db00d140c11e3d0e53f27567a1Daniel Dunbar        }
46747d6b46d9a9d75dea8ef9f6973dd50633c1f37963Fariborz Jahanian      }
46756de88a873a4cbe06d72602eef57d68006730a80bFariborz Jahanian    }
4676d96b35bc6becf8db00d140c11e3d0e53f27567a1Daniel Dunbar    S += RDecl->isUnion() ? ')' : '}';
4677ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner    return;
4678ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner  }
4679e6012c7ecb9d848f4091c8c48e7d9946cc36b23fFariborz Jahanian
46805471bc85b69912e3b448de004498a80c0de32296Douglas Gregor  if (const EnumType *ET = T->getAs<EnumType>()) {
46818b4bf90eb6d3d08cf3bfb86705f0fdb20b9c5875Fariborz Jahanian    if (FD && FD->isBitField())
468264fd7e86c1a90d9ff78e4a0bd79f69499667a4e3David Chisnall      EncodeBitField(this, S, T, FD);
46838b4bf90eb6d3d08cf3bfb86705f0fdb20b9c5875Fariborz Jahanian    else
46845471bc85b69912e3b448de004498a80c0de32296Douglas Gregor      S += ObjCEncodingForEnumType(this, ET);
4685ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner    return;
4686ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner  }
46871eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
4688dc8dab6fabf4bfd4f4b94bf572ac3342a5bbfcd7Bob Wilson  if (const BlockPointerType *BT = T->getAs<BlockPointerType>()) {
468921a98b188857d690aa4510c52ac4317ffa0908a8Steve Naroff    S += "@?"; // Unlike a pointer-to-function, which is "^?".
4690dc8dab6fabf4bfd4f4b94bf572ac3342a5bbfcd7Bob Wilson    if (EncodeBlockParameters) {
4691dc8dab6fabf4bfd4f4b94bf572ac3342a5bbfcd7Bob Wilson      const FunctionType *FT = BT->getPointeeType()->getAs<FunctionType>();
4692dc8dab6fabf4bfd4f4b94bf572ac3342a5bbfcd7Bob Wilson
4693dc8dab6fabf4bfd4f4b94bf572ac3342a5bbfcd7Bob Wilson      S += '<';
4694dc8dab6fabf4bfd4f4b94bf572ac3342a5bbfcd7Bob Wilson      // Block return type
4695dc8dab6fabf4bfd4f4b94bf572ac3342a5bbfcd7Bob Wilson      getObjCEncodingForTypeImpl(FT->getResultType(), S,
4696dc8dab6fabf4bfd4f4b94bf572ac3342a5bbfcd7Bob Wilson                                 ExpandPointedToStructures, ExpandStructures,
4697dc8dab6fabf4bfd4f4b94bf572ac3342a5bbfcd7Bob Wilson                                 FD,
4698dc8dab6fabf4bfd4f4b94bf572ac3342a5bbfcd7Bob Wilson                                 false /* OutermostType */,
4699dc8dab6fabf4bfd4f4b94bf572ac3342a5bbfcd7Bob Wilson                                 EncodingProperty,
4700dc8dab6fabf4bfd4f4b94bf572ac3342a5bbfcd7Bob Wilson                                 false /* StructField */,
4701dc8dab6fabf4bfd4f4b94bf572ac3342a5bbfcd7Bob Wilson                                 EncodeBlockParameters,
4702dc8dab6fabf4bfd4f4b94bf572ac3342a5bbfcd7Bob Wilson                                 EncodeClassNames);
4703dc8dab6fabf4bfd4f4b94bf572ac3342a5bbfcd7Bob Wilson      // Block self
4704dc8dab6fabf4bfd4f4b94bf572ac3342a5bbfcd7Bob Wilson      S += "@?";
4705dc8dab6fabf4bfd4f4b94bf572ac3342a5bbfcd7Bob Wilson      // Block parameters
4706dc8dab6fabf4bfd4f4b94bf572ac3342a5bbfcd7Bob Wilson      if (const FunctionProtoType *FPT = dyn_cast<FunctionProtoType>(FT)) {
4707dc8dab6fabf4bfd4f4b94bf572ac3342a5bbfcd7Bob Wilson        for (FunctionProtoType::arg_type_iterator I = FPT->arg_type_begin(),
4708dc8dab6fabf4bfd4f4b94bf572ac3342a5bbfcd7Bob Wilson               E = FPT->arg_type_end(); I && (I != E); ++I) {
4709dc8dab6fabf4bfd4f4b94bf572ac3342a5bbfcd7Bob Wilson          getObjCEncodingForTypeImpl(*I, S,
4710dc8dab6fabf4bfd4f4b94bf572ac3342a5bbfcd7Bob Wilson                                     ExpandPointedToStructures,
4711dc8dab6fabf4bfd4f4b94bf572ac3342a5bbfcd7Bob Wilson                                     ExpandStructures,
4712dc8dab6fabf4bfd4f4b94bf572ac3342a5bbfcd7Bob Wilson                                     FD,
4713dc8dab6fabf4bfd4f4b94bf572ac3342a5bbfcd7Bob Wilson                                     false /* OutermostType */,
4714dc8dab6fabf4bfd4f4b94bf572ac3342a5bbfcd7Bob Wilson                                     EncodingProperty,
4715dc8dab6fabf4bfd4f4b94bf572ac3342a5bbfcd7Bob Wilson                                     false /* StructField */,
4716dc8dab6fabf4bfd4f4b94bf572ac3342a5bbfcd7Bob Wilson                                     EncodeBlockParameters,
4717dc8dab6fabf4bfd4f4b94bf572ac3342a5bbfcd7Bob Wilson                                     EncodeClassNames);
4718dc8dab6fabf4bfd4f4b94bf572ac3342a5bbfcd7Bob Wilson        }
4719dc8dab6fabf4bfd4f4b94bf572ac3342a5bbfcd7Bob Wilson      }
4720dc8dab6fabf4bfd4f4b94bf572ac3342a5bbfcd7Bob Wilson      S += '>';
4721dc8dab6fabf4bfd4f4b94bf572ac3342a5bbfcd7Bob Wilson    }
4722ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner    return;
4723ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner  }
47241eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
4725c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall  // Ignore protocol qualifiers when mangling at this level.
4726c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall  if (const ObjCObjectType *OT = T->getAs<ObjCObjectType>())
4727c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall    T = OT->getBaseType();
4728c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall
47290953e767ff7817f97b3ab20896b229891eeff45bJohn McCall  if (const ObjCInterfaceType *OIT = T->getAs<ObjCInterfaceType>()) {
473043822eaeddeaa419b90f23c68af6b23c46788a58Fariborz Jahanian    // @encode(class_name)
47310953e767ff7817f97b3ab20896b229891eeff45bJohn McCall    ObjCInterfaceDecl *OI = OIT->getDecl();
473243822eaeddeaa419b90f23c68af6b23c46788a58Fariborz Jahanian    S += '{';
473343822eaeddeaa419b90f23c68af6b23c46788a58Fariborz Jahanian    const IdentifierInfo *II = OI->getIdentifier();
473443822eaeddeaa419b90f23c68af6b23c46788a58Fariborz Jahanian    S += II->getName();
473543822eaeddeaa419b90f23c68af6b23c46788a58Fariborz Jahanian    S += '=';
4736db8264e4c5ffd7af6fbad4ca4306bd382bb02691Jordy Rose    SmallVector<const ObjCIvarDecl*, 32> Ivars;
47372c18bb7c9fca66c30b6eabbdcbc6399d24a54fa9Fariborz Jahanian    DeepCollectObjCIvars(OI, true, Ivars);
47382c18bb7c9fca66c30b6eabbdcbc6399d24a54fa9Fariborz Jahanian    for (unsigned i = 0, e = Ivars.size(); i != e; ++i) {
4739db8264e4c5ffd7af6fbad4ca4306bd382bb02691Jordy Rose      const FieldDecl *Field = cast<FieldDecl>(Ivars[i]);
47402c18bb7c9fca66c30b6eabbdcbc6399d24a54fa9Fariborz Jahanian      if (Field->isBitField())
47412c18bb7c9fca66c30b6eabbdcbc6399d24a54fa9Fariborz Jahanian        getObjCEncodingForTypeImpl(Field->getType(), S, false, true, Field);
474243822eaeddeaa419b90f23c68af6b23c46788a58Fariborz Jahanian      else
47432c18bb7c9fca66c30b6eabbdcbc6399d24a54fa9Fariborz Jahanian        getObjCEncodingForTypeImpl(Field->getType(), S, false, true, FD);
474443822eaeddeaa419b90f23c68af6b23c46788a58Fariborz Jahanian    }
474543822eaeddeaa419b90f23c68af6b23c46788a58Fariborz Jahanian    S += '}';
4746ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner    return;
474743822eaeddeaa419b90f23c68af6b23c46788a58Fariborz Jahanian  }
47481eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
4749183700f494ec9b6701b6efe82bcb25f4c79ba561John McCall  if (const ObjCObjectPointerType *OPT = T->getAs<ObjCObjectPointerType>()) {
475014108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff    if (OPT->isObjCIdType()) {
475114108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff      S += '@';
475214108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff      return;
4753ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner    }
47541eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
475527d20a24a1b816cecbd142727d2c81af5a6a111bSteve Naroff    if (OPT->isObjCClassType() || OPT->isObjCQualifiedClassType()) {
475627d20a24a1b816cecbd142727d2c81af5a6a111bSteve Naroff      // FIXME: Consider if we need to output qualifiers for 'Class<p>'.
475727d20a24a1b816cecbd142727d2c81af5a6a111bSteve Naroff      // Since this is a binary compatibility issue, need to consult with runtime
475827d20a24a1b816cecbd142727d2c81af5a6a111bSteve Naroff      // folks. Fortunately, this is a *very* obsure construct.
475914108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff      S += '#';
476014108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff      return;
4761ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner    }
47621eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
4763ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner    if (OPT->isObjCQualifiedIdType()) {
47641eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      getObjCEncodingForTypeImpl(getObjCIdType(), S,
476514108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff                                 ExpandPointedToStructures,
476614108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff                                 ExpandStructures, FD);
4767dc8dab6fabf4bfd4f4b94bf572ac3342a5bbfcd7Bob Wilson      if (FD || EncodingProperty || EncodeClassNames) {
476814108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff        // Note that we do extended encoding of protocol qualifer list
476914108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff        // Only when doing ivar or property encoding.
477014108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff        S += '"';
477167ef8eaea8a0a2073147a8d863f0e3f30d525802Steve Naroff        for (ObjCObjectPointerType::qual_iterator I = OPT->qual_begin(),
477267ef8eaea8a0a2073147a8d863f0e3f30d525802Steve Naroff             E = OPT->qual_end(); I != E; ++I) {
477314108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff          S += '<';
477414108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff          S += (*I)->getNameAsString();
477514108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff          S += '>';
477614108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff        }
477714108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff        S += '"';
477814108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff      }
477914108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff      return;
4780ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner    }
47811eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
4782ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner    QualType PointeeTy = OPT->getPointeeType();
4783ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner    if (!EncodingProperty &&
4784ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner        isa<TypedefType>(PointeeTy.getTypePtr())) {
4785ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner      // Another historical/compatibility reason.
47861eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      // We encode the underlying type which comes out as
4787ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner      // {...};
4788ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner      S += '^';
47891eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      getObjCEncodingForTypeImpl(PointeeTy, S,
47901eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump                                 false, ExpandPointedToStructures,
4791ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner                                 NULL);
479214108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff      return;
479314108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff    }
4794ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner
4795ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner    S += '@';
4796dc8dab6fabf4bfd4f4b94bf572ac3342a5bbfcd7Bob Wilson    if (OPT->getInterfaceDecl() &&
4797dc8dab6fabf4bfd4f4b94bf572ac3342a5bbfcd7Bob Wilson        (FD || EncodingProperty || EncodeClassNames)) {
4798ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner      S += '"';
479927d20a24a1b816cecbd142727d2c81af5a6a111bSteve Naroff      S += OPT->getInterfaceDecl()->getIdentifier()->getName();
480067ef8eaea8a0a2073147a8d863f0e3f30d525802Steve Naroff      for (ObjCObjectPointerType::qual_iterator I = OPT->qual_begin(),
480167ef8eaea8a0a2073147a8d863f0e3f30d525802Steve Naroff           E = OPT->qual_end(); I != E; ++I) {
4802ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner        S += '<';
4803ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner        S += (*I)->getNameAsString();
4804ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner        S += '>';
48051eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      }
4806ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner      S += '"';
4807ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner    }
4808ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner    return;
4809ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner  }
48101eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
4811532ec7baf2d0791abc9551ef856a537711c5774aJohn McCall  // gcc just blithely ignores member pointers.
4812532ec7baf2d0791abc9551ef856a537711c5774aJohn McCall  // TODO: maybe there should be a mangling for these
4813532ec7baf2d0791abc9551ef856a537711c5774aJohn McCall  if (T->getAs<MemberPointerType>())
4814532ec7baf2d0791abc9551ef856a537711c5774aJohn McCall    return;
4815e6012c7ecb9d848f4091c8c48e7d9946cc36b23fFariborz Jahanian
4816e6012c7ecb9d848f4091c8c48e7d9946cc36b23fFariborz Jahanian  if (T->isVectorType()) {
4817e6012c7ecb9d848f4091c8c48e7d9946cc36b23fFariborz Jahanian    // This matches gcc's encoding, even though technically it is
4818e6012c7ecb9d848f4091c8c48e7d9946cc36b23fFariborz Jahanian    // insufficient.
4819e6012c7ecb9d848f4091c8c48e7d9946cc36b23fFariborz Jahanian    // FIXME. We should do a better job than gcc.
4820e6012c7ecb9d848f4091c8c48e7d9946cc36b23fFariborz Jahanian    return;
4821e6012c7ecb9d848f4091c8c48e7d9946cc36b23fFariborz Jahanian  }
4822e6012c7ecb9d848f4091c8c48e7d9946cc36b23fFariborz Jahanian
4823b219cfc4d75f0a03630b7c4509ef791b7e97b2c8David Blaikie  llvm_unreachable("@encode for type not implemented!");
482485f9bceab1542aafff012d4d28e998f4ba16e362Anders Carlsson}
482585f9bceab1542aafff012d4d28e998f4ba16e362Anders Carlsson
48262636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidisvoid ASTContext::getObjCEncodingForStructureImpl(RecordDecl *RDecl,
48272636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis                                                 std::string &S,
48282636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis                                                 const FieldDecl *FD,
48292636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis                                                 bool includeVBases) const {
48302636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis  assert(RDecl && "Expected non-null RecordDecl");
48312636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis  assert(!RDecl->isUnion() && "Should not be called for unions");
48322636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis  if (!RDecl->getDefinition())
48332636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis    return;
48342636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis
48352636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis  CXXRecordDecl *CXXRec = dyn_cast<CXXRecordDecl>(RDecl);
48362636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis  std::multimap<uint64_t, NamedDecl *> FieldOrBaseOffsets;
48372636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis  const ASTRecordLayout &layout = getASTRecordLayout(RDecl);
48382636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis
48392636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis  if (CXXRec) {
48402636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis    for (CXXRecordDecl::base_class_iterator
48412636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis           BI = CXXRec->bases_begin(),
48422636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis           BE = CXXRec->bases_end(); BI != BE; ++BI) {
48432636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis      if (!BI->isVirtual()) {
48442636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis        CXXRecordDecl *base = BI->getType()->getAsCXXRecordDecl();
4845829f20097e030229fd4c234b84948f53312eaf55Argyrios Kyrtzidis        if (base->isEmpty())
4846829f20097e030229fd4c234b84948f53312eaf55Argyrios Kyrtzidis          continue;
48472636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis        uint64_t offs = layout.getBaseClassOffsetInBits(base);
48482636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis        FieldOrBaseOffsets.insert(FieldOrBaseOffsets.upper_bound(offs),
48492636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis                                  std::make_pair(offs, base));
48502636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis      }
48512636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis    }
48522636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis  }
48532636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis
48542636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis  unsigned i = 0;
48552636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis  for (RecordDecl::field_iterator Field = RDecl->field_begin(),
48562636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis                               FieldEnd = RDecl->field_end();
48572636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis       Field != FieldEnd; ++Field, ++i) {
48582636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis    uint64_t offs = layout.getFieldOffset(i);
48592636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis    FieldOrBaseOffsets.insert(FieldOrBaseOffsets.upper_bound(offs),
4860581deb3da481053c4993c7600f97acf7768caac5David Blaikie                              std::make_pair(offs, *Field));
48612636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis  }
48622636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis
48632636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis  if (CXXRec && includeVBases) {
48642636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis    for (CXXRecordDecl::base_class_iterator
48652636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis           BI = CXXRec->vbases_begin(),
48662636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis           BE = CXXRec->vbases_end(); BI != BE; ++BI) {
48672636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis      CXXRecordDecl *base = BI->getType()->getAsCXXRecordDecl();
4868829f20097e030229fd4c234b84948f53312eaf55Argyrios Kyrtzidis      if (base->isEmpty())
4869829f20097e030229fd4c234b84948f53312eaf55Argyrios Kyrtzidis        continue;
48702636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis      uint64_t offs = layout.getVBaseClassOffsetInBits(base);
487119aa8609a5d04eed9b81aa1c4608e8a4f3df6ccdArgyrios Kyrtzidis      if (FieldOrBaseOffsets.find(offs) == FieldOrBaseOffsets.end())
487219aa8609a5d04eed9b81aa1c4608e8a4f3df6ccdArgyrios Kyrtzidis        FieldOrBaseOffsets.insert(FieldOrBaseOffsets.end(),
487319aa8609a5d04eed9b81aa1c4608e8a4f3df6ccdArgyrios Kyrtzidis                                  std::make_pair(offs, base));
48742636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis    }
48752636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis  }
48762636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis
48772636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis  CharUnits size;
48782636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis  if (CXXRec) {
48792636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis    size = includeVBases ? layout.getSize() : layout.getNonVirtualSize();
48802636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis  } else {
48812636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis    size = layout.getSize();
48822636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis  }
48832636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis
48842636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis  uint64_t CurOffs = 0;
48852636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis  std::multimap<uint64_t, NamedDecl *>::iterator
48862636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis    CurLayObj = FieldOrBaseOffsets.begin();
48872636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis
488858db7a575efc9a2f35266fe240feac3cf317753dDouglas Gregor  if (CXXRec && CXXRec->isDynamicClass() &&
488958db7a575efc9a2f35266fe240feac3cf317753dDouglas Gregor      (CurLayObj == FieldOrBaseOffsets.end() || CurLayObj->first != 0)) {
48902636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis    if (FD) {
48912636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis      S += "\"_vptr$";
48922636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis      std::string recname = CXXRec->getNameAsString();
48932636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis      if (recname.empty()) recname = "?";
48942636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis      S += recname;
48952636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis      S += '"';
48962636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis    }
48972636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis    S += "^^?";
48982636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis    CurOffs += getTypeSize(VoidPtrTy);
48992636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis  }
49002636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis
49012636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis  if (!RDecl->hasFlexibleArrayMember()) {
49022636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis    // Mark the end of the structure.
49032636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis    uint64_t offs = toBits(size);
49042636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis    FieldOrBaseOffsets.insert(FieldOrBaseOffsets.upper_bound(offs),
49052636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis                              std::make_pair(offs, (NamedDecl*)0));
49062636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis  }
49072636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis
49082636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis  for (; CurLayObj != FieldOrBaseOffsets.end(); ++CurLayObj) {
49092636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis    assert(CurOffs <= CurLayObj->first);
49102636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis
49112636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis    if (CurOffs < CurLayObj->first) {
49122636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis      uint64_t padding = CurLayObj->first - CurOffs;
49132636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis      // FIXME: There doesn't seem to be a way to indicate in the encoding that
49142636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis      // packing/alignment of members is different that normal, in which case
49152636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis      // the encoding will be out-of-sync with the real layout.
49162636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis      // If the runtime switches to just consider the size of types without
49172636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis      // taking into account alignment, we could make padding explicit in the
49182636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis      // encoding (e.g. using arrays of chars). The encoding strings would be
49192636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis      // longer then though.
49202636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis      CurOffs += padding;
49212636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis    }
49222636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis
49232636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis    NamedDecl *dcl = CurLayObj->second;
49242636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis    if (dcl == 0)
49252636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis      break; // reached end of structure.
49262636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis
49272636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis    if (CXXRecordDecl *base = dyn_cast<CXXRecordDecl>(dcl)) {
49282636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis      // We expand the bases without their virtual bases since those are going
49292636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis      // in the initial structure. Note that this differs from gcc which
49302636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis      // expands virtual bases each time one is encountered in the hierarchy,
49312636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis      // making the encoding type bigger than it really is.
49322636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis      getObjCEncodingForStructureImpl(base, S, FD, /*includeVBases*/false);
4933829f20097e030229fd4c234b84948f53312eaf55Argyrios Kyrtzidis      assert(!base->isEmpty());
4934829f20097e030229fd4c234b84948f53312eaf55Argyrios Kyrtzidis      CurOffs += toBits(getASTRecordLayout(base).getNonVirtualSize());
49352636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis    } else {
49362636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis      FieldDecl *field = cast<FieldDecl>(dcl);
49372636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis      if (FD) {
49382636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis        S += '"';
49392636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis        S += field->getNameAsString();
49402636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis        S += '"';
49412636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis      }
49422636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis
49432636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis      if (field->isBitField()) {
49442636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis        EncodeBitField(this, S, field->getType(), field);
4945a6b8b2c09610b8bc4330e948ece8b940c2386406Richard Smith        CurOffs += field->getBitWidthValue(*this);
49462636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis      } else {
49472636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis        QualType qt = field->getType();
49482636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis        getLegacyIntegralTypeEncoding(qt);
49492636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis        getObjCEncodingForTypeImpl(qt, S, false, true, FD,
49502636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis                                   /*OutermostType*/false,
49512636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis                                   /*EncodingProperty*/false,
49522636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis                                   /*StructField*/true);
49532636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis        CurOffs += getTypeSize(field->getType());
49542636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis      }
49552636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis    }
49562636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis  }
49572636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis}
49582636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis
49591eb4433ac451dc16f4133a88af2d002ac26c58efMike Stumpvoid ASTContext::getObjCEncodingForTypeQualifier(Decl::ObjCDeclQualifier QT,
4960ecb01e666665efabd2aa76a76f6080e2a78965faFariborz Jahanian                                                 std::string& S) const {
4961ecb01e666665efabd2aa76a76f6080e2a78965faFariborz Jahanian  if (QT & Decl::OBJC_TQ_In)
4962ecb01e666665efabd2aa76a76f6080e2a78965faFariborz Jahanian    S += 'n';
4963ecb01e666665efabd2aa76a76f6080e2a78965faFariborz Jahanian  if (QT & Decl::OBJC_TQ_Inout)
4964ecb01e666665efabd2aa76a76f6080e2a78965faFariborz Jahanian    S += 'N';
4965ecb01e666665efabd2aa76a76f6080e2a78965faFariborz Jahanian  if (QT & Decl::OBJC_TQ_Out)
4966ecb01e666665efabd2aa76a76f6080e2a78965faFariborz Jahanian    S += 'o';
4967ecb01e666665efabd2aa76a76f6080e2a78965faFariborz Jahanian  if (QT & Decl::OBJC_TQ_Bycopy)
4968ecb01e666665efabd2aa76a76f6080e2a78965faFariborz Jahanian    S += 'O';
4969ecb01e666665efabd2aa76a76f6080e2a78965faFariborz Jahanian  if (QT & Decl::OBJC_TQ_Byref)
4970ecb01e666665efabd2aa76a76f6080e2a78965faFariborz Jahanian    S += 'R';
4971ecb01e666665efabd2aa76a76f6080e2a78965faFariborz Jahanian  if (QT & Decl::OBJC_TQ_Oneway)
4972ecb01e666665efabd2aa76a76f6080e2a78965faFariborz Jahanian    S += 'V';
4973ecb01e666665efabd2aa76a76f6080e2a78965faFariborz Jahanian}
4974ecb01e666665efabd2aa76a76f6080e2a78965faFariborz Jahanian
49754dfd02a17c6d604c72e6936527c5e1c56d3ecb7aDouglas GregorTypedefDecl *ASTContext::getObjCIdDecl() const {
49764dfd02a17c6d604c72e6936527c5e1c56d3ecb7aDouglas Gregor  if (!ObjCIdDecl) {
49774dfd02a17c6d604c72e6936527c5e1c56d3ecb7aDouglas Gregor    QualType T = getObjCObjectType(ObjCBuiltinIdTy, 0, 0);
49784dfd02a17c6d604c72e6936527c5e1c56d3ecb7aDouglas Gregor    T = getObjCObjectPointerType(T);
49794dfd02a17c6d604c72e6936527c5e1c56d3ecb7aDouglas Gregor    TypeSourceInfo *IdInfo = getTrivialTypeSourceInfo(T);
49804dfd02a17c6d604c72e6936527c5e1c56d3ecb7aDouglas Gregor    ObjCIdDecl = TypedefDecl::Create(const_cast<ASTContext &>(*this),
49814dfd02a17c6d604c72e6936527c5e1c56d3ecb7aDouglas Gregor                                     getTranslationUnitDecl(),
49824dfd02a17c6d604c72e6936527c5e1c56d3ecb7aDouglas Gregor                                     SourceLocation(), SourceLocation(),
49834dfd02a17c6d604c72e6936527c5e1c56d3ecb7aDouglas Gregor                                     &Idents.get("id"), IdInfo);
49844dfd02a17c6d604c72e6936527c5e1c56d3ecb7aDouglas Gregor  }
49854dfd02a17c6d604c72e6936527c5e1c56d3ecb7aDouglas Gregor
49864dfd02a17c6d604c72e6936527c5e1c56d3ecb7aDouglas Gregor  return ObjCIdDecl;
49877e219e47de26346885d667131977bd9ca2d7662aSteve Naroff}
49887e219e47de26346885d667131977bd9ca2d7662aSteve Naroff
49897a27ea52b7bd635c89bec5a9c521a3bf7d204238Douglas GregorTypedefDecl *ASTContext::getObjCSelDecl() const {
49907a27ea52b7bd635c89bec5a9c521a3bf7d204238Douglas Gregor  if (!ObjCSelDecl) {
49917a27ea52b7bd635c89bec5a9c521a3bf7d204238Douglas Gregor    QualType SelT = getPointerType(ObjCBuiltinSelTy);
49927a27ea52b7bd635c89bec5a9c521a3bf7d204238Douglas Gregor    TypeSourceInfo *SelInfo = getTrivialTypeSourceInfo(SelT);
49937a27ea52b7bd635c89bec5a9c521a3bf7d204238Douglas Gregor    ObjCSelDecl = TypedefDecl::Create(const_cast<ASTContext &>(*this),
49947a27ea52b7bd635c89bec5a9c521a3bf7d204238Douglas Gregor                                      getTranslationUnitDecl(),
49957a27ea52b7bd635c89bec5a9c521a3bf7d204238Douglas Gregor                                      SourceLocation(), SourceLocation(),
49967a27ea52b7bd635c89bec5a9c521a3bf7d204238Douglas Gregor                                      &Idents.get("SEL"), SelInfo);
49977a27ea52b7bd635c89bec5a9c521a3bf7d204238Douglas Gregor  }
49987a27ea52b7bd635c89bec5a9c521a3bf7d204238Douglas Gregor  return ObjCSelDecl;
4999b62f6813406a03bf8a371c4e46c9fad51d102121Fariborz Jahanian}
5000b62f6813406a03bf8a371c4e46c9fad51d102121Fariborz Jahanian
500179d6726921897811232554ed94c5d77b5b7b3fc0Douglas GregorTypedefDecl *ASTContext::getObjCClassDecl() const {
500279d6726921897811232554ed94c5d77b5b7b3fc0Douglas Gregor  if (!ObjCClassDecl) {
500379d6726921897811232554ed94c5d77b5b7b3fc0Douglas Gregor    QualType T = getObjCObjectType(ObjCBuiltinClassTy, 0, 0);
500479d6726921897811232554ed94c5d77b5b7b3fc0Douglas Gregor    T = getObjCObjectPointerType(T);
500579d6726921897811232554ed94c5d77b5b7b3fc0Douglas Gregor    TypeSourceInfo *ClassInfo = getTrivialTypeSourceInfo(T);
500679d6726921897811232554ed94c5d77b5b7b3fc0Douglas Gregor    ObjCClassDecl = TypedefDecl::Create(const_cast<ASTContext &>(*this),
500779d6726921897811232554ed94c5d77b5b7b3fc0Douglas Gregor                                        getTranslationUnitDecl(),
500879d6726921897811232554ed94c5d77b5b7b3fc0Douglas Gregor                                        SourceLocation(), SourceLocation(),
500979d6726921897811232554ed94c5d77b5b7b3fc0Douglas Gregor                                        &Idents.get("Class"), ClassInfo);
501079d6726921897811232554ed94c5d77b5b7b3fc0Douglas Gregor  }
501179d6726921897811232554ed94c5d77b5b7b3fc0Douglas Gregor
501279d6726921897811232554ed94c5d77b5b7b3fc0Douglas Gregor  return ObjCClassDecl;
5013a6ea10e22b600d92e084f6b11b9b9a92d0eb2412Douglas Gregor}
5014a6ea10e22b600d92e084f6b11b9b9a92d0eb2412Douglas Gregor
5015a6ea10e22b600d92e084f6b11b9b9a92d0eb2412Douglas GregorObjCInterfaceDecl *ASTContext::getObjCProtocolDecl() const {
5016a6ea10e22b600d92e084f6b11b9b9a92d0eb2412Douglas Gregor  if (!ObjCProtocolClassDecl) {
5017a6ea10e22b600d92e084f6b11b9b9a92d0eb2412Douglas Gregor    ObjCProtocolClassDecl
5018a6ea10e22b600d92e084f6b11b9b9a92d0eb2412Douglas Gregor      = ObjCInterfaceDecl::Create(*this, getTranslationUnitDecl(),
5019a6ea10e22b600d92e084f6b11b9b9a92d0eb2412Douglas Gregor                                  SourceLocation(),
5020a6ea10e22b600d92e084f6b11b9b9a92d0eb2412Douglas Gregor                                  &Idents.get("Protocol"),
5021a6ea10e22b600d92e084f6b11b9b9a92d0eb2412Douglas Gregor                                  /*PrevDecl=*/0,
5022a6ea10e22b600d92e084f6b11b9b9a92d0eb2412Douglas Gregor                                  SourceLocation(), true);
5023a6ea10e22b600d92e084f6b11b9b9a92d0eb2412Douglas Gregor  }
5024a6ea10e22b600d92e084f6b11b9b9a92d0eb2412Douglas Gregor
5025a6ea10e22b600d92e084f6b11b9b9a92d0eb2412Douglas Gregor  return ObjCProtocolClassDecl;
50268baaca50f07d0c10bba69c8d88c1b9078c92d06dAnders Carlsson}
50278baaca50f07d0c10bba69c8d88c1b9078c92d06dAnders Carlsson
5028c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge//===----------------------------------------------------------------------===//
5029c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge// __builtin_va_list Construction Functions
5030c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge//===----------------------------------------------------------------------===//
5031c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge
5032c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Ingestatic TypedefDecl *CreateCharPtrBuiltinVaListDecl(const ASTContext *Context) {
5033c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge  // typedef char* __builtin_va_list;
5034c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge  QualType CharPtrType = Context->getPointerType(Context->CharTy);
5035c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge  TypeSourceInfo *TInfo
5036c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge    = Context->getTrivialTypeSourceInfo(CharPtrType);
5037c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge
5038c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge  TypedefDecl *VaListTypeDecl
5039c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge    = TypedefDecl::Create(const_cast<ASTContext &>(*Context),
5040c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge                          Context->getTranslationUnitDecl(),
5041c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge                          SourceLocation(), SourceLocation(),
5042c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge                          &Context->Idents.get("__builtin_va_list"),
5043c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge                          TInfo);
5044c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge  return VaListTypeDecl;
5045c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge}
5046c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge
5047c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Ingestatic TypedefDecl *CreateVoidPtrBuiltinVaListDecl(const ASTContext *Context) {
5048c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge  // typedef void* __builtin_va_list;
5049c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge  QualType VoidPtrType = Context->getPointerType(Context->VoidTy);
5050c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge  TypeSourceInfo *TInfo
5051c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge    = Context->getTrivialTypeSourceInfo(VoidPtrType);
5052c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge
5053c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge  TypedefDecl *VaListTypeDecl
5054c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge    = TypedefDecl::Create(const_cast<ASTContext &>(*Context),
5055c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge                          Context->getTranslationUnitDecl(),
5056c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge                          SourceLocation(), SourceLocation(),
5057c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge                          &Context->Idents.get("__builtin_va_list"),
5058c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge                          TInfo);
5059c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge  return VaListTypeDecl;
5060c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge}
5061c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge
5062c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Ingestatic TypedefDecl *CreatePowerABIBuiltinVaListDecl(const ASTContext *Context) {
5063c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge  // typedef struct __va_list_tag {
5064c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge  RecordDecl *VaListTagDecl;
5065c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge
5066c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge  VaListTagDecl = CreateRecordDecl(*Context, TTK_Struct,
5067c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge                                   Context->getTranslationUnitDecl(),
5068c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge                                   &Context->Idents.get("__va_list_tag"));
5069c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge  VaListTagDecl->startDefinition();
5070c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge
5071c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge  const size_t NumFields = 5;
5072c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge  QualType FieldTypes[NumFields];
5073c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge  const char *FieldNames[NumFields];
5074c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge
5075c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge  //   unsigned char gpr;
5076c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge  FieldTypes[0] = Context->UnsignedCharTy;
5077c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge  FieldNames[0] = "gpr";
5078c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge
5079c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge  //   unsigned char fpr;
5080c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge  FieldTypes[1] = Context->UnsignedCharTy;
5081c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge  FieldNames[1] = "fpr";
5082c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge
5083c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge  //   unsigned short reserved;
5084c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge  FieldTypes[2] = Context->UnsignedShortTy;
5085c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge  FieldNames[2] = "reserved";
5086c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge
5087c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge  //   void* overflow_arg_area;
5088c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge  FieldTypes[3] = Context->getPointerType(Context->VoidTy);
5089c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge  FieldNames[3] = "overflow_arg_area";
5090c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge
5091c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge  //   void* reg_save_area;
5092c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge  FieldTypes[4] = Context->getPointerType(Context->VoidTy);
5093c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge  FieldNames[4] = "reg_save_area";
5094c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge
5095c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge  // Create fields
5096c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge  for (unsigned i = 0; i < NumFields; ++i) {
5097c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge    FieldDecl *Field = FieldDecl::Create(*Context, VaListTagDecl,
5098c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge                                         SourceLocation(),
5099c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge                                         SourceLocation(),
5100c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge                                         &Context->Idents.get(FieldNames[i]),
5101c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge                                         FieldTypes[i], /*TInfo=*/0,
5102c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge                                         /*BitWidth=*/0,
5103c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge                                         /*Mutable=*/false,
5104c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge                                         ICIS_NoInit);
5105c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge    Field->setAccess(AS_public);
5106c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge    VaListTagDecl->addDecl(Field);
5107c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge  }
5108c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge  VaListTagDecl->completeDefinition();
5109c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge  QualType VaListTagType = Context->getRecordType(VaListTagDecl);
5110c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge
5111c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge  // } __va_list_tag;
5112c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge  TypedefDecl *VaListTagTypedefDecl
5113c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge    = TypedefDecl::Create(const_cast<ASTContext &>(*Context),
5114c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge                          Context->getTranslationUnitDecl(),
5115c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge                          SourceLocation(), SourceLocation(),
5116c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge                          &Context->Idents.get("__va_list_tag"),
5117c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge                          Context->getTrivialTypeSourceInfo(VaListTagType));
5118c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge  QualType VaListTagTypedefType =
5119c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge    Context->getTypedefType(VaListTagTypedefDecl);
5120c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge
5121c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge  // typedef __va_list_tag __builtin_va_list[1];
5122c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge  llvm::APInt Size(Context->getTypeSize(Context->getSizeType()), 1);
5123c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge  QualType VaListTagArrayType
5124c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge    = Context->getConstantArrayType(VaListTagTypedefType,
5125c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge                                    Size, ArrayType::Normal, 0);
5126c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge  TypeSourceInfo *TInfo
5127c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge    = Context->getTrivialTypeSourceInfo(VaListTagArrayType);
5128c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge  TypedefDecl *VaListTypedefDecl
5129c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge    = TypedefDecl::Create(const_cast<ASTContext &>(*Context),
5130c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge                          Context->getTranslationUnitDecl(),
5131c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge                          SourceLocation(), SourceLocation(),
5132c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge                          &Context->Idents.get("__builtin_va_list"),
5133c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge                          TInfo);
5134c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge
5135c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge  return VaListTypedefDecl;
5136c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge}
5137c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge
5138c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Ingestatic TypedefDecl *
5139c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador IngeCreateX86_64ABIBuiltinVaListDecl(const ASTContext *Context) {
5140c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge  // typedef struct __va_list_tag {
5141c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge  RecordDecl *VaListTagDecl;
5142c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge  VaListTagDecl = CreateRecordDecl(*Context, TTK_Struct,
5143c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge                                   Context->getTranslationUnitDecl(),
5144c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge                                   &Context->Idents.get("__va_list_tag"));
5145c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge  VaListTagDecl->startDefinition();
5146c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge
5147c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge  const size_t NumFields = 4;
5148c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge  QualType FieldTypes[NumFields];
5149c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge  const char *FieldNames[NumFields];
5150c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge
5151c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge  //   unsigned gp_offset;
5152c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge  FieldTypes[0] = Context->UnsignedIntTy;
5153c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge  FieldNames[0] = "gp_offset";
5154c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge
5155c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge  //   unsigned fp_offset;
5156c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge  FieldTypes[1] = Context->UnsignedIntTy;
5157c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge  FieldNames[1] = "fp_offset";
5158c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge
5159c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge  //   void* overflow_arg_area;
5160c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge  FieldTypes[2] = Context->getPointerType(Context->VoidTy);
5161c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge  FieldNames[2] = "overflow_arg_area";
5162c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge
5163c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge  //   void* reg_save_area;
5164c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge  FieldTypes[3] = Context->getPointerType(Context->VoidTy);
5165c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge  FieldNames[3] = "reg_save_area";
5166c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge
5167c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge  // Create fields
5168c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge  for (unsigned i = 0; i < NumFields; ++i) {
5169c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge    FieldDecl *Field = FieldDecl::Create(const_cast<ASTContext &>(*Context),
5170c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge                                         VaListTagDecl,
5171c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge                                         SourceLocation(),
5172c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge                                         SourceLocation(),
5173c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge                                         &Context->Idents.get(FieldNames[i]),
5174c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge                                         FieldTypes[i], /*TInfo=*/0,
5175c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge                                         /*BitWidth=*/0,
5176c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge                                         /*Mutable=*/false,
5177c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge                                         ICIS_NoInit);
5178c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge    Field->setAccess(AS_public);
5179c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge    VaListTagDecl->addDecl(Field);
5180c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge  }
5181c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge  VaListTagDecl->completeDefinition();
5182c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge  QualType VaListTagType = Context->getRecordType(VaListTagDecl);
5183c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge
5184c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge  // } __va_list_tag;
5185c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge  TypedefDecl *VaListTagTypedefDecl
5186c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge    = TypedefDecl::Create(const_cast<ASTContext &>(*Context),
5187c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge                          Context->getTranslationUnitDecl(),
5188c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge                          SourceLocation(), SourceLocation(),
5189c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge                          &Context->Idents.get("__va_list_tag"),
5190c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge                          Context->getTrivialTypeSourceInfo(VaListTagType));
5191c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge  QualType VaListTagTypedefType =
5192c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge    Context->getTypedefType(VaListTagTypedefDecl);
5193c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge
5194c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge  // typedef __va_list_tag __builtin_va_list[1];
5195c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge  llvm::APInt Size(Context->getTypeSize(Context->getSizeType()), 1);
5196c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge  QualType VaListTagArrayType
5197c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge    = Context->getConstantArrayType(VaListTagTypedefType,
5198c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge                                      Size, ArrayType::Normal,0);
5199c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge  TypeSourceInfo *TInfo
5200c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge    = Context->getTrivialTypeSourceInfo(VaListTagArrayType);
5201c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge  TypedefDecl *VaListTypedefDecl
5202c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge    = TypedefDecl::Create(const_cast<ASTContext &>(*Context),
5203c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge                          Context->getTranslationUnitDecl(),
5204c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge                          SourceLocation(), SourceLocation(),
5205c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge                          &Context->Idents.get("__builtin_va_list"),
5206c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge                          TInfo);
5207c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge
5208c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge  return VaListTypedefDecl;
5209c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge}
5210c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge
5211c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Ingestatic TypedefDecl *CreatePNaClABIBuiltinVaListDecl(const ASTContext *Context) {
5212c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge  // typedef int __builtin_va_list[4];
5213c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge  llvm::APInt Size(Context->getTypeSize(Context->getSizeType()), 4);
5214c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge  QualType IntArrayType
5215c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge    = Context->getConstantArrayType(Context->IntTy,
5216c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge				    Size, ArrayType::Normal, 0);
5217c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge  TypedefDecl *VaListTypedefDecl
5218c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge    = TypedefDecl::Create(const_cast<ASTContext &>(*Context),
5219c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge                          Context->getTranslationUnitDecl(),
5220c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge                          SourceLocation(), SourceLocation(),
5221c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge                          &Context->Idents.get("__builtin_va_list"),
5222c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge                          Context->getTrivialTypeSourceInfo(IntArrayType));
5223c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge
5224c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge  return VaListTypedefDecl;
5225c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge}
5226c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge
5227c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Ingestatic TypedefDecl *CreateVaListDecl(const ASTContext *Context,
5228c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge                                     TargetInfo::BuiltinVaListKind Kind) {
5229c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge  switch (Kind) {
5230c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge  case TargetInfo::CharPtrBuiltinVaList:
5231c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge    return CreateCharPtrBuiltinVaListDecl(Context);
5232c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge  case TargetInfo::VoidPtrBuiltinVaList:
5233c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge    return CreateVoidPtrBuiltinVaListDecl(Context);
5234c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge  case TargetInfo::PowerABIBuiltinVaList:
5235c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge    return CreatePowerABIBuiltinVaListDecl(Context);
5236c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge  case TargetInfo::X86_64ABIBuiltinVaList:
5237c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge    return CreateX86_64ABIBuiltinVaListDecl(Context);
5238c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge  case TargetInfo::PNaClABIBuiltinVaList:
5239c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge    return CreatePNaClABIBuiltinVaListDecl(Context);
5240c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge  }
5241c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge
5242c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge  llvm_unreachable("Unhandled __builtin_va_list type kind");
5243c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge}
5244c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge
5245c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador IngeTypedefDecl *ASTContext::getBuiltinVaListDecl() const {
5246c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge  if (!BuiltinVaListDecl)
5247c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge    BuiltinVaListDecl = CreateVaListDecl(this, Target->getBuiltinVaListKind());
5248c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge
5249c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge  return BuiltinVaListDecl;
5250c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge}
5251c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge
5252a526c5c67e5a0473c340903ee542ce570119665fTed Kremenekvoid ASTContext::setObjCConstantStringInterface(ObjCInterfaceDecl *Decl) {
52531eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  assert(ObjCConstantStringType.isNull() &&
52542198891824c38d45b2279de5d5e3ef9394eb457cSteve Naroff         "'NSConstantString' type already set!");
52551eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
5256a526c5c67e5a0473c340903ee542ce570119665fTed Kremenek  ObjCConstantStringType = getObjCInterfaceType(Decl);
52572198891824c38d45b2279de5d5e3ef9394eb457cSteve Naroff}
52582198891824c38d45b2279de5d5e3ef9394eb457cSteve Naroff
52590bd6feb9e9d40fc889fd47e899985125a43dfed8John McCall/// \brief Retrieve the template name that corresponds to a non-empty
52600bd6feb9e9d40fc889fd47e899985125a43dfed8John McCall/// lookup.
52614ba2a17694148e16eaa8d3917f657ffcd3667be4Jay FoadTemplateName
52624ba2a17694148e16eaa8d3917f657ffcd3667be4Jay FoadASTContext::getOverloadedTemplateName(UnresolvedSetIterator Begin,
52634ba2a17694148e16eaa8d3917f657ffcd3667be4Jay Foad                                      UnresolvedSetIterator End) const {
52640bd6feb9e9d40fc889fd47e899985125a43dfed8John McCall  unsigned size = End - Begin;
52650bd6feb9e9d40fc889fd47e899985125a43dfed8John McCall  assert(size > 1 && "set is not overloaded!");
52667532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor
52670bd6feb9e9d40fc889fd47e899985125a43dfed8John McCall  void *memory = Allocate(sizeof(OverloadedTemplateStorage) +
52680bd6feb9e9d40fc889fd47e899985125a43dfed8John McCall                          size * sizeof(FunctionTemplateDecl*));
52690bd6feb9e9d40fc889fd47e899985125a43dfed8John McCall  OverloadedTemplateStorage *OT = new(memory) OverloadedTemplateStorage(size);
52700bd6feb9e9d40fc889fd47e899985125a43dfed8John McCall
52710bd6feb9e9d40fc889fd47e899985125a43dfed8John McCall  NamedDecl **Storage = OT->getStorage();
5272eec51cf1ba5f0e62c9cdb81b5c63babdd6e649abJohn McCall  for (UnresolvedSetIterator I = Begin; I != End; ++I) {
52730bd6feb9e9d40fc889fd47e899985125a43dfed8John McCall    NamedDecl *D = *I;
52740bd6feb9e9d40fc889fd47e899985125a43dfed8John McCall    assert(isa<FunctionTemplateDecl>(D) ||
52750bd6feb9e9d40fc889fd47e899985125a43dfed8John McCall           (isa<UsingShadowDecl>(D) &&
52760bd6feb9e9d40fc889fd47e899985125a43dfed8John McCall            isa<FunctionTemplateDecl>(D->getUnderlyingDecl())));
52770bd6feb9e9d40fc889fd47e899985125a43dfed8John McCall    *Storage++ = D;
52787532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor  }
52797532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor
52800bd6feb9e9d40fc889fd47e899985125a43dfed8John McCall  return TemplateName(OT);
52817532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor}
52827532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor
5283d99cbe66403ee39c2ee58024b9582b95649a4fc5Douglas Gregor/// \brief Retrieve the template name that represents a qualified
5284d99cbe66403ee39c2ee58024b9582b95649a4fc5Douglas Gregor/// template name such as \c std::vector.
52854ba2a17694148e16eaa8d3917f657ffcd3667be4Jay FoadTemplateName
52864ba2a17694148e16eaa8d3917f657ffcd3667be4Jay FoadASTContext::getQualifiedTemplateName(NestedNameSpecifier *NNS,
52874ba2a17694148e16eaa8d3917f657ffcd3667be4Jay Foad                                     bool TemplateKeyword,
52884ba2a17694148e16eaa8d3917f657ffcd3667be4Jay Foad                                     TemplateDecl *Template) const {
52890f0ea2a96534c615ff5fdd81363989b23cf2164aDouglas Gregor  assert(NNS && "Missing nested-name-specifier in qualified template name");
52900f0ea2a96534c615ff5fdd81363989b23cf2164aDouglas Gregor
5291789b1f640205e81b5af250693246120f1ce9d147Douglas Gregor  // FIXME: Canonicalization?
5292d99cbe66403ee39c2ee58024b9582b95649a4fc5Douglas Gregor  llvm::FoldingSetNodeID ID;
5293d99cbe66403ee39c2ee58024b9582b95649a4fc5Douglas Gregor  QualifiedTemplateName::Profile(ID, NNS, TemplateKeyword, Template);
52941eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
5295d99cbe66403ee39c2ee58024b9582b95649a4fc5Douglas Gregor  void *InsertPos = 0;
5296d99cbe66403ee39c2ee58024b9582b95649a4fc5Douglas Gregor  QualifiedTemplateName *QTN =
52970bd6feb9e9d40fc889fd47e899985125a43dfed8John McCall    QualifiedTemplateNames.FindNodeOrInsertPos(ID, InsertPos);
5298d99cbe66403ee39c2ee58024b9582b95649a4fc5Douglas Gregor  if (!QTN) {
5299d99cbe66403ee39c2ee58024b9582b95649a4fc5Douglas Gregor    QTN = new (*this,4) QualifiedTemplateName(NNS, TemplateKeyword, Template);
5300d99cbe66403ee39c2ee58024b9582b95649a4fc5Douglas Gregor    QualifiedTemplateNames.InsertNode(QTN, InsertPos);
5301d99cbe66403ee39c2ee58024b9582b95649a4fc5Douglas Gregor  }
53021eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
5303d99cbe66403ee39c2ee58024b9582b95649a4fc5Douglas Gregor  return TemplateName(QTN);
5304d99cbe66403ee39c2ee58024b9582b95649a4fc5Douglas Gregor}
5305d99cbe66403ee39c2ee58024b9582b95649a4fc5Douglas Gregor
53067532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor/// \brief Retrieve the template name that represents a dependent
53077532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor/// template name such as \c MetaFun::template apply.
53084ba2a17694148e16eaa8d3917f657ffcd3667be4Jay FoadTemplateName
53094ba2a17694148e16eaa8d3917f657ffcd3667be4Jay FoadASTContext::getDependentTemplateName(NestedNameSpecifier *NNS,
53104ba2a17694148e16eaa8d3917f657ffcd3667be4Jay Foad                                     const IdentifierInfo *Name) const {
53111eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  assert((!NNS || NNS->isDependent()) &&
53123b6afbb99a1c44b4076f8e15fb7311405941b306Douglas Gregor         "Nested name specifier must be dependent");
53137532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor
53147532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor  llvm::FoldingSetNodeID ID;
53157532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor  DependentTemplateName::Profile(ID, NNS, Name);
53167532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor
53177532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor  void *InsertPos = 0;
53187532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor  DependentTemplateName *QTN =
53197532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor    DependentTemplateNames.FindNodeOrInsertPos(ID, InsertPos);
53207532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor
53217532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor  if (QTN)
53227532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor    return TemplateName(QTN);
53237532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor
53247532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor  NestedNameSpecifier *CanonNNS = getCanonicalNestedNameSpecifier(NNS);
53257532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor  if (CanonNNS == NNS) {
53267532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor    QTN = new (*this,4) DependentTemplateName(NNS, Name);
53277532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor  } else {
53287532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor    TemplateName Canon = getDependentTemplateName(CanonNNS, Name);
53297532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor    QTN = new (*this,4) DependentTemplateName(NNS, Name, Canon);
5330789b1f640205e81b5af250693246120f1ce9d147Douglas Gregor    DependentTemplateName *CheckQTN =
5331789b1f640205e81b5af250693246120f1ce9d147Douglas Gregor      DependentTemplateNames.FindNodeOrInsertPos(ID, InsertPos);
5332789b1f640205e81b5af250693246120f1ce9d147Douglas Gregor    assert(!CheckQTN && "Dependent type name canonicalization broken");
5333789b1f640205e81b5af250693246120f1ce9d147Douglas Gregor    (void)CheckQTN;
53347532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor  }
53357532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor
53367532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor  DependentTemplateNames.InsertNode(QTN, InsertPos);
53377532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor  return TemplateName(QTN);
53387532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor}
53397532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor
5340ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor/// \brief Retrieve the template name that represents a dependent
5341ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor/// template name such as \c MetaFun::template operator+.
5342ca1bdd7c269a2390d43c040a60511edd017ee130Douglas GregorTemplateName
5343ca1bdd7c269a2390d43c040a60511edd017ee130Douglas GregorASTContext::getDependentTemplateName(NestedNameSpecifier *NNS,
53444ba2a17694148e16eaa8d3917f657ffcd3667be4Jay Foad                                     OverloadedOperatorKind Operator) const {
5345ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor  assert((!NNS || NNS->isDependent()) &&
5346ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor         "Nested name specifier must be dependent");
5347ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor
5348ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor  llvm::FoldingSetNodeID ID;
5349ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor  DependentTemplateName::Profile(ID, NNS, Operator);
5350ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor
5351ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor  void *InsertPos = 0;
5352789b1f640205e81b5af250693246120f1ce9d147Douglas Gregor  DependentTemplateName *QTN
5353789b1f640205e81b5af250693246120f1ce9d147Douglas Gregor    = DependentTemplateNames.FindNodeOrInsertPos(ID, InsertPos);
5354ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor
5355ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor  if (QTN)
5356ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor    return TemplateName(QTN);
5357ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor
5358ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor  NestedNameSpecifier *CanonNNS = getCanonicalNestedNameSpecifier(NNS);
5359ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor  if (CanonNNS == NNS) {
5360ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor    QTN = new (*this,4) DependentTemplateName(NNS, Operator);
5361ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor  } else {
5362ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor    TemplateName Canon = getDependentTemplateName(CanonNNS, Operator);
5363ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor    QTN = new (*this,4) DependentTemplateName(NNS, Operator, Canon);
5364789b1f640205e81b5af250693246120f1ce9d147Douglas Gregor
5365789b1f640205e81b5af250693246120f1ce9d147Douglas Gregor    DependentTemplateName *CheckQTN
5366789b1f640205e81b5af250693246120f1ce9d147Douglas Gregor      = DependentTemplateNames.FindNodeOrInsertPos(ID, InsertPos);
5367789b1f640205e81b5af250693246120f1ce9d147Douglas Gregor    assert(!CheckQTN && "Dependent template name canonicalization broken");
5368789b1f640205e81b5af250693246120f1ce9d147Douglas Gregor    (void)CheckQTN;
5369ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor  }
5370ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor
5371ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor  DependentTemplateNames.InsertNode(QTN, InsertPos);
5372ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor  return TemplateName(QTN);
5373ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor}
5374ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor
53751aee05d08b2184acadeb36de300e216390780d6cDouglas GregorTemplateName
5376146060435c3efce95c95a092c7a1eb651cfb9ae0John McCallASTContext::getSubstTemplateTemplateParm(TemplateTemplateParmDecl *param,
5377146060435c3efce95c95a092c7a1eb651cfb9ae0John McCall                                         TemplateName replacement) const {
5378146060435c3efce95c95a092c7a1eb651cfb9ae0John McCall  llvm::FoldingSetNodeID ID;
5379146060435c3efce95c95a092c7a1eb651cfb9ae0John McCall  SubstTemplateTemplateParmStorage::Profile(ID, param, replacement);
5380146060435c3efce95c95a092c7a1eb651cfb9ae0John McCall
5381146060435c3efce95c95a092c7a1eb651cfb9ae0John McCall  void *insertPos = 0;
5382146060435c3efce95c95a092c7a1eb651cfb9ae0John McCall  SubstTemplateTemplateParmStorage *subst
5383146060435c3efce95c95a092c7a1eb651cfb9ae0John McCall    = SubstTemplateTemplateParms.FindNodeOrInsertPos(ID, insertPos);
5384146060435c3efce95c95a092c7a1eb651cfb9ae0John McCall
5385146060435c3efce95c95a092c7a1eb651cfb9ae0John McCall  if (!subst) {
5386146060435c3efce95c95a092c7a1eb651cfb9ae0John McCall    subst = new (*this) SubstTemplateTemplateParmStorage(param, replacement);
5387146060435c3efce95c95a092c7a1eb651cfb9ae0John McCall    SubstTemplateTemplateParms.InsertNode(subst, insertPos);
5388146060435c3efce95c95a092c7a1eb651cfb9ae0John McCall  }
5389146060435c3efce95c95a092c7a1eb651cfb9ae0John McCall
5390146060435c3efce95c95a092c7a1eb651cfb9ae0John McCall  return TemplateName(subst);
5391146060435c3efce95c95a092c7a1eb651cfb9ae0John McCall}
5392146060435c3efce95c95a092c7a1eb651cfb9ae0John McCall
5393146060435c3efce95c95a092c7a1eb651cfb9ae0John McCallTemplateName
53941aee05d08b2184acadeb36de300e216390780d6cDouglas GregorASTContext::getSubstTemplateTemplateParmPack(TemplateTemplateParmDecl *Param,
53951aee05d08b2184acadeb36de300e216390780d6cDouglas Gregor                                       const TemplateArgument &ArgPack) const {
53961aee05d08b2184acadeb36de300e216390780d6cDouglas Gregor  ASTContext &Self = const_cast<ASTContext &>(*this);
53971aee05d08b2184acadeb36de300e216390780d6cDouglas Gregor  llvm::FoldingSetNodeID ID;
53981aee05d08b2184acadeb36de300e216390780d6cDouglas Gregor  SubstTemplateTemplateParmPackStorage::Profile(ID, Self, Param, ArgPack);
53991aee05d08b2184acadeb36de300e216390780d6cDouglas Gregor
54001aee05d08b2184acadeb36de300e216390780d6cDouglas Gregor  void *InsertPos = 0;
54011aee05d08b2184acadeb36de300e216390780d6cDouglas Gregor  SubstTemplateTemplateParmPackStorage *Subst
54021aee05d08b2184acadeb36de300e216390780d6cDouglas Gregor    = SubstTemplateTemplateParmPacks.FindNodeOrInsertPos(ID, InsertPos);
54031aee05d08b2184acadeb36de300e216390780d6cDouglas Gregor
54041aee05d08b2184acadeb36de300e216390780d6cDouglas Gregor  if (!Subst) {
5405146060435c3efce95c95a092c7a1eb651cfb9ae0John McCall    Subst = new (*this) SubstTemplateTemplateParmPackStorage(Param,
54061aee05d08b2184acadeb36de300e216390780d6cDouglas Gregor                                                           ArgPack.pack_size(),
54071aee05d08b2184acadeb36de300e216390780d6cDouglas Gregor                                                         ArgPack.pack_begin());
54081aee05d08b2184acadeb36de300e216390780d6cDouglas Gregor    SubstTemplateTemplateParmPacks.InsertNode(Subst, InsertPos);
54091aee05d08b2184acadeb36de300e216390780d6cDouglas Gregor  }
54101aee05d08b2184acadeb36de300e216390780d6cDouglas Gregor
54111aee05d08b2184acadeb36de300e216390780d6cDouglas Gregor  return TemplateName(Subst);
54121aee05d08b2184acadeb36de300e216390780d6cDouglas Gregor}
54131aee05d08b2184acadeb36de300e216390780d6cDouglas Gregor
5414b4e66d5259f90e9aae4d40fc5de801e046c7df94Douglas Gregor/// getFromTargetType - Given one of the integer types provided by
5415d934112e6170b0fd940d8e40db6936cea2cdcf62Douglas Gregor/// TargetInfo, produce the corresponding type. The unsigned @p Type
5416d934112e6170b0fd940d8e40db6936cea2cdcf62Douglas Gregor/// is actually a value of type @c TargetInfo::IntType.
5417e27ec8ad56dbf1efb2de004b90fbbb86f740e3f1John McCallCanQualType ASTContext::getFromTargetType(unsigned Type) const {
5418b4e66d5259f90e9aae4d40fc5de801e046c7df94Douglas Gregor  switch (Type) {
5419e27ec8ad56dbf1efb2de004b90fbbb86f740e3f1John McCall  case TargetInfo::NoInt: return CanQualType();
5420b4e66d5259f90e9aae4d40fc5de801e046c7df94Douglas Gregor  case TargetInfo::SignedShort: return ShortTy;
5421b4e66d5259f90e9aae4d40fc5de801e046c7df94Douglas Gregor  case TargetInfo::UnsignedShort: return UnsignedShortTy;
5422b4e66d5259f90e9aae4d40fc5de801e046c7df94Douglas Gregor  case TargetInfo::SignedInt: return IntTy;
5423b4e66d5259f90e9aae4d40fc5de801e046c7df94Douglas Gregor  case TargetInfo::UnsignedInt: return UnsignedIntTy;
5424b4e66d5259f90e9aae4d40fc5de801e046c7df94Douglas Gregor  case TargetInfo::SignedLong: return LongTy;
5425b4e66d5259f90e9aae4d40fc5de801e046c7df94Douglas Gregor  case TargetInfo::UnsignedLong: return UnsignedLongTy;
5426b4e66d5259f90e9aae4d40fc5de801e046c7df94Douglas Gregor  case TargetInfo::SignedLongLong: return LongLongTy;
5427b4e66d5259f90e9aae4d40fc5de801e046c7df94Douglas Gregor  case TargetInfo::UnsignedLongLong: return UnsignedLongLongTy;
5428b4e66d5259f90e9aae4d40fc5de801e046c7df94Douglas Gregor  }
5429b4e66d5259f90e9aae4d40fc5de801e046c7df94Douglas Gregor
5430b219cfc4d75f0a03630b7c4509ef791b7e97b2c8David Blaikie  llvm_unreachable("Unhandled TargetInfo::IntType value");
5431b4e66d5259f90e9aae4d40fc5de801e046c7df94Douglas Gregor}
5432b6ccaac65ca72f72954eb3893bbd940bedd23f00Ted Kremenek
5433b6ccaac65ca72f72954eb3893bbd940bedd23f00Ted Kremenek//===----------------------------------------------------------------------===//
5434b6ccaac65ca72f72954eb3893bbd940bedd23f00Ted Kremenek//                        Type Predicates.
5435b6ccaac65ca72f72954eb3893bbd940bedd23f00Ted Kremenek//===----------------------------------------------------------------------===//
5436b6ccaac65ca72f72954eb3893bbd940bedd23f00Ted Kremenek
54374fd83ea566f4a0c083001c84b75da6cc8c99c1d6Fariborz Jahanian/// getObjCGCAttr - Returns one of GCNone, Weak or Strong objc's
54384fd83ea566f4a0c083001c84b75da6cc8c99c1d6Fariborz Jahanian/// garbage collection attribute.
54394fd83ea566f4a0c083001c84b75da6cc8c99c1d6Fariborz Jahanian///
5440ae278a3a57595349a411f6474938d4dd1b263a0eJohn McCallQualifiers::GC ASTContext::getObjCGCAttrKind(QualType Ty) const {
54414e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie  if (getLangOpts().getGC() == LangOptions::NonGC)
5442ae278a3a57595349a411f6474938d4dd1b263a0eJohn McCall    return Qualifiers::GCNone;
5443ae278a3a57595349a411f6474938d4dd1b263a0eJohn McCall
54444e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie  assert(getLangOpts().ObjC1);
5445ae278a3a57595349a411f6474938d4dd1b263a0eJohn McCall  Qualifiers::GC GCAttrs = Ty.getObjCGCAttr();
5446ae278a3a57595349a411f6474938d4dd1b263a0eJohn McCall
5447ae278a3a57595349a411f6474938d4dd1b263a0eJohn McCall  // Default behaviour under objective-C's gc is for ObjC pointers
5448ae278a3a57595349a411f6474938d4dd1b263a0eJohn McCall  // (or pointers to them) be treated as though they were declared
5449ae278a3a57595349a411f6474938d4dd1b263a0eJohn McCall  // as __strong.
5450ae278a3a57595349a411f6474938d4dd1b263a0eJohn McCall  if (GCAttrs == Qualifiers::GCNone) {
5451ae278a3a57595349a411f6474938d4dd1b263a0eJohn McCall    if (Ty->isObjCObjectPointerType() || Ty->isBlockPointerType())
5452ae278a3a57595349a411f6474938d4dd1b263a0eJohn McCall      return Qualifiers::Strong;
5453ae278a3a57595349a411f6474938d4dd1b263a0eJohn McCall    else if (Ty->isPointerType())
5454ae278a3a57595349a411f6474938d4dd1b263a0eJohn McCall      return getObjCGCAttrKind(Ty->getAs<PointerType>()->getPointeeType());
5455ae278a3a57595349a411f6474938d4dd1b263a0eJohn McCall  } else {
5456ae278a3a57595349a411f6474938d4dd1b263a0eJohn McCall    // It's not valid to set GC attributes on anything that isn't a
5457ae278a3a57595349a411f6474938d4dd1b263a0eJohn McCall    // pointer.
5458ae278a3a57595349a411f6474938d4dd1b263a0eJohn McCall#ifndef NDEBUG
5459ae278a3a57595349a411f6474938d4dd1b263a0eJohn McCall    QualType CT = Ty->getCanonicalTypeInternal();
5460ae278a3a57595349a411f6474938d4dd1b263a0eJohn McCall    while (const ArrayType *AT = dyn_cast<ArrayType>(CT))
5461ae278a3a57595349a411f6474938d4dd1b263a0eJohn McCall      CT = AT->getElementType();
5462ae278a3a57595349a411f6474938d4dd1b263a0eJohn McCall    assert(CT->isAnyPointerType() || CT->isBlockPointerType());
5463ae278a3a57595349a411f6474938d4dd1b263a0eJohn McCall#endif
54644fd83ea566f4a0c083001c84b75da6cc8c99c1d6Fariborz Jahanian  }
5465b7d2553edd2532d29b98b9e76bcf6a62bc48b417Chris Lattner  return GCAttrs;
54664fd83ea566f4a0c083001c84b75da6cc8c99c1d6Fariborz Jahanian}
54674fd83ea566f4a0c083001c84b75da6cc8c99c1d6Fariborz Jahanian
54686ac46a4a521366d7ab36ebe2ce4e624ab96b06f9Chris Lattner//===----------------------------------------------------------------------===//
54696ac46a4a521366d7ab36ebe2ce4e624ab96b06f9Chris Lattner//                        Type Compatibility Testing
54706ac46a4a521366d7ab36ebe2ce4e624ab96b06f9Chris Lattner//===----------------------------------------------------------------------===//
5471770951b5bb6028a8d326ddb4a13cef7d4a128162Chris Lattner
54721eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// areCompatVectorTypes - Return true if the two specified vector types are
54736ac46a4a521366d7ab36ebe2ce4e624ab96b06f9Chris Lattner/// compatible.
54746ac46a4a521366d7ab36ebe2ce4e624ab96b06f9Chris Lattnerstatic bool areCompatVectorTypes(const VectorType *LHS,
54756ac46a4a521366d7ab36ebe2ce4e624ab96b06f9Chris Lattner                                 const VectorType *RHS) {
5476467b27b9a24bdc823218ad1ad0e37673b6cc1e83John McCall  assert(LHS->isCanonicalUnqualified() && RHS->isCanonicalUnqualified());
54776ac46a4a521366d7ab36ebe2ce4e624ab96b06f9Chris Lattner  return LHS->getElementType() == RHS->getElementType() &&
547861710854be2b098428aff5316e64bd34b30fbcb7Chris Lattner         LHS->getNumElements() == RHS->getNumElements();
54796ac46a4a521366d7ab36ebe2ce4e624ab96b06f9Chris Lattner}
54806ac46a4a521366d7ab36ebe2ce4e624ab96b06f9Chris Lattner
5481255210ef415b9893f0e3794e8d9a704194c12f3cDouglas Gregorbool ASTContext::areCompatibleVectorTypes(QualType FirstVec,
5482255210ef415b9893f0e3794e8d9a704194c12f3cDouglas Gregor                                          QualType SecondVec) {
5483255210ef415b9893f0e3794e8d9a704194c12f3cDouglas Gregor  assert(FirstVec->isVectorType() && "FirstVec should be a vector type");
5484255210ef415b9893f0e3794e8d9a704194c12f3cDouglas Gregor  assert(SecondVec->isVectorType() && "SecondVec should be a vector type");
5485255210ef415b9893f0e3794e8d9a704194c12f3cDouglas Gregor
5486255210ef415b9893f0e3794e8d9a704194c12f3cDouglas Gregor  if (hasSameUnqualifiedType(FirstVec, SecondVec))
5487255210ef415b9893f0e3794e8d9a704194c12f3cDouglas Gregor    return true;
5488255210ef415b9893f0e3794e8d9a704194c12f3cDouglas Gregor
5489f69eb7cf8e616b5aad7911ec6f79b24b0a009227Bob Wilson  // Treat Neon vector types and most AltiVec vector types as if they are the
5490f69eb7cf8e616b5aad7911ec6f79b24b0a009227Bob Wilson  // equivalent GCC vector types.
5491255210ef415b9893f0e3794e8d9a704194c12f3cDouglas Gregor  const VectorType *First = FirstVec->getAs<VectorType>();
5492255210ef415b9893f0e3794e8d9a704194c12f3cDouglas Gregor  const VectorType *Second = SecondVec->getAs<VectorType>();
5493f69eb7cf8e616b5aad7911ec6f79b24b0a009227Bob Wilson  if (First->getNumElements() == Second->getNumElements() &&
5494255210ef415b9893f0e3794e8d9a704194c12f3cDouglas Gregor      hasSameType(First->getElementType(), Second->getElementType()) &&
5495f69eb7cf8e616b5aad7911ec6f79b24b0a009227Bob Wilson      First->getVectorKind() != VectorType::AltiVecPixel &&
5496f69eb7cf8e616b5aad7911ec6f79b24b0a009227Bob Wilson      First->getVectorKind() != VectorType::AltiVecBool &&
5497f69eb7cf8e616b5aad7911ec6f79b24b0a009227Bob Wilson      Second->getVectorKind() != VectorType::AltiVecPixel &&
5498f69eb7cf8e616b5aad7911ec6f79b24b0a009227Bob Wilson      Second->getVectorKind() != VectorType::AltiVecBool)
5499255210ef415b9893f0e3794e8d9a704194c12f3cDouglas Gregor    return true;
5500255210ef415b9893f0e3794e8d9a704194c12f3cDouglas Gregor
5501255210ef415b9893f0e3794e8d9a704194c12f3cDouglas Gregor  return false;
5502255210ef415b9893f0e3794e8d9a704194c12f3cDouglas Gregor}
5503255210ef415b9893f0e3794e8d9a704194c12f3cDouglas Gregor
55044084c306635b70f37029dca938444e6013f08684Steve Naroff//===----------------------------------------------------------------------===//
55054084c306635b70f37029dca938444e6013f08684Steve Naroff// ObjCQualifiedIdTypesAreCompatible - Compatibility testing for qualified id's.
55064084c306635b70f37029dca938444e6013f08684Steve Naroff//===----------------------------------------------------------------------===//
55074084c306635b70f37029dca938444e6013f08684Steve Naroff
55084084c306635b70f37029dca938444e6013f08684Steve Naroff/// ProtocolCompatibleWithProtocol - return 'true' if 'lProto' is in the
55094084c306635b70f37029dca938444e6013f08684Steve Naroff/// inheritance hierarchy of 'rProto'.
55104ba2a17694148e16eaa8d3917f657ffcd3667be4Jay Foadbool
55114ba2a17694148e16eaa8d3917f657ffcd3667be4Jay FoadASTContext::ProtocolCompatibleWithProtocol(ObjCProtocolDecl *lProto,
55124ba2a17694148e16eaa8d3917f657ffcd3667be4Jay Foad                                           ObjCProtocolDecl *rProto) const {
55133fc73ee0c613715ebce78e30b4d050ea715a007dDouglas Gregor  if (declaresSameEntity(lProto, rProto))
551414108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff    return true;
55154084c306635b70f37029dca938444e6013f08684Steve Naroff  for (ObjCProtocolDecl::protocol_iterator PI = rProto->protocol_begin(),
55164084c306635b70f37029dca938444e6013f08684Steve Naroff       E = rProto->protocol_end(); PI != E; ++PI)
55174084c306635b70f37029dca938444e6013f08684Steve Naroff    if (ProtocolCompatibleWithProtocol(lProto, *PI))
55184084c306635b70f37029dca938444e6013f08684Steve Naroff      return true;
55194084c306635b70f37029dca938444e6013f08684Steve Naroff  return false;
55204084c306635b70f37029dca938444e6013f08684Steve Naroff}
552114108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff
55224084c306635b70f37029dca938444e6013f08684Steve Naroff/// QualifiedIdConformsQualifiedId - compare id<p,...> with id<p1,...>
55234084c306635b70f37029dca938444e6013f08684Steve Naroff/// return true if lhs's protocols conform to rhs's protocol; false
55244084c306635b70f37029dca938444e6013f08684Steve Naroff/// otherwise.
55254084c306635b70f37029dca938444e6013f08684Steve Naroffbool ASTContext::QualifiedIdConformsQualifiedId(QualType lhs, QualType rhs) {
55264084c306635b70f37029dca938444e6013f08684Steve Naroff  if (lhs->isObjCQualifiedIdType() && rhs->isObjCQualifiedIdType())
55274084c306635b70f37029dca938444e6013f08684Steve Naroff    return ObjCQualifiedIdTypesAreCompatible(lhs, rhs, false);
55284084c306635b70f37029dca938444e6013f08684Steve Naroff  return false;
55294084c306635b70f37029dca938444e6013f08684Steve Naroff}
55304084c306635b70f37029dca938444e6013f08684Steve Naroff
5531a8f8dac6a29f6d33474a38a32ce9dd859b696da9Fariborz Jahanian/// ObjCQualifiedClassTypesAreCompatible - compare  Class<p,...> and
5532a8f8dac6a29f6d33474a38a32ce9dd859b696da9Fariborz Jahanian/// Class<p1, ...>.
5533a8f8dac6a29f6d33474a38a32ce9dd859b696da9Fariborz Jahanianbool ASTContext::ObjCQualifiedClassTypesAreCompatible(QualType lhs,
5534a8f8dac6a29f6d33474a38a32ce9dd859b696da9Fariborz Jahanian                                                      QualType rhs) {
5535a8f8dac6a29f6d33474a38a32ce9dd859b696da9Fariborz Jahanian  const ObjCObjectPointerType *lhsQID = lhs->getAs<ObjCObjectPointerType>();
5536a8f8dac6a29f6d33474a38a32ce9dd859b696da9Fariborz Jahanian  const ObjCObjectPointerType *rhsOPT = rhs->getAs<ObjCObjectPointerType>();
5537a8f8dac6a29f6d33474a38a32ce9dd859b696da9Fariborz Jahanian  assert ((lhsQID && rhsOPT) && "ObjCQualifiedClassTypesAreCompatible");
5538a8f8dac6a29f6d33474a38a32ce9dd859b696da9Fariborz Jahanian
5539a8f8dac6a29f6d33474a38a32ce9dd859b696da9Fariborz Jahanian  for (ObjCObjectPointerType::qual_iterator I = lhsQID->qual_begin(),
5540a8f8dac6a29f6d33474a38a32ce9dd859b696da9Fariborz Jahanian       E = lhsQID->qual_end(); I != E; ++I) {
5541a8f8dac6a29f6d33474a38a32ce9dd859b696da9Fariborz Jahanian    bool match = false;
5542a8f8dac6a29f6d33474a38a32ce9dd859b696da9Fariborz Jahanian    ObjCProtocolDecl *lhsProto = *I;
5543a8f8dac6a29f6d33474a38a32ce9dd859b696da9Fariborz Jahanian    for (ObjCObjectPointerType::qual_iterator J = rhsOPT->qual_begin(),
5544a8f8dac6a29f6d33474a38a32ce9dd859b696da9Fariborz Jahanian         E = rhsOPT->qual_end(); J != E; ++J) {
5545a8f8dac6a29f6d33474a38a32ce9dd859b696da9Fariborz Jahanian      ObjCProtocolDecl *rhsProto = *J;
5546a8f8dac6a29f6d33474a38a32ce9dd859b696da9Fariborz Jahanian      if (ProtocolCompatibleWithProtocol(lhsProto, rhsProto)) {
5547a8f8dac6a29f6d33474a38a32ce9dd859b696da9Fariborz Jahanian        match = true;
5548a8f8dac6a29f6d33474a38a32ce9dd859b696da9Fariborz Jahanian        break;
5549a8f8dac6a29f6d33474a38a32ce9dd859b696da9Fariborz Jahanian      }
5550a8f8dac6a29f6d33474a38a32ce9dd859b696da9Fariborz Jahanian    }
5551a8f8dac6a29f6d33474a38a32ce9dd859b696da9Fariborz Jahanian    if (!match)
5552a8f8dac6a29f6d33474a38a32ce9dd859b696da9Fariborz Jahanian      return false;
5553a8f8dac6a29f6d33474a38a32ce9dd859b696da9Fariborz Jahanian  }
5554a8f8dac6a29f6d33474a38a32ce9dd859b696da9Fariborz Jahanian  return true;
5555a8f8dac6a29f6d33474a38a32ce9dd859b696da9Fariborz Jahanian}
5556a8f8dac6a29f6d33474a38a32ce9dd859b696da9Fariborz Jahanian
55574084c306635b70f37029dca938444e6013f08684Steve Naroff/// ObjCQualifiedIdTypesAreCompatible - We know that one of lhs/rhs is an
55584084c306635b70f37029dca938444e6013f08684Steve Naroff/// ObjCQualifiedIDType.
55594084c306635b70f37029dca938444e6013f08684Steve Naroffbool ASTContext::ObjCQualifiedIdTypesAreCompatible(QualType lhs, QualType rhs,
55604084c306635b70f37029dca938444e6013f08684Steve Naroff                                                   bool compare) {
55614084c306635b70f37029dca938444e6013f08684Steve Naroff  // Allow id<P..> and an 'id' or void* type in all cases.
55621eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  if (lhs->isVoidPointerType() ||
55634084c306635b70f37029dca938444e6013f08684Steve Naroff      lhs->isObjCIdType() || lhs->isObjCClassType())
55644084c306635b70f37029dca938444e6013f08684Steve Naroff    return true;
55651eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  else if (rhs->isVoidPointerType() ||
55664084c306635b70f37029dca938444e6013f08684Steve Naroff           rhs->isObjCIdType() || rhs->isObjCClassType())
55674084c306635b70f37029dca938444e6013f08684Steve Naroff    return true;
55684084c306635b70f37029dca938444e6013f08684Steve Naroff
55694084c306635b70f37029dca938444e6013f08684Steve Naroff  if (const ObjCObjectPointerType *lhsQID = lhs->getAsObjCQualifiedIdType()) {
5570183700f494ec9b6701b6efe82bcb25f4c79ba561John McCall    const ObjCObjectPointerType *rhsOPT = rhs->getAs<ObjCObjectPointerType>();
55711eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
55724084c306635b70f37029dca938444e6013f08684Steve Naroff    if (!rhsOPT) return false;
55731eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
55744084c306635b70f37029dca938444e6013f08684Steve Naroff    if (rhsOPT->qual_empty()) {
55751eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      // If the RHS is a unqualified interface pointer "NSString*",
55764084c306635b70f37029dca938444e6013f08684Steve Naroff      // make sure we check the class hierarchy.
55774084c306635b70f37029dca938444e6013f08684Steve Naroff      if (ObjCInterfaceDecl *rhsID = rhsOPT->getInterfaceDecl()) {
55784084c306635b70f37029dca938444e6013f08684Steve Naroff        for (ObjCObjectPointerType::qual_iterator I = lhsQID->qual_begin(),
55794084c306635b70f37029dca938444e6013f08684Steve Naroff             E = lhsQID->qual_end(); I != E; ++I) {
55804084c306635b70f37029dca938444e6013f08684Steve Naroff          // when comparing an id<P> on lhs with a static type on rhs,
55814084c306635b70f37029dca938444e6013f08684Steve Naroff          // see if static class implements all of id's protocols, directly or
55824084c306635b70f37029dca938444e6013f08684Steve Naroff          // through its super class and categories.
55830fd8904c5f71a11d29f67716c3ebdf7ad1c855fbFariborz Jahanian          if (!rhsID->ClassImplementsProtocol(*I, true))
55844084c306635b70f37029dca938444e6013f08684Steve Naroff            return false;
55854084c306635b70f37029dca938444e6013f08684Steve Naroff        }
55864084c306635b70f37029dca938444e6013f08684Steve Naroff      }
55874084c306635b70f37029dca938444e6013f08684Steve Naroff      // If there are no qualifiers and no interface, we have an 'id'.
55884084c306635b70f37029dca938444e6013f08684Steve Naroff      return true;
55894084c306635b70f37029dca938444e6013f08684Steve Naroff    }
55901eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    // Both the right and left sides have qualifiers.
55914084c306635b70f37029dca938444e6013f08684Steve Naroff    for (ObjCObjectPointerType::qual_iterator I = lhsQID->qual_begin(),
55924084c306635b70f37029dca938444e6013f08684Steve Naroff         E = lhsQID->qual_end(); I != E; ++I) {
55934084c306635b70f37029dca938444e6013f08684Steve Naroff      ObjCProtocolDecl *lhsProto = *I;
55944084c306635b70f37029dca938444e6013f08684Steve Naroff      bool match = false;
5595de2e22d33afec98324a66a358dfe0951b3c7259aSteve Naroff
5596de2e22d33afec98324a66a358dfe0951b3c7259aSteve Naroff      // when comparing an id<P> on lhs with a static type on rhs,
5597de2e22d33afec98324a66a358dfe0951b3c7259aSteve Naroff      // see if static class implements all of id's protocols, directly or
5598de2e22d33afec98324a66a358dfe0951b3c7259aSteve Naroff      // through its super class and categories.
55994084c306635b70f37029dca938444e6013f08684Steve Naroff      for (ObjCObjectPointerType::qual_iterator J = rhsOPT->qual_begin(),
56004084c306635b70f37029dca938444e6013f08684Steve Naroff           E = rhsOPT->qual_end(); J != E; ++J) {
56014084c306635b70f37029dca938444e6013f08684Steve Naroff        ObjCProtocolDecl *rhsProto = *J;
56024084c306635b70f37029dca938444e6013f08684Steve Naroff        if (ProtocolCompatibleWithProtocol(lhsProto, rhsProto) ||
56034084c306635b70f37029dca938444e6013f08684Steve Naroff            (compare && ProtocolCompatibleWithProtocol(rhsProto, lhsProto))) {
56044084c306635b70f37029dca938444e6013f08684Steve Naroff          match = true;
56058f16756441450ed9fb39316e47d107fc2a1ef35bSteve Naroff          break;
56068f16756441450ed9fb39316e47d107fc2a1ef35bSteve Naroff        }
5607de2e22d33afec98324a66a358dfe0951b3c7259aSteve Naroff      }
56081eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      // If the RHS is a qualified interface pointer "NSString<P>*",
56094084c306635b70f37029dca938444e6013f08684Steve Naroff      // make sure we check the class hierarchy.
56104084c306635b70f37029dca938444e6013f08684Steve Naroff      if (ObjCInterfaceDecl *rhsID = rhsOPT->getInterfaceDecl()) {
56114084c306635b70f37029dca938444e6013f08684Steve Naroff        for (ObjCObjectPointerType::qual_iterator I = lhsQID->qual_begin(),
56124084c306635b70f37029dca938444e6013f08684Steve Naroff             E = lhsQID->qual_end(); I != E; ++I) {
56134084c306635b70f37029dca938444e6013f08684Steve Naroff          // when comparing an id<P> on lhs with a static type on rhs,
56144084c306635b70f37029dca938444e6013f08684Steve Naroff          // see if static class implements all of id's protocols, directly or
56154084c306635b70f37029dca938444e6013f08684Steve Naroff          // through its super class and categories.
56160fd8904c5f71a11d29f67716c3ebdf7ad1c855fbFariborz Jahanian          if (rhsID->ClassImplementsProtocol(*I, true)) {
56174084c306635b70f37029dca938444e6013f08684Steve Naroff            match = true;
56184084c306635b70f37029dca938444e6013f08684Steve Naroff            break;
56194084c306635b70f37029dca938444e6013f08684Steve Naroff          }
56204084c306635b70f37029dca938444e6013f08684Steve Naroff        }
56214084c306635b70f37029dca938444e6013f08684Steve Naroff      }
56224084c306635b70f37029dca938444e6013f08684Steve Naroff      if (!match)
5623de2e22d33afec98324a66a358dfe0951b3c7259aSteve Naroff        return false;
5624de2e22d33afec98324a66a358dfe0951b3c7259aSteve Naroff    }
56251eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
5626de2e22d33afec98324a66a358dfe0951b3c7259aSteve Naroff    return true;
5627de2e22d33afec98324a66a358dfe0951b3c7259aSteve Naroff  }
56281eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
56294084c306635b70f37029dca938444e6013f08684Steve Naroff  const ObjCObjectPointerType *rhsQID = rhs->getAsObjCQualifiedIdType();
56304084c306635b70f37029dca938444e6013f08684Steve Naroff  assert(rhsQID && "One of the LHS/RHS should be id<x>");
56314084c306635b70f37029dca938444e6013f08684Steve Naroff
56321eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  if (const ObjCObjectPointerType *lhsOPT =
56334084c306635b70f37029dca938444e6013f08684Steve Naroff        lhs->getAsObjCInterfacePointerType()) {
5634de5b17ea3c74d2844ed035a1edfa6479866139b5Fariborz Jahanian    // If both the right and left sides have qualifiers.
56354084c306635b70f37029dca938444e6013f08684Steve Naroff    for (ObjCObjectPointerType::qual_iterator I = lhsOPT->qual_begin(),
56364084c306635b70f37029dca938444e6013f08684Steve Naroff         E = lhsOPT->qual_end(); I != E; ++I) {
56374084c306635b70f37029dca938444e6013f08684Steve Naroff      ObjCProtocolDecl *lhsProto = *I;
56384084c306635b70f37029dca938444e6013f08684Steve Naroff      bool match = false;
56394084c306635b70f37029dca938444e6013f08684Steve Naroff
5640de5b17ea3c74d2844ed035a1edfa6479866139b5Fariborz Jahanian      // when comparing an id<P> on rhs with a static type on lhs,
56414084c306635b70f37029dca938444e6013f08684Steve Naroff      // see if static class implements all of id's protocols, directly or
56424084c306635b70f37029dca938444e6013f08684Steve Naroff      // through its super class and categories.
5643de5b17ea3c74d2844ed035a1edfa6479866139b5Fariborz Jahanian      // First, lhs protocols in the qualifier list must be found, direct
5644de5b17ea3c74d2844ed035a1edfa6479866139b5Fariborz Jahanian      // or indirect in rhs's qualifier list or it is a mismatch.
56454084c306635b70f37029dca938444e6013f08684Steve Naroff      for (ObjCObjectPointerType::qual_iterator J = rhsQID->qual_begin(),
56464084c306635b70f37029dca938444e6013f08684Steve Naroff           E = rhsQID->qual_end(); J != E; ++J) {
56474084c306635b70f37029dca938444e6013f08684Steve Naroff        ObjCProtocolDecl *rhsProto = *J;
56484084c306635b70f37029dca938444e6013f08684Steve Naroff        if (ProtocolCompatibleWithProtocol(lhsProto, rhsProto) ||
56494084c306635b70f37029dca938444e6013f08684Steve Naroff            (compare && ProtocolCompatibleWithProtocol(rhsProto, lhsProto))) {
56504084c306635b70f37029dca938444e6013f08684Steve Naroff          match = true;
56514084c306635b70f37029dca938444e6013f08684Steve Naroff          break;
56524084c306635b70f37029dca938444e6013f08684Steve Naroff        }
56534084c306635b70f37029dca938444e6013f08684Steve Naroff      }
56544084c306635b70f37029dca938444e6013f08684Steve Naroff      if (!match)
56554084c306635b70f37029dca938444e6013f08684Steve Naroff        return false;
56564084c306635b70f37029dca938444e6013f08684Steve Naroff    }
5657de5b17ea3c74d2844ed035a1edfa6479866139b5Fariborz Jahanian
5658de5b17ea3c74d2844ed035a1edfa6479866139b5Fariborz Jahanian    // Static class's protocols, or its super class or category protocols
5659de5b17ea3c74d2844ed035a1edfa6479866139b5Fariborz Jahanian    // must be found, direct or indirect in rhs's qualifier list or it is a mismatch.
5660de5b17ea3c74d2844ed035a1edfa6479866139b5Fariborz Jahanian    if (ObjCInterfaceDecl *lhsID = lhsOPT->getInterfaceDecl()) {
5661de5b17ea3c74d2844ed035a1edfa6479866139b5Fariborz Jahanian      llvm::SmallPtrSet<ObjCProtocolDecl *, 8> LHSInheritedProtocols;
5662de5b17ea3c74d2844ed035a1edfa6479866139b5Fariborz Jahanian      CollectInheritedProtocols(lhsID, LHSInheritedProtocols);
5663de5b17ea3c74d2844ed035a1edfa6479866139b5Fariborz Jahanian      // This is rather dubious but matches gcc's behavior. If lhs has
5664de5b17ea3c74d2844ed035a1edfa6479866139b5Fariborz Jahanian      // no type qualifier and its class has no static protocol(s)
5665de5b17ea3c74d2844ed035a1edfa6479866139b5Fariborz Jahanian      // assume that it is mismatch.
5666de5b17ea3c74d2844ed035a1edfa6479866139b5Fariborz Jahanian      if (LHSInheritedProtocols.empty() && lhsOPT->qual_empty())
5667de5b17ea3c74d2844ed035a1edfa6479866139b5Fariborz Jahanian        return false;
5668de5b17ea3c74d2844ed035a1edfa6479866139b5Fariborz Jahanian      for (llvm::SmallPtrSet<ObjCProtocolDecl*,8>::iterator I =
5669de5b17ea3c74d2844ed035a1edfa6479866139b5Fariborz Jahanian           LHSInheritedProtocols.begin(),
5670de5b17ea3c74d2844ed035a1edfa6479866139b5Fariborz Jahanian           E = LHSInheritedProtocols.end(); I != E; ++I) {
5671de5b17ea3c74d2844ed035a1edfa6479866139b5Fariborz Jahanian        bool match = false;
5672de5b17ea3c74d2844ed035a1edfa6479866139b5Fariborz Jahanian        ObjCProtocolDecl *lhsProto = (*I);
5673de5b17ea3c74d2844ed035a1edfa6479866139b5Fariborz Jahanian        for (ObjCObjectPointerType::qual_iterator J = rhsQID->qual_begin(),
5674de5b17ea3c74d2844ed035a1edfa6479866139b5Fariborz Jahanian             E = rhsQID->qual_end(); J != E; ++J) {
5675de5b17ea3c74d2844ed035a1edfa6479866139b5Fariborz Jahanian          ObjCProtocolDecl *rhsProto = *J;
5676de5b17ea3c74d2844ed035a1edfa6479866139b5Fariborz Jahanian          if (ProtocolCompatibleWithProtocol(lhsProto, rhsProto) ||
5677de5b17ea3c74d2844ed035a1edfa6479866139b5Fariborz Jahanian              (compare && ProtocolCompatibleWithProtocol(rhsProto, lhsProto))) {
5678de5b17ea3c74d2844ed035a1edfa6479866139b5Fariborz Jahanian            match = true;
5679de5b17ea3c74d2844ed035a1edfa6479866139b5Fariborz Jahanian            break;
5680de5b17ea3c74d2844ed035a1edfa6479866139b5Fariborz Jahanian          }
5681de5b17ea3c74d2844ed035a1edfa6479866139b5Fariborz Jahanian        }
5682de5b17ea3c74d2844ed035a1edfa6479866139b5Fariborz Jahanian        if (!match)
5683de5b17ea3c74d2844ed035a1edfa6479866139b5Fariborz Jahanian          return false;
5684de5b17ea3c74d2844ed035a1edfa6479866139b5Fariborz Jahanian      }
5685de5b17ea3c74d2844ed035a1edfa6479866139b5Fariborz Jahanian    }
56864084c306635b70f37029dca938444e6013f08684Steve Naroff    return true;
56874084c306635b70f37029dca938444e6013f08684Steve Naroff  }
56884084c306635b70f37029dca938444e6013f08684Steve Naroff  return false;
56894084c306635b70f37029dca938444e6013f08684Steve Naroff}
56904084c306635b70f37029dca938444e6013f08684Steve Naroff
56914084c306635b70f37029dca938444e6013f08684Steve Naroff/// canAssignObjCInterfaces - Return true if the two interface types are
56924084c306635b70f37029dca938444e6013f08684Steve Naroff/// compatible for assignment from RHS to LHS.  This handles validation of any
56934084c306635b70f37029dca938444e6013f08684Steve Naroff/// protocol qualifiers on the LHS or RHS.
56944084c306635b70f37029dca938444e6013f08684Steve Naroff///
56954084c306635b70f37029dca938444e6013f08684Steve Naroffbool ASTContext::canAssignObjCInterfaces(const ObjCObjectPointerType *LHSOPT,
56964084c306635b70f37029dca938444e6013f08684Steve Naroff                                         const ObjCObjectPointerType *RHSOPT) {
5697c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall  const ObjCObjectType* LHS = LHSOPT->getObjectType();
5698c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall  const ObjCObjectType* RHS = RHSOPT->getObjectType();
5699c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall
57004084c306635b70f37029dca938444e6013f08684Steve Naroff  // If either type represents the built-in 'id' or 'Class' types, return true.
5701c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall  if (LHS->isObjCUnqualifiedIdOrClass() ||
5702c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall      RHS->isObjCUnqualifiedIdOrClass())
57034084c306635b70f37029dca938444e6013f08684Steve Naroff    return true;
57044084c306635b70f37029dca938444e6013f08684Steve Naroff
5705c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall  if (LHS->isObjCQualifiedId() || RHS->isObjCQualifiedId())
57061eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    return ObjCQualifiedIdTypesAreCompatible(QualType(LHSOPT,0),
57071eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump                                             QualType(RHSOPT,0),
57084084c306635b70f37029dca938444e6013f08684Steve Naroff                                             false);
5709a8f8dac6a29f6d33474a38a32ce9dd859b696da9Fariborz Jahanian
5710a8f8dac6a29f6d33474a38a32ce9dd859b696da9Fariborz Jahanian  if (LHS->isObjCQualifiedClass() && RHS->isObjCQualifiedClass())
5711a8f8dac6a29f6d33474a38a32ce9dd859b696da9Fariborz Jahanian    return ObjCQualifiedClassTypesAreCompatible(QualType(LHSOPT,0),
5712a8f8dac6a29f6d33474a38a32ce9dd859b696da9Fariborz Jahanian                                                QualType(RHSOPT,0));
5713a8f8dac6a29f6d33474a38a32ce9dd859b696da9Fariborz Jahanian
5714c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall  // If we have 2 user-defined types, fall into that path.
5715c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall  if (LHS->getInterface() && RHS->getInterface())
57164084c306635b70f37029dca938444e6013f08684Steve Naroff    return canAssignObjCInterfaces(LHS, RHS);
57171eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
57184084c306635b70f37029dca938444e6013f08684Steve Naroff  return false;
571914108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff}
572014108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff
5721132f2a2da34f378fc675b9e174564b0f52c31d98Fariborz Jahanian/// canAssignObjCInterfacesInBlockPointer - This routine is specifically written
5722fc8f0e14ad142ed811e90fbd9a30e419e301c717Chris Lattner/// for providing type-safety for objective-c pointers used to pass/return
5723132f2a2da34f378fc675b9e174564b0f52c31d98Fariborz Jahanian/// arguments in block literals. When passed as arguments, passing 'A*' where
5724132f2a2da34f378fc675b9e174564b0f52c31d98Fariborz Jahanian/// 'id' is expected is not OK. Passing 'Sub *" where 'Super *" is expected is
5725132f2a2da34f378fc675b9e174564b0f52c31d98Fariborz Jahanian/// not OK. For the return type, the opposite is not OK.
5726132f2a2da34f378fc675b9e174564b0f52c31d98Fariborz Jahanianbool ASTContext::canAssignObjCInterfacesInBlockPointer(
5727132f2a2da34f378fc675b9e174564b0f52c31d98Fariborz Jahanian                                         const ObjCObjectPointerType *LHSOPT,
5728a4fdbfad150ae37bddaa4094d3925a27a1a1cf3fFariborz Jahanian                                         const ObjCObjectPointerType *RHSOPT,
5729a4fdbfad150ae37bddaa4094d3925a27a1a1cf3fFariborz Jahanian                                         bool BlockReturnType) {
5730a98344869c278295f38e106583906377520079b8Fariborz Jahanian  if (RHSOPT->isObjCBuiltinType() || LHSOPT->isObjCIdType())
5731132f2a2da34f378fc675b9e174564b0f52c31d98Fariborz Jahanian    return true;
5732132f2a2da34f378fc675b9e174564b0f52c31d98Fariborz Jahanian
5733132f2a2da34f378fc675b9e174564b0f52c31d98Fariborz Jahanian  if (LHSOPT->isObjCBuiltinType()) {
5734132f2a2da34f378fc675b9e174564b0f52c31d98Fariborz Jahanian    return RHSOPT->isObjCBuiltinType() || RHSOPT->isObjCQualifiedIdType();
5735132f2a2da34f378fc675b9e174564b0f52c31d98Fariborz Jahanian  }
5736132f2a2da34f378fc675b9e174564b0f52c31d98Fariborz Jahanian
5737a98344869c278295f38e106583906377520079b8Fariborz Jahanian  if (LHSOPT->isObjCQualifiedIdType() || RHSOPT->isObjCQualifiedIdType())
5738132f2a2da34f378fc675b9e174564b0f52c31d98Fariborz Jahanian    return ObjCQualifiedIdTypesAreCompatible(QualType(LHSOPT,0),
5739132f2a2da34f378fc675b9e174564b0f52c31d98Fariborz Jahanian                                             QualType(RHSOPT,0),
5740132f2a2da34f378fc675b9e174564b0f52c31d98Fariborz Jahanian                                             false);
5741132f2a2da34f378fc675b9e174564b0f52c31d98Fariborz Jahanian
5742132f2a2da34f378fc675b9e174564b0f52c31d98Fariborz Jahanian  const ObjCInterfaceType* LHS = LHSOPT->getInterfaceType();
5743132f2a2da34f378fc675b9e174564b0f52c31d98Fariborz Jahanian  const ObjCInterfaceType* RHS = RHSOPT->getInterfaceType();
5744132f2a2da34f378fc675b9e174564b0f52c31d98Fariborz Jahanian  if (LHS && RHS)  { // We have 2 user-defined types.
5745132f2a2da34f378fc675b9e174564b0f52c31d98Fariborz Jahanian    if (LHS != RHS) {
5746132f2a2da34f378fc675b9e174564b0f52c31d98Fariborz Jahanian      if (LHS->getDecl()->isSuperClassOf(RHS->getDecl()))
5747a4fdbfad150ae37bddaa4094d3925a27a1a1cf3fFariborz Jahanian        return BlockReturnType;
5748132f2a2da34f378fc675b9e174564b0f52c31d98Fariborz Jahanian      if (RHS->getDecl()->isSuperClassOf(LHS->getDecl()))
5749a4fdbfad150ae37bddaa4094d3925a27a1a1cf3fFariborz Jahanian        return !BlockReturnType;
5750132f2a2da34f378fc675b9e174564b0f52c31d98Fariborz Jahanian    }
5751132f2a2da34f378fc675b9e174564b0f52c31d98Fariborz Jahanian    else
5752132f2a2da34f378fc675b9e174564b0f52c31d98Fariborz Jahanian      return true;
5753132f2a2da34f378fc675b9e174564b0f52c31d98Fariborz Jahanian  }
5754132f2a2da34f378fc675b9e174564b0f52c31d98Fariborz Jahanian  return false;
5755132f2a2da34f378fc675b9e174564b0f52c31d98Fariborz Jahanian}
5756132f2a2da34f378fc675b9e174564b0f52c31d98Fariborz Jahanian
5757e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanian/// getIntersectionOfProtocols - This routine finds the intersection of set
5758e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanian/// of protocols inherited from two distinct objective-c pointer objects.
5759e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanian/// It is used to build composite qualifier list of the composite type of
5760e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanian/// the conditional expression involving two objective-c pointer objects.
5761e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanianstatic
5762e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanianvoid getIntersectionOfProtocols(ASTContext &Context,
5763e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanian                                const ObjCObjectPointerType *LHSOPT,
5764e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanian                                const ObjCObjectPointerType *RHSOPT,
57655f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner      SmallVectorImpl<ObjCProtocolDecl *> &IntersectionOfProtocols) {
5766e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanian
5767c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall  const ObjCObjectType* LHS = LHSOPT->getObjectType();
5768c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall  const ObjCObjectType* RHS = RHSOPT->getObjectType();
5769c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall  assert(LHS->getInterface() && "LHS must have an interface base");
5770c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall  assert(RHS->getInterface() && "RHS must have an interface base");
5771e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanian
5772e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanian  llvm::SmallPtrSet<ObjCProtocolDecl *, 8> InheritedProtocolSet;
5773e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanian  unsigned LHSNumProtocols = LHS->getNumProtocols();
5774e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanian  if (LHSNumProtocols > 0)
5775e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanian    InheritedProtocolSet.insert(LHS->qual_begin(), LHS->qual_end());
5776e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanian  else {
5777432a8893f7e30d141d7f279bd00b741a3cdac81fFariborz Jahanian    llvm::SmallPtrSet<ObjCProtocolDecl *, 8> LHSInheritedProtocols;
5778c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall    Context.CollectInheritedProtocols(LHS->getInterface(),
5779c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall                                      LHSInheritedProtocols);
5780e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanian    InheritedProtocolSet.insert(LHSInheritedProtocols.begin(),
5781e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanian                                LHSInheritedProtocols.end());
5782e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanian  }
5783e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanian
5784e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanian  unsigned RHSNumProtocols = RHS->getNumProtocols();
5785e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanian  if (RHSNumProtocols > 0) {
5786cb421fa690da545b58a720abe5f1c49b166dbde7Dan Gohman    ObjCProtocolDecl **RHSProtocols =
5787cb421fa690da545b58a720abe5f1c49b166dbde7Dan Gohman      const_cast<ObjCProtocolDecl **>(RHS->qual_begin());
5788e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanian    for (unsigned i = 0; i < RHSNumProtocols; ++i)
5789e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanian      if (InheritedProtocolSet.count(RHSProtocols[i]))
5790e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanian        IntersectionOfProtocols.push_back(RHSProtocols[i]);
57913060178ad9df29789505c1e6debcfc80a3a13587Chad Rosier  } else {
5792432a8893f7e30d141d7f279bd00b741a3cdac81fFariborz Jahanian    llvm::SmallPtrSet<ObjCProtocolDecl *, 8> RHSInheritedProtocols;
5793c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall    Context.CollectInheritedProtocols(RHS->getInterface(),
5794c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall                                      RHSInheritedProtocols);
5795432a8893f7e30d141d7f279bd00b741a3cdac81fFariborz Jahanian    for (llvm::SmallPtrSet<ObjCProtocolDecl*,8>::iterator I =
5796432a8893f7e30d141d7f279bd00b741a3cdac81fFariborz Jahanian         RHSInheritedProtocols.begin(),
5797432a8893f7e30d141d7f279bd00b741a3cdac81fFariborz Jahanian         E = RHSInheritedProtocols.end(); I != E; ++I)
5798432a8893f7e30d141d7f279bd00b741a3cdac81fFariborz Jahanian      if (InheritedProtocolSet.count((*I)))
5799432a8893f7e30d141d7f279bd00b741a3cdac81fFariborz Jahanian        IntersectionOfProtocols.push_back((*I));
5800e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanian  }
5801e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanian}
5802e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanian
5803db07b3f7cdcb505329c1280d7cf70791739a7cadFariborz Jahanian/// areCommonBaseCompatible - Returns common base class of the two classes if
5804db07b3f7cdcb505329c1280d7cf70791739a7cadFariborz Jahanian/// one found. Note that this is O'2 algorithm. But it will be called as the
5805db07b3f7cdcb505329c1280d7cf70791739a7cadFariborz Jahanian/// last type comparison in a ?-exp of ObjC pointer types before a
5806db07b3f7cdcb505329c1280d7cf70791739a7cadFariborz Jahanian/// warning is issued. So, its invokation is extremely rare.
5807db07b3f7cdcb505329c1280d7cf70791739a7cadFariborz JahanianQualType ASTContext::areCommonBaseCompatible(
5808c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall                                          const ObjCObjectPointerType *Lptr,
5809c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall                                          const ObjCObjectPointerType *Rptr) {
5810c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall  const ObjCObjectType *LHS = Lptr->getObjectType();
5811c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall  const ObjCObjectType *RHS = Rptr->getObjectType();
5812c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall  const ObjCInterfaceDecl* LDecl = LHS->getInterface();
5813c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall  const ObjCInterfaceDecl* RDecl = RHS->getInterface();
581460ef308e51c71b760d7f598c1b763ceb7b768148Douglas Gregor  if (!LDecl || !RDecl || (declaresSameEntity(LDecl, RDecl)))
5815db07b3f7cdcb505329c1280d7cf70791739a7cadFariborz Jahanian    return QualType();
5816db07b3f7cdcb505329c1280d7cf70791739a7cadFariborz Jahanian
58177c2bdcb4d30f2d370b4367664e6a11b075ce2cb3Fariborz Jahanian  do {
5818c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall    LHS = cast<ObjCInterfaceType>(getObjCInterfaceType(LDecl));
5819e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanian    if (canAssignObjCInterfaces(LHS, RHS)) {
58205f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner      SmallVector<ObjCProtocolDecl *, 8> Protocols;
5821c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall      getIntersectionOfProtocols(*this, Lptr, Rptr, Protocols);
5822c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall
5823c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall      QualType Result = QualType(LHS, 0);
5824c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall      if (!Protocols.empty())
5825c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall        Result = getObjCObjectType(Result, Protocols.data(), Protocols.size());
5826c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall      Result = getObjCObjectPointerType(Result);
5827c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall      return Result;
5828e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanian    }
58297c2bdcb4d30f2d370b4367664e6a11b075ce2cb3Fariborz Jahanian  } while ((LDecl = LDecl->getSuperClass()));
5830db07b3f7cdcb505329c1280d7cf70791739a7cadFariborz Jahanian
5831db07b3f7cdcb505329c1280d7cf70791739a7cadFariborz Jahanian  return QualType();
5832db07b3f7cdcb505329c1280d7cf70791739a7cadFariborz Jahanian}
5833db07b3f7cdcb505329c1280d7cf70791739a7cadFariborz Jahanian
5834c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCallbool ASTContext::canAssignObjCInterfaces(const ObjCObjectType *LHS,
5835c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall                                         const ObjCObjectType *RHS) {
5836c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall  assert(LHS->getInterface() && "LHS is not an interface type");
5837c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall  assert(RHS->getInterface() && "RHS is not an interface type");
5838c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall
58396ac46a4a521366d7ab36ebe2ce4e624ab96b06f9Chris Lattner  // Verify that the base decls are compatible: the RHS must be a subclass of
58406ac46a4a521366d7ab36ebe2ce4e624ab96b06f9Chris Lattner  // the LHS.
5841c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall  if (!LHS->getInterface()->isSuperClassOf(RHS->getInterface()))
58426ac46a4a521366d7ab36ebe2ce4e624ab96b06f9Chris Lattner    return false;
58431eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
58446ac46a4a521366d7ab36ebe2ce4e624ab96b06f9Chris Lattner  // RHS must have a superset of the protocols in the LHS.  If the LHS is not
58456ac46a4a521366d7ab36ebe2ce4e624ab96b06f9Chris Lattner  // protocol qualified at all, then we are good.
5846c15cb2af27514ecc879daba9aa01389c5203685dSteve Naroff  if (LHS->getNumProtocols() == 0)
58476ac46a4a521366d7ab36ebe2ce4e624ab96b06f9Chris Lattner    return true;
58481eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
5849b7bc34a83aff8af09f2a78aa6d1dcafe18ad8619Fariborz Jahanian  // Okay, we know the LHS has protocol qualifiers.  If the RHS doesn't,
5850b7bc34a83aff8af09f2a78aa6d1dcafe18ad8619Fariborz Jahanian  // more detailed analysis is required.
5851b7bc34a83aff8af09f2a78aa6d1dcafe18ad8619Fariborz Jahanian  if (RHS->getNumProtocols() == 0) {
5852b7bc34a83aff8af09f2a78aa6d1dcafe18ad8619Fariborz Jahanian    // OK, if LHS is a superclass of RHS *and*
5853b7bc34a83aff8af09f2a78aa6d1dcafe18ad8619Fariborz Jahanian    // this superclass is assignment compatible with LHS.
5854b7bc34a83aff8af09f2a78aa6d1dcafe18ad8619Fariborz Jahanian    // false otherwise.
5855627788c29976fbeb4ad47bcfcb3576889070e357Fariborz Jahanian    bool IsSuperClass =
5856627788c29976fbeb4ad47bcfcb3576889070e357Fariborz Jahanian      LHS->getInterface()->isSuperClassOf(RHS->getInterface());
5857627788c29976fbeb4ad47bcfcb3576889070e357Fariborz Jahanian    if (IsSuperClass) {
5858b7bc34a83aff8af09f2a78aa6d1dcafe18ad8619Fariborz Jahanian      // OK if conversion of LHS to SuperClass results in narrowing of types
5859b7bc34a83aff8af09f2a78aa6d1dcafe18ad8619Fariborz Jahanian      // ; i.e., SuperClass may implement at least one of the protocols
5860b7bc34a83aff8af09f2a78aa6d1dcafe18ad8619Fariborz Jahanian      // in LHS's protocol list. Example, SuperObj<P1> = lhs<P1,P2> is ok.
5861b7bc34a83aff8af09f2a78aa6d1dcafe18ad8619Fariborz Jahanian      // But not SuperObj<P1,P2,P3> = lhs<P1,P2>.
5862b7bc34a83aff8af09f2a78aa6d1dcafe18ad8619Fariborz Jahanian      llvm::SmallPtrSet<ObjCProtocolDecl *, 8> SuperClassInheritedProtocols;
5863627788c29976fbeb4ad47bcfcb3576889070e357Fariborz Jahanian      CollectInheritedProtocols(RHS->getInterface(), SuperClassInheritedProtocols);
5864b7bc34a83aff8af09f2a78aa6d1dcafe18ad8619Fariborz Jahanian      // If super class has no protocols, it is not a match.
5865b7bc34a83aff8af09f2a78aa6d1dcafe18ad8619Fariborz Jahanian      if (SuperClassInheritedProtocols.empty())
5866b7bc34a83aff8af09f2a78aa6d1dcafe18ad8619Fariborz Jahanian        return false;
5867b7bc34a83aff8af09f2a78aa6d1dcafe18ad8619Fariborz Jahanian
5868b7bc34a83aff8af09f2a78aa6d1dcafe18ad8619Fariborz Jahanian      for (ObjCObjectType::qual_iterator LHSPI = LHS->qual_begin(),
5869b7bc34a83aff8af09f2a78aa6d1dcafe18ad8619Fariborz Jahanian           LHSPE = LHS->qual_end();
5870b7bc34a83aff8af09f2a78aa6d1dcafe18ad8619Fariborz Jahanian           LHSPI != LHSPE; LHSPI++) {
5871b7bc34a83aff8af09f2a78aa6d1dcafe18ad8619Fariborz Jahanian        bool SuperImplementsProtocol = false;
5872b7bc34a83aff8af09f2a78aa6d1dcafe18ad8619Fariborz Jahanian        ObjCProtocolDecl *LHSProto = (*LHSPI);
5873b7bc34a83aff8af09f2a78aa6d1dcafe18ad8619Fariborz Jahanian
5874b7bc34a83aff8af09f2a78aa6d1dcafe18ad8619Fariborz Jahanian        for (llvm::SmallPtrSet<ObjCProtocolDecl*,8>::iterator I =
5875b7bc34a83aff8af09f2a78aa6d1dcafe18ad8619Fariborz Jahanian             SuperClassInheritedProtocols.begin(),
5876b7bc34a83aff8af09f2a78aa6d1dcafe18ad8619Fariborz Jahanian             E = SuperClassInheritedProtocols.end(); I != E; ++I) {
5877b7bc34a83aff8af09f2a78aa6d1dcafe18ad8619Fariborz Jahanian          ObjCProtocolDecl *SuperClassProto = (*I);
5878b7bc34a83aff8af09f2a78aa6d1dcafe18ad8619Fariborz Jahanian          if (SuperClassProto->lookupProtocolNamed(LHSProto->getIdentifier())) {
5879b7bc34a83aff8af09f2a78aa6d1dcafe18ad8619Fariborz Jahanian            SuperImplementsProtocol = true;
5880b7bc34a83aff8af09f2a78aa6d1dcafe18ad8619Fariborz Jahanian            break;
5881b7bc34a83aff8af09f2a78aa6d1dcafe18ad8619Fariborz Jahanian          }
5882b7bc34a83aff8af09f2a78aa6d1dcafe18ad8619Fariborz Jahanian        }
5883b7bc34a83aff8af09f2a78aa6d1dcafe18ad8619Fariborz Jahanian        if (!SuperImplementsProtocol)
5884b7bc34a83aff8af09f2a78aa6d1dcafe18ad8619Fariborz Jahanian          return false;
5885b7bc34a83aff8af09f2a78aa6d1dcafe18ad8619Fariborz Jahanian      }
5886b7bc34a83aff8af09f2a78aa6d1dcafe18ad8619Fariborz Jahanian      return true;
5887b7bc34a83aff8af09f2a78aa6d1dcafe18ad8619Fariborz Jahanian    }
5888b7bc34a83aff8af09f2a78aa6d1dcafe18ad8619Fariborz Jahanian    return false;
5889b7bc34a83aff8af09f2a78aa6d1dcafe18ad8619Fariborz Jahanian  }
58901eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
5891c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall  for (ObjCObjectType::qual_iterator LHSPI = LHS->qual_begin(),
5892c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall                                     LHSPE = LHS->qual_end();
589391b0b0cf6b537cbcbca0038c7032f87161a41d31Steve Naroff       LHSPI != LHSPE; LHSPI++) {
589491b0b0cf6b537cbcbca0038c7032f87161a41d31Steve Naroff    bool RHSImplementsProtocol = false;
589591b0b0cf6b537cbcbca0038c7032f87161a41d31Steve Naroff
589691b0b0cf6b537cbcbca0038c7032f87161a41d31Steve Naroff    // If the RHS doesn't implement the protocol on the left, the types
589791b0b0cf6b537cbcbca0038c7032f87161a41d31Steve Naroff    // are incompatible.
5898c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall    for (ObjCObjectType::qual_iterator RHSPI = RHS->qual_begin(),
5899c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall                                       RHSPE = RHS->qual_end();
59008f16756441450ed9fb39316e47d107fc2a1ef35bSteve Naroff         RHSPI != RHSPE; RHSPI++) {
59018f16756441450ed9fb39316e47d107fc2a1ef35bSteve Naroff      if ((*RHSPI)->lookupProtocolNamed((*LHSPI)->getIdentifier())) {
590291b0b0cf6b537cbcbca0038c7032f87161a41d31Steve Naroff        RHSImplementsProtocol = true;
59038f16756441450ed9fb39316e47d107fc2a1ef35bSteve Naroff        break;
59048f16756441450ed9fb39316e47d107fc2a1ef35bSteve Naroff      }
590591b0b0cf6b537cbcbca0038c7032f87161a41d31Steve Naroff    }
590691b0b0cf6b537cbcbca0038c7032f87161a41d31Steve Naroff    // FIXME: For better diagnostics, consider passing back the protocol name.
590791b0b0cf6b537cbcbca0038c7032f87161a41d31Steve Naroff    if (!RHSImplementsProtocol)
590891b0b0cf6b537cbcbca0038c7032f87161a41d31Steve Naroff      return false;
590991b0b0cf6b537cbcbca0038c7032f87161a41d31Steve Naroff  }
591091b0b0cf6b537cbcbca0038c7032f87161a41d31Steve Naroff  // The RHS implements all protocols listed on the LHS.
591191b0b0cf6b537cbcbca0038c7032f87161a41d31Steve Naroff  return true;
59126ac46a4a521366d7ab36ebe2ce4e624ab96b06f9Chris Lattner}
59136ac46a4a521366d7ab36ebe2ce4e624ab96b06f9Chris Lattner
5914389bf46ae41241a656ed71b00ac2177d7f385651Steve Naroffbool ASTContext::areComparableObjCPointerTypes(QualType LHS, QualType RHS) {
5915389bf46ae41241a656ed71b00ac2177d7f385651Steve Naroff  // get the "pointed to" types
5916183700f494ec9b6701b6efe82bcb25f4c79ba561John McCall  const ObjCObjectPointerType *LHSOPT = LHS->getAs<ObjCObjectPointerType>();
5917183700f494ec9b6701b6efe82bcb25f4c79ba561John McCall  const ObjCObjectPointerType *RHSOPT = RHS->getAs<ObjCObjectPointerType>();
59181eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
591914108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff  if (!LHSOPT || !RHSOPT)
5920389bf46ae41241a656ed71b00ac2177d7f385651Steve Naroff    return false;
592114108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff
592214108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff  return canAssignObjCInterfaces(LHSOPT, RHSOPT) ||
592314108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff         canAssignObjCInterfaces(RHSOPT, LHSOPT);
5924389bf46ae41241a656ed71b00ac2177d7f385651Steve Naroff}
5925389bf46ae41241a656ed71b00ac2177d7f385651Steve Naroff
5926569c3166874324c24011f8ade6978421f0d39b3cDouglas Gregorbool ASTContext::canBindObjCObjectType(QualType To, QualType From) {
5927569c3166874324c24011f8ade6978421f0d39b3cDouglas Gregor  return canAssignObjCInterfaces(
5928569c3166874324c24011f8ade6978421f0d39b3cDouglas Gregor                getObjCObjectPointerType(To)->getAs<ObjCObjectPointerType>(),
5929569c3166874324c24011f8ade6978421f0d39b3cDouglas Gregor                getObjCObjectPointerType(From)->getAs<ObjCObjectPointerType>());
5930569c3166874324c24011f8ade6978421f0d39b3cDouglas Gregor}
5931569c3166874324c24011f8ade6978421f0d39b3cDouglas Gregor
59321eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// typesAreCompatible - C99 6.7.3p9: For two qualified types to be compatible,
5933ec0550fa3653d46560bf4484a2e988329c228e39Steve Naroff/// both shall have the identically qualified version of a compatible type.
59341eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// C99 6.2.7p1: Two types have compatible types if their types are the
5935ec0550fa3653d46560bf4484a2e988329c228e39Steve Naroff/// same. See 6.7.[2,3,5] for additional rules.
5936447234dd459a00a5ed9b7c3e066162cd7a75bf2dDouglas Gregorbool ASTContext::typesAreCompatible(QualType LHS, QualType RHS,
5937447234dd459a00a5ed9b7c3e066162cd7a75bf2dDouglas Gregor                                    bool CompareUnqualified) {
59384e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie  if (getLangOpts().CPlusPlus)
59390e709abafbd939326850501f795cc7a92c88a354Douglas Gregor    return hasSameType(LHS, RHS);
59400e709abafbd939326850501f795cc7a92c88a354Douglas Gregor
5941447234dd459a00a5ed9b7c3e066162cd7a75bf2dDouglas Gregor  return !mergeTypes(LHS, RHS, false, CompareUnqualified).isNull();
59423d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman}
59433d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman
5944c286f3835eb6001c61664cef5d610dfaf80a6e9bFariborz Jahanianbool ASTContext::propertyTypesAreCompatible(QualType LHS, QualType RHS) {
594582378391dfc3b2fc22f63dbf1552bd9aed39dc42Fariborz Jahanian  return typesAreCompatible(LHS, RHS);
5946c286f3835eb6001c61664cef5d610dfaf80a6e9bFariborz Jahanian}
5947c286f3835eb6001c61664cef5d610dfaf80a6e9bFariborz Jahanian
5948132f2a2da34f378fc675b9e174564b0f52c31d98Fariborz Jahanianbool ASTContext::typesAreBlockPointerCompatible(QualType LHS, QualType RHS) {
5949132f2a2da34f378fc675b9e174564b0f52c31d98Fariborz Jahanian  return !mergeTypes(LHS, RHS, true).isNull();
5950132f2a2da34f378fc675b9e174564b0f52c31d98Fariborz Jahanian}
5951132f2a2da34f378fc675b9e174564b0f52c31d98Fariborz Jahanian
59524846675e0e42d1802b0ffd8972a45e72aeb3758dPeter Collingbourne/// mergeTransparentUnionType - if T is a transparent union type and a member
59534846675e0e42d1802b0ffd8972a45e72aeb3758dPeter Collingbourne/// of T is compatible with SubType, return the merged type, else return
59544846675e0e42d1802b0ffd8972a45e72aeb3758dPeter Collingbourne/// QualType()
59554846675e0e42d1802b0ffd8972a45e72aeb3758dPeter CollingbourneQualType ASTContext::mergeTransparentUnionType(QualType T, QualType SubType,
59564846675e0e42d1802b0ffd8972a45e72aeb3758dPeter Collingbourne                                               bool OfBlockPointer,
59574846675e0e42d1802b0ffd8972a45e72aeb3758dPeter Collingbourne                                               bool Unqualified) {
59584846675e0e42d1802b0ffd8972a45e72aeb3758dPeter Collingbourne  if (const RecordType *UT = T->getAsUnionType()) {
59594846675e0e42d1802b0ffd8972a45e72aeb3758dPeter Collingbourne    RecordDecl *UD = UT->getDecl();
59604846675e0e42d1802b0ffd8972a45e72aeb3758dPeter Collingbourne    if (UD->hasAttr<TransparentUnionAttr>()) {
59614846675e0e42d1802b0ffd8972a45e72aeb3758dPeter Collingbourne      for (RecordDecl::field_iterator it = UD->field_begin(),
59624846675e0e42d1802b0ffd8972a45e72aeb3758dPeter Collingbourne           itend = UD->field_end(); it != itend; ++it) {
5963f91d7572ee7fa6f2926c8e7e816039a1154a59f8Peter Collingbourne        QualType ET = it->getType().getUnqualifiedType();
59644846675e0e42d1802b0ffd8972a45e72aeb3758dPeter Collingbourne        QualType MT = mergeTypes(ET, SubType, OfBlockPointer, Unqualified);
59654846675e0e42d1802b0ffd8972a45e72aeb3758dPeter Collingbourne        if (!MT.isNull())
59664846675e0e42d1802b0ffd8972a45e72aeb3758dPeter Collingbourne          return MT;
59674846675e0e42d1802b0ffd8972a45e72aeb3758dPeter Collingbourne      }
59684846675e0e42d1802b0ffd8972a45e72aeb3758dPeter Collingbourne    }
59694846675e0e42d1802b0ffd8972a45e72aeb3758dPeter Collingbourne  }
59704846675e0e42d1802b0ffd8972a45e72aeb3758dPeter Collingbourne
59714846675e0e42d1802b0ffd8972a45e72aeb3758dPeter Collingbourne  return QualType();
59724846675e0e42d1802b0ffd8972a45e72aeb3758dPeter Collingbourne}
59734846675e0e42d1802b0ffd8972a45e72aeb3758dPeter Collingbourne
59744846675e0e42d1802b0ffd8972a45e72aeb3758dPeter Collingbourne/// mergeFunctionArgumentTypes - merge two types which appear as function
59754846675e0e42d1802b0ffd8972a45e72aeb3758dPeter Collingbourne/// argument types
59764846675e0e42d1802b0ffd8972a45e72aeb3758dPeter CollingbourneQualType ASTContext::mergeFunctionArgumentTypes(QualType lhs, QualType rhs,
59774846675e0e42d1802b0ffd8972a45e72aeb3758dPeter Collingbourne                                                bool OfBlockPointer,
59784846675e0e42d1802b0ffd8972a45e72aeb3758dPeter Collingbourne                                                bool Unqualified) {
59794846675e0e42d1802b0ffd8972a45e72aeb3758dPeter Collingbourne  // GNU extension: two types are compatible if they appear as a function
59804846675e0e42d1802b0ffd8972a45e72aeb3758dPeter Collingbourne  // argument, one of the types is a transparent union type and the other
59814846675e0e42d1802b0ffd8972a45e72aeb3758dPeter Collingbourne  // type is compatible with a union member
59824846675e0e42d1802b0ffd8972a45e72aeb3758dPeter Collingbourne  QualType lmerge = mergeTransparentUnionType(lhs, rhs, OfBlockPointer,
59834846675e0e42d1802b0ffd8972a45e72aeb3758dPeter Collingbourne                                              Unqualified);
59844846675e0e42d1802b0ffd8972a45e72aeb3758dPeter Collingbourne  if (!lmerge.isNull())
59854846675e0e42d1802b0ffd8972a45e72aeb3758dPeter Collingbourne    return lmerge;
59864846675e0e42d1802b0ffd8972a45e72aeb3758dPeter Collingbourne
59874846675e0e42d1802b0ffd8972a45e72aeb3758dPeter Collingbourne  QualType rmerge = mergeTransparentUnionType(rhs, lhs, OfBlockPointer,
59884846675e0e42d1802b0ffd8972a45e72aeb3758dPeter Collingbourne                                              Unqualified);
59894846675e0e42d1802b0ffd8972a45e72aeb3758dPeter Collingbourne  if (!rmerge.isNull())
59904846675e0e42d1802b0ffd8972a45e72aeb3758dPeter Collingbourne    return rmerge;
59914846675e0e42d1802b0ffd8972a45e72aeb3758dPeter Collingbourne
59924846675e0e42d1802b0ffd8972a45e72aeb3758dPeter Collingbourne  return mergeTypes(lhs, rhs, OfBlockPointer, Unqualified);
59934846675e0e42d1802b0ffd8972a45e72aeb3758dPeter Collingbourne}
59944846675e0e42d1802b0ffd8972a45e72aeb3758dPeter Collingbourne
5995132f2a2da34f378fc675b9e174564b0f52c31d98Fariborz JahanianQualType ASTContext::mergeFunctionTypes(QualType lhs, QualType rhs,
5996447234dd459a00a5ed9b7c3e066162cd7a75bf2dDouglas Gregor                                        bool OfBlockPointer,
5997447234dd459a00a5ed9b7c3e066162cd7a75bf2dDouglas Gregor                                        bool Unqualified) {
5998183700f494ec9b6701b6efe82bcb25f4c79ba561John McCall  const FunctionType *lbase = lhs->getAs<FunctionType>();
5999183700f494ec9b6701b6efe82bcb25f4c79ba561John McCall  const FunctionType *rbase = rhs->getAs<FunctionType>();
600072564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor  const FunctionProtoType *lproto = dyn_cast<FunctionProtoType>(lbase);
600172564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor  const FunctionProtoType *rproto = dyn_cast<FunctionProtoType>(rbase);
60023d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman  bool allLTypes = true;
60033d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman  bool allRTypes = true;
60043d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman
60053d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman  // Check return type
6006132f2a2da34f378fc675b9e174564b0f52c31d98Fariborz Jahanian  QualType retType;
6007d263fd1451299b1e5f5f1acb2bb13b0a4119aee8Fariborz Jahanian  if (OfBlockPointer) {
6008d263fd1451299b1e5f5f1acb2bb13b0a4119aee8Fariborz Jahanian    QualType RHS = rbase->getResultType();
6009d263fd1451299b1e5f5f1acb2bb13b0a4119aee8Fariborz Jahanian    QualType LHS = lbase->getResultType();
6010d263fd1451299b1e5f5f1acb2bb13b0a4119aee8Fariborz Jahanian    bool UnqualifiedResult = Unqualified;
6011d263fd1451299b1e5f5f1acb2bb13b0a4119aee8Fariborz Jahanian    if (!UnqualifiedResult)
6012d263fd1451299b1e5f5f1acb2bb13b0a4119aee8Fariborz Jahanian      UnqualifiedResult = (!RHS.hasQualifiers() && LHS.hasQualifiers());
6013a4fdbfad150ae37bddaa4094d3925a27a1a1cf3fFariborz Jahanian    retType = mergeTypes(LHS, RHS, true, UnqualifiedResult, true);
6014d263fd1451299b1e5f5f1acb2bb13b0a4119aee8Fariborz Jahanian  }
6015132f2a2da34f378fc675b9e174564b0f52c31d98Fariborz Jahanian  else
60168cc246c9a68c783a5b90d2e8b8927521cb3a49b7John McCall    retType = mergeTypes(lbase->getResultType(), rbase->getResultType(), false,
60178cc246c9a68c783a5b90d2e8b8927521cb3a49b7John McCall                         Unqualified);
60183d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman  if (retType.isNull()) return QualType();
6019447234dd459a00a5ed9b7c3e066162cd7a75bf2dDouglas Gregor
6020447234dd459a00a5ed9b7c3e066162cd7a75bf2dDouglas Gregor  if (Unqualified)
6021447234dd459a00a5ed9b7c3e066162cd7a75bf2dDouglas Gregor    retType = retType.getUnqualifiedType();
6022447234dd459a00a5ed9b7c3e066162cd7a75bf2dDouglas Gregor
6023447234dd459a00a5ed9b7c3e066162cd7a75bf2dDouglas Gregor  CanQualType LRetType = getCanonicalType(lbase->getResultType());
6024447234dd459a00a5ed9b7c3e066162cd7a75bf2dDouglas Gregor  CanQualType RRetType = getCanonicalType(rbase->getResultType());
6025447234dd459a00a5ed9b7c3e066162cd7a75bf2dDouglas Gregor  if (Unqualified) {
6026447234dd459a00a5ed9b7c3e066162cd7a75bf2dDouglas Gregor    LRetType = LRetType.getUnqualifiedType();
6027447234dd459a00a5ed9b7c3e066162cd7a75bf2dDouglas Gregor    RRetType = RRetType.getUnqualifiedType();
6028447234dd459a00a5ed9b7c3e066162cd7a75bf2dDouglas Gregor  }
6029447234dd459a00a5ed9b7c3e066162cd7a75bf2dDouglas Gregor
6030447234dd459a00a5ed9b7c3e066162cd7a75bf2dDouglas Gregor  if (getCanonicalType(retType) != LRetType)
603161710854be2b098428aff5316e64bd34b30fbcb7Chris Lattner    allLTypes = false;
6032447234dd459a00a5ed9b7c3e066162cd7a75bf2dDouglas Gregor  if (getCanonicalType(retType) != RRetType)
603361710854be2b098428aff5316e64bd34b30fbcb7Chris Lattner    allRTypes = false;
60348cc246c9a68c783a5b90d2e8b8927521cb3a49b7John McCall
60356a15c8586cc3e5d6e01c4c4e8b4374584569b58fDaniel Dunbar  // FIXME: double check this
60366a15c8586cc3e5d6e01c4c4e8b4374584569b58fDaniel Dunbar  // FIXME: should we error if lbase->getRegParmAttr() != 0 &&
60376a15c8586cc3e5d6e01c4c4e8b4374584569b58fDaniel Dunbar  //                           rbase->getRegParmAttr() != 0 &&
60386a15c8586cc3e5d6e01c4c4e8b4374584569b58fDaniel Dunbar  //                           lbase->getRegParmAttr() != rbase->getRegParmAttr()?
6039264ba48dc98f3f843935a485d5b086f7e0fdc4f1Rafael Espindola  FunctionType::ExtInfo lbaseInfo = lbase->getExtInfo();
6040264ba48dc98f3f843935a485d5b086f7e0fdc4f1Rafael Espindola  FunctionType::ExtInfo rbaseInfo = rbase->getExtInfo();
60418cc246c9a68c783a5b90d2e8b8927521cb3a49b7John McCall
60428cc246c9a68c783a5b90d2e8b8927521cb3a49b7John McCall  // Compatible functions must have compatible calling conventions
60438cc246c9a68c783a5b90d2e8b8927521cb3a49b7John McCall  if (!isSameCallConv(lbaseInfo.getCC(), rbaseInfo.getCC()))
60448cc246c9a68c783a5b90d2e8b8927521cb3a49b7John McCall    return QualType();
60458cc246c9a68c783a5b90d2e8b8927521cb3a49b7John McCall
60468cc246c9a68c783a5b90d2e8b8927521cb3a49b7John McCall  // Regparm is part of the calling convention.
6047a49218e17bcbb1acde0245773173e2c0c42f4f19Eli Friedman  if (lbaseInfo.getHasRegParm() != rbaseInfo.getHasRegParm())
6048a49218e17bcbb1acde0245773173e2c0c42f4f19Eli Friedman    return QualType();
60498cc246c9a68c783a5b90d2e8b8927521cb3a49b7John McCall  if (lbaseInfo.getRegParm() != rbaseInfo.getRegParm())
60508cc246c9a68c783a5b90d2e8b8927521cb3a49b7John McCall    return QualType();
60518cc246c9a68c783a5b90d2e8b8927521cb3a49b7John McCall
6052f85e193739c953358c865005855253af4f68a497John McCall  if (lbaseInfo.getProducesResult() != rbaseInfo.getProducesResult())
6053f85e193739c953358c865005855253af4f68a497John McCall    return QualType();
6054f85e193739c953358c865005855253af4f68a497John McCall
605553c8167d7a007daae87f342c0fedd03f1dcf1b62Fariborz Jahanian  // functypes which return are preferred over those that do not.
605653c8167d7a007daae87f342c0fedd03f1dcf1b62Fariborz Jahanian  if (lbaseInfo.getNoReturn() && !rbaseInfo.getNoReturn())
60576a15c8586cc3e5d6e01c4c4e8b4374584569b58fDaniel Dunbar    allLTypes = false;
605853c8167d7a007daae87f342c0fedd03f1dcf1b62Fariborz Jahanian  else if (!lbaseInfo.getNoReturn() && rbaseInfo.getNoReturn())
60596a15c8586cc3e5d6e01c4c4e8b4374584569b58fDaniel Dunbar    allRTypes = false;
606053c8167d7a007daae87f342c0fedd03f1dcf1b62Fariborz Jahanian  // FIXME: some uses, e.g. conditional exprs, really want this to be 'both'.
606153c8167d7a007daae87f342c0fedd03f1dcf1b62Fariborz Jahanian  bool NoReturn = lbaseInfo.getNoReturn() || rbaseInfo.getNoReturn();
60621eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
6063f85e193739c953358c865005855253af4f68a497John McCall  FunctionType::ExtInfo einfo = lbaseInfo.withNoReturn(NoReturn);
6064e23cf437fe76b1ed02d63c3f61b456fd48a915f5John McCall
60653d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman  if (lproto && rproto) { // two C99 style function prototypes
6066465226e23a3008bd68973513dda1f9e3cd27dbddSebastian Redl    assert(!lproto->hasExceptionSpec() && !rproto->hasExceptionSpec() &&
6067465226e23a3008bd68973513dda1f9e3cd27dbddSebastian Redl           "C++ shouldn't be here");
60683d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    unsigned lproto_nargs = lproto->getNumArgs();
60693d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    unsigned rproto_nargs = rproto->getNumArgs();
60703d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman
60713d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    // Compatible functions must have the same number of arguments
60723d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    if (lproto_nargs != rproto_nargs)
60733d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman      return QualType();
60743d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman
60753d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    // Variadic and non-variadic functions aren't compatible
60763d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    if (lproto->isVariadic() != rproto->isVariadic())
60773d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman      return QualType();
60783d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman
60797fb5e4888221cd36652d078c6b171ac55e7f406dArgyrios Kyrtzidis    if (lproto->getTypeQuals() != rproto->getTypeQuals())
60807fb5e4888221cd36652d078c6b171ac55e7f406dArgyrios Kyrtzidis      return QualType();
60817fb5e4888221cd36652d078c6b171ac55e7f406dArgyrios Kyrtzidis
608278213e4aec6d8d22b5828dd2687fa19116ebaa26Fariborz Jahanian    if (LangOpts.ObjCAutoRefCount &&
608378213e4aec6d8d22b5828dd2687fa19116ebaa26Fariborz Jahanian        !FunctionTypesMatchOnNSConsumedAttrs(rproto, lproto))
608478213e4aec6d8d22b5828dd2687fa19116ebaa26Fariborz Jahanian      return QualType();
608578213e4aec6d8d22b5828dd2687fa19116ebaa26Fariborz Jahanian
60863d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    // Check argument compatibility
60875f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner    SmallVector<QualType, 10> types;
60883d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    for (unsigned i = 0; i < lproto_nargs; i++) {
60893d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman      QualType largtype = lproto->getArgType(i).getUnqualifiedType();
60903d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman      QualType rargtype = rproto->getArgType(i).getUnqualifiedType();
60914846675e0e42d1802b0ffd8972a45e72aeb3758dPeter Collingbourne      QualType argtype = mergeFunctionArgumentTypes(largtype, rargtype,
60924846675e0e42d1802b0ffd8972a45e72aeb3758dPeter Collingbourne                                                    OfBlockPointer,
60934846675e0e42d1802b0ffd8972a45e72aeb3758dPeter Collingbourne                                                    Unqualified);
60943d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman      if (argtype.isNull()) return QualType();
6095447234dd459a00a5ed9b7c3e066162cd7a75bf2dDouglas Gregor
6096447234dd459a00a5ed9b7c3e066162cd7a75bf2dDouglas Gregor      if (Unqualified)
6097447234dd459a00a5ed9b7c3e066162cd7a75bf2dDouglas Gregor        argtype = argtype.getUnqualifiedType();
6098447234dd459a00a5ed9b7c3e066162cd7a75bf2dDouglas Gregor
60993d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman      types.push_back(argtype);
6100447234dd459a00a5ed9b7c3e066162cd7a75bf2dDouglas Gregor      if (Unqualified) {
6101447234dd459a00a5ed9b7c3e066162cd7a75bf2dDouglas Gregor        largtype = largtype.getUnqualifiedType();
6102447234dd459a00a5ed9b7c3e066162cd7a75bf2dDouglas Gregor        rargtype = rargtype.getUnqualifiedType();
6103447234dd459a00a5ed9b7c3e066162cd7a75bf2dDouglas Gregor      }
6104447234dd459a00a5ed9b7c3e066162cd7a75bf2dDouglas Gregor
610561710854be2b098428aff5316e64bd34b30fbcb7Chris Lattner      if (getCanonicalType(argtype) != getCanonicalType(largtype))
610661710854be2b098428aff5316e64bd34b30fbcb7Chris Lattner        allLTypes = false;
610761710854be2b098428aff5316e64bd34b30fbcb7Chris Lattner      if (getCanonicalType(argtype) != getCanonicalType(rargtype))
610861710854be2b098428aff5316e64bd34b30fbcb7Chris Lattner        allRTypes = false;
61093d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    }
611078213e4aec6d8d22b5828dd2687fa19116ebaa26Fariborz Jahanian
61113d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    if (allLTypes) return lhs;
61123d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    if (allRTypes) return rhs;
6113e23cf437fe76b1ed02d63c3f61b456fd48a915f5John McCall
6114e23cf437fe76b1ed02d63c3f61b456fd48a915f5John McCall    FunctionProtoType::ExtProtoInfo EPI = lproto->getExtProtoInfo();
6115e23cf437fe76b1ed02d63c3f61b456fd48a915f5John McCall    EPI.ExtInfo = einfo;
6116e23cf437fe76b1ed02d63c3f61b456fd48a915f5John McCall    return getFunctionType(retType, types.begin(), types.size(), EPI);
61173d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman  }
61183d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman
61193d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman  if (lproto) allRTypes = false;
61203d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman  if (rproto) allLTypes = false;
61213d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman
612272564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor  const FunctionProtoType *proto = lproto ? lproto : rproto;
61233d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman  if (proto) {
6124465226e23a3008bd68973513dda1f9e3cd27dbddSebastian Redl    assert(!proto->hasExceptionSpec() && "C++ shouldn't be here");
61253d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    if (proto->isVariadic()) return QualType();
61263d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    // Check that the types are compatible with the types that
61273d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    // would result from default argument promotions (C99 6.7.5.3p15).
61283d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    // The only types actually affected are promotable integer
61293d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    // types and floats, which would be passed as a different
61303d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    // type depending on whether the prototype is visible.
61313d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    unsigned proto_nargs = proto->getNumArgs();
61323d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    for (unsigned i = 0; i < proto_nargs; ++i) {
61333d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman      QualType argTy = proto->getArgType(i);
6134b0f8eacfdcd0d43f51e669f2d723992d4af9f746Douglas Gregor
6135b0f8eacfdcd0d43f51e669f2d723992d4af9f746Douglas Gregor      // Look at the promotion type of enum types, since that is the type used
6136b0f8eacfdcd0d43f51e669f2d723992d4af9f746Douglas Gregor      // to pass enum values.
6137b0f8eacfdcd0d43f51e669f2d723992d4af9f746Douglas Gregor      if (const EnumType *Enum = argTy->getAs<EnumType>())
6138b0f8eacfdcd0d43f51e669f2d723992d4af9f746Douglas Gregor        argTy = Enum->getDecl()->getPromotionType();
6139b0f8eacfdcd0d43f51e669f2d723992d4af9f746Douglas Gregor
61403d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman      if (argTy->isPromotableIntegerType() ||
61413d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman          getCanonicalType(argTy).getUnqualifiedType() == FloatTy)
61423d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman        return QualType();
61433d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    }
61443d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman
61453d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    if (allLTypes) return lhs;
61463d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    if (allRTypes) return rhs;
6147e23cf437fe76b1ed02d63c3f61b456fd48a915f5John McCall
6148e23cf437fe76b1ed02d63c3f61b456fd48a915f5John McCall    FunctionProtoType::ExtProtoInfo EPI = proto->getExtProtoInfo();
6149e23cf437fe76b1ed02d63c3f61b456fd48a915f5John McCall    EPI.ExtInfo = einfo;
61503d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    return getFunctionType(retType, proto->arg_type_begin(),
6151e23cf437fe76b1ed02d63c3f61b456fd48a915f5John McCall                           proto->getNumArgs(), EPI);
61523d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman  }
61533d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman
61543d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman  if (allLTypes) return lhs;
61553d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman  if (allRTypes) return rhs;
61568cc246c9a68c783a5b90d2e8b8927521cb3a49b7John McCall  return getFunctionNoProtoType(retType, einfo);
61573d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman}
61583d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman
6159132f2a2da34f378fc675b9e174564b0f52c31d98Fariborz JahanianQualType ASTContext::mergeTypes(QualType LHS, QualType RHS,
6160447234dd459a00a5ed9b7c3e066162cd7a75bf2dDouglas Gregor                                bool OfBlockPointer,
6161a4fdbfad150ae37bddaa4094d3925a27a1a1cf3fFariborz Jahanian                                bool Unqualified, bool BlockReturnType) {
616243d69750e7f7b26076e7474dec8839bb777b260fBill Wendling  // C++ [expr]: If an expression initially has the type "reference to T", the
616343d69750e7f7b26076e7474dec8839bb777b260fBill Wendling  // type is adjusted to "T" prior to any further analysis, the expression
616443d69750e7f7b26076e7474dec8839bb777b260fBill Wendling  // designates the object or function denoted by the reference, and the
61657c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl  // expression is an lvalue unless the reference is an rvalue reference and
61667c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl  // the expression is a function call (possibly inside parentheses).
61670e709abafbd939326850501f795cc7a92c88a354Douglas Gregor  assert(!LHS->getAs<ReferenceType>() && "LHS is a reference type?");
61680e709abafbd939326850501f795cc7a92c88a354Douglas Gregor  assert(!RHS->getAs<ReferenceType>() && "RHS is a reference type?");
6169447234dd459a00a5ed9b7c3e066162cd7a75bf2dDouglas Gregor
6170447234dd459a00a5ed9b7c3e066162cd7a75bf2dDouglas Gregor  if (Unqualified) {
6171447234dd459a00a5ed9b7c3e066162cd7a75bf2dDouglas Gregor    LHS = LHS.getUnqualifiedType();
6172447234dd459a00a5ed9b7c3e066162cd7a75bf2dDouglas Gregor    RHS = RHS.getUnqualifiedType();
6173447234dd459a00a5ed9b7c3e066162cd7a75bf2dDouglas Gregor  }
61740e709abafbd939326850501f795cc7a92c88a354Douglas Gregor
61753d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman  QualType LHSCan = getCanonicalType(LHS),
61763d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman           RHSCan = getCanonicalType(RHS);
61773d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman
6178f3692dc4a47dc48d10cec0415c6e9e39b7a39707Chris Lattner  // If two types are identical, they are compatible.
61793d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman  if (LHSCan == RHSCan)
61803d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    return LHS;
61813d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman
61820953e767ff7817f97b3ab20896b229891eeff45bJohn McCall  // If the qualifiers are different, the types aren't compatible... mostly.
6183a4923eb7c4b04d360cb2747641a5e92818edf804Douglas Gregor  Qualifiers LQuals = LHSCan.getLocalQualifiers();
6184a4923eb7c4b04d360cb2747641a5e92818edf804Douglas Gregor  Qualifiers RQuals = RHSCan.getLocalQualifiers();
61850953e767ff7817f97b3ab20896b229891eeff45bJohn McCall  if (LQuals != RQuals) {
61860953e767ff7817f97b3ab20896b229891eeff45bJohn McCall    // If any of these qualifiers are different, we have a type
61870953e767ff7817f97b3ab20896b229891eeff45bJohn McCall    // mismatch.
61880953e767ff7817f97b3ab20896b229891eeff45bJohn McCall    if (LQuals.getCVRQualifiers() != RQuals.getCVRQualifiers() ||
6189f85e193739c953358c865005855253af4f68a497John McCall        LQuals.getAddressSpace() != RQuals.getAddressSpace() ||
6190f85e193739c953358c865005855253af4f68a497John McCall        LQuals.getObjCLifetime() != RQuals.getObjCLifetime())
61910953e767ff7817f97b3ab20896b229891eeff45bJohn McCall      return QualType();
61920953e767ff7817f97b3ab20896b229891eeff45bJohn McCall
61930953e767ff7817f97b3ab20896b229891eeff45bJohn McCall    // Exactly one GC qualifier difference is allowed: __strong is
61940953e767ff7817f97b3ab20896b229891eeff45bJohn McCall    // okay if the other type has no GC qualifier but is an Objective
61950953e767ff7817f97b3ab20896b229891eeff45bJohn McCall    // C object pointer (i.e. implicitly strong by default).  We fix
61960953e767ff7817f97b3ab20896b229891eeff45bJohn McCall    // this by pretending that the unqualified type was actually
61970953e767ff7817f97b3ab20896b229891eeff45bJohn McCall    // qualified __strong.
61980953e767ff7817f97b3ab20896b229891eeff45bJohn McCall    Qualifiers::GC GC_L = LQuals.getObjCGCAttr();
61990953e767ff7817f97b3ab20896b229891eeff45bJohn McCall    Qualifiers::GC GC_R = RQuals.getObjCGCAttr();
62000953e767ff7817f97b3ab20896b229891eeff45bJohn McCall    assert((GC_L != GC_R) && "unequal qualifier sets had only equal elements");
62010953e767ff7817f97b3ab20896b229891eeff45bJohn McCall
62020953e767ff7817f97b3ab20896b229891eeff45bJohn McCall    if (GC_L == Qualifiers::Weak || GC_R == Qualifiers::Weak)
62030953e767ff7817f97b3ab20896b229891eeff45bJohn McCall      return QualType();
62040953e767ff7817f97b3ab20896b229891eeff45bJohn McCall
62050953e767ff7817f97b3ab20896b229891eeff45bJohn McCall    if (GC_L == Qualifiers::Strong && RHSCan->isObjCObjectPointerType()) {
62060953e767ff7817f97b3ab20896b229891eeff45bJohn McCall      return mergeTypes(LHS, getObjCGCQualType(RHS, Qualifiers::Strong));
62070953e767ff7817f97b3ab20896b229891eeff45bJohn McCall    }
62080953e767ff7817f97b3ab20896b229891eeff45bJohn McCall    if (GC_R == Qualifiers::Strong && LHSCan->isObjCObjectPointerType()) {
62090953e767ff7817f97b3ab20896b229891eeff45bJohn McCall      return mergeTypes(getObjCGCQualType(LHS, Qualifiers::Strong), RHS);
62100953e767ff7817f97b3ab20896b229891eeff45bJohn McCall    }
62113d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    return QualType();
62120953e767ff7817f97b3ab20896b229891eeff45bJohn McCall  }
62130953e767ff7817f97b3ab20896b229891eeff45bJohn McCall
62140953e767ff7817f97b3ab20896b229891eeff45bJohn McCall  // Okay, qualifiers are equal.
62153d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman
6216852d63b806c5cbd730c6b9d696e2e27d02546b49Eli Friedman  Type::TypeClass LHSClass = LHSCan->getTypeClass();
6217852d63b806c5cbd730c6b9d696e2e27d02546b49Eli Friedman  Type::TypeClass RHSClass = RHSCan->getTypeClass();
6218f3692dc4a47dc48d10cec0415c6e9e39b7a39707Chris Lattner
62191adb88370beab45af2f065afe86b51ccd59ec50dChris Lattner  // We want to consider the two function types to be the same for these
62201adb88370beab45af2f065afe86b51ccd59ec50dChris Lattner  // comparisons, just force one to the other.
62211adb88370beab45af2f065afe86b51ccd59ec50dChris Lattner  if (LHSClass == Type::FunctionProto) LHSClass = Type::FunctionNoProto;
62221adb88370beab45af2f065afe86b51ccd59ec50dChris Lattner  if (RHSClass == Type::FunctionProto) RHSClass = Type::FunctionNoProto;
62234c721d381fb279899337d120edd4a24d405e56b2Eli Friedman
62244c721d381fb279899337d120edd4a24d405e56b2Eli Friedman  // Same as above for arrays
6225a36a61f218b9f7a97f2c0f511e0b29eb42e8f78bChris Lattner  if (LHSClass == Type::VariableArray || LHSClass == Type::IncompleteArray)
6226a36a61f218b9f7a97f2c0f511e0b29eb42e8f78bChris Lattner    LHSClass = Type::ConstantArray;
6227a36a61f218b9f7a97f2c0f511e0b29eb42e8f78bChris Lattner  if (RHSClass == Type::VariableArray || RHSClass == Type::IncompleteArray)
6228a36a61f218b9f7a97f2c0f511e0b29eb42e8f78bChris Lattner    RHSClass = Type::ConstantArray;
62291eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
6230c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall  // ObjCInterfaces are just specialized ObjCObjects.
6231c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall  if (LHSClass == Type::ObjCInterface) LHSClass = Type::ObjCObject;
6232c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall  if (RHSClass == Type::ObjCInterface) RHSClass = Type::ObjCObject;
6233c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall
6234213541a68a3e137d11d2cefb612c6cdb410d7e8eNate Begeman  // Canonicalize ExtVector -> Vector.
6235213541a68a3e137d11d2cefb612c6cdb410d7e8eNate Begeman  if (LHSClass == Type::ExtVector) LHSClass = Type::Vector;
6236213541a68a3e137d11d2cefb612c6cdb410d7e8eNate Begeman  if (RHSClass == Type::ExtVector) RHSClass = Type::Vector;
62371eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
62384e78fd0a960eaa7e97467f2e8f390f3a57da279bSteve Naroff  // If the canonical type classes don't match.
62394e78fd0a960eaa7e97467f2e8f390f3a57da279bSteve Naroff  if (LHSClass != RHSClass) {
62401adb88370beab45af2f065afe86b51ccd59ec50dChris Lattner    // C99 6.7.2.2p4: Each enumerated type shall be compatible with char,
62411eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    // a signed integer type, or an unsigned integer type.
6242842aef8d942a880eeb9535d40de31a86838264cbJohn McCall    // Compatibility is based on the underlying type, not the promotion
6243842aef8d942a880eeb9535d40de31a86838264cbJohn McCall    // type.
6244183700f494ec9b6701b6efe82bcb25f4c79ba561John McCall    if (const EnumType* ETy = LHS->getAs<EnumType>()) {
6245b918d6b1f794266e126e5445d69233462087d764Fariborz Jahanian      QualType TINT = ETy->getDecl()->getIntegerType();
6246b918d6b1f794266e126e5445d69233462087d764Fariborz Jahanian      if (!TINT.isNull() && hasSameType(TINT, RHSCan.getUnqualifiedType()))
62473d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman        return RHS;
6248bab96968886f4b77083f4e26a28986ddb1e42d67Eli Friedman    }
6249183700f494ec9b6701b6efe82bcb25f4c79ba561John McCall    if (const EnumType* ETy = RHS->getAs<EnumType>()) {
6250b918d6b1f794266e126e5445d69233462087d764Fariborz Jahanian      QualType TINT = ETy->getDecl()->getIntegerType();
6251b918d6b1f794266e126e5445d69233462087d764Fariborz Jahanian      if (!TINT.isNull() && hasSameType(TINT, LHSCan.getUnqualifiedType()))
62523d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman        return LHS;
6253bab96968886f4b77083f4e26a28986ddb1e42d67Eli Friedman    }
6254e7cff2c00c3d74e38984a4db5c48d7e303d3ab8eFariborz Jahanian    // allow block pointer type to match an 'id' type.
6255419636316c04d570c183b4797fc811932c588358Fariborz Jahanian    if (OfBlockPointer && !BlockReturnType) {
6256419636316c04d570c183b4797fc811932c588358Fariborz Jahanian       if (LHS->isObjCIdType() && RHS->isBlockPointerType())
6257419636316c04d570c183b4797fc811932c588358Fariborz Jahanian         return LHS;
6258419636316c04d570c183b4797fc811932c588358Fariborz Jahanian      if (RHS->isObjCIdType() && LHS->isBlockPointerType())
6259419636316c04d570c183b4797fc811932c588358Fariborz Jahanian        return RHS;
6260419636316c04d570c183b4797fc811932c588358Fariborz Jahanian    }
6261e7cff2c00c3d74e38984a4db5c48d7e303d3ab8eFariborz Jahanian
62623d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    return QualType();
6263ec0550fa3653d46560bf4484a2e988329c228e39Steve Naroff  }
62643d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman
62654a74678ed6c3dedac05d02b1ee341f1db869f049Steve Naroff  // The canonical type classes match.
62661adb88370beab45af2f065afe86b51ccd59ec50dChris Lattner  switch (LHSClass) {
626772564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor#define TYPE(Class, Base)
626872564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor#define ABSTRACT_TYPE(Class, Base)
6269ad5e73887052193afda72db8efcb812bd083a4a8John McCall#define NON_CANONICAL_UNLESS_DEPENDENT_TYPE(Class, Base) case Type::Class:
627072564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor#define NON_CANONICAL_TYPE(Class, Base) case Type::Class:
627172564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor#define DEPENDENT_TYPE(Class, Base) case Type::Class:
627272564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor#include "clang/AST/TypeNodes.def"
6273b219cfc4d75f0a03630b7c4509ef791b7e97b2c8David Blaikie    llvm_unreachable("Non-canonical and dependent types shouldn't get here");
627472564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor
62757c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl  case Type::LValueReference:
62767c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl  case Type::RValueReference:
627772564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor  case Type::MemberPointer:
6278b219cfc4d75f0a03630b7c4509ef791b7e97b2c8David Blaikie    llvm_unreachable("C++ should never be in mergeTypes");
627972564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor
6280c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall  case Type::ObjCInterface:
628172564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor  case Type::IncompleteArray:
628272564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor  case Type::VariableArray:
628372564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor  case Type::FunctionProto:
628472564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor  case Type::ExtVector:
6285b219cfc4d75f0a03630b7c4509ef791b7e97b2c8David Blaikie    llvm_unreachable("Types are eliminated above");
628672564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor
62871adb88370beab45af2f065afe86b51ccd59ec50dChris Lattner  case Type::Pointer:
62883d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman  {
62893d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    // Merge two pointer types, while trying to preserve typedef info
62906217b80b7a1379b74cced1c076338262c3c980b3Ted Kremenek    QualType LHSPointee = LHS->getAs<PointerType>()->getPointeeType();
62916217b80b7a1379b74cced1c076338262c3c980b3Ted Kremenek    QualType RHSPointee = RHS->getAs<PointerType>()->getPointeeType();
6292447234dd459a00a5ed9b7c3e066162cd7a75bf2dDouglas Gregor    if (Unqualified) {
6293447234dd459a00a5ed9b7c3e066162cd7a75bf2dDouglas Gregor      LHSPointee = LHSPointee.getUnqualifiedType();
6294447234dd459a00a5ed9b7c3e066162cd7a75bf2dDouglas Gregor      RHSPointee = RHSPointee.getUnqualifiedType();
6295447234dd459a00a5ed9b7c3e066162cd7a75bf2dDouglas Gregor    }
6296447234dd459a00a5ed9b7c3e066162cd7a75bf2dDouglas Gregor    QualType ResultType = mergeTypes(LHSPointee, RHSPointee, false,
6297447234dd459a00a5ed9b7c3e066162cd7a75bf2dDouglas Gregor                                     Unqualified);
62983d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    if (ResultType.isNull()) return QualType();
629907d258756dc856c6987c394a0972884e6ed46765Eli Friedman    if (getCanonicalType(LHSPointee) == getCanonicalType(ResultType))
630061710854be2b098428aff5316e64bd34b30fbcb7Chris Lattner      return LHS;
630107d258756dc856c6987c394a0972884e6ed46765Eli Friedman    if (getCanonicalType(RHSPointee) == getCanonicalType(ResultType))
630261710854be2b098428aff5316e64bd34b30fbcb7Chris Lattner      return RHS;
63033d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    return getPointerType(ResultType);
63043d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman  }
6305c0febd58f5cbf4a93fd12f461863564dba0af76dSteve Naroff  case Type::BlockPointer:
6306c0febd58f5cbf4a93fd12f461863564dba0af76dSteve Naroff  {
6307c0febd58f5cbf4a93fd12f461863564dba0af76dSteve Naroff    // Merge two block pointer types, while trying to preserve typedef info
63086217b80b7a1379b74cced1c076338262c3c980b3Ted Kremenek    QualType LHSPointee = LHS->getAs<BlockPointerType>()->getPointeeType();
63096217b80b7a1379b74cced1c076338262c3c980b3Ted Kremenek    QualType RHSPointee = RHS->getAs<BlockPointerType>()->getPointeeType();
6310447234dd459a00a5ed9b7c3e066162cd7a75bf2dDouglas Gregor    if (Unqualified) {
6311447234dd459a00a5ed9b7c3e066162cd7a75bf2dDouglas Gregor      LHSPointee = LHSPointee.getUnqualifiedType();
6312447234dd459a00a5ed9b7c3e066162cd7a75bf2dDouglas Gregor      RHSPointee = RHSPointee.getUnqualifiedType();
6313447234dd459a00a5ed9b7c3e066162cd7a75bf2dDouglas Gregor    }
6314447234dd459a00a5ed9b7c3e066162cd7a75bf2dDouglas Gregor    QualType ResultType = mergeTypes(LHSPointee, RHSPointee, OfBlockPointer,
6315447234dd459a00a5ed9b7c3e066162cd7a75bf2dDouglas Gregor                                     Unqualified);
6316c0febd58f5cbf4a93fd12f461863564dba0af76dSteve Naroff    if (ResultType.isNull()) return QualType();
6317c0febd58f5cbf4a93fd12f461863564dba0af76dSteve Naroff    if (getCanonicalType(LHSPointee) == getCanonicalType(ResultType))
6318c0febd58f5cbf4a93fd12f461863564dba0af76dSteve Naroff      return LHS;
6319c0febd58f5cbf4a93fd12f461863564dba0af76dSteve Naroff    if (getCanonicalType(RHSPointee) == getCanonicalType(ResultType))
6320c0febd58f5cbf4a93fd12f461863564dba0af76dSteve Naroff      return RHS;
6321c0febd58f5cbf4a93fd12f461863564dba0af76dSteve Naroff    return getBlockPointerType(ResultType);
6322c0febd58f5cbf4a93fd12f461863564dba0af76dSteve Naroff  }
6323b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman  case Type::Atomic:
6324b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman  {
6325b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman    // Merge two pointer types, while trying to preserve typedef info
6326b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman    QualType LHSValue = LHS->getAs<AtomicType>()->getValueType();
6327b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman    QualType RHSValue = RHS->getAs<AtomicType>()->getValueType();
6328b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman    if (Unqualified) {
6329b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman      LHSValue = LHSValue.getUnqualifiedType();
6330b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman      RHSValue = RHSValue.getUnqualifiedType();
6331b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman    }
6332b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman    QualType ResultType = mergeTypes(LHSValue, RHSValue, false,
6333b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman                                     Unqualified);
6334b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman    if (ResultType.isNull()) return QualType();
6335b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman    if (getCanonicalType(LHSValue) == getCanonicalType(ResultType))
6336b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman      return LHS;
6337b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman    if (getCanonicalType(RHSValue) == getCanonicalType(ResultType))
6338b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman      return RHS;
6339b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman    return getAtomicType(ResultType);
6340b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman  }
63411adb88370beab45af2f065afe86b51ccd59ec50dChris Lattner  case Type::ConstantArray:
63423d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman  {
63433d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    const ConstantArrayType* LCAT = getAsConstantArrayType(LHS);
63443d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    const ConstantArrayType* RCAT = getAsConstantArrayType(RHS);
63453d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    if (LCAT && RCAT && RCAT->getSize() != LCAT->getSize())
63463d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman      return QualType();
63473d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman
63483d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    QualType LHSElem = getAsArrayType(LHS)->getElementType();
63493d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    QualType RHSElem = getAsArrayType(RHS)->getElementType();
6350447234dd459a00a5ed9b7c3e066162cd7a75bf2dDouglas Gregor    if (Unqualified) {
6351447234dd459a00a5ed9b7c3e066162cd7a75bf2dDouglas Gregor      LHSElem = LHSElem.getUnqualifiedType();
6352447234dd459a00a5ed9b7c3e066162cd7a75bf2dDouglas Gregor      RHSElem = RHSElem.getUnqualifiedType();
6353447234dd459a00a5ed9b7c3e066162cd7a75bf2dDouglas Gregor    }
6354447234dd459a00a5ed9b7c3e066162cd7a75bf2dDouglas Gregor
6355447234dd459a00a5ed9b7c3e066162cd7a75bf2dDouglas Gregor    QualType ResultType = mergeTypes(LHSElem, RHSElem, false, Unqualified);
63563d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    if (ResultType.isNull()) return QualType();
635761710854be2b098428aff5316e64bd34b30fbcb7Chris Lattner    if (LCAT && getCanonicalType(LHSElem) == getCanonicalType(ResultType))
635861710854be2b098428aff5316e64bd34b30fbcb7Chris Lattner      return LHS;
635961710854be2b098428aff5316e64bd34b30fbcb7Chris Lattner    if (RCAT && getCanonicalType(RHSElem) == getCanonicalType(ResultType))
636061710854be2b098428aff5316e64bd34b30fbcb7Chris Lattner      return RHS;
63613bc0f45a5e65814f42b22dcdf7249d1120d16f36Eli Friedman    if (LCAT) return getConstantArrayType(ResultType, LCAT->getSize(),
63623bc0f45a5e65814f42b22dcdf7249d1120d16f36Eli Friedman                                          ArrayType::ArraySizeModifier(), 0);
63633bc0f45a5e65814f42b22dcdf7249d1120d16f36Eli Friedman    if (RCAT) return getConstantArrayType(ResultType, RCAT->getSize(),
63643bc0f45a5e65814f42b22dcdf7249d1120d16f36Eli Friedman                                          ArrayType::ArraySizeModifier(), 0);
63653d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    const VariableArrayType* LVAT = getAsVariableArrayType(LHS);
63663d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    const VariableArrayType* RVAT = getAsVariableArrayType(RHS);
636761710854be2b098428aff5316e64bd34b30fbcb7Chris Lattner    if (LVAT && getCanonicalType(LHSElem) == getCanonicalType(ResultType))
636861710854be2b098428aff5316e64bd34b30fbcb7Chris Lattner      return LHS;
636961710854be2b098428aff5316e64bd34b30fbcb7Chris Lattner    if (RVAT && getCanonicalType(RHSElem) == getCanonicalType(ResultType))
637061710854be2b098428aff5316e64bd34b30fbcb7Chris Lattner      return RHS;
63713d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    if (LVAT) {
63723d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman      // FIXME: This isn't correct! But tricky to implement because
63733d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman      // the array's size has to be the size of LHS, but the type
63743d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman      // has to be different.
63753d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman      return LHS;
63763d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    }
63773d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    if (RVAT) {
63783d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman      // FIXME: This isn't correct! But tricky to implement because
63793d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman      // the array's size has to be the size of RHS, but the type
63803d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman      // has to be different.
63813d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman      return RHS;
63823d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    }
63833bc0f45a5e65814f42b22dcdf7249d1120d16f36Eli Friedman    if (getCanonicalType(LHSElem) == getCanonicalType(ResultType)) return LHS;
63843bc0f45a5e65814f42b22dcdf7249d1120d16f36Eli Friedman    if (getCanonicalType(RHSElem) == getCanonicalType(ResultType)) return RHS;
63857e7eb3da052a6d80ddf2377cab0384c798f73f75Douglas Gregor    return getIncompleteArrayType(ResultType,
63867e7eb3da052a6d80ddf2377cab0384c798f73f75Douglas Gregor                                  ArrayType::ArraySizeModifier(), 0);
63873d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman  }
63881adb88370beab45af2f065afe86b51ccd59ec50dChris Lattner  case Type::FunctionNoProto:
6389447234dd459a00a5ed9b7c3e066162cd7a75bf2dDouglas Gregor    return mergeFunctionTypes(LHS, RHS, OfBlockPointer, Unqualified);
639072564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor  case Type::Record:
639172564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor  case Type::Enum:
63923d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    return QualType();
63931adb88370beab45af2f065afe86b51ccd59ec50dChris Lattner  case Type::Builtin:
63943cc4c0c3058a788689b8fc73c0ac139544435c97Chris Lattner    // Only exactly equal builtin types are compatible, which is tested above.
63953d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    return QualType();
639664cfdb7da3cb744642fe8a99ad5c851ad3c930b2Daniel Dunbar  case Type::Complex:
639764cfdb7da3cb744642fe8a99ad5c851ad3c930b2Daniel Dunbar    // Distinct complex types are incompatible.
639864cfdb7da3cb744642fe8a99ad5c851ad3c930b2Daniel Dunbar    return QualType();
63993cc4c0c3058a788689b8fc73c0ac139544435c97Chris Lattner  case Type::Vector:
64005a61f0e5c5aaecd5713c3fa4b78be7167a7eeff2Eli Friedman    // FIXME: The merged type should be an ExtVector!
64011c471f3e1c0ec8cbc82447bb35908dfc55463e46John McCall    if (areCompatVectorTypes(LHSCan->getAs<VectorType>(),
64021c471f3e1c0ec8cbc82447bb35908dfc55463e46John McCall                             RHSCan->getAs<VectorType>()))
64033d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman      return LHS;
640461710854be2b098428aff5316e64bd34b30fbcb7Chris Lattner    return QualType();
6405c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall  case Type::ObjCObject: {
6406c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall    // Check if the types are assignment compatible.
64075a61f0e5c5aaecd5713c3fa4b78be7167a7eeff2Eli Friedman    // FIXME: This should be type compatibility, e.g. whether
64085a61f0e5c5aaecd5713c3fa4b78be7167a7eeff2Eli Friedman    // "LHS x; RHS x;" at global scope is legal.
6409c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall    const ObjCObjectType* LHSIface = LHS->getAs<ObjCObjectType>();
6410c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall    const ObjCObjectType* RHSIface = RHS->getAs<ObjCObjectType>();
6411c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall    if (canAssignObjCInterfaces(LHSIface, RHSIface))
64125fd659db11922fc12a58e478f7b745f9656b15a7Steve Naroff      return LHS;
64135fd659db11922fc12a58e478f7b745f9656b15a7Steve Naroff
64143d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    return QualType();
641561490e9a965cfee8a78c12c6802138844f04250dCedric Venet  }
641614108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff  case Type::ObjCObjectPointer: {
6417132f2a2da34f378fc675b9e174564b0f52c31d98Fariborz Jahanian    if (OfBlockPointer) {
6418132f2a2da34f378fc675b9e174564b0f52c31d98Fariborz Jahanian      if (canAssignObjCInterfacesInBlockPointer(
6419132f2a2da34f378fc675b9e174564b0f52c31d98Fariborz Jahanian                                          LHS->getAs<ObjCObjectPointerType>(),
6420a4fdbfad150ae37bddaa4094d3925a27a1a1cf3fFariborz Jahanian                                          RHS->getAs<ObjCObjectPointerType>(),
6421a4fdbfad150ae37bddaa4094d3925a27a1a1cf3fFariborz Jahanian                                          BlockReturnType))
64227530c034c0c71a64c5a9173206d9742ae847af8bDavid Blaikie        return LHS;
6423132f2a2da34f378fc675b9e174564b0f52c31d98Fariborz Jahanian      return QualType();
6424132f2a2da34f378fc675b9e174564b0f52c31d98Fariborz Jahanian    }
6425183700f494ec9b6701b6efe82bcb25f4c79ba561John McCall    if (canAssignObjCInterfaces(LHS->getAs<ObjCObjectPointerType>(),
6426183700f494ec9b6701b6efe82bcb25f4c79ba561John McCall                                RHS->getAs<ObjCObjectPointerType>()))
642714108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff      return LHS;
642814108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff
6429bc76dd06eb881c70c9775b74bab8b88cd747f173Steve Naroff    return QualType();
64307530c034c0c71a64c5a9173206d9742ae847af8bDavid Blaikie  }
6431ec0550fa3653d46560bf4484a2e988329c228e39Steve Naroff  }
643272564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor
64337530c034c0c71a64c5a9173206d9742ae847af8bDavid Blaikie  llvm_unreachable("Invalid Type::Class!");
6434ec0550fa3653d46560bf4484a2e988329c228e39Steve Naroff}
64357192f8e9592729882a09d84d77838db26e39ebd4Ted Kremenek
643678213e4aec6d8d22b5828dd2687fa19116ebaa26Fariborz Jahanianbool ASTContext::FunctionTypesMatchOnNSConsumedAttrs(
643778213e4aec6d8d22b5828dd2687fa19116ebaa26Fariborz Jahanian                   const FunctionProtoType *FromFunctionType,
643878213e4aec6d8d22b5828dd2687fa19116ebaa26Fariborz Jahanian                   const FunctionProtoType *ToFunctionType) {
643978213e4aec6d8d22b5828dd2687fa19116ebaa26Fariborz Jahanian  if (FromFunctionType->hasAnyConsumedArgs() !=
644078213e4aec6d8d22b5828dd2687fa19116ebaa26Fariborz Jahanian      ToFunctionType->hasAnyConsumedArgs())
644178213e4aec6d8d22b5828dd2687fa19116ebaa26Fariborz Jahanian    return false;
644278213e4aec6d8d22b5828dd2687fa19116ebaa26Fariborz Jahanian  FunctionProtoType::ExtProtoInfo FromEPI =
644378213e4aec6d8d22b5828dd2687fa19116ebaa26Fariborz Jahanian    FromFunctionType->getExtProtoInfo();
644478213e4aec6d8d22b5828dd2687fa19116ebaa26Fariborz Jahanian  FunctionProtoType::ExtProtoInfo ToEPI =
644578213e4aec6d8d22b5828dd2687fa19116ebaa26Fariborz Jahanian    ToFunctionType->getExtProtoInfo();
644678213e4aec6d8d22b5828dd2687fa19116ebaa26Fariborz Jahanian  if (FromEPI.ConsumedArguments && ToEPI.ConsumedArguments)
644778213e4aec6d8d22b5828dd2687fa19116ebaa26Fariborz Jahanian    for (unsigned ArgIdx = 0, NumArgs = FromFunctionType->getNumArgs();
644878213e4aec6d8d22b5828dd2687fa19116ebaa26Fariborz Jahanian         ArgIdx != NumArgs; ++ArgIdx)  {
644978213e4aec6d8d22b5828dd2687fa19116ebaa26Fariborz Jahanian      if (FromEPI.ConsumedArguments[ArgIdx] !=
645078213e4aec6d8d22b5828dd2687fa19116ebaa26Fariborz Jahanian          ToEPI.ConsumedArguments[ArgIdx])
645178213e4aec6d8d22b5828dd2687fa19116ebaa26Fariborz Jahanian        return false;
645278213e4aec6d8d22b5828dd2687fa19116ebaa26Fariborz Jahanian    }
645378213e4aec6d8d22b5828dd2687fa19116ebaa26Fariborz Jahanian  return true;
645478213e4aec6d8d22b5828dd2687fa19116ebaa26Fariborz Jahanian}
645578213e4aec6d8d22b5828dd2687fa19116ebaa26Fariborz Jahanian
64562390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian/// mergeObjCGCQualifiers - This routine merges ObjC's GC attribute of 'LHS' and
64572390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian/// 'RHS' attributes and returns the merged version; including for function
64582390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian/// return types.
64592390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz JahanianQualType ASTContext::mergeObjCGCQualifiers(QualType LHS, QualType RHS) {
64602390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian  QualType LHSCan = getCanonicalType(LHS),
64612390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian  RHSCan = getCanonicalType(RHS);
64622390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian  // If two types are identical, they are compatible.
64632390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian  if (LHSCan == RHSCan)
64642390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian    return LHS;
64652390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian  if (RHSCan->isFunctionType()) {
64662390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian    if (!LHSCan->isFunctionType())
64672390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian      return QualType();
64682390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian    QualType OldReturnType =
64692390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian      cast<FunctionType>(RHSCan.getTypePtr())->getResultType();
64702390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian    QualType NewReturnType =
64712390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian      cast<FunctionType>(LHSCan.getTypePtr())->getResultType();
64722390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian    QualType ResReturnType =
64732390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian      mergeObjCGCQualifiers(NewReturnType, OldReturnType);
64742390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian    if (ResReturnType.isNull())
64752390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian      return QualType();
64762390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian    if (ResReturnType == NewReturnType || ResReturnType == OldReturnType) {
64772390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian      // id foo(); ... __strong id foo(); or: __strong id foo(); ... id foo();
64782390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian      // In either case, use OldReturnType to build the new function type.
64792390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian      const FunctionType *F = LHS->getAs<FunctionType>();
64802390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian      if (const FunctionProtoType *FPT = cast<FunctionProtoType>(F)) {
6481e23cf437fe76b1ed02d63c3f61b456fd48a915f5John McCall        FunctionProtoType::ExtProtoInfo EPI = FPT->getExtProtoInfo();
6482e23cf437fe76b1ed02d63c3f61b456fd48a915f5John McCall        EPI.ExtInfo = getFunctionExtInfo(LHS);
64832390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian        QualType ResultType
64842390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian          = getFunctionType(OldReturnType, FPT->arg_type_begin(),
6485e23cf437fe76b1ed02d63c3f61b456fd48a915f5John McCall                            FPT->getNumArgs(), EPI);
64862390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian        return ResultType;
64872390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian      }
64882390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian    }
64892390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian    return QualType();
64902390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian  }
64912390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian
64922390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian  // If the qualifiers are different, the types can still be merged.
64932390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian  Qualifiers LQuals = LHSCan.getLocalQualifiers();
64942390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian  Qualifiers RQuals = RHSCan.getLocalQualifiers();
64952390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian  if (LQuals != RQuals) {
64962390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian    // If any of these qualifiers are different, we have a type mismatch.
64972390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian    if (LQuals.getCVRQualifiers() != RQuals.getCVRQualifiers() ||
64982390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian        LQuals.getAddressSpace() != RQuals.getAddressSpace())
64992390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian      return QualType();
65002390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian
65012390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian    // Exactly one GC qualifier difference is allowed: __strong is
65022390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian    // okay if the other type has no GC qualifier but is an Objective
65032390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian    // C object pointer (i.e. implicitly strong by default).  We fix
65042390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian    // this by pretending that the unqualified type was actually
65052390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian    // qualified __strong.
65062390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian    Qualifiers::GC GC_L = LQuals.getObjCGCAttr();
65072390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian    Qualifiers::GC GC_R = RQuals.getObjCGCAttr();
65082390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian    assert((GC_L != GC_R) && "unequal qualifier sets had only equal elements");
65092390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian
65102390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian    if (GC_L == Qualifiers::Weak || GC_R == Qualifiers::Weak)
65112390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian      return QualType();
65122390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian
65132390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian    if (GC_L == Qualifiers::Strong)
65142390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian      return LHS;
65152390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian    if (GC_R == Qualifiers::Strong)
65162390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian      return RHS;
65172390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian    return QualType();
65182390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian  }
65192390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian
65202390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian  if (LHSCan->isObjCObjectPointerType() && RHSCan->isObjCObjectPointerType()) {
65212390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian    QualType LHSBaseQT = LHS->getAs<ObjCObjectPointerType>()->getPointeeType();
65222390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian    QualType RHSBaseQT = RHS->getAs<ObjCObjectPointerType>()->getPointeeType();
65232390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian    QualType ResQT = mergeObjCGCQualifiers(LHSBaseQT, RHSBaseQT);
65242390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian    if (ResQT == LHSBaseQT)
65252390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian      return LHS;
65262390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian    if (ResQT == RHSBaseQT)
65272390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian      return RHS;
65282390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian  }
65292390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian  return QualType();
65302390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian}
65312390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian
65325426bf6456a5aeac416a9150de157904d101c819Chris Lattner//===----------------------------------------------------------------------===//
6533ad74a758189180b8ab8faea648e4766c3bfd7fcbEli Friedman//                         Integer Predicates
6534ad74a758189180b8ab8faea648e4766c3bfd7fcbEli Friedman//===----------------------------------------------------------------------===//
653588054dee0402e4d3c1f64e6b697acc47195c0d72Chris Lattner
65364ba2a17694148e16eaa8d3917f657ffcd3667be4Jay Foadunsigned ASTContext::getIntWidth(QualType T) const {
6537f4c7371fb1d3cebcfb40abad4537bb82515704eaJohn McCall  if (const EnumType *ET = dyn_cast<EnumType>(T))
653829a7f3342c3c6dd15d914c61ae22246c36d51ce7Eli Friedman    T = ET->getDecl()->getIntegerType();
65391274ccd90aec0b205fc838c3d504821ccfb55482Douglas Gregor  if (T->isBooleanType())
65401274ccd90aec0b205fc838c3d504821ccfb55482Douglas Gregor    return 1;
6541f98aba35e6c3da5aae61843fc01334939e4e12ecEli Friedman  // For builtin types, just use the standard type sizing method
6542ad74a758189180b8ab8faea648e4766c3bfd7fcbEli Friedman  return (unsigned)getTypeSize(T);
6543ad74a758189180b8ab8faea648e4766c3bfd7fcbEli Friedman}
6544ad74a758189180b8ab8faea648e4766c3bfd7fcbEli Friedman
6545ad74a758189180b8ab8faea648e4766c3bfd7fcbEli FriedmanQualType ASTContext::getCorrespondingUnsignedType(QualType T) {
6546f60946222721d9ba3c059563935c17b84703187aDouglas Gregor  assert(T->hasSignedIntegerRepresentation() && "Unexpected type");
65476a2b9261bf9c973c7122d9d1febce24a38fa862dChris Lattner
65486a2b9261bf9c973c7122d9d1febce24a38fa862dChris Lattner  // Turn <4 x signed int> -> <4 x unsigned int>
65496a2b9261bf9c973c7122d9d1febce24a38fa862dChris Lattner  if (const VectorType *VTy = T->getAs<VectorType>())
65506a2b9261bf9c973c7122d9d1febce24a38fa862dChris Lattner    return getVectorType(getCorrespondingUnsignedType(VTy->getElementType()),
6551e86d78cf4754a6aef2cf9a33d847aa15338e276fBob Wilson                         VTy->getNumElements(), VTy->getVectorKind());
65526a2b9261bf9c973c7122d9d1febce24a38fa862dChris Lattner
65536a2b9261bf9c973c7122d9d1febce24a38fa862dChris Lattner  // For enums, we return the unsigned version of the base type.
65546a2b9261bf9c973c7122d9d1febce24a38fa862dChris Lattner  if (const EnumType *ETy = T->getAs<EnumType>())
6555ad74a758189180b8ab8faea648e4766c3bfd7fcbEli Friedman    T = ETy->getDecl()->getIntegerType();
65566a2b9261bf9c973c7122d9d1febce24a38fa862dChris Lattner
65576a2b9261bf9c973c7122d9d1febce24a38fa862dChris Lattner  const BuiltinType *BTy = T->getAs<BuiltinType>();
65586a2b9261bf9c973c7122d9d1febce24a38fa862dChris Lattner  assert(BTy && "Unexpected signed integer type");
6559ad74a758189180b8ab8faea648e4766c3bfd7fcbEli Friedman  switch (BTy->getKind()) {
6560ad74a758189180b8ab8faea648e4766c3bfd7fcbEli Friedman  case BuiltinType::Char_S:
6561ad74a758189180b8ab8faea648e4766c3bfd7fcbEli Friedman  case BuiltinType::SChar:
6562ad74a758189180b8ab8faea648e4766c3bfd7fcbEli Friedman    return UnsignedCharTy;
6563ad74a758189180b8ab8faea648e4766c3bfd7fcbEli Friedman  case BuiltinType::Short:
6564ad74a758189180b8ab8faea648e4766c3bfd7fcbEli Friedman    return UnsignedShortTy;
6565ad74a758189180b8ab8faea648e4766c3bfd7fcbEli Friedman  case BuiltinType::Int:
6566ad74a758189180b8ab8faea648e4766c3bfd7fcbEli Friedman    return UnsignedIntTy;
6567ad74a758189180b8ab8faea648e4766c3bfd7fcbEli Friedman  case BuiltinType::Long:
6568ad74a758189180b8ab8faea648e4766c3bfd7fcbEli Friedman    return UnsignedLongTy;
6569ad74a758189180b8ab8faea648e4766c3bfd7fcbEli Friedman  case BuiltinType::LongLong:
6570ad74a758189180b8ab8faea648e4766c3bfd7fcbEli Friedman    return UnsignedLongLongTy;
65712df9ced9fd1e8c7d7b38443db07e0e811de22571Chris Lattner  case BuiltinType::Int128:
65722df9ced9fd1e8c7d7b38443db07e0e811de22571Chris Lattner    return UnsignedInt128Ty;
6573ad74a758189180b8ab8faea648e4766c3bfd7fcbEli Friedman  default:
6574b219cfc4d75f0a03630b7c4509ef791b7e97b2c8David Blaikie    llvm_unreachable("Unexpected signed integer type");
6575ad74a758189180b8ab8faea648e4766c3bfd7fcbEli Friedman  }
6576ad74a758189180b8ab8faea648e4766c3bfd7fcbEli Friedman}
6577ad74a758189180b8ab8faea648e4766c3bfd7fcbEli Friedman
65787b90340c9c7d07aef4e301e72b5e8a30d5f4f0c8Argyrios KyrtzidisASTMutationListener::~ASTMutationListener() { }
65797b90340c9c7d07aef4e301e72b5e8a30d5f4f0c8Argyrios Kyrtzidis
658086df27bbdbb98c39ec2184695c0561209f91beddChris Lattner
658186df27bbdbb98c39ec2184695c0561209f91beddChris Lattner//===----------------------------------------------------------------------===//
658286df27bbdbb98c39ec2184695c0561209f91beddChris Lattner//                          Builtin Type Computation
658386df27bbdbb98c39ec2184695c0561209f91beddChris Lattner//===----------------------------------------------------------------------===//
658486df27bbdbb98c39ec2184695c0561209f91beddChris Lattner
658586df27bbdbb98c39ec2184695c0561209f91beddChris Lattner/// DecodeTypeFromStr - This decodes one type descriptor from Str, advancing the
658633daae66462e8f51ee153463b32bdefd60c801aaChris Lattner/// pointer over the consumed characters.  This returns the resultant type.  If
658733daae66462e8f51ee153463b32bdefd60c801aaChris Lattner/// AllowTypeModifiers is false then modifier like * are not parsed, just basic
658833daae66462e8f51ee153463b32bdefd60c801aaChris Lattner/// types.  This allows "v2i*" to be parsed as a pointer to a v2i instead of
658933daae66462e8f51ee153463b32bdefd60c801aaChris Lattner/// a vector of "i*".
659014e0e7436cf6650a72052baea1f8ebe644cef489Chris Lattner///
659114e0e7436cf6650a72052baea1f8ebe644cef489Chris Lattner/// RequiresICE is filled in on return to indicate whether the value is required
659214e0e7436cf6650a72052baea1f8ebe644cef489Chris Lattner/// to be an Integer Constant Expression.
65934ba2a17694148e16eaa8d3917f657ffcd3667be4Jay Foadstatic QualType DecodeTypeFromStr(const char *&Str, const ASTContext &Context,
659486df27bbdbb98c39ec2184695c0561209f91beddChris Lattner                                  ASTContext::GetBuiltinTypeError &Error,
659514e0e7436cf6650a72052baea1f8ebe644cef489Chris Lattner                                  bool &RequiresICE,
659633daae66462e8f51ee153463b32bdefd60c801aaChris Lattner                                  bool AllowTypeModifiers) {
659786df27bbdbb98c39ec2184695c0561209f91beddChris Lattner  // Modifiers.
659886df27bbdbb98c39ec2184695c0561209f91beddChris Lattner  int HowLong = 0;
659986df27bbdbb98c39ec2184695c0561209f91beddChris Lattner  bool Signed = false, Unsigned = false;
660014e0e7436cf6650a72052baea1f8ebe644cef489Chris Lattner  RequiresICE = false;
6601393bd8e185692a451b2ba16bdfc9e7d3543b4217Chris Lattner
660233daae66462e8f51ee153463b32bdefd60c801aaChris Lattner  // Read the prefixed modifiers first.
660386df27bbdbb98c39ec2184695c0561209f91beddChris Lattner  bool Done = false;
660486df27bbdbb98c39ec2184695c0561209f91beddChris Lattner  while (!Done) {
660586df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    switch (*Str++) {
66061eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    default: Done = true; --Str; break;
6607393bd8e185692a451b2ba16bdfc9e7d3543b4217Chris Lattner    case 'I':
660814e0e7436cf6650a72052baea1f8ebe644cef489Chris Lattner      RequiresICE = true;
6609393bd8e185692a451b2ba16bdfc9e7d3543b4217Chris Lattner      break;
661086df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    case 'S':
661186df27bbdbb98c39ec2184695c0561209f91beddChris Lattner      assert(!Unsigned && "Can't use both 'S' and 'U' modifiers!");
661286df27bbdbb98c39ec2184695c0561209f91beddChris Lattner      assert(!Signed && "Can't use 'S' modifier multiple times!");
661386df27bbdbb98c39ec2184695c0561209f91beddChris Lattner      Signed = true;
661486df27bbdbb98c39ec2184695c0561209f91beddChris Lattner      break;
661586df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    case 'U':
661686df27bbdbb98c39ec2184695c0561209f91beddChris Lattner      assert(!Signed && "Can't use both 'S' and 'U' modifiers!");
661786df27bbdbb98c39ec2184695c0561209f91beddChris Lattner      assert(!Unsigned && "Can't use 'S' modifier multiple times!");
661886df27bbdbb98c39ec2184695c0561209f91beddChris Lattner      Unsigned = true;
661986df27bbdbb98c39ec2184695c0561209f91beddChris Lattner      break;
662086df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    case 'L':
662186df27bbdbb98c39ec2184695c0561209f91beddChris Lattner      assert(HowLong <= 2 && "Can't have LLLL modifier");
662286df27bbdbb98c39ec2184695c0561209f91beddChris Lattner      ++HowLong;
662386df27bbdbb98c39ec2184695c0561209f91beddChris Lattner      break;
662486df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    }
662586df27bbdbb98c39ec2184695c0561209f91beddChris Lattner  }
662686df27bbdbb98c39ec2184695c0561209f91beddChris Lattner
662786df27bbdbb98c39ec2184695c0561209f91beddChris Lattner  QualType Type;
66281eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
662986df27bbdbb98c39ec2184695c0561209f91beddChris Lattner  // Read the base type.
663086df27bbdbb98c39ec2184695c0561209f91beddChris Lattner  switch (*Str++) {
6631b219cfc4d75f0a03630b7c4509ef791b7e97b2c8David Blaikie  default: llvm_unreachable("Unknown builtin type letter!");
663286df27bbdbb98c39ec2184695c0561209f91beddChris Lattner  case 'v':
663386df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    assert(HowLong == 0 && !Signed && !Unsigned &&
663486df27bbdbb98c39ec2184695c0561209f91beddChris Lattner           "Bad modifiers used with 'v'!");
663586df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    Type = Context.VoidTy;
663686df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    break;
663786df27bbdbb98c39ec2184695c0561209f91beddChris Lattner  case 'f':
663886df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    assert(HowLong == 0 && !Signed && !Unsigned &&
663986df27bbdbb98c39ec2184695c0561209f91beddChris Lattner           "Bad modifiers used with 'f'!");
664086df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    Type = Context.FloatTy;
664186df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    break;
664286df27bbdbb98c39ec2184695c0561209f91beddChris Lattner  case 'd':
664386df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    assert(HowLong < 2 && !Signed && !Unsigned &&
664486df27bbdbb98c39ec2184695c0561209f91beddChris Lattner           "Bad modifiers used with 'd'!");
664586df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    if (HowLong)
664686df27bbdbb98c39ec2184695c0561209f91beddChris Lattner      Type = Context.LongDoubleTy;
664786df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    else
664886df27bbdbb98c39ec2184695c0561209f91beddChris Lattner      Type = Context.DoubleTy;
664986df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    break;
665086df27bbdbb98c39ec2184695c0561209f91beddChris Lattner  case 's':
665186df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    assert(HowLong == 0 && "Bad modifiers used with 's'!");
665286df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    if (Unsigned)
665386df27bbdbb98c39ec2184695c0561209f91beddChris Lattner      Type = Context.UnsignedShortTy;
665486df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    else
665586df27bbdbb98c39ec2184695c0561209f91beddChris Lattner      Type = Context.ShortTy;
665686df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    break;
665786df27bbdbb98c39ec2184695c0561209f91beddChris Lattner  case 'i':
665886df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    if (HowLong == 3)
665986df27bbdbb98c39ec2184695c0561209f91beddChris Lattner      Type = Unsigned ? Context.UnsignedInt128Ty : Context.Int128Ty;
666086df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    else if (HowLong == 2)
666186df27bbdbb98c39ec2184695c0561209f91beddChris Lattner      Type = Unsigned ? Context.UnsignedLongLongTy : Context.LongLongTy;
666286df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    else if (HowLong == 1)
666386df27bbdbb98c39ec2184695c0561209f91beddChris Lattner      Type = Unsigned ? Context.UnsignedLongTy : Context.LongTy;
666486df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    else
666586df27bbdbb98c39ec2184695c0561209f91beddChris Lattner      Type = Unsigned ? Context.UnsignedIntTy : Context.IntTy;
666686df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    break;
666786df27bbdbb98c39ec2184695c0561209f91beddChris Lattner  case 'c':
666886df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    assert(HowLong == 0 && "Bad modifiers used with 'c'!");
666986df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    if (Signed)
667086df27bbdbb98c39ec2184695c0561209f91beddChris Lattner      Type = Context.SignedCharTy;
667186df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    else if (Unsigned)
667286df27bbdbb98c39ec2184695c0561209f91beddChris Lattner      Type = Context.UnsignedCharTy;
667386df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    else
667486df27bbdbb98c39ec2184695c0561209f91beddChris Lattner      Type = Context.CharTy;
667586df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    break;
667686df27bbdbb98c39ec2184695c0561209f91beddChris Lattner  case 'b': // boolean
667786df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    assert(HowLong == 0 && !Signed && !Unsigned && "Bad modifiers for 'b'!");
667886df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    Type = Context.BoolTy;
667986df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    break;
668086df27bbdbb98c39ec2184695c0561209f91beddChris Lattner  case 'z':  // size_t.
668186df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    assert(HowLong == 0 && !Signed && !Unsigned && "Bad modifiers for 'z'!");
668286df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    Type = Context.getSizeType();
668386df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    break;
668486df27bbdbb98c39ec2184695c0561209f91beddChris Lattner  case 'F':
668586df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    Type = Context.getCFConstantStringType();
668686df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    break;
6687ba8bda05fefd3bb2f1ef201784b685f715bdde29Fariborz Jahanian  case 'G':
6688ba8bda05fefd3bb2f1ef201784b685f715bdde29Fariborz Jahanian    Type = Context.getObjCIdType();
6689ba8bda05fefd3bb2f1ef201784b685f715bdde29Fariborz Jahanian    break;
6690ba8bda05fefd3bb2f1ef201784b685f715bdde29Fariborz Jahanian  case 'H':
6691ba8bda05fefd3bb2f1ef201784b685f715bdde29Fariborz Jahanian    Type = Context.getObjCSelType();
6692ba8bda05fefd3bb2f1ef201784b685f715bdde29Fariborz Jahanian    break;
669386df27bbdbb98c39ec2184695c0561209f91beddChris Lattner  case 'a':
669486df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    Type = Context.getBuiltinVaListType();
669586df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    assert(!Type.isNull() && "builtin va list type not initialized!");
669686df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    break;
669786df27bbdbb98c39ec2184695c0561209f91beddChris Lattner  case 'A':
669886df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    // This is a "reference" to a va_list; however, what exactly
669986df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    // this means depends on how va_list is defined. There are two
670086df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    // different kinds of va_list: ones passed by value, and ones
670186df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    // passed by reference.  An example of a by-value va_list is
670286df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    // x86, where va_list is a char*. An example of by-ref va_list
670386df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    // is x86-64, where va_list is a __va_list_tag[1]. For x86,
670486df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    // we want this argument to be a char*&; for x86-64, we want
670586df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    // it to be a __va_list_tag*.
670686df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    Type = Context.getBuiltinVaListType();
670786df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    assert(!Type.isNull() && "builtin va list type not initialized!");
670814e0e7436cf6650a72052baea1f8ebe644cef489Chris Lattner    if (Type->isArrayType())
670986df27bbdbb98c39ec2184695c0561209f91beddChris Lattner      Type = Context.getArrayDecayedType(Type);
671014e0e7436cf6650a72052baea1f8ebe644cef489Chris Lattner    else
671186df27bbdbb98c39ec2184695c0561209f91beddChris Lattner      Type = Context.getLValueReferenceType(Type);
671286df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    break;
671386df27bbdbb98c39ec2184695c0561209f91beddChris Lattner  case 'V': {
671486df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    char *End;
671586df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    unsigned NumElements = strtoul(Str, &End, 10);
671686df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    assert(End != Str && "Missing vector size");
671786df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    Str = End;
67181eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
671914e0e7436cf6650a72052baea1f8ebe644cef489Chris Lattner    QualType ElementType = DecodeTypeFromStr(Str, Context, Error,
672014e0e7436cf6650a72052baea1f8ebe644cef489Chris Lattner                                             RequiresICE, false);
672114e0e7436cf6650a72052baea1f8ebe644cef489Chris Lattner    assert(!RequiresICE && "Can't require vector ICE");
672233daae66462e8f51ee153463b32bdefd60c801aaChris Lattner
672333daae66462e8f51ee153463b32bdefd60c801aaChris Lattner    // TODO: No way to make AltiVec vectors in builtins yet.
6724788b0fd67e1992f23555454efcdb16a19dfefac3Chris Lattner    Type = Context.getVectorType(ElementType, NumElements,
6725e86d78cf4754a6aef2cf9a33d847aa15338e276fBob Wilson                                 VectorType::GenericVector);
672686df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    break;
672786df27bbdbb98c39ec2184695c0561209f91beddChris Lattner  }
6728b4bc99ba5540a618c86c136c9112ead8fb1d9866Douglas Gregor  case 'E': {
6729b4bc99ba5540a618c86c136c9112ead8fb1d9866Douglas Gregor    char *End;
6730b4bc99ba5540a618c86c136c9112ead8fb1d9866Douglas Gregor
6731b4bc99ba5540a618c86c136c9112ead8fb1d9866Douglas Gregor    unsigned NumElements = strtoul(Str, &End, 10);
6732b4bc99ba5540a618c86c136c9112ead8fb1d9866Douglas Gregor    assert(End != Str && "Missing vector size");
6733b4bc99ba5540a618c86c136c9112ead8fb1d9866Douglas Gregor
6734b4bc99ba5540a618c86c136c9112ead8fb1d9866Douglas Gregor    Str = End;
6735b4bc99ba5540a618c86c136c9112ead8fb1d9866Douglas Gregor
6736b4bc99ba5540a618c86c136c9112ead8fb1d9866Douglas Gregor    QualType ElementType = DecodeTypeFromStr(Str, Context, Error, RequiresICE,
6737b4bc99ba5540a618c86c136c9112ead8fb1d9866Douglas Gregor                                             false);
6738b4bc99ba5540a618c86c136c9112ead8fb1d9866Douglas Gregor    Type = Context.getExtVectorType(ElementType, NumElements);
6739b4bc99ba5540a618c86c136c9112ead8fb1d9866Douglas Gregor    break;
6740b4bc99ba5540a618c86c136c9112ead8fb1d9866Douglas Gregor  }
6741d3a23b238a2b2c0f11e6ac4951c7410a8c5717bfDouglas Gregor  case 'X': {
674214e0e7436cf6650a72052baea1f8ebe644cef489Chris Lattner    QualType ElementType = DecodeTypeFromStr(Str, Context, Error, RequiresICE,
674314e0e7436cf6650a72052baea1f8ebe644cef489Chris Lattner                                             false);
674414e0e7436cf6650a72052baea1f8ebe644cef489Chris Lattner    assert(!RequiresICE && "Can't require complex ICE");
6745d3a23b238a2b2c0f11e6ac4951c7410a8c5717bfDouglas Gregor    Type = Context.getComplexType(ElementType);
6746d3a23b238a2b2c0f11e6ac4951c7410a8c5717bfDouglas Gregor    break;
6747cc075e4092900ac9eec9dc92e35096eae1ee29c7Fariborz Jahanian  }
6748cc075e4092900ac9eec9dc92e35096eae1ee29c7Fariborz Jahanian  case 'Y' : {
6749cc075e4092900ac9eec9dc92e35096eae1ee29c7Fariborz Jahanian    Type = Context.getPointerDiffType();
6750cc075e4092900ac9eec9dc92e35096eae1ee29c7Fariborz Jahanian    break;
6751cc075e4092900ac9eec9dc92e35096eae1ee29c7Fariborz Jahanian  }
67529a5a7e7351f78345a72c4956af25590f6d40ebcdChris Lattner  case 'P':
6753c29f77b769bcc5b6dc85e72c8e3cc2e348e5cf25Douglas Gregor    Type = Context.getFILEType();
6754c29f77b769bcc5b6dc85e72c8e3cc2e348e5cf25Douglas Gregor    if (Type.isNull()) {
6755f711c41dd9412a8182793259d355c4f6979ed5edMike Stump      Error = ASTContext::GE_Missing_stdio;
675686df27bbdbb98c39ec2184695c0561209f91beddChris Lattner      return QualType();
675786df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    }
6758fd612dbb23cd31c03c898ae53ff18d0dfd8488f9Mike Stump    break;
67599a5a7e7351f78345a72c4956af25590f6d40ebcdChris Lattner  case 'J':
6760f711c41dd9412a8182793259d355c4f6979ed5edMike Stump    if (Signed)
6761782fa308a765aeac2acb39c4e697c937ec21185bMike Stump      Type = Context.getsigjmp_bufType();
6762f711c41dd9412a8182793259d355c4f6979ed5edMike Stump    else
6763f711c41dd9412a8182793259d355c4f6979ed5edMike Stump      Type = Context.getjmp_bufType();
6764f711c41dd9412a8182793259d355c4f6979ed5edMike Stump
6765fd612dbb23cd31c03c898ae53ff18d0dfd8488f9Mike Stump    if (Type.isNull()) {
6766f711c41dd9412a8182793259d355c4f6979ed5edMike Stump      Error = ASTContext::GE_Missing_setjmp;
6767fd612dbb23cd31c03c898ae53ff18d0dfd8488f9Mike Stump      return QualType();
6768fd612dbb23cd31c03c898ae53ff18d0dfd8488f9Mike Stump    }
6769fd612dbb23cd31c03c898ae53ff18d0dfd8488f9Mike Stump    break;
6770e2d4f4ed44a32f179c6d48cd1dba8346ab2129d9Rafael Espindola  case 'K':
6771e2d4f4ed44a32f179c6d48cd1dba8346ab2129d9Rafael Espindola    assert(HowLong == 0 && !Signed && !Unsigned && "Bad modifiers for 'K'!");
6772e2d4f4ed44a32f179c6d48cd1dba8346ab2129d9Rafael Espindola    Type = Context.getucontext_tType();
6773e2d4f4ed44a32f179c6d48cd1dba8346ab2129d9Rafael Espindola
6774e2d4f4ed44a32f179c6d48cd1dba8346ab2129d9Rafael Espindola    if (Type.isNull()) {
6775e2d4f4ed44a32f179c6d48cd1dba8346ab2129d9Rafael Espindola      Error = ASTContext::GE_Missing_ucontext;
6776e2d4f4ed44a32f179c6d48cd1dba8346ab2129d9Rafael Espindola      return QualType();
6777e2d4f4ed44a32f179c6d48cd1dba8346ab2129d9Rafael Espindola    }
6778e2d4f4ed44a32f179c6d48cd1dba8346ab2129d9Rafael Espindola    break;
6779782fa308a765aeac2acb39c4e697c937ec21185bMike Stump  }
67801eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
678133daae66462e8f51ee153463b32bdefd60c801aaChris Lattner  // If there are modifiers and if we're allowed to parse them, go for it.
678233daae66462e8f51ee153463b32bdefd60c801aaChris Lattner  Done = !AllowTypeModifiers;
678386df27bbdbb98c39ec2184695c0561209f91beddChris Lattner  while (!Done) {
6784187ab37a05b8f7015b9f39cc8cd9129a0c6d0b48John McCall    switch (char c = *Str++) {
678533daae66462e8f51ee153463b32bdefd60c801aaChris Lattner    default: Done = true; --Str; break;
678633daae66462e8f51ee153463b32bdefd60c801aaChris Lattner    case '*':
678733daae66462e8f51ee153463b32bdefd60c801aaChris Lattner    case '&': {
678833daae66462e8f51ee153463b32bdefd60c801aaChris Lattner      // Both pointers and references can have their pointee types
678933daae66462e8f51ee153463b32bdefd60c801aaChris Lattner      // qualified with an address space.
679033daae66462e8f51ee153463b32bdefd60c801aaChris Lattner      char *End;
679133daae66462e8f51ee153463b32bdefd60c801aaChris Lattner      unsigned AddrSpace = strtoul(Str, &End, 10);
679233daae66462e8f51ee153463b32bdefd60c801aaChris Lattner      if (End != Str && AddrSpace != 0) {
679333daae66462e8f51ee153463b32bdefd60c801aaChris Lattner        Type = Context.getAddrSpaceQualType(Type, AddrSpace);
679433daae66462e8f51ee153463b32bdefd60c801aaChris Lattner        Str = End;
679533daae66462e8f51ee153463b32bdefd60c801aaChris Lattner      }
679633daae66462e8f51ee153463b32bdefd60c801aaChris Lattner      if (c == '*')
679733daae66462e8f51ee153463b32bdefd60c801aaChris Lattner        Type = Context.getPointerType(Type);
679833daae66462e8f51ee153463b32bdefd60c801aaChris Lattner      else
679933daae66462e8f51ee153463b32bdefd60c801aaChris Lattner        Type = Context.getLValueReferenceType(Type);
680033daae66462e8f51ee153463b32bdefd60c801aaChris Lattner      break;
680133daae66462e8f51ee153463b32bdefd60c801aaChris Lattner    }
680233daae66462e8f51ee153463b32bdefd60c801aaChris Lattner    // FIXME: There's no way to have a built-in with an rvalue ref arg.
680333daae66462e8f51ee153463b32bdefd60c801aaChris Lattner    case 'C':
680433daae66462e8f51ee153463b32bdefd60c801aaChris Lattner      Type = Type.withConst();
680533daae66462e8f51ee153463b32bdefd60c801aaChris Lattner      break;
680633daae66462e8f51ee153463b32bdefd60c801aaChris Lattner    case 'D':
680733daae66462e8f51ee153463b32bdefd60c801aaChris Lattner      Type = Context.getVolatileType(Type);
680833daae66462e8f51ee153463b32bdefd60c801aaChris Lattner      break;
680918932a0f2a94a7813ec461d1118c39ecf8aa936fTed Kremenek    case 'R':
681018932a0f2a94a7813ec461d1118c39ecf8aa936fTed Kremenek      Type = Type.withRestrict();
681118932a0f2a94a7813ec461d1118c39ecf8aa936fTed Kremenek      break;
681286df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    }
681386df27bbdbb98c39ec2184695c0561209f91beddChris Lattner  }
6814393bd8e185692a451b2ba16bdfc9e7d3543b4217Chris Lattner
681514e0e7436cf6650a72052baea1f8ebe644cef489Chris Lattner  assert((!RequiresICE || Type->isIntegralOrEnumerationType()) &&
6816393bd8e185692a451b2ba16bdfc9e7d3543b4217Chris Lattner         "Integer constant 'I' type must be an integer");
68171eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
681886df27bbdbb98c39ec2184695c0561209f91beddChris Lattner  return Type;
681986df27bbdbb98c39ec2184695c0561209f91beddChris Lattner}
682086df27bbdbb98c39ec2184695c0561209f91beddChris Lattner
682186df27bbdbb98c39ec2184695c0561209f91beddChris Lattner/// GetBuiltinType - Return the type for the specified builtin.
682233daae66462e8f51ee153463b32bdefd60c801aaChris LattnerQualType ASTContext::GetBuiltinType(unsigned Id,
682314e0e7436cf6650a72052baea1f8ebe644cef489Chris Lattner                                    GetBuiltinTypeError &Error,
68244ba2a17694148e16eaa8d3917f657ffcd3667be4Jay Foad                                    unsigned *IntegerConstantArgs) const {
682533daae66462e8f51ee153463b32bdefd60c801aaChris Lattner  const char *TypeStr = BuiltinInfo.GetTypeString(Id);
68261eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
68275f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner  SmallVector<QualType, 8> ArgTypes;
68281eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
682914e0e7436cf6650a72052baea1f8ebe644cef489Chris Lattner  bool RequiresICE = false;
683086df27bbdbb98c39ec2184695c0561209f91beddChris Lattner  Error = GE_None;
683114e0e7436cf6650a72052baea1f8ebe644cef489Chris Lattner  QualType ResType = DecodeTypeFromStr(TypeStr, *this, Error,
683214e0e7436cf6650a72052baea1f8ebe644cef489Chris Lattner                                       RequiresICE, true);
683386df27bbdbb98c39ec2184695c0561209f91beddChris Lattner  if (Error != GE_None)
683486df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    return QualType();
683514e0e7436cf6650a72052baea1f8ebe644cef489Chris Lattner
683614e0e7436cf6650a72052baea1f8ebe644cef489Chris Lattner  assert(!RequiresICE && "Result of intrinsic cannot be required to be an ICE");
683714e0e7436cf6650a72052baea1f8ebe644cef489Chris Lattner
683886df27bbdbb98c39ec2184695c0561209f91beddChris Lattner  while (TypeStr[0] && TypeStr[0] != '.') {
683914e0e7436cf6650a72052baea1f8ebe644cef489Chris Lattner    QualType Ty = DecodeTypeFromStr(TypeStr, *this, Error, RequiresICE, true);
684086df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    if (Error != GE_None)
684186df27bbdbb98c39ec2184695c0561209f91beddChris Lattner      return QualType();
684286df27bbdbb98c39ec2184695c0561209f91beddChris Lattner
684314e0e7436cf6650a72052baea1f8ebe644cef489Chris Lattner    // If this argument is required to be an IntegerConstantExpression and the
684414e0e7436cf6650a72052baea1f8ebe644cef489Chris Lattner    // caller cares, fill in the bitmask we return.
684514e0e7436cf6650a72052baea1f8ebe644cef489Chris Lattner    if (RequiresICE && IntegerConstantArgs)
684614e0e7436cf6650a72052baea1f8ebe644cef489Chris Lattner      *IntegerConstantArgs |= 1 << ArgTypes.size();
684714e0e7436cf6650a72052baea1f8ebe644cef489Chris Lattner
684886df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    // Do array -> pointer decay.  The builtin should use the decayed type.
684986df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    if (Ty->isArrayType())
685086df27bbdbb98c39ec2184695c0561209f91beddChris Lattner      Ty = getArrayDecayedType(Ty);
68511eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
685286df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    ArgTypes.push_back(Ty);
685386df27bbdbb98c39ec2184695c0561209f91beddChris Lattner  }
685486df27bbdbb98c39ec2184695c0561209f91beddChris Lattner
685586df27bbdbb98c39ec2184695c0561209f91beddChris Lattner  assert((TypeStr[0] != '.' || TypeStr[1] == 0) &&
685686df27bbdbb98c39ec2184695c0561209f91beddChris Lattner         "'.' should only occur at end of builtin type list!");
685786df27bbdbb98c39ec2184695c0561209f91beddChris Lattner
685800ccbefcffeb88ea3e2e6323e594fa968753ad14John McCall  FunctionType::ExtInfo EI;
685900ccbefcffeb88ea3e2e6323e594fa968753ad14John McCall  if (BuiltinInfo.isNoReturn(Id)) EI = EI.withNoReturn(true);
686000ccbefcffeb88ea3e2e6323e594fa968753ad14John McCall
686100ccbefcffeb88ea3e2e6323e594fa968753ad14John McCall  bool Variadic = (TypeStr[0] == '.');
686200ccbefcffeb88ea3e2e6323e594fa968753ad14John McCall
686300ccbefcffeb88ea3e2e6323e594fa968753ad14John McCall  // We really shouldn't be making a no-proto type here, especially in C++.
686400ccbefcffeb88ea3e2e6323e594fa968753ad14John McCall  if (ArgTypes.empty() && Variadic)
686500ccbefcffeb88ea3e2e6323e594fa968753ad14John McCall    return getFunctionNoProtoType(ResType, EI);
6866ce056bcaa1c97b89a4b2de2112c62d060863be2bDouglas Gregor
6867e23cf437fe76b1ed02d63c3f61b456fd48a915f5John McCall  FunctionProtoType::ExtProtoInfo EPI;
686800ccbefcffeb88ea3e2e6323e594fa968753ad14John McCall  EPI.ExtInfo = EI;
686900ccbefcffeb88ea3e2e6323e594fa968753ad14John McCall  EPI.Variadic = Variadic;
6870e23cf437fe76b1ed02d63c3f61b456fd48a915f5John McCall
6871e23cf437fe76b1ed02d63c3f61b456fd48a915f5John McCall  return getFunctionType(ResType, ArgTypes.data(), ArgTypes.size(), EPI);
687286df27bbdbb98c39ec2184695c0561209f91beddChris Lattner}
6873a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman
687490e99a84ddd020e8fda79643748243725a2ed071Argyrios KyrtzidisGVALinkage ASTContext::GetGVALinkageForFunction(const FunctionDecl *FD) {
687590e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis  GVALinkage External = GVA_StrongExternal;
687690e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis
687790e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis  Linkage L = FD->getLinkage();
687890e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis  switch (L) {
687990e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis  case NoLinkage:
688090e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis  case InternalLinkage:
688190e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis  case UniqueExternalLinkage:
688290e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis    return GVA_Internal;
688390e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis
688490e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis  case ExternalLinkage:
688590e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis    switch (FD->getTemplateSpecializationKind()) {
688690e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis    case TSK_Undeclared:
688790e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis    case TSK_ExplicitSpecialization:
688890e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis      External = GVA_StrongExternal;
688990e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis      break;
689090e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis
689190e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis    case TSK_ExplicitInstantiationDefinition:
689290e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis      return GVA_ExplicitTemplateInstantiation;
689390e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis
689490e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis    case TSK_ExplicitInstantiationDeclaration:
689590e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis    case TSK_ImplicitInstantiation:
689690e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis      External = GVA_TemplateInstantiation;
689790e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis      break;
689890e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis    }
689990e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis  }
690090e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis
690190e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis  if (!FD->isInlined())
690290e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis    return External;
690390e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis
69044e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie  if (!getLangOpts().CPlusPlus || FD->hasAttr<GNUInlineAttr>()) {
690590e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis    // GNU or C99 inline semantics. Determine whether this symbol should be
690690e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis    // externally visible.
690790e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis    if (FD->isInlineDefinitionExternallyVisible())
690890e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis      return External;
690990e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis
691090e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis    // C99 inline semantics, where the symbol is not externally visible.
691190e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis    return GVA_C99Inline;
691290e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis  }
691390e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis
691490e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis  // C++0x [temp.explicit]p9:
691590e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis  //   [ Note: The intent is that an inline function that is the subject of
691690e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis  //   an explicit instantiation declaration will still be implicitly
691790e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis  //   instantiated when used so that the body can be considered for
691890e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis  //   inlining, but that no out-of-line copy of the inline function would be
691990e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis  //   generated in the translation unit. -- end note ]
692090e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis  if (FD->getTemplateSpecializationKind()
692190e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis                                       == TSK_ExplicitInstantiationDeclaration)
692290e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis    return GVA_C99Inline;
692390e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis
692490e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis  return GVA_CXXInline;
692590e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis}
692690e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis
692790e99a84ddd020e8fda79643748243725a2ed071Argyrios KyrtzidisGVALinkage ASTContext::GetGVALinkageForVariable(const VarDecl *VD) {
692890e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis  // If this is a static data member, compute the kind of template
692990e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis  // specialization. Otherwise, this variable is not part of a
693090e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis  // template.
693190e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis  TemplateSpecializationKind TSK = TSK_Undeclared;
693290e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis  if (VD->isStaticDataMember())
693390e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis    TSK = VD->getTemplateSpecializationKind();
693490e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis
693590e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis  Linkage L = VD->getLinkage();
69364e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie  if (L == ExternalLinkage && getLangOpts().CPlusPlus &&
693790e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis      VD->getType()->getLinkage() == UniqueExternalLinkage)
693890e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis    L = UniqueExternalLinkage;
693990e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis
694090e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis  switch (L) {
694190e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis  case NoLinkage:
694290e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis  case InternalLinkage:
694390e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis  case UniqueExternalLinkage:
694490e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis    return GVA_Internal;
694590e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis
694690e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis  case ExternalLinkage:
694790e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis    switch (TSK) {
694890e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis    case TSK_Undeclared:
694990e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis    case TSK_ExplicitSpecialization:
695090e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis      return GVA_StrongExternal;
695190e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis
695290e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis    case TSK_ExplicitInstantiationDeclaration:
695390e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis      llvm_unreachable("Variable should not be instantiated");
695490e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis      // Fall through to treat this like any other instantiation.
695590e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis
695690e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis    case TSK_ExplicitInstantiationDefinition:
695790e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis      return GVA_ExplicitTemplateInstantiation;
695890e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis
695990e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis    case TSK_ImplicitInstantiation:
696090e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis      return GVA_TemplateInstantiation;
696190e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis    }
696290e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis  }
696390e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis
69647530c034c0c71a64c5a9173206d9742ae847af8bDavid Blaikie  llvm_unreachable("Invalid Linkage!");
696590e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis}
696690e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis
69674ac7c0bb39696e92fd220118fedc484c09a69870Argyrios Kyrtzidisbool ASTContext::DeclMustBeEmitted(const Decl *D) {
696890e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis  if (const VarDecl *VD = dyn_cast<VarDecl>(D)) {
696990e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis    if (!VD->isFileVarDecl())
697090e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis      return false;
6971144b38a7995cbe0928e34fbcc865bb2d2be4f7a3Argyrios Kyrtzidis  } else if (!isa<FunctionDecl>(D))
697290e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis    return false;
697390e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis
6974ab411c8c2efed8f2403bf8596e780c0f2f905a19Argyrios Kyrtzidis  // Weak references don't produce any output by themselves.
6975ab411c8c2efed8f2403bf8596e780c0f2f905a19Argyrios Kyrtzidis  if (D->hasAttr<WeakRefAttr>())
6976ab411c8c2efed8f2403bf8596e780c0f2f905a19Argyrios Kyrtzidis    return false;
6977ab411c8c2efed8f2403bf8596e780c0f2f905a19Argyrios Kyrtzidis
697890e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis  // Aliases and used decls are required.
697990e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis  if (D->hasAttr<AliasAttr>() || D->hasAttr<UsedAttr>())
698090e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis    return true;
698190e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis
698290e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis  if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
698390e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis    // Forward declarations aren't required.
698410620eb5164e31208fcbf0437cd79ae535ed0559Sean Hunt    if (!FD->doesThisDeclarationHaveABody())
6985dce67a70a86db8758c926a76fdd980f5369d5746Nick Lewycky      return FD->doesDeclarationForceExternallyVisibleDefinition();
698690e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis
698790e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis    // Constructors and destructors are required.
698890e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis    if (FD->hasAttr<ConstructorAttr>() || FD->hasAttr<DestructorAttr>())
698990e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis      return true;
699090e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis
699190e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis    // The key function for a class is required.
699290e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis    if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(FD)) {
699390e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis      const CXXRecordDecl *RD = MD->getParent();
699490e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis      if (MD->isOutOfLine() && RD->isDynamicClass()) {
699590e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis        const CXXMethodDecl *KeyFunc = getKeyFunction(RD);
699690e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis        if (KeyFunc && KeyFunc->getCanonicalDecl() == MD->getCanonicalDecl())
699790e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis          return true;
699890e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis      }
699990e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis    }
700090e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis
700190e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis    GVALinkage Linkage = GetGVALinkageForFunction(FD);
700290e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis
700390e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis    // static, static inline, always_inline, and extern inline functions can
700490e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis    // always be deferred.  Normal inline functions can be deferred in C99/C++.
700590e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis    // Implicit template instantiations can also be deferred in C++.
700690e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis    if (Linkage == GVA_Internal  || Linkage == GVA_C99Inline ||
70073a5aca8112e03814f78906dc5d032287adb272b5Anton Yartsev        Linkage == GVA_CXXInline || Linkage == GVA_TemplateInstantiation)
700890e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis      return false;
700990e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis    return true;
701090e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis  }
701194da1587f7d584fc61df793229d197969f204cd9Douglas Gregor
701290e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis  const VarDecl *VD = cast<VarDecl>(D);
701390e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis  assert(VD->isFileVarDecl() && "Expected file scoped var");
701490e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis
7015ab411c8c2efed8f2403bf8596e780c0f2f905a19Argyrios Kyrtzidis  if (VD->isThisDeclarationADefinition() == VarDecl::DeclarationOnly)
7016ab411c8c2efed8f2403bf8596e780c0f2f905a19Argyrios Kyrtzidis    return false;
7017ab411c8c2efed8f2403bf8596e780c0f2f905a19Argyrios Kyrtzidis
701890e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis  // Structs that have non-trivial constructors or destructors are required.
701990e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis
702090e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis  // FIXME: Handle references.
7021023df37c27ee8035664fb62f206ca58f4e2a169dSean Hunt  // FIXME: Be more selective about which constructors we care about.
702290e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis  if (const RecordType *RT = VD->getType()->getAs<RecordType>()) {
702390e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis    if (const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(RT->getDecl())) {
7024023df37c27ee8035664fb62f206ca58f4e2a169dSean Hunt      if (RD->hasDefinition() && !(RD->hasTrivialDefaultConstructor() &&
7025023df37c27ee8035664fb62f206ca58f4e2a169dSean Hunt                                   RD->hasTrivialCopyConstructor() &&
7026023df37c27ee8035664fb62f206ca58f4e2a169dSean Hunt                                   RD->hasTrivialMoveConstructor() &&
7027023df37c27ee8035664fb62f206ca58f4e2a169dSean Hunt                                   RD->hasTrivialDestructor()))
702890e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis        return true;
702990e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis    }
703090e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis  }
703190e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis
703290e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis  GVALinkage L = GetGVALinkageForVariable(VD);
703390e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis  if (L == GVA_Internal || L == GVA_TemplateInstantiation) {
703490e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis    if (!(VD->getInit() && VD->getInit()->HasSideEffects(*this)))
703590e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis      return false;
703690e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis  }
703790e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis
703890e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis  return true;
703990e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis}
7040071cc7deffad608165b1ddd5263e8bf181861520Charles Davis
7041ee743f903858e337434ac0335f147f4de4ecae05Charles DavisCallingConv ASTContext::getDefaultMethodCallConv() {
7042ee743f903858e337434ac0335f147f4de4ecae05Charles Davis  // Pass through to the C++ ABI object
7043ee743f903858e337434ac0335f147f4de4ecae05Charles Davis  return ABI->getDefaultMethodCallConv();
7044ee743f903858e337434ac0335f147f4de4ecae05Charles Davis}
7045ee743f903858e337434ac0335f147f4de4ecae05Charles Davis
70464ba2a17694148e16eaa8d3917f657ffcd3667be4Jay Foadbool ASTContext::isNearlyEmpty(const CXXRecordDecl *RD) const {
7047dae0cb52e4e3d46bbfc9a4510909522197a92e54Anders Carlsson  // Pass through to the C++ ABI object
7048dae0cb52e4e3d46bbfc9a4510909522197a92e54Anders Carlsson  return ABI->isNearlyEmpty(RD);
7049dae0cb52e4e3d46bbfc9a4510909522197a92e54Anders Carlsson}
7050dae0cb52e4e3d46bbfc9a4510909522197a92e54Anders Carlsson
705114110477887e3dc168ffc6c191e72d705051f99ePeter CollingbourneMangleContext *ASTContext::createMangleContext() {
7052bcfd1f55bfbb3e5944cd5e03d07b343e280838c4Douglas Gregor  switch (Target->getCXXABI()) {
705314110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne  case CXXABI_ARM:
705414110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne  case CXXABI_Itanium:
705514110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne    return createItaniumMangleContext(*this, getDiagnostics());
705614110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne  case CXXABI_Microsoft:
705714110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne    return createMicrosoftMangleContext(*this, getDiagnostics());
705814110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne  }
7059b219cfc4d75f0a03630b7c4509ef791b7e97b2c8David Blaikie  llvm_unreachable("Unsupported ABI");
706014110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne}
706114110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne
7062071cc7deffad608165b1ddd5263e8bf181861520Charles DavisCXXABI::~CXXABI() {}
7063ba29bd25515fbd99e98ba0fedb9d93617b27609eTed Kremenek
7064ba29bd25515fbd99e98ba0fedb9d93617b27609eTed Kremeneksize_t ASTContext::getSideTableAllocatedMemory() const {
70650c8cd1ac55720276ad41fa7b4f8785bead5afd32Ted Kremenek  return ASTRecordLayouts.getMemorySize()
70660c8cd1ac55720276ad41fa7b4f8785bead5afd32Ted Kremenek    + llvm::capacity_in_bytes(ObjCLayouts)
70670c8cd1ac55720276ad41fa7b4f8785bead5afd32Ted Kremenek    + llvm::capacity_in_bytes(KeyFunctions)
70680c8cd1ac55720276ad41fa7b4f8785bead5afd32Ted Kremenek    + llvm::capacity_in_bytes(ObjCImpls)
70690c8cd1ac55720276ad41fa7b4f8785bead5afd32Ted Kremenek    + llvm::capacity_in_bytes(BlockVarCopyInits)
70700c8cd1ac55720276ad41fa7b4f8785bead5afd32Ted Kremenek    + llvm::capacity_in_bytes(DeclAttrs)
70710c8cd1ac55720276ad41fa7b4f8785bead5afd32Ted Kremenek    + llvm::capacity_in_bytes(InstantiatedFromStaticDataMember)
70720c8cd1ac55720276ad41fa7b4f8785bead5afd32Ted Kremenek    + llvm::capacity_in_bytes(InstantiatedFromUsingDecl)
70730c8cd1ac55720276ad41fa7b4f8785bead5afd32Ted Kremenek    + llvm::capacity_in_bytes(InstantiatedFromUsingShadowDecl)
70740c8cd1ac55720276ad41fa7b4f8785bead5afd32Ted Kremenek    + llvm::capacity_in_bytes(InstantiatedFromUnnamedFieldDecl)
70750c8cd1ac55720276ad41fa7b4f8785bead5afd32Ted Kremenek    + llvm::capacity_in_bytes(OverriddenMethods)
70760c8cd1ac55720276ad41fa7b4f8785bead5afd32Ted Kremenek    + llvm::capacity_in_bytes(Types)
7077af0f4d0b2e38c810effc8b024ad2fb6604eec5d3Francois Pichet    + llvm::capacity_in_bytes(VariableArrayTypes)
70780d95f0d7b81110f77e99e833f766d19be7b7e072Francois Pichet    + llvm::capacity_in_bytes(ClassScopeSpecializationPattern);
7079ba29bd25515fbd99e98ba0fedb9d93617b27609eTed Kremenek}
7080d211cb709510fbe7e75167b9feee0050851d001aTed Kremenek
70819e8c92a9c9b949bbb0408fbbd9a58e34894b6efcDouglas Gregorunsigned ASTContext::getLambdaManglingNumber(CXXMethodDecl *CallOperator) {
70829e8c92a9c9b949bbb0408fbbd9a58e34894b6efcDouglas Gregor  CXXRecordDecl *Lambda = CallOperator->getParent();
70839e8c92a9c9b949bbb0408fbbd9a58e34894b6efcDouglas Gregor  return LambdaMangleContexts[Lambda->getDeclContext()]
70849e8c92a9c9b949bbb0408fbbd9a58e34894b6efcDouglas Gregor           .getManglingNumber(CallOperator);
70859e8c92a9c9b949bbb0408fbbd9a58e34894b6efcDouglas Gregor}
70869e8c92a9c9b949bbb0408fbbd9a58e34894b6efcDouglas Gregor
70879e8c92a9c9b949bbb0408fbbd9a58e34894b6efcDouglas Gregor
7088d211cb709510fbe7e75167b9feee0050851d001aTed Kremenekvoid ASTContext::setParameterIndex(const ParmVarDecl *D, unsigned int index) {
7089d211cb709510fbe7e75167b9feee0050851d001aTed Kremenek  ParamIndices[D] = index;
7090d211cb709510fbe7e75167b9feee0050851d001aTed Kremenek}
7091d211cb709510fbe7e75167b9feee0050851d001aTed Kremenek
7092d211cb709510fbe7e75167b9feee0050851d001aTed Kremenekunsigned ASTContext::getParameterIndex(const ParmVarDecl *D) const {
7093d211cb709510fbe7e75167b9feee0050851d001aTed Kremenek  ParameterIndexTable::const_iterator I = ParamIndices.find(D);
7094d211cb709510fbe7e75167b9feee0050851d001aTed Kremenek  assert(I != ParamIndices.end() &&
7095d211cb709510fbe7e75167b9feee0050851d001aTed Kremenek         "ParmIndices lacks entry set by ParmVarDecl");
7096d211cb709510fbe7e75167b9feee0050851d001aTed Kremenek  return I->second;
7097d211cb709510fbe7e75167b9feee0050851d001aTed Kremenek}
7098