ASTContext.cpp revision 0d729105ecb50a7e3cbe6e57c29149edfa5cf05a
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"
16aa58081902ad31927df02e8537d972eabe29d6dfDmitri Gribenko#include "clang/AST/CommentCommandTraits.h"
1749aa7ff1245abd03e6e998e01302df31e4c6f8f6Argyrios Kyrtzidis#include "clang/AST/DeclCXX.h"
18980e508ca70d6de75d2abfd96b4681fc98bb2698Steve Naroff#include "clang/AST/DeclObjC.h"
19aaba5e346dffdbad5d1c42765a89e4a7afb0da67Douglas Gregor#include "clang/AST/DeclTemplate.h"
20b17166c8077cd900cca83a895c43b30ea6660598Argyrios Kyrtzidis#include "clang/AST/TypeLoc.h"
21e91593ef084479340582b2ba177b44be50a717b7Daniel Dunbar#include "clang/AST/Expr.h"
22ea1471e0e967548c596a71469702f8846dbaf3c0John McCall#include "clang/AST/ExprCXX.h"
232cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor#include "clang/AST/ExternalASTSource.h"
247b90340c9c7d07aef4e301e72b5e8a30d5f4f0c8Argyrios Kyrtzidis#include "clang/AST/ASTMutationListener.h"
2519cc4abea06a9b49e0e16a50d335c064cd723572Anders Carlsson#include "clang/AST/RecordLayout.h"
2614110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne#include "clang/AST/Mangle.h"
271b63e4f732dbc73d90abf886b4d21f8e3a165f6dChris Lattner#include "clang/Basic/Builtins.h"
28a9376d470ccb0eac74fe09a6b2a18a890f1d17c4Chris Lattner#include "clang/Basic/SourceManager.h"
295f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer#include "clang/Basic/TargetInfo.h"
30f5942a44880be26878592eb052b737579349411eBenjamin Kramer#include "llvm/ADT/SmallString.h"
3185f9bceab1542aafff012d4d28e998f4ba16e362Anders Carlsson#include "llvm/ADT/StringExtras.h"
326fe7c8aa8c7546743ecd0ac0138c2cf5d8155386Nate Begeman#include "llvm/Support/MathExtras.h"
33f5942a44880be26878592eb052b737579349411eBenjamin Kramer#include "llvm/Support/raw_ostream.h"
340c8cd1ac55720276ad41fa7b4f8785bead5afd32Ted Kremenek#include "llvm/Support/Capacity.h"
35071cc7deffad608165b1ddd5263e8bf181861520Charles Davis#include "CXXABI.h"
362636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis#include <map>
3729445a0542d128cd7ee587ee52229670b9b54a12Anders Carlsson
385f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencerusing namespace clang;
395f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
401827403a7138946305c0058f262e02b595cf882fDouglas Gregorunsigned ASTContext::NumImplicitDefaultConstructors;
411827403a7138946305c0058f262e02b595cf882fDouglas Gregorunsigned ASTContext::NumImplicitDefaultConstructorsDeclared;
42225843186e3972ce798d3de00f86da9008b15a0eDouglas Gregorunsigned ASTContext::NumImplicitCopyConstructors;
43225843186e3972ce798d3de00f86da9008b15a0eDouglas Gregorunsigned ASTContext::NumImplicitCopyConstructorsDeclared;
44ffe37fdda5b4b4f162a45155c30d9f60ce110c12Sean Huntunsigned ASTContext::NumImplicitMoveConstructors;
45ffe37fdda5b4b4f162a45155c30d9f60ce110c12Sean Huntunsigned ASTContext::NumImplicitMoveConstructorsDeclared;
46a376d10acfacf19d6dfa41069f7929739a18dd7aDouglas Gregorunsigned ASTContext::NumImplicitCopyAssignmentOperators;
47a376d10acfacf19d6dfa41069f7929739a18dd7aDouglas Gregorunsigned ASTContext::NumImplicitCopyAssignmentOperatorsDeclared;
48ffe37fdda5b4b4f162a45155c30d9f60ce110c12Sean Huntunsigned ASTContext::NumImplicitMoveAssignmentOperators;
49ffe37fdda5b4b4f162a45155c30d9f60ce110c12Sean Huntunsigned ASTContext::NumImplicitMoveAssignmentOperatorsDeclared;
504923aa25eb39d64106a5817c02d560a3aecf8b2cDouglas Gregorunsigned ASTContext::NumImplicitDestructors;
514923aa25eb39d64106a5817c02d560a3aecf8b2cDouglas Gregorunsigned ASTContext::NumImplicitDestructorsDeclared;
524923aa25eb39d64106a5817c02d560a3aecf8b2cDouglas Gregor
535f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencerenum FloatingRank {
54aa4a99b4a62615db243f7a5c433169f2fc704420Anton Korobeynikov  HalfRank, FloatRank, DoubleRank, LongDoubleRank
555f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer};
565f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
57a5ef44ff5d93a3be6ca67782828157a71894cf0cDmitri GribenkoRawComment *ASTContext::getRawCommentForDeclNoCache(const Decl *D) const {
58aa0cd85838f2a024e589ea4e8c2094130065af21Dmitri Gribenko  if (!CommentsLoaded && ExternalSource) {
59aa0cd85838f2a024e589ea4e8c2094130065af21Dmitri Gribenko    ExternalSource->ReadComments();
60aa0cd85838f2a024e589ea4e8c2094130065af21Dmitri Gribenko    CommentsLoaded = true;
61aa0cd85838f2a024e589ea4e8c2094130065af21Dmitri Gribenko  }
62aa0cd85838f2a024e589ea4e8c2094130065af21Dmitri Gribenko
63aa0cd85838f2a024e589ea4e8c2094130065af21Dmitri Gribenko  assert(D);
64aa0cd85838f2a024e589ea4e8c2094130065af21Dmitri Gribenko
65c3fee3539fd00a6ce21dc1f574baf76686640072Dmitri Gribenko  // User can not attach documentation to implicit declarations.
66c3fee3539fd00a6ce21dc1f574baf76686640072Dmitri Gribenko  if (D->isImplicit())
67c3fee3539fd00a6ce21dc1f574baf76686640072Dmitri Gribenko    return NULL;
68c3fee3539fd00a6ce21dc1f574baf76686640072Dmitri Gribenko
69aa0cd85838f2a024e589ea4e8c2094130065af21Dmitri Gribenko  // TODO: handle comments for function parameters properly.
70aa0cd85838f2a024e589ea4e8c2094130065af21Dmitri Gribenko  if (isa<ParmVarDecl>(D))
71aa0cd85838f2a024e589ea4e8c2094130065af21Dmitri Gribenko    return NULL;
72aa0cd85838f2a024e589ea4e8c2094130065af21Dmitri Gribenko
7396b098674908eaa59a9128f3305cda6fbbdad563Dmitri Gribenko  // TODO: we could look up template parameter documentation in the template
7496b098674908eaa59a9128f3305cda6fbbdad563Dmitri Gribenko  // documentation.
7596b098674908eaa59a9128f3305cda6fbbdad563Dmitri Gribenko  if (isa<TemplateTypeParmDecl>(D) ||
7696b098674908eaa59a9128f3305cda6fbbdad563Dmitri Gribenko      isa<NonTypeTemplateParmDecl>(D) ||
7796b098674908eaa59a9128f3305cda6fbbdad563Dmitri Gribenko      isa<TemplateTemplateParmDecl>(D))
7896b098674908eaa59a9128f3305cda6fbbdad563Dmitri Gribenko    return NULL;
7996b098674908eaa59a9128f3305cda6fbbdad563Dmitri Gribenko
80811c820257746b1799b790b6adc7804f44154011Dmitri Gribenko  ArrayRef<RawComment *> RawComments = Comments.getComments();
81aa0cd85838f2a024e589ea4e8c2094130065af21Dmitri Gribenko
82aa0cd85838f2a024e589ea4e8c2094130065af21Dmitri Gribenko  // If there are no comments anywhere, we won't find anything.
83aa0cd85838f2a024e589ea4e8c2094130065af21Dmitri Gribenko  if (RawComments.empty())
84aa0cd85838f2a024e589ea4e8c2094130065af21Dmitri Gribenko    return NULL;
85aa0cd85838f2a024e589ea4e8c2094130065af21Dmitri Gribenko
86abd56c816e9164b17bb3e7154a511b0c9896ffdbDmitri Gribenko  // Find declaration location.
87abd56c816e9164b17bb3e7154a511b0c9896ffdbDmitri Gribenko  // For Objective-C declarations we generally don't expect to have multiple
88abd56c816e9164b17bb3e7154a511b0c9896ffdbDmitri Gribenko  // declarators, thus use declaration starting location as the "declaration
89abd56c816e9164b17bb3e7154a511b0c9896ffdbDmitri Gribenko  // location".
90abd56c816e9164b17bb3e7154a511b0c9896ffdbDmitri Gribenko  // For all other declarations multiple declarators are used quite frequently,
91abd56c816e9164b17bb3e7154a511b0c9896ffdbDmitri Gribenko  // so we use the location of the identifier as the "declaration location".
92abd56c816e9164b17bb3e7154a511b0c9896ffdbDmitri Gribenko  SourceLocation DeclLoc;
93abd56c816e9164b17bb3e7154a511b0c9896ffdbDmitri Gribenko  if (isa<ObjCMethodDecl>(D) || isa<ObjCContainerDecl>(D) ||
9496b098674908eaa59a9128f3305cda6fbbdad563Dmitri Gribenko      isa<ObjCPropertyDecl>(D) ||
95c27bc80a98b9558513b50956c930eedc9e461ae0Dmitri Gribenko      isa<RedeclarableTemplateDecl>(D) ||
96c27bc80a98b9558513b50956c930eedc9e461ae0Dmitri Gribenko      isa<ClassTemplateSpecializationDecl>(D))
97abd56c816e9164b17bb3e7154a511b0c9896ffdbDmitri Gribenko    DeclLoc = D->getLocStart();
98abd56c816e9164b17bb3e7154a511b0c9896ffdbDmitri Gribenko  else
99abd56c816e9164b17bb3e7154a511b0c9896ffdbDmitri Gribenko    DeclLoc = D->getLocation();
100abd56c816e9164b17bb3e7154a511b0c9896ffdbDmitri Gribenko
101aa0cd85838f2a024e589ea4e8c2094130065af21Dmitri Gribenko  // If the declaration doesn't map directly to a location in a file, we
102aa0cd85838f2a024e589ea4e8c2094130065af21Dmitri Gribenko  // can't find the comment.
103aa0cd85838f2a024e589ea4e8c2094130065af21Dmitri Gribenko  if (DeclLoc.isInvalid() || !DeclLoc.isFileID())
104aa0cd85838f2a024e589ea4e8c2094130065af21Dmitri Gribenko    return NULL;
105aa0cd85838f2a024e589ea4e8c2094130065af21Dmitri Gribenko
106aa0cd85838f2a024e589ea4e8c2094130065af21Dmitri Gribenko  // Find the comment that occurs just after this declaration.
107a444f1856459130bd3a1bb8995331c9e367db04fDmitri Gribenko  ArrayRef<RawComment *>::iterator Comment;
108a444f1856459130bd3a1bb8995331c9e367db04fDmitri Gribenko  {
109a444f1856459130bd3a1bb8995331c9e367db04fDmitri Gribenko    // When searching for comments during parsing, the comment we are looking
110a444f1856459130bd3a1bb8995331c9e367db04fDmitri Gribenko    // for is usually among the last two comments we parsed -- check them
111a444f1856459130bd3a1bb8995331c9e367db04fDmitri Gribenko    // first.
112a444f1856459130bd3a1bb8995331c9e367db04fDmitri Gribenko    RawComment CommentAtDeclLoc(SourceMgr, SourceRange(DeclLoc));
113a444f1856459130bd3a1bb8995331c9e367db04fDmitri Gribenko    BeforeThanCompare<RawComment> Compare(SourceMgr);
114a444f1856459130bd3a1bb8995331c9e367db04fDmitri Gribenko    ArrayRef<RawComment *>::iterator MaybeBeforeDecl = RawComments.end() - 1;
115a444f1856459130bd3a1bb8995331c9e367db04fDmitri Gribenko    bool Found = Compare(*MaybeBeforeDecl, &CommentAtDeclLoc);
116a444f1856459130bd3a1bb8995331c9e367db04fDmitri Gribenko    if (!Found && RawComments.size() >= 2) {
117a444f1856459130bd3a1bb8995331c9e367db04fDmitri Gribenko      MaybeBeforeDecl--;
118a444f1856459130bd3a1bb8995331c9e367db04fDmitri Gribenko      Found = Compare(*MaybeBeforeDecl, &CommentAtDeclLoc);
119a444f1856459130bd3a1bb8995331c9e367db04fDmitri Gribenko    }
120a444f1856459130bd3a1bb8995331c9e367db04fDmitri Gribenko
121a444f1856459130bd3a1bb8995331c9e367db04fDmitri Gribenko    if (Found) {
122a444f1856459130bd3a1bb8995331c9e367db04fDmitri Gribenko      Comment = MaybeBeforeDecl + 1;
123a444f1856459130bd3a1bb8995331c9e367db04fDmitri Gribenko      assert(Comment == std::lower_bound(RawComments.begin(), RawComments.end(),
124a444f1856459130bd3a1bb8995331c9e367db04fDmitri Gribenko                                         &CommentAtDeclLoc, Compare));
125a444f1856459130bd3a1bb8995331c9e367db04fDmitri Gribenko    } else {
126a444f1856459130bd3a1bb8995331c9e367db04fDmitri Gribenko      // Slow path.
127a444f1856459130bd3a1bb8995331c9e367db04fDmitri Gribenko      Comment = std::lower_bound(RawComments.begin(), RawComments.end(),
128a444f1856459130bd3a1bb8995331c9e367db04fDmitri Gribenko                                 &CommentAtDeclLoc, Compare);
129a444f1856459130bd3a1bb8995331c9e367db04fDmitri Gribenko    }
130a444f1856459130bd3a1bb8995331c9e367db04fDmitri Gribenko  }
131aa0cd85838f2a024e589ea4e8c2094130065af21Dmitri Gribenko
132aa0cd85838f2a024e589ea4e8c2094130065af21Dmitri Gribenko  // Decompose the location for the declaration and find the beginning of the
133aa0cd85838f2a024e589ea4e8c2094130065af21Dmitri Gribenko  // file buffer.
134aa0cd85838f2a024e589ea4e8c2094130065af21Dmitri Gribenko  std::pair<FileID, unsigned> DeclLocDecomp = SourceMgr.getDecomposedLoc(DeclLoc);
135aa0cd85838f2a024e589ea4e8c2094130065af21Dmitri Gribenko
136aa0cd85838f2a024e589ea4e8c2094130065af21Dmitri Gribenko  // First check whether we have a trailing comment.
137aa0cd85838f2a024e589ea4e8c2094130065af21Dmitri Gribenko  if (Comment != RawComments.end() &&
138811c820257746b1799b790b6adc7804f44154011Dmitri Gribenko      (*Comment)->isDocumentation() && (*Comment)->isTrailingComment() &&
1399c00676f2393335dc60c61faf944d4f8f622fac6Dmitri Gribenko      (isa<FieldDecl>(D) || isa<EnumConstantDecl>(D) || isa<VarDecl>(D))) {
140aa0cd85838f2a024e589ea4e8c2094130065af21Dmitri Gribenko    std::pair<FileID, unsigned> CommentBeginDecomp
141811c820257746b1799b790b6adc7804f44154011Dmitri Gribenko      = SourceMgr.getDecomposedLoc((*Comment)->getSourceRange().getBegin());
142aa0cd85838f2a024e589ea4e8c2094130065af21Dmitri Gribenko    // Check that Doxygen trailing comment comes after the declaration, starts
143aa0cd85838f2a024e589ea4e8c2094130065af21Dmitri Gribenko    // on the same line and in the same file as the declaration.
144aa0cd85838f2a024e589ea4e8c2094130065af21Dmitri Gribenko    if (DeclLocDecomp.first == CommentBeginDecomp.first &&
145aa0cd85838f2a024e589ea4e8c2094130065af21Dmitri Gribenko        SourceMgr.getLineNumber(DeclLocDecomp.first, DeclLocDecomp.second)
146aa0cd85838f2a024e589ea4e8c2094130065af21Dmitri Gribenko          == SourceMgr.getLineNumber(CommentBeginDecomp.first,
147aa0cd85838f2a024e589ea4e8c2094130065af21Dmitri Gribenko                                     CommentBeginDecomp.second)) {
148f50555eedef33fd5a67d369aa0ae8a6f1d201543Dmitri Gribenko      (*Comment)->setDecl(D);
149811c820257746b1799b790b6adc7804f44154011Dmitri Gribenko      return *Comment;
150aa0cd85838f2a024e589ea4e8c2094130065af21Dmitri Gribenko    }
151aa0cd85838f2a024e589ea4e8c2094130065af21Dmitri Gribenko  }
152aa0cd85838f2a024e589ea4e8c2094130065af21Dmitri Gribenko
153aa0cd85838f2a024e589ea4e8c2094130065af21Dmitri Gribenko  // The comment just after the declaration was not a trailing comment.
154aa0cd85838f2a024e589ea4e8c2094130065af21Dmitri Gribenko  // Let's look at the previous comment.
155aa0cd85838f2a024e589ea4e8c2094130065af21Dmitri Gribenko  if (Comment == RawComments.begin())
156aa0cd85838f2a024e589ea4e8c2094130065af21Dmitri Gribenko    return NULL;
157aa0cd85838f2a024e589ea4e8c2094130065af21Dmitri Gribenko  --Comment;
158aa0cd85838f2a024e589ea4e8c2094130065af21Dmitri Gribenko
159aa0cd85838f2a024e589ea4e8c2094130065af21Dmitri Gribenko  // Check that we actually have a non-member Doxygen comment.
160811c820257746b1799b790b6adc7804f44154011Dmitri Gribenko  if (!(*Comment)->isDocumentation() || (*Comment)->isTrailingComment())
161aa0cd85838f2a024e589ea4e8c2094130065af21Dmitri Gribenko    return NULL;
162aa0cd85838f2a024e589ea4e8c2094130065af21Dmitri Gribenko
163aa0cd85838f2a024e589ea4e8c2094130065af21Dmitri Gribenko  // Decompose the end of the comment.
164aa0cd85838f2a024e589ea4e8c2094130065af21Dmitri Gribenko  std::pair<FileID, unsigned> CommentEndDecomp
165811c820257746b1799b790b6adc7804f44154011Dmitri Gribenko    = SourceMgr.getDecomposedLoc((*Comment)->getSourceRange().getEnd());
166aa0cd85838f2a024e589ea4e8c2094130065af21Dmitri Gribenko
167aa0cd85838f2a024e589ea4e8c2094130065af21Dmitri Gribenko  // If the comment and the declaration aren't in the same file, then they
168aa0cd85838f2a024e589ea4e8c2094130065af21Dmitri Gribenko  // aren't related.
169aa0cd85838f2a024e589ea4e8c2094130065af21Dmitri Gribenko  if (DeclLocDecomp.first != CommentEndDecomp.first)
170aa0cd85838f2a024e589ea4e8c2094130065af21Dmitri Gribenko    return NULL;
171aa0cd85838f2a024e589ea4e8c2094130065af21Dmitri Gribenko
172aa0cd85838f2a024e589ea4e8c2094130065af21Dmitri Gribenko  // Get the corresponding buffer.
173aa0cd85838f2a024e589ea4e8c2094130065af21Dmitri Gribenko  bool Invalid = false;
174aa0cd85838f2a024e589ea4e8c2094130065af21Dmitri Gribenko  const char *Buffer = SourceMgr.getBufferData(DeclLocDecomp.first,
175aa0cd85838f2a024e589ea4e8c2094130065af21Dmitri Gribenko                                               &Invalid).data();
176aa0cd85838f2a024e589ea4e8c2094130065af21Dmitri Gribenko  if (Invalid)
177aa0cd85838f2a024e589ea4e8c2094130065af21Dmitri Gribenko    return NULL;
178aa0cd85838f2a024e589ea4e8c2094130065af21Dmitri Gribenko
179aa0cd85838f2a024e589ea4e8c2094130065af21Dmitri Gribenko  // Extract text between the comment and declaration.
180aa0cd85838f2a024e589ea4e8c2094130065af21Dmitri Gribenko  StringRef Text(Buffer + CommentEndDecomp.second,
181aa0cd85838f2a024e589ea4e8c2094130065af21Dmitri Gribenko                 DeclLocDecomp.second - CommentEndDecomp.second);
182aa0cd85838f2a024e589ea4e8c2094130065af21Dmitri Gribenko
1838bdb58a7835a9a90dd9b9791fccf269cbc1dcef3Dmitri Gribenko  // There should be no other declarations or preprocessor directives between
1848bdb58a7835a9a90dd9b9791fccf269cbc1dcef3Dmitri Gribenko  // comment and declaration.
185abd56c816e9164b17bb3e7154a511b0c9896ffdbDmitri Gribenko  if (Text.find_first_of(",;{}#@") != StringRef::npos)
186aa0cd85838f2a024e589ea4e8c2094130065af21Dmitri Gribenko    return NULL;
187aa0cd85838f2a024e589ea4e8c2094130065af21Dmitri Gribenko
188f50555eedef33fd5a67d369aa0ae8a6f1d201543Dmitri Gribenko   (*Comment)->setDecl(D);
189811c820257746b1799b790b6adc7804f44154011Dmitri Gribenko  return *Comment;
190aa0cd85838f2a024e589ea4e8c2094130065af21Dmitri Gribenko}
191aa0cd85838f2a024e589ea4e8c2094130065af21Dmitri Gribenko
192f50555eedef33fd5a67d369aa0ae8a6f1d201543Dmitri Gribenkoconst RawComment *ASTContext::getRawCommentForAnyRedecl(const Decl *D) const {
193cd81df2dcff4e13eea6edfbfd52a4458d978d174Douglas Gregor  // If we have a 'templated' declaration for a template, adjust 'D' to
194cd81df2dcff4e13eea6edfbfd52a4458d978d174Douglas Gregor  // refer to the actual template.
195cd81df2dcff4e13eea6edfbfd52a4458d978d174Douglas Gregor  if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
196cd81df2dcff4e13eea6edfbfd52a4458d978d174Douglas Gregor    if (const FunctionTemplateDecl *FTD = FD->getDescribedFunctionTemplate())
197cd81df2dcff4e13eea6edfbfd52a4458d978d174Douglas Gregor      D = FTD;
198cd81df2dcff4e13eea6edfbfd52a4458d978d174Douglas Gregor  } else if (const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(D)) {
199cd81df2dcff4e13eea6edfbfd52a4458d978d174Douglas Gregor    if (const ClassTemplateDecl *CTD = RD->getDescribedClassTemplate())
200cd81df2dcff4e13eea6edfbfd52a4458d978d174Douglas Gregor      D = CTD;
201cd81df2dcff4e13eea6edfbfd52a4458d978d174Douglas Gregor  }
202cd81df2dcff4e13eea6edfbfd52a4458d978d174Douglas Gregor  // FIXME: Alias templates?
203cd81df2dcff4e13eea6edfbfd52a4458d978d174Douglas Gregor
204f50555eedef33fd5a67d369aa0ae8a6f1d201543Dmitri Gribenko  // Check whether we have cached a comment for this declaration already.
205f50555eedef33fd5a67d369aa0ae8a6f1d201543Dmitri Gribenko  {
206f50555eedef33fd5a67d369aa0ae8a6f1d201543Dmitri Gribenko    llvm::DenseMap<const Decl *, RawCommentAndCacheFlags>::iterator Pos =
207f50555eedef33fd5a67d369aa0ae8a6f1d201543Dmitri Gribenko        RedeclComments.find(D);
208f50555eedef33fd5a67d369aa0ae8a6f1d201543Dmitri Gribenko    if (Pos != RedeclComments.end()) {
209f50555eedef33fd5a67d369aa0ae8a6f1d201543Dmitri Gribenko      const RawCommentAndCacheFlags &Raw = Pos->second;
210f50555eedef33fd5a67d369aa0ae8a6f1d201543Dmitri Gribenko      if (Raw.getKind() != RawCommentAndCacheFlags::NoCommentInDecl)
211f50555eedef33fd5a67d369aa0ae8a6f1d201543Dmitri Gribenko        return Raw.getRaw();
212f50555eedef33fd5a67d369aa0ae8a6f1d201543Dmitri Gribenko    }
213f50555eedef33fd5a67d369aa0ae8a6f1d201543Dmitri Gribenko  }
214f50555eedef33fd5a67d369aa0ae8a6f1d201543Dmitri Gribenko
215f50555eedef33fd5a67d369aa0ae8a6f1d201543Dmitri Gribenko  // Search for comments attached to declarations in the redeclaration chain.
216f50555eedef33fd5a67d369aa0ae8a6f1d201543Dmitri Gribenko  const RawComment *RC = NULL;
217f50555eedef33fd5a67d369aa0ae8a6f1d201543Dmitri Gribenko  for (Decl::redecl_iterator I = D->redecls_begin(),
218f50555eedef33fd5a67d369aa0ae8a6f1d201543Dmitri Gribenko                             E = D->redecls_end();
219f50555eedef33fd5a67d369aa0ae8a6f1d201543Dmitri Gribenko       I != E; ++I) {
220f50555eedef33fd5a67d369aa0ae8a6f1d201543Dmitri Gribenko    llvm::DenseMap<const Decl *, RawCommentAndCacheFlags>::iterator Pos =
221f50555eedef33fd5a67d369aa0ae8a6f1d201543Dmitri Gribenko        RedeclComments.find(*I);
222f50555eedef33fd5a67d369aa0ae8a6f1d201543Dmitri Gribenko    if (Pos != RedeclComments.end()) {
223f50555eedef33fd5a67d369aa0ae8a6f1d201543Dmitri Gribenko      const RawCommentAndCacheFlags &Raw = Pos->second;
224f50555eedef33fd5a67d369aa0ae8a6f1d201543Dmitri Gribenko      if (Raw.getKind() != RawCommentAndCacheFlags::NoCommentInDecl) {
225f50555eedef33fd5a67d369aa0ae8a6f1d201543Dmitri Gribenko        RC = Raw.getRaw();
226f50555eedef33fd5a67d369aa0ae8a6f1d201543Dmitri Gribenko        break;
227f50555eedef33fd5a67d369aa0ae8a6f1d201543Dmitri Gribenko      }
228f50555eedef33fd5a67d369aa0ae8a6f1d201543Dmitri Gribenko    } else {
229f50555eedef33fd5a67d369aa0ae8a6f1d201543Dmitri Gribenko      RC = getRawCommentForDeclNoCache(*I);
230f50555eedef33fd5a67d369aa0ae8a6f1d201543Dmitri Gribenko      RawCommentAndCacheFlags Raw;
231f50555eedef33fd5a67d369aa0ae8a6f1d201543Dmitri Gribenko      if (RC) {
232f50555eedef33fd5a67d369aa0ae8a6f1d201543Dmitri Gribenko        Raw.setRaw(RC);
233f50555eedef33fd5a67d369aa0ae8a6f1d201543Dmitri Gribenko        Raw.setKind(RawCommentAndCacheFlags::FromDecl);
234f50555eedef33fd5a67d369aa0ae8a6f1d201543Dmitri Gribenko      } else
235f50555eedef33fd5a67d369aa0ae8a6f1d201543Dmitri Gribenko        Raw.setKind(RawCommentAndCacheFlags::NoCommentInDecl);
236f50555eedef33fd5a67d369aa0ae8a6f1d201543Dmitri Gribenko      RedeclComments[*I] = Raw;
237f50555eedef33fd5a67d369aa0ae8a6f1d201543Dmitri Gribenko      if (RC)
238f50555eedef33fd5a67d369aa0ae8a6f1d201543Dmitri Gribenko        break;
239f50555eedef33fd5a67d369aa0ae8a6f1d201543Dmitri Gribenko    }
2408d3ba23f2d9e6c87794d059412a0808c9cbacb25Dmitri Gribenko  }
241aa0cd85838f2a024e589ea4e8c2094130065af21Dmitri Gribenko
2428376f5934a18b950ac7323d8a38ed231623010faDmitri Gribenko  // If we found a comment, it should be a documentation comment.
2438376f5934a18b950ac7323d8a38ed231623010faDmitri Gribenko  assert(!RC || RC->isDocumentation());
244f50555eedef33fd5a67d369aa0ae8a6f1d201543Dmitri Gribenko
245f50555eedef33fd5a67d369aa0ae8a6f1d201543Dmitri Gribenko  // Update cache for every declaration in the redeclaration chain.
246f50555eedef33fd5a67d369aa0ae8a6f1d201543Dmitri Gribenko  RawCommentAndCacheFlags Raw;
247f50555eedef33fd5a67d369aa0ae8a6f1d201543Dmitri Gribenko  Raw.setRaw(RC);
248f50555eedef33fd5a67d369aa0ae8a6f1d201543Dmitri Gribenko  Raw.setKind(RawCommentAndCacheFlags::FromRedecl);
249f50555eedef33fd5a67d369aa0ae8a6f1d201543Dmitri Gribenko
250f50555eedef33fd5a67d369aa0ae8a6f1d201543Dmitri Gribenko  for (Decl::redecl_iterator I = D->redecls_begin(),
251f50555eedef33fd5a67d369aa0ae8a6f1d201543Dmitri Gribenko                             E = D->redecls_end();
252f50555eedef33fd5a67d369aa0ae8a6f1d201543Dmitri Gribenko       I != E; ++I) {
253f50555eedef33fd5a67d369aa0ae8a6f1d201543Dmitri Gribenko    RawCommentAndCacheFlags &R = RedeclComments[*I];
254f50555eedef33fd5a67d369aa0ae8a6f1d201543Dmitri Gribenko    if (R.getKind() == RawCommentAndCacheFlags::NoCommentInDecl)
255f50555eedef33fd5a67d369aa0ae8a6f1d201543Dmitri Gribenko      R = Raw;
256f50555eedef33fd5a67d369aa0ae8a6f1d201543Dmitri Gribenko  }
257f50555eedef33fd5a67d369aa0ae8a6f1d201543Dmitri Gribenko
258aa0cd85838f2a024e589ea4e8c2094130065af21Dmitri Gribenko  return RC;
259aa0cd85838f2a024e589ea4e8c2094130065af21Dmitri Gribenko}
260aa0cd85838f2a024e589ea4e8c2094130065af21Dmitri Gribenko
2618d3ba23f2d9e6c87794d059412a0808c9cbacb25Dmitri Gribenkocomments::FullComment *ASTContext::getCommentForDecl(const Decl *D) const {
262f50555eedef33fd5a67d369aa0ae8a6f1d201543Dmitri Gribenko  const RawComment *RC = getRawCommentForAnyRedecl(D);
2638d3ba23f2d9e6c87794d059412a0808c9cbacb25Dmitri Gribenko  if (!RC)
2648d3ba23f2d9e6c87794d059412a0808c9cbacb25Dmitri Gribenko    return NULL;
2658d3ba23f2d9e6c87794d059412a0808c9cbacb25Dmitri Gribenko
266f50555eedef33fd5a67d369aa0ae8a6f1d201543Dmitri Gribenko  return RC->getParsed(*this);
2678d3ba23f2d9e6c87794d059412a0808c9cbacb25Dmitri Gribenko}
2688d3ba23f2d9e6c87794d059412a0808c9cbacb25Dmitri Gribenko
2693e1274f2b99cb99c03cc8e2c6517c37d330b597aDouglas Gregorvoid
2703e1274f2b99cb99c03cc8e2c6517c37d330b597aDouglas GregorASTContext::CanonicalTemplateTemplateParm::Profile(llvm::FoldingSetNodeID &ID,
2713e1274f2b99cb99c03cc8e2c6517c37d330b597aDouglas Gregor                                               TemplateTemplateParmDecl *Parm) {
2723e1274f2b99cb99c03cc8e2c6517c37d330b597aDouglas Gregor  ID.AddInteger(Parm->getDepth());
2733e1274f2b99cb99c03cc8e2c6517c37d330b597aDouglas Gregor  ID.AddInteger(Parm->getPosition());
27461c4d28e36cd3f1be392cb77f07436d1fa6b0f9fDouglas Gregor  ID.AddBoolean(Parm->isParameterPack());
2753e1274f2b99cb99c03cc8e2c6517c37d330b597aDouglas Gregor
2763e1274f2b99cb99c03cc8e2c6517c37d330b597aDouglas Gregor  TemplateParameterList *Params = Parm->getTemplateParameters();
2773e1274f2b99cb99c03cc8e2c6517c37d330b597aDouglas Gregor  ID.AddInteger(Params->size());
2783e1274f2b99cb99c03cc8e2c6517c37d330b597aDouglas Gregor  for (TemplateParameterList::const_iterator P = Params->begin(),
2793e1274f2b99cb99c03cc8e2c6517c37d330b597aDouglas Gregor                                          PEnd = Params->end();
2803e1274f2b99cb99c03cc8e2c6517c37d330b597aDouglas Gregor       P != PEnd; ++P) {
2813e1274f2b99cb99c03cc8e2c6517c37d330b597aDouglas Gregor    if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(*P)) {
2823e1274f2b99cb99c03cc8e2c6517c37d330b597aDouglas Gregor      ID.AddInteger(0);
2833e1274f2b99cb99c03cc8e2c6517c37d330b597aDouglas Gregor      ID.AddBoolean(TTP->isParameterPack());
2843e1274f2b99cb99c03cc8e2c6517c37d330b597aDouglas Gregor      continue;
2853e1274f2b99cb99c03cc8e2c6517c37d330b597aDouglas Gregor    }
2863e1274f2b99cb99c03cc8e2c6517c37d330b597aDouglas Gregor
2873e1274f2b99cb99c03cc8e2c6517c37d330b597aDouglas Gregor    if (NonTypeTemplateParmDecl *NTTP = dyn_cast<NonTypeTemplateParmDecl>(*P)) {
2883e1274f2b99cb99c03cc8e2c6517c37d330b597aDouglas Gregor      ID.AddInteger(1);
28961c4d28e36cd3f1be392cb77f07436d1fa6b0f9fDouglas Gregor      ID.AddBoolean(NTTP->isParameterPack());
2909e9c454b12671a624f666fc6fbf132fdf183effcEli Friedman      ID.AddPointer(NTTP->getType().getCanonicalType().getAsOpaquePtr());
2916952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor      if (NTTP->isExpandedParameterPack()) {
2926952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor        ID.AddBoolean(true);
2936952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor        ID.AddInteger(NTTP->getNumExpansionTypes());
2949e9c454b12671a624f666fc6fbf132fdf183effcEli Friedman        for (unsigned I = 0, N = NTTP->getNumExpansionTypes(); I != N; ++I) {
2959e9c454b12671a624f666fc6fbf132fdf183effcEli Friedman          QualType T = NTTP->getExpansionType(I);
2969e9c454b12671a624f666fc6fbf132fdf183effcEli Friedman          ID.AddPointer(T.getCanonicalType().getAsOpaquePtr());
2979e9c454b12671a624f666fc6fbf132fdf183effcEli Friedman        }
2986952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor      } else
2996952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor        ID.AddBoolean(false);
3003e1274f2b99cb99c03cc8e2c6517c37d330b597aDouglas Gregor      continue;
3013e1274f2b99cb99c03cc8e2c6517c37d330b597aDouglas Gregor    }
3023e1274f2b99cb99c03cc8e2c6517c37d330b597aDouglas Gregor
3033e1274f2b99cb99c03cc8e2c6517c37d330b597aDouglas Gregor    TemplateTemplateParmDecl *TTP = cast<TemplateTemplateParmDecl>(*P);
3043e1274f2b99cb99c03cc8e2c6517c37d330b597aDouglas Gregor    ID.AddInteger(2);
3053e1274f2b99cb99c03cc8e2c6517c37d330b597aDouglas Gregor    Profile(ID, TTP);
3063e1274f2b99cb99c03cc8e2c6517c37d330b597aDouglas Gregor  }
3073e1274f2b99cb99c03cc8e2c6517c37d330b597aDouglas Gregor}
3083e1274f2b99cb99c03cc8e2c6517c37d330b597aDouglas Gregor
3093e1274f2b99cb99c03cc8e2c6517c37d330b597aDouglas GregorTemplateTemplateParmDecl *
3103e1274f2b99cb99c03cc8e2c6517c37d330b597aDouglas GregorASTContext::getCanonicalTemplateTemplateParmDecl(
3114ba2a17694148e16eaa8d3917f657ffcd3667be4Jay Foad                                          TemplateTemplateParmDecl *TTP) const {
3123e1274f2b99cb99c03cc8e2c6517c37d330b597aDouglas Gregor  // Check if we already have a canonical template template parameter.
3133e1274f2b99cb99c03cc8e2c6517c37d330b597aDouglas Gregor  llvm::FoldingSetNodeID ID;
3143e1274f2b99cb99c03cc8e2c6517c37d330b597aDouglas Gregor  CanonicalTemplateTemplateParm::Profile(ID, TTP);
3153e1274f2b99cb99c03cc8e2c6517c37d330b597aDouglas Gregor  void *InsertPos = 0;
3163e1274f2b99cb99c03cc8e2c6517c37d330b597aDouglas Gregor  CanonicalTemplateTemplateParm *Canonical
3173e1274f2b99cb99c03cc8e2c6517c37d330b597aDouglas Gregor    = CanonTemplateTemplateParms.FindNodeOrInsertPos(ID, InsertPos);
3183e1274f2b99cb99c03cc8e2c6517c37d330b597aDouglas Gregor  if (Canonical)
3193e1274f2b99cb99c03cc8e2c6517c37d330b597aDouglas Gregor    return Canonical->getParam();
3203e1274f2b99cb99c03cc8e2c6517c37d330b597aDouglas Gregor
3213e1274f2b99cb99c03cc8e2c6517c37d330b597aDouglas Gregor  // Build a canonical template parameter list.
3223e1274f2b99cb99c03cc8e2c6517c37d330b597aDouglas Gregor  TemplateParameterList *Params = TTP->getTemplateParameters();
3235f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner  SmallVector<NamedDecl *, 4> CanonParams;
3243e1274f2b99cb99c03cc8e2c6517c37d330b597aDouglas Gregor  CanonParams.reserve(Params->size());
3253e1274f2b99cb99c03cc8e2c6517c37d330b597aDouglas Gregor  for (TemplateParameterList::const_iterator P = Params->begin(),
3263e1274f2b99cb99c03cc8e2c6517c37d330b597aDouglas Gregor                                          PEnd = Params->end();
3273e1274f2b99cb99c03cc8e2c6517c37d330b597aDouglas Gregor       P != PEnd; ++P) {
3283e1274f2b99cb99c03cc8e2c6517c37d330b597aDouglas Gregor    if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(*P))
3293e1274f2b99cb99c03cc8e2c6517c37d330b597aDouglas Gregor      CanonParams.push_back(
3303e1274f2b99cb99c03cc8e2c6517c37d330b597aDouglas Gregor                  TemplateTypeParmDecl::Create(*this, getTranslationUnitDecl(),
331344577e6b58f42d18dc8118c8903b49a85dc005eAbramo Bagnara                                               SourceLocation(),
332344577e6b58f42d18dc8118c8903b49a85dc005eAbramo Bagnara                                               SourceLocation(),
333344577e6b58f42d18dc8118c8903b49a85dc005eAbramo Bagnara                                               TTP->getDepth(),
3343e1274f2b99cb99c03cc8e2c6517c37d330b597aDouglas Gregor                                               TTP->getIndex(), 0, false,
3353e1274f2b99cb99c03cc8e2c6517c37d330b597aDouglas Gregor                                               TTP->isParameterPack()));
3363e1274f2b99cb99c03cc8e2c6517c37d330b597aDouglas Gregor    else if (NonTypeTemplateParmDecl *NTTP
3376952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor             = dyn_cast<NonTypeTemplateParmDecl>(*P)) {
3386952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor      QualType T = getCanonicalType(NTTP->getType());
3396952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor      TypeSourceInfo *TInfo = getTrivialTypeSourceInfo(T);
3406952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor      NonTypeTemplateParmDecl *Param;
3416952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor      if (NTTP->isExpandedParameterPack()) {
3425f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner        SmallVector<QualType, 2> ExpandedTypes;
3435f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner        SmallVector<TypeSourceInfo *, 2> ExpandedTInfos;
3446952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor        for (unsigned I = 0, N = NTTP->getNumExpansionTypes(); I != N; ++I) {
3456952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor          ExpandedTypes.push_back(getCanonicalType(NTTP->getExpansionType(I)));
3466952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor          ExpandedTInfos.push_back(
3476952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor                                getTrivialTypeSourceInfo(ExpandedTypes.back()));
3486952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor        }
3496952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor
3506952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor        Param = NonTypeTemplateParmDecl::Create(*this, getTranslationUnitDecl(),
351ff676cb48fe8bf7be2feaa251dc7c5fb15af4730Abramo Bagnara                                                SourceLocation(),
352ff676cb48fe8bf7be2feaa251dc7c5fb15af4730Abramo Bagnara                                                SourceLocation(),
3536952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor                                                NTTP->getDepth(),
3546952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor                                                NTTP->getPosition(), 0,
3556952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor                                                T,
3566952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor                                                TInfo,
3576952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor                                                ExpandedTypes.data(),
3586952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor                                                ExpandedTypes.size(),
3596952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor                                                ExpandedTInfos.data());
3606952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor      } else {
3616952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor        Param = NonTypeTemplateParmDecl::Create(*this, getTranslationUnitDecl(),
362ff676cb48fe8bf7be2feaa251dc7c5fb15af4730Abramo Bagnara                                                SourceLocation(),
363ff676cb48fe8bf7be2feaa251dc7c5fb15af4730Abramo Bagnara                                                SourceLocation(),
3646952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor                                                NTTP->getDepth(),
3656952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor                                                NTTP->getPosition(), 0,
3666952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor                                                T,
3676952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor                                                NTTP->isParameterPack(),
3686952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor                                                TInfo);
3696952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor      }
3706952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor      CanonParams.push_back(Param);
3716952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor
3726952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor    } else
3733e1274f2b99cb99c03cc8e2c6517c37d330b597aDouglas Gregor      CanonParams.push_back(getCanonicalTemplateTemplateParmDecl(
3743e1274f2b99cb99c03cc8e2c6517c37d330b597aDouglas Gregor                                           cast<TemplateTemplateParmDecl>(*P)));
3753e1274f2b99cb99c03cc8e2c6517c37d330b597aDouglas Gregor  }
3763e1274f2b99cb99c03cc8e2c6517c37d330b597aDouglas Gregor
3773e1274f2b99cb99c03cc8e2c6517c37d330b597aDouglas Gregor  TemplateTemplateParmDecl *CanonTTP
3783e1274f2b99cb99c03cc8e2c6517c37d330b597aDouglas Gregor    = TemplateTemplateParmDecl::Create(*this, getTranslationUnitDecl(),
3793e1274f2b99cb99c03cc8e2c6517c37d330b597aDouglas Gregor                                       SourceLocation(), TTP->getDepth(),
38061c4d28e36cd3f1be392cb77f07436d1fa6b0f9fDouglas Gregor                                       TTP->getPosition(),
38161c4d28e36cd3f1be392cb77f07436d1fa6b0f9fDouglas Gregor                                       TTP->isParameterPack(),
38261c4d28e36cd3f1be392cb77f07436d1fa6b0f9fDouglas Gregor                                       0,
3833e1274f2b99cb99c03cc8e2c6517c37d330b597aDouglas Gregor                         TemplateParameterList::Create(*this, SourceLocation(),
3843e1274f2b99cb99c03cc8e2c6517c37d330b597aDouglas Gregor                                                       SourceLocation(),
3853e1274f2b99cb99c03cc8e2c6517c37d330b597aDouglas Gregor                                                       CanonParams.data(),
3863e1274f2b99cb99c03cc8e2c6517c37d330b597aDouglas Gregor                                                       CanonParams.size(),
3873e1274f2b99cb99c03cc8e2c6517c37d330b597aDouglas Gregor                                                       SourceLocation()));
3883e1274f2b99cb99c03cc8e2c6517c37d330b597aDouglas Gregor
3893e1274f2b99cb99c03cc8e2c6517c37d330b597aDouglas Gregor  // Get the new insert position for the node we care about.
3903e1274f2b99cb99c03cc8e2c6517c37d330b597aDouglas Gregor  Canonical = CanonTemplateTemplateParms.FindNodeOrInsertPos(ID, InsertPos);
3913e1274f2b99cb99c03cc8e2c6517c37d330b597aDouglas Gregor  assert(Canonical == 0 && "Shouldn't be in the map!");
3923e1274f2b99cb99c03cc8e2c6517c37d330b597aDouglas Gregor  (void)Canonical;
3933e1274f2b99cb99c03cc8e2c6517c37d330b597aDouglas Gregor
3943e1274f2b99cb99c03cc8e2c6517c37d330b597aDouglas Gregor  // Create the canonical template template parameter entry.
3953e1274f2b99cb99c03cc8e2c6517c37d330b597aDouglas Gregor  Canonical = new (*this) CanonicalTemplateTemplateParm(CanonTTP);
3963e1274f2b99cb99c03cc8e2c6517c37d330b597aDouglas Gregor  CanonTemplateTemplateParms.InsertNode(Canonical, InsertPos);
3973e1274f2b99cb99c03cc8e2c6517c37d330b597aDouglas Gregor  return CanonTTP;
3983e1274f2b99cb99c03cc8e2c6517c37d330b597aDouglas Gregor}
3993e1274f2b99cb99c03cc8e2c6517c37d330b597aDouglas Gregor
400071cc7deffad608165b1ddd5263e8bf181861520Charles DavisCXXABI *ASTContext::createCXXABI(const TargetInfo &T) {
401ee79a4c30e5d1c5285551c9a25b8ec6d45d46aa7John McCall  if (!LangOpts.CPlusPlus) return 0;
402ee79a4c30e5d1c5285551c9a25b8ec6d45d46aa7John McCall
40320cf717034ba1f20fc47c025ecb72ed9b631ad13Charles Davis  switch (T.getCXXABI()) {
404ee79a4c30e5d1c5285551c9a25b8ec6d45d46aa7John McCall  case CXXABI_ARM:
405ee79a4c30e5d1c5285551c9a25b8ec6d45d46aa7John McCall    return CreateARMCXXABI(*this);
406ee79a4c30e5d1c5285551c9a25b8ec6d45d46aa7John McCall  case CXXABI_Itanium:
407071cc7deffad608165b1ddd5263e8bf181861520Charles Davis    return CreateItaniumCXXABI(*this);
40820cf717034ba1f20fc47c025ecb72ed9b631ad13Charles Davis  case CXXABI_Microsoft:
40920cf717034ba1f20fc47c025ecb72ed9b631ad13Charles Davis    return CreateMicrosoftCXXABI(*this);
41020cf717034ba1f20fc47c025ecb72ed9b631ad13Charles Davis  }
4117530c034c0c71a64c5a9173206d9742ae847af8bDavid Blaikie  llvm_unreachable("Invalid CXXABI type!");
412071cc7deffad608165b1ddd5263e8bf181861520Charles Davis}
413071cc7deffad608165b1ddd5263e8bf181861520Charles Davis
414bcfd1f55bfbb3e5944cd5e03d07b343e280838c4Douglas Gregorstatic const LangAS::Map *getAddressSpaceMap(const TargetInfo &T,
415207f4d8543529221932af82836016a2ef066c917Peter Collingbourne                                             const LangOptions &LOpts) {
416207f4d8543529221932af82836016a2ef066c917Peter Collingbourne  if (LOpts.FakeAddressSpaceMap) {
417207f4d8543529221932af82836016a2ef066c917Peter Collingbourne    // The fake address space map must have a distinct entry for each
418207f4d8543529221932af82836016a2ef066c917Peter Collingbourne    // language-specific address space.
419207f4d8543529221932af82836016a2ef066c917Peter Collingbourne    static const unsigned FakeAddrSpaceMap[] = {
420207f4d8543529221932af82836016a2ef066c917Peter Collingbourne      1, // opencl_global
421207f4d8543529221932af82836016a2ef066c917Peter Collingbourne      2, // opencl_local
4224dc34ebf2a0716bf77ba110dab6777a3fc4397ddPeter Collingbourne      3, // opencl_constant
4234dc34ebf2a0716bf77ba110dab6777a3fc4397ddPeter Collingbourne      4, // cuda_device
4244dc34ebf2a0716bf77ba110dab6777a3fc4397ddPeter Collingbourne      5, // cuda_constant
4254dc34ebf2a0716bf77ba110dab6777a3fc4397ddPeter Collingbourne      6  // cuda_shared
426207f4d8543529221932af82836016a2ef066c917Peter Collingbourne    };
427bcfd1f55bfbb3e5944cd5e03d07b343e280838c4Douglas Gregor    return &FakeAddrSpaceMap;
428207f4d8543529221932af82836016a2ef066c917Peter Collingbourne  } else {
429bcfd1f55bfbb3e5944cd5e03d07b343e280838c4Douglas Gregor    return &T.getAddressSpaceMap();
430207f4d8543529221932af82836016a2ef066c917Peter Collingbourne  }
431207f4d8543529221932af82836016a2ef066c917Peter Collingbourne}
432207f4d8543529221932af82836016a2ef066c917Peter Collingbourne
4333e3cd93b2fd9644e970c389e715c13883faf68b6Douglas GregorASTContext::ASTContext(LangOptions& LOpts, SourceManager &SM,
434bcfd1f55bfbb3e5944cd5e03d07b343e280838c4Douglas Gregor                       const TargetInfo *t,
435e91593ef084479340582b2ba177b44be50a717b7Daniel Dunbar                       IdentifierTable &idents, SelectorTable &sels,
4361b63e4f732dbc73d90abf886b4d21f8e3a165f6dChris Lattner                       Builtin::Context &builtins,
437bcfd1f55bfbb3e5944cd5e03d07b343e280838c4Douglas Gregor                       unsigned size_reserve,
438bcfd1f55bfbb3e5944cd5e03d07b343e280838c4Douglas Gregor                       bool DelayInitialization)
439bcfd1f55bfbb3e5944cd5e03d07b343e280838c4Douglas Gregor  : FunctionProtoTypes(this_()),
440bcfd1f55bfbb3e5944cd5e03d07b343e280838c4Douglas Gregor    TemplateSpecializationTypes(this_()),
441bcfd1f55bfbb3e5944cd5e03d07b343e280838c4Douglas Gregor    DependentTemplateSpecializationTypes(this_()),
442bcfd1f55bfbb3e5944cd5e03d07b343e280838c4Douglas Gregor    SubstTemplateTemplateParmPacks(this_()),
443bcfd1f55bfbb3e5944cd5e03d07b343e280838c4Douglas Gregor    GlobalNestedNameSpecifier(0),
444bcfd1f55bfbb3e5944cd5e03d07b343e280838c4Douglas Gregor    Int128Decl(0), UInt128Decl(0),
445c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge    BuiltinVaListDecl(0),
446a6ea10e22b600d92e084f6b11b9b9a92d0eb2412Douglas Gregor    ObjCIdDecl(0), ObjCSelDecl(0), ObjCClassDecl(0), ObjCProtocolClassDecl(0),
447e97179c675b341927807c718be215c8d1aab8acbDouglas Gregor    CFConstantStringTypeDecl(0), ObjCInstanceTypeDecl(0),
448bcfd1f55bfbb3e5944cd5e03d07b343e280838c4Douglas Gregor    FILEDecl(0),
449e2d4f4ed44a32f179c6d48cd1dba8346ab2129d9Rafael Espindola    jmp_bufDecl(0), sigjmp_bufDecl(0), ucontext_tDecl(0),
450e2d4f4ed44a32f179c6d48cd1dba8346ab2129d9Rafael Espindola    BlockDescriptorType(0), BlockDescriptorExtendedType(0),
451e2d4f4ed44a32f179c6d48cd1dba8346ab2129d9Rafael Espindola    cudaConfigureCallDecl(0),
452e664977aca2a05a77abab5a06dc0fb69e870cfb9Douglas Gregor    NullTypeSourceInfo(QualType()),
453e664977aca2a05a77abab5a06dc0fb69e870cfb9Douglas Gregor    FirstLocalImport(), LastLocalImport(),
454bcfd1f55bfbb3e5944cd5e03d07b343e280838c4Douglas Gregor    SourceMgr(SM), LangOpts(LOpts),
45530c42404202d2e2512e51efc6066bd614cfdb5a4Douglas Gregor    AddrSpaceMap(0), Target(t), PrintingPolicy(LOpts),
456bcfd1f55bfbb3e5944cd5e03d07b343e280838c4Douglas Gregor    Idents(idents), Selectors(sels),
457bcfd1f55bfbb3e5944cd5e03d07b343e280838c4Douglas Gregor    BuiltinInfo(builtins),
458bcfd1f55bfbb3e5944cd5e03d07b343e280838c4Douglas Gregor    DeclarationNames(*this),
45930c42404202d2e2512e51efc6066bd614cfdb5a4Douglas Gregor    ExternalSource(0), Listener(0),
460aa0cd85838f2a024e589ea4e8c2094130065af21Dmitri Gribenko    Comments(SM), CommentsLoaded(false),
461bcfd1f55bfbb3e5944cd5e03d07b343e280838c4Douglas Gregor    LastSDM(0, 0),
462bcfd1f55bfbb3e5944cd5e03d07b343e280838c4Douglas Gregor    UniqueBlockByRefTypeID(0)
463bcfd1f55bfbb3e5944cd5e03d07b343e280838c4Douglas Gregor{
4641eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  if (size_reserve > 0) Types.reserve(size_reserve);
465e91593ef084479340582b2ba177b44be50a717b7Daniel Dunbar  TUDecl = TranslationUnitDecl::Create(*this);
466bcfd1f55bfbb3e5944cd5e03d07b343e280838c4Douglas Gregor
467bcfd1f55bfbb3e5944cd5e03d07b343e280838c4Douglas Gregor  if (!DelayInitialization) {
468bcfd1f55bfbb3e5944cd5e03d07b343e280838c4Douglas Gregor    assert(t && "No target supplied for ASTContext initialization");
469bcfd1f55bfbb3e5944cd5e03d07b343e280838c4Douglas Gregor    InitBuiltinTypes(*t);
470bcfd1f55bfbb3e5944cd5e03d07b343e280838c4Douglas Gregor  }
471e91593ef084479340582b2ba177b44be50a717b7Daniel Dunbar}
472e91593ef084479340582b2ba177b44be50a717b7Daniel Dunbar
4735f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid SpencerASTContext::~ASTContext() {
4743478eb6872d836600caf45b0f81c2065d685d6e0Ted Kremenek  // Release the DenseMaps associated with DeclContext objects.
4753478eb6872d836600caf45b0f81c2065d685d6e0Ted Kremenek  // FIXME: Is this the ideal solution?
4763478eb6872d836600caf45b0f81c2065d685d6e0Ted Kremenek  ReleaseDeclContextMaps();
4777d10b7eb670b821741b4c96f6cf7afbc3bb39abeDouglas Gregor
47863fe86bee66fc145942c56b2cc564ea0b9b9ea12Douglas Gregor  // Call all of the deallocation functions.
47963fe86bee66fc145942c56b2cc564ea0b9b9ea12Douglas Gregor  for (unsigned I = 0, N = Deallocations.size(); I != N; ++I)
48063fe86bee66fc145942c56b2cc564ea0b9b9ea12Douglas Gregor    Deallocations[I].first(Deallocations[I].second);
4810054531488928a424666ac11fcdc6bcc5112de52Douglas Gregor
482dcfcfbec478f7ed96cd8d92f30c29bd4e30d5b9cTed Kremenek  // ASTRecordLayout objects in ASTRecordLayouts must always be destroyed
48363fe86bee66fc145942c56b2cc564ea0b9b9ea12Douglas Gregor  // because they can contain DenseMaps.
48463fe86bee66fc145942c56b2cc564ea0b9b9ea12Douglas Gregor  for (llvm::DenseMap<const ObjCContainerDecl*,
48563fe86bee66fc145942c56b2cc564ea0b9b9ea12Douglas Gregor       const ASTRecordLayout*>::iterator
48663fe86bee66fc145942c56b2cc564ea0b9b9ea12Douglas Gregor       I = ObjCLayouts.begin(), E = ObjCLayouts.end(); I != E; )
48763fe86bee66fc145942c56b2cc564ea0b9b9ea12Douglas Gregor    // Increment in loop to prevent using deallocated memory.
48863fe86bee66fc145942c56b2cc564ea0b9b9ea12Douglas Gregor    if (ASTRecordLayout *R = const_cast<ASTRecordLayout*>((I++)->second))
48963fe86bee66fc145942c56b2cc564ea0b9b9ea12Douglas Gregor      R->Destroy(*this);
49063fe86bee66fc145942c56b2cc564ea0b9b9ea12Douglas Gregor
491dcfcfbec478f7ed96cd8d92f30c29bd4e30d5b9cTed Kremenek  for (llvm::DenseMap<const RecordDecl*, const ASTRecordLayout*>::iterator
492dcfcfbec478f7ed96cd8d92f30c29bd4e30d5b9cTed Kremenek       I = ASTRecordLayouts.begin(), E = ASTRecordLayouts.end(); I != E; ) {
493dcfcfbec478f7ed96cd8d92f30c29bd4e30d5b9cTed Kremenek    // Increment in loop to prevent using deallocated memory.
494dcfcfbec478f7ed96cd8d92f30c29bd4e30d5b9cTed Kremenek    if (ASTRecordLayout *R = const_cast<ASTRecordLayout*>((I++)->second))
495dcfcfbec478f7ed96cd8d92f30c29bd4e30d5b9cTed Kremenek      R->Destroy(*this);
496dcfcfbec478f7ed96cd8d92f30c29bd4e30d5b9cTed Kremenek  }
4976320064d0c60fa8683f5623881c9394fd4aa7689Douglas Gregor
4986320064d0c60fa8683f5623881c9394fd4aa7689Douglas Gregor  for (llvm::DenseMap<const Decl*, AttrVec*>::iterator A = DeclAttrs.begin(),
4996320064d0c60fa8683f5623881c9394fd4aa7689Douglas Gregor                                                    AEnd = DeclAttrs.end();
5006320064d0c60fa8683f5623881c9394fd4aa7689Douglas Gregor       A != AEnd; ++A)
5016320064d0c60fa8683f5623881c9394fd4aa7689Douglas Gregor    A->second->~AttrVec();
5026320064d0c60fa8683f5623881c9394fd4aa7689Douglas Gregor}
503ab452ba8323d1985e08bade2bced588cddf2cc28Douglas Gregor
5040054531488928a424666ac11fcdc6bcc5112de52Douglas Gregorvoid ASTContext::AddDeallocation(void (*Callback)(void*), void *Data) {
5050054531488928a424666ac11fcdc6bcc5112de52Douglas Gregor  Deallocations.push_back(std::make_pair(Callback, Data));
5060054531488928a424666ac11fcdc6bcc5112de52Douglas Gregor}
5070054531488928a424666ac11fcdc6bcc5112de52Douglas Gregor
5081eb4433ac451dc16f4133a88af2d002ac26c58efMike Stumpvoid
5096f42b62b6194f53bcbc349f5d17388e1936535d7Dylan NoblesmithASTContext::setExternalSource(OwningPtr<ExternalASTSource> &Source) {
5102cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor  ExternalSource.reset(Source.take());
5112cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor}
5122cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor
5135f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencervoid ASTContext::PrintStats() const {
514cd92a65edc7cbbbb7e3aee8d31008594de90fa51Chandler Carruth  llvm::errs() << "\n*** AST Context Stats:\n";
515cd92a65edc7cbbbb7e3aee8d31008594de90fa51Chandler Carruth  llvm::errs() << "  " << Types.size() << " types total.\n";
5167c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl
517dbe833da54e1b6192991b64fc453cd50b5ee7909Douglas Gregor  unsigned counts[] = {
5181eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump#define TYPE(Name, Parent) 0,
519dbe833da54e1b6192991b64fc453cd50b5ee7909Douglas Gregor#define ABSTRACT_TYPE(Name, Parent)
520dbe833da54e1b6192991b64fc453cd50b5ee7909Douglas Gregor#include "clang/AST/TypeNodes.def"
521dbe833da54e1b6192991b64fc453cd50b5ee7909Douglas Gregor    0 // Extra
522dbe833da54e1b6192991b64fc453cd50b5ee7909Douglas Gregor  };
523c2ee10d79f70036af652a395ac1f8273f3d04e12Douglas Gregor
5245f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  for (unsigned i = 0, e = Types.size(); i != e; ++i) {
5255f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    Type *T = Types[i];
526dbe833da54e1b6192991b64fc453cd50b5ee7909Douglas Gregor    counts[(unsigned)T->getTypeClass()]++;
5275f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
5285f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
529dbe833da54e1b6192991b64fc453cd50b5ee7909Douglas Gregor  unsigned Idx = 0;
530dbe833da54e1b6192991b64fc453cd50b5ee7909Douglas Gregor  unsigned TotalBytes = 0;
531dbe833da54e1b6192991b64fc453cd50b5ee7909Douglas Gregor#define TYPE(Name, Parent)                                              \
532dbe833da54e1b6192991b64fc453cd50b5ee7909Douglas Gregor  if (counts[Idx])                                                      \
533cd92a65edc7cbbbb7e3aee8d31008594de90fa51Chandler Carruth    llvm::errs() << "    " << counts[Idx] << " " << #Name               \
534cd92a65edc7cbbbb7e3aee8d31008594de90fa51Chandler Carruth                 << " types\n";                                         \
535dbe833da54e1b6192991b64fc453cd50b5ee7909Douglas Gregor  TotalBytes += counts[Idx] * sizeof(Name##Type);                       \
536dbe833da54e1b6192991b64fc453cd50b5ee7909Douglas Gregor  ++Idx;
537dbe833da54e1b6192991b64fc453cd50b5ee7909Douglas Gregor#define ABSTRACT_TYPE(Name, Parent)
538dbe833da54e1b6192991b64fc453cd50b5ee7909Douglas Gregor#include "clang/AST/TypeNodes.def"
5391eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
540cd92a65edc7cbbbb7e3aee8d31008594de90fa51Chandler Carruth  llvm::errs() << "Total bytes = " << TotalBytes << "\n";
541cd92a65edc7cbbbb7e3aee8d31008594de90fa51Chandler Carruth
5424923aa25eb39d64106a5817c02d560a3aecf8b2cDouglas Gregor  // Implicit special member functions.
543cd92a65edc7cbbbb7e3aee8d31008594de90fa51Chandler Carruth  llvm::errs() << NumImplicitDefaultConstructorsDeclared << "/"
544cd92a65edc7cbbbb7e3aee8d31008594de90fa51Chandler Carruth               << NumImplicitDefaultConstructors
545cd92a65edc7cbbbb7e3aee8d31008594de90fa51Chandler Carruth               << " implicit default constructors created\n";
546cd92a65edc7cbbbb7e3aee8d31008594de90fa51Chandler Carruth  llvm::errs() << NumImplicitCopyConstructorsDeclared << "/"
547cd92a65edc7cbbbb7e3aee8d31008594de90fa51Chandler Carruth               << NumImplicitCopyConstructors
548cd92a65edc7cbbbb7e3aee8d31008594de90fa51Chandler Carruth               << " implicit copy constructors created\n";
5494e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie  if (getLangOpts().CPlusPlus)
550cd92a65edc7cbbbb7e3aee8d31008594de90fa51Chandler Carruth    llvm::errs() << NumImplicitMoveConstructorsDeclared << "/"
551cd92a65edc7cbbbb7e3aee8d31008594de90fa51Chandler Carruth                 << NumImplicitMoveConstructors
552cd92a65edc7cbbbb7e3aee8d31008594de90fa51Chandler Carruth                 << " implicit move constructors created\n";
553cd92a65edc7cbbbb7e3aee8d31008594de90fa51Chandler Carruth  llvm::errs() << NumImplicitCopyAssignmentOperatorsDeclared << "/"
554cd92a65edc7cbbbb7e3aee8d31008594de90fa51Chandler Carruth               << NumImplicitCopyAssignmentOperators
555cd92a65edc7cbbbb7e3aee8d31008594de90fa51Chandler Carruth               << " implicit copy assignment operators created\n";
5564e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie  if (getLangOpts().CPlusPlus)
557cd92a65edc7cbbbb7e3aee8d31008594de90fa51Chandler Carruth    llvm::errs() << NumImplicitMoveAssignmentOperatorsDeclared << "/"
558cd92a65edc7cbbbb7e3aee8d31008594de90fa51Chandler Carruth                 << NumImplicitMoveAssignmentOperators
559cd92a65edc7cbbbb7e3aee8d31008594de90fa51Chandler Carruth                 << " implicit move assignment operators created\n";
560cd92a65edc7cbbbb7e3aee8d31008594de90fa51Chandler Carruth  llvm::errs() << NumImplicitDestructorsDeclared << "/"
561cd92a65edc7cbbbb7e3aee8d31008594de90fa51Chandler Carruth               << NumImplicitDestructors
562cd92a65edc7cbbbb7e3aee8d31008594de90fa51Chandler Carruth               << " implicit destructors created\n";
563cd92a65edc7cbbbb7e3aee8d31008594de90fa51Chandler Carruth
5642cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor  if (ExternalSource.get()) {
565cd92a65edc7cbbbb7e3aee8d31008594de90fa51Chandler Carruth    llvm::errs() << "\n";
5662cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor    ExternalSource->PrintStats();
5672cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor  }
568cd92a65edc7cbbbb7e3aee8d31008594de90fa51Chandler Carruth
56963fe86bee66fc145942c56b2cc564ea0b9b9ea12Douglas Gregor  BumpAlloc.PrintStats();
5705f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
5715f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
572772eeaefef2c883aabe35caf4543e7e32d290183Douglas GregorTypedefDecl *ASTContext::getInt128Decl() const {
573772eeaefef2c883aabe35caf4543e7e32d290183Douglas Gregor  if (!Int128Decl) {
574772eeaefef2c883aabe35caf4543e7e32d290183Douglas Gregor    TypeSourceInfo *TInfo = getTrivialTypeSourceInfo(Int128Ty);
575772eeaefef2c883aabe35caf4543e7e32d290183Douglas Gregor    Int128Decl = TypedefDecl::Create(const_cast<ASTContext &>(*this),
576772eeaefef2c883aabe35caf4543e7e32d290183Douglas Gregor                                     getTranslationUnitDecl(),
577772eeaefef2c883aabe35caf4543e7e32d290183Douglas Gregor                                     SourceLocation(),
578772eeaefef2c883aabe35caf4543e7e32d290183Douglas Gregor                                     SourceLocation(),
579772eeaefef2c883aabe35caf4543e7e32d290183Douglas Gregor                                     &Idents.get("__int128_t"),
580772eeaefef2c883aabe35caf4543e7e32d290183Douglas Gregor                                     TInfo);
581772eeaefef2c883aabe35caf4543e7e32d290183Douglas Gregor  }
582772eeaefef2c883aabe35caf4543e7e32d290183Douglas Gregor
583772eeaefef2c883aabe35caf4543e7e32d290183Douglas Gregor  return Int128Decl;
584772eeaefef2c883aabe35caf4543e7e32d290183Douglas Gregor}
585772eeaefef2c883aabe35caf4543e7e32d290183Douglas Gregor
586772eeaefef2c883aabe35caf4543e7e32d290183Douglas GregorTypedefDecl *ASTContext::getUInt128Decl() const {
587772eeaefef2c883aabe35caf4543e7e32d290183Douglas Gregor  if (!UInt128Decl) {
588772eeaefef2c883aabe35caf4543e7e32d290183Douglas Gregor    TypeSourceInfo *TInfo = getTrivialTypeSourceInfo(UnsignedInt128Ty);
589772eeaefef2c883aabe35caf4543e7e32d290183Douglas Gregor    UInt128Decl = TypedefDecl::Create(const_cast<ASTContext &>(*this),
590772eeaefef2c883aabe35caf4543e7e32d290183Douglas Gregor                                     getTranslationUnitDecl(),
591772eeaefef2c883aabe35caf4543e7e32d290183Douglas Gregor                                     SourceLocation(),
592772eeaefef2c883aabe35caf4543e7e32d290183Douglas Gregor                                     SourceLocation(),
593772eeaefef2c883aabe35caf4543e7e32d290183Douglas Gregor                                     &Idents.get("__uint128_t"),
594772eeaefef2c883aabe35caf4543e7e32d290183Douglas Gregor                                     TInfo);
595772eeaefef2c883aabe35caf4543e7e32d290183Douglas Gregor  }
596772eeaefef2c883aabe35caf4543e7e32d290183Douglas Gregor
597772eeaefef2c883aabe35caf4543e7e32d290183Douglas Gregor  return UInt128Decl;
598772eeaefef2c883aabe35caf4543e7e32d290183Douglas Gregor}
5995f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
600e27ec8ad56dbf1efb2de004b90fbbb86f740e3f1John McCallvoid ASTContext::InitBuiltinType(CanQualType &R, BuiltinType::Kind K) {
6016b304a0254a13f42390b865ff5ba668a49cc58aeJohn McCall  BuiltinType *Ty = new (*this, TypeAlignment) BuiltinType(K);
602e27ec8ad56dbf1efb2de004b90fbbb86f740e3f1John McCall  R = CanQualType::CreateUnsafe(QualType(Ty, 0));
6036b304a0254a13f42390b865ff5ba668a49cc58aeJohn McCall  Types.push_back(Ty);
6045f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
6055f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
606bcfd1f55bfbb3e5944cd5e03d07b343e280838c4Douglas Gregorvoid ASTContext::InitBuiltinTypes(const TargetInfo &Target) {
607bcfd1f55bfbb3e5944cd5e03d07b343e280838c4Douglas Gregor  assert((!this->Target || this->Target == &Target) &&
608bcfd1f55bfbb3e5944cd5e03d07b343e280838c4Douglas Gregor         "Incorrect target reinitialization");
6095f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  assert(VoidTy.isNull() && "Context reinitialized?");
6101eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
611bcfd1f55bfbb3e5944cd5e03d07b343e280838c4Douglas Gregor  this->Target = &Target;
612bcfd1f55bfbb3e5944cd5e03d07b343e280838c4Douglas Gregor
613bcfd1f55bfbb3e5944cd5e03d07b343e280838c4Douglas Gregor  ABI.reset(createCXXABI(Target));
614bcfd1f55bfbb3e5944cd5e03d07b343e280838c4Douglas Gregor  AddrSpaceMap = getAddressSpaceMap(Target, LangOpts);
615bcfd1f55bfbb3e5944cd5e03d07b343e280838c4Douglas Gregor
6165f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // C99 6.2.5p19.
6175f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  InitBuiltinType(VoidTy,              BuiltinType::Void);
6181eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
6195f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // C99 6.2.5p2.
6205f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  InitBuiltinType(BoolTy,              BuiltinType::Bool);
6215f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // C99 6.2.5p3.
62215b91764d08e886391c865c4a444d7b51141c284Eli Friedman  if (LangOpts.CharIsSigned)
6235f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    InitBuiltinType(CharTy,            BuiltinType::Char_S);
6245f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  else
6255f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    InitBuiltinType(CharTy,            BuiltinType::Char_U);
6265f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // C99 6.2.5p4.
6275f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  InitBuiltinType(SignedCharTy,        BuiltinType::SChar);
6285f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  InitBuiltinType(ShortTy,             BuiltinType::Short);
6295f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  InitBuiltinType(IntTy,               BuiltinType::Int);
6305f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  InitBuiltinType(LongTy,              BuiltinType::Long);
6315f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  InitBuiltinType(LongLongTy,          BuiltinType::LongLong);
6321eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
6335f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // C99 6.2.5p6.
6345f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  InitBuiltinType(UnsignedCharTy,      BuiltinType::UChar);
6355f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  InitBuiltinType(UnsignedShortTy,     BuiltinType::UShort);
6365f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  InitBuiltinType(UnsignedIntTy,       BuiltinType::UInt);
6375f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  InitBuiltinType(UnsignedLongTy,      BuiltinType::ULong);
6385f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  InitBuiltinType(UnsignedLongLongTy,  BuiltinType::ULongLong);
6391eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
6405f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // C99 6.2.5p10.
6415f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  InitBuiltinType(FloatTy,             BuiltinType::Float);
6425f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  InitBuiltinType(DoubleTy,            BuiltinType::Double);
6435f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  InitBuiltinType(LongDoubleTy,        BuiltinType::LongDouble);
64464c438a4be2a871fa43c78264663ba1e9788b94dArgyrios Kyrtzidis
6452df9ced9fd1e8c7d7b38443db07e0e811de22571Chris Lattner  // GNU extension, 128-bit integers.
6462df9ced9fd1e8c7d7b38443db07e0e811de22571Chris Lattner  InitBuiltinType(Int128Ty,            BuiltinType::Int128);
6472df9ced9fd1e8c7d7b38443db07e0e811de22571Chris Lattner  InitBuiltinType(UnsignedInt128Ty,    BuiltinType::UInt128);
6482df9ced9fd1e8c7d7b38443db07e0e811de22571Chris Lattner
6493f59c975aa5d047f7edd1b900b5e885c38af0ef7Chris Lattner  if (LangOpts.CPlusPlus) { // C++ 3.9.1p5
650d3d77cd138f8e830f6547b6f83fcd5721ccf5f5dEli Friedman    if (TargetInfo::isTypeSigned(Target.getWCharType()))
6513f59c975aa5d047f7edd1b900b5e885c38af0ef7Chris Lattner      InitBuiltinType(WCharTy,           BuiltinType::WChar_S);
6523f59c975aa5d047f7edd1b900b5e885c38af0ef7Chris Lattner    else  // -fshort-wchar makes wchar_t be unsigned.
6533f59c975aa5d047f7edd1b900b5e885c38af0ef7Chris Lattner      InitBuiltinType(WCharTy,           BuiltinType::WChar_U);
6543f59c975aa5d047f7edd1b900b5e885c38af0ef7Chris Lattner  } else // C99
6553a2503227c3db04a3619735127483263c1075ef7Chris Lattner    WCharTy = getFromTargetType(Target.getWCharType());
65664c438a4be2a871fa43c78264663ba1e9788b94dArgyrios Kyrtzidis
657392da48160bd92ceb486792780467cbfdb2d0e8cJames Molloy  WIntTy = getFromTargetType(Target.getWIntType());
658392da48160bd92ceb486792780467cbfdb2d0e8cJames Molloy
659f5c209d23b20ada4a9b6235db50317239cbf6ae1Alisdair Meredith  if (LangOpts.CPlusPlus) // C++0x 3.9.1p5, extension for C++
660f5c209d23b20ada4a9b6235db50317239cbf6ae1Alisdair Meredith    InitBuiltinType(Char16Ty,           BuiltinType::Char16);
661f5c209d23b20ada4a9b6235db50317239cbf6ae1Alisdair Meredith  else // C99
662f5c209d23b20ada4a9b6235db50317239cbf6ae1Alisdair Meredith    Char16Ty = getFromTargetType(Target.getChar16Type());
663f5c209d23b20ada4a9b6235db50317239cbf6ae1Alisdair Meredith
664f5c209d23b20ada4a9b6235db50317239cbf6ae1Alisdair Meredith  if (LangOpts.CPlusPlus) // C++0x 3.9.1p5, extension for C++
665f5c209d23b20ada4a9b6235db50317239cbf6ae1Alisdair Meredith    InitBuiltinType(Char32Ty,           BuiltinType::Char32);
666f5c209d23b20ada4a9b6235db50317239cbf6ae1Alisdair Meredith  else // C99
667f5c209d23b20ada4a9b6235db50317239cbf6ae1Alisdair Meredith    Char32Ty = getFromTargetType(Target.getChar32Type());
668f5c209d23b20ada4a9b6235db50317239cbf6ae1Alisdair Meredith
669898574e7496ba8fd76290079d3a9d06954992734Douglas Gregor  // Placeholder type for type-dependent expressions whose type is
670898574e7496ba8fd76290079d3a9d06954992734Douglas Gregor  // completely unknown. No code should ever check a type against
671898574e7496ba8fd76290079d3a9d06954992734Douglas Gregor  // DependentTy and users should never see it; however, it is here to
672898574e7496ba8fd76290079d3a9d06954992734Douglas Gregor  // help diagnose failures to properly check for type-dependent
673898574e7496ba8fd76290079d3a9d06954992734Douglas Gregor  // expressions.
674898574e7496ba8fd76290079d3a9d06954992734Douglas Gregor  InitBuiltinType(DependentTy,         BuiltinType::Dependent);
6758e9bebdea69c590dedfbf27374114cb76fe12fbdDouglas Gregor
6762a984cad5ac3fdceeff2bd99daa7b90979313475John McCall  // Placeholder type for functions.
6772a984cad5ac3fdceeff2bd99daa7b90979313475John McCall  InitBuiltinType(OverloadTy,          BuiltinType::Overload);
6782a984cad5ac3fdceeff2bd99daa7b90979313475John McCall
679864c041e118155c2b1ce0ba36942a3da5a4a055eJohn McCall  // Placeholder type for bound members.
680864c041e118155c2b1ce0ba36942a3da5a4a055eJohn McCall  InitBuiltinType(BoundMemberTy,       BuiltinType::BoundMember);
681864c041e118155c2b1ce0ba36942a3da5a4a055eJohn McCall
6823c3b7f90a863af43fa63043d396553ecf205351cJohn McCall  // Placeholder type for pseudo-objects.
6833c3b7f90a863af43fa63043d396553ecf205351cJohn McCall  InitBuiltinType(PseudoObjectTy,      BuiltinType::PseudoObject);
6843c3b7f90a863af43fa63043d396553ecf205351cJohn McCall
6851de4d4e8cb2e9c88809fea8092bc6e835a5473d2John McCall  // "any" type; useful for debugger-like clients.
6861de4d4e8cb2e9c88809fea8092bc6e835a5473d2John McCall  InitBuiltinType(UnknownAnyTy,        BuiltinType::UnknownAny);
6871de4d4e8cb2e9c88809fea8092bc6e835a5473d2John McCall
6880ddaeb9b031070ec64afe92d9892875ac44df427John McCall  // Placeholder type for unbridged ARC casts.
6890ddaeb9b031070ec64afe92d9892875ac44df427John McCall  InitBuiltinType(ARCUnbridgedCastTy,  BuiltinType::ARCUnbridgedCast);
6900ddaeb9b031070ec64afe92d9892875ac44df427John McCall
6915f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // C99 6.2.5p11.
6925f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  FloatComplexTy      = getComplexType(FloatTy);
6935f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  DoubleComplexTy     = getComplexType(DoubleTy);
6945f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  LongDoubleComplexTy = getComplexType(LongDoubleTy);
6958e9bebdea69c590dedfbf27374114cb76fe12fbdDouglas Gregor
69613dcd00615de5c4279d97bdf63cd5f0a14fd9dccFariborz Jahanian  // Builtin types for 'id', 'Class', and 'SEL'.
697de2e22d33afec98324a66a358dfe0951b3c7259aSteve Naroff  InitBuiltinType(ObjCBuiltinIdTy, BuiltinType::ObjCId);
698de2e22d33afec98324a66a358dfe0951b3c7259aSteve Naroff  InitBuiltinType(ObjCBuiltinClassTy, BuiltinType::ObjCClass);
69913dcd00615de5c4279d97bdf63cd5f0a14fd9dccFariborz Jahanian  InitBuiltinType(ObjCBuiltinSelTy, BuiltinType::ObjCSel);
700ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek
701ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek  // Builtin type for __objc_yes and __objc_no
70293a49944e0e68e32bc22d45d44ee136b34beffb3Fariborz Jahanian  ObjCBuiltinBoolTy = (Target.useSignedCharForObjCBool() ?
70393a49944e0e68e32bc22d45d44ee136b34beffb3Fariborz Jahanian                       SignedCharTy : BoolTy);
704ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek
705a526c5c67e5a0473c340903ee542ce570119665fTed Kremenek  ObjCConstantStringType = QualType();
7061eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
70733e1d64ab5cd5d27f8530ccd056191fe2c9f3f2eFariborz Jahanian  // void * type
70833e1d64ab5cd5d27f8530ccd056191fe2c9f3f2eFariborz Jahanian  VoidPtrTy = getPointerType(VoidTy);
7096e8ed16ffef02b82995a90bdcf10ffff7d63839aSebastian Redl
7106e8ed16ffef02b82995a90bdcf10ffff7d63839aSebastian Redl  // nullptr type (C++0x 2.14.7)
7116e8ed16ffef02b82995a90bdcf10ffff7d63839aSebastian Redl  InitBuiltinType(NullPtrTy,           BuiltinType::NullPtr);
712aa4a99b4a62615db243f7a5c433169f2fc704420Anton Korobeynikov
713aa4a99b4a62615db243f7a5c433169f2fc704420Anton Korobeynikov  // half type (OpenCL 6.1.1.1) / ARM NEON __fp16
714aa4a99b4a62615db243f7a5c433169f2fc704420Anton Korobeynikov  InitBuiltinType(HalfTy, BuiltinType::Half);
715fb40e3f10ccef93c4f8fb6bd4fe5a108fa6cd369Meador Inge
716fb40e3f10ccef93c4f8fb6bd4fe5a108fa6cd369Meador Inge  // Builtin type used to help define __builtin_va_list.
717fb40e3f10ccef93c4f8fb6bd4fe5a108fa6cd369Meador Inge  VaListTagTy = QualType();
7185f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
7195f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
720d6471f7c1921c7802804ce3ff6fe9768310f72b9David BlaikieDiagnosticsEngine &ASTContext::getDiagnostics() const {
72178a916ec5ff5b66adec3c499e1b9af7b87668309Argyrios Kyrtzidis  return SourceMgr.getDiagnostics();
72278a916ec5ff5b66adec3c499e1b9af7b87668309Argyrios Kyrtzidis}
72378a916ec5ff5b66adec3c499e1b9af7b87668309Argyrios Kyrtzidis
7246320064d0c60fa8683f5623881c9394fd4aa7689Douglas GregorAttrVec& ASTContext::getDeclAttrs(const Decl *D) {
7256320064d0c60fa8683f5623881c9394fd4aa7689Douglas Gregor  AttrVec *&Result = DeclAttrs[D];
7266320064d0c60fa8683f5623881c9394fd4aa7689Douglas Gregor  if (!Result) {
7276320064d0c60fa8683f5623881c9394fd4aa7689Douglas Gregor    void *Mem = Allocate(sizeof(AttrVec));
7286320064d0c60fa8683f5623881c9394fd4aa7689Douglas Gregor    Result = new (Mem) AttrVec;
7296320064d0c60fa8683f5623881c9394fd4aa7689Douglas Gregor  }
7306320064d0c60fa8683f5623881c9394fd4aa7689Douglas Gregor
7316320064d0c60fa8683f5623881c9394fd4aa7689Douglas Gregor  return *Result;
7326320064d0c60fa8683f5623881c9394fd4aa7689Douglas Gregor}
7336320064d0c60fa8683f5623881c9394fd4aa7689Douglas Gregor
7346320064d0c60fa8683f5623881c9394fd4aa7689Douglas Gregor/// \brief Erase the attributes corresponding to the given declaration.
7356320064d0c60fa8683f5623881c9394fd4aa7689Douglas Gregorvoid ASTContext::eraseDeclAttrs(const Decl *D) {
7366320064d0c60fa8683f5623881c9394fd4aa7689Douglas Gregor  llvm::DenseMap<const Decl*, AttrVec*>::iterator Pos = DeclAttrs.find(D);
7376320064d0c60fa8683f5623881c9394fd4aa7689Douglas Gregor  if (Pos != DeclAttrs.end()) {
7386320064d0c60fa8683f5623881c9394fd4aa7689Douglas Gregor    Pos->second->~AttrVec();
7396320064d0c60fa8683f5623881c9394fd4aa7689Douglas Gregor    DeclAttrs.erase(Pos);
7406320064d0c60fa8683f5623881c9394fd4aa7689Douglas Gregor  }
7416320064d0c60fa8683f5623881c9394fd4aa7689Douglas Gregor}
7426320064d0c60fa8683f5623881c9394fd4aa7689Douglas Gregor
743251b4ff2578e26959a4c036140ccd61c5e9292f2Douglas GregorMemberSpecializationInfo *
744663b5a0be7261c29bc4c526a71cffcfa02d4153eDouglas GregorASTContext::getInstantiatedFromStaticDataMember(const VarDecl *Var) {
7457caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  assert(Var->isStaticDataMember() && "Not a static data member");
746663b5a0be7261c29bc4c526a71cffcfa02d4153eDouglas Gregor  llvm::DenseMap<const VarDecl *, MemberSpecializationInfo *>::iterator Pos
7477caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor    = InstantiatedFromStaticDataMember.find(Var);
7487caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  if (Pos == InstantiatedFromStaticDataMember.end())
7497caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor    return 0;
7501eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
7517caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  return Pos->second;
7527caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor}
7537caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor
7541eb4433ac451dc16f4133a88af2d002ac26c58efMike Stumpvoid
755251b4ff2578e26959a4c036140ccd61c5e9292f2Douglas GregorASTContext::setInstantiatedFromStaticDataMember(VarDecl *Inst, VarDecl *Tmpl,
7569421adc43891e272156fab640e5d5ee5054b779cArgyrios Kyrtzidis                                                TemplateSpecializationKind TSK,
7579421adc43891e272156fab640e5d5ee5054b779cArgyrios Kyrtzidis                                          SourceLocation PointOfInstantiation) {
7587caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  assert(Inst->isStaticDataMember() && "Not a static data member");
7597caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  assert(Tmpl->isStaticDataMember() && "Not a static data member");
7607caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  assert(!InstantiatedFromStaticDataMember[Inst] &&
7617caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor         "Already noted what static data member was instantiated from");
762251b4ff2578e26959a4c036140ccd61c5e9292f2Douglas Gregor  InstantiatedFromStaticDataMember[Inst]
7639421adc43891e272156fab640e5d5ee5054b779cArgyrios Kyrtzidis    = new (*this) MemberSpecializationInfo(Tmpl, TSK, PointOfInstantiation);
7647caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor}
7657caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor
766af0f4d0b2e38c810effc8b024ad2fb6604eec5d3Francois PichetFunctionDecl *ASTContext::getClassScopeSpecializationPattern(
767af0f4d0b2e38c810effc8b024ad2fb6604eec5d3Francois Pichet                                                     const FunctionDecl *FD){
768af0f4d0b2e38c810effc8b024ad2fb6604eec5d3Francois Pichet  assert(FD && "Specialization is 0");
769af0f4d0b2e38c810effc8b024ad2fb6604eec5d3Francois Pichet  llvm::DenseMap<const FunctionDecl*, FunctionDecl *>::const_iterator Pos
7700d95f0d7b81110f77e99e833f766d19be7b7e072Francois Pichet    = ClassScopeSpecializationPattern.find(FD);
7710d95f0d7b81110f77e99e833f766d19be7b7e072Francois Pichet  if (Pos == ClassScopeSpecializationPattern.end())
772af0f4d0b2e38c810effc8b024ad2fb6604eec5d3Francois Pichet    return 0;
773af0f4d0b2e38c810effc8b024ad2fb6604eec5d3Francois Pichet
774af0f4d0b2e38c810effc8b024ad2fb6604eec5d3Francois Pichet  return Pos->second;
775af0f4d0b2e38c810effc8b024ad2fb6604eec5d3Francois Pichet}
776af0f4d0b2e38c810effc8b024ad2fb6604eec5d3Francois Pichet
777af0f4d0b2e38c810effc8b024ad2fb6604eec5d3Francois Pichetvoid ASTContext::setClassScopeSpecializationPattern(FunctionDecl *FD,
778af0f4d0b2e38c810effc8b024ad2fb6604eec5d3Francois Pichet                                        FunctionDecl *Pattern) {
779af0f4d0b2e38c810effc8b024ad2fb6604eec5d3Francois Pichet  assert(FD && "Specialization is 0");
780af0f4d0b2e38c810effc8b024ad2fb6604eec5d3Francois Pichet  assert(Pattern && "Class scope specialization pattern is 0");
7810d95f0d7b81110f77e99e833f766d19be7b7e072Francois Pichet  ClassScopeSpecializationPattern[FD] = Pattern;
782af0f4d0b2e38c810effc8b024ad2fb6604eec5d3Francois Pichet}
783af0f4d0b2e38c810effc8b024ad2fb6604eec5d3Francois Pichet
7847ba107a1863ddfa1664555854f0d7bdb3c491c92John McCallNamedDecl *
785ed97649e9574b9d854fa4d6109c9333ae0993554John McCallASTContext::getInstantiatedFromUsingDecl(UsingDecl *UUD) {
7867ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall  llvm::DenseMap<UsingDecl *, NamedDecl *>::const_iterator Pos
787ed97649e9574b9d854fa4d6109c9333ae0993554John McCall    = InstantiatedFromUsingDecl.find(UUD);
788ed97649e9574b9d854fa4d6109c9333ae0993554John McCall  if (Pos == InstantiatedFromUsingDecl.end())
7890d8df780aef1acda5962347a32591efc629b6748Anders Carlsson    return 0;
7901eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
7910d8df780aef1acda5962347a32591efc629b6748Anders Carlsson  return Pos->second;
7920d8df780aef1acda5962347a32591efc629b6748Anders Carlsson}
7930d8df780aef1acda5962347a32591efc629b6748Anders Carlsson
7940d8df780aef1acda5962347a32591efc629b6748Anders Carlssonvoid
795ed97649e9574b9d854fa4d6109c9333ae0993554John McCallASTContext::setInstantiatedFromUsingDecl(UsingDecl *Inst, NamedDecl *Pattern) {
796ed97649e9574b9d854fa4d6109c9333ae0993554John McCall  assert((isa<UsingDecl>(Pattern) ||
797ed97649e9574b9d854fa4d6109c9333ae0993554John McCall          isa<UnresolvedUsingValueDecl>(Pattern) ||
798ed97649e9574b9d854fa4d6109c9333ae0993554John McCall          isa<UnresolvedUsingTypenameDecl>(Pattern)) &&
799ed97649e9574b9d854fa4d6109c9333ae0993554John McCall         "pattern decl is not a using decl");
800ed97649e9574b9d854fa4d6109c9333ae0993554John McCall  assert(!InstantiatedFromUsingDecl[Inst] && "pattern already exists");
801ed97649e9574b9d854fa4d6109c9333ae0993554John McCall  InstantiatedFromUsingDecl[Inst] = Pattern;
802ed97649e9574b9d854fa4d6109c9333ae0993554John McCall}
803ed97649e9574b9d854fa4d6109c9333ae0993554John McCall
804ed97649e9574b9d854fa4d6109c9333ae0993554John McCallUsingShadowDecl *
805ed97649e9574b9d854fa4d6109c9333ae0993554John McCallASTContext::getInstantiatedFromUsingShadowDecl(UsingShadowDecl *Inst) {
806ed97649e9574b9d854fa4d6109c9333ae0993554John McCall  llvm::DenseMap<UsingShadowDecl*, UsingShadowDecl*>::const_iterator Pos
807ed97649e9574b9d854fa4d6109c9333ae0993554John McCall    = InstantiatedFromUsingShadowDecl.find(Inst);
808ed97649e9574b9d854fa4d6109c9333ae0993554John McCall  if (Pos == InstantiatedFromUsingShadowDecl.end())
809ed97649e9574b9d854fa4d6109c9333ae0993554John McCall    return 0;
810ed97649e9574b9d854fa4d6109c9333ae0993554John McCall
811ed97649e9574b9d854fa4d6109c9333ae0993554John McCall  return Pos->second;
812ed97649e9574b9d854fa4d6109c9333ae0993554John McCall}
813ed97649e9574b9d854fa4d6109c9333ae0993554John McCall
814ed97649e9574b9d854fa4d6109c9333ae0993554John McCallvoid
815ed97649e9574b9d854fa4d6109c9333ae0993554John McCallASTContext::setInstantiatedFromUsingShadowDecl(UsingShadowDecl *Inst,
816ed97649e9574b9d854fa4d6109c9333ae0993554John McCall                                               UsingShadowDecl *Pattern) {
817ed97649e9574b9d854fa4d6109c9333ae0993554John McCall  assert(!InstantiatedFromUsingShadowDecl[Inst] && "pattern already exists");
818ed97649e9574b9d854fa4d6109c9333ae0993554John McCall  InstantiatedFromUsingShadowDecl[Inst] = Pattern;
8190d8df780aef1acda5962347a32591efc629b6748Anders Carlsson}
8200d8df780aef1acda5962347a32591efc629b6748Anders Carlsson
821d8b285fee4471f393da8ee30f552ceacdc362afaAnders CarlssonFieldDecl *ASTContext::getInstantiatedFromUnnamedFieldDecl(FieldDecl *Field) {
822d8b285fee4471f393da8ee30f552ceacdc362afaAnders Carlsson  llvm::DenseMap<FieldDecl *, FieldDecl *>::iterator Pos
823d8b285fee4471f393da8ee30f552ceacdc362afaAnders Carlsson    = InstantiatedFromUnnamedFieldDecl.find(Field);
824d8b285fee4471f393da8ee30f552ceacdc362afaAnders Carlsson  if (Pos == InstantiatedFromUnnamedFieldDecl.end())
825d8b285fee4471f393da8ee30f552ceacdc362afaAnders Carlsson    return 0;
8261eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
827d8b285fee4471f393da8ee30f552ceacdc362afaAnders Carlsson  return Pos->second;
828d8b285fee4471f393da8ee30f552ceacdc362afaAnders Carlsson}
829d8b285fee4471f393da8ee30f552ceacdc362afaAnders Carlsson
830d8b285fee4471f393da8ee30f552ceacdc362afaAnders Carlssonvoid ASTContext::setInstantiatedFromUnnamedFieldDecl(FieldDecl *Inst,
831d8b285fee4471f393da8ee30f552ceacdc362afaAnders Carlsson                                                     FieldDecl *Tmpl) {
832d8b285fee4471f393da8ee30f552ceacdc362afaAnders Carlsson  assert(!Inst->getDeclName() && "Instantiated field decl is not unnamed");
833d8b285fee4471f393da8ee30f552ceacdc362afaAnders Carlsson  assert(!Tmpl->getDeclName() && "Template field decl is not unnamed");
834d8b285fee4471f393da8ee30f552ceacdc362afaAnders Carlsson  assert(!InstantiatedFromUnnamedFieldDecl[Inst] &&
835d8b285fee4471f393da8ee30f552ceacdc362afaAnders Carlsson         "Already noted what unnamed field was instantiated from");
8361eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
837d8b285fee4471f393da8ee30f552ceacdc362afaAnders Carlsson  InstantiatedFromUnnamedFieldDecl[Inst] = Tmpl;
838d8b285fee4471f393da8ee30f552ceacdc362afaAnders Carlsson}
839d8b285fee4471f393da8ee30f552ceacdc362afaAnders Carlsson
84014d56ef43ff4921c6749f7340212fbb743fdbb9bFariborz Jahanianbool ASTContext::ZeroBitfieldFollowsNonBitfield(const FieldDecl *FD,
84114d56ef43ff4921c6749f7340212fbb743fdbb9bFariborz Jahanian                                    const FieldDecl *LastFD) const {
84214d56ef43ff4921c6749f7340212fbb743fdbb9bFariborz Jahanian  return (FD->isBitField() && LastFD && !LastFD->isBitField() &&
843a6b8b2c09610b8bc4330e948ece8b940c2386406Richard Smith          FD->getBitWidthValue(*this) == 0);
84414d56ef43ff4921c6749f7340212fbb743fdbb9bFariborz Jahanian}
84514d56ef43ff4921c6749f7340212fbb743fdbb9bFariborz Jahanian
846340fa242130c2d8d74c83edca0952e771aebe0e6Fariborz Jahanianbool ASTContext::ZeroBitfieldFollowsBitfield(const FieldDecl *FD,
847340fa242130c2d8d74c83edca0952e771aebe0e6Fariborz Jahanian                                             const FieldDecl *LastFD) const {
848340fa242130c2d8d74c83edca0952e771aebe0e6Fariborz Jahanian  return (FD->isBitField() && LastFD && LastFD->isBitField() &&
849a6b8b2c09610b8bc4330e948ece8b940c2386406Richard Smith          FD->getBitWidthValue(*this) == 0 &&
850a6b8b2c09610b8bc4330e948ece8b940c2386406Richard Smith          LastFD->getBitWidthValue(*this) != 0);
851340fa242130c2d8d74c83edca0952e771aebe0e6Fariborz Jahanian}
852340fa242130c2d8d74c83edca0952e771aebe0e6Fariborz Jahanian
8539b3acaa32548d0ce78b9c39a3911397f6738a47cFariborz Jahanianbool ASTContext::BitfieldFollowsBitfield(const FieldDecl *FD,
8549b3acaa32548d0ce78b9c39a3911397f6738a47cFariborz Jahanian                                         const FieldDecl *LastFD) const {
8559b3acaa32548d0ce78b9c39a3911397f6738a47cFariborz Jahanian  return (FD->isBitField() && LastFD && LastFD->isBitField() &&
856a6b8b2c09610b8bc4330e948ece8b940c2386406Richard Smith          FD->getBitWidthValue(*this) &&
857a6b8b2c09610b8bc4330e948ece8b940c2386406Richard Smith          LastFD->getBitWidthValue(*this));
8589b3acaa32548d0ce78b9c39a3911397f6738a47cFariborz Jahanian}
8599b3acaa32548d0ce78b9c39a3911397f6738a47cFariborz Jahanian
860dd7fddb5b6883326e52b278a9b7e9cefea29aae0Chad Rosierbool ASTContext::NonBitfieldFollowsBitfield(const FieldDecl *FD,
86152bbe7a1133c3cb57e9246f1b96c12940ea3821aFariborz Jahanian                                         const FieldDecl *LastFD) const {
86252bbe7a1133c3cb57e9246f1b96c12940ea3821aFariborz Jahanian  return (!FD->isBitField() && LastFD && LastFD->isBitField() &&
863a6b8b2c09610b8bc4330e948ece8b940c2386406Richard Smith          LastFD->getBitWidthValue(*this));
86452bbe7a1133c3cb57e9246f1b96c12940ea3821aFariborz Jahanian}
86552bbe7a1133c3cb57e9246f1b96c12940ea3821aFariborz Jahanian
866dd7fddb5b6883326e52b278a9b7e9cefea29aae0Chad Rosierbool ASTContext::BitfieldFollowsNonBitfield(const FieldDecl *FD,
86752bbe7a1133c3cb57e9246f1b96c12940ea3821aFariborz Jahanian                                             const FieldDecl *LastFD) const {
86852bbe7a1133c3cb57e9246f1b96c12940ea3821aFariborz Jahanian  return (FD->isBitField() && LastFD && !LastFD->isBitField() &&
869a6b8b2c09610b8bc4330e948ece8b940c2386406Richard Smith          FD->getBitWidthValue(*this));
87052bbe7a1133c3cb57e9246f1b96c12940ea3821aFariborz Jahanian}
87152bbe7a1133c3cb57e9246f1b96c12940ea3821aFariborz Jahanian
8727d10b7eb670b821741b4c96f6cf7afbc3bb39abeDouglas GregorASTContext::overridden_cxx_method_iterator
8737d10b7eb670b821741b4c96f6cf7afbc3bb39abeDouglas GregorASTContext::overridden_methods_begin(const CXXMethodDecl *Method) const {
8747d10b7eb670b821741b4c96f6cf7afbc3bb39abeDouglas Gregor  llvm::DenseMap<const CXXMethodDecl *, CXXMethodVector>::const_iterator Pos
8757d10b7eb670b821741b4c96f6cf7afbc3bb39abeDouglas Gregor    = OverriddenMethods.find(Method);
8767d10b7eb670b821741b4c96f6cf7afbc3bb39abeDouglas Gregor  if (Pos == OverriddenMethods.end())
8777d10b7eb670b821741b4c96f6cf7afbc3bb39abeDouglas Gregor    return 0;
8787d10b7eb670b821741b4c96f6cf7afbc3bb39abeDouglas Gregor
8797d10b7eb670b821741b4c96f6cf7afbc3bb39abeDouglas Gregor  return Pos->second.begin();
8807d10b7eb670b821741b4c96f6cf7afbc3bb39abeDouglas Gregor}
8817d10b7eb670b821741b4c96f6cf7afbc3bb39abeDouglas Gregor
8827d10b7eb670b821741b4c96f6cf7afbc3bb39abeDouglas GregorASTContext::overridden_cxx_method_iterator
8837d10b7eb670b821741b4c96f6cf7afbc3bb39abeDouglas GregorASTContext::overridden_methods_end(const CXXMethodDecl *Method) const {
8847d10b7eb670b821741b4c96f6cf7afbc3bb39abeDouglas Gregor  llvm::DenseMap<const CXXMethodDecl *, CXXMethodVector>::const_iterator Pos
8857d10b7eb670b821741b4c96f6cf7afbc3bb39abeDouglas Gregor    = OverriddenMethods.find(Method);
8867d10b7eb670b821741b4c96f6cf7afbc3bb39abeDouglas Gregor  if (Pos == OverriddenMethods.end())
8877d10b7eb670b821741b4c96f6cf7afbc3bb39abeDouglas Gregor    return 0;
8887d10b7eb670b821741b4c96f6cf7afbc3bb39abeDouglas Gregor
8897d10b7eb670b821741b4c96f6cf7afbc3bb39abeDouglas Gregor  return Pos->second.end();
8907d10b7eb670b821741b4c96f6cf7afbc3bb39abeDouglas Gregor}
8917d10b7eb670b821741b4c96f6cf7afbc3bb39abeDouglas Gregor
892c91e9f439ae85d5f79a6b65672f1d7d1b55ccda0Argyrios Kyrtzidisunsigned
893c91e9f439ae85d5f79a6b65672f1d7d1b55ccda0Argyrios KyrtzidisASTContext::overridden_methods_size(const CXXMethodDecl *Method) const {
894c91e9f439ae85d5f79a6b65672f1d7d1b55ccda0Argyrios Kyrtzidis  llvm::DenseMap<const CXXMethodDecl *, CXXMethodVector>::const_iterator Pos
895c91e9f439ae85d5f79a6b65672f1d7d1b55ccda0Argyrios Kyrtzidis    = OverriddenMethods.find(Method);
896c91e9f439ae85d5f79a6b65672f1d7d1b55ccda0Argyrios Kyrtzidis  if (Pos == OverriddenMethods.end())
897c91e9f439ae85d5f79a6b65672f1d7d1b55ccda0Argyrios Kyrtzidis    return 0;
898c91e9f439ae85d5f79a6b65672f1d7d1b55ccda0Argyrios Kyrtzidis
899c91e9f439ae85d5f79a6b65672f1d7d1b55ccda0Argyrios Kyrtzidis  return Pos->second.size();
900c91e9f439ae85d5f79a6b65672f1d7d1b55ccda0Argyrios Kyrtzidis}
901c91e9f439ae85d5f79a6b65672f1d7d1b55ccda0Argyrios Kyrtzidis
9027d10b7eb670b821741b4c96f6cf7afbc3bb39abeDouglas Gregorvoid ASTContext::addOverriddenMethod(const CXXMethodDecl *Method,
9037d10b7eb670b821741b4c96f6cf7afbc3bb39abeDouglas Gregor                                     const CXXMethodDecl *Overridden) {
9047d10b7eb670b821741b4c96f6cf7afbc3bb39abeDouglas Gregor  OverriddenMethods[Method].push_back(Overridden);
9057d10b7eb670b821741b4c96f6cf7afbc3bb39abeDouglas Gregor}
9067d10b7eb670b821741b4c96f6cf7afbc3bb39abeDouglas Gregor
907e664977aca2a05a77abab5a06dc0fb69e870cfb9Douglas Gregorvoid ASTContext::addedLocalImportDecl(ImportDecl *Import) {
908e664977aca2a05a77abab5a06dc0fb69e870cfb9Douglas Gregor  assert(!Import->NextLocalImport && "Import declaration already in the chain");
909e664977aca2a05a77abab5a06dc0fb69e870cfb9Douglas Gregor  assert(!Import->isFromASTFile() && "Non-local import declaration");
910e664977aca2a05a77abab5a06dc0fb69e870cfb9Douglas Gregor  if (!FirstLocalImport) {
911e664977aca2a05a77abab5a06dc0fb69e870cfb9Douglas Gregor    FirstLocalImport = Import;
912e664977aca2a05a77abab5a06dc0fb69e870cfb9Douglas Gregor    LastLocalImport = Import;
913e664977aca2a05a77abab5a06dc0fb69e870cfb9Douglas Gregor    return;
914e664977aca2a05a77abab5a06dc0fb69e870cfb9Douglas Gregor  }
915e664977aca2a05a77abab5a06dc0fb69e870cfb9Douglas Gregor
916e664977aca2a05a77abab5a06dc0fb69e870cfb9Douglas Gregor  LastLocalImport->NextLocalImport = Import;
917e664977aca2a05a77abab5a06dc0fb69e870cfb9Douglas Gregor  LastLocalImport = Import;
918e664977aca2a05a77abab5a06dc0fb69e870cfb9Douglas Gregor}
919e664977aca2a05a77abab5a06dc0fb69e870cfb9Douglas Gregor
920464175bba1318bef7905122e9fda20cff926df78Chris Lattner//===----------------------------------------------------------------------===//
921464175bba1318bef7905122e9fda20cff926df78Chris Lattner//                         Type Sizing and Analysis
922464175bba1318bef7905122e9fda20cff926df78Chris Lattner//===----------------------------------------------------------------------===//
923a7674d8a9a69f3f6fe16e70cf2a3b2b15fb7c43dChris Lattner
924b7cfe88e88cb4f46308de89cf3f0c81bfe624128Chris Lattner/// getFloatTypeSemantics - Return the APFloat 'semantics' for the specified
925b7cfe88e88cb4f46308de89cf3f0c81bfe624128Chris Lattner/// scalar floating point type.
926b7cfe88e88cb4f46308de89cf3f0c81bfe624128Chris Lattnerconst llvm::fltSemantics &ASTContext::getFloatTypeSemantics(QualType T) const {
927183700f494ec9b6701b6efe82bcb25f4c79ba561John McCall  const BuiltinType *BT = T->getAs<BuiltinType>();
928b7cfe88e88cb4f46308de89cf3f0c81bfe624128Chris Lattner  assert(BT && "Not a floating point type!");
929b7cfe88e88cb4f46308de89cf3f0c81bfe624128Chris Lattner  switch (BT->getKind()) {
930b219cfc4d75f0a03630b7c4509ef791b7e97b2c8David Blaikie  default: llvm_unreachable("Not a floating point type!");
931aa4a99b4a62615db243f7a5c433169f2fc704420Anton Korobeynikov  case BuiltinType::Half:       return Target->getHalfFormat();
932bcfd1f55bfbb3e5944cd5e03d07b343e280838c4Douglas Gregor  case BuiltinType::Float:      return Target->getFloatFormat();
933bcfd1f55bfbb3e5944cd5e03d07b343e280838c4Douglas Gregor  case BuiltinType::Double:     return Target->getDoubleFormat();
934bcfd1f55bfbb3e5944cd5e03d07b343e280838c4Douglas Gregor  case BuiltinType::LongDouble: return Target->getLongDoubleFormat();
935b7cfe88e88cb4f46308de89cf3f0c81bfe624128Chris Lattner  }
936b7cfe88e88cb4f46308de89cf3f0c81bfe624128Chris Lattner}
937b7cfe88e88cb4f46308de89cf3f0c81bfe624128Chris Lattner
9388b752f10c394b140f9ef89e049cbad1a7676fc25Ken Dyck/// getDeclAlign - Return a conservative estimate of the alignment of the
939af707ab8fbb9451e8febb8d766f6c043628125c4Chris Lattner/// specified decl.  Note that bitfields do not have a valid alignment, so
940af707ab8fbb9451e8febb8d766f6c043628125c4Chris Lattner/// this method will assert on them.
9415d484e8cf710207010720589d89602233de61d01Sebastian Redl/// If @p RefAsPointee, references are treated like their underlying type
9425d484e8cf710207010720589d89602233de61d01Sebastian Redl/// (for alignof), else they're treated like pointers (for CodeGen).
9434ba2a17694148e16eaa8d3917f657ffcd3667be4Jay FoadCharUnits ASTContext::getDeclAlign(const Decl *D, bool RefAsPointee) const {
944bcfd1f55bfbb3e5944cd5e03d07b343e280838c4Douglas Gregor  unsigned Align = Target->getCharWidth();
945dcdafb6a701aa9d81edcb088915f58933315dc05Eli Friedman
9464081a5c5f1381c4ec77f8ab3866693917e4329c4John McCall  bool UseAlignAttrOnly = false;
9474081a5c5f1381c4ec77f8ab3866693917e4329c4John McCall  if (unsigned AlignFromAttr = D->getMaxAlignment()) {
9484081a5c5f1381c4ec77f8ab3866693917e4329c4John McCall    Align = AlignFromAttr;
9494081a5c5f1381c4ec77f8ab3866693917e4329c4John McCall
9504081a5c5f1381c4ec77f8ab3866693917e4329c4John McCall    // __attribute__((aligned)) can increase or decrease alignment
9514081a5c5f1381c4ec77f8ab3866693917e4329c4John McCall    // *except* on a struct or struct member, where it only increases
9524081a5c5f1381c4ec77f8ab3866693917e4329c4John McCall    // alignment unless 'packed' is also specified.
9534081a5c5f1381c4ec77f8ab3866693917e4329c4John McCall    //
95482d0b0aab9088e977c2a44c4a5a90479c63149fePeter Collingbourne    // It is an error for alignas to decrease alignment, so we can
9554081a5c5f1381c4ec77f8ab3866693917e4329c4John McCall    // ignore that possibility;  Sema should diagnose it.
9564081a5c5f1381c4ec77f8ab3866693917e4329c4John McCall    if (isa<FieldDecl>(D)) {
9574081a5c5f1381c4ec77f8ab3866693917e4329c4John McCall      UseAlignAttrOnly = D->hasAttr<PackedAttr>() ||
9584081a5c5f1381c4ec77f8ab3866693917e4329c4John McCall        cast<FieldDecl>(D)->getParent()->hasAttr<PackedAttr>();
9594081a5c5f1381c4ec77f8ab3866693917e4329c4John McCall    } else {
9604081a5c5f1381c4ec77f8ab3866693917e4329c4John McCall      UseAlignAttrOnly = true;
9614081a5c5f1381c4ec77f8ab3866693917e4329c4John McCall    }
9624081a5c5f1381c4ec77f8ab3866693917e4329c4John McCall  }
96378a7d7d79964119a3f35b262eb154b5cbf1001edFariborz Jahanian  else if (isa<FieldDecl>(D))
96478a7d7d79964119a3f35b262eb154b5cbf1001edFariborz Jahanian      UseAlignAttrOnly =
96578a7d7d79964119a3f35b262eb154b5cbf1001edFariborz Jahanian        D->hasAttr<PackedAttr>() ||
96678a7d7d79964119a3f35b262eb154b5cbf1001edFariborz Jahanian        cast<FieldDecl>(D)->getParent()->hasAttr<PackedAttr>();
967dcdafb6a701aa9d81edcb088915f58933315dc05Eli Friedman
968ba4f5d5754c8291690d01ca9581926673d69b24cJohn McCall  // If we're using the align attribute only, just ignore everything
969ba4f5d5754c8291690d01ca9581926673d69b24cJohn McCall  // else about the declaration and its type.
9704081a5c5f1381c4ec77f8ab3866693917e4329c4John McCall  if (UseAlignAttrOnly) {
971ba4f5d5754c8291690d01ca9581926673d69b24cJohn McCall    // do nothing
972ba4f5d5754c8291690d01ca9581926673d69b24cJohn McCall
9734081a5c5f1381c4ec77f8ab3866693917e4329c4John McCall  } else if (const ValueDecl *VD = dyn_cast<ValueDecl>(D)) {
974af707ab8fbb9451e8febb8d766f6c043628125c4Chris Lattner    QualType T = VD->getType();
9756217b80b7a1379b74cced1c076338262c3c980b3Ted Kremenek    if (const ReferenceType* RT = T->getAs<ReferenceType>()) {
9765d484e8cf710207010720589d89602233de61d01Sebastian Redl      if (RefAsPointee)
9775d484e8cf710207010720589d89602233de61d01Sebastian Redl        T = RT->getPointeeType();
9785d484e8cf710207010720589d89602233de61d01Sebastian Redl      else
9795d484e8cf710207010720589d89602233de61d01Sebastian Redl        T = getPointerType(RT->getPointeeType());
9805d484e8cf710207010720589d89602233de61d01Sebastian Redl    }
9815d484e8cf710207010720589d89602233de61d01Sebastian Redl    if (!T->isIncompleteType() && !T->isFunctionType()) {
9823b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall      // Adjust alignments of declarations with array type by the
9833b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall      // large-array alignment on the target.
984bcfd1f55bfbb3e5944cd5e03d07b343e280838c4Douglas Gregor      unsigned MinWidth = Target->getLargeArrayMinWidth();
9853b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall      const ArrayType *arrayType;
9863b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall      if (MinWidth && (arrayType = getAsArrayType(T))) {
9873b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall        if (isa<VariableArrayType>(arrayType))
988bcfd1f55bfbb3e5944cd5e03d07b343e280838c4Douglas Gregor          Align = std::max(Align, Target->getLargeArrayAlign());
9893b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall        else if (isa<ConstantArrayType>(arrayType) &&
9903b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall                 MinWidth <= getTypeSize(cast<ConstantArrayType>(arrayType)))
991bcfd1f55bfbb3e5944cd5e03d07b343e280838c4Douglas Gregor          Align = std::max(Align, Target->getLargeArrayAlign());
9923b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall
9933b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall        // Walk through any array types while we're at it.
9943b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall        T = getBaseElementType(arrayType);
9956deecb0d46bcfd048e651d2db7c4fb0d6407da96Rafael Espindola      }
9969f1210c3280104417a4ad30f0a00825ac8fa718aChad Rosier      Align = std::max(Align, getPreferredTypeAlign(T.getTypePtr()));
997dcdafb6a701aa9d81edcb088915f58933315dc05Eli Friedman    }
998ba4f5d5754c8291690d01ca9581926673d69b24cJohn McCall
999ba4f5d5754c8291690d01ca9581926673d69b24cJohn McCall    // Fields can be subject to extra alignment constraints, like if
1000ba4f5d5754c8291690d01ca9581926673d69b24cJohn McCall    // the field is packed, the struct is packed, or the struct has a
1001ba4f5d5754c8291690d01ca9581926673d69b24cJohn McCall    // a max-field-alignment constraint (#pragma pack).  So calculate
1002ba4f5d5754c8291690d01ca9581926673d69b24cJohn McCall    // the actual alignment of the field within the struct, and then
1003ba4f5d5754c8291690d01ca9581926673d69b24cJohn McCall    // (as we're expected to) constrain that by the alignment of the type.
1004ba4f5d5754c8291690d01ca9581926673d69b24cJohn McCall    if (const FieldDecl *field = dyn_cast<FieldDecl>(VD)) {
1005ba4f5d5754c8291690d01ca9581926673d69b24cJohn McCall      // So calculate the alignment of the field.
1006ba4f5d5754c8291690d01ca9581926673d69b24cJohn McCall      const ASTRecordLayout &layout = getASTRecordLayout(field->getParent());
1007ba4f5d5754c8291690d01ca9581926673d69b24cJohn McCall
1008ba4f5d5754c8291690d01ca9581926673d69b24cJohn McCall      // Start with the record's overall alignment.
1009dac54c124e302d6f028ea5723c425b7f66fc7c71Ken Dyck      unsigned fieldAlign = toBits(layout.getAlignment());
1010ba4f5d5754c8291690d01ca9581926673d69b24cJohn McCall
1011ba4f5d5754c8291690d01ca9581926673d69b24cJohn McCall      // Use the GCD of that and the offset within the record.
1012ba4f5d5754c8291690d01ca9581926673d69b24cJohn McCall      uint64_t offset = layout.getFieldOffset(field->getFieldIndex());
1013ba4f5d5754c8291690d01ca9581926673d69b24cJohn McCall      if (offset > 0) {
1014ba4f5d5754c8291690d01ca9581926673d69b24cJohn McCall        // Alignment is always a power of 2, so the GCD will be a power of 2,
1015ba4f5d5754c8291690d01ca9581926673d69b24cJohn McCall        // which means we get to do this crazy thing instead of Euclid's.
1016ba4f5d5754c8291690d01ca9581926673d69b24cJohn McCall        uint64_t lowBitOfOffset = offset & (~offset + 1);
1017ba4f5d5754c8291690d01ca9581926673d69b24cJohn McCall        if (lowBitOfOffset < fieldAlign)
1018ba4f5d5754c8291690d01ca9581926673d69b24cJohn McCall          fieldAlign = static_cast<unsigned>(lowBitOfOffset);
1019ba4f5d5754c8291690d01ca9581926673d69b24cJohn McCall      }
1020ba4f5d5754c8291690d01ca9581926673d69b24cJohn McCall
1021ba4f5d5754c8291690d01ca9581926673d69b24cJohn McCall      Align = std::min(Align, fieldAlign);
102205f62474dd2b0f1cb69adbe0787f2868788aa949Charles Davis    }
1023af707ab8fbb9451e8febb8d766f6c043628125c4Chris Lattner  }
1024dcdafb6a701aa9d81edcb088915f58933315dc05Eli Friedman
1025eb6f5dc86531f794ba7746a2da4d28e37cf5da7eKen Dyck  return toCharUnitsFromBits(Align);
1026af707ab8fbb9451e8febb8d766f6c043628125c4Chris Lattner}
1027b7cfe88e88cb4f46308de89cf3f0c81bfe624128Chris Lattner
1028ea1471e0e967548c596a71469702f8846dbaf3c0John McCallstd::pair<CharUnits, CharUnits>
1029bee5a79fc95e3003d755031e3d2bb4410a71e1c1Ken DyckASTContext::getTypeInfoInChars(const Type *T) const {
1030ea1471e0e967548c596a71469702f8846dbaf3c0John McCall  std::pair<uint64_t, unsigned> Info = getTypeInfo(T);
1031eb6f5dc86531f794ba7746a2da4d28e37cf5da7eKen Dyck  return std::make_pair(toCharUnitsFromBits(Info.first),
1032eb6f5dc86531f794ba7746a2da4d28e37cf5da7eKen Dyck                        toCharUnitsFromBits(Info.second));
1033ea1471e0e967548c596a71469702f8846dbaf3c0John McCall}
1034ea1471e0e967548c596a71469702f8846dbaf3c0John McCall
1035ea1471e0e967548c596a71469702f8846dbaf3c0John McCallstd::pair<CharUnits, CharUnits>
1036bee5a79fc95e3003d755031e3d2bb4410a71e1c1Ken DyckASTContext::getTypeInfoInChars(QualType T) const {
1037ea1471e0e967548c596a71469702f8846dbaf3c0John McCall  return getTypeInfoInChars(T.getTypePtr());
1038ea1471e0e967548c596a71469702f8846dbaf3c0John McCall}
1039ea1471e0e967548c596a71469702f8846dbaf3c0John McCall
1040bc5419a2edc4030d1a623576fe339fbd3eed17a6Daniel Dunbarstd::pair<uint64_t, unsigned> ASTContext::getTypeInfo(const Type *T) const {
1041bc5419a2edc4030d1a623576fe339fbd3eed17a6Daniel Dunbar  TypeInfoMap::iterator it = MemoizedTypeInfo.find(T);
1042bc5419a2edc4030d1a623576fe339fbd3eed17a6Daniel Dunbar  if (it != MemoizedTypeInfo.end())
1043bc5419a2edc4030d1a623576fe339fbd3eed17a6Daniel Dunbar    return it->second;
1044bc5419a2edc4030d1a623576fe339fbd3eed17a6Daniel Dunbar
1045bc5419a2edc4030d1a623576fe339fbd3eed17a6Daniel Dunbar  std::pair<uint64_t, unsigned> Info = getTypeInfoImpl(T);
1046bc5419a2edc4030d1a623576fe339fbd3eed17a6Daniel Dunbar  MemoizedTypeInfo.insert(std::make_pair(T, Info));
1047bc5419a2edc4030d1a623576fe339fbd3eed17a6Daniel Dunbar  return Info;
1048bc5419a2edc4030d1a623576fe339fbd3eed17a6Daniel Dunbar}
1049bc5419a2edc4030d1a623576fe339fbd3eed17a6Daniel Dunbar
1050bc5419a2edc4030d1a623576fe339fbd3eed17a6Daniel Dunbar/// getTypeInfoImpl - Return the size of the specified type, in bits.  This
1051bc5419a2edc4030d1a623576fe339fbd3eed17a6Daniel Dunbar/// method does not work on incomplete types.
10520953e767ff7817f97b3ab20896b229891eeff45bJohn McCall///
10530953e767ff7817f97b3ab20896b229891eeff45bJohn McCall/// FIXME: Pointers into different addr spaces could have different sizes and
10540953e767ff7817f97b3ab20896b229891eeff45bJohn McCall/// alignment requirements: getPointerInfo should take an AddrSpace, this
10550953e767ff7817f97b3ab20896b229891eeff45bJohn McCall/// should take a QualType, &c.
1056d2d2a11a91d7ddf468bfb70f66362d24806ed601Chris Lattnerstd::pair<uint64_t, unsigned>
1057bc5419a2edc4030d1a623576fe339fbd3eed17a6Daniel DunbarASTContext::getTypeInfoImpl(const Type *T) const {
10585e301007e31e14c8ff647288e1b8bd8dbf8a5fe4Mike Stump  uint64_t Width=0;
10595e301007e31e14c8ff647288e1b8bd8dbf8a5fe4Mike Stump  unsigned Align=8;
1060a7674d8a9a69f3f6fe16e70cf2a3b2b15fb7c43dChris Lattner  switch (T->getTypeClass()) {
106172564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor#define TYPE(Class, Base)
106272564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor#define ABSTRACT_TYPE(Class, Base)
106318857644059c45da6776f1a288eec7b4cf3a844aDouglas Gregor#define NON_CANONICAL_TYPE(Class, Base)
106472564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor#define DEPENDENT_TYPE(Class, Base) case Type::Class:
106572564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor#include "clang/AST/TypeNodes.def"
1066d3d49bb27c7ffd9accc0a6c00e887111c0348845John McCall    llvm_unreachable("Should not see dependent types");
106772564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor
10685d2a6303467184b1f159bb6556efc434e50e3c28Chris Lattner  case Type::FunctionNoProto:
10695d2a6303467184b1f159bb6556efc434e50e3c28Chris Lattner  case Type::FunctionProto:
107018857644059c45da6776f1a288eec7b4cf3a844aDouglas Gregor    // GCC extension: alignof(function) = 32 bits
107118857644059c45da6776f1a288eec7b4cf3a844aDouglas Gregor    Width = 0;
107218857644059c45da6776f1a288eec7b4cf3a844aDouglas Gregor    Align = 32;
107318857644059c45da6776f1a288eec7b4cf3a844aDouglas Gregor    break;
107418857644059c45da6776f1a288eec7b4cf3a844aDouglas Gregor
107572564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor  case Type::IncompleteArray:
1076fb22d96692c5240fb8d611290dbf7eeed3759c73Steve Naroff  case Type::VariableArray:
107718857644059c45da6776f1a288eec7b4cf3a844aDouglas Gregor    Width = 0;
107818857644059c45da6776f1a288eec7b4cf3a844aDouglas Gregor    Align = getTypeAlign(cast<ArrayType>(T)->getElementType());
107918857644059c45da6776f1a288eec7b4cf3a844aDouglas Gregor    break;
108018857644059c45da6776f1a288eec7b4cf3a844aDouglas Gregor
1081fb22d96692c5240fb8d611290dbf7eeed3759c73Steve Naroff  case Type::ConstantArray: {
10821d75118af76cae2bfc06389cde410e14bd0a19fcDaniel Dunbar    const ConstantArrayType *CAT = cast<ConstantArrayType>(T);
10831eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
108498be4943e8dc4f3905629a7102668960873cf863Chris Lattner    std::pair<uint64_t, unsigned> EltInfo = getTypeInfo(CAT->getElementType());
1085fea966a4103ed9c018d1494b95e9d09b161f5a70Abramo Bagnara    uint64_t Size = CAT->getSize().getZExtValue();
1086bc5419a2edc4030d1a623576fe339fbd3eed17a6Daniel Dunbar    assert((Size == 0 || EltInfo.first <= (uint64_t)(-1)/Size) &&
1087bc5419a2edc4030d1a623576fe339fbd3eed17a6Daniel Dunbar           "Overflow in array type bit size evaluation");
1088fea966a4103ed9c018d1494b95e9d09b161f5a70Abramo Bagnara    Width = EltInfo.first*Size;
1089030d8846c7e520330007087e949f621989876e3aChris Lattner    Align = EltInfo.second;
1090cd88b4171753dcb2bc0a21d78f1597c796bb8a20Argyrios Kyrtzidis    Width = llvm::RoundUpToAlignment(Width, Align);
1091030d8846c7e520330007087e949f621989876e3aChris Lattner    break;
10925c09a02a5db85e08a432b6eeced9aa656349710dChristopher Lamb  }
1093213541a68a3e137d11d2cefb612c6cdb410d7e8eNate Begeman  case Type::ExtVector:
1094030d8846c7e520330007087e949f621989876e3aChris Lattner  case Type::Vector: {
10959fcfe926432f3c3f7e9a61219e55c352fd358e45Chris Lattner    const VectorType *VT = cast<VectorType>(T);
10969fcfe926432f3c3f7e9a61219e55c352fd358e45Chris Lattner    std::pair<uint64_t, unsigned> EltInfo = getTypeInfo(VT->getElementType());
10979fcfe926432f3c3f7e9a61219e55c352fd358e45Chris Lattner    Width = EltInfo.first*VT->getNumElements();
10984bd998bbc228915d2b9cae5b67879de48940d05eEli Friedman    Align = Width;
10996fe7c8aa8c7546743ecd0ac0138c2cf5d8155386Nate Begeman    // If the alignment is not a power of 2, round up to the next power of 2.
11006fe7c8aa8c7546743ecd0ac0138c2cf5d8155386Nate Begeman    // This happens for non-power-of-2 length vectors.
11018eefcd353c1d06a10104f69e5079ebab3183f9a3Dan Gohman    if (Align & (Align-1)) {
11029fcfe926432f3c3f7e9a61219e55c352fd358e45Chris Lattner      Align = llvm::NextPowerOf2(Align);
11039fcfe926432f3c3f7e9a61219e55c352fd358e45Chris Lattner      Width = llvm::RoundUpToAlignment(Width, Align);
11049fcfe926432f3c3f7e9a61219e55c352fd358e45Chris Lattner    }
1105f9e9af7df0cea6e997ac04131c7af6ca4384b0ccChad Rosier    // Adjust the alignment based on the target max.
1106f9e9af7df0cea6e997ac04131c7af6ca4384b0ccChad Rosier    uint64_t TargetVectorAlign = Target->getMaxVectorAlign();
1107f9e9af7df0cea6e997ac04131c7af6ca4384b0ccChad Rosier    if (TargetVectorAlign && TargetVectorAlign < Align)
1108f9e9af7df0cea6e997ac04131c7af6ca4384b0ccChad Rosier      Align = TargetVectorAlign;
1109030d8846c7e520330007087e949f621989876e3aChris Lattner    break;
1110030d8846c7e520330007087e949f621989876e3aChris Lattner  }
11115d2a6303467184b1f159bb6556efc434e50e3c28Chris Lattner
11129e9b6dc3fd413f5341fab54b681420eeb21cd169Chris Lattner  case Type::Builtin:
1113a7674d8a9a69f3f6fe16e70cf2a3b2b15fb7c43dChris Lattner    switch (cast<BuiltinType>(T)->getKind()) {
1114b219cfc4d75f0a03630b7c4509ef791b7e97b2c8David Blaikie    default: llvm_unreachable("Unknown builtin type!");
1115d2d2a11a91d7ddf468bfb70f66362d24806ed601Chris Lattner    case BuiltinType::Void:
111618857644059c45da6776f1a288eec7b4cf3a844aDouglas Gregor      // GCC extension: alignof(void) = 8 bits.
111718857644059c45da6776f1a288eec7b4cf3a844aDouglas Gregor      Width = 0;
111818857644059c45da6776f1a288eec7b4cf3a844aDouglas Gregor      Align = 8;
111918857644059c45da6776f1a288eec7b4cf3a844aDouglas Gregor      break;
112018857644059c45da6776f1a288eec7b4cf3a844aDouglas Gregor
11216f62c2abd8077bf70d2166d37e8caa426b34d8e4Chris Lattner    case BuiltinType::Bool:
1122bcfd1f55bfbb3e5944cd5e03d07b343e280838c4Douglas Gregor      Width = Target->getBoolWidth();
1123bcfd1f55bfbb3e5944cd5e03d07b343e280838c4Douglas Gregor      Align = Target->getBoolAlign();
11246f62c2abd8077bf70d2166d37e8caa426b34d8e4Chris Lattner      break;
1125692233e90a99c3a81dd04879d36eb9688f137c44Chris Lattner    case BuiltinType::Char_S:
1126692233e90a99c3a81dd04879d36eb9688f137c44Chris Lattner    case BuiltinType::Char_U:
1127692233e90a99c3a81dd04879d36eb9688f137c44Chris Lattner    case BuiltinType::UChar:
11286f62c2abd8077bf70d2166d37e8caa426b34d8e4Chris Lattner    case BuiltinType::SChar:
1129bcfd1f55bfbb3e5944cd5e03d07b343e280838c4Douglas Gregor      Width = Target->getCharWidth();
1130bcfd1f55bfbb3e5944cd5e03d07b343e280838c4Douglas Gregor      Align = Target->getCharAlign();
11316f62c2abd8077bf70d2166d37e8caa426b34d8e4Chris Lattner      break;
11323f59c975aa5d047f7edd1b900b5e885c38af0ef7Chris Lattner    case BuiltinType::WChar_S:
11333f59c975aa5d047f7edd1b900b5e885c38af0ef7Chris Lattner    case BuiltinType::WChar_U:
1134bcfd1f55bfbb3e5944cd5e03d07b343e280838c4Douglas Gregor      Width = Target->getWCharWidth();
1135bcfd1f55bfbb3e5944cd5e03d07b343e280838c4Douglas Gregor      Align = Target->getWCharAlign();
113664c438a4be2a871fa43c78264663ba1e9788b94dArgyrios Kyrtzidis      break;
1137f5c209d23b20ada4a9b6235db50317239cbf6ae1Alisdair Meredith    case BuiltinType::Char16:
1138bcfd1f55bfbb3e5944cd5e03d07b343e280838c4Douglas Gregor      Width = Target->getChar16Width();
1139bcfd1f55bfbb3e5944cd5e03d07b343e280838c4Douglas Gregor      Align = Target->getChar16Align();
1140f5c209d23b20ada4a9b6235db50317239cbf6ae1Alisdair Meredith      break;
1141f5c209d23b20ada4a9b6235db50317239cbf6ae1Alisdair Meredith    case BuiltinType::Char32:
1142bcfd1f55bfbb3e5944cd5e03d07b343e280838c4Douglas Gregor      Width = Target->getChar32Width();
1143bcfd1f55bfbb3e5944cd5e03d07b343e280838c4Douglas Gregor      Align = Target->getChar32Align();
1144f5c209d23b20ada4a9b6235db50317239cbf6ae1Alisdair Meredith      break;
1145692233e90a99c3a81dd04879d36eb9688f137c44Chris Lattner    case BuiltinType::UShort:
11466f62c2abd8077bf70d2166d37e8caa426b34d8e4Chris Lattner    case BuiltinType::Short:
1147bcfd1f55bfbb3e5944cd5e03d07b343e280838c4Douglas Gregor      Width = Target->getShortWidth();
1148bcfd1f55bfbb3e5944cd5e03d07b343e280838c4Douglas Gregor      Align = Target->getShortAlign();
11496f62c2abd8077bf70d2166d37e8caa426b34d8e4Chris Lattner      break;
1150692233e90a99c3a81dd04879d36eb9688f137c44Chris Lattner    case BuiltinType::UInt:
11516f62c2abd8077bf70d2166d37e8caa426b34d8e4Chris Lattner    case BuiltinType::Int:
1152bcfd1f55bfbb3e5944cd5e03d07b343e280838c4Douglas Gregor      Width = Target->getIntWidth();
1153bcfd1f55bfbb3e5944cd5e03d07b343e280838c4Douglas Gregor      Align = Target->getIntAlign();
11546f62c2abd8077bf70d2166d37e8caa426b34d8e4Chris Lattner      break;
1155692233e90a99c3a81dd04879d36eb9688f137c44Chris Lattner    case BuiltinType::ULong:
11566f62c2abd8077bf70d2166d37e8caa426b34d8e4Chris Lattner    case BuiltinType::Long:
1157bcfd1f55bfbb3e5944cd5e03d07b343e280838c4Douglas Gregor      Width = Target->getLongWidth();
1158bcfd1f55bfbb3e5944cd5e03d07b343e280838c4Douglas Gregor      Align = Target->getLongAlign();
11596f62c2abd8077bf70d2166d37e8caa426b34d8e4Chris Lattner      break;
1160692233e90a99c3a81dd04879d36eb9688f137c44Chris Lattner    case BuiltinType::ULongLong:
11616f62c2abd8077bf70d2166d37e8caa426b34d8e4Chris Lattner    case BuiltinType::LongLong:
1162bcfd1f55bfbb3e5944cd5e03d07b343e280838c4Douglas Gregor      Width = Target->getLongLongWidth();
1163bcfd1f55bfbb3e5944cd5e03d07b343e280838c4Douglas Gregor      Align = Target->getLongLongAlign();
11646f62c2abd8077bf70d2166d37e8caa426b34d8e4Chris Lattner      break;
1165ec16cb9b5a481d62a73ad47fa59034ced4d62022Chris Lattner    case BuiltinType::Int128:
1166ec16cb9b5a481d62a73ad47fa59034ced4d62022Chris Lattner    case BuiltinType::UInt128:
1167ec16cb9b5a481d62a73ad47fa59034ced4d62022Chris Lattner      Width = 128;
1168ec16cb9b5a481d62a73ad47fa59034ced4d62022Chris Lattner      Align = 128; // int128_t is 128-bit aligned on all targets.
1169ec16cb9b5a481d62a73ad47fa59034ced4d62022Chris Lattner      break;
1170aa4a99b4a62615db243f7a5c433169f2fc704420Anton Korobeynikov    case BuiltinType::Half:
1171aa4a99b4a62615db243f7a5c433169f2fc704420Anton Korobeynikov      Width = Target->getHalfWidth();
1172aa4a99b4a62615db243f7a5c433169f2fc704420Anton Korobeynikov      Align = Target->getHalfAlign();
1173aa4a99b4a62615db243f7a5c433169f2fc704420Anton Korobeynikov      break;
11746f62c2abd8077bf70d2166d37e8caa426b34d8e4Chris Lattner    case BuiltinType::Float:
1175bcfd1f55bfbb3e5944cd5e03d07b343e280838c4Douglas Gregor      Width = Target->getFloatWidth();
1176bcfd1f55bfbb3e5944cd5e03d07b343e280838c4Douglas Gregor      Align = Target->getFloatAlign();
11776f62c2abd8077bf70d2166d37e8caa426b34d8e4Chris Lattner      break;
11786f62c2abd8077bf70d2166d37e8caa426b34d8e4Chris Lattner    case BuiltinType::Double:
1179bcfd1f55bfbb3e5944cd5e03d07b343e280838c4Douglas Gregor      Width = Target->getDoubleWidth();
1180bcfd1f55bfbb3e5944cd5e03d07b343e280838c4Douglas Gregor      Align = Target->getDoubleAlign();
11816f62c2abd8077bf70d2166d37e8caa426b34d8e4Chris Lattner      break;
11826f62c2abd8077bf70d2166d37e8caa426b34d8e4Chris Lattner    case BuiltinType::LongDouble:
1183bcfd1f55bfbb3e5944cd5e03d07b343e280838c4Douglas Gregor      Width = Target->getLongDoubleWidth();
1184bcfd1f55bfbb3e5944cd5e03d07b343e280838c4Douglas Gregor      Align = Target->getLongDoubleAlign();
11856f62c2abd8077bf70d2166d37e8caa426b34d8e4Chris Lattner      break;
11866e8ed16ffef02b82995a90bdcf10ffff7d63839aSebastian Redl    case BuiltinType::NullPtr:
1187bcfd1f55bfbb3e5944cd5e03d07b343e280838c4Douglas Gregor      Width = Target->getPointerWidth(0); // C++ 3.9.1p11: sizeof(nullptr_t)
1188bcfd1f55bfbb3e5944cd5e03d07b343e280838c4Douglas Gregor      Align = Target->getPointerAlign(0); //   == sizeof(void*)
11891590d9c0fec4c710c2962e4bb71f76979b5163d3Sebastian Redl      break;
1190e04f5fc25cf49e8a5b836459d836c20dc3229a95Fariborz Jahanian    case BuiltinType::ObjCId:
1191e04f5fc25cf49e8a5b836459d836c20dc3229a95Fariborz Jahanian    case BuiltinType::ObjCClass:
1192e04f5fc25cf49e8a5b836459d836c20dc3229a95Fariborz Jahanian    case BuiltinType::ObjCSel:
1193bcfd1f55bfbb3e5944cd5e03d07b343e280838c4Douglas Gregor      Width = Target->getPointerWidth(0);
1194bcfd1f55bfbb3e5944cd5e03d07b343e280838c4Douglas Gregor      Align = Target->getPointerAlign(0);
1195e04f5fc25cf49e8a5b836459d836c20dc3229a95Fariborz Jahanian      break;
1196a7674d8a9a69f3f6fe16e70cf2a3b2b15fb7c43dChris Lattner    }
1197bfef6d7c67831a135d6ab79931f010f750a730adChris Lattner    break;
1198d1b3c2dd5bc1f3103bee6137957aa7c5f8f2f0bcSteve Naroff  case Type::ObjCObjectPointer:
1199bcfd1f55bfbb3e5944cd5e03d07b343e280838c4Douglas Gregor    Width = Target->getPointerWidth(0);
1200bcfd1f55bfbb3e5944cd5e03d07b343e280838c4Douglas Gregor    Align = Target->getPointerAlign(0);
12016f62c2abd8077bf70d2166d37e8caa426b34d8e4Chris Lattner    break;
1202485eeff9ba73376c8e01179bf1a501b1723446cbSteve Naroff  case Type::BlockPointer: {
1203207f4d8543529221932af82836016a2ef066c917Peter Collingbourne    unsigned AS = getTargetAddressSpace(
1204207f4d8543529221932af82836016a2ef066c917Peter Collingbourne        cast<BlockPointerType>(T)->getPointeeType());
1205bcfd1f55bfbb3e5944cd5e03d07b343e280838c4Douglas Gregor    Width = Target->getPointerWidth(AS);
1206bcfd1f55bfbb3e5944cd5e03d07b343e280838c4Douglas Gregor    Align = Target->getPointerAlign(AS);
1207485eeff9ba73376c8e01179bf1a501b1723446cbSteve Naroff    break;
1208485eeff9ba73376c8e01179bf1a501b1723446cbSteve Naroff  }
12095d484e8cf710207010720589d89602233de61d01Sebastian Redl  case Type::LValueReference:
12105d484e8cf710207010720589d89602233de61d01Sebastian Redl  case Type::RValueReference: {
12115d484e8cf710207010720589d89602233de61d01Sebastian Redl    // alignof and sizeof should never enter this code path here, so we go
12125d484e8cf710207010720589d89602233de61d01Sebastian Redl    // the pointer route.
1213207f4d8543529221932af82836016a2ef066c917Peter Collingbourne    unsigned AS = getTargetAddressSpace(
1214207f4d8543529221932af82836016a2ef066c917Peter Collingbourne        cast<ReferenceType>(T)->getPointeeType());
1215bcfd1f55bfbb3e5944cd5e03d07b343e280838c4Douglas Gregor    Width = Target->getPointerWidth(AS);
1216bcfd1f55bfbb3e5944cd5e03d07b343e280838c4Douglas Gregor    Align = Target->getPointerAlign(AS);
12175d484e8cf710207010720589d89602233de61d01Sebastian Redl    break;
12185d484e8cf710207010720589d89602233de61d01Sebastian Redl  }
1219f72a44330b9d9a4b2d93e9b91cfb8ab7bd4a0643Chris Lattner  case Type::Pointer: {
1220207f4d8543529221932af82836016a2ef066c917Peter Collingbourne    unsigned AS = getTargetAddressSpace(cast<PointerType>(T)->getPointeeType());
1221bcfd1f55bfbb3e5944cd5e03d07b343e280838c4Douglas Gregor    Width = Target->getPointerWidth(AS);
1222bcfd1f55bfbb3e5944cd5e03d07b343e280838c4Douglas Gregor    Align = Target->getPointerAlign(AS);
1223f72a44330b9d9a4b2d93e9b91cfb8ab7bd4a0643Chris Lattner    break;
1224f72a44330b9d9a4b2d93e9b91cfb8ab7bd4a0643Chris Lattner  }
1225f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl  case Type::MemberPointer: {
1226071cc7deffad608165b1ddd5263e8bf181861520Charles Davis    const MemberPointerType *MPT = cast<MemberPointerType>(T);
12271eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    std::pair<uint64_t, unsigned> PtrDiffInfo =
12281cca74ef3627a3a0ab14501d23e336548f6611b2Anders Carlsson      getTypeInfo(getPointerDiffType());
1229071cc7deffad608165b1ddd5263e8bf181861520Charles Davis    Width = PtrDiffInfo.first * ABI->getMemberPointerSize(MPT);
12301cca74ef3627a3a0ab14501d23e336548f6611b2Anders Carlsson    Align = PtrDiffInfo.second;
12311cca74ef3627a3a0ab14501d23e336548f6611b2Anders Carlsson    break;
1232f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl  }
12335d2a6303467184b1f159bb6556efc434e50e3c28Chris Lattner  case Type::Complex: {
12345d2a6303467184b1f159bb6556efc434e50e3c28Chris Lattner    // Complex types have the same alignment as their elements, but twice the
12355d2a6303467184b1f159bb6556efc434e50e3c28Chris Lattner    // size.
12361eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    std::pair<uint64_t, unsigned> EltInfo =
123798be4943e8dc4f3905629a7102668960873cf863Chris Lattner      getTypeInfo(cast<ComplexType>(T)->getElementType());
12389e9b6dc3fd413f5341fab54b681420eeb21cd169Chris Lattner    Width = EltInfo.first*2;
12395d2a6303467184b1f159bb6556efc434e50e3c28Chris Lattner    Align = EltInfo.second;
12405d2a6303467184b1f159bb6556efc434e50e3c28Chris Lattner    break;
12415d2a6303467184b1f159bb6556efc434e50e3c28Chris Lattner  }
1242c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall  case Type::ObjCObject:
1243c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall    return getTypeInfo(cast<ObjCObjectType>(T)->getBaseType().getTypePtr());
124444a3dded8080c5c9cfdad208ade8f8f7850d9a4fDevang Patel  case Type::ObjCInterface: {
12451d75118af76cae2bfc06389cde410e14bd0a19fcDaniel Dunbar    const ObjCInterfaceType *ObjCI = cast<ObjCInterfaceType>(T);
124644a3dded8080c5c9cfdad208ade8f8f7850d9a4fDevang Patel    const ASTRecordLayout &Layout = getASTObjCInterfaceLayout(ObjCI->getDecl());
1247dd76a9ab9ea675671200f94b18ce95766841952bKen Dyck    Width = toBits(Layout.getSize());
1248dac54c124e302d6f028ea5723c425b7f66fc7c71Ken Dyck    Align = toBits(Layout.getAlignment());
124944a3dded8080c5c9cfdad208ade8f8f7850d9a4fDevang Patel    break;
125044a3dded8080c5c9cfdad208ade8f8f7850d9a4fDevang Patel  }
125172564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor  case Type::Record:
125272564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor  case Type::Enum: {
12531d75118af76cae2bfc06389cde410e14bd0a19fcDaniel Dunbar    const TagType *TT = cast<TagType>(T);
12541d75118af76cae2bfc06389cde410e14bd0a19fcDaniel Dunbar
12551d75118af76cae2bfc06389cde410e14bd0a19fcDaniel Dunbar    if (TT->getDecl()->isInvalidDecl()) {
125622ce41d9fc8509da65858c75bf5b3c4dae2d8c04Douglas Gregor      Width = 8;
125722ce41d9fc8509da65858c75bf5b3c4dae2d8c04Douglas Gregor      Align = 8;
12588389eab190afef3462f6418b8d8fb70fb01c4005Chris Lattner      break;
12598389eab190afef3462f6418b8d8fb70fb01c4005Chris Lattner    }
12601eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
12611d75118af76cae2bfc06389cde410e14bd0a19fcDaniel Dunbar    if (const EnumType *ET = dyn_cast<EnumType>(TT))
12627176331b0f5cfaaa2b5aa487a6660e859e371119Chris Lattner      return getTypeInfo(ET->getDecl()->getIntegerType());
12637176331b0f5cfaaa2b5aa487a6660e859e371119Chris Lattner
12641d75118af76cae2bfc06389cde410e14bd0a19fcDaniel Dunbar    const RecordType *RT = cast<RecordType>(TT);
12657176331b0f5cfaaa2b5aa487a6660e859e371119Chris Lattner    const ASTRecordLayout &Layout = getASTRecordLayout(RT->getDecl());
1266dd76a9ab9ea675671200f94b18ce95766841952bKen Dyck    Width = toBits(Layout.getSize());
1267dac54c124e302d6f028ea5723c425b7f66fc7c71Ken Dyck    Align = toBits(Layout.getAlignment());
1268dc0d73e6495404418acf8548875aeaff07791a74Chris Lattner    break;
1269a7674d8a9a69f3f6fe16e70cf2a3b2b15fb7c43dChris Lattner  }
12707532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor
12719fcfe926432f3c3f7e9a61219e55c352fd358e45Chris Lattner  case Type::SubstTemplateTypeParm:
127249a832bd499d6f61c23655f1fac99f0dd229756eJohn McCall    return getTypeInfo(cast<SubstTemplateTypeParmType>(T)->
127349a832bd499d6f61c23655f1fac99f0dd229756eJohn McCall                       getReplacementType().getTypePtr());
127449a832bd499d6f61c23655f1fac99f0dd229756eJohn McCall
127534b41d939a1328f484511c6002ba2456db879a29Richard Smith  case Type::Auto: {
127634b41d939a1328f484511c6002ba2456db879a29Richard Smith    const AutoType *A = cast<AutoType>(T);
127734b41d939a1328f484511c6002ba2456db879a29Richard Smith    assert(A->isDeduced() && "Cannot request the size of a dependent type");
1278dc856aff4428380baa9afb5577ea04f8fb6beb13Matt Beaumont-Gay    return getTypeInfo(A->getDeducedType().getTypePtr());
127934b41d939a1328f484511c6002ba2456db879a29Richard Smith  }
128034b41d939a1328f484511c6002ba2456db879a29Richard Smith
1281075f8f1b6bed4d1b224c74f87508534cc6392ce6Abramo Bagnara  case Type::Paren:
1282075f8f1b6bed4d1b224c74f87508534cc6392ce6Abramo Bagnara    return getTypeInfo(cast<ParenType>(T)->getInnerType().getTypePtr());
1283075f8f1b6bed4d1b224c74f87508534cc6392ce6Abramo Bagnara
128418857644059c45da6776f1a288eec7b4cf3a844aDouglas Gregor  case Type::Typedef: {
1285162e1c1b487352434552147967c3dd296ebee2f7Richard Smith    const TypedefNameDecl *Typedef = cast<TypedefType>(T)->getDecl();
1286df1367af26cb2959775e9511108f12dcd2370a27Douglas Gregor    std::pair<uint64_t, unsigned> Info
1287df1367af26cb2959775e9511108f12dcd2370a27Douglas Gregor      = getTypeInfo(Typedef->getUnderlyingType().getTypePtr());
1288c1de52de64725945e5ae87e6f99ddedf161856e5Chris Lattner    // If the typedef has an aligned attribute on it, it overrides any computed
1289c1de52de64725945e5ae87e6f99ddedf161856e5Chris Lattner    // alignment we have.  This violates the GCC documentation (which says that
1290c1de52de64725945e5ae87e6f99ddedf161856e5Chris Lattner    // attribute(aligned) can only round up) but matches its implementation.
1291c1de52de64725945e5ae87e6f99ddedf161856e5Chris Lattner    if (unsigned AttrAlign = Typedef->getMaxAlignment())
1292c1de52de64725945e5ae87e6f99ddedf161856e5Chris Lattner      Align = AttrAlign;
1293c1de52de64725945e5ae87e6f99ddedf161856e5Chris Lattner    else
1294c1de52de64725945e5ae87e6f99ddedf161856e5Chris Lattner      Align = Info.second;
1295df1367af26cb2959775e9511108f12dcd2370a27Douglas Gregor    Width = Info.first;
12967532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor    break;
12977176331b0f5cfaaa2b5aa487a6660e859e371119Chris Lattner  }
129818857644059c45da6776f1a288eec7b4cf3a844aDouglas Gregor
129918857644059c45da6776f1a288eec7b4cf3a844aDouglas Gregor  case Type::TypeOfExpr:
130018857644059c45da6776f1a288eec7b4cf3a844aDouglas Gregor    return getTypeInfo(cast<TypeOfExprType>(T)->getUnderlyingExpr()->getType()
130118857644059c45da6776f1a288eec7b4cf3a844aDouglas Gregor                         .getTypePtr());
130218857644059c45da6776f1a288eec7b4cf3a844aDouglas Gregor
130318857644059c45da6776f1a288eec7b4cf3a844aDouglas Gregor  case Type::TypeOf:
130418857644059c45da6776f1a288eec7b4cf3a844aDouglas Gregor    return getTypeInfo(cast<TypeOfType>(T)->getUnderlyingType().getTypePtr());
130518857644059c45da6776f1a288eec7b4cf3a844aDouglas Gregor
1306395b475a4474f1c7574d927ad142ca0c7997cbcaAnders Carlsson  case Type::Decltype:
1307395b475a4474f1c7574d927ad142ca0c7997cbcaAnders Carlsson    return getTypeInfo(cast<DecltypeType>(T)->getUnderlyingExpr()->getType()
1308395b475a4474f1c7574d927ad142ca0c7997cbcaAnders Carlsson                        .getTypePtr());
1309395b475a4474f1c7574d927ad142ca0c7997cbcaAnders Carlsson
1310ca63c200346c0ca9e00194ec6e34a5a7b0ed9321Sean Hunt  case Type::UnaryTransform:
1311ca63c200346c0ca9e00194ec6e34a5a7b0ed9321Sean Hunt    return getTypeInfo(cast<UnaryTransformType>(T)->getUnderlyingType());
1312ca63c200346c0ca9e00194ec6e34a5a7b0ed9321Sean Hunt
1313465d41b92b2c862f3062c412a0538db65c6a2661Abramo Bagnara  case Type::Elaborated:
1314465d41b92b2c862f3062c412a0538db65c6a2661Abramo Bagnara    return getTypeInfo(cast<ElaboratedType>(T)->getNamedType().getTypePtr());
13151eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
13169d156a7b1b2771e191f2f5a45a7b7a694129463bJohn McCall  case Type::Attributed:
13179d156a7b1b2771e191f2f5a45a7b7a694129463bJohn McCall    return getTypeInfo(
13189d156a7b1b2771e191f2f5a45a7b7a694129463bJohn McCall                  cast<AttributedType>(T)->getEquivalentType().getTypePtr());
13199d156a7b1b2771e191f2f5a45a7b7a694129463bJohn McCall
13203e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith  case Type::TemplateSpecialization: {
13211eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    assert(getCanonicalType(T) != T &&
132218857644059c45da6776f1a288eec7b4cf3a844aDouglas Gregor           "Cannot request the size of a dependent type");
13233e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith    const TemplateSpecializationType *TST = cast<TemplateSpecializationType>(T);
13243e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith    // A type alias template specialization may refer to a typedef with the
13253e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith    // aligned attribute on it.
13263e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith    if (TST->isTypeAlias())
13273e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith      return getTypeInfo(TST->getAliasedType().getTypePtr());
13283e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith    else
13293e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith      return getTypeInfo(getCanonicalType(T));
13303e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith  }
13313e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith
1332b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman  case Type::Atomic: {
13332be460723940f8184ec36529b6f6ddf59c04e411Eli Friedman    std::pair<uint64_t, unsigned> Info
13342be460723940f8184ec36529b6f6ddf59c04e411Eli Friedman      = getTypeInfo(cast<AtomicType>(T)->getValueType());
13352be460723940f8184ec36529b6f6ddf59c04e411Eli Friedman    Width = Info.first;
13362be460723940f8184ec36529b6f6ddf59c04e411Eli Friedman    Align = Info.second;
13372be460723940f8184ec36529b6f6ddf59c04e411Eli Friedman    if (Width != 0 && Width <= Target->getMaxAtomicPromoteWidth() &&
13382be460723940f8184ec36529b6f6ddf59c04e411Eli Friedman        llvm::isPowerOf2_64(Width)) {
13392be460723940f8184ec36529b6f6ddf59c04e411Eli Friedman      // We can potentially perform lock-free atomic operations for this
13402be460723940f8184ec36529b6f6ddf59c04e411Eli Friedman      // type; promote the alignment appropriately.
13412be460723940f8184ec36529b6f6ddf59c04e411Eli Friedman      // FIXME: We could potentially promote the width here as well...
13422be460723940f8184ec36529b6f6ddf59c04e411Eli Friedman      // is that worthwhile?  (Non-struct atomic types generally have
13432be460723940f8184ec36529b6f6ddf59c04e411Eli Friedman      // power-of-two size anyway, but structs might not.  Requires a bit
13442be460723940f8184ec36529b6f6ddf59c04e411Eli Friedman      // of implementation work to make sure we zero out the extra bits.)
13452be460723940f8184ec36529b6f6ddf59c04e411Eli Friedman      Align = static_cast<unsigned>(Width);
13462be460723940f8184ec36529b6f6ddf59c04e411Eli Friedman    }
1347b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman  }
1348b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman
134918857644059c45da6776f1a288eec7b4cf3a844aDouglas Gregor  }
13501eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
13512be460723940f8184ec36529b6f6ddf59c04e411Eli Friedman  assert(llvm::isPowerOf2_32(Align) && "Alignment must be power of 2");
13529e9b6dc3fd413f5341fab54b681420eeb21cd169Chris Lattner  return std::make_pair(Width, Align);
1353a7674d8a9a69f3f6fe16e70cf2a3b2b15fb7c43dChris Lattner}
1354a7674d8a9a69f3f6fe16e70cf2a3b2b15fb7c43dChris Lattner
1355eb6f5dc86531f794ba7746a2da4d28e37cf5da7eKen Dyck/// toCharUnitsFromBits - Convert a size in bits to a size in characters.
1356eb6f5dc86531f794ba7746a2da4d28e37cf5da7eKen DyckCharUnits ASTContext::toCharUnitsFromBits(int64_t BitSize) const {
1357eb6f5dc86531f794ba7746a2da4d28e37cf5da7eKen Dyck  return CharUnits::fromQuantity(BitSize / getCharWidth());
1358eb6f5dc86531f794ba7746a2da4d28e37cf5da7eKen Dyck}
1359eb6f5dc86531f794ba7746a2da4d28e37cf5da7eKen Dyck
1360dd76a9ab9ea675671200f94b18ce95766841952bKen Dyck/// toBits - Convert a size in characters to a size in characters.
1361dd76a9ab9ea675671200f94b18ce95766841952bKen Dyckint64_t ASTContext::toBits(CharUnits CharSize) const {
1362dd76a9ab9ea675671200f94b18ce95766841952bKen Dyck  return CharSize.getQuantity() * getCharWidth();
1363dd76a9ab9ea675671200f94b18ce95766841952bKen Dyck}
1364dd76a9ab9ea675671200f94b18ce95766841952bKen Dyck
1365bdc601b196c48d4cd56a5ceb45d41ae4e87371abKen Dyck/// getTypeSizeInChars - Return the size of the specified type, in characters.
1366bdc601b196c48d4cd56a5ceb45d41ae4e87371abKen Dyck/// This method does not work on incomplete types.
13674ba2a17694148e16eaa8d3917f657ffcd3667be4Jay FoadCharUnits ASTContext::getTypeSizeInChars(QualType T) const {
1368eb6f5dc86531f794ba7746a2da4d28e37cf5da7eKen Dyck  return toCharUnitsFromBits(getTypeSize(T));
1369bdc601b196c48d4cd56a5ceb45d41ae4e87371abKen Dyck}
13704ba2a17694148e16eaa8d3917f657ffcd3667be4Jay FoadCharUnits ASTContext::getTypeSizeInChars(const Type *T) const {
1371eb6f5dc86531f794ba7746a2da4d28e37cf5da7eKen Dyck  return toCharUnitsFromBits(getTypeSize(T));
1372bdc601b196c48d4cd56a5ceb45d41ae4e87371abKen Dyck}
1373bdc601b196c48d4cd56a5ceb45d41ae4e87371abKen Dyck
137416e20cce43385001f33f8e3f90ee345609c805d1Ken Dyck/// getTypeAlignInChars - Return the ABI-specified alignment of a type, in
137586fa4311c8a330957ff5b765fbb0a7750ecd38c9Ken Dyck/// characters. This method does not work on incomplete types.
13764ba2a17694148e16eaa8d3917f657ffcd3667be4Jay FoadCharUnits ASTContext::getTypeAlignInChars(QualType T) const {
1377eb6f5dc86531f794ba7746a2da4d28e37cf5da7eKen Dyck  return toCharUnitsFromBits(getTypeAlign(T));
137886fa4311c8a330957ff5b765fbb0a7750ecd38c9Ken Dyck}
13794ba2a17694148e16eaa8d3917f657ffcd3667be4Jay FoadCharUnits ASTContext::getTypeAlignInChars(const Type *T) const {
1380eb6f5dc86531f794ba7746a2da4d28e37cf5da7eKen Dyck  return toCharUnitsFromBits(getTypeAlign(T));
138186fa4311c8a330957ff5b765fbb0a7750ecd38c9Ken Dyck}
138286fa4311c8a330957ff5b765fbb0a7750ecd38c9Ken Dyck
138334ebde404dc17d89487b07e6daaf1b47d5dfee39Chris Lattner/// getPreferredTypeAlign - Return the "preferred" alignment of the specified
138434ebde404dc17d89487b07e6daaf1b47d5dfee39Chris Lattner/// type for the current target in bits.  This can be different than the ABI
138534ebde404dc17d89487b07e6daaf1b47d5dfee39Chris Lattner/// alignment in cases where it is beneficial for performance to overalign
138634ebde404dc17d89487b07e6daaf1b47d5dfee39Chris Lattner/// a data type.
13874ba2a17694148e16eaa8d3917f657ffcd3667be4Jay Foadunsigned ASTContext::getPreferredTypeAlign(const Type *T) const {
138834ebde404dc17d89487b07e6daaf1b47d5dfee39Chris Lattner  unsigned ABIAlign = getTypeAlign(T);
13891eed60297ef4701b899c6a3b9680bf08f3403422Eli Friedman
13901eed60297ef4701b899c6a3b9680bf08f3403422Eli Friedman  // Double and long long should be naturally aligned if possible.
1391183700f494ec9b6701b6efe82bcb25f4c79ba561John McCall  if (const ComplexType* CT = T->getAs<ComplexType>())
13921eed60297ef4701b899c6a3b9680bf08f3403422Eli Friedman    T = CT->getElementType().getTypePtr();
13931eed60297ef4701b899c6a3b9680bf08f3403422Eli Friedman  if (T->isSpecificBuiltinType(BuiltinType::Double) ||
1394cde7a1dc68af2eb063a039b5a31c3b7dd92b1aa9Chad Rosier      T->isSpecificBuiltinType(BuiltinType::LongLong) ||
1395cde7a1dc68af2eb063a039b5a31c3b7dd92b1aa9Chad Rosier      T->isSpecificBuiltinType(BuiltinType::ULongLong))
13961eed60297ef4701b899c6a3b9680bf08f3403422Eli Friedman    return std::max(ABIAlign, (unsigned)getTypeSize(T));
13971eed60297ef4701b899c6a3b9680bf08f3403422Eli Friedman
139834ebde404dc17d89487b07e6daaf1b47d5dfee39Chris Lattner  return ABIAlign;
139934ebde404dc17d89487b07e6daaf1b47d5dfee39Chris Lattner}
140034ebde404dc17d89487b07e6daaf1b47d5dfee39Chris Lattner
14012c18bb7c9fca66c30b6eabbdcbc6399d24a54fa9Fariborz Jahanian/// DeepCollectObjCIvars -
14022c18bb7c9fca66c30b6eabbdcbc6399d24a54fa9Fariborz Jahanian/// This routine first collects all declared, but not synthesized, ivars in
14032c18bb7c9fca66c30b6eabbdcbc6399d24a54fa9Fariborz Jahanian/// super class and then collects all ivars, including those synthesized for
14042c18bb7c9fca66c30b6eabbdcbc6399d24a54fa9Fariborz Jahanian/// current class. This routine is used for implementation of current class
14052c18bb7c9fca66c30b6eabbdcbc6399d24a54fa9Fariborz Jahanian/// when all ivars, declared and synthesized are known.
14069820074dd47d37681085e964cd3392ac0b3e67b9Fariborz Jahanian///
14072c18bb7c9fca66c30b6eabbdcbc6399d24a54fa9Fariborz Jahanianvoid ASTContext::DeepCollectObjCIvars(const ObjCInterfaceDecl *OI,
14082c18bb7c9fca66c30b6eabbdcbc6399d24a54fa9Fariborz Jahanian                                      bool leafClass,
1409db8264e4c5ffd7af6fbad4ca4306bd382bb02691Jordy Rose                            SmallVectorImpl<const ObjCIvarDecl*> &Ivars) const {
14102c18bb7c9fca66c30b6eabbdcbc6399d24a54fa9Fariborz Jahanian  if (const ObjCInterfaceDecl *SuperClass = OI->getSuperClass())
14112c18bb7c9fca66c30b6eabbdcbc6399d24a54fa9Fariborz Jahanian    DeepCollectObjCIvars(SuperClass, false, Ivars);
14122c18bb7c9fca66c30b6eabbdcbc6399d24a54fa9Fariborz Jahanian  if (!leafClass) {
14132c18bb7c9fca66c30b6eabbdcbc6399d24a54fa9Fariborz Jahanian    for (ObjCInterfaceDecl::ivar_iterator I = OI->ivar_begin(),
14142c18bb7c9fca66c30b6eabbdcbc6399d24a54fa9Fariborz Jahanian         E = OI->ivar_end(); I != E; ++I)
1415581deb3da481053c4993c7600f97acf7768caac5David Blaikie      Ivars.push_back(*I);
14163060178ad9df29789505c1e6debcfc80a3a13587Chad Rosier  } else {
1417bf9eb88792e022e54a658657bf22e1925948e384Fariborz Jahanian    ObjCInterfaceDecl *IDecl = const_cast<ObjCInterfaceDecl *>(OI);
1418db8264e4c5ffd7af6fbad4ca4306bd382bb02691Jordy Rose    for (const ObjCIvarDecl *Iv = IDecl->all_declared_ivar_begin(); Iv;
1419bf9eb88792e022e54a658657bf22e1925948e384Fariborz Jahanian         Iv= Iv->getNextIvar())
1420bf9eb88792e022e54a658657bf22e1925948e384Fariborz Jahanian      Ivars.push_back(Iv);
1421bf9eb88792e022e54a658657bf22e1925948e384Fariborz Jahanian  }
14229820074dd47d37681085e964cd3392ac0b3e67b9Fariborz Jahanian}
14239820074dd47d37681085e964cd3392ac0b3e67b9Fariborz Jahanian
1424e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanian/// CollectInheritedProtocols - Collect all protocols in current class and
1425e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanian/// those inherited by it.
1426e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanianvoid ASTContext::CollectInheritedProtocols(const Decl *CDecl,
1427432a8893f7e30d141d7f279bd00b741a3cdac81fFariborz Jahanian                          llvm::SmallPtrSet<ObjCProtocolDecl*, 8> &Protocols) {
1428e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanian  if (const ObjCInterfaceDecl *OI = dyn_cast<ObjCInterfaceDecl>(CDecl)) {
142953b9441b5a81a24fa1f66f3f6416f1e36baa9c2fTed Kremenek    // We can use protocol_iterator here instead of
143053b9441b5a81a24fa1f66f3f6416f1e36baa9c2fTed Kremenek    // all_referenced_protocol_iterator since we are walking all categories.
143153b9441b5a81a24fa1f66f3f6416f1e36baa9c2fTed Kremenek    for (ObjCInterfaceDecl::all_protocol_iterator P = OI->all_referenced_protocol_begin(),
143253b9441b5a81a24fa1f66f3f6416f1e36baa9c2fTed Kremenek         PE = OI->all_referenced_protocol_end(); P != PE; ++P) {
1433e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanian      ObjCProtocolDecl *Proto = (*P);
14343fc73ee0c613715ebce78e30b4d050ea715a007dDouglas Gregor      Protocols.insert(Proto->getCanonicalDecl());
1435e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanian      for (ObjCProtocolDecl::protocol_iterator P = Proto->protocol_begin(),
1436b2f812165676230bce5d0215e49a4749c451ca9cFariborz Jahanian           PE = Proto->protocol_end(); P != PE; ++P) {
14373fc73ee0c613715ebce78e30b4d050ea715a007dDouglas Gregor        Protocols.insert((*P)->getCanonicalDecl());
1438e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanian        CollectInheritedProtocols(*P, Protocols);
1439e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanian      }
1440b2f812165676230bce5d0215e49a4749c451ca9cFariborz Jahanian    }
1441e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanian
1442e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanian    // Categories of this Interface.
1443e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanian    for (const ObjCCategoryDecl *CDeclChain = OI->getCategoryList();
1444e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanian         CDeclChain; CDeclChain = CDeclChain->getNextClassCategory())
1445e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanian      CollectInheritedProtocols(CDeclChain, Protocols);
1446e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanian    if (ObjCInterfaceDecl *SD = OI->getSuperClass())
1447e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanian      while (SD) {
1448e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanian        CollectInheritedProtocols(SD, Protocols);
1449e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanian        SD = SD->getSuperClass();
1450e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanian      }
1451b170ca5f4a8397c10e52050ff5df6885a3e6eca9Benjamin Kramer  } else if (const ObjCCategoryDecl *OC = dyn_cast<ObjCCategoryDecl>(CDecl)) {
145253b9441b5a81a24fa1f66f3f6416f1e36baa9c2fTed Kremenek    for (ObjCCategoryDecl::protocol_iterator P = OC->protocol_begin(),
1453e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanian         PE = OC->protocol_end(); P != PE; ++P) {
1454e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanian      ObjCProtocolDecl *Proto = (*P);
14553fc73ee0c613715ebce78e30b4d050ea715a007dDouglas Gregor      Protocols.insert(Proto->getCanonicalDecl());
1456e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanian      for (ObjCProtocolDecl::protocol_iterator P = Proto->protocol_begin(),
1457e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanian           PE = Proto->protocol_end(); P != PE; ++P)
1458e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanian        CollectInheritedProtocols(*P, Protocols);
1459e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanian    }
1460b170ca5f4a8397c10e52050ff5df6885a3e6eca9Benjamin Kramer  } else if (const ObjCProtocolDecl *OP = dyn_cast<ObjCProtocolDecl>(CDecl)) {
1461e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanian    for (ObjCProtocolDecl::protocol_iterator P = OP->protocol_begin(),
1462e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanian         PE = OP->protocol_end(); P != PE; ++P) {
1463e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanian      ObjCProtocolDecl *Proto = (*P);
14643fc73ee0c613715ebce78e30b4d050ea715a007dDouglas Gregor      Protocols.insert(Proto->getCanonicalDecl());
1465e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanian      for (ObjCProtocolDecl::protocol_iterator P = Proto->protocol_begin(),
1466e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanian           PE = Proto->protocol_end(); P != PE; ++P)
1467e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanian        CollectInheritedProtocols(*P, Protocols);
1468e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanian    }
1469e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanian  }
1470e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanian}
1471e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanian
14724ba2a17694148e16eaa8d3917f657ffcd3667be4Jay Foadunsigned ASTContext::CountNonClassIvars(const ObjCInterfaceDecl *OI) const {
14733bfacdf6f0706987c1b33c625cd68fb880881c22Fariborz Jahanian  unsigned count = 0;
14743bfacdf6f0706987c1b33c625cd68fb880881c22Fariborz Jahanian  // Count ivars declared in class extension.
147580aa1cd7973561889e51c1c152c8990a8de9c953Fariborz Jahanian  for (const ObjCCategoryDecl *CDecl = OI->getFirstClassExtension(); CDecl;
147680aa1cd7973561889e51c1c152c8990a8de9c953Fariborz Jahanian       CDecl = CDecl->getNextClassExtension())
1477b170ca5f4a8397c10e52050ff5df6885a3e6eca9Benjamin Kramer    count += CDecl->ivar_size();
1478b170ca5f4a8397c10e52050ff5df6885a3e6eca9Benjamin Kramer
14793bfacdf6f0706987c1b33c625cd68fb880881c22Fariborz Jahanian  // Count ivar defined in this class's implementation.  This
14803bfacdf6f0706987c1b33c625cd68fb880881c22Fariborz Jahanian  // includes synthesized ivars.
14813bfacdf6f0706987c1b33c625cd68fb880881c22Fariborz Jahanian  if (ObjCImplementationDecl *ImplDecl = OI->getImplementation())
1482b170ca5f4a8397c10e52050ff5df6885a3e6eca9Benjamin Kramer    count += ImplDecl->ivar_size();
1483b170ca5f4a8397c10e52050ff5df6885a3e6eca9Benjamin Kramer
14848e6ac1d80055fa37b9b84029c7e751624ba7f84cFariborz Jahanian  return count;
14858e6ac1d80055fa37b9b84029c7e751624ba7f84cFariborz Jahanian}
14868e6ac1d80055fa37b9b84029c7e751624ba7f84cFariborz Jahanian
14878deabc133c121f6c5561d0b2171a41cb2c29b2ceArgyrios Kyrtzidisbool ASTContext::isSentinelNullExpr(const Expr *E) {
14888deabc133c121f6c5561d0b2171a41cb2c29b2ceArgyrios Kyrtzidis  if (!E)
14898deabc133c121f6c5561d0b2171a41cb2c29b2ceArgyrios Kyrtzidis    return false;
14908deabc133c121f6c5561d0b2171a41cb2c29b2ceArgyrios Kyrtzidis
14918deabc133c121f6c5561d0b2171a41cb2c29b2ceArgyrios Kyrtzidis  // nullptr_t is always treated as null.
14928deabc133c121f6c5561d0b2171a41cb2c29b2ceArgyrios Kyrtzidis  if (E->getType()->isNullPtrType()) return true;
14938deabc133c121f6c5561d0b2171a41cb2c29b2ceArgyrios Kyrtzidis
14948deabc133c121f6c5561d0b2171a41cb2c29b2ceArgyrios Kyrtzidis  if (E->getType()->isAnyPointerType() &&
14958deabc133c121f6c5561d0b2171a41cb2c29b2ceArgyrios Kyrtzidis      E->IgnoreParenCasts()->isNullPointerConstant(*this,
14968deabc133c121f6c5561d0b2171a41cb2c29b2ceArgyrios Kyrtzidis                                                Expr::NPC_ValueDependentIsNull))
14978deabc133c121f6c5561d0b2171a41cb2c29b2ceArgyrios Kyrtzidis    return true;
14988deabc133c121f6c5561d0b2171a41cb2c29b2ceArgyrios Kyrtzidis
14998deabc133c121f6c5561d0b2171a41cb2c29b2ceArgyrios Kyrtzidis  // Unfortunately, __null has type 'int'.
15008deabc133c121f6c5561d0b2171a41cb2c29b2ceArgyrios Kyrtzidis  if (isa<GNUNullExpr>(E)) return true;
15018deabc133c121f6c5561d0b2171a41cb2c29b2ceArgyrios Kyrtzidis
15028deabc133c121f6c5561d0b2171a41cb2c29b2ceArgyrios Kyrtzidis  return false;
15038deabc133c121f6c5561d0b2171a41cb2c29b2ceArgyrios Kyrtzidis}
15048deabc133c121f6c5561d0b2171a41cb2c29b2ceArgyrios Kyrtzidis
15058a1d722f13df383600f36d77f842957c8adb5f1bArgyrios Kyrtzidis/// \brief Get the implementation of ObjCInterfaceDecl,or NULL if none exists.
15068a1d722f13df383600f36d77f842957c8adb5f1bArgyrios KyrtzidisObjCImplementationDecl *ASTContext::getObjCImplementation(ObjCInterfaceDecl *D) {
15078a1d722f13df383600f36d77f842957c8adb5f1bArgyrios Kyrtzidis  llvm::DenseMap<ObjCContainerDecl*, ObjCImplDecl*>::iterator
15088a1d722f13df383600f36d77f842957c8adb5f1bArgyrios Kyrtzidis    I = ObjCImpls.find(D);
15098a1d722f13df383600f36d77f842957c8adb5f1bArgyrios Kyrtzidis  if (I != ObjCImpls.end())
15108a1d722f13df383600f36d77f842957c8adb5f1bArgyrios Kyrtzidis    return cast<ObjCImplementationDecl>(I->second);
15118a1d722f13df383600f36d77f842957c8adb5f1bArgyrios Kyrtzidis  return 0;
15128a1d722f13df383600f36d77f842957c8adb5f1bArgyrios Kyrtzidis}
15138a1d722f13df383600f36d77f842957c8adb5f1bArgyrios Kyrtzidis/// \brief Get the implementation of ObjCCategoryDecl, or NULL if none exists.
15148a1d722f13df383600f36d77f842957c8adb5f1bArgyrios KyrtzidisObjCCategoryImplDecl *ASTContext::getObjCImplementation(ObjCCategoryDecl *D) {
15158a1d722f13df383600f36d77f842957c8adb5f1bArgyrios Kyrtzidis  llvm::DenseMap<ObjCContainerDecl*, ObjCImplDecl*>::iterator
15168a1d722f13df383600f36d77f842957c8adb5f1bArgyrios Kyrtzidis    I = ObjCImpls.find(D);
15178a1d722f13df383600f36d77f842957c8adb5f1bArgyrios Kyrtzidis  if (I != ObjCImpls.end())
15188a1d722f13df383600f36d77f842957c8adb5f1bArgyrios Kyrtzidis    return cast<ObjCCategoryImplDecl>(I->second);
15198a1d722f13df383600f36d77f842957c8adb5f1bArgyrios Kyrtzidis  return 0;
15208a1d722f13df383600f36d77f842957c8adb5f1bArgyrios Kyrtzidis}
15218a1d722f13df383600f36d77f842957c8adb5f1bArgyrios Kyrtzidis
15228a1d722f13df383600f36d77f842957c8adb5f1bArgyrios Kyrtzidis/// \brief Set the implementation of ObjCInterfaceDecl.
15238a1d722f13df383600f36d77f842957c8adb5f1bArgyrios Kyrtzidisvoid ASTContext::setObjCImplementation(ObjCInterfaceDecl *IFaceD,
15248a1d722f13df383600f36d77f842957c8adb5f1bArgyrios Kyrtzidis                           ObjCImplementationDecl *ImplD) {
15258a1d722f13df383600f36d77f842957c8adb5f1bArgyrios Kyrtzidis  assert(IFaceD && ImplD && "Passed null params");
15268a1d722f13df383600f36d77f842957c8adb5f1bArgyrios Kyrtzidis  ObjCImpls[IFaceD] = ImplD;
15278a1d722f13df383600f36d77f842957c8adb5f1bArgyrios Kyrtzidis}
15288a1d722f13df383600f36d77f842957c8adb5f1bArgyrios Kyrtzidis/// \brief Set the implementation of ObjCCategoryDecl.
15298a1d722f13df383600f36d77f842957c8adb5f1bArgyrios Kyrtzidisvoid ASTContext::setObjCImplementation(ObjCCategoryDecl *CatD,
15308a1d722f13df383600f36d77f842957c8adb5f1bArgyrios Kyrtzidis                           ObjCCategoryImplDecl *ImplD) {
15318a1d722f13df383600f36d77f842957c8adb5f1bArgyrios Kyrtzidis  assert(CatD && ImplD && "Passed null params");
15328a1d722f13df383600f36d77f842957c8adb5f1bArgyrios Kyrtzidis  ObjCImpls[CatD] = ImplD;
15338a1d722f13df383600f36d77f842957c8adb5f1bArgyrios Kyrtzidis}
15348a1d722f13df383600f36d77f842957c8adb5f1bArgyrios Kyrtzidis
153587ec9c257c99b4136af6c7f5be5a2d486906ba84Argyrios KyrtzidisObjCInterfaceDecl *ASTContext::getObjContainingInterface(NamedDecl *ND) const {
153687ec9c257c99b4136af6c7f5be5a2d486906ba84Argyrios Kyrtzidis  if (ObjCInterfaceDecl *ID = dyn_cast<ObjCInterfaceDecl>(ND->getDeclContext()))
153787ec9c257c99b4136af6c7f5be5a2d486906ba84Argyrios Kyrtzidis    return ID;
153887ec9c257c99b4136af6c7f5be5a2d486906ba84Argyrios Kyrtzidis  if (ObjCCategoryDecl *CD = dyn_cast<ObjCCategoryDecl>(ND->getDeclContext()))
153987ec9c257c99b4136af6c7f5be5a2d486906ba84Argyrios Kyrtzidis    return CD->getClassInterface();
154087ec9c257c99b4136af6c7f5be5a2d486906ba84Argyrios Kyrtzidis  if (ObjCImplDecl *IMD = dyn_cast<ObjCImplDecl>(ND->getDeclContext()))
154187ec9c257c99b4136af6c7f5be5a2d486906ba84Argyrios Kyrtzidis    return IMD->getClassInterface();
154287ec9c257c99b4136af6c7f5be5a2d486906ba84Argyrios Kyrtzidis
154387ec9c257c99b4136af6c7f5be5a2d486906ba84Argyrios Kyrtzidis  return 0;
154487ec9c257c99b4136af6c7f5be5a2d486906ba84Argyrios Kyrtzidis}
154587ec9c257c99b4136af6c7f5be5a2d486906ba84Argyrios Kyrtzidis
15461ceee5c42d5c410217f67d384eecc6ea4a2bba9bFariborz Jahanian/// \brief Get the copy initialization expression of VarDecl,or NULL if
15471ceee5c42d5c410217f67d384eecc6ea4a2bba9bFariborz Jahanian/// none exists.
1548830937bc1100fba7682f7c32c40512085870f50cFariborz JahanianExpr *ASTContext::getBlockVarCopyInits(const VarDecl*VD) {
1549d016ec204c54d766c4561d43548551ffde0e6b43Fariborz Jahanian  assert(VD && "Passed null params");
1550d016ec204c54d766c4561d43548551ffde0e6b43Fariborz Jahanian  assert(VD->hasAttr<BlocksAttr>() &&
1551d016ec204c54d766c4561d43548551ffde0e6b43Fariborz Jahanian         "getBlockVarCopyInits - not __block var");
1552830937bc1100fba7682f7c32c40512085870f50cFariborz Jahanian  llvm::DenseMap<const VarDecl*, Expr*>::iterator
1553d016ec204c54d766c4561d43548551ffde0e6b43Fariborz Jahanian    I = BlockVarCopyInits.find(VD);
15541ceee5c42d5c410217f67d384eecc6ea4a2bba9bFariborz Jahanian  return (I != BlockVarCopyInits.end()) ? cast<Expr>(I->second) : 0;
15551ceee5c42d5c410217f67d384eecc6ea4a2bba9bFariborz Jahanian}
15561ceee5c42d5c410217f67d384eecc6ea4a2bba9bFariborz Jahanian
15571ceee5c42d5c410217f67d384eecc6ea4a2bba9bFariborz Jahanian/// \brief Set the copy inialization expression of a block var decl.
15581ceee5c42d5c410217f67d384eecc6ea4a2bba9bFariborz Jahanianvoid ASTContext::setBlockVarCopyInits(VarDecl*VD, Expr* Init) {
15591ceee5c42d5c410217f67d384eecc6ea4a2bba9bFariborz Jahanian  assert(VD && Init && "Passed null params");
1560d016ec204c54d766c4561d43548551ffde0e6b43Fariborz Jahanian  assert(VD->hasAttr<BlocksAttr>() &&
1561d016ec204c54d766c4561d43548551ffde0e6b43Fariborz Jahanian         "setBlockVarCopyInits - not __block var");
15621ceee5c42d5c410217f67d384eecc6ea4a2bba9bFariborz Jahanian  BlockVarCopyInits[VD] = Init;
15631ceee5c42d5c410217f67d384eecc6ea4a2bba9bFariborz Jahanian}
15641ceee5c42d5c410217f67d384eecc6ea4a2bba9bFariborz Jahanian
1565a93c934af4fbf97cbe8e649d82e68ccacfe57c95John McCallTypeSourceInfo *ASTContext::CreateTypeSourceInfo(QualType T,
15664ba2a17694148e16eaa8d3917f657ffcd3667be4Jay Foad                                                 unsigned DataSize) const {
1567109de5ead1dfcb3bc985cddb8cb3ed5bcecad88dJohn McCall  if (!DataSize)
1568109de5ead1dfcb3bc985cddb8cb3ed5bcecad88dJohn McCall    DataSize = TypeLoc::getFullDataSizeForType(T);
1569109de5ead1dfcb3bc985cddb8cb3ed5bcecad88dJohn McCall  else
1570109de5ead1dfcb3bc985cddb8cb3ed5bcecad88dJohn McCall    assert(DataSize == TypeLoc::getFullDataSizeForType(T) &&
1571a93c934af4fbf97cbe8e649d82e68ccacfe57c95John McCall           "incorrect data size provided to CreateTypeSourceInfo!");
1572109de5ead1dfcb3bc985cddb8cb3ed5bcecad88dJohn McCall
1573a93c934af4fbf97cbe8e649d82e68ccacfe57c95John McCall  TypeSourceInfo *TInfo =
1574a93c934af4fbf97cbe8e649d82e68ccacfe57c95John McCall    (TypeSourceInfo*)BumpAlloc.Allocate(sizeof(TypeSourceInfo) + DataSize, 8);
1575a93c934af4fbf97cbe8e649d82e68ccacfe57c95John McCall  new (TInfo) TypeSourceInfo(T);
1576a93c934af4fbf97cbe8e649d82e68ccacfe57c95John McCall  return TInfo;
1577b17166c8077cd900cca83a895c43b30ea6660598Argyrios Kyrtzidis}
1578b17166c8077cd900cca83a895c43b30ea6660598Argyrios Kyrtzidis
1579a93c934af4fbf97cbe8e649d82e68ccacfe57c95John McCallTypeSourceInfo *ASTContext::getTrivialTypeSourceInfo(QualType T,
15806952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor                                                     SourceLocation L) const {
1581a93c934af4fbf97cbe8e649d82e68ccacfe57c95John McCall  TypeSourceInfo *DI = CreateTypeSourceInfo(T);
1582c21c7e9c2cded68f91be15be6847c9649242dc17Douglas Gregor  DI->getTypeLoc().initialize(const_cast<ASTContext &>(*this), L);
1583a4eb74d4dfe126c686dc708fec444c85ffb73b47John McCall  return DI;
1584a4eb74d4dfe126c686dc708fec444c85ffb73b47John McCall}
1585a4eb74d4dfe126c686dc708fec444c85ffb73b47John McCall
1586b2dbbb99e12806eaaf53b7ccabc32f42b5719443Daniel Dunbarconst ASTRecordLayout &
15874ba2a17694148e16eaa8d3917f657ffcd3667be4Jay FoadASTContext::getASTObjCInterfaceLayout(const ObjCInterfaceDecl *D) const {
1588b2dbbb99e12806eaaf53b7ccabc32f42b5719443Daniel Dunbar  return getObjCLayout(D, 0);
1589b2dbbb99e12806eaaf53b7ccabc32f42b5719443Daniel Dunbar}
1590b2dbbb99e12806eaaf53b7ccabc32f42b5719443Daniel Dunbar
1591b2dbbb99e12806eaaf53b7ccabc32f42b5719443Daniel Dunbarconst ASTRecordLayout &
15924ba2a17694148e16eaa8d3917f657ffcd3667be4Jay FoadASTContext::getASTObjCImplementationLayout(
15934ba2a17694148e16eaa8d3917f657ffcd3667be4Jay Foad                                        const ObjCImplementationDecl *D) const {
1594b2dbbb99e12806eaaf53b7ccabc32f42b5719443Daniel Dunbar  return getObjCLayout(D->getClassInterface(), D);
1595b2dbbb99e12806eaaf53b7ccabc32f42b5719443Daniel Dunbar}
1596b2dbbb99e12806eaaf53b7ccabc32f42b5719443Daniel Dunbar
1597a7674d8a9a69f3f6fe16e70cf2a3b2b15fb7c43dChris Lattner//===----------------------------------------------------------------------===//
1598a7674d8a9a69f3f6fe16e70cf2a3b2b15fb7c43dChris Lattner//                   Type creation/memoization methods
1599a7674d8a9a69f3f6fe16e70cf2a3b2b15fb7c43dChris Lattner//===----------------------------------------------------------------------===//
1600a7674d8a9a69f3f6fe16e70cf2a3b2b15fb7c43dChris Lattner
16014ba2a17694148e16eaa8d3917f657ffcd3667be4Jay FoadQualType
16023b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCallASTContext::getExtQualType(const Type *baseType, Qualifiers quals) const {
16033b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall  unsigned fastQuals = quals.getFastQualifiers();
16043b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall  quals.removeFastQualifiers();
16050953e767ff7817f97b3ab20896b229891eeff45bJohn McCall
16060953e767ff7817f97b3ab20896b229891eeff45bJohn McCall  // Check if we've already instantiated this type.
16070953e767ff7817f97b3ab20896b229891eeff45bJohn McCall  llvm::FoldingSetNodeID ID;
16083b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall  ExtQuals::Profile(ID, baseType, quals);
16093b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall  void *insertPos = 0;
16103b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall  if (ExtQuals *eq = ExtQualNodes.FindNodeOrInsertPos(ID, insertPos)) {
16113b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall    assert(eq->getQualifiers() == quals);
16123b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall    return QualType(eq, fastQuals);
16130953e767ff7817f97b3ab20896b229891eeff45bJohn McCall  }
16140953e767ff7817f97b3ab20896b229891eeff45bJohn McCall
16153b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall  // If the base type is not canonical, make the appropriate canonical type.
16163b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall  QualType canon;
16173b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall  if (!baseType->isCanonicalUnqualified()) {
16183b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall    SplitQualType canonSplit = baseType->getCanonicalTypeInternal().split();
1619200fa53fd420aa8369586f569dbece04930ad6a3John McCall    canonSplit.Quals.addConsistentQualifiers(quals);
1620200fa53fd420aa8369586f569dbece04930ad6a3John McCall    canon = getExtQualType(canonSplit.Ty, canonSplit.Quals);
16213b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall
16223b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall    // Re-find the insert position.
16233b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall    (void) ExtQualNodes.FindNodeOrInsertPos(ID, insertPos);
16243b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall  }
16253b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall
16263b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall  ExtQuals *eq = new (*this, TypeAlignment) ExtQuals(baseType, canon, quals);
16273b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall  ExtQualNodes.InsertNode(eq, insertPos);
16283b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall  return QualType(eq, fastQuals);
16290953e767ff7817f97b3ab20896b229891eeff45bJohn McCall}
16300953e767ff7817f97b3ab20896b229891eeff45bJohn McCall
16314ba2a17694148e16eaa8d3917f657ffcd3667be4Jay FoadQualType
16324ba2a17694148e16eaa8d3917f657ffcd3667be4Jay FoadASTContext::getAddrSpaceQualType(QualType T, unsigned AddressSpace) const {
1633f52ab250ff92bc51a9ac9a8e19bd43b63a5f844fChris Lattner  QualType CanT = getCanonicalType(T);
1634f52ab250ff92bc51a9ac9a8e19bd43b63a5f844fChris Lattner  if (CanT.getAddressSpace() == AddressSpace)
1635f46699ce225811d8d9dbab9d00189a0e54469457Chris Lattner    return T;
1636b7d2553edd2532d29b98b9e76bcf6a62bc48b417Chris Lattner
16370953e767ff7817f97b3ab20896b229891eeff45bJohn McCall  // If we are composing extended qualifiers together, merge together
16380953e767ff7817f97b3ab20896b229891eeff45bJohn McCall  // into one ExtQuals node.
16390953e767ff7817f97b3ab20896b229891eeff45bJohn McCall  QualifierCollector Quals;
16400953e767ff7817f97b3ab20896b229891eeff45bJohn McCall  const Type *TypeNode = Quals.strip(T);
16411eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
16420953e767ff7817f97b3ab20896b229891eeff45bJohn McCall  // If this type already has an address space specified, it cannot get
16430953e767ff7817f97b3ab20896b229891eeff45bJohn McCall  // another one.
16440953e767ff7817f97b3ab20896b229891eeff45bJohn McCall  assert(!Quals.hasAddressSpace() &&
16450953e767ff7817f97b3ab20896b229891eeff45bJohn McCall         "Type cannot be in multiple addr spaces!");
16460953e767ff7817f97b3ab20896b229891eeff45bJohn McCall  Quals.addAddressSpace(AddressSpace);
16471eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
16480953e767ff7817f97b3ab20896b229891eeff45bJohn McCall  return getExtQualType(TypeNode, Quals);
1649ebb97e98c03f8d7034bd3748a10e35f39a95c289Christopher Lamb}
1650ebb97e98c03f8d7034bd3748a10e35f39a95c289Christopher Lamb
1651b7d2553edd2532d29b98b9e76bcf6a62bc48b417Chris LattnerQualType ASTContext::getObjCGCQualType(QualType T,
16524ba2a17694148e16eaa8d3917f657ffcd3667be4Jay Foad                                       Qualifiers::GC GCAttr) const {
1653d33d9c0cc0cfdcd0b10f35a6acdfb25da4a64f19Fariborz Jahanian  QualType CanT = getCanonicalType(T);
1654b7d2553edd2532d29b98b9e76bcf6a62bc48b417Chris Lattner  if (CanT.getObjCGCAttr() == GCAttr)
1655d33d9c0cc0cfdcd0b10f35a6acdfb25da4a64f19Fariborz Jahanian    return T;
16561eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
16577f040a9d817cd1c72b565e92abff473510bf9e1dJohn McCall  if (const PointerType *ptr = T->getAs<PointerType>()) {
16587f040a9d817cd1c72b565e92abff473510bf9e1dJohn McCall    QualType Pointee = ptr->getPointeeType();
165958f9f2c884af6b72d036b746a016d8031d31cb7aSteve Naroff    if (Pointee->isAnyPointerType()) {
16604027cd1b924e29784a49085b1717f35cdd719146Fariborz Jahanian      QualType ResultType = getObjCGCQualType(Pointee, GCAttr);
16614027cd1b924e29784a49085b1717f35cdd719146Fariborz Jahanian      return getPointerType(ResultType);
16624027cd1b924e29784a49085b1717f35cdd719146Fariborz Jahanian    }
16634027cd1b924e29784a49085b1717f35cdd719146Fariborz Jahanian  }
16641eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
16650953e767ff7817f97b3ab20896b229891eeff45bJohn McCall  // If we are composing extended qualifiers together, merge together
16660953e767ff7817f97b3ab20896b229891eeff45bJohn McCall  // into one ExtQuals node.
16670953e767ff7817f97b3ab20896b229891eeff45bJohn McCall  QualifierCollector Quals;
16680953e767ff7817f97b3ab20896b229891eeff45bJohn McCall  const Type *TypeNode = Quals.strip(T);
16691eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
16700953e767ff7817f97b3ab20896b229891eeff45bJohn McCall  // If this type already has an ObjCGC specified, it cannot get
16710953e767ff7817f97b3ab20896b229891eeff45bJohn McCall  // another one.
16720953e767ff7817f97b3ab20896b229891eeff45bJohn McCall  assert(!Quals.hasObjCGCAttr() &&
16730953e767ff7817f97b3ab20896b229891eeff45bJohn McCall         "Type cannot have multiple ObjCGCs!");
16740953e767ff7817f97b3ab20896b229891eeff45bJohn McCall  Quals.addObjCGCAttr(GCAttr);
16751eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
16760953e767ff7817f97b3ab20896b229891eeff45bJohn McCall  return getExtQualType(TypeNode, Quals);
1677d33d9c0cc0cfdcd0b10f35a6acdfb25da4a64f19Fariborz Jahanian}
1678a7674d8a9a69f3f6fe16e70cf2a3b2b15fb7c43dChris Lattner
1679e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCallconst FunctionType *ASTContext::adjustFunctionType(const FunctionType *T,
1680e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall                                                   FunctionType::ExtInfo Info) {
1681e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall  if (T->getExtInfo() == Info)
1682e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall    return T;
1683e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall
1684e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall  QualType Result;
1685e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall  if (const FunctionNoProtoType *FNPT = dyn_cast<FunctionNoProtoType>(T)) {
1686e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall    Result = getFunctionNoProtoType(FNPT->getResultType(), Info);
1687e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall  } else {
1688e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall    const FunctionProtoType *FPT = cast<FunctionProtoType>(T);
1689e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall    FunctionProtoType::ExtProtoInfo EPI = FPT->getExtProtoInfo();
1690e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall    EPI.ExtInfo = Info;
1691e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall    Result = getFunctionType(FPT->getResultType(), FPT->arg_type_begin(),
1692e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall                             FPT->getNumArgs(), EPI);
1693e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall  }
1694e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall
1695e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall  return cast<FunctionType>(Result.getTypePtr());
1696e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall}
1697e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall
16985f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// getComplexType - Return the uniqued reference to the type for a complex
16995f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// number with the specified element type.
17004ba2a17694148e16eaa8d3917f657ffcd3667be4Jay FoadQualType ASTContext::getComplexType(QualType T) const {
17015f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // Unique pointers, to guarantee there is only one pointer of a particular
17025f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // structure.
17035f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  llvm::FoldingSetNodeID ID;
17045f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  ComplexType::Profile(ID, T);
17051eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
17065f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  void *InsertPos = 0;
17075f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  if (ComplexType *CT = ComplexTypes.FindNodeOrInsertPos(ID, InsertPos))
17085f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    return QualType(CT, 0);
17091eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
17105f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // If the pointee type isn't canonical, this won't be a canonical type either,
17115f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // so fill in the canonical type field.
17125f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  QualType Canonical;
1713467b27b9a24bdc823218ad1ad0e37673b6cc1e83John McCall  if (!T.isCanonical()) {
1714f52ab250ff92bc51a9ac9a8e19bd43b63a5f844fChris Lattner    Canonical = getComplexType(getCanonicalType(T));
17151eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
17165f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    // Get the new insert position for the node we care about.
17175f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    ComplexType *NewIP = ComplexTypes.FindNodeOrInsertPos(ID, InsertPos);
1718c6ed729f669044f5072a49d79041f455d971ece3Jeffrey Yasskin    assert(NewIP == 0 && "Shouldn't be in the map!"); (void)NewIP;
17195f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
17206b304a0254a13f42390b865ff5ba668a49cc58aeJohn McCall  ComplexType *New = new (*this, TypeAlignment) ComplexType(T, Canonical);
17215f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  Types.push_back(New);
17225f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  ComplexTypes.InsertNode(New, InsertPos);
17235f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  return QualType(New, 0);
17245f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
17255f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
17265f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// getPointerType - Return the uniqued reference to the type for a pointer to
17275f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// the specified type.
17284ba2a17694148e16eaa8d3917f657ffcd3667be4Jay FoadQualType ASTContext::getPointerType(QualType T) const {
17295f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // Unique pointers, to guarantee there is only one pointer of a particular
17305f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // structure.
17315f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  llvm::FoldingSetNodeID ID;
17325f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  PointerType::Profile(ID, T);
17331eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
17345f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  void *InsertPos = 0;
17355f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  if (PointerType *PT = PointerTypes.FindNodeOrInsertPos(ID, InsertPos))
17365f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    return QualType(PT, 0);
17371eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
17385f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // If the pointee type isn't canonical, this won't be a canonical type either,
17395f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // so fill in the canonical type field.
17405f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  QualType Canonical;
1741467b27b9a24bdc823218ad1ad0e37673b6cc1e83John McCall  if (!T.isCanonical()) {
1742f52ab250ff92bc51a9ac9a8e19bd43b63a5f844fChris Lattner    Canonical = getPointerType(getCanonicalType(T));
17431eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
17445f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    // Get the new insert position for the node we care about.
17455f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    PointerType *NewIP = PointerTypes.FindNodeOrInsertPos(ID, InsertPos);
1746c6ed729f669044f5072a49d79041f455d971ece3Jeffrey Yasskin    assert(NewIP == 0 && "Shouldn't be in the map!"); (void)NewIP;
17475f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
17486b304a0254a13f42390b865ff5ba668a49cc58aeJohn McCall  PointerType *New = new (*this, TypeAlignment) PointerType(T, Canonical);
17495f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  Types.push_back(New);
17505f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  PointerTypes.InsertNode(New, InsertPos);
17515f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  return QualType(New, 0);
17525f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
17535f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
17541eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// getBlockPointerType - Return the uniqued reference to the type for
17555618bd4a52c45fbbb605e3ba885663b2164db8a3Steve Naroff/// a pointer to the specified block.
17564ba2a17694148e16eaa8d3917f657ffcd3667be4Jay FoadQualType ASTContext::getBlockPointerType(QualType T) const {
1757296e8d5fdcf9946f51e866adc8d281379e51efe9Steve Naroff  assert(T->isFunctionType() && "block of function types only");
1758296e8d5fdcf9946f51e866adc8d281379e51efe9Steve Naroff  // Unique pointers, to guarantee there is only one block of a particular
17595618bd4a52c45fbbb605e3ba885663b2164db8a3Steve Naroff  // structure.
17605618bd4a52c45fbbb605e3ba885663b2164db8a3Steve Naroff  llvm::FoldingSetNodeID ID;
17615618bd4a52c45fbbb605e3ba885663b2164db8a3Steve Naroff  BlockPointerType::Profile(ID, T);
17621eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
17635618bd4a52c45fbbb605e3ba885663b2164db8a3Steve Naroff  void *InsertPos = 0;
17645618bd4a52c45fbbb605e3ba885663b2164db8a3Steve Naroff  if (BlockPointerType *PT =
17655618bd4a52c45fbbb605e3ba885663b2164db8a3Steve Naroff        BlockPointerTypes.FindNodeOrInsertPos(ID, InsertPos))
17665618bd4a52c45fbbb605e3ba885663b2164db8a3Steve Naroff    return QualType(PT, 0);
17671eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
17681eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  // If the block pointee type isn't canonical, this won't be a canonical
17695618bd4a52c45fbbb605e3ba885663b2164db8a3Steve Naroff  // type either so fill in the canonical type field.
17705618bd4a52c45fbbb605e3ba885663b2164db8a3Steve Naroff  QualType Canonical;
1771467b27b9a24bdc823218ad1ad0e37673b6cc1e83John McCall  if (!T.isCanonical()) {
17725618bd4a52c45fbbb605e3ba885663b2164db8a3Steve Naroff    Canonical = getBlockPointerType(getCanonicalType(T));
17731eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
17745618bd4a52c45fbbb605e3ba885663b2164db8a3Steve Naroff    // Get the new insert position for the node we care about.
17755618bd4a52c45fbbb605e3ba885663b2164db8a3Steve Naroff    BlockPointerType *NewIP =
17765618bd4a52c45fbbb605e3ba885663b2164db8a3Steve Naroff      BlockPointerTypes.FindNodeOrInsertPos(ID, InsertPos);
1777c6ed729f669044f5072a49d79041f455d971ece3Jeffrey Yasskin    assert(NewIP == 0 && "Shouldn't be in the map!"); (void)NewIP;
17785618bd4a52c45fbbb605e3ba885663b2164db8a3Steve Naroff  }
17796b304a0254a13f42390b865ff5ba668a49cc58aeJohn McCall  BlockPointerType *New
17806b304a0254a13f42390b865ff5ba668a49cc58aeJohn McCall    = new (*this, TypeAlignment) BlockPointerType(T, Canonical);
17815618bd4a52c45fbbb605e3ba885663b2164db8a3Steve Naroff  Types.push_back(New);
17825618bd4a52c45fbbb605e3ba885663b2164db8a3Steve Naroff  BlockPointerTypes.InsertNode(New, InsertPos);
17835618bd4a52c45fbbb605e3ba885663b2164db8a3Steve Naroff  return QualType(New, 0);
17845618bd4a52c45fbbb605e3ba885663b2164db8a3Steve Naroff}
17855618bd4a52c45fbbb605e3ba885663b2164db8a3Steve Naroff
17867c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl/// getLValueReferenceType - Return the uniqued reference to the type for an
17877c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl/// lvalue reference to the specified type.
17884ba2a17694148e16eaa8d3917f657ffcd3667be4Jay FoadQualType
17894ba2a17694148e16eaa8d3917f657ffcd3667be4Jay FoadASTContext::getLValueReferenceType(QualType T, bool SpelledAsLValue) const {
17909625e44c0252485277a340746ed8ac950686156fDouglas Gregor  assert(getCanonicalType(T) != OverloadTy &&
17919625e44c0252485277a340746ed8ac950686156fDouglas Gregor         "Unresolved overloaded function type");
17929625e44c0252485277a340746ed8ac950686156fDouglas Gregor
17935f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // Unique pointers, to guarantee there is only one pointer of a particular
17945f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // structure.
17955f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  llvm::FoldingSetNodeID ID;
179654e14c4db764c0636160d26c5bbf491637c83a76John McCall  ReferenceType::Profile(ID, T, SpelledAsLValue);
17975f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
17985f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  void *InsertPos = 0;
17997c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl  if (LValueReferenceType *RT =
18007c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl        LValueReferenceTypes.FindNodeOrInsertPos(ID, InsertPos))
18015f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    return QualType(RT, 0);
18027c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl
180354e14c4db764c0636160d26c5bbf491637c83a76John McCall  const ReferenceType *InnerRef = T->getAs<ReferenceType>();
180454e14c4db764c0636160d26c5bbf491637c83a76John McCall
18055f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // If the referencee type isn't canonical, this won't be a canonical type
18065f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // either, so fill in the canonical type field.
18075f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  QualType Canonical;
180854e14c4db764c0636160d26c5bbf491637c83a76John McCall  if (!SpelledAsLValue || InnerRef || !T.isCanonical()) {
180954e14c4db764c0636160d26c5bbf491637c83a76John McCall    QualType PointeeType = (InnerRef ? InnerRef->getPointeeType() : T);
181054e14c4db764c0636160d26c5bbf491637c83a76John McCall    Canonical = getLValueReferenceType(getCanonicalType(PointeeType));
18117c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl
18127c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl    // Get the new insert position for the node we care about.
18137c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl    LValueReferenceType *NewIP =
18147c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl      LValueReferenceTypes.FindNodeOrInsertPos(ID, InsertPos);
1815c6ed729f669044f5072a49d79041f455d971ece3Jeffrey Yasskin    assert(NewIP == 0 && "Shouldn't be in the map!"); (void)NewIP;
18167c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl  }
18177c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl
18186b304a0254a13f42390b865ff5ba668a49cc58aeJohn McCall  LValueReferenceType *New
181954e14c4db764c0636160d26c5bbf491637c83a76John McCall    = new (*this, TypeAlignment) LValueReferenceType(T, Canonical,
182054e14c4db764c0636160d26c5bbf491637c83a76John McCall                                                     SpelledAsLValue);
18217c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl  Types.push_back(New);
18227c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl  LValueReferenceTypes.InsertNode(New, InsertPos);
182354e14c4db764c0636160d26c5bbf491637c83a76John McCall
18247c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl  return QualType(New, 0);
18257c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl}
18267c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl
18277c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl/// getRValueReferenceType - Return the uniqued reference to the type for an
18287c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl/// rvalue reference to the specified type.
18294ba2a17694148e16eaa8d3917f657ffcd3667be4Jay FoadQualType ASTContext::getRValueReferenceType(QualType T) const {
18307c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl  // Unique pointers, to guarantee there is only one pointer of a particular
18317c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl  // structure.
18327c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl  llvm::FoldingSetNodeID ID;
183354e14c4db764c0636160d26c5bbf491637c83a76John McCall  ReferenceType::Profile(ID, T, false);
18347c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl
18357c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl  void *InsertPos = 0;
18367c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl  if (RValueReferenceType *RT =
18377c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl        RValueReferenceTypes.FindNodeOrInsertPos(ID, InsertPos))
18387c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl    return QualType(RT, 0);
18397c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl
184054e14c4db764c0636160d26c5bbf491637c83a76John McCall  const ReferenceType *InnerRef = T->getAs<ReferenceType>();
184154e14c4db764c0636160d26c5bbf491637c83a76John McCall
18427c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl  // If the referencee type isn't canonical, this won't be a canonical type
18437c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl  // either, so fill in the canonical type field.
18447c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl  QualType Canonical;
184554e14c4db764c0636160d26c5bbf491637c83a76John McCall  if (InnerRef || !T.isCanonical()) {
184654e14c4db764c0636160d26c5bbf491637c83a76John McCall    QualType PointeeType = (InnerRef ? InnerRef->getPointeeType() : T);
184754e14c4db764c0636160d26c5bbf491637c83a76John McCall    Canonical = getRValueReferenceType(getCanonicalType(PointeeType));
18487c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl
18495f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    // Get the new insert position for the node we care about.
18507c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl    RValueReferenceType *NewIP =
18517c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl      RValueReferenceTypes.FindNodeOrInsertPos(ID, InsertPos);
1852c6ed729f669044f5072a49d79041f455d971ece3Jeffrey Yasskin    assert(NewIP == 0 && "Shouldn't be in the map!"); (void)NewIP;
18535f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
18545f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
18556b304a0254a13f42390b865ff5ba668a49cc58aeJohn McCall  RValueReferenceType *New
18566b304a0254a13f42390b865ff5ba668a49cc58aeJohn McCall    = new (*this, TypeAlignment) RValueReferenceType(T, Canonical);
18575f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  Types.push_back(New);
18587c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl  RValueReferenceTypes.InsertNode(New, InsertPos);
18595f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  return QualType(New, 0);
18605f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
18615f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
1862f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl/// getMemberPointerType - Return the uniqued reference to the type for a
1863f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl/// member pointer to the specified type, in the specified class.
18644ba2a17694148e16eaa8d3917f657ffcd3667be4Jay FoadQualType ASTContext::getMemberPointerType(QualType T, const Type *Cls) const {
1865f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl  // Unique pointers, to guarantee there is only one pointer of a particular
1866f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl  // structure.
1867f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl  llvm::FoldingSetNodeID ID;
1868f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl  MemberPointerType::Profile(ID, T, Cls);
1869f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl
1870f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl  void *InsertPos = 0;
1871f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl  if (MemberPointerType *PT =
1872f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl      MemberPointerTypes.FindNodeOrInsertPos(ID, InsertPos))
1873f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl    return QualType(PT, 0);
1874f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl
1875f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl  // If the pointee or class type isn't canonical, this won't be a canonical
1876f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl  // type either, so fill in the canonical type field.
1877f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl  QualType Canonical;
187887c12c4a4667279dacb3d4a93c64b49148a0ff79Douglas Gregor  if (!T.isCanonical() || !Cls->isCanonicalUnqualified()) {
1879f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl    Canonical = getMemberPointerType(getCanonicalType(T),getCanonicalType(Cls));
1880f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl
1881f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl    // Get the new insert position for the node we care about.
1882f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl    MemberPointerType *NewIP =
1883f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl      MemberPointerTypes.FindNodeOrInsertPos(ID, InsertPos);
1884c6ed729f669044f5072a49d79041f455d971ece3Jeffrey Yasskin    assert(NewIP == 0 && "Shouldn't be in the map!"); (void)NewIP;
1885f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl  }
18866b304a0254a13f42390b865ff5ba668a49cc58aeJohn McCall  MemberPointerType *New
18876b304a0254a13f42390b865ff5ba668a49cc58aeJohn McCall    = new (*this, TypeAlignment) MemberPointerType(T, Cls, Canonical);
1888f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl  Types.push_back(New);
1889f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl  MemberPointerTypes.InsertNode(New, InsertPos);
1890f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl  return QualType(New, 0);
1891f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl}
1892f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl
18931eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// getConstantArrayType - Return the unique reference to the type for an
1894fb22d96692c5240fb8d611290dbf7eeed3759c73Steve Naroff/// array of the specified element type.
18951eb4433ac451dc16f4133a88af2d002ac26c58efMike StumpQualType ASTContext::getConstantArrayType(QualType EltTy,
189638aeec7299c48cb79523f7f89776fb258c84aeeaChris Lattner                                          const llvm::APInt &ArySizeIn,
1897c9406125e2cac9208098655ac8058c095c2c3a65Steve Naroff                                          ArrayType::ArraySizeModifier ASM,
189863e7d25d2e6036616b42f744fd4a39cd5f911960Abramo Bagnara                                          unsigned IndexTypeQuals) const {
1899923d56d436f750bc1f29db50e641078725558a1bSebastian Redl  assert((EltTy->isDependentType() ||
1900923d56d436f750bc1f29db50e641078725558a1bSebastian Redl          EltTy->isIncompleteType() || EltTy->isConstantSizeType()) &&
1901587cbdfd95f4b0aaccc14b31f5debe85d5daf7edEli Friedman         "Constant array of VLAs is illegal!");
1902587cbdfd95f4b0aaccc14b31f5debe85d5daf7edEli Friedman
190338aeec7299c48cb79523f7f89776fb258c84aeeaChris Lattner  // Convert the array size into a canonical width matching the pointer size for
190438aeec7299c48cb79523f7f89776fb258c84aeeaChris Lattner  // the target.
190538aeec7299c48cb79523f7f89776fb258c84aeeaChris Lattner  llvm::APInt ArySize(ArySizeIn);
19069f71a8f4c7a182a5236da9e747d57cc1d1bd24c2Jay Foad  ArySize =
1907bcfd1f55bfbb3e5944cd5e03d07b343e280838c4Douglas Gregor    ArySize.zextOrTrunc(Target->getPointerWidth(getTargetAddressSpace(EltTy)));
19081eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
19095f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  llvm::FoldingSetNodeID ID;
191063e7d25d2e6036616b42f744fd4a39cd5f911960Abramo Bagnara  ConstantArrayType::Profile(ID, EltTy, ArySize, ASM, IndexTypeQuals);
19111eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
19125f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  void *InsertPos = 0;
19131eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  if (ConstantArrayType *ATP =
19147192f8e9592729882a09d84d77838db26e39ebd4Ted Kremenek      ConstantArrayTypes.FindNodeOrInsertPos(ID, InsertPos))
19155f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    return QualType(ATP, 0);
19161eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
19173b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall  // If the element type isn't canonical or has qualifiers, this won't
19183b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall  // be a canonical type either, so fill in the canonical type field.
19193b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall  QualType Canon;
19203b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall  if (!EltTy.isCanonical() || EltTy.hasLocalQualifiers()) {
19213b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall    SplitQualType canonSplit = getCanonicalType(EltTy).split();
1922200fa53fd420aa8369586f569dbece04930ad6a3John McCall    Canon = getConstantArrayType(QualType(canonSplit.Ty, 0), ArySize,
192363e7d25d2e6036616b42f744fd4a39cd5f911960Abramo Bagnara                                 ASM, IndexTypeQuals);
1924200fa53fd420aa8369586f569dbece04930ad6a3John McCall    Canon = getQualifiedType(Canon, canonSplit.Quals);
19253b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall
19265f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    // Get the new insert position for the node we care about.
19271eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    ConstantArrayType *NewIP =
19287192f8e9592729882a09d84d77838db26e39ebd4Ted Kremenek      ConstantArrayTypes.FindNodeOrInsertPos(ID, InsertPos);
1929c6ed729f669044f5072a49d79041f455d971ece3Jeffrey Yasskin    assert(NewIP == 0 && "Shouldn't be in the map!"); (void)NewIP;
19305f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
19311eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
19326b304a0254a13f42390b865ff5ba668a49cc58aeJohn McCall  ConstantArrayType *New = new(*this,TypeAlignment)
193363e7d25d2e6036616b42f744fd4a39cd5f911960Abramo Bagnara    ConstantArrayType(EltTy, Canon, ArySize, ASM, IndexTypeQuals);
19347192f8e9592729882a09d84d77838db26e39ebd4Ted Kremenek  ConstantArrayTypes.InsertNode(New, InsertPos);
19355f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  Types.push_back(New);
19365f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  return QualType(New, 0);
19375f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
19385f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
1939ce8890371fcdb983ae487c87fa40606a34896ff7John McCall/// getVariableArrayDecayedType - Turns the given type, which may be
1940ce8890371fcdb983ae487c87fa40606a34896ff7John McCall/// variably-modified, into the corresponding type with all the known
1941ce8890371fcdb983ae487c87fa40606a34896ff7John McCall/// sizes replaced with [*].
1942ce8890371fcdb983ae487c87fa40606a34896ff7John McCallQualType ASTContext::getVariableArrayDecayedType(QualType type) const {
1943ce8890371fcdb983ae487c87fa40606a34896ff7John McCall  // Vastly most common case.
1944ce8890371fcdb983ae487c87fa40606a34896ff7John McCall  if (!type->isVariablyModifiedType()) return type;
1945ce8890371fcdb983ae487c87fa40606a34896ff7John McCall
1946ce8890371fcdb983ae487c87fa40606a34896ff7John McCall  QualType result;
1947ce8890371fcdb983ae487c87fa40606a34896ff7John McCall
1948ce8890371fcdb983ae487c87fa40606a34896ff7John McCall  SplitQualType split = type.getSplitDesugaredType();
1949200fa53fd420aa8369586f569dbece04930ad6a3John McCall  const Type *ty = split.Ty;
1950ce8890371fcdb983ae487c87fa40606a34896ff7John McCall  switch (ty->getTypeClass()) {
1951ce8890371fcdb983ae487c87fa40606a34896ff7John McCall#define TYPE(Class, Base)
1952ce8890371fcdb983ae487c87fa40606a34896ff7John McCall#define ABSTRACT_TYPE(Class, Base)
1953ce8890371fcdb983ae487c87fa40606a34896ff7John McCall#define NON_CANONICAL_TYPE(Class, Base) case Type::Class:
1954ce8890371fcdb983ae487c87fa40606a34896ff7John McCall#include "clang/AST/TypeNodes.def"
1955ce8890371fcdb983ae487c87fa40606a34896ff7John McCall    llvm_unreachable("didn't desugar past all non-canonical types?");
1956ce8890371fcdb983ae487c87fa40606a34896ff7John McCall
1957ce8890371fcdb983ae487c87fa40606a34896ff7John McCall  // These types should never be variably-modified.
1958ce8890371fcdb983ae487c87fa40606a34896ff7John McCall  case Type::Builtin:
1959ce8890371fcdb983ae487c87fa40606a34896ff7John McCall  case Type::Complex:
1960ce8890371fcdb983ae487c87fa40606a34896ff7John McCall  case Type::Vector:
1961ce8890371fcdb983ae487c87fa40606a34896ff7John McCall  case Type::ExtVector:
1962ce8890371fcdb983ae487c87fa40606a34896ff7John McCall  case Type::DependentSizedExtVector:
1963ce8890371fcdb983ae487c87fa40606a34896ff7John McCall  case Type::ObjCObject:
1964ce8890371fcdb983ae487c87fa40606a34896ff7John McCall  case Type::ObjCInterface:
1965ce8890371fcdb983ae487c87fa40606a34896ff7John McCall  case Type::ObjCObjectPointer:
1966ce8890371fcdb983ae487c87fa40606a34896ff7John McCall  case Type::Record:
1967ce8890371fcdb983ae487c87fa40606a34896ff7John McCall  case Type::Enum:
1968ce8890371fcdb983ae487c87fa40606a34896ff7John McCall  case Type::UnresolvedUsing:
1969ce8890371fcdb983ae487c87fa40606a34896ff7John McCall  case Type::TypeOfExpr:
1970ce8890371fcdb983ae487c87fa40606a34896ff7John McCall  case Type::TypeOf:
1971ce8890371fcdb983ae487c87fa40606a34896ff7John McCall  case Type::Decltype:
1972ca63c200346c0ca9e00194ec6e34a5a7b0ed9321Sean Hunt  case Type::UnaryTransform:
1973ce8890371fcdb983ae487c87fa40606a34896ff7John McCall  case Type::DependentName:
1974ce8890371fcdb983ae487c87fa40606a34896ff7John McCall  case Type::InjectedClassName:
1975ce8890371fcdb983ae487c87fa40606a34896ff7John McCall  case Type::TemplateSpecialization:
1976ce8890371fcdb983ae487c87fa40606a34896ff7John McCall  case Type::DependentTemplateSpecialization:
1977ce8890371fcdb983ae487c87fa40606a34896ff7John McCall  case Type::TemplateTypeParm:
1978ce8890371fcdb983ae487c87fa40606a34896ff7John McCall  case Type::SubstTemplateTypeParmPack:
197934b41d939a1328f484511c6002ba2456db879a29Richard Smith  case Type::Auto:
1980ce8890371fcdb983ae487c87fa40606a34896ff7John McCall  case Type::PackExpansion:
1981ce8890371fcdb983ae487c87fa40606a34896ff7John McCall    llvm_unreachable("type should never be variably-modified");
1982ce8890371fcdb983ae487c87fa40606a34896ff7John McCall
1983ce8890371fcdb983ae487c87fa40606a34896ff7John McCall  // These types can be variably-modified but should never need to
1984ce8890371fcdb983ae487c87fa40606a34896ff7John McCall  // further decay.
1985ce8890371fcdb983ae487c87fa40606a34896ff7John McCall  case Type::FunctionNoProto:
1986ce8890371fcdb983ae487c87fa40606a34896ff7John McCall  case Type::FunctionProto:
1987ce8890371fcdb983ae487c87fa40606a34896ff7John McCall  case Type::BlockPointer:
1988ce8890371fcdb983ae487c87fa40606a34896ff7John McCall  case Type::MemberPointer:
1989ce8890371fcdb983ae487c87fa40606a34896ff7John McCall    return type;
1990ce8890371fcdb983ae487c87fa40606a34896ff7John McCall
1991ce8890371fcdb983ae487c87fa40606a34896ff7John McCall  // These types can be variably-modified.  All these modifications
1992ce8890371fcdb983ae487c87fa40606a34896ff7John McCall  // preserve structure except as noted by comments.
1993ce8890371fcdb983ae487c87fa40606a34896ff7John McCall  // TODO: if we ever care about optimizing VLAs, there are no-op
1994ce8890371fcdb983ae487c87fa40606a34896ff7John McCall  // optimizations available here.
1995ce8890371fcdb983ae487c87fa40606a34896ff7John McCall  case Type::Pointer:
1996ce8890371fcdb983ae487c87fa40606a34896ff7John McCall    result = getPointerType(getVariableArrayDecayedType(
1997ce8890371fcdb983ae487c87fa40606a34896ff7John McCall                              cast<PointerType>(ty)->getPointeeType()));
1998ce8890371fcdb983ae487c87fa40606a34896ff7John McCall    break;
1999ce8890371fcdb983ae487c87fa40606a34896ff7John McCall
2000ce8890371fcdb983ae487c87fa40606a34896ff7John McCall  case Type::LValueReference: {
2001ce8890371fcdb983ae487c87fa40606a34896ff7John McCall    const LValueReferenceType *lv = cast<LValueReferenceType>(ty);
2002ce8890371fcdb983ae487c87fa40606a34896ff7John McCall    result = getLValueReferenceType(
2003ce8890371fcdb983ae487c87fa40606a34896ff7John McCall                 getVariableArrayDecayedType(lv->getPointeeType()),
2004ce8890371fcdb983ae487c87fa40606a34896ff7John McCall                                    lv->isSpelledAsLValue());
2005ce8890371fcdb983ae487c87fa40606a34896ff7John McCall    break;
2006745da3a5bb4ea35f93f50301e7fbbb7d78d3b6bbFariborz Jahanian  }
2007ce8890371fcdb983ae487c87fa40606a34896ff7John McCall
2008ce8890371fcdb983ae487c87fa40606a34896ff7John McCall  case Type::RValueReference: {
2009ce8890371fcdb983ae487c87fa40606a34896ff7John McCall    const RValueReferenceType *lv = cast<RValueReferenceType>(ty);
2010ce8890371fcdb983ae487c87fa40606a34896ff7John McCall    result = getRValueReferenceType(
2011ce8890371fcdb983ae487c87fa40606a34896ff7John McCall                 getVariableArrayDecayedType(lv->getPointeeType()));
2012ce8890371fcdb983ae487c87fa40606a34896ff7John McCall    break;
2013745da3a5bb4ea35f93f50301e7fbbb7d78d3b6bbFariborz Jahanian  }
2014745da3a5bb4ea35f93f50301e7fbbb7d78d3b6bbFariborz Jahanian
2015b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman  case Type::Atomic: {
2016b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman    const AtomicType *at = cast<AtomicType>(ty);
2017b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman    result = getAtomicType(getVariableArrayDecayedType(at->getValueType()));
2018b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman    break;
2019b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman  }
2020b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman
2021ce8890371fcdb983ae487c87fa40606a34896ff7John McCall  case Type::ConstantArray: {
2022ce8890371fcdb983ae487c87fa40606a34896ff7John McCall    const ConstantArrayType *cat = cast<ConstantArrayType>(ty);
2023ce8890371fcdb983ae487c87fa40606a34896ff7John McCall    result = getConstantArrayType(
2024ce8890371fcdb983ae487c87fa40606a34896ff7John McCall                 getVariableArrayDecayedType(cat->getElementType()),
2025ce8890371fcdb983ae487c87fa40606a34896ff7John McCall                                  cat->getSize(),
2026ce8890371fcdb983ae487c87fa40606a34896ff7John McCall                                  cat->getSizeModifier(),
2027ce8890371fcdb983ae487c87fa40606a34896ff7John McCall                                  cat->getIndexTypeCVRQualifiers());
2028ce8890371fcdb983ae487c87fa40606a34896ff7John McCall    break;
2029745da3a5bb4ea35f93f50301e7fbbb7d78d3b6bbFariborz Jahanian  }
2030745da3a5bb4ea35f93f50301e7fbbb7d78d3b6bbFariborz Jahanian
2031ce8890371fcdb983ae487c87fa40606a34896ff7John McCall  case Type::DependentSizedArray: {
2032ce8890371fcdb983ae487c87fa40606a34896ff7John McCall    const DependentSizedArrayType *dat = cast<DependentSizedArrayType>(ty);
2033ce8890371fcdb983ae487c87fa40606a34896ff7John McCall    result = getDependentSizedArrayType(
2034ce8890371fcdb983ae487c87fa40606a34896ff7John McCall                 getVariableArrayDecayedType(dat->getElementType()),
2035ce8890371fcdb983ae487c87fa40606a34896ff7John McCall                                        dat->getSizeExpr(),
2036ce8890371fcdb983ae487c87fa40606a34896ff7John McCall                                        dat->getSizeModifier(),
2037ce8890371fcdb983ae487c87fa40606a34896ff7John McCall                                        dat->getIndexTypeCVRQualifiers(),
2038ce8890371fcdb983ae487c87fa40606a34896ff7John McCall                                        dat->getBracketsRange());
2039ce8890371fcdb983ae487c87fa40606a34896ff7John McCall    break;
2040ce8890371fcdb983ae487c87fa40606a34896ff7John McCall  }
2041ce8890371fcdb983ae487c87fa40606a34896ff7John McCall
2042ce8890371fcdb983ae487c87fa40606a34896ff7John McCall  // Turn incomplete types into [*] types.
2043ce8890371fcdb983ae487c87fa40606a34896ff7John McCall  case Type::IncompleteArray: {
2044ce8890371fcdb983ae487c87fa40606a34896ff7John McCall    const IncompleteArrayType *iat = cast<IncompleteArrayType>(ty);
2045ce8890371fcdb983ae487c87fa40606a34896ff7John McCall    result = getVariableArrayType(
2046ce8890371fcdb983ae487c87fa40606a34896ff7John McCall                 getVariableArrayDecayedType(iat->getElementType()),
2047ce8890371fcdb983ae487c87fa40606a34896ff7John McCall                                  /*size*/ 0,
2048ce8890371fcdb983ae487c87fa40606a34896ff7John McCall                                  ArrayType::Normal,
2049ce8890371fcdb983ae487c87fa40606a34896ff7John McCall                                  iat->getIndexTypeCVRQualifiers(),
2050ce8890371fcdb983ae487c87fa40606a34896ff7John McCall                                  SourceRange());
2051ce8890371fcdb983ae487c87fa40606a34896ff7John McCall    break;
2052ce8890371fcdb983ae487c87fa40606a34896ff7John McCall  }
2053ce8890371fcdb983ae487c87fa40606a34896ff7John McCall
2054ce8890371fcdb983ae487c87fa40606a34896ff7John McCall  // Turn VLA types into [*] types.
2055ce8890371fcdb983ae487c87fa40606a34896ff7John McCall  case Type::VariableArray: {
2056ce8890371fcdb983ae487c87fa40606a34896ff7John McCall    const VariableArrayType *vat = cast<VariableArrayType>(ty);
2057ce8890371fcdb983ae487c87fa40606a34896ff7John McCall    result = getVariableArrayType(
2058ce8890371fcdb983ae487c87fa40606a34896ff7John McCall                 getVariableArrayDecayedType(vat->getElementType()),
2059ce8890371fcdb983ae487c87fa40606a34896ff7John McCall                                  /*size*/ 0,
2060ce8890371fcdb983ae487c87fa40606a34896ff7John McCall                                  ArrayType::Star,
2061ce8890371fcdb983ae487c87fa40606a34896ff7John McCall                                  vat->getIndexTypeCVRQualifiers(),
2062ce8890371fcdb983ae487c87fa40606a34896ff7John McCall                                  vat->getBracketsRange());
2063ce8890371fcdb983ae487c87fa40606a34896ff7John McCall    break;
2064ce8890371fcdb983ae487c87fa40606a34896ff7John McCall  }
2065ce8890371fcdb983ae487c87fa40606a34896ff7John McCall  }
2066ce8890371fcdb983ae487c87fa40606a34896ff7John McCall
2067ce8890371fcdb983ae487c87fa40606a34896ff7John McCall  // Apply the top-level qualifiers from the original.
2068200fa53fd420aa8369586f569dbece04930ad6a3John McCall  return getQualifiedType(result, split.Quals);
2069ce8890371fcdb983ae487c87fa40606a34896ff7John McCall}
2070745da3a5bb4ea35f93f50301e7fbbb7d78d3b6bbFariborz Jahanian
2071bdbf7b030a3e0ddb95240076683830e6f78c79a5Steve Naroff/// getVariableArrayType - Returns a non-unique reference to the type for a
2072bdbf7b030a3e0ddb95240076683830e6f78c79a5Steve Naroff/// variable array of the specified element type.
20737e7eb3da052a6d80ddf2377cab0384c798f73f75Douglas GregorQualType ASTContext::getVariableArrayType(QualType EltTy,
20747e7eb3da052a6d80ddf2377cab0384c798f73f75Douglas Gregor                                          Expr *NumElts,
2075c9406125e2cac9208098655ac8058c095c2c3a65Steve Naroff                                          ArrayType::ArraySizeModifier ASM,
207663e7d25d2e6036616b42f744fd4a39cd5f911960Abramo Bagnara                                          unsigned IndexTypeQuals,
20774ba2a17694148e16eaa8d3917f657ffcd3667be4Jay Foad                                          SourceRange Brackets) const {
2078c5773c4b8ce1ed6ed5c7112c9020c954a47dce96Eli Friedman  // Since we don't unique expressions, it isn't possible to unique VLA's
2079c5773c4b8ce1ed6ed5c7112c9020c954a47dce96Eli Friedman  // that have an expression provided for their size.
20803b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall  QualType Canon;
2081715e9c8a39437347e838aa108df443fe1086d359Douglas Gregor
20823b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall  // Be sure to pull qualifiers off the element type.
20833b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall  if (!EltTy.isCanonical() || EltTy.hasLocalQualifiers()) {
20843b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall    SplitQualType canonSplit = getCanonicalType(EltTy).split();
2085200fa53fd420aa8369586f569dbece04930ad6a3John McCall    Canon = getVariableArrayType(QualType(canonSplit.Ty, 0), NumElts, ASM,
208663e7d25d2e6036616b42f744fd4a39cd5f911960Abramo Bagnara                                 IndexTypeQuals, Brackets);
2087200fa53fd420aa8369586f569dbece04930ad6a3John McCall    Canon = getQualifiedType(Canon, canonSplit.Quals);
2088715e9c8a39437347e838aa108df443fe1086d359Douglas Gregor  }
2089715e9c8a39437347e838aa108df443fe1086d359Douglas Gregor
20906b304a0254a13f42390b865ff5ba668a49cc58aeJohn McCall  VariableArrayType *New = new(*this, TypeAlignment)
209163e7d25d2e6036616b42f744fd4a39cd5f911960Abramo Bagnara    VariableArrayType(EltTy, Canon, NumElts, ASM, IndexTypeQuals, Brackets);
2092c5773c4b8ce1ed6ed5c7112c9020c954a47dce96Eli Friedman
2093c5773c4b8ce1ed6ed5c7112c9020c954a47dce96Eli Friedman  VariableArrayTypes.push_back(New);
2094c5773c4b8ce1ed6ed5c7112c9020c954a47dce96Eli Friedman  Types.push_back(New);
2095c5773c4b8ce1ed6ed5c7112c9020c954a47dce96Eli Friedman  return QualType(New, 0);
2096c5773c4b8ce1ed6ed5c7112c9020c954a47dce96Eli Friedman}
2097c5773c4b8ce1ed6ed5c7112c9020c954a47dce96Eli Friedman
2098898574e7496ba8fd76290079d3a9d06954992734Douglas Gregor/// getDependentSizedArrayType - Returns a non-unique reference to
2099898574e7496ba8fd76290079d3a9d06954992734Douglas Gregor/// the type for a dependently-sized array of the specified element
210004d4beee4b86af20a9e4457023d3925cab8f9908Douglas Gregor/// type.
21013b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCallQualType ASTContext::getDependentSizedArrayType(QualType elementType,
21023b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall                                                Expr *numElements,
2103898574e7496ba8fd76290079d3a9d06954992734Douglas Gregor                                                ArrayType::ArraySizeModifier ASM,
21043b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall                                                unsigned elementTypeQuals,
21053b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall                                                SourceRange brackets) const {
21063b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall  assert((!numElements || numElements->isTypeDependent() ||
21073b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall          numElements->isValueDependent()) &&
2108898574e7496ba8fd76290079d3a9d06954992734Douglas Gregor         "Size must be type- or value-dependent!");
2109898574e7496ba8fd76290079d3a9d06954992734Douglas Gregor
21103b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall  // Dependently-sized array types that do not have a specified number
21113b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall  // of elements will have their sizes deduced from a dependent
21123b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall  // initializer.  We do no canonicalization here at all, which is okay
21133b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall  // because they can't be used in most locations.
21143b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall  if (!numElements) {
21153b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall    DependentSizedArrayType *newType
21163b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall      = new (*this, TypeAlignment)
21173b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall          DependentSizedArrayType(*this, elementType, QualType(),
21183b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall                                  numElements, ASM, elementTypeQuals,
21193b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall                                  brackets);
21203b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall    Types.push_back(newType);
21213b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall    return QualType(newType, 0);
2122cb78d8852a454684c987220132cdb5e54dd00121Douglas Gregor  }
2123cb78d8852a454684c987220132cdb5e54dd00121Douglas Gregor
21243b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall  // Otherwise, we actually build a new type every time, but we
21253b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall  // also build a canonical type.
21261eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
21273b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall  SplitQualType canonElementType = getCanonicalType(elementType).split();
2128898574e7496ba8fd76290079d3a9d06954992734Douglas Gregor
21293b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall  void *insertPos = 0;
21303b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall  llvm::FoldingSetNodeID ID;
21313b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall  DependentSizedArrayType::Profile(ID, *this,
2132200fa53fd420aa8369586f569dbece04930ad6a3John McCall                                   QualType(canonElementType.Ty, 0),
21333b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall                                   ASM, elementTypeQuals, numElements);
21343b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall
21353b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall  // Look for an existing type with these properties.
21363b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall  DependentSizedArrayType *canonTy =
21373b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall    DependentSizedArrayTypes.FindNodeOrInsertPos(ID, insertPos);
21383b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall
21393b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall  // If we don't have one, build one.
21403b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall  if (!canonTy) {
21413b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall    canonTy = new (*this, TypeAlignment)
2142200fa53fd420aa8369586f569dbece04930ad6a3John McCall      DependentSizedArrayType(*this, QualType(canonElementType.Ty, 0),
21433b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall                              QualType(), numElements, ASM, elementTypeQuals,
21443b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall                              brackets);
21453b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall    DependentSizedArrayTypes.InsertNode(canonTy, insertPos);
21463b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall    Types.push_back(canonTy);
21473b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall  }
21483b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall
21493b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall  // Apply qualifiers from the element type to the array.
21503b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall  QualType canon = getQualifiedType(QualType(canonTy,0),
2151200fa53fd420aa8369586f569dbece04930ad6a3John McCall                                    canonElementType.Quals);
21523b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall
21533b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall  // If we didn't need extra canonicalization for the element type,
21543b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall  // then just use that as our result.
2155200fa53fd420aa8369586f569dbece04930ad6a3John McCall  if (QualType(canonElementType.Ty, 0) == elementType)
21563b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall    return canon;
21573b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall
21583b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall  // Otherwise, we need to build a type which follows the spelling
21593b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall  // of the element type.
21603b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall  DependentSizedArrayType *sugaredType
21613b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall    = new (*this, TypeAlignment)
21623b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall        DependentSizedArrayType(*this, elementType, canon, numElements,
21633b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall                                ASM, elementTypeQuals, brackets);
21643b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall  Types.push_back(sugaredType);
21653b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall  return QualType(sugaredType, 0);
21663b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall}
21673b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall
21683b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCallQualType ASTContext::getIncompleteArrayType(QualType elementType,
2169c5773c4b8ce1ed6ed5c7112c9020c954a47dce96Eli Friedman                                            ArrayType::ArraySizeModifier ASM,
21703b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall                                            unsigned elementTypeQuals) const {
2171c5773c4b8ce1ed6ed5c7112c9020c954a47dce96Eli Friedman  llvm::FoldingSetNodeID ID;
21723b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall  IncompleteArrayType::Profile(ID, elementType, ASM, elementTypeQuals);
2173c5773c4b8ce1ed6ed5c7112c9020c954a47dce96Eli Friedman
21743b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall  void *insertPos = 0;
21753b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall  if (IncompleteArrayType *iat =
21763b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall       IncompleteArrayTypes.FindNodeOrInsertPos(ID, insertPos))
21773b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall    return QualType(iat, 0);
2178c5773c4b8ce1ed6ed5c7112c9020c954a47dce96Eli Friedman
2179c5773c4b8ce1ed6ed5c7112c9020c954a47dce96Eli Friedman  // If the element type isn't canonical, this won't be a canonical type
21803b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall  // either, so fill in the canonical type field.  We also have to pull
21813b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall  // qualifiers off the element type.
21823b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall  QualType canon;
2183c5773c4b8ce1ed6ed5c7112c9020c954a47dce96Eli Friedman
21843b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall  if (!elementType.isCanonical() || elementType.hasLocalQualifiers()) {
21853b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall    SplitQualType canonSplit = getCanonicalType(elementType).split();
2186200fa53fd420aa8369586f569dbece04930ad6a3John McCall    canon = getIncompleteArrayType(QualType(canonSplit.Ty, 0),
21873b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall                                   ASM, elementTypeQuals);
2188200fa53fd420aa8369586f569dbece04930ad6a3John McCall    canon = getQualifiedType(canon, canonSplit.Quals);
2189c5773c4b8ce1ed6ed5c7112c9020c954a47dce96Eli Friedman
2190c5773c4b8ce1ed6ed5c7112c9020c954a47dce96Eli Friedman    // Get the new insert position for the node we care about.
21913b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall    IncompleteArrayType *existing =
21923b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall      IncompleteArrayTypes.FindNodeOrInsertPos(ID, insertPos);
21933b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall    assert(!existing && "Shouldn't be in the map!"); (void) existing;
21942bd24ba6d10f8c811c8e2a57c8397e07082ba497Ted Kremenek  }
2195c5773c4b8ce1ed6ed5c7112c9020c954a47dce96Eli Friedman
21963b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall  IncompleteArrayType *newType = new (*this, TypeAlignment)
21973b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall    IncompleteArrayType(elementType, canon, ASM, elementTypeQuals);
2198c5773c4b8ce1ed6ed5c7112c9020c954a47dce96Eli Friedman
21993b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall  IncompleteArrayTypes.InsertNode(newType, insertPos);
22003b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall  Types.push_back(newType);
22013b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall  return QualType(newType, 0);
2202fb22d96692c5240fb8d611290dbf7eeed3759c73Steve Naroff}
2203fb22d96692c5240fb8d611290dbf7eeed3759c73Steve Naroff
220473322924127c873c13101b705dd823f5539ffa5fSteve Naroff/// getVectorType - Return the unique reference to a vector type of
220573322924127c873c13101b705dd823f5539ffa5fSteve Naroff/// the specified element type and size. VectorType must be a built-in type.
220682287d19ded35248c4ce6a425ce74116a13ce44eJohn ThompsonQualType ASTContext::getVectorType(QualType vecType, unsigned NumElts,
22074ba2a17694148e16eaa8d3917f657ffcd3667be4Jay Foad                                   VectorType::VectorKind VecKind) const {
22083b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall  assert(vecType->isBuiltinType());
22091eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
22105f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // Check if we've already instantiated a vector of this type.
22115f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  llvm::FoldingSetNodeID ID;
2212e86d78cf4754a6aef2cf9a33d847aa15338e276fBob Wilson  VectorType::Profile(ID, vecType, NumElts, Type::Vector, VecKind);
2213788b0fd67e1992f23555454efcdb16a19dfefac3Chris Lattner
22145f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  void *InsertPos = 0;
22155f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  if (VectorType *VTP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos))
22165f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    return QualType(VTP, 0);
22175f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
22185f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // If the element type isn't canonical, this won't be a canonical type either,
22195f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // so fill in the canonical type field.
22205f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  QualType Canonical;
2221255210ef415b9893f0e3794e8d9a704194c12f3cDouglas Gregor  if (!vecType.isCanonical()) {
2222231da7eb3dd13007e5e40fffe48998e5ef284e06Bob Wilson    Canonical = getVectorType(getCanonicalType(vecType), NumElts, VecKind);
22231eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
22245f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    // Get the new insert position for the node we care about.
22255f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    VectorType *NewIP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos);
2226c6ed729f669044f5072a49d79041f455d971ece3Jeffrey Yasskin    assert(NewIP == 0 && "Shouldn't be in the map!"); (void)NewIP;
22275f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
22286b304a0254a13f42390b865ff5ba668a49cc58aeJohn McCall  VectorType *New = new (*this, TypeAlignment)
2229e86d78cf4754a6aef2cf9a33d847aa15338e276fBob Wilson    VectorType(vecType, NumElts, Canonical, VecKind);
22305f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  VectorTypes.InsertNode(New, InsertPos);
22315f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  Types.push_back(New);
22325f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  return QualType(New, 0);
22335f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
22345f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
2235213541a68a3e137d11d2cefb612c6cdb410d7e8eNate Begeman/// getExtVectorType - Return the unique reference to an extended vector type of
223673322924127c873c13101b705dd823f5539ffa5fSteve Naroff/// the specified element type and size. VectorType must be a built-in type.
22374ba2a17694148e16eaa8d3917f657ffcd3667be4Jay FoadQualType
22384ba2a17694148e16eaa8d3917f657ffcd3667be4Jay FoadASTContext::getExtVectorType(QualType vecType, unsigned NumElts) const {
22394ac01401b1ec602a1f58c217544d3dcb5fcbd7f1Douglas Gregor  assert(vecType->isBuiltinType() || vecType->isDependentType());
22401eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
224173322924127c873c13101b705dd823f5539ffa5fSteve Naroff  // Check if we've already instantiated a vector of this type.
224273322924127c873c13101b705dd823f5539ffa5fSteve Naroff  llvm::FoldingSetNodeID ID;
2243788b0fd67e1992f23555454efcdb16a19dfefac3Chris Lattner  VectorType::Profile(ID, vecType, NumElts, Type::ExtVector,
2244e86d78cf4754a6aef2cf9a33d847aa15338e276fBob Wilson                      VectorType::GenericVector);
224573322924127c873c13101b705dd823f5539ffa5fSteve Naroff  void *InsertPos = 0;
224673322924127c873c13101b705dd823f5539ffa5fSteve Naroff  if (VectorType *VTP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos))
224773322924127c873c13101b705dd823f5539ffa5fSteve Naroff    return QualType(VTP, 0);
224873322924127c873c13101b705dd823f5539ffa5fSteve Naroff
224973322924127c873c13101b705dd823f5539ffa5fSteve Naroff  // If the element type isn't canonical, this won't be a canonical type either,
225073322924127c873c13101b705dd823f5539ffa5fSteve Naroff  // so fill in the canonical type field.
225173322924127c873c13101b705dd823f5539ffa5fSteve Naroff  QualType Canonical;
2252467b27b9a24bdc823218ad1ad0e37673b6cc1e83John McCall  if (!vecType.isCanonical()) {
2253213541a68a3e137d11d2cefb612c6cdb410d7e8eNate Begeman    Canonical = getExtVectorType(getCanonicalType(vecType), NumElts);
22541eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
225573322924127c873c13101b705dd823f5539ffa5fSteve Naroff    // Get the new insert position for the node we care about.
225673322924127c873c13101b705dd823f5539ffa5fSteve Naroff    VectorType *NewIP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos);
2257c6ed729f669044f5072a49d79041f455d971ece3Jeffrey Yasskin    assert(NewIP == 0 && "Shouldn't be in the map!"); (void)NewIP;
225873322924127c873c13101b705dd823f5539ffa5fSteve Naroff  }
22596b304a0254a13f42390b865ff5ba668a49cc58aeJohn McCall  ExtVectorType *New = new (*this, TypeAlignment)
22606b304a0254a13f42390b865ff5ba668a49cc58aeJohn McCall    ExtVectorType(vecType, NumElts, Canonical);
226173322924127c873c13101b705dd823f5539ffa5fSteve Naroff  VectorTypes.InsertNode(New, InsertPos);
226273322924127c873c13101b705dd823f5539ffa5fSteve Naroff  Types.push_back(New);
226373322924127c873c13101b705dd823f5539ffa5fSteve Naroff  return QualType(New, 0);
226473322924127c873c13101b705dd823f5539ffa5fSteve Naroff}
226573322924127c873c13101b705dd823f5539ffa5fSteve Naroff
22664ba2a17694148e16eaa8d3917f657ffcd3667be4Jay FoadQualType
22674ba2a17694148e16eaa8d3917f657ffcd3667be4Jay FoadASTContext::getDependentSizedExtVectorType(QualType vecType,
22684ba2a17694148e16eaa8d3917f657ffcd3667be4Jay Foad                                           Expr *SizeExpr,
22694ba2a17694148e16eaa8d3917f657ffcd3667be4Jay Foad                                           SourceLocation AttrLoc) const {
22702ec09f1dc123e1942ed756e8ee4fef86451eac9eDouglas Gregor  llvm::FoldingSetNodeID ID;
22711eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  DependentSizedExtVectorType::Profile(ID, *this, getCanonicalType(vecType),
22722ec09f1dc123e1942ed756e8ee4fef86451eac9eDouglas Gregor                                       SizeExpr);
22731eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
22742ec09f1dc123e1942ed756e8ee4fef86451eac9eDouglas Gregor  void *InsertPos = 0;
22752ec09f1dc123e1942ed756e8ee4fef86451eac9eDouglas Gregor  DependentSizedExtVectorType *Canon
22762ec09f1dc123e1942ed756e8ee4fef86451eac9eDouglas Gregor    = DependentSizedExtVectorTypes.FindNodeOrInsertPos(ID, InsertPos);
22772ec09f1dc123e1942ed756e8ee4fef86451eac9eDouglas Gregor  DependentSizedExtVectorType *New;
22782ec09f1dc123e1942ed756e8ee4fef86451eac9eDouglas Gregor  if (Canon) {
22792ec09f1dc123e1942ed756e8ee4fef86451eac9eDouglas Gregor    // We already have a canonical version of this array type; use it as
22802ec09f1dc123e1942ed756e8ee4fef86451eac9eDouglas Gregor    // the canonical type for a newly-built type.
22816b304a0254a13f42390b865ff5ba668a49cc58aeJohn McCall    New = new (*this, TypeAlignment)
22826b304a0254a13f42390b865ff5ba668a49cc58aeJohn McCall      DependentSizedExtVectorType(*this, vecType, QualType(Canon, 0),
22836b304a0254a13f42390b865ff5ba668a49cc58aeJohn McCall                                  SizeExpr, AttrLoc);
22842ec09f1dc123e1942ed756e8ee4fef86451eac9eDouglas Gregor  } else {
22852ec09f1dc123e1942ed756e8ee4fef86451eac9eDouglas Gregor    QualType CanonVecTy = getCanonicalType(vecType);
22862ec09f1dc123e1942ed756e8ee4fef86451eac9eDouglas Gregor    if (CanonVecTy == vecType) {
22876b304a0254a13f42390b865ff5ba668a49cc58aeJohn McCall      New = new (*this, TypeAlignment)
22886b304a0254a13f42390b865ff5ba668a49cc58aeJohn McCall        DependentSizedExtVectorType(*this, vecType, QualType(), SizeExpr,
22896b304a0254a13f42390b865ff5ba668a49cc58aeJohn McCall                                    AttrLoc);
2290789b1f640205e81b5af250693246120f1ce9d147Douglas Gregor
2291789b1f640205e81b5af250693246120f1ce9d147Douglas Gregor      DependentSizedExtVectorType *CanonCheck
2292789b1f640205e81b5af250693246120f1ce9d147Douglas Gregor        = DependentSizedExtVectorTypes.FindNodeOrInsertPos(ID, InsertPos);
2293789b1f640205e81b5af250693246120f1ce9d147Douglas Gregor      assert(!CanonCheck && "Dependent-sized ext_vector canonical type broken");
2294789b1f640205e81b5af250693246120f1ce9d147Douglas Gregor      (void)CanonCheck;
22952ec09f1dc123e1942ed756e8ee4fef86451eac9eDouglas Gregor      DependentSizedExtVectorTypes.InsertNode(New, InsertPos);
22962ec09f1dc123e1942ed756e8ee4fef86451eac9eDouglas Gregor    } else {
22972ec09f1dc123e1942ed756e8ee4fef86451eac9eDouglas Gregor      QualType Canon = getDependentSizedExtVectorType(CanonVecTy, SizeExpr,
22982ec09f1dc123e1942ed756e8ee4fef86451eac9eDouglas Gregor                                                      SourceLocation());
22996b304a0254a13f42390b865ff5ba668a49cc58aeJohn McCall      New = new (*this, TypeAlignment)
23006b304a0254a13f42390b865ff5ba668a49cc58aeJohn McCall        DependentSizedExtVectorType(*this, vecType, Canon, SizeExpr, AttrLoc);
23012ec09f1dc123e1942ed756e8ee4fef86451eac9eDouglas Gregor    }
23022ec09f1dc123e1942ed756e8ee4fef86451eac9eDouglas Gregor  }
23031eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
23049cdda0cf8528e3d595be9bfa002f0450074beb4dDouglas Gregor  Types.push_back(New);
23059cdda0cf8528e3d595be9bfa002f0450074beb4dDouglas Gregor  return QualType(New, 0);
23069cdda0cf8528e3d595be9bfa002f0450074beb4dDouglas Gregor}
23079cdda0cf8528e3d595be9bfa002f0450074beb4dDouglas Gregor
230872564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor/// getFunctionNoProtoType - Return a K&R style C function type like 'int()'.
23095f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer///
23104ba2a17694148e16eaa8d3917f657ffcd3667be4Jay FoadQualType
23114ba2a17694148e16eaa8d3917f657ffcd3667be4Jay FoadASTContext::getFunctionNoProtoType(QualType ResultTy,
23124ba2a17694148e16eaa8d3917f657ffcd3667be4Jay Foad                                   const FunctionType::ExtInfo &Info) const {
2313cfe9af250f466e7e38becea4428990448ae07737Roman Divacky  const CallingConv DefaultCC = Info.getCC();
2314cfe9af250f466e7e38becea4428990448ae07737Roman Divacky  const CallingConv CallConv = (LangOpts.MRTD && DefaultCC == CC_Default) ?
2315cfe9af250f466e7e38becea4428990448ae07737Roman Divacky                               CC_X86StdCall : DefaultCC;
23165f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // Unique functions, to guarantee there is only one function of a particular
23175f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // structure.
23185f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  llvm::FoldingSetNodeID ID;
2319264ba48dc98f3f843935a485d5b086f7e0fdc4f1Rafael Espindola  FunctionNoProtoType::Profile(ID, ResultTy, Info);
23201eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
23215f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  void *InsertPos = 0;
23221eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  if (FunctionNoProtoType *FT =
232372564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor        FunctionNoProtoTypes.FindNodeOrInsertPos(ID, InsertPos))
23245f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    return QualType(FT, 0);
23251eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
23265f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  QualType Canonical;
2327ab8bbf4ebd3e3e6eab913cb044772a62b7581941Douglas Gregor  if (!ResultTy.isCanonical() ||
232804a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall      getCanonicalCallConv(CallConv) != CallConv) {
2329264ba48dc98f3f843935a485d5b086f7e0fdc4f1Rafael Espindola    Canonical =
2330264ba48dc98f3f843935a485d5b086f7e0fdc4f1Rafael Espindola      getFunctionNoProtoType(getCanonicalType(ResultTy),
2331264ba48dc98f3f843935a485d5b086f7e0fdc4f1Rafael Espindola                     Info.withCallingConv(getCanonicalCallConv(CallConv)));
23321eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
23335f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    // Get the new insert position for the node we care about.
233472564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor    FunctionNoProtoType *NewIP =
233572564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor      FunctionNoProtoTypes.FindNodeOrInsertPos(ID, InsertPos);
2336c6ed729f669044f5072a49d79041f455d971ece3Jeffrey Yasskin    assert(NewIP == 0 && "Shouldn't be in the map!"); (void)NewIP;
23375f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
23381eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
2339cfe9af250f466e7e38becea4428990448ae07737Roman Divacky  FunctionProtoType::ExtInfo newInfo = Info.withCallingConv(CallConv);
23406b304a0254a13f42390b865ff5ba668a49cc58aeJohn McCall  FunctionNoProtoType *New = new (*this, TypeAlignment)
2341cfe9af250f466e7e38becea4428990448ae07737Roman Divacky    FunctionNoProtoType(ResultTy, Canonical, newInfo);
23425f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  Types.push_back(New);
234372564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor  FunctionNoProtoTypes.InsertNode(New, InsertPos);
23445f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  return QualType(New, 0);
23455f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
23465f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
23475f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// getFunctionType - Return a normal function type with a typed argument
23485f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// list.  isVariadic indicates whether the argument list includes '...'.
23494ba2a17694148e16eaa8d3917f657ffcd3667be4Jay FoadQualType
23504ba2a17694148e16eaa8d3917f657ffcd3667be4Jay FoadASTContext::getFunctionType(QualType ResultTy,
23514ba2a17694148e16eaa8d3917f657ffcd3667be4Jay Foad                            const QualType *ArgArray, unsigned NumArgs,
23524ba2a17694148e16eaa8d3917f657ffcd3667be4Jay Foad                            const FunctionProtoType::ExtProtoInfo &EPI) const {
23535f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // Unique functions, to guarantee there is only one function of a particular
23545f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // structure.
23555f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  llvm::FoldingSetNodeID ID;
23568026f6d82f7fa544bc0453714fe94bca62a1196eSebastian Redl  FunctionProtoType::Profile(ID, ResultTy, ArgArray, NumArgs, EPI, *this);
23575f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
23585f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  void *InsertPos = 0;
23591eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  if (FunctionProtoType *FTP =
236072564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor        FunctionProtoTypes.FindNodeOrInsertPos(ID, InsertPos))
23615f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    return QualType(FTP, 0);
2362465226e23a3008bd68973513dda1f9e3cd27dbddSebastian Redl
2363465226e23a3008bd68973513dda1f9e3cd27dbddSebastian Redl  // Determine whether the type being created is already canonical or not.
2364eefb3d5b49c844347f212073a7e975b8118fe8e9Richard Smith  bool isCanonical =
2365eefb3d5b49c844347f212073a7e975b8118fe8e9Richard Smith    EPI.ExceptionSpecType == EST_None && ResultTy.isCanonical() &&
2366eefb3d5b49c844347f212073a7e975b8118fe8e9Richard Smith    !EPI.HasTrailingReturn;
23675f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  for (unsigned i = 0; i != NumArgs && isCanonical; ++i)
236854e14c4db764c0636160d26c5bbf491637c83a76John McCall    if (!ArgArray[i].isCanonicalAsParam())
23695f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      isCanonical = false;
23705f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
2371cfe9af250f466e7e38becea4428990448ae07737Roman Divacky  const CallingConv DefaultCC = EPI.ExtInfo.getCC();
2372cfe9af250f466e7e38becea4428990448ae07737Roman Divacky  const CallingConv CallConv = (LangOpts.MRTD && DefaultCC == CC_Default) ?
2373cfe9af250f466e7e38becea4428990448ae07737Roman Divacky                               CC_X86StdCall : DefaultCC;
2374e23cf437fe76b1ed02d63c3f61b456fd48a915f5John McCall
23755f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // If this type isn't canonical, get the canonical version of it.
2376465226e23a3008bd68973513dda1f9e3cd27dbddSebastian Redl  // The exception spec is not part of the canonical type.
23775f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  QualType Canonical;
237804a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall  if (!isCanonical || getCanonicalCallConv(CallConv) != CallConv) {
23795f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner    SmallVector<QualType, 16> CanonicalArgs;
23805f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    CanonicalArgs.reserve(NumArgs);
23815f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    for (unsigned i = 0; i != NumArgs; ++i)
238254e14c4db764c0636160d26c5bbf491637c83a76John McCall      CanonicalArgs.push_back(getCanonicalParamType(ArgArray[i]));
2383465226e23a3008bd68973513dda1f9e3cd27dbddSebastian Redl
2384e23cf437fe76b1ed02d63c3f61b456fd48a915f5John McCall    FunctionProtoType::ExtProtoInfo CanonicalEPI = EPI;
2385eefb3d5b49c844347f212073a7e975b8118fe8e9Richard Smith    CanonicalEPI.HasTrailingReturn = false;
23868b5b4099c61a136e9a1714c4d8a593febe942268Sebastian Redl    CanonicalEPI.ExceptionSpecType = EST_None;
23878b5b4099c61a136e9a1714c4d8a593febe942268Sebastian Redl    CanonicalEPI.NumExceptions = 0;
2388e23cf437fe76b1ed02d63c3f61b456fd48a915f5John McCall    CanonicalEPI.ExtInfo
2389e23cf437fe76b1ed02d63c3f61b456fd48a915f5John McCall      = CanonicalEPI.ExtInfo.withCallingConv(getCanonicalCallConv(CallConv));
2390e23cf437fe76b1ed02d63c3f61b456fd48a915f5John McCall
2391f52ab250ff92bc51a9ac9a8e19bd43b63a5f844fChris Lattner    Canonical = getFunctionType(getCanonicalType(ResultTy),
2392beaaccd8e2a8748f77b66e2b330fb9136937e14cJay Foad                                CanonicalArgs.data(), NumArgs,
2393e23cf437fe76b1ed02d63c3f61b456fd48a915f5John McCall                                CanonicalEPI);
2394465226e23a3008bd68973513dda1f9e3cd27dbddSebastian Redl
23955f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    // Get the new insert position for the node we care about.
239672564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor    FunctionProtoType *NewIP =
239772564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor      FunctionProtoTypes.FindNodeOrInsertPos(ID, InsertPos);
2398c6ed729f669044f5072a49d79041f455d971ece3Jeffrey Yasskin    assert(NewIP == 0 && "Shouldn't be in the map!"); (void)NewIP;
23995f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
2400465226e23a3008bd68973513dda1f9e3cd27dbddSebastian Redl
2401f85e193739c953358c865005855253af4f68a497John McCall  // FunctionProtoType objects are allocated with extra bytes after
2402f85e193739c953358c865005855253af4f68a497John McCall  // them for three variable size arrays at the end:
2403f85e193739c953358c865005855253af4f68a497John McCall  //  - parameter types
2404f85e193739c953358c865005855253af4f68a497John McCall  //  - exception types
2405f85e193739c953358c865005855253af4f68a497John McCall  //  - consumed-arguments flags
2406f85e193739c953358c865005855253af4f68a497John McCall  // Instead of the exception types, there could be a noexcept
2407b9d0b76e42fd2d4cdfd135220302458d03ad09feRichard Smith  // expression, or information used to resolve the exception
2408b9d0b76e42fd2d4cdfd135220302458d03ad09feRichard Smith  // specification.
2409e23cf437fe76b1ed02d63c3f61b456fd48a915f5John McCall  size_t Size = sizeof(FunctionProtoType) +
241060618fa7f88d5162bb5b40988b6b38d4d75d6fc6Sebastian Redl                NumArgs * sizeof(QualType);
2411b9d0b76e42fd2d4cdfd135220302458d03ad09feRichard Smith  if (EPI.ExceptionSpecType == EST_Dynamic) {
241260618fa7f88d5162bb5b40988b6b38d4d75d6fc6Sebastian Redl    Size += EPI.NumExceptions * sizeof(QualType);
2413b9d0b76e42fd2d4cdfd135220302458d03ad09feRichard Smith  } else if (EPI.ExceptionSpecType == EST_ComputedNoexcept) {
24148026f6d82f7fa544bc0453714fe94bca62a1196eSebastian Redl    Size += sizeof(Expr*);
2415e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith  } else if (EPI.ExceptionSpecType == EST_Uninstantiated) {
241613bffc532bafd45d4a77867993c1afb83c7661beRichard Smith    Size += 2 * sizeof(FunctionDecl*);
2417b9d0b76e42fd2d4cdfd135220302458d03ad09feRichard Smith  } else if (EPI.ExceptionSpecType == EST_Unevaluated) {
2418b9d0b76e42fd2d4cdfd135220302458d03ad09feRichard Smith    Size += sizeof(FunctionDecl*);
241960618fa7f88d5162bb5b40988b6b38d4d75d6fc6Sebastian Redl  }
2420f85e193739c953358c865005855253af4f68a497John McCall  if (EPI.ConsumedArguments)
2421f85e193739c953358c865005855253af4f68a497John McCall    Size += NumArgs * sizeof(bool);
2422f85e193739c953358c865005855253af4f68a497John McCall
2423e23cf437fe76b1ed02d63c3f61b456fd48a915f5John McCall  FunctionProtoType *FTP = (FunctionProtoType*) Allocate(Size, TypeAlignment);
2424cfe9af250f466e7e38becea4428990448ae07737Roman Divacky  FunctionProtoType::ExtProtoInfo newEPI = EPI;
2425cfe9af250f466e7e38becea4428990448ae07737Roman Divacky  newEPI.ExtInfo = EPI.ExtInfo.withCallingConv(CallConv);
24268026f6d82f7fa544bc0453714fe94bca62a1196eSebastian Redl  new (FTP) FunctionProtoType(ResultTy, ArgArray, NumArgs, Canonical, newEPI);
24275f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  Types.push_back(FTP);
242872564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor  FunctionProtoTypes.InsertNode(FTP, InsertPos);
24295f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  return QualType(FTP, 0);
24305f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
24315f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
24323cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall#ifndef NDEBUG
24333cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCallstatic bool NeedsInjectedClassNameType(const RecordDecl *D) {
24343cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall  if (!isa<CXXRecordDecl>(D)) return false;
24353cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall  const CXXRecordDecl *RD = cast<CXXRecordDecl>(D);
24363cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall  if (isa<ClassTemplatePartialSpecializationDecl>(RD))
24373cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall    return true;
24383cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall  if (RD->getDescribedClassTemplate() &&
24393cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall      !isa<ClassTemplateSpecializationDecl>(RD))
24403cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall    return true;
24413cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall  return false;
24423cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall}
24433cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall#endif
24443cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall
24453cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall/// getInjectedClassNameType - Return the unique reference to the
24463cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall/// injected class name type for the specified templated declaration.
24473cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCallQualType ASTContext::getInjectedClassNameType(CXXRecordDecl *Decl,
24484ba2a17694148e16eaa8d3917f657ffcd3667be4Jay Foad                                              QualType TST) const {
24493cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall  assert(NeedsInjectedClassNameType(Decl));
24503cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall  if (Decl->TypeForDecl) {
24513cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall    assert(isa<InjectedClassNameType>(Decl->TypeForDecl));
2452ef96ee0be5f100789f451641542a69cd719144d2Douglas Gregor  } else if (CXXRecordDecl *PrevDecl = Decl->getPreviousDecl()) {
24533cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall    assert(PrevDecl->TypeForDecl && "previous declaration has no type");
24543cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall    Decl->TypeForDecl = PrevDecl->TypeForDecl;
24553cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall    assert(isa<InjectedClassNameType>(Decl->TypeForDecl));
24563cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall  } else {
2457f4c7371fb1d3cebcfb40abad4537bb82515704eaJohn McCall    Type *newType =
245831f17ecbef57b5679c017c375db330546b7b5145John McCall      new (*this, TypeAlignment) InjectedClassNameType(Decl, TST);
2459f4c7371fb1d3cebcfb40abad4537bb82515704eaJohn McCall    Decl->TypeForDecl = newType;
2460f4c7371fb1d3cebcfb40abad4537bb82515704eaJohn McCall    Types.push_back(newType);
24613cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall  }
24623cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall  return QualType(Decl->TypeForDecl, 0);
24633cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall}
24643cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall
24652ce52f3fb95bf544db6bd3d91a72bce7d9cceb6cDouglas Gregor/// getTypeDeclType - Return the unique reference to the type for the
24662ce52f3fb95bf544db6bd3d91a72bce7d9cceb6cDouglas Gregor/// specified type declaration.
24674ba2a17694148e16eaa8d3917f657ffcd3667be4Jay FoadQualType ASTContext::getTypeDeclTypeSlow(const TypeDecl *Decl) const {
24681e6759e9e33dcaa73ce14c8a908ac9f87ac16463Argyrios Kyrtzidis  assert(Decl && "Passed null for Decl param");
2469becb8d5a6ab5103393eac5344ae69bcb860601ddJohn McCall  assert(!Decl->TypeForDecl && "TypeForDecl present in slow case");
24701eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
2471162e1c1b487352434552147967c3dd296ebee2f7Richard Smith  if (const TypedefNameDecl *Typedef = dyn_cast<TypedefNameDecl>(Decl))
24722ce52f3fb95bf544db6bd3d91a72bce7d9cceb6cDouglas Gregor    return getTypedefType(Typedef);
2473becb8d5a6ab5103393eac5344ae69bcb860601ddJohn McCall
2474becb8d5a6ab5103393eac5344ae69bcb860601ddJohn McCall  assert(!isa<TemplateTypeParmDecl>(Decl) &&
2475becb8d5a6ab5103393eac5344ae69bcb860601ddJohn McCall         "Template type parameter types are always available.");
2476becb8d5a6ab5103393eac5344ae69bcb860601ddJohn McCall
247719c8576b7328f4dc2d07682f5da552875c1912efJohn McCall  if (const RecordDecl *Record = dyn_cast<RecordDecl>(Decl)) {
2478ef96ee0be5f100789f451641542a69cd719144d2Douglas Gregor    assert(!Record->getPreviousDecl() &&
2479becb8d5a6ab5103393eac5344ae69bcb860601ddJohn McCall           "struct/union has previous declaration");
2480becb8d5a6ab5103393eac5344ae69bcb860601ddJohn McCall    assert(!NeedsInjectedClassNameType(Record));
2481400f5125e2432d648f2c8a31b36a7f318a880c47Argyrios Kyrtzidis    return getRecordType(Record);
248219c8576b7328f4dc2d07682f5da552875c1912efJohn McCall  } else if (const EnumDecl *Enum = dyn_cast<EnumDecl>(Decl)) {
2483ef96ee0be5f100789f451641542a69cd719144d2Douglas Gregor    assert(!Enum->getPreviousDecl() &&
2484becb8d5a6ab5103393eac5344ae69bcb860601ddJohn McCall           "enum has previous declaration");
2485400f5125e2432d648f2c8a31b36a7f318a880c47Argyrios Kyrtzidis    return getEnumType(Enum);
248619c8576b7328f4dc2d07682f5da552875c1912efJohn McCall  } else if (const UnresolvedUsingTypenameDecl *Using =
2487ed97649e9574b9d854fa4d6109c9333ae0993554John McCall               dyn_cast<UnresolvedUsingTypenameDecl>(Decl)) {
2488f4c7371fb1d3cebcfb40abad4537bb82515704eaJohn McCall    Type *newType = new (*this, TypeAlignment) UnresolvedUsingType(Using);
2489f4c7371fb1d3cebcfb40abad4537bb82515704eaJohn McCall    Decl->TypeForDecl = newType;
2490f4c7371fb1d3cebcfb40abad4537bb82515704eaJohn McCall    Types.push_back(newType);
24919fdbab3cbc2fc04bcaf5768023d83707f3151144Mike Stump  } else
2492becb8d5a6ab5103393eac5344ae69bcb860601ddJohn McCall    llvm_unreachable("TypeDecl without a type?");
249349aa7ff1245abd03e6e998e01302df31e4c6f8f6Argyrios Kyrtzidis
249449aa7ff1245abd03e6e998e01302df31e4c6f8f6Argyrios Kyrtzidis  return QualType(Decl->TypeForDecl, 0);
24952ce52f3fb95bf544db6bd3d91a72bce7d9cceb6cDouglas Gregor}
24962ce52f3fb95bf544db6bd3d91a72bce7d9cceb6cDouglas Gregor
24975f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// getTypedefType - Return the unique reference to the type for the
2498162e1c1b487352434552147967c3dd296ebee2f7Richard Smith/// specified typedef name decl.
24999763e221e16026ddf487d2564ed349d2c874a1a1Argyrios KyrtzidisQualType
2500162e1c1b487352434552147967c3dd296ebee2f7Richard SmithASTContext::getTypedefType(const TypedefNameDecl *Decl,
2501162e1c1b487352434552147967c3dd296ebee2f7Richard Smith                           QualType Canonical) const {
25025f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  if (Decl->TypeForDecl) return QualType(Decl->TypeForDecl, 0);
25031eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
25049763e221e16026ddf487d2564ed349d2c874a1a1Argyrios Kyrtzidis  if (Canonical.isNull())
25059763e221e16026ddf487d2564ed349d2c874a1a1Argyrios Kyrtzidis    Canonical = getCanonicalType(Decl->getUnderlyingType());
2506f4c7371fb1d3cebcfb40abad4537bb82515704eaJohn McCall  TypedefType *newType = new(*this, TypeAlignment)
25076b304a0254a13f42390b865ff5ba668a49cc58aeJohn McCall    TypedefType(Type::Typedef, Decl, Canonical);
2508f4c7371fb1d3cebcfb40abad4537bb82515704eaJohn McCall  Decl->TypeForDecl = newType;
2509f4c7371fb1d3cebcfb40abad4537bb82515704eaJohn McCall  Types.push_back(newType);
2510f4c7371fb1d3cebcfb40abad4537bb82515704eaJohn McCall  return QualType(newType, 0);
25115f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
25125f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
25134ba2a17694148e16eaa8d3917f657ffcd3667be4Jay FoadQualType ASTContext::getRecordType(const RecordDecl *Decl) const {
2514400f5125e2432d648f2c8a31b36a7f318a880c47Argyrios Kyrtzidis  if (Decl->TypeForDecl) return QualType(Decl->TypeForDecl, 0);
2515400f5125e2432d648f2c8a31b36a7f318a880c47Argyrios Kyrtzidis
2516ef96ee0be5f100789f451641542a69cd719144d2Douglas Gregor  if (const RecordDecl *PrevDecl = Decl->getPreviousDecl())
2517400f5125e2432d648f2c8a31b36a7f318a880c47Argyrios Kyrtzidis    if (PrevDecl->TypeForDecl)
2518400f5125e2432d648f2c8a31b36a7f318a880c47Argyrios Kyrtzidis      return QualType(Decl->TypeForDecl = PrevDecl->TypeForDecl, 0);
2519400f5125e2432d648f2c8a31b36a7f318a880c47Argyrios Kyrtzidis
2520f4c7371fb1d3cebcfb40abad4537bb82515704eaJohn McCall  RecordType *newType = new (*this, TypeAlignment) RecordType(Decl);
2521f4c7371fb1d3cebcfb40abad4537bb82515704eaJohn McCall  Decl->TypeForDecl = newType;
2522f4c7371fb1d3cebcfb40abad4537bb82515704eaJohn McCall  Types.push_back(newType);
2523f4c7371fb1d3cebcfb40abad4537bb82515704eaJohn McCall  return QualType(newType, 0);
2524400f5125e2432d648f2c8a31b36a7f318a880c47Argyrios Kyrtzidis}
2525400f5125e2432d648f2c8a31b36a7f318a880c47Argyrios Kyrtzidis
25264ba2a17694148e16eaa8d3917f657ffcd3667be4Jay FoadQualType ASTContext::getEnumType(const EnumDecl *Decl) const {
2527400f5125e2432d648f2c8a31b36a7f318a880c47Argyrios Kyrtzidis  if (Decl->TypeForDecl) return QualType(Decl->TypeForDecl, 0);
2528400f5125e2432d648f2c8a31b36a7f318a880c47Argyrios Kyrtzidis
2529ef96ee0be5f100789f451641542a69cd719144d2Douglas Gregor  if (const EnumDecl *PrevDecl = Decl->getPreviousDecl())
2530400f5125e2432d648f2c8a31b36a7f318a880c47Argyrios Kyrtzidis    if (PrevDecl->TypeForDecl)
2531400f5125e2432d648f2c8a31b36a7f318a880c47Argyrios Kyrtzidis      return QualType(Decl->TypeForDecl = PrevDecl->TypeForDecl, 0);
2532400f5125e2432d648f2c8a31b36a7f318a880c47Argyrios Kyrtzidis
2533f4c7371fb1d3cebcfb40abad4537bb82515704eaJohn McCall  EnumType *newType = new (*this, TypeAlignment) EnumType(Decl);
2534f4c7371fb1d3cebcfb40abad4537bb82515704eaJohn McCall  Decl->TypeForDecl = newType;
2535f4c7371fb1d3cebcfb40abad4537bb82515704eaJohn McCall  Types.push_back(newType);
2536f4c7371fb1d3cebcfb40abad4537bb82515704eaJohn McCall  return QualType(newType, 0);
2537400f5125e2432d648f2c8a31b36a7f318a880c47Argyrios Kyrtzidis}
2538400f5125e2432d648f2c8a31b36a7f318a880c47Argyrios Kyrtzidis
25399d156a7b1b2771e191f2f5a45a7b7a694129463bJohn McCallQualType ASTContext::getAttributedType(AttributedType::Kind attrKind,
25409d156a7b1b2771e191f2f5a45a7b7a694129463bJohn McCall                                       QualType modifiedType,
25419d156a7b1b2771e191f2f5a45a7b7a694129463bJohn McCall                                       QualType equivalentType) {
25429d156a7b1b2771e191f2f5a45a7b7a694129463bJohn McCall  llvm::FoldingSetNodeID id;
25439d156a7b1b2771e191f2f5a45a7b7a694129463bJohn McCall  AttributedType::Profile(id, attrKind, modifiedType, equivalentType);
25449d156a7b1b2771e191f2f5a45a7b7a694129463bJohn McCall
25459d156a7b1b2771e191f2f5a45a7b7a694129463bJohn McCall  void *insertPos = 0;
25469d156a7b1b2771e191f2f5a45a7b7a694129463bJohn McCall  AttributedType *type = AttributedTypes.FindNodeOrInsertPos(id, insertPos);
25479d156a7b1b2771e191f2f5a45a7b7a694129463bJohn McCall  if (type) return QualType(type, 0);
25489d156a7b1b2771e191f2f5a45a7b7a694129463bJohn McCall
25499d156a7b1b2771e191f2f5a45a7b7a694129463bJohn McCall  QualType canon = getCanonicalType(equivalentType);
25509d156a7b1b2771e191f2f5a45a7b7a694129463bJohn McCall  type = new (*this, TypeAlignment)
25519d156a7b1b2771e191f2f5a45a7b7a694129463bJohn McCall           AttributedType(canon, attrKind, modifiedType, equivalentType);
25529d156a7b1b2771e191f2f5a45a7b7a694129463bJohn McCall
25539d156a7b1b2771e191f2f5a45a7b7a694129463bJohn McCall  Types.push_back(type);
25549d156a7b1b2771e191f2f5a45a7b7a694129463bJohn McCall  AttributedTypes.InsertNode(type, insertPos);
25559d156a7b1b2771e191f2f5a45a7b7a694129463bJohn McCall
25569d156a7b1b2771e191f2f5a45a7b7a694129463bJohn McCall  return QualType(type, 0);
25579d156a7b1b2771e191f2f5a45a7b7a694129463bJohn McCall}
25589d156a7b1b2771e191f2f5a45a7b7a694129463bJohn McCall
25599d156a7b1b2771e191f2f5a45a7b7a694129463bJohn McCall
256049a832bd499d6f61c23655f1fac99f0dd229756eJohn McCall/// \brief Retrieve a substitution-result type.
256149a832bd499d6f61c23655f1fac99f0dd229756eJohn McCallQualType
256249a832bd499d6f61c23655f1fac99f0dd229756eJohn McCallASTContext::getSubstTemplateTypeParmType(const TemplateTypeParmType *Parm,
25634ba2a17694148e16eaa8d3917f657ffcd3667be4Jay Foad                                         QualType Replacement) const {
2564467b27b9a24bdc823218ad1ad0e37673b6cc1e83John McCall  assert(Replacement.isCanonical()
256549a832bd499d6f61c23655f1fac99f0dd229756eJohn McCall         && "replacement types must always be canonical");
256649a832bd499d6f61c23655f1fac99f0dd229756eJohn McCall
256749a832bd499d6f61c23655f1fac99f0dd229756eJohn McCall  llvm::FoldingSetNodeID ID;
256849a832bd499d6f61c23655f1fac99f0dd229756eJohn McCall  SubstTemplateTypeParmType::Profile(ID, Parm, Replacement);
256949a832bd499d6f61c23655f1fac99f0dd229756eJohn McCall  void *InsertPos = 0;
257049a832bd499d6f61c23655f1fac99f0dd229756eJohn McCall  SubstTemplateTypeParmType *SubstParm
257149a832bd499d6f61c23655f1fac99f0dd229756eJohn McCall    = SubstTemplateTypeParmTypes.FindNodeOrInsertPos(ID, InsertPos);
257249a832bd499d6f61c23655f1fac99f0dd229756eJohn McCall
257349a832bd499d6f61c23655f1fac99f0dd229756eJohn McCall  if (!SubstParm) {
257449a832bd499d6f61c23655f1fac99f0dd229756eJohn McCall    SubstParm = new (*this, TypeAlignment)
257549a832bd499d6f61c23655f1fac99f0dd229756eJohn McCall      SubstTemplateTypeParmType(Parm, Replacement);
257649a832bd499d6f61c23655f1fac99f0dd229756eJohn McCall    Types.push_back(SubstParm);
257749a832bd499d6f61c23655f1fac99f0dd229756eJohn McCall    SubstTemplateTypeParmTypes.InsertNode(SubstParm, InsertPos);
257849a832bd499d6f61c23655f1fac99f0dd229756eJohn McCall  }
257949a832bd499d6f61c23655f1fac99f0dd229756eJohn McCall
258049a832bd499d6f61c23655f1fac99f0dd229756eJohn McCall  return QualType(SubstParm, 0);
258149a832bd499d6f61c23655f1fac99f0dd229756eJohn McCall}
258249a832bd499d6f61c23655f1fac99f0dd229756eJohn McCall
2583c3069d618f4661d923cb1b5c4525b082fce73b04Douglas Gregor/// \brief Retrieve a
2584c3069d618f4661d923cb1b5c4525b082fce73b04Douglas GregorQualType ASTContext::getSubstTemplateTypeParmPackType(
2585c3069d618f4661d923cb1b5c4525b082fce73b04Douglas Gregor                                          const TemplateTypeParmType *Parm,
2586c3069d618f4661d923cb1b5c4525b082fce73b04Douglas Gregor                                              const TemplateArgument &ArgPack) {
2587c3069d618f4661d923cb1b5c4525b082fce73b04Douglas Gregor#ifndef NDEBUG
2588c3069d618f4661d923cb1b5c4525b082fce73b04Douglas Gregor  for (TemplateArgument::pack_iterator P = ArgPack.pack_begin(),
2589c3069d618f4661d923cb1b5c4525b082fce73b04Douglas Gregor                                    PEnd = ArgPack.pack_end();
2590c3069d618f4661d923cb1b5c4525b082fce73b04Douglas Gregor       P != PEnd; ++P) {
2591c3069d618f4661d923cb1b5c4525b082fce73b04Douglas Gregor    assert(P->getKind() == TemplateArgument::Type &&"Pack contains a non-type");
2592c3069d618f4661d923cb1b5c4525b082fce73b04Douglas Gregor    assert(P->getAsType().isCanonical() && "Pack contains non-canonical type");
2593c3069d618f4661d923cb1b5c4525b082fce73b04Douglas Gregor  }
2594c3069d618f4661d923cb1b5c4525b082fce73b04Douglas Gregor#endif
2595c3069d618f4661d923cb1b5c4525b082fce73b04Douglas Gregor
2596c3069d618f4661d923cb1b5c4525b082fce73b04Douglas Gregor  llvm::FoldingSetNodeID ID;
2597c3069d618f4661d923cb1b5c4525b082fce73b04Douglas Gregor  SubstTemplateTypeParmPackType::Profile(ID, Parm, ArgPack);
2598c3069d618f4661d923cb1b5c4525b082fce73b04Douglas Gregor  void *InsertPos = 0;
2599c3069d618f4661d923cb1b5c4525b082fce73b04Douglas Gregor  if (SubstTemplateTypeParmPackType *SubstParm
2600c3069d618f4661d923cb1b5c4525b082fce73b04Douglas Gregor        = SubstTemplateTypeParmPackTypes.FindNodeOrInsertPos(ID, InsertPos))
2601c3069d618f4661d923cb1b5c4525b082fce73b04Douglas Gregor    return QualType(SubstParm, 0);
2602c3069d618f4661d923cb1b5c4525b082fce73b04Douglas Gregor
2603c3069d618f4661d923cb1b5c4525b082fce73b04Douglas Gregor  QualType Canon;
2604c3069d618f4661d923cb1b5c4525b082fce73b04Douglas Gregor  if (!Parm->isCanonicalUnqualified()) {
2605c3069d618f4661d923cb1b5c4525b082fce73b04Douglas Gregor    Canon = getCanonicalType(QualType(Parm, 0));
2606c3069d618f4661d923cb1b5c4525b082fce73b04Douglas Gregor    Canon = getSubstTemplateTypeParmPackType(cast<TemplateTypeParmType>(Canon),
2607c3069d618f4661d923cb1b5c4525b082fce73b04Douglas Gregor                                             ArgPack);
2608c3069d618f4661d923cb1b5c4525b082fce73b04Douglas Gregor    SubstTemplateTypeParmPackTypes.FindNodeOrInsertPos(ID, InsertPos);
2609c3069d618f4661d923cb1b5c4525b082fce73b04Douglas Gregor  }
2610c3069d618f4661d923cb1b5c4525b082fce73b04Douglas Gregor
2611c3069d618f4661d923cb1b5c4525b082fce73b04Douglas Gregor  SubstTemplateTypeParmPackType *SubstParm
2612c3069d618f4661d923cb1b5c4525b082fce73b04Douglas Gregor    = new (*this, TypeAlignment) SubstTemplateTypeParmPackType(Parm, Canon,
2613c3069d618f4661d923cb1b5c4525b082fce73b04Douglas Gregor                                                               ArgPack);
2614c3069d618f4661d923cb1b5c4525b082fce73b04Douglas Gregor  Types.push_back(SubstParm);
2615c3069d618f4661d923cb1b5c4525b082fce73b04Douglas Gregor  SubstTemplateTypeParmTypes.InsertNode(SubstParm, InsertPos);
2616c3069d618f4661d923cb1b5c4525b082fce73b04Douglas Gregor  return QualType(SubstParm, 0);
2617c3069d618f4661d923cb1b5c4525b082fce73b04Douglas Gregor}
2618c3069d618f4661d923cb1b5c4525b082fce73b04Douglas Gregor
2619fab9d67cebb87be968e7ae31a3b549a5279b5d51Douglas Gregor/// \brief Retrieve the template type parameter type for a template
26201eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// parameter or parameter pack with the given depth, index, and (optionally)
262176e4ce42a30cee4dc40ce7c6014874fbc4f9baa7Anders Carlsson/// name.
26221eb4433ac451dc16f4133a88af2d002ac26c58efMike StumpQualType ASTContext::getTemplateTypeParmType(unsigned Depth, unsigned Index,
262376e4ce42a30cee4dc40ce7c6014874fbc4f9baa7Anders Carlsson                                             bool ParameterPack,
26244fb86f8c4585e53c21c847ad3de9e3b2de123cd9Chandler Carruth                                             TemplateTypeParmDecl *TTPDecl) const {
2625fab9d67cebb87be968e7ae31a3b549a5279b5d51Douglas Gregor  llvm::FoldingSetNodeID ID;
26264fb86f8c4585e53c21c847ad3de9e3b2de123cd9Chandler Carruth  TemplateTypeParmType::Profile(ID, Depth, Index, ParameterPack, TTPDecl);
2627fab9d67cebb87be968e7ae31a3b549a5279b5d51Douglas Gregor  void *InsertPos = 0;
26281eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  TemplateTypeParmType *TypeParm
2629fab9d67cebb87be968e7ae31a3b549a5279b5d51Douglas Gregor    = TemplateTypeParmTypes.FindNodeOrInsertPos(ID, InsertPos);
2630fab9d67cebb87be968e7ae31a3b549a5279b5d51Douglas Gregor
2631fab9d67cebb87be968e7ae31a3b549a5279b5d51Douglas Gregor  if (TypeParm)
2632fab9d67cebb87be968e7ae31a3b549a5279b5d51Douglas Gregor    return QualType(TypeParm, 0);
26331eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
26344fb86f8c4585e53c21c847ad3de9e3b2de123cd9Chandler Carruth  if (TTPDecl) {
263576e4ce42a30cee4dc40ce7c6014874fbc4f9baa7Anders Carlsson    QualType Canon = getTemplateTypeParmType(Depth, Index, ParameterPack);
26364fb86f8c4585e53c21c847ad3de9e3b2de123cd9Chandler Carruth    TypeParm = new (*this, TypeAlignment) TemplateTypeParmType(TTPDecl, Canon);
2637789b1f640205e81b5af250693246120f1ce9d147Douglas Gregor
2638789b1f640205e81b5af250693246120f1ce9d147Douglas Gregor    TemplateTypeParmType *TypeCheck
2639789b1f640205e81b5af250693246120f1ce9d147Douglas Gregor      = TemplateTypeParmTypes.FindNodeOrInsertPos(ID, InsertPos);
2640789b1f640205e81b5af250693246120f1ce9d147Douglas Gregor    assert(!TypeCheck && "Template type parameter canonical type broken");
2641789b1f640205e81b5af250693246120f1ce9d147Douglas Gregor    (void)TypeCheck;
264276e4ce42a30cee4dc40ce7c6014874fbc4f9baa7Anders Carlsson  } else
26436b304a0254a13f42390b865ff5ba668a49cc58aeJohn McCall    TypeParm = new (*this, TypeAlignment)
26446b304a0254a13f42390b865ff5ba668a49cc58aeJohn McCall      TemplateTypeParmType(Depth, Index, ParameterPack);
2645fab9d67cebb87be968e7ae31a3b549a5279b5d51Douglas Gregor
2646fab9d67cebb87be968e7ae31a3b549a5279b5d51Douglas Gregor  Types.push_back(TypeParm);
2647fab9d67cebb87be968e7ae31a3b549a5279b5d51Douglas Gregor  TemplateTypeParmTypes.InsertNode(TypeParm, InsertPos);
2648fab9d67cebb87be968e7ae31a3b549a5279b5d51Douglas Gregor
2649fab9d67cebb87be968e7ae31a3b549a5279b5d51Douglas Gregor  return QualType(TypeParm, 0);
2650fab9d67cebb87be968e7ae31a3b549a5279b5d51Douglas Gregor}
2651fab9d67cebb87be968e7ae31a3b549a5279b5d51Douglas Gregor
26523cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCallTypeSourceInfo *
26533cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCallASTContext::getTemplateSpecializationTypeInfo(TemplateName Name,
26543cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall                                              SourceLocation NameLoc,
26553cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall                                        const TemplateArgumentListInfo &Args,
26563e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith                                              QualType Underlying) const {
26577c3179cf463c3b3b8c21dbb955f933ba50b74f28Douglas Gregor  assert(!Name.getAsDependentTemplateName() &&
26587c3179cf463c3b3b8c21dbb955f933ba50b74f28Douglas Gregor         "No dependent template names here!");
26593e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith  QualType TST = getTemplateSpecializationType(Name, Args, Underlying);
26603cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall
26613cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall  TypeSourceInfo *DI = CreateTypeSourceInfo(TST);
26623cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall  TemplateSpecializationTypeLoc TL
26633cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall    = cast<TemplateSpecializationTypeLoc>(DI->getTypeLoc());
266455d23c925b058be29b792008ddb7d68f6c4fa9a0Abramo Bagnara  TL.setTemplateKeywordLoc(SourceLocation());
26653cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall  TL.setTemplateNameLoc(NameLoc);
26663cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall  TL.setLAngleLoc(Args.getLAngleLoc());
26673cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall  TL.setRAngleLoc(Args.getRAngleLoc());
26683cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall  for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i)
26693cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall    TL.setArgLocInfo(i, Args[i].getLocInfo());
26703cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall  return DI;
26713cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall}
26723cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall
26731eb4433ac451dc16f4133a88af2d002ac26c58efMike StumpQualType
26747532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas GregorASTContext::getTemplateSpecializationType(TemplateName Template,
2675d5532b6cfff2977e0c59fa6ead7f7973984a620dJohn McCall                                          const TemplateArgumentListInfo &Args,
26763e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith                                          QualType Underlying) const {
26777c3179cf463c3b3b8c21dbb955f933ba50b74f28Douglas Gregor  assert(!Template.getAsDependentTemplateName() &&
26787c3179cf463c3b3b8c21dbb955f933ba50b74f28Douglas Gregor         "No dependent template names here!");
26797c3179cf463c3b3b8c21dbb955f933ba50b74f28Douglas Gregor
2680d5532b6cfff2977e0c59fa6ead7f7973984a620dJohn McCall  unsigned NumArgs = Args.size();
2681d5532b6cfff2977e0c59fa6ead7f7973984a620dJohn McCall
26825f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner  SmallVector<TemplateArgument, 4> ArgVec;
2683833ca991c1bfc967f0995974ca86f66ba1f666b5John McCall  ArgVec.reserve(NumArgs);
2684833ca991c1bfc967f0995974ca86f66ba1f666b5John McCall  for (unsigned i = 0; i != NumArgs; ++i)
2685833ca991c1bfc967f0995974ca86f66ba1f666b5John McCall    ArgVec.push_back(Args[i].getArgument());
2686833ca991c1bfc967f0995974ca86f66ba1f666b5John McCall
268731f17ecbef57b5679c017c375db330546b7b5145John McCall  return getTemplateSpecializationType(Template, ArgVec.data(), NumArgs,
26883e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith                                       Underlying);
2689833ca991c1bfc967f0995974ca86f66ba1f666b5John McCall}
2690833ca991c1bfc967f0995974ca86f66ba1f666b5John McCall
2691b70126a328f89937f46db42f9e3cba1592887c91Douglas Gregor#ifndef NDEBUG
2692b70126a328f89937f46db42f9e3cba1592887c91Douglas Gregorstatic bool hasAnyPackExpansions(const TemplateArgument *Args,
2693b70126a328f89937f46db42f9e3cba1592887c91Douglas Gregor                                 unsigned NumArgs) {
2694b70126a328f89937f46db42f9e3cba1592887c91Douglas Gregor  for (unsigned I = 0; I != NumArgs; ++I)
2695b70126a328f89937f46db42f9e3cba1592887c91Douglas Gregor    if (Args[I].isPackExpansion())
2696b70126a328f89937f46db42f9e3cba1592887c91Douglas Gregor      return true;
2697b70126a328f89937f46db42f9e3cba1592887c91Douglas Gregor
2698b70126a328f89937f46db42f9e3cba1592887c91Douglas Gregor  return true;
2699b70126a328f89937f46db42f9e3cba1592887c91Douglas Gregor}
2700b70126a328f89937f46db42f9e3cba1592887c91Douglas Gregor#endif
2701b70126a328f89937f46db42f9e3cba1592887c91Douglas Gregor
2702833ca991c1bfc967f0995974ca86f66ba1f666b5John McCallQualType
2703833ca991c1bfc967f0995974ca86f66ba1f666b5John McCallASTContext::getTemplateSpecializationType(TemplateName Template,
27047532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor                                          const TemplateArgument *Args,
27057532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor                                          unsigned NumArgs,
27063e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith                                          QualType Underlying) const {
27077c3179cf463c3b3b8c21dbb955f933ba50b74f28Douglas Gregor  assert(!Template.getAsDependentTemplateName() &&
27087c3179cf463c3b3b8c21dbb955f933ba50b74f28Douglas Gregor         "No dependent template names here!");
27090f0ea2a96534c615ff5fdd81363989b23cf2164aDouglas Gregor  // Look through qualified template names.
27100f0ea2a96534c615ff5fdd81363989b23cf2164aDouglas Gregor  if (QualifiedTemplateName *QTN = Template.getAsQualifiedTemplateName())
27110f0ea2a96534c615ff5fdd81363989b23cf2164aDouglas Gregor    Template = TemplateName(QTN->getTemplateDecl());
27127c3179cf463c3b3b8c21dbb955f933ba50b74f28Douglas Gregor
2713b70126a328f89937f46db42f9e3cba1592887c91Douglas Gregor  bool IsTypeAlias =
27143e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith    Template.getAsTemplateDecl() &&
27153e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith    isa<TypeAliasTemplateDecl>(Template.getAsTemplateDecl());
27163e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith  QualType CanonType;
27173e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith  if (!Underlying.isNull())
27183e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith    CanonType = getCanonicalType(Underlying);
27193e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith  else {
2720b70126a328f89937f46db42f9e3cba1592887c91Douglas Gregor    // We can get here with an alias template when the specialization contains
2721b70126a328f89937f46db42f9e3cba1592887c91Douglas Gregor    // a pack expansion that does not match up with a parameter pack.
2722b70126a328f89937f46db42f9e3cba1592887c91Douglas Gregor    assert((!IsTypeAlias || hasAnyPackExpansions(Args, NumArgs)) &&
2723b70126a328f89937f46db42f9e3cba1592887c91Douglas Gregor           "Caller must compute aliased type");
2724b70126a328f89937f46db42f9e3cba1592887c91Douglas Gregor    IsTypeAlias = false;
27253e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith    CanonType = getCanonicalTemplateSpecializationType(Template, Args,
27263e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith                                                       NumArgs);
27273e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith  }
2728fc705b84347e6fb4746a1a7e26949f64c2f2f358Douglas Gregor
27291275ae098acda31fe0e434510c729fcfed0458a1Douglas Gregor  // Allocate the (non-canonical) template specialization type, but don't
27301275ae098acda31fe0e434510c729fcfed0458a1Douglas Gregor  // try to unique it: these types typically have location information that
27311275ae098acda31fe0e434510c729fcfed0458a1Douglas Gregor  // we don't unique and don't want to lose.
27323e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith  void *Mem = Allocate(sizeof(TemplateSpecializationType) +
27333e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith                       sizeof(TemplateArgument) * NumArgs +
2734b70126a328f89937f46db42f9e3cba1592887c91Douglas Gregor                       (IsTypeAlias? sizeof(QualType) : 0),
27356b304a0254a13f42390b865ff5ba668a49cc58aeJohn McCall                       TypeAlignment);
27361eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  TemplateSpecializationType *Spec
2737b70126a328f89937f46db42f9e3cba1592887c91Douglas Gregor    = new (Mem) TemplateSpecializationType(Template, Args, NumArgs, CanonType,
2738b70126a328f89937f46db42f9e3cba1592887c91Douglas Gregor                                         IsTypeAlias ? Underlying : QualType());
27391eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
274055f6b14230c94272efbbcdd89a92224c8db9f225Douglas Gregor  Types.push_back(Spec);
27411eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  return QualType(Spec, 0);
274255f6b14230c94272efbbcdd89a92224c8db9f225Douglas Gregor}
274355f6b14230c94272efbbcdd89a92224c8db9f225Douglas Gregor
27441eb4433ac451dc16f4133a88af2d002ac26c58efMike StumpQualType
27459763e221e16026ddf487d2564ed349d2c874a1a1Argyrios KyrtzidisASTContext::getCanonicalTemplateSpecializationType(TemplateName Template,
27469763e221e16026ddf487d2564ed349d2c874a1a1Argyrios Kyrtzidis                                                   const TemplateArgument *Args,
27474ba2a17694148e16eaa8d3917f657ffcd3667be4Jay Foad                                                   unsigned NumArgs) const {
27487c3179cf463c3b3b8c21dbb955f933ba50b74f28Douglas Gregor  assert(!Template.getAsDependentTemplateName() &&
27497c3179cf463c3b3b8c21dbb955f933ba50b74f28Douglas Gregor         "No dependent template names here!");
27503e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith
27510f0ea2a96534c615ff5fdd81363989b23cf2164aDouglas Gregor  // Look through qualified template names.
27520f0ea2a96534c615ff5fdd81363989b23cf2164aDouglas Gregor  if (QualifiedTemplateName *QTN = Template.getAsQualifiedTemplateName())
27530f0ea2a96534c615ff5fdd81363989b23cf2164aDouglas Gregor    Template = TemplateName(QTN->getTemplateDecl());
27547c3179cf463c3b3b8c21dbb955f933ba50b74f28Douglas Gregor
27559763e221e16026ddf487d2564ed349d2c874a1a1Argyrios Kyrtzidis  // Build the canonical template specialization type.
27569763e221e16026ddf487d2564ed349d2c874a1a1Argyrios Kyrtzidis  TemplateName CanonTemplate = getCanonicalTemplateName(Template);
27575f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner  SmallVector<TemplateArgument, 4> CanonArgs;
27589763e221e16026ddf487d2564ed349d2c874a1a1Argyrios Kyrtzidis  CanonArgs.reserve(NumArgs);
27599763e221e16026ddf487d2564ed349d2c874a1a1Argyrios Kyrtzidis  for (unsigned I = 0; I != NumArgs; ++I)
27609763e221e16026ddf487d2564ed349d2c874a1a1Argyrios Kyrtzidis    CanonArgs.push_back(getCanonicalTemplateArgument(Args[I]));
27619763e221e16026ddf487d2564ed349d2c874a1a1Argyrios Kyrtzidis
27629763e221e16026ddf487d2564ed349d2c874a1a1Argyrios Kyrtzidis  // Determine whether this canonical template specialization type already
27639763e221e16026ddf487d2564ed349d2c874a1a1Argyrios Kyrtzidis  // exists.
27649763e221e16026ddf487d2564ed349d2c874a1a1Argyrios Kyrtzidis  llvm::FoldingSetNodeID ID;
27659763e221e16026ddf487d2564ed349d2c874a1a1Argyrios Kyrtzidis  TemplateSpecializationType::Profile(ID, CanonTemplate,
27669763e221e16026ddf487d2564ed349d2c874a1a1Argyrios Kyrtzidis                                      CanonArgs.data(), NumArgs, *this);
27679763e221e16026ddf487d2564ed349d2c874a1a1Argyrios Kyrtzidis
27689763e221e16026ddf487d2564ed349d2c874a1a1Argyrios Kyrtzidis  void *InsertPos = 0;
27699763e221e16026ddf487d2564ed349d2c874a1a1Argyrios Kyrtzidis  TemplateSpecializationType *Spec
27709763e221e16026ddf487d2564ed349d2c874a1a1Argyrios Kyrtzidis    = TemplateSpecializationTypes.FindNodeOrInsertPos(ID, InsertPos);
27719763e221e16026ddf487d2564ed349d2c874a1a1Argyrios Kyrtzidis
27729763e221e16026ddf487d2564ed349d2c874a1a1Argyrios Kyrtzidis  if (!Spec) {
27739763e221e16026ddf487d2564ed349d2c874a1a1Argyrios Kyrtzidis    // Allocate a new canonical template specialization type.
27749763e221e16026ddf487d2564ed349d2c874a1a1Argyrios Kyrtzidis    void *Mem = Allocate((sizeof(TemplateSpecializationType) +
27759763e221e16026ddf487d2564ed349d2c874a1a1Argyrios Kyrtzidis                          sizeof(TemplateArgument) * NumArgs),
27769763e221e16026ddf487d2564ed349d2c874a1a1Argyrios Kyrtzidis                         TypeAlignment);
27779763e221e16026ddf487d2564ed349d2c874a1a1Argyrios Kyrtzidis    Spec = new (Mem) TemplateSpecializationType(CanonTemplate,
27789763e221e16026ddf487d2564ed349d2c874a1a1Argyrios Kyrtzidis                                                CanonArgs.data(), NumArgs,
27793e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith                                                QualType(), QualType());
27809763e221e16026ddf487d2564ed349d2c874a1a1Argyrios Kyrtzidis    Types.push_back(Spec);
27819763e221e16026ddf487d2564ed349d2c874a1a1Argyrios Kyrtzidis    TemplateSpecializationTypes.InsertNode(Spec, InsertPos);
27829763e221e16026ddf487d2564ed349d2c874a1a1Argyrios Kyrtzidis  }
27839763e221e16026ddf487d2564ed349d2c874a1a1Argyrios Kyrtzidis
27849763e221e16026ddf487d2564ed349d2c874a1a1Argyrios Kyrtzidis  assert(Spec->isDependentType() &&
27859763e221e16026ddf487d2564ed349d2c874a1a1Argyrios Kyrtzidis         "Non-dependent template-id type must have a canonical type");
27869763e221e16026ddf487d2564ed349d2c874a1a1Argyrios Kyrtzidis  return QualType(Spec, 0);
27879763e221e16026ddf487d2564ed349d2c874a1a1Argyrios Kyrtzidis}
27889763e221e16026ddf487d2564ed349d2c874a1a1Argyrios Kyrtzidis
27899763e221e16026ddf487d2564ed349d2c874a1a1Argyrios KyrtzidisQualType
2790465d41b92b2c862f3062c412a0538db65c6a2661Abramo BagnaraASTContext::getElaboratedType(ElaboratedTypeKeyword Keyword,
2791465d41b92b2c862f3062c412a0538db65c6a2661Abramo Bagnara                              NestedNameSpecifier *NNS,
27924ba2a17694148e16eaa8d3917f657ffcd3667be4Jay Foad                              QualType NamedType) const {
2793e4e5b054b4917f0ee493bb2fda5b1ec749bfb9a1Douglas Gregor  llvm::FoldingSetNodeID ID;
2794465d41b92b2c862f3062c412a0538db65c6a2661Abramo Bagnara  ElaboratedType::Profile(ID, Keyword, NNS, NamedType);
2795e4e5b054b4917f0ee493bb2fda5b1ec749bfb9a1Douglas Gregor
2796e4e5b054b4917f0ee493bb2fda5b1ec749bfb9a1Douglas Gregor  void *InsertPos = 0;
2797465d41b92b2c862f3062c412a0538db65c6a2661Abramo Bagnara  ElaboratedType *T = ElaboratedTypes.FindNodeOrInsertPos(ID, InsertPos);
2798e4e5b054b4917f0ee493bb2fda5b1ec749bfb9a1Douglas Gregor  if (T)
2799e4e5b054b4917f0ee493bb2fda5b1ec749bfb9a1Douglas Gregor    return QualType(T, 0);
2800e4e5b054b4917f0ee493bb2fda5b1ec749bfb9a1Douglas Gregor
2801789b1f640205e81b5af250693246120f1ce9d147Douglas Gregor  QualType Canon = NamedType;
2802789b1f640205e81b5af250693246120f1ce9d147Douglas Gregor  if (!Canon.isCanonical()) {
2803789b1f640205e81b5af250693246120f1ce9d147Douglas Gregor    Canon = getCanonicalType(NamedType);
2804465d41b92b2c862f3062c412a0538db65c6a2661Abramo Bagnara    ElaboratedType *CheckT = ElaboratedTypes.FindNodeOrInsertPos(ID, InsertPos);
2805465d41b92b2c862f3062c412a0538db65c6a2661Abramo Bagnara    assert(!CheckT && "Elaborated canonical type broken");
2806789b1f640205e81b5af250693246120f1ce9d147Douglas Gregor    (void)CheckT;
2807789b1f640205e81b5af250693246120f1ce9d147Douglas Gregor  }
2808789b1f640205e81b5af250693246120f1ce9d147Douglas Gregor
2809465d41b92b2c862f3062c412a0538db65c6a2661Abramo Bagnara  T = new (*this) ElaboratedType(Keyword, NNS, NamedType, Canon);
2810e4e5b054b4917f0ee493bb2fda5b1ec749bfb9a1Douglas Gregor  Types.push_back(T);
2811465d41b92b2c862f3062c412a0538db65c6a2661Abramo Bagnara  ElaboratedTypes.InsertNode(T, InsertPos);
2812e4e5b054b4917f0ee493bb2fda5b1ec749bfb9a1Douglas Gregor  return QualType(T, 0);
2813e4e5b054b4917f0ee493bb2fda5b1ec749bfb9a1Douglas Gregor}
2814e4e5b054b4917f0ee493bb2fda5b1ec749bfb9a1Douglas Gregor
2815075f8f1b6bed4d1b224c74f87508534cc6392ce6Abramo BagnaraQualType
28164ba2a17694148e16eaa8d3917f657ffcd3667be4Jay FoadASTContext::getParenType(QualType InnerType) const {
2817075f8f1b6bed4d1b224c74f87508534cc6392ce6Abramo Bagnara  llvm::FoldingSetNodeID ID;
2818075f8f1b6bed4d1b224c74f87508534cc6392ce6Abramo Bagnara  ParenType::Profile(ID, InnerType);
2819075f8f1b6bed4d1b224c74f87508534cc6392ce6Abramo Bagnara
2820075f8f1b6bed4d1b224c74f87508534cc6392ce6Abramo Bagnara  void *InsertPos = 0;
2821075f8f1b6bed4d1b224c74f87508534cc6392ce6Abramo Bagnara  ParenType *T = ParenTypes.FindNodeOrInsertPos(ID, InsertPos);
2822075f8f1b6bed4d1b224c74f87508534cc6392ce6Abramo Bagnara  if (T)
2823075f8f1b6bed4d1b224c74f87508534cc6392ce6Abramo Bagnara    return QualType(T, 0);
2824075f8f1b6bed4d1b224c74f87508534cc6392ce6Abramo Bagnara
2825075f8f1b6bed4d1b224c74f87508534cc6392ce6Abramo Bagnara  QualType Canon = InnerType;
2826075f8f1b6bed4d1b224c74f87508534cc6392ce6Abramo Bagnara  if (!Canon.isCanonical()) {
2827075f8f1b6bed4d1b224c74f87508534cc6392ce6Abramo Bagnara    Canon = getCanonicalType(InnerType);
2828075f8f1b6bed4d1b224c74f87508534cc6392ce6Abramo Bagnara    ParenType *CheckT = ParenTypes.FindNodeOrInsertPos(ID, InsertPos);
2829075f8f1b6bed4d1b224c74f87508534cc6392ce6Abramo Bagnara    assert(!CheckT && "Paren canonical type broken");
2830075f8f1b6bed4d1b224c74f87508534cc6392ce6Abramo Bagnara    (void)CheckT;
2831075f8f1b6bed4d1b224c74f87508534cc6392ce6Abramo Bagnara  }
2832075f8f1b6bed4d1b224c74f87508534cc6392ce6Abramo Bagnara
2833075f8f1b6bed4d1b224c74f87508534cc6392ce6Abramo Bagnara  T = new (*this) ParenType(InnerType, Canon);
2834075f8f1b6bed4d1b224c74f87508534cc6392ce6Abramo Bagnara  Types.push_back(T);
2835075f8f1b6bed4d1b224c74f87508534cc6392ce6Abramo Bagnara  ParenTypes.InsertNode(T, InsertPos);
2836075f8f1b6bed4d1b224c74f87508534cc6392ce6Abramo Bagnara  return QualType(T, 0);
2837075f8f1b6bed4d1b224c74f87508534cc6392ce6Abramo Bagnara}
2838075f8f1b6bed4d1b224c74f87508534cc6392ce6Abramo Bagnara
28394a2023f5014e82389d5980d307b89c545dbbac81Douglas GregorQualType ASTContext::getDependentNameType(ElaboratedTypeKeyword Keyword,
28404a2023f5014e82389d5980d307b89c545dbbac81Douglas Gregor                                          NestedNameSpecifier *NNS,
28414a2023f5014e82389d5980d307b89c545dbbac81Douglas Gregor                                          const IdentifierInfo *Name,
28424ba2a17694148e16eaa8d3917f657ffcd3667be4Jay Foad                                          QualType Canon) const {
2843d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor  assert(NNS->isDependent() && "nested-name-specifier must be dependent");
2844d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor
2845d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor  if (Canon.isNull()) {
2846d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor    NestedNameSpecifier *CanonNNS = getCanonicalNestedNameSpecifier(NNS);
28474a2023f5014e82389d5980d307b89c545dbbac81Douglas Gregor    ElaboratedTypeKeyword CanonKeyword = Keyword;
28484a2023f5014e82389d5980d307b89c545dbbac81Douglas Gregor    if (Keyword == ETK_None)
28494a2023f5014e82389d5980d307b89c545dbbac81Douglas Gregor      CanonKeyword = ETK_Typename;
28504a2023f5014e82389d5980d307b89c545dbbac81Douglas Gregor
28514a2023f5014e82389d5980d307b89c545dbbac81Douglas Gregor    if (CanonNNS != NNS || CanonKeyword != Keyword)
28524a2023f5014e82389d5980d307b89c545dbbac81Douglas Gregor      Canon = getDependentNameType(CanonKeyword, CanonNNS, Name);
2853d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor  }
2854d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor
2855d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor  llvm::FoldingSetNodeID ID;
28564a2023f5014e82389d5980d307b89c545dbbac81Douglas Gregor  DependentNameType::Profile(ID, Keyword, NNS, Name);
2857d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor
2858d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor  void *InsertPos = 0;
28594714c12a1ab759156b78be8f109ea4c12213af57Douglas Gregor  DependentNameType *T
28604714c12a1ab759156b78be8f109ea4c12213af57Douglas Gregor    = DependentNameTypes.FindNodeOrInsertPos(ID, InsertPos);
2861d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor  if (T)
2862d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor    return QualType(T, 0);
2863d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor
28644a2023f5014e82389d5980d307b89c545dbbac81Douglas Gregor  T = new (*this) DependentNameType(Keyword, NNS, Name, Canon);
2865d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor  Types.push_back(T);
28664714c12a1ab759156b78be8f109ea4c12213af57Douglas Gregor  DependentNameTypes.InsertNode(T, InsertPos);
28671eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  return QualType(T, 0);
2868d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor}
2869d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor
28701eb4433ac451dc16f4133a88af2d002ac26c58efMike StumpQualType
287133500955d731c73717af52088b7fc0e7a85681e7John McCallASTContext::getDependentTemplateSpecializationType(
287233500955d731c73717af52088b7fc0e7a85681e7John McCall                                 ElaboratedTypeKeyword Keyword,
28734a2023f5014e82389d5980d307b89c545dbbac81Douglas Gregor                                 NestedNameSpecifier *NNS,
287433500955d731c73717af52088b7fc0e7a85681e7John McCall                                 const IdentifierInfo *Name,
28754ba2a17694148e16eaa8d3917f657ffcd3667be4Jay Foad                                 const TemplateArgumentListInfo &Args) const {
287633500955d731c73717af52088b7fc0e7a85681e7John McCall  // TODO: avoid this copy
28775f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner  SmallVector<TemplateArgument, 16> ArgCopy;
287833500955d731c73717af52088b7fc0e7a85681e7John McCall  for (unsigned I = 0, E = Args.size(); I != E; ++I)
287933500955d731c73717af52088b7fc0e7a85681e7John McCall    ArgCopy.push_back(Args[I].getArgument());
288033500955d731c73717af52088b7fc0e7a85681e7John McCall  return getDependentTemplateSpecializationType(Keyword, NNS, Name,
288133500955d731c73717af52088b7fc0e7a85681e7John McCall                                                ArgCopy.size(),
288233500955d731c73717af52088b7fc0e7a85681e7John McCall                                                ArgCopy.data());
288333500955d731c73717af52088b7fc0e7a85681e7John McCall}
288433500955d731c73717af52088b7fc0e7a85681e7John McCall
288533500955d731c73717af52088b7fc0e7a85681e7John McCallQualType
288633500955d731c73717af52088b7fc0e7a85681e7John McCallASTContext::getDependentTemplateSpecializationType(
288733500955d731c73717af52088b7fc0e7a85681e7John McCall                                 ElaboratedTypeKeyword Keyword,
288833500955d731c73717af52088b7fc0e7a85681e7John McCall                                 NestedNameSpecifier *NNS,
288933500955d731c73717af52088b7fc0e7a85681e7John McCall                                 const IdentifierInfo *Name,
289033500955d731c73717af52088b7fc0e7a85681e7John McCall                                 unsigned NumArgs,
28914ba2a17694148e16eaa8d3917f657ffcd3667be4Jay Foad                                 const TemplateArgument *Args) const {
2892aa2187de137e5b809dcbbe14f3b61ae907a3d8aaDouglas Gregor  assert((!NNS || NNS->isDependent()) &&
2893aa2187de137e5b809dcbbe14f3b61ae907a3d8aaDouglas Gregor         "nested-name-specifier must be dependent");
28941734317845d60307d474b5da8a8d33adbaf5e723Douglas Gregor
2895789b1f640205e81b5af250693246120f1ce9d147Douglas Gregor  llvm::FoldingSetNodeID ID;
289633500955d731c73717af52088b7fc0e7a85681e7John McCall  DependentTemplateSpecializationType::Profile(ID, *this, Keyword, NNS,
289733500955d731c73717af52088b7fc0e7a85681e7John McCall                                               Name, NumArgs, Args);
2898789b1f640205e81b5af250693246120f1ce9d147Douglas Gregor
2899789b1f640205e81b5af250693246120f1ce9d147Douglas Gregor  void *InsertPos = 0;
290033500955d731c73717af52088b7fc0e7a85681e7John McCall  DependentTemplateSpecializationType *T
290133500955d731c73717af52088b7fc0e7a85681e7John McCall    = DependentTemplateSpecializationTypes.FindNodeOrInsertPos(ID, InsertPos);
2902789b1f640205e81b5af250693246120f1ce9d147Douglas Gregor  if (T)
2903789b1f640205e81b5af250693246120f1ce9d147Douglas Gregor    return QualType(T, 0);
2904789b1f640205e81b5af250693246120f1ce9d147Douglas Gregor
290533500955d731c73717af52088b7fc0e7a85681e7John McCall  NestedNameSpecifier *CanonNNS = getCanonicalNestedNameSpecifier(NNS);
290633500955d731c73717af52088b7fc0e7a85681e7John McCall
290733500955d731c73717af52088b7fc0e7a85681e7John McCall  ElaboratedTypeKeyword CanonKeyword = Keyword;
290833500955d731c73717af52088b7fc0e7a85681e7John McCall  if (Keyword == ETK_None) CanonKeyword = ETK_Typename;
29091734317845d60307d474b5da8a8d33adbaf5e723Douglas Gregor
291033500955d731c73717af52088b7fc0e7a85681e7John McCall  bool AnyNonCanonArgs = false;
29115f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner  SmallVector<TemplateArgument, 16> CanonArgs(NumArgs);
291233500955d731c73717af52088b7fc0e7a85681e7John McCall  for (unsigned I = 0; I != NumArgs; ++I) {
291333500955d731c73717af52088b7fc0e7a85681e7John McCall    CanonArgs[I] = getCanonicalTemplateArgument(Args[I]);
291433500955d731c73717af52088b7fc0e7a85681e7John McCall    if (!CanonArgs[I].structurallyEquals(Args[I]))
291533500955d731c73717af52088b7fc0e7a85681e7John McCall      AnyNonCanonArgs = true;
2916789b1f640205e81b5af250693246120f1ce9d147Douglas Gregor  }
29171734317845d60307d474b5da8a8d33adbaf5e723Douglas Gregor
291833500955d731c73717af52088b7fc0e7a85681e7John McCall  QualType Canon;
291933500955d731c73717af52088b7fc0e7a85681e7John McCall  if (AnyNonCanonArgs || CanonNNS != NNS || CanonKeyword != Keyword) {
292033500955d731c73717af52088b7fc0e7a85681e7John McCall    Canon = getDependentTemplateSpecializationType(CanonKeyword, CanonNNS,
292133500955d731c73717af52088b7fc0e7a85681e7John McCall                                                   Name, NumArgs,
292233500955d731c73717af52088b7fc0e7a85681e7John McCall                                                   CanonArgs.data());
292333500955d731c73717af52088b7fc0e7a85681e7John McCall
292433500955d731c73717af52088b7fc0e7a85681e7John McCall    // Find the insert position again.
292533500955d731c73717af52088b7fc0e7a85681e7John McCall    DependentTemplateSpecializationTypes.FindNodeOrInsertPos(ID, InsertPos);
292633500955d731c73717af52088b7fc0e7a85681e7John McCall  }
292733500955d731c73717af52088b7fc0e7a85681e7John McCall
292833500955d731c73717af52088b7fc0e7a85681e7John McCall  void *Mem = Allocate((sizeof(DependentTemplateSpecializationType) +
292933500955d731c73717af52088b7fc0e7a85681e7John McCall                        sizeof(TemplateArgument) * NumArgs),
293033500955d731c73717af52088b7fc0e7a85681e7John McCall                       TypeAlignment);
2931ef99001908e799c388f1363b1e607dad5f5b57d3John McCall  T = new (Mem) DependentTemplateSpecializationType(Keyword, NNS,
293233500955d731c73717af52088b7fc0e7a85681e7John McCall                                                    Name, NumArgs, Args, Canon);
29331734317845d60307d474b5da8a8d33adbaf5e723Douglas Gregor  Types.push_back(T);
293433500955d731c73717af52088b7fc0e7a85681e7John McCall  DependentTemplateSpecializationTypes.InsertNode(T, InsertPos);
29351eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  return QualType(T, 0);
29361734317845d60307d474b5da8a8d33adbaf5e723Douglas Gregor}
29371734317845d60307d474b5da8a8d33adbaf5e723Douglas Gregor
2938cded4f649cd4b7ba7d461c25c6482ef52b8d3a2aDouglas GregorQualType ASTContext::getPackExpansionType(QualType Pattern,
2939cded4f649cd4b7ba7d461c25c6482ef52b8d3a2aDouglas Gregor                                      llvm::Optional<unsigned> NumExpansions) {
29407536dd5e6c99584481b7dab68b7e7d8df9c54054Douglas Gregor  llvm::FoldingSetNodeID ID;
2941cded4f649cd4b7ba7d461c25c6482ef52b8d3a2aDouglas Gregor  PackExpansionType::Profile(ID, Pattern, NumExpansions);
29427536dd5e6c99584481b7dab68b7e7d8df9c54054Douglas Gregor
29437536dd5e6c99584481b7dab68b7e7d8df9c54054Douglas Gregor  assert(Pattern->containsUnexpandedParameterPack() &&
29447536dd5e6c99584481b7dab68b7e7d8df9c54054Douglas Gregor         "Pack expansions must expand one or more parameter packs");
29457536dd5e6c99584481b7dab68b7e7d8df9c54054Douglas Gregor  void *InsertPos = 0;
29467536dd5e6c99584481b7dab68b7e7d8df9c54054Douglas Gregor  PackExpansionType *T
29477536dd5e6c99584481b7dab68b7e7d8df9c54054Douglas Gregor    = PackExpansionTypes.FindNodeOrInsertPos(ID, InsertPos);
29487536dd5e6c99584481b7dab68b7e7d8df9c54054Douglas Gregor  if (T)
29497536dd5e6c99584481b7dab68b7e7d8df9c54054Douglas Gregor    return QualType(T, 0);
29507536dd5e6c99584481b7dab68b7e7d8df9c54054Douglas Gregor
29517536dd5e6c99584481b7dab68b7e7d8df9c54054Douglas Gregor  QualType Canon;
29527536dd5e6c99584481b7dab68b7e7d8df9c54054Douglas Gregor  if (!Pattern.isCanonical()) {
2953d8672ef2d343a0dbfe838724fb2d9fb4efea6041Richard Smith    Canon = getCanonicalType(Pattern);
2954d8672ef2d343a0dbfe838724fb2d9fb4efea6041Richard Smith    // The canonical type might not contain an unexpanded parameter pack, if it
2955d8672ef2d343a0dbfe838724fb2d9fb4efea6041Richard Smith    // contains an alias template specialization which ignores one of its
2956d8672ef2d343a0dbfe838724fb2d9fb4efea6041Richard Smith    // parameters.
2957d8672ef2d343a0dbfe838724fb2d9fb4efea6041Richard Smith    if (Canon->containsUnexpandedParameterPack()) {
2958d8672ef2d343a0dbfe838724fb2d9fb4efea6041Richard Smith      Canon = getPackExpansionType(getCanonicalType(Pattern), NumExpansions);
2959d8672ef2d343a0dbfe838724fb2d9fb4efea6041Richard Smith
2960d8672ef2d343a0dbfe838724fb2d9fb4efea6041Richard Smith      // Find the insert position again, in case we inserted an element into
2961d8672ef2d343a0dbfe838724fb2d9fb4efea6041Richard Smith      // PackExpansionTypes and invalidated our insert position.
2962d8672ef2d343a0dbfe838724fb2d9fb4efea6041Richard Smith      PackExpansionTypes.FindNodeOrInsertPos(ID, InsertPos);
2963d8672ef2d343a0dbfe838724fb2d9fb4efea6041Richard Smith    }
29647536dd5e6c99584481b7dab68b7e7d8df9c54054Douglas Gregor  }
29657536dd5e6c99584481b7dab68b7e7d8df9c54054Douglas Gregor
2966cded4f649cd4b7ba7d461c25c6482ef52b8d3a2aDouglas Gregor  T = new (*this) PackExpansionType(Pattern, Canon, NumExpansions);
29677536dd5e6c99584481b7dab68b7e7d8df9c54054Douglas Gregor  Types.push_back(T);
29687536dd5e6c99584481b7dab68b7e7d8df9c54054Douglas Gregor  PackExpansionTypes.InsertNode(T, InsertPos);
29697536dd5e6c99584481b7dab68b7e7d8df9c54054Douglas Gregor  return QualType(T, 0);
29707536dd5e6c99584481b7dab68b7e7d8df9c54054Douglas Gregor}
29717536dd5e6c99584481b7dab68b7e7d8df9c54054Douglas Gregor
297288cb27a160adc305783a44f922ee4b216006ebf9Chris Lattner/// CmpProtocolNames - Comparison predicate for sorting protocols
297388cb27a160adc305783a44f922ee4b216006ebf9Chris Lattner/// alphabetically.
297488cb27a160adc305783a44f922ee4b216006ebf9Chris Lattnerstatic bool CmpProtocolNames(const ObjCProtocolDecl *LHS,
297588cb27a160adc305783a44f922ee4b216006ebf9Chris Lattner                            const ObjCProtocolDecl *RHS) {
29762e1cd4264d363ca869bf37ef160902f211d21b8cDouglas Gregor  return LHS->getDeclName() < RHS->getDeclName();
297788cb27a160adc305783a44f922ee4b216006ebf9Chris Lattner}
297888cb27a160adc305783a44f922ee4b216006ebf9Chris Lattner
2979c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCallstatic bool areSortedAndUniqued(ObjCProtocolDecl * const *Protocols,
298054e14c4db764c0636160d26c5bbf491637c83a76John McCall                                unsigned NumProtocols) {
298154e14c4db764c0636160d26c5bbf491637c83a76John McCall  if (NumProtocols == 0) return true;
298254e14c4db764c0636160d26c5bbf491637c83a76John McCall
298361cc296de6c1f82fa84c0abb3ecd142a584838efDouglas Gregor  if (Protocols[0]->getCanonicalDecl() != Protocols[0])
298461cc296de6c1f82fa84c0abb3ecd142a584838efDouglas Gregor    return false;
298561cc296de6c1f82fa84c0abb3ecd142a584838efDouglas Gregor
298654e14c4db764c0636160d26c5bbf491637c83a76John McCall  for (unsigned i = 1; i != NumProtocols; ++i)
298761cc296de6c1f82fa84c0abb3ecd142a584838efDouglas Gregor    if (!CmpProtocolNames(Protocols[i-1], Protocols[i]) ||
298861cc296de6c1f82fa84c0abb3ecd142a584838efDouglas Gregor        Protocols[i]->getCanonicalDecl() != Protocols[i])
298954e14c4db764c0636160d26c5bbf491637c83a76John McCall      return false;
299054e14c4db764c0636160d26c5bbf491637c83a76John McCall  return true;
299154e14c4db764c0636160d26c5bbf491637c83a76John McCall}
299254e14c4db764c0636160d26c5bbf491637c83a76John McCall
299354e14c4db764c0636160d26c5bbf491637c83a76John McCallstatic void SortAndUniqueProtocols(ObjCProtocolDecl **Protocols,
299488cb27a160adc305783a44f922ee4b216006ebf9Chris Lattner                                   unsigned &NumProtocols) {
299588cb27a160adc305783a44f922ee4b216006ebf9Chris Lattner  ObjCProtocolDecl **ProtocolsEnd = Protocols+NumProtocols;
29961eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
299788cb27a160adc305783a44f922ee4b216006ebf9Chris Lattner  // Sort protocols, keyed by name.
299888cb27a160adc305783a44f922ee4b216006ebf9Chris Lattner  std::sort(Protocols, Protocols+NumProtocols, CmpProtocolNames);
299988cb27a160adc305783a44f922ee4b216006ebf9Chris Lattner
300061cc296de6c1f82fa84c0abb3ecd142a584838efDouglas Gregor  // Canonicalize.
300161cc296de6c1f82fa84c0abb3ecd142a584838efDouglas Gregor  for (unsigned I = 0, N = NumProtocols; I != N; ++I)
300261cc296de6c1f82fa84c0abb3ecd142a584838efDouglas Gregor    Protocols[I] = Protocols[I]->getCanonicalDecl();
300361cc296de6c1f82fa84c0abb3ecd142a584838efDouglas Gregor
300488cb27a160adc305783a44f922ee4b216006ebf9Chris Lattner  // Remove duplicates.
300588cb27a160adc305783a44f922ee4b216006ebf9Chris Lattner  ProtocolsEnd = std::unique(Protocols, ProtocolsEnd);
300688cb27a160adc305783a44f922ee4b216006ebf9Chris Lattner  NumProtocols = ProtocolsEnd-Protocols;
300788cb27a160adc305783a44f922ee4b216006ebf9Chris Lattner}
300888cb27a160adc305783a44f922ee4b216006ebf9Chris Lattner
3009c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCallQualType ASTContext::getObjCObjectType(QualType BaseType,
3010c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall                                       ObjCProtocolDecl * const *Protocols,
30114ba2a17694148e16eaa8d3917f657ffcd3667be4Jay Foad                                       unsigned NumProtocols) const {
3012c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall  // If the base type is an interface and there aren't any protocols
3013c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall  // to add, then the interface type will do just fine.
3014c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall  if (!NumProtocols && isa<ObjCInterfaceType>(BaseType))
3015c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall    return BaseType;
3016d1b3c2dd5bc1f3103bee6137957aa7c5f8f2f0bcSteve Naroff
3017c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall  // Look in the folding set for an existing type.
3018c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall  llvm::FoldingSetNodeID ID;
3019c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall  ObjCObjectTypeImpl::Profile(ID, BaseType, Protocols, NumProtocols);
3020d1b3c2dd5bc1f3103bee6137957aa7c5f8f2f0bcSteve Naroff  void *InsertPos = 0;
3021c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall  if (ObjCObjectType *QT = ObjCObjectTypes.FindNodeOrInsertPos(ID, InsertPos))
3022c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall    return QualType(QT, 0);
3023d1b3c2dd5bc1f3103bee6137957aa7c5f8f2f0bcSteve Naroff
3024c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall  // Build the canonical type, which has the canonical base type and
3025c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall  // a sorted-and-uniqued list of protocols.
302654e14c4db764c0636160d26c5bbf491637c83a76John McCall  QualType Canonical;
3027c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall  bool ProtocolsSorted = areSortedAndUniqued(Protocols, NumProtocols);
3028c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall  if (!ProtocolsSorted || !BaseType.isCanonical()) {
3029c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall    if (!ProtocolsSorted) {
30305f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner      SmallVector<ObjCProtocolDecl*, 8> Sorted(Protocols,
30310237941e0beb0c929934b66ad29443b484d987feBenjamin Kramer                                                     Protocols + NumProtocols);
303254e14c4db764c0636160d26c5bbf491637c83a76John McCall      unsigned UniqueCount = NumProtocols;
303354e14c4db764c0636160d26c5bbf491637c83a76John McCall
303454e14c4db764c0636160d26c5bbf491637c83a76John McCall      SortAndUniqueProtocols(&Sorted[0], UniqueCount);
3035c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall      Canonical = getObjCObjectType(getCanonicalType(BaseType),
3036c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall                                    &Sorted[0], UniqueCount);
303754e14c4db764c0636160d26c5bbf491637c83a76John McCall    } else {
3038c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall      Canonical = getObjCObjectType(getCanonicalType(BaseType),
3039c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall                                    Protocols, NumProtocols);
304054e14c4db764c0636160d26c5bbf491637c83a76John McCall    }
304154e14c4db764c0636160d26c5bbf491637c83a76John McCall
304254e14c4db764c0636160d26c5bbf491637c83a76John McCall    // Regenerate InsertPos.
3043c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall    ObjCObjectTypes.FindNodeOrInsertPos(ID, InsertPos);
304454e14c4db764c0636160d26c5bbf491637c83a76John McCall  }
304554e14c4db764c0636160d26c5bbf491637c83a76John McCall
3046c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall  unsigned Size = sizeof(ObjCObjectTypeImpl);
3047c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall  Size += NumProtocols * sizeof(ObjCProtocolDecl *);
3048fd6a0887a099256c35a5b23e9afd517ffe95fa0aDouglas Gregor  void *Mem = Allocate(Size, TypeAlignment);
3049c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall  ObjCObjectTypeImpl *T =
3050c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall    new (Mem) ObjCObjectTypeImpl(Canonical, BaseType, Protocols, NumProtocols);
30511eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
3052c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall  Types.push_back(T);
3053c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall  ObjCObjectTypes.InsertNode(T, InsertPos);
3054c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall  return QualType(T, 0);
3055d1b3c2dd5bc1f3103bee6137957aa7c5f8f2f0bcSteve Naroff}
305688cb27a160adc305783a44f922ee4b216006ebf9Chris Lattner
3057c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall/// getObjCObjectPointerType - Return a ObjCObjectPointerType type for
3058c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall/// the given object type.
30594ba2a17694148e16eaa8d3917f657ffcd3667be4Jay FoadQualType ASTContext::getObjCObjectPointerType(QualType ObjectT) const {
30604b6c9051c6522894978c9ba6a819a659d102db36Fariborz Jahanian  llvm::FoldingSetNodeID ID;
3061c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall  ObjCObjectPointerType::Profile(ID, ObjectT);
30621eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
30634b6c9051c6522894978c9ba6a819a659d102db36Fariborz Jahanian  void *InsertPos = 0;
3064c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall  if (ObjCObjectPointerType *QT =
3065c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall              ObjCObjectPointerTypes.FindNodeOrInsertPos(ID, InsertPos))
30664b6c9051c6522894978c9ba6a819a659d102db36Fariborz Jahanian    return QualType(QT, 0);
30671eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
3068c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall  // Find the canonical object type.
306954e14c4db764c0636160d26c5bbf491637c83a76John McCall  QualType Canonical;
3070c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall  if (!ObjectT.isCanonical()) {
3071c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall    Canonical = getObjCObjectPointerType(getCanonicalType(ObjectT));
307254e14c4db764c0636160d26c5bbf491637c83a76John McCall
3073c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall    // Regenerate InsertPos.
3074c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall    ObjCObjectPointerTypes.FindNodeOrInsertPos(ID, InsertPos);
307554e14c4db764c0636160d26c5bbf491637c83a76John McCall  }
307654e14c4db764c0636160d26c5bbf491637c83a76John McCall
3077c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall  // No match.
3078c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall  void *Mem = Allocate(sizeof(ObjCObjectPointerType), TypeAlignment);
3079c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall  ObjCObjectPointerType *QType =
3080c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall    new (Mem) ObjCObjectPointerType(Canonical, ObjectT);
308124fab41057e4b67ed69a6b4027d5ae0f2f6934dcArgyrios Kyrtzidis
308224fab41057e4b67ed69a6b4027d5ae0f2f6934dcArgyrios Kyrtzidis  Types.push_back(QType);
3083c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall  ObjCObjectPointerTypes.InsertNode(QType, InsertPos);
308424fab41057e4b67ed69a6b4027d5ae0f2f6934dcArgyrios Kyrtzidis  return QualType(QType, 0);
308524fab41057e4b67ed69a6b4027d5ae0f2f6934dcArgyrios Kyrtzidis}
308624fab41057e4b67ed69a6b4027d5ae0f2f6934dcArgyrios Kyrtzidis
3087deacbdca554298ccdf636f19c6094a8825ec6b34Douglas Gregor/// getObjCInterfaceType - Return the unique reference to the type for the
3088deacbdca554298ccdf636f19c6094a8825ec6b34Douglas Gregor/// specified ObjC interface decl. The list of protocols is optional.
30890af550115df1f57f17a4f125ff0e8b34820c65d1Douglas GregorQualType ASTContext::getObjCInterfaceType(const ObjCInterfaceDecl *Decl,
30900af550115df1f57f17a4f125ff0e8b34820c65d1Douglas Gregor                                          ObjCInterfaceDecl *PrevDecl) const {
3091deacbdca554298ccdf636f19c6094a8825ec6b34Douglas Gregor  if (Decl->TypeForDecl)
3092deacbdca554298ccdf636f19c6094a8825ec6b34Douglas Gregor    return QualType(Decl->TypeForDecl, 0);
309374c730ad1f6818b676b0bad46d806a9176950328Sebastian Redl
30940af550115df1f57f17a4f125ff0e8b34820c65d1Douglas Gregor  if (PrevDecl) {
30950af550115df1f57f17a4f125ff0e8b34820c65d1Douglas Gregor    assert(PrevDecl->TypeForDecl && "previous decl has no TypeForDecl");
30960af550115df1f57f17a4f125ff0e8b34820c65d1Douglas Gregor    Decl->TypeForDecl = PrevDecl->TypeForDecl;
30970af550115df1f57f17a4f125ff0e8b34820c65d1Douglas Gregor    return QualType(PrevDecl->TypeForDecl, 0);
30980af550115df1f57f17a4f125ff0e8b34820c65d1Douglas Gregor  }
30990af550115df1f57f17a4f125ff0e8b34820c65d1Douglas Gregor
31008d2dbbf9ddfd9d762a341d83f83d840ff68ce03dDouglas Gregor  // Prefer the definition, if there is one.
31018d2dbbf9ddfd9d762a341d83f83d840ff68ce03dDouglas Gregor  if (const ObjCInterfaceDecl *Def = Decl->getDefinition())
31028d2dbbf9ddfd9d762a341d83f83d840ff68ce03dDouglas Gregor    Decl = Def;
31038d2dbbf9ddfd9d762a341d83f83d840ff68ce03dDouglas Gregor
3104deacbdca554298ccdf636f19c6094a8825ec6b34Douglas Gregor  void *Mem = Allocate(sizeof(ObjCInterfaceType), TypeAlignment);
3105deacbdca554298ccdf636f19c6094a8825ec6b34Douglas Gregor  ObjCInterfaceType *T = new (Mem) ObjCInterfaceType(Decl);
3106deacbdca554298ccdf636f19c6094a8825ec6b34Douglas Gregor  Decl->TypeForDecl = T;
3107deacbdca554298ccdf636f19c6094a8825ec6b34Douglas Gregor  Types.push_back(T);
3108deacbdca554298ccdf636f19c6094a8825ec6b34Douglas Gregor  return QualType(T, 0);
3109c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall}
3110c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall
311172564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor/// getTypeOfExprType - Unlike many "get<Type>" functions, we can't unique
311272564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor/// TypeOfExprType AST's (since expression's are never shared). For example,
31139752f25748d954df99087d741ea35db37ff16beaSteve Naroff/// multiple declarations that refer to "typeof(x)" all contain different
31141eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// DeclRefExpr's. This doesn't effect the type checker, since it operates
31159752f25748d954df99087d741ea35db37ff16beaSteve Naroff/// on canonical type's (which are always unique).
31164ba2a17694148e16eaa8d3917f657ffcd3667be4Jay FoadQualType ASTContext::getTypeOfExprType(Expr *tofExpr) const {
3117dd0257c77719a13d4acd513df40b04300cbfc871Douglas Gregor  TypeOfExprType *toe;
3118b197572cf1cd70a817a1f546478cb2cb9112c48eDouglas Gregor  if (tofExpr->isTypeDependent()) {
3119b197572cf1cd70a817a1f546478cb2cb9112c48eDouglas Gregor    llvm::FoldingSetNodeID ID;
3120b197572cf1cd70a817a1f546478cb2cb9112c48eDouglas Gregor    DependentTypeOfExprType::Profile(ID, *this, tofExpr);
31211eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
3122b197572cf1cd70a817a1f546478cb2cb9112c48eDouglas Gregor    void *InsertPos = 0;
3123b197572cf1cd70a817a1f546478cb2cb9112c48eDouglas Gregor    DependentTypeOfExprType *Canon
3124b197572cf1cd70a817a1f546478cb2cb9112c48eDouglas Gregor      = DependentTypeOfExprTypes.FindNodeOrInsertPos(ID, InsertPos);
3125b197572cf1cd70a817a1f546478cb2cb9112c48eDouglas Gregor    if (Canon) {
3126b197572cf1cd70a817a1f546478cb2cb9112c48eDouglas Gregor      // We already have a "canonical" version of an identical, dependent
3127b197572cf1cd70a817a1f546478cb2cb9112c48eDouglas Gregor      // typeof(expr) type. Use that as our canonical type.
31286b304a0254a13f42390b865ff5ba668a49cc58aeJohn McCall      toe = new (*this, TypeAlignment) TypeOfExprType(tofExpr,
3129b197572cf1cd70a817a1f546478cb2cb9112c48eDouglas Gregor                                          QualType((TypeOfExprType*)Canon, 0));
31303060178ad9df29789505c1e6debcfc80a3a13587Chad Rosier    } else {
3131b197572cf1cd70a817a1f546478cb2cb9112c48eDouglas Gregor      // Build a new, canonical typeof(expr) type.
31326b304a0254a13f42390b865ff5ba668a49cc58aeJohn McCall      Canon
31336b304a0254a13f42390b865ff5ba668a49cc58aeJohn McCall        = new (*this, TypeAlignment) DependentTypeOfExprType(*this, tofExpr);
3134b197572cf1cd70a817a1f546478cb2cb9112c48eDouglas Gregor      DependentTypeOfExprTypes.InsertNode(Canon, InsertPos);
3135b197572cf1cd70a817a1f546478cb2cb9112c48eDouglas Gregor      toe = Canon;
3136b197572cf1cd70a817a1f546478cb2cb9112c48eDouglas Gregor    }
3137b197572cf1cd70a817a1f546478cb2cb9112c48eDouglas Gregor  } else {
3138dd0257c77719a13d4acd513df40b04300cbfc871Douglas Gregor    QualType Canonical = getCanonicalType(tofExpr->getType());
31396b304a0254a13f42390b865ff5ba668a49cc58aeJohn McCall    toe = new (*this, TypeAlignment) TypeOfExprType(tofExpr, Canonical);
3140dd0257c77719a13d4acd513df40b04300cbfc871Douglas Gregor  }
31419752f25748d954df99087d741ea35db37ff16beaSteve Naroff  Types.push_back(toe);
31429752f25748d954df99087d741ea35db37ff16beaSteve Naroff  return QualType(toe, 0);
3143d1861fd633d5096a00777c918eb8575ea7162fe7Steve Naroff}
3144d1861fd633d5096a00777c918eb8575ea7162fe7Steve Naroff
31459752f25748d954df99087d741ea35db37ff16beaSteve Naroff/// getTypeOfType -  Unlike many "get<Type>" functions, we don't unique
31469752f25748d954df99087d741ea35db37ff16beaSteve Naroff/// TypeOfType AST's. The only motivation to unique these nodes would be
31479752f25748d954df99087d741ea35db37ff16beaSteve Naroff/// memory savings. Since typeof(t) is fairly uncommon, space shouldn't be
31481eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// an issue. This doesn't effect the type checker, since it operates
31499752f25748d954df99087d741ea35db37ff16beaSteve Naroff/// on canonical type's (which are always unique).
31504ba2a17694148e16eaa8d3917f657ffcd3667be4Jay FoadQualType ASTContext::getTypeOfType(QualType tofType) const {
3151f52ab250ff92bc51a9ac9a8e19bd43b63a5f844fChris Lattner  QualType Canonical = getCanonicalType(tofType);
31526b304a0254a13f42390b865ff5ba668a49cc58aeJohn McCall  TypeOfType *tot = new (*this, TypeAlignment) TypeOfType(tofType, Canonical);
31539752f25748d954df99087d741ea35db37ff16beaSteve Naroff  Types.push_back(tot);
31549752f25748d954df99087d741ea35db37ff16beaSteve Naroff  return QualType(tot, 0);
3155d1861fd633d5096a00777c918eb8575ea7162fe7Steve Naroff}
3156d1861fd633d5096a00777c918eb8575ea7162fe7Steve Naroff
315760a9a2a404a4cf259d39133383e922aa00ca9043Anders Carlsson
3158395b475a4474f1c7574d927ad142ca0c7997cbcaAnders Carlsson/// getDecltypeType -  Unlike many "get<Type>" functions, we don't unique
3159395b475a4474f1c7574d927ad142ca0c7997cbcaAnders Carlsson/// DecltypeType AST's. The only motivation to unique these nodes would be
3160395b475a4474f1c7574d927ad142ca0c7997cbcaAnders Carlsson/// memory savings. Since decltype(t) is fairly uncommon, space shouldn't be
31611eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// an issue. This doesn't effect the type checker, since it operates
316239e02032b01874a0d02ba85a4cd3922adda81376David Blaikie/// on canonical types (which are always unique).
3163f8af98286022f72157d84951b48fde5fb369ab29Douglas GregorQualType ASTContext::getDecltypeType(Expr *e, QualType UnderlyingType) const {
3164dd0257c77719a13d4acd513df40b04300cbfc871Douglas Gregor  DecltypeType *dt;
3165561f81243f665cf2001caadc45df505f826b72d6Douglas Gregor
3166561f81243f665cf2001caadc45df505f826b72d6Douglas Gregor  // C++0x [temp.type]p2:
3167561f81243f665cf2001caadc45df505f826b72d6Douglas Gregor  //   If an expression e involves a template parameter, decltype(e) denotes a
3168561f81243f665cf2001caadc45df505f826b72d6Douglas Gregor  //   unique dependent type. Two such decltype-specifiers refer to the same
3169561f81243f665cf2001caadc45df505f826b72d6Douglas Gregor  //   type only if their expressions are equivalent (14.5.6.1).
3170561f81243f665cf2001caadc45df505f826b72d6Douglas Gregor  if (e->isInstantiationDependent()) {
31719d702ae1cd5cfa19d884cbef77e1df99395138bbDouglas Gregor    llvm::FoldingSetNodeID ID;
31729d702ae1cd5cfa19d884cbef77e1df99395138bbDouglas Gregor    DependentDecltypeType::Profile(ID, *this, e);
31731eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
31749d702ae1cd5cfa19d884cbef77e1df99395138bbDouglas Gregor    void *InsertPos = 0;
31759d702ae1cd5cfa19d884cbef77e1df99395138bbDouglas Gregor    DependentDecltypeType *Canon
31769d702ae1cd5cfa19d884cbef77e1df99395138bbDouglas Gregor      = DependentDecltypeTypes.FindNodeOrInsertPos(ID, InsertPos);
31779d702ae1cd5cfa19d884cbef77e1df99395138bbDouglas Gregor    if (Canon) {
31789d702ae1cd5cfa19d884cbef77e1df99395138bbDouglas Gregor      // We already have a "canonical" version of an equivalent, dependent
31799d702ae1cd5cfa19d884cbef77e1df99395138bbDouglas Gregor      // decltype type. Use that as our canonical type.
31800d729105ecb50a7e3cbe6e57c29149edfa5cf05aRichard Smith      dt = new (*this, TypeAlignment) DecltypeType(e, UnderlyingType,
31819d702ae1cd5cfa19d884cbef77e1df99395138bbDouglas Gregor                                       QualType((DecltypeType*)Canon, 0));
31823060178ad9df29789505c1e6debcfc80a3a13587Chad Rosier    } else {
31839d702ae1cd5cfa19d884cbef77e1df99395138bbDouglas Gregor      // Build a new, canonical typeof(expr) type.
31846b304a0254a13f42390b865ff5ba668a49cc58aeJohn McCall      Canon = new (*this, TypeAlignment) DependentDecltypeType(*this, e);
31859d702ae1cd5cfa19d884cbef77e1df99395138bbDouglas Gregor      DependentDecltypeTypes.InsertNode(Canon, InsertPos);
31869d702ae1cd5cfa19d884cbef77e1df99395138bbDouglas Gregor      dt = Canon;
31879d702ae1cd5cfa19d884cbef77e1df99395138bbDouglas Gregor    }
31889d702ae1cd5cfa19d884cbef77e1df99395138bbDouglas Gregor  } else {
3189f8af98286022f72157d84951b48fde5fb369ab29Douglas Gregor    dt = new (*this, TypeAlignment) DecltypeType(e, UnderlyingType,
3190f8af98286022f72157d84951b48fde5fb369ab29Douglas Gregor                                      getCanonicalType(UnderlyingType));
3191dd0257c77719a13d4acd513df40b04300cbfc871Douglas Gregor  }
3192395b475a4474f1c7574d927ad142ca0c7997cbcaAnders Carlsson  Types.push_back(dt);
3193395b475a4474f1c7574d927ad142ca0c7997cbcaAnders Carlsson  return QualType(dt, 0);
3194395b475a4474f1c7574d927ad142ca0c7997cbcaAnders Carlsson}
3195395b475a4474f1c7574d927ad142ca0c7997cbcaAnders Carlsson
3196ca63c200346c0ca9e00194ec6e34a5a7b0ed9321Sean Hunt/// getUnaryTransformationType - We don't unique these, since the memory
3197ca63c200346c0ca9e00194ec6e34a5a7b0ed9321Sean Hunt/// savings are minimal and these are rare.
3198ca63c200346c0ca9e00194ec6e34a5a7b0ed9321Sean HuntQualType ASTContext::getUnaryTransformType(QualType BaseType,
3199ca63c200346c0ca9e00194ec6e34a5a7b0ed9321Sean Hunt                                           QualType UnderlyingType,
3200ca63c200346c0ca9e00194ec6e34a5a7b0ed9321Sean Hunt                                           UnaryTransformType::UTTKind Kind)
3201ca63c200346c0ca9e00194ec6e34a5a7b0ed9321Sean Hunt    const {
3202ca63c200346c0ca9e00194ec6e34a5a7b0ed9321Sean Hunt  UnaryTransformType *Ty =
320369d9775da47a4b9f165dbc33277f02982928a94eDouglas Gregor    new (*this, TypeAlignment) UnaryTransformType (BaseType, UnderlyingType,
320469d9775da47a4b9f165dbc33277f02982928a94eDouglas Gregor                                                   Kind,
320569d9775da47a4b9f165dbc33277f02982928a94eDouglas Gregor                                 UnderlyingType->isDependentType() ?
320612fc4b0624706b474fa10308631fa8daf92f340fPeter Collingbourne                                 QualType() : getCanonicalType(UnderlyingType));
3207ca63c200346c0ca9e00194ec6e34a5a7b0ed9321Sean Hunt  Types.push_back(Ty);
3208ca63c200346c0ca9e00194ec6e34a5a7b0ed9321Sean Hunt  return QualType(Ty, 0);
3209ca63c200346c0ca9e00194ec6e34a5a7b0ed9321Sean Hunt}
3210ca63c200346c0ca9e00194ec6e34a5a7b0ed9321Sean Hunt
3211483b9f3bc05c5409e2c6643f1c9d91e21c8ff9d2Richard Smith/// getAutoType - We only unique auto types after they've been deduced.
321234b41d939a1328f484511c6002ba2456db879a29Richard SmithQualType ASTContext::getAutoType(QualType DeducedType) const {
3213483b9f3bc05c5409e2c6643f1c9d91e21c8ff9d2Richard Smith  void *InsertPos = 0;
3214483b9f3bc05c5409e2c6643f1c9d91e21c8ff9d2Richard Smith  if (!DeducedType.isNull()) {
3215483b9f3bc05c5409e2c6643f1c9d91e21c8ff9d2Richard Smith    // Look in the folding set for an existing type.
3216483b9f3bc05c5409e2c6643f1c9d91e21c8ff9d2Richard Smith    llvm::FoldingSetNodeID ID;
3217483b9f3bc05c5409e2c6643f1c9d91e21c8ff9d2Richard Smith    AutoType::Profile(ID, DeducedType);
3218483b9f3bc05c5409e2c6643f1c9d91e21c8ff9d2Richard Smith    if (AutoType *AT = AutoTypes.FindNodeOrInsertPos(ID, InsertPos))
3219483b9f3bc05c5409e2c6643f1c9d91e21c8ff9d2Richard Smith      return QualType(AT, 0);
3220483b9f3bc05c5409e2c6643f1c9d91e21c8ff9d2Richard Smith  }
3221483b9f3bc05c5409e2c6643f1c9d91e21c8ff9d2Richard Smith
3222483b9f3bc05c5409e2c6643f1c9d91e21c8ff9d2Richard Smith  AutoType *AT = new (*this, TypeAlignment) AutoType(DeducedType);
3223483b9f3bc05c5409e2c6643f1c9d91e21c8ff9d2Richard Smith  Types.push_back(AT);
3224483b9f3bc05c5409e2c6643f1c9d91e21c8ff9d2Richard Smith  if (InsertPos)
3225483b9f3bc05c5409e2c6643f1c9d91e21c8ff9d2Richard Smith    AutoTypes.InsertNode(AT, InsertPos);
3226483b9f3bc05c5409e2c6643f1c9d91e21c8ff9d2Richard Smith  return QualType(AT, 0);
322734b41d939a1328f484511c6002ba2456db879a29Richard Smith}
322834b41d939a1328f484511c6002ba2456db879a29Richard Smith
3229b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman/// getAtomicType - Return the uniqued reference to the atomic type for
3230b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman/// the given value type.
3231b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli FriedmanQualType ASTContext::getAtomicType(QualType T) const {
3232b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman  // Unique pointers, to guarantee there is only one pointer of a particular
3233b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman  // structure.
3234b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman  llvm::FoldingSetNodeID ID;
3235b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman  AtomicType::Profile(ID, T);
3236b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman
3237b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman  void *InsertPos = 0;
3238b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman  if (AtomicType *AT = AtomicTypes.FindNodeOrInsertPos(ID, InsertPos))
3239b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman    return QualType(AT, 0);
3240b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman
3241b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman  // If the atomic value type isn't canonical, this won't be a canonical type
3242b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman  // either, so fill in the canonical type field.
3243b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman  QualType Canonical;
3244b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman  if (!T.isCanonical()) {
3245b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman    Canonical = getAtomicType(getCanonicalType(T));
3246b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman
3247b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman    // Get the new insert position for the node we care about.
3248b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman    AtomicType *NewIP = AtomicTypes.FindNodeOrInsertPos(ID, InsertPos);
3249b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman    assert(NewIP == 0 && "Shouldn't be in the map!"); (void)NewIP;
3250b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman  }
3251b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman  AtomicType *New = new (*this, TypeAlignment) AtomicType(T, Canonical);
3252b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman  Types.push_back(New);
3253b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman  AtomicTypes.InsertNode(New, InsertPos);
3254b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman  return QualType(New, 0);
3255b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman}
3256b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman
3257ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith/// getAutoDeductType - Get type pattern for deducing against 'auto'.
3258ad762fcdc16b9e4705b12b09d92b8c026212b906Richard SmithQualType ASTContext::getAutoDeductType() const {
3259ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith  if (AutoDeductTy.isNull())
3260ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith    AutoDeductTy = getAutoType(QualType());
3261ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith  assert(!AutoDeductTy.isNull() && "can't build 'auto' pattern");
3262ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith  return AutoDeductTy;
3263ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith}
3264ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith
3265ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith/// getAutoRRefDeductType - Get type pattern for deducing against 'auto &&'.
3266ad762fcdc16b9e4705b12b09d92b8c026212b906Richard SmithQualType ASTContext::getAutoRRefDeductType() const {
3267ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith  if (AutoRRefDeductTy.isNull())
3268ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith    AutoRRefDeductTy = getRValueReferenceType(getAutoDeductType());
3269ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith  assert(!AutoRRefDeductTy.isNull() && "can't build 'auto &&' pattern");
3270ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith  return AutoRRefDeductTy;
3271ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith}
3272ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith
32735f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// getTagDeclType - Return the unique reference to the type for the
32745f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// specified TagDecl (struct/union/class/enum) decl.
32754ba2a17694148e16eaa8d3917f657ffcd3667be4Jay FoadQualType ASTContext::getTagDeclType(const TagDecl *Decl) const {
3276d778f88d32b96a74c9edb7342c81357606a7cdc0Ted Kremenek  assert (Decl);
3277e607ed068334bacb8d7b093996b4671c6ca79e25Mike Stump  // FIXME: What is the design on getTagDeclType when it requires casting
3278e607ed068334bacb8d7b093996b4671c6ca79e25Mike Stump  // away const?  mutable?
3279e607ed068334bacb8d7b093996b4671c6ca79e25Mike Stump  return getTypeDeclType(const_cast<TagDecl*>(Decl));
32805f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
32815f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
32821eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// getSizeType - Return the unique type for "size_t" (C99 7.17), the result
32831eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// of the sizeof operator (C99 6.5.3.4p4). The value is target dependent and
32841eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// needs to agree with the definition in <stddef.h>.
3285a3ccda58913cc1a4b8564e349448b12acc462da7Anders CarlssonCanQualType ASTContext::getSizeType() const {
3286bcfd1f55bfbb3e5944cd5e03d07b343e280838c4Douglas Gregor  return getFromTargetType(Target->getSizeType());
32875f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
32885f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
328929e97cb35fab314388f62b68fefa78947e93c1dcHans Wennborg/// getIntMaxType - Return the unique type for "intmax_t" (C99 7.18.1.5).
329029e97cb35fab314388f62b68fefa78947e93c1dcHans WennborgCanQualType ASTContext::getIntMaxType() const {
329129e97cb35fab314388f62b68fefa78947e93c1dcHans Wennborg  return getFromTargetType(Target->getIntMaxType());
329229e97cb35fab314388f62b68fefa78947e93c1dcHans Wennborg}
329329e97cb35fab314388f62b68fefa78947e93c1dcHans Wennborg
329429e97cb35fab314388f62b68fefa78947e93c1dcHans Wennborg/// getUIntMaxType - Return the unique type for "uintmax_t" (C99 7.18.1.5).
329529e97cb35fab314388f62b68fefa78947e93c1dcHans WennborgCanQualType ASTContext::getUIntMaxType() const {
329629e97cb35fab314388f62b68fefa78947e93c1dcHans Wennborg  return getFromTargetType(Target->getUIntMaxType());
329729e97cb35fab314388f62b68fefa78947e93c1dcHans Wennborg}
329829e97cb35fab314388f62b68fefa78947e93c1dcHans Wennborg
329964c438a4be2a871fa43c78264663ba1e9788b94dArgyrios Kyrtzidis/// getSignedWCharType - Return the type of "signed wchar_t".
330064c438a4be2a871fa43c78264663ba1e9788b94dArgyrios Kyrtzidis/// Used when in C++, as a GCC extension.
330164c438a4be2a871fa43c78264663ba1e9788b94dArgyrios KyrtzidisQualType ASTContext::getSignedWCharType() const {
330264c438a4be2a871fa43c78264663ba1e9788b94dArgyrios Kyrtzidis  // FIXME: derive from "Target" ?
330364c438a4be2a871fa43c78264663ba1e9788b94dArgyrios Kyrtzidis  return WCharTy;
330464c438a4be2a871fa43c78264663ba1e9788b94dArgyrios Kyrtzidis}
330564c438a4be2a871fa43c78264663ba1e9788b94dArgyrios Kyrtzidis
330664c438a4be2a871fa43c78264663ba1e9788b94dArgyrios Kyrtzidis/// getUnsignedWCharType - Return the type of "unsigned wchar_t".
330764c438a4be2a871fa43c78264663ba1e9788b94dArgyrios Kyrtzidis/// Used when in C++, as a GCC extension.
330864c438a4be2a871fa43c78264663ba1e9788b94dArgyrios KyrtzidisQualType ASTContext::getUnsignedWCharType() const {
330964c438a4be2a871fa43c78264663ba1e9788b94dArgyrios Kyrtzidis  // FIXME: derive from "Target" ?
331064c438a4be2a871fa43c78264663ba1e9788b94dArgyrios Kyrtzidis  return UnsignedIntTy;
331164c438a4be2a871fa43c78264663ba1e9788b94dArgyrios Kyrtzidis}
331264c438a4be2a871fa43c78264663ba1e9788b94dArgyrios Kyrtzidis
331329e97cb35fab314388f62b68fefa78947e93c1dcHans Wennborg/// getPointerDiffType - Return the unique type for "ptrdiff_t" (C99 7.17)
33148b9023ba35a86838789e2c9034a6128728c547aaChris Lattner/// defined in <stddef.h>. Pointer - pointer requires this (C99 6.5.6p9).
33158b9023ba35a86838789e2c9034a6128728c547aaChris LattnerQualType ASTContext::getPointerDiffType() const {
3316bcfd1f55bfbb3e5944cd5e03d07b343e280838c4Douglas Gregor  return getFromTargetType(Target->getPtrDiffType(0));
33178b9023ba35a86838789e2c9034a6128728c547aaChris Lattner}
33188b9023ba35a86838789e2c9034a6128728c547aaChris Lattner
3319e6327747b72bb687c948270f702ff53c30f411a6Chris Lattner//===----------------------------------------------------------------------===//
3320e6327747b72bb687c948270f702ff53c30f411a6Chris Lattner//                              Type Operators
3321e6327747b72bb687c948270f702ff53c30f411a6Chris Lattner//===----------------------------------------------------------------------===//
3322e6327747b72bb687c948270f702ff53c30f411a6Chris Lattner
33234ba2a17694148e16eaa8d3917f657ffcd3667be4Jay FoadCanQualType ASTContext::getCanonicalParamType(QualType T) const {
332454e14c4db764c0636160d26c5bbf491637c83a76John McCall  // Push qualifiers into arrays, and then discard any remaining
332554e14c4db764c0636160d26c5bbf491637c83a76John McCall  // qualifiers.
332654e14c4db764c0636160d26c5bbf491637c83a76John McCall  T = getCanonicalType(T);
3327745da3a5bb4ea35f93f50301e7fbbb7d78d3b6bbFariborz Jahanian  T = getVariableArrayDecayedType(T);
332854e14c4db764c0636160d26c5bbf491637c83a76John McCall  const Type *Ty = T.getTypePtr();
332954e14c4db764c0636160d26c5bbf491637c83a76John McCall  QualType Result;
333054e14c4db764c0636160d26c5bbf491637c83a76John McCall  if (isa<ArrayType>(Ty)) {
333154e14c4db764c0636160d26c5bbf491637c83a76John McCall    Result = getArrayDecayedType(QualType(Ty,0));
333254e14c4db764c0636160d26c5bbf491637c83a76John McCall  } else if (isa<FunctionType>(Ty)) {
333354e14c4db764c0636160d26c5bbf491637c83a76John McCall    Result = getPointerType(QualType(Ty, 0));
333454e14c4db764c0636160d26c5bbf491637c83a76John McCall  } else {
333554e14c4db764c0636160d26c5bbf491637c83a76John McCall    Result = QualType(Ty, 0);
333654e14c4db764c0636160d26c5bbf491637c83a76John McCall  }
333754e14c4db764c0636160d26c5bbf491637c83a76John McCall
333854e14c4db764c0636160d26c5bbf491637c83a76John McCall  return CanQualType::CreateUnsafe(Result);
333954e14c4db764c0636160d26c5bbf491637c83a76John McCall}
334054e14c4db764c0636160d26c5bbf491637c83a76John McCall
334162c28c831bbf207cc36e683e7c321fc33bf8928cJohn McCallQualType ASTContext::getUnqualifiedArrayType(QualType type,
334262c28c831bbf207cc36e683e7c321fc33bf8928cJohn McCall                                             Qualifiers &quals) {
334362c28c831bbf207cc36e683e7c321fc33bf8928cJohn McCall  SplitQualType splitType = type.getSplitUnqualifiedType();
334462c28c831bbf207cc36e683e7c321fc33bf8928cJohn McCall
334562c28c831bbf207cc36e683e7c321fc33bf8928cJohn McCall  // FIXME: getSplitUnqualifiedType() actually walks all the way to
334662c28c831bbf207cc36e683e7c321fc33bf8928cJohn McCall  // the unqualified desugared type and then drops it on the floor.
334762c28c831bbf207cc36e683e7c321fc33bf8928cJohn McCall  // We then have to strip that sugar back off with
334862c28c831bbf207cc36e683e7c321fc33bf8928cJohn McCall  // getUnqualifiedDesugaredType(), which is silly.
334962c28c831bbf207cc36e683e7c321fc33bf8928cJohn McCall  const ArrayType *AT =
3350200fa53fd420aa8369586f569dbece04930ad6a3John McCall    dyn_cast<ArrayType>(splitType.Ty->getUnqualifiedDesugaredType());
335162c28c831bbf207cc36e683e7c321fc33bf8928cJohn McCall
335262c28c831bbf207cc36e683e7c321fc33bf8928cJohn McCall  // If we don't have an array, just use the results in splitType.
33539dadd94e1c17fa030d1f88d8f2113ff59ccc6714Douglas Gregor  if (!AT) {
3354200fa53fd420aa8369586f569dbece04930ad6a3John McCall    quals = splitType.Quals;
3355200fa53fd420aa8369586f569dbece04930ad6a3John McCall    return QualType(splitType.Ty, 0);
335628e318cc6008c2bc008f0caee70dc736a03d6289Chandler Carruth  }
335728e318cc6008c2bc008f0caee70dc736a03d6289Chandler Carruth
335862c28c831bbf207cc36e683e7c321fc33bf8928cJohn McCall  // Otherwise, recurse on the array's element type.
335962c28c831bbf207cc36e683e7c321fc33bf8928cJohn McCall  QualType elementType = AT->getElementType();
336062c28c831bbf207cc36e683e7c321fc33bf8928cJohn McCall  QualType unqualElementType = getUnqualifiedArrayType(elementType, quals);
336162c28c831bbf207cc36e683e7c321fc33bf8928cJohn McCall
336262c28c831bbf207cc36e683e7c321fc33bf8928cJohn McCall  // If that didn't change the element type, AT has no qualifiers, so we
336362c28c831bbf207cc36e683e7c321fc33bf8928cJohn McCall  // can just use the results in splitType.
336462c28c831bbf207cc36e683e7c321fc33bf8928cJohn McCall  if (elementType == unqualElementType) {
336562c28c831bbf207cc36e683e7c321fc33bf8928cJohn McCall    assert(quals.empty()); // from the recursive call
3366200fa53fd420aa8369586f569dbece04930ad6a3John McCall    quals = splitType.Quals;
3367200fa53fd420aa8369586f569dbece04930ad6a3John McCall    return QualType(splitType.Ty, 0);
336862c28c831bbf207cc36e683e7c321fc33bf8928cJohn McCall  }
336962c28c831bbf207cc36e683e7c321fc33bf8928cJohn McCall
337062c28c831bbf207cc36e683e7c321fc33bf8928cJohn McCall  // Otherwise, add in the qualifiers from the outermost type, then
337162c28c831bbf207cc36e683e7c321fc33bf8928cJohn McCall  // build the type back up.
3372200fa53fd420aa8369586f569dbece04930ad6a3John McCall  quals.addConsistentQualifiers(splitType.Quals);
337328e318cc6008c2bc008f0caee70dc736a03d6289Chandler Carruth
33749dadd94e1c17fa030d1f88d8f2113ff59ccc6714Douglas Gregor  if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(AT)) {
337562c28c831bbf207cc36e683e7c321fc33bf8928cJohn McCall    return getConstantArrayType(unqualElementType, CAT->getSize(),
337628e318cc6008c2bc008f0caee70dc736a03d6289Chandler Carruth                                CAT->getSizeModifier(), 0);
337728e318cc6008c2bc008f0caee70dc736a03d6289Chandler Carruth  }
337828e318cc6008c2bc008f0caee70dc736a03d6289Chandler Carruth
33799dadd94e1c17fa030d1f88d8f2113ff59ccc6714Douglas Gregor  if (const IncompleteArrayType *IAT = dyn_cast<IncompleteArrayType>(AT)) {
338062c28c831bbf207cc36e683e7c321fc33bf8928cJohn McCall    return getIncompleteArrayType(unqualElementType, IAT->getSizeModifier(), 0);
338128e318cc6008c2bc008f0caee70dc736a03d6289Chandler Carruth  }
338228e318cc6008c2bc008f0caee70dc736a03d6289Chandler Carruth
33839dadd94e1c17fa030d1f88d8f2113ff59ccc6714Douglas Gregor  if (const VariableArrayType *VAT = dyn_cast<VariableArrayType>(AT)) {
338462c28c831bbf207cc36e683e7c321fc33bf8928cJohn McCall    return getVariableArrayType(unqualElementType,
33853fa5cae9b3812cab9fab6c042c3329bb70a3d046John McCall                                VAT->getSizeExpr(),
33869dadd94e1c17fa030d1f88d8f2113ff59ccc6714Douglas Gregor                                VAT->getSizeModifier(),
33879dadd94e1c17fa030d1f88d8f2113ff59ccc6714Douglas Gregor                                VAT->getIndexTypeCVRQualifiers(),
33889dadd94e1c17fa030d1f88d8f2113ff59ccc6714Douglas Gregor                                VAT->getBracketsRange());
33899dadd94e1c17fa030d1f88d8f2113ff59ccc6714Douglas Gregor  }
33909dadd94e1c17fa030d1f88d8f2113ff59ccc6714Douglas Gregor
33919dadd94e1c17fa030d1f88d8f2113ff59ccc6714Douglas Gregor  const DependentSizedArrayType *DSAT = cast<DependentSizedArrayType>(AT);
339262c28c831bbf207cc36e683e7c321fc33bf8928cJohn McCall  return getDependentSizedArrayType(unqualElementType, DSAT->getSizeExpr(),
339328e318cc6008c2bc008f0caee70dc736a03d6289Chandler Carruth                                    DSAT->getSizeModifier(), 0,
339428e318cc6008c2bc008f0caee70dc736a03d6289Chandler Carruth                                    SourceRange());
339528e318cc6008c2bc008f0caee70dc736a03d6289Chandler Carruth}
339628e318cc6008c2bc008f0caee70dc736a03d6289Chandler Carruth
33975a57efd7bf88a4a13018e0471ded8063a4abe8afDouglas Gregor/// UnwrapSimilarPointerTypes - If T1 and T2 are pointer types  that
33985a57efd7bf88a4a13018e0471ded8063a4abe8afDouglas Gregor/// may be similar (C++ 4.4), replaces T1 and T2 with the type that
33995a57efd7bf88a4a13018e0471ded8063a4abe8afDouglas Gregor/// they point to and return true. If T1 and T2 aren't pointer types
34005a57efd7bf88a4a13018e0471ded8063a4abe8afDouglas Gregor/// or pointer-to-member types, or if they are not similar at this
34015a57efd7bf88a4a13018e0471ded8063a4abe8afDouglas Gregor/// level, returns false and leaves T1 and T2 unchanged. Top-level
34025a57efd7bf88a4a13018e0471ded8063a4abe8afDouglas Gregor/// qualifiers on T1 and T2 are ignored. This function will typically
34035a57efd7bf88a4a13018e0471ded8063a4abe8afDouglas Gregor/// be called in a loop that successively "unwraps" pointer and
34045a57efd7bf88a4a13018e0471ded8063a4abe8afDouglas Gregor/// pointer-to-member types to compare them at each level.
34055a57efd7bf88a4a13018e0471ded8063a4abe8afDouglas Gregorbool ASTContext::UnwrapSimilarPointerTypes(QualType &T1, QualType &T2) {
34065a57efd7bf88a4a13018e0471ded8063a4abe8afDouglas Gregor  const PointerType *T1PtrType = T1->getAs<PointerType>(),
34075a57efd7bf88a4a13018e0471ded8063a4abe8afDouglas Gregor                    *T2PtrType = T2->getAs<PointerType>();
34085a57efd7bf88a4a13018e0471ded8063a4abe8afDouglas Gregor  if (T1PtrType && T2PtrType) {
34095a57efd7bf88a4a13018e0471ded8063a4abe8afDouglas Gregor    T1 = T1PtrType->getPointeeType();
34105a57efd7bf88a4a13018e0471ded8063a4abe8afDouglas Gregor    T2 = T2PtrType->getPointeeType();
34115a57efd7bf88a4a13018e0471ded8063a4abe8afDouglas Gregor    return true;
34125a57efd7bf88a4a13018e0471ded8063a4abe8afDouglas Gregor  }
34135a57efd7bf88a4a13018e0471ded8063a4abe8afDouglas Gregor
34145a57efd7bf88a4a13018e0471ded8063a4abe8afDouglas Gregor  const MemberPointerType *T1MPType = T1->getAs<MemberPointerType>(),
34155a57efd7bf88a4a13018e0471ded8063a4abe8afDouglas Gregor                          *T2MPType = T2->getAs<MemberPointerType>();
34165a57efd7bf88a4a13018e0471ded8063a4abe8afDouglas Gregor  if (T1MPType && T2MPType &&
34175a57efd7bf88a4a13018e0471ded8063a4abe8afDouglas Gregor      hasSameUnqualifiedType(QualType(T1MPType->getClass(), 0),
34185a57efd7bf88a4a13018e0471ded8063a4abe8afDouglas Gregor                             QualType(T2MPType->getClass(), 0))) {
34195a57efd7bf88a4a13018e0471ded8063a4abe8afDouglas Gregor    T1 = T1MPType->getPointeeType();
34205a57efd7bf88a4a13018e0471ded8063a4abe8afDouglas Gregor    T2 = T2MPType->getPointeeType();
34215a57efd7bf88a4a13018e0471ded8063a4abe8afDouglas Gregor    return true;
34225a57efd7bf88a4a13018e0471ded8063a4abe8afDouglas Gregor  }
34235a57efd7bf88a4a13018e0471ded8063a4abe8afDouglas Gregor
34244e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie  if (getLangOpts().ObjC1) {
34255a57efd7bf88a4a13018e0471ded8063a4abe8afDouglas Gregor    const ObjCObjectPointerType *T1OPType = T1->getAs<ObjCObjectPointerType>(),
34265a57efd7bf88a4a13018e0471ded8063a4abe8afDouglas Gregor                                *T2OPType = T2->getAs<ObjCObjectPointerType>();
34275a57efd7bf88a4a13018e0471ded8063a4abe8afDouglas Gregor    if (T1OPType && T2OPType) {
34285a57efd7bf88a4a13018e0471ded8063a4abe8afDouglas Gregor      T1 = T1OPType->getPointeeType();
34295a57efd7bf88a4a13018e0471ded8063a4abe8afDouglas Gregor      T2 = T2OPType->getPointeeType();
34305a57efd7bf88a4a13018e0471ded8063a4abe8afDouglas Gregor      return true;
34315a57efd7bf88a4a13018e0471ded8063a4abe8afDouglas Gregor    }
34325a57efd7bf88a4a13018e0471ded8063a4abe8afDouglas Gregor  }
34335a57efd7bf88a4a13018e0471ded8063a4abe8afDouglas Gregor
34345a57efd7bf88a4a13018e0471ded8063a4abe8afDouglas Gregor  // FIXME: Block pointers, too?
34355a57efd7bf88a4a13018e0471ded8063a4abe8afDouglas Gregor
34365a57efd7bf88a4a13018e0471ded8063a4abe8afDouglas Gregor  return false;
34375a57efd7bf88a4a13018e0471ded8063a4abe8afDouglas Gregor}
34385a57efd7bf88a4a13018e0471ded8063a4abe8afDouglas Gregor
34394ba2a17694148e16eaa8d3917f657ffcd3667be4Jay FoadDeclarationNameInfo
34404ba2a17694148e16eaa8d3917f657ffcd3667be4Jay FoadASTContext::getNameForTemplate(TemplateName Name,
34414ba2a17694148e16eaa8d3917f657ffcd3667be4Jay Foad                               SourceLocation NameLoc) const {
3442146060435c3efce95c95a092c7a1eb651cfb9ae0John McCall  switch (Name.getKind()) {
3443146060435c3efce95c95a092c7a1eb651cfb9ae0John McCall  case TemplateName::QualifiedTemplate:
3444146060435c3efce95c95a092c7a1eb651cfb9ae0John McCall  case TemplateName::Template:
34452577743c5650c646fb705df01403707e94f2df04Abramo Bagnara    // DNInfo work in progress: CHECKME: what about DNLoc?
3446146060435c3efce95c95a092c7a1eb651cfb9ae0John McCall    return DeclarationNameInfo(Name.getAsTemplateDecl()->getDeclName(),
3447146060435c3efce95c95a092c7a1eb651cfb9ae0John McCall                               NameLoc);
34482577743c5650c646fb705df01403707e94f2df04Abramo Bagnara
3449146060435c3efce95c95a092c7a1eb651cfb9ae0John McCall  case TemplateName::OverloadedTemplate: {
3450146060435c3efce95c95a092c7a1eb651cfb9ae0John McCall    OverloadedTemplateStorage *Storage = Name.getAsOverloadedTemplate();
3451146060435c3efce95c95a092c7a1eb651cfb9ae0John McCall    // DNInfo work in progress: CHECKME: what about DNLoc?
3452146060435c3efce95c95a092c7a1eb651cfb9ae0John McCall    return DeclarationNameInfo((*Storage->begin())->getDeclName(), NameLoc);
3453146060435c3efce95c95a092c7a1eb651cfb9ae0John McCall  }
3454146060435c3efce95c95a092c7a1eb651cfb9ae0John McCall
3455146060435c3efce95c95a092c7a1eb651cfb9ae0John McCall  case TemplateName::DependentTemplate: {
3456146060435c3efce95c95a092c7a1eb651cfb9ae0John McCall    DependentTemplateName *DTN = Name.getAsDependentTemplateName();
34572577743c5650c646fb705df01403707e94f2df04Abramo Bagnara    DeclarationName DName;
345880ad16f4b2b350ddbaae21a52975e63df5aafc2cJohn McCall    if (DTN->isIdentifier()) {
34592577743c5650c646fb705df01403707e94f2df04Abramo Bagnara      DName = DeclarationNames.getIdentifier(DTN->getIdentifier());
34602577743c5650c646fb705df01403707e94f2df04Abramo Bagnara      return DeclarationNameInfo(DName, NameLoc);
346180ad16f4b2b350ddbaae21a52975e63df5aafc2cJohn McCall    } else {
34622577743c5650c646fb705df01403707e94f2df04Abramo Bagnara      DName = DeclarationNames.getCXXOperatorName(DTN->getOperator());
34632577743c5650c646fb705df01403707e94f2df04Abramo Bagnara      // DNInfo work in progress: FIXME: source locations?
34642577743c5650c646fb705df01403707e94f2df04Abramo Bagnara      DeclarationNameLoc DNLoc;
34652577743c5650c646fb705df01403707e94f2df04Abramo Bagnara      DNLoc.CXXOperatorName.BeginOpNameLoc = SourceLocation().getRawEncoding();
34662577743c5650c646fb705df01403707e94f2df04Abramo Bagnara      DNLoc.CXXOperatorName.EndOpNameLoc = SourceLocation().getRawEncoding();
34672577743c5650c646fb705df01403707e94f2df04Abramo Bagnara      return DeclarationNameInfo(DName, NameLoc, DNLoc);
346880ad16f4b2b350ddbaae21a52975e63df5aafc2cJohn McCall    }
346980ad16f4b2b350ddbaae21a52975e63df5aafc2cJohn McCall  }
347080ad16f4b2b350ddbaae21a52975e63df5aafc2cJohn McCall
3471146060435c3efce95c95a092c7a1eb651cfb9ae0John McCall  case TemplateName::SubstTemplateTemplateParm: {
3472146060435c3efce95c95a092c7a1eb651cfb9ae0John McCall    SubstTemplateTemplateParmStorage *subst
3473146060435c3efce95c95a092c7a1eb651cfb9ae0John McCall      = Name.getAsSubstTemplateTemplateParm();
3474146060435c3efce95c95a092c7a1eb651cfb9ae0John McCall    return DeclarationNameInfo(subst->getParameter()->getDeclName(),
3475146060435c3efce95c95a092c7a1eb651cfb9ae0John McCall                               NameLoc);
3476146060435c3efce95c95a092c7a1eb651cfb9ae0John McCall  }
3477146060435c3efce95c95a092c7a1eb651cfb9ae0John McCall
3478146060435c3efce95c95a092c7a1eb651cfb9ae0John McCall  case TemplateName::SubstTemplateTemplateParmPack: {
3479146060435c3efce95c95a092c7a1eb651cfb9ae0John McCall    SubstTemplateTemplateParmPackStorage *subst
3480146060435c3efce95c95a092c7a1eb651cfb9ae0John McCall      = Name.getAsSubstTemplateTemplateParmPack();
3481146060435c3efce95c95a092c7a1eb651cfb9ae0John McCall    return DeclarationNameInfo(subst->getParameterPack()->getDeclName(),
3482146060435c3efce95c95a092c7a1eb651cfb9ae0John McCall                               NameLoc);
3483146060435c3efce95c95a092c7a1eb651cfb9ae0John McCall  }
3484146060435c3efce95c95a092c7a1eb651cfb9ae0John McCall  }
3485146060435c3efce95c95a092c7a1eb651cfb9ae0John McCall
3486146060435c3efce95c95a092c7a1eb651cfb9ae0John McCall  llvm_unreachable("bad template name kind!");
348780ad16f4b2b350ddbaae21a52975e63df5aafc2cJohn McCall}
348880ad16f4b2b350ddbaae21a52975e63df5aafc2cJohn McCall
34894ba2a17694148e16eaa8d3917f657ffcd3667be4Jay FoadTemplateName ASTContext::getCanonicalTemplateName(TemplateName Name) const {
3490146060435c3efce95c95a092c7a1eb651cfb9ae0John McCall  switch (Name.getKind()) {
3491146060435c3efce95c95a092c7a1eb651cfb9ae0John McCall  case TemplateName::QualifiedTemplate:
3492146060435c3efce95c95a092c7a1eb651cfb9ae0John McCall  case TemplateName::Template: {
3493146060435c3efce95c95a092c7a1eb651cfb9ae0John McCall    TemplateDecl *Template = Name.getAsTemplateDecl();
34943e1274f2b99cb99c03cc8e2c6517c37d330b597aDouglas Gregor    if (TemplateTemplateParmDecl *TTP
3495146060435c3efce95c95a092c7a1eb651cfb9ae0John McCall          = dyn_cast<TemplateTemplateParmDecl>(Template))
34963e1274f2b99cb99c03cc8e2c6517c37d330b597aDouglas Gregor      Template = getCanonicalTemplateTemplateParmDecl(TTP);
34973e1274f2b99cb99c03cc8e2c6517c37d330b597aDouglas Gregor
34983e1274f2b99cb99c03cc8e2c6517c37d330b597aDouglas Gregor    // The canonical template name is the canonical template declaration.
349997fbaa2a38804268a024f1a104b43fcf8b4411b0Argyrios Kyrtzidis    return TemplateName(cast<TemplateDecl>(Template->getCanonicalDecl()));
35003e1274f2b99cb99c03cc8e2c6517c37d330b597aDouglas Gregor  }
350125a3ef7cc5fd55dc8cc67c6e6770c8595657e082Douglas Gregor
3502146060435c3efce95c95a092c7a1eb651cfb9ae0John McCall  case TemplateName::OverloadedTemplate:
3503146060435c3efce95c95a092c7a1eb651cfb9ae0John McCall    llvm_unreachable("cannot canonicalize overloaded template");
3504146060435c3efce95c95a092c7a1eb651cfb9ae0John McCall
3505146060435c3efce95c95a092c7a1eb651cfb9ae0John McCall  case TemplateName::DependentTemplate: {
3506146060435c3efce95c95a092c7a1eb651cfb9ae0John McCall    DependentTemplateName *DTN = Name.getAsDependentTemplateName();
3507146060435c3efce95c95a092c7a1eb651cfb9ae0John McCall    assert(DTN && "Non-dependent template names must refer to template decls.");
3508146060435c3efce95c95a092c7a1eb651cfb9ae0John McCall    return DTN->CanonicalTemplateName;
3509146060435c3efce95c95a092c7a1eb651cfb9ae0John McCall  }
3510146060435c3efce95c95a092c7a1eb651cfb9ae0John McCall
3511146060435c3efce95c95a092c7a1eb651cfb9ae0John McCall  case TemplateName::SubstTemplateTemplateParm: {
3512146060435c3efce95c95a092c7a1eb651cfb9ae0John McCall    SubstTemplateTemplateParmStorage *subst
3513146060435c3efce95c95a092c7a1eb651cfb9ae0John McCall      = Name.getAsSubstTemplateTemplateParm();
3514146060435c3efce95c95a092c7a1eb651cfb9ae0John McCall    return getCanonicalTemplateName(subst->getReplacement());
35151aee05d08b2184acadeb36de300e216390780d6cDouglas Gregor  }
35161eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
3517146060435c3efce95c95a092c7a1eb651cfb9ae0John McCall  case TemplateName::SubstTemplateTemplateParmPack: {
3518146060435c3efce95c95a092c7a1eb651cfb9ae0John McCall    SubstTemplateTemplateParmPackStorage *subst
3519146060435c3efce95c95a092c7a1eb651cfb9ae0John McCall                                  = Name.getAsSubstTemplateTemplateParmPack();
3520146060435c3efce95c95a092c7a1eb651cfb9ae0John McCall    TemplateTemplateParmDecl *canonParameter
3521146060435c3efce95c95a092c7a1eb651cfb9ae0John McCall      = getCanonicalTemplateTemplateParmDecl(subst->getParameterPack());
3522146060435c3efce95c95a092c7a1eb651cfb9ae0John McCall    TemplateArgument canonArgPack
3523146060435c3efce95c95a092c7a1eb651cfb9ae0John McCall      = getCanonicalTemplateArgument(subst->getArgumentPack());
3524146060435c3efce95c95a092c7a1eb651cfb9ae0John McCall    return getSubstTemplateTemplateParmPack(canonParameter, canonArgPack);
3525146060435c3efce95c95a092c7a1eb651cfb9ae0John McCall  }
3526146060435c3efce95c95a092c7a1eb651cfb9ae0John McCall  }
3527146060435c3efce95c95a092c7a1eb651cfb9ae0John McCall
3528146060435c3efce95c95a092c7a1eb651cfb9ae0John McCall  llvm_unreachable("bad template name!");
352925a3ef7cc5fd55dc8cc67c6e6770c8595657e082Douglas Gregor}
353025a3ef7cc5fd55dc8cc67c6e6770c8595657e082Douglas Gregor
3531db0d4b751e83b8841b8f48f913f17e50467f13d4Douglas Gregorbool ASTContext::hasSameTemplateName(TemplateName X, TemplateName Y) {
3532db0d4b751e83b8841b8f48f913f17e50467f13d4Douglas Gregor  X = getCanonicalTemplateName(X);
3533db0d4b751e83b8841b8f48f913f17e50467f13d4Douglas Gregor  Y = getCanonicalTemplateName(Y);
3534db0d4b751e83b8841b8f48f913f17e50467f13d4Douglas Gregor  return X.getAsVoidPointer() == Y.getAsVoidPointer();
3535db0d4b751e83b8841b8f48f913f17e50467f13d4Douglas Gregor}
3536db0d4b751e83b8841b8f48f913f17e50467f13d4Douglas Gregor
35371eb4433ac451dc16f4133a88af2d002ac26c58efMike StumpTemplateArgument
35384ba2a17694148e16eaa8d3917f657ffcd3667be4Jay FoadASTContext::getCanonicalTemplateArgument(const TemplateArgument &Arg) const {
35391275ae098acda31fe0e434510c729fcfed0458a1Douglas Gregor  switch (Arg.getKind()) {
35401275ae098acda31fe0e434510c729fcfed0458a1Douglas Gregor    case TemplateArgument::Null:
35411275ae098acda31fe0e434510c729fcfed0458a1Douglas Gregor      return Arg;
35421eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
35431275ae098acda31fe0e434510c729fcfed0458a1Douglas Gregor    case TemplateArgument::Expression:
35441275ae098acda31fe0e434510c729fcfed0458a1Douglas Gregor      return Arg;
35451eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
3546d2008e2c80d6c9282044ec873a937a17a0f33579Douglas Gregor    case TemplateArgument::Declaration: {
3547d2008e2c80d6c9282044ec873a937a17a0f33579Douglas Gregor      if (Decl *D = Arg.getAsDecl())
3548d2008e2c80d6c9282044ec873a937a17a0f33579Douglas Gregor          return TemplateArgument(D->getCanonicalDecl());
3549d2008e2c80d6c9282044ec873a937a17a0f33579Douglas Gregor      return TemplateArgument((Decl*)0);
3550d2008e2c80d6c9282044ec873a937a17a0f33579Douglas Gregor    }
35511eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
3552788cd06cf8e868a67158aafec5de3a1f408d14f3Douglas Gregor    case TemplateArgument::Template:
3553788cd06cf8e868a67158aafec5de3a1f408d14f3Douglas Gregor      return TemplateArgument(getCanonicalTemplateName(Arg.getAsTemplate()));
3554a7fc901a2e39bfe55bfcff5934b2d9fdf9656491Douglas Gregor
3555a7fc901a2e39bfe55bfcff5934b2d9fdf9656491Douglas Gregor    case TemplateArgument::TemplateExpansion:
3556a7fc901a2e39bfe55bfcff5934b2d9fdf9656491Douglas Gregor      return TemplateArgument(getCanonicalTemplateName(
3557a7fc901a2e39bfe55bfcff5934b2d9fdf9656491Douglas Gregor                                         Arg.getAsTemplateOrTemplatePattern()),
35582be29f423acad3bbe39099a78db2805acb5bdf17Douglas Gregor                              Arg.getNumTemplateExpansions());
3559a7fc901a2e39bfe55bfcff5934b2d9fdf9656491Douglas Gregor
35601275ae098acda31fe0e434510c729fcfed0458a1Douglas Gregor    case TemplateArgument::Integral:
3561855243789cb44799c03f4c7216d3d6308805f549Benjamin Kramer      return TemplateArgument(Arg, getCanonicalType(Arg.getIntegralType()));
35621eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
35631275ae098acda31fe0e434510c729fcfed0458a1Douglas Gregor    case TemplateArgument::Type:
3564833ca991c1bfc967f0995974ca86f66ba1f666b5John McCall      return TemplateArgument(getCanonicalType(Arg.getAsType()));
35651eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
35661275ae098acda31fe0e434510c729fcfed0458a1Douglas Gregor    case TemplateArgument::Pack: {
356787dd697dcc8ecb64df73ae64d61b8c80ff0c157cDouglas Gregor      if (Arg.pack_size() == 0)
356887dd697dcc8ecb64df73ae64d61b8c80ff0c157cDouglas Gregor        return Arg;
356987dd697dcc8ecb64df73ae64d61b8c80ff0c157cDouglas Gregor
3570910f8008fea79120489a53593fe971b0b8a4a740Douglas Gregor      TemplateArgument *CanonArgs
3571910f8008fea79120489a53593fe971b0b8a4a740Douglas Gregor        = new (*this) TemplateArgument[Arg.pack_size()];
35721275ae098acda31fe0e434510c729fcfed0458a1Douglas Gregor      unsigned Idx = 0;
35731eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      for (TemplateArgument::pack_iterator A = Arg.pack_begin(),
35741275ae098acda31fe0e434510c729fcfed0458a1Douglas Gregor                                        AEnd = Arg.pack_end();
35751275ae098acda31fe0e434510c729fcfed0458a1Douglas Gregor           A != AEnd; (void)++A, ++Idx)
35761275ae098acda31fe0e434510c729fcfed0458a1Douglas Gregor        CanonArgs[Idx] = getCanonicalTemplateArgument(*A);
35771eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
3578910f8008fea79120489a53593fe971b0b8a4a740Douglas Gregor      return TemplateArgument(CanonArgs, Arg.pack_size());
35791275ae098acda31fe0e434510c729fcfed0458a1Douglas Gregor    }
35801275ae098acda31fe0e434510c729fcfed0458a1Douglas Gregor  }
35811275ae098acda31fe0e434510c729fcfed0458a1Douglas Gregor
35821275ae098acda31fe0e434510c729fcfed0458a1Douglas Gregor  // Silence GCC warning
3583b219cfc4d75f0a03630b7c4509ef791b7e97b2c8David Blaikie  llvm_unreachable("Unhandled template argument kind");
35841275ae098acda31fe0e434510c729fcfed0458a1Douglas Gregor}
35851275ae098acda31fe0e434510c729fcfed0458a1Douglas Gregor
3586d57959af02b4af695276f4204443afe6e5d86bd8Douglas GregorNestedNameSpecifier *
35874ba2a17694148e16eaa8d3917f657ffcd3667be4Jay FoadASTContext::getCanonicalNestedNameSpecifier(NestedNameSpecifier *NNS) const {
35881eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  if (!NNS)
3589d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor    return 0;
3590d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor
3591d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor  switch (NNS->getKind()) {
3592d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor  case NestedNameSpecifier::Identifier:
3593d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor    // Canonicalize the prefix but keep the identifier the same.
35941eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    return NestedNameSpecifier::Create(*this,
3595d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor                         getCanonicalNestedNameSpecifier(NNS->getPrefix()),
3596d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor                                       NNS->getAsIdentifier());
3597d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor
3598d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor  case NestedNameSpecifier::Namespace:
3599d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor    // A namespace is canonical; build a nested-name-specifier with
3600d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor    // this namespace and no prefix.
360114aba76042e041b2c5e439bf4ae353a0a3c7fd73Douglas Gregor    return NestedNameSpecifier::Create(*this, 0,
360214aba76042e041b2c5e439bf4ae353a0a3c7fd73Douglas Gregor                                 NNS->getAsNamespace()->getOriginalNamespace());
360314aba76042e041b2c5e439bf4ae353a0a3c7fd73Douglas Gregor
360414aba76042e041b2c5e439bf4ae353a0a3c7fd73Douglas Gregor  case NestedNameSpecifier::NamespaceAlias:
360514aba76042e041b2c5e439bf4ae353a0a3c7fd73Douglas Gregor    // A namespace is canonical; build a nested-name-specifier with
360614aba76042e041b2c5e439bf4ae353a0a3c7fd73Douglas Gregor    // this namespace and no prefix.
360714aba76042e041b2c5e439bf4ae353a0a3c7fd73Douglas Gregor    return NestedNameSpecifier::Create(*this, 0,
360814aba76042e041b2c5e439bf4ae353a0a3c7fd73Douglas Gregor                                    NNS->getAsNamespaceAlias()->getNamespace()
360914aba76042e041b2c5e439bf4ae353a0a3c7fd73Douglas Gregor                                                      ->getOriginalNamespace());
3610d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor
3611d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor  case NestedNameSpecifier::TypeSpec:
3612d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor  case NestedNameSpecifier::TypeSpecWithTemplate: {
3613d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor    QualType T = getCanonicalType(QualType(NNS->getAsType(), 0));
3614264bf66d55563dd86a3d7e06738aa427de512d2cDouglas Gregor
3615264bf66d55563dd86a3d7e06738aa427de512d2cDouglas Gregor    // If we have some kind of dependent-named type (e.g., "typename T::type"),
3616264bf66d55563dd86a3d7e06738aa427de512d2cDouglas Gregor    // break it apart into its prefix and identifier, then reconsititute those
3617264bf66d55563dd86a3d7e06738aa427de512d2cDouglas Gregor    // as the canonical nested-name-specifier. This is required to canonicalize
3618264bf66d55563dd86a3d7e06738aa427de512d2cDouglas Gregor    // a dependent nested-name-specifier involving typedefs of dependent-name
3619264bf66d55563dd86a3d7e06738aa427de512d2cDouglas Gregor    // types, e.g.,
3620264bf66d55563dd86a3d7e06738aa427de512d2cDouglas Gregor    //   typedef typename T::type T1;
3621264bf66d55563dd86a3d7e06738aa427de512d2cDouglas Gregor    //   typedef typename T1::type T2;
362216412ef25a2203b7066d0db2b41f944631e5cf79Eli Friedman    if (const DependentNameType *DNT = T->getAs<DependentNameType>())
362316412ef25a2203b7066d0db2b41f944631e5cf79Eli Friedman      return NestedNameSpecifier::Create(*this, DNT->getQualifier(),
3624264bf66d55563dd86a3d7e06738aa427de512d2cDouglas Gregor                           const_cast<IdentifierInfo *>(DNT->getIdentifier()));
3625264bf66d55563dd86a3d7e06738aa427de512d2cDouglas Gregor
362616412ef25a2203b7066d0db2b41f944631e5cf79Eli Friedman    // Otherwise, just canonicalize the type, and force it to be a TypeSpec.
362716412ef25a2203b7066d0db2b41f944631e5cf79Eli Friedman    // FIXME: Why are TypeSpec and TypeSpecWithTemplate distinct in the
362816412ef25a2203b7066d0db2b41f944631e5cf79Eli Friedman    // first place?
36293b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall    return NestedNameSpecifier::Create(*this, 0, false,
36303b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall                                       const_cast<Type*>(T.getTypePtr()));
3631d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor  }
3632d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor
3633d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor  case NestedNameSpecifier::Global:
3634d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor    // The global specifier is canonical and unique.
3635d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor    return NNS;
3636d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor  }
3637d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor
36387530c034c0c71a64c5a9173206d9742ae847af8bDavid Blaikie  llvm_unreachable("Invalid NestedNameSpecifier::Kind!");
3639d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor}
3640d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor
3641c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner
36424ba2a17694148e16eaa8d3917f657ffcd3667be4Jay Foadconst ArrayType *ASTContext::getAsArrayType(QualType T) const {
3643c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner  // Handle the non-qualified case efficiently.
3644a4923eb7c4b04d360cb2747641a5e92818edf804Douglas Gregor  if (!T.hasLocalQualifiers()) {
3645c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner    // Handle the common positive case fast.
3646c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner    if (const ArrayType *AT = dyn_cast<ArrayType>(T))
3647c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner      return AT;
3648c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner  }
36491eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
36500953e767ff7817f97b3ab20896b229891eeff45bJohn McCall  // Handle the common negative case fast.
36513b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall  if (!isa<ArrayType>(T.getCanonicalType()))
3652c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner    return 0;
36531eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
36540953e767ff7817f97b3ab20896b229891eeff45bJohn McCall  // Apply any qualifiers from the array type to the element type.  This
3655c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner  // implements C99 6.7.3p8: "If the specification of an array type includes
3656c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner  // any type qualifiers, the element type is so qualified, not the array type."
36571eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
3658c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner  // If we get here, we either have type qualifiers on the type, or we have
3659c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner  // sugar such as a typedef in the way.  If we have type qualifiers on the type
366050d62d1b4a98adbc83de8f8cd1379ea1c25656f7Douglas Gregor  // we must propagate them down into the element type.
36610953e767ff7817f97b3ab20896b229891eeff45bJohn McCall
36623b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall  SplitQualType split = T.getSplitDesugaredType();
3663200fa53fd420aa8369586f569dbece04930ad6a3John McCall  Qualifiers qs = split.Quals;
36641eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
3665c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner  // If we have a simple case, just return now.
3666200fa53fd420aa8369586f569dbece04930ad6a3John McCall  const ArrayType *ATy = dyn_cast<ArrayType>(split.Ty);
36673b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall  if (ATy == 0 || qs.empty())
3668c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner    return ATy;
36691eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
3670c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner  // Otherwise, we have an array and we have qualifiers on it.  Push the
3671c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner  // qualifiers into the array element type and return a new array type.
36723b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall  QualType NewEltTy = getQualifiedType(ATy->getElementType(), qs);
36731eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
3674c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner  if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(ATy))
3675c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner    return cast<ArrayType>(getConstantArrayType(NewEltTy, CAT->getSize(),
3676c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner                                                CAT->getSizeModifier(),
36770953e767ff7817f97b3ab20896b229891eeff45bJohn McCall                                           CAT->getIndexTypeCVRQualifiers()));
3678c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner  if (const IncompleteArrayType *IAT = dyn_cast<IncompleteArrayType>(ATy))
3679c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner    return cast<ArrayType>(getIncompleteArrayType(NewEltTy,
3680c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner                                                  IAT->getSizeModifier(),
36810953e767ff7817f97b3ab20896b229891eeff45bJohn McCall                                           IAT->getIndexTypeCVRQualifiers()));
3682898574e7496ba8fd76290079d3a9d06954992734Douglas Gregor
36831eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  if (const DependentSizedArrayType *DSAT
3684898574e7496ba8fd76290079d3a9d06954992734Douglas Gregor        = dyn_cast<DependentSizedArrayType>(ATy))
3685898574e7496ba8fd76290079d3a9d06954992734Douglas Gregor    return cast<ArrayType>(
36861eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump                     getDependentSizedArrayType(NewEltTy,
36873fa5cae9b3812cab9fab6c042c3329bb70a3d046John McCall                                                DSAT->getSizeExpr(),
3688898574e7496ba8fd76290079d3a9d06954992734Douglas Gregor                                                DSAT->getSizeModifier(),
36890953e767ff7817f97b3ab20896b229891eeff45bJohn McCall                                              DSAT->getIndexTypeCVRQualifiers(),
36907e7eb3da052a6d80ddf2377cab0384c798f73f75Douglas Gregor                                                DSAT->getBracketsRange()));
36911eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
3692c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner  const VariableArrayType *VAT = cast<VariableArrayType>(ATy);
36937e7eb3da052a6d80ddf2377cab0384c798f73f75Douglas Gregor  return cast<ArrayType>(getVariableArrayType(NewEltTy,
36943fa5cae9b3812cab9fab6c042c3329bb70a3d046John McCall                                              VAT->getSizeExpr(),
3695c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner                                              VAT->getSizeModifier(),
36960953e767ff7817f97b3ab20896b229891eeff45bJohn McCall                                              VAT->getIndexTypeCVRQualifiers(),
36977e7eb3da052a6d80ddf2377cab0384c798f73f75Douglas Gregor                                              VAT->getBracketsRange()));
369877c9647cae939104c6cb2b6a4dd8ca859d2e5770Chris Lattner}
369977c9647cae939104c6cb2b6a4dd8ca859d2e5770Chris Lattner
3700ad9689f3531c49e4bff467d9469993606800068cAbramo BagnaraQualType ASTContext::getAdjustedParameterType(QualType T) const {
370179e6bd379773447a74cc3e579d9081e4c5cb6d63Douglas Gregor  // C99 6.7.5.3p7:
370279e6bd379773447a74cc3e579d9081e4c5cb6d63Douglas Gregor  //   A declaration of a parameter as "array of type" shall be
370379e6bd379773447a74cc3e579d9081e4c5cb6d63Douglas Gregor  //   adjusted to "qualified pointer to type", where the type
370479e6bd379773447a74cc3e579d9081e4c5cb6d63Douglas Gregor  //   qualifiers (if any) are those specified within the [ and ] of
370579e6bd379773447a74cc3e579d9081e4c5cb6d63Douglas Gregor  //   the array type derivation.
370679e6bd379773447a74cc3e579d9081e4c5cb6d63Douglas Gregor  if (T->isArrayType())
370779e6bd379773447a74cc3e579d9081e4c5cb6d63Douglas Gregor    return getArrayDecayedType(T);
370879e6bd379773447a74cc3e579d9081e4c5cb6d63Douglas Gregor
370979e6bd379773447a74cc3e579d9081e4c5cb6d63Douglas Gregor  // C99 6.7.5.3p8:
371079e6bd379773447a74cc3e579d9081e4c5cb6d63Douglas Gregor  //   A declaration of a parameter as "function returning type"
371179e6bd379773447a74cc3e579d9081e4c5cb6d63Douglas Gregor  //   shall be adjusted to "pointer to function returning type", as
371279e6bd379773447a74cc3e579d9081e4c5cb6d63Douglas Gregor  //   in 6.3.2.1.
371379e6bd379773447a74cc3e579d9081e4c5cb6d63Douglas Gregor  if (T->isFunctionType())
371479e6bd379773447a74cc3e579d9081e4c5cb6d63Douglas Gregor    return getPointerType(T);
371579e6bd379773447a74cc3e579d9081e4c5cb6d63Douglas Gregor
371679e6bd379773447a74cc3e579d9081e4c5cb6d63Douglas Gregor  return T;
371779e6bd379773447a74cc3e579d9081e4c5cb6d63Douglas Gregor}
371879e6bd379773447a74cc3e579d9081e4c5cb6d63Douglas Gregor
3719ad9689f3531c49e4bff467d9469993606800068cAbramo BagnaraQualType ASTContext::getSignatureParameterType(QualType T) const {
372079e6bd379773447a74cc3e579d9081e4c5cb6d63Douglas Gregor  T = getVariableArrayDecayedType(T);
372179e6bd379773447a74cc3e579d9081e4c5cb6d63Douglas Gregor  T = getAdjustedParameterType(T);
372279e6bd379773447a74cc3e579d9081e4c5cb6d63Douglas Gregor  return T.getUnqualifiedType();
372379e6bd379773447a74cc3e579d9081e4c5cb6d63Douglas Gregor}
372479e6bd379773447a74cc3e579d9081e4c5cb6d63Douglas Gregor
3725e6327747b72bb687c948270f702ff53c30f411a6Chris Lattner/// getArrayDecayedType - Return the properly qualified result of decaying the
3726e6327747b72bb687c948270f702ff53c30f411a6Chris Lattner/// specified array type to a pointer.  This operation is non-trivial when
3727e6327747b72bb687c948270f702ff53c30f411a6Chris Lattner/// handling typedefs etc.  The canonical type of "T" must be an array type,
3728e6327747b72bb687c948270f702ff53c30f411a6Chris Lattner/// this returns a pointer to a properly qualified element of the array.
3729e6327747b72bb687c948270f702ff53c30f411a6Chris Lattner///
3730e6327747b72bb687c948270f702ff53c30f411a6Chris Lattner/// See C99 6.7.5.3p7 and C99 6.3.2.1p3.
37314ba2a17694148e16eaa8d3917f657ffcd3667be4Jay FoadQualType ASTContext::getArrayDecayedType(QualType Ty) const {
3732c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner  // Get the element type with 'getAsArrayType' so that we don't lose any
3733c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner  // typedefs in the element type of the array.  This also handles propagation
3734c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner  // of type qualifiers from the array type into the element type if present
3735c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner  // (C99 6.7.3p8).
3736c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner  const ArrayType *PrettyArrayType = getAsArrayType(Ty);
3737c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner  assert(PrettyArrayType && "Not an array type!");
37381eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
3739c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner  QualType PtrTy = getPointerType(PrettyArrayType->getElementType());
3740e6327747b72bb687c948270f702ff53c30f411a6Chris Lattner
3741e6327747b72bb687c948270f702ff53c30f411a6Chris Lattner  // int x[restrict 4] ->  int *restrict
37420953e767ff7817f97b3ab20896b229891eeff45bJohn McCall  return getQualifiedType(PtrTy, PrettyArrayType->getIndexTypeQualifiers());
3743e6327747b72bb687c948270f702ff53c30f411a6Chris Lattner}
3744e6327747b72bb687c948270f702ff53c30f411a6Chris Lattner
37453b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCallQualType ASTContext::getBaseElementType(const ArrayType *array) const {
37463b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall  return getBaseElementType(array->getElementType());
37475e03f9ea8174ae588c5e69ec6b5ef4c68f8fd766Douglas Gregor}
37485e03f9ea8174ae588c5e69ec6b5ef4c68f8fd766Douglas Gregor
37493b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCallQualType ASTContext::getBaseElementType(QualType type) const {
37503b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall  Qualifiers qs;
37513b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall  while (true) {
37523b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall    SplitQualType split = type.getSplitDesugaredType();
3753200fa53fd420aa8369586f569dbece04930ad6a3John McCall    const ArrayType *array = split.Ty->getAsArrayTypeUnsafe();
37543b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall    if (!array) break;
37551eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
37563b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall    type = array->getElementType();
3757200fa53fd420aa8369586f569dbece04930ad6a3John McCall    qs.addConsistentQualifiers(split.Quals);
37583b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall  }
37591eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
37603b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall  return getQualifiedType(type, qs);
37616183a99b064b397d98297904fbd6cf00fe1f453dAnders Carlsson}
37626183a99b064b397d98297904fbd6cf00fe1f453dAnders Carlsson
37630de78998e7bda473b408437053e48661b510d453Fariborz Jahanian/// getConstantArrayElementCount - Returns number of constant array elements.
37641eb4433ac451dc16f4133a88af2d002ac26c58efMike Stumpuint64_t
37650de78998e7bda473b408437053e48661b510d453Fariborz JahanianASTContext::getConstantArrayElementCount(const ConstantArrayType *CA)  const {
37660de78998e7bda473b408437053e48661b510d453Fariborz Jahanian  uint64_t ElementCount = 1;
37670de78998e7bda473b408437053e48661b510d453Fariborz Jahanian  do {
37680de78998e7bda473b408437053e48661b510d453Fariborz Jahanian    ElementCount *= CA->getSize().getZExtValue();
37690de78998e7bda473b408437053e48661b510d453Fariborz Jahanian    CA = dyn_cast<ConstantArrayType>(CA->getElementType());
37700de78998e7bda473b408437053e48661b510d453Fariborz Jahanian  } while (CA);
37710de78998e7bda473b408437053e48661b510d453Fariborz Jahanian  return ElementCount;
37720de78998e7bda473b408437053e48661b510d453Fariborz Jahanian}
37730de78998e7bda473b408437053e48661b510d453Fariborz Jahanian
37745f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// getFloatingRank - Return a relative rank for floating point types.
37755f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// This routine will assert if passed a built-in type that isn't a float.
3776a75cea3f6be0daa8054d36af81a6ffda1713f82dChris Lattnerstatic FloatingRank getFloatingRank(QualType T) {
3777183700f494ec9b6701b6efe82bcb25f4c79ba561John McCall  if (const ComplexType *CT = T->getAs<ComplexType>())
37785f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    return getFloatingRank(CT->getElementType());
3779a75cea3f6be0daa8054d36af81a6ffda1713f82dChris Lattner
3780183700f494ec9b6701b6efe82bcb25f4c79ba561John McCall  assert(T->getAs<BuiltinType>() && "getFloatingRank(): not a floating type");
3781183700f494ec9b6701b6efe82bcb25f4c79ba561John McCall  switch (T->getAs<BuiltinType>()->getKind()) {
3782b219cfc4d75f0a03630b7c4509ef791b7e97b2c8David Blaikie  default: llvm_unreachable("getFloatingRank(): not a floating type");
3783aa4a99b4a62615db243f7a5c433169f2fc704420Anton Korobeynikov  case BuiltinType::Half:       return HalfRank;
37845f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case BuiltinType::Float:      return FloatRank;
37855f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case BuiltinType::Double:     return DoubleRank;
37865f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case BuiltinType::LongDouble: return LongDoubleRank;
37875f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
37885f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
37895f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
37901eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// getFloatingTypeOfSizeWithinDomain - Returns a real floating
37911eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// point or a complex type (based on typeDomain/typeSize).
3792716c7304ff5d27a95e1e7823acd1d09d5ec3e37fSteve Naroff/// 'typeDomain' is a real floating point or complex type.
3793716c7304ff5d27a95e1e7823acd1d09d5ec3e37fSteve Naroff/// 'typeSize' is a real floating point or complex type.
37941361b11066239ea15764a2a844405352d87296b3Chris LattnerQualType ASTContext::getFloatingTypeOfSizeWithinDomain(QualType Size,
37951361b11066239ea15764a2a844405352d87296b3Chris Lattner                                                       QualType Domain) const {
37961361b11066239ea15764a2a844405352d87296b3Chris Lattner  FloatingRank EltRank = getFloatingRank(Size);
37971361b11066239ea15764a2a844405352d87296b3Chris Lattner  if (Domain->isComplexType()) {
37981361b11066239ea15764a2a844405352d87296b3Chris Lattner    switch (EltRank) {
3799561d3abc881033776ece385a01a510e1cbc1fa92David Blaikie    case HalfRank: llvm_unreachable("Complex half is not supported");
3800f1448a0e4a1e868ff873a8530a61a09cb68666ccSteve Naroff    case FloatRank:      return FloatComplexTy;
3801f1448a0e4a1e868ff873a8530a61a09cb68666ccSteve Naroff    case DoubleRank:     return DoubleComplexTy;
3802f1448a0e4a1e868ff873a8530a61a09cb68666ccSteve Naroff    case LongDoubleRank: return LongDoubleComplexTy;
3803f1448a0e4a1e868ff873a8530a61a09cb68666ccSteve Naroff    }
3804f1448a0e4a1e868ff873a8530a61a09cb68666ccSteve Naroff  }
38051361b11066239ea15764a2a844405352d87296b3Chris Lattner
38061361b11066239ea15764a2a844405352d87296b3Chris Lattner  assert(Domain->isRealFloatingType() && "Unknown domain!");
38071361b11066239ea15764a2a844405352d87296b3Chris Lattner  switch (EltRank) {
3808561d3abc881033776ece385a01a510e1cbc1fa92David Blaikie  case HalfRank: llvm_unreachable("Half ranks are not valid here");
38091361b11066239ea15764a2a844405352d87296b3Chris Lattner  case FloatRank:      return FloatTy;
38101361b11066239ea15764a2a844405352d87296b3Chris Lattner  case DoubleRank:     return DoubleTy;
38111361b11066239ea15764a2a844405352d87296b3Chris Lattner  case LongDoubleRank: return LongDoubleTy;
38125f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
3813561d3abc881033776ece385a01a510e1cbc1fa92David Blaikie  llvm_unreachable("getFloatingRank(): illegal value for rank");
38145f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
38155f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
38167cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner/// getFloatingTypeOrder - Compare the rank of the two specified floating
38177cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner/// point types, ignoring the domain of the type (i.e. 'double' ==
38187cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner/// '_Complex double').  If LHS > RHS, return 1.  If LHS == RHS, return 0. If
38191eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// LHS < RHS, return -1.
38204ba2a17694148e16eaa8d3917f657ffcd3667be4Jay Foadint ASTContext::getFloatingTypeOrder(QualType LHS, QualType RHS) const {
3821a75cea3f6be0daa8054d36af81a6ffda1713f82dChris Lattner  FloatingRank LHSR = getFloatingRank(LHS);
3822a75cea3f6be0daa8054d36af81a6ffda1713f82dChris Lattner  FloatingRank RHSR = getFloatingRank(RHS);
38231eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
3824a75cea3f6be0daa8054d36af81a6ffda1713f82dChris Lattner  if (LHSR == RHSR)
3825fb0d49669aa370b4c0993c5cee60275ef9fd6518Steve Naroff    return 0;
3826a75cea3f6be0daa8054d36af81a6ffda1713f82dChris Lattner  if (LHSR > RHSR)
3827fb0d49669aa370b4c0993c5cee60275ef9fd6518Steve Naroff    return 1;
3828fb0d49669aa370b4c0993c5cee60275ef9fd6518Steve Naroff  return -1;
38295f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
38305f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
3831f52ab250ff92bc51a9ac9a8e19bd43b63a5f844fChris Lattner/// getIntegerRank - Return an integer conversion rank (C99 6.3.1.1p1). This
3832f52ab250ff92bc51a9ac9a8e19bd43b63a5f844fChris Lattner/// routine will assert if passed a built-in type that isn't an integer or enum,
3833f52ab250ff92bc51a9ac9a8e19bd43b63a5f844fChris Lattner/// or if it is not canonicalized.
3834f4c7371fb1d3cebcfb40abad4537bb82515704eaJohn McCallunsigned ASTContext::getIntegerRank(const Type *T) const {
3835467b27b9a24bdc823218ad1ad0e37673b6cc1e83John McCall  assert(T->isCanonicalUnqualified() && "T should be canonicalized");
3836f5c209d23b20ada4a9b6235db50317239cbf6ae1Alisdair Meredith
3837f52ab250ff92bc51a9ac9a8e19bd43b63a5f844fChris Lattner  switch (cast<BuiltinType>(T)->getKind()) {
3838b219cfc4d75f0a03630b7c4509ef791b7e97b2c8David Blaikie  default: llvm_unreachable("getIntegerRank(): not a built-in integer");
38397cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner  case BuiltinType::Bool:
3840f98aba35e6c3da5aae61843fc01334939e4e12ecEli Friedman    return 1 + (getIntWidth(BoolTy) << 3);
38417cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner  case BuiltinType::Char_S:
38427cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner  case BuiltinType::Char_U:
38437cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner  case BuiltinType::SChar:
38447cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner  case BuiltinType::UChar:
3845f98aba35e6c3da5aae61843fc01334939e4e12ecEli Friedman    return 2 + (getIntWidth(CharTy) << 3);
38467cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner  case BuiltinType::Short:
38477cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner  case BuiltinType::UShort:
3848f98aba35e6c3da5aae61843fc01334939e4e12ecEli Friedman    return 3 + (getIntWidth(ShortTy) << 3);
38497cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner  case BuiltinType::Int:
38507cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner  case BuiltinType::UInt:
3851f98aba35e6c3da5aae61843fc01334939e4e12ecEli Friedman    return 4 + (getIntWidth(IntTy) << 3);
38527cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner  case BuiltinType::Long:
38537cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner  case BuiltinType::ULong:
3854f98aba35e6c3da5aae61843fc01334939e4e12ecEli Friedman    return 5 + (getIntWidth(LongTy) << 3);
38557cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner  case BuiltinType::LongLong:
38567cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner  case BuiltinType::ULongLong:
3857f98aba35e6c3da5aae61843fc01334939e4e12ecEli Friedman    return 6 + (getIntWidth(LongLongTy) << 3);
38582df9ced9fd1e8c7d7b38443db07e0e811de22571Chris Lattner  case BuiltinType::Int128:
38592df9ced9fd1e8c7d7b38443db07e0e811de22571Chris Lattner  case BuiltinType::UInt128:
38602df9ced9fd1e8c7d7b38443db07e0e811de22571Chris Lattner    return 7 + (getIntWidth(Int128Ty) << 3);
3861f52ab250ff92bc51a9ac9a8e19bd43b63a5f844fChris Lattner  }
3862f52ab250ff92bc51a9ac9a8e19bd43b63a5f844fChris Lattner}
3863f52ab250ff92bc51a9ac9a8e19bd43b63a5f844fChris Lattner
386404e8357f6801e9ff52673e7e899a67bbabf9de93Eli Friedman/// \brief Whether this is a promotable bitfield reference according
386504e8357f6801e9ff52673e7e899a67bbabf9de93Eli Friedman/// to C99 6.3.1.1p2, bullet 2 (and GCC extensions).
386604e8357f6801e9ff52673e7e899a67bbabf9de93Eli Friedman///
386704e8357f6801e9ff52673e7e899a67bbabf9de93Eli Friedman/// \returns the type this bit-field will promote to, or NULL if no
386804e8357f6801e9ff52673e7e899a67bbabf9de93Eli Friedman/// promotion occurs.
38694ba2a17694148e16eaa8d3917f657ffcd3667be4Jay FoadQualType ASTContext::isPromotableBitField(Expr *E) const {
3870ceafbdeb93ecf323cca74e660bf54504c86f3b71Douglas Gregor  if (E->isTypeDependent() || E->isValueDependent())
3871ceafbdeb93ecf323cca74e660bf54504c86f3b71Douglas Gregor    return QualType();
3872ceafbdeb93ecf323cca74e660bf54504c86f3b71Douglas Gregor
387304e8357f6801e9ff52673e7e899a67bbabf9de93Eli Friedman  FieldDecl *Field = E->getBitField();
387404e8357f6801e9ff52673e7e899a67bbabf9de93Eli Friedman  if (!Field)
387504e8357f6801e9ff52673e7e899a67bbabf9de93Eli Friedman    return QualType();
387604e8357f6801e9ff52673e7e899a67bbabf9de93Eli Friedman
387704e8357f6801e9ff52673e7e899a67bbabf9de93Eli Friedman  QualType FT = Field->getType();
387804e8357f6801e9ff52673e7e899a67bbabf9de93Eli Friedman
3879a6b8b2c09610b8bc4330e948ece8b940c2386406Richard Smith  uint64_t BitWidth = Field->getBitWidthValue(*this);
388004e8357f6801e9ff52673e7e899a67bbabf9de93Eli Friedman  uint64_t IntSize = getTypeSize(IntTy);
388104e8357f6801e9ff52673e7e899a67bbabf9de93Eli Friedman  // GCC extension compatibility: if the bit-field size is less than or equal
388204e8357f6801e9ff52673e7e899a67bbabf9de93Eli Friedman  // to the size of int, it gets promoted no matter what its type is.
388304e8357f6801e9ff52673e7e899a67bbabf9de93Eli Friedman  // For instance, unsigned long bf : 4 gets promoted to signed int.
388404e8357f6801e9ff52673e7e899a67bbabf9de93Eli Friedman  if (BitWidth < IntSize)
388504e8357f6801e9ff52673e7e899a67bbabf9de93Eli Friedman    return IntTy;
388604e8357f6801e9ff52673e7e899a67bbabf9de93Eli Friedman
388704e8357f6801e9ff52673e7e899a67bbabf9de93Eli Friedman  if (BitWidth == IntSize)
388804e8357f6801e9ff52673e7e899a67bbabf9de93Eli Friedman    return FT->isSignedIntegerType() ? IntTy : UnsignedIntTy;
388904e8357f6801e9ff52673e7e899a67bbabf9de93Eli Friedman
389004e8357f6801e9ff52673e7e899a67bbabf9de93Eli Friedman  // Types bigger than int are not subject to promotions, and therefore act
389104e8357f6801e9ff52673e7e899a67bbabf9de93Eli Friedman  // like the base type.
389204e8357f6801e9ff52673e7e899a67bbabf9de93Eli Friedman  // FIXME: This doesn't quite match what gcc does, but what gcc does here
389304e8357f6801e9ff52673e7e899a67bbabf9de93Eli Friedman  // is ridiculous.
389404e8357f6801e9ff52673e7e899a67bbabf9de93Eli Friedman  return QualType();
389504e8357f6801e9ff52673e7e899a67bbabf9de93Eli Friedman}
389604e8357f6801e9ff52673e7e899a67bbabf9de93Eli Friedman
3897a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman/// getPromotedIntegerType - Returns the type that Promotable will
3898a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman/// promote to: C99 6.3.1.1p2, assuming that Promotable is a promotable
3899a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman/// integer type.
39004ba2a17694148e16eaa8d3917f657ffcd3667be4Jay FoadQualType ASTContext::getPromotedIntegerType(QualType Promotable) const {
3901a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman  assert(!Promotable.isNull());
3902a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman  assert(Promotable->isPromotableIntegerType());
3903842aef8d942a880eeb9535d40de31a86838264cbJohn McCall  if (const EnumType *ET = Promotable->getAs<EnumType>())
3904842aef8d942a880eeb9535d40de31a86838264cbJohn McCall    return ET->getDecl()->getPromotionType();
390568a2dc446fe6d32d5da3557902100ed06b21b12bEli Friedman
390668a2dc446fe6d32d5da3557902100ed06b21b12bEli Friedman  if (const BuiltinType *BT = Promotable->getAs<BuiltinType>()) {
390768a2dc446fe6d32d5da3557902100ed06b21b12bEli Friedman    // C++ [conv.prom]: A prvalue of type char16_t, char32_t, or wchar_t
390868a2dc446fe6d32d5da3557902100ed06b21b12bEli Friedman    // (3.9.1) can be converted to a prvalue of the first of the following
390968a2dc446fe6d32d5da3557902100ed06b21b12bEli Friedman    // types that can represent all the values of its underlying type:
391068a2dc446fe6d32d5da3557902100ed06b21b12bEli Friedman    // int, unsigned int, long int, unsigned long int, long long int, or
391168a2dc446fe6d32d5da3557902100ed06b21b12bEli Friedman    // unsigned long long int [...]
391268a2dc446fe6d32d5da3557902100ed06b21b12bEli Friedman    // FIXME: Is there some better way to compute this?
391368a2dc446fe6d32d5da3557902100ed06b21b12bEli Friedman    if (BT->getKind() == BuiltinType::WChar_S ||
391468a2dc446fe6d32d5da3557902100ed06b21b12bEli Friedman        BT->getKind() == BuiltinType::WChar_U ||
391568a2dc446fe6d32d5da3557902100ed06b21b12bEli Friedman        BT->getKind() == BuiltinType::Char16 ||
391668a2dc446fe6d32d5da3557902100ed06b21b12bEli Friedman        BT->getKind() == BuiltinType::Char32) {
391768a2dc446fe6d32d5da3557902100ed06b21b12bEli Friedman      bool FromIsSigned = BT->getKind() == BuiltinType::WChar_S;
391868a2dc446fe6d32d5da3557902100ed06b21b12bEli Friedman      uint64_t FromSize = getTypeSize(BT);
391968a2dc446fe6d32d5da3557902100ed06b21b12bEli Friedman      QualType PromoteTypes[] = { IntTy, UnsignedIntTy, LongTy, UnsignedLongTy,
392068a2dc446fe6d32d5da3557902100ed06b21b12bEli Friedman                                  LongLongTy, UnsignedLongLongTy };
392168a2dc446fe6d32d5da3557902100ed06b21b12bEli Friedman      for (size_t Idx = 0; Idx < llvm::array_lengthof(PromoteTypes); ++Idx) {
392268a2dc446fe6d32d5da3557902100ed06b21b12bEli Friedman        uint64_t ToSize = getTypeSize(PromoteTypes[Idx]);
392368a2dc446fe6d32d5da3557902100ed06b21b12bEli Friedman        if (FromSize < ToSize ||
392468a2dc446fe6d32d5da3557902100ed06b21b12bEli Friedman            (FromSize == ToSize &&
392568a2dc446fe6d32d5da3557902100ed06b21b12bEli Friedman             FromIsSigned == PromoteTypes[Idx]->isSignedIntegerType()))
392668a2dc446fe6d32d5da3557902100ed06b21b12bEli Friedman          return PromoteTypes[Idx];
392768a2dc446fe6d32d5da3557902100ed06b21b12bEli Friedman      }
392868a2dc446fe6d32d5da3557902100ed06b21b12bEli Friedman      llvm_unreachable("char type should fit into long long");
392968a2dc446fe6d32d5da3557902100ed06b21b12bEli Friedman    }
393068a2dc446fe6d32d5da3557902100ed06b21b12bEli Friedman  }
393168a2dc446fe6d32d5da3557902100ed06b21b12bEli Friedman
393268a2dc446fe6d32d5da3557902100ed06b21b12bEli Friedman  // At this point, we should have a signed or unsigned integer type.
3933a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman  if (Promotable->isSignedIntegerType())
3934a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman    return IntTy;
3935a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman  uint64_t PromotableSize = getTypeSize(Promotable);
3936a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman  uint64_t IntSize = getTypeSize(IntTy);
3937a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman  assert(Promotable->isUnsignedIntegerType() && PromotableSize <= IntSize);
3938a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman  return (PromotableSize != IntSize) ? IntTy : UnsignedIntTy;
3939a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman}
3940a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman
394131862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis/// \brief Recurses in pointer/array types until it finds an objc retainable
394231862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis/// type and returns its ownership.
394331862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios KyrtzidisQualifiers::ObjCLifetime ASTContext::getInnerObjCOwnership(QualType T) const {
394431862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis  while (!T.isNull()) {
394531862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis    if (T.getObjCLifetime() != Qualifiers::OCL_None)
394631862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis      return T.getObjCLifetime();
394731862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis    if (T->isArrayType())
394831862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis      T = getBaseElementType(T);
394931862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis    else if (const PointerType *PT = T->getAs<PointerType>())
395031862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis      T = PT->getPointeeType();
395131862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis    else if (const ReferenceType *RT = T->getAs<ReferenceType>())
395228445f0b62f6aed851ff87ce64d9b19200d3211fArgyrios Kyrtzidis      T = RT->getPointeeType();
395331862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis    else
395431862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis      break;
395531862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis  }
395631862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis
395731862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis  return Qualifiers::OCL_None;
395831862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis}
395931862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis
39601eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// getIntegerTypeOrder - Returns the highest ranked integer type:
39617cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner/// C99 6.3.1.8p1.  If LHS > RHS, return 1.  If LHS == RHS, return 0. If
39621eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// LHS < RHS, return -1.
39634ba2a17694148e16eaa8d3917f657ffcd3667be4Jay Foadint ASTContext::getIntegerTypeOrder(QualType LHS, QualType RHS) const {
3964f4c7371fb1d3cebcfb40abad4537bb82515704eaJohn McCall  const Type *LHSC = getCanonicalType(LHS).getTypePtr();
3965f4c7371fb1d3cebcfb40abad4537bb82515704eaJohn McCall  const Type *RHSC = getCanonicalType(RHS).getTypePtr();
39667cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner  if (LHSC == RHSC) return 0;
39671eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
3968f52ab250ff92bc51a9ac9a8e19bd43b63a5f844fChris Lattner  bool LHSUnsigned = LHSC->isUnsignedIntegerType();
3969f52ab250ff92bc51a9ac9a8e19bd43b63a5f844fChris Lattner  bool RHSUnsigned = RHSC->isUnsignedIntegerType();
39701eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
39717cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner  unsigned LHSRank = getIntegerRank(LHSC);
39727cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner  unsigned RHSRank = getIntegerRank(RHSC);
39731eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
39747cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner  if (LHSUnsigned == RHSUnsigned) {  // Both signed or both unsigned.
39757cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner    if (LHSRank == RHSRank) return 0;
39767cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner    return LHSRank > RHSRank ? 1 : -1;
39777cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner  }
39781eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
39797cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner  // Otherwise, the LHS is signed and the RHS is unsigned or visa versa.
39807cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner  if (LHSUnsigned) {
39817cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner    // If the unsigned [LHS] type is larger, return it.
39827cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner    if (LHSRank >= RHSRank)
39837cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner      return 1;
39841eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
39857cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner    // If the signed type can represent all values of the unsigned type, it
39867cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner    // wins.  Because we are dealing with 2's complement and types that are
39871eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    // powers of two larger than each other, this is always safe.
39887cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner    return -1;
39897cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner  }
39907cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner
39917cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner  // If the unsigned [RHS] type is larger, return it.
39927cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner  if (RHSRank >= LHSRank)
39937cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner    return -1;
39941eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
39957cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner  // If the signed type can represent all values of the unsigned type, it
39967cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner  // wins.  Because we are dealing with 2's complement and types that are
39971eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  // powers of two larger than each other, this is always safe.
39987cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner  return 1;
39995f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
400071993dd85eed9cc42c6b2fa61ee5c53026b74817Anders Carlsson
400179cbc7dd2aacd85a28f469b5dc73c4ea296e7072Anders Carlssonstatic RecordDecl *
4002ba877adeb49ed6dc17f27fa3a3bcd0cca713fd68Abramo BagnaraCreateRecordDecl(const ASTContext &Ctx, RecordDecl::TagKind TK,
4003ba877adeb49ed6dc17f27fa3a3bcd0cca713fd68Abramo Bagnara                 DeclContext *DC, IdentifierInfo *Id) {
4004ba877adeb49ed6dc17f27fa3a3bcd0cca713fd68Abramo Bagnara  SourceLocation Loc;
40054e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie  if (Ctx.getLangOpts().CPlusPlus)
4006ba877adeb49ed6dc17f27fa3a3bcd0cca713fd68Abramo Bagnara    return CXXRecordDecl::Create(Ctx, TK, DC, Loc, Loc, Id);
400779cbc7dd2aacd85a28f469b5dc73c4ea296e7072Anders Carlsson  else
4008ba877adeb49ed6dc17f27fa3a3bcd0cca713fd68Abramo Bagnara    return RecordDecl::Create(Ctx, TK, DC, Loc, Loc, Id);
400979cbc7dd2aacd85a28f469b5dc73c4ea296e7072Anders Carlsson}
4010ba877adeb49ed6dc17f27fa3a3bcd0cca713fd68Abramo Bagnara
40111eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump// getCFConstantStringType - Return the type used for constant CFStrings.
40124ba2a17694148e16eaa8d3917f657ffcd3667be4Jay FoadQualType ASTContext::getCFConstantStringType() const {
401371993dd85eed9cc42c6b2fa61ee5c53026b74817Anders Carlsson  if (!CFConstantStringTypeDecl) {
40141eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    CFConstantStringTypeDecl =
4015ba877adeb49ed6dc17f27fa3a3bcd0cca713fd68Abramo Bagnara      CreateRecordDecl(*this, TTK_Struct, TUDecl,
401679cbc7dd2aacd85a28f469b5dc73c4ea296e7072Anders Carlsson                       &Idents.get("NSConstantString"));
40175cfa011e61e14e6f2e1659047d809706c0e4c6a3John McCall    CFConstantStringTypeDecl->startDefinition();
401879cbc7dd2aacd85a28f469b5dc73c4ea296e7072Anders Carlsson
4019f06273f8bbacb086a46bde456429c8d08f6d07eeAnders Carlsson    QualType FieldTypes[4];
40201eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
402171993dd85eed9cc42c6b2fa61ee5c53026b74817Anders Carlsson    // const int *isa;
40220953e767ff7817f97b3ab20896b229891eeff45bJohn McCall    FieldTypes[0] = getPointerType(IntTy.withConst());
4023f06273f8bbacb086a46bde456429c8d08f6d07eeAnders Carlsson    // int flags;
4024f06273f8bbacb086a46bde456429c8d08f6d07eeAnders Carlsson    FieldTypes[1] = IntTy;
402571993dd85eed9cc42c6b2fa61ee5c53026b74817Anders Carlsson    // const char *str;
40260953e767ff7817f97b3ab20896b229891eeff45bJohn McCall    FieldTypes[2] = getPointerType(CharTy.withConst());
402771993dd85eed9cc42c6b2fa61ee5c53026b74817Anders Carlsson    // long length;
40281eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    FieldTypes[3] = LongTy;
40291eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
403044b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor    // Create fields
403144b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor    for (unsigned i = 0; i < 4; ++i) {
40321eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      FieldDecl *Field = FieldDecl::Create(*this, CFConstantStringTypeDecl,
4033ff676cb48fe8bf7be2feaa251dc7c5fb15af4730Abramo Bagnara                                           SourceLocation(),
403444b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor                                           SourceLocation(), 0,
4035a93c934af4fbf97cbe8e649d82e68ccacfe57c95John McCall                                           FieldTypes[i], /*TInfo=*/0,
40361eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump                                           /*BitWidth=*/0,
40377a614d8380297fcd2bc23986241905d97222948cRichard Smith                                           /*Mutable=*/false,
4038ca5233044ef679840d1ad1c46a36b16e2ee8a6e1Richard Smith                                           ICIS_NoInit);
40392888b65aae768f54062505330df7be230a0510c7John McCall      Field->setAccess(AS_public);
404017945a0f64fe03ff6ec0c2146005a87636e3ac12Argyrios Kyrtzidis      CFConstantStringTypeDecl->addDecl(Field);
404144b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor    }
404244b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor
4043838db383b69b9fb55f55c8e9546477df198a4faaDouglas Gregor    CFConstantStringTypeDecl->completeDefinition();
404471993dd85eed9cc42c6b2fa61ee5c53026b74817Anders Carlsson  }
40451eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
404671993dd85eed9cc42c6b2fa61ee5c53026b74817Anders Carlsson  return getTagDeclType(CFConstantStringTypeDecl);
40478467583c2704e7a9691ea56939a029015f0ade0aGabor Greif}
4048b2cf3573d7351094f6247fcca94703ce88eb9ee0Anders Carlsson
4049319ac896a0fef7365d5589b8021db7e41207fe42Douglas Gregorvoid ASTContext::setCFConstantStringType(QualType T) {
40506217b80b7a1379b74cced1c076338262c3c980b3Ted Kremenek  const RecordType *Rec = T->getAs<RecordType>();
4051319ac896a0fef7365d5589b8021db7e41207fe42Douglas Gregor  assert(Rec && "Invalid CFConstantStringType");
4052319ac896a0fef7365d5589b8021db7e41207fe42Douglas Gregor  CFConstantStringTypeDecl = Rec->getDecl();
4053319ac896a0fef7365d5589b8021db7e41207fe42Douglas Gregor}
4054319ac896a0fef7365d5589b8021db7e41207fe42Douglas Gregor
40554ba2a17694148e16eaa8d3917f657ffcd3667be4Jay FoadQualType ASTContext::getBlockDescriptorType() const {
4056adaaad3715c9c26cdcfdfe3401a13d7b4423ddcfMike Stump  if (BlockDescriptorType)
4057adaaad3715c9c26cdcfdfe3401a13d7b4423ddcfMike Stump    return getTagDeclType(BlockDescriptorType);
4058adaaad3715c9c26cdcfdfe3401a13d7b4423ddcfMike Stump
4059adaaad3715c9c26cdcfdfe3401a13d7b4423ddcfMike Stump  RecordDecl *T;
4060adaaad3715c9c26cdcfdfe3401a13d7b4423ddcfMike Stump  // FIXME: Needs the FlagAppleBlock bit.
4061ba877adeb49ed6dc17f27fa3a3bcd0cca713fd68Abramo Bagnara  T = CreateRecordDecl(*this, TTK_Struct, TUDecl,
406279cbc7dd2aacd85a28f469b5dc73c4ea296e7072Anders Carlsson                       &Idents.get("__block_descriptor"));
40635cfa011e61e14e6f2e1659047d809706c0e4c6a3John McCall  T->startDefinition();
4064adaaad3715c9c26cdcfdfe3401a13d7b4423ddcfMike Stump
4065adaaad3715c9c26cdcfdfe3401a13d7b4423ddcfMike Stump  QualType FieldTypes[] = {
4066adaaad3715c9c26cdcfdfe3401a13d7b4423ddcfMike Stump    UnsignedLongTy,
4067adaaad3715c9c26cdcfdfe3401a13d7b4423ddcfMike Stump    UnsignedLongTy,
4068adaaad3715c9c26cdcfdfe3401a13d7b4423ddcfMike Stump  };
4069adaaad3715c9c26cdcfdfe3401a13d7b4423ddcfMike Stump
4070adaaad3715c9c26cdcfdfe3401a13d7b4423ddcfMike Stump  const char *FieldNames[] = {
4071adaaad3715c9c26cdcfdfe3401a13d7b4423ddcfMike Stump    "reserved",
4072083c25eea14bb4cc4ecc3ec763c60e2e609e22bdMike Stump    "Size"
4073adaaad3715c9c26cdcfdfe3401a13d7b4423ddcfMike Stump  };
4074adaaad3715c9c26cdcfdfe3401a13d7b4423ddcfMike Stump
4075adaaad3715c9c26cdcfdfe3401a13d7b4423ddcfMike Stump  for (size_t i = 0; i < 2; ++i) {
4076ff676cb48fe8bf7be2feaa251dc7c5fb15af4730Abramo Bagnara    FieldDecl *Field = FieldDecl::Create(*this, T, SourceLocation(),
4077adaaad3715c9c26cdcfdfe3401a13d7b4423ddcfMike Stump                                         SourceLocation(),
4078adaaad3715c9c26cdcfdfe3401a13d7b4423ddcfMike Stump                                         &Idents.get(FieldNames[i]),
4079a93c934af4fbf97cbe8e649d82e68ccacfe57c95John McCall                                         FieldTypes[i], /*TInfo=*/0,
4080adaaad3715c9c26cdcfdfe3401a13d7b4423ddcfMike Stump                                         /*BitWidth=*/0,
40817a614d8380297fcd2bc23986241905d97222948cRichard Smith                                         /*Mutable=*/false,
4082ca5233044ef679840d1ad1c46a36b16e2ee8a6e1Richard Smith                                         ICIS_NoInit);
40832888b65aae768f54062505330df7be230a0510c7John McCall    Field->setAccess(AS_public);
4084adaaad3715c9c26cdcfdfe3401a13d7b4423ddcfMike Stump    T->addDecl(Field);
4085adaaad3715c9c26cdcfdfe3401a13d7b4423ddcfMike Stump  }
4086adaaad3715c9c26cdcfdfe3401a13d7b4423ddcfMike Stump
4087838db383b69b9fb55f55c8e9546477df198a4faaDouglas Gregor  T->completeDefinition();
4088adaaad3715c9c26cdcfdfe3401a13d7b4423ddcfMike Stump
4089adaaad3715c9c26cdcfdfe3401a13d7b4423ddcfMike Stump  BlockDescriptorType = T;
4090adaaad3715c9c26cdcfdfe3401a13d7b4423ddcfMike Stump
4091adaaad3715c9c26cdcfdfe3401a13d7b4423ddcfMike Stump  return getTagDeclType(BlockDescriptorType);
4092adaaad3715c9c26cdcfdfe3401a13d7b4423ddcfMike Stump}
4093adaaad3715c9c26cdcfdfe3401a13d7b4423ddcfMike Stump
40944ba2a17694148e16eaa8d3917f657ffcd3667be4Jay FoadQualType ASTContext::getBlockDescriptorExtendedType() const {
4095083c25eea14bb4cc4ecc3ec763c60e2e609e22bdMike Stump  if (BlockDescriptorExtendedType)
4096083c25eea14bb4cc4ecc3ec763c60e2e609e22bdMike Stump    return getTagDeclType(BlockDescriptorExtendedType);
4097083c25eea14bb4cc4ecc3ec763c60e2e609e22bdMike Stump
4098083c25eea14bb4cc4ecc3ec763c60e2e609e22bdMike Stump  RecordDecl *T;
4099083c25eea14bb4cc4ecc3ec763c60e2e609e22bdMike Stump  // FIXME: Needs the FlagAppleBlock bit.
4100ba877adeb49ed6dc17f27fa3a3bcd0cca713fd68Abramo Bagnara  T = CreateRecordDecl(*this, TTK_Struct, TUDecl,
410179cbc7dd2aacd85a28f469b5dc73c4ea296e7072Anders Carlsson                       &Idents.get("__block_descriptor_withcopydispose"));
41025cfa011e61e14e6f2e1659047d809706c0e4c6a3John McCall  T->startDefinition();
4103083c25eea14bb4cc4ecc3ec763c60e2e609e22bdMike Stump
4104083c25eea14bb4cc4ecc3ec763c60e2e609e22bdMike Stump  QualType FieldTypes[] = {
4105083c25eea14bb4cc4ecc3ec763c60e2e609e22bdMike Stump    UnsignedLongTy,
4106083c25eea14bb4cc4ecc3ec763c60e2e609e22bdMike Stump    UnsignedLongTy,
4107083c25eea14bb4cc4ecc3ec763c60e2e609e22bdMike Stump    getPointerType(VoidPtrTy),
4108083c25eea14bb4cc4ecc3ec763c60e2e609e22bdMike Stump    getPointerType(VoidPtrTy)
4109083c25eea14bb4cc4ecc3ec763c60e2e609e22bdMike Stump  };
4110083c25eea14bb4cc4ecc3ec763c60e2e609e22bdMike Stump
4111083c25eea14bb4cc4ecc3ec763c60e2e609e22bdMike Stump  const char *FieldNames[] = {
4112083c25eea14bb4cc4ecc3ec763c60e2e609e22bdMike Stump    "reserved",
4113083c25eea14bb4cc4ecc3ec763c60e2e609e22bdMike Stump    "Size",
4114083c25eea14bb4cc4ecc3ec763c60e2e609e22bdMike Stump    "CopyFuncPtr",
4115083c25eea14bb4cc4ecc3ec763c60e2e609e22bdMike Stump    "DestroyFuncPtr"
4116083c25eea14bb4cc4ecc3ec763c60e2e609e22bdMike Stump  };
4117083c25eea14bb4cc4ecc3ec763c60e2e609e22bdMike Stump
4118083c25eea14bb4cc4ecc3ec763c60e2e609e22bdMike Stump  for (size_t i = 0; i < 4; ++i) {
4119ff676cb48fe8bf7be2feaa251dc7c5fb15af4730Abramo Bagnara    FieldDecl *Field = FieldDecl::Create(*this, T, SourceLocation(),
4120083c25eea14bb4cc4ecc3ec763c60e2e609e22bdMike Stump                                         SourceLocation(),
4121083c25eea14bb4cc4ecc3ec763c60e2e609e22bdMike Stump                                         &Idents.get(FieldNames[i]),
4122a93c934af4fbf97cbe8e649d82e68ccacfe57c95John McCall                                         FieldTypes[i], /*TInfo=*/0,
4123083c25eea14bb4cc4ecc3ec763c60e2e609e22bdMike Stump                                         /*BitWidth=*/0,
41247a614d8380297fcd2bc23986241905d97222948cRichard Smith                                         /*Mutable=*/false,
4125ca5233044ef679840d1ad1c46a36b16e2ee8a6e1Richard Smith                                         ICIS_NoInit);
41262888b65aae768f54062505330df7be230a0510c7John McCall    Field->setAccess(AS_public);
4127083c25eea14bb4cc4ecc3ec763c60e2e609e22bdMike Stump    T->addDecl(Field);
4128083c25eea14bb4cc4ecc3ec763c60e2e609e22bdMike Stump  }
4129083c25eea14bb4cc4ecc3ec763c60e2e609e22bdMike Stump
4130838db383b69b9fb55f55c8e9546477df198a4faaDouglas Gregor  T->completeDefinition();
4131083c25eea14bb4cc4ecc3ec763c60e2e609e22bdMike Stump
4132083c25eea14bb4cc4ecc3ec763c60e2e609e22bdMike Stump  BlockDescriptorExtendedType = T;
4133083c25eea14bb4cc4ecc3ec763c60e2e609e22bdMike Stump
4134083c25eea14bb4cc4ecc3ec763c60e2e609e22bdMike Stump  return getTagDeclType(BlockDescriptorExtendedType);
4135083c25eea14bb4cc4ecc3ec763c60e2e609e22bdMike Stump}
4136083c25eea14bb4cc4ecc3ec763c60e2e609e22bdMike Stump
41374ba2a17694148e16eaa8d3917f657ffcd3667be4Jay Foadbool ASTContext::BlockRequiresCopying(QualType Ty) const {
4138f85e193739c953358c865005855253af4f68a497John McCall  if (Ty->isObjCRetainableType())
4139af7b44d847d3e44c43346d508b2e55a6254b6e9dMike Stump    return true;
41404e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie  if (getLangOpts().CPlusPlus) {
4141e38be617437ccdcc180c5a49e447cbcd07539292Fariborz Jahanian    if (const RecordType *RT = Ty->getAs<RecordType>()) {
4142e38be617437ccdcc180c5a49e447cbcd07539292Fariborz Jahanian      CXXRecordDecl *RD = cast<CXXRecordDecl>(RT->getDecl());
4143ffe37fdda5b4b4f162a45155c30d9f60ce110c12Sean Hunt      return RD->hasConstCopyConstructor();
4144e38be617437ccdcc180c5a49e447cbcd07539292Fariborz Jahanian
4145e38be617437ccdcc180c5a49e447cbcd07539292Fariborz Jahanian    }
4146e38be617437ccdcc180c5a49e447cbcd07539292Fariborz Jahanian  }
4147af7b44d847d3e44c43346d508b2e55a6254b6e9dMike Stump  return false;
4148af7b44d847d3e44c43346d508b2e55a6254b6e9dMike Stump}
4149af7b44d847d3e44c43346d508b2e55a6254b6e9dMike Stump
41504ba2a17694148e16eaa8d3917f657ffcd3667be4Jay FoadQualType
41515f9e272e632e951b1efe824cd16acb4d96077930Chris LattnerASTContext::BuildByRefType(StringRef DeclName, QualType Ty) const {
4152af7b44d847d3e44c43346d508b2e55a6254b6e9dMike Stump  //  type = struct __Block_byref_1_X {
4153ea26cb522e88fc86b0d1cae61dcefcfe4cc20231Mike Stump  //    void *__isa;
4154af7b44d847d3e44c43346d508b2e55a6254b6e9dMike Stump  //    struct __Block_byref_1_X *__forwarding;
4155ea26cb522e88fc86b0d1cae61dcefcfe4cc20231Mike Stump  //    unsigned int __flags;
4156ea26cb522e88fc86b0d1cae61dcefcfe4cc20231Mike Stump  //    unsigned int __size;
4157a7e6845660f91ec611427e1db842780e1ec12bdbEli Friedman  //    void *__copy_helper;            // as needed
4158a7e6845660f91ec611427e1db842780e1ec12bdbEli Friedman  //    void *__destroy_help            // as needed
4159af7b44d847d3e44c43346d508b2e55a6254b6e9dMike Stump  //    int X;
4160ea26cb522e88fc86b0d1cae61dcefcfe4cc20231Mike Stump  //  } *
4161ea26cb522e88fc86b0d1cae61dcefcfe4cc20231Mike Stump
4162af7b44d847d3e44c43346d508b2e55a6254b6e9dMike Stump  bool HasCopyAndDispose = BlockRequiresCopying(Ty);
4163af7b44d847d3e44c43346d508b2e55a6254b6e9dMike Stump
4164af7b44d847d3e44c43346d508b2e55a6254b6e9dMike Stump  // FIXME: Move up
4165f7ccbad5d9949e7ddd1cbef43d482553b811e026Dylan Noblesmith  SmallString<36> Name;
4166f5942a44880be26878592eb052b737579349411eBenjamin Kramer  llvm::raw_svector_ostream(Name) << "__Block_byref_" <<
4167f5942a44880be26878592eb052b737579349411eBenjamin Kramer                                  ++UniqueBlockByRefTypeID << '_' << DeclName;
4168af7b44d847d3e44c43346d508b2e55a6254b6e9dMike Stump  RecordDecl *T;
4169ba877adeb49ed6dc17f27fa3a3bcd0cca713fd68Abramo Bagnara  T = CreateRecordDecl(*this, TTK_Struct, TUDecl, &Idents.get(Name.str()));
4170af7b44d847d3e44c43346d508b2e55a6254b6e9dMike Stump  T->startDefinition();
4171af7b44d847d3e44c43346d508b2e55a6254b6e9dMike Stump  QualType Int32Ty = IntTy;
4172af7b44d847d3e44c43346d508b2e55a6254b6e9dMike Stump  assert(getIntWidth(IntTy) == 32 && "non-32bit int not supported");
4173af7b44d847d3e44c43346d508b2e55a6254b6e9dMike Stump  QualType FieldTypes[] = {
4174af7b44d847d3e44c43346d508b2e55a6254b6e9dMike Stump    getPointerType(VoidPtrTy),
4175af7b44d847d3e44c43346d508b2e55a6254b6e9dMike Stump    getPointerType(getTagDeclType(T)),
4176af7b44d847d3e44c43346d508b2e55a6254b6e9dMike Stump    Int32Ty,
4177af7b44d847d3e44c43346d508b2e55a6254b6e9dMike Stump    Int32Ty,
4178af7b44d847d3e44c43346d508b2e55a6254b6e9dMike Stump    getPointerType(VoidPtrTy),
4179af7b44d847d3e44c43346d508b2e55a6254b6e9dMike Stump    getPointerType(VoidPtrTy),
4180af7b44d847d3e44c43346d508b2e55a6254b6e9dMike Stump    Ty
4181af7b44d847d3e44c43346d508b2e55a6254b6e9dMike Stump  };
4182af7b44d847d3e44c43346d508b2e55a6254b6e9dMike Stump
41835f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner  StringRef FieldNames[] = {
4184af7b44d847d3e44c43346d508b2e55a6254b6e9dMike Stump    "__isa",
4185af7b44d847d3e44c43346d508b2e55a6254b6e9dMike Stump    "__forwarding",
4186af7b44d847d3e44c43346d508b2e55a6254b6e9dMike Stump    "__flags",
4187af7b44d847d3e44c43346d508b2e55a6254b6e9dMike Stump    "__size",
4188af7b44d847d3e44c43346d508b2e55a6254b6e9dMike Stump    "__copy_helper",
4189af7b44d847d3e44c43346d508b2e55a6254b6e9dMike Stump    "__destroy_helper",
4190af7b44d847d3e44c43346d508b2e55a6254b6e9dMike Stump    DeclName,
4191af7b44d847d3e44c43346d508b2e55a6254b6e9dMike Stump  };
4192af7b44d847d3e44c43346d508b2e55a6254b6e9dMike Stump
4193af7b44d847d3e44c43346d508b2e55a6254b6e9dMike Stump  for (size_t i = 0; i < 7; ++i) {
4194af7b44d847d3e44c43346d508b2e55a6254b6e9dMike Stump    if (!HasCopyAndDispose && i >=4 && i <= 5)
4195af7b44d847d3e44c43346d508b2e55a6254b6e9dMike Stump      continue;
4196af7b44d847d3e44c43346d508b2e55a6254b6e9dMike Stump    FieldDecl *Field = FieldDecl::Create(*this, T, SourceLocation(),
4197ff676cb48fe8bf7be2feaa251dc7c5fb15af4730Abramo Bagnara                                         SourceLocation(),
4198af7b44d847d3e44c43346d508b2e55a6254b6e9dMike Stump                                         &Idents.get(FieldNames[i]),
4199a93c934af4fbf97cbe8e649d82e68ccacfe57c95John McCall                                         FieldTypes[i], /*TInfo=*/0,
42007a614d8380297fcd2bc23986241905d97222948cRichard Smith                                         /*BitWidth=*/0, /*Mutable=*/false,
4201ca5233044ef679840d1ad1c46a36b16e2ee8a6e1Richard Smith                                         ICIS_NoInit);
42022888b65aae768f54062505330df7be230a0510c7John McCall    Field->setAccess(AS_public);
4203af7b44d847d3e44c43346d508b2e55a6254b6e9dMike Stump    T->addDecl(Field);
4204af7b44d847d3e44c43346d508b2e55a6254b6e9dMike Stump  }
4205af7b44d847d3e44c43346d508b2e55a6254b6e9dMike Stump
4206838db383b69b9fb55f55c8e9546477df198a4faaDouglas Gregor  T->completeDefinition();
4207af7b44d847d3e44c43346d508b2e55a6254b6e9dMike Stump
4208af7b44d847d3e44c43346d508b2e55a6254b6e9dMike Stump  return getPointerType(getTagDeclType(T));
4209ea26cb522e88fc86b0d1cae61dcefcfe4cc20231Mike Stump}
4210ea26cb522e88fc86b0d1cae61dcefcfe4cc20231Mike Stump
4211e97179c675b341927807c718be215c8d1aab8acbDouglas GregorTypedefDecl *ASTContext::getObjCInstanceTypeDecl() {
4212e97179c675b341927807c718be215c8d1aab8acbDouglas Gregor  if (!ObjCInstanceTypeDecl)
4213e97179c675b341927807c718be215c8d1aab8acbDouglas Gregor    ObjCInstanceTypeDecl = TypedefDecl::Create(*this,
4214e97179c675b341927807c718be215c8d1aab8acbDouglas Gregor                                               getTranslationUnitDecl(),
4215e97179c675b341927807c718be215c8d1aab8acbDouglas Gregor                                               SourceLocation(),
4216e97179c675b341927807c718be215c8d1aab8acbDouglas Gregor                                               SourceLocation(),
4217e97179c675b341927807c718be215c8d1aab8acbDouglas Gregor                                               &Idents.get("instancetype"),
4218e97179c675b341927807c718be215c8d1aab8acbDouglas Gregor                                     getTrivialTypeSourceInfo(getObjCIdType()));
4219e97179c675b341927807c718be215c8d1aab8acbDouglas Gregor  return ObjCInstanceTypeDecl;
4220e97179c675b341927807c718be215c8d1aab8acbDouglas Gregor}
4221e97179c675b341927807c718be215c8d1aab8acbDouglas Gregor
4222e8c49533521c40643653f943d47229e62d277f88Anders Carlsson// This returns true if a type has been typedefed to BOOL:
4223e8c49533521c40643653f943d47229e62d277f88Anders Carlsson// typedef <type> BOOL;
42242d99833e8c956775f2183601cd120b65b569c867Chris Lattnerstatic bool isTypeTypedefedAsBOOL(QualType T) {
4225e8c49533521c40643653f943d47229e62d277f88Anders Carlsson  if (const TypedefType *TT = dyn_cast<TypedefType>(T))
4226bb49c3ee5d270485f4b273691fd14bc97403fa5dChris Lattner    if (IdentifierInfo *II = TT->getDecl()->getIdentifier())
4227bb49c3ee5d270485f4b273691fd14bc97403fa5dChris Lattner      return II->isStr("BOOL");
42281eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
422985f9bceab1542aafff012d4d28e998f4ba16e362Anders Carlsson  return false;
423085f9bceab1542aafff012d4d28e998f4ba16e362Anders Carlsson}
423185f9bceab1542aafff012d4d28e998f4ba16e362Anders Carlsson
4232a526c5c67e5a0473c340903ee542ce570119665fTed Kremenek/// getObjCEncodingTypeSize returns size of type for objective-c encoding
423333e1d64ab5cd5d27f8530ccd056191fe2c9f3f2eFariborz Jahanian/// purpose.
42344ba2a17694148e16eaa8d3917f657ffcd3667be4Jay FoadCharUnits ASTContext::getObjCEncodingTypeSize(QualType type) const {
4235f968d8374791c37bc464efd9168c2d33dd73605fDouglas Gregor  if (!type->isIncompleteArrayType() && type->isIncompleteType())
4236f968d8374791c37bc464efd9168c2d33dd73605fDouglas Gregor    return CharUnits::Zero();
4237f968d8374791c37bc464efd9168c2d33dd73605fDouglas Gregor
4238199c3d6cd16aebbb9c7f0d42af9d922c9628bf70Ken Dyck  CharUnits sz = getTypeSizeInChars(type);
42391eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
424033e1d64ab5cd5d27f8530ccd056191fe2c9f3f2eFariborz Jahanian  // Make all integer and enum types at least as large as an int
42412ade35e2cfd554e49d35a52047cea98a82787af9Douglas Gregor  if (sz.isPositive() && type->isIntegralOrEnumerationType())
4242199c3d6cd16aebbb9c7f0d42af9d922c9628bf70Ken Dyck    sz = std::max(sz, getTypeSizeInChars(IntTy));
424333e1d64ab5cd5d27f8530ccd056191fe2c9f3f2eFariborz Jahanian  // Treat arrays as pointers, since that's how they're passed in.
424433e1d64ab5cd5d27f8530ccd056191fe2c9f3f2eFariborz Jahanian  else if (type->isArrayType())
4245199c3d6cd16aebbb9c7f0d42af9d922c9628bf70Ken Dyck    sz = getTypeSizeInChars(VoidPtrTy);
4246aa8741a1db98eef05f09b1200dba94aa5dc3bc3dKen Dyck  return sz;
4247199c3d6cd16aebbb9c7f0d42af9d922c9628bf70Ken Dyck}
4248199c3d6cd16aebbb9c7f0d42af9d922c9628bf70Ken Dyck
4249199c3d6cd16aebbb9c7f0d42af9d922c9628bf70Ken Dyckstatic inline
4250199c3d6cd16aebbb9c7f0d42af9d922c9628bf70Ken Dyckstd::string charUnitsToString(const CharUnits &CU) {
4251199c3d6cd16aebbb9c7f0d42af9d922c9628bf70Ken Dyck  return llvm::itostr(CU.getQuantity());
425233e1d64ab5cd5d27f8530ccd056191fe2c9f3f2eFariborz Jahanian}
425333e1d64ab5cd5d27f8530ccd056191fe2c9f3f2eFariborz Jahanian
42546b5a61b6dc400027fd793dcadceeb9da944a37eaJohn McCall/// getObjCEncodingForBlock - Return the encoded type for this block
42555e530af5d51572a0ed5dbe50da54bd333840c63dDavid Chisnall/// declaration.
42566b5a61b6dc400027fd793dcadceeb9da944a37eaJohn McCallstd::string ASTContext::getObjCEncodingForBlock(const BlockExpr *Expr) const {
42576b5a61b6dc400027fd793dcadceeb9da944a37eaJohn McCall  std::string S;
42586b5a61b6dc400027fd793dcadceeb9da944a37eaJohn McCall
42595e530af5d51572a0ed5dbe50da54bd333840c63dDavid Chisnall  const BlockDecl *Decl = Expr->getBlockDecl();
42605e530af5d51572a0ed5dbe50da54bd333840c63dDavid Chisnall  QualType BlockTy =
42615e530af5d51572a0ed5dbe50da54bd333840c63dDavid Chisnall      Expr->getType()->getAs<BlockPointerType>()->getPointeeType();
42625e530af5d51572a0ed5dbe50da54bd333840c63dDavid Chisnall  // Encode result type.
4263c71a4915ca216847599d03cab4ed1c5086b0eb43John McCall  getObjCEncodingForType(BlockTy->getAs<FunctionType>()->getResultType(), S);
42645e530af5d51572a0ed5dbe50da54bd333840c63dDavid Chisnall  // Compute size of all parameters.
42655e530af5d51572a0ed5dbe50da54bd333840c63dDavid Chisnall  // Start with computing size of a pointer in number of bytes.
42665e530af5d51572a0ed5dbe50da54bd333840c63dDavid Chisnall  // FIXME: There might(should) be a better way of doing this computation!
42675e530af5d51572a0ed5dbe50da54bd333840c63dDavid Chisnall  SourceLocation Loc;
4268199c3d6cd16aebbb9c7f0d42af9d922c9628bf70Ken Dyck  CharUnits PtrSize = getTypeSizeInChars(VoidPtrTy);
4269199c3d6cd16aebbb9c7f0d42af9d922c9628bf70Ken Dyck  CharUnits ParmOffset = PtrSize;
42706f46c2653c1545cc3fef0c0df996d18160160ce8Fariborz Jahanian  for (BlockDecl::param_const_iterator PI = Decl->param_begin(),
42715e530af5d51572a0ed5dbe50da54bd333840c63dDavid Chisnall       E = Decl->param_end(); PI != E; ++PI) {
42725e530af5d51572a0ed5dbe50da54bd333840c63dDavid Chisnall    QualType PType = (*PI)->getType();
4273aa8741a1db98eef05f09b1200dba94aa5dc3bc3dKen Dyck    CharUnits sz = getObjCEncodingTypeSize(PType);
4274075a54354dc6e3644b12206e5127855091783fd6Fariborz Jahanian    if (sz.isZero())
4275075a54354dc6e3644b12206e5127855091783fd6Fariborz Jahanian      continue;
4276199c3d6cd16aebbb9c7f0d42af9d922c9628bf70Ken Dyck    assert (sz.isPositive() && "BlockExpr - Incomplete param type");
42775e530af5d51572a0ed5dbe50da54bd333840c63dDavid Chisnall    ParmOffset += sz;
42785e530af5d51572a0ed5dbe50da54bd333840c63dDavid Chisnall  }
42795e530af5d51572a0ed5dbe50da54bd333840c63dDavid Chisnall  // Size of the argument frame
4280199c3d6cd16aebbb9c7f0d42af9d922c9628bf70Ken Dyck  S += charUnitsToString(ParmOffset);
42815e530af5d51572a0ed5dbe50da54bd333840c63dDavid Chisnall  // Block pointer and offset.
42825e530af5d51572a0ed5dbe50da54bd333840c63dDavid Chisnall  S += "@?0";
42835e530af5d51572a0ed5dbe50da54bd333840c63dDavid Chisnall
42845e530af5d51572a0ed5dbe50da54bd333840c63dDavid Chisnall  // Argument types.
42855e530af5d51572a0ed5dbe50da54bd333840c63dDavid Chisnall  ParmOffset = PtrSize;
42865e530af5d51572a0ed5dbe50da54bd333840c63dDavid Chisnall  for (BlockDecl::param_const_iterator PI = Decl->param_begin(), E =
42875e530af5d51572a0ed5dbe50da54bd333840c63dDavid Chisnall       Decl->param_end(); PI != E; ++PI) {
42885e530af5d51572a0ed5dbe50da54bd333840c63dDavid Chisnall    ParmVarDecl *PVDecl = *PI;
42895e530af5d51572a0ed5dbe50da54bd333840c63dDavid Chisnall    QualType PType = PVDecl->getOriginalType();
42905e530af5d51572a0ed5dbe50da54bd333840c63dDavid Chisnall    if (const ArrayType *AT =
42915e530af5d51572a0ed5dbe50da54bd333840c63dDavid Chisnall          dyn_cast<ArrayType>(PType->getCanonicalTypeInternal())) {
42925e530af5d51572a0ed5dbe50da54bd333840c63dDavid Chisnall      // Use array's original type only if it has known number of
42935389f48b24937ad7b4093307128b3cbf25235654David Chisnall      // elements.
42945389f48b24937ad7b4093307128b3cbf25235654David Chisnall      if (!isa<ConstantArrayType>(AT))
42955389f48b24937ad7b4093307128b3cbf25235654David Chisnall        PType = PVDecl->getType();
42965389f48b24937ad7b4093307128b3cbf25235654David Chisnall    } else if (PType->isFunctionType())
42975389f48b24937ad7b4093307128b3cbf25235654David Chisnall      PType = PVDecl->getType();
42985389f48b24937ad7b4093307128b3cbf25235654David Chisnall    getObjCEncodingForType(PType, S);
42995389f48b24937ad7b4093307128b3cbf25235654David Chisnall    S += charUnitsToString(ParmOffset);
43005389f48b24937ad7b4093307128b3cbf25235654David Chisnall    ParmOffset += getObjCEncodingTypeSize(PType);
43015389f48b24937ad7b4093307128b3cbf25235654David Chisnall  }
43026b5a61b6dc400027fd793dcadceeb9da944a37eaJohn McCall
43036b5a61b6dc400027fd793dcadceeb9da944a37eaJohn McCall  return S;
43045389f48b24937ad7b4093307128b3cbf25235654David Chisnall}
43055389f48b24937ad7b4093307128b3cbf25235654David Chisnall
4306f968d8374791c37bc464efd9168c2d33dd73605fDouglas Gregorbool ASTContext::getObjCEncodingForFunctionDecl(const FunctionDecl *Decl,
43075389f48b24937ad7b4093307128b3cbf25235654David Chisnall                                                std::string& S) {
43085389f48b24937ad7b4093307128b3cbf25235654David Chisnall  // Encode result type.
43095389f48b24937ad7b4093307128b3cbf25235654David Chisnall  getObjCEncodingForType(Decl->getResultType(), S);
43105389f48b24937ad7b4093307128b3cbf25235654David Chisnall  CharUnits ParmOffset;
43115389f48b24937ad7b4093307128b3cbf25235654David Chisnall  // Compute size of all parameters.
43125389f48b24937ad7b4093307128b3cbf25235654David Chisnall  for (FunctionDecl::param_const_iterator PI = Decl->param_begin(),
43135389f48b24937ad7b4093307128b3cbf25235654David Chisnall       E = Decl->param_end(); PI != E; ++PI) {
43145389f48b24937ad7b4093307128b3cbf25235654David Chisnall    QualType PType = (*PI)->getType();
43155389f48b24937ad7b4093307128b3cbf25235654David Chisnall    CharUnits sz = getObjCEncodingTypeSize(PType);
4316f968d8374791c37bc464efd9168c2d33dd73605fDouglas Gregor    if (sz.isZero())
43177e68ba5660a9b2b854df4ae7a556e6b91d738c6cFariborz Jahanian      continue;
43187e68ba5660a9b2b854df4ae7a556e6b91d738c6cFariborz Jahanian
43195389f48b24937ad7b4093307128b3cbf25235654David Chisnall    assert (sz.isPositive() &&
4320f968d8374791c37bc464efd9168c2d33dd73605fDouglas Gregor        "getObjCEncodingForFunctionDecl - Incomplete param type");
43215389f48b24937ad7b4093307128b3cbf25235654David Chisnall    ParmOffset += sz;
43225389f48b24937ad7b4093307128b3cbf25235654David Chisnall  }
43235389f48b24937ad7b4093307128b3cbf25235654David Chisnall  S += charUnitsToString(ParmOffset);
43245389f48b24937ad7b4093307128b3cbf25235654David Chisnall  ParmOffset = CharUnits::Zero();
43255389f48b24937ad7b4093307128b3cbf25235654David Chisnall
43265389f48b24937ad7b4093307128b3cbf25235654David Chisnall  // Argument types.
43275389f48b24937ad7b4093307128b3cbf25235654David Chisnall  for (FunctionDecl::param_const_iterator PI = Decl->param_begin(),
43285389f48b24937ad7b4093307128b3cbf25235654David Chisnall       E = Decl->param_end(); PI != E; ++PI) {
43295389f48b24937ad7b4093307128b3cbf25235654David Chisnall    ParmVarDecl *PVDecl = *PI;
43305389f48b24937ad7b4093307128b3cbf25235654David Chisnall    QualType PType = PVDecl->getOriginalType();
43315389f48b24937ad7b4093307128b3cbf25235654David Chisnall    if (const ArrayType *AT =
43325389f48b24937ad7b4093307128b3cbf25235654David Chisnall          dyn_cast<ArrayType>(PType->getCanonicalTypeInternal())) {
43335389f48b24937ad7b4093307128b3cbf25235654David Chisnall      // Use array's original type only if it has known number of
43345e530af5d51572a0ed5dbe50da54bd333840c63dDavid Chisnall      // elements.
43355e530af5d51572a0ed5dbe50da54bd333840c63dDavid Chisnall      if (!isa<ConstantArrayType>(AT))
43365e530af5d51572a0ed5dbe50da54bd333840c63dDavid Chisnall        PType = PVDecl->getType();
43375e530af5d51572a0ed5dbe50da54bd333840c63dDavid Chisnall    } else if (PType->isFunctionType())
43385e530af5d51572a0ed5dbe50da54bd333840c63dDavid Chisnall      PType = PVDecl->getType();
43395e530af5d51572a0ed5dbe50da54bd333840c63dDavid Chisnall    getObjCEncodingForType(PType, S);
4340199c3d6cd16aebbb9c7f0d42af9d922c9628bf70Ken Dyck    S += charUnitsToString(ParmOffset);
4341aa8741a1db98eef05f09b1200dba94aa5dc3bc3dKen Dyck    ParmOffset += getObjCEncodingTypeSize(PType);
43425e530af5d51572a0ed5dbe50da54bd333840c63dDavid Chisnall  }
4343f968d8374791c37bc464efd9168c2d33dd73605fDouglas Gregor
4344f968d8374791c37bc464efd9168c2d33dd73605fDouglas Gregor  return false;
43455e530af5d51572a0ed5dbe50da54bd333840c63dDavid Chisnall}
43465e530af5d51572a0ed5dbe50da54bd333840c63dDavid Chisnall
4347dc8dab6fabf4bfd4f4b94bf572ac3342a5bbfcd7Bob Wilson/// getObjCEncodingForMethodParameter - Return the encoded type for a single
4348dc8dab6fabf4bfd4f4b94bf572ac3342a5bbfcd7Bob Wilson/// method parameter or return type. If Extended, include class names and
4349dc8dab6fabf4bfd4f4b94bf572ac3342a5bbfcd7Bob Wilson/// block object types.
4350dc8dab6fabf4bfd4f4b94bf572ac3342a5bbfcd7Bob Wilsonvoid ASTContext::getObjCEncodingForMethodParameter(Decl::ObjCDeclQualifier QT,
4351dc8dab6fabf4bfd4f4b94bf572ac3342a5bbfcd7Bob Wilson                                                   QualType T, std::string& S,
4352dc8dab6fabf4bfd4f4b94bf572ac3342a5bbfcd7Bob Wilson                                                   bool Extended) const {
4353dc8dab6fabf4bfd4f4b94bf572ac3342a5bbfcd7Bob Wilson  // Encode type qualifer, 'in', 'inout', etc. for the parameter.
4354dc8dab6fabf4bfd4f4b94bf572ac3342a5bbfcd7Bob Wilson  getObjCEncodingForTypeQualifier(QT, S);
4355dc8dab6fabf4bfd4f4b94bf572ac3342a5bbfcd7Bob Wilson  // Encode parameter type.
4356dc8dab6fabf4bfd4f4b94bf572ac3342a5bbfcd7Bob Wilson  getObjCEncodingForTypeImpl(T, S, true, true, 0,
4357dc8dab6fabf4bfd4f4b94bf572ac3342a5bbfcd7Bob Wilson                             true     /*OutermostType*/,
4358dc8dab6fabf4bfd4f4b94bf572ac3342a5bbfcd7Bob Wilson                             false    /*EncodingProperty*/,
4359dc8dab6fabf4bfd4f4b94bf572ac3342a5bbfcd7Bob Wilson                             false    /*StructField*/,
4360dc8dab6fabf4bfd4f4b94bf572ac3342a5bbfcd7Bob Wilson                             Extended /*EncodeBlockParameters*/,
4361dc8dab6fabf4bfd4f4b94bf572ac3342a5bbfcd7Bob Wilson                             Extended /*EncodeClassNames*/);
4362dc8dab6fabf4bfd4f4b94bf572ac3342a5bbfcd7Bob Wilson}
4363dc8dab6fabf4bfd4f4b94bf572ac3342a5bbfcd7Bob Wilson
4364a526c5c67e5a0473c340903ee542ce570119665fTed Kremenek/// getObjCEncodingForMethodDecl - Return the encoded type for this method
436533e1d64ab5cd5d27f8530ccd056191fe2c9f3f2eFariborz Jahanian/// declaration.
4366f968d8374791c37bc464efd9168c2d33dd73605fDouglas Gregorbool ASTContext::getObjCEncodingForMethodDecl(const ObjCMethodDecl *Decl,
4367dc8dab6fabf4bfd4f4b94bf572ac3342a5bbfcd7Bob Wilson                                              std::string& S,
4368dc8dab6fabf4bfd4f4b94bf572ac3342a5bbfcd7Bob Wilson                                              bool Extended) const {
4369c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar  // FIXME: This is not very efficient.
4370dc8dab6fabf4bfd4f4b94bf572ac3342a5bbfcd7Bob Wilson  // Encode return type.
4371dc8dab6fabf4bfd4f4b94bf572ac3342a5bbfcd7Bob Wilson  getObjCEncodingForMethodParameter(Decl->getObjCDeclQualifier(),
4372dc8dab6fabf4bfd4f4b94bf572ac3342a5bbfcd7Bob Wilson                                    Decl->getResultType(), S, Extended);
437333e1d64ab5cd5d27f8530ccd056191fe2c9f3f2eFariborz Jahanian  // Compute size of all parameters.
437433e1d64ab5cd5d27f8530ccd056191fe2c9f3f2eFariborz Jahanian  // Start with computing size of a pointer in number of bytes.
437533e1d64ab5cd5d27f8530ccd056191fe2c9f3f2eFariborz Jahanian  // FIXME: There might(should) be a better way of doing this computation!
437633e1d64ab5cd5d27f8530ccd056191fe2c9f3f2eFariborz Jahanian  SourceLocation Loc;
4377199c3d6cd16aebbb9c7f0d42af9d922c9628bf70Ken Dyck  CharUnits PtrSize = getTypeSizeInChars(VoidPtrTy);
437833e1d64ab5cd5d27f8530ccd056191fe2c9f3f2eFariborz Jahanian  // The first two arguments (self and _cmd) are pointers; account for
437933e1d64ab5cd5d27f8530ccd056191fe2c9f3f2eFariborz Jahanian  // their size.
4380199c3d6cd16aebbb9c7f0d42af9d922c9628bf70Ken Dyck  CharUnits ParmOffset = 2 * PtrSize;
4381491306a83c4f0f49f95a3bcbca8580cb98a91c7aArgyrios Kyrtzidis  for (ObjCMethodDecl::param_const_iterator PI = Decl->param_begin(),
43827732cc9c0fdc97a2f8cce4e5933d8103213d1aefFariborz Jahanian       E = Decl->sel_param_end(); PI != E; ++PI) {
438389951a86b594513c2a013532ed45d197413b1087Chris Lattner    QualType PType = (*PI)->getType();
4384aa8741a1db98eef05f09b1200dba94aa5dc3bc3dKen Dyck    CharUnits sz = getObjCEncodingTypeSize(PType);
4385f968d8374791c37bc464efd9168c2d33dd73605fDouglas Gregor    if (sz.isZero())
43867e68ba5660a9b2b854df4ae7a556e6b91d738c6cFariborz Jahanian      continue;
43877e68ba5660a9b2b854df4ae7a556e6b91d738c6cFariborz Jahanian
4388199c3d6cd16aebbb9c7f0d42af9d922c9628bf70Ken Dyck    assert (sz.isPositive() &&
4389199c3d6cd16aebbb9c7f0d42af9d922c9628bf70Ken Dyck        "getObjCEncodingForMethodDecl - Incomplete param type");
439033e1d64ab5cd5d27f8530ccd056191fe2c9f3f2eFariborz Jahanian    ParmOffset += sz;
439133e1d64ab5cd5d27f8530ccd056191fe2c9f3f2eFariborz Jahanian  }
4392199c3d6cd16aebbb9c7f0d42af9d922c9628bf70Ken Dyck  S += charUnitsToString(ParmOffset);
439333e1d64ab5cd5d27f8530ccd056191fe2c9f3f2eFariborz Jahanian  S += "@0:";
4394199c3d6cd16aebbb9c7f0d42af9d922c9628bf70Ken Dyck  S += charUnitsToString(PtrSize);
43951eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
439633e1d64ab5cd5d27f8530ccd056191fe2c9f3f2eFariborz Jahanian  // Argument types.
439733e1d64ab5cd5d27f8530ccd056191fe2c9f3f2eFariborz Jahanian  ParmOffset = 2 * PtrSize;
4398491306a83c4f0f49f95a3bcbca8580cb98a91c7aArgyrios Kyrtzidis  for (ObjCMethodDecl::param_const_iterator PI = Decl->param_begin(),
43997732cc9c0fdc97a2f8cce4e5933d8103213d1aefFariborz Jahanian       E = Decl->sel_param_end(); PI != E; ++PI) {
4400491306a83c4f0f49f95a3bcbca8580cb98a91c7aArgyrios Kyrtzidis    const ParmVarDecl *PVDecl = *PI;
44011eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    QualType PType = PVDecl->getOriginalType();
44024306d3cb9116605728252e2738df24b9f6ab53c3Fariborz Jahanian    if (const ArrayType *AT =
4403ab76d45e023fc5ae966968344e180cd09fdcc746Steve Naroff          dyn_cast<ArrayType>(PType->getCanonicalTypeInternal())) {
4404ab76d45e023fc5ae966968344e180cd09fdcc746Steve Naroff      // Use array's original type only if it has known number of
4405ab76d45e023fc5ae966968344e180cd09fdcc746Steve Naroff      // elements.
4406bb3fde337fb712c0e6da8790d431621be4793048Steve Naroff      if (!isa<ConstantArrayType>(AT))
4407ab76d45e023fc5ae966968344e180cd09fdcc746Steve Naroff        PType = PVDecl->getType();
4408ab76d45e023fc5ae966968344e180cd09fdcc746Steve Naroff    } else if (PType->isFunctionType())
4409ab76d45e023fc5ae966968344e180cd09fdcc746Steve Naroff      PType = PVDecl->getType();
4410dc8dab6fabf4bfd4f4b94bf572ac3342a5bbfcd7Bob Wilson    getObjCEncodingForMethodParameter(PVDecl->getObjCDeclQualifier(),
4411dc8dab6fabf4bfd4f4b94bf572ac3342a5bbfcd7Bob Wilson                                      PType, S, Extended);
4412199c3d6cd16aebbb9c7f0d42af9d922c9628bf70Ken Dyck    S += charUnitsToString(ParmOffset);
4413aa8741a1db98eef05f09b1200dba94aa5dc3bc3dKen Dyck    ParmOffset += getObjCEncodingTypeSize(PType);
441433e1d64ab5cd5d27f8530ccd056191fe2c9f3f2eFariborz Jahanian  }
4415f968d8374791c37bc464efd9168c2d33dd73605fDouglas Gregor
4416f968d8374791c37bc464efd9168c2d33dd73605fDouglas Gregor  return false;
441733e1d64ab5cd5d27f8530ccd056191fe2c9f3f2eFariborz Jahanian}
441833e1d64ab5cd5d27f8530ccd056191fe2c9f3f2eFariborz Jahanian
4419c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar/// getObjCEncodingForPropertyDecl - Return the encoded type for this
442083bccb85ff4b9981c4250c45494b439df8cbf983Fariborz Jahanian/// property declaration. If non-NULL, Container must be either an
4421c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar/// ObjCCategoryImplDecl or ObjCImplementationDecl; it should only be
4422c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar/// NULL when getting encodings for protocol properties.
44231eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// Property attributes are stored as a comma-delimited C string. The simple
44241eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// attributes readonly and bycopy are encoded as single characters. The
44251eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// parametrized attributes, getter=name, setter=name, and ivar=name, are
44261eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// encoded as single characters, followed by an identifier. Property types
44271eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// are also encoded as a parametrized attribute. The characters used to encode
442883bccb85ff4b9981c4250c45494b439df8cbf983Fariborz Jahanian/// these attributes are defined by the following enumeration:
442983bccb85ff4b9981c4250c45494b439df8cbf983Fariborz Jahanian/// @code
443083bccb85ff4b9981c4250c45494b439df8cbf983Fariborz Jahanian/// enum PropertyAttributes {
443183bccb85ff4b9981c4250c45494b439df8cbf983Fariborz Jahanian/// kPropertyReadOnly = 'R',   // property is read-only.
443283bccb85ff4b9981c4250c45494b439df8cbf983Fariborz Jahanian/// kPropertyBycopy = 'C',     // property is a copy of the value last assigned
443383bccb85ff4b9981c4250c45494b439df8cbf983Fariborz Jahanian/// kPropertyByref = '&',  // property is a reference to the value last assigned
443483bccb85ff4b9981c4250c45494b439df8cbf983Fariborz Jahanian/// kPropertyDynamic = 'D',    // property is dynamic
443583bccb85ff4b9981c4250c45494b439df8cbf983Fariborz Jahanian/// kPropertyGetter = 'G',     // followed by getter selector name
443683bccb85ff4b9981c4250c45494b439df8cbf983Fariborz Jahanian/// kPropertySetter = 'S',     // followed by setter selector name
443783bccb85ff4b9981c4250c45494b439df8cbf983Fariborz Jahanian/// kPropertyInstanceVariable = 'V'  // followed by instance variable  name
44380d4cb85130d91da61c45aecb9fd31c7097a7cfccBob Wilson/// kPropertyType = 'T'              // followed by old-style type encoding.
443983bccb85ff4b9981c4250c45494b439df8cbf983Fariborz Jahanian/// kPropertyWeak = 'W'              // 'weak' property
444083bccb85ff4b9981c4250c45494b439df8cbf983Fariborz Jahanian/// kPropertyStrong = 'P'            // property GC'able
444183bccb85ff4b9981c4250c45494b439df8cbf983Fariborz Jahanian/// kPropertyNonAtomic = 'N'         // property non-atomic
444283bccb85ff4b9981c4250c45494b439df8cbf983Fariborz Jahanian/// };
444383bccb85ff4b9981c4250c45494b439df8cbf983Fariborz Jahanian/// @endcode
44441eb4433ac451dc16f4133a88af2d002ac26c58efMike Stumpvoid ASTContext::getObjCEncodingForPropertyDecl(const ObjCPropertyDecl *PD,
4445c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar                                                const Decl *Container,
44464ba2a17694148e16eaa8d3917f657ffcd3667be4Jay Foad                                                std::string& S) const {
4447c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar  // Collect information from the property implementation decl(s).
4448c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar  bool Dynamic = false;
4449c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar  ObjCPropertyImplDecl *SynthesizePID = 0;
4450c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar
4451c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar  // FIXME: Duplicated code due to poor abstraction.
4452c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar  if (Container) {
44531eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    if (const ObjCCategoryImplDecl *CID =
4454c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar        dyn_cast<ObjCCategoryImplDecl>(Container)) {
4455c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar      for (ObjCCategoryImplDecl::propimpl_iterator
445617945a0f64fe03ff6ec0c2146005a87636e3ac12Argyrios Kyrtzidis             i = CID->propimpl_begin(), e = CID->propimpl_end();
4457653f1b1bf293a9bd96fd4dd6372e779cc7af1597Douglas Gregor           i != e; ++i) {
4458581deb3da481053c4993c7600f97acf7768caac5David Blaikie        ObjCPropertyImplDecl *PID = *i;
4459c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar        if (PID->getPropertyDecl() == PD) {
4460c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar          if (PID->getPropertyImplementation()==ObjCPropertyImplDecl::Dynamic) {
4461c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar            Dynamic = true;
4462c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar          } else {
4463c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar            SynthesizePID = PID;
4464c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar          }
4465c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar        }
4466c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar      }
4467c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar    } else {
446861710854be2b098428aff5316e64bd34b30fbcb7Chris Lattner      const ObjCImplementationDecl *OID=cast<ObjCImplementationDecl>(Container);
4469c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar      for (ObjCCategoryImplDecl::propimpl_iterator
447017945a0f64fe03ff6ec0c2146005a87636e3ac12Argyrios Kyrtzidis             i = OID->propimpl_begin(), e = OID->propimpl_end();
4471653f1b1bf293a9bd96fd4dd6372e779cc7af1597Douglas Gregor           i != e; ++i) {
4472581deb3da481053c4993c7600f97acf7768caac5David Blaikie        ObjCPropertyImplDecl *PID = *i;
4473c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar        if (PID->getPropertyDecl() == PD) {
4474c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar          if (PID->getPropertyImplementation()==ObjCPropertyImplDecl::Dynamic) {
4475c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar            Dynamic = true;
4476c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar          } else {
4477c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar            SynthesizePID = PID;
4478c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar          }
4479c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar        }
44801eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      }
4481c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar    }
4482c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar  }
4483c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar
4484c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar  // FIXME: This is not very efficient.
4485c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar  S = "T";
4486c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar
4487c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar  // Encode result type.
4488090b3f71702c5626d8520f9608d77c6f26dcfa15Fariborz Jahanian  // GCC has some special rules regarding encoding of properties which
4489090b3f71702c5626d8520f9608d77c6f26dcfa15Fariborz Jahanian  // closely resembles encoding of ivars.
44901eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  getObjCEncodingForTypeImpl(PD->getType(), S, true, true, 0,
4491090b3f71702c5626d8520f9608d77c6f26dcfa15Fariborz Jahanian                             true /* outermost type */,
4492090b3f71702c5626d8520f9608d77c6f26dcfa15Fariborz Jahanian                             true /* encoding for property */);
4493c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar
4494c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar  if (PD->isReadOnly()) {
4495c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar    S += ",R";
4496c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar  } else {
4497c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar    switch (PD->getSetterKind()) {
4498c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar    case ObjCPropertyDecl::Assign: break;
4499c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar    case ObjCPropertyDecl::Copy:   S += ",C"; break;
45001eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    case ObjCPropertyDecl::Retain: S += ",&"; break;
45013a02b44e3948f7762dbfba94b7961281ca29d022Fariborz Jahanian    case ObjCPropertyDecl::Weak:   S += ",W"; break;
4502c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar    }
4503c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar  }
4504c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar
4505c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar  // It really isn't clear at all what this means, since properties
4506c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar  // are "dynamic by default".
4507c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar  if (Dynamic)
4508c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar    S += ",D";
4509c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar
4510090b3f71702c5626d8520f9608d77c6f26dcfa15Fariborz Jahanian  if (PD->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_nonatomic)
4511090b3f71702c5626d8520f9608d77c6f26dcfa15Fariborz Jahanian    S += ",N";
45121eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
4513c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar  if (PD->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_getter) {
4514c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar    S += ",G";
4515077bf5e2f48acfa9e7d69429b6e4ba86ea14896dChris Lattner    S += PD->getGetterName().getAsString();
4516c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar  }
4517c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar
4518c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar  if (PD->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_setter) {
4519c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar    S += ",S";
4520077bf5e2f48acfa9e7d69429b6e4ba86ea14896dChris Lattner    S += PD->getSetterName().getAsString();
4521c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar  }
4522c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar
4523c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar  if (SynthesizePID) {
4524c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar    const ObjCIvarDecl *OID = SynthesizePID->getPropertyIvarDecl();
4525c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar    S += ",V";
452639f34e97d6a468f0a7dfa5664c61217cffc65b74Chris Lattner    S += OID->getNameAsString();
4527c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar  }
4528c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar
4529c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar  // FIXME: OBJCGC: weak & strong
4530c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar}
4531c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar
4532a1c033e9514865f3a7b0d8b3b20e6de926cfec6cFariborz Jahanian/// getLegacyIntegralTypeEncoding -
45331eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// Another legacy compatibility encoding: 32-bit longs are encoded as
45341eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// 'l' or 'L' , but not always.  For typedefs, we need to use
4535a1c033e9514865f3a7b0d8b3b20e6de926cfec6cFariborz Jahanian/// 'i' or 'I' instead if encoding a struct field, or a pointer!
4536a1c033e9514865f3a7b0d8b3b20e6de926cfec6cFariborz Jahanian///
4537a1c033e9514865f3a7b0d8b3b20e6de926cfec6cFariborz Jahanianvoid ASTContext::getLegacyIntegralTypeEncoding (QualType &PointeeTy) const {
45388e1fab243ab8023b7ee3899745386b3b3a4258f8Mike Stump  if (isa<TypedefType>(PointeeTy.getTypePtr())) {
4539183700f494ec9b6701b6efe82bcb25f4c79ba561John McCall    if (const BuiltinType *BT = PointeeTy->getAs<BuiltinType>()) {
45404ba2a17694148e16eaa8d3917f657ffcd3667be4Jay Foad      if (BT->getKind() == BuiltinType::ULong && getIntWidth(PointeeTy) == 32)
4541a1c033e9514865f3a7b0d8b3b20e6de926cfec6cFariborz Jahanian        PointeeTy = UnsignedIntTy;
45421eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      else
45434ba2a17694148e16eaa8d3917f657ffcd3667be4Jay Foad        if (BT->getKind() == BuiltinType::Long && getIntWidth(PointeeTy) == 32)
4544a1c033e9514865f3a7b0d8b3b20e6de926cfec6cFariborz Jahanian          PointeeTy = IntTy;
4545a1c033e9514865f3a7b0d8b3b20e6de926cfec6cFariborz Jahanian    }
4546a1c033e9514865f3a7b0d8b3b20e6de926cfec6cFariborz Jahanian  }
4547a1c033e9514865f3a7b0d8b3b20e6de926cfec6cFariborz Jahanian}
4548a1c033e9514865f3a7b0d8b3b20e6de926cfec6cFariborz Jahanian
45497d6b46d9a9d75dea8ef9f6973dd50633c1f37963Fariborz Jahanianvoid ASTContext::getObjCEncodingForType(QualType T, std::string& S,
45504ba2a17694148e16eaa8d3917f657ffcd3667be4Jay Foad                                        const FieldDecl *Field) const {
455182a6cfbc421cc99c5b7313271f399f7ef95056ecDaniel Dunbar  // We follow the behavior of gcc, expanding structures which are
455282a6cfbc421cc99c5b7313271f399f7ef95056ecDaniel Dunbar  // directly pointed to, and expanding embedded structures. Note that
455382a6cfbc421cc99c5b7313271f399f7ef95056ecDaniel Dunbar  // these rules are sufficient to prevent recursive encoding of the
455482a6cfbc421cc99c5b7313271f399f7ef95056ecDaniel Dunbar  // same type.
45551eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  getObjCEncodingForTypeImpl(T, S, true, true, Field,
45565b8c7d9fb620ba3a71e996d61e7b9bdf763b5c09Fariborz Jahanian                             true /* outermost type */);
455782a6cfbc421cc99c5b7313271f399f7ef95056ecDaniel Dunbar}
455882a6cfbc421cc99c5b7313271f399f7ef95056ecDaniel Dunbar
455964fd7e86c1a90d9ff78e4a0bd79f69499667a4e3David Chisnallstatic char ObjCEncodingForPrimitiveKind(const ASTContext *C, QualType T) {
456064fd7e86c1a90d9ff78e4a0bd79f69499667a4e3David Chisnall    switch (T->getAs<BuiltinType>()->getKind()) {
4561b219cfc4d75f0a03630b7c4509ef791b7e97b2c8David Blaikie    default: llvm_unreachable("Unhandled builtin type kind");
456264fd7e86c1a90d9ff78e4a0bd79f69499667a4e3David Chisnall    case BuiltinType::Void:       return 'v';
456364fd7e86c1a90d9ff78e4a0bd79f69499667a4e3David Chisnall    case BuiltinType::Bool:       return 'B';
456464fd7e86c1a90d9ff78e4a0bd79f69499667a4e3David Chisnall    case BuiltinType::Char_U:
456564fd7e86c1a90d9ff78e4a0bd79f69499667a4e3David Chisnall    case BuiltinType::UChar:      return 'C';
456664fd7e86c1a90d9ff78e4a0bd79f69499667a4e3David Chisnall    case BuiltinType::UShort:     return 'S';
456764fd7e86c1a90d9ff78e4a0bd79f69499667a4e3David Chisnall    case BuiltinType::UInt:       return 'I';
456864fd7e86c1a90d9ff78e4a0bd79f69499667a4e3David Chisnall    case BuiltinType::ULong:
45694ba2a17694148e16eaa8d3917f657ffcd3667be4Jay Foad        return C->getIntWidth(T) == 32 ? 'L' : 'Q';
457064fd7e86c1a90d9ff78e4a0bd79f69499667a4e3David Chisnall    case BuiltinType::UInt128:    return 'T';
457164fd7e86c1a90d9ff78e4a0bd79f69499667a4e3David Chisnall    case BuiltinType::ULongLong:  return 'Q';
457264fd7e86c1a90d9ff78e4a0bd79f69499667a4e3David Chisnall    case BuiltinType::Char_S:
457364fd7e86c1a90d9ff78e4a0bd79f69499667a4e3David Chisnall    case BuiltinType::SChar:      return 'c';
457464fd7e86c1a90d9ff78e4a0bd79f69499667a4e3David Chisnall    case BuiltinType::Short:      return 's';
45753f59c975aa5d047f7edd1b900b5e885c38af0ef7Chris Lattner    case BuiltinType::WChar_S:
45763f59c975aa5d047f7edd1b900b5e885c38af0ef7Chris Lattner    case BuiltinType::WChar_U:
457764fd7e86c1a90d9ff78e4a0bd79f69499667a4e3David Chisnall    case BuiltinType::Int:        return 'i';
457864fd7e86c1a90d9ff78e4a0bd79f69499667a4e3David Chisnall    case BuiltinType::Long:
45794ba2a17694148e16eaa8d3917f657ffcd3667be4Jay Foad      return C->getIntWidth(T) == 32 ? 'l' : 'q';
458064fd7e86c1a90d9ff78e4a0bd79f69499667a4e3David Chisnall    case BuiltinType::LongLong:   return 'q';
458164fd7e86c1a90d9ff78e4a0bd79f69499667a4e3David Chisnall    case BuiltinType::Int128:     return 't';
458264fd7e86c1a90d9ff78e4a0bd79f69499667a4e3David Chisnall    case BuiltinType::Float:      return 'f';
458364fd7e86c1a90d9ff78e4a0bd79f69499667a4e3David Chisnall    case BuiltinType::Double:     return 'd';
45843a0be84b2aed8563150cdbd976a98838afa261ebDaniel Dunbar    case BuiltinType::LongDouble: return 'D';
458564fd7e86c1a90d9ff78e4a0bd79f69499667a4e3David Chisnall    }
458664fd7e86c1a90d9ff78e4a0bd79f69499667a4e3David Chisnall}
458764fd7e86c1a90d9ff78e4a0bd79f69499667a4e3David Chisnall
45885471bc85b69912e3b448de004498a80c0de32296Douglas Gregorstatic char ObjCEncodingForEnumType(const ASTContext *C, const EnumType *ET) {
45895471bc85b69912e3b448de004498a80c0de32296Douglas Gregor  EnumDecl *Enum = ET->getDecl();
45905471bc85b69912e3b448de004498a80c0de32296Douglas Gregor
45915471bc85b69912e3b448de004498a80c0de32296Douglas Gregor  // The encoding of an non-fixed enum type is always 'i', regardless of size.
45925471bc85b69912e3b448de004498a80c0de32296Douglas Gregor  if (!Enum->isFixed())
45935471bc85b69912e3b448de004498a80c0de32296Douglas Gregor    return 'i';
45945471bc85b69912e3b448de004498a80c0de32296Douglas Gregor
45955471bc85b69912e3b448de004498a80c0de32296Douglas Gregor  // The encoding of a fixed enum type matches its fixed underlying type.
45965471bc85b69912e3b448de004498a80c0de32296Douglas Gregor  return ObjCEncodingForPrimitiveKind(C, Enum->getIntegerType());
45975471bc85b69912e3b448de004498a80c0de32296Douglas Gregor}
45985471bc85b69912e3b448de004498a80c0de32296Douglas Gregor
45994ba2a17694148e16eaa8d3917f657ffcd3667be4Jay Foadstatic void EncodeBitField(const ASTContext *Ctx, std::string& S,
460064fd7e86c1a90d9ff78e4a0bd79f69499667a4e3David Chisnall                           QualType T, const FieldDecl *FD) {
4601a6b8b2c09610b8bc4330e948ece8b940c2386406Richard Smith  assert(FD->isBitField() && "not a bitfield - getObjCEncodingForTypeImpl");
46028b4bf90eb6d3d08cf3bfb86705f0fdb20b9c5875Fariborz Jahanian  S += 'b';
460364fd7e86c1a90d9ff78e4a0bd79f69499667a4e3David Chisnall  // The NeXT runtime encodes bit fields as b followed by the number of bits.
460464fd7e86c1a90d9ff78e4a0bd79f69499667a4e3David Chisnall  // The GNU runtime requires more information; bitfields are encoded as b,
460564fd7e86c1a90d9ff78e4a0bd79f69499667a4e3David Chisnall  // then the offset (in bits) of the first element, then the type of the
460664fd7e86c1a90d9ff78e4a0bd79f69499667a4e3David Chisnall  // bitfield, then the size in bits.  For example, in this structure:
460764fd7e86c1a90d9ff78e4a0bd79f69499667a4e3David Chisnall  //
460864fd7e86c1a90d9ff78e4a0bd79f69499667a4e3David Chisnall  // struct
460964fd7e86c1a90d9ff78e4a0bd79f69499667a4e3David Chisnall  // {
461064fd7e86c1a90d9ff78e4a0bd79f69499667a4e3David Chisnall  //    int integer;
461164fd7e86c1a90d9ff78e4a0bd79f69499667a4e3David Chisnall  //    int flags:2;
461264fd7e86c1a90d9ff78e4a0bd79f69499667a4e3David Chisnall  // };
461364fd7e86c1a90d9ff78e4a0bd79f69499667a4e3David Chisnall  // On a 32-bit system, the encoding for flags would be b2 for the NeXT
461464fd7e86c1a90d9ff78e4a0bd79f69499667a4e3David Chisnall  // runtime, but b32i2 for the GNU runtime.  The reason for this extra
461564fd7e86c1a90d9ff78e4a0bd79f69499667a4e3David Chisnall  // information is not especially sensible, but we're stuck with it for
461664fd7e86c1a90d9ff78e4a0bd79f69499667a4e3David Chisnall  // compatibility with GCC, although providing it breaks anything that
461764fd7e86c1a90d9ff78e4a0bd79f69499667a4e3David Chisnall  // actually uses runtime introspection and wants to work on both runtimes...
4618260611a32535c851237926bfcf78869b13c07d5bJohn McCall  if (Ctx->getLangOpts().ObjCRuntime.isGNUFamily()) {
461964fd7e86c1a90d9ff78e4a0bd79f69499667a4e3David Chisnall    const RecordDecl *RD = FD->getParent();
462064fd7e86c1a90d9ff78e4a0bd79f69499667a4e3David Chisnall    const ASTRecordLayout &RL = Ctx->getASTRecordLayout(RD);
462182905749d5c8d8b4edec11de754a73349cb96603Eli Friedman    S += llvm::utostr(RL.getFieldOffset(FD->getFieldIndex()));
46225471bc85b69912e3b448de004498a80c0de32296Douglas Gregor    if (const EnumType *ET = T->getAs<EnumType>())
46235471bc85b69912e3b448de004498a80c0de32296Douglas Gregor      S += ObjCEncodingForEnumType(Ctx, ET);
4624c7ff82c2040f45eaad2eddea0e4461dddc972cd1David Chisnall    else
46254ba2a17694148e16eaa8d3917f657ffcd3667be4Jay Foad      S += ObjCEncodingForPrimitiveKind(Ctx, T);
462664fd7e86c1a90d9ff78e4a0bd79f69499667a4e3David Chisnall  }
4627a6b8b2c09610b8bc4330e948ece8b940c2386406Richard Smith  S += llvm::utostr(FD->getBitWidthValue(*Ctx));
46288b4bf90eb6d3d08cf3bfb86705f0fdb20b9c5875Fariborz Jahanian}
46298b4bf90eb6d3d08cf3bfb86705f0fdb20b9c5875Fariborz Jahanian
463001eb9b9683535d8a65c704ad2c545903409e2d36Daniel Dunbar// FIXME: Use SmallString for accumulating string.
463182a6cfbc421cc99c5b7313271f399f7ef95056ecDaniel Dunbarvoid ASTContext::getObjCEncodingForTypeImpl(QualType T, std::string& S,
463282a6cfbc421cc99c5b7313271f399f7ef95056ecDaniel Dunbar                                            bool ExpandPointedToStructures,
463382a6cfbc421cc99c5b7313271f399f7ef95056ecDaniel Dunbar                                            bool ExpandStructures,
4634153bfe5795e2c1a5a738e73d3784964e082237fcDaniel Dunbar                                            const FieldDecl *FD,
4635090b3f71702c5626d8520f9608d77c6f26dcfa15Fariborz Jahanian                                            bool OutermostType,
46362636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis                                            bool EncodingProperty,
4637dc8dab6fabf4bfd4f4b94bf572ac3342a5bbfcd7Bob Wilson                                            bool StructField,
4638dc8dab6fabf4bfd4f4b94bf572ac3342a5bbfcd7Bob Wilson                                            bool EncodeBlockParameters,
4639dc8dab6fabf4bfd4f4b94bf572ac3342a5bbfcd7Bob Wilson                                            bool EncodeClassNames) const {
464064fd7e86c1a90d9ff78e4a0bd79f69499667a4e3David Chisnall  if (T->getAs<BuiltinType>()) {
4641ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner    if (FD && FD->isBitField())
464264fd7e86c1a90d9ff78e4a0bd79f69499667a4e3David Chisnall      return EncodeBitField(this, S, T, FD);
464364fd7e86c1a90d9ff78e4a0bd79f69499667a4e3David Chisnall    S += ObjCEncodingForPrimitiveKind(this, T);
4644ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner    return;
4645ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner  }
46461eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
4647183700f494ec9b6701b6efe82bcb25f4c79ba561John McCall  if (const ComplexType *CT = T->getAs<ComplexType>()) {
4648c612f7bc9a6379cd7e7c2dd306d05938e890051bAnders Carlsson    S += 'j';
46491eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    getObjCEncodingForTypeImpl(CT->getElementType(), S, false, false, 0, false,
4650c612f7bc9a6379cd7e7c2dd306d05938e890051bAnders Carlsson                               false);
4651ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner    return;
4652ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner  }
465360bce3ef20a4c9684e3825cdd739fefb9810327dFariborz Jahanian
4654aa1d76163e4b0b1cc54e222be67379f8c02e8ffaFariborz Jahanian  // encoding for pointer or r3eference types.
4655aa1d76163e4b0b1cc54e222be67379f8c02e8ffaFariborz Jahanian  QualType PointeeTy;
46566217b80b7a1379b74cced1c076338262c3c980b3Ted Kremenek  if (const PointerType *PT = T->getAs<PointerType>()) {
46578d2c0a9814e56c2b22e22d1045181c735aef62fdFariborz Jahanian    if (PT->isObjCSelType()) {
46588d2c0a9814e56c2b22e22d1045181c735aef62fdFariborz Jahanian      S += ':';
46598d2c0a9814e56c2b22e22d1045181c735aef62fdFariborz Jahanian      return;
46608d2c0a9814e56c2b22e22d1045181c735aef62fdFariborz Jahanian    }
4661aa1d76163e4b0b1cc54e222be67379f8c02e8ffaFariborz Jahanian    PointeeTy = PT->getPointeeType();
4662aa1d76163e4b0b1cc54e222be67379f8c02e8ffaFariborz Jahanian  }
4663aa1d76163e4b0b1cc54e222be67379f8c02e8ffaFariborz Jahanian  else if (const ReferenceType *RT = T->getAs<ReferenceType>())
4664aa1d76163e4b0b1cc54e222be67379f8c02e8ffaFariborz Jahanian    PointeeTy = RT->getPointeeType();
4665aa1d76163e4b0b1cc54e222be67379f8c02e8ffaFariborz Jahanian  if (!PointeeTy.isNull()) {
4666a1c033e9514865f3a7b0d8b3b20e6de926cfec6cFariborz Jahanian    bool isReadOnly = false;
4667a1c033e9514865f3a7b0d8b3b20e6de926cfec6cFariborz Jahanian    // For historical/compatibility reasons, the read-only qualifier of the
4668a1c033e9514865f3a7b0d8b3b20e6de926cfec6cFariborz Jahanian    // pointee gets emitted _before_ the '^'.  The read-only qualifier of
4669a1c033e9514865f3a7b0d8b3b20e6de926cfec6cFariborz Jahanian    // the pointer itself gets ignored, _unless_ we are looking at a typedef!
46701eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    // Also, do not emit the 'r' for anything but the outermost type!
46718e1fab243ab8023b7ee3899745386b3b3a4258f8Mike Stump    if (isa<TypedefType>(T.getTypePtr())) {
4672a1c033e9514865f3a7b0d8b3b20e6de926cfec6cFariborz Jahanian      if (OutermostType && T.isConstQualified()) {
4673a1c033e9514865f3a7b0d8b3b20e6de926cfec6cFariborz Jahanian        isReadOnly = true;
4674a1c033e9514865f3a7b0d8b3b20e6de926cfec6cFariborz Jahanian        S += 'r';
4675a1c033e9514865f3a7b0d8b3b20e6de926cfec6cFariborz Jahanian      }
46769fdbab3cbc2fc04bcaf5768023d83707f3151144Mike Stump    } else if (OutermostType) {
4677a1c033e9514865f3a7b0d8b3b20e6de926cfec6cFariborz Jahanian      QualType P = PointeeTy;
46786217b80b7a1379b74cced1c076338262c3c980b3Ted Kremenek      while (P->getAs<PointerType>())
46796217b80b7a1379b74cced1c076338262c3c980b3Ted Kremenek        P = P->getAs<PointerType>()->getPointeeType();
4680a1c033e9514865f3a7b0d8b3b20e6de926cfec6cFariborz Jahanian      if (P.isConstQualified()) {
4681a1c033e9514865f3a7b0d8b3b20e6de926cfec6cFariborz Jahanian        isReadOnly = true;
4682a1c033e9514865f3a7b0d8b3b20e6de926cfec6cFariborz Jahanian        S += 'r';
4683a1c033e9514865f3a7b0d8b3b20e6de926cfec6cFariborz Jahanian      }
4684a1c033e9514865f3a7b0d8b3b20e6de926cfec6cFariborz Jahanian    }
4685a1c033e9514865f3a7b0d8b3b20e6de926cfec6cFariborz Jahanian    if (isReadOnly) {
4686a1c033e9514865f3a7b0d8b3b20e6de926cfec6cFariborz Jahanian      // Another legacy compatibility encoding. Some ObjC qualifier and type
4687a1c033e9514865f3a7b0d8b3b20e6de926cfec6cFariborz Jahanian      // combinations need to be rearranged.
4688a1c033e9514865f3a7b0d8b3b20e6de926cfec6cFariborz Jahanian      // Rewrite "in const" from "nr" to "rn"
46895f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner      if (StringRef(S).endswith("nr"))
46900237941e0beb0c929934b66ad29443b484d987feBenjamin Kramer        S.replace(S.end()-2, S.end(), "rn");
4691a1c033e9514865f3a7b0d8b3b20e6de926cfec6cFariborz Jahanian    }
46921eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
469385f9bceab1542aafff012d4d28e998f4ba16e362Anders Carlsson    if (PointeeTy->isCharType()) {
469485f9bceab1542aafff012d4d28e998f4ba16e362Anders Carlsson      // char pointer types should be encoded as '*' unless it is a
469585f9bceab1542aafff012d4d28e998f4ba16e362Anders Carlsson      // type that has been typedef'd to 'BOOL'.
4696e8c49533521c40643653f943d47229e62d277f88Anders Carlsson      if (!isTypeTypedefedAsBOOL(PointeeTy)) {
469785f9bceab1542aafff012d4d28e998f4ba16e362Anders Carlsson        S += '*';
469885f9bceab1542aafff012d4d28e998f4ba16e362Anders Carlsson        return;
469985f9bceab1542aafff012d4d28e998f4ba16e362Anders Carlsson      }
47006217b80b7a1379b74cced1c076338262c3c980b3Ted Kremenek    } else if (const RecordType *RTy = PointeeTy->getAs<RecordType>()) {
47019533a7fdb8397421f3be52e879442460a87389f6Steve Naroff      // GCC binary compat: Need to convert "struct objc_class *" to "#".
47029533a7fdb8397421f3be52e879442460a87389f6Steve Naroff      if (RTy->getDecl()->getIdentifier() == &Idents.get("objc_class")) {
47039533a7fdb8397421f3be52e879442460a87389f6Steve Naroff        S += '#';
47049533a7fdb8397421f3be52e879442460a87389f6Steve Naroff        return;
47059533a7fdb8397421f3be52e879442460a87389f6Steve Naroff      }
47069533a7fdb8397421f3be52e879442460a87389f6Steve Naroff      // GCC binary compat: Need to convert "struct objc_object *" to "@".
47079533a7fdb8397421f3be52e879442460a87389f6Steve Naroff      if (RTy->getDecl()->getIdentifier() == &Idents.get("objc_object")) {
47089533a7fdb8397421f3be52e879442460a87389f6Steve Naroff        S += '@';
47099533a7fdb8397421f3be52e879442460a87389f6Steve Naroff        return;
47109533a7fdb8397421f3be52e879442460a87389f6Steve Naroff      }
47119533a7fdb8397421f3be52e879442460a87389f6Steve Naroff      // fall through...
471285f9bceab1542aafff012d4d28e998f4ba16e362Anders Carlsson    }
471385f9bceab1542aafff012d4d28e998f4ba16e362Anders Carlsson    S += '^';
4714a1c033e9514865f3a7b0d8b3b20e6de926cfec6cFariborz Jahanian    getLegacyIntegralTypeEncoding(PointeeTy);
4715a1c033e9514865f3a7b0d8b3b20e6de926cfec6cFariborz Jahanian
47161eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    getObjCEncodingForTypeImpl(PointeeTy, S, false, ExpandPointedToStructures,
471743822eaeddeaa419b90f23c68af6b23c46788a58Fariborz Jahanian                               NULL);
4718ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner    return;
4719ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner  }
4720aa1d76163e4b0b1cc54e222be67379f8c02e8ffaFariborz Jahanian
4721ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner  if (const ArrayType *AT =
4722ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner      // Ignore type qualifiers etc.
4723ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner        dyn_cast<ArrayType>(T->getCanonicalTypeInternal())) {
47242636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis    if (isa<IncompleteArrayType>(AT) && !StructField) {
4725559a83330416affb0e341a2c53800cbf924a5178Anders Carlsson      // Incomplete arrays are encoded as a pointer to the array element.
4726559a83330416affb0e341a2c53800cbf924a5178Anders Carlsson      S += '^';
4727559a83330416affb0e341a2c53800cbf924a5178Anders Carlsson
47281eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      getObjCEncodingForTypeImpl(AT->getElementType(), S,
4729559a83330416affb0e341a2c53800cbf924a5178Anders Carlsson                                 false, ExpandStructures, FD);
4730559a83330416affb0e341a2c53800cbf924a5178Anders Carlsson    } else {
4731559a83330416affb0e341a2c53800cbf924a5178Anders Carlsson      S += '[';
47321eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
47332636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis      if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(AT)) {
47342636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis        if (getTypeSize(CAT->getElementType()) == 0)
47352636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis          S += '0';
47362636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis        else
47372636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis          S += llvm::utostr(CAT->getSize().getZExtValue());
47382636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis      } else {
4739559a83330416affb0e341a2c53800cbf924a5178Anders Carlsson        //Variable length arrays are encoded as a regular array with 0 elements.
47402636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis        assert((isa<VariableArrayType>(AT) || isa<IncompleteArrayType>(AT)) &&
47412636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis               "Unknown array type!");
4742559a83330416affb0e341a2c53800cbf924a5178Anders Carlsson        S += '0';
4743559a83330416affb0e341a2c53800cbf924a5178Anders Carlsson      }
47441eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
47451eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      getObjCEncodingForTypeImpl(AT->getElementType(), S,
4746559a83330416affb0e341a2c53800cbf924a5178Anders Carlsson                                 false, ExpandStructures, FD);
4747559a83330416affb0e341a2c53800cbf924a5178Anders Carlsson      S += ']';
4748559a83330416affb0e341a2c53800cbf924a5178Anders Carlsson    }
4749ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner    return;
4750ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner  }
47511eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
4752183700f494ec9b6701b6efe82bcb25f4c79ba561John McCall  if (T->getAs<FunctionType>()) {
4753c0a87b7db06643178ad2cbce0767548c139ea387Anders Carlsson    S += '?';
4754ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner    return;
4755ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner  }
47561eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
47576217b80b7a1379b74cced1c076338262c3c980b3Ted Kremenek  if (const RecordType *RTy = T->getAs<RecordType>()) {
475882a6cfbc421cc99c5b7313271f399f7ef95056ecDaniel Dunbar    RecordDecl *RDecl = RTy->getDecl();
4759d96b35bc6becf8db00d140c11e3d0e53f27567a1Daniel Dunbar    S += RDecl->isUnion() ? '(' : '{';
4760502a4a1ce4c34cf78c8182d9798da0a51d9b7302Daniel Dunbar    // Anonymous structures print as '?'
4761502a4a1ce4c34cf78c8182d9798da0a51d9b7302Daniel Dunbar    if (const IdentifierInfo *II = RDecl->getIdentifier()) {
4762502a4a1ce4c34cf78c8182d9798da0a51d9b7302Daniel Dunbar      S += II->getName();
47636fb94391dc7cb11fd4bbdb969bbab11b6b48c223Fariborz Jahanian      if (ClassTemplateSpecializationDecl *Spec
47646fb94391dc7cb11fd4bbdb969bbab11b6b48c223Fariborz Jahanian          = dyn_cast<ClassTemplateSpecializationDecl>(RDecl)) {
47656fb94391dc7cb11fd4bbdb969bbab11b6b48c223Fariborz Jahanian        const TemplateArgumentList &TemplateArgs = Spec->getTemplateArgs();
47666fb94391dc7cb11fd4bbdb969bbab11b6b48c223Fariborz Jahanian        std::string TemplateArgsStr
47676fb94391dc7cb11fd4bbdb969bbab11b6b48c223Fariborz Jahanian          = TemplateSpecializationType::PrintTemplateArgumentList(
4768910f8008fea79120489a53593fe971b0b8a4a740Douglas Gregor                                            TemplateArgs.data(),
4769910f8008fea79120489a53593fe971b0b8a4a740Douglas Gregor                                            TemplateArgs.size(),
477030c42404202d2e2512e51efc6066bd614cfdb5a4Douglas Gregor                                            (*this).getPrintingPolicy());
47716fb94391dc7cb11fd4bbdb969bbab11b6b48c223Fariborz Jahanian
47726fb94391dc7cb11fd4bbdb969bbab11b6b48c223Fariborz Jahanian        S += TemplateArgsStr;
47736fb94391dc7cb11fd4bbdb969bbab11b6b48c223Fariborz Jahanian      }
4774502a4a1ce4c34cf78c8182d9798da0a51d9b7302Daniel Dunbar    } else {
4775502a4a1ce4c34cf78c8182d9798da0a51d9b7302Daniel Dunbar      S += '?';
4776502a4a1ce4c34cf78c8182d9798da0a51d9b7302Daniel Dunbar    }
47770d504c1da852e58ff802545c823ecff3b6c654b8Daniel Dunbar    if (ExpandStructures) {
47787d6b46d9a9d75dea8ef9f6973dd50633c1f37963Fariborz Jahanian      S += '=';
47792636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis      if (!RDecl->isUnion()) {
47802636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis        getObjCEncodingForStructureImpl(RDecl, S, FD);
47812636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis      } else {
47822636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis        for (RecordDecl::field_iterator Field = RDecl->field_begin(),
47832636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis                                     FieldEnd = RDecl->field_end();
47842636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis             Field != FieldEnd; ++Field) {
47852636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis          if (FD) {
47862636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis            S += '"';
47872636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis            S += Field->getNameAsString();
47882636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis            S += '"';
47892636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis          }
47901eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
47912636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis          // Special case bit-fields.
47922636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis          if (Field->isBitField()) {
47932636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis            getObjCEncodingForTypeImpl(Field->getType(), S, false, true,
4794581deb3da481053c4993c7600f97acf7768caac5David Blaikie                                       *Field);
47952636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis          } else {
47962636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis            QualType qt = Field->getType();
47972636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis            getLegacyIntegralTypeEncoding(qt);
47982636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis            getObjCEncodingForTypeImpl(qt, S, false, true,
47992636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis                                       FD, /*OutermostType*/false,
48002636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis                                       /*EncodingProperty*/false,
48012636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis                                       /*StructField*/true);
48022636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis          }
4803d96b35bc6becf8db00d140c11e3d0e53f27567a1Daniel Dunbar        }
48047d6b46d9a9d75dea8ef9f6973dd50633c1f37963Fariborz Jahanian      }
48056de88a873a4cbe06d72602eef57d68006730a80bFariborz Jahanian    }
4806d96b35bc6becf8db00d140c11e3d0e53f27567a1Daniel Dunbar    S += RDecl->isUnion() ? ')' : '}';
4807ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner    return;
4808ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner  }
4809e6012c7ecb9d848f4091c8c48e7d9946cc36b23fFariborz Jahanian
48105471bc85b69912e3b448de004498a80c0de32296Douglas Gregor  if (const EnumType *ET = T->getAs<EnumType>()) {
48118b4bf90eb6d3d08cf3bfb86705f0fdb20b9c5875Fariborz Jahanian    if (FD && FD->isBitField())
481264fd7e86c1a90d9ff78e4a0bd79f69499667a4e3David Chisnall      EncodeBitField(this, S, T, FD);
48138b4bf90eb6d3d08cf3bfb86705f0fdb20b9c5875Fariborz Jahanian    else
48145471bc85b69912e3b448de004498a80c0de32296Douglas Gregor      S += ObjCEncodingForEnumType(this, ET);
4815ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner    return;
4816ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner  }
48171eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
4818dc8dab6fabf4bfd4f4b94bf572ac3342a5bbfcd7Bob Wilson  if (const BlockPointerType *BT = T->getAs<BlockPointerType>()) {
481921a98b188857d690aa4510c52ac4317ffa0908a8Steve Naroff    S += "@?"; // Unlike a pointer-to-function, which is "^?".
4820dc8dab6fabf4bfd4f4b94bf572ac3342a5bbfcd7Bob Wilson    if (EncodeBlockParameters) {
4821dc8dab6fabf4bfd4f4b94bf572ac3342a5bbfcd7Bob Wilson      const FunctionType *FT = BT->getPointeeType()->getAs<FunctionType>();
4822dc8dab6fabf4bfd4f4b94bf572ac3342a5bbfcd7Bob Wilson
4823dc8dab6fabf4bfd4f4b94bf572ac3342a5bbfcd7Bob Wilson      S += '<';
4824dc8dab6fabf4bfd4f4b94bf572ac3342a5bbfcd7Bob Wilson      // Block return type
4825dc8dab6fabf4bfd4f4b94bf572ac3342a5bbfcd7Bob Wilson      getObjCEncodingForTypeImpl(FT->getResultType(), S,
4826dc8dab6fabf4bfd4f4b94bf572ac3342a5bbfcd7Bob Wilson                                 ExpandPointedToStructures, ExpandStructures,
4827dc8dab6fabf4bfd4f4b94bf572ac3342a5bbfcd7Bob Wilson                                 FD,
4828dc8dab6fabf4bfd4f4b94bf572ac3342a5bbfcd7Bob Wilson                                 false /* OutermostType */,
4829dc8dab6fabf4bfd4f4b94bf572ac3342a5bbfcd7Bob Wilson                                 EncodingProperty,
4830dc8dab6fabf4bfd4f4b94bf572ac3342a5bbfcd7Bob Wilson                                 false /* StructField */,
4831dc8dab6fabf4bfd4f4b94bf572ac3342a5bbfcd7Bob Wilson                                 EncodeBlockParameters,
4832dc8dab6fabf4bfd4f4b94bf572ac3342a5bbfcd7Bob Wilson                                 EncodeClassNames);
4833dc8dab6fabf4bfd4f4b94bf572ac3342a5bbfcd7Bob Wilson      // Block self
4834dc8dab6fabf4bfd4f4b94bf572ac3342a5bbfcd7Bob Wilson      S += "@?";
4835dc8dab6fabf4bfd4f4b94bf572ac3342a5bbfcd7Bob Wilson      // Block parameters
4836dc8dab6fabf4bfd4f4b94bf572ac3342a5bbfcd7Bob Wilson      if (const FunctionProtoType *FPT = dyn_cast<FunctionProtoType>(FT)) {
4837dc8dab6fabf4bfd4f4b94bf572ac3342a5bbfcd7Bob Wilson        for (FunctionProtoType::arg_type_iterator I = FPT->arg_type_begin(),
4838dc8dab6fabf4bfd4f4b94bf572ac3342a5bbfcd7Bob Wilson               E = FPT->arg_type_end(); I && (I != E); ++I) {
4839dc8dab6fabf4bfd4f4b94bf572ac3342a5bbfcd7Bob Wilson          getObjCEncodingForTypeImpl(*I, S,
4840dc8dab6fabf4bfd4f4b94bf572ac3342a5bbfcd7Bob Wilson                                     ExpandPointedToStructures,
4841dc8dab6fabf4bfd4f4b94bf572ac3342a5bbfcd7Bob Wilson                                     ExpandStructures,
4842dc8dab6fabf4bfd4f4b94bf572ac3342a5bbfcd7Bob Wilson                                     FD,
4843dc8dab6fabf4bfd4f4b94bf572ac3342a5bbfcd7Bob Wilson                                     false /* OutermostType */,
4844dc8dab6fabf4bfd4f4b94bf572ac3342a5bbfcd7Bob Wilson                                     EncodingProperty,
4845dc8dab6fabf4bfd4f4b94bf572ac3342a5bbfcd7Bob Wilson                                     false /* StructField */,
4846dc8dab6fabf4bfd4f4b94bf572ac3342a5bbfcd7Bob Wilson                                     EncodeBlockParameters,
4847dc8dab6fabf4bfd4f4b94bf572ac3342a5bbfcd7Bob Wilson                                     EncodeClassNames);
4848dc8dab6fabf4bfd4f4b94bf572ac3342a5bbfcd7Bob Wilson        }
4849dc8dab6fabf4bfd4f4b94bf572ac3342a5bbfcd7Bob Wilson      }
4850dc8dab6fabf4bfd4f4b94bf572ac3342a5bbfcd7Bob Wilson      S += '>';
4851dc8dab6fabf4bfd4f4b94bf572ac3342a5bbfcd7Bob Wilson    }
4852ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner    return;
4853ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner  }
48541eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
4855c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall  // Ignore protocol qualifiers when mangling at this level.
4856c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall  if (const ObjCObjectType *OT = T->getAs<ObjCObjectType>())
4857c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall    T = OT->getBaseType();
4858c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall
48590953e767ff7817f97b3ab20896b229891eeff45bJohn McCall  if (const ObjCInterfaceType *OIT = T->getAs<ObjCInterfaceType>()) {
486043822eaeddeaa419b90f23c68af6b23c46788a58Fariborz Jahanian    // @encode(class_name)
48610953e767ff7817f97b3ab20896b229891eeff45bJohn McCall    ObjCInterfaceDecl *OI = OIT->getDecl();
486243822eaeddeaa419b90f23c68af6b23c46788a58Fariborz Jahanian    S += '{';
486343822eaeddeaa419b90f23c68af6b23c46788a58Fariborz Jahanian    const IdentifierInfo *II = OI->getIdentifier();
486443822eaeddeaa419b90f23c68af6b23c46788a58Fariborz Jahanian    S += II->getName();
486543822eaeddeaa419b90f23c68af6b23c46788a58Fariborz Jahanian    S += '=';
4866db8264e4c5ffd7af6fbad4ca4306bd382bb02691Jordy Rose    SmallVector<const ObjCIvarDecl*, 32> Ivars;
48672c18bb7c9fca66c30b6eabbdcbc6399d24a54fa9Fariborz Jahanian    DeepCollectObjCIvars(OI, true, Ivars);
48682c18bb7c9fca66c30b6eabbdcbc6399d24a54fa9Fariborz Jahanian    for (unsigned i = 0, e = Ivars.size(); i != e; ++i) {
4869db8264e4c5ffd7af6fbad4ca4306bd382bb02691Jordy Rose      const FieldDecl *Field = cast<FieldDecl>(Ivars[i]);
48702c18bb7c9fca66c30b6eabbdcbc6399d24a54fa9Fariborz Jahanian      if (Field->isBitField())
48712c18bb7c9fca66c30b6eabbdcbc6399d24a54fa9Fariborz Jahanian        getObjCEncodingForTypeImpl(Field->getType(), S, false, true, Field);
487243822eaeddeaa419b90f23c68af6b23c46788a58Fariborz Jahanian      else
48732c18bb7c9fca66c30b6eabbdcbc6399d24a54fa9Fariborz Jahanian        getObjCEncodingForTypeImpl(Field->getType(), S, false, true, FD);
487443822eaeddeaa419b90f23c68af6b23c46788a58Fariborz Jahanian    }
487543822eaeddeaa419b90f23c68af6b23c46788a58Fariborz Jahanian    S += '}';
4876ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner    return;
487743822eaeddeaa419b90f23c68af6b23c46788a58Fariborz Jahanian  }
48781eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
4879183700f494ec9b6701b6efe82bcb25f4c79ba561John McCall  if (const ObjCObjectPointerType *OPT = T->getAs<ObjCObjectPointerType>()) {
488014108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff    if (OPT->isObjCIdType()) {
488114108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff      S += '@';
488214108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff      return;
4883ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner    }
48841eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
488527d20a24a1b816cecbd142727d2c81af5a6a111bSteve Naroff    if (OPT->isObjCClassType() || OPT->isObjCQualifiedClassType()) {
488627d20a24a1b816cecbd142727d2c81af5a6a111bSteve Naroff      // FIXME: Consider if we need to output qualifiers for 'Class<p>'.
488727d20a24a1b816cecbd142727d2c81af5a6a111bSteve Naroff      // Since this is a binary compatibility issue, need to consult with runtime
488827d20a24a1b816cecbd142727d2c81af5a6a111bSteve Naroff      // folks. Fortunately, this is a *very* obsure construct.
488914108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff      S += '#';
489014108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff      return;
4891ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner    }
48921eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
4893ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner    if (OPT->isObjCQualifiedIdType()) {
48941eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      getObjCEncodingForTypeImpl(getObjCIdType(), S,
489514108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff                                 ExpandPointedToStructures,
489614108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff                                 ExpandStructures, FD);
4897dc8dab6fabf4bfd4f4b94bf572ac3342a5bbfcd7Bob Wilson      if (FD || EncodingProperty || EncodeClassNames) {
489814108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff        // Note that we do extended encoding of protocol qualifer list
489914108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff        // Only when doing ivar or property encoding.
490014108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff        S += '"';
490167ef8eaea8a0a2073147a8d863f0e3f30d525802Steve Naroff        for (ObjCObjectPointerType::qual_iterator I = OPT->qual_begin(),
490267ef8eaea8a0a2073147a8d863f0e3f30d525802Steve Naroff             E = OPT->qual_end(); I != E; ++I) {
490314108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff          S += '<';
490414108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff          S += (*I)->getNameAsString();
490514108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff          S += '>';
490614108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff        }
490714108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff        S += '"';
490814108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff      }
490914108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff      return;
4910ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner    }
49111eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
4912ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner    QualType PointeeTy = OPT->getPointeeType();
4913ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner    if (!EncodingProperty &&
4914ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner        isa<TypedefType>(PointeeTy.getTypePtr())) {
4915ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner      // Another historical/compatibility reason.
49161eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      // We encode the underlying type which comes out as
4917ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner      // {...};
4918ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner      S += '^';
49191eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      getObjCEncodingForTypeImpl(PointeeTy, S,
49201eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump                                 false, ExpandPointedToStructures,
4921ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner                                 NULL);
492214108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff      return;
492314108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff    }
4924ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner
4925ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner    S += '@';
4926dc8dab6fabf4bfd4f4b94bf572ac3342a5bbfcd7Bob Wilson    if (OPT->getInterfaceDecl() &&
4927dc8dab6fabf4bfd4f4b94bf572ac3342a5bbfcd7Bob Wilson        (FD || EncodingProperty || EncodeClassNames)) {
4928ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner      S += '"';
492927d20a24a1b816cecbd142727d2c81af5a6a111bSteve Naroff      S += OPT->getInterfaceDecl()->getIdentifier()->getName();
493067ef8eaea8a0a2073147a8d863f0e3f30d525802Steve Naroff      for (ObjCObjectPointerType::qual_iterator I = OPT->qual_begin(),
493167ef8eaea8a0a2073147a8d863f0e3f30d525802Steve Naroff           E = OPT->qual_end(); I != E; ++I) {
4932ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner        S += '<';
4933ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner        S += (*I)->getNameAsString();
4934ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner        S += '>';
49351eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      }
4936ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner      S += '"';
4937ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner    }
4938ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner    return;
4939ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner  }
49401eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
4941532ec7baf2d0791abc9551ef856a537711c5774aJohn McCall  // gcc just blithely ignores member pointers.
4942532ec7baf2d0791abc9551ef856a537711c5774aJohn McCall  // TODO: maybe there should be a mangling for these
4943532ec7baf2d0791abc9551ef856a537711c5774aJohn McCall  if (T->getAs<MemberPointerType>())
4944532ec7baf2d0791abc9551ef856a537711c5774aJohn McCall    return;
4945e6012c7ecb9d848f4091c8c48e7d9946cc36b23fFariborz Jahanian
4946e6012c7ecb9d848f4091c8c48e7d9946cc36b23fFariborz Jahanian  if (T->isVectorType()) {
4947e6012c7ecb9d848f4091c8c48e7d9946cc36b23fFariborz Jahanian    // This matches gcc's encoding, even though technically it is
4948e6012c7ecb9d848f4091c8c48e7d9946cc36b23fFariborz Jahanian    // insufficient.
4949e6012c7ecb9d848f4091c8c48e7d9946cc36b23fFariborz Jahanian    // FIXME. We should do a better job than gcc.
4950e6012c7ecb9d848f4091c8c48e7d9946cc36b23fFariborz Jahanian    return;
4951e6012c7ecb9d848f4091c8c48e7d9946cc36b23fFariborz Jahanian  }
4952e6012c7ecb9d848f4091c8c48e7d9946cc36b23fFariborz Jahanian
4953b219cfc4d75f0a03630b7c4509ef791b7e97b2c8David Blaikie  llvm_unreachable("@encode for type not implemented!");
495485f9bceab1542aafff012d4d28e998f4ba16e362Anders Carlsson}
495585f9bceab1542aafff012d4d28e998f4ba16e362Anders Carlsson
49562636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidisvoid ASTContext::getObjCEncodingForStructureImpl(RecordDecl *RDecl,
49572636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis                                                 std::string &S,
49582636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis                                                 const FieldDecl *FD,
49592636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis                                                 bool includeVBases) const {
49602636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis  assert(RDecl && "Expected non-null RecordDecl");
49612636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis  assert(!RDecl->isUnion() && "Should not be called for unions");
49622636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis  if (!RDecl->getDefinition())
49632636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis    return;
49642636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis
49652636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis  CXXRecordDecl *CXXRec = dyn_cast<CXXRecordDecl>(RDecl);
49662636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis  std::multimap<uint64_t, NamedDecl *> FieldOrBaseOffsets;
49672636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis  const ASTRecordLayout &layout = getASTRecordLayout(RDecl);
49682636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis
49692636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis  if (CXXRec) {
49702636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis    for (CXXRecordDecl::base_class_iterator
49712636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis           BI = CXXRec->bases_begin(),
49722636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis           BE = CXXRec->bases_end(); BI != BE; ++BI) {
49732636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis      if (!BI->isVirtual()) {
49742636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis        CXXRecordDecl *base = BI->getType()->getAsCXXRecordDecl();
4975829f20097e030229fd4c234b84948f53312eaf55Argyrios Kyrtzidis        if (base->isEmpty())
4976829f20097e030229fd4c234b84948f53312eaf55Argyrios Kyrtzidis          continue;
4977d4f5198ae07d9a4958d8191bac694ded12173ad9Benjamin Kramer        uint64_t offs = toBits(layout.getBaseClassOffset(base));
49782636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis        FieldOrBaseOffsets.insert(FieldOrBaseOffsets.upper_bound(offs),
49792636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis                                  std::make_pair(offs, base));
49802636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis      }
49812636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis    }
49822636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis  }
49832636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis
49842636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis  unsigned i = 0;
49852636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis  for (RecordDecl::field_iterator Field = RDecl->field_begin(),
49862636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis                               FieldEnd = RDecl->field_end();
49872636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis       Field != FieldEnd; ++Field, ++i) {
49882636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis    uint64_t offs = layout.getFieldOffset(i);
49892636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis    FieldOrBaseOffsets.insert(FieldOrBaseOffsets.upper_bound(offs),
4990581deb3da481053c4993c7600f97acf7768caac5David Blaikie                              std::make_pair(offs, *Field));
49912636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis  }
49922636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis
49932636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis  if (CXXRec && includeVBases) {
49942636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis    for (CXXRecordDecl::base_class_iterator
49952636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis           BI = CXXRec->vbases_begin(),
49962636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis           BE = CXXRec->vbases_end(); BI != BE; ++BI) {
49972636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis      CXXRecordDecl *base = BI->getType()->getAsCXXRecordDecl();
4998829f20097e030229fd4c234b84948f53312eaf55Argyrios Kyrtzidis      if (base->isEmpty())
4999829f20097e030229fd4c234b84948f53312eaf55Argyrios Kyrtzidis        continue;
5000d4f5198ae07d9a4958d8191bac694ded12173ad9Benjamin Kramer      uint64_t offs = toBits(layout.getVBaseClassOffset(base));
500119aa8609a5d04eed9b81aa1c4608e8a4f3df6ccdArgyrios Kyrtzidis      if (FieldOrBaseOffsets.find(offs) == FieldOrBaseOffsets.end())
500219aa8609a5d04eed9b81aa1c4608e8a4f3df6ccdArgyrios Kyrtzidis        FieldOrBaseOffsets.insert(FieldOrBaseOffsets.end(),
500319aa8609a5d04eed9b81aa1c4608e8a4f3df6ccdArgyrios Kyrtzidis                                  std::make_pair(offs, base));
50042636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis    }
50052636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis  }
50062636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis
50072636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis  CharUnits size;
50082636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis  if (CXXRec) {
50092636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis    size = includeVBases ? layout.getSize() : layout.getNonVirtualSize();
50102636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis  } else {
50112636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis    size = layout.getSize();
50122636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis  }
50132636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis
50142636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis  uint64_t CurOffs = 0;
50152636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis  std::multimap<uint64_t, NamedDecl *>::iterator
50162636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis    CurLayObj = FieldOrBaseOffsets.begin();
50172636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis
501858db7a575efc9a2f35266fe240feac3cf317753dDouglas Gregor  if (CXXRec && CXXRec->isDynamicClass() &&
501958db7a575efc9a2f35266fe240feac3cf317753dDouglas Gregor      (CurLayObj == FieldOrBaseOffsets.end() || CurLayObj->first != 0)) {
50202636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis    if (FD) {
50212636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis      S += "\"_vptr$";
50222636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis      std::string recname = CXXRec->getNameAsString();
50232636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis      if (recname.empty()) recname = "?";
50242636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis      S += recname;
50252636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis      S += '"';
50262636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis    }
50272636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis    S += "^^?";
50282636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis    CurOffs += getTypeSize(VoidPtrTy);
50292636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis  }
50302636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis
50312636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis  if (!RDecl->hasFlexibleArrayMember()) {
50322636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis    // Mark the end of the structure.
50332636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis    uint64_t offs = toBits(size);
50342636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis    FieldOrBaseOffsets.insert(FieldOrBaseOffsets.upper_bound(offs),
50352636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis                              std::make_pair(offs, (NamedDecl*)0));
50362636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis  }
50372636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis
50382636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis  for (; CurLayObj != FieldOrBaseOffsets.end(); ++CurLayObj) {
50392636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis    assert(CurOffs <= CurLayObj->first);
50402636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis
50412636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis    if (CurOffs < CurLayObj->first) {
50422636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis      uint64_t padding = CurLayObj->first - CurOffs;
50432636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis      // FIXME: There doesn't seem to be a way to indicate in the encoding that
50442636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis      // packing/alignment of members is different that normal, in which case
50452636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis      // the encoding will be out-of-sync with the real layout.
50462636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis      // If the runtime switches to just consider the size of types without
50472636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis      // taking into account alignment, we could make padding explicit in the
50482636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis      // encoding (e.g. using arrays of chars). The encoding strings would be
50492636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis      // longer then though.
50502636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis      CurOffs += padding;
50512636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis    }
50522636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis
50532636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis    NamedDecl *dcl = CurLayObj->second;
50542636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis    if (dcl == 0)
50552636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis      break; // reached end of structure.
50562636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis
50572636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis    if (CXXRecordDecl *base = dyn_cast<CXXRecordDecl>(dcl)) {
50582636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis      // We expand the bases without their virtual bases since those are going
50592636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis      // in the initial structure. Note that this differs from gcc which
50602636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis      // expands virtual bases each time one is encountered in the hierarchy,
50612636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis      // making the encoding type bigger than it really is.
50622636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis      getObjCEncodingForStructureImpl(base, S, FD, /*includeVBases*/false);
5063829f20097e030229fd4c234b84948f53312eaf55Argyrios Kyrtzidis      assert(!base->isEmpty());
5064829f20097e030229fd4c234b84948f53312eaf55Argyrios Kyrtzidis      CurOffs += toBits(getASTRecordLayout(base).getNonVirtualSize());
50652636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis    } else {
50662636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis      FieldDecl *field = cast<FieldDecl>(dcl);
50672636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis      if (FD) {
50682636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis        S += '"';
50692636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis        S += field->getNameAsString();
50702636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis        S += '"';
50712636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis      }
50722636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis
50732636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis      if (field->isBitField()) {
50742636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis        EncodeBitField(this, S, field->getType(), field);
5075a6b8b2c09610b8bc4330e948ece8b940c2386406Richard Smith        CurOffs += field->getBitWidthValue(*this);
50762636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis      } else {
50772636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis        QualType qt = field->getType();
50782636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis        getLegacyIntegralTypeEncoding(qt);
50792636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis        getObjCEncodingForTypeImpl(qt, S, false, true, FD,
50802636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis                                   /*OutermostType*/false,
50812636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis                                   /*EncodingProperty*/false,
50822636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis                                   /*StructField*/true);
50832636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis        CurOffs += getTypeSize(field->getType());
50842636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis      }
50852636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis    }
50862636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis  }
50872636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis}
50882636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis
50891eb4433ac451dc16f4133a88af2d002ac26c58efMike Stumpvoid ASTContext::getObjCEncodingForTypeQualifier(Decl::ObjCDeclQualifier QT,
5090ecb01e666665efabd2aa76a76f6080e2a78965faFariborz Jahanian                                                 std::string& S) const {
5091ecb01e666665efabd2aa76a76f6080e2a78965faFariborz Jahanian  if (QT & Decl::OBJC_TQ_In)
5092ecb01e666665efabd2aa76a76f6080e2a78965faFariborz Jahanian    S += 'n';
5093ecb01e666665efabd2aa76a76f6080e2a78965faFariborz Jahanian  if (QT & Decl::OBJC_TQ_Inout)
5094ecb01e666665efabd2aa76a76f6080e2a78965faFariborz Jahanian    S += 'N';
5095ecb01e666665efabd2aa76a76f6080e2a78965faFariborz Jahanian  if (QT & Decl::OBJC_TQ_Out)
5096ecb01e666665efabd2aa76a76f6080e2a78965faFariborz Jahanian    S += 'o';
5097ecb01e666665efabd2aa76a76f6080e2a78965faFariborz Jahanian  if (QT & Decl::OBJC_TQ_Bycopy)
5098ecb01e666665efabd2aa76a76f6080e2a78965faFariborz Jahanian    S += 'O';
5099ecb01e666665efabd2aa76a76f6080e2a78965faFariborz Jahanian  if (QT & Decl::OBJC_TQ_Byref)
5100ecb01e666665efabd2aa76a76f6080e2a78965faFariborz Jahanian    S += 'R';
5101ecb01e666665efabd2aa76a76f6080e2a78965faFariborz Jahanian  if (QT & Decl::OBJC_TQ_Oneway)
5102ecb01e666665efabd2aa76a76f6080e2a78965faFariborz Jahanian    S += 'V';
5103ecb01e666665efabd2aa76a76f6080e2a78965faFariborz Jahanian}
5104ecb01e666665efabd2aa76a76f6080e2a78965faFariborz Jahanian
51054dfd02a17c6d604c72e6936527c5e1c56d3ecb7aDouglas GregorTypedefDecl *ASTContext::getObjCIdDecl() const {
51064dfd02a17c6d604c72e6936527c5e1c56d3ecb7aDouglas Gregor  if (!ObjCIdDecl) {
51074dfd02a17c6d604c72e6936527c5e1c56d3ecb7aDouglas Gregor    QualType T = getObjCObjectType(ObjCBuiltinIdTy, 0, 0);
51084dfd02a17c6d604c72e6936527c5e1c56d3ecb7aDouglas Gregor    T = getObjCObjectPointerType(T);
51094dfd02a17c6d604c72e6936527c5e1c56d3ecb7aDouglas Gregor    TypeSourceInfo *IdInfo = getTrivialTypeSourceInfo(T);
51104dfd02a17c6d604c72e6936527c5e1c56d3ecb7aDouglas Gregor    ObjCIdDecl = TypedefDecl::Create(const_cast<ASTContext &>(*this),
51114dfd02a17c6d604c72e6936527c5e1c56d3ecb7aDouglas Gregor                                     getTranslationUnitDecl(),
51124dfd02a17c6d604c72e6936527c5e1c56d3ecb7aDouglas Gregor                                     SourceLocation(), SourceLocation(),
51134dfd02a17c6d604c72e6936527c5e1c56d3ecb7aDouglas Gregor                                     &Idents.get("id"), IdInfo);
51144dfd02a17c6d604c72e6936527c5e1c56d3ecb7aDouglas Gregor  }
51154dfd02a17c6d604c72e6936527c5e1c56d3ecb7aDouglas Gregor
51164dfd02a17c6d604c72e6936527c5e1c56d3ecb7aDouglas Gregor  return ObjCIdDecl;
51177e219e47de26346885d667131977bd9ca2d7662aSteve Naroff}
51187e219e47de26346885d667131977bd9ca2d7662aSteve Naroff
51197a27ea52b7bd635c89bec5a9c521a3bf7d204238Douglas GregorTypedefDecl *ASTContext::getObjCSelDecl() const {
51207a27ea52b7bd635c89bec5a9c521a3bf7d204238Douglas Gregor  if (!ObjCSelDecl) {
51217a27ea52b7bd635c89bec5a9c521a3bf7d204238Douglas Gregor    QualType SelT = getPointerType(ObjCBuiltinSelTy);
51227a27ea52b7bd635c89bec5a9c521a3bf7d204238Douglas Gregor    TypeSourceInfo *SelInfo = getTrivialTypeSourceInfo(SelT);
51237a27ea52b7bd635c89bec5a9c521a3bf7d204238Douglas Gregor    ObjCSelDecl = TypedefDecl::Create(const_cast<ASTContext &>(*this),
51247a27ea52b7bd635c89bec5a9c521a3bf7d204238Douglas Gregor                                      getTranslationUnitDecl(),
51257a27ea52b7bd635c89bec5a9c521a3bf7d204238Douglas Gregor                                      SourceLocation(), SourceLocation(),
51267a27ea52b7bd635c89bec5a9c521a3bf7d204238Douglas Gregor                                      &Idents.get("SEL"), SelInfo);
51277a27ea52b7bd635c89bec5a9c521a3bf7d204238Douglas Gregor  }
51287a27ea52b7bd635c89bec5a9c521a3bf7d204238Douglas Gregor  return ObjCSelDecl;
5129b62f6813406a03bf8a371c4e46c9fad51d102121Fariborz Jahanian}
5130b62f6813406a03bf8a371c4e46c9fad51d102121Fariborz Jahanian
513179d6726921897811232554ed94c5d77b5b7b3fc0Douglas GregorTypedefDecl *ASTContext::getObjCClassDecl() const {
513279d6726921897811232554ed94c5d77b5b7b3fc0Douglas Gregor  if (!ObjCClassDecl) {
513379d6726921897811232554ed94c5d77b5b7b3fc0Douglas Gregor    QualType T = getObjCObjectType(ObjCBuiltinClassTy, 0, 0);
513479d6726921897811232554ed94c5d77b5b7b3fc0Douglas Gregor    T = getObjCObjectPointerType(T);
513579d6726921897811232554ed94c5d77b5b7b3fc0Douglas Gregor    TypeSourceInfo *ClassInfo = getTrivialTypeSourceInfo(T);
513679d6726921897811232554ed94c5d77b5b7b3fc0Douglas Gregor    ObjCClassDecl = TypedefDecl::Create(const_cast<ASTContext &>(*this),
513779d6726921897811232554ed94c5d77b5b7b3fc0Douglas Gregor                                        getTranslationUnitDecl(),
513879d6726921897811232554ed94c5d77b5b7b3fc0Douglas Gregor                                        SourceLocation(), SourceLocation(),
513979d6726921897811232554ed94c5d77b5b7b3fc0Douglas Gregor                                        &Idents.get("Class"), ClassInfo);
514079d6726921897811232554ed94c5d77b5b7b3fc0Douglas Gregor  }
514179d6726921897811232554ed94c5d77b5b7b3fc0Douglas Gregor
514279d6726921897811232554ed94c5d77b5b7b3fc0Douglas Gregor  return ObjCClassDecl;
5143a6ea10e22b600d92e084f6b11b9b9a92d0eb2412Douglas Gregor}
5144a6ea10e22b600d92e084f6b11b9b9a92d0eb2412Douglas Gregor
5145a6ea10e22b600d92e084f6b11b9b9a92d0eb2412Douglas GregorObjCInterfaceDecl *ASTContext::getObjCProtocolDecl() const {
5146a6ea10e22b600d92e084f6b11b9b9a92d0eb2412Douglas Gregor  if (!ObjCProtocolClassDecl) {
5147a6ea10e22b600d92e084f6b11b9b9a92d0eb2412Douglas Gregor    ObjCProtocolClassDecl
5148a6ea10e22b600d92e084f6b11b9b9a92d0eb2412Douglas Gregor      = ObjCInterfaceDecl::Create(*this, getTranslationUnitDecl(),
5149a6ea10e22b600d92e084f6b11b9b9a92d0eb2412Douglas Gregor                                  SourceLocation(),
5150a6ea10e22b600d92e084f6b11b9b9a92d0eb2412Douglas Gregor                                  &Idents.get("Protocol"),
5151a6ea10e22b600d92e084f6b11b9b9a92d0eb2412Douglas Gregor                                  /*PrevDecl=*/0,
5152a6ea10e22b600d92e084f6b11b9b9a92d0eb2412Douglas Gregor                                  SourceLocation(), true);
5153a6ea10e22b600d92e084f6b11b9b9a92d0eb2412Douglas Gregor  }
5154a6ea10e22b600d92e084f6b11b9b9a92d0eb2412Douglas Gregor
5155a6ea10e22b600d92e084f6b11b9b9a92d0eb2412Douglas Gregor  return ObjCProtocolClassDecl;
51568baaca50f07d0c10bba69c8d88c1b9078c92d06dAnders Carlsson}
51578baaca50f07d0c10bba69c8d88c1b9078c92d06dAnders Carlsson
5158c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge//===----------------------------------------------------------------------===//
5159c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge// __builtin_va_list Construction Functions
5160c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge//===----------------------------------------------------------------------===//
5161c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge
5162c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Ingestatic TypedefDecl *CreateCharPtrBuiltinVaListDecl(const ASTContext *Context) {
5163c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge  // typedef char* __builtin_va_list;
5164c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge  QualType CharPtrType = Context->getPointerType(Context->CharTy);
5165c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge  TypeSourceInfo *TInfo
5166c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge    = Context->getTrivialTypeSourceInfo(CharPtrType);
5167c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge
5168c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge  TypedefDecl *VaListTypeDecl
5169c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge    = TypedefDecl::Create(const_cast<ASTContext &>(*Context),
5170c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge                          Context->getTranslationUnitDecl(),
5171c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge                          SourceLocation(), SourceLocation(),
5172c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge                          &Context->Idents.get("__builtin_va_list"),
5173c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge                          TInfo);
5174c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge  return VaListTypeDecl;
5175c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge}
5176c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge
5177c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Ingestatic TypedefDecl *CreateVoidPtrBuiltinVaListDecl(const ASTContext *Context) {
5178c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge  // typedef void* __builtin_va_list;
5179c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge  QualType VoidPtrType = Context->getPointerType(Context->VoidTy);
5180c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge  TypeSourceInfo *TInfo
5181c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge    = Context->getTrivialTypeSourceInfo(VoidPtrType);
5182c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge
5183c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge  TypedefDecl *VaListTypeDecl
5184c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge    = TypedefDecl::Create(const_cast<ASTContext &>(*Context),
5185c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge                          Context->getTranslationUnitDecl(),
5186c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge                          SourceLocation(), SourceLocation(),
5187c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge                          &Context->Idents.get("__builtin_va_list"),
5188c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge                          TInfo);
5189c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge  return VaListTypeDecl;
5190c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge}
5191c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge
5192c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Ingestatic TypedefDecl *CreatePowerABIBuiltinVaListDecl(const ASTContext *Context) {
5193c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge  // typedef struct __va_list_tag {
5194c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge  RecordDecl *VaListTagDecl;
5195c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge
5196c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge  VaListTagDecl = CreateRecordDecl(*Context, TTK_Struct,
5197c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge                                   Context->getTranslationUnitDecl(),
5198c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge                                   &Context->Idents.get("__va_list_tag"));
5199c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge  VaListTagDecl->startDefinition();
5200c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge
5201c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge  const size_t NumFields = 5;
5202c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge  QualType FieldTypes[NumFields];
5203c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge  const char *FieldNames[NumFields];
5204c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge
5205c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge  //   unsigned char gpr;
5206c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge  FieldTypes[0] = Context->UnsignedCharTy;
5207c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge  FieldNames[0] = "gpr";
5208c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge
5209c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge  //   unsigned char fpr;
5210c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge  FieldTypes[1] = Context->UnsignedCharTy;
5211c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge  FieldNames[1] = "fpr";
5212c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge
5213c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge  //   unsigned short reserved;
5214c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge  FieldTypes[2] = Context->UnsignedShortTy;
5215c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge  FieldNames[2] = "reserved";
5216c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge
5217c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge  //   void* overflow_arg_area;
5218c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge  FieldTypes[3] = Context->getPointerType(Context->VoidTy);
5219c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge  FieldNames[3] = "overflow_arg_area";
5220c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge
5221c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge  //   void* reg_save_area;
5222c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge  FieldTypes[4] = Context->getPointerType(Context->VoidTy);
5223c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge  FieldNames[4] = "reg_save_area";
5224c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge
5225c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge  // Create fields
5226c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge  for (unsigned i = 0; i < NumFields; ++i) {
5227c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge    FieldDecl *Field = FieldDecl::Create(*Context, VaListTagDecl,
5228c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge                                         SourceLocation(),
5229c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge                                         SourceLocation(),
5230c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge                                         &Context->Idents.get(FieldNames[i]),
5231c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge                                         FieldTypes[i], /*TInfo=*/0,
5232c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge                                         /*BitWidth=*/0,
5233c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge                                         /*Mutable=*/false,
5234c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge                                         ICIS_NoInit);
5235c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge    Field->setAccess(AS_public);
5236c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge    VaListTagDecl->addDecl(Field);
5237c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge  }
5238c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge  VaListTagDecl->completeDefinition();
5239c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge  QualType VaListTagType = Context->getRecordType(VaListTagDecl);
5240fb40e3f10ccef93c4f8fb6bd4fe5a108fa6cd369Meador Inge  Context->VaListTagTy = VaListTagType;
5241c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge
5242c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge  // } __va_list_tag;
5243c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge  TypedefDecl *VaListTagTypedefDecl
5244c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge    = TypedefDecl::Create(const_cast<ASTContext &>(*Context),
5245c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge                          Context->getTranslationUnitDecl(),
5246c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge                          SourceLocation(), SourceLocation(),
5247c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge                          &Context->Idents.get("__va_list_tag"),
5248c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge                          Context->getTrivialTypeSourceInfo(VaListTagType));
5249c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge  QualType VaListTagTypedefType =
5250c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge    Context->getTypedefType(VaListTagTypedefDecl);
5251c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge
5252c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge  // typedef __va_list_tag __builtin_va_list[1];
5253c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge  llvm::APInt Size(Context->getTypeSize(Context->getSizeType()), 1);
5254c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge  QualType VaListTagArrayType
5255c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge    = Context->getConstantArrayType(VaListTagTypedefType,
5256c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge                                    Size, ArrayType::Normal, 0);
5257c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge  TypeSourceInfo *TInfo
5258c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge    = Context->getTrivialTypeSourceInfo(VaListTagArrayType);
5259c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge  TypedefDecl *VaListTypedefDecl
5260c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge    = TypedefDecl::Create(const_cast<ASTContext &>(*Context),
5261c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge                          Context->getTranslationUnitDecl(),
5262c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge                          SourceLocation(), SourceLocation(),
5263c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge                          &Context->Idents.get("__builtin_va_list"),
5264c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge                          TInfo);
5265c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge
5266c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge  return VaListTypedefDecl;
5267c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge}
5268c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge
5269c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Ingestatic TypedefDecl *
5270c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador IngeCreateX86_64ABIBuiltinVaListDecl(const ASTContext *Context) {
5271c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge  // typedef struct __va_list_tag {
5272c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge  RecordDecl *VaListTagDecl;
5273c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge  VaListTagDecl = CreateRecordDecl(*Context, TTK_Struct,
5274c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge                                   Context->getTranslationUnitDecl(),
5275c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge                                   &Context->Idents.get("__va_list_tag"));
5276c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge  VaListTagDecl->startDefinition();
5277c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge
5278c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge  const size_t NumFields = 4;
5279c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge  QualType FieldTypes[NumFields];
5280c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge  const char *FieldNames[NumFields];
5281c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge
5282c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge  //   unsigned gp_offset;
5283c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge  FieldTypes[0] = Context->UnsignedIntTy;
5284c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge  FieldNames[0] = "gp_offset";
5285c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge
5286c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge  //   unsigned fp_offset;
5287c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge  FieldTypes[1] = Context->UnsignedIntTy;
5288c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge  FieldNames[1] = "fp_offset";
5289c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge
5290c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge  //   void* overflow_arg_area;
5291c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge  FieldTypes[2] = Context->getPointerType(Context->VoidTy);
5292c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge  FieldNames[2] = "overflow_arg_area";
5293c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge
5294c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge  //   void* reg_save_area;
5295c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge  FieldTypes[3] = Context->getPointerType(Context->VoidTy);
5296c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge  FieldNames[3] = "reg_save_area";
5297c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge
5298c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge  // Create fields
5299c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge  for (unsigned i = 0; i < NumFields; ++i) {
5300c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge    FieldDecl *Field = FieldDecl::Create(const_cast<ASTContext &>(*Context),
5301c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge                                         VaListTagDecl,
5302c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge                                         SourceLocation(),
5303c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge                                         SourceLocation(),
5304c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge                                         &Context->Idents.get(FieldNames[i]),
5305c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge                                         FieldTypes[i], /*TInfo=*/0,
5306c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge                                         /*BitWidth=*/0,
5307c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge                                         /*Mutable=*/false,
5308c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge                                         ICIS_NoInit);
5309c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge    Field->setAccess(AS_public);
5310c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge    VaListTagDecl->addDecl(Field);
5311c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge  }
5312c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge  VaListTagDecl->completeDefinition();
5313c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge  QualType VaListTagType = Context->getRecordType(VaListTagDecl);
5314fb40e3f10ccef93c4f8fb6bd4fe5a108fa6cd369Meador Inge  Context->VaListTagTy = VaListTagType;
5315c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge
5316c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge  // } __va_list_tag;
5317c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge  TypedefDecl *VaListTagTypedefDecl
5318c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge    = TypedefDecl::Create(const_cast<ASTContext &>(*Context),
5319c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge                          Context->getTranslationUnitDecl(),
5320c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge                          SourceLocation(), SourceLocation(),
5321c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge                          &Context->Idents.get("__va_list_tag"),
5322c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge                          Context->getTrivialTypeSourceInfo(VaListTagType));
5323c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge  QualType VaListTagTypedefType =
5324c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge    Context->getTypedefType(VaListTagTypedefDecl);
5325c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge
5326c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge  // typedef __va_list_tag __builtin_va_list[1];
5327c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge  llvm::APInt Size(Context->getTypeSize(Context->getSizeType()), 1);
5328c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge  QualType VaListTagArrayType
5329c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge    = Context->getConstantArrayType(VaListTagTypedefType,
5330c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge                                      Size, ArrayType::Normal,0);
5331c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge  TypeSourceInfo *TInfo
5332c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge    = Context->getTrivialTypeSourceInfo(VaListTagArrayType);
5333c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge  TypedefDecl *VaListTypedefDecl
5334c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge    = TypedefDecl::Create(const_cast<ASTContext &>(*Context),
5335c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge                          Context->getTranslationUnitDecl(),
5336c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge                          SourceLocation(), SourceLocation(),
5337c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge                          &Context->Idents.get("__builtin_va_list"),
5338c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge                          TInfo);
5339c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge
5340c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge  return VaListTypedefDecl;
5341c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge}
5342c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge
5343c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Ingestatic TypedefDecl *CreatePNaClABIBuiltinVaListDecl(const ASTContext *Context) {
5344c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge  // typedef int __builtin_va_list[4];
5345c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge  llvm::APInt Size(Context->getTypeSize(Context->getSizeType()), 4);
5346c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge  QualType IntArrayType
5347c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge    = Context->getConstantArrayType(Context->IntTy,
5348c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge				    Size, ArrayType::Normal, 0);
5349c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge  TypedefDecl *VaListTypedefDecl
5350c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge    = TypedefDecl::Create(const_cast<ASTContext &>(*Context),
5351c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge                          Context->getTranslationUnitDecl(),
5352c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge                          SourceLocation(), SourceLocation(),
5353c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge                          &Context->Idents.get("__builtin_va_list"),
5354c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge                          Context->getTrivialTypeSourceInfo(IntArrayType));
5355c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge
5356c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge  return VaListTypedefDecl;
5357c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge}
5358c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge
5359c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Ingestatic TypedefDecl *CreateVaListDecl(const ASTContext *Context,
5360c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge                                     TargetInfo::BuiltinVaListKind Kind) {
5361c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge  switch (Kind) {
5362c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge  case TargetInfo::CharPtrBuiltinVaList:
5363c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge    return CreateCharPtrBuiltinVaListDecl(Context);
5364c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge  case TargetInfo::VoidPtrBuiltinVaList:
5365c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge    return CreateVoidPtrBuiltinVaListDecl(Context);
5366c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge  case TargetInfo::PowerABIBuiltinVaList:
5367c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge    return CreatePowerABIBuiltinVaListDecl(Context);
5368c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge  case TargetInfo::X86_64ABIBuiltinVaList:
5369c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge    return CreateX86_64ABIBuiltinVaListDecl(Context);
5370c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge  case TargetInfo::PNaClABIBuiltinVaList:
5371c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge    return CreatePNaClABIBuiltinVaListDecl(Context);
5372c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge  }
5373c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge
5374c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge  llvm_unreachable("Unhandled __builtin_va_list type kind");
5375c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge}
5376c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge
5377c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador IngeTypedefDecl *ASTContext::getBuiltinVaListDecl() const {
5378c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge  if (!BuiltinVaListDecl)
5379c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge    BuiltinVaListDecl = CreateVaListDecl(this, Target->getBuiltinVaListKind());
5380c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge
5381c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge  return BuiltinVaListDecl;
5382c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge}
5383c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge
5384fb40e3f10ccef93c4f8fb6bd4fe5a108fa6cd369Meador IngeQualType ASTContext::getVaListTagType() const {
5385fb40e3f10ccef93c4f8fb6bd4fe5a108fa6cd369Meador Inge  // Force the creation of VaListTagTy by building the __builtin_va_list
5386fb40e3f10ccef93c4f8fb6bd4fe5a108fa6cd369Meador Inge  // declaration.
5387fb40e3f10ccef93c4f8fb6bd4fe5a108fa6cd369Meador Inge  if (VaListTagTy.isNull())
5388fb40e3f10ccef93c4f8fb6bd4fe5a108fa6cd369Meador Inge    (void) getBuiltinVaListDecl();
5389fb40e3f10ccef93c4f8fb6bd4fe5a108fa6cd369Meador Inge
5390fb40e3f10ccef93c4f8fb6bd4fe5a108fa6cd369Meador Inge  return VaListTagTy;
5391fb40e3f10ccef93c4f8fb6bd4fe5a108fa6cd369Meador Inge}
5392fb40e3f10ccef93c4f8fb6bd4fe5a108fa6cd369Meador Inge
5393a526c5c67e5a0473c340903ee542ce570119665fTed Kremenekvoid ASTContext::setObjCConstantStringInterface(ObjCInterfaceDecl *Decl) {
53941eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  assert(ObjCConstantStringType.isNull() &&
53952198891824c38d45b2279de5d5e3ef9394eb457cSteve Naroff         "'NSConstantString' type already set!");
53961eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
5397a526c5c67e5a0473c340903ee542ce570119665fTed Kremenek  ObjCConstantStringType = getObjCInterfaceType(Decl);
53982198891824c38d45b2279de5d5e3ef9394eb457cSteve Naroff}
53992198891824c38d45b2279de5d5e3ef9394eb457cSteve Naroff
54000bd6feb9e9d40fc889fd47e899985125a43dfed8John McCall/// \brief Retrieve the template name that corresponds to a non-empty
54010bd6feb9e9d40fc889fd47e899985125a43dfed8John McCall/// lookup.
54024ba2a17694148e16eaa8d3917f657ffcd3667be4Jay FoadTemplateName
54034ba2a17694148e16eaa8d3917f657ffcd3667be4Jay FoadASTContext::getOverloadedTemplateName(UnresolvedSetIterator Begin,
54044ba2a17694148e16eaa8d3917f657ffcd3667be4Jay Foad                                      UnresolvedSetIterator End) const {
54050bd6feb9e9d40fc889fd47e899985125a43dfed8John McCall  unsigned size = End - Begin;
54060bd6feb9e9d40fc889fd47e899985125a43dfed8John McCall  assert(size > 1 && "set is not overloaded!");
54077532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor
54080bd6feb9e9d40fc889fd47e899985125a43dfed8John McCall  void *memory = Allocate(sizeof(OverloadedTemplateStorage) +
54090bd6feb9e9d40fc889fd47e899985125a43dfed8John McCall                          size * sizeof(FunctionTemplateDecl*));
54100bd6feb9e9d40fc889fd47e899985125a43dfed8John McCall  OverloadedTemplateStorage *OT = new(memory) OverloadedTemplateStorage(size);
54110bd6feb9e9d40fc889fd47e899985125a43dfed8John McCall
54120bd6feb9e9d40fc889fd47e899985125a43dfed8John McCall  NamedDecl **Storage = OT->getStorage();
5413eec51cf1ba5f0e62c9cdb81b5c63babdd6e649abJohn McCall  for (UnresolvedSetIterator I = Begin; I != End; ++I) {
54140bd6feb9e9d40fc889fd47e899985125a43dfed8John McCall    NamedDecl *D = *I;
54150bd6feb9e9d40fc889fd47e899985125a43dfed8John McCall    assert(isa<FunctionTemplateDecl>(D) ||
54160bd6feb9e9d40fc889fd47e899985125a43dfed8John McCall           (isa<UsingShadowDecl>(D) &&
54170bd6feb9e9d40fc889fd47e899985125a43dfed8John McCall            isa<FunctionTemplateDecl>(D->getUnderlyingDecl())));
54180bd6feb9e9d40fc889fd47e899985125a43dfed8John McCall    *Storage++ = D;
54197532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor  }
54207532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor
54210bd6feb9e9d40fc889fd47e899985125a43dfed8John McCall  return TemplateName(OT);
54227532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor}
54237532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor
5424d99cbe66403ee39c2ee58024b9582b95649a4fc5Douglas Gregor/// \brief Retrieve the template name that represents a qualified
5425d99cbe66403ee39c2ee58024b9582b95649a4fc5Douglas Gregor/// template name such as \c std::vector.
54264ba2a17694148e16eaa8d3917f657ffcd3667be4Jay FoadTemplateName
54274ba2a17694148e16eaa8d3917f657ffcd3667be4Jay FoadASTContext::getQualifiedTemplateName(NestedNameSpecifier *NNS,
54284ba2a17694148e16eaa8d3917f657ffcd3667be4Jay Foad                                     bool TemplateKeyword,
54294ba2a17694148e16eaa8d3917f657ffcd3667be4Jay Foad                                     TemplateDecl *Template) const {
54300f0ea2a96534c615ff5fdd81363989b23cf2164aDouglas Gregor  assert(NNS && "Missing nested-name-specifier in qualified template name");
54310f0ea2a96534c615ff5fdd81363989b23cf2164aDouglas Gregor
5432789b1f640205e81b5af250693246120f1ce9d147Douglas Gregor  // FIXME: Canonicalization?
5433d99cbe66403ee39c2ee58024b9582b95649a4fc5Douglas Gregor  llvm::FoldingSetNodeID ID;
5434d99cbe66403ee39c2ee58024b9582b95649a4fc5Douglas Gregor  QualifiedTemplateName::Profile(ID, NNS, TemplateKeyword, Template);
54351eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
5436d99cbe66403ee39c2ee58024b9582b95649a4fc5Douglas Gregor  void *InsertPos = 0;
5437d99cbe66403ee39c2ee58024b9582b95649a4fc5Douglas Gregor  QualifiedTemplateName *QTN =
54380bd6feb9e9d40fc889fd47e899985125a43dfed8John McCall    QualifiedTemplateNames.FindNodeOrInsertPos(ID, InsertPos);
5439d99cbe66403ee39c2ee58024b9582b95649a4fc5Douglas Gregor  if (!QTN) {
5440d99cbe66403ee39c2ee58024b9582b95649a4fc5Douglas Gregor    QTN = new (*this,4) QualifiedTemplateName(NNS, TemplateKeyword, Template);
5441d99cbe66403ee39c2ee58024b9582b95649a4fc5Douglas Gregor    QualifiedTemplateNames.InsertNode(QTN, InsertPos);
5442d99cbe66403ee39c2ee58024b9582b95649a4fc5Douglas Gregor  }
54431eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
5444d99cbe66403ee39c2ee58024b9582b95649a4fc5Douglas Gregor  return TemplateName(QTN);
5445d99cbe66403ee39c2ee58024b9582b95649a4fc5Douglas Gregor}
5446d99cbe66403ee39c2ee58024b9582b95649a4fc5Douglas Gregor
54477532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor/// \brief Retrieve the template name that represents a dependent
54487532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor/// template name such as \c MetaFun::template apply.
54494ba2a17694148e16eaa8d3917f657ffcd3667be4Jay FoadTemplateName
54504ba2a17694148e16eaa8d3917f657ffcd3667be4Jay FoadASTContext::getDependentTemplateName(NestedNameSpecifier *NNS,
54514ba2a17694148e16eaa8d3917f657ffcd3667be4Jay Foad                                     const IdentifierInfo *Name) const {
54521eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  assert((!NNS || NNS->isDependent()) &&
54533b6afbb99a1c44b4076f8e15fb7311405941b306Douglas Gregor         "Nested name specifier must be dependent");
54547532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor
54557532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor  llvm::FoldingSetNodeID ID;
54567532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor  DependentTemplateName::Profile(ID, NNS, Name);
54577532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor
54587532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor  void *InsertPos = 0;
54597532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor  DependentTemplateName *QTN =
54607532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor    DependentTemplateNames.FindNodeOrInsertPos(ID, InsertPos);
54617532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor
54627532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor  if (QTN)
54637532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor    return TemplateName(QTN);
54647532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor
54657532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor  NestedNameSpecifier *CanonNNS = getCanonicalNestedNameSpecifier(NNS);
54667532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor  if (CanonNNS == NNS) {
54677532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor    QTN = new (*this,4) DependentTemplateName(NNS, Name);
54687532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor  } else {
54697532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor    TemplateName Canon = getDependentTemplateName(CanonNNS, Name);
54707532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor    QTN = new (*this,4) DependentTemplateName(NNS, Name, Canon);
5471789b1f640205e81b5af250693246120f1ce9d147Douglas Gregor    DependentTemplateName *CheckQTN =
5472789b1f640205e81b5af250693246120f1ce9d147Douglas Gregor      DependentTemplateNames.FindNodeOrInsertPos(ID, InsertPos);
5473789b1f640205e81b5af250693246120f1ce9d147Douglas Gregor    assert(!CheckQTN && "Dependent type name canonicalization broken");
5474789b1f640205e81b5af250693246120f1ce9d147Douglas Gregor    (void)CheckQTN;
54757532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor  }
54767532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor
54777532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor  DependentTemplateNames.InsertNode(QTN, InsertPos);
54787532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor  return TemplateName(QTN);
54797532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor}
54807532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor
5481ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor/// \brief Retrieve the template name that represents a dependent
5482ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor/// template name such as \c MetaFun::template operator+.
5483ca1bdd7c269a2390d43c040a60511edd017ee130Douglas GregorTemplateName
5484ca1bdd7c269a2390d43c040a60511edd017ee130Douglas GregorASTContext::getDependentTemplateName(NestedNameSpecifier *NNS,
54854ba2a17694148e16eaa8d3917f657ffcd3667be4Jay Foad                                     OverloadedOperatorKind Operator) const {
5486ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor  assert((!NNS || NNS->isDependent()) &&
5487ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor         "Nested name specifier must be dependent");
5488ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor
5489ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor  llvm::FoldingSetNodeID ID;
5490ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor  DependentTemplateName::Profile(ID, NNS, Operator);
5491ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor
5492ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor  void *InsertPos = 0;
5493789b1f640205e81b5af250693246120f1ce9d147Douglas Gregor  DependentTemplateName *QTN
5494789b1f640205e81b5af250693246120f1ce9d147Douglas Gregor    = DependentTemplateNames.FindNodeOrInsertPos(ID, InsertPos);
5495ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor
5496ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor  if (QTN)
5497ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor    return TemplateName(QTN);
5498ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor
5499ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor  NestedNameSpecifier *CanonNNS = getCanonicalNestedNameSpecifier(NNS);
5500ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor  if (CanonNNS == NNS) {
5501ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor    QTN = new (*this,4) DependentTemplateName(NNS, Operator);
5502ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor  } else {
5503ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor    TemplateName Canon = getDependentTemplateName(CanonNNS, Operator);
5504ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor    QTN = new (*this,4) DependentTemplateName(NNS, Operator, Canon);
5505789b1f640205e81b5af250693246120f1ce9d147Douglas Gregor
5506789b1f640205e81b5af250693246120f1ce9d147Douglas Gregor    DependentTemplateName *CheckQTN
5507789b1f640205e81b5af250693246120f1ce9d147Douglas Gregor      = DependentTemplateNames.FindNodeOrInsertPos(ID, InsertPos);
5508789b1f640205e81b5af250693246120f1ce9d147Douglas Gregor    assert(!CheckQTN && "Dependent template name canonicalization broken");
5509789b1f640205e81b5af250693246120f1ce9d147Douglas Gregor    (void)CheckQTN;
5510ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor  }
5511ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor
5512ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor  DependentTemplateNames.InsertNode(QTN, InsertPos);
5513ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor  return TemplateName(QTN);
5514ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor}
5515ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor
55161aee05d08b2184acadeb36de300e216390780d6cDouglas GregorTemplateName
5517146060435c3efce95c95a092c7a1eb651cfb9ae0John McCallASTContext::getSubstTemplateTemplateParm(TemplateTemplateParmDecl *param,
5518146060435c3efce95c95a092c7a1eb651cfb9ae0John McCall                                         TemplateName replacement) const {
5519146060435c3efce95c95a092c7a1eb651cfb9ae0John McCall  llvm::FoldingSetNodeID ID;
5520146060435c3efce95c95a092c7a1eb651cfb9ae0John McCall  SubstTemplateTemplateParmStorage::Profile(ID, param, replacement);
5521146060435c3efce95c95a092c7a1eb651cfb9ae0John McCall
5522146060435c3efce95c95a092c7a1eb651cfb9ae0John McCall  void *insertPos = 0;
5523146060435c3efce95c95a092c7a1eb651cfb9ae0John McCall  SubstTemplateTemplateParmStorage *subst
5524146060435c3efce95c95a092c7a1eb651cfb9ae0John McCall    = SubstTemplateTemplateParms.FindNodeOrInsertPos(ID, insertPos);
5525146060435c3efce95c95a092c7a1eb651cfb9ae0John McCall
5526146060435c3efce95c95a092c7a1eb651cfb9ae0John McCall  if (!subst) {
5527146060435c3efce95c95a092c7a1eb651cfb9ae0John McCall    subst = new (*this) SubstTemplateTemplateParmStorage(param, replacement);
5528146060435c3efce95c95a092c7a1eb651cfb9ae0John McCall    SubstTemplateTemplateParms.InsertNode(subst, insertPos);
5529146060435c3efce95c95a092c7a1eb651cfb9ae0John McCall  }
5530146060435c3efce95c95a092c7a1eb651cfb9ae0John McCall
5531146060435c3efce95c95a092c7a1eb651cfb9ae0John McCall  return TemplateName(subst);
5532146060435c3efce95c95a092c7a1eb651cfb9ae0John McCall}
5533146060435c3efce95c95a092c7a1eb651cfb9ae0John McCall
5534146060435c3efce95c95a092c7a1eb651cfb9ae0John McCallTemplateName
55351aee05d08b2184acadeb36de300e216390780d6cDouglas GregorASTContext::getSubstTemplateTemplateParmPack(TemplateTemplateParmDecl *Param,
55361aee05d08b2184acadeb36de300e216390780d6cDouglas Gregor                                       const TemplateArgument &ArgPack) const {
55371aee05d08b2184acadeb36de300e216390780d6cDouglas Gregor  ASTContext &Self = const_cast<ASTContext &>(*this);
55381aee05d08b2184acadeb36de300e216390780d6cDouglas Gregor  llvm::FoldingSetNodeID ID;
55391aee05d08b2184acadeb36de300e216390780d6cDouglas Gregor  SubstTemplateTemplateParmPackStorage::Profile(ID, Self, Param, ArgPack);
55401aee05d08b2184acadeb36de300e216390780d6cDouglas Gregor
55411aee05d08b2184acadeb36de300e216390780d6cDouglas Gregor  void *InsertPos = 0;
55421aee05d08b2184acadeb36de300e216390780d6cDouglas Gregor  SubstTemplateTemplateParmPackStorage *Subst
55431aee05d08b2184acadeb36de300e216390780d6cDouglas Gregor    = SubstTemplateTemplateParmPacks.FindNodeOrInsertPos(ID, InsertPos);
55441aee05d08b2184acadeb36de300e216390780d6cDouglas Gregor
55451aee05d08b2184acadeb36de300e216390780d6cDouglas Gregor  if (!Subst) {
5546146060435c3efce95c95a092c7a1eb651cfb9ae0John McCall    Subst = new (*this) SubstTemplateTemplateParmPackStorage(Param,
55471aee05d08b2184acadeb36de300e216390780d6cDouglas Gregor                                                           ArgPack.pack_size(),
55481aee05d08b2184acadeb36de300e216390780d6cDouglas Gregor                                                         ArgPack.pack_begin());
55491aee05d08b2184acadeb36de300e216390780d6cDouglas Gregor    SubstTemplateTemplateParmPacks.InsertNode(Subst, InsertPos);
55501aee05d08b2184acadeb36de300e216390780d6cDouglas Gregor  }
55511aee05d08b2184acadeb36de300e216390780d6cDouglas Gregor
55521aee05d08b2184acadeb36de300e216390780d6cDouglas Gregor  return TemplateName(Subst);
55531aee05d08b2184acadeb36de300e216390780d6cDouglas Gregor}
55541aee05d08b2184acadeb36de300e216390780d6cDouglas Gregor
5555b4e66d5259f90e9aae4d40fc5de801e046c7df94Douglas Gregor/// getFromTargetType - Given one of the integer types provided by
5556d934112e6170b0fd940d8e40db6936cea2cdcf62Douglas Gregor/// TargetInfo, produce the corresponding type. The unsigned @p Type
5557d934112e6170b0fd940d8e40db6936cea2cdcf62Douglas Gregor/// is actually a value of type @c TargetInfo::IntType.
5558e27ec8ad56dbf1efb2de004b90fbbb86f740e3f1John McCallCanQualType ASTContext::getFromTargetType(unsigned Type) const {
5559b4e66d5259f90e9aae4d40fc5de801e046c7df94Douglas Gregor  switch (Type) {
5560e27ec8ad56dbf1efb2de004b90fbbb86f740e3f1John McCall  case TargetInfo::NoInt: return CanQualType();
5561b4e66d5259f90e9aae4d40fc5de801e046c7df94Douglas Gregor  case TargetInfo::SignedShort: return ShortTy;
5562b4e66d5259f90e9aae4d40fc5de801e046c7df94Douglas Gregor  case TargetInfo::UnsignedShort: return UnsignedShortTy;
5563b4e66d5259f90e9aae4d40fc5de801e046c7df94Douglas Gregor  case TargetInfo::SignedInt: return IntTy;
5564b4e66d5259f90e9aae4d40fc5de801e046c7df94Douglas Gregor  case TargetInfo::UnsignedInt: return UnsignedIntTy;
5565b4e66d5259f90e9aae4d40fc5de801e046c7df94Douglas Gregor  case TargetInfo::SignedLong: return LongTy;
5566b4e66d5259f90e9aae4d40fc5de801e046c7df94Douglas Gregor  case TargetInfo::UnsignedLong: return UnsignedLongTy;
5567b4e66d5259f90e9aae4d40fc5de801e046c7df94Douglas Gregor  case TargetInfo::SignedLongLong: return LongLongTy;
5568b4e66d5259f90e9aae4d40fc5de801e046c7df94Douglas Gregor  case TargetInfo::UnsignedLongLong: return UnsignedLongLongTy;
5569b4e66d5259f90e9aae4d40fc5de801e046c7df94Douglas Gregor  }
5570b4e66d5259f90e9aae4d40fc5de801e046c7df94Douglas Gregor
5571b219cfc4d75f0a03630b7c4509ef791b7e97b2c8David Blaikie  llvm_unreachable("Unhandled TargetInfo::IntType value");
5572b4e66d5259f90e9aae4d40fc5de801e046c7df94Douglas Gregor}
5573b6ccaac65ca72f72954eb3893bbd940bedd23f00Ted Kremenek
5574b6ccaac65ca72f72954eb3893bbd940bedd23f00Ted Kremenek//===----------------------------------------------------------------------===//
5575b6ccaac65ca72f72954eb3893bbd940bedd23f00Ted Kremenek//                        Type Predicates.
5576b6ccaac65ca72f72954eb3893bbd940bedd23f00Ted Kremenek//===----------------------------------------------------------------------===//
5577b6ccaac65ca72f72954eb3893bbd940bedd23f00Ted Kremenek
55784fd83ea566f4a0c083001c84b75da6cc8c99c1d6Fariborz Jahanian/// getObjCGCAttr - Returns one of GCNone, Weak or Strong objc's
55794fd83ea566f4a0c083001c84b75da6cc8c99c1d6Fariborz Jahanian/// garbage collection attribute.
55804fd83ea566f4a0c083001c84b75da6cc8c99c1d6Fariborz Jahanian///
5581ae278a3a57595349a411f6474938d4dd1b263a0eJohn McCallQualifiers::GC ASTContext::getObjCGCAttrKind(QualType Ty) const {
55824e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie  if (getLangOpts().getGC() == LangOptions::NonGC)
5583ae278a3a57595349a411f6474938d4dd1b263a0eJohn McCall    return Qualifiers::GCNone;
5584ae278a3a57595349a411f6474938d4dd1b263a0eJohn McCall
55854e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie  assert(getLangOpts().ObjC1);
5586ae278a3a57595349a411f6474938d4dd1b263a0eJohn McCall  Qualifiers::GC GCAttrs = Ty.getObjCGCAttr();
5587ae278a3a57595349a411f6474938d4dd1b263a0eJohn McCall
5588ae278a3a57595349a411f6474938d4dd1b263a0eJohn McCall  // Default behaviour under objective-C's gc is for ObjC pointers
5589ae278a3a57595349a411f6474938d4dd1b263a0eJohn McCall  // (or pointers to them) be treated as though they were declared
5590ae278a3a57595349a411f6474938d4dd1b263a0eJohn McCall  // as __strong.
5591ae278a3a57595349a411f6474938d4dd1b263a0eJohn McCall  if (GCAttrs == Qualifiers::GCNone) {
5592ae278a3a57595349a411f6474938d4dd1b263a0eJohn McCall    if (Ty->isObjCObjectPointerType() || Ty->isBlockPointerType())
5593ae278a3a57595349a411f6474938d4dd1b263a0eJohn McCall      return Qualifiers::Strong;
5594ae278a3a57595349a411f6474938d4dd1b263a0eJohn McCall    else if (Ty->isPointerType())
5595ae278a3a57595349a411f6474938d4dd1b263a0eJohn McCall      return getObjCGCAttrKind(Ty->getAs<PointerType>()->getPointeeType());
5596ae278a3a57595349a411f6474938d4dd1b263a0eJohn McCall  } else {
5597ae278a3a57595349a411f6474938d4dd1b263a0eJohn McCall    // It's not valid to set GC attributes on anything that isn't a
5598ae278a3a57595349a411f6474938d4dd1b263a0eJohn McCall    // pointer.
5599ae278a3a57595349a411f6474938d4dd1b263a0eJohn McCall#ifndef NDEBUG
5600ae278a3a57595349a411f6474938d4dd1b263a0eJohn McCall    QualType CT = Ty->getCanonicalTypeInternal();
5601ae278a3a57595349a411f6474938d4dd1b263a0eJohn McCall    while (const ArrayType *AT = dyn_cast<ArrayType>(CT))
5602ae278a3a57595349a411f6474938d4dd1b263a0eJohn McCall      CT = AT->getElementType();
5603ae278a3a57595349a411f6474938d4dd1b263a0eJohn McCall    assert(CT->isAnyPointerType() || CT->isBlockPointerType());
5604ae278a3a57595349a411f6474938d4dd1b263a0eJohn McCall#endif
56054fd83ea566f4a0c083001c84b75da6cc8c99c1d6Fariborz Jahanian  }
5606b7d2553edd2532d29b98b9e76bcf6a62bc48b417Chris Lattner  return GCAttrs;
56074fd83ea566f4a0c083001c84b75da6cc8c99c1d6Fariborz Jahanian}
56084fd83ea566f4a0c083001c84b75da6cc8c99c1d6Fariborz Jahanian
56096ac46a4a521366d7ab36ebe2ce4e624ab96b06f9Chris Lattner//===----------------------------------------------------------------------===//
56106ac46a4a521366d7ab36ebe2ce4e624ab96b06f9Chris Lattner//                        Type Compatibility Testing
56116ac46a4a521366d7ab36ebe2ce4e624ab96b06f9Chris Lattner//===----------------------------------------------------------------------===//
5612770951b5bb6028a8d326ddb4a13cef7d4a128162Chris Lattner
56131eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// areCompatVectorTypes - Return true if the two specified vector types are
56146ac46a4a521366d7ab36ebe2ce4e624ab96b06f9Chris Lattner/// compatible.
56156ac46a4a521366d7ab36ebe2ce4e624ab96b06f9Chris Lattnerstatic bool areCompatVectorTypes(const VectorType *LHS,
56166ac46a4a521366d7ab36ebe2ce4e624ab96b06f9Chris Lattner                                 const VectorType *RHS) {
5617467b27b9a24bdc823218ad1ad0e37673b6cc1e83John McCall  assert(LHS->isCanonicalUnqualified() && RHS->isCanonicalUnqualified());
56186ac46a4a521366d7ab36ebe2ce4e624ab96b06f9Chris Lattner  return LHS->getElementType() == RHS->getElementType() &&
561961710854be2b098428aff5316e64bd34b30fbcb7Chris Lattner         LHS->getNumElements() == RHS->getNumElements();
56206ac46a4a521366d7ab36ebe2ce4e624ab96b06f9Chris Lattner}
56216ac46a4a521366d7ab36ebe2ce4e624ab96b06f9Chris Lattner
5622255210ef415b9893f0e3794e8d9a704194c12f3cDouglas Gregorbool ASTContext::areCompatibleVectorTypes(QualType FirstVec,
5623255210ef415b9893f0e3794e8d9a704194c12f3cDouglas Gregor                                          QualType SecondVec) {
5624255210ef415b9893f0e3794e8d9a704194c12f3cDouglas Gregor  assert(FirstVec->isVectorType() && "FirstVec should be a vector type");
5625255210ef415b9893f0e3794e8d9a704194c12f3cDouglas Gregor  assert(SecondVec->isVectorType() && "SecondVec should be a vector type");
5626255210ef415b9893f0e3794e8d9a704194c12f3cDouglas Gregor
5627255210ef415b9893f0e3794e8d9a704194c12f3cDouglas Gregor  if (hasSameUnqualifiedType(FirstVec, SecondVec))
5628255210ef415b9893f0e3794e8d9a704194c12f3cDouglas Gregor    return true;
5629255210ef415b9893f0e3794e8d9a704194c12f3cDouglas Gregor
5630f69eb7cf8e616b5aad7911ec6f79b24b0a009227Bob Wilson  // Treat Neon vector types and most AltiVec vector types as if they are the
5631f69eb7cf8e616b5aad7911ec6f79b24b0a009227Bob Wilson  // equivalent GCC vector types.
5632255210ef415b9893f0e3794e8d9a704194c12f3cDouglas Gregor  const VectorType *First = FirstVec->getAs<VectorType>();
5633255210ef415b9893f0e3794e8d9a704194c12f3cDouglas Gregor  const VectorType *Second = SecondVec->getAs<VectorType>();
5634f69eb7cf8e616b5aad7911ec6f79b24b0a009227Bob Wilson  if (First->getNumElements() == Second->getNumElements() &&
5635255210ef415b9893f0e3794e8d9a704194c12f3cDouglas Gregor      hasSameType(First->getElementType(), Second->getElementType()) &&
5636f69eb7cf8e616b5aad7911ec6f79b24b0a009227Bob Wilson      First->getVectorKind() != VectorType::AltiVecPixel &&
5637f69eb7cf8e616b5aad7911ec6f79b24b0a009227Bob Wilson      First->getVectorKind() != VectorType::AltiVecBool &&
5638f69eb7cf8e616b5aad7911ec6f79b24b0a009227Bob Wilson      Second->getVectorKind() != VectorType::AltiVecPixel &&
5639f69eb7cf8e616b5aad7911ec6f79b24b0a009227Bob Wilson      Second->getVectorKind() != VectorType::AltiVecBool)
5640255210ef415b9893f0e3794e8d9a704194c12f3cDouglas Gregor    return true;
5641255210ef415b9893f0e3794e8d9a704194c12f3cDouglas Gregor
5642255210ef415b9893f0e3794e8d9a704194c12f3cDouglas Gregor  return false;
5643255210ef415b9893f0e3794e8d9a704194c12f3cDouglas Gregor}
5644255210ef415b9893f0e3794e8d9a704194c12f3cDouglas Gregor
56454084c306635b70f37029dca938444e6013f08684Steve Naroff//===----------------------------------------------------------------------===//
56464084c306635b70f37029dca938444e6013f08684Steve Naroff// ObjCQualifiedIdTypesAreCompatible - Compatibility testing for qualified id's.
56474084c306635b70f37029dca938444e6013f08684Steve Naroff//===----------------------------------------------------------------------===//
56484084c306635b70f37029dca938444e6013f08684Steve Naroff
56494084c306635b70f37029dca938444e6013f08684Steve Naroff/// ProtocolCompatibleWithProtocol - return 'true' if 'lProto' is in the
56504084c306635b70f37029dca938444e6013f08684Steve Naroff/// inheritance hierarchy of 'rProto'.
56514ba2a17694148e16eaa8d3917f657ffcd3667be4Jay Foadbool
56524ba2a17694148e16eaa8d3917f657ffcd3667be4Jay FoadASTContext::ProtocolCompatibleWithProtocol(ObjCProtocolDecl *lProto,
56534ba2a17694148e16eaa8d3917f657ffcd3667be4Jay Foad                                           ObjCProtocolDecl *rProto) const {
56543fc73ee0c613715ebce78e30b4d050ea715a007dDouglas Gregor  if (declaresSameEntity(lProto, rProto))
565514108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff    return true;
56564084c306635b70f37029dca938444e6013f08684Steve Naroff  for (ObjCProtocolDecl::protocol_iterator PI = rProto->protocol_begin(),
56574084c306635b70f37029dca938444e6013f08684Steve Naroff       E = rProto->protocol_end(); PI != E; ++PI)
56584084c306635b70f37029dca938444e6013f08684Steve Naroff    if (ProtocolCompatibleWithProtocol(lProto, *PI))
56594084c306635b70f37029dca938444e6013f08684Steve Naroff      return true;
56604084c306635b70f37029dca938444e6013f08684Steve Naroff  return false;
56614084c306635b70f37029dca938444e6013f08684Steve Naroff}
566214108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff
56634084c306635b70f37029dca938444e6013f08684Steve Naroff/// QualifiedIdConformsQualifiedId - compare id<p,...> with id<p1,...>
56644084c306635b70f37029dca938444e6013f08684Steve Naroff/// return true if lhs's protocols conform to rhs's protocol; false
56654084c306635b70f37029dca938444e6013f08684Steve Naroff/// otherwise.
56664084c306635b70f37029dca938444e6013f08684Steve Naroffbool ASTContext::QualifiedIdConformsQualifiedId(QualType lhs, QualType rhs) {
56674084c306635b70f37029dca938444e6013f08684Steve Naroff  if (lhs->isObjCQualifiedIdType() && rhs->isObjCQualifiedIdType())
56684084c306635b70f37029dca938444e6013f08684Steve Naroff    return ObjCQualifiedIdTypesAreCompatible(lhs, rhs, false);
56694084c306635b70f37029dca938444e6013f08684Steve Naroff  return false;
56704084c306635b70f37029dca938444e6013f08684Steve Naroff}
56714084c306635b70f37029dca938444e6013f08684Steve Naroff
5672a8f8dac6a29f6d33474a38a32ce9dd859b696da9Fariborz Jahanian/// ObjCQualifiedClassTypesAreCompatible - compare  Class<p,...> and
5673a8f8dac6a29f6d33474a38a32ce9dd859b696da9Fariborz Jahanian/// Class<p1, ...>.
5674a8f8dac6a29f6d33474a38a32ce9dd859b696da9Fariborz Jahanianbool ASTContext::ObjCQualifiedClassTypesAreCompatible(QualType lhs,
5675a8f8dac6a29f6d33474a38a32ce9dd859b696da9Fariborz Jahanian                                                      QualType rhs) {
5676a8f8dac6a29f6d33474a38a32ce9dd859b696da9Fariborz Jahanian  const ObjCObjectPointerType *lhsQID = lhs->getAs<ObjCObjectPointerType>();
5677a8f8dac6a29f6d33474a38a32ce9dd859b696da9Fariborz Jahanian  const ObjCObjectPointerType *rhsOPT = rhs->getAs<ObjCObjectPointerType>();
5678a8f8dac6a29f6d33474a38a32ce9dd859b696da9Fariborz Jahanian  assert ((lhsQID && rhsOPT) && "ObjCQualifiedClassTypesAreCompatible");
5679a8f8dac6a29f6d33474a38a32ce9dd859b696da9Fariborz Jahanian
5680a8f8dac6a29f6d33474a38a32ce9dd859b696da9Fariborz Jahanian  for (ObjCObjectPointerType::qual_iterator I = lhsQID->qual_begin(),
5681a8f8dac6a29f6d33474a38a32ce9dd859b696da9Fariborz Jahanian       E = lhsQID->qual_end(); I != E; ++I) {
5682a8f8dac6a29f6d33474a38a32ce9dd859b696da9Fariborz Jahanian    bool match = false;
5683a8f8dac6a29f6d33474a38a32ce9dd859b696da9Fariborz Jahanian    ObjCProtocolDecl *lhsProto = *I;
5684a8f8dac6a29f6d33474a38a32ce9dd859b696da9Fariborz Jahanian    for (ObjCObjectPointerType::qual_iterator J = rhsOPT->qual_begin(),
5685a8f8dac6a29f6d33474a38a32ce9dd859b696da9Fariborz Jahanian         E = rhsOPT->qual_end(); J != E; ++J) {
5686a8f8dac6a29f6d33474a38a32ce9dd859b696da9Fariborz Jahanian      ObjCProtocolDecl *rhsProto = *J;
5687a8f8dac6a29f6d33474a38a32ce9dd859b696da9Fariborz Jahanian      if (ProtocolCompatibleWithProtocol(lhsProto, rhsProto)) {
5688a8f8dac6a29f6d33474a38a32ce9dd859b696da9Fariborz Jahanian        match = true;
5689a8f8dac6a29f6d33474a38a32ce9dd859b696da9Fariborz Jahanian        break;
5690a8f8dac6a29f6d33474a38a32ce9dd859b696da9Fariborz Jahanian      }
5691a8f8dac6a29f6d33474a38a32ce9dd859b696da9Fariborz Jahanian    }
5692a8f8dac6a29f6d33474a38a32ce9dd859b696da9Fariborz Jahanian    if (!match)
5693a8f8dac6a29f6d33474a38a32ce9dd859b696da9Fariborz Jahanian      return false;
5694a8f8dac6a29f6d33474a38a32ce9dd859b696da9Fariborz Jahanian  }
5695a8f8dac6a29f6d33474a38a32ce9dd859b696da9Fariborz Jahanian  return true;
5696a8f8dac6a29f6d33474a38a32ce9dd859b696da9Fariborz Jahanian}
5697a8f8dac6a29f6d33474a38a32ce9dd859b696da9Fariborz Jahanian
56984084c306635b70f37029dca938444e6013f08684Steve Naroff/// ObjCQualifiedIdTypesAreCompatible - We know that one of lhs/rhs is an
56994084c306635b70f37029dca938444e6013f08684Steve Naroff/// ObjCQualifiedIDType.
57004084c306635b70f37029dca938444e6013f08684Steve Naroffbool ASTContext::ObjCQualifiedIdTypesAreCompatible(QualType lhs, QualType rhs,
57014084c306635b70f37029dca938444e6013f08684Steve Naroff                                                   bool compare) {
57024084c306635b70f37029dca938444e6013f08684Steve Naroff  // Allow id<P..> and an 'id' or void* type in all cases.
57031eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  if (lhs->isVoidPointerType() ||
57044084c306635b70f37029dca938444e6013f08684Steve Naroff      lhs->isObjCIdType() || lhs->isObjCClassType())
57054084c306635b70f37029dca938444e6013f08684Steve Naroff    return true;
57061eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  else if (rhs->isVoidPointerType() ||
57074084c306635b70f37029dca938444e6013f08684Steve Naroff           rhs->isObjCIdType() || rhs->isObjCClassType())
57084084c306635b70f37029dca938444e6013f08684Steve Naroff    return true;
57094084c306635b70f37029dca938444e6013f08684Steve Naroff
57104084c306635b70f37029dca938444e6013f08684Steve Naroff  if (const ObjCObjectPointerType *lhsQID = lhs->getAsObjCQualifiedIdType()) {
5711183700f494ec9b6701b6efe82bcb25f4c79ba561John McCall    const ObjCObjectPointerType *rhsOPT = rhs->getAs<ObjCObjectPointerType>();
57121eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
57134084c306635b70f37029dca938444e6013f08684Steve Naroff    if (!rhsOPT) return false;
57141eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
57154084c306635b70f37029dca938444e6013f08684Steve Naroff    if (rhsOPT->qual_empty()) {
57161eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      // If the RHS is a unqualified interface pointer "NSString*",
57174084c306635b70f37029dca938444e6013f08684Steve Naroff      // make sure we check the class hierarchy.
57184084c306635b70f37029dca938444e6013f08684Steve Naroff      if (ObjCInterfaceDecl *rhsID = rhsOPT->getInterfaceDecl()) {
57194084c306635b70f37029dca938444e6013f08684Steve Naroff        for (ObjCObjectPointerType::qual_iterator I = lhsQID->qual_begin(),
57204084c306635b70f37029dca938444e6013f08684Steve Naroff             E = lhsQID->qual_end(); I != E; ++I) {
57214084c306635b70f37029dca938444e6013f08684Steve Naroff          // when comparing an id<P> on lhs with a static type on rhs,
57224084c306635b70f37029dca938444e6013f08684Steve Naroff          // see if static class implements all of id's protocols, directly or
57234084c306635b70f37029dca938444e6013f08684Steve Naroff          // through its super class and categories.
57240fd8904c5f71a11d29f67716c3ebdf7ad1c855fbFariborz Jahanian          if (!rhsID->ClassImplementsProtocol(*I, true))
57254084c306635b70f37029dca938444e6013f08684Steve Naroff            return false;
57264084c306635b70f37029dca938444e6013f08684Steve Naroff        }
57274084c306635b70f37029dca938444e6013f08684Steve Naroff      }
57284084c306635b70f37029dca938444e6013f08684Steve Naroff      // If there are no qualifiers and no interface, we have an 'id'.
57294084c306635b70f37029dca938444e6013f08684Steve Naroff      return true;
57304084c306635b70f37029dca938444e6013f08684Steve Naroff    }
57311eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    // Both the right and left sides have qualifiers.
57324084c306635b70f37029dca938444e6013f08684Steve Naroff    for (ObjCObjectPointerType::qual_iterator I = lhsQID->qual_begin(),
57334084c306635b70f37029dca938444e6013f08684Steve Naroff         E = lhsQID->qual_end(); I != E; ++I) {
57344084c306635b70f37029dca938444e6013f08684Steve Naroff      ObjCProtocolDecl *lhsProto = *I;
57354084c306635b70f37029dca938444e6013f08684Steve Naroff      bool match = false;
5736de2e22d33afec98324a66a358dfe0951b3c7259aSteve Naroff
5737de2e22d33afec98324a66a358dfe0951b3c7259aSteve Naroff      // when comparing an id<P> on lhs with a static type on rhs,
5738de2e22d33afec98324a66a358dfe0951b3c7259aSteve Naroff      // see if static class implements all of id's protocols, directly or
5739de2e22d33afec98324a66a358dfe0951b3c7259aSteve Naroff      // through its super class and categories.
57404084c306635b70f37029dca938444e6013f08684Steve Naroff      for (ObjCObjectPointerType::qual_iterator J = rhsOPT->qual_begin(),
57414084c306635b70f37029dca938444e6013f08684Steve Naroff           E = rhsOPT->qual_end(); J != E; ++J) {
57424084c306635b70f37029dca938444e6013f08684Steve Naroff        ObjCProtocolDecl *rhsProto = *J;
57434084c306635b70f37029dca938444e6013f08684Steve Naroff        if (ProtocolCompatibleWithProtocol(lhsProto, rhsProto) ||
57444084c306635b70f37029dca938444e6013f08684Steve Naroff            (compare && ProtocolCompatibleWithProtocol(rhsProto, lhsProto))) {
57454084c306635b70f37029dca938444e6013f08684Steve Naroff          match = true;
57468f16756441450ed9fb39316e47d107fc2a1ef35bSteve Naroff          break;
57478f16756441450ed9fb39316e47d107fc2a1ef35bSteve Naroff        }
5748de2e22d33afec98324a66a358dfe0951b3c7259aSteve Naroff      }
57491eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      // If the RHS is a qualified interface pointer "NSString<P>*",
57504084c306635b70f37029dca938444e6013f08684Steve Naroff      // make sure we check the class hierarchy.
57514084c306635b70f37029dca938444e6013f08684Steve Naroff      if (ObjCInterfaceDecl *rhsID = rhsOPT->getInterfaceDecl()) {
57524084c306635b70f37029dca938444e6013f08684Steve Naroff        for (ObjCObjectPointerType::qual_iterator I = lhsQID->qual_begin(),
57534084c306635b70f37029dca938444e6013f08684Steve Naroff             E = lhsQID->qual_end(); I != E; ++I) {
57544084c306635b70f37029dca938444e6013f08684Steve Naroff          // when comparing an id<P> on lhs with a static type on rhs,
57554084c306635b70f37029dca938444e6013f08684Steve Naroff          // see if static class implements all of id's protocols, directly or
57564084c306635b70f37029dca938444e6013f08684Steve Naroff          // through its super class and categories.
57570fd8904c5f71a11d29f67716c3ebdf7ad1c855fbFariborz Jahanian          if (rhsID->ClassImplementsProtocol(*I, true)) {
57584084c306635b70f37029dca938444e6013f08684Steve Naroff            match = true;
57594084c306635b70f37029dca938444e6013f08684Steve Naroff            break;
57604084c306635b70f37029dca938444e6013f08684Steve Naroff          }
57614084c306635b70f37029dca938444e6013f08684Steve Naroff        }
57624084c306635b70f37029dca938444e6013f08684Steve Naroff      }
57634084c306635b70f37029dca938444e6013f08684Steve Naroff      if (!match)
5764de2e22d33afec98324a66a358dfe0951b3c7259aSteve Naroff        return false;
5765de2e22d33afec98324a66a358dfe0951b3c7259aSteve Naroff    }
57661eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
5767de2e22d33afec98324a66a358dfe0951b3c7259aSteve Naroff    return true;
5768de2e22d33afec98324a66a358dfe0951b3c7259aSteve Naroff  }
57691eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
57704084c306635b70f37029dca938444e6013f08684Steve Naroff  const ObjCObjectPointerType *rhsQID = rhs->getAsObjCQualifiedIdType();
57714084c306635b70f37029dca938444e6013f08684Steve Naroff  assert(rhsQID && "One of the LHS/RHS should be id<x>");
57724084c306635b70f37029dca938444e6013f08684Steve Naroff
57731eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  if (const ObjCObjectPointerType *lhsOPT =
57744084c306635b70f37029dca938444e6013f08684Steve Naroff        lhs->getAsObjCInterfacePointerType()) {
5775de5b17ea3c74d2844ed035a1edfa6479866139b5Fariborz Jahanian    // If both the right and left sides have qualifiers.
57764084c306635b70f37029dca938444e6013f08684Steve Naroff    for (ObjCObjectPointerType::qual_iterator I = lhsOPT->qual_begin(),
57774084c306635b70f37029dca938444e6013f08684Steve Naroff         E = lhsOPT->qual_end(); I != E; ++I) {
57784084c306635b70f37029dca938444e6013f08684Steve Naroff      ObjCProtocolDecl *lhsProto = *I;
57794084c306635b70f37029dca938444e6013f08684Steve Naroff      bool match = false;
57804084c306635b70f37029dca938444e6013f08684Steve Naroff
5781de5b17ea3c74d2844ed035a1edfa6479866139b5Fariborz Jahanian      // when comparing an id<P> on rhs with a static type on lhs,
57824084c306635b70f37029dca938444e6013f08684Steve Naroff      // see if static class implements all of id's protocols, directly or
57834084c306635b70f37029dca938444e6013f08684Steve Naroff      // through its super class and categories.
5784de5b17ea3c74d2844ed035a1edfa6479866139b5Fariborz Jahanian      // First, lhs protocols in the qualifier list must be found, direct
5785de5b17ea3c74d2844ed035a1edfa6479866139b5Fariborz Jahanian      // or indirect in rhs's qualifier list or it is a mismatch.
57864084c306635b70f37029dca938444e6013f08684Steve Naroff      for (ObjCObjectPointerType::qual_iterator J = rhsQID->qual_begin(),
57874084c306635b70f37029dca938444e6013f08684Steve Naroff           E = rhsQID->qual_end(); J != E; ++J) {
57884084c306635b70f37029dca938444e6013f08684Steve Naroff        ObjCProtocolDecl *rhsProto = *J;
57894084c306635b70f37029dca938444e6013f08684Steve Naroff        if (ProtocolCompatibleWithProtocol(lhsProto, rhsProto) ||
57904084c306635b70f37029dca938444e6013f08684Steve Naroff            (compare && ProtocolCompatibleWithProtocol(rhsProto, lhsProto))) {
57914084c306635b70f37029dca938444e6013f08684Steve Naroff          match = true;
57924084c306635b70f37029dca938444e6013f08684Steve Naroff          break;
57934084c306635b70f37029dca938444e6013f08684Steve Naroff        }
57944084c306635b70f37029dca938444e6013f08684Steve Naroff      }
57954084c306635b70f37029dca938444e6013f08684Steve Naroff      if (!match)
57964084c306635b70f37029dca938444e6013f08684Steve Naroff        return false;
57974084c306635b70f37029dca938444e6013f08684Steve Naroff    }
5798de5b17ea3c74d2844ed035a1edfa6479866139b5Fariborz Jahanian
5799de5b17ea3c74d2844ed035a1edfa6479866139b5Fariborz Jahanian    // Static class's protocols, or its super class or category protocols
5800de5b17ea3c74d2844ed035a1edfa6479866139b5Fariborz Jahanian    // must be found, direct or indirect in rhs's qualifier list or it is a mismatch.
5801de5b17ea3c74d2844ed035a1edfa6479866139b5Fariborz Jahanian    if (ObjCInterfaceDecl *lhsID = lhsOPT->getInterfaceDecl()) {
5802de5b17ea3c74d2844ed035a1edfa6479866139b5Fariborz Jahanian      llvm::SmallPtrSet<ObjCProtocolDecl *, 8> LHSInheritedProtocols;
5803de5b17ea3c74d2844ed035a1edfa6479866139b5Fariborz Jahanian      CollectInheritedProtocols(lhsID, LHSInheritedProtocols);
5804de5b17ea3c74d2844ed035a1edfa6479866139b5Fariborz Jahanian      // This is rather dubious but matches gcc's behavior. If lhs has
5805de5b17ea3c74d2844ed035a1edfa6479866139b5Fariborz Jahanian      // no type qualifier and its class has no static protocol(s)
5806de5b17ea3c74d2844ed035a1edfa6479866139b5Fariborz Jahanian      // assume that it is mismatch.
5807de5b17ea3c74d2844ed035a1edfa6479866139b5Fariborz Jahanian      if (LHSInheritedProtocols.empty() && lhsOPT->qual_empty())
5808de5b17ea3c74d2844ed035a1edfa6479866139b5Fariborz Jahanian        return false;
5809de5b17ea3c74d2844ed035a1edfa6479866139b5Fariborz Jahanian      for (llvm::SmallPtrSet<ObjCProtocolDecl*,8>::iterator I =
5810de5b17ea3c74d2844ed035a1edfa6479866139b5Fariborz Jahanian           LHSInheritedProtocols.begin(),
5811de5b17ea3c74d2844ed035a1edfa6479866139b5Fariborz Jahanian           E = LHSInheritedProtocols.end(); I != E; ++I) {
5812de5b17ea3c74d2844ed035a1edfa6479866139b5Fariborz Jahanian        bool match = false;
5813de5b17ea3c74d2844ed035a1edfa6479866139b5Fariborz Jahanian        ObjCProtocolDecl *lhsProto = (*I);
5814de5b17ea3c74d2844ed035a1edfa6479866139b5Fariborz Jahanian        for (ObjCObjectPointerType::qual_iterator J = rhsQID->qual_begin(),
5815de5b17ea3c74d2844ed035a1edfa6479866139b5Fariborz Jahanian             E = rhsQID->qual_end(); J != E; ++J) {
5816de5b17ea3c74d2844ed035a1edfa6479866139b5Fariborz Jahanian          ObjCProtocolDecl *rhsProto = *J;
5817de5b17ea3c74d2844ed035a1edfa6479866139b5Fariborz Jahanian          if (ProtocolCompatibleWithProtocol(lhsProto, rhsProto) ||
5818de5b17ea3c74d2844ed035a1edfa6479866139b5Fariborz Jahanian              (compare && ProtocolCompatibleWithProtocol(rhsProto, lhsProto))) {
5819de5b17ea3c74d2844ed035a1edfa6479866139b5Fariborz Jahanian            match = true;
5820de5b17ea3c74d2844ed035a1edfa6479866139b5Fariborz Jahanian            break;
5821de5b17ea3c74d2844ed035a1edfa6479866139b5Fariborz Jahanian          }
5822de5b17ea3c74d2844ed035a1edfa6479866139b5Fariborz Jahanian        }
5823de5b17ea3c74d2844ed035a1edfa6479866139b5Fariborz Jahanian        if (!match)
5824de5b17ea3c74d2844ed035a1edfa6479866139b5Fariborz Jahanian          return false;
5825de5b17ea3c74d2844ed035a1edfa6479866139b5Fariborz Jahanian      }
5826de5b17ea3c74d2844ed035a1edfa6479866139b5Fariborz Jahanian    }
58274084c306635b70f37029dca938444e6013f08684Steve Naroff    return true;
58284084c306635b70f37029dca938444e6013f08684Steve Naroff  }
58294084c306635b70f37029dca938444e6013f08684Steve Naroff  return false;
58304084c306635b70f37029dca938444e6013f08684Steve Naroff}
58314084c306635b70f37029dca938444e6013f08684Steve Naroff
58324084c306635b70f37029dca938444e6013f08684Steve Naroff/// canAssignObjCInterfaces - Return true if the two interface types are
58334084c306635b70f37029dca938444e6013f08684Steve Naroff/// compatible for assignment from RHS to LHS.  This handles validation of any
58344084c306635b70f37029dca938444e6013f08684Steve Naroff/// protocol qualifiers on the LHS or RHS.
58354084c306635b70f37029dca938444e6013f08684Steve Naroff///
58364084c306635b70f37029dca938444e6013f08684Steve Naroffbool ASTContext::canAssignObjCInterfaces(const ObjCObjectPointerType *LHSOPT,
58374084c306635b70f37029dca938444e6013f08684Steve Naroff                                         const ObjCObjectPointerType *RHSOPT) {
5838c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall  const ObjCObjectType* LHS = LHSOPT->getObjectType();
5839c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall  const ObjCObjectType* RHS = RHSOPT->getObjectType();
5840c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall
58414084c306635b70f37029dca938444e6013f08684Steve Naroff  // If either type represents the built-in 'id' or 'Class' types, return true.
5842c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall  if (LHS->isObjCUnqualifiedIdOrClass() ||
5843c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall      RHS->isObjCUnqualifiedIdOrClass())
58444084c306635b70f37029dca938444e6013f08684Steve Naroff    return true;
58454084c306635b70f37029dca938444e6013f08684Steve Naroff
5846c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall  if (LHS->isObjCQualifiedId() || RHS->isObjCQualifiedId())
58471eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    return ObjCQualifiedIdTypesAreCompatible(QualType(LHSOPT,0),
58481eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump                                             QualType(RHSOPT,0),
58494084c306635b70f37029dca938444e6013f08684Steve Naroff                                             false);
5850a8f8dac6a29f6d33474a38a32ce9dd859b696da9Fariborz Jahanian
5851a8f8dac6a29f6d33474a38a32ce9dd859b696da9Fariborz Jahanian  if (LHS->isObjCQualifiedClass() && RHS->isObjCQualifiedClass())
5852a8f8dac6a29f6d33474a38a32ce9dd859b696da9Fariborz Jahanian    return ObjCQualifiedClassTypesAreCompatible(QualType(LHSOPT,0),
5853a8f8dac6a29f6d33474a38a32ce9dd859b696da9Fariborz Jahanian                                                QualType(RHSOPT,0));
5854a8f8dac6a29f6d33474a38a32ce9dd859b696da9Fariborz Jahanian
5855c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall  // If we have 2 user-defined types, fall into that path.
5856c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall  if (LHS->getInterface() && RHS->getInterface())
58574084c306635b70f37029dca938444e6013f08684Steve Naroff    return canAssignObjCInterfaces(LHS, RHS);
58581eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
58594084c306635b70f37029dca938444e6013f08684Steve Naroff  return false;
586014108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff}
586114108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff
5862132f2a2da34f378fc675b9e174564b0f52c31d98Fariborz Jahanian/// canAssignObjCInterfacesInBlockPointer - This routine is specifically written
5863fc8f0e14ad142ed811e90fbd9a30e419e301c717Chris Lattner/// for providing type-safety for objective-c pointers used to pass/return
5864132f2a2da34f378fc675b9e174564b0f52c31d98Fariborz Jahanian/// arguments in block literals. When passed as arguments, passing 'A*' where
5865132f2a2da34f378fc675b9e174564b0f52c31d98Fariborz Jahanian/// 'id' is expected is not OK. Passing 'Sub *" where 'Super *" is expected is
5866132f2a2da34f378fc675b9e174564b0f52c31d98Fariborz Jahanian/// not OK. For the return type, the opposite is not OK.
5867132f2a2da34f378fc675b9e174564b0f52c31d98Fariborz Jahanianbool ASTContext::canAssignObjCInterfacesInBlockPointer(
5868132f2a2da34f378fc675b9e174564b0f52c31d98Fariborz Jahanian                                         const ObjCObjectPointerType *LHSOPT,
5869a4fdbfad150ae37bddaa4094d3925a27a1a1cf3fFariborz Jahanian                                         const ObjCObjectPointerType *RHSOPT,
5870a4fdbfad150ae37bddaa4094d3925a27a1a1cf3fFariborz Jahanian                                         bool BlockReturnType) {
5871a98344869c278295f38e106583906377520079b8Fariborz Jahanian  if (RHSOPT->isObjCBuiltinType() || LHSOPT->isObjCIdType())
5872132f2a2da34f378fc675b9e174564b0f52c31d98Fariborz Jahanian    return true;
5873132f2a2da34f378fc675b9e174564b0f52c31d98Fariborz Jahanian
5874132f2a2da34f378fc675b9e174564b0f52c31d98Fariborz Jahanian  if (LHSOPT->isObjCBuiltinType()) {
5875132f2a2da34f378fc675b9e174564b0f52c31d98Fariborz Jahanian    return RHSOPT->isObjCBuiltinType() || RHSOPT->isObjCQualifiedIdType();
5876132f2a2da34f378fc675b9e174564b0f52c31d98Fariborz Jahanian  }
5877132f2a2da34f378fc675b9e174564b0f52c31d98Fariborz Jahanian
5878a98344869c278295f38e106583906377520079b8Fariborz Jahanian  if (LHSOPT->isObjCQualifiedIdType() || RHSOPT->isObjCQualifiedIdType())
5879132f2a2da34f378fc675b9e174564b0f52c31d98Fariborz Jahanian    return ObjCQualifiedIdTypesAreCompatible(QualType(LHSOPT,0),
5880132f2a2da34f378fc675b9e174564b0f52c31d98Fariborz Jahanian                                             QualType(RHSOPT,0),
5881132f2a2da34f378fc675b9e174564b0f52c31d98Fariborz Jahanian                                             false);
5882132f2a2da34f378fc675b9e174564b0f52c31d98Fariborz Jahanian
5883132f2a2da34f378fc675b9e174564b0f52c31d98Fariborz Jahanian  const ObjCInterfaceType* LHS = LHSOPT->getInterfaceType();
5884132f2a2da34f378fc675b9e174564b0f52c31d98Fariborz Jahanian  const ObjCInterfaceType* RHS = RHSOPT->getInterfaceType();
5885132f2a2da34f378fc675b9e174564b0f52c31d98Fariborz Jahanian  if (LHS && RHS)  { // We have 2 user-defined types.
5886132f2a2da34f378fc675b9e174564b0f52c31d98Fariborz Jahanian    if (LHS != RHS) {
5887132f2a2da34f378fc675b9e174564b0f52c31d98Fariborz Jahanian      if (LHS->getDecl()->isSuperClassOf(RHS->getDecl()))
5888a4fdbfad150ae37bddaa4094d3925a27a1a1cf3fFariborz Jahanian        return BlockReturnType;
5889132f2a2da34f378fc675b9e174564b0f52c31d98Fariborz Jahanian      if (RHS->getDecl()->isSuperClassOf(LHS->getDecl()))
5890a4fdbfad150ae37bddaa4094d3925a27a1a1cf3fFariborz Jahanian        return !BlockReturnType;
5891132f2a2da34f378fc675b9e174564b0f52c31d98Fariborz Jahanian    }
5892132f2a2da34f378fc675b9e174564b0f52c31d98Fariborz Jahanian    else
5893132f2a2da34f378fc675b9e174564b0f52c31d98Fariborz Jahanian      return true;
5894132f2a2da34f378fc675b9e174564b0f52c31d98Fariborz Jahanian  }
5895132f2a2da34f378fc675b9e174564b0f52c31d98Fariborz Jahanian  return false;
5896132f2a2da34f378fc675b9e174564b0f52c31d98Fariborz Jahanian}
5897132f2a2da34f378fc675b9e174564b0f52c31d98Fariborz Jahanian
5898e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanian/// getIntersectionOfProtocols - This routine finds the intersection of set
5899e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanian/// of protocols inherited from two distinct objective-c pointer objects.
5900e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanian/// It is used to build composite qualifier list of the composite type of
5901e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanian/// the conditional expression involving two objective-c pointer objects.
5902e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanianstatic
5903e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanianvoid getIntersectionOfProtocols(ASTContext &Context,
5904e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanian                                const ObjCObjectPointerType *LHSOPT,
5905e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanian                                const ObjCObjectPointerType *RHSOPT,
59065f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner      SmallVectorImpl<ObjCProtocolDecl *> &IntersectionOfProtocols) {
5907e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanian
5908c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall  const ObjCObjectType* LHS = LHSOPT->getObjectType();
5909c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall  const ObjCObjectType* RHS = RHSOPT->getObjectType();
5910c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall  assert(LHS->getInterface() && "LHS must have an interface base");
5911c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall  assert(RHS->getInterface() && "RHS must have an interface base");
5912e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanian
5913e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanian  llvm::SmallPtrSet<ObjCProtocolDecl *, 8> InheritedProtocolSet;
5914e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanian  unsigned LHSNumProtocols = LHS->getNumProtocols();
5915e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanian  if (LHSNumProtocols > 0)
5916e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanian    InheritedProtocolSet.insert(LHS->qual_begin(), LHS->qual_end());
5917e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanian  else {
5918432a8893f7e30d141d7f279bd00b741a3cdac81fFariborz Jahanian    llvm::SmallPtrSet<ObjCProtocolDecl *, 8> LHSInheritedProtocols;
5919c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall    Context.CollectInheritedProtocols(LHS->getInterface(),
5920c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall                                      LHSInheritedProtocols);
5921e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanian    InheritedProtocolSet.insert(LHSInheritedProtocols.begin(),
5922e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanian                                LHSInheritedProtocols.end());
5923e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanian  }
5924e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanian
5925e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanian  unsigned RHSNumProtocols = RHS->getNumProtocols();
5926e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanian  if (RHSNumProtocols > 0) {
5927cb421fa690da545b58a720abe5f1c49b166dbde7Dan Gohman    ObjCProtocolDecl **RHSProtocols =
5928cb421fa690da545b58a720abe5f1c49b166dbde7Dan Gohman      const_cast<ObjCProtocolDecl **>(RHS->qual_begin());
5929e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanian    for (unsigned i = 0; i < RHSNumProtocols; ++i)
5930e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanian      if (InheritedProtocolSet.count(RHSProtocols[i]))
5931e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanian        IntersectionOfProtocols.push_back(RHSProtocols[i]);
59323060178ad9df29789505c1e6debcfc80a3a13587Chad Rosier  } else {
5933432a8893f7e30d141d7f279bd00b741a3cdac81fFariborz Jahanian    llvm::SmallPtrSet<ObjCProtocolDecl *, 8> RHSInheritedProtocols;
5934c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall    Context.CollectInheritedProtocols(RHS->getInterface(),
5935c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall                                      RHSInheritedProtocols);
5936432a8893f7e30d141d7f279bd00b741a3cdac81fFariborz Jahanian    for (llvm::SmallPtrSet<ObjCProtocolDecl*,8>::iterator I =
5937432a8893f7e30d141d7f279bd00b741a3cdac81fFariborz Jahanian         RHSInheritedProtocols.begin(),
5938432a8893f7e30d141d7f279bd00b741a3cdac81fFariborz Jahanian         E = RHSInheritedProtocols.end(); I != E; ++I)
5939432a8893f7e30d141d7f279bd00b741a3cdac81fFariborz Jahanian      if (InheritedProtocolSet.count((*I)))
5940432a8893f7e30d141d7f279bd00b741a3cdac81fFariborz Jahanian        IntersectionOfProtocols.push_back((*I));
5941e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanian  }
5942e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanian}
5943e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanian
5944db07b3f7cdcb505329c1280d7cf70791739a7cadFariborz Jahanian/// areCommonBaseCompatible - Returns common base class of the two classes if
5945db07b3f7cdcb505329c1280d7cf70791739a7cadFariborz Jahanian/// one found. Note that this is O'2 algorithm. But it will be called as the
5946db07b3f7cdcb505329c1280d7cf70791739a7cadFariborz Jahanian/// last type comparison in a ?-exp of ObjC pointer types before a
5947db07b3f7cdcb505329c1280d7cf70791739a7cadFariborz Jahanian/// warning is issued. So, its invokation is extremely rare.
5948db07b3f7cdcb505329c1280d7cf70791739a7cadFariborz JahanianQualType ASTContext::areCommonBaseCompatible(
5949c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall                                          const ObjCObjectPointerType *Lptr,
5950c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall                                          const ObjCObjectPointerType *Rptr) {
5951c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall  const ObjCObjectType *LHS = Lptr->getObjectType();
5952c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall  const ObjCObjectType *RHS = Rptr->getObjectType();
5953c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall  const ObjCInterfaceDecl* LDecl = LHS->getInterface();
5954c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall  const ObjCInterfaceDecl* RDecl = RHS->getInterface();
595560ef308e51c71b760d7f598c1b763ceb7b768148Douglas Gregor  if (!LDecl || !RDecl || (declaresSameEntity(LDecl, RDecl)))
5956db07b3f7cdcb505329c1280d7cf70791739a7cadFariborz Jahanian    return QualType();
5957db07b3f7cdcb505329c1280d7cf70791739a7cadFariborz Jahanian
59587c2bdcb4d30f2d370b4367664e6a11b075ce2cb3Fariborz Jahanian  do {
5959c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall    LHS = cast<ObjCInterfaceType>(getObjCInterfaceType(LDecl));
5960e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanian    if (canAssignObjCInterfaces(LHS, RHS)) {
59615f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner      SmallVector<ObjCProtocolDecl *, 8> Protocols;
5962c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall      getIntersectionOfProtocols(*this, Lptr, Rptr, Protocols);
5963c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall
5964c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall      QualType Result = QualType(LHS, 0);
5965c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall      if (!Protocols.empty())
5966c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall        Result = getObjCObjectType(Result, Protocols.data(), Protocols.size());
5967c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall      Result = getObjCObjectPointerType(Result);
5968c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall      return Result;
5969e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanian    }
59707c2bdcb4d30f2d370b4367664e6a11b075ce2cb3Fariborz Jahanian  } while ((LDecl = LDecl->getSuperClass()));
5971db07b3f7cdcb505329c1280d7cf70791739a7cadFariborz Jahanian
5972db07b3f7cdcb505329c1280d7cf70791739a7cadFariborz Jahanian  return QualType();
5973db07b3f7cdcb505329c1280d7cf70791739a7cadFariborz Jahanian}
5974db07b3f7cdcb505329c1280d7cf70791739a7cadFariborz Jahanian
5975c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCallbool ASTContext::canAssignObjCInterfaces(const ObjCObjectType *LHS,
5976c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall                                         const ObjCObjectType *RHS) {
5977c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall  assert(LHS->getInterface() && "LHS is not an interface type");
5978c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall  assert(RHS->getInterface() && "RHS is not an interface type");
5979c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall
59806ac46a4a521366d7ab36ebe2ce4e624ab96b06f9Chris Lattner  // Verify that the base decls are compatible: the RHS must be a subclass of
59816ac46a4a521366d7ab36ebe2ce4e624ab96b06f9Chris Lattner  // the LHS.
5982c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall  if (!LHS->getInterface()->isSuperClassOf(RHS->getInterface()))
59836ac46a4a521366d7ab36ebe2ce4e624ab96b06f9Chris Lattner    return false;
59841eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
59856ac46a4a521366d7ab36ebe2ce4e624ab96b06f9Chris Lattner  // RHS must have a superset of the protocols in the LHS.  If the LHS is not
59866ac46a4a521366d7ab36ebe2ce4e624ab96b06f9Chris Lattner  // protocol qualified at all, then we are good.
5987c15cb2af27514ecc879daba9aa01389c5203685dSteve Naroff  if (LHS->getNumProtocols() == 0)
59886ac46a4a521366d7ab36ebe2ce4e624ab96b06f9Chris Lattner    return true;
59891eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
5990b7bc34a83aff8af09f2a78aa6d1dcafe18ad8619Fariborz Jahanian  // Okay, we know the LHS has protocol qualifiers.  If the RHS doesn't,
5991b7bc34a83aff8af09f2a78aa6d1dcafe18ad8619Fariborz Jahanian  // more detailed analysis is required.
5992b7bc34a83aff8af09f2a78aa6d1dcafe18ad8619Fariborz Jahanian  if (RHS->getNumProtocols() == 0) {
5993b7bc34a83aff8af09f2a78aa6d1dcafe18ad8619Fariborz Jahanian    // OK, if LHS is a superclass of RHS *and*
5994b7bc34a83aff8af09f2a78aa6d1dcafe18ad8619Fariborz Jahanian    // this superclass is assignment compatible with LHS.
5995b7bc34a83aff8af09f2a78aa6d1dcafe18ad8619Fariborz Jahanian    // false otherwise.
5996627788c29976fbeb4ad47bcfcb3576889070e357Fariborz Jahanian    bool IsSuperClass =
5997627788c29976fbeb4ad47bcfcb3576889070e357Fariborz Jahanian      LHS->getInterface()->isSuperClassOf(RHS->getInterface());
5998627788c29976fbeb4ad47bcfcb3576889070e357Fariborz Jahanian    if (IsSuperClass) {
5999b7bc34a83aff8af09f2a78aa6d1dcafe18ad8619Fariborz Jahanian      // OK if conversion of LHS to SuperClass results in narrowing of types
6000b7bc34a83aff8af09f2a78aa6d1dcafe18ad8619Fariborz Jahanian      // ; i.e., SuperClass may implement at least one of the protocols
6001b7bc34a83aff8af09f2a78aa6d1dcafe18ad8619Fariborz Jahanian      // in LHS's protocol list. Example, SuperObj<P1> = lhs<P1,P2> is ok.
6002b7bc34a83aff8af09f2a78aa6d1dcafe18ad8619Fariborz Jahanian      // But not SuperObj<P1,P2,P3> = lhs<P1,P2>.
6003b7bc34a83aff8af09f2a78aa6d1dcafe18ad8619Fariborz Jahanian      llvm::SmallPtrSet<ObjCProtocolDecl *, 8> SuperClassInheritedProtocols;
6004627788c29976fbeb4ad47bcfcb3576889070e357Fariborz Jahanian      CollectInheritedProtocols(RHS->getInterface(), SuperClassInheritedProtocols);
6005b7bc34a83aff8af09f2a78aa6d1dcafe18ad8619Fariborz Jahanian      // If super class has no protocols, it is not a match.
6006b7bc34a83aff8af09f2a78aa6d1dcafe18ad8619Fariborz Jahanian      if (SuperClassInheritedProtocols.empty())
6007b7bc34a83aff8af09f2a78aa6d1dcafe18ad8619Fariborz Jahanian        return false;
6008b7bc34a83aff8af09f2a78aa6d1dcafe18ad8619Fariborz Jahanian
6009b7bc34a83aff8af09f2a78aa6d1dcafe18ad8619Fariborz Jahanian      for (ObjCObjectType::qual_iterator LHSPI = LHS->qual_begin(),
6010b7bc34a83aff8af09f2a78aa6d1dcafe18ad8619Fariborz Jahanian           LHSPE = LHS->qual_end();
6011b7bc34a83aff8af09f2a78aa6d1dcafe18ad8619Fariborz Jahanian           LHSPI != LHSPE; LHSPI++) {
6012b7bc34a83aff8af09f2a78aa6d1dcafe18ad8619Fariborz Jahanian        bool SuperImplementsProtocol = false;
6013b7bc34a83aff8af09f2a78aa6d1dcafe18ad8619Fariborz Jahanian        ObjCProtocolDecl *LHSProto = (*LHSPI);
6014b7bc34a83aff8af09f2a78aa6d1dcafe18ad8619Fariborz Jahanian
6015b7bc34a83aff8af09f2a78aa6d1dcafe18ad8619Fariborz Jahanian        for (llvm::SmallPtrSet<ObjCProtocolDecl*,8>::iterator I =
6016b7bc34a83aff8af09f2a78aa6d1dcafe18ad8619Fariborz Jahanian             SuperClassInheritedProtocols.begin(),
6017b7bc34a83aff8af09f2a78aa6d1dcafe18ad8619Fariborz Jahanian             E = SuperClassInheritedProtocols.end(); I != E; ++I) {
6018b7bc34a83aff8af09f2a78aa6d1dcafe18ad8619Fariborz Jahanian          ObjCProtocolDecl *SuperClassProto = (*I);
6019b7bc34a83aff8af09f2a78aa6d1dcafe18ad8619Fariborz Jahanian          if (SuperClassProto->lookupProtocolNamed(LHSProto->getIdentifier())) {
6020b7bc34a83aff8af09f2a78aa6d1dcafe18ad8619Fariborz Jahanian            SuperImplementsProtocol = true;
6021b7bc34a83aff8af09f2a78aa6d1dcafe18ad8619Fariborz Jahanian            break;
6022b7bc34a83aff8af09f2a78aa6d1dcafe18ad8619Fariborz Jahanian          }
6023b7bc34a83aff8af09f2a78aa6d1dcafe18ad8619Fariborz Jahanian        }
6024b7bc34a83aff8af09f2a78aa6d1dcafe18ad8619Fariborz Jahanian        if (!SuperImplementsProtocol)
6025b7bc34a83aff8af09f2a78aa6d1dcafe18ad8619Fariborz Jahanian          return false;
6026b7bc34a83aff8af09f2a78aa6d1dcafe18ad8619Fariborz Jahanian      }
6027b7bc34a83aff8af09f2a78aa6d1dcafe18ad8619Fariborz Jahanian      return true;
6028b7bc34a83aff8af09f2a78aa6d1dcafe18ad8619Fariborz Jahanian    }
6029b7bc34a83aff8af09f2a78aa6d1dcafe18ad8619Fariborz Jahanian    return false;
6030b7bc34a83aff8af09f2a78aa6d1dcafe18ad8619Fariborz Jahanian  }
60311eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
6032c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall  for (ObjCObjectType::qual_iterator LHSPI = LHS->qual_begin(),
6033c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall                                     LHSPE = LHS->qual_end();
603491b0b0cf6b537cbcbca0038c7032f87161a41d31Steve Naroff       LHSPI != LHSPE; LHSPI++) {
603591b0b0cf6b537cbcbca0038c7032f87161a41d31Steve Naroff    bool RHSImplementsProtocol = false;
603691b0b0cf6b537cbcbca0038c7032f87161a41d31Steve Naroff
603791b0b0cf6b537cbcbca0038c7032f87161a41d31Steve Naroff    // If the RHS doesn't implement the protocol on the left, the types
603891b0b0cf6b537cbcbca0038c7032f87161a41d31Steve Naroff    // are incompatible.
6039c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall    for (ObjCObjectType::qual_iterator RHSPI = RHS->qual_begin(),
6040c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall                                       RHSPE = RHS->qual_end();
60418f16756441450ed9fb39316e47d107fc2a1ef35bSteve Naroff         RHSPI != RHSPE; RHSPI++) {
60428f16756441450ed9fb39316e47d107fc2a1ef35bSteve Naroff      if ((*RHSPI)->lookupProtocolNamed((*LHSPI)->getIdentifier())) {
604391b0b0cf6b537cbcbca0038c7032f87161a41d31Steve Naroff        RHSImplementsProtocol = true;
60448f16756441450ed9fb39316e47d107fc2a1ef35bSteve Naroff        break;
60458f16756441450ed9fb39316e47d107fc2a1ef35bSteve Naroff      }
604691b0b0cf6b537cbcbca0038c7032f87161a41d31Steve Naroff    }
604791b0b0cf6b537cbcbca0038c7032f87161a41d31Steve Naroff    // FIXME: For better diagnostics, consider passing back the protocol name.
604891b0b0cf6b537cbcbca0038c7032f87161a41d31Steve Naroff    if (!RHSImplementsProtocol)
604991b0b0cf6b537cbcbca0038c7032f87161a41d31Steve Naroff      return false;
605091b0b0cf6b537cbcbca0038c7032f87161a41d31Steve Naroff  }
605191b0b0cf6b537cbcbca0038c7032f87161a41d31Steve Naroff  // The RHS implements all protocols listed on the LHS.
605291b0b0cf6b537cbcbca0038c7032f87161a41d31Steve Naroff  return true;
60536ac46a4a521366d7ab36ebe2ce4e624ab96b06f9Chris Lattner}
60546ac46a4a521366d7ab36ebe2ce4e624ab96b06f9Chris Lattner
6055389bf46ae41241a656ed71b00ac2177d7f385651Steve Naroffbool ASTContext::areComparableObjCPointerTypes(QualType LHS, QualType RHS) {
6056389bf46ae41241a656ed71b00ac2177d7f385651Steve Naroff  // get the "pointed to" types
6057183700f494ec9b6701b6efe82bcb25f4c79ba561John McCall  const ObjCObjectPointerType *LHSOPT = LHS->getAs<ObjCObjectPointerType>();
6058183700f494ec9b6701b6efe82bcb25f4c79ba561John McCall  const ObjCObjectPointerType *RHSOPT = RHS->getAs<ObjCObjectPointerType>();
60591eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
606014108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff  if (!LHSOPT || !RHSOPT)
6061389bf46ae41241a656ed71b00ac2177d7f385651Steve Naroff    return false;
606214108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff
606314108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff  return canAssignObjCInterfaces(LHSOPT, RHSOPT) ||
606414108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff         canAssignObjCInterfaces(RHSOPT, LHSOPT);
6065389bf46ae41241a656ed71b00ac2177d7f385651Steve Naroff}
6066389bf46ae41241a656ed71b00ac2177d7f385651Steve Naroff
6067569c3166874324c24011f8ade6978421f0d39b3cDouglas Gregorbool ASTContext::canBindObjCObjectType(QualType To, QualType From) {
6068569c3166874324c24011f8ade6978421f0d39b3cDouglas Gregor  return canAssignObjCInterfaces(
6069569c3166874324c24011f8ade6978421f0d39b3cDouglas Gregor                getObjCObjectPointerType(To)->getAs<ObjCObjectPointerType>(),
6070569c3166874324c24011f8ade6978421f0d39b3cDouglas Gregor                getObjCObjectPointerType(From)->getAs<ObjCObjectPointerType>());
6071569c3166874324c24011f8ade6978421f0d39b3cDouglas Gregor}
6072569c3166874324c24011f8ade6978421f0d39b3cDouglas Gregor
60731eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// typesAreCompatible - C99 6.7.3p9: For two qualified types to be compatible,
6074ec0550fa3653d46560bf4484a2e988329c228e39Steve Naroff/// both shall have the identically qualified version of a compatible type.
60751eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// C99 6.2.7p1: Two types have compatible types if their types are the
6076ec0550fa3653d46560bf4484a2e988329c228e39Steve Naroff/// same. See 6.7.[2,3,5] for additional rules.
6077447234dd459a00a5ed9b7c3e066162cd7a75bf2dDouglas Gregorbool ASTContext::typesAreCompatible(QualType LHS, QualType RHS,
6078447234dd459a00a5ed9b7c3e066162cd7a75bf2dDouglas Gregor                                    bool CompareUnqualified) {
60794e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie  if (getLangOpts().CPlusPlus)
60800e709abafbd939326850501f795cc7a92c88a354Douglas Gregor    return hasSameType(LHS, RHS);
60810e709abafbd939326850501f795cc7a92c88a354Douglas Gregor
6082447234dd459a00a5ed9b7c3e066162cd7a75bf2dDouglas Gregor  return !mergeTypes(LHS, RHS, false, CompareUnqualified).isNull();
60833d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman}
60843d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman
6085c286f3835eb6001c61664cef5d610dfaf80a6e9bFariborz Jahanianbool ASTContext::propertyTypesAreCompatible(QualType LHS, QualType RHS) {
608682378391dfc3b2fc22f63dbf1552bd9aed39dc42Fariborz Jahanian  return typesAreCompatible(LHS, RHS);
6087c286f3835eb6001c61664cef5d610dfaf80a6e9bFariborz Jahanian}
6088c286f3835eb6001c61664cef5d610dfaf80a6e9bFariborz Jahanian
6089132f2a2da34f378fc675b9e174564b0f52c31d98Fariborz Jahanianbool ASTContext::typesAreBlockPointerCompatible(QualType LHS, QualType RHS) {
6090132f2a2da34f378fc675b9e174564b0f52c31d98Fariborz Jahanian  return !mergeTypes(LHS, RHS, true).isNull();
6091132f2a2da34f378fc675b9e174564b0f52c31d98Fariborz Jahanian}
6092132f2a2da34f378fc675b9e174564b0f52c31d98Fariborz Jahanian
60934846675e0e42d1802b0ffd8972a45e72aeb3758dPeter Collingbourne/// mergeTransparentUnionType - if T is a transparent union type and a member
60944846675e0e42d1802b0ffd8972a45e72aeb3758dPeter Collingbourne/// of T is compatible with SubType, return the merged type, else return
60954846675e0e42d1802b0ffd8972a45e72aeb3758dPeter Collingbourne/// QualType()
60964846675e0e42d1802b0ffd8972a45e72aeb3758dPeter CollingbourneQualType ASTContext::mergeTransparentUnionType(QualType T, QualType SubType,
60974846675e0e42d1802b0ffd8972a45e72aeb3758dPeter Collingbourne                                               bool OfBlockPointer,
60984846675e0e42d1802b0ffd8972a45e72aeb3758dPeter Collingbourne                                               bool Unqualified) {
60994846675e0e42d1802b0ffd8972a45e72aeb3758dPeter Collingbourne  if (const RecordType *UT = T->getAsUnionType()) {
61004846675e0e42d1802b0ffd8972a45e72aeb3758dPeter Collingbourne    RecordDecl *UD = UT->getDecl();
61014846675e0e42d1802b0ffd8972a45e72aeb3758dPeter Collingbourne    if (UD->hasAttr<TransparentUnionAttr>()) {
61024846675e0e42d1802b0ffd8972a45e72aeb3758dPeter Collingbourne      for (RecordDecl::field_iterator it = UD->field_begin(),
61034846675e0e42d1802b0ffd8972a45e72aeb3758dPeter Collingbourne           itend = UD->field_end(); it != itend; ++it) {
6104f91d7572ee7fa6f2926c8e7e816039a1154a59f8Peter Collingbourne        QualType ET = it->getType().getUnqualifiedType();
61054846675e0e42d1802b0ffd8972a45e72aeb3758dPeter Collingbourne        QualType MT = mergeTypes(ET, SubType, OfBlockPointer, Unqualified);
61064846675e0e42d1802b0ffd8972a45e72aeb3758dPeter Collingbourne        if (!MT.isNull())
61074846675e0e42d1802b0ffd8972a45e72aeb3758dPeter Collingbourne          return MT;
61084846675e0e42d1802b0ffd8972a45e72aeb3758dPeter Collingbourne      }
61094846675e0e42d1802b0ffd8972a45e72aeb3758dPeter Collingbourne    }
61104846675e0e42d1802b0ffd8972a45e72aeb3758dPeter Collingbourne  }
61114846675e0e42d1802b0ffd8972a45e72aeb3758dPeter Collingbourne
61124846675e0e42d1802b0ffd8972a45e72aeb3758dPeter Collingbourne  return QualType();
61134846675e0e42d1802b0ffd8972a45e72aeb3758dPeter Collingbourne}
61144846675e0e42d1802b0ffd8972a45e72aeb3758dPeter Collingbourne
61154846675e0e42d1802b0ffd8972a45e72aeb3758dPeter Collingbourne/// mergeFunctionArgumentTypes - merge two types which appear as function
61164846675e0e42d1802b0ffd8972a45e72aeb3758dPeter Collingbourne/// argument types
61174846675e0e42d1802b0ffd8972a45e72aeb3758dPeter CollingbourneQualType ASTContext::mergeFunctionArgumentTypes(QualType lhs, QualType rhs,
61184846675e0e42d1802b0ffd8972a45e72aeb3758dPeter Collingbourne                                                bool OfBlockPointer,
61194846675e0e42d1802b0ffd8972a45e72aeb3758dPeter Collingbourne                                                bool Unqualified) {
61204846675e0e42d1802b0ffd8972a45e72aeb3758dPeter Collingbourne  // GNU extension: two types are compatible if they appear as a function
61214846675e0e42d1802b0ffd8972a45e72aeb3758dPeter Collingbourne  // argument, one of the types is a transparent union type and the other
61224846675e0e42d1802b0ffd8972a45e72aeb3758dPeter Collingbourne  // type is compatible with a union member
61234846675e0e42d1802b0ffd8972a45e72aeb3758dPeter Collingbourne  QualType lmerge = mergeTransparentUnionType(lhs, rhs, OfBlockPointer,
61244846675e0e42d1802b0ffd8972a45e72aeb3758dPeter Collingbourne                                              Unqualified);
61254846675e0e42d1802b0ffd8972a45e72aeb3758dPeter Collingbourne  if (!lmerge.isNull())
61264846675e0e42d1802b0ffd8972a45e72aeb3758dPeter Collingbourne    return lmerge;
61274846675e0e42d1802b0ffd8972a45e72aeb3758dPeter Collingbourne
61284846675e0e42d1802b0ffd8972a45e72aeb3758dPeter Collingbourne  QualType rmerge = mergeTransparentUnionType(rhs, lhs, OfBlockPointer,
61294846675e0e42d1802b0ffd8972a45e72aeb3758dPeter Collingbourne                                              Unqualified);
61304846675e0e42d1802b0ffd8972a45e72aeb3758dPeter Collingbourne  if (!rmerge.isNull())
61314846675e0e42d1802b0ffd8972a45e72aeb3758dPeter Collingbourne    return rmerge;
61324846675e0e42d1802b0ffd8972a45e72aeb3758dPeter Collingbourne
61334846675e0e42d1802b0ffd8972a45e72aeb3758dPeter Collingbourne  return mergeTypes(lhs, rhs, OfBlockPointer, Unqualified);
61344846675e0e42d1802b0ffd8972a45e72aeb3758dPeter Collingbourne}
61354846675e0e42d1802b0ffd8972a45e72aeb3758dPeter Collingbourne
6136132f2a2da34f378fc675b9e174564b0f52c31d98Fariborz JahanianQualType ASTContext::mergeFunctionTypes(QualType lhs, QualType rhs,
6137447234dd459a00a5ed9b7c3e066162cd7a75bf2dDouglas Gregor                                        bool OfBlockPointer,
6138447234dd459a00a5ed9b7c3e066162cd7a75bf2dDouglas Gregor                                        bool Unqualified) {
6139183700f494ec9b6701b6efe82bcb25f4c79ba561John McCall  const FunctionType *lbase = lhs->getAs<FunctionType>();
6140183700f494ec9b6701b6efe82bcb25f4c79ba561John McCall  const FunctionType *rbase = rhs->getAs<FunctionType>();
614172564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor  const FunctionProtoType *lproto = dyn_cast<FunctionProtoType>(lbase);
614272564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor  const FunctionProtoType *rproto = dyn_cast<FunctionProtoType>(rbase);
61433d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman  bool allLTypes = true;
61443d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman  bool allRTypes = true;
61453d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman
61463d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman  // Check return type
6147132f2a2da34f378fc675b9e174564b0f52c31d98Fariborz Jahanian  QualType retType;
6148d263fd1451299b1e5f5f1acb2bb13b0a4119aee8Fariborz Jahanian  if (OfBlockPointer) {
6149d263fd1451299b1e5f5f1acb2bb13b0a4119aee8Fariborz Jahanian    QualType RHS = rbase->getResultType();
6150d263fd1451299b1e5f5f1acb2bb13b0a4119aee8Fariborz Jahanian    QualType LHS = lbase->getResultType();
6151d263fd1451299b1e5f5f1acb2bb13b0a4119aee8Fariborz Jahanian    bool UnqualifiedResult = Unqualified;
6152d263fd1451299b1e5f5f1acb2bb13b0a4119aee8Fariborz Jahanian    if (!UnqualifiedResult)
6153d263fd1451299b1e5f5f1acb2bb13b0a4119aee8Fariborz Jahanian      UnqualifiedResult = (!RHS.hasQualifiers() && LHS.hasQualifiers());
6154a4fdbfad150ae37bddaa4094d3925a27a1a1cf3fFariborz Jahanian    retType = mergeTypes(LHS, RHS, true, UnqualifiedResult, true);
6155d263fd1451299b1e5f5f1acb2bb13b0a4119aee8Fariborz Jahanian  }
6156132f2a2da34f378fc675b9e174564b0f52c31d98Fariborz Jahanian  else
61578cc246c9a68c783a5b90d2e8b8927521cb3a49b7John McCall    retType = mergeTypes(lbase->getResultType(), rbase->getResultType(), false,
61588cc246c9a68c783a5b90d2e8b8927521cb3a49b7John McCall                         Unqualified);
61593d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman  if (retType.isNull()) return QualType();
6160447234dd459a00a5ed9b7c3e066162cd7a75bf2dDouglas Gregor
6161447234dd459a00a5ed9b7c3e066162cd7a75bf2dDouglas Gregor  if (Unqualified)
6162447234dd459a00a5ed9b7c3e066162cd7a75bf2dDouglas Gregor    retType = retType.getUnqualifiedType();
6163447234dd459a00a5ed9b7c3e066162cd7a75bf2dDouglas Gregor
6164447234dd459a00a5ed9b7c3e066162cd7a75bf2dDouglas Gregor  CanQualType LRetType = getCanonicalType(lbase->getResultType());
6165447234dd459a00a5ed9b7c3e066162cd7a75bf2dDouglas Gregor  CanQualType RRetType = getCanonicalType(rbase->getResultType());
6166447234dd459a00a5ed9b7c3e066162cd7a75bf2dDouglas Gregor  if (Unqualified) {
6167447234dd459a00a5ed9b7c3e066162cd7a75bf2dDouglas Gregor    LRetType = LRetType.getUnqualifiedType();
6168447234dd459a00a5ed9b7c3e066162cd7a75bf2dDouglas Gregor    RRetType = RRetType.getUnqualifiedType();
6169447234dd459a00a5ed9b7c3e066162cd7a75bf2dDouglas Gregor  }
6170447234dd459a00a5ed9b7c3e066162cd7a75bf2dDouglas Gregor
6171447234dd459a00a5ed9b7c3e066162cd7a75bf2dDouglas Gregor  if (getCanonicalType(retType) != LRetType)
617261710854be2b098428aff5316e64bd34b30fbcb7Chris Lattner    allLTypes = false;
6173447234dd459a00a5ed9b7c3e066162cd7a75bf2dDouglas Gregor  if (getCanonicalType(retType) != RRetType)
617461710854be2b098428aff5316e64bd34b30fbcb7Chris Lattner    allRTypes = false;
61758cc246c9a68c783a5b90d2e8b8927521cb3a49b7John McCall
61766a15c8586cc3e5d6e01c4c4e8b4374584569b58fDaniel Dunbar  // FIXME: double check this
61776a15c8586cc3e5d6e01c4c4e8b4374584569b58fDaniel Dunbar  // FIXME: should we error if lbase->getRegParmAttr() != 0 &&
61786a15c8586cc3e5d6e01c4c4e8b4374584569b58fDaniel Dunbar  //                           rbase->getRegParmAttr() != 0 &&
61796a15c8586cc3e5d6e01c4c4e8b4374584569b58fDaniel Dunbar  //                           lbase->getRegParmAttr() != rbase->getRegParmAttr()?
6180264ba48dc98f3f843935a485d5b086f7e0fdc4f1Rafael Espindola  FunctionType::ExtInfo lbaseInfo = lbase->getExtInfo();
6181264ba48dc98f3f843935a485d5b086f7e0fdc4f1Rafael Espindola  FunctionType::ExtInfo rbaseInfo = rbase->getExtInfo();
61828cc246c9a68c783a5b90d2e8b8927521cb3a49b7John McCall
61838cc246c9a68c783a5b90d2e8b8927521cb3a49b7John McCall  // Compatible functions must have compatible calling conventions
61848cc246c9a68c783a5b90d2e8b8927521cb3a49b7John McCall  if (!isSameCallConv(lbaseInfo.getCC(), rbaseInfo.getCC()))
61858cc246c9a68c783a5b90d2e8b8927521cb3a49b7John McCall    return QualType();
61868cc246c9a68c783a5b90d2e8b8927521cb3a49b7John McCall
61878cc246c9a68c783a5b90d2e8b8927521cb3a49b7John McCall  // Regparm is part of the calling convention.
6188a49218e17bcbb1acde0245773173e2c0c42f4f19Eli Friedman  if (lbaseInfo.getHasRegParm() != rbaseInfo.getHasRegParm())
6189a49218e17bcbb1acde0245773173e2c0c42f4f19Eli Friedman    return QualType();
61908cc246c9a68c783a5b90d2e8b8927521cb3a49b7John McCall  if (lbaseInfo.getRegParm() != rbaseInfo.getRegParm())
61918cc246c9a68c783a5b90d2e8b8927521cb3a49b7John McCall    return QualType();
61928cc246c9a68c783a5b90d2e8b8927521cb3a49b7John McCall
6193f85e193739c953358c865005855253af4f68a497John McCall  if (lbaseInfo.getProducesResult() != rbaseInfo.getProducesResult())
6194f85e193739c953358c865005855253af4f68a497John McCall    return QualType();
6195f85e193739c953358c865005855253af4f68a497John McCall
619653c8167d7a007daae87f342c0fedd03f1dcf1b62Fariborz Jahanian  // functypes which return are preferred over those that do not.
619753c8167d7a007daae87f342c0fedd03f1dcf1b62Fariborz Jahanian  if (lbaseInfo.getNoReturn() && !rbaseInfo.getNoReturn())
61986a15c8586cc3e5d6e01c4c4e8b4374584569b58fDaniel Dunbar    allLTypes = false;
619953c8167d7a007daae87f342c0fedd03f1dcf1b62Fariborz Jahanian  else if (!lbaseInfo.getNoReturn() && rbaseInfo.getNoReturn())
62006a15c8586cc3e5d6e01c4c4e8b4374584569b58fDaniel Dunbar    allRTypes = false;
620153c8167d7a007daae87f342c0fedd03f1dcf1b62Fariborz Jahanian  // FIXME: some uses, e.g. conditional exprs, really want this to be 'both'.
620253c8167d7a007daae87f342c0fedd03f1dcf1b62Fariborz Jahanian  bool NoReturn = lbaseInfo.getNoReturn() || rbaseInfo.getNoReturn();
62031eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
6204f85e193739c953358c865005855253af4f68a497John McCall  FunctionType::ExtInfo einfo = lbaseInfo.withNoReturn(NoReturn);
6205e23cf437fe76b1ed02d63c3f61b456fd48a915f5John McCall
62063d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman  if (lproto && rproto) { // two C99 style function prototypes
6207465226e23a3008bd68973513dda1f9e3cd27dbddSebastian Redl    assert(!lproto->hasExceptionSpec() && !rproto->hasExceptionSpec() &&
6208465226e23a3008bd68973513dda1f9e3cd27dbddSebastian Redl           "C++ shouldn't be here");
62093d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    unsigned lproto_nargs = lproto->getNumArgs();
62103d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    unsigned rproto_nargs = rproto->getNumArgs();
62113d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman
62123d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    // Compatible functions must have the same number of arguments
62133d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    if (lproto_nargs != rproto_nargs)
62143d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman      return QualType();
62153d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman
62163d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    // Variadic and non-variadic functions aren't compatible
62173d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    if (lproto->isVariadic() != rproto->isVariadic())
62183d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman      return QualType();
62193d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman
62207fb5e4888221cd36652d078c6b171ac55e7f406dArgyrios Kyrtzidis    if (lproto->getTypeQuals() != rproto->getTypeQuals())
62217fb5e4888221cd36652d078c6b171ac55e7f406dArgyrios Kyrtzidis      return QualType();
62227fb5e4888221cd36652d078c6b171ac55e7f406dArgyrios Kyrtzidis
622378213e4aec6d8d22b5828dd2687fa19116ebaa26Fariborz Jahanian    if (LangOpts.ObjCAutoRefCount &&
622478213e4aec6d8d22b5828dd2687fa19116ebaa26Fariborz Jahanian        !FunctionTypesMatchOnNSConsumedAttrs(rproto, lproto))
622578213e4aec6d8d22b5828dd2687fa19116ebaa26Fariborz Jahanian      return QualType();
622678213e4aec6d8d22b5828dd2687fa19116ebaa26Fariborz Jahanian
62273d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    // Check argument compatibility
62285f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner    SmallVector<QualType, 10> types;
62293d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    for (unsigned i = 0; i < lproto_nargs; i++) {
62303d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman      QualType largtype = lproto->getArgType(i).getUnqualifiedType();
62313d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman      QualType rargtype = rproto->getArgType(i).getUnqualifiedType();
62324846675e0e42d1802b0ffd8972a45e72aeb3758dPeter Collingbourne      QualType argtype = mergeFunctionArgumentTypes(largtype, rargtype,
62334846675e0e42d1802b0ffd8972a45e72aeb3758dPeter Collingbourne                                                    OfBlockPointer,
62344846675e0e42d1802b0ffd8972a45e72aeb3758dPeter Collingbourne                                                    Unqualified);
62353d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman      if (argtype.isNull()) return QualType();
6236447234dd459a00a5ed9b7c3e066162cd7a75bf2dDouglas Gregor
6237447234dd459a00a5ed9b7c3e066162cd7a75bf2dDouglas Gregor      if (Unqualified)
6238447234dd459a00a5ed9b7c3e066162cd7a75bf2dDouglas Gregor        argtype = argtype.getUnqualifiedType();
6239447234dd459a00a5ed9b7c3e066162cd7a75bf2dDouglas Gregor
62403d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman      types.push_back(argtype);
6241447234dd459a00a5ed9b7c3e066162cd7a75bf2dDouglas Gregor      if (Unqualified) {
6242447234dd459a00a5ed9b7c3e066162cd7a75bf2dDouglas Gregor        largtype = largtype.getUnqualifiedType();
6243447234dd459a00a5ed9b7c3e066162cd7a75bf2dDouglas Gregor        rargtype = rargtype.getUnqualifiedType();
6244447234dd459a00a5ed9b7c3e066162cd7a75bf2dDouglas Gregor      }
6245447234dd459a00a5ed9b7c3e066162cd7a75bf2dDouglas Gregor
624661710854be2b098428aff5316e64bd34b30fbcb7Chris Lattner      if (getCanonicalType(argtype) != getCanonicalType(largtype))
624761710854be2b098428aff5316e64bd34b30fbcb7Chris Lattner        allLTypes = false;
624861710854be2b098428aff5316e64bd34b30fbcb7Chris Lattner      if (getCanonicalType(argtype) != getCanonicalType(rargtype))
624961710854be2b098428aff5316e64bd34b30fbcb7Chris Lattner        allRTypes = false;
62503d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    }
625178213e4aec6d8d22b5828dd2687fa19116ebaa26Fariborz Jahanian
62523d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    if (allLTypes) return lhs;
62533d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    if (allRTypes) return rhs;
6254e23cf437fe76b1ed02d63c3f61b456fd48a915f5John McCall
6255e23cf437fe76b1ed02d63c3f61b456fd48a915f5John McCall    FunctionProtoType::ExtProtoInfo EPI = lproto->getExtProtoInfo();
6256e23cf437fe76b1ed02d63c3f61b456fd48a915f5John McCall    EPI.ExtInfo = einfo;
6257e23cf437fe76b1ed02d63c3f61b456fd48a915f5John McCall    return getFunctionType(retType, types.begin(), types.size(), EPI);
62583d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman  }
62593d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman
62603d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman  if (lproto) allRTypes = false;
62613d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman  if (rproto) allLTypes = false;
62623d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman
626372564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor  const FunctionProtoType *proto = lproto ? lproto : rproto;
62643d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman  if (proto) {
6265465226e23a3008bd68973513dda1f9e3cd27dbddSebastian Redl    assert(!proto->hasExceptionSpec() && "C++ shouldn't be here");
62663d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    if (proto->isVariadic()) return QualType();
62673d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    // Check that the types are compatible with the types that
62683d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    // would result from default argument promotions (C99 6.7.5.3p15).
62693d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    // The only types actually affected are promotable integer
62703d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    // types and floats, which would be passed as a different
62713d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    // type depending on whether the prototype is visible.
62723d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    unsigned proto_nargs = proto->getNumArgs();
62733d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    for (unsigned i = 0; i < proto_nargs; ++i) {
62743d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman      QualType argTy = proto->getArgType(i);
6275b0f8eacfdcd0d43f51e669f2d723992d4af9f746Douglas Gregor
6276b0f8eacfdcd0d43f51e669f2d723992d4af9f746Douglas Gregor      // Look at the promotion type of enum types, since that is the type used
6277b0f8eacfdcd0d43f51e669f2d723992d4af9f746Douglas Gregor      // to pass enum values.
6278b0f8eacfdcd0d43f51e669f2d723992d4af9f746Douglas Gregor      if (const EnumType *Enum = argTy->getAs<EnumType>())
6279b0f8eacfdcd0d43f51e669f2d723992d4af9f746Douglas Gregor        argTy = Enum->getDecl()->getPromotionType();
6280b0f8eacfdcd0d43f51e669f2d723992d4af9f746Douglas Gregor
62813d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman      if (argTy->isPromotableIntegerType() ||
62823d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman          getCanonicalType(argTy).getUnqualifiedType() == FloatTy)
62833d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman        return QualType();
62843d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    }
62853d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman
62863d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    if (allLTypes) return lhs;
62873d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    if (allRTypes) return rhs;
6288e23cf437fe76b1ed02d63c3f61b456fd48a915f5John McCall
6289e23cf437fe76b1ed02d63c3f61b456fd48a915f5John McCall    FunctionProtoType::ExtProtoInfo EPI = proto->getExtProtoInfo();
6290e23cf437fe76b1ed02d63c3f61b456fd48a915f5John McCall    EPI.ExtInfo = einfo;
62913d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    return getFunctionType(retType, proto->arg_type_begin(),
6292e23cf437fe76b1ed02d63c3f61b456fd48a915f5John McCall                           proto->getNumArgs(), EPI);
62933d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman  }
62943d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman
62953d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman  if (allLTypes) return lhs;
62963d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman  if (allRTypes) return rhs;
62978cc246c9a68c783a5b90d2e8b8927521cb3a49b7John McCall  return getFunctionNoProtoType(retType, einfo);
62983d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman}
62993d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman
6300132f2a2da34f378fc675b9e174564b0f52c31d98Fariborz JahanianQualType ASTContext::mergeTypes(QualType LHS, QualType RHS,
6301447234dd459a00a5ed9b7c3e066162cd7a75bf2dDouglas Gregor                                bool OfBlockPointer,
6302a4fdbfad150ae37bddaa4094d3925a27a1a1cf3fFariborz Jahanian                                bool Unqualified, bool BlockReturnType) {
630343d69750e7f7b26076e7474dec8839bb777b260fBill Wendling  // C++ [expr]: If an expression initially has the type "reference to T", the
630443d69750e7f7b26076e7474dec8839bb777b260fBill Wendling  // type is adjusted to "T" prior to any further analysis, the expression
630543d69750e7f7b26076e7474dec8839bb777b260fBill Wendling  // designates the object or function denoted by the reference, and the
63067c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl  // expression is an lvalue unless the reference is an rvalue reference and
63077c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl  // the expression is a function call (possibly inside parentheses).
63080e709abafbd939326850501f795cc7a92c88a354Douglas Gregor  assert(!LHS->getAs<ReferenceType>() && "LHS is a reference type?");
63090e709abafbd939326850501f795cc7a92c88a354Douglas Gregor  assert(!RHS->getAs<ReferenceType>() && "RHS is a reference type?");
6310447234dd459a00a5ed9b7c3e066162cd7a75bf2dDouglas Gregor
6311447234dd459a00a5ed9b7c3e066162cd7a75bf2dDouglas Gregor  if (Unqualified) {
6312447234dd459a00a5ed9b7c3e066162cd7a75bf2dDouglas Gregor    LHS = LHS.getUnqualifiedType();
6313447234dd459a00a5ed9b7c3e066162cd7a75bf2dDouglas Gregor    RHS = RHS.getUnqualifiedType();
6314447234dd459a00a5ed9b7c3e066162cd7a75bf2dDouglas Gregor  }
63150e709abafbd939326850501f795cc7a92c88a354Douglas Gregor
63163d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman  QualType LHSCan = getCanonicalType(LHS),
63173d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman           RHSCan = getCanonicalType(RHS);
63183d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman
6319f3692dc4a47dc48d10cec0415c6e9e39b7a39707Chris Lattner  // If two types are identical, they are compatible.
63203d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman  if (LHSCan == RHSCan)
63213d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    return LHS;
63223d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman
63230953e767ff7817f97b3ab20896b229891eeff45bJohn McCall  // If the qualifiers are different, the types aren't compatible... mostly.
6324a4923eb7c4b04d360cb2747641a5e92818edf804Douglas Gregor  Qualifiers LQuals = LHSCan.getLocalQualifiers();
6325a4923eb7c4b04d360cb2747641a5e92818edf804Douglas Gregor  Qualifiers RQuals = RHSCan.getLocalQualifiers();
63260953e767ff7817f97b3ab20896b229891eeff45bJohn McCall  if (LQuals != RQuals) {
63270953e767ff7817f97b3ab20896b229891eeff45bJohn McCall    // If any of these qualifiers are different, we have a type
63280953e767ff7817f97b3ab20896b229891eeff45bJohn McCall    // mismatch.
63290953e767ff7817f97b3ab20896b229891eeff45bJohn McCall    if (LQuals.getCVRQualifiers() != RQuals.getCVRQualifiers() ||
6330f85e193739c953358c865005855253af4f68a497John McCall        LQuals.getAddressSpace() != RQuals.getAddressSpace() ||
6331f85e193739c953358c865005855253af4f68a497John McCall        LQuals.getObjCLifetime() != RQuals.getObjCLifetime())
63320953e767ff7817f97b3ab20896b229891eeff45bJohn McCall      return QualType();
63330953e767ff7817f97b3ab20896b229891eeff45bJohn McCall
63340953e767ff7817f97b3ab20896b229891eeff45bJohn McCall    // Exactly one GC qualifier difference is allowed: __strong is
63350953e767ff7817f97b3ab20896b229891eeff45bJohn McCall    // okay if the other type has no GC qualifier but is an Objective
63360953e767ff7817f97b3ab20896b229891eeff45bJohn McCall    // C object pointer (i.e. implicitly strong by default).  We fix
63370953e767ff7817f97b3ab20896b229891eeff45bJohn McCall    // this by pretending that the unqualified type was actually
63380953e767ff7817f97b3ab20896b229891eeff45bJohn McCall    // qualified __strong.
63390953e767ff7817f97b3ab20896b229891eeff45bJohn McCall    Qualifiers::GC GC_L = LQuals.getObjCGCAttr();
63400953e767ff7817f97b3ab20896b229891eeff45bJohn McCall    Qualifiers::GC GC_R = RQuals.getObjCGCAttr();
63410953e767ff7817f97b3ab20896b229891eeff45bJohn McCall    assert((GC_L != GC_R) && "unequal qualifier sets had only equal elements");
63420953e767ff7817f97b3ab20896b229891eeff45bJohn McCall
63430953e767ff7817f97b3ab20896b229891eeff45bJohn McCall    if (GC_L == Qualifiers::Weak || GC_R == Qualifiers::Weak)
63440953e767ff7817f97b3ab20896b229891eeff45bJohn McCall      return QualType();
63450953e767ff7817f97b3ab20896b229891eeff45bJohn McCall
63460953e767ff7817f97b3ab20896b229891eeff45bJohn McCall    if (GC_L == Qualifiers::Strong && RHSCan->isObjCObjectPointerType()) {
63470953e767ff7817f97b3ab20896b229891eeff45bJohn McCall      return mergeTypes(LHS, getObjCGCQualType(RHS, Qualifiers::Strong));
63480953e767ff7817f97b3ab20896b229891eeff45bJohn McCall    }
63490953e767ff7817f97b3ab20896b229891eeff45bJohn McCall    if (GC_R == Qualifiers::Strong && LHSCan->isObjCObjectPointerType()) {
63500953e767ff7817f97b3ab20896b229891eeff45bJohn McCall      return mergeTypes(getObjCGCQualType(LHS, Qualifiers::Strong), RHS);
63510953e767ff7817f97b3ab20896b229891eeff45bJohn McCall    }
63523d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    return QualType();
63530953e767ff7817f97b3ab20896b229891eeff45bJohn McCall  }
63540953e767ff7817f97b3ab20896b229891eeff45bJohn McCall
63550953e767ff7817f97b3ab20896b229891eeff45bJohn McCall  // Okay, qualifiers are equal.
63563d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman
6357852d63b806c5cbd730c6b9d696e2e27d02546b49Eli Friedman  Type::TypeClass LHSClass = LHSCan->getTypeClass();
6358852d63b806c5cbd730c6b9d696e2e27d02546b49Eli Friedman  Type::TypeClass RHSClass = RHSCan->getTypeClass();
6359f3692dc4a47dc48d10cec0415c6e9e39b7a39707Chris Lattner
63601adb88370beab45af2f065afe86b51ccd59ec50dChris Lattner  // We want to consider the two function types to be the same for these
63611adb88370beab45af2f065afe86b51ccd59ec50dChris Lattner  // comparisons, just force one to the other.
63621adb88370beab45af2f065afe86b51ccd59ec50dChris Lattner  if (LHSClass == Type::FunctionProto) LHSClass = Type::FunctionNoProto;
63631adb88370beab45af2f065afe86b51ccd59ec50dChris Lattner  if (RHSClass == Type::FunctionProto) RHSClass = Type::FunctionNoProto;
63644c721d381fb279899337d120edd4a24d405e56b2Eli Friedman
63654c721d381fb279899337d120edd4a24d405e56b2Eli Friedman  // Same as above for arrays
6366a36a61f218b9f7a97f2c0f511e0b29eb42e8f78bChris Lattner  if (LHSClass == Type::VariableArray || LHSClass == Type::IncompleteArray)
6367a36a61f218b9f7a97f2c0f511e0b29eb42e8f78bChris Lattner    LHSClass = Type::ConstantArray;
6368a36a61f218b9f7a97f2c0f511e0b29eb42e8f78bChris Lattner  if (RHSClass == Type::VariableArray || RHSClass == Type::IncompleteArray)
6369a36a61f218b9f7a97f2c0f511e0b29eb42e8f78bChris Lattner    RHSClass = Type::ConstantArray;
63701eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
6371c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall  // ObjCInterfaces are just specialized ObjCObjects.
6372c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall  if (LHSClass == Type::ObjCInterface) LHSClass = Type::ObjCObject;
6373c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall  if (RHSClass == Type::ObjCInterface) RHSClass = Type::ObjCObject;
6374c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall
6375213541a68a3e137d11d2cefb612c6cdb410d7e8eNate Begeman  // Canonicalize ExtVector -> Vector.
6376213541a68a3e137d11d2cefb612c6cdb410d7e8eNate Begeman  if (LHSClass == Type::ExtVector) LHSClass = Type::Vector;
6377213541a68a3e137d11d2cefb612c6cdb410d7e8eNate Begeman  if (RHSClass == Type::ExtVector) RHSClass = Type::Vector;
63781eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
63794e78fd0a960eaa7e97467f2e8f390f3a57da279bSteve Naroff  // If the canonical type classes don't match.
63804e78fd0a960eaa7e97467f2e8f390f3a57da279bSteve Naroff  if (LHSClass != RHSClass) {
63811adb88370beab45af2f065afe86b51ccd59ec50dChris Lattner    // C99 6.7.2.2p4: Each enumerated type shall be compatible with char,
63821eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    // a signed integer type, or an unsigned integer type.
6383842aef8d942a880eeb9535d40de31a86838264cbJohn McCall    // Compatibility is based on the underlying type, not the promotion
6384842aef8d942a880eeb9535d40de31a86838264cbJohn McCall    // type.
6385183700f494ec9b6701b6efe82bcb25f4c79ba561John McCall    if (const EnumType* ETy = LHS->getAs<EnumType>()) {
6386b918d6b1f794266e126e5445d69233462087d764Fariborz Jahanian      QualType TINT = ETy->getDecl()->getIntegerType();
6387b918d6b1f794266e126e5445d69233462087d764Fariborz Jahanian      if (!TINT.isNull() && hasSameType(TINT, RHSCan.getUnqualifiedType()))
63883d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman        return RHS;
6389bab96968886f4b77083f4e26a28986ddb1e42d67Eli Friedman    }
6390183700f494ec9b6701b6efe82bcb25f4c79ba561John McCall    if (const EnumType* ETy = RHS->getAs<EnumType>()) {
6391b918d6b1f794266e126e5445d69233462087d764Fariborz Jahanian      QualType TINT = ETy->getDecl()->getIntegerType();
6392b918d6b1f794266e126e5445d69233462087d764Fariborz Jahanian      if (!TINT.isNull() && hasSameType(TINT, LHSCan.getUnqualifiedType()))
63933d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman        return LHS;
6394bab96968886f4b77083f4e26a28986ddb1e42d67Eli Friedman    }
6395e7cff2c00c3d74e38984a4db5c48d7e303d3ab8eFariborz Jahanian    // allow block pointer type to match an 'id' type.
6396419636316c04d570c183b4797fc811932c588358Fariborz Jahanian    if (OfBlockPointer && !BlockReturnType) {
6397419636316c04d570c183b4797fc811932c588358Fariborz Jahanian       if (LHS->isObjCIdType() && RHS->isBlockPointerType())
6398419636316c04d570c183b4797fc811932c588358Fariborz Jahanian         return LHS;
6399419636316c04d570c183b4797fc811932c588358Fariborz Jahanian      if (RHS->isObjCIdType() && LHS->isBlockPointerType())
6400419636316c04d570c183b4797fc811932c588358Fariborz Jahanian        return RHS;
6401419636316c04d570c183b4797fc811932c588358Fariborz Jahanian    }
6402e7cff2c00c3d74e38984a4db5c48d7e303d3ab8eFariborz Jahanian
64033d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    return QualType();
6404ec0550fa3653d46560bf4484a2e988329c228e39Steve Naroff  }
64053d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman
64064a74678ed6c3dedac05d02b1ee341f1db869f049Steve Naroff  // The canonical type classes match.
64071adb88370beab45af2f065afe86b51ccd59ec50dChris Lattner  switch (LHSClass) {
640872564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor#define TYPE(Class, Base)
640972564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor#define ABSTRACT_TYPE(Class, Base)
6410ad5e73887052193afda72db8efcb812bd083a4a8John McCall#define NON_CANONICAL_UNLESS_DEPENDENT_TYPE(Class, Base) case Type::Class:
641172564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor#define NON_CANONICAL_TYPE(Class, Base) case Type::Class:
641272564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor#define DEPENDENT_TYPE(Class, Base) case Type::Class:
641372564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor#include "clang/AST/TypeNodes.def"
6414b219cfc4d75f0a03630b7c4509ef791b7e97b2c8David Blaikie    llvm_unreachable("Non-canonical and dependent types shouldn't get here");
641572564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor
64167c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl  case Type::LValueReference:
64177c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl  case Type::RValueReference:
641872564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor  case Type::MemberPointer:
6419b219cfc4d75f0a03630b7c4509ef791b7e97b2c8David Blaikie    llvm_unreachable("C++ should never be in mergeTypes");
642072564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor
6421c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall  case Type::ObjCInterface:
642272564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor  case Type::IncompleteArray:
642372564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor  case Type::VariableArray:
642472564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor  case Type::FunctionProto:
642572564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor  case Type::ExtVector:
6426b219cfc4d75f0a03630b7c4509ef791b7e97b2c8David Blaikie    llvm_unreachable("Types are eliminated above");
642772564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor
64281adb88370beab45af2f065afe86b51ccd59ec50dChris Lattner  case Type::Pointer:
64293d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman  {
64303d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    // Merge two pointer types, while trying to preserve typedef info
64316217b80b7a1379b74cced1c076338262c3c980b3Ted Kremenek    QualType LHSPointee = LHS->getAs<PointerType>()->getPointeeType();
64326217b80b7a1379b74cced1c076338262c3c980b3Ted Kremenek    QualType RHSPointee = RHS->getAs<PointerType>()->getPointeeType();
6433447234dd459a00a5ed9b7c3e066162cd7a75bf2dDouglas Gregor    if (Unqualified) {
6434447234dd459a00a5ed9b7c3e066162cd7a75bf2dDouglas Gregor      LHSPointee = LHSPointee.getUnqualifiedType();
6435447234dd459a00a5ed9b7c3e066162cd7a75bf2dDouglas Gregor      RHSPointee = RHSPointee.getUnqualifiedType();
6436447234dd459a00a5ed9b7c3e066162cd7a75bf2dDouglas Gregor    }
6437447234dd459a00a5ed9b7c3e066162cd7a75bf2dDouglas Gregor    QualType ResultType = mergeTypes(LHSPointee, RHSPointee, false,
6438447234dd459a00a5ed9b7c3e066162cd7a75bf2dDouglas Gregor                                     Unqualified);
64393d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    if (ResultType.isNull()) return QualType();
644007d258756dc856c6987c394a0972884e6ed46765Eli Friedman    if (getCanonicalType(LHSPointee) == getCanonicalType(ResultType))
644161710854be2b098428aff5316e64bd34b30fbcb7Chris Lattner      return LHS;
644207d258756dc856c6987c394a0972884e6ed46765Eli Friedman    if (getCanonicalType(RHSPointee) == getCanonicalType(ResultType))
644361710854be2b098428aff5316e64bd34b30fbcb7Chris Lattner      return RHS;
64443d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    return getPointerType(ResultType);
64453d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman  }
6446c0febd58f5cbf4a93fd12f461863564dba0af76dSteve Naroff  case Type::BlockPointer:
6447c0febd58f5cbf4a93fd12f461863564dba0af76dSteve Naroff  {
6448c0febd58f5cbf4a93fd12f461863564dba0af76dSteve Naroff    // Merge two block pointer types, while trying to preserve typedef info
64496217b80b7a1379b74cced1c076338262c3c980b3Ted Kremenek    QualType LHSPointee = LHS->getAs<BlockPointerType>()->getPointeeType();
64506217b80b7a1379b74cced1c076338262c3c980b3Ted Kremenek    QualType RHSPointee = RHS->getAs<BlockPointerType>()->getPointeeType();
6451447234dd459a00a5ed9b7c3e066162cd7a75bf2dDouglas Gregor    if (Unqualified) {
6452447234dd459a00a5ed9b7c3e066162cd7a75bf2dDouglas Gregor      LHSPointee = LHSPointee.getUnqualifiedType();
6453447234dd459a00a5ed9b7c3e066162cd7a75bf2dDouglas Gregor      RHSPointee = RHSPointee.getUnqualifiedType();
6454447234dd459a00a5ed9b7c3e066162cd7a75bf2dDouglas Gregor    }
6455447234dd459a00a5ed9b7c3e066162cd7a75bf2dDouglas Gregor    QualType ResultType = mergeTypes(LHSPointee, RHSPointee, OfBlockPointer,
6456447234dd459a00a5ed9b7c3e066162cd7a75bf2dDouglas Gregor                                     Unqualified);
6457c0febd58f5cbf4a93fd12f461863564dba0af76dSteve Naroff    if (ResultType.isNull()) return QualType();
6458c0febd58f5cbf4a93fd12f461863564dba0af76dSteve Naroff    if (getCanonicalType(LHSPointee) == getCanonicalType(ResultType))
6459c0febd58f5cbf4a93fd12f461863564dba0af76dSteve Naroff      return LHS;
6460c0febd58f5cbf4a93fd12f461863564dba0af76dSteve Naroff    if (getCanonicalType(RHSPointee) == getCanonicalType(ResultType))
6461c0febd58f5cbf4a93fd12f461863564dba0af76dSteve Naroff      return RHS;
6462c0febd58f5cbf4a93fd12f461863564dba0af76dSteve Naroff    return getBlockPointerType(ResultType);
6463c0febd58f5cbf4a93fd12f461863564dba0af76dSteve Naroff  }
6464b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman  case Type::Atomic:
6465b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman  {
6466b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman    // Merge two pointer types, while trying to preserve typedef info
6467b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman    QualType LHSValue = LHS->getAs<AtomicType>()->getValueType();
6468b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman    QualType RHSValue = RHS->getAs<AtomicType>()->getValueType();
6469b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman    if (Unqualified) {
6470b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman      LHSValue = LHSValue.getUnqualifiedType();
6471b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman      RHSValue = RHSValue.getUnqualifiedType();
6472b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman    }
6473b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman    QualType ResultType = mergeTypes(LHSValue, RHSValue, false,
6474b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman                                     Unqualified);
6475b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman    if (ResultType.isNull()) return QualType();
6476b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman    if (getCanonicalType(LHSValue) == getCanonicalType(ResultType))
6477b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman      return LHS;
6478b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman    if (getCanonicalType(RHSValue) == getCanonicalType(ResultType))
6479b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman      return RHS;
6480b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman    return getAtomicType(ResultType);
6481b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman  }
64821adb88370beab45af2f065afe86b51ccd59ec50dChris Lattner  case Type::ConstantArray:
64833d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman  {
64843d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    const ConstantArrayType* LCAT = getAsConstantArrayType(LHS);
64853d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    const ConstantArrayType* RCAT = getAsConstantArrayType(RHS);
64863d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    if (LCAT && RCAT && RCAT->getSize() != LCAT->getSize())
64873d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman      return QualType();
64883d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman
64893d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    QualType LHSElem = getAsArrayType(LHS)->getElementType();
64903d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    QualType RHSElem = getAsArrayType(RHS)->getElementType();
6491447234dd459a00a5ed9b7c3e066162cd7a75bf2dDouglas Gregor    if (Unqualified) {
6492447234dd459a00a5ed9b7c3e066162cd7a75bf2dDouglas Gregor      LHSElem = LHSElem.getUnqualifiedType();
6493447234dd459a00a5ed9b7c3e066162cd7a75bf2dDouglas Gregor      RHSElem = RHSElem.getUnqualifiedType();
6494447234dd459a00a5ed9b7c3e066162cd7a75bf2dDouglas Gregor    }
6495447234dd459a00a5ed9b7c3e066162cd7a75bf2dDouglas Gregor
6496447234dd459a00a5ed9b7c3e066162cd7a75bf2dDouglas Gregor    QualType ResultType = mergeTypes(LHSElem, RHSElem, false, Unqualified);
64973d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    if (ResultType.isNull()) return QualType();
649861710854be2b098428aff5316e64bd34b30fbcb7Chris Lattner    if (LCAT && getCanonicalType(LHSElem) == getCanonicalType(ResultType))
649961710854be2b098428aff5316e64bd34b30fbcb7Chris Lattner      return LHS;
650061710854be2b098428aff5316e64bd34b30fbcb7Chris Lattner    if (RCAT && getCanonicalType(RHSElem) == getCanonicalType(ResultType))
650161710854be2b098428aff5316e64bd34b30fbcb7Chris Lattner      return RHS;
65023bc0f45a5e65814f42b22dcdf7249d1120d16f36Eli Friedman    if (LCAT) return getConstantArrayType(ResultType, LCAT->getSize(),
65033bc0f45a5e65814f42b22dcdf7249d1120d16f36Eli Friedman                                          ArrayType::ArraySizeModifier(), 0);
65043bc0f45a5e65814f42b22dcdf7249d1120d16f36Eli Friedman    if (RCAT) return getConstantArrayType(ResultType, RCAT->getSize(),
65053bc0f45a5e65814f42b22dcdf7249d1120d16f36Eli Friedman                                          ArrayType::ArraySizeModifier(), 0);
65063d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    const VariableArrayType* LVAT = getAsVariableArrayType(LHS);
65073d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    const VariableArrayType* RVAT = getAsVariableArrayType(RHS);
650861710854be2b098428aff5316e64bd34b30fbcb7Chris Lattner    if (LVAT && getCanonicalType(LHSElem) == getCanonicalType(ResultType))
650961710854be2b098428aff5316e64bd34b30fbcb7Chris Lattner      return LHS;
651061710854be2b098428aff5316e64bd34b30fbcb7Chris Lattner    if (RVAT && getCanonicalType(RHSElem) == getCanonicalType(ResultType))
651161710854be2b098428aff5316e64bd34b30fbcb7Chris Lattner      return RHS;
65123d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    if (LVAT) {
65133d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman      // FIXME: This isn't correct! But tricky to implement because
65143d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman      // the array's size has to be the size of LHS, but the type
65153d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman      // has to be different.
65163d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman      return LHS;
65173d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    }
65183d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    if (RVAT) {
65193d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman      // FIXME: This isn't correct! But tricky to implement because
65203d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman      // the array's size has to be the size of RHS, but the type
65213d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman      // has to be different.
65223d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman      return RHS;
65233d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    }
65243bc0f45a5e65814f42b22dcdf7249d1120d16f36Eli Friedman    if (getCanonicalType(LHSElem) == getCanonicalType(ResultType)) return LHS;
65253bc0f45a5e65814f42b22dcdf7249d1120d16f36Eli Friedman    if (getCanonicalType(RHSElem) == getCanonicalType(ResultType)) return RHS;
65267e7eb3da052a6d80ddf2377cab0384c798f73f75Douglas Gregor    return getIncompleteArrayType(ResultType,
65277e7eb3da052a6d80ddf2377cab0384c798f73f75Douglas Gregor                                  ArrayType::ArraySizeModifier(), 0);
65283d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman  }
65291adb88370beab45af2f065afe86b51ccd59ec50dChris Lattner  case Type::FunctionNoProto:
6530447234dd459a00a5ed9b7c3e066162cd7a75bf2dDouglas Gregor    return mergeFunctionTypes(LHS, RHS, OfBlockPointer, Unqualified);
653172564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor  case Type::Record:
653272564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor  case Type::Enum:
65333d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    return QualType();
65341adb88370beab45af2f065afe86b51ccd59ec50dChris Lattner  case Type::Builtin:
65353cc4c0c3058a788689b8fc73c0ac139544435c97Chris Lattner    // Only exactly equal builtin types are compatible, which is tested above.
65363d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    return QualType();
653764cfdb7da3cb744642fe8a99ad5c851ad3c930b2Daniel Dunbar  case Type::Complex:
653864cfdb7da3cb744642fe8a99ad5c851ad3c930b2Daniel Dunbar    // Distinct complex types are incompatible.
653964cfdb7da3cb744642fe8a99ad5c851ad3c930b2Daniel Dunbar    return QualType();
65403cc4c0c3058a788689b8fc73c0ac139544435c97Chris Lattner  case Type::Vector:
65415a61f0e5c5aaecd5713c3fa4b78be7167a7eeff2Eli Friedman    // FIXME: The merged type should be an ExtVector!
65421c471f3e1c0ec8cbc82447bb35908dfc55463e46John McCall    if (areCompatVectorTypes(LHSCan->getAs<VectorType>(),
65431c471f3e1c0ec8cbc82447bb35908dfc55463e46John McCall                             RHSCan->getAs<VectorType>()))
65443d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman      return LHS;
654561710854be2b098428aff5316e64bd34b30fbcb7Chris Lattner    return QualType();
6546c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall  case Type::ObjCObject: {
6547c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall    // Check if the types are assignment compatible.
65485a61f0e5c5aaecd5713c3fa4b78be7167a7eeff2Eli Friedman    // FIXME: This should be type compatibility, e.g. whether
65495a61f0e5c5aaecd5713c3fa4b78be7167a7eeff2Eli Friedman    // "LHS x; RHS x;" at global scope is legal.
6550c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall    const ObjCObjectType* LHSIface = LHS->getAs<ObjCObjectType>();
6551c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall    const ObjCObjectType* RHSIface = RHS->getAs<ObjCObjectType>();
6552c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall    if (canAssignObjCInterfaces(LHSIface, RHSIface))
65535fd659db11922fc12a58e478f7b745f9656b15a7Steve Naroff      return LHS;
65545fd659db11922fc12a58e478f7b745f9656b15a7Steve Naroff
65553d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    return QualType();
655661490e9a965cfee8a78c12c6802138844f04250dCedric Venet  }
655714108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff  case Type::ObjCObjectPointer: {
6558132f2a2da34f378fc675b9e174564b0f52c31d98Fariborz Jahanian    if (OfBlockPointer) {
6559132f2a2da34f378fc675b9e174564b0f52c31d98Fariborz Jahanian      if (canAssignObjCInterfacesInBlockPointer(
6560132f2a2da34f378fc675b9e174564b0f52c31d98Fariborz Jahanian                                          LHS->getAs<ObjCObjectPointerType>(),
6561a4fdbfad150ae37bddaa4094d3925a27a1a1cf3fFariborz Jahanian                                          RHS->getAs<ObjCObjectPointerType>(),
6562a4fdbfad150ae37bddaa4094d3925a27a1a1cf3fFariborz Jahanian                                          BlockReturnType))
65637530c034c0c71a64c5a9173206d9742ae847af8bDavid Blaikie        return LHS;
6564132f2a2da34f378fc675b9e174564b0f52c31d98Fariborz Jahanian      return QualType();
6565132f2a2da34f378fc675b9e174564b0f52c31d98Fariborz Jahanian    }
6566183700f494ec9b6701b6efe82bcb25f4c79ba561John McCall    if (canAssignObjCInterfaces(LHS->getAs<ObjCObjectPointerType>(),
6567183700f494ec9b6701b6efe82bcb25f4c79ba561John McCall                                RHS->getAs<ObjCObjectPointerType>()))
656814108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff      return LHS;
656914108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff
6570bc76dd06eb881c70c9775b74bab8b88cd747f173Steve Naroff    return QualType();
65717530c034c0c71a64c5a9173206d9742ae847af8bDavid Blaikie  }
6572ec0550fa3653d46560bf4484a2e988329c228e39Steve Naroff  }
657372564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor
65747530c034c0c71a64c5a9173206d9742ae847af8bDavid Blaikie  llvm_unreachable("Invalid Type::Class!");
6575ec0550fa3653d46560bf4484a2e988329c228e39Steve Naroff}
65767192f8e9592729882a09d84d77838db26e39ebd4Ted Kremenek
657778213e4aec6d8d22b5828dd2687fa19116ebaa26Fariborz Jahanianbool ASTContext::FunctionTypesMatchOnNSConsumedAttrs(
657878213e4aec6d8d22b5828dd2687fa19116ebaa26Fariborz Jahanian                   const FunctionProtoType *FromFunctionType,
657978213e4aec6d8d22b5828dd2687fa19116ebaa26Fariborz Jahanian                   const FunctionProtoType *ToFunctionType) {
658078213e4aec6d8d22b5828dd2687fa19116ebaa26Fariborz Jahanian  if (FromFunctionType->hasAnyConsumedArgs() !=
658178213e4aec6d8d22b5828dd2687fa19116ebaa26Fariborz Jahanian      ToFunctionType->hasAnyConsumedArgs())
658278213e4aec6d8d22b5828dd2687fa19116ebaa26Fariborz Jahanian    return false;
658378213e4aec6d8d22b5828dd2687fa19116ebaa26Fariborz Jahanian  FunctionProtoType::ExtProtoInfo FromEPI =
658478213e4aec6d8d22b5828dd2687fa19116ebaa26Fariborz Jahanian    FromFunctionType->getExtProtoInfo();
658578213e4aec6d8d22b5828dd2687fa19116ebaa26Fariborz Jahanian  FunctionProtoType::ExtProtoInfo ToEPI =
658678213e4aec6d8d22b5828dd2687fa19116ebaa26Fariborz Jahanian    ToFunctionType->getExtProtoInfo();
658778213e4aec6d8d22b5828dd2687fa19116ebaa26Fariborz Jahanian  if (FromEPI.ConsumedArguments && ToEPI.ConsumedArguments)
658878213e4aec6d8d22b5828dd2687fa19116ebaa26Fariborz Jahanian    for (unsigned ArgIdx = 0, NumArgs = FromFunctionType->getNumArgs();
658978213e4aec6d8d22b5828dd2687fa19116ebaa26Fariborz Jahanian         ArgIdx != NumArgs; ++ArgIdx)  {
659078213e4aec6d8d22b5828dd2687fa19116ebaa26Fariborz Jahanian      if (FromEPI.ConsumedArguments[ArgIdx] !=
659178213e4aec6d8d22b5828dd2687fa19116ebaa26Fariborz Jahanian          ToEPI.ConsumedArguments[ArgIdx])
659278213e4aec6d8d22b5828dd2687fa19116ebaa26Fariborz Jahanian        return false;
659378213e4aec6d8d22b5828dd2687fa19116ebaa26Fariborz Jahanian    }
659478213e4aec6d8d22b5828dd2687fa19116ebaa26Fariborz Jahanian  return true;
659578213e4aec6d8d22b5828dd2687fa19116ebaa26Fariborz Jahanian}
659678213e4aec6d8d22b5828dd2687fa19116ebaa26Fariborz Jahanian
65972390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian/// mergeObjCGCQualifiers - This routine merges ObjC's GC attribute of 'LHS' and
65982390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian/// 'RHS' attributes and returns the merged version; including for function
65992390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian/// return types.
66002390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz JahanianQualType ASTContext::mergeObjCGCQualifiers(QualType LHS, QualType RHS) {
66012390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian  QualType LHSCan = getCanonicalType(LHS),
66022390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian  RHSCan = getCanonicalType(RHS);
66032390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian  // If two types are identical, they are compatible.
66042390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian  if (LHSCan == RHSCan)
66052390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian    return LHS;
66062390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian  if (RHSCan->isFunctionType()) {
66072390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian    if (!LHSCan->isFunctionType())
66082390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian      return QualType();
66092390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian    QualType OldReturnType =
66102390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian      cast<FunctionType>(RHSCan.getTypePtr())->getResultType();
66112390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian    QualType NewReturnType =
66122390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian      cast<FunctionType>(LHSCan.getTypePtr())->getResultType();
66132390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian    QualType ResReturnType =
66142390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian      mergeObjCGCQualifiers(NewReturnType, OldReturnType);
66152390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian    if (ResReturnType.isNull())
66162390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian      return QualType();
66172390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian    if (ResReturnType == NewReturnType || ResReturnType == OldReturnType) {
66182390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian      // id foo(); ... __strong id foo(); or: __strong id foo(); ... id foo();
66192390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian      // In either case, use OldReturnType to build the new function type.
66202390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian      const FunctionType *F = LHS->getAs<FunctionType>();
66212390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian      if (const FunctionProtoType *FPT = cast<FunctionProtoType>(F)) {
6622e23cf437fe76b1ed02d63c3f61b456fd48a915f5John McCall        FunctionProtoType::ExtProtoInfo EPI = FPT->getExtProtoInfo();
6623e23cf437fe76b1ed02d63c3f61b456fd48a915f5John McCall        EPI.ExtInfo = getFunctionExtInfo(LHS);
66242390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian        QualType ResultType
66252390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian          = getFunctionType(OldReturnType, FPT->arg_type_begin(),
6626e23cf437fe76b1ed02d63c3f61b456fd48a915f5John McCall                            FPT->getNumArgs(), EPI);
66272390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian        return ResultType;
66282390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian      }
66292390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian    }
66302390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian    return QualType();
66312390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian  }
66322390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian
66332390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian  // If the qualifiers are different, the types can still be merged.
66342390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian  Qualifiers LQuals = LHSCan.getLocalQualifiers();
66352390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian  Qualifiers RQuals = RHSCan.getLocalQualifiers();
66362390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian  if (LQuals != RQuals) {
66372390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian    // If any of these qualifiers are different, we have a type mismatch.
66382390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian    if (LQuals.getCVRQualifiers() != RQuals.getCVRQualifiers() ||
66392390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian        LQuals.getAddressSpace() != RQuals.getAddressSpace())
66402390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian      return QualType();
66412390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian
66422390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian    // Exactly one GC qualifier difference is allowed: __strong is
66432390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian    // okay if the other type has no GC qualifier but is an Objective
66442390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian    // C object pointer (i.e. implicitly strong by default).  We fix
66452390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian    // this by pretending that the unqualified type was actually
66462390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian    // qualified __strong.
66472390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian    Qualifiers::GC GC_L = LQuals.getObjCGCAttr();
66482390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian    Qualifiers::GC GC_R = RQuals.getObjCGCAttr();
66492390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian    assert((GC_L != GC_R) && "unequal qualifier sets had only equal elements");
66502390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian
66512390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian    if (GC_L == Qualifiers::Weak || GC_R == Qualifiers::Weak)
66522390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian      return QualType();
66532390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian
66542390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian    if (GC_L == Qualifiers::Strong)
66552390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian      return LHS;
66562390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian    if (GC_R == Qualifiers::Strong)
66572390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian      return RHS;
66582390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian    return QualType();
66592390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian  }
66602390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian
66612390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian  if (LHSCan->isObjCObjectPointerType() && RHSCan->isObjCObjectPointerType()) {
66622390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian    QualType LHSBaseQT = LHS->getAs<ObjCObjectPointerType>()->getPointeeType();
66632390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian    QualType RHSBaseQT = RHS->getAs<ObjCObjectPointerType>()->getPointeeType();
66642390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian    QualType ResQT = mergeObjCGCQualifiers(LHSBaseQT, RHSBaseQT);
66652390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian    if (ResQT == LHSBaseQT)
66662390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian      return LHS;
66672390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian    if (ResQT == RHSBaseQT)
66682390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian      return RHS;
66692390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian  }
66702390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian  return QualType();
66712390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian}
66722390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian
66735426bf6456a5aeac416a9150de157904d101c819Chris Lattner//===----------------------------------------------------------------------===//
6674ad74a758189180b8ab8faea648e4766c3bfd7fcbEli Friedman//                         Integer Predicates
6675ad74a758189180b8ab8faea648e4766c3bfd7fcbEli Friedman//===----------------------------------------------------------------------===//
667688054dee0402e4d3c1f64e6b697acc47195c0d72Chris Lattner
66774ba2a17694148e16eaa8d3917f657ffcd3667be4Jay Foadunsigned ASTContext::getIntWidth(QualType T) const {
6678f4c7371fb1d3cebcfb40abad4537bb82515704eaJohn McCall  if (const EnumType *ET = dyn_cast<EnumType>(T))
667929a7f3342c3c6dd15d914c61ae22246c36d51ce7Eli Friedman    T = ET->getDecl()->getIntegerType();
66801274ccd90aec0b205fc838c3d504821ccfb55482Douglas Gregor  if (T->isBooleanType())
66811274ccd90aec0b205fc838c3d504821ccfb55482Douglas Gregor    return 1;
6682f98aba35e6c3da5aae61843fc01334939e4e12ecEli Friedman  // For builtin types, just use the standard type sizing method
6683ad74a758189180b8ab8faea648e4766c3bfd7fcbEli Friedman  return (unsigned)getTypeSize(T);
6684ad74a758189180b8ab8faea648e4766c3bfd7fcbEli Friedman}
6685ad74a758189180b8ab8faea648e4766c3bfd7fcbEli Friedman
6686ad74a758189180b8ab8faea648e4766c3bfd7fcbEli FriedmanQualType ASTContext::getCorrespondingUnsignedType(QualType T) {
6687f60946222721d9ba3c059563935c17b84703187aDouglas Gregor  assert(T->hasSignedIntegerRepresentation() && "Unexpected type");
66886a2b9261bf9c973c7122d9d1febce24a38fa862dChris Lattner
66896a2b9261bf9c973c7122d9d1febce24a38fa862dChris Lattner  // Turn <4 x signed int> -> <4 x unsigned int>
66906a2b9261bf9c973c7122d9d1febce24a38fa862dChris Lattner  if (const VectorType *VTy = T->getAs<VectorType>())
66916a2b9261bf9c973c7122d9d1febce24a38fa862dChris Lattner    return getVectorType(getCorrespondingUnsignedType(VTy->getElementType()),
6692e86d78cf4754a6aef2cf9a33d847aa15338e276fBob Wilson                         VTy->getNumElements(), VTy->getVectorKind());
66936a2b9261bf9c973c7122d9d1febce24a38fa862dChris Lattner
66946a2b9261bf9c973c7122d9d1febce24a38fa862dChris Lattner  // For enums, we return the unsigned version of the base type.
66956a2b9261bf9c973c7122d9d1febce24a38fa862dChris Lattner  if (const EnumType *ETy = T->getAs<EnumType>())
6696ad74a758189180b8ab8faea648e4766c3bfd7fcbEli Friedman    T = ETy->getDecl()->getIntegerType();
66976a2b9261bf9c973c7122d9d1febce24a38fa862dChris Lattner
66986a2b9261bf9c973c7122d9d1febce24a38fa862dChris Lattner  const BuiltinType *BTy = T->getAs<BuiltinType>();
66996a2b9261bf9c973c7122d9d1febce24a38fa862dChris Lattner  assert(BTy && "Unexpected signed integer type");
6700ad74a758189180b8ab8faea648e4766c3bfd7fcbEli Friedman  switch (BTy->getKind()) {
6701ad74a758189180b8ab8faea648e4766c3bfd7fcbEli Friedman  case BuiltinType::Char_S:
6702ad74a758189180b8ab8faea648e4766c3bfd7fcbEli Friedman  case BuiltinType::SChar:
6703ad74a758189180b8ab8faea648e4766c3bfd7fcbEli Friedman    return UnsignedCharTy;
6704ad74a758189180b8ab8faea648e4766c3bfd7fcbEli Friedman  case BuiltinType::Short:
6705ad74a758189180b8ab8faea648e4766c3bfd7fcbEli Friedman    return UnsignedShortTy;
6706ad74a758189180b8ab8faea648e4766c3bfd7fcbEli Friedman  case BuiltinType::Int:
6707ad74a758189180b8ab8faea648e4766c3bfd7fcbEli Friedman    return UnsignedIntTy;
6708ad74a758189180b8ab8faea648e4766c3bfd7fcbEli Friedman  case BuiltinType::Long:
6709ad74a758189180b8ab8faea648e4766c3bfd7fcbEli Friedman    return UnsignedLongTy;
6710ad74a758189180b8ab8faea648e4766c3bfd7fcbEli Friedman  case BuiltinType::LongLong:
6711ad74a758189180b8ab8faea648e4766c3bfd7fcbEli Friedman    return UnsignedLongLongTy;
67122df9ced9fd1e8c7d7b38443db07e0e811de22571Chris Lattner  case BuiltinType::Int128:
67132df9ced9fd1e8c7d7b38443db07e0e811de22571Chris Lattner    return UnsignedInt128Ty;
6714ad74a758189180b8ab8faea648e4766c3bfd7fcbEli Friedman  default:
6715b219cfc4d75f0a03630b7c4509ef791b7e97b2c8David Blaikie    llvm_unreachable("Unexpected signed integer type");
6716ad74a758189180b8ab8faea648e4766c3bfd7fcbEli Friedman  }
6717ad74a758189180b8ab8faea648e4766c3bfd7fcbEli Friedman}
6718ad74a758189180b8ab8faea648e4766c3bfd7fcbEli Friedman
67197b90340c9c7d07aef4e301e72b5e8a30d5f4f0c8Argyrios KyrtzidisASTMutationListener::~ASTMutationListener() { }
67207b90340c9c7d07aef4e301e72b5e8a30d5f4f0c8Argyrios Kyrtzidis
672186df27bbdbb98c39ec2184695c0561209f91beddChris Lattner
672286df27bbdbb98c39ec2184695c0561209f91beddChris Lattner//===----------------------------------------------------------------------===//
672386df27bbdbb98c39ec2184695c0561209f91beddChris Lattner//                          Builtin Type Computation
672486df27bbdbb98c39ec2184695c0561209f91beddChris Lattner//===----------------------------------------------------------------------===//
672586df27bbdbb98c39ec2184695c0561209f91beddChris Lattner
672686df27bbdbb98c39ec2184695c0561209f91beddChris Lattner/// DecodeTypeFromStr - This decodes one type descriptor from Str, advancing the
672733daae66462e8f51ee153463b32bdefd60c801aaChris Lattner/// pointer over the consumed characters.  This returns the resultant type.  If
672833daae66462e8f51ee153463b32bdefd60c801aaChris Lattner/// AllowTypeModifiers is false then modifier like * are not parsed, just basic
672933daae66462e8f51ee153463b32bdefd60c801aaChris Lattner/// types.  This allows "v2i*" to be parsed as a pointer to a v2i instead of
673033daae66462e8f51ee153463b32bdefd60c801aaChris Lattner/// a vector of "i*".
673114e0e7436cf6650a72052baea1f8ebe644cef489Chris Lattner///
673214e0e7436cf6650a72052baea1f8ebe644cef489Chris Lattner/// RequiresICE is filled in on return to indicate whether the value is required
673314e0e7436cf6650a72052baea1f8ebe644cef489Chris Lattner/// to be an Integer Constant Expression.
67344ba2a17694148e16eaa8d3917f657ffcd3667be4Jay Foadstatic QualType DecodeTypeFromStr(const char *&Str, const ASTContext &Context,
673586df27bbdbb98c39ec2184695c0561209f91beddChris Lattner                                  ASTContext::GetBuiltinTypeError &Error,
673614e0e7436cf6650a72052baea1f8ebe644cef489Chris Lattner                                  bool &RequiresICE,
673733daae66462e8f51ee153463b32bdefd60c801aaChris Lattner                                  bool AllowTypeModifiers) {
673886df27bbdbb98c39ec2184695c0561209f91beddChris Lattner  // Modifiers.
673986df27bbdbb98c39ec2184695c0561209f91beddChris Lattner  int HowLong = 0;
674086df27bbdbb98c39ec2184695c0561209f91beddChris Lattner  bool Signed = false, Unsigned = false;
674114e0e7436cf6650a72052baea1f8ebe644cef489Chris Lattner  RequiresICE = false;
6742393bd8e185692a451b2ba16bdfc9e7d3543b4217Chris Lattner
674333daae66462e8f51ee153463b32bdefd60c801aaChris Lattner  // Read the prefixed modifiers first.
674486df27bbdbb98c39ec2184695c0561209f91beddChris Lattner  bool Done = false;
674586df27bbdbb98c39ec2184695c0561209f91beddChris Lattner  while (!Done) {
674686df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    switch (*Str++) {
67471eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    default: Done = true; --Str; break;
6748393bd8e185692a451b2ba16bdfc9e7d3543b4217Chris Lattner    case 'I':
674914e0e7436cf6650a72052baea1f8ebe644cef489Chris Lattner      RequiresICE = true;
6750393bd8e185692a451b2ba16bdfc9e7d3543b4217Chris Lattner      break;
675186df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    case 'S':
675286df27bbdbb98c39ec2184695c0561209f91beddChris Lattner      assert(!Unsigned && "Can't use both 'S' and 'U' modifiers!");
675386df27bbdbb98c39ec2184695c0561209f91beddChris Lattner      assert(!Signed && "Can't use 'S' modifier multiple times!");
675486df27bbdbb98c39ec2184695c0561209f91beddChris Lattner      Signed = true;
675586df27bbdbb98c39ec2184695c0561209f91beddChris Lattner      break;
675686df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    case 'U':
675786df27bbdbb98c39ec2184695c0561209f91beddChris Lattner      assert(!Signed && "Can't use both 'S' and 'U' modifiers!");
675886df27bbdbb98c39ec2184695c0561209f91beddChris Lattner      assert(!Unsigned && "Can't use 'S' modifier multiple times!");
675986df27bbdbb98c39ec2184695c0561209f91beddChris Lattner      Unsigned = true;
676086df27bbdbb98c39ec2184695c0561209f91beddChris Lattner      break;
676186df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    case 'L':
676286df27bbdbb98c39ec2184695c0561209f91beddChris Lattner      assert(HowLong <= 2 && "Can't have LLLL modifier");
676386df27bbdbb98c39ec2184695c0561209f91beddChris Lattner      ++HowLong;
676486df27bbdbb98c39ec2184695c0561209f91beddChris Lattner      break;
676586df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    }
676686df27bbdbb98c39ec2184695c0561209f91beddChris Lattner  }
676786df27bbdbb98c39ec2184695c0561209f91beddChris Lattner
676886df27bbdbb98c39ec2184695c0561209f91beddChris Lattner  QualType Type;
67691eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
677086df27bbdbb98c39ec2184695c0561209f91beddChris Lattner  // Read the base type.
677186df27bbdbb98c39ec2184695c0561209f91beddChris Lattner  switch (*Str++) {
6772b219cfc4d75f0a03630b7c4509ef791b7e97b2c8David Blaikie  default: llvm_unreachable("Unknown builtin type letter!");
677386df27bbdbb98c39ec2184695c0561209f91beddChris Lattner  case 'v':
677486df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    assert(HowLong == 0 && !Signed && !Unsigned &&
677586df27bbdbb98c39ec2184695c0561209f91beddChris Lattner           "Bad modifiers used with 'v'!");
677686df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    Type = Context.VoidTy;
677786df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    break;
677886df27bbdbb98c39ec2184695c0561209f91beddChris Lattner  case 'f':
677986df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    assert(HowLong == 0 && !Signed && !Unsigned &&
678086df27bbdbb98c39ec2184695c0561209f91beddChris Lattner           "Bad modifiers used with 'f'!");
678186df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    Type = Context.FloatTy;
678286df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    break;
678386df27bbdbb98c39ec2184695c0561209f91beddChris Lattner  case 'd':
678486df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    assert(HowLong < 2 && !Signed && !Unsigned &&
678586df27bbdbb98c39ec2184695c0561209f91beddChris Lattner           "Bad modifiers used with 'd'!");
678686df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    if (HowLong)
678786df27bbdbb98c39ec2184695c0561209f91beddChris Lattner      Type = Context.LongDoubleTy;
678886df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    else
678986df27bbdbb98c39ec2184695c0561209f91beddChris Lattner      Type = Context.DoubleTy;
679086df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    break;
679186df27bbdbb98c39ec2184695c0561209f91beddChris Lattner  case 's':
679286df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    assert(HowLong == 0 && "Bad modifiers used with 's'!");
679386df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    if (Unsigned)
679486df27bbdbb98c39ec2184695c0561209f91beddChris Lattner      Type = Context.UnsignedShortTy;
679586df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    else
679686df27bbdbb98c39ec2184695c0561209f91beddChris Lattner      Type = Context.ShortTy;
679786df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    break;
679886df27bbdbb98c39ec2184695c0561209f91beddChris Lattner  case 'i':
679986df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    if (HowLong == 3)
680086df27bbdbb98c39ec2184695c0561209f91beddChris Lattner      Type = Unsigned ? Context.UnsignedInt128Ty : Context.Int128Ty;
680186df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    else if (HowLong == 2)
680286df27bbdbb98c39ec2184695c0561209f91beddChris Lattner      Type = Unsigned ? Context.UnsignedLongLongTy : Context.LongLongTy;
680386df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    else if (HowLong == 1)
680486df27bbdbb98c39ec2184695c0561209f91beddChris Lattner      Type = Unsigned ? Context.UnsignedLongTy : Context.LongTy;
680586df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    else
680686df27bbdbb98c39ec2184695c0561209f91beddChris Lattner      Type = Unsigned ? Context.UnsignedIntTy : Context.IntTy;
680786df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    break;
680886df27bbdbb98c39ec2184695c0561209f91beddChris Lattner  case 'c':
680986df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    assert(HowLong == 0 && "Bad modifiers used with 'c'!");
681086df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    if (Signed)
681186df27bbdbb98c39ec2184695c0561209f91beddChris Lattner      Type = Context.SignedCharTy;
681286df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    else if (Unsigned)
681386df27bbdbb98c39ec2184695c0561209f91beddChris Lattner      Type = Context.UnsignedCharTy;
681486df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    else
681586df27bbdbb98c39ec2184695c0561209f91beddChris Lattner      Type = Context.CharTy;
681686df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    break;
681786df27bbdbb98c39ec2184695c0561209f91beddChris Lattner  case 'b': // boolean
681886df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    assert(HowLong == 0 && !Signed && !Unsigned && "Bad modifiers for 'b'!");
681986df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    Type = Context.BoolTy;
682086df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    break;
682186df27bbdbb98c39ec2184695c0561209f91beddChris Lattner  case 'z':  // size_t.
682286df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    assert(HowLong == 0 && !Signed && !Unsigned && "Bad modifiers for 'z'!");
682386df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    Type = Context.getSizeType();
682486df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    break;
682586df27bbdbb98c39ec2184695c0561209f91beddChris Lattner  case 'F':
682686df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    Type = Context.getCFConstantStringType();
682786df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    break;
6828ba8bda05fefd3bb2f1ef201784b685f715bdde29Fariborz Jahanian  case 'G':
6829ba8bda05fefd3bb2f1ef201784b685f715bdde29Fariborz Jahanian    Type = Context.getObjCIdType();
6830ba8bda05fefd3bb2f1ef201784b685f715bdde29Fariborz Jahanian    break;
6831ba8bda05fefd3bb2f1ef201784b685f715bdde29Fariborz Jahanian  case 'H':
6832ba8bda05fefd3bb2f1ef201784b685f715bdde29Fariborz Jahanian    Type = Context.getObjCSelType();
6833ba8bda05fefd3bb2f1ef201784b685f715bdde29Fariborz Jahanian    break;
683486df27bbdbb98c39ec2184695c0561209f91beddChris Lattner  case 'a':
683586df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    Type = Context.getBuiltinVaListType();
683686df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    assert(!Type.isNull() && "builtin va list type not initialized!");
683786df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    break;
683886df27bbdbb98c39ec2184695c0561209f91beddChris Lattner  case 'A':
683986df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    // This is a "reference" to a va_list; however, what exactly
684086df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    // this means depends on how va_list is defined. There are two
684186df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    // different kinds of va_list: ones passed by value, and ones
684286df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    // passed by reference.  An example of a by-value va_list is
684386df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    // x86, where va_list is a char*. An example of by-ref va_list
684486df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    // is x86-64, where va_list is a __va_list_tag[1]. For x86,
684586df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    // we want this argument to be a char*&; for x86-64, we want
684686df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    // it to be a __va_list_tag*.
684786df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    Type = Context.getBuiltinVaListType();
684886df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    assert(!Type.isNull() && "builtin va list type not initialized!");
684914e0e7436cf6650a72052baea1f8ebe644cef489Chris Lattner    if (Type->isArrayType())
685086df27bbdbb98c39ec2184695c0561209f91beddChris Lattner      Type = Context.getArrayDecayedType(Type);
685114e0e7436cf6650a72052baea1f8ebe644cef489Chris Lattner    else
685286df27bbdbb98c39ec2184695c0561209f91beddChris Lattner      Type = Context.getLValueReferenceType(Type);
685386df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    break;
685486df27bbdbb98c39ec2184695c0561209f91beddChris Lattner  case 'V': {
685586df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    char *End;
685686df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    unsigned NumElements = strtoul(Str, &End, 10);
685786df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    assert(End != Str && "Missing vector size");
685886df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    Str = End;
68591eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
686014e0e7436cf6650a72052baea1f8ebe644cef489Chris Lattner    QualType ElementType = DecodeTypeFromStr(Str, Context, Error,
686114e0e7436cf6650a72052baea1f8ebe644cef489Chris Lattner                                             RequiresICE, false);
686214e0e7436cf6650a72052baea1f8ebe644cef489Chris Lattner    assert(!RequiresICE && "Can't require vector ICE");
686333daae66462e8f51ee153463b32bdefd60c801aaChris Lattner
686433daae66462e8f51ee153463b32bdefd60c801aaChris Lattner    // TODO: No way to make AltiVec vectors in builtins yet.
6865788b0fd67e1992f23555454efcdb16a19dfefac3Chris Lattner    Type = Context.getVectorType(ElementType, NumElements,
6866e86d78cf4754a6aef2cf9a33d847aa15338e276fBob Wilson                                 VectorType::GenericVector);
686786df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    break;
686886df27bbdbb98c39ec2184695c0561209f91beddChris Lattner  }
6869b4bc99ba5540a618c86c136c9112ead8fb1d9866Douglas Gregor  case 'E': {
6870b4bc99ba5540a618c86c136c9112ead8fb1d9866Douglas Gregor    char *End;
6871b4bc99ba5540a618c86c136c9112ead8fb1d9866Douglas Gregor
6872b4bc99ba5540a618c86c136c9112ead8fb1d9866Douglas Gregor    unsigned NumElements = strtoul(Str, &End, 10);
6873b4bc99ba5540a618c86c136c9112ead8fb1d9866Douglas Gregor    assert(End != Str && "Missing vector size");
6874b4bc99ba5540a618c86c136c9112ead8fb1d9866Douglas Gregor
6875b4bc99ba5540a618c86c136c9112ead8fb1d9866Douglas Gregor    Str = End;
6876b4bc99ba5540a618c86c136c9112ead8fb1d9866Douglas Gregor
6877b4bc99ba5540a618c86c136c9112ead8fb1d9866Douglas Gregor    QualType ElementType = DecodeTypeFromStr(Str, Context, Error, RequiresICE,
6878b4bc99ba5540a618c86c136c9112ead8fb1d9866Douglas Gregor                                             false);
6879b4bc99ba5540a618c86c136c9112ead8fb1d9866Douglas Gregor    Type = Context.getExtVectorType(ElementType, NumElements);
6880b4bc99ba5540a618c86c136c9112ead8fb1d9866Douglas Gregor    break;
6881b4bc99ba5540a618c86c136c9112ead8fb1d9866Douglas Gregor  }
6882d3a23b238a2b2c0f11e6ac4951c7410a8c5717bfDouglas Gregor  case 'X': {
688314e0e7436cf6650a72052baea1f8ebe644cef489Chris Lattner    QualType ElementType = DecodeTypeFromStr(Str, Context, Error, RequiresICE,
688414e0e7436cf6650a72052baea1f8ebe644cef489Chris Lattner                                             false);
688514e0e7436cf6650a72052baea1f8ebe644cef489Chris Lattner    assert(!RequiresICE && "Can't require complex ICE");
6886d3a23b238a2b2c0f11e6ac4951c7410a8c5717bfDouglas Gregor    Type = Context.getComplexType(ElementType);
6887d3a23b238a2b2c0f11e6ac4951c7410a8c5717bfDouglas Gregor    break;
6888cc075e4092900ac9eec9dc92e35096eae1ee29c7Fariborz Jahanian  }
6889cc075e4092900ac9eec9dc92e35096eae1ee29c7Fariborz Jahanian  case 'Y' : {
6890cc075e4092900ac9eec9dc92e35096eae1ee29c7Fariborz Jahanian    Type = Context.getPointerDiffType();
6891cc075e4092900ac9eec9dc92e35096eae1ee29c7Fariborz Jahanian    break;
6892cc075e4092900ac9eec9dc92e35096eae1ee29c7Fariborz Jahanian  }
68939a5a7e7351f78345a72c4956af25590f6d40ebcdChris Lattner  case 'P':
6894c29f77b769bcc5b6dc85e72c8e3cc2e348e5cf25Douglas Gregor    Type = Context.getFILEType();
6895c29f77b769bcc5b6dc85e72c8e3cc2e348e5cf25Douglas Gregor    if (Type.isNull()) {
6896f711c41dd9412a8182793259d355c4f6979ed5edMike Stump      Error = ASTContext::GE_Missing_stdio;
689786df27bbdbb98c39ec2184695c0561209f91beddChris Lattner      return QualType();
689886df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    }
6899fd612dbb23cd31c03c898ae53ff18d0dfd8488f9Mike Stump    break;
69009a5a7e7351f78345a72c4956af25590f6d40ebcdChris Lattner  case 'J':
6901f711c41dd9412a8182793259d355c4f6979ed5edMike Stump    if (Signed)
6902782fa308a765aeac2acb39c4e697c937ec21185bMike Stump      Type = Context.getsigjmp_bufType();
6903f711c41dd9412a8182793259d355c4f6979ed5edMike Stump    else
6904f711c41dd9412a8182793259d355c4f6979ed5edMike Stump      Type = Context.getjmp_bufType();
6905f711c41dd9412a8182793259d355c4f6979ed5edMike Stump
6906fd612dbb23cd31c03c898ae53ff18d0dfd8488f9Mike Stump    if (Type.isNull()) {
6907f711c41dd9412a8182793259d355c4f6979ed5edMike Stump      Error = ASTContext::GE_Missing_setjmp;
6908fd612dbb23cd31c03c898ae53ff18d0dfd8488f9Mike Stump      return QualType();
6909fd612dbb23cd31c03c898ae53ff18d0dfd8488f9Mike Stump    }
6910fd612dbb23cd31c03c898ae53ff18d0dfd8488f9Mike Stump    break;
6911e2d4f4ed44a32f179c6d48cd1dba8346ab2129d9Rafael Espindola  case 'K':
6912e2d4f4ed44a32f179c6d48cd1dba8346ab2129d9Rafael Espindola    assert(HowLong == 0 && !Signed && !Unsigned && "Bad modifiers for 'K'!");
6913e2d4f4ed44a32f179c6d48cd1dba8346ab2129d9Rafael Espindola    Type = Context.getucontext_tType();
6914e2d4f4ed44a32f179c6d48cd1dba8346ab2129d9Rafael Espindola
6915e2d4f4ed44a32f179c6d48cd1dba8346ab2129d9Rafael Espindola    if (Type.isNull()) {
6916e2d4f4ed44a32f179c6d48cd1dba8346ab2129d9Rafael Espindola      Error = ASTContext::GE_Missing_ucontext;
6917e2d4f4ed44a32f179c6d48cd1dba8346ab2129d9Rafael Espindola      return QualType();
6918e2d4f4ed44a32f179c6d48cd1dba8346ab2129d9Rafael Espindola    }
6919e2d4f4ed44a32f179c6d48cd1dba8346ab2129d9Rafael Espindola    break;
6920782fa308a765aeac2acb39c4e697c937ec21185bMike Stump  }
69211eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
692233daae66462e8f51ee153463b32bdefd60c801aaChris Lattner  // If there are modifiers and if we're allowed to parse them, go for it.
692333daae66462e8f51ee153463b32bdefd60c801aaChris Lattner  Done = !AllowTypeModifiers;
692486df27bbdbb98c39ec2184695c0561209f91beddChris Lattner  while (!Done) {
6925187ab37a05b8f7015b9f39cc8cd9129a0c6d0b48John McCall    switch (char c = *Str++) {
692633daae66462e8f51ee153463b32bdefd60c801aaChris Lattner    default: Done = true; --Str; break;
692733daae66462e8f51ee153463b32bdefd60c801aaChris Lattner    case '*':
692833daae66462e8f51ee153463b32bdefd60c801aaChris Lattner    case '&': {
692933daae66462e8f51ee153463b32bdefd60c801aaChris Lattner      // Both pointers and references can have their pointee types
693033daae66462e8f51ee153463b32bdefd60c801aaChris Lattner      // qualified with an address space.
693133daae66462e8f51ee153463b32bdefd60c801aaChris Lattner      char *End;
693233daae66462e8f51ee153463b32bdefd60c801aaChris Lattner      unsigned AddrSpace = strtoul(Str, &End, 10);
693333daae66462e8f51ee153463b32bdefd60c801aaChris Lattner      if (End != Str && AddrSpace != 0) {
693433daae66462e8f51ee153463b32bdefd60c801aaChris Lattner        Type = Context.getAddrSpaceQualType(Type, AddrSpace);
693533daae66462e8f51ee153463b32bdefd60c801aaChris Lattner        Str = End;
693633daae66462e8f51ee153463b32bdefd60c801aaChris Lattner      }
693733daae66462e8f51ee153463b32bdefd60c801aaChris Lattner      if (c == '*')
693833daae66462e8f51ee153463b32bdefd60c801aaChris Lattner        Type = Context.getPointerType(Type);
693933daae66462e8f51ee153463b32bdefd60c801aaChris Lattner      else
694033daae66462e8f51ee153463b32bdefd60c801aaChris Lattner        Type = Context.getLValueReferenceType(Type);
694133daae66462e8f51ee153463b32bdefd60c801aaChris Lattner      break;
694233daae66462e8f51ee153463b32bdefd60c801aaChris Lattner    }
694333daae66462e8f51ee153463b32bdefd60c801aaChris Lattner    // FIXME: There's no way to have a built-in with an rvalue ref arg.
694433daae66462e8f51ee153463b32bdefd60c801aaChris Lattner    case 'C':
694533daae66462e8f51ee153463b32bdefd60c801aaChris Lattner      Type = Type.withConst();
694633daae66462e8f51ee153463b32bdefd60c801aaChris Lattner      break;
694733daae66462e8f51ee153463b32bdefd60c801aaChris Lattner    case 'D':
694833daae66462e8f51ee153463b32bdefd60c801aaChris Lattner      Type = Context.getVolatileType(Type);
694933daae66462e8f51ee153463b32bdefd60c801aaChris Lattner      break;
695018932a0f2a94a7813ec461d1118c39ecf8aa936fTed Kremenek    case 'R':
695118932a0f2a94a7813ec461d1118c39ecf8aa936fTed Kremenek      Type = Type.withRestrict();
695218932a0f2a94a7813ec461d1118c39ecf8aa936fTed Kremenek      break;
695386df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    }
695486df27bbdbb98c39ec2184695c0561209f91beddChris Lattner  }
6955393bd8e185692a451b2ba16bdfc9e7d3543b4217Chris Lattner
695614e0e7436cf6650a72052baea1f8ebe644cef489Chris Lattner  assert((!RequiresICE || Type->isIntegralOrEnumerationType()) &&
6957393bd8e185692a451b2ba16bdfc9e7d3543b4217Chris Lattner         "Integer constant 'I' type must be an integer");
69581eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
695986df27bbdbb98c39ec2184695c0561209f91beddChris Lattner  return Type;
696086df27bbdbb98c39ec2184695c0561209f91beddChris Lattner}
696186df27bbdbb98c39ec2184695c0561209f91beddChris Lattner
696286df27bbdbb98c39ec2184695c0561209f91beddChris Lattner/// GetBuiltinType - Return the type for the specified builtin.
696333daae66462e8f51ee153463b32bdefd60c801aaChris LattnerQualType ASTContext::GetBuiltinType(unsigned Id,
696414e0e7436cf6650a72052baea1f8ebe644cef489Chris Lattner                                    GetBuiltinTypeError &Error,
69654ba2a17694148e16eaa8d3917f657ffcd3667be4Jay Foad                                    unsigned *IntegerConstantArgs) const {
696633daae66462e8f51ee153463b32bdefd60c801aaChris Lattner  const char *TypeStr = BuiltinInfo.GetTypeString(Id);
69671eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
69685f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner  SmallVector<QualType, 8> ArgTypes;
69691eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
697014e0e7436cf6650a72052baea1f8ebe644cef489Chris Lattner  bool RequiresICE = false;
697186df27bbdbb98c39ec2184695c0561209f91beddChris Lattner  Error = GE_None;
697214e0e7436cf6650a72052baea1f8ebe644cef489Chris Lattner  QualType ResType = DecodeTypeFromStr(TypeStr, *this, Error,
697314e0e7436cf6650a72052baea1f8ebe644cef489Chris Lattner                                       RequiresICE, true);
697486df27bbdbb98c39ec2184695c0561209f91beddChris Lattner  if (Error != GE_None)
697586df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    return QualType();
697614e0e7436cf6650a72052baea1f8ebe644cef489Chris Lattner
697714e0e7436cf6650a72052baea1f8ebe644cef489Chris Lattner  assert(!RequiresICE && "Result of intrinsic cannot be required to be an ICE");
697814e0e7436cf6650a72052baea1f8ebe644cef489Chris Lattner
697986df27bbdbb98c39ec2184695c0561209f91beddChris Lattner  while (TypeStr[0] && TypeStr[0] != '.') {
698014e0e7436cf6650a72052baea1f8ebe644cef489Chris Lattner    QualType Ty = DecodeTypeFromStr(TypeStr, *this, Error, RequiresICE, true);
698186df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    if (Error != GE_None)
698286df27bbdbb98c39ec2184695c0561209f91beddChris Lattner      return QualType();
698386df27bbdbb98c39ec2184695c0561209f91beddChris Lattner
698414e0e7436cf6650a72052baea1f8ebe644cef489Chris Lattner    // If this argument is required to be an IntegerConstantExpression and the
698514e0e7436cf6650a72052baea1f8ebe644cef489Chris Lattner    // caller cares, fill in the bitmask we return.
698614e0e7436cf6650a72052baea1f8ebe644cef489Chris Lattner    if (RequiresICE && IntegerConstantArgs)
698714e0e7436cf6650a72052baea1f8ebe644cef489Chris Lattner      *IntegerConstantArgs |= 1 << ArgTypes.size();
698814e0e7436cf6650a72052baea1f8ebe644cef489Chris Lattner
698986df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    // Do array -> pointer decay.  The builtin should use the decayed type.
699086df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    if (Ty->isArrayType())
699186df27bbdbb98c39ec2184695c0561209f91beddChris Lattner      Ty = getArrayDecayedType(Ty);
69921eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
699386df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    ArgTypes.push_back(Ty);
699486df27bbdbb98c39ec2184695c0561209f91beddChris Lattner  }
699586df27bbdbb98c39ec2184695c0561209f91beddChris Lattner
699686df27bbdbb98c39ec2184695c0561209f91beddChris Lattner  assert((TypeStr[0] != '.' || TypeStr[1] == 0) &&
699786df27bbdbb98c39ec2184695c0561209f91beddChris Lattner         "'.' should only occur at end of builtin type list!");
699886df27bbdbb98c39ec2184695c0561209f91beddChris Lattner
699900ccbefcffeb88ea3e2e6323e594fa968753ad14John McCall  FunctionType::ExtInfo EI;
700000ccbefcffeb88ea3e2e6323e594fa968753ad14John McCall  if (BuiltinInfo.isNoReturn(Id)) EI = EI.withNoReturn(true);
700100ccbefcffeb88ea3e2e6323e594fa968753ad14John McCall
700200ccbefcffeb88ea3e2e6323e594fa968753ad14John McCall  bool Variadic = (TypeStr[0] == '.');
700300ccbefcffeb88ea3e2e6323e594fa968753ad14John McCall
700400ccbefcffeb88ea3e2e6323e594fa968753ad14John McCall  // We really shouldn't be making a no-proto type here, especially in C++.
700500ccbefcffeb88ea3e2e6323e594fa968753ad14John McCall  if (ArgTypes.empty() && Variadic)
700600ccbefcffeb88ea3e2e6323e594fa968753ad14John McCall    return getFunctionNoProtoType(ResType, EI);
7007ce056bcaa1c97b89a4b2de2112c62d060863be2bDouglas Gregor
7008e23cf437fe76b1ed02d63c3f61b456fd48a915f5John McCall  FunctionProtoType::ExtProtoInfo EPI;
700900ccbefcffeb88ea3e2e6323e594fa968753ad14John McCall  EPI.ExtInfo = EI;
701000ccbefcffeb88ea3e2e6323e594fa968753ad14John McCall  EPI.Variadic = Variadic;
7011e23cf437fe76b1ed02d63c3f61b456fd48a915f5John McCall
7012e23cf437fe76b1ed02d63c3f61b456fd48a915f5John McCall  return getFunctionType(ResType, ArgTypes.data(), ArgTypes.size(), EPI);
701386df27bbdbb98c39ec2184695c0561209f91beddChris Lattner}
7014a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman
701590e99a84ddd020e8fda79643748243725a2ed071Argyrios KyrtzidisGVALinkage ASTContext::GetGVALinkageForFunction(const FunctionDecl *FD) {
701690e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis  GVALinkage External = GVA_StrongExternal;
701790e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis
701890e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis  Linkage L = FD->getLinkage();
701990e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis  switch (L) {
702090e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis  case NoLinkage:
702190e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis  case InternalLinkage:
702290e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis  case UniqueExternalLinkage:
702390e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis    return GVA_Internal;
702490e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis
702590e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis  case ExternalLinkage:
702690e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis    switch (FD->getTemplateSpecializationKind()) {
702790e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis    case TSK_Undeclared:
702890e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis    case TSK_ExplicitSpecialization:
702990e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis      External = GVA_StrongExternal;
703090e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis      break;
703190e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis
703290e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis    case TSK_ExplicitInstantiationDefinition:
703390e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis      return GVA_ExplicitTemplateInstantiation;
703490e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis
703590e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis    case TSK_ExplicitInstantiationDeclaration:
703690e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis    case TSK_ImplicitInstantiation:
703790e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis      External = GVA_TemplateInstantiation;
703890e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis      break;
703990e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis    }
704090e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis  }
704190e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis
704290e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis  if (!FD->isInlined())
704390e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis    return External;
704490e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis
70454e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie  if (!getLangOpts().CPlusPlus || FD->hasAttr<GNUInlineAttr>()) {
704690e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis    // GNU or C99 inline semantics. Determine whether this symbol should be
704790e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis    // externally visible.
704890e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis    if (FD->isInlineDefinitionExternallyVisible())
704990e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis      return External;
705090e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis
705190e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis    // C99 inline semantics, where the symbol is not externally visible.
705290e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis    return GVA_C99Inline;
705390e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis  }
705490e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis
705590e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis  // C++0x [temp.explicit]p9:
705690e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis  //   [ Note: The intent is that an inline function that is the subject of
705790e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis  //   an explicit instantiation declaration will still be implicitly
705890e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis  //   instantiated when used so that the body can be considered for
705990e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis  //   inlining, but that no out-of-line copy of the inline function would be
706090e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis  //   generated in the translation unit. -- end note ]
706190e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis  if (FD->getTemplateSpecializationKind()
706290e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis                                       == TSK_ExplicitInstantiationDeclaration)
706390e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis    return GVA_C99Inline;
706490e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis
706590e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis  return GVA_CXXInline;
706690e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis}
706790e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis
706890e99a84ddd020e8fda79643748243725a2ed071Argyrios KyrtzidisGVALinkage ASTContext::GetGVALinkageForVariable(const VarDecl *VD) {
706990e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis  // If this is a static data member, compute the kind of template
707090e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis  // specialization. Otherwise, this variable is not part of a
707190e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis  // template.
707290e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis  TemplateSpecializationKind TSK = TSK_Undeclared;
707390e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis  if (VD->isStaticDataMember())
707490e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis    TSK = VD->getTemplateSpecializationKind();
707590e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis
707690e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis  Linkage L = VD->getLinkage();
70774e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie  if (L == ExternalLinkage && getLangOpts().CPlusPlus &&
707890e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis      VD->getType()->getLinkage() == UniqueExternalLinkage)
707990e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis    L = UniqueExternalLinkage;
708090e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis
708190e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis  switch (L) {
708290e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis  case NoLinkage:
708390e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis  case InternalLinkage:
708490e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis  case UniqueExternalLinkage:
708590e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis    return GVA_Internal;
708690e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis
708790e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis  case ExternalLinkage:
708890e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis    switch (TSK) {
708990e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis    case TSK_Undeclared:
709090e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis    case TSK_ExplicitSpecialization:
709190e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis      return GVA_StrongExternal;
709290e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis
709390e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis    case TSK_ExplicitInstantiationDeclaration:
709490e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis      llvm_unreachable("Variable should not be instantiated");
709590e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis      // Fall through to treat this like any other instantiation.
709690e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis
709790e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis    case TSK_ExplicitInstantiationDefinition:
709890e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis      return GVA_ExplicitTemplateInstantiation;
709990e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis
710090e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis    case TSK_ImplicitInstantiation:
710190e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis      return GVA_TemplateInstantiation;
710290e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis    }
710390e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis  }
710490e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis
71057530c034c0c71a64c5a9173206d9742ae847af8bDavid Blaikie  llvm_unreachable("Invalid Linkage!");
710690e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis}
710790e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis
71084ac7c0bb39696e92fd220118fedc484c09a69870Argyrios Kyrtzidisbool ASTContext::DeclMustBeEmitted(const Decl *D) {
710990e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis  if (const VarDecl *VD = dyn_cast<VarDecl>(D)) {
711090e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis    if (!VD->isFileVarDecl())
711190e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis      return false;
7112144b38a7995cbe0928e34fbcc865bb2d2be4f7a3Argyrios Kyrtzidis  } else if (!isa<FunctionDecl>(D))
711390e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis    return false;
711490e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis
7115ab411c8c2efed8f2403bf8596e780c0f2f905a19Argyrios Kyrtzidis  // Weak references don't produce any output by themselves.
7116ab411c8c2efed8f2403bf8596e780c0f2f905a19Argyrios Kyrtzidis  if (D->hasAttr<WeakRefAttr>())
7117ab411c8c2efed8f2403bf8596e780c0f2f905a19Argyrios Kyrtzidis    return false;
7118ab411c8c2efed8f2403bf8596e780c0f2f905a19Argyrios Kyrtzidis
711990e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis  // Aliases and used decls are required.
712090e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis  if (D->hasAttr<AliasAttr>() || D->hasAttr<UsedAttr>())
712190e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis    return true;
712290e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis
712390e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis  if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
712490e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis    // Forward declarations aren't required.
712510620eb5164e31208fcbf0437cd79ae535ed0559Sean Hunt    if (!FD->doesThisDeclarationHaveABody())
7126dce67a70a86db8758c926a76fdd980f5369d5746Nick Lewycky      return FD->doesDeclarationForceExternallyVisibleDefinition();
712790e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis
712890e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis    // Constructors and destructors are required.
712990e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis    if (FD->hasAttr<ConstructorAttr>() || FD->hasAttr<DestructorAttr>())
713090e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis      return true;
713190e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis
713290e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis    // The key function for a class is required.
713390e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis    if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(FD)) {
713490e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis      const CXXRecordDecl *RD = MD->getParent();
713590e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis      if (MD->isOutOfLine() && RD->isDynamicClass()) {
713690e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis        const CXXMethodDecl *KeyFunc = getKeyFunction(RD);
713790e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis        if (KeyFunc && KeyFunc->getCanonicalDecl() == MD->getCanonicalDecl())
713890e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis          return true;
713990e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis      }
714090e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis    }
714190e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis
714290e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis    GVALinkage Linkage = GetGVALinkageForFunction(FD);
714390e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis
714490e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis    // static, static inline, always_inline, and extern inline functions can
714590e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis    // always be deferred.  Normal inline functions can be deferred in C99/C++.
714690e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis    // Implicit template instantiations can also be deferred in C++.
714790e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis    if (Linkage == GVA_Internal  || Linkage == GVA_C99Inline ||
71483a5aca8112e03814f78906dc5d032287adb272b5Anton Yartsev        Linkage == GVA_CXXInline || Linkage == GVA_TemplateInstantiation)
714990e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis      return false;
715090e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis    return true;
715190e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis  }
715294da1587f7d584fc61df793229d197969f204cd9Douglas Gregor
715390e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis  const VarDecl *VD = cast<VarDecl>(D);
715490e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis  assert(VD->isFileVarDecl() && "Expected file scoped var");
715590e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis
7156ab411c8c2efed8f2403bf8596e780c0f2f905a19Argyrios Kyrtzidis  if (VD->isThisDeclarationADefinition() == VarDecl::DeclarationOnly)
7157ab411c8c2efed8f2403bf8596e780c0f2f905a19Argyrios Kyrtzidis    return false;
7158ab411c8c2efed8f2403bf8596e780c0f2f905a19Argyrios Kyrtzidis
715990e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis  // Structs that have non-trivial constructors or destructors are required.
716090e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis
716190e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis  // FIXME: Handle references.
7162023df37c27ee8035664fb62f206ca58f4e2a169dSean Hunt  // FIXME: Be more selective about which constructors we care about.
716390e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis  if (const RecordType *RT = VD->getType()->getAs<RecordType>()) {
716490e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis    if (const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(RT->getDecl())) {
7165023df37c27ee8035664fb62f206ca58f4e2a169dSean Hunt      if (RD->hasDefinition() && !(RD->hasTrivialDefaultConstructor() &&
7166023df37c27ee8035664fb62f206ca58f4e2a169dSean Hunt                                   RD->hasTrivialCopyConstructor() &&
7167023df37c27ee8035664fb62f206ca58f4e2a169dSean Hunt                                   RD->hasTrivialMoveConstructor() &&
7168023df37c27ee8035664fb62f206ca58f4e2a169dSean Hunt                                   RD->hasTrivialDestructor()))
716990e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis        return true;
717090e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis    }
717190e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis  }
717290e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis
717390e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis  GVALinkage L = GetGVALinkageForVariable(VD);
717490e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis  if (L == GVA_Internal || L == GVA_TemplateInstantiation) {
717590e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis    if (!(VD->getInit() && VD->getInit()->HasSideEffects(*this)))
717690e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis      return false;
717790e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis  }
717890e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis
717990e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis  return true;
718090e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis}
7181071cc7deffad608165b1ddd5263e8bf181861520Charles Davis
71828f88a1dcc57cfe8580eb1558a783ad8499bfe8e0Timur IskhodzhanovCallingConv ASTContext::getDefaultCXXMethodCallConv(bool isVariadic) {
7183ee743f903858e337434ac0335f147f4de4ecae05Charles Davis  // Pass through to the C++ ABI object
71848f88a1dcc57cfe8580eb1558a783ad8499bfe8e0Timur Iskhodzhanov  return ABI->getDefaultMethodCallConv(isVariadic);
71858f88a1dcc57cfe8580eb1558a783ad8499bfe8e0Timur Iskhodzhanov}
71868f88a1dcc57cfe8580eb1558a783ad8499bfe8e0Timur Iskhodzhanov
71878f88a1dcc57cfe8580eb1558a783ad8499bfe8e0Timur IskhodzhanovCallingConv ASTContext::getCanonicalCallConv(CallingConv CC) const {
71888f88a1dcc57cfe8580eb1558a783ad8499bfe8e0Timur Iskhodzhanov  if (CC == CC_C && !LangOpts.MRTD && getTargetInfo().getCXXABI() != CXXABI_Microsoft)
71898f88a1dcc57cfe8580eb1558a783ad8499bfe8e0Timur Iskhodzhanov    return CC_Default;
71908f88a1dcc57cfe8580eb1558a783ad8499bfe8e0Timur Iskhodzhanov  return CC;
7191ee743f903858e337434ac0335f147f4de4ecae05Charles Davis}
7192ee743f903858e337434ac0335f147f4de4ecae05Charles Davis
71934ba2a17694148e16eaa8d3917f657ffcd3667be4Jay Foadbool ASTContext::isNearlyEmpty(const CXXRecordDecl *RD) const {
7194dae0cb52e4e3d46bbfc9a4510909522197a92e54Anders Carlsson  // Pass through to the C++ ABI object
7195dae0cb52e4e3d46bbfc9a4510909522197a92e54Anders Carlsson  return ABI->isNearlyEmpty(RD);
7196dae0cb52e4e3d46bbfc9a4510909522197a92e54Anders Carlsson}
7197dae0cb52e4e3d46bbfc9a4510909522197a92e54Anders Carlsson
719814110477887e3dc168ffc6c191e72d705051f99ePeter CollingbourneMangleContext *ASTContext::createMangleContext() {
7199bcfd1f55bfbb3e5944cd5e03d07b343e280838c4Douglas Gregor  switch (Target->getCXXABI()) {
720014110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne  case CXXABI_ARM:
720114110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne  case CXXABI_Itanium:
720214110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne    return createItaniumMangleContext(*this, getDiagnostics());
720314110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne  case CXXABI_Microsoft:
720414110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne    return createMicrosoftMangleContext(*this, getDiagnostics());
720514110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne  }
7206b219cfc4d75f0a03630b7c4509ef791b7e97b2c8David Blaikie  llvm_unreachable("Unsupported ABI");
720714110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne}
720814110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne
7209071cc7deffad608165b1ddd5263e8bf181861520Charles DavisCXXABI::~CXXABI() {}
7210ba29bd25515fbd99e98ba0fedb9d93617b27609eTed Kremenek
7211ba29bd25515fbd99e98ba0fedb9d93617b27609eTed Kremeneksize_t ASTContext::getSideTableAllocatedMemory() const {
72120c8cd1ac55720276ad41fa7b4f8785bead5afd32Ted Kremenek  return ASTRecordLayouts.getMemorySize()
72130c8cd1ac55720276ad41fa7b4f8785bead5afd32Ted Kremenek    + llvm::capacity_in_bytes(ObjCLayouts)
72140c8cd1ac55720276ad41fa7b4f8785bead5afd32Ted Kremenek    + llvm::capacity_in_bytes(KeyFunctions)
72150c8cd1ac55720276ad41fa7b4f8785bead5afd32Ted Kremenek    + llvm::capacity_in_bytes(ObjCImpls)
72160c8cd1ac55720276ad41fa7b4f8785bead5afd32Ted Kremenek    + llvm::capacity_in_bytes(BlockVarCopyInits)
72170c8cd1ac55720276ad41fa7b4f8785bead5afd32Ted Kremenek    + llvm::capacity_in_bytes(DeclAttrs)
72180c8cd1ac55720276ad41fa7b4f8785bead5afd32Ted Kremenek    + llvm::capacity_in_bytes(InstantiatedFromStaticDataMember)
72190c8cd1ac55720276ad41fa7b4f8785bead5afd32Ted Kremenek    + llvm::capacity_in_bytes(InstantiatedFromUsingDecl)
72200c8cd1ac55720276ad41fa7b4f8785bead5afd32Ted Kremenek    + llvm::capacity_in_bytes(InstantiatedFromUsingShadowDecl)
72210c8cd1ac55720276ad41fa7b4f8785bead5afd32Ted Kremenek    + llvm::capacity_in_bytes(InstantiatedFromUnnamedFieldDecl)
72220c8cd1ac55720276ad41fa7b4f8785bead5afd32Ted Kremenek    + llvm::capacity_in_bytes(OverriddenMethods)
72230c8cd1ac55720276ad41fa7b4f8785bead5afd32Ted Kremenek    + llvm::capacity_in_bytes(Types)
7224af0f4d0b2e38c810effc8b024ad2fb6604eec5d3Francois Pichet    + llvm::capacity_in_bytes(VariableArrayTypes)
72250d95f0d7b81110f77e99e833f766d19be7b7e072Francois Pichet    + llvm::capacity_in_bytes(ClassScopeSpecializationPattern);
7226ba29bd25515fbd99e98ba0fedb9d93617b27609eTed Kremenek}
7227d211cb709510fbe7e75167b9feee0050851d001aTed Kremenek
72289e8c92a9c9b949bbb0408fbbd9a58e34894b6efcDouglas Gregorunsigned ASTContext::getLambdaManglingNumber(CXXMethodDecl *CallOperator) {
72299e8c92a9c9b949bbb0408fbbd9a58e34894b6efcDouglas Gregor  CXXRecordDecl *Lambda = CallOperator->getParent();
72309e8c92a9c9b949bbb0408fbbd9a58e34894b6efcDouglas Gregor  return LambdaMangleContexts[Lambda->getDeclContext()]
72319e8c92a9c9b949bbb0408fbbd9a58e34894b6efcDouglas Gregor           .getManglingNumber(CallOperator);
72329e8c92a9c9b949bbb0408fbbd9a58e34894b6efcDouglas Gregor}
72339e8c92a9c9b949bbb0408fbbd9a58e34894b6efcDouglas Gregor
72349e8c92a9c9b949bbb0408fbbd9a58e34894b6efcDouglas Gregor
7235d211cb709510fbe7e75167b9feee0050851d001aTed Kremenekvoid ASTContext::setParameterIndex(const ParmVarDecl *D, unsigned int index) {
7236d211cb709510fbe7e75167b9feee0050851d001aTed Kremenek  ParamIndices[D] = index;
7237d211cb709510fbe7e75167b9feee0050851d001aTed Kremenek}
7238d211cb709510fbe7e75167b9feee0050851d001aTed Kremenek
7239d211cb709510fbe7e75167b9feee0050851d001aTed Kremenekunsigned ASTContext::getParameterIndex(const ParmVarDecl *D) const {
7240d211cb709510fbe7e75167b9feee0050851d001aTed Kremenek  ParameterIndexTable::const_iterator I = ParamIndices.find(D);
7241d211cb709510fbe7e75167b9feee0050851d001aTed Kremenek  assert(I != ParamIndices.end() &&
7242d211cb709510fbe7e75167b9feee0050851d001aTed Kremenek         "ParmIndices lacks entry set by ParmVarDecl");
7243d211cb709510fbe7e75167b9feee0050851d001aTed Kremenek  return I->second;
7244d211cb709510fbe7e75167b9feee0050851d001aTed Kremenek}
7245