ASTContext.cpp revision 0a74a4ccc4ecfe1a2792ab72c83815323d8fc914
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"
1555fc873017f10f6f566b182b70f6fc22aefa3464Chandler Carruth#include "CXXABI.h"
162fa67efeaf66a9332c30a026dc1c21bef6c33a6cBenjamin Kramer#include "clang/AST/ASTMutationListener.h"
172fa67efeaf66a9332c30a026dc1c21bef6c33a6cBenjamin Kramer#include "clang/AST/Attr.h"
18bdc601b196c48d4cd56a5ceb45d41ae4e87371abKen Dyck#include "clang/AST/CharUnits.h"
192fa67efeaf66a9332c30a026dc1c21bef6c33a6cBenjamin Kramer#include "clang/AST/Comment.h"
20aa58081902ad31927df02e8537d972eabe29d6dfDmitri Gribenko#include "clang/AST/CommentCommandTraits.h"
2149aa7ff1245abd03e6e998e01302df31e4c6f8f6Argyrios Kyrtzidis#include "clang/AST/DeclCXX.h"
22980e508ca70d6de75d2abfd96b4681fc98bb2698Steve Naroff#include "clang/AST/DeclObjC.h"
23aaba5e346dffdbad5d1c42765a89e4a7afb0da67Douglas Gregor#include "clang/AST/DeclTemplate.h"
24e91593ef084479340582b2ba177b44be50a717b7Daniel Dunbar#include "clang/AST/Expr.h"
25ea1471e0e967548c596a71469702f8846dbaf3c0John McCall#include "clang/AST/ExprCXX.h"
262cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor#include "clang/AST/ExternalASTSource.h"
2714110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne#include "clang/AST/Mangle.h"
282fa67efeaf66a9332c30a026dc1c21bef6c33a6cBenjamin Kramer#include "clang/AST/RecordLayout.h"
292fa67efeaf66a9332c30a026dc1c21bef6c33a6cBenjamin Kramer#include "clang/AST/TypeLoc.h"
301b63e4f732dbc73d90abf886b4d21f8e3a165f6dChris Lattner#include "clang/Basic/Builtins.h"
31a9376d470ccb0eac74fe09a6b2a18a890f1d17c4Chris Lattner#include "clang/Basic/SourceManager.h"
325f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer#include "clang/Basic/TargetInfo.h"
33f5942a44880be26878592eb052b737579349411eBenjamin Kramer#include "llvm/ADT/SmallString.h"
3485f9bceab1542aafff012d4d28e998f4ba16e362Anders Carlsson#include "llvm/ADT/StringExtras.h"
352fa67efeaf66a9332c30a026dc1c21bef6c33a6cBenjamin Kramer#include "llvm/Support/Capacity.h"
366fe7c8aa8c7546743ecd0ac0138c2cf5d8155386Nate Begeman#include "llvm/Support/MathExtras.h"
37f5942a44880be26878592eb052b737579349411eBenjamin Kramer#include "llvm/Support/raw_ostream.h"
382636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis#include <map>
3929445a0542d128cd7ee587ee52229670b9b54a12Anders Carlsson
405f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencerusing namespace clang;
415f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
421827403a7138946305c0058f262e02b595cf882fDouglas Gregorunsigned ASTContext::NumImplicitDefaultConstructors;
431827403a7138946305c0058f262e02b595cf882fDouglas Gregorunsigned ASTContext::NumImplicitDefaultConstructorsDeclared;
44225843186e3972ce798d3de00f86da9008b15a0eDouglas Gregorunsigned ASTContext::NumImplicitCopyConstructors;
45225843186e3972ce798d3de00f86da9008b15a0eDouglas Gregorunsigned ASTContext::NumImplicitCopyConstructorsDeclared;
46ffe37fdda5b4b4f162a45155c30d9f60ce110c12Sean Huntunsigned ASTContext::NumImplicitMoveConstructors;
47ffe37fdda5b4b4f162a45155c30d9f60ce110c12Sean Huntunsigned ASTContext::NumImplicitMoveConstructorsDeclared;
48a376d10acfacf19d6dfa41069f7929739a18dd7aDouglas Gregorunsigned ASTContext::NumImplicitCopyAssignmentOperators;
49a376d10acfacf19d6dfa41069f7929739a18dd7aDouglas Gregorunsigned ASTContext::NumImplicitCopyAssignmentOperatorsDeclared;
50ffe37fdda5b4b4f162a45155c30d9f60ce110c12Sean Huntunsigned ASTContext::NumImplicitMoveAssignmentOperators;
51ffe37fdda5b4b4f162a45155c30d9f60ce110c12Sean Huntunsigned ASTContext::NumImplicitMoveAssignmentOperatorsDeclared;
524923aa25eb39d64106a5817c02d560a3aecf8b2cDouglas Gregorunsigned ASTContext::NumImplicitDestructors;
534923aa25eb39d64106a5817c02d560a3aecf8b2cDouglas Gregorunsigned ASTContext::NumImplicitDestructorsDeclared;
544923aa25eb39d64106a5817c02d560a3aecf8b2cDouglas Gregor
555f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencerenum FloatingRank {
56aa4a99b4a62615db243f7a5c433169f2fc704420Anton Korobeynikov  HalfRank, FloatRank, DoubleRank, LongDoubleRank
575f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer};
585f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
59a5ef44ff5d93a3be6ca67782828157a71894cf0cDmitri GribenkoRawComment *ASTContext::getRawCommentForDeclNoCache(const Decl *D) const {
60aa0cd85838f2a024e589ea4e8c2094130065af21Dmitri Gribenko  if (!CommentsLoaded && ExternalSource) {
61aa0cd85838f2a024e589ea4e8c2094130065af21Dmitri Gribenko    ExternalSource->ReadComments();
62aa0cd85838f2a024e589ea4e8c2094130065af21Dmitri Gribenko    CommentsLoaded = true;
63aa0cd85838f2a024e589ea4e8c2094130065af21Dmitri Gribenko  }
64aa0cd85838f2a024e589ea4e8c2094130065af21Dmitri Gribenko
65aa0cd85838f2a024e589ea4e8c2094130065af21Dmitri Gribenko  assert(D);
66aa0cd85838f2a024e589ea4e8c2094130065af21Dmitri Gribenko
67c3fee3539fd00a6ce21dc1f574baf76686640072Dmitri Gribenko  // User can not attach documentation to implicit declarations.
68c3fee3539fd00a6ce21dc1f574baf76686640072Dmitri Gribenko  if (D->isImplicit())
69c3fee3539fd00a6ce21dc1f574baf76686640072Dmitri Gribenko    return NULL;
70c3fee3539fd00a6ce21dc1f574baf76686640072Dmitri Gribenko
71c41ace950dcf2254c9aa48e73647b89c35109f80Dmitri Gribenko  // User can not attach documentation to implicit instantiations.
72c41ace950dcf2254c9aa48e73647b89c35109f80Dmitri Gribenko  if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
73c41ace950dcf2254c9aa48e73647b89c35109f80Dmitri Gribenko    if (FD->getTemplateSpecializationKind() == TSK_ImplicitInstantiation)
74c41ace950dcf2254c9aa48e73647b89c35109f80Dmitri Gribenko      return NULL;
75c41ace950dcf2254c9aa48e73647b89c35109f80Dmitri Gribenko  }
76c41ace950dcf2254c9aa48e73647b89c35109f80Dmitri Gribenko
77dce750b15eb5eb797ac9bbea118333d7d1896831Dmitri Gribenko  if (const VarDecl *VD = dyn_cast<VarDecl>(D)) {
78dce750b15eb5eb797ac9bbea118333d7d1896831Dmitri Gribenko    if (VD->isStaticDataMember() &&
79dce750b15eb5eb797ac9bbea118333d7d1896831Dmitri Gribenko        VD->getTemplateSpecializationKind() == TSK_ImplicitInstantiation)
80dce750b15eb5eb797ac9bbea118333d7d1896831Dmitri Gribenko      return NULL;
81dce750b15eb5eb797ac9bbea118333d7d1896831Dmitri Gribenko  }
82dce750b15eb5eb797ac9bbea118333d7d1896831Dmitri Gribenko
83dce750b15eb5eb797ac9bbea118333d7d1896831Dmitri Gribenko  if (const CXXRecordDecl *CRD = dyn_cast<CXXRecordDecl>(D)) {
84dce750b15eb5eb797ac9bbea118333d7d1896831Dmitri Gribenko    if (CRD->getTemplateSpecializationKind() == TSK_ImplicitInstantiation)
85dce750b15eb5eb797ac9bbea118333d7d1896831Dmitri Gribenko      return NULL;
86dce750b15eb5eb797ac9bbea118333d7d1896831Dmitri Gribenko  }
87dce750b15eb5eb797ac9bbea118333d7d1896831Dmitri Gribenko
88d1e5c0df2acb01192eedda14534fe990e1e26c46Dmitri Gribenko  if (const ClassTemplateSpecializationDecl *CTSD =
89d1e5c0df2acb01192eedda14534fe990e1e26c46Dmitri Gribenko          dyn_cast<ClassTemplateSpecializationDecl>(D)) {
90d1e5c0df2acb01192eedda14534fe990e1e26c46Dmitri Gribenko    TemplateSpecializationKind TSK = CTSD->getSpecializationKind();
91d1e5c0df2acb01192eedda14534fe990e1e26c46Dmitri Gribenko    if (TSK == TSK_ImplicitInstantiation ||
92d1e5c0df2acb01192eedda14534fe990e1e26c46Dmitri Gribenko        TSK == TSK_Undeclared)
93d1e5c0df2acb01192eedda14534fe990e1e26c46Dmitri Gribenko      return NULL;
94d1e5c0df2acb01192eedda14534fe990e1e26c46Dmitri Gribenko  }
95d1e5c0df2acb01192eedda14534fe990e1e26c46Dmitri Gribenko
96dce750b15eb5eb797ac9bbea118333d7d1896831Dmitri Gribenko  if (const EnumDecl *ED = dyn_cast<EnumDecl>(D)) {
97dce750b15eb5eb797ac9bbea118333d7d1896831Dmitri Gribenko    if (ED->getTemplateSpecializationKind() == TSK_ImplicitInstantiation)
98dce750b15eb5eb797ac9bbea118333d7d1896831Dmitri Gribenko      return NULL;
99dce750b15eb5eb797ac9bbea118333d7d1896831Dmitri Gribenko  }
100099ecfb0ed137665a3394187030d8fd7183fd9d4Fariborz Jahanian  if (const TagDecl *TD = dyn_cast<TagDecl>(D)) {
101099ecfb0ed137665a3394187030d8fd7183fd9d4Fariborz Jahanian    // When tag declaration (but not definition!) is part of the
102099ecfb0ed137665a3394187030d8fd7183fd9d4Fariborz Jahanian    // decl-specifier-seq of some other declaration, it doesn't get comment
103099ecfb0ed137665a3394187030d8fd7183fd9d4Fariborz Jahanian    if (TD->isEmbeddedInDeclarator() && !TD->isCompleteDefinition())
104099ecfb0ed137665a3394187030d8fd7183fd9d4Fariborz Jahanian      return NULL;
105099ecfb0ed137665a3394187030d8fd7183fd9d4Fariborz Jahanian  }
106aa0cd85838f2a024e589ea4e8c2094130065af21Dmitri Gribenko  // TODO: handle comments for function parameters properly.
107aa0cd85838f2a024e589ea4e8c2094130065af21Dmitri Gribenko  if (isa<ParmVarDecl>(D))
108aa0cd85838f2a024e589ea4e8c2094130065af21Dmitri Gribenko    return NULL;
109aa0cd85838f2a024e589ea4e8c2094130065af21Dmitri Gribenko
11096b098674908eaa59a9128f3305cda6fbbdad563Dmitri Gribenko  // TODO: we could look up template parameter documentation in the template
11196b098674908eaa59a9128f3305cda6fbbdad563Dmitri Gribenko  // documentation.
11296b098674908eaa59a9128f3305cda6fbbdad563Dmitri Gribenko  if (isa<TemplateTypeParmDecl>(D) ||
11396b098674908eaa59a9128f3305cda6fbbdad563Dmitri Gribenko      isa<NonTypeTemplateParmDecl>(D) ||
11496b098674908eaa59a9128f3305cda6fbbdad563Dmitri Gribenko      isa<TemplateTemplateParmDecl>(D))
11596b098674908eaa59a9128f3305cda6fbbdad563Dmitri Gribenko    return NULL;
11696b098674908eaa59a9128f3305cda6fbbdad563Dmitri Gribenko
117811c820257746b1799b790b6adc7804f44154011Dmitri Gribenko  ArrayRef<RawComment *> RawComments = Comments.getComments();
118aa0cd85838f2a024e589ea4e8c2094130065af21Dmitri Gribenko
119aa0cd85838f2a024e589ea4e8c2094130065af21Dmitri Gribenko  // If there are no comments anywhere, we won't find anything.
120aa0cd85838f2a024e589ea4e8c2094130065af21Dmitri Gribenko  if (RawComments.empty())
121aa0cd85838f2a024e589ea4e8c2094130065af21Dmitri Gribenko    return NULL;
122aa0cd85838f2a024e589ea4e8c2094130065af21Dmitri Gribenko
123abd56c816e9164b17bb3e7154a511b0c9896ffdbDmitri Gribenko  // Find declaration location.
124abd56c816e9164b17bb3e7154a511b0c9896ffdbDmitri Gribenko  // For Objective-C declarations we generally don't expect to have multiple
125abd56c816e9164b17bb3e7154a511b0c9896ffdbDmitri Gribenko  // declarators, thus use declaration starting location as the "declaration
126abd56c816e9164b17bb3e7154a511b0c9896ffdbDmitri Gribenko  // location".
127abd56c816e9164b17bb3e7154a511b0c9896ffdbDmitri Gribenko  // For all other declarations multiple declarators are used quite frequently,
128abd56c816e9164b17bb3e7154a511b0c9896ffdbDmitri Gribenko  // so we use the location of the identifier as the "declaration location".
129abd56c816e9164b17bb3e7154a511b0c9896ffdbDmitri Gribenko  SourceLocation DeclLoc;
130abd56c816e9164b17bb3e7154a511b0c9896ffdbDmitri Gribenko  if (isa<ObjCMethodDecl>(D) || isa<ObjCContainerDecl>(D) ||
13196b098674908eaa59a9128f3305cda6fbbdad563Dmitri Gribenko      isa<ObjCPropertyDecl>(D) ||
132c27bc80a98b9558513b50956c930eedc9e461ae0Dmitri Gribenko      isa<RedeclarableTemplateDecl>(D) ||
133c27bc80a98b9558513b50956c930eedc9e461ae0Dmitri Gribenko      isa<ClassTemplateSpecializationDecl>(D))
134abd56c816e9164b17bb3e7154a511b0c9896ffdbDmitri Gribenko    DeclLoc = D->getLocStart();
135abd56c816e9164b17bb3e7154a511b0c9896ffdbDmitri Gribenko  else
136abd56c816e9164b17bb3e7154a511b0c9896ffdbDmitri Gribenko    DeclLoc = D->getLocation();
137abd56c816e9164b17bb3e7154a511b0c9896ffdbDmitri Gribenko
138aa0cd85838f2a024e589ea4e8c2094130065af21Dmitri Gribenko  // If the declaration doesn't map directly to a location in a file, we
139aa0cd85838f2a024e589ea4e8c2094130065af21Dmitri Gribenko  // can't find the comment.
140aa0cd85838f2a024e589ea4e8c2094130065af21Dmitri Gribenko  if (DeclLoc.isInvalid() || !DeclLoc.isFileID())
141aa0cd85838f2a024e589ea4e8c2094130065af21Dmitri Gribenko    return NULL;
142aa0cd85838f2a024e589ea4e8c2094130065af21Dmitri Gribenko
143aa0cd85838f2a024e589ea4e8c2094130065af21Dmitri Gribenko  // Find the comment that occurs just after this declaration.
144a444f1856459130bd3a1bb8995331c9e367db04fDmitri Gribenko  ArrayRef<RawComment *>::iterator Comment;
145a444f1856459130bd3a1bb8995331c9e367db04fDmitri Gribenko  {
146a444f1856459130bd3a1bb8995331c9e367db04fDmitri Gribenko    // When searching for comments during parsing, the comment we are looking
147a444f1856459130bd3a1bb8995331c9e367db04fDmitri Gribenko    // for is usually among the last two comments we parsed -- check them
148a444f1856459130bd3a1bb8995331c9e367db04fDmitri Gribenko    // first.
1496fd7d3067dd06584ef3940e88e31fea1a0e83588Dmitri Gribenko    RawComment CommentAtDeclLoc(
1506fd7d3067dd06584ef3940e88e31fea1a0e83588Dmitri Gribenko        SourceMgr, SourceRange(DeclLoc), false,
1516fd7d3067dd06584ef3940e88e31fea1a0e83588Dmitri Gribenko        LangOpts.CommentOpts.ParseAllComments);
152a444f1856459130bd3a1bb8995331c9e367db04fDmitri Gribenko    BeforeThanCompare<RawComment> Compare(SourceMgr);
153a444f1856459130bd3a1bb8995331c9e367db04fDmitri Gribenko    ArrayRef<RawComment *>::iterator MaybeBeforeDecl = RawComments.end() - 1;
154a444f1856459130bd3a1bb8995331c9e367db04fDmitri Gribenko    bool Found = Compare(*MaybeBeforeDecl, &CommentAtDeclLoc);
155a444f1856459130bd3a1bb8995331c9e367db04fDmitri Gribenko    if (!Found && RawComments.size() >= 2) {
156a444f1856459130bd3a1bb8995331c9e367db04fDmitri Gribenko      MaybeBeforeDecl--;
157a444f1856459130bd3a1bb8995331c9e367db04fDmitri Gribenko      Found = Compare(*MaybeBeforeDecl, &CommentAtDeclLoc);
158a444f1856459130bd3a1bb8995331c9e367db04fDmitri Gribenko    }
159a444f1856459130bd3a1bb8995331c9e367db04fDmitri Gribenko
160a444f1856459130bd3a1bb8995331c9e367db04fDmitri Gribenko    if (Found) {
161a444f1856459130bd3a1bb8995331c9e367db04fDmitri Gribenko      Comment = MaybeBeforeDecl + 1;
162a444f1856459130bd3a1bb8995331c9e367db04fDmitri Gribenko      assert(Comment == std::lower_bound(RawComments.begin(), RawComments.end(),
163a444f1856459130bd3a1bb8995331c9e367db04fDmitri Gribenko                                         &CommentAtDeclLoc, Compare));
164a444f1856459130bd3a1bb8995331c9e367db04fDmitri Gribenko    } else {
165a444f1856459130bd3a1bb8995331c9e367db04fDmitri Gribenko      // Slow path.
166a444f1856459130bd3a1bb8995331c9e367db04fDmitri Gribenko      Comment = std::lower_bound(RawComments.begin(), RawComments.end(),
167a444f1856459130bd3a1bb8995331c9e367db04fDmitri Gribenko                                 &CommentAtDeclLoc, Compare);
168a444f1856459130bd3a1bb8995331c9e367db04fDmitri Gribenko    }
169a444f1856459130bd3a1bb8995331c9e367db04fDmitri Gribenko  }
170aa0cd85838f2a024e589ea4e8c2094130065af21Dmitri Gribenko
171aa0cd85838f2a024e589ea4e8c2094130065af21Dmitri Gribenko  // Decompose the location for the declaration and find the beginning of the
172aa0cd85838f2a024e589ea4e8c2094130065af21Dmitri Gribenko  // file buffer.
173aa0cd85838f2a024e589ea4e8c2094130065af21Dmitri Gribenko  std::pair<FileID, unsigned> DeclLocDecomp = SourceMgr.getDecomposedLoc(DeclLoc);
174aa0cd85838f2a024e589ea4e8c2094130065af21Dmitri Gribenko
175aa0cd85838f2a024e589ea4e8c2094130065af21Dmitri Gribenko  // First check whether we have a trailing comment.
176aa0cd85838f2a024e589ea4e8c2094130065af21Dmitri Gribenko  if (Comment != RawComments.end() &&
177811c820257746b1799b790b6adc7804f44154011Dmitri Gribenko      (*Comment)->isDocumentation() && (*Comment)->isTrailingComment() &&
1789c00676f2393335dc60c61faf944d4f8f622fac6Dmitri Gribenko      (isa<FieldDecl>(D) || isa<EnumConstantDecl>(D) || isa<VarDecl>(D))) {
179aa0cd85838f2a024e589ea4e8c2094130065af21Dmitri Gribenko    std::pair<FileID, unsigned> CommentBeginDecomp
180811c820257746b1799b790b6adc7804f44154011Dmitri Gribenko      = SourceMgr.getDecomposedLoc((*Comment)->getSourceRange().getBegin());
181aa0cd85838f2a024e589ea4e8c2094130065af21Dmitri Gribenko    // Check that Doxygen trailing comment comes after the declaration, starts
182aa0cd85838f2a024e589ea4e8c2094130065af21Dmitri Gribenko    // on the same line and in the same file as the declaration.
183aa0cd85838f2a024e589ea4e8c2094130065af21Dmitri Gribenko    if (DeclLocDecomp.first == CommentBeginDecomp.first &&
184aa0cd85838f2a024e589ea4e8c2094130065af21Dmitri Gribenko        SourceMgr.getLineNumber(DeclLocDecomp.first, DeclLocDecomp.second)
185aa0cd85838f2a024e589ea4e8c2094130065af21Dmitri Gribenko          == SourceMgr.getLineNumber(CommentBeginDecomp.first,
186aa0cd85838f2a024e589ea4e8c2094130065af21Dmitri Gribenko                                     CommentBeginDecomp.second)) {
187811c820257746b1799b790b6adc7804f44154011Dmitri Gribenko      return *Comment;
188aa0cd85838f2a024e589ea4e8c2094130065af21Dmitri Gribenko    }
189aa0cd85838f2a024e589ea4e8c2094130065af21Dmitri Gribenko  }
190aa0cd85838f2a024e589ea4e8c2094130065af21Dmitri Gribenko
191aa0cd85838f2a024e589ea4e8c2094130065af21Dmitri Gribenko  // The comment just after the declaration was not a trailing comment.
192aa0cd85838f2a024e589ea4e8c2094130065af21Dmitri Gribenko  // Let's look at the previous comment.
193aa0cd85838f2a024e589ea4e8c2094130065af21Dmitri Gribenko  if (Comment == RawComments.begin())
194aa0cd85838f2a024e589ea4e8c2094130065af21Dmitri Gribenko    return NULL;
195aa0cd85838f2a024e589ea4e8c2094130065af21Dmitri Gribenko  --Comment;
196aa0cd85838f2a024e589ea4e8c2094130065af21Dmitri Gribenko
197aa0cd85838f2a024e589ea4e8c2094130065af21Dmitri Gribenko  // Check that we actually have a non-member Doxygen comment.
198811c820257746b1799b790b6adc7804f44154011Dmitri Gribenko  if (!(*Comment)->isDocumentation() || (*Comment)->isTrailingComment())
199aa0cd85838f2a024e589ea4e8c2094130065af21Dmitri Gribenko    return NULL;
200aa0cd85838f2a024e589ea4e8c2094130065af21Dmitri Gribenko
201aa0cd85838f2a024e589ea4e8c2094130065af21Dmitri Gribenko  // Decompose the end of the comment.
202aa0cd85838f2a024e589ea4e8c2094130065af21Dmitri Gribenko  std::pair<FileID, unsigned> CommentEndDecomp
203811c820257746b1799b790b6adc7804f44154011Dmitri Gribenko    = SourceMgr.getDecomposedLoc((*Comment)->getSourceRange().getEnd());
204aa0cd85838f2a024e589ea4e8c2094130065af21Dmitri Gribenko
205aa0cd85838f2a024e589ea4e8c2094130065af21Dmitri Gribenko  // If the comment and the declaration aren't in the same file, then they
206aa0cd85838f2a024e589ea4e8c2094130065af21Dmitri Gribenko  // aren't related.
207aa0cd85838f2a024e589ea4e8c2094130065af21Dmitri Gribenko  if (DeclLocDecomp.first != CommentEndDecomp.first)
208aa0cd85838f2a024e589ea4e8c2094130065af21Dmitri Gribenko    return NULL;
209aa0cd85838f2a024e589ea4e8c2094130065af21Dmitri Gribenko
210aa0cd85838f2a024e589ea4e8c2094130065af21Dmitri Gribenko  // Get the corresponding buffer.
211aa0cd85838f2a024e589ea4e8c2094130065af21Dmitri Gribenko  bool Invalid = false;
212aa0cd85838f2a024e589ea4e8c2094130065af21Dmitri Gribenko  const char *Buffer = SourceMgr.getBufferData(DeclLocDecomp.first,
213aa0cd85838f2a024e589ea4e8c2094130065af21Dmitri Gribenko                                               &Invalid).data();
214aa0cd85838f2a024e589ea4e8c2094130065af21Dmitri Gribenko  if (Invalid)
215aa0cd85838f2a024e589ea4e8c2094130065af21Dmitri Gribenko    return NULL;
216aa0cd85838f2a024e589ea4e8c2094130065af21Dmitri Gribenko
217aa0cd85838f2a024e589ea4e8c2094130065af21Dmitri Gribenko  // Extract text between the comment and declaration.
218aa0cd85838f2a024e589ea4e8c2094130065af21Dmitri Gribenko  StringRef Text(Buffer + CommentEndDecomp.second,
219aa0cd85838f2a024e589ea4e8c2094130065af21Dmitri Gribenko                 DeclLocDecomp.second - CommentEndDecomp.second);
220aa0cd85838f2a024e589ea4e8c2094130065af21Dmitri Gribenko
2218bdb58a7835a9a90dd9b9791fccf269cbc1dcef3Dmitri Gribenko  // There should be no other declarations or preprocessor directives between
2228bdb58a7835a9a90dd9b9791fccf269cbc1dcef3Dmitri Gribenko  // comment and declaration.
223abd56c816e9164b17bb3e7154a511b0c9896ffdbDmitri Gribenko  if (Text.find_first_of(",;{}#@") != StringRef::npos)
224aa0cd85838f2a024e589ea4e8c2094130065af21Dmitri Gribenko    return NULL;
225aa0cd85838f2a024e589ea4e8c2094130065af21Dmitri Gribenko
226811c820257746b1799b790b6adc7804f44154011Dmitri Gribenko  return *Comment;
227aa0cd85838f2a024e589ea4e8c2094130065af21Dmitri Gribenko}
228aa0cd85838f2a024e589ea4e8c2094130065af21Dmitri Gribenko
229c41ace950dcf2254c9aa48e73647b89c35109f80Dmitri Gribenkonamespace {
230c41ace950dcf2254c9aa48e73647b89c35109f80Dmitri Gribenko/// If we have a 'templated' declaration for a template, adjust 'D' to
231c41ace950dcf2254c9aa48e73647b89c35109f80Dmitri Gribenko/// refer to the actual template.
2322125c9010e259548a8c476fa998a561889555c95Dmitri Gribenko/// If we have an implicit instantiation, adjust 'D' to refer to template.
233c41ace950dcf2254c9aa48e73647b89c35109f80Dmitri Gribenkoconst Decl *adjustDeclToTemplate(const Decl *D) {
234cd81df2dcff4e13eea6edfbfd52a4458d978d174Douglas Gregor  if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
2352125c9010e259548a8c476fa998a561889555c95Dmitri Gribenko    // Is this function declaration part of a function template?
236cd81df2dcff4e13eea6edfbfd52a4458d978d174Douglas Gregor    if (const FunctionTemplateDecl *FTD = FD->getDescribedFunctionTemplate())
2372125c9010e259548a8c476fa998a561889555c95Dmitri Gribenko      return FTD;
2382125c9010e259548a8c476fa998a561889555c95Dmitri Gribenko
2392125c9010e259548a8c476fa998a561889555c95Dmitri Gribenko    // Nothing to do if function is not an implicit instantiation.
2402125c9010e259548a8c476fa998a561889555c95Dmitri Gribenko    if (FD->getTemplateSpecializationKind() != TSK_ImplicitInstantiation)
2412125c9010e259548a8c476fa998a561889555c95Dmitri Gribenko      return D;
2422125c9010e259548a8c476fa998a561889555c95Dmitri Gribenko
2432125c9010e259548a8c476fa998a561889555c95Dmitri Gribenko    // Function is an implicit instantiation of a function template?
2442125c9010e259548a8c476fa998a561889555c95Dmitri Gribenko    if (const FunctionTemplateDecl *FTD = FD->getPrimaryTemplate())
2452125c9010e259548a8c476fa998a561889555c95Dmitri Gribenko      return FTD;
2462125c9010e259548a8c476fa998a561889555c95Dmitri Gribenko
2472125c9010e259548a8c476fa998a561889555c95Dmitri Gribenko    // Function is instantiated from a member definition of a class template?
2482125c9010e259548a8c476fa998a561889555c95Dmitri Gribenko    if (const FunctionDecl *MemberDecl =
2492125c9010e259548a8c476fa998a561889555c95Dmitri Gribenko            FD->getInstantiatedFromMemberFunction())
2502125c9010e259548a8c476fa998a561889555c95Dmitri Gribenko      return MemberDecl;
2512125c9010e259548a8c476fa998a561889555c95Dmitri Gribenko
2522125c9010e259548a8c476fa998a561889555c95Dmitri Gribenko    return D;
2532125c9010e259548a8c476fa998a561889555c95Dmitri Gribenko  }
2542125c9010e259548a8c476fa998a561889555c95Dmitri Gribenko  if (const VarDecl *VD = dyn_cast<VarDecl>(D)) {
2552125c9010e259548a8c476fa998a561889555c95Dmitri Gribenko    // Static data member is instantiated from a member definition of a class
2562125c9010e259548a8c476fa998a561889555c95Dmitri Gribenko    // template?
2572125c9010e259548a8c476fa998a561889555c95Dmitri Gribenko    if (VD->isStaticDataMember())
2582125c9010e259548a8c476fa998a561889555c95Dmitri Gribenko      if (const VarDecl *MemberDecl = VD->getInstantiatedFromStaticDataMember())
2592125c9010e259548a8c476fa998a561889555c95Dmitri Gribenko        return MemberDecl;
2602125c9010e259548a8c476fa998a561889555c95Dmitri Gribenko
2612125c9010e259548a8c476fa998a561889555c95Dmitri Gribenko    return D;
2622125c9010e259548a8c476fa998a561889555c95Dmitri Gribenko  }
2632125c9010e259548a8c476fa998a561889555c95Dmitri Gribenko  if (const CXXRecordDecl *CRD = dyn_cast<CXXRecordDecl>(D)) {
2642125c9010e259548a8c476fa998a561889555c95Dmitri Gribenko    // Is this class declaration part of a class template?
2652125c9010e259548a8c476fa998a561889555c95Dmitri Gribenko    if (const ClassTemplateDecl *CTD = CRD->getDescribedClassTemplate())
2662125c9010e259548a8c476fa998a561889555c95Dmitri Gribenko      return CTD;
2672125c9010e259548a8c476fa998a561889555c95Dmitri Gribenko
2682125c9010e259548a8c476fa998a561889555c95Dmitri Gribenko    // Class is an implicit instantiation of a class template or partial
2692125c9010e259548a8c476fa998a561889555c95Dmitri Gribenko    // specialization?
2702125c9010e259548a8c476fa998a561889555c95Dmitri Gribenko    if (const ClassTemplateSpecializationDecl *CTSD =
2712125c9010e259548a8c476fa998a561889555c95Dmitri Gribenko            dyn_cast<ClassTemplateSpecializationDecl>(CRD)) {
2722125c9010e259548a8c476fa998a561889555c95Dmitri Gribenko      if (CTSD->getSpecializationKind() != TSK_ImplicitInstantiation)
2732125c9010e259548a8c476fa998a561889555c95Dmitri Gribenko        return D;
2742125c9010e259548a8c476fa998a561889555c95Dmitri Gribenko      llvm::PointerUnion<ClassTemplateDecl *,
2752125c9010e259548a8c476fa998a561889555c95Dmitri Gribenko                         ClassTemplatePartialSpecializationDecl *>
2762125c9010e259548a8c476fa998a561889555c95Dmitri Gribenko          PU = CTSD->getSpecializedTemplateOrPartial();
2772125c9010e259548a8c476fa998a561889555c95Dmitri Gribenko      return PU.is<ClassTemplateDecl*>() ?
2782125c9010e259548a8c476fa998a561889555c95Dmitri Gribenko          static_cast<const Decl*>(PU.get<ClassTemplateDecl *>()) :
2792125c9010e259548a8c476fa998a561889555c95Dmitri Gribenko          static_cast<const Decl*>(
2802125c9010e259548a8c476fa998a561889555c95Dmitri Gribenko              PU.get<ClassTemplatePartialSpecializationDecl *>());
2812125c9010e259548a8c476fa998a561889555c95Dmitri Gribenko    }
2822125c9010e259548a8c476fa998a561889555c95Dmitri Gribenko
2832125c9010e259548a8c476fa998a561889555c95Dmitri Gribenko    // Class is instantiated from a member definition of a class template?
2842125c9010e259548a8c476fa998a561889555c95Dmitri Gribenko    if (const MemberSpecializationInfo *Info =
2852125c9010e259548a8c476fa998a561889555c95Dmitri Gribenko                   CRD->getMemberSpecializationInfo())
2862125c9010e259548a8c476fa998a561889555c95Dmitri Gribenko      return Info->getInstantiatedFrom();
2872125c9010e259548a8c476fa998a561889555c95Dmitri Gribenko
2882125c9010e259548a8c476fa998a561889555c95Dmitri Gribenko    return D;
2892125c9010e259548a8c476fa998a561889555c95Dmitri Gribenko  }
2902125c9010e259548a8c476fa998a561889555c95Dmitri Gribenko  if (const EnumDecl *ED = dyn_cast<EnumDecl>(D)) {
2912125c9010e259548a8c476fa998a561889555c95Dmitri Gribenko    // Enum is instantiated from a member definition of a class template?
2922125c9010e259548a8c476fa998a561889555c95Dmitri Gribenko    if (const EnumDecl *MemberDecl = ED->getInstantiatedFromMemberEnum())
2932125c9010e259548a8c476fa998a561889555c95Dmitri Gribenko      return MemberDecl;
2942125c9010e259548a8c476fa998a561889555c95Dmitri Gribenko
2952125c9010e259548a8c476fa998a561889555c95Dmitri Gribenko    return D;
296cd81df2dcff4e13eea6edfbfd52a4458d978d174Douglas Gregor  }
2972125c9010e259548a8c476fa998a561889555c95Dmitri Gribenko  // FIXME: Adjust alias templates?
298c41ace950dcf2254c9aa48e73647b89c35109f80Dmitri Gribenko  return D;
299c41ace950dcf2254c9aa48e73647b89c35109f80Dmitri Gribenko}
300c41ace950dcf2254c9aa48e73647b89c35109f80Dmitri Gribenko} // unnamed namespace
301c41ace950dcf2254c9aa48e73647b89c35109f80Dmitri Gribenko
3021599eac40a3b28de0824013dc2fb90551dfa01b0Dmitri Gribenkoconst RawComment *ASTContext::getRawCommentForAnyRedecl(
3031599eac40a3b28de0824013dc2fb90551dfa01b0Dmitri Gribenko                                                const Decl *D,
3041599eac40a3b28de0824013dc2fb90551dfa01b0Dmitri Gribenko                                                const Decl **OriginalDecl) const {
305c41ace950dcf2254c9aa48e73647b89c35109f80Dmitri Gribenko  D = adjustDeclToTemplate(D);
306cd81df2dcff4e13eea6edfbfd52a4458d978d174Douglas Gregor
307f50555eedef33fd5a67d369aa0ae8a6f1d201543Dmitri Gribenko  // Check whether we have cached a comment for this declaration already.
308f50555eedef33fd5a67d369aa0ae8a6f1d201543Dmitri Gribenko  {
309f50555eedef33fd5a67d369aa0ae8a6f1d201543Dmitri Gribenko    llvm::DenseMap<const Decl *, RawCommentAndCacheFlags>::iterator Pos =
310f50555eedef33fd5a67d369aa0ae8a6f1d201543Dmitri Gribenko        RedeclComments.find(D);
311f50555eedef33fd5a67d369aa0ae8a6f1d201543Dmitri Gribenko    if (Pos != RedeclComments.end()) {
312f50555eedef33fd5a67d369aa0ae8a6f1d201543Dmitri Gribenko      const RawCommentAndCacheFlags &Raw = Pos->second;
3131599eac40a3b28de0824013dc2fb90551dfa01b0Dmitri Gribenko      if (Raw.getKind() != RawCommentAndCacheFlags::NoCommentInDecl) {
3141599eac40a3b28de0824013dc2fb90551dfa01b0Dmitri Gribenko        if (OriginalDecl)
3151599eac40a3b28de0824013dc2fb90551dfa01b0Dmitri Gribenko          *OriginalDecl = Raw.getOriginalDecl();
316f50555eedef33fd5a67d369aa0ae8a6f1d201543Dmitri Gribenko        return Raw.getRaw();
3171599eac40a3b28de0824013dc2fb90551dfa01b0Dmitri Gribenko      }
318f50555eedef33fd5a67d369aa0ae8a6f1d201543Dmitri Gribenko    }
319f50555eedef33fd5a67d369aa0ae8a6f1d201543Dmitri Gribenko  }
320f50555eedef33fd5a67d369aa0ae8a6f1d201543Dmitri Gribenko
321f50555eedef33fd5a67d369aa0ae8a6f1d201543Dmitri Gribenko  // Search for comments attached to declarations in the redeclaration chain.
322f50555eedef33fd5a67d369aa0ae8a6f1d201543Dmitri Gribenko  const RawComment *RC = NULL;
3231599eac40a3b28de0824013dc2fb90551dfa01b0Dmitri Gribenko  const Decl *OriginalDeclForRC = NULL;
324f50555eedef33fd5a67d369aa0ae8a6f1d201543Dmitri Gribenko  for (Decl::redecl_iterator I = D->redecls_begin(),
325f50555eedef33fd5a67d369aa0ae8a6f1d201543Dmitri Gribenko                             E = D->redecls_end();
326f50555eedef33fd5a67d369aa0ae8a6f1d201543Dmitri Gribenko       I != E; ++I) {
327f50555eedef33fd5a67d369aa0ae8a6f1d201543Dmitri Gribenko    llvm::DenseMap<const Decl *, RawCommentAndCacheFlags>::iterator Pos =
328f50555eedef33fd5a67d369aa0ae8a6f1d201543Dmitri Gribenko        RedeclComments.find(*I);
329f50555eedef33fd5a67d369aa0ae8a6f1d201543Dmitri Gribenko    if (Pos != RedeclComments.end()) {
330f50555eedef33fd5a67d369aa0ae8a6f1d201543Dmitri Gribenko      const RawCommentAndCacheFlags &Raw = Pos->second;
331f50555eedef33fd5a67d369aa0ae8a6f1d201543Dmitri Gribenko      if (Raw.getKind() != RawCommentAndCacheFlags::NoCommentInDecl) {
332f50555eedef33fd5a67d369aa0ae8a6f1d201543Dmitri Gribenko        RC = Raw.getRaw();
3331599eac40a3b28de0824013dc2fb90551dfa01b0Dmitri Gribenko        OriginalDeclForRC = Raw.getOriginalDecl();
334f50555eedef33fd5a67d369aa0ae8a6f1d201543Dmitri Gribenko        break;
335f50555eedef33fd5a67d369aa0ae8a6f1d201543Dmitri Gribenko      }
336f50555eedef33fd5a67d369aa0ae8a6f1d201543Dmitri Gribenko    } else {
337f50555eedef33fd5a67d369aa0ae8a6f1d201543Dmitri Gribenko      RC = getRawCommentForDeclNoCache(*I);
3381599eac40a3b28de0824013dc2fb90551dfa01b0Dmitri Gribenko      OriginalDeclForRC = *I;
339f50555eedef33fd5a67d369aa0ae8a6f1d201543Dmitri Gribenko      RawCommentAndCacheFlags Raw;
340f50555eedef33fd5a67d369aa0ae8a6f1d201543Dmitri Gribenko      if (RC) {
341f50555eedef33fd5a67d369aa0ae8a6f1d201543Dmitri Gribenko        Raw.setRaw(RC);
342f50555eedef33fd5a67d369aa0ae8a6f1d201543Dmitri Gribenko        Raw.setKind(RawCommentAndCacheFlags::FromDecl);
343f50555eedef33fd5a67d369aa0ae8a6f1d201543Dmitri Gribenko      } else
344f50555eedef33fd5a67d369aa0ae8a6f1d201543Dmitri Gribenko        Raw.setKind(RawCommentAndCacheFlags::NoCommentInDecl);
3451599eac40a3b28de0824013dc2fb90551dfa01b0Dmitri Gribenko      Raw.setOriginalDecl(*I);
346f50555eedef33fd5a67d369aa0ae8a6f1d201543Dmitri Gribenko      RedeclComments[*I] = Raw;
347f50555eedef33fd5a67d369aa0ae8a6f1d201543Dmitri Gribenko      if (RC)
348f50555eedef33fd5a67d369aa0ae8a6f1d201543Dmitri Gribenko        break;
349f50555eedef33fd5a67d369aa0ae8a6f1d201543Dmitri Gribenko    }
3508d3ba23f2d9e6c87794d059412a0808c9cbacb25Dmitri Gribenko  }
351aa0cd85838f2a024e589ea4e8c2094130065af21Dmitri Gribenko
3528376f5934a18b950ac7323d8a38ed231623010faDmitri Gribenko  // If we found a comment, it should be a documentation comment.
3538376f5934a18b950ac7323d8a38ed231623010faDmitri Gribenko  assert(!RC || RC->isDocumentation());
354f50555eedef33fd5a67d369aa0ae8a6f1d201543Dmitri Gribenko
3551599eac40a3b28de0824013dc2fb90551dfa01b0Dmitri Gribenko  if (OriginalDecl)
3561599eac40a3b28de0824013dc2fb90551dfa01b0Dmitri Gribenko    *OriginalDecl = OriginalDeclForRC;
3571599eac40a3b28de0824013dc2fb90551dfa01b0Dmitri Gribenko
358f50555eedef33fd5a67d369aa0ae8a6f1d201543Dmitri Gribenko  // Update cache for every declaration in the redeclaration chain.
359f50555eedef33fd5a67d369aa0ae8a6f1d201543Dmitri Gribenko  RawCommentAndCacheFlags Raw;
360f50555eedef33fd5a67d369aa0ae8a6f1d201543Dmitri Gribenko  Raw.setRaw(RC);
361f50555eedef33fd5a67d369aa0ae8a6f1d201543Dmitri Gribenko  Raw.setKind(RawCommentAndCacheFlags::FromRedecl);
3621599eac40a3b28de0824013dc2fb90551dfa01b0Dmitri Gribenko  Raw.setOriginalDecl(OriginalDeclForRC);
363f50555eedef33fd5a67d369aa0ae8a6f1d201543Dmitri Gribenko
364f50555eedef33fd5a67d369aa0ae8a6f1d201543Dmitri Gribenko  for (Decl::redecl_iterator I = D->redecls_begin(),
365f50555eedef33fd5a67d369aa0ae8a6f1d201543Dmitri Gribenko                             E = D->redecls_end();
366f50555eedef33fd5a67d369aa0ae8a6f1d201543Dmitri Gribenko       I != E; ++I) {
367f50555eedef33fd5a67d369aa0ae8a6f1d201543Dmitri Gribenko    RawCommentAndCacheFlags &R = RedeclComments[*I];
368f50555eedef33fd5a67d369aa0ae8a6f1d201543Dmitri Gribenko    if (R.getKind() == RawCommentAndCacheFlags::NoCommentInDecl)
369f50555eedef33fd5a67d369aa0ae8a6f1d201543Dmitri Gribenko      R = Raw;
370f50555eedef33fd5a67d369aa0ae8a6f1d201543Dmitri Gribenko  }
371f50555eedef33fd5a67d369aa0ae8a6f1d201543Dmitri Gribenko
372aa0cd85838f2a024e589ea4e8c2094130065af21Dmitri Gribenko  return RC;
373aa0cd85838f2a024e589ea4e8c2094130065af21Dmitri Gribenko}
374aa0cd85838f2a024e589ea4e8c2094130065af21Dmitri Gribenko
375bf967be66ea8c51b66c61659c23240f762a56dbeFariborz Jahanianstatic void addRedeclaredMethods(const ObjCMethodDecl *ObjCMethod,
376bf967be66ea8c51b66c61659c23240f762a56dbeFariborz Jahanian                   SmallVectorImpl<const NamedDecl *> &Redeclared) {
377bf967be66ea8c51b66c61659c23240f762a56dbeFariborz Jahanian  const DeclContext *DC = ObjCMethod->getDeclContext();
378bf967be66ea8c51b66c61659c23240f762a56dbeFariborz Jahanian  if (const ObjCImplDecl *IMD = dyn_cast<ObjCImplDecl>(DC)) {
379bf967be66ea8c51b66c61659c23240f762a56dbeFariborz Jahanian    const ObjCInterfaceDecl *ID = IMD->getClassInterface();
380bf967be66ea8c51b66c61659c23240f762a56dbeFariborz Jahanian    if (!ID)
381bf967be66ea8c51b66c61659c23240f762a56dbeFariborz Jahanian      return;
382bf967be66ea8c51b66c61659c23240f762a56dbeFariborz Jahanian    // Add redeclared method here.
383d329724745b49f894b768d47275b7c2713106e89Douglas Gregor    for (ObjCInterfaceDecl::known_extensions_iterator
384d329724745b49f894b768d47275b7c2713106e89Douglas Gregor           Ext = ID->known_extensions_begin(),
385d329724745b49f894b768d47275b7c2713106e89Douglas Gregor           ExtEnd = ID->known_extensions_end();
386d329724745b49f894b768d47275b7c2713106e89Douglas Gregor         Ext != ExtEnd; ++Ext) {
387bf967be66ea8c51b66c61659c23240f762a56dbeFariborz Jahanian      if (ObjCMethodDecl *RedeclaredMethod =
388d329724745b49f894b768d47275b7c2713106e89Douglas Gregor            Ext->getMethod(ObjCMethod->getSelector(),
389bf967be66ea8c51b66c61659c23240f762a56dbeFariborz Jahanian                                  ObjCMethod->isInstanceMethod()))
390bf967be66ea8c51b66c61659c23240f762a56dbeFariborz Jahanian        Redeclared.push_back(RedeclaredMethod);
391bf967be66ea8c51b66c61659c23240f762a56dbeFariborz Jahanian    }
392bf967be66ea8c51b66c61659c23240f762a56dbeFariborz Jahanian  }
393bf967be66ea8c51b66c61659c23240f762a56dbeFariborz Jahanian}
394bf967be66ea8c51b66c61659c23240f762a56dbeFariborz Jahanian
395749ace614b6ea1ae11d194a60b18e1e43e1db243Fariborz Jahaniancomments::FullComment *ASTContext::cloneFullComment(comments::FullComment *FC,
396749ace614b6ea1ae11d194a60b18e1e43e1db243Fariborz Jahanian                                                    const Decl *D) const {
397749ace614b6ea1ae11d194a60b18e1e43e1db243Fariborz Jahanian  comments::DeclInfo *ThisDeclInfo = new (*this) comments::DeclInfo;
398749ace614b6ea1ae11d194a60b18e1e43e1db243Fariborz Jahanian  ThisDeclInfo->CommentDecl = D;
399749ace614b6ea1ae11d194a60b18e1e43e1db243Fariborz Jahanian  ThisDeclInfo->IsFilled = false;
400749ace614b6ea1ae11d194a60b18e1e43e1db243Fariborz Jahanian  ThisDeclInfo->fill();
401749ace614b6ea1ae11d194a60b18e1e43e1db243Fariborz Jahanian  ThisDeclInfo->CommentDecl = FC->getDecl();
402749ace614b6ea1ae11d194a60b18e1e43e1db243Fariborz Jahanian  comments::FullComment *CFC =
403749ace614b6ea1ae11d194a60b18e1e43e1db243Fariborz Jahanian    new (*this) comments::FullComment(FC->getBlocks(),
404749ace614b6ea1ae11d194a60b18e1e43e1db243Fariborz Jahanian                                      ThisDeclInfo);
405749ace614b6ea1ae11d194a60b18e1e43e1db243Fariborz Jahanian  return CFC;
406749ace614b6ea1ae11d194a60b18e1e43e1db243Fariborz Jahanian
407749ace614b6ea1ae11d194a60b18e1e43e1db243Fariborz Jahanian}
408749ace614b6ea1ae11d194a60b18e1e43e1db243Fariborz Jahanian
4090a74a4ccc4ecfe1a2792ab72c83815323d8fc914Richard Smithcomments::FullComment *ASTContext::getLocalCommentForDeclUncached(const Decl *D) const {
4100a74a4ccc4ecfe1a2792ab72c83815323d8fc914Richard Smith  const RawComment *RC = getRawCommentForDeclNoCache(D);
4110a74a4ccc4ecfe1a2792ab72c83815323d8fc914Richard Smith  return RC ? RC->parse(*this, 0, D) : 0;
4120a74a4ccc4ecfe1a2792ab72c83815323d8fc914Richard Smith}
4130a74a4ccc4ecfe1a2792ab72c83815323d8fc914Richard Smith
4141952354bd376062c3ab3d328c0fc6c36530c9309Dmitri Gribenkocomments::FullComment *ASTContext::getCommentForDecl(
4151952354bd376062c3ab3d328c0fc6c36530c9309Dmitri Gribenko                                              const Decl *D,
4161952354bd376062c3ab3d328c0fc6c36530c9309Dmitri Gribenko                                              const Preprocessor *PP) const {
417fbff0c4510c7f0e0f30a005960e434b973f5bd21Fariborz Jahanian  if (D->isInvalidDecl())
418fbff0c4510c7f0e0f30a005960e434b973f5bd21Fariborz Jahanian    return NULL;
419c41ace950dcf2254c9aa48e73647b89c35109f80Dmitri Gribenko  D = adjustDeclToTemplate(D);
420bf967be66ea8c51b66c61659c23240f762a56dbeFariborz Jahanian
421c41ace950dcf2254c9aa48e73647b89c35109f80Dmitri Gribenko  const Decl *Canonical = D->getCanonicalDecl();
422c41ace950dcf2254c9aa48e73647b89c35109f80Dmitri Gribenko  llvm::DenseMap<const Decl *, comments::FullComment *>::iterator Pos =
423c41ace950dcf2254c9aa48e73647b89c35109f80Dmitri Gribenko      ParsedComments.find(Canonical);
424bf967be66ea8c51b66c61659c23240f762a56dbeFariborz Jahanian
425bf967be66ea8c51b66c61659c23240f762a56dbeFariborz Jahanian  if (Pos != ParsedComments.end()) {
426749ace614b6ea1ae11d194a60b18e1e43e1db243Fariborz Jahanian    if (Canonical != D) {
427bf967be66ea8c51b66c61659c23240f762a56dbeFariborz Jahanian      comments::FullComment *FC = Pos->second;
428749ace614b6ea1ae11d194a60b18e1e43e1db243Fariborz Jahanian      comments::FullComment *CFC = cloneFullComment(FC, D);
429bf967be66ea8c51b66c61659c23240f762a56dbeFariborz Jahanian      return CFC;
430bf967be66ea8c51b66c61659c23240f762a56dbeFariborz Jahanian    }
431c41ace950dcf2254c9aa48e73647b89c35109f80Dmitri Gribenko    return Pos->second;
432bf967be66ea8c51b66c61659c23240f762a56dbeFariborz Jahanian  }
433bf967be66ea8c51b66c61659c23240f762a56dbeFariborz Jahanian
4341599eac40a3b28de0824013dc2fb90551dfa01b0Dmitri Gribenko  const Decl *OriginalDecl;
435bf967be66ea8c51b66c61659c23240f762a56dbeFariborz Jahanian
4361599eac40a3b28de0824013dc2fb90551dfa01b0Dmitri Gribenko  const RawComment *RC = getRawCommentForAnyRedecl(D, &OriginalDecl);
437bf967be66ea8c51b66c61659c23240f762a56dbeFariborz Jahanian  if (!RC) {
438bf967be66ea8c51b66c61659c23240f762a56dbeFariborz Jahanian    if (isa<ObjCMethodDecl>(D) || isa<FunctionDecl>(D)) {
4391e905da74dbfd353d77dfc548fc9d6ff420d515aDmitri Gribenko      SmallVector<const NamedDecl*, 8> Overridden;
440c328d9c22a4397dd7313d06be5b82d700297b246Fariborz Jahanian      const ObjCMethodDecl *OMD = dyn_cast<ObjCMethodDecl>(D);
44123799e3ec4b341753e4fb63a7e995cf4ac3b6066Fariborz Jahanian      if (OMD && OMD->isPropertyAccessor())
44223799e3ec4b341753e4fb63a7e995cf4ac3b6066Fariborz Jahanian        if (const ObjCPropertyDecl *PDecl = OMD->findPropertyDecl())
44323799e3ec4b341753e4fb63a7e995cf4ac3b6066Fariborz Jahanian          if (comments::FullComment *FC = getCommentForDecl(PDecl, PP))
44423799e3ec4b341753e4fb63a7e995cf4ac3b6066Fariborz Jahanian            return cloneFullComment(FC, D);
445c328d9c22a4397dd7313d06be5b82d700297b246Fariborz Jahanian      if (OMD)
4461e905da74dbfd353d77dfc548fc9d6ff420d515aDmitri Gribenko        addRedeclaredMethods(OMD, Overridden);
4471e905da74dbfd353d77dfc548fc9d6ff420d515aDmitri Gribenko      getOverriddenMethods(dyn_cast<NamedDecl>(D), Overridden);
44823799e3ec4b341753e4fb63a7e995cf4ac3b6066Fariborz Jahanian      for (unsigned i = 0, e = Overridden.size(); i < e; i++)
44923799e3ec4b341753e4fb63a7e995cf4ac3b6066Fariborz Jahanian        if (comments::FullComment *FC = getCommentForDecl(Overridden[i], PP))
45023799e3ec4b341753e4fb63a7e995cf4ac3b6066Fariborz Jahanian          return cloneFullComment(FC, D);
451bf967be66ea8c51b66c61659c23240f762a56dbeFariborz Jahanian    }
4524857fdcdcb61f314a53ea45be3cbfaf60bb9255bFariborz Jahanian    else if (const TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D)) {
453d1e5c0df2acb01192eedda14534fe990e1e26c46Dmitri Gribenko      // Attach any tag type's documentation to its typedef if latter
45423799e3ec4b341753e4fb63a7e995cf4ac3b6066Fariborz Jahanian      // does not have one of its own.
45541170b55ba635afb806394d44f2b7f1f6095df37Fariborz Jahanian      QualType QT = TD->getUnderlyingType();
456d1e5c0df2acb01192eedda14534fe990e1e26c46Dmitri Gribenko      if (const TagType *TT = QT->getAs<TagType>())
457d1e5c0df2acb01192eedda14534fe990e1e26c46Dmitri Gribenko        if (const Decl *TD = TT->getDecl())
458d1e5c0df2acb01192eedda14534fe990e1e26c46Dmitri Gribenko          if (comments::FullComment *FC = getCommentForDecl(TD, PP))
45923799e3ec4b341753e4fb63a7e995cf4ac3b6066Fariborz Jahanian            return cloneFullComment(FC, D);
46041170b55ba635afb806394d44f2b7f1f6095df37Fariborz Jahanian    }
461622bb4acc00fd63bac545ca23f7c42fd909dc0e7Fariborz Jahanian    else if (const ObjCInterfaceDecl *IC = dyn_cast<ObjCInterfaceDecl>(D)) {
462622bb4acc00fd63bac545ca23f7c42fd909dc0e7Fariborz Jahanian      while (IC->getSuperClass()) {
463622bb4acc00fd63bac545ca23f7c42fd909dc0e7Fariborz Jahanian        IC = IC->getSuperClass();
464622bb4acc00fd63bac545ca23f7c42fd909dc0e7Fariborz Jahanian        if (comments::FullComment *FC = getCommentForDecl(IC, PP))
465622bb4acc00fd63bac545ca23f7c42fd909dc0e7Fariborz Jahanian          return cloneFullComment(FC, D);
466622bb4acc00fd63bac545ca23f7c42fd909dc0e7Fariborz Jahanian      }
467622bb4acc00fd63bac545ca23f7c42fd909dc0e7Fariborz Jahanian    }
468622bb4acc00fd63bac545ca23f7c42fd909dc0e7Fariborz Jahanian    else if (const ObjCCategoryDecl *CD = dyn_cast<ObjCCategoryDecl>(D)) {
469622bb4acc00fd63bac545ca23f7c42fd909dc0e7Fariborz Jahanian      if (const ObjCInterfaceDecl *IC = CD->getClassInterface())
470622bb4acc00fd63bac545ca23f7c42fd909dc0e7Fariborz Jahanian        if (comments::FullComment *FC = getCommentForDecl(IC, PP))
471622bb4acc00fd63bac545ca23f7c42fd909dc0e7Fariborz Jahanian          return cloneFullComment(FC, D);
472622bb4acc00fd63bac545ca23f7c42fd909dc0e7Fariborz Jahanian    }
473622bb4acc00fd63bac545ca23f7c42fd909dc0e7Fariborz Jahanian    else if (const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(D)) {
474622bb4acc00fd63bac545ca23f7c42fd909dc0e7Fariborz Jahanian      if (!(RD = RD->getDefinition()))
475622bb4acc00fd63bac545ca23f7c42fd909dc0e7Fariborz Jahanian        return NULL;
476622bb4acc00fd63bac545ca23f7c42fd909dc0e7Fariborz Jahanian      // Check non-virtual bases.
477622bb4acc00fd63bac545ca23f7c42fd909dc0e7Fariborz Jahanian      for (CXXRecordDecl::base_class_const_iterator I =
478622bb4acc00fd63bac545ca23f7c42fd909dc0e7Fariborz Jahanian           RD->bases_begin(), E = RD->bases_end(); I != E; ++I) {
47991efca0fa2ef5e63b48692e3439f5c6e6bde350cFariborz Jahanian        if (I->isVirtual() || (I->getAccessSpecifier() != AS_public))
480622bb4acc00fd63bac545ca23f7c42fd909dc0e7Fariborz Jahanian          continue;
481622bb4acc00fd63bac545ca23f7c42fd909dc0e7Fariborz Jahanian        QualType Ty = I->getType();
482622bb4acc00fd63bac545ca23f7c42fd909dc0e7Fariborz Jahanian        if (Ty.isNull())
483622bb4acc00fd63bac545ca23f7c42fd909dc0e7Fariborz Jahanian          continue;
484622bb4acc00fd63bac545ca23f7c42fd909dc0e7Fariborz Jahanian        if (const CXXRecordDecl *NonVirtualBase = Ty->getAsCXXRecordDecl()) {
485622bb4acc00fd63bac545ca23f7c42fd909dc0e7Fariborz Jahanian          if (!(NonVirtualBase= NonVirtualBase->getDefinition()))
486622bb4acc00fd63bac545ca23f7c42fd909dc0e7Fariborz Jahanian            continue;
487622bb4acc00fd63bac545ca23f7c42fd909dc0e7Fariborz Jahanian
488622bb4acc00fd63bac545ca23f7c42fd909dc0e7Fariborz Jahanian          if (comments::FullComment *FC = getCommentForDecl((NonVirtualBase), PP))
489622bb4acc00fd63bac545ca23f7c42fd909dc0e7Fariborz Jahanian            return cloneFullComment(FC, D);
490622bb4acc00fd63bac545ca23f7c42fd909dc0e7Fariborz Jahanian        }
491622bb4acc00fd63bac545ca23f7c42fd909dc0e7Fariborz Jahanian      }
492622bb4acc00fd63bac545ca23f7c42fd909dc0e7Fariborz Jahanian      // Check virtual bases.
493622bb4acc00fd63bac545ca23f7c42fd909dc0e7Fariborz Jahanian      for (CXXRecordDecl::base_class_const_iterator I =
494622bb4acc00fd63bac545ca23f7c42fd909dc0e7Fariborz Jahanian           RD->vbases_begin(), E = RD->vbases_end(); I != E; ++I) {
49591efca0fa2ef5e63b48692e3439f5c6e6bde350cFariborz Jahanian        if (I->getAccessSpecifier() != AS_public)
49691efca0fa2ef5e63b48692e3439f5c6e6bde350cFariborz Jahanian          continue;
497622bb4acc00fd63bac545ca23f7c42fd909dc0e7Fariborz Jahanian        QualType Ty = I->getType();
498622bb4acc00fd63bac545ca23f7c42fd909dc0e7Fariborz Jahanian        if (Ty.isNull())
499622bb4acc00fd63bac545ca23f7c42fd909dc0e7Fariborz Jahanian          continue;
500622bb4acc00fd63bac545ca23f7c42fd909dc0e7Fariborz Jahanian        if (const CXXRecordDecl *VirtualBase = Ty->getAsCXXRecordDecl()) {
501622bb4acc00fd63bac545ca23f7c42fd909dc0e7Fariborz Jahanian          if (!(VirtualBase= VirtualBase->getDefinition()))
502622bb4acc00fd63bac545ca23f7c42fd909dc0e7Fariborz Jahanian            continue;
503622bb4acc00fd63bac545ca23f7c42fd909dc0e7Fariborz Jahanian          if (comments::FullComment *FC = getCommentForDecl((VirtualBase), PP))
504622bb4acc00fd63bac545ca23f7c42fd909dc0e7Fariborz Jahanian            return cloneFullComment(FC, D);
505622bb4acc00fd63bac545ca23f7c42fd909dc0e7Fariborz Jahanian        }
506622bb4acc00fd63bac545ca23f7c42fd909dc0e7Fariborz Jahanian      }
507622bb4acc00fd63bac545ca23f7c42fd909dc0e7Fariborz Jahanian    }
5088d3ba23f2d9e6c87794d059412a0808c9cbacb25Dmitri Gribenko    return NULL;
509bf967be66ea8c51b66c61659c23240f762a56dbeFariborz Jahanian  }
510bf967be66ea8c51b66c61659c23240f762a56dbeFariborz Jahanian
5114b41c65feb93eeb6f6d27b49a2045ea1b72de9d1Dmitri Gribenko  // If the RawComment was attached to other redeclaration of this Decl, we
5124b41c65feb93eeb6f6d27b49a2045ea1b72de9d1Dmitri Gribenko  // should parse the comment in context of that other Decl.  This is important
5134b41c65feb93eeb6f6d27b49a2045ea1b72de9d1Dmitri Gribenko  // because comments can contain references to parameter names which can be
5144b41c65feb93eeb6f6d27b49a2045ea1b72de9d1Dmitri Gribenko  // different across redeclarations.
5151599eac40a3b28de0824013dc2fb90551dfa01b0Dmitri Gribenko  if (D != OriginalDecl)
5161952354bd376062c3ab3d328c0fc6c36530c9309Dmitri Gribenko    return getCommentForDecl(OriginalDecl, PP);
5171599eac40a3b28de0824013dc2fb90551dfa01b0Dmitri Gribenko
5181952354bd376062c3ab3d328c0fc6c36530c9309Dmitri Gribenko  comments::FullComment *FC = RC->parse(*this, PP, D);
519c41ace950dcf2254c9aa48e73647b89c35109f80Dmitri Gribenko  ParsedComments[Canonical] = FC;
520c41ace950dcf2254c9aa48e73647b89c35109f80Dmitri Gribenko  return FC;
5218d3ba23f2d9e6c87794d059412a0808c9cbacb25Dmitri Gribenko}
5228d3ba23f2d9e6c87794d059412a0808c9cbacb25Dmitri Gribenko
5233e1274f2b99cb99c03cc8e2c6517c37d330b597aDouglas Gregorvoid
5243e1274f2b99cb99c03cc8e2c6517c37d330b597aDouglas GregorASTContext::CanonicalTemplateTemplateParm::Profile(llvm::FoldingSetNodeID &ID,
5253e1274f2b99cb99c03cc8e2c6517c37d330b597aDouglas Gregor                                               TemplateTemplateParmDecl *Parm) {
5263e1274f2b99cb99c03cc8e2c6517c37d330b597aDouglas Gregor  ID.AddInteger(Parm->getDepth());
5273e1274f2b99cb99c03cc8e2c6517c37d330b597aDouglas Gregor  ID.AddInteger(Parm->getPosition());
52861c4d28e36cd3f1be392cb77f07436d1fa6b0f9fDouglas Gregor  ID.AddBoolean(Parm->isParameterPack());
5293e1274f2b99cb99c03cc8e2c6517c37d330b597aDouglas Gregor
5303e1274f2b99cb99c03cc8e2c6517c37d330b597aDouglas Gregor  TemplateParameterList *Params = Parm->getTemplateParameters();
5313e1274f2b99cb99c03cc8e2c6517c37d330b597aDouglas Gregor  ID.AddInteger(Params->size());
5323e1274f2b99cb99c03cc8e2c6517c37d330b597aDouglas Gregor  for (TemplateParameterList::const_iterator P = Params->begin(),
5333e1274f2b99cb99c03cc8e2c6517c37d330b597aDouglas Gregor                                          PEnd = Params->end();
5343e1274f2b99cb99c03cc8e2c6517c37d330b597aDouglas Gregor       P != PEnd; ++P) {
5353e1274f2b99cb99c03cc8e2c6517c37d330b597aDouglas Gregor    if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(*P)) {
5363e1274f2b99cb99c03cc8e2c6517c37d330b597aDouglas Gregor      ID.AddInteger(0);
5373e1274f2b99cb99c03cc8e2c6517c37d330b597aDouglas Gregor      ID.AddBoolean(TTP->isParameterPack());
5383e1274f2b99cb99c03cc8e2c6517c37d330b597aDouglas Gregor      continue;
5393e1274f2b99cb99c03cc8e2c6517c37d330b597aDouglas Gregor    }
5403e1274f2b99cb99c03cc8e2c6517c37d330b597aDouglas Gregor
5413e1274f2b99cb99c03cc8e2c6517c37d330b597aDouglas Gregor    if (NonTypeTemplateParmDecl *NTTP = dyn_cast<NonTypeTemplateParmDecl>(*P)) {
5423e1274f2b99cb99c03cc8e2c6517c37d330b597aDouglas Gregor      ID.AddInteger(1);
54361c4d28e36cd3f1be392cb77f07436d1fa6b0f9fDouglas Gregor      ID.AddBoolean(NTTP->isParameterPack());
5449e9c454b12671a624f666fc6fbf132fdf183effcEli Friedman      ID.AddPointer(NTTP->getType().getCanonicalType().getAsOpaquePtr());
5456952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor      if (NTTP->isExpandedParameterPack()) {
5466952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor        ID.AddBoolean(true);
5476952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor        ID.AddInteger(NTTP->getNumExpansionTypes());
5489e9c454b12671a624f666fc6fbf132fdf183effcEli Friedman        for (unsigned I = 0, N = NTTP->getNumExpansionTypes(); I != N; ++I) {
5499e9c454b12671a624f666fc6fbf132fdf183effcEli Friedman          QualType T = NTTP->getExpansionType(I);
5509e9c454b12671a624f666fc6fbf132fdf183effcEli Friedman          ID.AddPointer(T.getCanonicalType().getAsOpaquePtr());
5519e9c454b12671a624f666fc6fbf132fdf183effcEli Friedman        }
5526952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor      } else
5536952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor        ID.AddBoolean(false);
5543e1274f2b99cb99c03cc8e2c6517c37d330b597aDouglas Gregor      continue;
5553e1274f2b99cb99c03cc8e2c6517c37d330b597aDouglas Gregor    }
5563e1274f2b99cb99c03cc8e2c6517c37d330b597aDouglas Gregor
5573e1274f2b99cb99c03cc8e2c6517c37d330b597aDouglas Gregor    TemplateTemplateParmDecl *TTP = cast<TemplateTemplateParmDecl>(*P);
5583e1274f2b99cb99c03cc8e2c6517c37d330b597aDouglas Gregor    ID.AddInteger(2);
5593e1274f2b99cb99c03cc8e2c6517c37d330b597aDouglas Gregor    Profile(ID, TTP);
5603e1274f2b99cb99c03cc8e2c6517c37d330b597aDouglas Gregor  }
5613e1274f2b99cb99c03cc8e2c6517c37d330b597aDouglas Gregor}
5623e1274f2b99cb99c03cc8e2c6517c37d330b597aDouglas Gregor
5633e1274f2b99cb99c03cc8e2c6517c37d330b597aDouglas GregorTemplateTemplateParmDecl *
5643e1274f2b99cb99c03cc8e2c6517c37d330b597aDouglas GregorASTContext::getCanonicalTemplateTemplateParmDecl(
5654ba2a17694148e16eaa8d3917f657ffcd3667be4Jay Foad                                          TemplateTemplateParmDecl *TTP) const {
5663e1274f2b99cb99c03cc8e2c6517c37d330b597aDouglas Gregor  // Check if we already have a canonical template template parameter.
5673e1274f2b99cb99c03cc8e2c6517c37d330b597aDouglas Gregor  llvm::FoldingSetNodeID ID;
5683e1274f2b99cb99c03cc8e2c6517c37d330b597aDouglas Gregor  CanonicalTemplateTemplateParm::Profile(ID, TTP);
5693e1274f2b99cb99c03cc8e2c6517c37d330b597aDouglas Gregor  void *InsertPos = 0;
5703e1274f2b99cb99c03cc8e2c6517c37d330b597aDouglas Gregor  CanonicalTemplateTemplateParm *Canonical
5713e1274f2b99cb99c03cc8e2c6517c37d330b597aDouglas Gregor    = CanonTemplateTemplateParms.FindNodeOrInsertPos(ID, InsertPos);
5723e1274f2b99cb99c03cc8e2c6517c37d330b597aDouglas Gregor  if (Canonical)
5733e1274f2b99cb99c03cc8e2c6517c37d330b597aDouglas Gregor    return Canonical->getParam();
5743e1274f2b99cb99c03cc8e2c6517c37d330b597aDouglas Gregor
5753e1274f2b99cb99c03cc8e2c6517c37d330b597aDouglas Gregor  // Build a canonical template parameter list.
5763e1274f2b99cb99c03cc8e2c6517c37d330b597aDouglas Gregor  TemplateParameterList *Params = TTP->getTemplateParameters();
5775f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner  SmallVector<NamedDecl *, 4> CanonParams;
5783e1274f2b99cb99c03cc8e2c6517c37d330b597aDouglas Gregor  CanonParams.reserve(Params->size());
5793e1274f2b99cb99c03cc8e2c6517c37d330b597aDouglas Gregor  for (TemplateParameterList::const_iterator P = Params->begin(),
5803e1274f2b99cb99c03cc8e2c6517c37d330b597aDouglas Gregor                                          PEnd = Params->end();
5813e1274f2b99cb99c03cc8e2c6517c37d330b597aDouglas Gregor       P != PEnd; ++P) {
5823e1274f2b99cb99c03cc8e2c6517c37d330b597aDouglas Gregor    if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(*P))
5833e1274f2b99cb99c03cc8e2c6517c37d330b597aDouglas Gregor      CanonParams.push_back(
5843e1274f2b99cb99c03cc8e2c6517c37d330b597aDouglas Gregor                  TemplateTypeParmDecl::Create(*this, getTranslationUnitDecl(),
585344577e6b58f42d18dc8118c8903b49a85dc005eAbramo Bagnara                                               SourceLocation(),
586344577e6b58f42d18dc8118c8903b49a85dc005eAbramo Bagnara                                               SourceLocation(),
587344577e6b58f42d18dc8118c8903b49a85dc005eAbramo Bagnara                                               TTP->getDepth(),
5883e1274f2b99cb99c03cc8e2c6517c37d330b597aDouglas Gregor                                               TTP->getIndex(), 0, false,
5893e1274f2b99cb99c03cc8e2c6517c37d330b597aDouglas Gregor                                               TTP->isParameterPack()));
5903e1274f2b99cb99c03cc8e2c6517c37d330b597aDouglas Gregor    else if (NonTypeTemplateParmDecl *NTTP
5916952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor             = dyn_cast<NonTypeTemplateParmDecl>(*P)) {
5926952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor      QualType T = getCanonicalType(NTTP->getType());
5936952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor      TypeSourceInfo *TInfo = getTrivialTypeSourceInfo(T);
5946952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor      NonTypeTemplateParmDecl *Param;
5956952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor      if (NTTP->isExpandedParameterPack()) {
5965f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner        SmallVector<QualType, 2> ExpandedTypes;
5975f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner        SmallVector<TypeSourceInfo *, 2> ExpandedTInfos;
5986952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor        for (unsigned I = 0, N = NTTP->getNumExpansionTypes(); I != N; ++I) {
5996952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor          ExpandedTypes.push_back(getCanonicalType(NTTP->getExpansionType(I)));
6006952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor          ExpandedTInfos.push_back(
6016952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor                                getTrivialTypeSourceInfo(ExpandedTypes.back()));
6026952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor        }
6036952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor
6046952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor        Param = NonTypeTemplateParmDecl::Create(*this, getTranslationUnitDecl(),
605ff676cb48fe8bf7be2feaa251dc7c5fb15af4730Abramo Bagnara                                                SourceLocation(),
606ff676cb48fe8bf7be2feaa251dc7c5fb15af4730Abramo Bagnara                                                SourceLocation(),
6076952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor                                                NTTP->getDepth(),
6086952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor                                                NTTP->getPosition(), 0,
6096952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor                                                T,
6106952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor                                                TInfo,
6116952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor                                                ExpandedTypes.data(),
6126952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor                                                ExpandedTypes.size(),
6136952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor                                                ExpandedTInfos.data());
6146952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor      } else {
6156952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor        Param = NonTypeTemplateParmDecl::Create(*this, getTranslationUnitDecl(),
616ff676cb48fe8bf7be2feaa251dc7c5fb15af4730Abramo Bagnara                                                SourceLocation(),
617ff676cb48fe8bf7be2feaa251dc7c5fb15af4730Abramo Bagnara                                                SourceLocation(),
6186952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor                                                NTTP->getDepth(),
6196952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor                                                NTTP->getPosition(), 0,
6206952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor                                                T,
6216952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor                                                NTTP->isParameterPack(),
6226952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor                                                TInfo);
6236952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor      }
6246952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor      CanonParams.push_back(Param);
6256952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor
6266952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor    } else
6273e1274f2b99cb99c03cc8e2c6517c37d330b597aDouglas Gregor      CanonParams.push_back(getCanonicalTemplateTemplateParmDecl(
6283e1274f2b99cb99c03cc8e2c6517c37d330b597aDouglas Gregor                                           cast<TemplateTemplateParmDecl>(*P)));
6293e1274f2b99cb99c03cc8e2c6517c37d330b597aDouglas Gregor  }
6303e1274f2b99cb99c03cc8e2c6517c37d330b597aDouglas Gregor
6313e1274f2b99cb99c03cc8e2c6517c37d330b597aDouglas Gregor  TemplateTemplateParmDecl *CanonTTP
6323e1274f2b99cb99c03cc8e2c6517c37d330b597aDouglas Gregor    = TemplateTemplateParmDecl::Create(*this, getTranslationUnitDecl(),
6333e1274f2b99cb99c03cc8e2c6517c37d330b597aDouglas Gregor                                       SourceLocation(), TTP->getDepth(),
63461c4d28e36cd3f1be392cb77f07436d1fa6b0f9fDouglas Gregor                                       TTP->getPosition(),
63561c4d28e36cd3f1be392cb77f07436d1fa6b0f9fDouglas Gregor                                       TTP->isParameterPack(),
63661c4d28e36cd3f1be392cb77f07436d1fa6b0f9fDouglas Gregor                                       0,
6373e1274f2b99cb99c03cc8e2c6517c37d330b597aDouglas Gregor                         TemplateParameterList::Create(*this, SourceLocation(),
6383e1274f2b99cb99c03cc8e2c6517c37d330b597aDouglas Gregor                                                       SourceLocation(),
6393e1274f2b99cb99c03cc8e2c6517c37d330b597aDouglas Gregor                                                       CanonParams.data(),
6403e1274f2b99cb99c03cc8e2c6517c37d330b597aDouglas Gregor                                                       CanonParams.size(),
6413e1274f2b99cb99c03cc8e2c6517c37d330b597aDouglas Gregor                                                       SourceLocation()));
6423e1274f2b99cb99c03cc8e2c6517c37d330b597aDouglas Gregor
6433e1274f2b99cb99c03cc8e2c6517c37d330b597aDouglas Gregor  // Get the new insert position for the node we care about.
6443e1274f2b99cb99c03cc8e2c6517c37d330b597aDouglas Gregor  Canonical = CanonTemplateTemplateParms.FindNodeOrInsertPos(ID, InsertPos);
6453e1274f2b99cb99c03cc8e2c6517c37d330b597aDouglas Gregor  assert(Canonical == 0 && "Shouldn't be in the map!");
6463e1274f2b99cb99c03cc8e2c6517c37d330b597aDouglas Gregor  (void)Canonical;
6473e1274f2b99cb99c03cc8e2c6517c37d330b597aDouglas Gregor
6483e1274f2b99cb99c03cc8e2c6517c37d330b597aDouglas Gregor  // Create the canonical template template parameter entry.
6493e1274f2b99cb99c03cc8e2c6517c37d330b597aDouglas Gregor  Canonical = new (*this) CanonicalTemplateTemplateParm(CanonTTP);
6503e1274f2b99cb99c03cc8e2c6517c37d330b597aDouglas Gregor  CanonTemplateTemplateParms.InsertNode(Canonical, InsertPos);
6513e1274f2b99cb99c03cc8e2c6517c37d330b597aDouglas Gregor  return CanonTTP;
6523e1274f2b99cb99c03cc8e2c6517c37d330b597aDouglas Gregor}
6533e1274f2b99cb99c03cc8e2c6517c37d330b597aDouglas Gregor
654071cc7deffad608165b1ddd5263e8bf181861520Charles DavisCXXABI *ASTContext::createCXXABI(const TargetInfo &T) {
655ee79a4c30e5d1c5285551c9a25b8ec6d45d46aa7John McCall  if (!LangOpts.CPlusPlus) return 0;
656ee79a4c30e5d1c5285551c9a25b8ec6d45d46aa7John McCall
657b8b2c9da87e7d70a1679db026f40548b3192b705John McCall  switch (T.getCXXABI().getKind()) {
658b8b2c9da87e7d70a1679db026f40548b3192b705John McCall  case TargetCXXABI::GenericARM:
659b8b2c9da87e7d70a1679db026f40548b3192b705John McCall  case TargetCXXABI::iOS:
660ee79a4c30e5d1c5285551c9a25b8ec6d45d46aa7John McCall    return CreateARMCXXABI(*this);
661c264e16a42b3f6c36521857a29ea0949d9781c22Tim Northover  case TargetCXXABI::GenericAArch64: // Same as Itanium at this level
662b8b2c9da87e7d70a1679db026f40548b3192b705John McCall  case TargetCXXABI::GenericItanium:
663071cc7deffad608165b1ddd5263e8bf181861520Charles Davis    return CreateItaniumCXXABI(*this);
664b8b2c9da87e7d70a1679db026f40548b3192b705John McCall  case TargetCXXABI::Microsoft:
66520cf717034ba1f20fc47c025ecb72ed9b631ad13Charles Davis    return CreateMicrosoftCXXABI(*this);
66620cf717034ba1f20fc47c025ecb72ed9b631ad13Charles Davis  }
6677530c034c0c71a64c5a9173206d9742ae847af8bDavid Blaikie  llvm_unreachable("Invalid CXXABI type!");
668071cc7deffad608165b1ddd5263e8bf181861520Charles Davis}
669071cc7deffad608165b1ddd5263e8bf181861520Charles Davis
670bcfd1f55bfbb3e5944cd5e03d07b343e280838c4Douglas Gregorstatic const LangAS::Map *getAddressSpaceMap(const TargetInfo &T,
671207f4d8543529221932af82836016a2ef066c917Peter Collingbourne                                             const LangOptions &LOpts) {
672207f4d8543529221932af82836016a2ef066c917Peter Collingbourne  if (LOpts.FakeAddressSpaceMap) {
673207f4d8543529221932af82836016a2ef066c917Peter Collingbourne    // The fake address space map must have a distinct entry for each
674207f4d8543529221932af82836016a2ef066c917Peter Collingbourne    // language-specific address space.
675207f4d8543529221932af82836016a2ef066c917Peter Collingbourne    static const unsigned FakeAddrSpaceMap[] = {
676207f4d8543529221932af82836016a2ef066c917Peter Collingbourne      1, // opencl_global
677207f4d8543529221932af82836016a2ef066c917Peter Collingbourne      2, // opencl_local
6784dc34ebf2a0716bf77ba110dab6777a3fc4397ddPeter Collingbourne      3, // opencl_constant
6794dc34ebf2a0716bf77ba110dab6777a3fc4397ddPeter Collingbourne      4, // cuda_device
6804dc34ebf2a0716bf77ba110dab6777a3fc4397ddPeter Collingbourne      5, // cuda_constant
6814dc34ebf2a0716bf77ba110dab6777a3fc4397ddPeter Collingbourne      6  // cuda_shared
682207f4d8543529221932af82836016a2ef066c917Peter Collingbourne    };
683bcfd1f55bfbb3e5944cd5e03d07b343e280838c4Douglas Gregor    return &FakeAddrSpaceMap;
684207f4d8543529221932af82836016a2ef066c917Peter Collingbourne  } else {
685bcfd1f55bfbb3e5944cd5e03d07b343e280838c4Douglas Gregor    return &T.getAddressSpaceMap();
686207f4d8543529221932af82836016a2ef066c917Peter Collingbourne  }
687207f4d8543529221932af82836016a2ef066c917Peter Collingbourne}
688207f4d8543529221932af82836016a2ef066c917Peter Collingbourne
6893e3cd93b2fd9644e970c389e715c13883faf68b6Douglas GregorASTContext::ASTContext(LangOptions& LOpts, SourceManager &SM,
690bcfd1f55bfbb3e5944cd5e03d07b343e280838c4Douglas Gregor                       const TargetInfo *t,
691e91593ef084479340582b2ba177b44be50a717b7Daniel Dunbar                       IdentifierTable &idents, SelectorTable &sels,
6921b63e4f732dbc73d90abf886b4d21f8e3a165f6dChris Lattner                       Builtin::Context &builtins,
693bcfd1f55bfbb3e5944cd5e03d07b343e280838c4Douglas Gregor                       unsigned size_reserve,
694bcfd1f55bfbb3e5944cd5e03d07b343e280838c4Douglas Gregor                       bool DelayInitialization)
695bcfd1f55bfbb3e5944cd5e03d07b343e280838c4Douglas Gregor  : FunctionProtoTypes(this_()),
696bcfd1f55bfbb3e5944cd5e03d07b343e280838c4Douglas Gregor    TemplateSpecializationTypes(this_()),
697bcfd1f55bfbb3e5944cd5e03d07b343e280838c4Douglas Gregor    DependentTemplateSpecializationTypes(this_()),
698bcfd1f55bfbb3e5944cd5e03d07b343e280838c4Douglas Gregor    SubstTemplateTemplateParmPacks(this_()),
699bcfd1f55bfbb3e5944cd5e03d07b343e280838c4Douglas Gregor    GlobalNestedNameSpecifier(0),
700bcfd1f55bfbb3e5944cd5e03d07b343e280838c4Douglas Gregor    Int128Decl(0), UInt128Decl(0),
701c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge    BuiltinVaListDecl(0),
702a6ea10e22b600d92e084f6b11b9b9a92d0eb2412Douglas Gregor    ObjCIdDecl(0), ObjCSelDecl(0), ObjCClassDecl(0), ObjCProtocolClassDecl(0),
703961713055e636170da59d7006a878cb4ba518a5dFariborz Jahanian    BOOLDecl(0),
704e97179c675b341927807c718be215c8d1aab8acbDouglas Gregor    CFConstantStringTypeDecl(0), ObjCInstanceTypeDecl(0),
705bcfd1f55bfbb3e5944cd5e03d07b343e280838c4Douglas Gregor    FILEDecl(0),
706e2d4f4ed44a32f179c6d48cd1dba8346ab2129d9Rafael Espindola    jmp_bufDecl(0), sigjmp_bufDecl(0), ucontext_tDecl(0),
707e2d4f4ed44a32f179c6d48cd1dba8346ab2129d9Rafael Espindola    BlockDescriptorType(0), BlockDescriptorExtendedType(0),
708e2d4f4ed44a32f179c6d48cd1dba8346ab2129d9Rafael Espindola    cudaConfigureCallDecl(0),
709e664977aca2a05a77abab5a06dc0fb69e870cfb9Douglas Gregor    NullTypeSourceInfo(QualType()),
710e664977aca2a05a77abab5a06dc0fb69e870cfb9Douglas Gregor    FirstLocalImport(), LastLocalImport(),
711bcfd1f55bfbb3e5944cd5e03d07b343e280838c4Douglas Gregor    SourceMgr(SM), LangOpts(LOpts),
71230c42404202d2e2512e51efc6066bd614cfdb5a4Douglas Gregor    AddrSpaceMap(0), Target(t), PrintingPolicy(LOpts),
713bcfd1f55bfbb3e5944cd5e03d07b343e280838c4Douglas Gregor    Idents(idents), Selectors(sels),
714bcfd1f55bfbb3e5944cd5e03d07b343e280838c4Douglas Gregor    BuiltinInfo(builtins),
715bcfd1f55bfbb3e5944cd5e03d07b343e280838c4Douglas Gregor    DeclarationNames(*this),
71630c42404202d2e2512e51efc6066bd614cfdb5a4Douglas Gregor    ExternalSource(0), Listener(0),
717aa0cd85838f2a024e589ea4e8c2094130065af21Dmitri Gribenko    Comments(SM), CommentsLoaded(false),
7186ebf09130479bc7605aa09a3e6c4dc2ba3513495Dmitri Gribenko    CommentCommandTraits(BumpAlloc, LOpts.CommentOpts),
719bcfd1f55bfbb3e5944cd5e03d07b343e280838c4Douglas Gregor    LastSDM(0, 0),
720f7992132207d7ddc8810a135cec97ebe5805e500Fariborz Jahanian    UniqueBlockByRefTypeID(0)
721bcfd1f55bfbb3e5944cd5e03d07b343e280838c4Douglas Gregor{
7221eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  if (size_reserve > 0) Types.reserve(size_reserve);
723e91593ef084479340582b2ba177b44be50a717b7Daniel Dunbar  TUDecl = TranslationUnitDecl::Create(*this);
724bcfd1f55bfbb3e5944cd5e03d07b343e280838c4Douglas Gregor
725bcfd1f55bfbb3e5944cd5e03d07b343e280838c4Douglas Gregor  if (!DelayInitialization) {
726bcfd1f55bfbb3e5944cd5e03d07b343e280838c4Douglas Gregor    assert(t && "No target supplied for ASTContext initialization");
727bcfd1f55bfbb3e5944cd5e03d07b343e280838c4Douglas Gregor    InitBuiltinTypes(*t);
728bcfd1f55bfbb3e5944cd5e03d07b343e280838c4Douglas Gregor  }
729e91593ef084479340582b2ba177b44be50a717b7Daniel Dunbar}
730e91593ef084479340582b2ba177b44be50a717b7Daniel Dunbar
7315f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid SpencerASTContext::~ASTContext() {
7323478eb6872d836600caf45b0f81c2065d685d6e0Ted Kremenek  // Release the DenseMaps associated with DeclContext objects.
7333478eb6872d836600caf45b0f81c2065d685d6e0Ted Kremenek  // FIXME: Is this the ideal solution?
7343478eb6872d836600caf45b0f81c2065d685d6e0Ted Kremenek  ReleaseDeclContextMaps();
7357d10b7eb670b821741b4c96f6cf7afbc3bb39abeDouglas Gregor
73663fe86bee66fc145942c56b2cc564ea0b9b9ea12Douglas Gregor  // Call all of the deallocation functions.
73763fe86bee66fc145942c56b2cc564ea0b9b9ea12Douglas Gregor  for (unsigned I = 0, N = Deallocations.size(); I != N; ++I)
73863fe86bee66fc145942c56b2cc564ea0b9b9ea12Douglas Gregor    Deallocations[I].first(Deallocations[I].second);
7390054531488928a424666ac11fcdc6bcc5112de52Douglas Gregor
740dcfcfbec478f7ed96cd8d92f30c29bd4e30d5b9cTed Kremenek  // ASTRecordLayout objects in ASTRecordLayouts must always be destroyed
74163fe86bee66fc145942c56b2cc564ea0b9b9ea12Douglas Gregor  // because they can contain DenseMaps.
74263fe86bee66fc145942c56b2cc564ea0b9b9ea12Douglas Gregor  for (llvm::DenseMap<const ObjCContainerDecl*,
74363fe86bee66fc145942c56b2cc564ea0b9b9ea12Douglas Gregor       const ASTRecordLayout*>::iterator
74463fe86bee66fc145942c56b2cc564ea0b9b9ea12Douglas Gregor       I = ObjCLayouts.begin(), E = ObjCLayouts.end(); I != E; )
74563fe86bee66fc145942c56b2cc564ea0b9b9ea12Douglas Gregor    // Increment in loop to prevent using deallocated memory.
74663fe86bee66fc145942c56b2cc564ea0b9b9ea12Douglas Gregor    if (ASTRecordLayout *R = const_cast<ASTRecordLayout*>((I++)->second))
74763fe86bee66fc145942c56b2cc564ea0b9b9ea12Douglas Gregor      R->Destroy(*this);
74863fe86bee66fc145942c56b2cc564ea0b9b9ea12Douglas Gregor
749dcfcfbec478f7ed96cd8d92f30c29bd4e30d5b9cTed Kremenek  for (llvm::DenseMap<const RecordDecl*, const ASTRecordLayout*>::iterator
750dcfcfbec478f7ed96cd8d92f30c29bd4e30d5b9cTed Kremenek       I = ASTRecordLayouts.begin(), E = ASTRecordLayouts.end(); I != E; ) {
751dcfcfbec478f7ed96cd8d92f30c29bd4e30d5b9cTed Kremenek    // Increment in loop to prevent using deallocated memory.
752dcfcfbec478f7ed96cd8d92f30c29bd4e30d5b9cTed Kremenek    if (ASTRecordLayout *R = const_cast<ASTRecordLayout*>((I++)->second))
753dcfcfbec478f7ed96cd8d92f30c29bd4e30d5b9cTed Kremenek      R->Destroy(*this);
754dcfcfbec478f7ed96cd8d92f30c29bd4e30d5b9cTed Kremenek  }
7556320064d0c60fa8683f5623881c9394fd4aa7689Douglas Gregor
7566320064d0c60fa8683f5623881c9394fd4aa7689Douglas Gregor  for (llvm::DenseMap<const Decl*, AttrVec*>::iterator A = DeclAttrs.begin(),
7576320064d0c60fa8683f5623881c9394fd4aa7689Douglas Gregor                                                    AEnd = DeclAttrs.end();
7586320064d0c60fa8683f5623881c9394fd4aa7689Douglas Gregor       A != AEnd; ++A)
7596320064d0c60fa8683f5623881c9394fd4aa7689Douglas Gregor    A->second->~AttrVec();
7606320064d0c60fa8683f5623881c9394fd4aa7689Douglas Gregor}
761ab452ba8323d1985e08bade2bced588cddf2cc28Douglas Gregor
7620054531488928a424666ac11fcdc6bcc5112de52Douglas Gregorvoid ASTContext::AddDeallocation(void (*Callback)(void*), void *Data) {
7630054531488928a424666ac11fcdc6bcc5112de52Douglas Gregor  Deallocations.push_back(std::make_pair(Callback, Data));
7640054531488928a424666ac11fcdc6bcc5112de52Douglas Gregor}
7650054531488928a424666ac11fcdc6bcc5112de52Douglas Gregor
7661eb4433ac451dc16f4133a88af2d002ac26c58efMike Stumpvoid
7676f42b62b6194f53bcbc349f5d17388e1936535d7Dylan NoblesmithASTContext::setExternalSource(OwningPtr<ExternalASTSource> &Source) {
7682cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor  ExternalSource.reset(Source.take());
7692cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor}
7702cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor
7715f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencervoid ASTContext::PrintStats() const {
772cd92a65edc7cbbbb7e3aee8d31008594de90fa51Chandler Carruth  llvm::errs() << "\n*** AST Context Stats:\n";
773cd92a65edc7cbbbb7e3aee8d31008594de90fa51Chandler Carruth  llvm::errs() << "  " << Types.size() << " types total.\n";
7747c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl
775dbe833da54e1b6192991b64fc453cd50b5ee7909Douglas Gregor  unsigned counts[] = {
7761eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump#define TYPE(Name, Parent) 0,
777dbe833da54e1b6192991b64fc453cd50b5ee7909Douglas Gregor#define ABSTRACT_TYPE(Name, Parent)
778dbe833da54e1b6192991b64fc453cd50b5ee7909Douglas Gregor#include "clang/AST/TypeNodes.def"
779dbe833da54e1b6192991b64fc453cd50b5ee7909Douglas Gregor    0 // Extra
780dbe833da54e1b6192991b64fc453cd50b5ee7909Douglas Gregor  };
781c2ee10d79f70036af652a395ac1f8273f3d04e12Douglas Gregor
7825f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  for (unsigned i = 0, e = Types.size(); i != e; ++i) {
7835f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    Type *T = Types[i];
784dbe833da54e1b6192991b64fc453cd50b5ee7909Douglas Gregor    counts[(unsigned)T->getTypeClass()]++;
7855f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
7865f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
787dbe833da54e1b6192991b64fc453cd50b5ee7909Douglas Gregor  unsigned Idx = 0;
788dbe833da54e1b6192991b64fc453cd50b5ee7909Douglas Gregor  unsigned TotalBytes = 0;
789dbe833da54e1b6192991b64fc453cd50b5ee7909Douglas Gregor#define TYPE(Name, Parent)                                              \
790dbe833da54e1b6192991b64fc453cd50b5ee7909Douglas Gregor  if (counts[Idx])                                                      \
791cd92a65edc7cbbbb7e3aee8d31008594de90fa51Chandler Carruth    llvm::errs() << "    " << counts[Idx] << " " << #Name               \
792cd92a65edc7cbbbb7e3aee8d31008594de90fa51Chandler Carruth                 << " types\n";                                         \
793dbe833da54e1b6192991b64fc453cd50b5ee7909Douglas Gregor  TotalBytes += counts[Idx] * sizeof(Name##Type);                       \
794dbe833da54e1b6192991b64fc453cd50b5ee7909Douglas Gregor  ++Idx;
795dbe833da54e1b6192991b64fc453cd50b5ee7909Douglas Gregor#define ABSTRACT_TYPE(Name, Parent)
796dbe833da54e1b6192991b64fc453cd50b5ee7909Douglas Gregor#include "clang/AST/TypeNodes.def"
7971eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
798cd92a65edc7cbbbb7e3aee8d31008594de90fa51Chandler Carruth  llvm::errs() << "Total bytes = " << TotalBytes << "\n";
799cd92a65edc7cbbbb7e3aee8d31008594de90fa51Chandler Carruth
8004923aa25eb39d64106a5817c02d560a3aecf8b2cDouglas Gregor  // Implicit special member functions.
801cd92a65edc7cbbbb7e3aee8d31008594de90fa51Chandler Carruth  llvm::errs() << NumImplicitDefaultConstructorsDeclared << "/"
802cd92a65edc7cbbbb7e3aee8d31008594de90fa51Chandler Carruth               << NumImplicitDefaultConstructors
803cd92a65edc7cbbbb7e3aee8d31008594de90fa51Chandler Carruth               << " implicit default constructors created\n";
804cd92a65edc7cbbbb7e3aee8d31008594de90fa51Chandler Carruth  llvm::errs() << NumImplicitCopyConstructorsDeclared << "/"
805cd92a65edc7cbbbb7e3aee8d31008594de90fa51Chandler Carruth               << NumImplicitCopyConstructors
806cd92a65edc7cbbbb7e3aee8d31008594de90fa51Chandler Carruth               << " implicit copy constructors created\n";
8074e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie  if (getLangOpts().CPlusPlus)
808cd92a65edc7cbbbb7e3aee8d31008594de90fa51Chandler Carruth    llvm::errs() << NumImplicitMoveConstructorsDeclared << "/"
809cd92a65edc7cbbbb7e3aee8d31008594de90fa51Chandler Carruth                 << NumImplicitMoveConstructors
810cd92a65edc7cbbbb7e3aee8d31008594de90fa51Chandler Carruth                 << " implicit move constructors created\n";
811cd92a65edc7cbbbb7e3aee8d31008594de90fa51Chandler Carruth  llvm::errs() << NumImplicitCopyAssignmentOperatorsDeclared << "/"
812cd92a65edc7cbbbb7e3aee8d31008594de90fa51Chandler Carruth               << NumImplicitCopyAssignmentOperators
813cd92a65edc7cbbbb7e3aee8d31008594de90fa51Chandler Carruth               << " implicit copy assignment operators created\n";
8144e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie  if (getLangOpts().CPlusPlus)
815cd92a65edc7cbbbb7e3aee8d31008594de90fa51Chandler Carruth    llvm::errs() << NumImplicitMoveAssignmentOperatorsDeclared << "/"
816cd92a65edc7cbbbb7e3aee8d31008594de90fa51Chandler Carruth                 << NumImplicitMoveAssignmentOperators
817cd92a65edc7cbbbb7e3aee8d31008594de90fa51Chandler Carruth                 << " implicit move assignment operators created\n";
818cd92a65edc7cbbbb7e3aee8d31008594de90fa51Chandler Carruth  llvm::errs() << NumImplicitDestructorsDeclared << "/"
819cd92a65edc7cbbbb7e3aee8d31008594de90fa51Chandler Carruth               << NumImplicitDestructors
820cd92a65edc7cbbbb7e3aee8d31008594de90fa51Chandler Carruth               << " implicit destructors created\n";
821cd92a65edc7cbbbb7e3aee8d31008594de90fa51Chandler Carruth
8222cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor  if (ExternalSource.get()) {
823cd92a65edc7cbbbb7e3aee8d31008594de90fa51Chandler Carruth    llvm::errs() << "\n";
8242cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor    ExternalSource->PrintStats();
8252cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor  }
826cd92a65edc7cbbbb7e3aee8d31008594de90fa51Chandler Carruth
82763fe86bee66fc145942c56b2cc564ea0b9b9ea12Douglas Gregor  BumpAlloc.PrintStats();
8285f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
8295f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
830772eeaefef2c883aabe35caf4543e7e32d290183Douglas GregorTypedefDecl *ASTContext::getInt128Decl() const {
831772eeaefef2c883aabe35caf4543e7e32d290183Douglas Gregor  if (!Int128Decl) {
832772eeaefef2c883aabe35caf4543e7e32d290183Douglas Gregor    TypeSourceInfo *TInfo = getTrivialTypeSourceInfo(Int128Ty);
833772eeaefef2c883aabe35caf4543e7e32d290183Douglas Gregor    Int128Decl = TypedefDecl::Create(const_cast<ASTContext &>(*this),
834772eeaefef2c883aabe35caf4543e7e32d290183Douglas Gregor                                     getTranslationUnitDecl(),
835772eeaefef2c883aabe35caf4543e7e32d290183Douglas Gregor                                     SourceLocation(),
836772eeaefef2c883aabe35caf4543e7e32d290183Douglas Gregor                                     SourceLocation(),
837772eeaefef2c883aabe35caf4543e7e32d290183Douglas Gregor                                     &Idents.get("__int128_t"),
838772eeaefef2c883aabe35caf4543e7e32d290183Douglas Gregor                                     TInfo);
839772eeaefef2c883aabe35caf4543e7e32d290183Douglas Gregor  }
840772eeaefef2c883aabe35caf4543e7e32d290183Douglas Gregor
841772eeaefef2c883aabe35caf4543e7e32d290183Douglas Gregor  return Int128Decl;
842772eeaefef2c883aabe35caf4543e7e32d290183Douglas Gregor}
843772eeaefef2c883aabe35caf4543e7e32d290183Douglas Gregor
844772eeaefef2c883aabe35caf4543e7e32d290183Douglas GregorTypedefDecl *ASTContext::getUInt128Decl() const {
845772eeaefef2c883aabe35caf4543e7e32d290183Douglas Gregor  if (!UInt128Decl) {
846772eeaefef2c883aabe35caf4543e7e32d290183Douglas Gregor    TypeSourceInfo *TInfo = getTrivialTypeSourceInfo(UnsignedInt128Ty);
847772eeaefef2c883aabe35caf4543e7e32d290183Douglas Gregor    UInt128Decl = TypedefDecl::Create(const_cast<ASTContext &>(*this),
848772eeaefef2c883aabe35caf4543e7e32d290183Douglas Gregor                                     getTranslationUnitDecl(),
849772eeaefef2c883aabe35caf4543e7e32d290183Douglas Gregor                                     SourceLocation(),
850772eeaefef2c883aabe35caf4543e7e32d290183Douglas Gregor                                     SourceLocation(),
851772eeaefef2c883aabe35caf4543e7e32d290183Douglas Gregor                                     &Idents.get("__uint128_t"),
852772eeaefef2c883aabe35caf4543e7e32d290183Douglas Gregor                                     TInfo);
853772eeaefef2c883aabe35caf4543e7e32d290183Douglas Gregor  }
854772eeaefef2c883aabe35caf4543e7e32d290183Douglas Gregor
855772eeaefef2c883aabe35caf4543e7e32d290183Douglas Gregor  return UInt128Decl;
856772eeaefef2c883aabe35caf4543e7e32d290183Douglas Gregor}
8575f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
858e27ec8ad56dbf1efb2de004b90fbbb86f740e3f1John McCallvoid ASTContext::InitBuiltinType(CanQualType &R, BuiltinType::Kind K) {
8596b304a0254a13f42390b865ff5ba668a49cc58aeJohn McCall  BuiltinType *Ty = new (*this, TypeAlignment) BuiltinType(K);
860e27ec8ad56dbf1efb2de004b90fbbb86f740e3f1John McCall  R = CanQualType::CreateUnsafe(QualType(Ty, 0));
8616b304a0254a13f42390b865ff5ba668a49cc58aeJohn McCall  Types.push_back(Ty);
8625f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
8635f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
864bcfd1f55bfbb3e5944cd5e03d07b343e280838c4Douglas Gregorvoid ASTContext::InitBuiltinTypes(const TargetInfo &Target) {
865bcfd1f55bfbb3e5944cd5e03d07b343e280838c4Douglas Gregor  assert((!this->Target || this->Target == &Target) &&
866bcfd1f55bfbb3e5944cd5e03d07b343e280838c4Douglas Gregor         "Incorrect target reinitialization");
8675f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  assert(VoidTy.isNull() && "Context reinitialized?");
8681eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
869bcfd1f55bfbb3e5944cd5e03d07b343e280838c4Douglas Gregor  this->Target = &Target;
870bcfd1f55bfbb3e5944cd5e03d07b343e280838c4Douglas Gregor
871bcfd1f55bfbb3e5944cd5e03d07b343e280838c4Douglas Gregor  ABI.reset(createCXXABI(Target));
872bcfd1f55bfbb3e5944cd5e03d07b343e280838c4Douglas Gregor  AddrSpaceMap = getAddressSpaceMap(Target, LangOpts);
873bcfd1f55bfbb3e5944cd5e03d07b343e280838c4Douglas Gregor
8745f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // C99 6.2.5p19.
8755f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  InitBuiltinType(VoidTy,              BuiltinType::Void);
8761eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
8775f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // C99 6.2.5p2.
8785f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  InitBuiltinType(BoolTy,              BuiltinType::Bool);
8795f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // C99 6.2.5p3.
88015b91764d08e886391c865c4a444d7b51141c284Eli Friedman  if (LangOpts.CharIsSigned)
8815f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    InitBuiltinType(CharTy,            BuiltinType::Char_S);
8825f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  else
8835f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    InitBuiltinType(CharTy,            BuiltinType::Char_U);
8845f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // C99 6.2.5p4.
8855f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  InitBuiltinType(SignedCharTy,        BuiltinType::SChar);
8865f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  InitBuiltinType(ShortTy,             BuiltinType::Short);
8875f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  InitBuiltinType(IntTy,               BuiltinType::Int);
8885f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  InitBuiltinType(LongTy,              BuiltinType::Long);
8895f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  InitBuiltinType(LongLongTy,          BuiltinType::LongLong);
8901eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
8915f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // C99 6.2.5p6.
8925f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  InitBuiltinType(UnsignedCharTy,      BuiltinType::UChar);
8935f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  InitBuiltinType(UnsignedShortTy,     BuiltinType::UShort);
8945f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  InitBuiltinType(UnsignedIntTy,       BuiltinType::UInt);
8955f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  InitBuiltinType(UnsignedLongTy,      BuiltinType::ULong);
8965f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  InitBuiltinType(UnsignedLongLongTy,  BuiltinType::ULongLong);
8971eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
8985f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // C99 6.2.5p10.
8995f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  InitBuiltinType(FloatTy,             BuiltinType::Float);
9005f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  InitBuiltinType(DoubleTy,            BuiltinType::Double);
9015f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  InitBuiltinType(LongDoubleTy,        BuiltinType::LongDouble);
90264c438a4be2a871fa43c78264663ba1e9788b94dArgyrios Kyrtzidis
9032df9ced9fd1e8c7d7b38443db07e0e811de22571Chris Lattner  // GNU extension, 128-bit integers.
9042df9ced9fd1e8c7d7b38443db07e0e811de22571Chris Lattner  InitBuiltinType(Int128Ty,            BuiltinType::Int128);
9052df9ced9fd1e8c7d7b38443db07e0e811de22571Chris Lattner  InitBuiltinType(UnsignedInt128Ty,    BuiltinType::UInt128);
9062df9ced9fd1e8c7d7b38443db07e0e811de22571Chris Lattner
90715f92bad58c8650b1306729744b1a1230197497aHans Wennborg  // C++ 3.9.1p5
90815f92bad58c8650b1306729744b1a1230197497aHans Wennborg  if (TargetInfo::isTypeSigned(Target.getWCharType()))
90915f92bad58c8650b1306729744b1a1230197497aHans Wennborg    InitBuiltinType(WCharTy,           BuiltinType::WChar_S);
91015f92bad58c8650b1306729744b1a1230197497aHans Wennborg  else  // -fshort-wchar makes wchar_t be unsigned.
91115f92bad58c8650b1306729744b1a1230197497aHans Wennborg    InitBuiltinType(WCharTy,           BuiltinType::WChar_U);
91215f92bad58c8650b1306729744b1a1230197497aHans Wennborg  if (LangOpts.CPlusPlus && LangOpts.WChar)
91315f92bad58c8650b1306729744b1a1230197497aHans Wennborg    WideCharTy = WCharTy;
91415f92bad58c8650b1306729744b1a1230197497aHans Wennborg  else {
91515f92bad58c8650b1306729744b1a1230197497aHans Wennborg    // C99 (or C++ using -fno-wchar).
91615f92bad58c8650b1306729744b1a1230197497aHans Wennborg    WideCharTy = getFromTargetType(Target.getWCharType());
91715f92bad58c8650b1306729744b1a1230197497aHans Wennborg  }
91864c438a4be2a871fa43c78264663ba1e9788b94dArgyrios Kyrtzidis
919392da48160bd92ceb486792780467cbfdb2d0e8cJames Molloy  WIntTy = getFromTargetType(Target.getWIntType());
920392da48160bd92ceb486792780467cbfdb2d0e8cJames Molloy
921f5c209d23b20ada4a9b6235db50317239cbf6ae1Alisdair Meredith  if (LangOpts.CPlusPlus) // C++0x 3.9.1p5, extension for C++
922f5c209d23b20ada4a9b6235db50317239cbf6ae1Alisdair Meredith    InitBuiltinType(Char16Ty,           BuiltinType::Char16);
923f5c209d23b20ada4a9b6235db50317239cbf6ae1Alisdair Meredith  else // C99
924f5c209d23b20ada4a9b6235db50317239cbf6ae1Alisdair Meredith    Char16Ty = getFromTargetType(Target.getChar16Type());
925f5c209d23b20ada4a9b6235db50317239cbf6ae1Alisdair Meredith
926f5c209d23b20ada4a9b6235db50317239cbf6ae1Alisdair Meredith  if (LangOpts.CPlusPlus) // C++0x 3.9.1p5, extension for C++
927f5c209d23b20ada4a9b6235db50317239cbf6ae1Alisdair Meredith    InitBuiltinType(Char32Ty,           BuiltinType::Char32);
928f5c209d23b20ada4a9b6235db50317239cbf6ae1Alisdair Meredith  else // C99
929f5c209d23b20ada4a9b6235db50317239cbf6ae1Alisdair Meredith    Char32Ty = getFromTargetType(Target.getChar32Type());
930f5c209d23b20ada4a9b6235db50317239cbf6ae1Alisdair Meredith
931898574e7496ba8fd76290079d3a9d06954992734Douglas Gregor  // Placeholder type for type-dependent expressions whose type is
932898574e7496ba8fd76290079d3a9d06954992734Douglas Gregor  // completely unknown. No code should ever check a type against
933898574e7496ba8fd76290079d3a9d06954992734Douglas Gregor  // DependentTy and users should never see it; however, it is here to
934898574e7496ba8fd76290079d3a9d06954992734Douglas Gregor  // help diagnose failures to properly check for type-dependent
935898574e7496ba8fd76290079d3a9d06954992734Douglas Gregor  // expressions.
936898574e7496ba8fd76290079d3a9d06954992734Douglas Gregor  InitBuiltinType(DependentTy,         BuiltinType::Dependent);
9378e9bebdea69c590dedfbf27374114cb76fe12fbdDouglas Gregor
9382a984cad5ac3fdceeff2bd99daa7b90979313475John McCall  // Placeholder type for functions.
9392a984cad5ac3fdceeff2bd99daa7b90979313475John McCall  InitBuiltinType(OverloadTy,          BuiltinType::Overload);
9402a984cad5ac3fdceeff2bd99daa7b90979313475John McCall
941864c041e118155c2b1ce0ba36942a3da5a4a055eJohn McCall  // Placeholder type for bound members.
942864c041e118155c2b1ce0ba36942a3da5a4a055eJohn McCall  InitBuiltinType(BoundMemberTy,       BuiltinType::BoundMember);
943864c041e118155c2b1ce0ba36942a3da5a4a055eJohn McCall
9443c3b7f90a863af43fa63043d396553ecf205351cJohn McCall  // Placeholder type for pseudo-objects.
9453c3b7f90a863af43fa63043d396553ecf205351cJohn McCall  InitBuiltinType(PseudoObjectTy,      BuiltinType::PseudoObject);
9463c3b7f90a863af43fa63043d396553ecf205351cJohn McCall
9471de4d4e8cb2e9c88809fea8092bc6e835a5473d2John McCall  // "any" type; useful for debugger-like clients.
9481de4d4e8cb2e9c88809fea8092bc6e835a5473d2John McCall  InitBuiltinType(UnknownAnyTy,        BuiltinType::UnknownAny);
9491de4d4e8cb2e9c88809fea8092bc6e835a5473d2John McCall
9500ddaeb9b031070ec64afe92d9892875ac44df427John McCall  // Placeholder type for unbridged ARC casts.
9510ddaeb9b031070ec64afe92d9892875ac44df427John McCall  InitBuiltinType(ARCUnbridgedCastTy,  BuiltinType::ARCUnbridgedCast);
9520ddaeb9b031070ec64afe92d9892875ac44df427John McCall
953a6c66cedc022c9e5d45a937d6b8cff491a6bf81bEli Friedman  // Placeholder type for builtin functions.
954a6c66cedc022c9e5d45a937d6b8cff491a6bf81bEli Friedman  InitBuiltinType(BuiltinFnTy,  BuiltinType::BuiltinFn);
955a6c66cedc022c9e5d45a937d6b8cff491a6bf81bEli Friedman
9565f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // C99 6.2.5p11.
9575f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  FloatComplexTy      = getComplexType(FloatTy);
9585f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  DoubleComplexTy     = getComplexType(DoubleTy);
9595f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  LongDoubleComplexTy = getComplexType(LongDoubleTy);
9608e9bebdea69c590dedfbf27374114cb76fe12fbdDouglas Gregor
96113dcd00615de5c4279d97bdf63cd5f0a14fd9dccFariborz Jahanian  // Builtin types for 'id', 'Class', and 'SEL'.
962de2e22d33afec98324a66a358dfe0951b3c7259aSteve Naroff  InitBuiltinType(ObjCBuiltinIdTy, BuiltinType::ObjCId);
963de2e22d33afec98324a66a358dfe0951b3c7259aSteve Naroff  InitBuiltinType(ObjCBuiltinClassTy, BuiltinType::ObjCClass);
96413dcd00615de5c4279d97bdf63cd5f0a14fd9dccFariborz Jahanian  InitBuiltinType(ObjCBuiltinSelTy, BuiltinType::ObjCSel);
965b13621d08e20ac7aa550e05896de8a57ee99c1e8Guy Benyei
966b13621d08e20ac7aa550e05896de8a57ee99c1e8Guy Benyei  if (LangOpts.OpenCL) {
967b13621d08e20ac7aa550e05896de8a57ee99c1e8Guy Benyei    InitBuiltinType(OCLImage1dTy, BuiltinType::OCLImage1d);
968b13621d08e20ac7aa550e05896de8a57ee99c1e8Guy Benyei    InitBuiltinType(OCLImage1dArrayTy, BuiltinType::OCLImage1dArray);
969b13621d08e20ac7aa550e05896de8a57ee99c1e8Guy Benyei    InitBuiltinType(OCLImage1dBufferTy, BuiltinType::OCLImage1dBuffer);
970b13621d08e20ac7aa550e05896de8a57ee99c1e8Guy Benyei    InitBuiltinType(OCLImage2dTy, BuiltinType::OCLImage2d);
971b13621d08e20ac7aa550e05896de8a57ee99c1e8Guy Benyei    InitBuiltinType(OCLImage2dArrayTy, BuiltinType::OCLImage2dArray);
972b13621d08e20ac7aa550e05896de8a57ee99c1e8Guy Benyei    InitBuiltinType(OCLImage3dTy, BuiltinType::OCLImage3d);
973e6b9d802fb7b16d93474c4f1c179ab36202e8a8bGuy Benyei
97421f18c4fda167dc5f72feddbd6a7ac1b63200a0dGuy Benyei    InitBuiltinType(OCLSamplerTy, BuiltinType::OCLSampler);
975e6b9d802fb7b16d93474c4f1c179ab36202e8a8bGuy Benyei    InitBuiltinType(OCLEventTy, BuiltinType::OCLEvent);
976b13621d08e20ac7aa550e05896de8a57ee99c1e8Guy Benyei  }
977ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek
978ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek  // Builtin type for __objc_yes and __objc_no
97993a49944e0e68e32bc22d45d44ee136b34beffb3Fariborz Jahanian  ObjCBuiltinBoolTy = (Target.useSignedCharForObjCBool() ?
98093a49944e0e68e32bc22d45d44ee136b34beffb3Fariborz Jahanian                       SignedCharTy : BoolTy);
981ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek
982a526c5c67e5a0473c340903ee542ce570119665fTed Kremenek  ObjCConstantStringType = QualType();
983f7992132207d7ddc8810a135cec97ebe5805e500Fariborz Jahanian
984f7992132207d7ddc8810a135cec97ebe5805e500Fariborz Jahanian  ObjCSuperType = QualType();
9851eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
98633e1d64ab5cd5d27f8530ccd056191fe2c9f3f2eFariborz Jahanian  // void * type
98733e1d64ab5cd5d27f8530ccd056191fe2c9f3f2eFariborz Jahanian  VoidPtrTy = getPointerType(VoidTy);
9886e8ed16ffef02b82995a90bdcf10ffff7d63839aSebastian Redl
9896e8ed16ffef02b82995a90bdcf10ffff7d63839aSebastian Redl  // nullptr type (C++0x 2.14.7)
9906e8ed16ffef02b82995a90bdcf10ffff7d63839aSebastian Redl  InitBuiltinType(NullPtrTy,           BuiltinType::NullPtr);
991aa4a99b4a62615db243f7a5c433169f2fc704420Anton Korobeynikov
992aa4a99b4a62615db243f7a5c433169f2fc704420Anton Korobeynikov  // half type (OpenCL 6.1.1.1) / ARM NEON __fp16
993aa4a99b4a62615db243f7a5c433169f2fc704420Anton Korobeynikov  InitBuiltinType(HalfTy, BuiltinType::Half);
994fb40e3f10ccef93c4f8fb6bd4fe5a108fa6cd369Meador Inge
995fb40e3f10ccef93c4f8fb6bd4fe5a108fa6cd369Meador Inge  // Builtin type used to help define __builtin_va_list.
996fb40e3f10ccef93c4f8fb6bd4fe5a108fa6cd369Meador Inge  VaListTagTy = QualType();
9975f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
9985f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
999d6471f7c1921c7802804ce3ff6fe9768310f72b9David BlaikieDiagnosticsEngine &ASTContext::getDiagnostics() const {
100078a916ec5ff5b66adec3c499e1b9af7b87668309Argyrios Kyrtzidis  return SourceMgr.getDiagnostics();
100178a916ec5ff5b66adec3c499e1b9af7b87668309Argyrios Kyrtzidis}
100278a916ec5ff5b66adec3c499e1b9af7b87668309Argyrios Kyrtzidis
10036320064d0c60fa8683f5623881c9394fd4aa7689Douglas GregorAttrVec& ASTContext::getDeclAttrs(const Decl *D) {
10046320064d0c60fa8683f5623881c9394fd4aa7689Douglas Gregor  AttrVec *&Result = DeclAttrs[D];
10056320064d0c60fa8683f5623881c9394fd4aa7689Douglas Gregor  if (!Result) {
10066320064d0c60fa8683f5623881c9394fd4aa7689Douglas Gregor    void *Mem = Allocate(sizeof(AttrVec));
10076320064d0c60fa8683f5623881c9394fd4aa7689Douglas Gregor    Result = new (Mem) AttrVec;
10086320064d0c60fa8683f5623881c9394fd4aa7689Douglas Gregor  }
10096320064d0c60fa8683f5623881c9394fd4aa7689Douglas Gregor
10106320064d0c60fa8683f5623881c9394fd4aa7689Douglas Gregor  return *Result;
10116320064d0c60fa8683f5623881c9394fd4aa7689Douglas Gregor}
10126320064d0c60fa8683f5623881c9394fd4aa7689Douglas Gregor
10136320064d0c60fa8683f5623881c9394fd4aa7689Douglas Gregor/// \brief Erase the attributes corresponding to the given declaration.
10146320064d0c60fa8683f5623881c9394fd4aa7689Douglas Gregorvoid ASTContext::eraseDeclAttrs(const Decl *D) {
10156320064d0c60fa8683f5623881c9394fd4aa7689Douglas Gregor  llvm::DenseMap<const Decl*, AttrVec*>::iterator Pos = DeclAttrs.find(D);
10166320064d0c60fa8683f5623881c9394fd4aa7689Douglas Gregor  if (Pos != DeclAttrs.end()) {
10176320064d0c60fa8683f5623881c9394fd4aa7689Douglas Gregor    Pos->second->~AttrVec();
10186320064d0c60fa8683f5623881c9394fd4aa7689Douglas Gregor    DeclAttrs.erase(Pos);
10196320064d0c60fa8683f5623881c9394fd4aa7689Douglas Gregor  }
10206320064d0c60fa8683f5623881c9394fd4aa7689Douglas Gregor}
10216320064d0c60fa8683f5623881c9394fd4aa7689Douglas Gregor
1022251b4ff2578e26959a4c036140ccd61c5e9292f2Douglas GregorMemberSpecializationInfo *
1023663b5a0be7261c29bc4c526a71cffcfa02d4153eDouglas GregorASTContext::getInstantiatedFromStaticDataMember(const VarDecl *Var) {
10247caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  assert(Var->isStaticDataMember() && "Not a static data member");
1025663b5a0be7261c29bc4c526a71cffcfa02d4153eDouglas Gregor  llvm::DenseMap<const VarDecl *, MemberSpecializationInfo *>::iterator Pos
10267caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor    = InstantiatedFromStaticDataMember.find(Var);
10277caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  if (Pos == InstantiatedFromStaticDataMember.end())
10287caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor    return 0;
10291eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
10307caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  return Pos->second;
10317caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor}
10327caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor
10331eb4433ac451dc16f4133a88af2d002ac26c58efMike Stumpvoid
1034251b4ff2578e26959a4c036140ccd61c5e9292f2Douglas GregorASTContext::setInstantiatedFromStaticDataMember(VarDecl *Inst, VarDecl *Tmpl,
10359421adc43891e272156fab640e5d5ee5054b779cArgyrios Kyrtzidis                                                TemplateSpecializationKind TSK,
10369421adc43891e272156fab640e5d5ee5054b779cArgyrios Kyrtzidis                                          SourceLocation PointOfInstantiation) {
10377caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  assert(Inst->isStaticDataMember() && "Not a static data member");
10387caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  assert(Tmpl->isStaticDataMember() && "Not a static data member");
10397caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  assert(!InstantiatedFromStaticDataMember[Inst] &&
10407caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor         "Already noted what static data member was instantiated from");
1041251b4ff2578e26959a4c036140ccd61c5e9292f2Douglas Gregor  InstantiatedFromStaticDataMember[Inst]
10429421adc43891e272156fab640e5d5ee5054b779cArgyrios Kyrtzidis    = new (*this) MemberSpecializationInfo(Tmpl, TSK, PointOfInstantiation);
10437caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor}
10447caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor
1045af0f4d0b2e38c810effc8b024ad2fb6604eec5d3Francois PichetFunctionDecl *ASTContext::getClassScopeSpecializationPattern(
1046af0f4d0b2e38c810effc8b024ad2fb6604eec5d3Francois Pichet                                                     const FunctionDecl *FD){
1047af0f4d0b2e38c810effc8b024ad2fb6604eec5d3Francois Pichet  assert(FD && "Specialization is 0");
1048af0f4d0b2e38c810effc8b024ad2fb6604eec5d3Francois Pichet  llvm::DenseMap<const FunctionDecl*, FunctionDecl *>::const_iterator Pos
10490d95f0d7b81110f77e99e833f766d19be7b7e072Francois Pichet    = ClassScopeSpecializationPattern.find(FD);
10500d95f0d7b81110f77e99e833f766d19be7b7e072Francois Pichet  if (Pos == ClassScopeSpecializationPattern.end())
1051af0f4d0b2e38c810effc8b024ad2fb6604eec5d3Francois Pichet    return 0;
1052af0f4d0b2e38c810effc8b024ad2fb6604eec5d3Francois Pichet
1053af0f4d0b2e38c810effc8b024ad2fb6604eec5d3Francois Pichet  return Pos->second;
1054af0f4d0b2e38c810effc8b024ad2fb6604eec5d3Francois Pichet}
1055af0f4d0b2e38c810effc8b024ad2fb6604eec5d3Francois Pichet
1056af0f4d0b2e38c810effc8b024ad2fb6604eec5d3Francois Pichetvoid ASTContext::setClassScopeSpecializationPattern(FunctionDecl *FD,
1057af0f4d0b2e38c810effc8b024ad2fb6604eec5d3Francois Pichet                                        FunctionDecl *Pattern) {
1058af0f4d0b2e38c810effc8b024ad2fb6604eec5d3Francois Pichet  assert(FD && "Specialization is 0");
1059af0f4d0b2e38c810effc8b024ad2fb6604eec5d3Francois Pichet  assert(Pattern && "Class scope specialization pattern is 0");
10600d95f0d7b81110f77e99e833f766d19be7b7e072Francois Pichet  ClassScopeSpecializationPattern[FD] = Pattern;
1061af0f4d0b2e38c810effc8b024ad2fb6604eec5d3Francois Pichet}
1062af0f4d0b2e38c810effc8b024ad2fb6604eec5d3Francois Pichet
10637ba107a1863ddfa1664555854f0d7bdb3c491c92John McCallNamedDecl *
1064ed97649e9574b9d854fa4d6109c9333ae0993554John McCallASTContext::getInstantiatedFromUsingDecl(UsingDecl *UUD) {
10657ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall  llvm::DenseMap<UsingDecl *, NamedDecl *>::const_iterator Pos
1066ed97649e9574b9d854fa4d6109c9333ae0993554John McCall    = InstantiatedFromUsingDecl.find(UUD);
1067ed97649e9574b9d854fa4d6109c9333ae0993554John McCall  if (Pos == InstantiatedFromUsingDecl.end())
10680d8df780aef1acda5962347a32591efc629b6748Anders Carlsson    return 0;
10691eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
10700d8df780aef1acda5962347a32591efc629b6748Anders Carlsson  return Pos->second;
10710d8df780aef1acda5962347a32591efc629b6748Anders Carlsson}
10720d8df780aef1acda5962347a32591efc629b6748Anders Carlsson
10730d8df780aef1acda5962347a32591efc629b6748Anders Carlssonvoid
1074ed97649e9574b9d854fa4d6109c9333ae0993554John McCallASTContext::setInstantiatedFromUsingDecl(UsingDecl *Inst, NamedDecl *Pattern) {
1075ed97649e9574b9d854fa4d6109c9333ae0993554John McCall  assert((isa<UsingDecl>(Pattern) ||
1076ed97649e9574b9d854fa4d6109c9333ae0993554John McCall          isa<UnresolvedUsingValueDecl>(Pattern) ||
1077ed97649e9574b9d854fa4d6109c9333ae0993554John McCall          isa<UnresolvedUsingTypenameDecl>(Pattern)) &&
1078ed97649e9574b9d854fa4d6109c9333ae0993554John McCall         "pattern decl is not a using decl");
1079ed97649e9574b9d854fa4d6109c9333ae0993554John McCall  assert(!InstantiatedFromUsingDecl[Inst] && "pattern already exists");
1080ed97649e9574b9d854fa4d6109c9333ae0993554John McCall  InstantiatedFromUsingDecl[Inst] = Pattern;
1081ed97649e9574b9d854fa4d6109c9333ae0993554John McCall}
1082ed97649e9574b9d854fa4d6109c9333ae0993554John McCall
1083ed97649e9574b9d854fa4d6109c9333ae0993554John McCallUsingShadowDecl *
1084ed97649e9574b9d854fa4d6109c9333ae0993554John McCallASTContext::getInstantiatedFromUsingShadowDecl(UsingShadowDecl *Inst) {
1085ed97649e9574b9d854fa4d6109c9333ae0993554John McCall  llvm::DenseMap<UsingShadowDecl*, UsingShadowDecl*>::const_iterator Pos
1086ed97649e9574b9d854fa4d6109c9333ae0993554John McCall    = InstantiatedFromUsingShadowDecl.find(Inst);
1087ed97649e9574b9d854fa4d6109c9333ae0993554John McCall  if (Pos == InstantiatedFromUsingShadowDecl.end())
1088ed97649e9574b9d854fa4d6109c9333ae0993554John McCall    return 0;
1089ed97649e9574b9d854fa4d6109c9333ae0993554John McCall
1090ed97649e9574b9d854fa4d6109c9333ae0993554John McCall  return Pos->second;
1091ed97649e9574b9d854fa4d6109c9333ae0993554John McCall}
1092ed97649e9574b9d854fa4d6109c9333ae0993554John McCall
1093ed97649e9574b9d854fa4d6109c9333ae0993554John McCallvoid
1094ed97649e9574b9d854fa4d6109c9333ae0993554John McCallASTContext::setInstantiatedFromUsingShadowDecl(UsingShadowDecl *Inst,
1095ed97649e9574b9d854fa4d6109c9333ae0993554John McCall                                               UsingShadowDecl *Pattern) {
1096ed97649e9574b9d854fa4d6109c9333ae0993554John McCall  assert(!InstantiatedFromUsingShadowDecl[Inst] && "pattern already exists");
1097ed97649e9574b9d854fa4d6109c9333ae0993554John McCall  InstantiatedFromUsingShadowDecl[Inst] = Pattern;
10980d8df780aef1acda5962347a32591efc629b6748Anders Carlsson}
10990d8df780aef1acda5962347a32591efc629b6748Anders Carlsson
1100d8b285fee4471f393da8ee30f552ceacdc362afaAnders CarlssonFieldDecl *ASTContext::getInstantiatedFromUnnamedFieldDecl(FieldDecl *Field) {
1101d8b285fee4471f393da8ee30f552ceacdc362afaAnders Carlsson  llvm::DenseMap<FieldDecl *, FieldDecl *>::iterator Pos
1102d8b285fee4471f393da8ee30f552ceacdc362afaAnders Carlsson    = InstantiatedFromUnnamedFieldDecl.find(Field);
1103d8b285fee4471f393da8ee30f552ceacdc362afaAnders Carlsson  if (Pos == InstantiatedFromUnnamedFieldDecl.end())
1104d8b285fee4471f393da8ee30f552ceacdc362afaAnders Carlsson    return 0;
11051eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1106d8b285fee4471f393da8ee30f552ceacdc362afaAnders Carlsson  return Pos->second;
1107d8b285fee4471f393da8ee30f552ceacdc362afaAnders Carlsson}
1108d8b285fee4471f393da8ee30f552ceacdc362afaAnders Carlsson
1109d8b285fee4471f393da8ee30f552ceacdc362afaAnders Carlssonvoid ASTContext::setInstantiatedFromUnnamedFieldDecl(FieldDecl *Inst,
1110d8b285fee4471f393da8ee30f552ceacdc362afaAnders Carlsson                                                     FieldDecl *Tmpl) {
1111d8b285fee4471f393da8ee30f552ceacdc362afaAnders Carlsson  assert(!Inst->getDeclName() && "Instantiated field decl is not unnamed");
1112d8b285fee4471f393da8ee30f552ceacdc362afaAnders Carlsson  assert(!Tmpl->getDeclName() && "Template field decl is not unnamed");
1113d8b285fee4471f393da8ee30f552ceacdc362afaAnders Carlsson  assert(!InstantiatedFromUnnamedFieldDecl[Inst] &&
1114d8b285fee4471f393da8ee30f552ceacdc362afaAnders Carlsson         "Already noted what unnamed field was instantiated from");
11151eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1116d8b285fee4471f393da8ee30f552ceacdc362afaAnders Carlsson  InstantiatedFromUnnamedFieldDecl[Inst] = Tmpl;
1117d8b285fee4471f393da8ee30f552ceacdc362afaAnders Carlsson}
1118d8b285fee4471f393da8ee30f552ceacdc362afaAnders Carlsson
111914d56ef43ff4921c6749f7340212fbb743fdbb9bFariborz Jahanianbool ASTContext::ZeroBitfieldFollowsNonBitfield(const FieldDecl *FD,
112014d56ef43ff4921c6749f7340212fbb743fdbb9bFariborz Jahanian                                    const FieldDecl *LastFD) const {
112114d56ef43ff4921c6749f7340212fbb743fdbb9bFariborz Jahanian  return (FD->isBitField() && LastFD && !LastFD->isBitField() &&
1122a6b8b2c09610b8bc4330e948ece8b940c2386406Richard Smith          FD->getBitWidthValue(*this) == 0);
112314d56ef43ff4921c6749f7340212fbb743fdbb9bFariborz Jahanian}
112414d56ef43ff4921c6749f7340212fbb743fdbb9bFariborz Jahanian
1125340fa242130c2d8d74c83edca0952e771aebe0e6Fariborz Jahanianbool ASTContext::ZeroBitfieldFollowsBitfield(const FieldDecl *FD,
1126340fa242130c2d8d74c83edca0952e771aebe0e6Fariborz Jahanian                                             const FieldDecl *LastFD) const {
1127340fa242130c2d8d74c83edca0952e771aebe0e6Fariborz Jahanian  return (FD->isBitField() && LastFD && LastFD->isBitField() &&
1128a6b8b2c09610b8bc4330e948ece8b940c2386406Richard Smith          FD->getBitWidthValue(*this) == 0 &&
1129a6b8b2c09610b8bc4330e948ece8b940c2386406Richard Smith          LastFD->getBitWidthValue(*this) != 0);
1130340fa242130c2d8d74c83edca0952e771aebe0e6Fariborz Jahanian}
1131340fa242130c2d8d74c83edca0952e771aebe0e6Fariborz Jahanian
11329b3acaa32548d0ce78b9c39a3911397f6738a47cFariborz Jahanianbool ASTContext::BitfieldFollowsBitfield(const FieldDecl *FD,
11339b3acaa32548d0ce78b9c39a3911397f6738a47cFariborz Jahanian                                         const FieldDecl *LastFD) const {
11349b3acaa32548d0ce78b9c39a3911397f6738a47cFariborz Jahanian  return (FD->isBitField() && LastFD && LastFD->isBitField() &&
1135a6b8b2c09610b8bc4330e948ece8b940c2386406Richard Smith          FD->getBitWidthValue(*this) &&
1136a6b8b2c09610b8bc4330e948ece8b940c2386406Richard Smith          LastFD->getBitWidthValue(*this));
11379b3acaa32548d0ce78b9c39a3911397f6738a47cFariborz Jahanian}
11389b3acaa32548d0ce78b9c39a3911397f6738a47cFariborz Jahanian
1139dd7fddb5b6883326e52b278a9b7e9cefea29aae0Chad Rosierbool ASTContext::NonBitfieldFollowsBitfield(const FieldDecl *FD,
114052bbe7a1133c3cb57e9246f1b96c12940ea3821aFariborz Jahanian                                         const FieldDecl *LastFD) const {
114152bbe7a1133c3cb57e9246f1b96c12940ea3821aFariborz Jahanian  return (!FD->isBitField() && LastFD && LastFD->isBitField() &&
1142a6b8b2c09610b8bc4330e948ece8b940c2386406Richard Smith          LastFD->getBitWidthValue(*this));
114352bbe7a1133c3cb57e9246f1b96c12940ea3821aFariborz Jahanian}
114452bbe7a1133c3cb57e9246f1b96c12940ea3821aFariborz Jahanian
1145dd7fddb5b6883326e52b278a9b7e9cefea29aae0Chad Rosierbool ASTContext::BitfieldFollowsNonBitfield(const FieldDecl *FD,
114652bbe7a1133c3cb57e9246f1b96c12940ea3821aFariborz Jahanian                                             const FieldDecl *LastFD) const {
114752bbe7a1133c3cb57e9246f1b96c12940ea3821aFariborz Jahanian  return (FD->isBitField() && LastFD && !LastFD->isBitField() &&
1148a6b8b2c09610b8bc4330e948ece8b940c2386406Richard Smith          FD->getBitWidthValue(*this));
114952bbe7a1133c3cb57e9246f1b96c12940ea3821aFariborz Jahanian}
115052bbe7a1133c3cb57e9246f1b96c12940ea3821aFariborz Jahanian
11517d10b7eb670b821741b4c96f6cf7afbc3bb39abeDouglas GregorASTContext::overridden_cxx_method_iterator
11527d10b7eb670b821741b4c96f6cf7afbc3bb39abeDouglas GregorASTContext::overridden_methods_begin(const CXXMethodDecl *Method) const {
11537d10b7eb670b821741b4c96f6cf7afbc3bb39abeDouglas Gregor  llvm::DenseMap<const CXXMethodDecl *, CXXMethodVector>::const_iterator Pos
115438eb1e161f602ee810dfb8a5a0d8462572f22689Argyrios Kyrtzidis    = OverriddenMethods.find(Method->getCanonicalDecl());
11557d10b7eb670b821741b4c96f6cf7afbc3bb39abeDouglas Gregor  if (Pos == OverriddenMethods.end())
11567d10b7eb670b821741b4c96f6cf7afbc3bb39abeDouglas Gregor    return 0;
11577d10b7eb670b821741b4c96f6cf7afbc3bb39abeDouglas Gregor
11587d10b7eb670b821741b4c96f6cf7afbc3bb39abeDouglas Gregor  return Pos->second.begin();
11597d10b7eb670b821741b4c96f6cf7afbc3bb39abeDouglas Gregor}
11607d10b7eb670b821741b4c96f6cf7afbc3bb39abeDouglas Gregor
11617d10b7eb670b821741b4c96f6cf7afbc3bb39abeDouglas GregorASTContext::overridden_cxx_method_iterator
11627d10b7eb670b821741b4c96f6cf7afbc3bb39abeDouglas GregorASTContext::overridden_methods_end(const CXXMethodDecl *Method) const {
11637d10b7eb670b821741b4c96f6cf7afbc3bb39abeDouglas Gregor  llvm::DenseMap<const CXXMethodDecl *, CXXMethodVector>::const_iterator Pos
116438eb1e161f602ee810dfb8a5a0d8462572f22689Argyrios Kyrtzidis    = OverriddenMethods.find(Method->getCanonicalDecl());
11657d10b7eb670b821741b4c96f6cf7afbc3bb39abeDouglas Gregor  if (Pos == OverriddenMethods.end())
11667d10b7eb670b821741b4c96f6cf7afbc3bb39abeDouglas Gregor    return 0;
11677d10b7eb670b821741b4c96f6cf7afbc3bb39abeDouglas Gregor
11687d10b7eb670b821741b4c96f6cf7afbc3bb39abeDouglas Gregor  return Pos->second.end();
11697d10b7eb670b821741b4c96f6cf7afbc3bb39abeDouglas Gregor}
11707d10b7eb670b821741b4c96f6cf7afbc3bb39abeDouglas Gregor
1171c91e9f439ae85d5f79a6b65672f1d7d1b55ccda0Argyrios Kyrtzidisunsigned
1172c91e9f439ae85d5f79a6b65672f1d7d1b55ccda0Argyrios KyrtzidisASTContext::overridden_methods_size(const CXXMethodDecl *Method) const {
1173c91e9f439ae85d5f79a6b65672f1d7d1b55ccda0Argyrios Kyrtzidis  llvm::DenseMap<const CXXMethodDecl *, CXXMethodVector>::const_iterator Pos
117438eb1e161f602ee810dfb8a5a0d8462572f22689Argyrios Kyrtzidis    = OverriddenMethods.find(Method->getCanonicalDecl());
1175c91e9f439ae85d5f79a6b65672f1d7d1b55ccda0Argyrios Kyrtzidis  if (Pos == OverriddenMethods.end())
1176c91e9f439ae85d5f79a6b65672f1d7d1b55ccda0Argyrios Kyrtzidis    return 0;
1177c91e9f439ae85d5f79a6b65672f1d7d1b55ccda0Argyrios Kyrtzidis
1178c91e9f439ae85d5f79a6b65672f1d7d1b55ccda0Argyrios Kyrtzidis  return Pos->second.size();
1179c91e9f439ae85d5f79a6b65672f1d7d1b55ccda0Argyrios Kyrtzidis}
1180c91e9f439ae85d5f79a6b65672f1d7d1b55ccda0Argyrios Kyrtzidis
11817d10b7eb670b821741b4c96f6cf7afbc3bb39abeDouglas Gregorvoid ASTContext::addOverriddenMethod(const CXXMethodDecl *Method,
11827d10b7eb670b821741b4c96f6cf7afbc3bb39abeDouglas Gregor                                     const CXXMethodDecl *Overridden) {
118338eb1e161f602ee810dfb8a5a0d8462572f22689Argyrios Kyrtzidis  assert(Method->isCanonicalDecl() && Overridden->isCanonicalDecl());
11847d10b7eb670b821741b4c96f6cf7afbc3bb39abeDouglas Gregor  OverriddenMethods[Method].push_back(Overridden);
11857d10b7eb670b821741b4c96f6cf7afbc3bb39abeDouglas Gregor}
11867d10b7eb670b821741b4c96f6cf7afbc3bb39abeDouglas Gregor
11871e905da74dbfd353d77dfc548fc9d6ff420d515aDmitri Gribenkovoid ASTContext::getOverriddenMethods(
11881e905da74dbfd353d77dfc548fc9d6ff420d515aDmitri Gribenko                      const NamedDecl *D,
11891e905da74dbfd353d77dfc548fc9d6ff420d515aDmitri Gribenko                      SmallVectorImpl<const NamedDecl *> &Overridden) const {
119021c3607282550779c9ae5fe784928597807fd110Argyrios Kyrtzidis  assert(D);
119121c3607282550779c9ae5fe784928597807fd110Argyrios Kyrtzidis
119221c3607282550779c9ae5fe784928597807fd110Argyrios Kyrtzidis  if (const CXXMethodDecl *CXXMethod = dyn_cast<CXXMethodDecl>(D)) {
1193685d10462b875f8c056d27488c0a1b4e13ef680fArgyrios Kyrtzidis    Overridden.append(overridden_methods_begin(CXXMethod),
1194685d10462b875f8c056d27488c0a1b4e13ef680fArgyrios Kyrtzidis                      overridden_methods_end(CXXMethod));
119521c3607282550779c9ae5fe784928597807fd110Argyrios Kyrtzidis    return;
119621c3607282550779c9ae5fe784928597807fd110Argyrios Kyrtzidis  }
119721c3607282550779c9ae5fe784928597807fd110Argyrios Kyrtzidis
119821c3607282550779c9ae5fe784928597807fd110Argyrios Kyrtzidis  const ObjCMethodDecl *Method = dyn_cast<ObjCMethodDecl>(D);
119921c3607282550779c9ae5fe784928597807fd110Argyrios Kyrtzidis  if (!Method)
120021c3607282550779c9ae5fe784928597807fd110Argyrios Kyrtzidis    return;
120121c3607282550779c9ae5fe784928597807fd110Argyrios Kyrtzidis
1202740ae67dbf8dac44dbc8d6593a60f4f37a0a2aa5Argyrios Kyrtzidis  SmallVector<const ObjCMethodDecl *, 8> OverDecls;
1203740ae67dbf8dac44dbc8d6593a60f4f37a0a2aa5Argyrios Kyrtzidis  Method->getOverriddenMethods(OverDecls);
1204bc0a2bb8dc122f8daae890ec82cecfe2054859ebArgyrios Kyrtzidis  Overridden.append(OverDecls.begin(), OverDecls.end());
120521c3607282550779c9ae5fe784928597807fd110Argyrios Kyrtzidis}
120621c3607282550779c9ae5fe784928597807fd110Argyrios Kyrtzidis
1207e664977aca2a05a77abab5a06dc0fb69e870cfb9Douglas Gregorvoid ASTContext::addedLocalImportDecl(ImportDecl *Import) {
1208e664977aca2a05a77abab5a06dc0fb69e870cfb9Douglas Gregor  assert(!Import->NextLocalImport && "Import declaration already in the chain");
1209e664977aca2a05a77abab5a06dc0fb69e870cfb9Douglas Gregor  assert(!Import->isFromASTFile() && "Non-local import declaration");
1210e664977aca2a05a77abab5a06dc0fb69e870cfb9Douglas Gregor  if (!FirstLocalImport) {
1211e664977aca2a05a77abab5a06dc0fb69e870cfb9Douglas Gregor    FirstLocalImport = Import;
1212e664977aca2a05a77abab5a06dc0fb69e870cfb9Douglas Gregor    LastLocalImport = Import;
1213e664977aca2a05a77abab5a06dc0fb69e870cfb9Douglas Gregor    return;
1214e664977aca2a05a77abab5a06dc0fb69e870cfb9Douglas Gregor  }
1215e664977aca2a05a77abab5a06dc0fb69e870cfb9Douglas Gregor
1216e664977aca2a05a77abab5a06dc0fb69e870cfb9Douglas Gregor  LastLocalImport->NextLocalImport = Import;
1217e664977aca2a05a77abab5a06dc0fb69e870cfb9Douglas Gregor  LastLocalImport = Import;
1218e664977aca2a05a77abab5a06dc0fb69e870cfb9Douglas Gregor}
1219e664977aca2a05a77abab5a06dc0fb69e870cfb9Douglas Gregor
1220464175bba1318bef7905122e9fda20cff926df78Chris Lattner//===----------------------------------------------------------------------===//
1221464175bba1318bef7905122e9fda20cff926df78Chris Lattner//                         Type Sizing and Analysis
1222464175bba1318bef7905122e9fda20cff926df78Chris Lattner//===----------------------------------------------------------------------===//
1223a7674d8a9a69f3f6fe16e70cf2a3b2b15fb7c43dChris Lattner
1224b7cfe88e88cb4f46308de89cf3f0c81bfe624128Chris Lattner/// getFloatTypeSemantics - Return the APFloat 'semantics' for the specified
1225b7cfe88e88cb4f46308de89cf3f0c81bfe624128Chris Lattner/// scalar floating point type.
1226b7cfe88e88cb4f46308de89cf3f0c81bfe624128Chris Lattnerconst llvm::fltSemantics &ASTContext::getFloatTypeSemantics(QualType T) const {
1227183700f494ec9b6701b6efe82bcb25f4c79ba561John McCall  const BuiltinType *BT = T->getAs<BuiltinType>();
1228b7cfe88e88cb4f46308de89cf3f0c81bfe624128Chris Lattner  assert(BT && "Not a floating point type!");
1229b7cfe88e88cb4f46308de89cf3f0c81bfe624128Chris Lattner  switch (BT->getKind()) {
1230b219cfc4d75f0a03630b7c4509ef791b7e97b2c8David Blaikie  default: llvm_unreachable("Not a floating point type!");
1231aa4a99b4a62615db243f7a5c433169f2fc704420Anton Korobeynikov  case BuiltinType::Half:       return Target->getHalfFormat();
1232bcfd1f55bfbb3e5944cd5e03d07b343e280838c4Douglas Gregor  case BuiltinType::Float:      return Target->getFloatFormat();
1233bcfd1f55bfbb3e5944cd5e03d07b343e280838c4Douglas Gregor  case BuiltinType::Double:     return Target->getDoubleFormat();
1234bcfd1f55bfbb3e5944cd5e03d07b343e280838c4Douglas Gregor  case BuiltinType::LongDouble: return Target->getLongDoubleFormat();
1235b7cfe88e88cb4f46308de89cf3f0c81bfe624128Chris Lattner  }
1236b7cfe88e88cb4f46308de89cf3f0c81bfe624128Chris Lattner}
1237b7cfe88e88cb4f46308de89cf3f0c81bfe624128Chris Lattner
12388b752f10c394b140f9ef89e049cbad1a7676fc25Ken Dyck/// getDeclAlign - Return a conservative estimate of the alignment of the
1239af707ab8fbb9451e8febb8d766f6c043628125c4Chris Lattner/// specified decl.  Note that bitfields do not have a valid alignment, so
1240af707ab8fbb9451e8febb8d766f6c043628125c4Chris Lattner/// this method will assert on them.
12415d484e8cf710207010720589d89602233de61d01Sebastian Redl/// If @p RefAsPointee, references are treated like their underlying type
12425d484e8cf710207010720589d89602233de61d01Sebastian Redl/// (for alignof), else they're treated like pointers (for CodeGen).
12434ba2a17694148e16eaa8d3917f657ffcd3667be4Jay FoadCharUnits ASTContext::getDeclAlign(const Decl *D, bool RefAsPointee) const {
1244bcfd1f55bfbb3e5944cd5e03d07b343e280838c4Douglas Gregor  unsigned Align = Target->getCharWidth();
1245dcdafb6a701aa9d81edcb088915f58933315dc05Eli Friedman
12464081a5c5f1381c4ec77f8ab3866693917e4329c4John McCall  bool UseAlignAttrOnly = false;
12474081a5c5f1381c4ec77f8ab3866693917e4329c4John McCall  if (unsigned AlignFromAttr = D->getMaxAlignment()) {
12484081a5c5f1381c4ec77f8ab3866693917e4329c4John McCall    Align = AlignFromAttr;
12494081a5c5f1381c4ec77f8ab3866693917e4329c4John McCall
12504081a5c5f1381c4ec77f8ab3866693917e4329c4John McCall    // __attribute__((aligned)) can increase or decrease alignment
12514081a5c5f1381c4ec77f8ab3866693917e4329c4John McCall    // *except* on a struct or struct member, where it only increases
12524081a5c5f1381c4ec77f8ab3866693917e4329c4John McCall    // alignment unless 'packed' is also specified.
12534081a5c5f1381c4ec77f8ab3866693917e4329c4John McCall    //
125482d0b0aab9088e977c2a44c4a5a90479c63149fePeter Collingbourne    // It is an error for alignas to decrease alignment, so we can
12554081a5c5f1381c4ec77f8ab3866693917e4329c4John McCall    // ignore that possibility;  Sema should diagnose it.
12564081a5c5f1381c4ec77f8ab3866693917e4329c4John McCall    if (isa<FieldDecl>(D)) {
12574081a5c5f1381c4ec77f8ab3866693917e4329c4John McCall      UseAlignAttrOnly = D->hasAttr<PackedAttr>() ||
12584081a5c5f1381c4ec77f8ab3866693917e4329c4John McCall        cast<FieldDecl>(D)->getParent()->hasAttr<PackedAttr>();
12594081a5c5f1381c4ec77f8ab3866693917e4329c4John McCall    } else {
12604081a5c5f1381c4ec77f8ab3866693917e4329c4John McCall      UseAlignAttrOnly = true;
12614081a5c5f1381c4ec77f8ab3866693917e4329c4John McCall    }
12624081a5c5f1381c4ec77f8ab3866693917e4329c4John McCall  }
126378a7d7d79964119a3f35b262eb154b5cbf1001edFariborz Jahanian  else if (isa<FieldDecl>(D))
126478a7d7d79964119a3f35b262eb154b5cbf1001edFariborz Jahanian      UseAlignAttrOnly =
126578a7d7d79964119a3f35b262eb154b5cbf1001edFariborz Jahanian        D->hasAttr<PackedAttr>() ||
126678a7d7d79964119a3f35b262eb154b5cbf1001edFariborz Jahanian        cast<FieldDecl>(D)->getParent()->hasAttr<PackedAttr>();
1267dcdafb6a701aa9d81edcb088915f58933315dc05Eli Friedman
1268ba4f5d5754c8291690d01ca9581926673d69b24cJohn McCall  // If we're using the align attribute only, just ignore everything
1269ba4f5d5754c8291690d01ca9581926673d69b24cJohn McCall  // else about the declaration and its type.
12704081a5c5f1381c4ec77f8ab3866693917e4329c4John McCall  if (UseAlignAttrOnly) {
1271ba4f5d5754c8291690d01ca9581926673d69b24cJohn McCall    // do nothing
1272ba4f5d5754c8291690d01ca9581926673d69b24cJohn McCall
12734081a5c5f1381c4ec77f8ab3866693917e4329c4John McCall  } else if (const ValueDecl *VD = dyn_cast<ValueDecl>(D)) {
1274af707ab8fbb9451e8febb8d766f6c043628125c4Chris Lattner    QualType T = VD->getType();
12756217b80b7a1379b74cced1c076338262c3c980b3Ted Kremenek    if (const ReferenceType* RT = T->getAs<ReferenceType>()) {
12765d484e8cf710207010720589d89602233de61d01Sebastian Redl      if (RefAsPointee)
12775d484e8cf710207010720589d89602233de61d01Sebastian Redl        T = RT->getPointeeType();
12785d484e8cf710207010720589d89602233de61d01Sebastian Redl      else
12795d484e8cf710207010720589d89602233de61d01Sebastian Redl        T = getPointerType(RT->getPointeeType());
12805d484e8cf710207010720589d89602233de61d01Sebastian Redl    }
12815d484e8cf710207010720589d89602233de61d01Sebastian Redl    if (!T->isIncompleteType() && !T->isFunctionType()) {
12823b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall      // Adjust alignments of declarations with array type by the
12833b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall      // large-array alignment on the target.
1284bcfd1f55bfbb3e5944cd5e03d07b343e280838c4Douglas Gregor      unsigned MinWidth = Target->getLargeArrayMinWidth();
12853b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall      const ArrayType *arrayType;
12863b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall      if (MinWidth && (arrayType = getAsArrayType(T))) {
12873b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall        if (isa<VariableArrayType>(arrayType))
1288bcfd1f55bfbb3e5944cd5e03d07b343e280838c4Douglas Gregor          Align = std::max(Align, Target->getLargeArrayAlign());
12893b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall        else if (isa<ConstantArrayType>(arrayType) &&
12903b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall                 MinWidth <= getTypeSize(cast<ConstantArrayType>(arrayType)))
1291bcfd1f55bfbb3e5944cd5e03d07b343e280838c4Douglas Gregor          Align = std::max(Align, Target->getLargeArrayAlign());
12923b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall
12933b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall        // Walk through any array types while we're at it.
12943b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall        T = getBaseElementType(arrayType);
12956deecb0d46bcfd048e651d2db7c4fb0d6407da96Rafael Espindola      }
12969f1210c3280104417a4ad30f0a00825ac8fa718aChad Rosier      Align = std::max(Align, getPreferredTypeAlign(T.getTypePtr()));
12976b20351a1d6178addfaa86716aaba36f2e9ea188Ulrich Weigand      if (const VarDecl *VD = dyn_cast<VarDecl>(D)) {
12986b20351a1d6178addfaa86716aaba36f2e9ea188Ulrich Weigand        if (VD->hasGlobalStorage())
12996b20351a1d6178addfaa86716aaba36f2e9ea188Ulrich Weigand          Align = std::max(Align, getTargetInfo().getMinGlobalAlign());
13006b20351a1d6178addfaa86716aaba36f2e9ea188Ulrich Weigand      }
1301dcdafb6a701aa9d81edcb088915f58933315dc05Eli Friedman    }
1302ba4f5d5754c8291690d01ca9581926673d69b24cJohn McCall
1303ba4f5d5754c8291690d01ca9581926673d69b24cJohn McCall    // Fields can be subject to extra alignment constraints, like if
1304ba4f5d5754c8291690d01ca9581926673d69b24cJohn McCall    // the field is packed, the struct is packed, or the struct has a
1305ba4f5d5754c8291690d01ca9581926673d69b24cJohn McCall    // a max-field-alignment constraint (#pragma pack).  So calculate
1306ba4f5d5754c8291690d01ca9581926673d69b24cJohn McCall    // the actual alignment of the field within the struct, and then
1307ba4f5d5754c8291690d01ca9581926673d69b24cJohn McCall    // (as we're expected to) constrain that by the alignment of the type.
1308ba4f5d5754c8291690d01ca9581926673d69b24cJohn McCall    if (const FieldDecl *field = dyn_cast<FieldDecl>(VD)) {
1309ba4f5d5754c8291690d01ca9581926673d69b24cJohn McCall      // So calculate the alignment of the field.
1310ba4f5d5754c8291690d01ca9581926673d69b24cJohn McCall      const ASTRecordLayout &layout = getASTRecordLayout(field->getParent());
1311ba4f5d5754c8291690d01ca9581926673d69b24cJohn McCall
1312ba4f5d5754c8291690d01ca9581926673d69b24cJohn McCall      // Start with the record's overall alignment.
1313dac54c124e302d6f028ea5723c425b7f66fc7c71Ken Dyck      unsigned fieldAlign = toBits(layout.getAlignment());
1314ba4f5d5754c8291690d01ca9581926673d69b24cJohn McCall
1315ba4f5d5754c8291690d01ca9581926673d69b24cJohn McCall      // Use the GCD of that and the offset within the record.
1316ba4f5d5754c8291690d01ca9581926673d69b24cJohn McCall      uint64_t offset = layout.getFieldOffset(field->getFieldIndex());
1317ba4f5d5754c8291690d01ca9581926673d69b24cJohn McCall      if (offset > 0) {
1318ba4f5d5754c8291690d01ca9581926673d69b24cJohn McCall        // Alignment is always a power of 2, so the GCD will be a power of 2,
1319ba4f5d5754c8291690d01ca9581926673d69b24cJohn McCall        // which means we get to do this crazy thing instead of Euclid's.
1320ba4f5d5754c8291690d01ca9581926673d69b24cJohn McCall        uint64_t lowBitOfOffset = offset & (~offset + 1);
1321ba4f5d5754c8291690d01ca9581926673d69b24cJohn McCall        if (lowBitOfOffset < fieldAlign)
1322ba4f5d5754c8291690d01ca9581926673d69b24cJohn McCall          fieldAlign = static_cast<unsigned>(lowBitOfOffset);
1323ba4f5d5754c8291690d01ca9581926673d69b24cJohn McCall      }
1324ba4f5d5754c8291690d01ca9581926673d69b24cJohn McCall
1325ba4f5d5754c8291690d01ca9581926673d69b24cJohn McCall      Align = std::min(Align, fieldAlign);
132605f62474dd2b0f1cb69adbe0787f2868788aa949Charles Davis    }
1327af707ab8fbb9451e8febb8d766f6c043628125c4Chris Lattner  }
1328dcdafb6a701aa9d81edcb088915f58933315dc05Eli Friedman
1329eb6f5dc86531f794ba7746a2da4d28e37cf5da7eKen Dyck  return toCharUnitsFromBits(Align);
1330af707ab8fbb9451e8febb8d766f6c043628125c4Chris Lattner}
1331b7cfe88e88cb4f46308de89cf3f0c81bfe624128Chris Lattner
1332929bbfb0b69165b55da3c56abf22aa10e20dadc6John McCall// getTypeInfoDataSizeInChars - Return the size of a type, in
1333929bbfb0b69165b55da3c56abf22aa10e20dadc6John McCall// chars. If the type is a record, its data size is returned.  This is
1334929bbfb0b69165b55da3c56abf22aa10e20dadc6John McCall// the size of the memcpy that's performed when assigning this type
1335929bbfb0b69165b55da3c56abf22aa10e20dadc6John McCall// using a trivial copy/move assignment operator.
1336929bbfb0b69165b55da3c56abf22aa10e20dadc6John McCallstd::pair<CharUnits, CharUnits>
1337929bbfb0b69165b55da3c56abf22aa10e20dadc6John McCallASTContext::getTypeInfoDataSizeInChars(QualType T) const {
1338929bbfb0b69165b55da3c56abf22aa10e20dadc6John McCall  std::pair<CharUnits, CharUnits> sizeAndAlign = getTypeInfoInChars(T);
1339929bbfb0b69165b55da3c56abf22aa10e20dadc6John McCall
1340929bbfb0b69165b55da3c56abf22aa10e20dadc6John McCall  // In C++, objects can sometimes be allocated into the tail padding
1341929bbfb0b69165b55da3c56abf22aa10e20dadc6John McCall  // of a base-class subobject.  We decide whether that's possible
1342929bbfb0b69165b55da3c56abf22aa10e20dadc6John McCall  // during class layout, so here we can just trust the layout results.
1343929bbfb0b69165b55da3c56abf22aa10e20dadc6John McCall  if (getLangOpts().CPlusPlus) {
1344929bbfb0b69165b55da3c56abf22aa10e20dadc6John McCall    if (const RecordType *RT = T->getAs<RecordType>()) {
1345929bbfb0b69165b55da3c56abf22aa10e20dadc6John McCall      const ASTRecordLayout &layout = getASTRecordLayout(RT->getDecl());
1346929bbfb0b69165b55da3c56abf22aa10e20dadc6John McCall      sizeAndAlign.first = layout.getDataSize();
1347929bbfb0b69165b55da3c56abf22aa10e20dadc6John McCall    }
1348929bbfb0b69165b55da3c56abf22aa10e20dadc6John McCall  }
1349929bbfb0b69165b55da3c56abf22aa10e20dadc6John McCall
1350929bbfb0b69165b55da3c56abf22aa10e20dadc6John McCall  return sizeAndAlign;
1351929bbfb0b69165b55da3c56abf22aa10e20dadc6John McCall}
1352929bbfb0b69165b55da3c56abf22aa10e20dadc6John McCall
1353910f17e331221cd0833d0b5b49013cbbc7ef122aRichard Trieu/// getConstantArrayInfoInChars - Performing the computation in CharUnits
1354910f17e331221cd0833d0b5b49013cbbc7ef122aRichard Trieu/// instead of in bits prevents overflowing the uint64_t for some large arrays.
1355910f17e331221cd0833d0b5b49013cbbc7ef122aRichard Trieustd::pair<CharUnits, CharUnits>
1356910f17e331221cd0833d0b5b49013cbbc7ef122aRichard Trieustatic getConstantArrayInfoInChars(const ASTContext &Context,
1357910f17e331221cd0833d0b5b49013cbbc7ef122aRichard Trieu                                   const ConstantArrayType *CAT) {
1358910f17e331221cd0833d0b5b49013cbbc7ef122aRichard Trieu  std::pair<CharUnits, CharUnits> EltInfo =
1359910f17e331221cd0833d0b5b49013cbbc7ef122aRichard Trieu      Context.getTypeInfoInChars(CAT->getElementType());
1360910f17e331221cd0833d0b5b49013cbbc7ef122aRichard Trieu  uint64_t Size = CAT->getSize().getZExtValue();
13611069b731a0dc872069c2d6572eefe424b0cceff3Richard Trieu  assert((Size == 0 || static_cast<uint64_t>(EltInfo.first.getQuantity()) <=
13621069b731a0dc872069c2d6572eefe424b0cceff3Richard Trieu              (uint64_t)(-1)/Size) &&
1363910f17e331221cd0833d0b5b49013cbbc7ef122aRichard Trieu         "Overflow in array type char size evaluation");
1364910f17e331221cd0833d0b5b49013cbbc7ef122aRichard Trieu  uint64_t Width = EltInfo.first.getQuantity() * Size;
1365910f17e331221cd0833d0b5b49013cbbc7ef122aRichard Trieu  unsigned Align = EltInfo.second.getQuantity();
1366910f17e331221cd0833d0b5b49013cbbc7ef122aRichard Trieu  Width = llvm::RoundUpToAlignment(Width, Align);
1367910f17e331221cd0833d0b5b49013cbbc7ef122aRichard Trieu  return std::make_pair(CharUnits::fromQuantity(Width),
1368910f17e331221cd0833d0b5b49013cbbc7ef122aRichard Trieu                        CharUnits::fromQuantity(Align));
1369910f17e331221cd0833d0b5b49013cbbc7ef122aRichard Trieu}
1370910f17e331221cd0833d0b5b49013cbbc7ef122aRichard Trieu
1371ea1471e0e967548c596a71469702f8846dbaf3c0John McCallstd::pair<CharUnits, CharUnits>
1372bee5a79fc95e3003d755031e3d2bb4410a71e1c1Ken DyckASTContext::getTypeInfoInChars(const Type *T) const {
1373910f17e331221cd0833d0b5b49013cbbc7ef122aRichard Trieu  if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(T))
1374910f17e331221cd0833d0b5b49013cbbc7ef122aRichard Trieu    return getConstantArrayInfoInChars(*this, CAT);
1375ea1471e0e967548c596a71469702f8846dbaf3c0John McCall  std::pair<uint64_t, unsigned> Info = getTypeInfo(T);
1376eb6f5dc86531f794ba7746a2da4d28e37cf5da7eKen Dyck  return std::make_pair(toCharUnitsFromBits(Info.first),
1377eb6f5dc86531f794ba7746a2da4d28e37cf5da7eKen Dyck                        toCharUnitsFromBits(Info.second));
1378ea1471e0e967548c596a71469702f8846dbaf3c0John McCall}
1379ea1471e0e967548c596a71469702f8846dbaf3c0John McCall
1380ea1471e0e967548c596a71469702f8846dbaf3c0John McCallstd::pair<CharUnits, CharUnits>
1381bee5a79fc95e3003d755031e3d2bb4410a71e1c1Ken DyckASTContext::getTypeInfoInChars(QualType T) const {
1382ea1471e0e967548c596a71469702f8846dbaf3c0John McCall  return getTypeInfoInChars(T.getTypePtr());
1383ea1471e0e967548c596a71469702f8846dbaf3c0John McCall}
1384ea1471e0e967548c596a71469702f8846dbaf3c0John McCall
1385bc5419a2edc4030d1a623576fe339fbd3eed17a6Daniel Dunbarstd::pair<uint64_t, unsigned> ASTContext::getTypeInfo(const Type *T) const {
1386bc5419a2edc4030d1a623576fe339fbd3eed17a6Daniel Dunbar  TypeInfoMap::iterator it = MemoizedTypeInfo.find(T);
1387bc5419a2edc4030d1a623576fe339fbd3eed17a6Daniel Dunbar  if (it != MemoizedTypeInfo.end())
1388bc5419a2edc4030d1a623576fe339fbd3eed17a6Daniel Dunbar    return it->second;
1389bc5419a2edc4030d1a623576fe339fbd3eed17a6Daniel Dunbar
1390bc5419a2edc4030d1a623576fe339fbd3eed17a6Daniel Dunbar  std::pair<uint64_t, unsigned> Info = getTypeInfoImpl(T);
1391bc5419a2edc4030d1a623576fe339fbd3eed17a6Daniel Dunbar  MemoizedTypeInfo.insert(std::make_pair(T, Info));
1392bc5419a2edc4030d1a623576fe339fbd3eed17a6Daniel Dunbar  return Info;
1393bc5419a2edc4030d1a623576fe339fbd3eed17a6Daniel Dunbar}
1394bc5419a2edc4030d1a623576fe339fbd3eed17a6Daniel Dunbar
1395bc5419a2edc4030d1a623576fe339fbd3eed17a6Daniel Dunbar/// getTypeInfoImpl - Return the size of the specified type, in bits.  This
1396bc5419a2edc4030d1a623576fe339fbd3eed17a6Daniel Dunbar/// method does not work on incomplete types.
13970953e767ff7817f97b3ab20896b229891eeff45bJohn McCall///
13980953e767ff7817f97b3ab20896b229891eeff45bJohn McCall/// FIXME: Pointers into different addr spaces could have different sizes and
13990953e767ff7817f97b3ab20896b229891eeff45bJohn McCall/// alignment requirements: getPointerInfo should take an AddrSpace, this
14000953e767ff7817f97b3ab20896b229891eeff45bJohn McCall/// should take a QualType, &c.
1401d2d2a11a91d7ddf468bfb70f66362d24806ed601Chris Lattnerstd::pair<uint64_t, unsigned>
1402bc5419a2edc4030d1a623576fe339fbd3eed17a6Daniel DunbarASTContext::getTypeInfoImpl(const Type *T) const {
14035e301007e31e14c8ff647288e1b8bd8dbf8a5fe4Mike Stump  uint64_t Width=0;
14045e301007e31e14c8ff647288e1b8bd8dbf8a5fe4Mike Stump  unsigned Align=8;
1405a7674d8a9a69f3f6fe16e70cf2a3b2b15fb7c43dChris Lattner  switch (T->getTypeClass()) {
140672564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor#define TYPE(Class, Base)
140772564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor#define ABSTRACT_TYPE(Class, Base)
140818857644059c45da6776f1a288eec7b4cf3a844aDouglas Gregor#define NON_CANONICAL_TYPE(Class, Base)
140972564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor#define DEPENDENT_TYPE(Class, Base) case Type::Class:
141072564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor#include "clang/AST/TypeNodes.def"
1411d3d49bb27c7ffd9accc0a6c00e887111c0348845John McCall    llvm_unreachable("Should not see dependent types");
141272564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor
14135d2a6303467184b1f159bb6556efc434e50e3c28Chris Lattner  case Type::FunctionNoProto:
14145d2a6303467184b1f159bb6556efc434e50e3c28Chris Lattner  case Type::FunctionProto:
141518857644059c45da6776f1a288eec7b4cf3a844aDouglas Gregor    // GCC extension: alignof(function) = 32 bits
141618857644059c45da6776f1a288eec7b4cf3a844aDouglas Gregor    Width = 0;
141718857644059c45da6776f1a288eec7b4cf3a844aDouglas Gregor    Align = 32;
141818857644059c45da6776f1a288eec7b4cf3a844aDouglas Gregor    break;
141918857644059c45da6776f1a288eec7b4cf3a844aDouglas Gregor
142072564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor  case Type::IncompleteArray:
1421fb22d96692c5240fb8d611290dbf7eeed3759c73Steve Naroff  case Type::VariableArray:
142218857644059c45da6776f1a288eec7b4cf3a844aDouglas Gregor    Width = 0;
142318857644059c45da6776f1a288eec7b4cf3a844aDouglas Gregor    Align = getTypeAlign(cast<ArrayType>(T)->getElementType());
142418857644059c45da6776f1a288eec7b4cf3a844aDouglas Gregor    break;
142518857644059c45da6776f1a288eec7b4cf3a844aDouglas Gregor
1426fb22d96692c5240fb8d611290dbf7eeed3759c73Steve Naroff  case Type::ConstantArray: {
14271d75118af76cae2bfc06389cde410e14bd0a19fcDaniel Dunbar    const ConstantArrayType *CAT = cast<ConstantArrayType>(T);
14281eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
142998be4943e8dc4f3905629a7102668960873cf863Chris Lattner    std::pair<uint64_t, unsigned> EltInfo = getTypeInfo(CAT->getElementType());
1430fea966a4103ed9c018d1494b95e9d09b161f5a70Abramo Bagnara    uint64_t Size = CAT->getSize().getZExtValue();
1431bc5419a2edc4030d1a623576fe339fbd3eed17a6Daniel Dunbar    assert((Size == 0 || EltInfo.first <= (uint64_t)(-1)/Size) &&
1432bc5419a2edc4030d1a623576fe339fbd3eed17a6Daniel Dunbar           "Overflow in array type bit size evaluation");
1433fea966a4103ed9c018d1494b95e9d09b161f5a70Abramo Bagnara    Width = EltInfo.first*Size;
1434030d8846c7e520330007087e949f621989876e3aChris Lattner    Align = EltInfo.second;
1435cd88b4171753dcb2bc0a21d78f1597c796bb8a20Argyrios Kyrtzidis    Width = llvm::RoundUpToAlignment(Width, Align);
1436030d8846c7e520330007087e949f621989876e3aChris Lattner    break;
14375c09a02a5db85e08a432b6eeced9aa656349710dChristopher Lamb  }
1438213541a68a3e137d11d2cefb612c6cdb410d7e8eNate Begeman  case Type::ExtVector:
1439030d8846c7e520330007087e949f621989876e3aChris Lattner  case Type::Vector: {
14409fcfe926432f3c3f7e9a61219e55c352fd358e45Chris Lattner    const VectorType *VT = cast<VectorType>(T);
14419fcfe926432f3c3f7e9a61219e55c352fd358e45Chris Lattner    std::pair<uint64_t, unsigned> EltInfo = getTypeInfo(VT->getElementType());
14429fcfe926432f3c3f7e9a61219e55c352fd358e45Chris Lattner    Width = EltInfo.first*VT->getNumElements();
14434bd998bbc228915d2b9cae5b67879de48940d05eEli Friedman    Align = Width;
14446fe7c8aa8c7546743ecd0ac0138c2cf5d8155386Nate Begeman    // If the alignment is not a power of 2, round up to the next power of 2.
14456fe7c8aa8c7546743ecd0ac0138c2cf5d8155386Nate Begeman    // This happens for non-power-of-2 length vectors.
14468eefcd353c1d06a10104f69e5079ebab3183f9a3Dan Gohman    if (Align & (Align-1)) {
14479fcfe926432f3c3f7e9a61219e55c352fd358e45Chris Lattner      Align = llvm::NextPowerOf2(Align);
14489fcfe926432f3c3f7e9a61219e55c352fd358e45Chris Lattner      Width = llvm::RoundUpToAlignment(Width, Align);
14499fcfe926432f3c3f7e9a61219e55c352fd358e45Chris Lattner    }
1450f9e9af7df0cea6e997ac04131c7af6ca4384b0ccChad Rosier    // Adjust the alignment based on the target max.
1451f9e9af7df0cea6e997ac04131c7af6ca4384b0ccChad Rosier    uint64_t TargetVectorAlign = Target->getMaxVectorAlign();
1452f9e9af7df0cea6e997ac04131c7af6ca4384b0ccChad Rosier    if (TargetVectorAlign && TargetVectorAlign < Align)
1453f9e9af7df0cea6e997ac04131c7af6ca4384b0ccChad Rosier      Align = TargetVectorAlign;
1454030d8846c7e520330007087e949f621989876e3aChris Lattner    break;
1455030d8846c7e520330007087e949f621989876e3aChris Lattner  }
14565d2a6303467184b1f159bb6556efc434e50e3c28Chris Lattner
14579e9b6dc3fd413f5341fab54b681420eeb21cd169Chris Lattner  case Type::Builtin:
1458a7674d8a9a69f3f6fe16e70cf2a3b2b15fb7c43dChris Lattner    switch (cast<BuiltinType>(T)->getKind()) {
1459b219cfc4d75f0a03630b7c4509ef791b7e97b2c8David Blaikie    default: llvm_unreachable("Unknown builtin type!");
1460d2d2a11a91d7ddf468bfb70f66362d24806ed601Chris Lattner    case BuiltinType::Void:
146118857644059c45da6776f1a288eec7b4cf3a844aDouglas Gregor      // GCC extension: alignof(void) = 8 bits.
146218857644059c45da6776f1a288eec7b4cf3a844aDouglas Gregor      Width = 0;
146318857644059c45da6776f1a288eec7b4cf3a844aDouglas Gregor      Align = 8;
146418857644059c45da6776f1a288eec7b4cf3a844aDouglas Gregor      break;
146518857644059c45da6776f1a288eec7b4cf3a844aDouglas Gregor
14666f62c2abd8077bf70d2166d37e8caa426b34d8e4Chris Lattner    case BuiltinType::Bool:
1467bcfd1f55bfbb3e5944cd5e03d07b343e280838c4Douglas Gregor      Width = Target->getBoolWidth();
1468bcfd1f55bfbb3e5944cd5e03d07b343e280838c4Douglas Gregor      Align = Target->getBoolAlign();
14696f62c2abd8077bf70d2166d37e8caa426b34d8e4Chris Lattner      break;
1470692233e90a99c3a81dd04879d36eb9688f137c44Chris Lattner    case BuiltinType::Char_S:
1471692233e90a99c3a81dd04879d36eb9688f137c44Chris Lattner    case BuiltinType::Char_U:
1472692233e90a99c3a81dd04879d36eb9688f137c44Chris Lattner    case BuiltinType::UChar:
14736f62c2abd8077bf70d2166d37e8caa426b34d8e4Chris Lattner    case BuiltinType::SChar:
1474bcfd1f55bfbb3e5944cd5e03d07b343e280838c4Douglas Gregor      Width = Target->getCharWidth();
1475bcfd1f55bfbb3e5944cd5e03d07b343e280838c4Douglas Gregor      Align = Target->getCharAlign();
14766f62c2abd8077bf70d2166d37e8caa426b34d8e4Chris Lattner      break;
14773f59c975aa5d047f7edd1b900b5e885c38af0ef7Chris Lattner    case BuiltinType::WChar_S:
14783f59c975aa5d047f7edd1b900b5e885c38af0ef7Chris Lattner    case BuiltinType::WChar_U:
1479bcfd1f55bfbb3e5944cd5e03d07b343e280838c4Douglas Gregor      Width = Target->getWCharWidth();
1480bcfd1f55bfbb3e5944cd5e03d07b343e280838c4Douglas Gregor      Align = Target->getWCharAlign();
148164c438a4be2a871fa43c78264663ba1e9788b94dArgyrios Kyrtzidis      break;
1482f5c209d23b20ada4a9b6235db50317239cbf6ae1Alisdair Meredith    case BuiltinType::Char16:
1483bcfd1f55bfbb3e5944cd5e03d07b343e280838c4Douglas Gregor      Width = Target->getChar16Width();
1484bcfd1f55bfbb3e5944cd5e03d07b343e280838c4Douglas Gregor      Align = Target->getChar16Align();
1485f5c209d23b20ada4a9b6235db50317239cbf6ae1Alisdair Meredith      break;
1486f5c209d23b20ada4a9b6235db50317239cbf6ae1Alisdair Meredith    case BuiltinType::Char32:
1487bcfd1f55bfbb3e5944cd5e03d07b343e280838c4Douglas Gregor      Width = Target->getChar32Width();
1488bcfd1f55bfbb3e5944cd5e03d07b343e280838c4Douglas Gregor      Align = Target->getChar32Align();
1489f5c209d23b20ada4a9b6235db50317239cbf6ae1Alisdair Meredith      break;
1490692233e90a99c3a81dd04879d36eb9688f137c44Chris Lattner    case BuiltinType::UShort:
14916f62c2abd8077bf70d2166d37e8caa426b34d8e4Chris Lattner    case BuiltinType::Short:
1492bcfd1f55bfbb3e5944cd5e03d07b343e280838c4Douglas Gregor      Width = Target->getShortWidth();
1493bcfd1f55bfbb3e5944cd5e03d07b343e280838c4Douglas Gregor      Align = Target->getShortAlign();
14946f62c2abd8077bf70d2166d37e8caa426b34d8e4Chris Lattner      break;
1495692233e90a99c3a81dd04879d36eb9688f137c44Chris Lattner    case BuiltinType::UInt:
14966f62c2abd8077bf70d2166d37e8caa426b34d8e4Chris Lattner    case BuiltinType::Int:
1497bcfd1f55bfbb3e5944cd5e03d07b343e280838c4Douglas Gregor      Width = Target->getIntWidth();
1498bcfd1f55bfbb3e5944cd5e03d07b343e280838c4Douglas Gregor      Align = Target->getIntAlign();
14996f62c2abd8077bf70d2166d37e8caa426b34d8e4Chris Lattner      break;
1500692233e90a99c3a81dd04879d36eb9688f137c44Chris Lattner    case BuiltinType::ULong:
15016f62c2abd8077bf70d2166d37e8caa426b34d8e4Chris Lattner    case BuiltinType::Long:
1502bcfd1f55bfbb3e5944cd5e03d07b343e280838c4Douglas Gregor      Width = Target->getLongWidth();
1503bcfd1f55bfbb3e5944cd5e03d07b343e280838c4Douglas Gregor      Align = Target->getLongAlign();
15046f62c2abd8077bf70d2166d37e8caa426b34d8e4Chris Lattner      break;
1505692233e90a99c3a81dd04879d36eb9688f137c44Chris Lattner    case BuiltinType::ULongLong:
15066f62c2abd8077bf70d2166d37e8caa426b34d8e4Chris Lattner    case BuiltinType::LongLong:
1507bcfd1f55bfbb3e5944cd5e03d07b343e280838c4Douglas Gregor      Width = Target->getLongLongWidth();
1508bcfd1f55bfbb3e5944cd5e03d07b343e280838c4Douglas Gregor      Align = Target->getLongLongAlign();
15096f62c2abd8077bf70d2166d37e8caa426b34d8e4Chris Lattner      break;
1510ec16cb9b5a481d62a73ad47fa59034ced4d62022Chris Lattner    case BuiltinType::Int128:
1511ec16cb9b5a481d62a73ad47fa59034ced4d62022Chris Lattner    case BuiltinType::UInt128:
1512ec16cb9b5a481d62a73ad47fa59034ced4d62022Chris Lattner      Width = 128;
1513ec16cb9b5a481d62a73ad47fa59034ced4d62022Chris Lattner      Align = 128; // int128_t is 128-bit aligned on all targets.
1514ec16cb9b5a481d62a73ad47fa59034ced4d62022Chris Lattner      break;
1515aa4a99b4a62615db243f7a5c433169f2fc704420Anton Korobeynikov    case BuiltinType::Half:
1516aa4a99b4a62615db243f7a5c433169f2fc704420Anton Korobeynikov      Width = Target->getHalfWidth();
1517aa4a99b4a62615db243f7a5c433169f2fc704420Anton Korobeynikov      Align = Target->getHalfAlign();
1518aa4a99b4a62615db243f7a5c433169f2fc704420Anton Korobeynikov      break;
15196f62c2abd8077bf70d2166d37e8caa426b34d8e4Chris Lattner    case BuiltinType::Float:
1520bcfd1f55bfbb3e5944cd5e03d07b343e280838c4Douglas Gregor      Width = Target->getFloatWidth();
1521bcfd1f55bfbb3e5944cd5e03d07b343e280838c4Douglas Gregor      Align = Target->getFloatAlign();
15226f62c2abd8077bf70d2166d37e8caa426b34d8e4Chris Lattner      break;
15236f62c2abd8077bf70d2166d37e8caa426b34d8e4Chris Lattner    case BuiltinType::Double:
1524bcfd1f55bfbb3e5944cd5e03d07b343e280838c4Douglas Gregor      Width = Target->getDoubleWidth();
1525bcfd1f55bfbb3e5944cd5e03d07b343e280838c4Douglas Gregor      Align = Target->getDoubleAlign();
15266f62c2abd8077bf70d2166d37e8caa426b34d8e4Chris Lattner      break;
15276f62c2abd8077bf70d2166d37e8caa426b34d8e4Chris Lattner    case BuiltinType::LongDouble:
1528bcfd1f55bfbb3e5944cd5e03d07b343e280838c4Douglas Gregor      Width = Target->getLongDoubleWidth();
1529bcfd1f55bfbb3e5944cd5e03d07b343e280838c4Douglas Gregor      Align = Target->getLongDoubleAlign();
15306f62c2abd8077bf70d2166d37e8caa426b34d8e4Chris Lattner      break;
15316e8ed16ffef02b82995a90bdcf10ffff7d63839aSebastian Redl    case BuiltinType::NullPtr:
1532bcfd1f55bfbb3e5944cd5e03d07b343e280838c4Douglas Gregor      Width = Target->getPointerWidth(0); // C++ 3.9.1p11: sizeof(nullptr_t)
1533bcfd1f55bfbb3e5944cd5e03d07b343e280838c4Douglas Gregor      Align = Target->getPointerAlign(0); //   == sizeof(void*)
15341590d9c0fec4c710c2962e4bb71f76979b5163d3Sebastian Redl      break;
1535e04f5fc25cf49e8a5b836459d836c20dc3229a95Fariborz Jahanian    case BuiltinType::ObjCId:
1536e04f5fc25cf49e8a5b836459d836c20dc3229a95Fariborz Jahanian    case BuiltinType::ObjCClass:
1537e04f5fc25cf49e8a5b836459d836c20dc3229a95Fariborz Jahanian    case BuiltinType::ObjCSel:
1538bcfd1f55bfbb3e5944cd5e03d07b343e280838c4Douglas Gregor      Width = Target->getPointerWidth(0);
1539bcfd1f55bfbb3e5944cd5e03d07b343e280838c4Douglas Gregor      Align = Target->getPointerAlign(0);
1540e04f5fc25cf49e8a5b836459d836c20dc3229a95Fariborz Jahanian      break;
154121f18c4fda167dc5f72feddbd6a7ac1b63200a0dGuy Benyei    case BuiltinType::OCLSampler:
154221f18c4fda167dc5f72feddbd6a7ac1b63200a0dGuy Benyei      // Samplers are modeled as integers.
154321f18c4fda167dc5f72feddbd6a7ac1b63200a0dGuy Benyei      Width = Target->getIntWidth();
154421f18c4fda167dc5f72feddbd6a7ac1b63200a0dGuy Benyei      Align = Target->getIntAlign();
154521f18c4fda167dc5f72feddbd6a7ac1b63200a0dGuy Benyei      break;
1546e6b9d802fb7b16d93474c4f1c179ab36202e8a8bGuy Benyei    case BuiltinType::OCLEvent:
1547b13621d08e20ac7aa550e05896de8a57ee99c1e8Guy Benyei    case BuiltinType::OCLImage1d:
1548b13621d08e20ac7aa550e05896de8a57ee99c1e8Guy Benyei    case BuiltinType::OCLImage1dArray:
1549b13621d08e20ac7aa550e05896de8a57ee99c1e8Guy Benyei    case BuiltinType::OCLImage1dBuffer:
1550b13621d08e20ac7aa550e05896de8a57ee99c1e8Guy Benyei    case BuiltinType::OCLImage2d:
1551b13621d08e20ac7aa550e05896de8a57ee99c1e8Guy Benyei    case BuiltinType::OCLImage2dArray:
1552b13621d08e20ac7aa550e05896de8a57ee99c1e8Guy Benyei    case BuiltinType::OCLImage3d:
1553b13621d08e20ac7aa550e05896de8a57ee99c1e8Guy Benyei      // Currently these types are pointers to opaque types.
1554b13621d08e20ac7aa550e05896de8a57ee99c1e8Guy Benyei      Width = Target->getPointerWidth(0);
1555b13621d08e20ac7aa550e05896de8a57ee99c1e8Guy Benyei      Align = Target->getPointerAlign(0);
1556b13621d08e20ac7aa550e05896de8a57ee99c1e8Guy Benyei      break;
1557a7674d8a9a69f3f6fe16e70cf2a3b2b15fb7c43dChris Lattner    }
1558bfef6d7c67831a135d6ab79931f010f750a730adChris Lattner    break;
1559d1b3c2dd5bc1f3103bee6137957aa7c5f8f2f0bcSteve Naroff  case Type::ObjCObjectPointer:
1560bcfd1f55bfbb3e5944cd5e03d07b343e280838c4Douglas Gregor    Width = Target->getPointerWidth(0);
1561bcfd1f55bfbb3e5944cd5e03d07b343e280838c4Douglas Gregor    Align = Target->getPointerAlign(0);
15626f62c2abd8077bf70d2166d37e8caa426b34d8e4Chris Lattner    break;
1563485eeff9ba73376c8e01179bf1a501b1723446cbSteve Naroff  case Type::BlockPointer: {
1564207f4d8543529221932af82836016a2ef066c917Peter Collingbourne    unsigned AS = getTargetAddressSpace(
1565207f4d8543529221932af82836016a2ef066c917Peter Collingbourne        cast<BlockPointerType>(T)->getPointeeType());
1566bcfd1f55bfbb3e5944cd5e03d07b343e280838c4Douglas Gregor    Width = Target->getPointerWidth(AS);
1567bcfd1f55bfbb3e5944cd5e03d07b343e280838c4Douglas Gregor    Align = Target->getPointerAlign(AS);
1568485eeff9ba73376c8e01179bf1a501b1723446cbSteve Naroff    break;
1569485eeff9ba73376c8e01179bf1a501b1723446cbSteve Naroff  }
15705d484e8cf710207010720589d89602233de61d01Sebastian Redl  case Type::LValueReference:
15715d484e8cf710207010720589d89602233de61d01Sebastian Redl  case Type::RValueReference: {
15725d484e8cf710207010720589d89602233de61d01Sebastian Redl    // alignof and sizeof should never enter this code path here, so we go
15735d484e8cf710207010720589d89602233de61d01Sebastian Redl    // the pointer route.
1574207f4d8543529221932af82836016a2ef066c917Peter Collingbourne    unsigned AS = getTargetAddressSpace(
1575207f4d8543529221932af82836016a2ef066c917Peter Collingbourne        cast<ReferenceType>(T)->getPointeeType());
1576bcfd1f55bfbb3e5944cd5e03d07b343e280838c4Douglas Gregor    Width = Target->getPointerWidth(AS);
1577bcfd1f55bfbb3e5944cd5e03d07b343e280838c4Douglas Gregor    Align = Target->getPointerAlign(AS);
15785d484e8cf710207010720589d89602233de61d01Sebastian Redl    break;
15795d484e8cf710207010720589d89602233de61d01Sebastian Redl  }
1580f72a44330b9d9a4b2d93e9b91cfb8ab7bd4a0643Chris Lattner  case Type::Pointer: {
1581207f4d8543529221932af82836016a2ef066c917Peter Collingbourne    unsigned AS = getTargetAddressSpace(cast<PointerType>(T)->getPointeeType());
1582bcfd1f55bfbb3e5944cd5e03d07b343e280838c4Douglas Gregor    Width = Target->getPointerWidth(AS);
1583bcfd1f55bfbb3e5944cd5e03d07b343e280838c4Douglas Gregor    Align = Target->getPointerAlign(AS);
1584f72a44330b9d9a4b2d93e9b91cfb8ab7bd4a0643Chris Lattner    break;
1585f72a44330b9d9a4b2d93e9b91cfb8ab7bd4a0643Chris Lattner  }
1586f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl  case Type::MemberPointer: {
1587071cc7deffad608165b1ddd5263e8bf181861520Charles Davis    const MemberPointerType *MPT = cast<MemberPointerType>(T);
158884e9ab44af3a16f66d62590505db2036ef0aa03bReid Kleckner    llvm::tie(Width, Align) = ABI->getMemberPointerWidthAndAlign(MPT);
15891cca74ef3627a3a0ab14501d23e336548f6611b2Anders Carlsson    break;
1590f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl  }
15915d2a6303467184b1f159bb6556efc434e50e3c28Chris Lattner  case Type::Complex: {
15925d2a6303467184b1f159bb6556efc434e50e3c28Chris Lattner    // Complex types have the same alignment as their elements, but twice the
15935d2a6303467184b1f159bb6556efc434e50e3c28Chris Lattner    // size.
15941eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    std::pair<uint64_t, unsigned> EltInfo =
159598be4943e8dc4f3905629a7102668960873cf863Chris Lattner      getTypeInfo(cast<ComplexType>(T)->getElementType());
15969e9b6dc3fd413f5341fab54b681420eeb21cd169Chris Lattner    Width = EltInfo.first*2;
15975d2a6303467184b1f159bb6556efc434e50e3c28Chris Lattner    Align = EltInfo.second;
15985d2a6303467184b1f159bb6556efc434e50e3c28Chris Lattner    break;
15995d2a6303467184b1f159bb6556efc434e50e3c28Chris Lattner  }
1600c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall  case Type::ObjCObject:
1601c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall    return getTypeInfo(cast<ObjCObjectType>(T)->getBaseType().getTypePtr());
160244a3dded8080c5c9cfdad208ade8f8f7850d9a4fDevang Patel  case Type::ObjCInterface: {
16031d75118af76cae2bfc06389cde410e14bd0a19fcDaniel Dunbar    const ObjCInterfaceType *ObjCI = cast<ObjCInterfaceType>(T);
160444a3dded8080c5c9cfdad208ade8f8f7850d9a4fDevang Patel    const ASTRecordLayout &Layout = getASTObjCInterfaceLayout(ObjCI->getDecl());
1605dd76a9ab9ea675671200f94b18ce95766841952bKen Dyck    Width = toBits(Layout.getSize());
1606dac54c124e302d6f028ea5723c425b7f66fc7c71Ken Dyck    Align = toBits(Layout.getAlignment());
160744a3dded8080c5c9cfdad208ade8f8f7850d9a4fDevang Patel    break;
160844a3dded8080c5c9cfdad208ade8f8f7850d9a4fDevang Patel  }
160972564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor  case Type::Record:
161072564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor  case Type::Enum: {
16111d75118af76cae2bfc06389cde410e14bd0a19fcDaniel Dunbar    const TagType *TT = cast<TagType>(T);
16121d75118af76cae2bfc06389cde410e14bd0a19fcDaniel Dunbar
16131d75118af76cae2bfc06389cde410e14bd0a19fcDaniel Dunbar    if (TT->getDecl()->isInvalidDecl()) {
161422ce41d9fc8509da65858c75bf5b3c4dae2d8c04Douglas Gregor      Width = 8;
161522ce41d9fc8509da65858c75bf5b3c4dae2d8c04Douglas Gregor      Align = 8;
16168389eab190afef3462f6418b8d8fb70fb01c4005Chris Lattner      break;
16178389eab190afef3462f6418b8d8fb70fb01c4005Chris Lattner    }
16181eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
16191d75118af76cae2bfc06389cde410e14bd0a19fcDaniel Dunbar    if (const EnumType *ET = dyn_cast<EnumType>(TT))
16207176331b0f5cfaaa2b5aa487a6660e859e371119Chris Lattner      return getTypeInfo(ET->getDecl()->getIntegerType());
16217176331b0f5cfaaa2b5aa487a6660e859e371119Chris Lattner
16221d75118af76cae2bfc06389cde410e14bd0a19fcDaniel Dunbar    const RecordType *RT = cast<RecordType>(TT);
16237176331b0f5cfaaa2b5aa487a6660e859e371119Chris Lattner    const ASTRecordLayout &Layout = getASTRecordLayout(RT->getDecl());
1624dd76a9ab9ea675671200f94b18ce95766841952bKen Dyck    Width = toBits(Layout.getSize());
1625dac54c124e302d6f028ea5723c425b7f66fc7c71Ken Dyck    Align = toBits(Layout.getAlignment());
1626dc0d73e6495404418acf8548875aeaff07791a74Chris Lattner    break;
1627a7674d8a9a69f3f6fe16e70cf2a3b2b15fb7c43dChris Lattner  }
16287532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor
16299fcfe926432f3c3f7e9a61219e55c352fd358e45Chris Lattner  case Type::SubstTemplateTypeParm:
163049a832bd499d6f61c23655f1fac99f0dd229756eJohn McCall    return getTypeInfo(cast<SubstTemplateTypeParmType>(T)->
163149a832bd499d6f61c23655f1fac99f0dd229756eJohn McCall                       getReplacementType().getTypePtr());
163249a832bd499d6f61c23655f1fac99f0dd229756eJohn McCall
163334b41d939a1328f484511c6002ba2456db879a29Richard Smith  case Type::Auto: {
163434b41d939a1328f484511c6002ba2456db879a29Richard Smith    const AutoType *A = cast<AutoType>(T);
1635dc7a4f5d7a7e3b60d4dc4a80338d7a2728540998Richard Smith    assert(!A->getDeducedType().isNull() &&
1636dc7a4f5d7a7e3b60d4dc4a80338d7a2728540998Richard Smith           "cannot request the size of an undeduced or dependent auto type");
1637dc856aff4428380baa9afb5577ea04f8fb6beb13Matt Beaumont-Gay    return getTypeInfo(A->getDeducedType().getTypePtr());
163834b41d939a1328f484511c6002ba2456db879a29Richard Smith  }
163934b41d939a1328f484511c6002ba2456db879a29Richard Smith
1640075f8f1b6bed4d1b224c74f87508534cc6392ce6Abramo Bagnara  case Type::Paren:
1641075f8f1b6bed4d1b224c74f87508534cc6392ce6Abramo Bagnara    return getTypeInfo(cast<ParenType>(T)->getInnerType().getTypePtr());
1642075f8f1b6bed4d1b224c74f87508534cc6392ce6Abramo Bagnara
164318857644059c45da6776f1a288eec7b4cf3a844aDouglas Gregor  case Type::Typedef: {
1644162e1c1b487352434552147967c3dd296ebee2f7Richard Smith    const TypedefNameDecl *Typedef = cast<TypedefType>(T)->getDecl();
1645df1367af26cb2959775e9511108f12dcd2370a27Douglas Gregor    std::pair<uint64_t, unsigned> Info
1646df1367af26cb2959775e9511108f12dcd2370a27Douglas Gregor      = getTypeInfo(Typedef->getUnderlyingType().getTypePtr());
1647c1de52de64725945e5ae87e6f99ddedf161856e5Chris Lattner    // If the typedef has an aligned attribute on it, it overrides any computed
1648c1de52de64725945e5ae87e6f99ddedf161856e5Chris Lattner    // alignment we have.  This violates the GCC documentation (which says that
1649c1de52de64725945e5ae87e6f99ddedf161856e5Chris Lattner    // attribute(aligned) can only round up) but matches its implementation.
1650c1de52de64725945e5ae87e6f99ddedf161856e5Chris Lattner    if (unsigned AttrAlign = Typedef->getMaxAlignment())
1651c1de52de64725945e5ae87e6f99ddedf161856e5Chris Lattner      Align = AttrAlign;
1652c1de52de64725945e5ae87e6f99ddedf161856e5Chris Lattner    else
1653c1de52de64725945e5ae87e6f99ddedf161856e5Chris Lattner      Align = Info.second;
1654df1367af26cb2959775e9511108f12dcd2370a27Douglas Gregor    Width = Info.first;
16557532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor    break;
16567176331b0f5cfaaa2b5aa487a6660e859e371119Chris Lattner  }
165718857644059c45da6776f1a288eec7b4cf3a844aDouglas Gregor
165818857644059c45da6776f1a288eec7b4cf3a844aDouglas Gregor  case Type::TypeOfExpr:
165918857644059c45da6776f1a288eec7b4cf3a844aDouglas Gregor    return getTypeInfo(cast<TypeOfExprType>(T)->getUnderlyingExpr()->getType()
166018857644059c45da6776f1a288eec7b4cf3a844aDouglas Gregor                         .getTypePtr());
166118857644059c45da6776f1a288eec7b4cf3a844aDouglas Gregor
166218857644059c45da6776f1a288eec7b4cf3a844aDouglas Gregor  case Type::TypeOf:
166318857644059c45da6776f1a288eec7b4cf3a844aDouglas Gregor    return getTypeInfo(cast<TypeOfType>(T)->getUnderlyingType().getTypePtr());
166418857644059c45da6776f1a288eec7b4cf3a844aDouglas Gregor
1665395b475a4474f1c7574d927ad142ca0c7997cbcaAnders Carlsson  case Type::Decltype:
1666395b475a4474f1c7574d927ad142ca0c7997cbcaAnders Carlsson    return getTypeInfo(cast<DecltypeType>(T)->getUnderlyingExpr()->getType()
1667395b475a4474f1c7574d927ad142ca0c7997cbcaAnders Carlsson                        .getTypePtr());
1668395b475a4474f1c7574d927ad142ca0c7997cbcaAnders Carlsson
1669ca63c200346c0ca9e00194ec6e34a5a7b0ed9321Sean Hunt  case Type::UnaryTransform:
1670ca63c200346c0ca9e00194ec6e34a5a7b0ed9321Sean Hunt    return getTypeInfo(cast<UnaryTransformType>(T)->getUnderlyingType());
1671ca63c200346c0ca9e00194ec6e34a5a7b0ed9321Sean Hunt
1672465d41b92b2c862f3062c412a0538db65c6a2661Abramo Bagnara  case Type::Elaborated:
1673465d41b92b2c862f3062c412a0538db65c6a2661Abramo Bagnara    return getTypeInfo(cast<ElaboratedType>(T)->getNamedType().getTypePtr());
16741eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
16759d156a7b1b2771e191f2f5a45a7b7a694129463bJohn McCall  case Type::Attributed:
16769d156a7b1b2771e191f2f5a45a7b7a694129463bJohn McCall    return getTypeInfo(
16779d156a7b1b2771e191f2f5a45a7b7a694129463bJohn McCall                  cast<AttributedType>(T)->getEquivalentType().getTypePtr());
16789d156a7b1b2771e191f2f5a45a7b7a694129463bJohn McCall
16793e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith  case Type::TemplateSpecialization: {
16801eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    assert(getCanonicalType(T) != T &&
168118857644059c45da6776f1a288eec7b4cf3a844aDouglas Gregor           "Cannot request the size of a dependent type");
16823e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith    const TemplateSpecializationType *TST = cast<TemplateSpecializationType>(T);
16833e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith    // A type alias template specialization may refer to a typedef with the
16843e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith    // aligned attribute on it.
16853e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith    if (TST->isTypeAlias())
16863e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith      return getTypeInfo(TST->getAliasedType().getTypePtr());
16873e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith    else
16883e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith      return getTypeInfo(getCanonicalType(T));
16893e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith  }
16903e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith
1691b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman  case Type::Atomic: {
16929eda3abe7e183b05834947391c0cdc291f4ee0d8John McCall    // Start with the base type information.
16932be460723940f8184ec36529b6f6ddf59c04e411Eli Friedman    std::pair<uint64_t, unsigned> Info
16942be460723940f8184ec36529b6f6ddf59c04e411Eli Friedman      = getTypeInfo(cast<AtomicType>(T)->getValueType());
16952be460723940f8184ec36529b6f6ddf59c04e411Eli Friedman    Width = Info.first;
16962be460723940f8184ec36529b6f6ddf59c04e411Eli Friedman    Align = Info.second;
16979eda3abe7e183b05834947391c0cdc291f4ee0d8John McCall
16989eda3abe7e183b05834947391c0cdc291f4ee0d8John McCall    // If the size of the type doesn't exceed the platform's max
16999eda3abe7e183b05834947391c0cdc291f4ee0d8John McCall    // atomic promotion width, make the size and alignment more
17009eda3abe7e183b05834947391c0cdc291f4ee0d8John McCall    // favorable to atomic operations:
17019eda3abe7e183b05834947391c0cdc291f4ee0d8John McCall    if (Width != 0 && Width <= Target->getMaxAtomicPromoteWidth()) {
17029eda3abe7e183b05834947391c0cdc291f4ee0d8John McCall      // Round the size up to a power of 2.
17039eda3abe7e183b05834947391c0cdc291f4ee0d8John McCall      if (!llvm::isPowerOf2_64(Width))
17049eda3abe7e183b05834947391c0cdc291f4ee0d8John McCall        Width = llvm::NextPowerOf2(Width);
17059eda3abe7e183b05834947391c0cdc291f4ee0d8John McCall
17069eda3abe7e183b05834947391c0cdc291f4ee0d8John McCall      // Set the alignment equal to the size.
17072be460723940f8184ec36529b6f6ddf59c04e411Eli Friedman      Align = static_cast<unsigned>(Width);
17082be460723940f8184ec36529b6f6ddf59c04e411Eli Friedman    }
1709b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman  }
1710b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman
171118857644059c45da6776f1a288eec7b4cf3a844aDouglas Gregor  }
17121eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
17132be460723940f8184ec36529b6f6ddf59c04e411Eli Friedman  assert(llvm::isPowerOf2_32(Align) && "Alignment must be power of 2");
17149e9b6dc3fd413f5341fab54b681420eeb21cd169Chris Lattner  return std::make_pair(Width, Align);
1715a7674d8a9a69f3f6fe16e70cf2a3b2b15fb7c43dChris Lattner}
1716a7674d8a9a69f3f6fe16e70cf2a3b2b15fb7c43dChris Lattner
1717eb6f5dc86531f794ba7746a2da4d28e37cf5da7eKen Dyck/// toCharUnitsFromBits - Convert a size in bits to a size in characters.
1718eb6f5dc86531f794ba7746a2da4d28e37cf5da7eKen DyckCharUnits ASTContext::toCharUnitsFromBits(int64_t BitSize) const {
1719eb6f5dc86531f794ba7746a2da4d28e37cf5da7eKen Dyck  return CharUnits::fromQuantity(BitSize / getCharWidth());
1720eb6f5dc86531f794ba7746a2da4d28e37cf5da7eKen Dyck}
1721eb6f5dc86531f794ba7746a2da4d28e37cf5da7eKen Dyck
1722dd76a9ab9ea675671200f94b18ce95766841952bKen Dyck/// toBits - Convert a size in characters to a size in characters.
1723dd76a9ab9ea675671200f94b18ce95766841952bKen Dyckint64_t ASTContext::toBits(CharUnits CharSize) const {
1724dd76a9ab9ea675671200f94b18ce95766841952bKen Dyck  return CharSize.getQuantity() * getCharWidth();
1725dd76a9ab9ea675671200f94b18ce95766841952bKen Dyck}
1726dd76a9ab9ea675671200f94b18ce95766841952bKen Dyck
1727bdc601b196c48d4cd56a5ceb45d41ae4e87371abKen Dyck/// getTypeSizeInChars - Return the size of the specified type, in characters.
1728bdc601b196c48d4cd56a5ceb45d41ae4e87371abKen Dyck/// This method does not work on incomplete types.
17294ba2a17694148e16eaa8d3917f657ffcd3667be4Jay FoadCharUnits ASTContext::getTypeSizeInChars(QualType T) const {
1730910f17e331221cd0833d0b5b49013cbbc7ef122aRichard Trieu  return getTypeInfoInChars(T).first;
1731bdc601b196c48d4cd56a5ceb45d41ae4e87371abKen Dyck}
17324ba2a17694148e16eaa8d3917f657ffcd3667be4Jay FoadCharUnits ASTContext::getTypeSizeInChars(const Type *T) const {
1733910f17e331221cd0833d0b5b49013cbbc7ef122aRichard Trieu  return getTypeInfoInChars(T).first;
1734bdc601b196c48d4cd56a5ceb45d41ae4e87371abKen Dyck}
1735bdc601b196c48d4cd56a5ceb45d41ae4e87371abKen Dyck
173616e20cce43385001f33f8e3f90ee345609c805d1Ken Dyck/// getTypeAlignInChars - Return the ABI-specified alignment of a type, in
173786fa4311c8a330957ff5b765fbb0a7750ecd38c9Ken Dyck/// characters. This method does not work on incomplete types.
17384ba2a17694148e16eaa8d3917f657ffcd3667be4Jay FoadCharUnits ASTContext::getTypeAlignInChars(QualType T) const {
1739eb6f5dc86531f794ba7746a2da4d28e37cf5da7eKen Dyck  return toCharUnitsFromBits(getTypeAlign(T));
174086fa4311c8a330957ff5b765fbb0a7750ecd38c9Ken Dyck}
17414ba2a17694148e16eaa8d3917f657ffcd3667be4Jay FoadCharUnits ASTContext::getTypeAlignInChars(const Type *T) const {
1742eb6f5dc86531f794ba7746a2da4d28e37cf5da7eKen Dyck  return toCharUnitsFromBits(getTypeAlign(T));
174386fa4311c8a330957ff5b765fbb0a7750ecd38c9Ken Dyck}
174486fa4311c8a330957ff5b765fbb0a7750ecd38c9Ken Dyck
174534ebde404dc17d89487b07e6daaf1b47d5dfee39Chris Lattner/// getPreferredTypeAlign - Return the "preferred" alignment of the specified
174634ebde404dc17d89487b07e6daaf1b47d5dfee39Chris Lattner/// type for the current target in bits.  This can be different than the ABI
174734ebde404dc17d89487b07e6daaf1b47d5dfee39Chris Lattner/// alignment in cases where it is beneficial for performance to overalign
174834ebde404dc17d89487b07e6daaf1b47d5dfee39Chris Lattner/// a data type.
17494ba2a17694148e16eaa8d3917f657ffcd3667be4Jay Foadunsigned ASTContext::getPreferredTypeAlign(const Type *T) const {
175034ebde404dc17d89487b07e6daaf1b47d5dfee39Chris Lattner  unsigned ABIAlign = getTypeAlign(T);
17511eed60297ef4701b899c6a3b9680bf08f3403422Eli Friedman
17521eed60297ef4701b899c6a3b9680bf08f3403422Eli Friedman  // Double and long long should be naturally aligned if possible.
1753183700f494ec9b6701b6efe82bcb25f4c79ba561John McCall  if (const ComplexType* CT = T->getAs<ComplexType>())
17541eed60297ef4701b899c6a3b9680bf08f3403422Eli Friedman    T = CT->getElementType().getTypePtr();
17551eed60297ef4701b899c6a3b9680bf08f3403422Eli Friedman  if (T->isSpecificBuiltinType(BuiltinType::Double) ||
1756cde7a1dc68af2eb063a039b5a31c3b7dd92b1aa9Chad Rosier      T->isSpecificBuiltinType(BuiltinType::LongLong) ||
1757cde7a1dc68af2eb063a039b5a31c3b7dd92b1aa9Chad Rosier      T->isSpecificBuiltinType(BuiltinType::ULongLong))
17581eed60297ef4701b899c6a3b9680bf08f3403422Eli Friedman    return std::max(ABIAlign, (unsigned)getTypeSize(T));
17591eed60297ef4701b899c6a3b9680bf08f3403422Eli Friedman
176034ebde404dc17d89487b07e6daaf1b47d5dfee39Chris Lattner  return ABIAlign;
176134ebde404dc17d89487b07e6daaf1b47d5dfee39Chris Lattner}
176234ebde404dc17d89487b07e6daaf1b47d5dfee39Chris Lattner
17636b20351a1d6178addfaa86716aaba36f2e9ea188Ulrich Weigand/// getAlignOfGlobalVar - Return the alignment in bits that should be given
17646b20351a1d6178addfaa86716aaba36f2e9ea188Ulrich Weigand/// to a global variable of the specified type.
17656b20351a1d6178addfaa86716aaba36f2e9ea188Ulrich Weigandunsigned ASTContext::getAlignOfGlobalVar(QualType T) const {
17666b20351a1d6178addfaa86716aaba36f2e9ea188Ulrich Weigand  return std::max(getTypeAlign(T), getTargetInfo().getMinGlobalAlign());
17676b20351a1d6178addfaa86716aaba36f2e9ea188Ulrich Weigand}
17686b20351a1d6178addfaa86716aaba36f2e9ea188Ulrich Weigand
17696b20351a1d6178addfaa86716aaba36f2e9ea188Ulrich Weigand/// getAlignOfGlobalVarInChars - Return the alignment in characters that
17706b20351a1d6178addfaa86716aaba36f2e9ea188Ulrich Weigand/// should be given to a global variable of the specified type.
17716b20351a1d6178addfaa86716aaba36f2e9ea188Ulrich WeigandCharUnits ASTContext::getAlignOfGlobalVarInChars(QualType T) const {
17726b20351a1d6178addfaa86716aaba36f2e9ea188Ulrich Weigand  return toCharUnitsFromBits(getAlignOfGlobalVar(T));
17736b20351a1d6178addfaa86716aaba36f2e9ea188Ulrich Weigand}
17746b20351a1d6178addfaa86716aaba36f2e9ea188Ulrich Weigand
17752c18bb7c9fca66c30b6eabbdcbc6399d24a54fa9Fariborz Jahanian/// DeepCollectObjCIvars -
17762c18bb7c9fca66c30b6eabbdcbc6399d24a54fa9Fariborz Jahanian/// This routine first collects all declared, but not synthesized, ivars in
17772c18bb7c9fca66c30b6eabbdcbc6399d24a54fa9Fariborz Jahanian/// super class and then collects all ivars, including those synthesized for
17782c18bb7c9fca66c30b6eabbdcbc6399d24a54fa9Fariborz Jahanian/// current class. This routine is used for implementation of current class
17792c18bb7c9fca66c30b6eabbdcbc6399d24a54fa9Fariborz Jahanian/// when all ivars, declared and synthesized are known.
17809820074dd47d37681085e964cd3392ac0b3e67b9Fariborz Jahanian///
17812c18bb7c9fca66c30b6eabbdcbc6399d24a54fa9Fariborz Jahanianvoid ASTContext::DeepCollectObjCIvars(const ObjCInterfaceDecl *OI,
17822c18bb7c9fca66c30b6eabbdcbc6399d24a54fa9Fariborz Jahanian                                      bool leafClass,
1783db8264e4c5ffd7af6fbad4ca4306bd382bb02691Jordy Rose                            SmallVectorImpl<const ObjCIvarDecl*> &Ivars) const {
17842c18bb7c9fca66c30b6eabbdcbc6399d24a54fa9Fariborz Jahanian  if (const ObjCInterfaceDecl *SuperClass = OI->getSuperClass())
17852c18bb7c9fca66c30b6eabbdcbc6399d24a54fa9Fariborz Jahanian    DeepCollectObjCIvars(SuperClass, false, Ivars);
17862c18bb7c9fca66c30b6eabbdcbc6399d24a54fa9Fariborz Jahanian  if (!leafClass) {
17872c18bb7c9fca66c30b6eabbdcbc6399d24a54fa9Fariborz Jahanian    for (ObjCInterfaceDecl::ivar_iterator I = OI->ivar_begin(),
17882c18bb7c9fca66c30b6eabbdcbc6399d24a54fa9Fariborz Jahanian         E = OI->ivar_end(); I != E; ++I)
1789581deb3da481053c4993c7600f97acf7768caac5David Blaikie      Ivars.push_back(*I);
17903060178ad9df29789505c1e6debcfc80a3a13587Chad Rosier  } else {
1791bf9eb88792e022e54a658657bf22e1925948e384Fariborz Jahanian    ObjCInterfaceDecl *IDecl = const_cast<ObjCInterfaceDecl *>(OI);
1792db8264e4c5ffd7af6fbad4ca4306bd382bb02691Jordy Rose    for (const ObjCIvarDecl *Iv = IDecl->all_declared_ivar_begin(); Iv;
1793bf9eb88792e022e54a658657bf22e1925948e384Fariborz Jahanian         Iv= Iv->getNextIvar())
1794bf9eb88792e022e54a658657bf22e1925948e384Fariborz Jahanian      Ivars.push_back(Iv);
1795bf9eb88792e022e54a658657bf22e1925948e384Fariborz Jahanian  }
17969820074dd47d37681085e964cd3392ac0b3e67b9Fariborz Jahanian}
17979820074dd47d37681085e964cd3392ac0b3e67b9Fariborz Jahanian
1798e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanian/// CollectInheritedProtocols - Collect all protocols in current class and
1799e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanian/// those inherited by it.
1800e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanianvoid ASTContext::CollectInheritedProtocols(const Decl *CDecl,
1801432a8893f7e30d141d7f279bd00b741a3cdac81fFariborz Jahanian                          llvm::SmallPtrSet<ObjCProtocolDecl*, 8> &Protocols) {
1802e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanian  if (const ObjCInterfaceDecl *OI = dyn_cast<ObjCInterfaceDecl>(CDecl)) {
180353b9441b5a81a24fa1f66f3f6416f1e36baa9c2fTed Kremenek    // We can use protocol_iterator here instead of
180453b9441b5a81a24fa1f66f3f6416f1e36baa9c2fTed Kremenek    // all_referenced_protocol_iterator since we are walking all categories.
180553b9441b5a81a24fa1f66f3f6416f1e36baa9c2fTed Kremenek    for (ObjCInterfaceDecl::all_protocol_iterator P = OI->all_referenced_protocol_begin(),
180653b9441b5a81a24fa1f66f3f6416f1e36baa9c2fTed Kremenek         PE = OI->all_referenced_protocol_end(); P != PE; ++P) {
1807e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanian      ObjCProtocolDecl *Proto = (*P);
18083fc73ee0c613715ebce78e30b4d050ea715a007dDouglas Gregor      Protocols.insert(Proto->getCanonicalDecl());
1809e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanian      for (ObjCProtocolDecl::protocol_iterator P = Proto->protocol_begin(),
1810b2f812165676230bce5d0215e49a4749c451ca9cFariborz Jahanian           PE = Proto->protocol_end(); P != PE; ++P) {
18113fc73ee0c613715ebce78e30b4d050ea715a007dDouglas Gregor        Protocols.insert((*P)->getCanonicalDecl());
1812e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanian        CollectInheritedProtocols(*P, Protocols);
1813e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanian      }
1814b2f812165676230bce5d0215e49a4749c451ca9cFariborz Jahanian    }
1815e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanian
1816e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanian    // Categories of this Interface.
1817d329724745b49f894b768d47275b7c2713106e89Douglas Gregor    for (ObjCInterfaceDecl::visible_categories_iterator
1818d329724745b49f894b768d47275b7c2713106e89Douglas Gregor           Cat = OI->visible_categories_begin(),
1819d329724745b49f894b768d47275b7c2713106e89Douglas Gregor           CatEnd = OI->visible_categories_end();
1820d329724745b49f894b768d47275b7c2713106e89Douglas Gregor         Cat != CatEnd; ++Cat) {
1821d329724745b49f894b768d47275b7c2713106e89Douglas Gregor      CollectInheritedProtocols(*Cat, Protocols);
1822d329724745b49f894b768d47275b7c2713106e89Douglas Gregor    }
1823d329724745b49f894b768d47275b7c2713106e89Douglas Gregor
1824e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanian    if (ObjCInterfaceDecl *SD = OI->getSuperClass())
1825e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanian      while (SD) {
1826e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanian        CollectInheritedProtocols(SD, Protocols);
1827e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanian        SD = SD->getSuperClass();
1828e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanian      }
1829b170ca5f4a8397c10e52050ff5df6885a3e6eca9Benjamin Kramer  } else if (const ObjCCategoryDecl *OC = dyn_cast<ObjCCategoryDecl>(CDecl)) {
183053b9441b5a81a24fa1f66f3f6416f1e36baa9c2fTed Kremenek    for (ObjCCategoryDecl::protocol_iterator P = OC->protocol_begin(),
1831e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanian         PE = OC->protocol_end(); P != PE; ++P) {
1832e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanian      ObjCProtocolDecl *Proto = (*P);
18333fc73ee0c613715ebce78e30b4d050ea715a007dDouglas Gregor      Protocols.insert(Proto->getCanonicalDecl());
1834e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanian      for (ObjCProtocolDecl::protocol_iterator P = Proto->protocol_begin(),
1835e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanian           PE = Proto->protocol_end(); P != PE; ++P)
1836e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanian        CollectInheritedProtocols(*P, Protocols);
1837e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanian    }
1838b170ca5f4a8397c10e52050ff5df6885a3e6eca9Benjamin Kramer  } else if (const ObjCProtocolDecl *OP = dyn_cast<ObjCProtocolDecl>(CDecl)) {
1839e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanian    for (ObjCProtocolDecl::protocol_iterator P = OP->protocol_begin(),
1840e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanian         PE = OP->protocol_end(); P != PE; ++P) {
1841e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanian      ObjCProtocolDecl *Proto = (*P);
18423fc73ee0c613715ebce78e30b4d050ea715a007dDouglas Gregor      Protocols.insert(Proto->getCanonicalDecl());
1843e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanian      for (ObjCProtocolDecl::protocol_iterator P = Proto->protocol_begin(),
1844e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanian           PE = Proto->protocol_end(); P != PE; ++P)
1845e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanian        CollectInheritedProtocols(*P, Protocols);
1846e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanian    }
1847e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanian  }
1848e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanian}
1849e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanian
18504ba2a17694148e16eaa8d3917f657ffcd3667be4Jay Foadunsigned ASTContext::CountNonClassIvars(const ObjCInterfaceDecl *OI) const {
18513bfacdf6f0706987c1b33c625cd68fb880881c22Fariborz Jahanian  unsigned count = 0;
18523bfacdf6f0706987c1b33c625cd68fb880881c22Fariborz Jahanian  // Count ivars declared in class extension.
1853d329724745b49f894b768d47275b7c2713106e89Douglas Gregor  for (ObjCInterfaceDecl::known_extensions_iterator
1854d329724745b49f894b768d47275b7c2713106e89Douglas Gregor         Ext = OI->known_extensions_begin(),
1855d329724745b49f894b768d47275b7c2713106e89Douglas Gregor         ExtEnd = OI->known_extensions_end();
1856d329724745b49f894b768d47275b7c2713106e89Douglas Gregor       Ext != ExtEnd; ++Ext) {
1857d329724745b49f894b768d47275b7c2713106e89Douglas Gregor    count += Ext->ivar_size();
1858d329724745b49f894b768d47275b7c2713106e89Douglas Gregor  }
1859d329724745b49f894b768d47275b7c2713106e89Douglas Gregor
18603bfacdf6f0706987c1b33c625cd68fb880881c22Fariborz Jahanian  // Count ivar defined in this class's implementation.  This
18613bfacdf6f0706987c1b33c625cd68fb880881c22Fariborz Jahanian  // includes synthesized ivars.
18623bfacdf6f0706987c1b33c625cd68fb880881c22Fariborz Jahanian  if (ObjCImplementationDecl *ImplDecl = OI->getImplementation())
1863b170ca5f4a8397c10e52050ff5df6885a3e6eca9Benjamin Kramer    count += ImplDecl->ivar_size();
1864b170ca5f4a8397c10e52050ff5df6885a3e6eca9Benjamin Kramer
18658e6ac1d80055fa37b9b84029c7e751624ba7f84cFariborz Jahanian  return count;
18668e6ac1d80055fa37b9b84029c7e751624ba7f84cFariborz Jahanian}
18678e6ac1d80055fa37b9b84029c7e751624ba7f84cFariborz Jahanian
18688deabc133c121f6c5561d0b2171a41cb2c29b2ceArgyrios Kyrtzidisbool ASTContext::isSentinelNullExpr(const Expr *E) {
18698deabc133c121f6c5561d0b2171a41cb2c29b2ceArgyrios Kyrtzidis  if (!E)
18708deabc133c121f6c5561d0b2171a41cb2c29b2ceArgyrios Kyrtzidis    return false;
18718deabc133c121f6c5561d0b2171a41cb2c29b2ceArgyrios Kyrtzidis
18728deabc133c121f6c5561d0b2171a41cb2c29b2ceArgyrios Kyrtzidis  // nullptr_t is always treated as null.
18738deabc133c121f6c5561d0b2171a41cb2c29b2ceArgyrios Kyrtzidis  if (E->getType()->isNullPtrType()) return true;
18748deabc133c121f6c5561d0b2171a41cb2c29b2ceArgyrios Kyrtzidis
18758deabc133c121f6c5561d0b2171a41cb2c29b2ceArgyrios Kyrtzidis  if (E->getType()->isAnyPointerType() &&
18768deabc133c121f6c5561d0b2171a41cb2c29b2ceArgyrios Kyrtzidis      E->IgnoreParenCasts()->isNullPointerConstant(*this,
18778deabc133c121f6c5561d0b2171a41cb2c29b2ceArgyrios Kyrtzidis                                                Expr::NPC_ValueDependentIsNull))
18788deabc133c121f6c5561d0b2171a41cb2c29b2ceArgyrios Kyrtzidis    return true;
18798deabc133c121f6c5561d0b2171a41cb2c29b2ceArgyrios Kyrtzidis
18808deabc133c121f6c5561d0b2171a41cb2c29b2ceArgyrios Kyrtzidis  // Unfortunately, __null has type 'int'.
18818deabc133c121f6c5561d0b2171a41cb2c29b2ceArgyrios Kyrtzidis  if (isa<GNUNullExpr>(E)) return true;
18828deabc133c121f6c5561d0b2171a41cb2c29b2ceArgyrios Kyrtzidis
18838deabc133c121f6c5561d0b2171a41cb2c29b2ceArgyrios Kyrtzidis  return false;
18848deabc133c121f6c5561d0b2171a41cb2c29b2ceArgyrios Kyrtzidis}
18858deabc133c121f6c5561d0b2171a41cb2c29b2ceArgyrios Kyrtzidis
18868a1d722f13df383600f36d77f842957c8adb5f1bArgyrios Kyrtzidis/// \brief Get the implementation of ObjCInterfaceDecl,or NULL if none exists.
18878a1d722f13df383600f36d77f842957c8adb5f1bArgyrios KyrtzidisObjCImplementationDecl *ASTContext::getObjCImplementation(ObjCInterfaceDecl *D) {
18888a1d722f13df383600f36d77f842957c8adb5f1bArgyrios Kyrtzidis  llvm::DenseMap<ObjCContainerDecl*, ObjCImplDecl*>::iterator
18898a1d722f13df383600f36d77f842957c8adb5f1bArgyrios Kyrtzidis    I = ObjCImpls.find(D);
18908a1d722f13df383600f36d77f842957c8adb5f1bArgyrios Kyrtzidis  if (I != ObjCImpls.end())
18918a1d722f13df383600f36d77f842957c8adb5f1bArgyrios Kyrtzidis    return cast<ObjCImplementationDecl>(I->second);
18928a1d722f13df383600f36d77f842957c8adb5f1bArgyrios Kyrtzidis  return 0;
18938a1d722f13df383600f36d77f842957c8adb5f1bArgyrios Kyrtzidis}
18948a1d722f13df383600f36d77f842957c8adb5f1bArgyrios Kyrtzidis/// \brief Get the implementation of ObjCCategoryDecl, or NULL if none exists.
18958a1d722f13df383600f36d77f842957c8adb5f1bArgyrios KyrtzidisObjCCategoryImplDecl *ASTContext::getObjCImplementation(ObjCCategoryDecl *D) {
18968a1d722f13df383600f36d77f842957c8adb5f1bArgyrios Kyrtzidis  llvm::DenseMap<ObjCContainerDecl*, ObjCImplDecl*>::iterator
18978a1d722f13df383600f36d77f842957c8adb5f1bArgyrios Kyrtzidis    I = ObjCImpls.find(D);
18988a1d722f13df383600f36d77f842957c8adb5f1bArgyrios Kyrtzidis  if (I != ObjCImpls.end())
18998a1d722f13df383600f36d77f842957c8adb5f1bArgyrios Kyrtzidis    return cast<ObjCCategoryImplDecl>(I->second);
19008a1d722f13df383600f36d77f842957c8adb5f1bArgyrios Kyrtzidis  return 0;
19018a1d722f13df383600f36d77f842957c8adb5f1bArgyrios Kyrtzidis}
19028a1d722f13df383600f36d77f842957c8adb5f1bArgyrios Kyrtzidis
19038a1d722f13df383600f36d77f842957c8adb5f1bArgyrios Kyrtzidis/// \brief Set the implementation of ObjCInterfaceDecl.
19048a1d722f13df383600f36d77f842957c8adb5f1bArgyrios Kyrtzidisvoid ASTContext::setObjCImplementation(ObjCInterfaceDecl *IFaceD,
19058a1d722f13df383600f36d77f842957c8adb5f1bArgyrios Kyrtzidis                           ObjCImplementationDecl *ImplD) {
19068a1d722f13df383600f36d77f842957c8adb5f1bArgyrios Kyrtzidis  assert(IFaceD && ImplD && "Passed null params");
19078a1d722f13df383600f36d77f842957c8adb5f1bArgyrios Kyrtzidis  ObjCImpls[IFaceD] = ImplD;
19088a1d722f13df383600f36d77f842957c8adb5f1bArgyrios Kyrtzidis}
19098a1d722f13df383600f36d77f842957c8adb5f1bArgyrios Kyrtzidis/// \brief Set the implementation of ObjCCategoryDecl.
19108a1d722f13df383600f36d77f842957c8adb5f1bArgyrios Kyrtzidisvoid ASTContext::setObjCImplementation(ObjCCategoryDecl *CatD,
19118a1d722f13df383600f36d77f842957c8adb5f1bArgyrios Kyrtzidis                           ObjCCategoryImplDecl *ImplD) {
19128a1d722f13df383600f36d77f842957c8adb5f1bArgyrios Kyrtzidis  assert(CatD && ImplD && "Passed null params");
19138a1d722f13df383600f36d77f842957c8adb5f1bArgyrios Kyrtzidis  ObjCImpls[CatD] = ImplD;
19148a1d722f13df383600f36d77f842957c8adb5f1bArgyrios Kyrtzidis}
19158a1d722f13df383600f36d77f842957c8adb5f1bArgyrios Kyrtzidis
1916b35cc2d46c4188a0b2d094b3104ce69092c34802Dmitri Gribenkoconst ObjCInterfaceDecl *ASTContext::getObjContainingInterface(
1917b35cc2d46c4188a0b2d094b3104ce69092c34802Dmitri Gribenko                                              const NamedDecl *ND) const {
1918b35cc2d46c4188a0b2d094b3104ce69092c34802Dmitri Gribenko  if (const ObjCInterfaceDecl *ID =
1919b35cc2d46c4188a0b2d094b3104ce69092c34802Dmitri Gribenko          dyn_cast<ObjCInterfaceDecl>(ND->getDeclContext()))
192087ec9c257c99b4136af6c7f5be5a2d486906ba84Argyrios Kyrtzidis    return ID;
1921b35cc2d46c4188a0b2d094b3104ce69092c34802Dmitri Gribenko  if (const ObjCCategoryDecl *CD =
1922b35cc2d46c4188a0b2d094b3104ce69092c34802Dmitri Gribenko          dyn_cast<ObjCCategoryDecl>(ND->getDeclContext()))
192387ec9c257c99b4136af6c7f5be5a2d486906ba84Argyrios Kyrtzidis    return CD->getClassInterface();
1924b35cc2d46c4188a0b2d094b3104ce69092c34802Dmitri Gribenko  if (const ObjCImplDecl *IMD =
1925b35cc2d46c4188a0b2d094b3104ce69092c34802Dmitri Gribenko          dyn_cast<ObjCImplDecl>(ND->getDeclContext()))
192687ec9c257c99b4136af6c7f5be5a2d486906ba84Argyrios Kyrtzidis    return IMD->getClassInterface();
192787ec9c257c99b4136af6c7f5be5a2d486906ba84Argyrios Kyrtzidis
192887ec9c257c99b4136af6c7f5be5a2d486906ba84Argyrios Kyrtzidis  return 0;
192987ec9c257c99b4136af6c7f5be5a2d486906ba84Argyrios Kyrtzidis}
193087ec9c257c99b4136af6c7f5be5a2d486906ba84Argyrios Kyrtzidis
19311ceee5c42d5c410217f67d384eecc6ea4a2bba9bFariborz Jahanian/// \brief Get the copy initialization expression of VarDecl,or NULL if
19321ceee5c42d5c410217f67d384eecc6ea4a2bba9bFariborz Jahanian/// none exists.
1933830937bc1100fba7682f7c32c40512085870f50cFariborz JahanianExpr *ASTContext::getBlockVarCopyInits(const VarDecl*VD) {
1934d016ec204c54d766c4561d43548551ffde0e6b43Fariborz Jahanian  assert(VD && "Passed null params");
1935d016ec204c54d766c4561d43548551ffde0e6b43Fariborz Jahanian  assert(VD->hasAttr<BlocksAttr>() &&
1936d016ec204c54d766c4561d43548551ffde0e6b43Fariborz Jahanian         "getBlockVarCopyInits - not __block var");
1937830937bc1100fba7682f7c32c40512085870f50cFariborz Jahanian  llvm::DenseMap<const VarDecl*, Expr*>::iterator
1938d016ec204c54d766c4561d43548551ffde0e6b43Fariborz Jahanian    I = BlockVarCopyInits.find(VD);
19391ceee5c42d5c410217f67d384eecc6ea4a2bba9bFariborz Jahanian  return (I != BlockVarCopyInits.end()) ? cast<Expr>(I->second) : 0;
19401ceee5c42d5c410217f67d384eecc6ea4a2bba9bFariborz Jahanian}
19411ceee5c42d5c410217f67d384eecc6ea4a2bba9bFariborz Jahanian
19421ceee5c42d5c410217f67d384eecc6ea4a2bba9bFariborz Jahanian/// \brief Set the copy inialization expression of a block var decl.
19431ceee5c42d5c410217f67d384eecc6ea4a2bba9bFariborz Jahanianvoid ASTContext::setBlockVarCopyInits(VarDecl*VD, Expr* Init) {
19441ceee5c42d5c410217f67d384eecc6ea4a2bba9bFariborz Jahanian  assert(VD && Init && "Passed null params");
1945d016ec204c54d766c4561d43548551ffde0e6b43Fariborz Jahanian  assert(VD->hasAttr<BlocksAttr>() &&
1946d016ec204c54d766c4561d43548551ffde0e6b43Fariborz Jahanian         "setBlockVarCopyInits - not __block var");
19471ceee5c42d5c410217f67d384eecc6ea4a2bba9bFariborz Jahanian  BlockVarCopyInits[VD] = Init;
19481ceee5c42d5c410217f67d384eecc6ea4a2bba9bFariborz Jahanian}
19491ceee5c42d5c410217f67d384eecc6ea4a2bba9bFariborz Jahanian
1950a93c934af4fbf97cbe8e649d82e68ccacfe57c95John McCallTypeSourceInfo *ASTContext::CreateTypeSourceInfo(QualType T,
19514ba2a17694148e16eaa8d3917f657ffcd3667be4Jay Foad                                                 unsigned DataSize) const {
1952109de5ead1dfcb3bc985cddb8cb3ed5bcecad88dJohn McCall  if (!DataSize)
1953109de5ead1dfcb3bc985cddb8cb3ed5bcecad88dJohn McCall    DataSize = TypeLoc::getFullDataSizeForType(T);
1954109de5ead1dfcb3bc985cddb8cb3ed5bcecad88dJohn McCall  else
1955109de5ead1dfcb3bc985cddb8cb3ed5bcecad88dJohn McCall    assert(DataSize == TypeLoc::getFullDataSizeForType(T) &&
1956a93c934af4fbf97cbe8e649d82e68ccacfe57c95John McCall           "incorrect data size provided to CreateTypeSourceInfo!");
1957109de5ead1dfcb3bc985cddb8cb3ed5bcecad88dJohn McCall
1958a93c934af4fbf97cbe8e649d82e68ccacfe57c95John McCall  TypeSourceInfo *TInfo =
1959a93c934af4fbf97cbe8e649d82e68ccacfe57c95John McCall    (TypeSourceInfo*)BumpAlloc.Allocate(sizeof(TypeSourceInfo) + DataSize, 8);
1960a93c934af4fbf97cbe8e649d82e68ccacfe57c95John McCall  new (TInfo) TypeSourceInfo(T);
1961a93c934af4fbf97cbe8e649d82e68ccacfe57c95John McCall  return TInfo;
1962b17166c8077cd900cca83a895c43b30ea6660598Argyrios Kyrtzidis}
1963b17166c8077cd900cca83a895c43b30ea6660598Argyrios Kyrtzidis
1964a93c934af4fbf97cbe8e649d82e68ccacfe57c95John McCallTypeSourceInfo *ASTContext::getTrivialTypeSourceInfo(QualType T,
19656952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor                                                     SourceLocation L) const {
1966a93c934af4fbf97cbe8e649d82e68ccacfe57c95John McCall  TypeSourceInfo *DI = CreateTypeSourceInfo(T);
1967c21c7e9c2cded68f91be15be6847c9649242dc17Douglas Gregor  DI->getTypeLoc().initialize(const_cast<ASTContext &>(*this), L);
1968a4eb74d4dfe126c686dc708fec444c85ffb73b47John McCall  return DI;
1969a4eb74d4dfe126c686dc708fec444c85ffb73b47John McCall}
1970a4eb74d4dfe126c686dc708fec444c85ffb73b47John McCall
1971b2dbbb99e12806eaaf53b7ccabc32f42b5719443Daniel Dunbarconst ASTRecordLayout &
19724ba2a17694148e16eaa8d3917f657ffcd3667be4Jay FoadASTContext::getASTObjCInterfaceLayout(const ObjCInterfaceDecl *D) const {
1973b2dbbb99e12806eaaf53b7ccabc32f42b5719443Daniel Dunbar  return getObjCLayout(D, 0);
1974b2dbbb99e12806eaaf53b7ccabc32f42b5719443Daniel Dunbar}
1975b2dbbb99e12806eaaf53b7ccabc32f42b5719443Daniel Dunbar
1976b2dbbb99e12806eaaf53b7ccabc32f42b5719443Daniel Dunbarconst ASTRecordLayout &
19774ba2a17694148e16eaa8d3917f657ffcd3667be4Jay FoadASTContext::getASTObjCImplementationLayout(
19784ba2a17694148e16eaa8d3917f657ffcd3667be4Jay Foad                                        const ObjCImplementationDecl *D) const {
1979b2dbbb99e12806eaaf53b7ccabc32f42b5719443Daniel Dunbar  return getObjCLayout(D->getClassInterface(), D);
1980b2dbbb99e12806eaaf53b7ccabc32f42b5719443Daniel Dunbar}
1981b2dbbb99e12806eaaf53b7ccabc32f42b5719443Daniel Dunbar
1982a7674d8a9a69f3f6fe16e70cf2a3b2b15fb7c43dChris Lattner//===----------------------------------------------------------------------===//
1983a7674d8a9a69f3f6fe16e70cf2a3b2b15fb7c43dChris Lattner//                   Type creation/memoization methods
1984a7674d8a9a69f3f6fe16e70cf2a3b2b15fb7c43dChris Lattner//===----------------------------------------------------------------------===//
1985a7674d8a9a69f3f6fe16e70cf2a3b2b15fb7c43dChris Lattner
19864ba2a17694148e16eaa8d3917f657ffcd3667be4Jay FoadQualType
19873b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCallASTContext::getExtQualType(const Type *baseType, Qualifiers quals) const {
19883b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall  unsigned fastQuals = quals.getFastQualifiers();
19893b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall  quals.removeFastQualifiers();
19900953e767ff7817f97b3ab20896b229891eeff45bJohn McCall
19910953e767ff7817f97b3ab20896b229891eeff45bJohn McCall  // Check if we've already instantiated this type.
19920953e767ff7817f97b3ab20896b229891eeff45bJohn McCall  llvm::FoldingSetNodeID ID;
19933b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall  ExtQuals::Profile(ID, baseType, quals);
19943b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall  void *insertPos = 0;
19953b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall  if (ExtQuals *eq = ExtQualNodes.FindNodeOrInsertPos(ID, insertPos)) {
19963b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall    assert(eq->getQualifiers() == quals);
19973b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall    return QualType(eq, fastQuals);
19980953e767ff7817f97b3ab20896b229891eeff45bJohn McCall  }
19990953e767ff7817f97b3ab20896b229891eeff45bJohn McCall
20003b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall  // If the base type is not canonical, make the appropriate canonical type.
20013b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall  QualType canon;
20023b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall  if (!baseType->isCanonicalUnqualified()) {
20033b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall    SplitQualType canonSplit = baseType->getCanonicalTypeInternal().split();
2004200fa53fd420aa8369586f569dbece04930ad6a3John McCall    canonSplit.Quals.addConsistentQualifiers(quals);
2005200fa53fd420aa8369586f569dbece04930ad6a3John McCall    canon = getExtQualType(canonSplit.Ty, canonSplit.Quals);
20063b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall
20073b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall    // Re-find the insert position.
20083b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall    (void) ExtQualNodes.FindNodeOrInsertPos(ID, insertPos);
20093b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall  }
20103b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall
20113b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall  ExtQuals *eq = new (*this, TypeAlignment) ExtQuals(baseType, canon, quals);
20123b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall  ExtQualNodes.InsertNode(eq, insertPos);
20133b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall  return QualType(eq, fastQuals);
20140953e767ff7817f97b3ab20896b229891eeff45bJohn McCall}
20150953e767ff7817f97b3ab20896b229891eeff45bJohn McCall
20164ba2a17694148e16eaa8d3917f657ffcd3667be4Jay FoadQualType
20174ba2a17694148e16eaa8d3917f657ffcd3667be4Jay FoadASTContext::getAddrSpaceQualType(QualType T, unsigned AddressSpace) const {
2018f52ab250ff92bc51a9ac9a8e19bd43b63a5f844fChris Lattner  QualType CanT = getCanonicalType(T);
2019f52ab250ff92bc51a9ac9a8e19bd43b63a5f844fChris Lattner  if (CanT.getAddressSpace() == AddressSpace)
2020f46699ce225811d8d9dbab9d00189a0e54469457Chris Lattner    return T;
2021b7d2553edd2532d29b98b9e76bcf6a62bc48b417Chris Lattner
20220953e767ff7817f97b3ab20896b229891eeff45bJohn McCall  // If we are composing extended qualifiers together, merge together
20230953e767ff7817f97b3ab20896b229891eeff45bJohn McCall  // into one ExtQuals node.
20240953e767ff7817f97b3ab20896b229891eeff45bJohn McCall  QualifierCollector Quals;
20250953e767ff7817f97b3ab20896b229891eeff45bJohn McCall  const Type *TypeNode = Quals.strip(T);
20261eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
20270953e767ff7817f97b3ab20896b229891eeff45bJohn McCall  // If this type already has an address space specified, it cannot get
20280953e767ff7817f97b3ab20896b229891eeff45bJohn McCall  // another one.
20290953e767ff7817f97b3ab20896b229891eeff45bJohn McCall  assert(!Quals.hasAddressSpace() &&
20300953e767ff7817f97b3ab20896b229891eeff45bJohn McCall         "Type cannot be in multiple addr spaces!");
20310953e767ff7817f97b3ab20896b229891eeff45bJohn McCall  Quals.addAddressSpace(AddressSpace);
20321eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
20330953e767ff7817f97b3ab20896b229891eeff45bJohn McCall  return getExtQualType(TypeNode, Quals);
2034ebb97e98c03f8d7034bd3748a10e35f39a95c289Christopher Lamb}
2035ebb97e98c03f8d7034bd3748a10e35f39a95c289Christopher Lamb
2036b7d2553edd2532d29b98b9e76bcf6a62bc48b417Chris LattnerQualType ASTContext::getObjCGCQualType(QualType T,
20374ba2a17694148e16eaa8d3917f657ffcd3667be4Jay Foad                                       Qualifiers::GC GCAttr) const {
2038d33d9c0cc0cfdcd0b10f35a6acdfb25da4a64f19Fariborz Jahanian  QualType CanT = getCanonicalType(T);
2039b7d2553edd2532d29b98b9e76bcf6a62bc48b417Chris Lattner  if (CanT.getObjCGCAttr() == GCAttr)
2040d33d9c0cc0cfdcd0b10f35a6acdfb25da4a64f19Fariborz Jahanian    return T;
20411eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
20427f040a9d817cd1c72b565e92abff473510bf9e1dJohn McCall  if (const PointerType *ptr = T->getAs<PointerType>()) {
20437f040a9d817cd1c72b565e92abff473510bf9e1dJohn McCall    QualType Pointee = ptr->getPointeeType();
204458f9f2c884af6b72d036b746a016d8031d31cb7aSteve Naroff    if (Pointee->isAnyPointerType()) {
20454027cd1b924e29784a49085b1717f35cdd719146Fariborz Jahanian      QualType ResultType = getObjCGCQualType(Pointee, GCAttr);
20464027cd1b924e29784a49085b1717f35cdd719146Fariborz Jahanian      return getPointerType(ResultType);
20474027cd1b924e29784a49085b1717f35cdd719146Fariborz Jahanian    }
20484027cd1b924e29784a49085b1717f35cdd719146Fariborz Jahanian  }
20491eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
20500953e767ff7817f97b3ab20896b229891eeff45bJohn McCall  // If we are composing extended qualifiers together, merge together
20510953e767ff7817f97b3ab20896b229891eeff45bJohn McCall  // into one ExtQuals node.
20520953e767ff7817f97b3ab20896b229891eeff45bJohn McCall  QualifierCollector Quals;
20530953e767ff7817f97b3ab20896b229891eeff45bJohn McCall  const Type *TypeNode = Quals.strip(T);
20541eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
20550953e767ff7817f97b3ab20896b229891eeff45bJohn McCall  // If this type already has an ObjCGC specified, it cannot get
20560953e767ff7817f97b3ab20896b229891eeff45bJohn McCall  // another one.
20570953e767ff7817f97b3ab20896b229891eeff45bJohn McCall  assert(!Quals.hasObjCGCAttr() &&
20580953e767ff7817f97b3ab20896b229891eeff45bJohn McCall         "Type cannot have multiple ObjCGCs!");
20590953e767ff7817f97b3ab20896b229891eeff45bJohn McCall  Quals.addObjCGCAttr(GCAttr);
20601eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
20610953e767ff7817f97b3ab20896b229891eeff45bJohn McCall  return getExtQualType(TypeNode, Quals);
2062d33d9c0cc0cfdcd0b10f35a6acdfb25da4a64f19Fariborz Jahanian}
2063a7674d8a9a69f3f6fe16e70cf2a3b2b15fb7c43dChris Lattner
2064e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCallconst FunctionType *ASTContext::adjustFunctionType(const FunctionType *T,
2065e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall                                                   FunctionType::ExtInfo Info) {
2066e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall  if (T->getExtInfo() == Info)
2067e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall    return T;
2068e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall
2069e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall  QualType Result;
2070e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall  if (const FunctionNoProtoType *FNPT = dyn_cast<FunctionNoProtoType>(T)) {
2071e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall    Result = getFunctionNoProtoType(FNPT->getResultType(), Info);
2072e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall  } else {
2073e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall    const FunctionProtoType *FPT = cast<FunctionProtoType>(T);
2074e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall    FunctionProtoType::ExtProtoInfo EPI = FPT->getExtProtoInfo();
2075e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall    EPI.ExtInfo = Info;
2076bea522ff43a3f11c7a2bc7949119dbb9fce19e39Jordan Rose    Result = getFunctionType(FPT->getResultType(),
2077bea522ff43a3f11c7a2bc7949119dbb9fce19e39Jordan Rose                             ArrayRef<QualType>(FPT->arg_type_begin(),
2078bea522ff43a3f11c7a2bc7949119dbb9fce19e39Jordan Rose                                                FPT->getNumArgs()),
2079bea522ff43a3f11c7a2bc7949119dbb9fce19e39Jordan Rose                             EPI);
2080e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall  }
2081e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall
2082e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall  return cast<FunctionType>(Result.getTypePtr());
2083e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall}
2084e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall
208560e141e1f87211ca831de6821003d80fe20a06f3Richard Smithvoid ASTContext::adjustDeducedFunctionResultType(FunctionDecl *FD,
208660e141e1f87211ca831de6821003d80fe20a06f3Richard Smith                                                 QualType ResultType) {
20879dadfab2faebe40e7dbbfd0801c15174b69bd726Richard Smith  FD = FD->getMostRecentDecl();
20889dadfab2faebe40e7dbbfd0801c15174b69bd726Richard Smith  while (true) {
208960e141e1f87211ca831de6821003d80fe20a06f3Richard Smith    const FunctionProtoType *FPT = FD->getType()->castAs<FunctionProtoType>();
209060e141e1f87211ca831de6821003d80fe20a06f3Richard Smith    FunctionProtoType::ExtProtoInfo EPI = FPT->getExtProtoInfo();
209160e141e1f87211ca831de6821003d80fe20a06f3Richard Smith    FD->setType(getFunctionType(ResultType, FPT->getArgTypes(), EPI));
20929dadfab2faebe40e7dbbfd0801c15174b69bd726Richard Smith    if (FunctionDecl *Next = FD->getPreviousDecl())
20939dadfab2faebe40e7dbbfd0801c15174b69bd726Richard Smith      FD = Next;
20949dadfab2faebe40e7dbbfd0801c15174b69bd726Richard Smith    else
20959dadfab2faebe40e7dbbfd0801c15174b69bd726Richard Smith      break;
209660e141e1f87211ca831de6821003d80fe20a06f3Richard Smith  }
20979dadfab2faebe40e7dbbfd0801c15174b69bd726Richard Smith  if (ASTMutationListener *L = getASTMutationListener())
20989dadfab2faebe40e7dbbfd0801c15174b69bd726Richard Smith    L->DeducedReturnType(FD, ResultType);
209960e141e1f87211ca831de6821003d80fe20a06f3Richard Smith}
210060e141e1f87211ca831de6821003d80fe20a06f3Richard Smith
21015f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// getComplexType - Return the uniqued reference to the type for a complex
21025f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// number with the specified element type.
21034ba2a17694148e16eaa8d3917f657ffcd3667be4Jay FoadQualType ASTContext::getComplexType(QualType T) const {
21045f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // Unique pointers, to guarantee there is only one pointer of a particular
21055f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // structure.
21065f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  llvm::FoldingSetNodeID ID;
21075f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  ComplexType::Profile(ID, T);
21081eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
21095f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  void *InsertPos = 0;
21105f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  if (ComplexType *CT = ComplexTypes.FindNodeOrInsertPos(ID, InsertPos))
21115f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    return QualType(CT, 0);
21121eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
21135f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // If the pointee type isn't canonical, this won't be a canonical type either,
21145f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // so fill in the canonical type field.
21155f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  QualType Canonical;
2116467b27b9a24bdc823218ad1ad0e37673b6cc1e83John McCall  if (!T.isCanonical()) {
2117f52ab250ff92bc51a9ac9a8e19bd43b63a5f844fChris Lattner    Canonical = getComplexType(getCanonicalType(T));
21181eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
21195f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    // Get the new insert position for the node we care about.
21205f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    ComplexType *NewIP = ComplexTypes.FindNodeOrInsertPos(ID, InsertPos);
2121c6ed729f669044f5072a49d79041f455d971ece3Jeffrey Yasskin    assert(NewIP == 0 && "Shouldn't be in the map!"); (void)NewIP;
21225f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
21236b304a0254a13f42390b865ff5ba668a49cc58aeJohn McCall  ComplexType *New = new (*this, TypeAlignment) ComplexType(T, Canonical);
21245f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  Types.push_back(New);
21255f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  ComplexTypes.InsertNode(New, InsertPos);
21265f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  return QualType(New, 0);
21275f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
21285f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
21295f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// getPointerType - Return the uniqued reference to the type for a pointer to
21305f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// the specified type.
21314ba2a17694148e16eaa8d3917f657ffcd3667be4Jay FoadQualType ASTContext::getPointerType(QualType T) const {
21325f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // Unique pointers, to guarantee there is only one pointer of a particular
21335f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // structure.
21345f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  llvm::FoldingSetNodeID ID;
21355f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  PointerType::Profile(ID, T);
21361eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
21375f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  void *InsertPos = 0;
21385f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  if (PointerType *PT = PointerTypes.FindNodeOrInsertPos(ID, InsertPos))
21395f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    return QualType(PT, 0);
21401eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
21415f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // If the pointee type isn't canonical, this won't be a canonical type either,
21425f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // so fill in the canonical type field.
21435f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  QualType Canonical;
2144c90cc9374f18396251760b9fe6468fd1232e9f0eBob Wilson  if (!T.isCanonical()) {
2145f52ab250ff92bc51a9ac9a8e19bd43b63a5f844fChris Lattner    Canonical = getPointerType(getCanonicalType(T));
21461eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
2147c90cc9374f18396251760b9fe6468fd1232e9f0eBob Wilson    // Get the new insert position for the node we care about.
2148c90cc9374f18396251760b9fe6468fd1232e9f0eBob Wilson    PointerType *NewIP = PointerTypes.FindNodeOrInsertPos(ID, InsertPos);
2149c90cc9374f18396251760b9fe6468fd1232e9f0eBob Wilson    assert(NewIP == 0 && "Shouldn't be in the map!"); (void)NewIP;
2150c90cc9374f18396251760b9fe6468fd1232e9f0eBob Wilson  }
21516b304a0254a13f42390b865ff5ba668a49cc58aeJohn McCall  PointerType *New = new (*this, TypeAlignment) PointerType(T, Canonical);
21525f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  Types.push_back(New);
21535f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  PointerTypes.InsertNode(New, InsertPos);
21545f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  return QualType(New, 0);
21555f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
21565f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
21571eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// getBlockPointerType - Return the uniqued reference to the type for
21585618bd4a52c45fbbb605e3ba885663b2164db8a3Steve Naroff/// a pointer to the specified block.
21594ba2a17694148e16eaa8d3917f657ffcd3667be4Jay FoadQualType ASTContext::getBlockPointerType(QualType T) const {
2160296e8d5fdcf9946f51e866adc8d281379e51efe9Steve Naroff  assert(T->isFunctionType() && "block of function types only");
2161296e8d5fdcf9946f51e866adc8d281379e51efe9Steve Naroff  // Unique pointers, to guarantee there is only one block of a particular
21625618bd4a52c45fbbb605e3ba885663b2164db8a3Steve Naroff  // structure.
21635618bd4a52c45fbbb605e3ba885663b2164db8a3Steve Naroff  llvm::FoldingSetNodeID ID;
21645618bd4a52c45fbbb605e3ba885663b2164db8a3Steve Naroff  BlockPointerType::Profile(ID, T);
21651eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
21665618bd4a52c45fbbb605e3ba885663b2164db8a3Steve Naroff  void *InsertPos = 0;
21675618bd4a52c45fbbb605e3ba885663b2164db8a3Steve Naroff  if (BlockPointerType *PT =
21685618bd4a52c45fbbb605e3ba885663b2164db8a3Steve Naroff        BlockPointerTypes.FindNodeOrInsertPos(ID, InsertPos))
21695618bd4a52c45fbbb605e3ba885663b2164db8a3Steve Naroff    return QualType(PT, 0);
21701eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
21711eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  // If the block pointee type isn't canonical, this won't be a canonical
21725618bd4a52c45fbbb605e3ba885663b2164db8a3Steve Naroff  // type either so fill in the canonical type field.
21735618bd4a52c45fbbb605e3ba885663b2164db8a3Steve Naroff  QualType Canonical;
2174467b27b9a24bdc823218ad1ad0e37673b6cc1e83John McCall  if (!T.isCanonical()) {
21755618bd4a52c45fbbb605e3ba885663b2164db8a3Steve Naroff    Canonical = getBlockPointerType(getCanonicalType(T));
21761eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
21775618bd4a52c45fbbb605e3ba885663b2164db8a3Steve Naroff    // Get the new insert position for the node we care about.
21785618bd4a52c45fbbb605e3ba885663b2164db8a3Steve Naroff    BlockPointerType *NewIP =
21795618bd4a52c45fbbb605e3ba885663b2164db8a3Steve Naroff      BlockPointerTypes.FindNodeOrInsertPos(ID, InsertPos);
2180c6ed729f669044f5072a49d79041f455d971ece3Jeffrey Yasskin    assert(NewIP == 0 && "Shouldn't be in the map!"); (void)NewIP;
21815618bd4a52c45fbbb605e3ba885663b2164db8a3Steve Naroff  }
21826b304a0254a13f42390b865ff5ba668a49cc58aeJohn McCall  BlockPointerType *New
21836b304a0254a13f42390b865ff5ba668a49cc58aeJohn McCall    = new (*this, TypeAlignment) BlockPointerType(T, Canonical);
21845618bd4a52c45fbbb605e3ba885663b2164db8a3Steve Naroff  Types.push_back(New);
21855618bd4a52c45fbbb605e3ba885663b2164db8a3Steve Naroff  BlockPointerTypes.InsertNode(New, InsertPos);
21865618bd4a52c45fbbb605e3ba885663b2164db8a3Steve Naroff  return QualType(New, 0);
21875618bd4a52c45fbbb605e3ba885663b2164db8a3Steve Naroff}
21885618bd4a52c45fbbb605e3ba885663b2164db8a3Steve Naroff
21897c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl/// getLValueReferenceType - Return the uniqued reference to the type for an
21907c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl/// lvalue reference to the specified type.
21914ba2a17694148e16eaa8d3917f657ffcd3667be4Jay FoadQualType
21924ba2a17694148e16eaa8d3917f657ffcd3667be4Jay FoadASTContext::getLValueReferenceType(QualType T, bool SpelledAsLValue) const {
21939625e44c0252485277a340746ed8ac950686156fDouglas Gregor  assert(getCanonicalType(T) != OverloadTy &&
21949625e44c0252485277a340746ed8ac950686156fDouglas Gregor         "Unresolved overloaded function type");
21959625e44c0252485277a340746ed8ac950686156fDouglas Gregor
21965f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // Unique pointers, to guarantee there is only one pointer of a particular
21975f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // structure.
21985f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  llvm::FoldingSetNodeID ID;
219954e14c4db764c0636160d26c5bbf491637c83a76John McCall  ReferenceType::Profile(ID, T, SpelledAsLValue);
22005f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
22015f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  void *InsertPos = 0;
22027c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl  if (LValueReferenceType *RT =
22037c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl        LValueReferenceTypes.FindNodeOrInsertPos(ID, InsertPos))
22045f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    return QualType(RT, 0);
22057c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl
220654e14c4db764c0636160d26c5bbf491637c83a76John McCall  const ReferenceType *InnerRef = T->getAs<ReferenceType>();
220754e14c4db764c0636160d26c5bbf491637c83a76John McCall
22085f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // If the referencee type isn't canonical, this won't be a canonical type
22095f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // either, so fill in the canonical type field.
22105f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  QualType Canonical;
221154e14c4db764c0636160d26c5bbf491637c83a76John McCall  if (!SpelledAsLValue || InnerRef || !T.isCanonical()) {
221254e14c4db764c0636160d26c5bbf491637c83a76John McCall    QualType PointeeType = (InnerRef ? InnerRef->getPointeeType() : T);
221354e14c4db764c0636160d26c5bbf491637c83a76John McCall    Canonical = getLValueReferenceType(getCanonicalType(PointeeType));
22147c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl
22157c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl    // Get the new insert position for the node we care about.
22167c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl    LValueReferenceType *NewIP =
22177c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl      LValueReferenceTypes.FindNodeOrInsertPos(ID, InsertPos);
2218c6ed729f669044f5072a49d79041f455d971ece3Jeffrey Yasskin    assert(NewIP == 0 && "Shouldn't be in the map!"); (void)NewIP;
22197c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl  }
22207c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl
22216b304a0254a13f42390b865ff5ba668a49cc58aeJohn McCall  LValueReferenceType *New
222254e14c4db764c0636160d26c5bbf491637c83a76John McCall    = new (*this, TypeAlignment) LValueReferenceType(T, Canonical,
222354e14c4db764c0636160d26c5bbf491637c83a76John McCall                                                     SpelledAsLValue);
22247c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl  Types.push_back(New);
22257c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl  LValueReferenceTypes.InsertNode(New, InsertPos);
222654e14c4db764c0636160d26c5bbf491637c83a76John McCall
22277c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl  return QualType(New, 0);
22287c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl}
22297c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl
22307c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl/// getRValueReferenceType - Return the uniqued reference to the type for an
22317c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl/// rvalue reference to the specified type.
22324ba2a17694148e16eaa8d3917f657ffcd3667be4Jay FoadQualType ASTContext::getRValueReferenceType(QualType T) const {
22337c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl  // Unique pointers, to guarantee there is only one pointer of a particular
22347c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl  // structure.
22357c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl  llvm::FoldingSetNodeID ID;
223654e14c4db764c0636160d26c5bbf491637c83a76John McCall  ReferenceType::Profile(ID, T, false);
22377c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl
22387c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl  void *InsertPos = 0;
22397c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl  if (RValueReferenceType *RT =
22407c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl        RValueReferenceTypes.FindNodeOrInsertPos(ID, InsertPos))
22417c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl    return QualType(RT, 0);
22427c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl
224354e14c4db764c0636160d26c5bbf491637c83a76John McCall  const ReferenceType *InnerRef = T->getAs<ReferenceType>();
224454e14c4db764c0636160d26c5bbf491637c83a76John McCall
22457c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl  // If the referencee type isn't canonical, this won't be a canonical type
22467c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl  // either, so fill in the canonical type field.
22477c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl  QualType Canonical;
224854e14c4db764c0636160d26c5bbf491637c83a76John McCall  if (InnerRef || !T.isCanonical()) {
224954e14c4db764c0636160d26c5bbf491637c83a76John McCall    QualType PointeeType = (InnerRef ? InnerRef->getPointeeType() : T);
225054e14c4db764c0636160d26c5bbf491637c83a76John McCall    Canonical = getRValueReferenceType(getCanonicalType(PointeeType));
22517c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl
22525f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    // Get the new insert position for the node we care about.
22537c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl    RValueReferenceType *NewIP =
22547c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl      RValueReferenceTypes.FindNodeOrInsertPos(ID, InsertPos);
2255c6ed729f669044f5072a49d79041f455d971ece3Jeffrey Yasskin    assert(NewIP == 0 && "Shouldn't be in the map!"); (void)NewIP;
22565f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
22575f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
22586b304a0254a13f42390b865ff5ba668a49cc58aeJohn McCall  RValueReferenceType *New
22596b304a0254a13f42390b865ff5ba668a49cc58aeJohn McCall    = new (*this, TypeAlignment) RValueReferenceType(T, Canonical);
22605f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  Types.push_back(New);
22617c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl  RValueReferenceTypes.InsertNode(New, InsertPos);
22625f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  return QualType(New, 0);
22635f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
22645f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
2265f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl/// getMemberPointerType - Return the uniqued reference to the type for a
2266f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl/// member pointer to the specified type, in the specified class.
22674ba2a17694148e16eaa8d3917f657ffcd3667be4Jay FoadQualType ASTContext::getMemberPointerType(QualType T, const Type *Cls) const {
2268f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl  // Unique pointers, to guarantee there is only one pointer of a particular
2269f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl  // structure.
2270f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl  llvm::FoldingSetNodeID ID;
2271f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl  MemberPointerType::Profile(ID, T, Cls);
2272f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl
2273f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl  void *InsertPos = 0;
2274f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl  if (MemberPointerType *PT =
2275f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl      MemberPointerTypes.FindNodeOrInsertPos(ID, InsertPos))
2276f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl    return QualType(PT, 0);
2277f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl
2278f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl  // If the pointee or class type isn't canonical, this won't be a canonical
2279f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl  // type either, so fill in the canonical type field.
2280f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl  QualType Canonical;
228187c12c4a4667279dacb3d4a93c64b49148a0ff79Douglas Gregor  if (!T.isCanonical() || !Cls->isCanonicalUnqualified()) {
2282f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl    Canonical = getMemberPointerType(getCanonicalType(T),getCanonicalType(Cls));
2283f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl
2284f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl    // Get the new insert position for the node we care about.
2285f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl    MemberPointerType *NewIP =
2286f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl      MemberPointerTypes.FindNodeOrInsertPos(ID, InsertPos);
2287c6ed729f669044f5072a49d79041f455d971ece3Jeffrey Yasskin    assert(NewIP == 0 && "Shouldn't be in the map!"); (void)NewIP;
2288f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl  }
22896b304a0254a13f42390b865ff5ba668a49cc58aeJohn McCall  MemberPointerType *New
22906b304a0254a13f42390b865ff5ba668a49cc58aeJohn McCall    = new (*this, TypeAlignment) MemberPointerType(T, Cls, Canonical);
2291f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl  Types.push_back(New);
2292f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl  MemberPointerTypes.InsertNode(New, InsertPos);
2293f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl  return QualType(New, 0);
2294f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl}
2295f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl
22961eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// getConstantArrayType - Return the unique reference to the type for an
2297fb22d96692c5240fb8d611290dbf7eeed3759c73Steve Naroff/// array of the specified element type.
22981eb4433ac451dc16f4133a88af2d002ac26c58efMike StumpQualType ASTContext::getConstantArrayType(QualType EltTy,
229938aeec7299c48cb79523f7f89776fb258c84aeeaChris Lattner                                          const llvm::APInt &ArySizeIn,
2300c9406125e2cac9208098655ac8058c095c2c3a65Steve Naroff                                          ArrayType::ArraySizeModifier ASM,
230163e7d25d2e6036616b42f744fd4a39cd5f911960Abramo Bagnara                                          unsigned IndexTypeQuals) const {
2302923d56d436f750bc1f29db50e641078725558a1bSebastian Redl  assert((EltTy->isDependentType() ||
2303923d56d436f750bc1f29db50e641078725558a1bSebastian Redl          EltTy->isIncompleteType() || EltTy->isConstantSizeType()) &&
2304587cbdfd95f4b0aaccc14b31f5debe85d5daf7edEli Friedman         "Constant array of VLAs is illegal!");
2305587cbdfd95f4b0aaccc14b31f5debe85d5daf7edEli Friedman
230638aeec7299c48cb79523f7f89776fb258c84aeeaChris Lattner  // Convert the array size into a canonical width matching the pointer size for
230738aeec7299c48cb79523f7f89776fb258c84aeeaChris Lattner  // the target.
230838aeec7299c48cb79523f7f89776fb258c84aeeaChris Lattner  llvm::APInt ArySize(ArySizeIn);
23099f71a8f4c7a182a5236da9e747d57cc1d1bd24c2Jay Foad  ArySize =
2310bcfd1f55bfbb3e5944cd5e03d07b343e280838c4Douglas Gregor    ArySize.zextOrTrunc(Target->getPointerWidth(getTargetAddressSpace(EltTy)));
23111eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
23125f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  llvm::FoldingSetNodeID ID;
231363e7d25d2e6036616b42f744fd4a39cd5f911960Abramo Bagnara  ConstantArrayType::Profile(ID, EltTy, ArySize, ASM, IndexTypeQuals);
23141eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
23155f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  void *InsertPos = 0;
23161eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  if (ConstantArrayType *ATP =
23177192f8e9592729882a09d84d77838db26e39ebd4Ted Kremenek      ConstantArrayTypes.FindNodeOrInsertPos(ID, InsertPos))
23185f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    return QualType(ATP, 0);
23191eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
23203b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall  // If the element type isn't canonical or has qualifiers, this won't
23213b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall  // be a canonical type either, so fill in the canonical type field.
23223b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall  QualType Canon;
23233b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall  if (!EltTy.isCanonical() || EltTy.hasLocalQualifiers()) {
23243b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall    SplitQualType canonSplit = getCanonicalType(EltTy).split();
2325200fa53fd420aa8369586f569dbece04930ad6a3John McCall    Canon = getConstantArrayType(QualType(canonSplit.Ty, 0), ArySize,
232663e7d25d2e6036616b42f744fd4a39cd5f911960Abramo Bagnara                                 ASM, IndexTypeQuals);
2327200fa53fd420aa8369586f569dbece04930ad6a3John McCall    Canon = getQualifiedType(Canon, canonSplit.Quals);
23283b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall
23295f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    // Get the new insert position for the node we care about.
23301eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    ConstantArrayType *NewIP =
23317192f8e9592729882a09d84d77838db26e39ebd4Ted Kremenek      ConstantArrayTypes.FindNodeOrInsertPos(ID, InsertPos);
2332c6ed729f669044f5072a49d79041f455d971ece3Jeffrey Yasskin    assert(NewIP == 0 && "Shouldn't be in the map!"); (void)NewIP;
23335f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
23341eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
23356b304a0254a13f42390b865ff5ba668a49cc58aeJohn McCall  ConstantArrayType *New = new(*this,TypeAlignment)
233663e7d25d2e6036616b42f744fd4a39cd5f911960Abramo Bagnara    ConstantArrayType(EltTy, Canon, ArySize, ASM, IndexTypeQuals);
23377192f8e9592729882a09d84d77838db26e39ebd4Ted Kremenek  ConstantArrayTypes.InsertNode(New, InsertPos);
23385f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  Types.push_back(New);
23395f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  return QualType(New, 0);
23405f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
23415f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
2342ce8890371fcdb983ae487c87fa40606a34896ff7John McCall/// getVariableArrayDecayedType - Turns the given type, which may be
2343ce8890371fcdb983ae487c87fa40606a34896ff7John McCall/// variably-modified, into the corresponding type with all the known
2344ce8890371fcdb983ae487c87fa40606a34896ff7John McCall/// sizes replaced with [*].
2345ce8890371fcdb983ae487c87fa40606a34896ff7John McCallQualType ASTContext::getVariableArrayDecayedType(QualType type) const {
2346ce8890371fcdb983ae487c87fa40606a34896ff7John McCall  // Vastly most common case.
2347ce8890371fcdb983ae487c87fa40606a34896ff7John McCall  if (!type->isVariablyModifiedType()) return type;
2348ce8890371fcdb983ae487c87fa40606a34896ff7John McCall
2349ce8890371fcdb983ae487c87fa40606a34896ff7John McCall  QualType result;
2350ce8890371fcdb983ae487c87fa40606a34896ff7John McCall
2351ce8890371fcdb983ae487c87fa40606a34896ff7John McCall  SplitQualType split = type.getSplitDesugaredType();
2352200fa53fd420aa8369586f569dbece04930ad6a3John McCall  const Type *ty = split.Ty;
2353ce8890371fcdb983ae487c87fa40606a34896ff7John McCall  switch (ty->getTypeClass()) {
2354ce8890371fcdb983ae487c87fa40606a34896ff7John McCall#define TYPE(Class, Base)
2355ce8890371fcdb983ae487c87fa40606a34896ff7John McCall#define ABSTRACT_TYPE(Class, Base)
2356ce8890371fcdb983ae487c87fa40606a34896ff7John McCall#define NON_CANONICAL_TYPE(Class, Base) case Type::Class:
2357ce8890371fcdb983ae487c87fa40606a34896ff7John McCall#include "clang/AST/TypeNodes.def"
2358ce8890371fcdb983ae487c87fa40606a34896ff7John McCall    llvm_unreachable("didn't desugar past all non-canonical types?");
2359ce8890371fcdb983ae487c87fa40606a34896ff7John McCall
2360ce8890371fcdb983ae487c87fa40606a34896ff7John McCall  // These types should never be variably-modified.
2361ce8890371fcdb983ae487c87fa40606a34896ff7John McCall  case Type::Builtin:
2362ce8890371fcdb983ae487c87fa40606a34896ff7John McCall  case Type::Complex:
2363ce8890371fcdb983ae487c87fa40606a34896ff7John McCall  case Type::Vector:
2364ce8890371fcdb983ae487c87fa40606a34896ff7John McCall  case Type::ExtVector:
2365ce8890371fcdb983ae487c87fa40606a34896ff7John McCall  case Type::DependentSizedExtVector:
2366ce8890371fcdb983ae487c87fa40606a34896ff7John McCall  case Type::ObjCObject:
2367ce8890371fcdb983ae487c87fa40606a34896ff7John McCall  case Type::ObjCInterface:
2368ce8890371fcdb983ae487c87fa40606a34896ff7John McCall  case Type::ObjCObjectPointer:
2369ce8890371fcdb983ae487c87fa40606a34896ff7John McCall  case Type::Record:
2370ce8890371fcdb983ae487c87fa40606a34896ff7John McCall  case Type::Enum:
2371ce8890371fcdb983ae487c87fa40606a34896ff7John McCall  case Type::UnresolvedUsing:
2372ce8890371fcdb983ae487c87fa40606a34896ff7John McCall  case Type::TypeOfExpr:
2373ce8890371fcdb983ae487c87fa40606a34896ff7John McCall  case Type::TypeOf:
2374ce8890371fcdb983ae487c87fa40606a34896ff7John McCall  case Type::Decltype:
2375ca63c200346c0ca9e00194ec6e34a5a7b0ed9321Sean Hunt  case Type::UnaryTransform:
2376ce8890371fcdb983ae487c87fa40606a34896ff7John McCall  case Type::DependentName:
2377ce8890371fcdb983ae487c87fa40606a34896ff7John McCall  case Type::InjectedClassName:
2378ce8890371fcdb983ae487c87fa40606a34896ff7John McCall  case Type::TemplateSpecialization:
2379ce8890371fcdb983ae487c87fa40606a34896ff7John McCall  case Type::DependentTemplateSpecialization:
2380ce8890371fcdb983ae487c87fa40606a34896ff7John McCall  case Type::TemplateTypeParm:
2381ce8890371fcdb983ae487c87fa40606a34896ff7John McCall  case Type::SubstTemplateTypeParmPack:
238234b41d939a1328f484511c6002ba2456db879a29Richard Smith  case Type::Auto:
2383ce8890371fcdb983ae487c87fa40606a34896ff7John McCall  case Type::PackExpansion:
2384ce8890371fcdb983ae487c87fa40606a34896ff7John McCall    llvm_unreachable("type should never be variably-modified");
2385ce8890371fcdb983ae487c87fa40606a34896ff7John McCall
2386ce8890371fcdb983ae487c87fa40606a34896ff7John McCall  // These types can be variably-modified but should never need to
2387ce8890371fcdb983ae487c87fa40606a34896ff7John McCall  // further decay.
2388ce8890371fcdb983ae487c87fa40606a34896ff7John McCall  case Type::FunctionNoProto:
2389ce8890371fcdb983ae487c87fa40606a34896ff7John McCall  case Type::FunctionProto:
2390ce8890371fcdb983ae487c87fa40606a34896ff7John McCall  case Type::BlockPointer:
2391ce8890371fcdb983ae487c87fa40606a34896ff7John McCall  case Type::MemberPointer:
2392ce8890371fcdb983ae487c87fa40606a34896ff7John McCall    return type;
2393ce8890371fcdb983ae487c87fa40606a34896ff7John McCall
2394ce8890371fcdb983ae487c87fa40606a34896ff7John McCall  // These types can be variably-modified.  All these modifications
2395ce8890371fcdb983ae487c87fa40606a34896ff7John McCall  // preserve structure except as noted by comments.
2396ce8890371fcdb983ae487c87fa40606a34896ff7John McCall  // TODO: if we ever care about optimizing VLAs, there are no-op
2397ce8890371fcdb983ae487c87fa40606a34896ff7John McCall  // optimizations available here.
2398ce8890371fcdb983ae487c87fa40606a34896ff7John McCall  case Type::Pointer:
2399ce8890371fcdb983ae487c87fa40606a34896ff7John McCall    result = getPointerType(getVariableArrayDecayedType(
2400ce8890371fcdb983ae487c87fa40606a34896ff7John McCall                              cast<PointerType>(ty)->getPointeeType()));
2401ce8890371fcdb983ae487c87fa40606a34896ff7John McCall    break;
2402ce8890371fcdb983ae487c87fa40606a34896ff7John McCall
2403ce8890371fcdb983ae487c87fa40606a34896ff7John McCall  case Type::LValueReference: {
2404ce8890371fcdb983ae487c87fa40606a34896ff7John McCall    const LValueReferenceType *lv = cast<LValueReferenceType>(ty);
2405ce8890371fcdb983ae487c87fa40606a34896ff7John McCall    result = getLValueReferenceType(
2406ce8890371fcdb983ae487c87fa40606a34896ff7John McCall                 getVariableArrayDecayedType(lv->getPointeeType()),
2407ce8890371fcdb983ae487c87fa40606a34896ff7John McCall                                    lv->isSpelledAsLValue());
2408ce8890371fcdb983ae487c87fa40606a34896ff7John McCall    break;
2409745da3a5bb4ea35f93f50301e7fbbb7d78d3b6bbFariborz Jahanian  }
2410ce8890371fcdb983ae487c87fa40606a34896ff7John McCall
2411ce8890371fcdb983ae487c87fa40606a34896ff7John McCall  case Type::RValueReference: {
2412ce8890371fcdb983ae487c87fa40606a34896ff7John McCall    const RValueReferenceType *lv = cast<RValueReferenceType>(ty);
2413ce8890371fcdb983ae487c87fa40606a34896ff7John McCall    result = getRValueReferenceType(
2414ce8890371fcdb983ae487c87fa40606a34896ff7John McCall                 getVariableArrayDecayedType(lv->getPointeeType()));
2415ce8890371fcdb983ae487c87fa40606a34896ff7John McCall    break;
2416745da3a5bb4ea35f93f50301e7fbbb7d78d3b6bbFariborz Jahanian  }
2417745da3a5bb4ea35f93f50301e7fbbb7d78d3b6bbFariborz Jahanian
2418b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman  case Type::Atomic: {
2419b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman    const AtomicType *at = cast<AtomicType>(ty);
2420b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman    result = getAtomicType(getVariableArrayDecayedType(at->getValueType()));
2421b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman    break;
2422b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman  }
2423b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman
2424ce8890371fcdb983ae487c87fa40606a34896ff7John McCall  case Type::ConstantArray: {
2425ce8890371fcdb983ae487c87fa40606a34896ff7John McCall    const ConstantArrayType *cat = cast<ConstantArrayType>(ty);
2426ce8890371fcdb983ae487c87fa40606a34896ff7John McCall    result = getConstantArrayType(
2427ce8890371fcdb983ae487c87fa40606a34896ff7John McCall                 getVariableArrayDecayedType(cat->getElementType()),
2428ce8890371fcdb983ae487c87fa40606a34896ff7John McCall                                  cat->getSize(),
2429ce8890371fcdb983ae487c87fa40606a34896ff7John McCall                                  cat->getSizeModifier(),
2430ce8890371fcdb983ae487c87fa40606a34896ff7John McCall                                  cat->getIndexTypeCVRQualifiers());
2431ce8890371fcdb983ae487c87fa40606a34896ff7John McCall    break;
2432745da3a5bb4ea35f93f50301e7fbbb7d78d3b6bbFariborz Jahanian  }
2433745da3a5bb4ea35f93f50301e7fbbb7d78d3b6bbFariborz Jahanian
2434ce8890371fcdb983ae487c87fa40606a34896ff7John McCall  case Type::DependentSizedArray: {
2435ce8890371fcdb983ae487c87fa40606a34896ff7John McCall    const DependentSizedArrayType *dat = cast<DependentSizedArrayType>(ty);
2436ce8890371fcdb983ae487c87fa40606a34896ff7John McCall    result = getDependentSizedArrayType(
2437ce8890371fcdb983ae487c87fa40606a34896ff7John McCall                 getVariableArrayDecayedType(dat->getElementType()),
2438ce8890371fcdb983ae487c87fa40606a34896ff7John McCall                                        dat->getSizeExpr(),
2439ce8890371fcdb983ae487c87fa40606a34896ff7John McCall                                        dat->getSizeModifier(),
2440ce8890371fcdb983ae487c87fa40606a34896ff7John McCall                                        dat->getIndexTypeCVRQualifiers(),
2441ce8890371fcdb983ae487c87fa40606a34896ff7John McCall                                        dat->getBracketsRange());
2442ce8890371fcdb983ae487c87fa40606a34896ff7John McCall    break;
2443ce8890371fcdb983ae487c87fa40606a34896ff7John McCall  }
2444ce8890371fcdb983ae487c87fa40606a34896ff7John McCall
2445ce8890371fcdb983ae487c87fa40606a34896ff7John McCall  // Turn incomplete types into [*] types.
2446ce8890371fcdb983ae487c87fa40606a34896ff7John McCall  case Type::IncompleteArray: {
2447ce8890371fcdb983ae487c87fa40606a34896ff7John McCall    const IncompleteArrayType *iat = cast<IncompleteArrayType>(ty);
2448ce8890371fcdb983ae487c87fa40606a34896ff7John McCall    result = getVariableArrayType(
2449ce8890371fcdb983ae487c87fa40606a34896ff7John McCall                 getVariableArrayDecayedType(iat->getElementType()),
2450ce8890371fcdb983ae487c87fa40606a34896ff7John McCall                                  /*size*/ 0,
2451ce8890371fcdb983ae487c87fa40606a34896ff7John McCall                                  ArrayType::Normal,
2452ce8890371fcdb983ae487c87fa40606a34896ff7John McCall                                  iat->getIndexTypeCVRQualifiers(),
2453ce8890371fcdb983ae487c87fa40606a34896ff7John McCall                                  SourceRange());
2454ce8890371fcdb983ae487c87fa40606a34896ff7John McCall    break;
2455ce8890371fcdb983ae487c87fa40606a34896ff7John McCall  }
2456ce8890371fcdb983ae487c87fa40606a34896ff7John McCall
2457ce8890371fcdb983ae487c87fa40606a34896ff7John McCall  // Turn VLA types into [*] types.
2458ce8890371fcdb983ae487c87fa40606a34896ff7John McCall  case Type::VariableArray: {
2459ce8890371fcdb983ae487c87fa40606a34896ff7John McCall    const VariableArrayType *vat = cast<VariableArrayType>(ty);
2460ce8890371fcdb983ae487c87fa40606a34896ff7John McCall    result = getVariableArrayType(
2461ce8890371fcdb983ae487c87fa40606a34896ff7John McCall                 getVariableArrayDecayedType(vat->getElementType()),
2462ce8890371fcdb983ae487c87fa40606a34896ff7John McCall                                  /*size*/ 0,
2463ce8890371fcdb983ae487c87fa40606a34896ff7John McCall                                  ArrayType::Star,
2464ce8890371fcdb983ae487c87fa40606a34896ff7John McCall                                  vat->getIndexTypeCVRQualifiers(),
2465ce8890371fcdb983ae487c87fa40606a34896ff7John McCall                                  vat->getBracketsRange());
2466ce8890371fcdb983ae487c87fa40606a34896ff7John McCall    break;
2467ce8890371fcdb983ae487c87fa40606a34896ff7John McCall  }
2468ce8890371fcdb983ae487c87fa40606a34896ff7John McCall  }
2469ce8890371fcdb983ae487c87fa40606a34896ff7John McCall
2470ce8890371fcdb983ae487c87fa40606a34896ff7John McCall  // Apply the top-level qualifiers from the original.
2471200fa53fd420aa8369586f569dbece04930ad6a3John McCall  return getQualifiedType(result, split.Quals);
2472ce8890371fcdb983ae487c87fa40606a34896ff7John McCall}
2473745da3a5bb4ea35f93f50301e7fbbb7d78d3b6bbFariborz Jahanian
2474bdbf7b030a3e0ddb95240076683830e6f78c79a5Steve Naroff/// getVariableArrayType - Returns a non-unique reference to the type for a
2475bdbf7b030a3e0ddb95240076683830e6f78c79a5Steve Naroff/// variable array of the specified element type.
24767e7eb3da052a6d80ddf2377cab0384c798f73f75Douglas GregorQualType ASTContext::getVariableArrayType(QualType EltTy,
24777e7eb3da052a6d80ddf2377cab0384c798f73f75Douglas Gregor                                          Expr *NumElts,
2478c9406125e2cac9208098655ac8058c095c2c3a65Steve Naroff                                          ArrayType::ArraySizeModifier ASM,
247963e7d25d2e6036616b42f744fd4a39cd5f911960Abramo Bagnara                                          unsigned IndexTypeQuals,
24804ba2a17694148e16eaa8d3917f657ffcd3667be4Jay Foad                                          SourceRange Brackets) const {
2481c5773c4b8ce1ed6ed5c7112c9020c954a47dce96Eli Friedman  // Since we don't unique expressions, it isn't possible to unique VLA's
2482c5773c4b8ce1ed6ed5c7112c9020c954a47dce96Eli Friedman  // that have an expression provided for their size.
24833b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall  QualType Canon;
2484715e9c8a39437347e838aa108df443fe1086d359Douglas Gregor
24853b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall  // Be sure to pull qualifiers off the element type.
24863b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall  if (!EltTy.isCanonical() || EltTy.hasLocalQualifiers()) {
24873b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall    SplitQualType canonSplit = getCanonicalType(EltTy).split();
2488200fa53fd420aa8369586f569dbece04930ad6a3John McCall    Canon = getVariableArrayType(QualType(canonSplit.Ty, 0), NumElts, ASM,
248963e7d25d2e6036616b42f744fd4a39cd5f911960Abramo Bagnara                                 IndexTypeQuals, Brackets);
2490200fa53fd420aa8369586f569dbece04930ad6a3John McCall    Canon = getQualifiedType(Canon, canonSplit.Quals);
2491715e9c8a39437347e838aa108df443fe1086d359Douglas Gregor  }
2492715e9c8a39437347e838aa108df443fe1086d359Douglas Gregor
24936b304a0254a13f42390b865ff5ba668a49cc58aeJohn McCall  VariableArrayType *New = new(*this, TypeAlignment)
249463e7d25d2e6036616b42f744fd4a39cd5f911960Abramo Bagnara    VariableArrayType(EltTy, Canon, NumElts, ASM, IndexTypeQuals, Brackets);
2495c5773c4b8ce1ed6ed5c7112c9020c954a47dce96Eli Friedman
2496c5773c4b8ce1ed6ed5c7112c9020c954a47dce96Eli Friedman  VariableArrayTypes.push_back(New);
2497c5773c4b8ce1ed6ed5c7112c9020c954a47dce96Eli Friedman  Types.push_back(New);
2498c5773c4b8ce1ed6ed5c7112c9020c954a47dce96Eli Friedman  return QualType(New, 0);
2499c5773c4b8ce1ed6ed5c7112c9020c954a47dce96Eli Friedman}
2500c5773c4b8ce1ed6ed5c7112c9020c954a47dce96Eli Friedman
2501898574e7496ba8fd76290079d3a9d06954992734Douglas Gregor/// getDependentSizedArrayType - Returns a non-unique reference to
2502898574e7496ba8fd76290079d3a9d06954992734Douglas Gregor/// the type for a dependently-sized array of the specified element
250304d4beee4b86af20a9e4457023d3925cab8f9908Douglas Gregor/// type.
25043b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCallQualType ASTContext::getDependentSizedArrayType(QualType elementType,
25053b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall                                                Expr *numElements,
2506898574e7496ba8fd76290079d3a9d06954992734Douglas Gregor                                                ArrayType::ArraySizeModifier ASM,
25073b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall                                                unsigned elementTypeQuals,
25083b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall                                                SourceRange brackets) const {
25093b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall  assert((!numElements || numElements->isTypeDependent() ||
25103b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall          numElements->isValueDependent()) &&
2511898574e7496ba8fd76290079d3a9d06954992734Douglas Gregor         "Size must be type- or value-dependent!");
2512898574e7496ba8fd76290079d3a9d06954992734Douglas Gregor
25133b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall  // Dependently-sized array types that do not have a specified number
25143b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall  // of elements will have their sizes deduced from a dependent
25153b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall  // initializer.  We do no canonicalization here at all, which is okay
25163b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall  // because they can't be used in most locations.
25173b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall  if (!numElements) {
25183b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall    DependentSizedArrayType *newType
25193b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall      = new (*this, TypeAlignment)
25203b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall          DependentSizedArrayType(*this, elementType, QualType(),
25213b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall                                  numElements, ASM, elementTypeQuals,
25223b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall                                  brackets);
25233b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall    Types.push_back(newType);
25243b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall    return QualType(newType, 0);
2525cb78d8852a454684c987220132cdb5e54dd00121Douglas Gregor  }
2526cb78d8852a454684c987220132cdb5e54dd00121Douglas Gregor
25273b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall  // Otherwise, we actually build a new type every time, but we
25283b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall  // also build a canonical type.
25291eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
25303b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall  SplitQualType canonElementType = getCanonicalType(elementType).split();
2531898574e7496ba8fd76290079d3a9d06954992734Douglas Gregor
25323b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall  void *insertPos = 0;
25333b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall  llvm::FoldingSetNodeID ID;
25343b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall  DependentSizedArrayType::Profile(ID, *this,
2535200fa53fd420aa8369586f569dbece04930ad6a3John McCall                                   QualType(canonElementType.Ty, 0),
25363b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall                                   ASM, elementTypeQuals, numElements);
25373b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall
25383b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall  // Look for an existing type with these properties.
25393b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall  DependentSizedArrayType *canonTy =
25403b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall    DependentSizedArrayTypes.FindNodeOrInsertPos(ID, insertPos);
25413b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall
25423b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall  // If we don't have one, build one.
25433b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall  if (!canonTy) {
25443b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall    canonTy = new (*this, TypeAlignment)
2545200fa53fd420aa8369586f569dbece04930ad6a3John McCall      DependentSizedArrayType(*this, QualType(canonElementType.Ty, 0),
25463b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall                              QualType(), numElements, ASM, elementTypeQuals,
25473b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall                              brackets);
25483b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall    DependentSizedArrayTypes.InsertNode(canonTy, insertPos);
25493b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall    Types.push_back(canonTy);
25503b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall  }
25513b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall
25523b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall  // Apply qualifiers from the element type to the array.
25533b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall  QualType canon = getQualifiedType(QualType(canonTy,0),
2554200fa53fd420aa8369586f569dbece04930ad6a3John McCall                                    canonElementType.Quals);
25553b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall
25563b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall  // If we didn't need extra canonicalization for the element type,
25573b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall  // then just use that as our result.
2558200fa53fd420aa8369586f569dbece04930ad6a3John McCall  if (QualType(canonElementType.Ty, 0) == elementType)
25593b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall    return canon;
25603b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall
25613b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall  // Otherwise, we need to build a type which follows the spelling
25623b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall  // of the element type.
25633b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall  DependentSizedArrayType *sugaredType
25643b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall    = new (*this, TypeAlignment)
25653b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall        DependentSizedArrayType(*this, elementType, canon, numElements,
25663b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall                                ASM, elementTypeQuals, brackets);
25673b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall  Types.push_back(sugaredType);
25683b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall  return QualType(sugaredType, 0);
25693b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall}
25703b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall
25713b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCallQualType ASTContext::getIncompleteArrayType(QualType elementType,
2572c5773c4b8ce1ed6ed5c7112c9020c954a47dce96Eli Friedman                                            ArrayType::ArraySizeModifier ASM,
25733b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall                                            unsigned elementTypeQuals) const {
2574c5773c4b8ce1ed6ed5c7112c9020c954a47dce96Eli Friedman  llvm::FoldingSetNodeID ID;
25753b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall  IncompleteArrayType::Profile(ID, elementType, ASM, elementTypeQuals);
2576c5773c4b8ce1ed6ed5c7112c9020c954a47dce96Eli Friedman
25773b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall  void *insertPos = 0;
25783b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall  if (IncompleteArrayType *iat =
25793b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall       IncompleteArrayTypes.FindNodeOrInsertPos(ID, insertPos))
25803b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall    return QualType(iat, 0);
2581c5773c4b8ce1ed6ed5c7112c9020c954a47dce96Eli Friedman
2582c5773c4b8ce1ed6ed5c7112c9020c954a47dce96Eli Friedman  // If the element type isn't canonical, this won't be a canonical type
25833b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall  // either, so fill in the canonical type field.  We also have to pull
25843b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall  // qualifiers off the element type.
25853b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall  QualType canon;
2586c5773c4b8ce1ed6ed5c7112c9020c954a47dce96Eli Friedman
25873b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall  if (!elementType.isCanonical() || elementType.hasLocalQualifiers()) {
25883b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall    SplitQualType canonSplit = getCanonicalType(elementType).split();
2589200fa53fd420aa8369586f569dbece04930ad6a3John McCall    canon = getIncompleteArrayType(QualType(canonSplit.Ty, 0),
25903b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall                                   ASM, elementTypeQuals);
2591200fa53fd420aa8369586f569dbece04930ad6a3John McCall    canon = getQualifiedType(canon, canonSplit.Quals);
2592c5773c4b8ce1ed6ed5c7112c9020c954a47dce96Eli Friedman
2593c5773c4b8ce1ed6ed5c7112c9020c954a47dce96Eli Friedman    // Get the new insert position for the node we care about.
25943b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall    IncompleteArrayType *existing =
25953b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall      IncompleteArrayTypes.FindNodeOrInsertPos(ID, insertPos);
25963b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall    assert(!existing && "Shouldn't be in the map!"); (void) existing;
25972bd24ba6d10f8c811c8e2a57c8397e07082ba497Ted Kremenek  }
2598c5773c4b8ce1ed6ed5c7112c9020c954a47dce96Eli Friedman
25993b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall  IncompleteArrayType *newType = new (*this, TypeAlignment)
26003b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall    IncompleteArrayType(elementType, canon, ASM, elementTypeQuals);
2601c5773c4b8ce1ed6ed5c7112c9020c954a47dce96Eli Friedman
26023b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall  IncompleteArrayTypes.InsertNode(newType, insertPos);
26033b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall  Types.push_back(newType);
26043b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall  return QualType(newType, 0);
2605fb22d96692c5240fb8d611290dbf7eeed3759c73Steve Naroff}
2606fb22d96692c5240fb8d611290dbf7eeed3759c73Steve Naroff
260773322924127c873c13101b705dd823f5539ffa5fSteve Naroff/// getVectorType - Return the unique reference to a vector type of
260873322924127c873c13101b705dd823f5539ffa5fSteve Naroff/// the specified element type and size. VectorType must be a built-in type.
260982287d19ded35248c4ce6a425ce74116a13ce44eJohn ThompsonQualType ASTContext::getVectorType(QualType vecType, unsigned NumElts,
26104ba2a17694148e16eaa8d3917f657ffcd3667be4Jay Foad                                   VectorType::VectorKind VecKind) const {
26113b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall  assert(vecType->isBuiltinType());
26121eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
26135f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // Check if we've already instantiated a vector of this type.
26145f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  llvm::FoldingSetNodeID ID;
2615e86d78cf4754a6aef2cf9a33d847aa15338e276fBob Wilson  VectorType::Profile(ID, vecType, NumElts, Type::Vector, VecKind);
2616788b0fd67e1992f23555454efcdb16a19dfefac3Chris Lattner
26175f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  void *InsertPos = 0;
26185f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  if (VectorType *VTP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos))
26195f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    return QualType(VTP, 0);
26205f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
26215f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // If the element type isn't canonical, this won't be a canonical type either,
26225f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // so fill in the canonical type field.
26235f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  QualType Canonical;
2624255210ef415b9893f0e3794e8d9a704194c12f3cDouglas Gregor  if (!vecType.isCanonical()) {
2625231da7eb3dd13007e5e40fffe48998e5ef284e06Bob Wilson    Canonical = getVectorType(getCanonicalType(vecType), NumElts, VecKind);
26261eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
26275f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    // Get the new insert position for the node we care about.
26285f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    VectorType *NewIP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos);
2629c6ed729f669044f5072a49d79041f455d971ece3Jeffrey Yasskin    assert(NewIP == 0 && "Shouldn't be in the map!"); (void)NewIP;
26305f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
26316b304a0254a13f42390b865ff5ba668a49cc58aeJohn McCall  VectorType *New = new (*this, TypeAlignment)
2632e86d78cf4754a6aef2cf9a33d847aa15338e276fBob Wilson    VectorType(vecType, NumElts, Canonical, VecKind);
26335f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  VectorTypes.InsertNode(New, InsertPos);
26345f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  Types.push_back(New);
26355f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  return QualType(New, 0);
26365f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
26375f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
2638213541a68a3e137d11d2cefb612c6cdb410d7e8eNate Begeman/// getExtVectorType - Return the unique reference to an extended vector type of
263973322924127c873c13101b705dd823f5539ffa5fSteve Naroff/// the specified element type and size. VectorType must be a built-in type.
26404ba2a17694148e16eaa8d3917f657ffcd3667be4Jay FoadQualType
26414ba2a17694148e16eaa8d3917f657ffcd3667be4Jay FoadASTContext::getExtVectorType(QualType vecType, unsigned NumElts) const {
26424ac01401b1ec602a1f58c217544d3dcb5fcbd7f1Douglas Gregor  assert(vecType->isBuiltinType() || vecType->isDependentType());
26431eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
264473322924127c873c13101b705dd823f5539ffa5fSteve Naroff  // Check if we've already instantiated a vector of this type.
264573322924127c873c13101b705dd823f5539ffa5fSteve Naroff  llvm::FoldingSetNodeID ID;
2646788b0fd67e1992f23555454efcdb16a19dfefac3Chris Lattner  VectorType::Profile(ID, vecType, NumElts, Type::ExtVector,
2647e86d78cf4754a6aef2cf9a33d847aa15338e276fBob Wilson                      VectorType::GenericVector);
264873322924127c873c13101b705dd823f5539ffa5fSteve Naroff  void *InsertPos = 0;
264973322924127c873c13101b705dd823f5539ffa5fSteve Naroff  if (VectorType *VTP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos))
265073322924127c873c13101b705dd823f5539ffa5fSteve Naroff    return QualType(VTP, 0);
265173322924127c873c13101b705dd823f5539ffa5fSteve Naroff
265273322924127c873c13101b705dd823f5539ffa5fSteve Naroff  // If the element type isn't canonical, this won't be a canonical type either,
265373322924127c873c13101b705dd823f5539ffa5fSteve Naroff  // so fill in the canonical type field.
265473322924127c873c13101b705dd823f5539ffa5fSteve Naroff  QualType Canonical;
2655467b27b9a24bdc823218ad1ad0e37673b6cc1e83John McCall  if (!vecType.isCanonical()) {
2656213541a68a3e137d11d2cefb612c6cdb410d7e8eNate Begeman    Canonical = getExtVectorType(getCanonicalType(vecType), NumElts);
26571eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
265873322924127c873c13101b705dd823f5539ffa5fSteve Naroff    // Get the new insert position for the node we care about.
265973322924127c873c13101b705dd823f5539ffa5fSteve Naroff    VectorType *NewIP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos);
2660c6ed729f669044f5072a49d79041f455d971ece3Jeffrey Yasskin    assert(NewIP == 0 && "Shouldn't be in the map!"); (void)NewIP;
266173322924127c873c13101b705dd823f5539ffa5fSteve Naroff  }
26626b304a0254a13f42390b865ff5ba668a49cc58aeJohn McCall  ExtVectorType *New = new (*this, TypeAlignment)
26636b304a0254a13f42390b865ff5ba668a49cc58aeJohn McCall    ExtVectorType(vecType, NumElts, Canonical);
266473322924127c873c13101b705dd823f5539ffa5fSteve Naroff  VectorTypes.InsertNode(New, InsertPos);
266573322924127c873c13101b705dd823f5539ffa5fSteve Naroff  Types.push_back(New);
266673322924127c873c13101b705dd823f5539ffa5fSteve Naroff  return QualType(New, 0);
266773322924127c873c13101b705dd823f5539ffa5fSteve Naroff}
266873322924127c873c13101b705dd823f5539ffa5fSteve Naroff
26694ba2a17694148e16eaa8d3917f657ffcd3667be4Jay FoadQualType
26704ba2a17694148e16eaa8d3917f657ffcd3667be4Jay FoadASTContext::getDependentSizedExtVectorType(QualType vecType,
26714ba2a17694148e16eaa8d3917f657ffcd3667be4Jay Foad                                           Expr *SizeExpr,
26724ba2a17694148e16eaa8d3917f657ffcd3667be4Jay Foad                                           SourceLocation AttrLoc) const {
26732ec09f1dc123e1942ed756e8ee4fef86451eac9eDouglas Gregor  llvm::FoldingSetNodeID ID;
26741eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  DependentSizedExtVectorType::Profile(ID, *this, getCanonicalType(vecType),
26752ec09f1dc123e1942ed756e8ee4fef86451eac9eDouglas Gregor                                       SizeExpr);
26761eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
26772ec09f1dc123e1942ed756e8ee4fef86451eac9eDouglas Gregor  void *InsertPos = 0;
26782ec09f1dc123e1942ed756e8ee4fef86451eac9eDouglas Gregor  DependentSizedExtVectorType *Canon
26792ec09f1dc123e1942ed756e8ee4fef86451eac9eDouglas Gregor    = DependentSizedExtVectorTypes.FindNodeOrInsertPos(ID, InsertPos);
26802ec09f1dc123e1942ed756e8ee4fef86451eac9eDouglas Gregor  DependentSizedExtVectorType *New;
26812ec09f1dc123e1942ed756e8ee4fef86451eac9eDouglas Gregor  if (Canon) {
26822ec09f1dc123e1942ed756e8ee4fef86451eac9eDouglas Gregor    // We already have a canonical version of this array type; use it as
26832ec09f1dc123e1942ed756e8ee4fef86451eac9eDouglas Gregor    // the canonical type for a newly-built type.
26846b304a0254a13f42390b865ff5ba668a49cc58aeJohn McCall    New = new (*this, TypeAlignment)
26856b304a0254a13f42390b865ff5ba668a49cc58aeJohn McCall      DependentSizedExtVectorType(*this, vecType, QualType(Canon, 0),
26866b304a0254a13f42390b865ff5ba668a49cc58aeJohn McCall                                  SizeExpr, AttrLoc);
26872ec09f1dc123e1942ed756e8ee4fef86451eac9eDouglas Gregor  } else {
26882ec09f1dc123e1942ed756e8ee4fef86451eac9eDouglas Gregor    QualType CanonVecTy = getCanonicalType(vecType);
26892ec09f1dc123e1942ed756e8ee4fef86451eac9eDouglas Gregor    if (CanonVecTy == vecType) {
26906b304a0254a13f42390b865ff5ba668a49cc58aeJohn McCall      New = new (*this, TypeAlignment)
26916b304a0254a13f42390b865ff5ba668a49cc58aeJohn McCall        DependentSizedExtVectorType(*this, vecType, QualType(), SizeExpr,
26926b304a0254a13f42390b865ff5ba668a49cc58aeJohn McCall                                    AttrLoc);
2693789b1f640205e81b5af250693246120f1ce9d147Douglas Gregor
2694789b1f640205e81b5af250693246120f1ce9d147Douglas Gregor      DependentSizedExtVectorType *CanonCheck
2695789b1f640205e81b5af250693246120f1ce9d147Douglas Gregor        = DependentSizedExtVectorTypes.FindNodeOrInsertPos(ID, InsertPos);
2696789b1f640205e81b5af250693246120f1ce9d147Douglas Gregor      assert(!CanonCheck && "Dependent-sized ext_vector canonical type broken");
2697789b1f640205e81b5af250693246120f1ce9d147Douglas Gregor      (void)CanonCheck;
26982ec09f1dc123e1942ed756e8ee4fef86451eac9eDouglas Gregor      DependentSizedExtVectorTypes.InsertNode(New, InsertPos);
26992ec09f1dc123e1942ed756e8ee4fef86451eac9eDouglas Gregor    } else {
27002ec09f1dc123e1942ed756e8ee4fef86451eac9eDouglas Gregor      QualType Canon = getDependentSizedExtVectorType(CanonVecTy, SizeExpr,
27012ec09f1dc123e1942ed756e8ee4fef86451eac9eDouglas Gregor                                                      SourceLocation());
27026b304a0254a13f42390b865ff5ba668a49cc58aeJohn McCall      New = new (*this, TypeAlignment)
27036b304a0254a13f42390b865ff5ba668a49cc58aeJohn McCall        DependentSizedExtVectorType(*this, vecType, Canon, SizeExpr, AttrLoc);
27042ec09f1dc123e1942ed756e8ee4fef86451eac9eDouglas Gregor    }
27052ec09f1dc123e1942ed756e8ee4fef86451eac9eDouglas Gregor  }
27061eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
27079cdda0cf8528e3d595be9bfa002f0450074beb4dDouglas Gregor  Types.push_back(New);
27089cdda0cf8528e3d595be9bfa002f0450074beb4dDouglas Gregor  return QualType(New, 0);
27099cdda0cf8528e3d595be9bfa002f0450074beb4dDouglas Gregor}
27109cdda0cf8528e3d595be9bfa002f0450074beb4dDouglas Gregor
271172564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor/// getFunctionNoProtoType - Return a K&R style C function type like 'int()'.
27125f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer///
27134ba2a17694148e16eaa8d3917f657ffcd3667be4Jay FoadQualType
27144ba2a17694148e16eaa8d3917f657ffcd3667be4Jay FoadASTContext::getFunctionNoProtoType(QualType ResultTy,
27154ba2a17694148e16eaa8d3917f657ffcd3667be4Jay Foad                                   const FunctionType::ExtInfo &Info) const {
2716cfe9af250f466e7e38becea4428990448ae07737Roman Divacky  const CallingConv DefaultCC = Info.getCC();
2717cfe9af250f466e7e38becea4428990448ae07737Roman Divacky  const CallingConv CallConv = (LangOpts.MRTD && DefaultCC == CC_Default) ?
2718cfe9af250f466e7e38becea4428990448ae07737Roman Divacky                               CC_X86StdCall : DefaultCC;
27195f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // Unique functions, to guarantee there is only one function of a particular
27205f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // structure.
27215f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  llvm::FoldingSetNodeID ID;
2722264ba48dc98f3f843935a485d5b086f7e0fdc4f1Rafael Espindola  FunctionNoProtoType::Profile(ID, ResultTy, Info);
27231eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
27245f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  void *InsertPos = 0;
27251eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  if (FunctionNoProtoType *FT =
272672564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor        FunctionNoProtoTypes.FindNodeOrInsertPos(ID, InsertPos))
27275f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    return QualType(FT, 0);
27281eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
27295f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  QualType Canonical;
2730ab8bbf4ebd3e3e6eab913cb044772a62b7581941Douglas Gregor  if (!ResultTy.isCanonical() ||
273104a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall      getCanonicalCallConv(CallConv) != CallConv) {
2732264ba48dc98f3f843935a485d5b086f7e0fdc4f1Rafael Espindola    Canonical =
2733264ba48dc98f3f843935a485d5b086f7e0fdc4f1Rafael Espindola      getFunctionNoProtoType(getCanonicalType(ResultTy),
2734264ba48dc98f3f843935a485d5b086f7e0fdc4f1Rafael Espindola                     Info.withCallingConv(getCanonicalCallConv(CallConv)));
27351eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
27365f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    // Get the new insert position for the node we care about.
273772564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor    FunctionNoProtoType *NewIP =
273872564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor      FunctionNoProtoTypes.FindNodeOrInsertPos(ID, InsertPos);
2739c6ed729f669044f5072a49d79041f455d971ece3Jeffrey Yasskin    assert(NewIP == 0 && "Shouldn't be in the map!"); (void)NewIP;
27405f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
27411eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
2742cfe9af250f466e7e38becea4428990448ae07737Roman Divacky  FunctionProtoType::ExtInfo newInfo = Info.withCallingConv(CallConv);
27436b304a0254a13f42390b865ff5ba668a49cc58aeJohn McCall  FunctionNoProtoType *New = new (*this, TypeAlignment)
2744cfe9af250f466e7e38becea4428990448ae07737Roman Divacky    FunctionNoProtoType(ResultTy, Canonical, newInfo);
27455f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  Types.push_back(New);
274672564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor  FunctionNoProtoTypes.InsertNode(New, InsertPos);
27475f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  return QualType(New, 0);
27485f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
27495f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
275002dd79830979e6d83d4420377e8f4c9e4a77439bDouglas Gregor/// \brief Determine whether \p T is canonical as the result type of a function.
275102dd79830979e6d83d4420377e8f4c9e4a77439bDouglas Gregorstatic bool isCanonicalResultType(QualType T) {
275202dd79830979e6d83d4420377e8f4c9e4a77439bDouglas Gregor  return T.isCanonical() &&
275302dd79830979e6d83d4420377e8f4c9e4a77439bDouglas Gregor         (T.getObjCLifetime() == Qualifiers::OCL_None ||
275402dd79830979e6d83d4420377e8f4c9e4a77439bDouglas Gregor          T.getObjCLifetime() == Qualifiers::OCL_ExplicitNone);
275502dd79830979e6d83d4420377e8f4c9e4a77439bDouglas Gregor}
275602dd79830979e6d83d4420377e8f4c9e4a77439bDouglas Gregor
27575f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// getFunctionType - Return a normal function type with a typed argument
27585f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// list.  isVariadic indicates whether the argument list includes '...'.
27594ba2a17694148e16eaa8d3917f657ffcd3667be4Jay FoadQualType
2760bea522ff43a3f11c7a2bc7949119dbb9fce19e39Jordan RoseASTContext::getFunctionType(QualType ResultTy, ArrayRef<QualType> ArgArray,
27614ba2a17694148e16eaa8d3917f657ffcd3667be4Jay Foad                            const FunctionProtoType::ExtProtoInfo &EPI) const {
2762bea522ff43a3f11c7a2bc7949119dbb9fce19e39Jordan Rose  size_t NumArgs = ArgArray.size();
2763bea522ff43a3f11c7a2bc7949119dbb9fce19e39Jordan Rose
27645f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // Unique functions, to guarantee there is only one function of a particular
27655f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // structure.
27665f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  llvm::FoldingSetNodeID ID;
2767bea522ff43a3f11c7a2bc7949119dbb9fce19e39Jordan Rose  FunctionProtoType::Profile(ID, ResultTy, ArgArray.begin(), NumArgs, EPI,
2768bea522ff43a3f11c7a2bc7949119dbb9fce19e39Jordan Rose                             *this);
27695f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
27705f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  void *InsertPos = 0;
27711eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  if (FunctionProtoType *FTP =
277272564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor        FunctionProtoTypes.FindNodeOrInsertPos(ID, InsertPos))
27735f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    return QualType(FTP, 0);
2774465226e23a3008bd68973513dda1f9e3cd27dbddSebastian Redl
2775465226e23a3008bd68973513dda1f9e3cd27dbddSebastian Redl  // Determine whether the type being created is already canonical or not.
2776eefb3d5b49c844347f212073a7e975b8118fe8e9Richard Smith  bool isCanonical =
277702dd79830979e6d83d4420377e8f4c9e4a77439bDouglas Gregor    EPI.ExceptionSpecType == EST_None && isCanonicalResultType(ResultTy) &&
2778eefb3d5b49c844347f212073a7e975b8118fe8e9Richard Smith    !EPI.HasTrailingReturn;
27795f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  for (unsigned i = 0; i != NumArgs && isCanonical; ++i)
278054e14c4db764c0636160d26c5bbf491637c83a76John McCall    if (!ArgArray[i].isCanonicalAsParam())
27815f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      isCanonical = false;
27825f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
2783cfe9af250f466e7e38becea4428990448ae07737Roman Divacky  const CallingConv DefaultCC = EPI.ExtInfo.getCC();
2784cfe9af250f466e7e38becea4428990448ae07737Roman Divacky  const CallingConv CallConv = (LangOpts.MRTD && DefaultCC == CC_Default) ?
2785cfe9af250f466e7e38becea4428990448ae07737Roman Divacky                               CC_X86StdCall : DefaultCC;
2786e23cf437fe76b1ed02d63c3f61b456fd48a915f5John McCall
27875f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // If this type isn't canonical, get the canonical version of it.
2788465226e23a3008bd68973513dda1f9e3cd27dbddSebastian Redl  // The exception spec is not part of the canonical type.
27895f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  QualType Canonical;
279004a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall  if (!isCanonical || getCanonicalCallConv(CallConv) != CallConv) {
27915f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner    SmallVector<QualType, 16> CanonicalArgs;
27925f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    CanonicalArgs.reserve(NumArgs);
27935f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    for (unsigned i = 0; i != NumArgs; ++i)
279454e14c4db764c0636160d26c5bbf491637c83a76John McCall      CanonicalArgs.push_back(getCanonicalParamType(ArgArray[i]));
2795465226e23a3008bd68973513dda1f9e3cd27dbddSebastian Redl
2796e23cf437fe76b1ed02d63c3f61b456fd48a915f5John McCall    FunctionProtoType::ExtProtoInfo CanonicalEPI = EPI;
2797eefb3d5b49c844347f212073a7e975b8118fe8e9Richard Smith    CanonicalEPI.HasTrailingReturn = false;
27988b5b4099c61a136e9a1714c4d8a593febe942268Sebastian Redl    CanonicalEPI.ExceptionSpecType = EST_None;
27998b5b4099c61a136e9a1714c4d8a593febe942268Sebastian Redl    CanonicalEPI.NumExceptions = 0;
2800e23cf437fe76b1ed02d63c3f61b456fd48a915f5John McCall    CanonicalEPI.ExtInfo
2801e23cf437fe76b1ed02d63c3f61b456fd48a915f5John McCall      = CanonicalEPI.ExtInfo.withCallingConv(getCanonicalCallConv(CallConv));
2802e23cf437fe76b1ed02d63c3f61b456fd48a915f5John McCall
280302dd79830979e6d83d4420377e8f4c9e4a77439bDouglas Gregor    // Result types do not have ARC lifetime qualifiers.
280402dd79830979e6d83d4420377e8f4c9e4a77439bDouglas Gregor    QualType CanResultTy = getCanonicalType(ResultTy);
280502dd79830979e6d83d4420377e8f4c9e4a77439bDouglas Gregor    if (ResultTy.getQualifiers().hasObjCLifetime()) {
280602dd79830979e6d83d4420377e8f4c9e4a77439bDouglas Gregor      Qualifiers Qs = CanResultTy.getQualifiers();
280702dd79830979e6d83d4420377e8f4c9e4a77439bDouglas Gregor      Qs.removeObjCLifetime();
280802dd79830979e6d83d4420377e8f4c9e4a77439bDouglas Gregor      CanResultTy = getQualifiedType(CanResultTy.getUnqualifiedType(), Qs);
280902dd79830979e6d83d4420377e8f4c9e4a77439bDouglas Gregor    }
281002dd79830979e6d83d4420377e8f4c9e4a77439bDouglas Gregor
2811bea522ff43a3f11c7a2bc7949119dbb9fce19e39Jordan Rose    Canonical = getFunctionType(CanResultTy, CanonicalArgs, CanonicalEPI);
2812465226e23a3008bd68973513dda1f9e3cd27dbddSebastian Redl
28135f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    // Get the new insert position for the node we care about.
281472564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor    FunctionProtoType *NewIP =
281572564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor      FunctionProtoTypes.FindNodeOrInsertPos(ID, InsertPos);
2816c6ed729f669044f5072a49d79041f455d971ece3Jeffrey Yasskin    assert(NewIP == 0 && "Shouldn't be in the map!"); (void)NewIP;
28175f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
2818465226e23a3008bd68973513dda1f9e3cd27dbddSebastian Redl
2819f85e193739c953358c865005855253af4f68a497John McCall  // FunctionProtoType objects are allocated with extra bytes after
2820f85e193739c953358c865005855253af4f68a497John McCall  // them for three variable size arrays at the end:
2821f85e193739c953358c865005855253af4f68a497John McCall  //  - parameter types
2822f85e193739c953358c865005855253af4f68a497John McCall  //  - exception types
2823f85e193739c953358c865005855253af4f68a497John McCall  //  - consumed-arguments flags
2824f85e193739c953358c865005855253af4f68a497John McCall  // Instead of the exception types, there could be a noexcept
2825b9d0b76e42fd2d4cdfd135220302458d03ad09feRichard Smith  // expression, or information used to resolve the exception
2826b9d0b76e42fd2d4cdfd135220302458d03ad09feRichard Smith  // specification.
2827e23cf437fe76b1ed02d63c3f61b456fd48a915f5John McCall  size_t Size = sizeof(FunctionProtoType) +
282860618fa7f88d5162bb5b40988b6b38d4d75d6fc6Sebastian Redl                NumArgs * sizeof(QualType);
2829b9d0b76e42fd2d4cdfd135220302458d03ad09feRichard Smith  if (EPI.ExceptionSpecType == EST_Dynamic) {
283060618fa7f88d5162bb5b40988b6b38d4d75d6fc6Sebastian Redl    Size += EPI.NumExceptions * sizeof(QualType);
2831b9d0b76e42fd2d4cdfd135220302458d03ad09feRichard Smith  } else if (EPI.ExceptionSpecType == EST_ComputedNoexcept) {
28328026f6d82f7fa544bc0453714fe94bca62a1196eSebastian Redl    Size += sizeof(Expr*);
2833e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith  } else if (EPI.ExceptionSpecType == EST_Uninstantiated) {
283413bffc532bafd45d4a77867993c1afb83c7661beRichard Smith    Size += 2 * sizeof(FunctionDecl*);
2835b9d0b76e42fd2d4cdfd135220302458d03ad09feRichard Smith  } else if (EPI.ExceptionSpecType == EST_Unevaluated) {
2836b9d0b76e42fd2d4cdfd135220302458d03ad09feRichard Smith    Size += sizeof(FunctionDecl*);
283760618fa7f88d5162bb5b40988b6b38d4d75d6fc6Sebastian Redl  }
2838f85e193739c953358c865005855253af4f68a497John McCall  if (EPI.ConsumedArguments)
2839f85e193739c953358c865005855253af4f68a497John McCall    Size += NumArgs * sizeof(bool);
2840f85e193739c953358c865005855253af4f68a497John McCall
2841e23cf437fe76b1ed02d63c3f61b456fd48a915f5John McCall  FunctionProtoType *FTP = (FunctionProtoType*) Allocate(Size, TypeAlignment);
2842cfe9af250f466e7e38becea4428990448ae07737Roman Divacky  FunctionProtoType::ExtProtoInfo newEPI = EPI;
2843cfe9af250f466e7e38becea4428990448ae07737Roman Divacky  newEPI.ExtInfo = EPI.ExtInfo.withCallingConv(CallConv);
2844bea522ff43a3f11c7a2bc7949119dbb9fce19e39Jordan Rose  new (FTP) FunctionProtoType(ResultTy, ArgArray, Canonical, newEPI);
28455f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  Types.push_back(FTP);
284672564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor  FunctionProtoTypes.InsertNode(FTP, InsertPos);
28475f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  return QualType(FTP, 0);
28485f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
28495f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
28503cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall#ifndef NDEBUG
28513cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCallstatic bool NeedsInjectedClassNameType(const RecordDecl *D) {
28523cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall  if (!isa<CXXRecordDecl>(D)) return false;
28533cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall  const CXXRecordDecl *RD = cast<CXXRecordDecl>(D);
28543cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall  if (isa<ClassTemplatePartialSpecializationDecl>(RD))
28553cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall    return true;
28563cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall  if (RD->getDescribedClassTemplate() &&
28573cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall      !isa<ClassTemplateSpecializationDecl>(RD))
28583cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall    return true;
28593cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall  return false;
28603cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall}
28613cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall#endif
28623cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall
28633cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall/// getInjectedClassNameType - Return the unique reference to the
28643cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall/// injected class name type for the specified templated declaration.
28653cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCallQualType ASTContext::getInjectedClassNameType(CXXRecordDecl *Decl,
28664ba2a17694148e16eaa8d3917f657ffcd3667be4Jay Foad                                              QualType TST) const {
28673cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall  assert(NeedsInjectedClassNameType(Decl));
28683cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall  if (Decl->TypeForDecl) {
28693cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall    assert(isa<InjectedClassNameType>(Decl->TypeForDecl));
2870ef96ee0be5f100789f451641542a69cd719144d2Douglas Gregor  } else if (CXXRecordDecl *PrevDecl = Decl->getPreviousDecl()) {
28713cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall    assert(PrevDecl->TypeForDecl && "previous declaration has no type");
28723cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall    Decl->TypeForDecl = PrevDecl->TypeForDecl;
28733cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall    assert(isa<InjectedClassNameType>(Decl->TypeForDecl));
28743cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall  } else {
2875f4c7371fb1d3cebcfb40abad4537bb82515704eaJohn McCall    Type *newType =
287631f17ecbef57b5679c017c375db330546b7b5145John McCall      new (*this, TypeAlignment) InjectedClassNameType(Decl, TST);
2877f4c7371fb1d3cebcfb40abad4537bb82515704eaJohn McCall    Decl->TypeForDecl = newType;
2878f4c7371fb1d3cebcfb40abad4537bb82515704eaJohn McCall    Types.push_back(newType);
28793cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall  }
28803cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall  return QualType(Decl->TypeForDecl, 0);
28813cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall}
28823cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall
28832ce52f3fb95bf544db6bd3d91a72bce7d9cceb6cDouglas Gregor/// getTypeDeclType - Return the unique reference to the type for the
28842ce52f3fb95bf544db6bd3d91a72bce7d9cceb6cDouglas Gregor/// specified type declaration.
28854ba2a17694148e16eaa8d3917f657ffcd3667be4Jay FoadQualType ASTContext::getTypeDeclTypeSlow(const TypeDecl *Decl) const {
28861e6759e9e33dcaa73ce14c8a908ac9f87ac16463Argyrios Kyrtzidis  assert(Decl && "Passed null for Decl param");
2887becb8d5a6ab5103393eac5344ae69bcb860601ddJohn McCall  assert(!Decl->TypeForDecl && "TypeForDecl present in slow case");
28881eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
2889162e1c1b487352434552147967c3dd296ebee2f7Richard Smith  if (const TypedefNameDecl *Typedef = dyn_cast<TypedefNameDecl>(Decl))
28902ce52f3fb95bf544db6bd3d91a72bce7d9cceb6cDouglas Gregor    return getTypedefType(Typedef);
2891becb8d5a6ab5103393eac5344ae69bcb860601ddJohn McCall
2892becb8d5a6ab5103393eac5344ae69bcb860601ddJohn McCall  assert(!isa<TemplateTypeParmDecl>(Decl) &&
2893becb8d5a6ab5103393eac5344ae69bcb860601ddJohn McCall         "Template type parameter types are always available.");
2894becb8d5a6ab5103393eac5344ae69bcb860601ddJohn McCall
289519c8576b7328f4dc2d07682f5da552875c1912efJohn McCall  if (const RecordDecl *Record = dyn_cast<RecordDecl>(Decl)) {
2896ef96ee0be5f100789f451641542a69cd719144d2Douglas Gregor    assert(!Record->getPreviousDecl() &&
2897becb8d5a6ab5103393eac5344ae69bcb860601ddJohn McCall           "struct/union has previous declaration");
2898becb8d5a6ab5103393eac5344ae69bcb860601ddJohn McCall    assert(!NeedsInjectedClassNameType(Record));
2899400f5125e2432d648f2c8a31b36a7f318a880c47Argyrios Kyrtzidis    return getRecordType(Record);
290019c8576b7328f4dc2d07682f5da552875c1912efJohn McCall  } else if (const EnumDecl *Enum = dyn_cast<EnumDecl>(Decl)) {
2901ef96ee0be5f100789f451641542a69cd719144d2Douglas Gregor    assert(!Enum->getPreviousDecl() &&
2902becb8d5a6ab5103393eac5344ae69bcb860601ddJohn McCall           "enum has previous declaration");
2903400f5125e2432d648f2c8a31b36a7f318a880c47Argyrios Kyrtzidis    return getEnumType(Enum);
290419c8576b7328f4dc2d07682f5da552875c1912efJohn McCall  } else if (const UnresolvedUsingTypenameDecl *Using =
2905ed97649e9574b9d854fa4d6109c9333ae0993554John McCall               dyn_cast<UnresolvedUsingTypenameDecl>(Decl)) {
2906f4c7371fb1d3cebcfb40abad4537bb82515704eaJohn McCall    Type *newType = new (*this, TypeAlignment) UnresolvedUsingType(Using);
2907f4c7371fb1d3cebcfb40abad4537bb82515704eaJohn McCall    Decl->TypeForDecl = newType;
2908f4c7371fb1d3cebcfb40abad4537bb82515704eaJohn McCall    Types.push_back(newType);
29099fdbab3cbc2fc04bcaf5768023d83707f3151144Mike Stump  } else
2910becb8d5a6ab5103393eac5344ae69bcb860601ddJohn McCall    llvm_unreachable("TypeDecl without a type?");
291149aa7ff1245abd03e6e998e01302df31e4c6f8f6Argyrios Kyrtzidis
291249aa7ff1245abd03e6e998e01302df31e4c6f8f6Argyrios Kyrtzidis  return QualType(Decl->TypeForDecl, 0);
29132ce52f3fb95bf544db6bd3d91a72bce7d9cceb6cDouglas Gregor}
29142ce52f3fb95bf544db6bd3d91a72bce7d9cceb6cDouglas Gregor
29155f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// getTypedefType - Return the unique reference to the type for the
2916162e1c1b487352434552147967c3dd296ebee2f7Richard Smith/// specified typedef name decl.
29179763e221e16026ddf487d2564ed349d2c874a1a1Argyrios KyrtzidisQualType
2918162e1c1b487352434552147967c3dd296ebee2f7Richard SmithASTContext::getTypedefType(const TypedefNameDecl *Decl,
2919162e1c1b487352434552147967c3dd296ebee2f7Richard Smith                           QualType Canonical) const {
29205f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  if (Decl->TypeForDecl) return QualType(Decl->TypeForDecl, 0);
29211eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
29229763e221e16026ddf487d2564ed349d2c874a1a1Argyrios Kyrtzidis  if (Canonical.isNull())
29239763e221e16026ddf487d2564ed349d2c874a1a1Argyrios Kyrtzidis    Canonical = getCanonicalType(Decl->getUnderlyingType());
2924f4c7371fb1d3cebcfb40abad4537bb82515704eaJohn McCall  TypedefType *newType = new(*this, TypeAlignment)
29256b304a0254a13f42390b865ff5ba668a49cc58aeJohn McCall    TypedefType(Type::Typedef, Decl, Canonical);
2926f4c7371fb1d3cebcfb40abad4537bb82515704eaJohn McCall  Decl->TypeForDecl = newType;
2927f4c7371fb1d3cebcfb40abad4537bb82515704eaJohn McCall  Types.push_back(newType);
2928f4c7371fb1d3cebcfb40abad4537bb82515704eaJohn McCall  return QualType(newType, 0);
29295f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
29305f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
29314ba2a17694148e16eaa8d3917f657ffcd3667be4Jay FoadQualType ASTContext::getRecordType(const RecordDecl *Decl) const {
2932400f5125e2432d648f2c8a31b36a7f318a880c47Argyrios Kyrtzidis  if (Decl->TypeForDecl) return QualType(Decl->TypeForDecl, 0);
2933400f5125e2432d648f2c8a31b36a7f318a880c47Argyrios Kyrtzidis
2934ef96ee0be5f100789f451641542a69cd719144d2Douglas Gregor  if (const RecordDecl *PrevDecl = Decl->getPreviousDecl())
2935400f5125e2432d648f2c8a31b36a7f318a880c47Argyrios Kyrtzidis    if (PrevDecl->TypeForDecl)
2936400f5125e2432d648f2c8a31b36a7f318a880c47Argyrios Kyrtzidis      return QualType(Decl->TypeForDecl = PrevDecl->TypeForDecl, 0);
2937400f5125e2432d648f2c8a31b36a7f318a880c47Argyrios Kyrtzidis
2938f4c7371fb1d3cebcfb40abad4537bb82515704eaJohn McCall  RecordType *newType = new (*this, TypeAlignment) RecordType(Decl);
2939f4c7371fb1d3cebcfb40abad4537bb82515704eaJohn McCall  Decl->TypeForDecl = newType;
2940f4c7371fb1d3cebcfb40abad4537bb82515704eaJohn McCall  Types.push_back(newType);
2941f4c7371fb1d3cebcfb40abad4537bb82515704eaJohn McCall  return QualType(newType, 0);
2942400f5125e2432d648f2c8a31b36a7f318a880c47Argyrios Kyrtzidis}
2943400f5125e2432d648f2c8a31b36a7f318a880c47Argyrios Kyrtzidis
29444ba2a17694148e16eaa8d3917f657ffcd3667be4Jay FoadQualType ASTContext::getEnumType(const EnumDecl *Decl) const {
2945400f5125e2432d648f2c8a31b36a7f318a880c47Argyrios Kyrtzidis  if (Decl->TypeForDecl) return QualType(Decl->TypeForDecl, 0);
2946400f5125e2432d648f2c8a31b36a7f318a880c47Argyrios Kyrtzidis
2947ef96ee0be5f100789f451641542a69cd719144d2Douglas Gregor  if (const EnumDecl *PrevDecl = Decl->getPreviousDecl())
2948400f5125e2432d648f2c8a31b36a7f318a880c47Argyrios Kyrtzidis    if (PrevDecl->TypeForDecl)
2949400f5125e2432d648f2c8a31b36a7f318a880c47Argyrios Kyrtzidis      return QualType(Decl->TypeForDecl = PrevDecl->TypeForDecl, 0);
2950400f5125e2432d648f2c8a31b36a7f318a880c47Argyrios Kyrtzidis
2951f4c7371fb1d3cebcfb40abad4537bb82515704eaJohn McCall  EnumType *newType = new (*this, TypeAlignment) EnumType(Decl);
2952f4c7371fb1d3cebcfb40abad4537bb82515704eaJohn McCall  Decl->TypeForDecl = newType;
2953f4c7371fb1d3cebcfb40abad4537bb82515704eaJohn McCall  Types.push_back(newType);
2954f4c7371fb1d3cebcfb40abad4537bb82515704eaJohn McCall  return QualType(newType, 0);
2955400f5125e2432d648f2c8a31b36a7f318a880c47Argyrios Kyrtzidis}
2956400f5125e2432d648f2c8a31b36a7f318a880c47Argyrios Kyrtzidis
29579d156a7b1b2771e191f2f5a45a7b7a694129463bJohn McCallQualType ASTContext::getAttributedType(AttributedType::Kind attrKind,
29589d156a7b1b2771e191f2f5a45a7b7a694129463bJohn McCall                                       QualType modifiedType,
29599d156a7b1b2771e191f2f5a45a7b7a694129463bJohn McCall                                       QualType equivalentType) {
29609d156a7b1b2771e191f2f5a45a7b7a694129463bJohn McCall  llvm::FoldingSetNodeID id;
29619d156a7b1b2771e191f2f5a45a7b7a694129463bJohn McCall  AttributedType::Profile(id, attrKind, modifiedType, equivalentType);
29629d156a7b1b2771e191f2f5a45a7b7a694129463bJohn McCall
29639d156a7b1b2771e191f2f5a45a7b7a694129463bJohn McCall  void *insertPos = 0;
29649d156a7b1b2771e191f2f5a45a7b7a694129463bJohn McCall  AttributedType *type = AttributedTypes.FindNodeOrInsertPos(id, insertPos);
29659d156a7b1b2771e191f2f5a45a7b7a694129463bJohn McCall  if (type) return QualType(type, 0);
29669d156a7b1b2771e191f2f5a45a7b7a694129463bJohn McCall
29679d156a7b1b2771e191f2f5a45a7b7a694129463bJohn McCall  QualType canon = getCanonicalType(equivalentType);
29689d156a7b1b2771e191f2f5a45a7b7a694129463bJohn McCall  type = new (*this, TypeAlignment)
29699d156a7b1b2771e191f2f5a45a7b7a694129463bJohn McCall           AttributedType(canon, attrKind, modifiedType, equivalentType);
29709d156a7b1b2771e191f2f5a45a7b7a694129463bJohn McCall
29719d156a7b1b2771e191f2f5a45a7b7a694129463bJohn McCall  Types.push_back(type);
29729d156a7b1b2771e191f2f5a45a7b7a694129463bJohn McCall  AttributedTypes.InsertNode(type, insertPos);
29739d156a7b1b2771e191f2f5a45a7b7a694129463bJohn McCall
29749d156a7b1b2771e191f2f5a45a7b7a694129463bJohn McCall  return QualType(type, 0);
29759d156a7b1b2771e191f2f5a45a7b7a694129463bJohn McCall}
29769d156a7b1b2771e191f2f5a45a7b7a694129463bJohn McCall
29779d156a7b1b2771e191f2f5a45a7b7a694129463bJohn McCall
297849a832bd499d6f61c23655f1fac99f0dd229756eJohn McCall/// \brief Retrieve a substitution-result type.
297949a832bd499d6f61c23655f1fac99f0dd229756eJohn McCallQualType
298049a832bd499d6f61c23655f1fac99f0dd229756eJohn McCallASTContext::getSubstTemplateTypeParmType(const TemplateTypeParmType *Parm,
29814ba2a17694148e16eaa8d3917f657ffcd3667be4Jay Foad                                         QualType Replacement) const {
2982467b27b9a24bdc823218ad1ad0e37673b6cc1e83John McCall  assert(Replacement.isCanonical()
298349a832bd499d6f61c23655f1fac99f0dd229756eJohn McCall         && "replacement types must always be canonical");
298449a832bd499d6f61c23655f1fac99f0dd229756eJohn McCall
298549a832bd499d6f61c23655f1fac99f0dd229756eJohn McCall  llvm::FoldingSetNodeID ID;
298649a832bd499d6f61c23655f1fac99f0dd229756eJohn McCall  SubstTemplateTypeParmType::Profile(ID, Parm, Replacement);
298749a832bd499d6f61c23655f1fac99f0dd229756eJohn McCall  void *InsertPos = 0;
298849a832bd499d6f61c23655f1fac99f0dd229756eJohn McCall  SubstTemplateTypeParmType *SubstParm
298949a832bd499d6f61c23655f1fac99f0dd229756eJohn McCall    = SubstTemplateTypeParmTypes.FindNodeOrInsertPos(ID, InsertPos);
299049a832bd499d6f61c23655f1fac99f0dd229756eJohn McCall
299149a832bd499d6f61c23655f1fac99f0dd229756eJohn McCall  if (!SubstParm) {
299249a832bd499d6f61c23655f1fac99f0dd229756eJohn McCall    SubstParm = new (*this, TypeAlignment)
299349a832bd499d6f61c23655f1fac99f0dd229756eJohn McCall      SubstTemplateTypeParmType(Parm, Replacement);
299449a832bd499d6f61c23655f1fac99f0dd229756eJohn McCall    Types.push_back(SubstParm);
299549a832bd499d6f61c23655f1fac99f0dd229756eJohn McCall    SubstTemplateTypeParmTypes.InsertNode(SubstParm, InsertPos);
299649a832bd499d6f61c23655f1fac99f0dd229756eJohn McCall  }
299749a832bd499d6f61c23655f1fac99f0dd229756eJohn McCall
299849a832bd499d6f61c23655f1fac99f0dd229756eJohn McCall  return QualType(SubstParm, 0);
299949a832bd499d6f61c23655f1fac99f0dd229756eJohn McCall}
300049a832bd499d6f61c23655f1fac99f0dd229756eJohn McCall
3001c3069d618f4661d923cb1b5c4525b082fce73b04Douglas Gregor/// \brief Retrieve a
3002c3069d618f4661d923cb1b5c4525b082fce73b04Douglas GregorQualType ASTContext::getSubstTemplateTypeParmPackType(
3003c3069d618f4661d923cb1b5c4525b082fce73b04Douglas Gregor                                          const TemplateTypeParmType *Parm,
3004c3069d618f4661d923cb1b5c4525b082fce73b04Douglas Gregor                                              const TemplateArgument &ArgPack) {
3005c3069d618f4661d923cb1b5c4525b082fce73b04Douglas Gregor#ifndef NDEBUG
3006c3069d618f4661d923cb1b5c4525b082fce73b04Douglas Gregor  for (TemplateArgument::pack_iterator P = ArgPack.pack_begin(),
3007c3069d618f4661d923cb1b5c4525b082fce73b04Douglas Gregor                                    PEnd = ArgPack.pack_end();
3008c3069d618f4661d923cb1b5c4525b082fce73b04Douglas Gregor       P != PEnd; ++P) {
3009c3069d618f4661d923cb1b5c4525b082fce73b04Douglas Gregor    assert(P->getKind() == TemplateArgument::Type &&"Pack contains a non-type");
3010c3069d618f4661d923cb1b5c4525b082fce73b04Douglas Gregor    assert(P->getAsType().isCanonical() && "Pack contains non-canonical type");
3011c3069d618f4661d923cb1b5c4525b082fce73b04Douglas Gregor  }
3012c3069d618f4661d923cb1b5c4525b082fce73b04Douglas Gregor#endif
3013c3069d618f4661d923cb1b5c4525b082fce73b04Douglas Gregor
3014c3069d618f4661d923cb1b5c4525b082fce73b04Douglas Gregor  llvm::FoldingSetNodeID ID;
3015c3069d618f4661d923cb1b5c4525b082fce73b04Douglas Gregor  SubstTemplateTypeParmPackType::Profile(ID, Parm, ArgPack);
3016c3069d618f4661d923cb1b5c4525b082fce73b04Douglas Gregor  void *InsertPos = 0;
3017c3069d618f4661d923cb1b5c4525b082fce73b04Douglas Gregor  if (SubstTemplateTypeParmPackType *SubstParm
3018c3069d618f4661d923cb1b5c4525b082fce73b04Douglas Gregor        = SubstTemplateTypeParmPackTypes.FindNodeOrInsertPos(ID, InsertPos))
3019c3069d618f4661d923cb1b5c4525b082fce73b04Douglas Gregor    return QualType(SubstParm, 0);
3020c3069d618f4661d923cb1b5c4525b082fce73b04Douglas Gregor
3021c3069d618f4661d923cb1b5c4525b082fce73b04Douglas Gregor  QualType Canon;
3022c3069d618f4661d923cb1b5c4525b082fce73b04Douglas Gregor  if (!Parm->isCanonicalUnqualified()) {
3023c3069d618f4661d923cb1b5c4525b082fce73b04Douglas Gregor    Canon = getCanonicalType(QualType(Parm, 0));
3024c3069d618f4661d923cb1b5c4525b082fce73b04Douglas Gregor    Canon = getSubstTemplateTypeParmPackType(cast<TemplateTypeParmType>(Canon),
3025c3069d618f4661d923cb1b5c4525b082fce73b04Douglas Gregor                                             ArgPack);
3026c3069d618f4661d923cb1b5c4525b082fce73b04Douglas Gregor    SubstTemplateTypeParmPackTypes.FindNodeOrInsertPos(ID, InsertPos);
3027c3069d618f4661d923cb1b5c4525b082fce73b04Douglas Gregor  }
3028c3069d618f4661d923cb1b5c4525b082fce73b04Douglas Gregor
3029c3069d618f4661d923cb1b5c4525b082fce73b04Douglas Gregor  SubstTemplateTypeParmPackType *SubstParm
3030c3069d618f4661d923cb1b5c4525b082fce73b04Douglas Gregor    = new (*this, TypeAlignment) SubstTemplateTypeParmPackType(Parm, Canon,
3031c3069d618f4661d923cb1b5c4525b082fce73b04Douglas Gregor                                                               ArgPack);
3032c3069d618f4661d923cb1b5c4525b082fce73b04Douglas Gregor  Types.push_back(SubstParm);
3033c3069d618f4661d923cb1b5c4525b082fce73b04Douglas Gregor  SubstTemplateTypeParmTypes.InsertNode(SubstParm, InsertPos);
3034c3069d618f4661d923cb1b5c4525b082fce73b04Douglas Gregor  return QualType(SubstParm, 0);
3035c3069d618f4661d923cb1b5c4525b082fce73b04Douglas Gregor}
3036c3069d618f4661d923cb1b5c4525b082fce73b04Douglas Gregor
3037fab9d67cebb87be968e7ae31a3b549a5279b5d51Douglas Gregor/// \brief Retrieve the template type parameter type for a template
30381eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// parameter or parameter pack with the given depth, index, and (optionally)
303976e4ce42a30cee4dc40ce7c6014874fbc4f9baa7Anders Carlsson/// name.
30401eb4433ac451dc16f4133a88af2d002ac26c58efMike StumpQualType ASTContext::getTemplateTypeParmType(unsigned Depth, unsigned Index,
304176e4ce42a30cee4dc40ce7c6014874fbc4f9baa7Anders Carlsson                                             bool ParameterPack,
30424fb86f8c4585e53c21c847ad3de9e3b2de123cd9Chandler Carruth                                             TemplateTypeParmDecl *TTPDecl) const {
3043fab9d67cebb87be968e7ae31a3b549a5279b5d51Douglas Gregor  llvm::FoldingSetNodeID ID;
30444fb86f8c4585e53c21c847ad3de9e3b2de123cd9Chandler Carruth  TemplateTypeParmType::Profile(ID, Depth, Index, ParameterPack, TTPDecl);
3045fab9d67cebb87be968e7ae31a3b549a5279b5d51Douglas Gregor  void *InsertPos = 0;
30461eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  TemplateTypeParmType *TypeParm
3047fab9d67cebb87be968e7ae31a3b549a5279b5d51Douglas Gregor    = TemplateTypeParmTypes.FindNodeOrInsertPos(ID, InsertPos);
3048fab9d67cebb87be968e7ae31a3b549a5279b5d51Douglas Gregor
3049fab9d67cebb87be968e7ae31a3b549a5279b5d51Douglas Gregor  if (TypeParm)
3050fab9d67cebb87be968e7ae31a3b549a5279b5d51Douglas Gregor    return QualType(TypeParm, 0);
30511eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
30524fb86f8c4585e53c21c847ad3de9e3b2de123cd9Chandler Carruth  if (TTPDecl) {
305376e4ce42a30cee4dc40ce7c6014874fbc4f9baa7Anders Carlsson    QualType Canon = getTemplateTypeParmType(Depth, Index, ParameterPack);
30544fb86f8c4585e53c21c847ad3de9e3b2de123cd9Chandler Carruth    TypeParm = new (*this, TypeAlignment) TemplateTypeParmType(TTPDecl, Canon);
3055789b1f640205e81b5af250693246120f1ce9d147Douglas Gregor
3056789b1f640205e81b5af250693246120f1ce9d147Douglas Gregor    TemplateTypeParmType *TypeCheck
3057789b1f640205e81b5af250693246120f1ce9d147Douglas Gregor      = TemplateTypeParmTypes.FindNodeOrInsertPos(ID, InsertPos);
3058789b1f640205e81b5af250693246120f1ce9d147Douglas Gregor    assert(!TypeCheck && "Template type parameter canonical type broken");
3059789b1f640205e81b5af250693246120f1ce9d147Douglas Gregor    (void)TypeCheck;
306076e4ce42a30cee4dc40ce7c6014874fbc4f9baa7Anders Carlsson  } else
30616b304a0254a13f42390b865ff5ba668a49cc58aeJohn McCall    TypeParm = new (*this, TypeAlignment)
30626b304a0254a13f42390b865ff5ba668a49cc58aeJohn McCall      TemplateTypeParmType(Depth, Index, ParameterPack);
3063fab9d67cebb87be968e7ae31a3b549a5279b5d51Douglas Gregor
3064fab9d67cebb87be968e7ae31a3b549a5279b5d51Douglas Gregor  Types.push_back(TypeParm);
3065fab9d67cebb87be968e7ae31a3b549a5279b5d51Douglas Gregor  TemplateTypeParmTypes.InsertNode(TypeParm, InsertPos);
3066fab9d67cebb87be968e7ae31a3b549a5279b5d51Douglas Gregor
3067fab9d67cebb87be968e7ae31a3b549a5279b5d51Douglas Gregor  return QualType(TypeParm, 0);
3068fab9d67cebb87be968e7ae31a3b549a5279b5d51Douglas Gregor}
3069fab9d67cebb87be968e7ae31a3b549a5279b5d51Douglas Gregor
30703cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCallTypeSourceInfo *
30713cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCallASTContext::getTemplateSpecializationTypeInfo(TemplateName Name,
30723cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall                                              SourceLocation NameLoc,
30733cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall                                        const TemplateArgumentListInfo &Args,
30743e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith                                              QualType Underlying) const {
30757c3179cf463c3b3b8c21dbb955f933ba50b74f28Douglas Gregor  assert(!Name.getAsDependentTemplateName() &&
30767c3179cf463c3b3b8c21dbb955f933ba50b74f28Douglas Gregor         "No dependent template names here!");
30773e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith  QualType TST = getTemplateSpecializationType(Name, Args, Underlying);
30783cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall
30793cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall  TypeSourceInfo *DI = CreateTypeSourceInfo(TST);
308039e6ab4be93d9c5e729a578ddd9d415cd2d49872David Blaikie  TemplateSpecializationTypeLoc TL =
308139e6ab4be93d9c5e729a578ddd9d415cd2d49872David Blaikie      DI->getTypeLoc().castAs<TemplateSpecializationTypeLoc>();
308255d23c925b058be29b792008ddb7d68f6c4fa9a0Abramo Bagnara  TL.setTemplateKeywordLoc(SourceLocation());
30833cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall  TL.setTemplateNameLoc(NameLoc);
30843cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall  TL.setLAngleLoc(Args.getLAngleLoc());
30853cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall  TL.setRAngleLoc(Args.getRAngleLoc());
30863cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall  for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i)
30873cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall    TL.setArgLocInfo(i, Args[i].getLocInfo());
30883cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall  return DI;
30893cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall}
30903cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall
30911eb4433ac451dc16f4133a88af2d002ac26c58efMike StumpQualType
30927532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas GregorASTContext::getTemplateSpecializationType(TemplateName Template,
3093d5532b6cfff2977e0c59fa6ead7f7973984a620dJohn McCall                                          const TemplateArgumentListInfo &Args,
30943e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith                                          QualType Underlying) const {
30957c3179cf463c3b3b8c21dbb955f933ba50b74f28Douglas Gregor  assert(!Template.getAsDependentTemplateName() &&
30967c3179cf463c3b3b8c21dbb955f933ba50b74f28Douglas Gregor         "No dependent template names here!");
30977c3179cf463c3b3b8c21dbb955f933ba50b74f28Douglas Gregor
3098d5532b6cfff2977e0c59fa6ead7f7973984a620dJohn McCall  unsigned NumArgs = Args.size();
3099d5532b6cfff2977e0c59fa6ead7f7973984a620dJohn McCall
31005f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner  SmallVector<TemplateArgument, 4> ArgVec;
3101833ca991c1bfc967f0995974ca86f66ba1f666b5John McCall  ArgVec.reserve(NumArgs);
3102833ca991c1bfc967f0995974ca86f66ba1f666b5John McCall  for (unsigned i = 0; i != NumArgs; ++i)
3103833ca991c1bfc967f0995974ca86f66ba1f666b5John McCall    ArgVec.push_back(Args[i].getArgument());
3104833ca991c1bfc967f0995974ca86f66ba1f666b5John McCall
310531f17ecbef57b5679c017c375db330546b7b5145John McCall  return getTemplateSpecializationType(Template, ArgVec.data(), NumArgs,
31063e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith                                       Underlying);
3107833ca991c1bfc967f0995974ca86f66ba1f666b5John McCall}
3108833ca991c1bfc967f0995974ca86f66ba1f666b5John McCall
3109b70126a328f89937f46db42f9e3cba1592887c91Douglas Gregor#ifndef NDEBUG
3110b70126a328f89937f46db42f9e3cba1592887c91Douglas Gregorstatic bool hasAnyPackExpansions(const TemplateArgument *Args,
3111b70126a328f89937f46db42f9e3cba1592887c91Douglas Gregor                                 unsigned NumArgs) {
3112b70126a328f89937f46db42f9e3cba1592887c91Douglas Gregor  for (unsigned I = 0; I != NumArgs; ++I)
3113b70126a328f89937f46db42f9e3cba1592887c91Douglas Gregor    if (Args[I].isPackExpansion())
3114b70126a328f89937f46db42f9e3cba1592887c91Douglas Gregor      return true;
3115b70126a328f89937f46db42f9e3cba1592887c91Douglas Gregor
3116b70126a328f89937f46db42f9e3cba1592887c91Douglas Gregor  return true;
3117b70126a328f89937f46db42f9e3cba1592887c91Douglas Gregor}
3118b70126a328f89937f46db42f9e3cba1592887c91Douglas Gregor#endif
3119b70126a328f89937f46db42f9e3cba1592887c91Douglas Gregor
3120833ca991c1bfc967f0995974ca86f66ba1f666b5John McCallQualType
3121833ca991c1bfc967f0995974ca86f66ba1f666b5John McCallASTContext::getTemplateSpecializationType(TemplateName Template,
31227532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor                                          const TemplateArgument *Args,
31237532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor                                          unsigned NumArgs,
31243e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith                                          QualType Underlying) const {
31257c3179cf463c3b3b8c21dbb955f933ba50b74f28Douglas Gregor  assert(!Template.getAsDependentTemplateName() &&
31267c3179cf463c3b3b8c21dbb955f933ba50b74f28Douglas Gregor         "No dependent template names here!");
31270f0ea2a96534c615ff5fdd81363989b23cf2164aDouglas Gregor  // Look through qualified template names.
31280f0ea2a96534c615ff5fdd81363989b23cf2164aDouglas Gregor  if (QualifiedTemplateName *QTN = Template.getAsQualifiedTemplateName())
31290f0ea2a96534c615ff5fdd81363989b23cf2164aDouglas Gregor    Template = TemplateName(QTN->getTemplateDecl());
31307c3179cf463c3b3b8c21dbb955f933ba50b74f28Douglas Gregor
3131b70126a328f89937f46db42f9e3cba1592887c91Douglas Gregor  bool IsTypeAlias =
31323e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith    Template.getAsTemplateDecl() &&
31333e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith    isa<TypeAliasTemplateDecl>(Template.getAsTemplateDecl());
31343e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith  QualType CanonType;
31353e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith  if (!Underlying.isNull())
31363e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith    CanonType = getCanonicalType(Underlying);
31373e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith  else {
3138b70126a328f89937f46db42f9e3cba1592887c91Douglas Gregor    // We can get here with an alias template when the specialization contains
3139b70126a328f89937f46db42f9e3cba1592887c91Douglas Gregor    // a pack expansion that does not match up with a parameter pack.
3140b70126a328f89937f46db42f9e3cba1592887c91Douglas Gregor    assert((!IsTypeAlias || hasAnyPackExpansions(Args, NumArgs)) &&
3141b70126a328f89937f46db42f9e3cba1592887c91Douglas Gregor           "Caller must compute aliased type");
3142b70126a328f89937f46db42f9e3cba1592887c91Douglas Gregor    IsTypeAlias = false;
31433e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith    CanonType = getCanonicalTemplateSpecializationType(Template, Args,
31443e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith                                                       NumArgs);
31453e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith  }
3146fc705b84347e6fb4746a1a7e26949f64c2f2f358Douglas Gregor
31471275ae098acda31fe0e434510c729fcfed0458a1Douglas Gregor  // Allocate the (non-canonical) template specialization type, but don't
31481275ae098acda31fe0e434510c729fcfed0458a1Douglas Gregor  // try to unique it: these types typically have location information that
31491275ae098acda31fe0e434510c729fcfed0458a1Douglas Gregor  // we don't unique and don't want to lose.
31503e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith  void *Mem = Allocate(sizeof(TemplateSpecializationType) +
31513e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith                       sizeof(TemplateArgument) * NumArgs +
3152b70126a328f89937f46db42f9e3cba1592887c91Douglas Gregor                       (IsTypeAlias? sizeof(QualType) : 0),
31536b304a0254a13f42390b865ff5ba668a49cc58aeJohn McCall                       TypeAlignment);
31541eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  TemplateSpecializationType *Spec
3155b70126a328f89937f46db42f9e3cba1592887c91Douglas Gregor    = new (Mem) TemplateSpecializationType(Template, Args, NumArgs, CanonType,
3156b70126a328f89937f46db42f9e3cba1592887c91Douglas Gregor                                         IsTypeAlias ? Underlying : QualType());
31571eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
315855f6b14230c94272efbbcdd89a92224c8db9f225Douglas Gregor  Types.push_back(Spec);
31591eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  return QualType(Spec, 0);
316055f6b14230c94272efbbcdd89a92224c8db9f225Douglas Gregor}
316155f6b14230c94272efbbcdd89a92224c8db9f225Douglas Gregor
31621eb4433ac451dc16f4133a88af2d002ac26c58efMike StumpQualType
31639763e221e16026ddf487d2564ed349d2c874a1a1Argyrios KyrtzidisASTContext::getCanonicalTemplateSpecializationType(TemplateName Template,
31649763e221e16026ddf487d2564ed349d2c874a1a1Argyrios Kyrtzidis                                                   const TemplateArgument *Args,
31654ba2a17694148e16eaa8d3917f657ffcd3667be4Jay Foad                                                   unsigned NumArgs) const {
31667c3179cf463c3b3b8c21dbb955f933ba50b74f28Douglas Gregor  assert(!Template.getAsDependentTemplateName() &&
31677c3179cf463c3b3b8c21dbb955f933ba50b74f28Douglas Gregor         "No dependent template names here!");
31683e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith
31690f0ea2a96534c615ff5fdd81363989b23cf2164aDouglas Gregor  // Look through qualified template names.
31700f0ea2a96534c615ff5fdd81363989b23cf2164aDouglas Gregor  if (QualifiedTemplateName *QTN = Template.getAsQualifiedTemplateName())
31710f0ea2a96534c615ff5fdd81363989b23cf2164aDouglas Gregor    Template = TemplateName(QTN->getTemplateDecl());
31727c3179cf463c3b3b8c21dbb955f933ba50b74f28Douglas Gregor
31739763e221e16026ddf487d2564ed349d2c874a1a1Argyrios Kyrtzidis  // Build the canonical template specialization type.
31749763e221e16026ddf487d2564ed349d2c874a1a1Argyrios Kyrtzidis  TemplateName CanonTemplate = getCanonicalTemplateName(Template);
31755f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner  SmallVector<TemplateArgument, 4> CanonArgs;
31769763e221e16026ddf487d2564ed349d2c874a1a1Argyrios Kyrtzidis  CanonArgs.reserve(NumArgs);
31779763e221e16026ddf487d2564ed349d2c874a1a1Argyrios Kyrtzidis  for (unsigned I = 0; I != NumArgs; ++I)
31789763e221e16026ddf487d2564ed349d2c874a1a1Argyrios Kyrtzidis    CanonArgs.push_back(getCanonicalTemplateArgument(Args[I]));
31799763e221e16026ddf487d2564ed349d2c874a1a1Argyrios Kyrtzidis
31809763e221e16026ddf487d2564ed349d2c874a1a1Argyrios Kyrtzidis  // Determine whether this canonical template specialization type already
31819763e221e16026ddf487d2564ed349d2c874a1a1Argyrios Kyrtzidis  // exists.
31829763e221e16026ddf487d2564ed349d2c874a1a1Argyrios Kyrtzidis  llvm::FoldingSetNodeID ID;
31839763e221e16026ddf487d2564ed349d2c874a1a1Argyrios Kyrtzidis  TemplateSpecializationType::Profile(ID, CanonTemplate,
31849763e221e16026ddf487d2564ed349d2c874a1a1Argyrios Kyrtzidis                                      CanonArgs.data(), NumArgs, *this);
31859763e221e16026ddf487d2564ed349d2c874a1a1Argyrios Kyrtzidis
31869763e221e16026ddf487d2564ed349d2c874a1a1Argyrios Kyrtzidis  void *InsertPos = 0;
31879763e221e16026ddf487d2564ed349d2c874a1a1Argyrios Kyrtzidis  TemplateSpecializationType *Spec
31889763e221e16026ddf487d2564ed349d2c874a1a1Argyrios Kyrtzidis    = TemplateSpecializationTypes.FindNodeOrInsertPos(ID, InsertPos);
31899763e221e16026ddf487d2564ed349d2c874a1a1Argyrios Kyrtzidis
31909763e221e16026ddf487d2564ed349d2c874a1a1Argyrios Kyrtzidis  if (!Spec) {
31919763e221e16026ddf487d2564ed349d2c874a1a1Argyrios Kyrtzidis    // Allocate a new canonical template specialization type.
31929763e221e16026ddf487d2564ed349d2c874a1a1Argyrios Kyrtzidis    void *Mem = Allocate((sizeof(TemplateSpecializationType) +
31939763e221e16026ddf487d2564ed349d2c874a1a1Argyrios Kyrtzidis                          sizeof(TemplateArgument) * NumArgs),
31949763e221e16026ddf487d2564ed349d2c874a1a1Argyrios Kyrtzidis                         TypeAlignment);
31959763e221e16026ddf487d2564ed349d2c874a1a1Argyrios Kyrtzidis    Spec = new (Mem) TemplateSpecializationType(CanonTemplate,
31969763e221e16026ddf487d2564ed349d2c874a1a1Argyrios Kyrtzidis                                                CanonArgs.data(), NumArgs,
31973e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith                                                QualType(), QualType());
31989763e221e16026ddf487d2564ed349d2c874a1a1Argyrios Kyrtzidis    Types.push_back(Spec);
31999763e221e16026ddf487d2564ed349d2c874a1a1Argyrios Kyrtzidis    TemplateSpecializationTypes.InsertNode(Spec, InsertPos);
32009763e221e16026ddf487d2564ed349d2c874a1a1Argyrios Kyrtzidis  }
32019763e221e16026ddf487d2564ed349d2c874a1a1Argyrios Kyrtzidis
32029763e221e16026ddf487d2564ed349d2c874a1a1Argyrios Kyrtzidis  assert(Spec->isDependentType() &&
32039763e221e16026ddf487d2564ed349d2c874a1a1Argyrios Kyrtzidis         "Non-dependent template-id type must have a canonical type");
32049763e221e16026ddf487d2564ed349d2c874a1a1Argyrios Kyrtzidis  return QualType(Spec, 0);
32059763e221e16026ddf487d2564ed349d2c874a1a1Argyrios Kyrtzidis}
32069763e221e16026ddf487d2564ed349d2c874a1a1Argyrios Kyrtzidis
32079763e221e16026ddf487d2564ed349d2c874a1a1Argyrios KyrtzidisQualType
3208465d41b92b2c862f3062c412a0538db65c6a2661Abramo BagnaraASTContext::getElaboratedType(ElaboratedTypeKeyword Keyword,
3209465d41b92b2c862f3062c412a0538db65c6a2661Abramo Bagnara                              NestedNameSpecifier *NNS,
32104ba2a17694148e16eaa8d3917f657ffcd3667be4Jay Foad                              QualType NamedType) const {
3211e4e5b054b4917f0ee493bb2fda5b1ec749bfb9a1Douglas Gregor  llvm::FoldingSetNodeID ID;
3212465d41b92b2c862f3062c412a0538db65c6a2661Abramo Bagnara  ElaboratedType::Profile(ID, Keyword, NNS, NamedType);
3213e4e5b054b4917f0ee493bb2fda5b1ec749bfb9a1Douglas Gregor
3214e4e5b054b4917f0ee493bb2fda5b1ec749bfb9a1Douglas Gregor  void *InsertPos = 0;
3215465d41b92b2c862f3062c412a0538db65c6a2661Abramo Bagnara  ElaboratedType *T = ElaboratedTypes.FindNodeOrInsertPos(ID, InsertPos);
3216e4e5b054b4917f0ee493bb2fda5b1ec749bfb9a1Douglas Gregor  if (T)
3217e4e5b054b4917f0ee493bb2fda5b1ec749bfb9a1Douglas Gregor    return QualType(T, 0);
3218e4e5b054b4917f0ee493bb2fda5b1ec749bfb9a1Douglas Gregor
3219789b1f640205e81b5af250693246120f1ce9d147Douglas Gregor  QualType Canon = NamedType;
3220789b1f640205e81b5af250693246120f1ce9d147Douglas Gregor  if (!Canon.isCanonical()) {
3221789b1f640205e81b5af250693246120f1ce9d147Douglas Gregor    Canon = getCanonicalType(NamedType);
3222465d41b92b2c862f3062c412a0538db65c6a2661Abramo Bagnara    ElaboratedType *CheckT = ElaboratedTypes.FindNodeOrInsertPos(ID, InsertPos);
3223465d41b92b2c862f3062c412a0538db65c6a2661Abramo Bagnara    assert(!CheckT && "Elaborated canonical type broken");
3224789b1f640205e81b5af250693246120f1ce9d147Douglas Gregor    (void)CheckT;
3225789b1f640205e81b5af250693246120f1ce9d147Douglas Gregor  }
3226789b1f640205e81b5af250693246120f1ce9d147Douglas Gregor
3227465d41b92b2c862f3062c412a0538db65c6a2661Abramo Bagnara  T = new (*this) ElaboratedType(Keyword, NNS, NamedType, Canon);
3228e4e5b054b4917f0ee493bb2fda5b1ec749bfb9a1Douglas Gregor  Types.push_back(T);
3229465d41b92b2c862f3062c412a0538db65c6a2661Abramo Bagnara  ElaboratedTypes.InsertNode(T, InsertPos);
3230e4e5b054b4917f0ee493bb2fda5b1ec749bfb9a1Douglas Gregor  return QualType(T, 0);
3231e4e5b054b4917f0ee493bb2fda5b1ec749bfb9a1Douglas Gregor}
3232e4e5b054b4917f0ee493bb2fda5b1ec749bfb9a1Douglas Gregor
3233075f8f1b6bed4d1b224c74f87508534cc6392ce6Abramo BagnaraQualType
32344ba2a17694148e16eaa8d3917f657ffcd3667be4Jay FoadASTContext::getParenType(QualType InnerType) const {
3235075f8f1b6bed4d1b224c74f87508534cc6392ce6Abramo Bagnara  llvm::FoldingSetNodeID ID;
3236075f8f1b6bed4d1b224c74f87508534cc6392ce6Abramo Bagnara  ParenType::Profile(ID, InnerType);
3237075f8f1b6bed4d1b224c74f87508534cc6392ce6Abramo Bagnara
3238075f8f1b6bed4d1b224c74f87508534cc6392ce6Abramo Bagnara  void *InsertPos = 0;
3239075f8f1b6bed4d1b224c74f87508534cc6392ce6Abramo Bagnara  ParenType *T = ParenTypes.FindNodeOrInsertPos(ID, InsertPos);
3240075f8f1b6bed4d1b224c74f87508534cc6392ce6Abramo Bagnara  if (T)
3241075f8f1b6bed4d1b224c74f87508534cc6392ce6Abramo Bagnara    return QualType(T, 0);
3242075f8f1b6bed4d1b224c74f87508534cc6392ce6Abramo Bagnara
3243075f8f1b6bed4d1b224c74f87508534cc6392ce6Abramo Bagnara  QualType Canon = InnerType;
3244075f8f1b6bed4d1b224c74f87508534cc6392ce6Abramo Bagnara  if (!Canon.isCanonical()) {
3245075f8f1b6bed4d1b224c74f87508534cc6392ce6Abramo Bagnara    Canon = getCanonicalType(InnerType);
3246075f8f1b6bed4d1b224c74f87508534cc6392ce6Abramo Bagnara    ParenType *CheckT = ParenTypes.FindNodeOrInsertPos(ID, InsertPos);
3247075f8f1b6bed4d1b224c74f87508534cc6392ce6Abramo Bagnara    assert(!CheckT && "Paren canonical type broken");
3248075f8f1b6bed4d1b224c74f87508534cc6392ce6Abramo Bagnara    (void)CheckT;
3249075f8f1b6bed4d1b224c74f87508534cc6392ce6Abramo Bagnara  }
3250075f8f1b6bed4d1b224c74f87508534cc6392ce6Abramo Bagnara
3251075f8f1b6bed4d1b224c74f87508534cc6392ce6Abramo Bagnara  T = new (*this) ParenType(InnerType, Canon);
3252075f8f1b6bed4d1b224c74f87508534cc6392ce6Abramo Bagnara  Types.push_back(T);
3253075f8f1b6bed4d1b224c74f87508534cc6392ce6Abramo Bagnara  ParenTypes.InsertNode(T, InsertPos);
3254075f8f1b6bed4d1b224c74f87508534cc6392ce6Abramo Bagnara  return QualType(T, 0);
3255075f8f1b6bed4d1b224c74f87508534cc6392ce6Abramo Bagnara}
3256075f8f1b6bed4d1b224c74f87508534cc6392ce6Abramo Bagnara
32574a2023f5014e82389d5980d307b89c545dbbac81Douglas GregorQualType ASTContext::getDependentNameType(ElaboratedTypeKeyword Keyword,
32584a2023f5014e82389d5980d307b89c545dbbac81Douglas Gregor                                          NestedNameSpecifier *NNS,
32594a2023f5014e82389d5980d307b89c545dbbac81Douglas Gregor                                          const IdentifierInfo *Name,
32604ba2a17694148e16eaa8d3917f657ffcd3667be4Jay Foad                                          QualType Canon) const {
3261d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor  assert(NNS->isDependent() && "nested-name-specifier must be dependent");
3262d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor
3263d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor  if (Canon.isNull()) {
3264d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor    NestedNameSpecifier *CanonNNS = getCanonicalNestedNameSpecifier(NNS);
32654a2023f5014e82389d5980d307b89c545dbbac81Douglas Gregor    ElaboratedTypeKeyword CanonKeyword = Keyword;
32664a2023f5014e82389d5980d307b89c545dbbac81Douglas Gregor    if (Keyword == ETK_None)
32674a2023f5014e82389d5980d307b89c545dbbac81Douglas Gregor      CanonKeyword = ETK_Typename;
32684a2023f5014e82389d5980d307b89c545dbbac81Douglas Gregor
32694a2023f5014e82389d5980d307b89c545dbbac81Douglas Gregor    if (CanonNNS != NNS || CanonKeyword != Keyword)
32704a2023f5014e82389d5980d307b89c545dbbac81Douglas Gregor      Canon = getDependentNameType(CanonKeyword, CanonNNS, Name);
3271d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor  }
3272d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor
3273d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor  llvm::FoldingSetNodeID ID;
32744a2023f5014e82389d5980d307b89c545dbbac81Douglas Gregor  DependentNameType::Profile(ID, Keyword, NNS, Name);
3275d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor
3276d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor  void *InsertPos = 0;
32774714c12a1ab759156b78be8f109ea4c12213af57Douglas Gregor  DependentNameType *T
32784714c12a1ab759156b78be8f109ea4c12213af57Douglas Gregor    = DependentNameTypes.FindNodeOrInsertPos(ID, InsertPos);
3279d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor  if (T)
3280d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor    return QualType(T, 0);
3281d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor
32824a2023f5014e82389d5980d307b89c545dbbac81Douglas Gregor  T = new (*this) DependentNameType(Keyword, NNS, Name, Canon);
3283d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor  Types.push_back(T);
32844714c12a1ab759156b78be8f109ea4c12213af57Douglas Gregor  DependentNameTypes.InsertNode(T, InsertPos);
32851eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  return QualType(T, 0);
3286d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor}
3287d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor
32881eb4433ac451dc16f4133a88af2d002ac26c58efMike StumpQualType
328933500955d731c73717af52088b7fc0e7a85681e7John McCallASTContext::getDependentTemplateSpecializationType(
329033500955d731c73717af52088b7fc0e7a85681e7John McCall                                 ElaboratedTypeKeyword Keyword,
32914a2023f5014e82389d5980d307b89c545dbbac81Douglas Gregor                                 NestedNameSpecifier *NNS,
329233500955d731c73717af52088b7fc0e7a85681e7John McCall                                 const IdentifierInfo *Name,
32934ba2a17694148e16eaa8d3917f657ffcd3667be4Jay Foad                                 const TemplateArgumentListInfo &Args) const {
329433500955d731c73717af52088b7fc0e7a85681e7John McCall  // TODO: avoid this copy
32955f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner  SmallVector<TemplateArgument, 16> ArgCopy;
329633500955d731c73717af52088b7fc0e7a85681e7John McCall  for (unsigned I = 0, E = Args.size(); I != E; ++I)
329733500955d731c73717af52088b7fc0e7a85681e7John McCall    ArgCopy.push_back(Args[I].getArgument());
329833500955d731c73717af52088b7fc0e7a85681e7John McCall  return getDependentTemplateSpecializationType(Keyword, NNS, Name,
329933500955d731c73717af52088b7fc0e7a85681e7John McCall                                                ArgCopy.size(),
330033500955d731c73717af52088b7fc0e7a85681e7John McCall                                                ArgCopy.data());
330133500955d731c73717af52088b7fc0e7a85681e7John McCall}
330233500955d731c73717af52088b7fc0e7a85681e7John McCall
330333500955d731c73717af52088b7fc0e7a85681e7John McCallQualType
330433500955d731c73717af52088b7fc0e7a85681e7John McCallASTContext::getDependentTemplateSpecializationType(
330533500955d731c73717af52088b7fc0e7a85681e7John McCall                                 ElaboratedTypeKeyword Keyword,
330633500955d731c73717af52088b7fc0e7a85681e7John McCall                                 NestedNameSpecifier *NNS,
330733500955d731c73717af52088b7fc0e7a85681e7John McCall                                 const IdentifierInfo *Name,
330833500955d731c73717af52088b7fc0e7a85681e7John McCall                                 unsigned NumArgs,
33094ba2a17694148e16eaa8d3917f657ffcd3667be4Jay Foad                                 const TemplateArgument *Args) const {
3310aa2187de137e5b809dcbbe14f3b61ae907a3d8aaDouglas Gregor  assert((!NNS || NNS->isDependent()) &&
3311aa2187de137e5b809dcbbe14f3b61ae907a3d8aaDouglas Gregor         "nested-name-specifier must be dependent");
33121734317845d60307d474b5da8a8d33adbaf5e723Douglas Gregor
3313789b1f640205e81b5af250693246120f1ce9d147Douglas Gregor  llvm::FoldingSetNodeID ID;
331433500955d731c73717af52088b7fc0e7a85681e7John McCall  DependentTemplateSpecializationType::Profile(ID, *this, Keyword, NNS,
331533500955d731c73717af52088b7fc0e7a85681e7John McCall                                               Name, NumArgs, Args);
3316789b1f640205e81b5af250693246120f1ce9d147Douglas Gregor
3317789b1f640205e81b5af250693246120f1ce9d147Douglas Gregor  void *InsertPos = 0;
331833500955d731c73717af52088b7fc0e7a85681e7John McCall  DependentTemplateSpecializationType *T
331933500955d731c73717af52088b7fc0e7a85681e7John McCall    = DependentTemplateSpecializationTypes.FindNodeOrInsertPos(ID, InsertPos);
3320789b1f640205e81b5af250693246120f1ce9d147Douglas Gregor  if (T)
3321789b1f640205e81b5af250693246120f1ce9d147Douglas Gregor    return QualType(T, 0);
3322789b1f640205e81b5af250693246120f1ce9d147Douglas Gregor
332333500955d731c73717af52088b7fc0e7a85681e7John McCall  NestedNameSpecifier *CanonNNS = getCanonicalNestedNameSpecifier(NNS);
332433500955d731c73717af52088b7fc0e7a85681e7John McCall
332533500955d731c73717af52088b7fc0e7a85681e7John McCall  ElaboratedTypeKeyword CanonKeyword = Keyword;
332633500955d731c73717af52088b7fc0e7a85681e7John McCall  if (Keyword == ETK_None) CanonKeyword = ETK_Typename;
33271734317845d60307d474b5da8a8d33adbaf5e723Douglas Gregor
332833500955d731c73717af52088b7fc0e7a85681e7John McCall  bool AnyNonCanonArgs = false;
33295f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner  SmallVector<TemplateArgument, 16> CanonArgs(NumArgs);
333033500955d731c73717af52088b7fc0e7a85681e7John McCall  for (unsigned I = 0; I != NumArgs; ++I) {
333133500955d731c73717af52088b7fc0e7a85681e7John McCall    CanonArgs[I] = getCanonicalTemplateArgument(Args[I]);
333233500955d731c73717af52088b7fc0e7a85681e7John McCall    if (!CanonArgs[I].structurallyEquals(Args[I]))
333333500955d731c73717af52088b7fc0e7a85681e7John McCall      AnyNonCanonArgs = true;
3334789b1f640205e81b5af250693246120f1ce9d147Douglas Gregor  }
33351734317845d60307d474b5da8a8d33adbaf5e723Douglas Gregor
333633500955d731c73717af52088b7fc0e7a85681e7John McCall  QualType Canon;
333733500955d731c73717af52088b7fc0e7a85681e7John McCall  if (AnyNonCanonArgs || CanonNNS != NNS || CanonKeyword != Keyword) {
333833500955d731c73717af52088b7fc0e7a85681e7John McCall    Canon = getDependentTemplateSpecializationType(CanonKeyword, CanonNNS,
333933500955d731c73717af52088b7fc0e7a85681e7John McCall                                                   Name, NumArgs,
334033500955d731c73717af52088b7fc0e7a85681e7John McCall                                                   CanonArgs.data());
334133500955d731c73717af52088b7fc0e7a85681e7John McCall
334233500955d731c73717af52088b7fc0e7a85681e7John McCall    // Find the insert position again.
334333500955d731c73717af52088b7fc0e7a85681e7John McCall    DependentTemplateSpecializationTypes.FindNodeOrInsertPos(ID, InsertPos);
334433500955d731c73717af52088b7fc0e7a85681e7John McCall  }
334533500955d731c73717af52088b7fc0e7a85681e7John McCall
334633500955d731c73717af52088b7fc0e7a85681e7John McCall  void *Mem = Allocate((sizeof(DependentTemplateSpecializationType) +
334733500955d731c73717af52088b7fc0e7a85681e7John McCall                        sizeof(TemplateArgument) * NumArgs),
334833500955d731c73717af52088b7fc0e7a85681e7John McCall                       TypeAlignment);
3349ef99001908e799c388f1363b1e607dad5f5b57d3John McCall  T = new (Mem) DependentTemplateSpecializationType(Keyword, NNS,
335033500955d731c73717af52088b7fc0e7a85681e7John McCall                                                    Name, NumArgs, Args, Canon);
33511734317845d60307d474b5da8a8d33adbaf5e723Douglas Gregor  Types.push_back(T);
335233500955d731c73717af52088b7fc0e7a85681e7John McCall  DependentTemplateSpecializationTypes.InsertNode(T, InsertPos);
33531eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  return QualType(T, 0);
33541734317845d60307d474b5da8a8d33adbaf5e723Douglas Gregor}
33551734317845d60307d474b5da8a8d33adbaf5e723Douglas Gregor
3356cded4f649cd4b7ba7d461c25c6482ef52b8d3a2aDouglas GregorQualType ASTContext::getPackExpansionType(QualType Pattern,
3357dc84cd5efdd3430efb22546b4ac656aa0540b210David Blaikie                                          Optional<unsigned> NumExpansions) {
33587536dd5e6c99584481b7dab68b7e7d8df9c54054Douglas Gregor  llvm::FoldingSetNodeID ID;
3359cded4f649cd4b7ba7d461c25c6482ef52b8d3a2aDouglas Gregor  PackExpansionType::Profile(ID, Pattern, NumExpansions);
33607536dd5e6c99584481b7dab68b7e7d8df9c54054Douglas Gregor
33617536dd5e6c99584481b7dab68b7e7d8df9c54054Douglas Gregor  assert(Pattern->containsUnexpandedParameterPack() &&
33627536dd5e6c99584481b7dab68b7e7d8df9c54054Douglas Gregor         "Pack expansions must expand one or more parameter packs");
33637536dd5e6c99584481b7dab68b7e7d8df9c54054Douglas Gregor  void *InsertPos = 0;
33647536dd5e6c99584481b7dab68b7e7d8df9c54054Douglas Gregor  PackExpansionType *T
33657536dd5e6c99584481b7dab68b7e7d8df9c54054Douglas Gregor    = PackExpansionTypes.FindNodeOrInsertPos(ID, InsertPos);
33667536dd5e6c99584481b7dab68b7e7d8df9c54054Douglas Gregor  if (T)
33677536dd5e6c99584481b7dab68b7e7d8df9c54054Douglas Gregor    return QualType(T, 0);
33687536dd5e6c99584481b7dab68b7e7d8df9c54054Douglas Gregor
33697536dd5e6c99584481b7dab68b7e7d8df9c54054Douglas Gregor  QualType Canon;
33707536dd5e6c99584481b7dab68b7e7d8df9c54054Douglas Gregor  if (!Pattern.isCanonical()) {
3371d8672ef2d343a0dbfe838724fb2d9fb4efea6041Richard Smith    Canon = getCanonicalType(Pattern);
3372d8672ef2d343a0dbfe838724fb2d9fb4efea6041Richard Smith    // The canonical type might not contain an unexpanded parameter pack, if it
3373d8672ef2d343a0dbfe838724fb2d9fb4efea6041Richard Smith    // contains an alias template specialization which ignores one of its
3374d8672ef2d343a0dbfe838724fb2d9fb4efea6041Richard Smith    // parameters.
3375d8672ef2d343a0dbfe838724fb2d9fb4efea6041Richard Smith    if (Canon->containsUnexpandedParameterPack()) {
3376d8672ef2d343a0dbfe838724fb2d9fb4efea6041Richard Smith      Canon = getPackExpansionType(getCanonicalType(Pattern), NumExpansions);
3377d8672ef2d343a0dbfe838724fb2d9fb4efea6041Richard Smith
3378d8672ef2d343a0dbfe838724fb2d9fb4efea6041Richard Smith      // Find the insert position again, in case we inserted an element into
3379d8672ef2d343a0dbfe838724fb2d9fb4efea6041Richard Smith      // PackExpansionTypes and invalidated our insert position.
3380d8672ef2d343a0dbfe838724fb2d9fb4efea6041Richard Smith      PackExpansionTypes.FindNodeOrInsertPos(ID, InsertPos);
3381d8672ef2d343a0dbfe838724fb2d9fb4efea6041Richard Smith    }
33827536dd5e6c99584481b7dab68b7e7d8df9c54054Douglas Gregor  }
33837536dd5e6c99584481b7dab68b7e7d8df9c54054Douglas Gregor
3384cded4f649cd4b7ba7d461c25c6482ef52b8d3a2aDouglas Gregor  T = new (*this) PackExpansionType(Pattern, Canon, NumExpansions);
33857536dd5e6c99584481b7dab68b7e7d8df9c54054Douglas Gregor  Types.push_back(T);
33867536dd5e6c99584481b7dab68b7e7d8df9c54054Douglas Gregor  PackExpansionTypes.InsertNode(T, InsertPos);
33877536dd5e6c99584481b7dab68b7e7d8df9c54054Douglas Gregor  return QualType(T, 0);
33887536dd5e6c99584481b7dab68b7e7d8df9c54054Douglas Gregor}
33897536dd5e6c99584481b7dab68b7e7d8df9c54054Douglas Gregor
339088cb27a160adc305783a44f922ee4b216006ebf9Chris Lattner/// CmpProtocolNames - Comparison predicate for sorting protocols
339188cb27a160adc305783a44f922ee4b216006ebf9Chris Lattner/// alphabetically.
339288cb27a160adc305783a44f922ee4b216006ebf9Chris Lattnerstatic bool CmpProtocolNames(const ObjCProtocolDecl *LHS,
339388cb27a160adc305783a44f922ee4b216006ebf9Chris Lattner                            const ObjCProtocolDecl *RHS) {
33942e1cd4264d363ca869bf37ef160902f211d21b8cDouglas Gregor  return LHS->getDeclName() < RHS->getDeclName();
339588cb27a160adc305783a44f922ee4b216006ebf9Chris Lattner}
339688cb27a160adc305783a44f922ee4b216006ebf9Chris Lattner
3397c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCallstatic bool areSortedAndUniqued(ObjCProtocolDecl * const *Protocols,
339854e14c4db764c0636160d26c5bbf491637c83a76John McCall                                unsigned NumProtocols) {
339954e14c4db764c0636160d26c5bbf491637c83a76John McCall  if (NumProtocols == 0) return true;
340054e14c4db764c0636160d26c5bbf491637c83a76John McCall
340161cc296de6c1f82fa84c0abb3ecd142a584838efDouglas Gregor  if (Protocols[0]->getCanonicalDecl() != Protocols[0])
340261cc296de6c1f82fa84c0abb3ecd142a584838efDouglas Gregor    return false;
340361cc296de6c1f82fa84c0abb3ecd142a584838efDouglas Gregor
340454e14c4db764c0636160d26c5bbf491637c83a76John McCall  for (unsigned i = 1; i != NumProtocols; ++i)
340561cc296de6c1f82fa84c0abb3ecd142a584838efDouglas Gregor    if (!CmpProtocolNames(Protocols[i-1], Protocols[i]) ||
340661cc296de6c1f82fa84c0abb3ecd142a584838efDouglas Gregor        Protocols[i]->getCanonicalDecl() != Protocols[i])
340754e14c4db764c0636160d26c5bbf491637c83a76John McCall      return false;
340854e14c4db764c0636160d26c5bbf491637c83a76John McCall  return true;
340954e14c4db764c0636160d26c5bbf491637c83a76John McCall}
341054e14c4db764c0636160d26c5bbf491637c83a76John McCall
341154e14c4db764c0636160d26c5bbf491637c83a76John McCallstatic void SortAndUniqueProtocols(ObjCProtocolDecl **Protocols,
341288cb27a160adc305783a44f922ee4b216006ebf9Chris Lattner                                   unsigned &NumProtocols) {
341388cb27a160adc305783a44f922ee4b216006ebf9Chris Lattner  ObjCProtocolDecl **ProtocolsEnd = Protocols+NumProtocols;
34141eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
341588cb27a160adc305783a44f922ee4b216006ebf9Chris Lattner  // Sort protocols, keyed by name.
341688cb27a160adc305783a44f922ee4b216006ebf9Chris Lattner  std::sort(Protocols, Protocols+NumProtocols, CmpProtocolNames);
341788cb27a160adc305783a44f922ee4b216006ebf9Chris Lattner
341861cc296de6c1f82fa84c0abb3ecd142a584838efDouglas Gregor  // Canonicalize.
341961cc296de6c1f82fa84c0abb3ecd142a584838efDouglas Gregor  for (unsigned I = 0, N = NumProtocols; I != N; ++I)
342061cc296de6c1f82fa84c0abb3ecd142a584838efDouglas Gregor    Protocols[I] = Protocols[I]->getCanonicalDecl();
342161cc296de6c1f82fa84c0abb3ecd142a584838efDouglas Gregor
342288cb27a160adc305783a44f922ee4b216006ebf9Chris Lattner  // Remove duplicates.
342388cb27a160adc305783a44f922ee4b216006ebf9Chris Lattner  ProtocolsEnd = std::unique(Protocols, ProtocolsEnd);
342488cb27a160adc305783a44f922ee4b216006ebf9Chris Lattner  NumProtocols = ProtocolsEnd-Protocols;
342588cb27a160adc305783a44f922ee4b216006ebf9Chris Lattner}
342688cb27a160adc305783a44f922ee4b216006ebf9Chris Lattner
3427c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCallQualType ASTContext::getObjCObjectType(QualType BaseType,
3428c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall                                       ObjCProtocolDecl * const *Protocols,
34294ba2a17694148e16eaa8d3917f657ffcd3667be4Jay Foad                                       unsigned NumProtocols) const {
3430c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall  // If the base type is an interface and there aren't any protocols
3431c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall  // to add, then the interface type will do just fine.
3432c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall  if (!NumProtocols && isa<ObjCInterfaceType>(BaseType))
3433c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall    return BaseType;
3434d1b3c2dd5bc1f3103bee6137957aa7c5f8f2f0bcSteve Naroff
3435c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall  // Look in the folding set for an existing type.
3436c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall  llvm::FoldingSetNodeID ID;
3437c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall  ObjCObjectTypeImpl::Profile(ID, BaseType, Protocols, NumProtocols);
3438d1b3c2dd5bc1f3103bee6137957aa7c5f8f2f0bcSteve Naroff  void *InsertPos = 0;
3439c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall  if (ObjCObjectType *QT = ObjCObjectTypes.FindNodeOrInsertPos(ID, InsertPos))
3440c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall    return QualType(QT, 0);
3441d1b3c2dd5bc1f3103bee6137957aa7c5f8f2f0bcSteve Naroff
3442c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall  // Build the canonical type, which has the canonical base type and
3443c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall  // a sorted-and-uniqued list of protocols.
344454e14c4db764c0636160d26c5bbf491637c83a76John McCall  QualType Canonical;
3445c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall  bool ProtocolsSorted = areSortedAndUniqued(Protocols, NumProtocols);
3446c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall  if (!ProtocolsSorted || !BaseType.isCanonical()) {
3447c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall    if (!ProtocolsSorted) {
34485f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner      SmallVector<ObjCProtocolDecl*, 8> Sorted(Protocols,
34490237941e0beb0c929934b66ad29443b484d987feBenjamin Kramer                                                     Protocols + NumProtocols);
345054e14c4db764c0636160d26c5bbf491637c83a76John McCall      unsigned UniqueCount = NumProtocols;
345154e14c4db764c0636160d26c5bbf491637c83a76John McCall
345254e14c4db764c0636160d26c5bbf491637c83a76John McCall      SortAndUniqueProtocols(&Sorted[0], UniqueCount);
3453c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall      Canonical = getObjCObjectType(getCanonicalType(BaseType),
3454c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall                                    &Sorted[0], UniqueCount);
345554e14c4db764c0636160d26c5bbf491637c83a76John McCall    } else {
3456c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall      Canonical = getObjCObjectType(getCanonicalType(BaseType),
3457c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall                                    Protocols, NumProtocols);
345854e14c4db764c0636160d26c5bbf491637c83a76John McCall    }
345954e14c4db764c0636160d26c5bbf491637c83a76John McCall
346054e14c4db764c0636160d26c5bbf491637c83a76John McCall    // Regenerate InsertPos.
3461c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall    ObjCObjectTypes.FindNodeOrInsertPos(ID, InsertPos);
346254e14c4db764c0636160d26c5bbf491637c83a76John McCall  }
346354e14c4db764c0636160d26c5bbf491637c83a76John McCall
3464c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall  unsigned Size = sizeof(ObjCObjectTypeImpl);
3465c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall  Size += NumProtocols * sizeof(ObjCProtocolDecl *);
3466fd6a0887a099256c35a5b23e9afd517ffe95fa0aDouglas Gregor  void *Mem = Allocate(Size, TypeAlignment);
3467c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall  ObjCObjectTypeImpl *T =
3468c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall    new (Mem) ObjCObjectTypeImpl(Canonical, BaseType, Protocols, NumProtocols);
34691eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
3470c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall  Types.push_back(T);
3471c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall  ObjCObjectTypes.InsertNode(T, InsertPos);
3472c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall  return QualType(T, 0);
3473d1b3c2dd5bc1f3103bee6137957aa7c5f8f2f0bcSteve Naroff}
347488cb27a160adc305783a44f922ee4b216006ebf9Chris Lattner
3475c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall/// getObjCObjectPointerType - Return a ObjCObjectPointerType type for
3476c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall/// the given object type.
34774ba2a17694148e16eaa8d3917f657ffcd3667be4Jay FoadQualType ASTContext::getObjCObjectPointerType(QualType ObjectT) const {
34784b6c9051c6522894978c9ba6a819a659d102db36Fariborz Jahanian  llvm::FoldingSetNodeID ID;
3479c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall  ObjCObjectPointerType::Profile(ID, ObjectT);
34801eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
34814b6c9051c6522894978c9ba6a819a659d102db36Fariborz Jahanian  void *InsertPos = 0;
3482c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall  if (ObjCObjectPointerType *QT =
3483c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall              ObjCObjectPointerTypes.FindNodeOrInsertPos(ID, InsertPos))
34844b6c9051c6522894978c9ba6a819a659d102db36Fariborz Jahanian    return QualType(QT, 0);
34851eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
3486c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall  // Find the canonical object type.
348754e14c4db764c0636160d26c5bbf491637c83a76John McCall  QualType Canonical;
3488c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall  if (!ObjectT.isCanonical()) {
3489c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall    Canonical = getObjCObjectPointerType(getCanonicalType(ObjectT));
349054e14c4db764c0636160d26c5bbf491637c83a76John McCall
3491c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall    // Regenerate InsertPos.
3492c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall    ObjCObjectPointerTypes.FindNodeOrInsertPos(ID, InsertPos);
349354e14c4db764c0636160d26c5bbf491637c83a76John McCall  }
349454e14c4db764c0636160d26c5bbf491637c83a76John McCall
3495c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall  // No match.
3496c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall  void *Mem = Allocate(sizeof(ObjCObjectPointerType), TypeAlignment);
3497c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall  ObjCObjectPointerType *QType =
3498c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall    new (Mem) ObjCObjectPointerType(Canonical, ObjectT);
349924fab41057e4b67ed69a6b4027d5ae0f2f6934dcArgyrios Kyrtzidis
350024fab41057e4b67ed69a6b4027d5ae0f2f6934dcArgyrios Kyrtzidis  Types.push_back(QType);
3501c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall  ObjCObjectPointerTypes.InsertNode(QType, InsertPos);
350224fab41057e4b67ed69a6b4027d5ae0f2f6934dcArgyrios Kyrtzidis  return QualType(QType, 0);
350324fab41057e4b67ed69a6b4027d5ae0f2f6934dcArgyrios Kyrtzidis}
350424fab41057e4b67ed69a6b4027d5ae0f2f6934dcArgyrios Kyrtzidis
3505deacbdca554298ccdf636f19c6094a8825ec6b34Douglas Gregor/// getObjCInterfaceType - Return the unique reference to the type for the
3506deacbdca554298ccdf636f19c6094a8825ec6b34Douglas Gregor/// specified ObjC interface decl. The list of protocols is optional.
35070af550115df1f57f17a4f125ff0e8b34820c65d1Douglas GregorQualType ASTContext::getObjCInterfaceType(const ObjCInterfaceDecl *Decl,
35080af550115df1f57f17a4f125ff0e8b34820c65d1Douglas Gregor                                          ObjCInterfaceDecl *PrevDecl) const {
3509deacbdca554298ccdf636f19c6094a8825ec6b34Douglas Gregor  if (Decl->TypeForDecl)
3510deacbdca554298ccdf636f19c6094a8825ec6b34Douglas Gregor    return QualType(Decl->TypeForDecl, 0);
351174c730ad1f6818b676b0bad46d806a9176950328Sebastian Redl
35120af550115df1f57f17a4f125ff0e8b34820c65d1Douglas Gregor  if (PrevDecl) {
35130af550115df1f57f17a4f125ff0e8b34820c65d1Douglas Gregor    assert(PrevDecl->TypeForDecl && "previous decl has no TypeForDecl");
35140af550115df1f57f17a4f125ff0e8b34820c65d1Douglas Gregor    Decl->TypeForDecl = PrevDecl->TypeForDecl;
35150af550115df1f57f17a4f125ff0e8b34820c65d1Douglas Gregor    return QualType(PrevDecl->TypeForDecl, 0);
35160af550115df1f57f17a4f125ff0e8b34820c65d1Douglas Gregor  }
35170af550115df1f57f17a4f125ff0e8b34820c65d1Douglas Gregor
35188d2dbbf9ddfd9d762a341d83f83d840ff68ce03dDouglas Gregor  // Prefer the definition, if there is one.
35198d2dbbf9ddfd9d762a341d83f83d840ff68ce03dDouglas Gregor  if (const ObjCInterfaceDecl *Def = Decl->getDefinition())
35208d2dbbf9ddfd9d762a341d83f83d840ff68ce03dDouglas Gregor    Decl = Def;
35218d2dbbf9ddfd9d762a341d83f83d840ff68ce03dDouglas Gregor
3522deacbdca554298ccdf636f19c6094a8825ec6b34Douglas Gregor  void *Mem = Allocate(sizeof(ObjCInterfaceType), TypeAlignment);
3523deacbdca554298ccdf636f19c6094a8825ec6b34Douglas Gregor  ObjCInterfaceType *T = new (Mem) ObjCInterfaceType(Decl);
3524deacbdca554298ccdf636f19c6094a8825ec6b34Douglas Gregor  Decl->TypeForDecl = T;
3525deacbdca554298ccdf636f19c6094a8825ec6b34Douglas Gregor  Types.push_back(T);
3526deacbdca554298ccdf636f19c6094a8825ec6b34Douglas Gregor  return QualType(T, 0);
3527c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall}
3528c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall
352972564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor/// getTypeOfExprType - Unlike many "get<Type>" functions, we can't unique
353072564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor/// TypeOfExprType AST's (since expression's are never shared). For example,
35319752f25748d954df99087d741ea35db37ff16beaSteve Naroff/// multiple declarations that refer to "typeof(x)" all contain different
35321eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// DeclRefExpr's. This doesn't effect the type checker, since it operates
35339752f25748d954df99087d741ea35db37ff16beaSteve Naroff/// on canonical type's (which are always unique).
35344ba2a17694148e16eaa8d3917f657ffcd3667be4Jay FoadQualType ASTContext::getTypeOfExprType(Expr *tofExpr) const {
3535dd0257c77719a13d4acd513df40b04300cbfc871Douglas Gregor  TypeOfExprType *toe;
3536b197572cf1cd70a817a1f546478cb2cb9112c48eDouglas Gregor  if (tofExpr->isTypeDependent()) {
3537b197572cf1cd70a817a1f546478cb2cb9112c48eDouglas Gregor    llvm::FoldingSetNodeID ID;
3538b197572cf1cd70a817a1f546478cb2cb9112c48eDouglas Gregor    DependentTypeOfExprType::Profile(ID, *this, tofExpr);
35391eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
3540b197572cf1cd70a817a1f546478cb2cb9112c48eDouglas Gregor    void *InsertPos = 0;
3541b197572cf1cd70a817a1f546478cb2cb9112c48eDouglas Gregor    DependentTypeOfExprType *Canon
3542b197572cf1cd70a817a1f546478cb2cb9112c48eDouglas Gregor      = DependentTypeOfExprTypes.FindNodeOrInsertPos(ID, InsertPos);
3543b197572cf1cd70a817a1f546478cb2cb9112c48eDouglas Gregor    if (Canon) {
3544b197572cf1cd70a817a1f546478cb2cb9112c48eDouglas Gregor      // We already have a "canonical" version of an identical, dependent
3545b197572cf1cd70a817a1f546478cb2cb9112c48eDouglas Gregor      // typeof(expr) type. Use that as our canonical type.
35466b304a0254a13f42390b865ff5ba668a49cc58aeJohn McCall      toe = new (*this, TypeAlignment) TypeOfExprType(tofExpr,
3547b197572cf1cd70a817a1f546478cb2cb9112c48eDouglas Gregor                                          QualType((TypeOfExprType*)Canon, 0));
35483060178ad9df29789505c1e6debcfc80a3a13587Chad Rosier    } else {
3549b197572cf1cd70a817a1f546478cb2cb9112c48eDouglas Gregor      // Build a new, canonical typeof(expr) type.
35506b304a0254a13f42390b865ff5ba668a49cc58aeJohn McCall      Canon
35516b304a0254a13f42390b865ff5ba668a49cc58aeJohn McCall        = new (*this, TypeAlignment) DependentTypeOfExprType(*this, tofExpr);
3552b197572cf1cd70a817a1f546478cb2cb9112c48eDouglas Gregor      DependentTypeOfExprTypes.InsertNode(Canon, InsertPos);
3553b197572cf1cd70a817a1f546478cb2cb9112c48eDouglas Gregor      toe = Canon;
3554b197572cf1cd70a817a1f546478cb2cb9112c48eDouglas Gregor    }
3555b197572cf1cd70a817a1f546478cb2cb9112c48eDouglas Gregor  } else {
3556dd0257c77719a13d4acd513df40b04300cbfc871Douglas Gregor    QualType Canonical = getCanonicalType(tofExpr->getType());
35576b304a0254a13f42390b865ff5ba668a49cc58aeJohn McCall    toe = new (*this, TypeAlignment) TypeOfExprType(tofExpr, Canonical);
3558dd0257c77719a13d4acd513df40b04300cbfc871Douglas Gregor  }
35599752f25748d954df99087d741ea35db37ff16beaSteve Naroff  Types.push_back(toe);
35609752f25748d954df99087d741ea35db37ff16beaSteve Naroff  return QualType(toe, 0);
3561d1861fd633d5096a00777c918eb8575ea7162fe7Steve Naroff}
3562d1861fd633d5096a00777c918eb8575ea7162fe7Steve Naroff
35639752f25748d954df99087d741ea35db37ff16beaSteve Naroff/// getTypeOfType -  Unlike many "get<Type>" functions, we don't unique
35649752f25748d954df99087d741ea35db37ff16beaSteve Naroff/// TypeOfType AST's. The only motivation to unique these nodes would be
35659752f25748d954df99087d741ea35db37ff16beaSteve Naroff/// memory savings. Since typeof(t) is fairly uncommon, space shouldn't be
35661eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// an issue. This doesn't effect the type checker, since it operates
35679752f25748d954df99087d741ea35db37ff16beaSteve Naroff/// on canonical type's (which are always unique).
35684ba2a17694148e16eaa8d3917f657ffcd3667be4Jay FoadQualType ASTContext::getTypeOfType(QualType tofType) const {
3569f52ab250ff92bc51a9ac9a8e19bd43b63a5f844fChris Lattner  QualType Canonical = getCanonicalType(tofType);
35706b304a0254a13f42390b865ff5ba668a49cc58aeJohn McCall  TypeOfType *tot = new (*this, TypeAlignment) TypeOfType(tofType, Canonical);
35719752f25748d954df99087d741ea35db37ff16beaSteve Naroff  Types.push_back(tot);
35729752f25748d954df99087d741ea35db37ff16beaSteve Naroff  return QualType(tot, 0);
3573d1861fd633d5096a00777c918eb8575ea7162fe7Steve Naroff}
3574d1861fd633d5096a00777c918eb8575ea7162fe7Steve Naroff
357560a9a2a404a4cf259d39133383e922aa00ca9043Anders Carlsson
3576395b475a4474f1c7574d927ad142ca0c7997cbcaAnders Carlsson/// getDecltypeType -  Unlike many "get<Type>" functions, we don't unique
3577395b475a4474f1c7574d927ad142ca0c7997cbcaAnders Carlsson/// DecltypeType AST's. The only motivation to unique these nodes would be
3578395b475a4474f1c7574d927ad142ca0c7997cbcaAnders Carlsson/// memory savings. Since decltype(t) is fairly uncommon, space shouldn't be
35791eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// an issue. This doesn't effect the type checker, since it operates
358039e02032b01874a0d02ba85a4cd3922adda81376David Blaikie/// on canonical types (which are always unique).
3581f8af98286022f72157d84951b48fde5fb369ab29Douglas GregorQualType ASTContext::getDecltypeType(Expr *e, QualType UnderlyingType) const {
3582dd0257c77719a13d4acd513df40b04300cbfc871Douglas Gregor  DecltypeType *dt;
3583561f81243f665cf2001caadc45df505f826b72d6Douglas Gregor
3584561f81243f665cf2001caadc45df505f826b72d6Douglas Gregor  // C++0x [temp.type]p2:
3585561f81243f665cf2001caadc45df505f826b72d6Douglas Gregor  //   If an expression e involves a template parameter, decltype(e) denotes a
3586561f81243f665cf2001caadc45df505f826b72d6Douglas Gregor  //   unique dependent type. Two such decltype-specifiers refer to the same
3587561f81243f665cf2001caadc45df505f826b72d6Douglas Gregor  //   type only if their expressions are equivalent (14.5.6.1).
3588561f81243f665cf2001caadc45df505f826b72d6Douglas Gregor  if (e->isInstantiationDependent()) {
35899d702ae1cd5cfa19d884cbef77e1df99395138bbDouglas Gregor    llvm::FoldingSetNodeID ID;
35909d702ae1cd5cfa19d884cbef77e1df99395138bbDouglas Gregor    DependentDecltypeType::Profile(ID, *this, e);
35911eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
35929d702ae1cd5cfa19d884cbef77e1df99395138bbDouglas Gregor    void *InsertPos = 0;
35939d702ae1cd5cfa19d884cbef77e1df99395138bbDouglas Gregor    DependentDecltypeType *Canon
35949d702ae1cd5cfa19d884cbef77e1df99395138bbDouglas Gregor      = DependentDecltypeTypes.FindNodeOrInsertPos(ID, InsertPos);
35959d702ae1cd5cfa19d884cbef77e1df99395138bbDouglas Gregor    if (Canon) {
35969d702ae1cd5cfa19d884cbef77e1df99395138bbDouglas Gregor      // We already have a "canonical" version of an equivalent, dependent
35979d702ae1cd5cfa19d884cbef77e1df99395138bbDouglas Gregor      // decltype type. Use that as our canonical type.
35980d729105ecb50a7e3cbe6e57c29149edfa5cf05aRichard Smith      dt = new (*this, TypeAlignment) DecltypeType(e, UnderlyingType,
35999d702ae1cd5cfa19d884cbef77e1df99395138bbDouglas Gregor                                       QualType((DecltypeType*)Canon, 0));
36003060178ad9df29789505c1e6debcfc80a3a13587Chad Rosier    } else {
36019d702ae1cd5cfa19d884cbef77e1df99395138bbDouglas Gregor      // Build a new, canonical typeof(expr) type.
36026b304a0254a13f42390b865ff5ba668a49cc58aeJohn McCall      Canon = new (*this, TypeAlignment) DependentDecltypeType(*this, e);
36039d702ae1cd5cfa19d884cbef77e1df99395138bbDouglas Gregor      DependentDecltypeTypes.InsertNode(Canon, InsertPos);
36049d702ae1cd5cfa19d884cbef77e1df99395138bbDouglas Gregor      dt = Canon;
36059d702ae1cd5cfa19d884cbef77e1df99395138bbDouglas Gregor    }
36069d702ae1cd5cfa19d884cbef77e1df99395138bbDouglas Gregor  } else {
3607f8af98286022f72157d84951b48fde5fb369ab29Douglas Gregor    dt = new (*this, TypeAlignment) DecltypeType(e, UnderlyingType,
3608f8af98286022f72157d84951b48fde5fb369ab29Douglas Gregor                                      getCanonicalType(UnderlyingType));
3609dd0257c77719a13d4acd513df40b04300cbfc871Douglas Gregor  }
3610395b475a4474f1c7574d927ad142ca0c7997cbcaAnders Carlsson  Types.push_back(dt);
3611395b475a4474f1c7574d927ad142ca0c7997cbcaAnders Carlsson  return QualType(dt, 0);
3612395b475a4474f1c7574d927ad142ca0c7997cbcaAnders Carlsson}
3613395b475a4474f1c7574d927ad142ca0c7997cbcaAnders Carlsson
3614ca63c200346c0ca9e00194ec6e34a5a7b0ed9321Sean Hunt/// getUnaryTransformationType - We don't unique these, since the memory
3615ca63c200346c0ca9e00194ec6e34a5a7b0ed9321Sean Hunt/// savings are minimal and these are rare.
3616ca63c200346c0ca9e00194ec6e34a5a7b0ed9321Sean HuntQualType ASTContext::getUnaryTransformType(QualType BaseType,
3617ca63c200346c0ca9e00194ec6e34a5a7b0ed9321Sean Hunt                                           QualType UnderlyingType,
3618ca63c200346c0ca9e00194ec6e34a5a7b0ed9321Sean Hunt                                           UnaryTransformType::UTTKind Kind)
3619ca63c200346c0ca9e00194ec6e34a5a7b0ed9321Sean Hunt    const {
3620ca63c200346c0ca9e00194ec6e34a5a7b0ed9321Sean Hunt  UnaryTransformType *Ty =
362169d9775da47a4b9f165dbc33277f02982928a94eDouglas Gregor    new (*this, TypeAlignment) UnaryTransformType (BaseType, UnderlyingType,
362269d9775da47a4b9f165dbc33277f02982928a94eDouglas Gregor                                                   Kind,
362369d9775da47a4b9f165dbc33277f02982928a94eDouglas Gregor                                 UnderlyingType->isDependentType() ?
362412fc4b0624706b474fa10308631fa8daf92f340fPeter Collingbourne                                 QualType() : getCanonicalType(UnderlyingType));
3625ca63c200346c0ca9e00194ec6e34a5a7b0ed9321Sean Hunt  Types.push_back(Ty);
3626ca63c200346c0ca9e00194ec6e34a5a7b0ed9321Sean Hunt  return QualType(Ty, 0);
3627ca63c200346c0ca9e00194ec6e34a5a7b0ed9321Sean Hunt}
3628ca63c200346c0ca9e00194ec6e34a5a7b0ed9321Sean Hunt
362960e141e1f87211ca831de6821003d80fe20a06f3Richard Smith/// getAutoType - Return the uniqued reference to the 'auto' type which has been
363060e141e1f87211ca831de6821003d80fe20a06f3Richard Smith/// deduced to the given type, or to the canonical undeduced 'auto' type, or the
363160e141e1f87211ca831de6821003d80fe20a06f3Richard Smith/// canonical deduced-but-dependent 'auto' type.
363260e141e1f87211ca831de6821003d80fe20a06f3Richard SmithQualType ASTContext::getAutoType(QualType DeducedType, bool IsDecltypeAuto,
3633dc7a4f5d7a7e3b60d4dc4a80338d7a2728540998Richard Smith                                 bool IsDependent) const {
363460e141e1f87211ca831de6821003d80fe20a06f3Richard Smith  if (DeducedType.isNull() && !IsDecltypeAuto && !IsDependent)
363560e141e1f87211ca831de6821003d80fe20a06f3Richard Smith    return getAutoDeductType();
363660e141e1f87211ca831de6821003d80fe20a06f3Richard Smith
363760e141e1f87211ca831de6821003d80fe20a06f3Richard Smith  // Look in the folding set for an existing type.
3638483b9f3bc05c5409e2c6643f1c9d91e21c8ff9d2Richard Smith  void *InsertPos = 0;
363960e141e1f87211ca831de6821003d80fe20a06f3Richard Smith  llvm::FoldingSetNodeID ID;
364060e141e1f87211ca831de6821003d80fe20a06f3Richard Smith  AutoType::Profile(ID, DeducedType, IsDecltypeAuto, IsDependent);
364160e141e1f87211ca831de6821003d80fe20a06f3Richard Smith  if (AutoType *AT = AutoTypes.FindNodeOrInsertPos(ID, InsertPos))
364260e141e1f87211ca831de6821003d80fe20a06f3Richard Smith    return QualType(AT, 0);
3643483b9f3bc05c5409e2c6643f1c9d91e21c8ff9d2Richard Smith
3644a2c3646c35dd09d21b74826240aa916545b1873fRichard Smith  AutoType *AT = new (*this, TypeAlignment) AutoType(DeducedType,
3645dc7a4f5d7a7e3b60d4dc4a80338d7a2728540998Richard Smith                                                     IsDecltypeAuto,
3646dc7a4f5d7a7e3b60d4dc4a80338d7a2728540998Richard Smith                                                     IsDependent);
3647483b9f3bc05c5409e2c6643f1c9d91e21c8ff9d2Richard Smith  Types.push_back(AT);
3648483b9f3bc05c5409e2c6643f1c9d91e21c8ff9d2Richard Smith  if (InsertPos)
3649483b9f3bc05c5409e2c6643f1c9d91e21c8ff9d2Richard Smith    AutoTypes.InsertNode(AT, InsertPos);
3650483b9f3bc05c5409e2c6643f1c9d91e21c8ff9d2Richard Smith  return QualType(AT, 0);
365134b41d939a1328f484511c6002ba2456db879a29Richard Smith}
365234b41d939a1328f484511c6002ba2456db879a29Richard Smith
3653b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman/// getAtomicType - Return the uniqued reference to the atomic type for
3654b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman/// the given value type.
3655b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli FriedmanQualType ASTContext::getAtomicType(QualType T) const {
3656b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman  // Unique pointers, to guarantee there is only one pointer of a particular
3657b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman  // structure.
3658b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman  llvm::FoldingSetNodeID ID;
3659b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman  AtomicType::Profile(ID, T);
3660b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman
3661b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman  void *InsertPos = 0;
3662b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman  if (AtomicType *AT = AtomicTypes.FindNodeOrInsertPos(ID, InsertPos))
3663b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman    return QualType(AT, 0);
3664b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman
3665b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman  // If the atomic value type isn't canonical, this won't be a canonical type
3666b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman  // either, so fill in the canonical type field.
3667b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman  QualType Canonical;
3668b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman  if (!T.isCanonical()) {
3669b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman    Canonical = getAtomicType(getCanonicalType(T));
3670b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman
3671b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman    // Get the new insert position for the node we care about.
3672b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman    AtomicType *NewIP = AtomicTypes.FindNodeOrInsertPos(ID, InsertPos);
3673b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman    assert(NewIP == 0 && "Shouldn't be in the map!"); (void)NewIP;
3674b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman  }
3675b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman  AtomicType *New = new (*this, TypeAlignment) AtomicType(T, Canonical);
3676b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman  Types.push_back(New);
3677b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman  AtomicTypes.InsertNode(New, InsertPos);
3678b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman  return QualType(New, 0);
3679b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman}
3680b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman
3681ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith/// getAutoDeductType - Get type pattern for deducing against 'auto'.
3682ad762fcdc16b9e4705b12b09d92b8c026212b906Richard SmithQualType ASTContext::getAutoDeductType() const {
3683ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith  if (AutoDeductTy.isNull())
368460e141e1f87211ca831de6821003d80fe20a06f3Richard Smith    AutoDeductTy = QualType(
368560e141e1f87211ca831de6821003d80fe20a06f3Richard Smith      new (*this, TypeAlignment) AutoType(QualType(), /*decltype(auto)*/false,
368660e141e1f87211ca831de6821003d80fe20a06f3Richard Smith                                          /*dependent*/false),
368760e141e1f87211ca831de6821003d80fe20a06f3Richard Smith      0);
3688ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith  return AutoDeductTy;
3689ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith}
3690ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith
3691ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith/// getAutoRRefDeductType - Get type pattern for deducing against 'auto &&'.
3692ad762fcdc16b9e4705b12b09d92b8c026212b906Richard SmithQualType ASTContext::getAutoRRefDeductType() const {
3693ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith  if (AutoRRefDeductTy.isNull())
3694ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith    AutoRRefDeductTy = getRValueReferenceType(getAutoDeductType());
3695ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith  assert(!AutoRRefDeductTy.isNull() && "can't build 'auto &&' pattern");
3696ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith  return AutoRRefDeductTy;
3697ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith}
3698ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith
36995f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// getTagDeclType - Return the unique reference to the type for the
37005f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// specified TagDecl (struct/union/class/enum) decl.
37014ba2a17694148e16eaa8d3917f657ffcd3667be4Jay FoadQualType ASTContext::getTagDeclType(const TagDecl *Decl) const {
3702d778f88d32b96a74c9edb7342c81357606a7cdc0Ted Kremenek  assert (Decl);
3703e607ed068334bacb8d7b093996b4671c6ca79e25Mike Stump  // FIXME: What is the design on getTagDeclType when it requires casting
3704e607ed068334bacb8d7b093996b4671c6ca79e25Mike Stump  // away const?  mutable?
3705e607ed068334bacb8d7b093996b4671c6ca79e25Mike Stump  return getTypeDeclType(const_cast<TagDecl*>(Decl));
37065f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
37075f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
37081eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// getSizeType - Return the unique type for "size_t" (C99 7.17), the result
37091eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// of the sizeof operator (C99 6.5.3.4p4). The value is target dependent and
37101eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// needs to agree with the definition in <stddef.h>.
3711a3ccda58913cc1a4b8564e349448b12acc462da7Anders CarlssonCanQualType ASTContext::getSizeType() const {
3712bcfd1f55bfbb3e5944cd5e03d07b343e280838c4Douglas Gregor  return getFromTargetType(Target->getSizeType());
37135f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
37145f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
371529e97cb35fab314388f62b68fefa78947e93c1dcHans Wennborg/// getIntMaxType - Return the unique type for "intmax_t" (C99 7.18.1.5).
371629e97cb35fab314388f62b68fefa78947e93c1dcHans WennborgCanQualType ASTContext::getIntMaxType() const {
371729e97cb35fab314388f62b68fefa78947e93c1dcHans Wennborg  return getFromTargetType(Target->getIntMaxType());
371829e97cb35fab314388f62b68fefa78947e93c1dcHans Wennborg}
371929e97cb35fab314388f62b68fefa78947e93c1dcHans Wennborg
372029e97cb35fab314388f62b68fefa78947e93c1dcHans Wennborg/// getUIntMaxType - Return the unique type for "uintmax_t" (C99 7.18.1.5).
372129e97cb35fab314388f62b68fefa78947e93c1dcHans WennborgCanQualType ASTContext::getUIntMaxType() const {
372229e97cb35fab314388f62b68fefa78947e93c1dcHans Wennborg  return getFromTargetType(Target->getUIntMaxType());
372329e97cb35fab314388f62b68fefa78947e93c1dcHans Wennborg}
372429e97cb35fab314388f62b68fefa78947e93c1dcHans Wennborg
372564c438a4be2a871fa43c78264663ba1e9788b94dArgyrios Kyrtzidis/// getSignedWCharType - Return the type of "signed wchar_t".
372664c438a4be2a871fa43c78264663ba1e9788b94dArgyrios Kyrtzidis/// Used when in C++, as a GCC extension.
372764c438a4be2a871fa43c78264663ba1e9788b94dArgyrios KyrtzidisQualType ASTContext::getSignedWCharType() const {
372864c438a4be2a871fa43c78264663ba1e9788b94dArgyrios Kyrtzidis  // FIXME: derive from "Target" ?
372964c438a4be2a871fa43c78264663ba1e9788b94dArgyrios Kyrtzidis  return WCharTy;
373064c438a4be2a871fa43c78264663ba1e9788b94dArgyrios Kyrtzidis}
373164c438a4be2a871fa43c78264663ba1e9788b94dArgyrios Kyrtzidis
373264c438a4be2a871fa43c78264663ba1e9788b94dArgyrios Kyrtzidis/// getUnsignedWCharType - Return the type of "unsigned wchar_t".
373364c438a4be2a871fa43c78264663ba1e9788b94dArgyrios Kyrtzidis/// Used when in C++, as a GCC extension.
373464c438a4be2a871fa43c78264663ba1e9788b94dArgyrios KyrtzidisQualType ASTContext::getUnsignedWCharType() const {
373564c438a4be2a871fa43c78264663ba1e9788b94dArgyrios Kyrtzidis  // FIXME: derive from "Target" ?
373664c438a4be2a871fa43c78264663ba1e9788b94dArgyrios Kyrtzidis  return UnsignedIntTy;
373764c438a4be2a871fa43c78264663ba1e9788b94dArgyrios Kyrtzidis}
373864c438a4be2a871fa43c78264663ba1e9788b94dArgyrios Kyrtzidis
37399677eb864f7354f140cd1bf59557843851e65de4Enea ZaffanellaQualType ASTContext::getIntPtrType() const {
37409677eb864f7354f140cd1bf59557843851e65de4Enea Zaffanella  return getFromTargetType(Target->getIntPtrType());
37419677eb864f7354f140cd1bf59557843851e65de4Enea Zaffanella}
37429677eb864f7354f140cd1bf59557843851e65de4Enea Zaffanella
37439677eb864f7354f140cd1bf59557843851e65de4Enea ZaffanellaQualType ASTContext::getUIntPtrType() const {
37449677eb864f7354f140cd1bf59557843851e65de4Enea Zaffanella  return getCorrespondingUnsignedType(getIntPtrType());
37459677eb864f7354f140cd1bf59557843851e65de4Enea Zaffanella}
37469677eb864f7354f140cd1bf59557843851e65de4Enea Zaffanella
374729e97cb35fab314388f62b68fefa78947e93c1dcHans Wennborg/// getPointerDiffType - Return the unique type for "ptrdiff_t" (C99 7.17)
37488b9023ba35a86838789e2c9034a6128728c547aaChris Lattner/// defined in <stddef.h>. Pointer - pointer requires this (C99 6.5.6p9).
37498b9023ba35a86838789e2c9034a6128728c547aaChris LattnerQualType ASTContext::getPointerDiffType() const {
3750bcfd1f55bfbb3e5944cd5e03d07b343e280838c4Douglas Gregor  return getFromTargetType(Target->getPtrDiffType(0));
37518b9023ba35a86838789e2c9034a6128728c547aaChris Lattner}
37528b9023ba35a86838789e2c9034a6128728c547aaChris Lattner
37536902e4146d426998ff6a94b16776c1b3f805a048Eli Friedman/// \brief Return the unique type for "pid_t" defined in
37546902e4146d426998ff6a94b16776c1b3f805a048Eli Friedman/// <sys/types.h>. We need this to compute the correct type for vfork().
37556902e4146d426998ff6a94b16776c1b3f805a048Eli FriedmanQualType ASTContext::getProcessIDType() const {
37566902e4146d426998ff6a94b16776c1b3f805a048Eli Friedman  return getFromTargetType(Target->getProcessIDType());
37576902e4146d426998ff6a94b16776c1b3f805a048Eli Friedman}
37586902e4146d426998ff6a94b16776c1b3f805a048Eli Friedman
3759e6327747b72bb687c948270f702ff53c30f411a6Chris Lattner//===----------------------------------------------------------------------===//
3760e6327747b72bb687c948270f702ff53c30f411a6Chris Lattner//                              Type Operators
3761e6327747b72bb687c948270f702ff53c30f411a6Chris Lattner//===----------------------------------------------------------------------===//
3762e6327747b72bb687c948270f702ff53c30f411a6Chris Lattner
37634ba2a17694148e16eaa8d3917f657ffcd3667be4Jay FoadCanQualType ASTContext::getCanonicalParamType(QualType T) const {
376454e14c4db764c0636160d26c5bbf491637c83a76John McCall  // Push qualifiers into arrays, and then discard any remaining
376554e14c4db764c0636160d26c5bbf491637c83a76John McCall  // qualifiers.
376654e14c4db764c0636160d26c5bbf491637c83a76John McCall  T = getCanonicalType(T);
3767745da3a5bb4ea35f93f50301e7fbbb7d78d3b6bbFariborz Jahanian  T = getVariableArrayDecayedType(T);
376854e14c4db764c0636160d26c5bbf491637c83a76John McCall  const Type *Ty = T.getTypePtr();
376954e14c4db764c0636160d26c5bbf491637c83a76John McCall  QualType Result;
377054e14c4db764c0636160d26c5bbf491637c83a76John McCall  if (isa<ArrayType>(Ty)) {
377154e14c4db764c0636160d26c5bbf491637c83a76John McCall    Result = getArrayDecayedType(QualType(Ty,0));
377254e14c4db764c0636160d26c5bbf491637c83a76John McCall  } else if (isa<FunctionType>(Ty)) {
377354e14c4db764c0636160d26c5bbf491637c83a76John McCall    Result = getPointerType(QualType(Ty, 0));
377454e14c4db764c0636160d26c5bbf491637c83a76John McCall  } else {
377554e14c4db764c0636160d26c5bbf491637c83a76John McCall    Result = QualType(Ty, 0);
377654e14c4db764c0636160d26c5bbf491637c83a76John McCall  }
377754e14c4db764c0636160d26c5bbf491637c83a76John McCall
377854e14c4db764c0636160d26c5bbf491637c83a76John McCall  return CanQualType::CreateUnsafe(Result);
377954e14c4db764c0636160d26c5bbf491637c83a76John McCall}
378054e14c4db764c0636160d26c5bbf491637c83a76John McCall
378162c28c831bbf207cc36e683e7c321fc33bf8928cJohn McCallQualType ASTContext::getUnqualifiedArrayType(QualType type,
378262c28c831bbf207cc36e683e7c321fc33bf8928cJohn McCall                                             Qualifiers &quals) {
378362c28c831bbf207cc36e683e7c321fc33bf8928cJohn McCall  SplitQualType splitType = type.getSplitUnqualifiedType();
378462c28c831bbf207cc36e683e7c321fc33bf8928cJohn McCall
378562c28c831bbf207cc36e683e7c321fc33bf8928cJohn McCall  // FIXME: getSplitUnqualifiedType() actually walks all the way to
378662c28c831bbf207cc36e683e7c321fc33bf8928cJohn McCall  // the unqualified desugared type and then drops it on the floor.
378762c28c831bbf207cc36e683e7c321fc33bf8928cJohn McCall  // We then have to strip that sugar back off with
378862c28c831bbf207cc36e683e7c321fc33bf8928cJohn McCall  // getUnqualifiedDesugaredType(), which is silly.
378962c28c831bbf207cc36e683e7c321fc33bf8928cJohn McCall  const ArrayType *AT =
3790200fa53fd420aa8369586f569dbece04930ad6a3John McCall    dyn_cast<ArrayType>(splitType.Ty->getUnqualifiedDesugaredType());
379162c28c831bbf207cc36e683e7c321fc33bf8928cJohn McCall
379262c28c831bbf207cc36e683e7c321fc33bf8928cJohn McCall  // If we don't have an array, just use the results in splitType.
37939dadd94e1c17fa030d1f88d8f2113ff59ccc6714Douglas Gregor  if (!AT) {
3794200fa53fd420aa8369586f569dbece04930ad6a3John McCall    quals = splitType.Quals;
3795200fa53fd420aa8369586f569dbece04930ad6a3John McCall    return QualType(splitType.Ty, 0);
379628e318cc6008c2bc008f0caee70dc736a03d6289Chandler Carruth  }
379728e318cc6008c2bc008f0caee70dc736a03d6289Chandler Carruth
379862c28c831bbf207cc36e683e7c321fc33bf8928cJohn McCall  // Otherwise, recurse on the array's element type.
379962c28c831bbf207cc36e683e7c321fc33bf8928cJohn McCall  QualType elementType = AT->getElementType();
380062c28c831bbf207cc36e683e7c321fc33bf8928cJohn McCall  QualType unqualElementType = getUnqualifiedArrayType(elementType, quals);
380162c28c831bbf207cc36e683e7c321fc33bf8928cJohn McCall
380262c28c831bbf207cc36e683e7c321fc33bf8928cJohn McCall  // If that didn't change the element type, AT has no qualifiers, so we
380362c28c831bbf207cc36e683e7c321fc33bf8928cJohn McCall  // can just use the results in splitType.
380462c28c831bbf207cc36e683e7c321fc33bf8928cJohn McCall  if (elementType == unqualElementType) {
380562c28c831bbf207cc36e683e7c321fc33bf8928cJohn McCall    assert(quals.empty()); // from the recursive call
3806200fa53fd420aa8369586f569dbece04930ad6a3John McCall    quals = splitType.Quals;
3807200fa53fd420aa8369586f569dbece04930ad6a3John McCall    return QualType(splitType.Ty, 0);
380862c28c831bbf207cc36e683e7c321fc33bf8928cJohn McCall  }
380962c28c831bbf207cc36e683e7c321fc33bf8928cJohn McCall
381062c28c831bbf207cc36e683e7c321fc33bf8928cJohn McCall  // Otherwise, add in the qualifiers from the outermost type, then
381162c28c831bbf207cc36e683e7c321fc33bf8928cJohn McCall  // build the type back up.
3812200fa53fd420aa8369586f569dbece04930ad6a3John McCall  quals.addConsistentQualifiers(splitType.Quals);
381328e318cc6008c2bc008f0caee70dc736a03d6289Chandler Carruth
38149dadd94e1c17fa030d1f88d8f2113ff59ccc6714Douglas Gregor  if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(AT)) {
381562c28c831bbf207cc36e683e7c321fc33bf8928cJohn McCall    return getConstantArrayType(unqualElementType, CAT->getSize(),
381628e318cc6008c2bc008f0caee70dc736a03d6289Chandler Carruth                                CAT->getSizeModifier(), 0);
381728e318cc6008c2bc008f0caee70dc736a03d6289Chandler Carruth  }
381828e318cc6008c2bc008f0caee70dc736a03d6289Chandler Carruth
38199dadd94e1c17fa030d1f88d8f2113ff59ccc6714Douglas Gregor  if (const IncompleteArrayType *IAT = dyn_cast<IncompleteArrayType>(AT)) {
382062c28c831bbf207cc36e683e7c321fc33bf8928cJohn McCall    return getIncompleteArrayType(unqualElementType, IAT->getSizeModifier(), 0);
382128e318cc6008c2bc008f0caee70dc736a03d6289Chandler Carruth  }
382228e318cc6008c2bc008f0caee70dc736a03d6289Chandler Carruth
38239dadd94e1c17fa030d1f88d8f2113ff59ccc6714Douglas Gregor  if (const VariableArrayType *VAT = dyn_cast<VariableArrayType>(AT)) {
382462c28c831bbf207cc36e683e7c321fc33bf8928cJohn McCall    return getVariableArrayType(unqualElementType,
38253fa5cae9b3812cab9fab6c042c3329bb70a3d046John McCall                                VAT->getSizeExpr(),
38269dadd94e1c17fa030d1f88d8f2113ff59ccc6714Douglas Gregor                                VAT->getSizeModifier(),
38279dadd94e1c17fa030d1f88d8f2113ff59ccc6714Douglas Gregor                                VAT->getIndexTypeCVRQualifiers(),
38289dadd94e1c17fa030d1f88d8f2113ff59ccc6714Douglas Gregor                                VAT->getBracketsRange());
38299dadd94e1c17fa030d1f88d8f2113ff59ccc6714Douglas Gregor  }
38309dadd94e1c17fa030d1f88d8f2113ff59ccc6714Douglas Gregor
38319dadd94e1c17fa030d1f88d8f2113ff59ccc6714Douglas Gregor  const DependentSizedArrayType *DSAT = cast<DependentSizedArrayType>(AT);
383262c28c831bbf207cc36e683e7c321fc33bf8928cJohn McCall  return getDependentSizedArrayType(unqualElementType, DSAT->getSizeExpr(),
383328e318cc6008c2bc008f0caee70dc736a03d6289Chandler Carruth                                    DSAT->getSizeModifier(), 0,
383428e318cc6008c2bc008f0caee70dc736a03d6289Chandler Carruth                                    SourceRange());
383528e318cc6008c2bc008f0caee70dc736a03d6289Chandler Carruth}
383628e318cc6008c2bc008f0caee70dc736a03d6289Chandler Carruth
38375a57efd7bf88a4a13018e0471ded8063a4abe8afDouglas Gregor/// UnwrapSimilarPointerTypes - If T1 and T2 are pointer types  that
38385a57efd7bf88a4a13018e0471ded8063a4abe8afDouglas Gregor/// may be similar (C++ 4.4), replaces T1 and T2 with the type that
38395a57efd7bf88a4a13018e0471ded8063a4abe8afDouglas Gregor/// they point to and return true. If T1 and T2 aren't pointer types
38405a57efd7bf88a4a13018e0471ded8063a4abe8afDouglas Gregor/// or pointer-to-member types, or if they are not similar at this
38415a57efd7bf88a4a13018e0471ded8063a4abe8afDouglas Gregor/// level, returns false and leaves T1 and T2 unchanged. Top-level
38425a57efd7bf88a4a13018e0471ded8063a4abe8afDouglas Gregor/// qualifiers on T1 and T2 are ignored. This function will typically
38435a57efd7bf88a4a13018e0471ded8063a4abe8afDouglas Gregor/// be called in a loop that successively "unwraps" pointer and
38445a57efd7bf88a4a13018e0471ded8063a4abe8afDouglas Gregor/// pointer-to-member types to compare them at each level.
38455a57efd7bf88a4a13018e0471ded8063a4abe8afDouglas Gregorbool ASTContext::UnwrapSimilarPointerTypes(QualType &T1, QualType &T2) {
38465a57efd7bf88a4a13018e0471ded8063a4abe8afDouglas Gregor  const PointerType *T1PtrType = T1->getAs<PointerType>(),
38475a57efd7bf88a4a13018e0471ded8063a4abe8afDouglas Gregor                    *T2PtrType = T2->getAs<PointerType>();
38485a57efd7bf88a4a13018e0471ded8063a4abe8afDouglas Gregor  if (T1PtrType && T2PtrType) {
38495a57efd7bf88a4a13018e0471ded8063a4abe8afDouglas Gregor    T1 = T1PtrType->getPointeeType();
38505a57efd7bf88a4a13018e0471ded8063a4abe8afDouglas Gregor    T2 = T2PtrType->getPointeeType();
38515a57efd7bf88a4a13018e0471ded8063a4abe8afDouglas Gregor    return true;
38525a57efd7bf88a4a13018e0471ded8063a4abe8afDouglas Gregor  }
38535a57efd7bf88a4a13018e0471ded8063a4abe8afDouglas Gregor
38545a57efd7bf88a4a13018e0471ded8063a4abe8afDouglas Gregor  const MemberPointerType *T1MPType = T1->getAs<MemberPointerType>(),
38555a57efd7bf88a4a13018e0471ded8063a4abe8afDouglas Gregor                          *T2MPType = T2->getAs<MemberPointerType>();
38565a57efd7bf88a4a13018e0471ded8063a4abe8afDouglas Gregor  if (T1MPType && T2MPType &&
38575a57efd7bf88a4a13018e0471ded8063a4abe8afDouglas Gregor      hasSameUnqualifiedType(QualType(T1MPType->getClass(), 0),
38585a57efd7bf88a4a13018e0471ded8063a4abe8afDouglas Gregor                             QualType(T2MPType->getClass(), 0))) {
38595a57efd7bf88a4a13018e0471ded8063a4abe8afDouglas Gregor    T1 = T1MPType->getPointeeType();
38605a57efd7bf88a4a13018e0471ded8063a4abe8afDouglas Gregor    T2 = T2MPType->getPointeeType();
38615a57efd7bf88a4a13018e0471ded8063a4abe8afDouglas Gregor    return true;
38625a57efd7bf88a4a13018e0471ded8063a4abe8afDouglas Gregor  }
38635a57efd7bf88a4a13018e0471ded8063a4abe8afDouglas Gregor
38644e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie  if (getLangOpts().ObjC1) {
38655a57efd7bf88a4a13018e0471ded8063a4abe8afDouglas Gregor    const ObjCObjectPointerType *T1OPType = T1->getAs<ObjCObjectPointerType>(),
38665a57efd7bf88a4a13018e0471ded8063a4abe8afDouglas Gregor                                *T2OPType = T2->getAs<ObjCObjectPointerType>();
38675a57efd7bf88a4a13018e0471ded8063a4abe8afDouglas Gregor    if (T1OPType && T2OPType) {
38685a57efd7bf88a4a13018e0471ded8063a4abe8afDouglas Gregor      T1 = T1OPType->getPointeeType();
38695a57efd7bf88a4a13018e0471ded8063a4abe8afDouglas Gregor      T2 = T2OPType->getPointeeType();
38705a57efd7bf88a4a13018e0471ded8063a4abe8afDouglas Gregor      return true;
38715a57efd7bf88a4a13018e0471ded8063a4abe8afDouglas Gregor    }
38725a57efd7bf88a4a13018e0471ded8063a4abe8afDouglas Gregor  }
38735a57efd7bf88a4a13018e0471ded8063a4abe8afDouglas Gregor
38745a57efd7bf88a4a13018e0471ded8063a4abe8afDouglas Gregor  // FIXME: Block pointers, too?
38755a57efd7bf88a4a13018e0471ded8063a4abe8afDouglas Gregor
38765a57efd7bf88a4a13018e0471ded8063a4abe8afDouglas Gregor  return false;
38775a57efd7bf88a4a13018e0471ded8063a4abe8afDouglas Gregor}
38785a57efd7bf88a4a13018e0471ded8063a4abe8afDouglas Gregor
38794ba2a17694148e16eaa8d3917f657ffcd3667be4Jay FoadDeclarationNameInfo
38804ba2a17694148e16eaa8d3917f657ffcd3667be4Jay FoadASTContext::getNameForTemplate(TemplateName Name,
38814ba2a17694148e16eaa8d3917f657ffcd3667be4Jay Foad                               SourceLocation NameLoc) const {
3882146060435c3efce95c95a092c7a1eb651cfb9ae0John McCall  switch (Name.getKind()) {
3883146060435c3efce95c95a092c7a1eb651cfb9ae0John McCall  case TemplateName::QualifiedTemplate:
3884146060435c3efce95c95a092c7a1eb651cfb9ae0John McCall  case TemplateName::Template:
38852577743c5650c646fb705df01403707e94f2df04Abramo Bagnara    // DNInfo work in progress: CHECKME: what about DNLoc?
3886146060435c3efce95c95a092c7a1eb651cfb9ae0John McCall    return DeclarationNameInfo(Name.getAsTemplateDecl()->getDeclName(),
3887146060435c3efce95c95a092c7a1eb651cfb9ae0John McCall                               NameLoc);
38882577743c5650c646fb705df01403707e94f2df04Abramo Bagnara
3889146060435c3efce95c95a092c7a1eb651cfb9ae0John McCall  case TemplateName::OverloadedTemplate: {
3890146060435c3efce95c95a092c7a1eb651cfb9ae0John McCall    OverloadedTemplateStorage *Storage = Name.getAsOverloadedTemplate();
3891146060435c3efce95c95a092c7a1eb651cfb9ae0John McCall    // DNInfo work in progress: CHECKME: what about DNLoc?
3892146060435c3efce95c95a092c7a1eb651cfb9ae0John McCall    return DeclarationNameInfo((*Storage->begin())->getDeclName(), NameLoc);
3893146060435c3efce95c95a092c7a1eb651cfb9ae0John McCall  }
3894146060435c3efce95c95a092c7a1eb651cfb9ae0John McCall
3895146060435c3efce95c95a092c7a1eb651cfb9ae0John McCall  case TemplateName::DependentTemplate: {
3896146060435c3efce95c95a092c7a1eb651cfb9ae0John McCall    DependentTemplateName *DTN = Name.getAsDependentTemplateName();
38972577743c5650c646fb705df01403707e94f2df04Abramo Bagnara    DeclarationName DName;
389880ad16f4b2b350ddbaae21a52975e63df5aafc2cJohn McCall    if (DTN->isIdentifier()) {
38992577743c5650c646fb705df01403707e94f2df04Abramo Bagnara      DName = DeclarationNames.getIdentifier(DTN->getIdentifier());
39002577743c5650c646fb705df01403707e94f2df04Abramo Bagnara      return DeclarationNameInfo(DName, NameLoc);
390180ad16f4b2b350ddbaae21a52975e63df5aafc2cJohn McCall    } else {
39022577743c5650c646fb705df01403707e94f2df04Abramo Bagnara      DName = DeclarationNames.getCXXOperatorName(DTN->getOperator());
39032577743c5650c646fb705df01403707e94f2df04Abramo Bagnara      // DNInfo work in progress: FIXME: source locations?
39042577743c5650c646fb705df01403707e94f2df04Abramo Bagnara      DeclarationNameLoc DNLoc;
39052577743c5650c646fb705df01403707e94f2df04Abramo Bagnara      DNLoc.CXXOperatorName.BeginOpNameLoc = SourceLocation().getRawEncoding();
39062577743c5650c646fb705df01403707e94f2df04Abramo Bagnara      DNLoc.CXXOperatorName.EndOpNameLoc = SourceLocation().getRawEncoding();
39072577743c5650c646fb705df01403707e94f2df04Abramo Bagnara      return DeclarationNameInfo(DName, NameLoc, DNLoc);
390880ad16f4b2b350ddbaae21a52975e63df5aafc2cJohn McCall    }
390980ad16f4b2b350ddbaae21a52975e63df5aafc2cJohn McCall  }
391080ad16f4b2b350ddbaae21a52975e63df5aafc2cJohn McCall
3911146060435c3efce95c95a092c7a1eb651cfb9ae0John McCall  case TemplateName::SubstTemplateTemplateParm: {
3912146060435c3efce95c95a092c7a1eb651cfb9ae0John McCall    SubstTemplateTemplateParmStorage *subst
3913146060435c3efce95c95a092c7a1eb651cfb9ae0John McCall      = Name.getAsSubstTemplateTemplateParm();
3914146060435c3efce95c95a092c7a1eb651cfb9ae0John McCall    return DeclarationNameInfo(subst->getParameter()->getDeclName(),
3915146060435c3efce95c95a092c7a1eb651cfb9ae0John McCall                               NameLoc);
3916146060435c3efce95c95a092c7a1eb651cfb9ae0John McCall  }
3917146060435c3efce95c95a092c7a1eb651cfb9ae0John McCall
3918146060435c3efce95c95a092c7a1eb651cfb9ae0John McCall  case TemplateName::SubstTemplateTemplateParmPack: {
3919146060435c3efce95c95a092c7a1eb651cfb9ae0John McCall    SubstTemplateTemplateParmPackStorage *subst
3920146060435c3efce95c95a092c7a1eb651cfb9ae0John McCall      = Name.getAsSubstTemplateTemplateParmPack();
3921146060435c3efce95c95a092c7a1eb651cfb9ae0John McCall    return DeclarationNameInfo(subst->getParameterPack()->getDeclName(),
3922146060435c3efce95c95a092c7a1eb651cfb9ae0John McCall                               NameLoc);
3923146060435c3efce95c95a092c7a1eb651cfb9ae0John McCall  }
3924146060435c3efce95c95a092c7a1eb651cfb9ae0John McCall  }
3925146060435c3efce95c95a092c7a1eb651cfb9ae0John McCall
3926146060435c3efce95c95a092c7a1eb651cfb9ae0John McCall  llvm_unreachable("bad template name kind!");
392780ad16f4b2b350ddbaae21a52975e63df5aafc2cJohn McCall}
392880ad16f4b2b350ddbaae21a52975e63df5aafc2cJohn McCall
39294ba2a17694148e16eaa8d3917f657ffcd3667be4Jay FoadTemplateName ASTContext::getCanonicalTemplateName(TemplateName Name) const {
3930146060435c3efce95c95a092c7a1eb651cfb9ae0John McCall  switch (Name.getKind()) {
3931146060435c3efce95c95a092c7a1eb651cfb9ae0John McCall  case TemplateName::QualifiedTemplate:
3932146060435c3efce95c95a092c7a1eb651cfb9ae0John McCall  case TemplateName::Template: {
3933146060435c3efce95c95a092c7a1eb651cfb9ae0John McCall    TemplateDecl *Template = Name.getAsTemplateDecl();
39343e1274f2b99cb99c03cc8e2c6517c37d330b597aDouglas Gregor    if (TemplateTemplateParmDecl *TTP
3935146060435c3efce95c95a092c7a1eb651cfb9ae0John McCall          = dyn_cast<TemplateTemplateParmDecl>(Template))
39363e1274f2b99cb99c03cc8e2c6517c37d330b597aDouglas Gregor      Template = getCanonicalTemplateTemplateParmDecl(TTP);
39373e1274f2b99cb99c03cc8e2c6517c37d330b597aDouglas Gregor
39383e1274f2b99cb99c03cc8e2c6517c37d330b597aDouglas Gregor    // The canonical template name is the canonical template declaration.
393997fbaa2a38804268a024f1a104b43fcf8b4411b0Argyrios Kyrtzidis    return TemplateName(cast<TemplateDecl>(Template->getCanonicalDecl()));
39403e1274f2b99cb99c03cc8e2c6517c37d330b597aDouglas Gregor  }
394125a3ef7cc5fd55dc8cc67c6e6770c8595657e082Douglas Gregor
3942146060435c3efce95c95a092c7a1eb651cfb9ae0John McCall  case TemplateName::OverloadedTemplate:
3943146060435c3efce95c95a092c7a1eb651cfb9ae0John McCall    llvm_unreachable("cannot canonicalize overloaded template");
3944146060435c3efce95c95a092c7a1eb651cfb9ae0John McCall
3945146060435c3efce95c95a092c7a1eb651cfb9ae0John McCall  case TemplateName::DependentTemplate: {
3946146060435c3efce95c95a092c7a1eb651cfb9ae0John McCall    DependentTemplateName *DTN = Name.getAsDependentTemplateName();
3947146060435c3efce95c95a092c7a1eb651cfb9ae0John McCall    assert(DTN && "Non-dependent template names must refer to template decls.");
3948146060435c3efce95c95a092c7a1eb651cfb9ae0John McCall    return DTN->CanonicalTemplateName;
3949146060435c3efce95c95a092c7a1eb651cfb9ae0John McCall  }
3950146060435c3efce95c95a092c7a1eb651cfb9ae0John McCall
3951146060435c3efce95c95a092c7a1eb651cfb9ae0John McCall  case TemplateName::SubstTemplateTemplateParm: {
3952146060435c3efce95c95a092c7a1eb651cfb9ae0John McCall    SubstTemplateTemplateParmStorage *subst
3953146060435c3efce95c95a092c7a1eb651cfb9ae0John McCall      = Name.getAsSubstTemplateTemplateParm();
3954146060435c3efce95c95a092c7a1eb651cfb9ae0John McCall    return getCanonicalTemplateName(subst->getReplacement());
39551aee05d08b2184acadeb36de300e216390780d6cDouglas Gregor  }
39561eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
3957146060435c3efce95c95a092c7a1eb651cfb9ae0John McCall  case TemplateName::SubstTemplateTemplateParmPack: {
3958146060435c3efce95c95a092c7a1eb651cfb9ae0John McCall    SubstTemplateTemplateParmPackStorage *subst
3959146060435c3efce95c95a092c7a1eb651cfb9ae0John McCall                                  = Name.getAsSubstTemplateTemplateParmPack();
3960146060435c3efce95c95a092c7a1eb651cfb9ae0John McCall    TemplateTemplateParmDecl *canonParameter
3961146060435c3efce95c95a092c7a1eb651cfb9ae0John McCall      = getCanonicalTemplateTemplateParmDecl(subst->getParameterPack());
3962146060435c3efce95c95a092c7a1eb651cfb9ae0John McCall    TemplateArgument canonArgPack
3963146060435c3efce95c95a092c7a1eb651cfb9ae0John McCall      = getCanonicalTemplateArgument(subst->getArgumentPack());
3964146060435c3efce95c95a092c7a1eb651cfb9ae0John McCall    return getSubstTemplateTemplateParmPack(canonParameter, canonArgPack);
3965146060435c3efce95c95a092c7a1eb651cfb9ae0John McCall  }
3966146060435c3efce95c95a092c7a1eb651cfb9ae0John McCall  }
3967146060435c3efce95c95a092c7a1eb651cfb9ae0John McCall
3968146060435c3efce95c95a092c7a1eb651cfb9ae0John McCall  llvm_unreachable("bad template name!");
396925a3ef7cc5fd55dc8cc67c6e6770c8595657e082Douglas Gregor}
397025a3ef7cc5fd55dc8cc67c6e6770c8595657e082Douglas Gregor
3971db0d4b751e83b8841b8f48f913f17e50467f13d4Douglas Gregorbool ASTContext::hasSameTemplateName(TemplateName X, TemplateName Y) {
3972db0d4b751e83b8841b8f48f913f17e50467f13d4Douglas Gregor  X = getCanonicalTemplateName(X);
3973db0d4b751e83b8841b8f48f913f17e50467f13d4Douglas Gregor  Y = getCanonicalTemplateName(Y);
3974db0d4b751e83b8841b8f48f913f17e50467f13d4Douglas Gregor  return X.getAsVoidPointer() == Y.getAsVoidPointer();
3975db0d4b751e83b8841b8f48f913f17e50467f13d4Douglas Gregor}
3976db0d4b751e83b8841b8f48f913f17e50467f13d4Douglas Gregor
39771eb4433ac451dc16f4133a88af2d002ac26c58efMike StumpTemplateArgument
39784ba2a17694148e16eaa8d3917f657ffcd3667be4Jay FoadASTContext::getCanonicalTemplateArgument(const TemplateArgument &Arg) const {
39791275ae098acda31fe0e434510c729fcfed0458a1Douglas Gregor  switch (Arg.getKind()) {
39801275ae098acda31fe0e434510c729fcfed0458a1Douglas Gregor    case TemplateArgument::Null:
39811275ae098acda31fe0e434510c729fcfed0458a1Douglas Gregor      return Arg;
39821eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
39831275ae098acda31fe0e434510c729fcfed0458a1Douglas Gregor    case TemplateArgument::Expression:
39841275ae098acda31fe0e434510c729fcfed0458a1Douglas Gregor      return Arg;
39851eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
3986d2008e2c80d6c9282044ec873a937a17a0f33579Douglas Gregor    case TemplateArgument::Declaration: {
3987d7a6b1640e565487d163023a6a2e83f55476ae96Eli Friedman      ValueDecl *D = cast<ValueDecl>(Arg.getAsDecl()->getCanonicalDecl());
3988d7a6b1640e565487d163023a6a2e83f55476ae96Eli Friedman      return TemplateArgument(D, Arg.isDeclForReferenceParam());
3989d2008e2c80d6c9282044ec873a937a17a0f33579Douglas Gregor    }
39901eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
3991d7a6b1640e565487d163023a6a2e83f55476ae96Eli Friedman    case TemplateArgument::NullPtr:
3992d7a6b1640e565487d163023a6a2e83f55476ae96Eli Friedman      return TemplateArgument(getCanonicalType(Arg.getNullPtrType()),
3993d7a6b1640e565487d163023a6a2e83f55476ae96Eli Friedman                              /*isNullPtr*/true);
3994d7a6b1640e565487d163023a6a2e83f55476ae96Eli Friedman
3995788cd06cf8e868a67158aafec5de3a1f408d14f3Douglas Gregor    case TemplateArgument::Template:
3996788cd06cf8e868a67158aafec5de3a1f408d14f3Douglas Gregor      return TemplateArgument(getCanonicalTemplateName(Arg.getAsTemplate()));
3997a7fc901a2e39bfe55bfcff5934b2d9fdf9656491Douglas Gregor
3998a7fc901a2e39bfe55bfcff5934b2d9fdf9656491Douglas Gregor    case TemplateArgument::TemplateExpansion:
3999a7fc901a2e39bfe55bfcff5934b2d9fdf9656491Douglas Gregor      return TemplateArgument(getCanonicalTemplateName(
4000a7fc901a2e39bfe55bfcff5934b2d9fdf9656491Douglas Gregor                                         Arg.getAsTemplateOrTemplatePattern()),
40012be29f423acad3bbe39099a78db2805acb5bdf17Douglas Gregor                              Arg.getNumTemplateExpansions());
4002a7fc901a2e39bfe55bfcff5934b2d9fdf9656491Douglas Gregor
40031275ae098acda31fe0e434510c729fcfed0458a1Douglas Gregor    case TemplateArgument::Integral:
4004855243789cb44799c03f4c7216d3d6308805f549Benjamin Kramer      return TemplateArgument(Arg, getCanonicalType(Arg.getIntegralType()));
40051eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
40061275ae098acda31fe0e434510c729fcfed0458a1Douglas Gregor    case TemplateArgument::Type:
4007833ca991c1bfc967f0995974ca86f66ba1f666b5John McCall      return TemplateArgument(getCanonicalType(Arg.getAsType()));
40081eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
40091275ae098acda31fe0e434510c729fcfed0458a1Douglas Gregor    case TemplateArgument::Pack: {
401087dd697dcc8ecb64df73ae64d61b8c80ff0c157cDouglas Gregor      if (Arg.pack_size() == 0)
401187dd697dcc8ecb64df73ae64d61b8c80ff0c157cDouglas Gregor        return Arg;
401287dd697dcc8ecb64df73ae64d61b8c80ff0c157cDouglas Gregor
4013910f8008fea79120489a53593fe971b0b8a4a740Douglas Gregor      TemplateArgument *CanonArgs
4014910f8008fea79120489a53593fe971b0b8a4a740Douglas Gregor        = new (*this) TemplateArgument[Arg.pack_size()];
40151275ae098acda31fe0e434510c729fcfed0458a1Douglas Gregor      unsigned Idx = 0;
40161eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      for (TemplateArgument::pack_iterator A = Arg.pack_begin(),
40171275ae098acda31fe0e434510c729fcfed0458a1Douglas Gregor                                        AEnd = Arg.pack_end();
40181275ae098acda31fe0e434510c729fcfed0458a1Douglas Gregor           A != AEnd; (void)++A, ++Idx)
40191275ae098acda31fe0e434510c729fcfed0458a1Douglas Gregor        CanonArgs[Idx] = getCanonicalTemplateArgument(*A);
40201eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
4021910f8008fea79120489a53593fe971b0b8a4a740Douglas Gregor      return TemplateArgument(CanonArgs, Arg.pack_size());
40221275ae098acda31fe0e434510c729fcfed0458a1Douglas Gregor    }
40231275ae098acda31fe0e434510c729fcfed0458a1Douglas Gregor  }
40241275ae098acda31fe0e434510c729fcfed0458a1Douglas Gregor
40251275ae098acda31fe0e434510c729fcfed0458a1Douglas Gregor  // Silence GCC warning
4026b219cfc4d75f0a03630b7c4509ef791b7e97b2c8David Blaikie  llvm_unreachable("Unhandled template argument kind");
40271275ae098acda31fe0e434510c729fcfed0458a1Douglas Gregor}
40281275ae098acda31fe0e434510c729fcfed0458a1Douglas Gregor
4029d57959af02b4af695276f4204443afe6e5d86bd8Douglas GregorNestedNameSpecifier *
40304ba2a17694148e16eaa8d3917f657ffcd3667be4Jay FoadASTContext::getCanonicalNestedNameSpecifier(NestedNameSpecifier *NNS) const {
40311eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  if (!NNS)
4032d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor    return 0;
4033d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor
4034d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor  switch (NNS->getKind()) {
4035d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor  case NestedNameSpecifier::Identifier:
4036d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor    // Canonicalize the prefix but keep the identifier the same.
40371eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    return NestedNameSpecifier::Create(*this,
4038d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor                         getCanonicalNestedNameSpecifier(NNS->getPrefix()),
4039d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor                                       NNS->getAsIdentifier());
4040d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor
4041d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor  case NestedNameSpecifier::Namespace:
4042d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor    // A namespace is canonical; build a nested-name-specifier with
4043d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor    // this namespace and no prefix.
404414aba76042e041b2c5e439bf4ae353a0a3c7fd73Douglas Gregor    return NestedNameSpecifier::Create(*this, 0,
404514aba76042e041b2c5e439bf4ae353a0a3c7fd73Douglas Gregor                                 NNS->getAsNamespace()->getOriginalNamespace());
404614aba76042e041b2c5e439bf4ae353a0a3c7fd73Douglas Gregor
404714aba76042e041b2c5e439bf4ae353a0a3c7fd73Douglas Gregor  case NestedNameSpecifier::NamespaceAlias:
404814aba76042e041b2c5e439bf4ae353a0a3c7fd73Douglas Gregor    // A namespace is canonical; build a nested-name-specifier with
404914aba76042e041b2c5e439bf4ae353a0a3c7fd73Douglas Gregor    // this namespace and no prefix.
405014aba76042e041b2c5e439bf4ae353a0a3c7fd73Douglas Gregor    return NestedNameSpecifier::Create(*this, 0,
405114aba76042e041b2c5e439bf4ae353a0a3c7fd73Douglas Gregor                                    NNS->getAsNamespaceAlias()->getNamespace()
405214aba76042e041b2c5e439bf4ae353a0a3c7fd73Douglas Gregor                                                      ->getOriginalNamespace());
4053d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor
4054d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor  case NestedNameSpecifier::TypeSpec:
4055d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor  case NestedNameSpecifier::TypeSpecWithTemplate: {
4056d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor    QualType T = getCanonicalType(QualType(NNS->getAsType(), 0));
4057264bf66d55563dd86a3d7e06738aa427de512d2cDouglas Gregor
4058264bf66d55563dd86a3d7e06738aa427de512d2cDouglas Gregor    // If we have some kind of dependent-named type (e.g., "typename T::type"),
4059264bf66d55563dd86a3d7e06738aa427de512d2cDouglas Gregor    // break it apart into its prefix and identifier, then reconsititute those
4060264bf66d55563dd86a3d7e06738aa427de512d2cDouglas Gregor    // as the canonical nested-name-specifier. This is required to canonicalize
4061264bf66d55563dd86a3d7e06738aa427de512d2cDouglas Gregor    // a dependent nested-name-specifier involving typedefs of dependent-name
4062264bf66d55563dd86a3d7e06738aa427de512d2cDouglas Gregor    // types, e.g.,
4063264bf66d55563dd86a3d7e06738aa427de512d2cDouglas Gregor    //   typedef typename T::type T1;
4064264bf66d55563dd86a3d7e06738aa427de512d2cDouglas Gregor    //   typedef typename T1::type T2;
406516412ef25a2203b7066d0db2b41f944631e5cf79Eli Friedman    if (const DependentNameType *DNT = T->getAs<DependentNameType>())
406616412ef25a2203b7066d0db2b41f944631e5cf79Eli Friedman      return NestedNameSpecifier::Create(*this, DNT->getQualifier(),
4067264bf66d55563dd86a3d7e06738aa427de512d2cDouglas Gregor                           const_cast<IdentifierInfo *>(DNT->getIdentifier()));
4068264bf66d55563dd86a3d7e06738aa427de512d2cDouglas Gregor
406916412ef25a2203b7066d0db2b41f944631e5cf79Eli Friedman    // Otherwise, just canonicalize the type, and force it to be a TypeSpec.
407016412ef25a2203b7066d0db2b41f944631e5cf79Eli Friedman    // FIXME: Why are TypeSpec and TypeSpecWithTemplate distinct in the
407116412ef25a2203b7066d0db2b41f944631e5cf79Eli Friedman    // first place?
40723b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall    return NestedNameSpecifier::Create(*this, 0, false,
40733b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall                                       const_cast<Type*>(T.getTypePtr()));
4074d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor  }
4075d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor
4076d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor  case NestedNameSpecifier::Global:
4077d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor    // The global specifier is canonical and unique.
4078d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor    return NNS;
4079d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor  }
4080d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor
40817530c034c0c71a64c5a9173206d9742ae847af8bDavid Blaikie  llvm_unreachable("Invalid NestedNameSpecifier::Kind!");
4082d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor}
4083d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor
4084c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner
40854ba2a17694148e16eaa8d3917f657ffcd3667be4Jay Foadconst ArrayType *ASTContext::getAsArrayType(QualType T) const {
4086c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner  // Handle the non-qualified case efficiently.
4087a4923eb7c4b04d360cb2747641a5e92818edf804Douglas Gregor  if (!T.hasLocalQualifiers()) {
4088c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner    // Handle the common positive case fast.
4089c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner    if (const ArrayType *AT = dyn_cast<ArrayType>(T))
4090c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner      return AT;
4091c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner  }
40921eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
40930953e767ff7817f97b3ab20896b229891eeff45bJohn McCall  // Handle the common negative case fast.
40943b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall  if (!isa<ArrayType>(T.getCanonicalType()))
4095c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner    return 0;
40961eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
40970953e767ff7817f97b3ab20896b229891eeff45bJohn McCall  // Apply any qualifiers from the array type to the element type.  This
4098c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner  // implements C99 6.7.3p8: "If the specification of an array type includes
4099c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner  // any type qualifiers, the element type is so qualified, not the array type."
41001eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
4101c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner  // If we get here, we either have type qualifiers on the type, or we have
4102c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner  // sugar such as a typedef in the way.  If we have type qualifiers on the type
410350d62d1b4a98adbc83de8f8cd1379ea1c25656f7Douglas Gregor  // we must propagate them down into the element type.
41040953e767ff7817f97b3ab20896b229891eeff45bJohn McCall
41053b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall  SplitQualType split = T.getSplitDesugaredType();
4106200fa53fd420aa8369586f569dbece04930ad6a3John McCall  Qualifiers qs = split.Quals;
41071eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
4108c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner  // If we have a simple case, just return now.
4109200fa53fd420aa8369586f569dbece04930ad6a3John McCall  const ArrayType *ATy = dyn_cast<ArrayType>(split.Ty);
41103b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall  if (ATy == 0 || qs.empty())
4111c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner    return ATy;
41121eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
4113c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner  // Otherwise, we have an array and we have qualifiers on it.  Push the
4114c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner  // qualifiers into the array element type and return a new array type.
41153b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall  QualType NewEltTy = getQualifiedType(ATy->getElementType(), qs);
41161eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
4117c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner  if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(ATy))
4118c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner    return cast<ArrayType>(getConstantArrayType(NewEltTy, CAT->getSize(),
4119c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner                                                CAT->getSizeModifier(),
41200953e767ff7817f97b3ab20896b229891eeff45bJohn McCall                                           CAT->getIndexTypeCVRQualifiers()));
4121c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner  if (const IncompleteArrayType *IAT = dyn_cast<IncompleteArrayType>(ATy))
4122c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner    return cast<ArrayType>(getIncompleteArrayType(NewEltTy,
4123c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner                                                  IAT->getSizeModifier(),
41240953e767ff7817f97b3ab20896b229891eeff45bJohn McCall                                           IAT->getIndexTypeCVRQualifiers()));
4125898574e7496ba8fd76290079d3a9d06954992734Douglas Gregor
41261eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  if (const DependentSizedArrayType *DSAT
4127898574e7496ba8fd76290079d3a9d06954992734Douglas Gregor        = dyn_cast<DependentSizedArrayType>(ATy))
4128898574e7496ba8fd76290079d3a9d06954992734Douglas Gregor    return cast<ArrayType>(
41291eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump                     getDependentSizedArrayType(NewEltTy,
41303fa5cae9b3812cab9fab6c042c3329bb70a3d046John McCall                                                DSAT->getSizeExpr(),
4131898574e7496ba8fd76290079d3a9d06954992734Douglas Gregor                                                DSAT->getSizeModifier(),
41320953e767ff7817f97b3ab20896b229891eeff45bJohn McCall                                              DSAT->getIndexTypeCVRQualifiers(),
41337e7eb3da052a6d80ddf2377cab0384c798f73f75Douglas Gregor                                                DSAT->getBracketsRange()));
41341eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
4135c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner  const VariableArrayType *VAT = cast<VariableArrayType>(ATy);
41367e7eb3da052a6d80ddf2377cab0384c798f73f75Douglas Gregor  return cast<ArrayType>(getVariableArrayType(NewEltTy,
41373fa5cae9b3812cab9fab6c042c3329bb70a3d046John McCall                                              VAT->getSizeExpr(),
4138c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner                                              VAT->getSizeModifier(),
41390953e767ff7817f97b3ab20896b229891eeff45bJohn McCall                                              VAT->getIndexTypeCVRQualifiers(),
41407e7eb3da052a6d80ddf2377cab0384c798f73f75Douglas Gregor                                              VAT->getBracketsRange()));
414177c9647cae939104c6cb2b6a4dd8ca859d2e5770Chris Lattner}
414277c9647cae939104c6cb2b6a4dd8ca859d2e5770Chris Lattner
4143ad9689f3531c49e4bff467d9469993606800068cAbramo BagnaraQualType ASTContext::getAdjustedParameterType(QualType T) const {
414479e6bd379773447a74cc3e579d9081e4c5cb6d63Douglas Gregor  // C99 6.7.5.3p7:
414579e6bd379773447a74cc3e579d9081e4c5cb6d63Douglas Gregor  //   A declaration of a parameter as "array of type" shall be
414679e6bd379773447a74cc3e579d9081e4c5cb6d63Douglas Gregor  //   adjusted to "qualified pointer to type", where the type
414779e6bd379773447a74cc3e579d9081e4c5cb6d63Douglas Gregor  //   qualifiers (if any) are those specified within the [ and ] of
414879e6bd379773447a74cc3e579d9081e4c5cb6d63Douglas Gregor  //   the array type derivation.
414979e6bd379773447a74cc3e579d9081e4c5cb6d63Douglas Gregor  if (T->isArrayType())
415079e6bd379773447a74cc3e579d9081e4c5cb6d63Douglas Gregor    return getArrayDecayedType(T);
415179e6bd379773447a74cc3e579d9081e4c5cb6d63Douglas Gregor
415279e6bd379773447a74cc3e579d9081e4c5cb6d63Douglas Gregor  // C99 6.7.5.3p8:
415379e6bd379773447a74cc3e579d9081e4c5cb6d63Douglas Gregor  //   A declaration of a parameter as "function returning type"
415479e6bd379773447a74cc3e579d9081e4c5cb6d63Douglas Gregor  //   shall be adjusted to "pointer to function returning type", as
415579e6bd379773447a74cc3e579d9081e4c5cb6d63Douglas Gregor  //   in 6.3.2.1.
415679e6bd379773447a74cc3e579d9081e4c5cb6d63Douglas Gregor  if (T->isFunctionType())
415779e6bd379773447a74cc3e579d9081e4c5cb6d63Douglas Gregor    return getPointerType(T);
415879e6bd379773447a74cc3e579d9081e4c5cb6d63Douglas Gregor
415979e6bd379773447a74cc3e579d9081e4c5cb6d63Douglas Gregor  return T;
416079e6bd379773447a74cc3e579d9081e4c5cb6d63Douglas Gregor}
416179e6bd379773447a74cc3e579d9081e4c5cb6d63Douglas Gregor
4162ad9689f3531c49e4bff467d9469993606800068cAbramo BagnaraQualType ASTContext::getSignatureParameterType(QualType T) const {
416379e6bd379773447a74cc3e579d9081e4c5cb6d63Douglas Gregor  T = getVariableArrayDecayedType(T);
416479e6bd379773447a74cc3e579d9081e4c5cb6d63Douglas Gregor  T = getAdjustedParameterType(T);
416579e6bd379773447a74cc3e579d9081e4c5cb6d63Douglas Gregor  return T.getUnqualifiedType();
416679e6bd379773447a74cc3e579d9081e4c5cb6d63Douglas Gregor}
416779e6bd379773447a74cc3e579d9081e4c5cb6d63Douglas Gregor
4168e6327747b72bb687c948270f702ff53c30f411a6Chris Lattner/// getArrayDecayedType - Return the properly qualified result of decaying the
4169e6327747b72bb687c948270f702ff53c30f411a6Chris Lattner/// specified array type to a pointer.  This operation is non-trivial when
4170e6327747b72bb687c948270f702ff53c30f411a6Chris Lattner/// handling typedefs etc.  The canonical type of "T" must be an array type,
4171e6327747b72bb687c948270f702ff53c30f411a6Chris Lattner/// this returns a pointer to a properly qualified element of the array.
4172e6327747b72bb687c948270f702ff53c30f411a6Chris Lattner///
4173e6327747b72bb687c948270f702ff53c30f411a6Chris Lattner/// See C99 6.7.5.3p7 and C99 6.3.2.1p3.
41744ba2a17694148e16eaa8d3917f657ffcd3667be4Jay FoadQualType ASTContext::getArrayDecayedType(QualType Ty) const {
4175c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner  // Get the element type with 'getAsArrayType' so that we don't lose any
4176c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner  // typedefs in the element type of the array.  This also handles propagation
4177c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner  // of type qualifiers from the array type into the element type if present
4178c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner  // (C99 6.7.3p8).
4179c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner  const ArrayType *PrettyArrayType = getAsArrayType(Ty);
4180c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner  assert(PrettyArrayType && "Not an array type!");
41811eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
4182c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner  QualType PtrTy = getPointerType(PrettyArrayType->getElementType());
4183e6327747b72bb687c948270f702ff53c30f411a6Chris Lattner
4184e6327747b72bb687c948270f702ff53c30f411a6Chris Lattner  // int x[restrict 4] ->  int *restrict
41850953e767ff7817f97b3ab20896b229891eeff45bJohn McCall  return getQualifiedType(PtrTy, PrettyArrayType->getIndexTypeQualifiers());
4186e6327747b72bb687c948270f702ff53c30f411a6Chris Lattner}
4187e6327747b72bb687c948270f702ff53c30f411a6Chris Lattner
41883b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCallQualType ASTContext::getBaseElementType(const ArrayType *array) const {
41893b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall  return getBaseElementType(array->getElementType());
41905e03f9ea8174ae588c5e69ec6b5ef4c68f8fd766Douglas Gregor}
41915e03f9ea8174ae588c5e69ec6b5ef4c68f8fd766Douglas Gregor
41923b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCallQualType ASTContext::getBaseElementType(QualType type) const {
41933b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall  Qualifiers qs;
41943b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall  while (true) {
41953b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall    SplitQualType split = type.getSplitDesugaredType();
4196200fa53fd420aa8369586f569dbece04930ad6a3John McCall    const ArrayType *array = split.Ty->getAsArrayTypeUnsafe();
41973b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall    if (!array) break;
41981eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
41993b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall    type = array->getElementType();
4200200fa53fd420aa8369586f569dbece04930ad6a3John McCall    qs.addConsistentQualifiers(split.Quals);
42013b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall  }
42021eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
42033b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall  return getQualifiedType(type, qs);
42046183a99b064b397d98297904fbd6cf00fe1f453dAnders Carlsson}
42056183a99b064b397d98297904fbd6cf00fe1f453dAnders Carlsson
42060de78998e7bda473b408437053e48661b510d453Fariborz Jahanian/// getConstantArrayElementCount - Returns number of constant array elements.
42071eb4433ac451dc16f4133a88af2d002ac26c58efMike Stumpuint64_t
42080de78998e7bda473b408437053e48661b510d453Fariborz JahanianASTContext::getConstantArrayElementCount(const ConstantArrayType *CA)  const {
42090de78998e7bda473b408437053e48661b510d453Fariborz Jahanian  uint64_t ElementCount = 1;
42100de78998e7bda473b408437053e48661b510d453Fariborz Jahanian  do {
42110de78998e7bda473b408437053e48661b510d453Fariborz Jahanian    ElementCount *= CA->getSize().getZExtValue();
4212d5e839429771ad4d1a8b3db598cbbc6d93621f75Richard Smith    CA = dyn_cast_or_null<ConstantArrayType>(
4213d5e839429771ad4d1a8b3db598cbbc6d93621f75Richard Smith      CA->getElementType()->getAsArrayTypeUnsafe());
42140de78998e7bda473b408437053e48661b510d453Fariborz Jahanian  } while (CA);
42150de78998e7bda473b408437053e48661b510d453Fariborz Jahanian  return ElementCount;
42160de78998e7bda473b408437053e48661b510d453Fariborz Jahanian}
42170de78998e7bda473b408437053e48661b510d453Fariborz Jahanian
42185f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// getFloatingRank - Return a relative rank for floating point types.
42195f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// This routine will assert if passed a built-in type that isn't a float.
4220a75cea3f6be0daa8054d36af81a6ffda1713f82dChris Lattnerstatic FloatingRank getFloatingRank(QualType T) {
4221183700f494ec9b6701b6efe82bcb25f4c79ba561John McCall  if (const ComplexType *CT = T->getAs<ComplexType>())
42225f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    return getFloatingRank(CT->getElementType());
4223a75cea3f6be0daa8054d36af81a6ffda1713f82dChris Lattner
4224183700f494ec9b6701b6efe82bcb25f4c79ba561John McCall  assert(T->getAs<BuiltinType>() && "getFloatingRank(): not a floating type");
4225183700f494ec9b6701b6efe82bcb25f4c79ba561John McCall  switch (T->getAs<BuiltinType>()->getKind()) {
4226b219cfc4d75f0a03630b7c4509ef791b7e97b2c8David Blaikie  default: llvm_unreachable("getFloatingRank(): not a floating type");
4227aa4a99b4a62615db243f7a5c433169f2fc704420Anton Korobeynikov  case BuiltinType::Half:       return HalfRank;
42285f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case BuiltinType::Float:      return FloatRank;
42295f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case BuiltinType::Double:     return DoubleRank;
42305f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case BuiltinType::LongDouble: return LongDoubleRank;
42315f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
42325f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
42335f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
42341eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// getFloatingTypeOfSizeWithinDomain - Returns a real floating
42351eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// point or a complex type (based on typeDomain/typeSize).
4236716c7304ff5d27a95e1e7823acd1d09d5ec3e37fSteve Naroff/// 'typeDomain' is a real floating point or complex type.
4237716c7304ff5d27a95e1e7823acd1d09d5ec3e37fSteve Naroff/// 'typeSize' is a real floating point or complex type.
42381361b11066239ea15764a2a844405352d87296b3Chris LattnerQualType ASTContext::getFloatingTypeOfSizeWithinDomain(QualType Size,
42391361b11066239ea15764a2a844405352d87296b3Chris Lattner                                                       QualType Domain) const {
42401361b11066239ea15764a2a844405352d87296b3Chris Lattner  FloatingRank EltRank = getFloatingRank(Size);
42411361b11066239ea15764a2a844405352d87296b3Chris Lattner  if (Domain->isComplexType()) {
42421361b11066239ea15764a2a844405352d87296b3Chris Lattner    switch (EltRank) {
4243561d3abc881033776ece385a01a510e1cbc1fa92David Blaikie    case HalfRank: llvm_unreachable("Complex half is not supported");
4244f1448a0e4a1e868ff873a8530a61a09cb68666ccSteve Naroff    case FloatRank:      return FloatComplexTy;
4245f1448a0e4a1e868ff873a8530a61a09cb68666ccSteve Naroff    case DoubleRank:     return DoubleComplexTy;
4246f1448a0e4a1e868ff873a8530a61a09cb68666ccSteve Naroff    case LongDoubleRank: return LongDoubleComplexTy;
4247f1448a0e4a1e868ff873a8530a61a09cb68666ccSteve Naroff    }
4248f1448a0e4a1e868ff873a8530a61a09cb68666ccSteve Naroff  }
42491361b11066239ea15764a2a844405352d87296b3Chris Lattner
42501361b11066239ea15764a2a844405352d87296b3Chris Lattner  assert(Domain->isRealFloatingType() && "Unknown domain!");
42511361b11066239ea15764a2a844405352d87296b3Chris Lattner  switch (EltRank) {
425219dbb20ac4371fae3190379a7e7bd467af3c00aaJoey Gouly  case HalfRank:       return HalfTy;
42531361b11066239ea15764a2a844405352d87296b3Chris Lattner  case FloatRank:      return FloatTy;
42541361b11066239ea15764a2a844405352d87296b3Chris Lattner  case DoubleRank:     return DoubleTy;
42551361b11066239ea15764a2a844405352d87296b3Chris Lattner  case LongDoubleRank: return LongDoubleTy;
42565f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
4257561d3abc881033776ece385a01a510e1cbc1fa92David Blaikie  llvm_unreachable("getFloatingRank(): illegal value for rank");
42585f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
42595f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
42607cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner/// getFloatingTypeOrder - Compare the rank of the two specified floating
42617cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner/// point types, ignoring the domain of the type (i.e. 'double' ==
42627cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner/// '_Complex double').  If LHS > RHS, return 1.  If LHS == RHS, return 0. If
42631eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// LHS < RHS, return -1.
42644ba2a17694148e16eaa8d3917f657ffcd3667be4Jay Foadint ASTContext::getFloatingTypeOrder(QualType LHS, QualType RHS) const {
4265a75cea3f6be0daa8054d36af81a6ffda1713f82dChris Lattner  FloatingRank LHSR = getFloatingRank(LHS);
4266a75cea3f6be0daa8054d36af81a6ffda1713f82dChris Lattner  FloatingRank RHSR = getFloatingRank(RHS);
42671eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
4268a75cea3f6be0daa8054d36af81a6ffda1713f82dChris Lattner  if (LHSR == RHSR)
4269fb0d49669aa370b4c0993c5cee60275ef9fd6518Steve Naroff    return 0;
4270a75cea3f6be0daa8054d36af81a6ffda1713f82dChris Lattner  if (LHSR > RHSR)
4271fb0d49669aa370b4c0993c5cee60275ef9fd6518Steve Naroff    return 1;
4272fb0d49669aa370b4c0993c5cee60275ef9fd6518Steve Naroff  return -1;
42735f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
42745f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
4275f52ab250ff92bc51a9ac9a8e19bd43b63a5f844fChris Lattner/// getIntegerRank - Return an integer conversion rank (C99 6.3.1.1p1). This
4276f52ab250ff92bc51a9ac9a8e19bd43b63a5f844fChris Lattner/// routine will assert if passed a built-in type that isn't an integer or enum,
4277f52ab250ff92bc51a9ac9a8e19bd43b63a5f844fChris Lattner/// or if it is not canonicalized.
4278f4c7371fb1d3cebcfb40abad4537bb82515704eaJohn McCallunsigned ASTContext::getIntegerRank(const Type *T) const {
4279467b27b9a24bdc823218ad1ad0e37673b6cc1e83John McCall  assert(T->isCanonicalUnqualified() && "T should be canonicalized");
4280f5c209d23b20ada4a9b6235db50317239cbf6ae1Alisdair Meredith
4281f52ab250ff92bc51a9ac9a8e19bd43b63a5f844fChris Lattner  switch (cast<BuiltinType>(T)->getKind()) {
4282b219cfc4d75f0a03630b7c4509ef791b7e97b2c8David Blaikie  default: llvm_unreachable("getIntegerRank(): not a built-in integer");
42837cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner  case BuiltinType::Bool:
4284f98aba35e6c3da5aae61843fc01334939e4e12ecEli Friedman    return 1 + (getIntWidth(BoolTy) << 3);
42857cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner  case BuiltinType::Char_S:
42867cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner  case BuiltinType::Char_U:
42877cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner  case BuiltinType::SChar:
42887cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner  case BuiltinType::UChar:
4289f98aba35e6c3da5aae61843fc01334939e4e12ecEli Friedman    return 2 + (getIntWidth(CharTy) << 3);
42907cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner  case BuiltinType::Short:
42917cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner  case BuiltinType::UShort:
4292f98aba35e6c3da5aae61843fc01334939e4e12ecEli Friedman    return 3 + (getIntWidth(ShortTy) << 3);
42937cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner  case BuiltinType::Int:
42947cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner  case BuiltinType::UInt:
4295f98aba35e6c3da5aae61843fc01334939e4e12ecEli Friedman    return 4 + (getIntWidth(IntTy) << 3);
42967cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner  case BuiltinType::Long:
42977cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner  case BuiltinType::ULong:
4298f98aba35e6c3da5aae61843fc01334939e4e12ecEli Friedman    return 5 + (getIntWidth(LongTy) << 3);
42997cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner  case BuiltinType::LongLong:
43007cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner  case BuiltinType::ULongLong:
4301f98aba35e6c3da5aae61843fc01334939e4e12ecEli Friedman    return 6 + (getIntWidth(LongLongTy) << 3);
43022df9ced9fd1e8c7d7b38443db07e0e811de22571Chris Lattner  case BuiltinType::Int128:
43032df9ced9fd1e8c7d7b38443db07e0e811de22571Chris Lattner  case BuiltinType::UInt128:
43042df9ced9fd1e8c7d7b38443db07e0e811de22571Chris Lattner    return 7 + (getIntWidth(Int128Ty) << 3);
4305f52ab250ff92bc51a9ac9a8e19bd43b63a5f844fChris Lattner  }
4306f52ab250ff92bc51a9ac9a8e19bd43b63a5f844fChris Lattner}
4307f52ab250ff92bc51a9ac9a8e19bd43b63a5f844fChris Lattner
430804e8357f6801e9ff52673e7e899a67bbabf9de93Eli Friedman/// \brief Whether this is a promotable bitfield reference according
430904e8357f6801e9ff52673e7e899a67bbabf9de93Eli Friedman/// to C99 6.3.1.1p2, bullet 2 (and GCC extensions).
431004e8357f6801e9ff52673e7e899a67bbabf9de93Eli Friedman///
431104e8357f6801e9ff52673e7e899a67bbabf9de93Eli Friedman/// \returns the type this bit-field will promote to, or NULL if no
431204e8357f6801e9ff52673e7e899a67bbabf9de93Eli Friedman/// promotion occurs.
43134ba2a17694148e16eaa8d3917f657ffcd3667be4Jay FoadQualType ASTContext::isPromotableBitField(Expr *E) const {
4314ceafbdeb93ecf323cca74e660bf54504c86f3b71Douglas Gregor  if (E->isTypeDependent() || E->isValueDependent())
4315ceafbdeb93ecf323cca74e660bf54504c86f3b71Douglas Gregor    return QualType();
4316ceafbdeb93ecf323cca74e660bf54504c86f3b71Douglas Gregor
4317993f43f24d7a45a5cd4678a3316b0852261fc5d4John McCall  FieldDecl *Field = E->getSourceBitField(); // FIXME: conditional bit-fields?
431804e8357f6801e9ff52673e7e899a67bbabf9de93Eli Friedman  if (!Field)
431904e8357f6801e9ff52673e7e899a67bbabf9de93Eli Friedman    return QualType();
432004e8357f6801e9ff52673e7e899a67bbabf9de93Eli Friedman
432104e8357f6801e9ff52673e7e899a67bbabf9de93Eli Friedman  QualType FT = Field->getType();
432204e8357f6801e9ff52673e7e899a67bbabf9de93Eli Friedman
4323a6b8b2c09610b8bc4330e948ece8b940c2386406Richard Smith  uint64_t BitWidth = Field->getBitWidthValue(*this);
432404e8357f6801e9ff52673e7e899a67bbabf9de93Eli Friedman  uint64_t IntSize = getTypeSize(IntTy);
432504e8357f6801e9ff52673e7e899a67bbabf9de93Eli Friedman  // GCC extension compatibility: if the bit-field size is less than or equal
432604e8357f6801e9ff52673e7e899a67bbabf9de93Eli Friedman  // to the size of int, it gets promoted no matter what its type is.
432704e8357f6801e9ff52673e7e899a67bbabf9de93Eli Friedman  // For instance, unsigned long bf : 4 gets promoted to signed int.
432804e8357f6801e9ff52673e7e899a67bbabf9de93Eli Friedman  if (BitWidth < IntSize)
432904e8357f6801e9ff52673e7e899a67bbabf9de93Eli Friedman    return IntTy;
433004e8357f6801e9ff52673e7e899a67bbabf9de93Eli Friedman
433104e8357f6801e9ff52673e7e899a67bbabf9de93Eli Friedman  if (BitWidth == IntSize)
433204e8357f6801e9ff52673e7e899a67bbabf9de93Eli Friedman    return FT->isSignedIntegerType() ? IntTy : UnsignedIntTy;
433304e8357f6801e9ff52673e7e899a67bbabf9de93Eli Friedman
433404e8357f6801e9ff52673e7e899a67bbabf9de93Eli Friedman  // Types bigger than int are not subject to promotions, and therefore act
433504e8357f6801e9ff52673e7e899a67bbabf9de93Eli Friedman  // like the base type.
433604e8357f6801e9ff52673e7e899a67bbabf9de93Eli Friedman  // FIXME: This doesn't quite match what gcc does, but what gcc does here
433704e8357f6801e9ff52673e7e899a67bbabf9de93Eli Friedman  // is ridiculous.
433804e8357f6801e9ff52673e7e899a67bbabf9de93Eli Friedman  return QualType();
433904e8357f6801e9ff52673e7e899a67bbabf9de93Eli Friedman}
434004e8357f6801e9ff52673e7e899a67bbabf9de93Eli Friedman
4341a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman/// getPromotedIntegerType - Returns the type that Promotable will
4342a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman/// promote to: C99 6.3.1.1p2, assuming that Promotable is a promotable
4343a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman/// integer type.
43444ba2a17694148e16eaa8d3917f657ffcd3667be4Jay FoadQualType ASTContext::getPromotedIntegerType(QualType Promotable) const {
4345a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman  assert(!Promotable.isNull());
4346a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman  assert(Promotable->isPromotableIntegerType());
4347842aef8d942a880eeb9535d40de31a86838264cbJohn McCall  if (const EnumType *ET = Promotable->getAs<EnumType>())
4348842aef8d942a880eeb9535d40de31a86838264cbJohn McCall    return ET->getDecl()->getPromotionType();
434968a2dc446fe6d32d5da3557902100ed06b21b12bEli Friedman
435068a2dc446fe6d32d5da3557902100ed06b21b12bEli Friedman  if (const BuiltinType *BT = Promotable->getAs<BuiltinType>()) {
435168a2dc446fe6d32d5da3557902100ed06b21b12bEli Friedman    // C++ [conv.prom]: A prvalue of type char16_t, char32_t, or wchar_t
435268a2dc446fe6d32d5da3557902100ed06b21b12bEli Friedman    // (3.9.1) can be converted to a prvalue of the first of the following
435368a2dc446fe6d32d5da3557902100ed06b21b12bEli Friedman    // types that can represent all the values of its underlying type:
435468a2dc446fe6d32d5da3557902100ed06b21b12bEli Friedman    // int, unsigned int, long int, unsigned long int, long long int, or
435568a2dc446fe6d32d5da3557902100ed06b21b12bEli Friedman    // unsigned long long int [...]
435668a2dc446fe6d32d5da3557902100ed06b21b12bEli Friedman    // FIXME: Is there some better way to compute this?
435768a2dc446fe6d32d5da3557902100ed06b21b12bEli Friedman    if (BT->getKind() == BuiltinType::WChar_S ||
435868a2dc446fe6d32d5da3557902100ed06b21b12bEli Friedman        BT->getKind() == BuiltinType::WChar_U ||
435968a2dc446fe6d32d5da3557902100ed06b21b12bEli Friedman        BT->getKind() == BuiltinType::Char16 ||
436068a2dc446fe6d32d5da3557902100ed06b21b12bEli Friedman        BT->getKind() == BuiltinType::Char32) {
436168a2dc446fe6d32d5da3557902100ed06b21b12bEli Friedman      bool FromIsSigned = BT->getKind() == BuiltinType::WChar_S;
436268a2dc446fe6d32d5da3557902100ed06b21b12bEli Friedman      uint64_t FromSize = getTypeSize(BT);
436368a2dc446fe6d32d5da3557902100ed06b21b12bEli Friedman      QualType PromoteTypes[] = { IntTy, UnsignedIntTy, LongTy, UnsignedLongTy,
436468a2dc446fe6d32d5da3557902100ed06b21b12bEli Friedman                                  LongLongTy, UnsignedLongLongTy };
436568a2dc446fe6d32d5da3557902100ed06b21b12bEli Friedman      for (size_t Idx = 0; Idx < llvm::array_lengthof(PromoteTypes); ++Idx) {
436668a2dc446fe6d32d5da3557902100ed06b21b12bEli Friedman        uint64_t ToSize = getTypeSize(PromoteTypes[Idx]);
436768a2dc446fe6d32d5da3557902100ed06b21b12bEli Friedman        if (FromSize < ToSize ||
436868a2dc446fe6d32d5da3557902100ed06b21b12bEli Friedman            (FromSize == ToSize &&
436968a2dc446fe6d32d5da3557902100ed06b21b12bEli Friedman             FromIsSigned == PromoteTypes[Idx]->isSignedIntegerType()))
437068a2dc446fe6d32d5da3557902100ed06b21b12bEli Friedman          return PromoteTypes[Idx];
437168a2dc446fe6d32d5da3557902100ed06b21b12bEli Friedman      }
437268a2dc446fe6d32d5da3557902100ed06b21b12bEli Friedman      llvm_unreachable("char type should fit into long long");
437368a2dc446fe6d32d5da3557902100ed06b21b12bEli Friedman    }
437468a2dc446fe6d32d5da3557902100ed06b21b12bEli Friedman  }
437568a2dc446fe6d32d5da3557902100ed06b21b12bEli Friedman
437668a2dc446fe6d32d5da3557902100ed06b21b12bEli Friedman  // At this point, we should have a signed or unsigned integer type.
4377a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman  if (Promotable->isSignedIntegerType())
4378a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman    return IntTy;
43795b64e77c2d51ca3e9313ed4107d3c4d927895cd6Eli Friedman  uint64_t PromotableSize = getIntWidth(Promotable);
43805b64e77c2d51ca3e9313ed4107d3c4d927895cd6Eli Friedman  uint64_t IntSize = getIntWidth(IntTy);
4381a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman  assert(Promotable->isUnsignedIntegerType() && PromotableSize <= IntSize);
4382a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman  return (PromotableSize != IntSize) ? IntTy : UnsignedIntTy;
4383a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman}
4384a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman
438531862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis/// \brief Recurses in pointer/array types until it finds an objc retainable
438631862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis/// type and returns its ownership.
438731862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios KyrtzidisQualifiers::ObjCLifetime ASTContext::getInnerObjCOwnership(QualType T) const {
438831862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis  while (!T.isNull()) {
438931862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis    if (T.getObjCLifetime() != Qualifiers::OCL_None)
439031862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis      return T.getObjCLifetime();
439131862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis    if (T->isArrayType())
439231862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis      T = getBaseElementType(T);
439331862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis    else if (const PointerType *PT = T->getAs<PointerType>())
439431862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis      T = PT->getPointeeType();
439531862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis    else if (const ReferenceType *RT = T->getAs<ReferenceType>())
439628445f0b62f6aed851ff87ce64d9b19200d3211fArgyrios Kyrtzidis      T = RT->getPointeeType();
439731862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis    else
439831862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis      break;
439931862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis  }
440031862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis
440131862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis  return Qualifiers::OCL_None;
440231862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis}
440331862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis
44041eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// getIntegerTypeOrder - Returns the highest ranked integer type:
44057cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner/// C99 6.3.1.8p1.  If LHS > RHS, return 1.  If LHS == RHS, return 0. If
44061eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// LHS < RHS, return -1.
44074ba2a17694148e16eaa8d3917f657ffcd3667be4Jay Foadint ASTContext::getIntegerTypeOrder(QualType LHS, QualType RHS) const {
4408f4c7371fb1d3cebcfb40abad4537bb82515704eaJohn McCall  const Type *LHSC = getCanonicalType(LHS).getTypePtr();
4409f4c7371fb1d3cebcfb40abad4537bb82515704eaJohn McCall  const Type *RHSC = getCanonicalType(RHS).getTypePtr();
44107cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner  if (LHSC == RHSC) return 0;
44111eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
4412f52ab250ff92bc51a9ac9a8e19bd43b63a5f844fChris Lattner  bool LHSUnsigned = LHSC->isUnsignedIntegerType();
4413f52ab250ff92bc51a9ac9a8e19bd43b63a5f844fChris Lattner  bool RHSUnsigned = RHSC->isUnsignedIntegerType();
44141eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
44157cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner  unsigned LHSRank = getIntegerRank(LHSC);
44167cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner  unsigned RHSRank = getIntegerRank(RHSC);
44171eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
44187cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner  if (LHSUnsigned == RHSUnsigned) {  // Both signed or both unsigned.
44197cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner    if (LHSRank == RHSRank) return 0;
44207cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner    return LHSRank > RHSRank ? 1 : -1;
44217cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner  }
44221eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
44237cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner  // Otherwise, the LHS is signed and the RHS is unsigned or visa versa.
44247cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner  if (LHSUnsigned) {
44257cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner    // If the unsigned [LHS] type is larger, return it.
44267cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner    if (LHSRank >= RHSRank)
44277cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner      return 1;
44281eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
44297cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner    // If the signed type can represent all values of the unsigned type, it
44307cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner    // wins.  Because we are dealing with 2's complement and types that are
44311eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    // powers of two larger than each other, this is always safe.
44327cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner    return -1;
44337cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner  }
44347cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner
44357cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner  // If the unsigned [RHS] type is larger, return it.
44367cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner  if (RHSRank >= LHSRank)
44377cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner    return -1;
44381eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
44397cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner  // If the signed type can represent all values of the unsigned type, it
44407cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner  // wins.  Because we are dealing with 2's complement and types that are
44411eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  // powers of two larger than each other, this is always safe.
44427cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner  return 1;
44435f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
444471993dd85eed9cc42c6b2fa61ee5c53026b74817Anders Carlsson
444579cbc7dd2aacd85a28f469b5dc73c4ea296e7072Anders Carlssonstatic RecordDecl *
4446ba877adeb49ed6dc17f27fa3a3bcd0cca713fd68Abramo BagnaraCreateRecordDecl(const ASTContext &Ctx, RecordDecl::TagKind TK,
4447ba877adeb49ed6dc17f27fa3a3bcd0cca713fd68Abramo Bagnara                 DeclContext *DC, IdentifierInfo *Id) {
4448ba877adeb49ed6dc17f27fa3a3bcd0cca713fd68Abramo Bagnara  SourceLocation Loc;
44494e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie  if (Ctx.getLangOpts().CPlusPlus)
4450ba877adeb49ed6dc17f27fa3a3bcd0cca713fd68Abramo Bagnara    return CXXRecordDecl::Create(Ctx, TK, DC, Loc, Loc, Id);
445179cbc7dd2aacd85a28f469b5dc73c4ea296e7072Anders Carlsson  else
4452ba877adeb49ed6dc17f27fa3a3bcd0cca713fd68Abramo Bagnara    return RecordDecl::Create(Ctx, TK, DC, Loc, Loc, Id);
445379cbc7dd2aacd85a28f469b5dc73c4ea296e7072Anders Carlsson}
4454ba877adeb49ed6dc17f27fa3a3bcd0cca713fd68Abramo Bagnara
44551eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump// getCFConstantStringType - Return the type used for constant CFStrings.
44564ba2a17694148e16eaa8d3917f657ffcd3667be4Jay FoadQualType ASTContext::getCFConstantStringType() const {
445771993dd85eed9cc42c6b2fa61ee5c53026b74817Anders Carlsson  if (!CFConstantStringTypeDecl) {
44581eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    CFConstantStringTypeDecl =
4459ba877adeb49ed6dc17f27fa3a3bcd0cca713fd68Abramo Bagnara      CreateRecordDecl(*this, TTK_Struct, TUDecl,
446079cbc7dd2aacd85a28f469b5dc73c4ea296e7072Anders Carlsson                       &Idents.get("NSConstantString"));
44615cfa011e61e14e6f2e1659047d809706c0e4c6a3John McCall    CFConstantStringTypeDecl->startDefinition();
446279cbc7dd2aacd85a28f469b5dc73c4ea296e7072Anders Carlsson
4463f06273f8bbacb086a46bde456429c8d08f6d07eeAnders Carlsson    QualType FieldTypes[4];
44641eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
446571993dd85eed9cc42c6b2fa61ee5c53026b74817Anders Carlsson    // const int *isa;
44660953e767ff7817f97b3ab20896b229891eeff45bJohn McCall    FieldTypes[0] = getPointerType(IntTy.withConst());
4467f06273f8bbacb086a46bde456429c8d08f6d07eeAnders Carlsson    // int flags;
4468f06273f8bbacb086a46bde456429c8d08f6d07eeAnders Carlsson    FieldTypes[1] = IntTy;
446971993dd85eed9cc42c6b2fa61ee5c53026b74817Anders Carlsson    // const char *str;
44700953e767ff7817f97b3ab20896b229891eeff45bJohn McCall    FieldTypes[2] = getPointerType(CharTy.withConst());
447171993dd85eed9cc42c6b2fa61ee5c53026b74817Anders Carlsson    // long length;
44721eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    FieldTypes[3] = LongTy;
44731eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
447444b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor    // Create fields
447544b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor    for (unsigned i = 0; i < 4; ++i) {
44761eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      FieldDecl *Field = FieldDecl::Create(*this, CFConstantStringTypeDecl,
4477ff676cb48fe8bf7be2feaa251dc7c5fb15af4730Abramo Bagnara                                           SourceLocation(),
447844b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor                                           SourceLocation(), 0,
4479a93c934af4fbf97cbe8e649d82e68ccacfe57c95John McCall                                           FieldTypes[i], /*TInfo=*/0,
44801eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump                                           /*BitWidth=*/0,
44817a614d8380297fcd2bc23986241905d97222948cRichard Smith                                           /*Mutable=*/false,
4482ca5233044ef679840d1ad1c46a36b16e2ee8a6e1Richard Smith                                           ICIS_NoInit);
44832888b65aae768f54062505330df7be230a0510c7John McCall      Field->setAccess(AS_public);
448417945a0f64fe03ff6ec0c2146005a87636e3ac12Argyrios Kyrtzidis      CFConstantStringTypeDecl->addDecl(Field);
448544b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor    }
448644b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor
4487838db383b69b9fb55f55c8e9546477df198a4faaDouglas Gregor    CFConstantStringTypeDecl->completeDefinition();
448871993dd85eed9cc42c6b2fa61ee5c53026b74817Anders Carlsson  }
44891eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
449071993dd85eed9cc42c6b2fa61ee5c53026b74817Anders Carlsson  return getTagDeclType(CFConstantStringTypeDecl);
44918467583c2704e7a9691ea56939a029015f0ade0aGabor Greif}
4492b2cf3573d7351094f6247fcca94703ce88eb9ee0Anders Carlsson
4493f7992132207d7ddc8810a135cec97ebe5805e500Fariborz JahanianQualType ASTContext::getObjCSuperType() const {
4494f7992132207d7ddc8810a135cec97ebe5805e500Fariborz Jahanian  if (ObjCSuperType.isNull()) {
4495f7992132207d7ddc8810a135cec97ebe5805e500Fariborz Jahanian    RecordDecl *ObjCSuperTypeDecl  =
4496f7992132207d7ddc8810a135cec97ebe5805e500Fariborz Jahanian      CreateRecordDecl(*this, TTK_Struct, TUDecl, &Idents.get("objc_super"));
4497f7992132207d7ddc8810a135cec97ebe5805e500Fariborz Jahanian    TUDecl->addDecl(ObjCSuperTypeDecl);
4498f7992132207d7ddc8810a135cec97ebe5805e500Fariborz Jahanian    ObjCSuperType = getTagDeclType(ObjCSuperTypeDecl);
4499f7992132207d7ddc8810a135cec97ebe5805e500Fariborz Jahanian  }
4500f7992132207d7ddc8810a135cec97ebe5805e500Fariborz Jahanian  return ObjCSuperType;
4501f7992132207d7ddc8810a135cec97ebe5805e500Fariborz Jahanian}
4502f7992132207d7ddc8810a135cec97ebe5805e500Fariborz Jahanian
4503319ac896a0fef7365d5589b8021db7e41207fe42Douglas Gregorvoid ASTContext::setCFConstantStringType(QualType T) {
45046217b80b7a1379b74cced1c076338262c3c980b3Ted Kremenek  const RecordType *Rec = T->getAs<RecordType>();
4505319ac896a0fef7365d5589b8021db7e41207fe42Douglas Gregor  assert(Rec && "Invalid CFConstantStringType");
4506319ac896a0fef7365d5589b8021db7e41207fe42Douglas Gregor  CFConstantStringTypeDecl = Rec->getDecl();
4507319ac896a0fef7365d5589b8021db7e41207fe42Douglas Gregor}
4508319ac896a0fef7365d5589b8021db7e41207fe42Douglas Gregor
45094ba2a17694148e16eaa8d3917f657ffcd3667be4Jay FoadQualType ASTContext::getBlockDescriptorType() const {
4510adaaad3715c9c26cdcfdfe3401a13d7b4423ddcfMike Stump  if (BlockDescriptorType)
4511adaaad3715c9c26cdcfdfe3401a13d7b4423ddcfMike Stump    return getTagDeclType(BlockDescriptorType);
4512adaaad3715c9c26cdcfdfe3401a13d7b4423ddcfMike Stump
4513adaaad3715c9c26cdcfdfe3401a13d7b4423ddcfMike Stump  RecordDecl *T;
4514adaaad3715c9c26cdcfdfe3401a13d7b4423ddcfMike Stump  // FIXME: Needs the FlagAppleBlock bit.
4515ba877adeb49ed6dc17f27fa3a3bcd0cca713fd68Abramo Bagnara  T = CreateRecordDecl(*this, TTK_Struct, TUDecl,
451679cbc7dd2aacd85a28f469b5dc73c4ea296e7072Anders Carlsson                       &Idents.get("__block_descriptor"));
45175cfa011e61e14e6f2e1659047d809706c0e4c6a3John McCall  T->startDefinition();
4518adaaad3715c9c26cdcfdfe3401a13d7b4423ddcfMike Stump
4519adaaad3715c9c26cdcfdfe3401a13d7b4423ddcfMike Stump  QualType FieldTypes[] = {
4520adaaad3715c9c26cdcfdfe3401a13d7b4423ddcfMike Stump    UnsignedLongTy,
4521adaaad3715c9c26cdcfdfe3401a13d7b4423ddcfMike Stump    UnsignedLongTy,
4522adaaad3715c9c26cdcfdfe3401a13d7b4423ddcfMike Stump  };
4523adaaad3715c9c26cdcfdfe3401a13d7b4423ddcfMike Stump
4524adaaad3715c9c26cdcfdfe3401a13d7b4423ddcfMike Stump  const char *FieldNames[] = {
4525adaaad3715c9c26cdcfdfe3401a13d7b4423ddcfMike Stump    "reserved",
4526083c25eea14bb4cc4ecc3ec763c60e2e609e22bdMike Stump    "Size"
4527adaaad3715c9c26cdcfdfe3401a13d7b4423ddcfMike Stump  };
4528adaaad3715c9c26cdcfdfe3401a13d7b4423ddcfMike Stump
4529adaaad3715c9c26cdcfdfe3401a13d7b4423ddcfMike Stump  for (size_t i = 0; i < 2; ++i) {
4530ff676cb48fe8bf7be2feaa251dc7c5fb15af4730Abramo Bagnara    FieldDecl *Field = FieldDecl::Create(*this, T, SourceLocation(),
4531adaaad3715c9c26cdcfdfe3401a13d7b4423ddcfMike Stump                                         SourceLocation(),
4532adaaad3715c9c26cdcfdfe3401a13d7b4423ddcfMike Stump                                         &Idents.get(FieldNames[i]),
4533a93c934af4fbf97cbe8e649d82e68ccacfe57c95John McCall                                         FieldTypes[i], /*TInfo=*/0,
4534adaaad3715c9c26cdcfdfe3401a13d7b4423ddcfMike Stump                                         /*BitWidth=*/0,
45357a614d8380297fcd2bc23986241905d97222948cRichard Smith                                         /*Mutable=*/false,
4536ca5233044ef679840d1ad1c46a36b16e2ee8a6e1Richard Smith                                         ICIS_NoInit);
45372888b65aae768f54062505330df7be230a0510c7John McCall    Field->setAccess(AS_public);
4538adaaad3715c9c26cdcfdfe3401a13d7b4423ddcfMike Stump    T->addDecl(Field);
4539adaaad3715c9c26cdcfdfe3401a13d7b4423ddcfMike Stump  }
4540adaaad3715c9c26cdcfdfe3401a13d7b4423ddcfMike Stump
4541838db383b69b9fb55f55c8e9546477df198a4faaDouglas Gregor  T->completeDefinition();
4542adaaad3715c9c26cdcfdfe3401a13d7b4423ddcfMike Stump
4543adaaad3715c9c26cdcfdfe3401a13d7b4423ddcfMike Stump  BlockDescriptorType = T;
4544adaaad3715c9c26cdcfdfe3401a13d7b4423ddcfMike Stump
4545adaaad3715c9c26cdcfdfe3401a13d7b4423ddcfMike Stump  return getTagDeclType(BlockDescriptorType);
4546adaaad3715c9c26cdcfdfe3401a13d7b4423ddcfMike Stump}
4547adaaad3715c9c26cdcfdfe3401a13d7b4423ddcfMike Stump
45484ba2a17694148e16eaa8d3917f657ffcd3667be4Jay FoadQualType ASTContext::getBlockDescriptorExtendedType() const {
4549083c25eea14bb4cc4ecc3ec763c60e2e609e22bdMike Stump  if (BlockDescriptorExtendedType)
4550083c25eea14bb4cc4ecc3ec763c60e2e609e22bdMike Stump    return getTagDeclType(BlockDescriptorExtendedType);
4551083c25eea14bb4cc4ecc3ec763c60e2e609e22bdMike Stump
4552083c25eea14bb4cc4ecc3ec763c60e2e609e22bdMike Stump  RecordDecl *T;
4553083c25eea14bb4cc4ecc3ec763c60e2e609e22bdMike Stump  // FIXME: Needs the FlagAppleBlock bit.
4554ba877adeb49ed6dc17f27fa3a3bcd0cca713fd68Abramo Bagnara  T = CreateRecordDecl(*this, TTK_Struct, TUDecl,
455579cbc7dd2aacd85a28f469b5dc73c4ea296e7072Anders Carlsson                       &Idents.get("__block_descriptor_withcopydispose"));
45565cfa011e61e14e6f2e1659047d809706c0e4c6a3John McCall  T->startDefinition();
4557083c25eea14bb4cc4ecc3ec763c60e2e609e22bdMike Stump
4558083c25eea14bb4cc4ecc3ec763c60e2e609e22bdMike Stump  QualType FieldTypes[] = {
4559083c25eea14bb4cc4ecc3ec763c60e2e609e22bdMike Stump    UnsignedLongTy,
4560083c25eea14bb4cc4ecc3ec763c60e2e609e22bdMike Stump    UnsignedLongTy,
4561083c25eea14bb4cc4ecc3ec763c60e2e609e22bdMike Stump    getPointerType(VoidPtrTy),
4562083c25eea14bb4cc4ecc3ec763c60e2e609e22bdMike Stump    getPointerType(VoidPtrTy)
4563083c25eea14bb4cc4ecc3ec763c60e2e609e22bdMike Stump  };
4564083c25eea14bb4cc4ecc3ec763c60e2e609e22bdMike Stump
4565083c25eea14bb4cc4ecc3ec763c60e2e609e22bdMike Stump  const char *FieldNames[] = {
4566083c25eea14bb4cc4ecc3ec763c60e2e609e22bdMike Stump    "reserved",
4567083c25eea14bb4cc4ecc3ec763c60e2e609e22bdMike Stump    "Size",
4568083c25eea14bb4cc4ecc3ec763c60e2e609e22bdMike Stump    "CopyFuncPtr",
4569083c25eea14bb4cc4ecc3ec763c60e2e609e22bdMike Stump    "DestroyFuncPtr"
4570083c25eea14bb4cc4ecc3ec763c60e2e609e22bdMike Stump  };
4571083c25eea14bb4cc4ecc3ec763c60e2e609e22bdMike Stump
4572083c25eea14bb4cc4ecc3ec763c60e2e609e22bdMike Stump  for (size_t i = 0; i < 4; ++i) {
4573ff676cb48fe8bf7be2feaa251dc7c5fb15af4730Abramo Bagnara    FieldDecl *Field = FieldDecl::Create(*this, T, SourceLocation(),
4574083c25eea14bb4cc4ecc3ec763c60e2e609e22bdMike Stump                                         SourceLocation(),
4575083c25eea14bb4cc4ecc3ec763c60e2e609e22bdMike Stump                                         &Idents.get(FieldNames[i]),
4576a93c934af4fbf97cbe8e649d82e68ccacfe57c95John McCall                                         FieldTypes[i], /*TInfo=*/0,
4577083c25eea14bb4cc4ecc3ec763c60e2e609e22bdMike Stump                                         /*BitWidth=*/0,
45787a614d8380297fcd2bc23986241905d97222948cRichard Smith                                         /*Mutable=*/false,
4579ca5233044ef679840d1ad1c46a36b16e2ee8a6e1Richard Smith                                         ICIS_NoInit);
45802888b65aae768f54062505330df7be230a0510c7John McCall    Field->setAccess(AS_public);
4581083c25eea14bb4cc4ecc3ec763c60e2e609e22bdMike Stump    T->addDecl(Field);
4582083c25eea14bb4cc4ecc3ec763c60e2e609e22bdMike Stump  }
4583083c25eea14bb4cc4ecc3ec763c60e2e609e22bdMike Stump
4584838db383b69b9fb55f55c8e9546477df198a4faaDouglas Gregor  T->completeDefinition();
4585083c25eea14bb4cc4ecc3ec763c60e2e609e22bdMike Stump
4586083c25eea14bb4cc4ecc3ec763c60e2e609e22bdMike Stump  BlockDescriptorExtendedType = T;
4587083c25eea14bb4cc4ecc3ec763c60e2e609e22bdMike Stump
4588083c25eea14bb4cc4ecc3ec763c60e2e609e22bdMike Stump  return getTagDeclType(BlockDescriptorExtendedType);
4589083c25eea14bb4cc4ecc3ec763c60e2e609e22bdMike Stump}
4590083c25eea14bb4cc4ecc3ec763c60e2e609e22bdMike Stump
4591b15c8984ea300624fbbde385d3907667ce1043faFariborz Jahanian/// BlockRequiresCopying - Returns true if byref variable "D" of type "Ty"
4592b15c8984ea300624fbbde385d3907667ce1043faFariborz Jahanian/// requires copy/dispose. Note that this must match the logic
4593b15c8984ea300624fbbde385d3907667ce1043faFariborz Jahanian/// in buildByrefHelpers.
4594b15c8984ea300624fbbde385d3907667ce1043faFariborz Jahanianbool ASTContext::BlockRequiresCopying(QualType Ty,
4595b15c8984ea300624fbbde385d3907667ce1043faFariborz Jahanian                                      const VarDecl *D) {
4596b15c8984ea300624fbbde385d3907667ce1043faFariborz Jahanian  if (const CXXRecordDecl *record = Ty->getAsCXXRecordDecl()) {
4597b15c8984ea300624fbbde385d3907667ce1043faFariborz Jahanian    const Expr *copyExpr = getBlockVarCopyInits(D);
4598b15c8984ea300624fbbde385d3907667ce1043faFariborz Jahanian    if (!copyExpr && record->hasTrivialDestructor()) return false;
4599b15c8984ea300624fbbde385d3907667ce1043faFariborz Jahanian
4600af7b44d847d3e44c43346d508b2e55a6254b6e9dMike Stump    return true;
4601b15c8984ea300624fbbde385d3907667ce1043faFariborz Jahanian  }
4602b15c8984ea300624fbbde385d3907667ce1043faFariborz Jahanian
4603b15c8984ea300624fbbde385d3907667ce1043faFariborz Jahanian  if (!Ty->isObjCRetainableType()) return false;
4604b15c8984ea300624fbbde385d3907667ce1043faFariborz Jahanian
4605b15c8984ea300624fbbde385d3907667ce1043faFariborz Jahanian  Qualifiers qs = Ty.getQualifiers();
4606b15c8984ea300624fbbde385d3907667ce1043faFariborz Jahanian
4607b15c8984ea300624fbbde385d3907667ce1043faFariborz Jahanian  // If we have lifetime, that dominates.
4608b15c8984ea300624fbbde385d3907667ce1043faFariborz Jahanian  if (Qualifiers::ObjCLifetime lifetime = qs.getObjCLifetime()) {
4609b15c8984ea300624fbbde385d3907667ce1043faFariborz Jahanian    assert(getLangOpts().ObjCAutoRefCount);
4610b15c8984ea300624fbbde385d3907667ce1043faFariborz Jahanian
4611b15c8984ea300624fbbde385d3907667ce1043faFariborz Jahanian    switch (lifetime) {
4612b15c8984ea300624fbbde385d3907667ce1043faFariborz Jahanian      case Qualifiers::OCL_None: llvm_unreachable("impossible");
4613b15c8984ea300624fbbde385d3907667ce1043faFariborz Jahanian
4614b15c8984ea300624fbbde385d3907667ce1043faFariborz Jahanian      // These are just bits as far as the runtime is concerned.
4615b15c8984ea300624fbbde385d3907667ce1043faFariborz Jahanian      case Qualifiers::OCL_ExplicitNone:
4616b15c8984ea300624fbbde385d3907667ce1043faFariborz Jahanian      case Qualifiers::OCL_Autoreleasing:
4617b15c8984ea300624fbbde385d3907667ce1043faFariborz Jahanian        return false;
4618b15c8984ea300624fbbde385d3907667ce1043faFariborz Jahanian
4619b15c8984ea300624fbbde385d3907667ce1043faFariborz Jahanian      // Tell the runtime that this is ARC __weak, called by the
4620b15c8984ea300624fbbde385d3907667ce1043faFariborz Jahanian      // byref routines.
4621b15c8984ea300624fbbde385d3907667ce1043faFariborz Jahanian      case Qualifiers::OCL_Weak:
4622b15c8984ea300624fbbde385d3907667ce1043faFariborz Jahanian      // ARC __strong __block variables need to be retained.
4623b15c8984ea300624fbbde385d3907667ce1043faFariborz Jahanian      case Qualifiers::OCL_Strong:
4624b15c8984ea300624fbbde385d3907667ce1043faFariborz Jahanian        return true;
4625e38be617437ccdcc180c5a49e447cbcd07539292Fariborz Jahanian    }
4626b15c8984ea300624fbbde385d3907667ce1043faFariborz Jahanian    llvm_unreachable("fell out of lifetime switch!");
4627e38be617437ccdcc180c5a49e447cbcd07539292Fariborz Jahanian  }
4628b15c8984ea300624fbbde385d3907667ce1043faFariborz Jahanian  return (Ty->isBlockPointerType() || isObjCNSObjectType(Ty) ||
4629b15c8984ea300624fbbde385d3907667ce1043faFariborz Jahanian          Ty->isObjCObjectPointerType());
4630af7b44d847d3e44c43346d508b2e55a6254b6e9dMike Stump}
4631af7b44d847d3e44c43346d508b2e55a6254b6e9dMike Stump
46323ca23d7dc6cb61e6f363a58d9256d548199d120cFariborz Jahanianbool ASTContext::getByrefLifetime(QualType Ty,
46333ca23d7dc6cb61e6f363a58d9256d548199d120cFariborz Jahanian                              Qualifiers::ObjCLifetime &LifeTime,
46343ca23d7dc6cb61e6f363a58d9256d548199d120cFariborz Jahanian                              bool &HasByrefExtendedLayout) const {
46353ca23d7dc6cb61e6f363a58d9256d548199d120cFariborz Jahanian
46363ca23d7dc6cb61e6f363a58d9256d548199d120cFariborz Jahanian  if (!getLangOpts().ObjC1 ||
46373ca23d7dc6cb61e6f363a58d9256d548199d120cFariborz Jahanian      getLangOpts().getGC() != LangOptions::NonGC)
46383ca23d7dc6cb61e6f363a58d9256d548199d120cFariborz Jahanian    return false;
46393ca23d7dc6cb61e6f363a58d9256d548199d120cFariborz Jahanian
46403ca23d7dc6cb61e6f363a58d9256d548199d120cFariborz Jahanian  HasByrefExtendedLayout = false;
464134db84fdb092f89ea3678a0792074a5b9253829aFariborz Jahanian  if (Ty->isRecordType()) {
46423ca23d7dc6cb61e6f363a58d9256d548199d120cFariborz Jahanian    HasByrefExtendedLayout = true;
46433ca23d7dc6cb61e6f363a58d9256d548199d120cFariborz Jahanian    LifeTime = Qualifiers::OCL_None;
46443ca23d7dc6cb61e6f363a58d9256d548199d120cFariborz Jahanian  }
46453ca23d7dc6cb61e6f363a58d9256d548199d120cFariborz Jahanian  else if (getLangOpts().ObjCAutoRefCount)
46463ca23d7dc6cb61e6f363a58d9256d548199d120cFariborz Jahanian    LifeTime = Ty.getObjCLifetime();
46473ca23d7dc6cb61e6f363a58d9256d548199d120cFariborz Jahanian  // MRR.
46483ca23d7dc6cb61e6f363a58d9256d548199d120cFariborz Jahanian  else if (Ty->isObjCObjectPointerType() || Ty->isBlockPointerType())
46493ca23d7dc6cb61e6f363a58d9256d548199d120cFariborz Jahanian    LifeTime = Qualifiers::OCL_ExplicitNone;
46503ca23d7dc6cb61e6f363a58d9256d548199d120cFariborz Jahanian  else
46513ca23d7dc6cb61e6f363a58d9256d548199d120cFariborz Jahanian    LifeTime = Qualifiers::OCL_None;
46523ca23d7dc6cb61e6f363a58d9256d548199d120cFariborz Jahanian  return true;
46533ca23d7dc6cb61e6f363a58d9256d548199d120cFariborz Jahanian}
46543ca23d7dc6cb61e6f363a58d9256d548199d120cFariborz Jahanian
4655e97179c675b341927807c718be215c8d1aab8acbDouglas GregorTypedefDecl *ASTContext::getObjCInstanceTypeDecl() {
4656e97179c675b341927807c718be215c8d1aab8acbDouglas Gregor  if (!ObjCInstanceTypeDecl)
4657e97179c675b341927807c718be215c8d1aab8acbDouglas Gregor    ObjCInstanceTypeDecl = TypedefDecl::Create(*this,
4658e97179c675b341927807c718be215c8d1aab8acbDouglas Gregor                                               getTranslationUnitDecl(),
4659e97179c675b341927807c718be215c8d1aab8acbDouglas Gregor                                               SourceLocation(),
4660e97179c675b341927807c718be215c8d1aab8acbDouglas Gregor                                               SourceLocation(),
4661e97179c675b341927807c718be215c8d1aab8acbDouglas Gregor                                               &Idents.get("instancetype"),
4662e97179c675b341927807c718be215c8d1aab8acbDouglas Gregor                                     getTrivialTypeSourceInfo(getObjCIdType()));
4663e97179c675b341927807c718be215c8d1aab8acbDouglas Gregor  return ObjCInstanceTypeDecl;
4664e97179c675b341927807c718be215c8d1aab8acbDouglas Gregor}
4665e97179c675b341927807c718be215c8d1aab8acbDouglas Gregor
4666e8c49533521c40643653f943d47229e62d277f88Anders Carlsson// This returns true if a type has been typedefed to BOOL:
4667e8c49533521c40643653f943d47229e62d277f88Anders Carlsson// typedef <type> BOOL;
46682d99833e8c956775f2183601cd120b65b569c867Chris Lattnerstatic bool isTypeTypedefedAsBOOL(QualType T) {
4669e8c49533521c40643653f943d47229e62d277f88Anders Carlsson  if (const TypedefType *TT = dyn_cast<TypedefType>(T))
4670bb49c3ee5d270485f4b273691fd14bc97403fa5dChris Lattner    if (IdentifierInfo *II = TT->getDecl()->getIdentifier())
4671bb49c3ee5d270485f4b273691fd14bc97403fa5dChris Lattner      return II->isStr("BOOL");
46721eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
467385f9bceab1542aafff012d4d28e998f4ba16e362Anders Carlsson  return false;
467485f9bceab1542aafff012d4d28e998f4ba16e362Anders Carlsson}
467585f9bceab1542aafff012d4d28e998f4ba16e362Anders Carlsson
4676a526c5c67e5a0473c340903ee542ce570119665fTed Kremenek/// getObjCEncodingTypeSize returns size of type for objective-c encoding
467733e1d64ab5cd5d27f8530ccd056191fe2c9f3f2eFariborz Jahanian/// purpose.
46784ba2a17694148e16eaa8d3917f657ffcd3667be4Jay FoadCharUnits ASTContext::getObjCEncodingTypeSize(QualType type) const {
4679f968d8374791c37bc464efd9168c2d33dd73605fDouglas Gregor  if (!type->isIncompleteArrayType() && type->isIncompleteType())
4680f968d8374791c37bc464efd9168c2d33dd73605fDouglas Gregor    return CharUnits::Zero();
4681f968d8374791c37bc464efd9168c2d33dd73605fDouglas Gregor
4682199c3d6cd16aebbb9c7f0d42af9d922c9628bf70Ken Dyck  CharUnits sz = getTypeSizeInChars(type);
46831eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
468433e1d64ab5cd5d27f8530ccd056191fe2c9f3f2eFariborz Jahanian  // Make all integer and enum types at least as large as an int
46852ade35e2cfd554e49d35a52047cea98a82787af9Douglas Gregor  if (sz.isPositive() && type->isIntegralOrEnumerationType())
4686199c3d6cd16aebbb9c7f0d42af9d922c9628bf70Ken Dyck    sz = std::max(sz, getTypeSizeInChars(IntTy));
468733e1d64ab5cd5d27f8530ccd056191fe2c9f3f2eFariborz Jahanian  // Treat arrays as pointers, since that's how they're passed in.
468833e1d64ab5cd5d27f8530ccd056191fe2c9f3f2eFariborz Jahanian  else if (type->isArrayType())
4689199c3d6cd16aebbb9c7f0d42af9d922c9628bf70Ken Dyck    sz = getTypeSizeInChars(VoidPtrTy);
4690aa8741a1db98eef05f09b1200dba94aa5dc3bc3dKen Dyck  return sz;
4691199c3d6cd16aebbb9c7f0d42af9d922c9628bf70Ken Dyck}
4692199c3d6cd16aebbb9c7f0d42af9d922c9628bf70Ken Dyck
4693199c3d6cd16aebbb9c7f0d42af9d922c9628bf70Ken Dyckstatic inline
4694199c3d6cd16aebbb9c7f0d42af9d922c9628bf70Ken Dyckstd::string charUnitsToString(const CharUnits &CU) {
4695199c3d6cd16aebbb9c7f0d42af9d922c9628bf70Ken Dyck  return llvm::itostr(CU.getQuantity());
469633e1d64ab5cd5d27f8530ccd056191fe2c9f3f2eFariborz Jahanian}
469733e1d64ab5cd5d27f8530ccd056191fe2c9f3f2eFariborz Jahanian
46986b5a61b6dc400027fd793dcadceeb9da944a37eaJohn McCall/// getObjCEncodingForBlock - Return the encoded type for this block
46995e530af5d51572a0ed5dbe50da54bd333840c63dDavid Chisnall/// declaration.
47006b5a61b6dc400027fd793dcadceeb9da944a37eaJohn McCallstd::string ASTContext::getObjCEncodingForBlock(const BlockExpr *Expr) const {
47016b5a61b6dc400027fd793dcadceeb9da944a37eaJohn McCall  std::string S;
47026b5a61b6dc400027fd793dcadceeb9da944a37eaJohn McCall
47035e530af5d51572a0ed5dbe50da54bd333840c63dDavid Chisnall  const BlockDecl *Decl = Expr->getBlockDecl();
47045e530af5d51572a0ed5dbe50da54bd333840c63dDavid Chisnall  QualType BlockTy =
47055e530af5d51572a0ed5dbe50da54bd333840c63dDavid Chisnall      Expr->getType()->getAs<BlockPointerType>()->getPointeeType();
47065e530af5d51572a0ed5dbe50da54bd333840c63dDavid Chisnall  // Encode result type.
47073d145f660a23d5bdabdd1ead83c51f3df1489b09Fariborz Jahanian  if (getLangOpts().EncodeExtendedBlockSig)
470806cffc0511ba76d979fad9ef6944e685936b62f0Fariborz Jahanian    getObjCEncodingForMethodParameter(Decl::OBJC_TQ_None,
470906cffc0511ba76d979fad9ef6944e685936b62f0Fariborz Jahanian                            BlockTy->getAs<FunctionType>()->getResultType(),
471006cffc0511ba76d979fad9ef6944e685936b62f0Fariborz Jahanian                            S, true /*Extended*/);
471106cffc0511ba76d979fad9ef6944e685936b62f0Fariborz Jahanian  else
471206cffc0511ba76d979fad9ef6944e685936b62f0Fariborz Jahanian    getObjCEncodingForType(BlockTy->getAs<FunctionType>()->getResultType(),
471306cffc0511ba76d979fad9ef6944e685936b62f0Fariborz Jahanian                           S);
47145e530af5d51572a0ed5dbe50da54bd333840c63dDavid Chisnall  // Compute size of all parameters.
47155e530af5d51572a0ed5dbe50da54bd333840c63dDavid Chisnall  // Start with computing size of a pointer in number of bytes.
47165e530af5d51572a0ed5dbe50da54bd333840c63dDavid Chisnall  // FIXME: There might(should) be a better way of doing this computation!
47175e530af5d51572a0ed5dbe50da54bd333840c63dDavid Chisnall  SourceLocation Loc;
4718199c3d6cd16aebbb9c7f0d42af9d922c9628bf70Ken Dyck  CharUnits PtrSize = getTypeSizeInChars(VoidPtrTy);
4719199c3d6cd16aebbb9c7f0d42af9d922c9628bf70Ken Dyck  CharUnits ParmOffset = PtrSize;
47206f46c2653c1545cc3fef0c0df996d18160160ce8Fariborz Jahanian  for (BlockDecl::param_const_iterator PI = Decl->param_begin(),
47215e530af5d51572a0ed5dbe50da54bd333840c63dDavid Chisnall       E = Decl->param_end(); PI != E; ++PI) {
47225e530af5d51572a0ed5dbe50da54bd333840c63dDavid Chisnall    QualType PType = (*PI)->getType();
4723aa8741a1db98eef05f09b1200dba94aa5dc3bc3dKen Dyck    CharUnits sz = getObjCEncodingTypeSize(PType);
4724075a54354dc6e3644b12206e5127855091783fd6Fariborz Jahanian    if (sz.isZero())
4725075a54354dc6e3644b12206e5127855091783fd6Fariborz Jahanian      continue;
4726199c3d6cd16aebbb9c7f0d42af9d922c9628bf70Ken Dyck    assert (sz.isPositive() && "BlockExpr - Incomplete param type");
47275e530af5d51572a0ed5dbe50da54bd333840c63dDavid Chisnall    ParmOffset += sz;
47285e530af5d51572a0ed5dbe50da54bd333840c63dDavid Chisnall  }
47295e530af5d51572a0ed5dbe50da54bd333840c63dDavid Chisnall  // Size of the argument frame
4730199c3d6cd16aebbb9c7f0d42af9d922c9628bf70Ken Dyck  S += charUnitsToString(ParmOffset);
47315e530af5d51572a0ed5dbe50da54bd333840c63dDavid Chisnall  // Block pointer and offset.
47325e530af5d51572a0ed5dbe50da54bd333840c63dDavid Chisnall  S += "@?0";
47335e530af5d51572a0ed5dbe50da54bd333840c63dDavid Chisnall
47345e530af5d51572a0ed5dbe50da54bd333840c63dDavid Chisnall  // Argument types.
47355e530af5d51572a0ed5dbe50da54bd333840c63dDavid Chisnall  ParmOffset = PtrSize;
47365e530af5d51572a0ed5dbe50da54bd333840c63dDavid Chisnall  for (BlockDecl::param_const_iterator PI = Decl->param_begin(), E =
47375e530af5d51572a0ed5dbe50da54bd333840c63dDavid Chisnall       Decl->param_end(); PI != E; ++PI) {
47385e530af5d51572a0ed5dbe50da54bd333840c63dDavid Chisnall    ParmVarDecl *PVDecl = *PI;
47395e530af5d51572a0ed5dbe50da54bd333840c63dDavid Chisnall    QualType PType = PVDecl->getOriginalType();
47405e530af5d51572a0ed5dbe50da54bd333840c63dDavid Chisnall    if (const ArrayType *AT =
47415e530af5d51572a0ed5dbe50da54bd333840c63dDavid Chisnall          dyn_cast<ArrayType>(PType->getCanonicalTypeInternal())) {
47425e530af5d51572a0ed5dbe50da54bd333840c63dDavid Chisnall      // Use array's original type only if it has known number of
47435389f48b24937ad7b4093307128b3cbf25235654David Chisnall      // elements.
47445389f48b24937ad7b4093307128b3cbf25235654David Chisnall      if (!isa<ConstantArrayType>(AT))
47455389f48b24937ad7b4093307128b3cbf25235654David Chisnall        PType = PVDecl->getType();
47465389f48b24937ad7b4093307128b3cbf25235654David Chisnall    } else if (PType->isFunctionType())
47475389f48b24937ad7b4093307128b3cbf25235654David Chisnall      PType = PVDecl->getType();
47483d145f660a23d5bdabdd1ead83c51f3df1489b09Fariborz Jahanian    if (getLangOpts().EncodeExtendedBlockSig)
474906cffc0511ba76d979fad9ef6944e685936b62f0Fariborz Jahanian      getObjCEncodingForMethodParameter(Decl::OBJC_TQ_None, PType,
475006cffc0511ba76d979fad9ef6944e685936b62f0Fariborz Jahanian                                      S, true /*Extended*/);
475106cffc0511ba76d979fad9ef6944e685936b62f0Fariborz Jahanian    else
475206cffc0511ba76d979fad9ef6944e685936b62f0Fariborz Jahanian      getObjCEncodingForType(PType, S);
47535389f48b24937ad7b4093307128b3cbf25235654David Chisnall    S += charUnitsToString(ParmOffset);
47545389f48b24937ad7b4093307128b3cbf25235654David Chisnall    ParmOffset += getObjCEncodingTypeSize(PType);
47555389f48b24937ad7b4093307128b3cbf25235654David Chisnall  }
47566b5a61b6dc400027fd793dcadceeb9da944a37eaJohn McCall
47576b5a61b6dc400027fd793dcadceeb9da944a37eaJohn McCall  return S;
47585389f48b24937ad7b4093307128b3cbf25235654David Chisnall}
47595389f48b24937ad7b4093307128b3cbf25235654David Chisnall
4760f968d8374791c37bc464efd9168c2d33dd73605fDouglas Gregorbool ASTContext::getObjCEncodingForFunctionDecl(const FunctionDecl *Decl,
47615389f48b24937ad7b4093307128b3cbf25235654David Chisnall                                                std::string& S) {
47625389f48b24937ad7b4093307128b3cbf25235654David Chisnall  // Encode result type.
47635389f48b24937ad7b4093307128b3cbf25235654David Chisnall  getObjCEncodingForType(Decl->getResultType(), S);
47645389f48b24937ad7b4093307128b3cbf25235654David Chisnall  CharUnits ParmOffset;
47655389f48b24937ad7b4093307128b3cbf25235654David Chisnall  // Compute size of all parameters.
47665389f48b24937ad7b4093307128b3cbf25235654David Chisnall  for (FunctionDecl::param_const_iterator PI = Decl->param_begin(),
47675389f48b24937ad7b4093307128b3cbf25235654David Chisnall       E = Decl->param_end(); PI != E; ++PI) {
47685389f48b24937ad7b4093307128b3cbf25235654David Chisnall    QualType PType = (*PI)->getType();
47695389f48b24937ad7b4093307128b3cbf25235654David Chisnall    CharUnits sz = getObjCEncodingTypeSize(PType);
4770f968d8374791c37bc464efd9168c2d33dd73605fDouglas Gregor    if (sz.isZero())
47717e68ba5660a9b2b854df4ae7a556e6b91d738c6cFariborz Jahanian      continue;
47727e68ba5660a9b2b854df4ae7a556e6b91d738c6cFariborz Jahanian
47735389f48b24937ad7b4093307128b3cbf25235654David Chisnall    assert (sz.isPositive() &&
4774f968d8374791c37bc464efd9168c2d33dd73605fDouglas Gregor        "getObjCEncodingForFunctionDecl - Incomplete param type");
47755389f48b24937ad7b4093307128b3cbf25235654David Chisnall    ParmOffset += sz;
47765389f48b24937ad7b4093307128b3cbf25235654David Chisnall  }
47775389f48b24937ad7b4093307128b3cbf25235654David Chisnall  S += charUnitsToString(ParmOffset);
47785389f48b24937ad7b4093307128b3cbf25235654David Chisnall  ParmOffset = CharUnits::Zero();
47795389f48b24937ad7b4093307128b3cbf25235654David Chisnall
47805389f48b24937ad7b4093307128b3cbf25235654David Chisnall  // Argument types.
47815389f48b24937ad7b4093307128b3cbf25235654David Chisnall  for (FunctionDecl::param_const_iterator PI = Decl->param_begin(),
47825389f48b24937ad7b4093307128b3cbf25235654David Chisnall       E = Decl->param_end(); PI != E; ++PI) {
47835389f48b24937ad7b4093307128b3cbf25235654David Chisnall    ParmVarDecl *PVDecl = *PI;
47845389f48b24937ad7b4093307128b3cbf25235654David Chisnall    QualType PType = PVDecl->getOriginalType();
47855389f48b24937ad7b4093307128b3cbf25235654David Chisnall    if (const ArrayType *AT =
47865389f48b24937ad7b4093307128b3cbf25235654David Chisnall          dyn_cast<ArrayType>(PType->getCanonicalTypeInternal())) {
47875389f48b24937ad7b4093307128b3cbf25235654David Chisnall      // Use array's original type only if it has known number of
47885e530af5d51572a0ed5dbe50da54bd333840c63dDavid Chisnall      // elements.
47895e530af5d51572a0ed5dbe50da54bd333840c63dDavid Chisnall      if (!isa<ConstantArrayType>(AT))
47905e530af5d51572a0ed5dbe50da54bd333840c63dDavid Chisnall        PType = PVDecl->getType();
47915e530af5d51572a0ed5dbe50da54bd333840c63dDavid Chisnall    } else if (PType->isFunctionType())
47925e530af5d51572a0ed5dbe50da54bd333840c63dDavid Chisnall      PType = PVDecl->getType();
47935e530af5d51572a0ed5dbe50da54bd333840c63dDavid Chisnall    getObjCEncodingForType(PType, S);
4794199c3d6cd16aebbb9c7f0d42af9d922c9628bf70Ken Dyck    S += charUnitsToString(ParmOffset);
4795aa8741a1db98eef05f09b1200dba94aa5dc3bc3dKen Dyck    ParmOffset += getObjCEncodingTypeSize(PType);
47965e530af5d51572a0ed5dbe50da54bd333840c63dDavid Chisnall  }
4797f968d8374791c37bc464efd9168c2d33dd73605fDouglas Gregor
4798f968d8374791c37bc464efd9168c2d33dd73605fDouglas Gregor  return false;
47995e530af5d51572a0ed5dbe50da54bd333840c63dDavid Chisnall}
48005e530af5d51572a0ed5dbe50da54bd333840c63dDavid Chisnall
4801dc8dab6fabf4bfd4f4b94bf572ac3342a5bbfcd7Bob Wilson/// getObjCEncodingForMethodParameter - Return the encoded type for a single
4802dc8dab6fabf4bfd4f4b94bf572ac3342a5bbfcd7Bob Wilson/// method parameter or return type. If Extended, include class names and
4803dc8dab6fabf4bfd4f4b94bf572ac3342a5bbfcd7Bob Wilson/// block object types.
4804dc8dab6fabf4bfd4f4b94bf572ac3342a5bbfcd7Bob Wilsonvoid ASTContext::getObjCEncodingForMethodParameter(Decl::ObjCDeclQualifier QT,
4805dc8dab6fabf4bfd4f4b94bf572ac3342a5bbfcd7Bob Wilson                                                   QualType T, std::string& S,
4806dc8dab6fabf4bfd4f4b94bf572ac3342a5bbfcd7Bob Wilson                                                   bool Extended) const {
4807dc8dab6fabf4bfd4f4b94bf572ac3342a5bbfcd7Bob Wilson  // Encode type qualifer, 'in', 'inout', etc. for the parameter.
4808dc8dab6fabf4bfd4f4b94bf572ac3342a5bbfcd7Bob Wilson  getObjCEncodingForTypeQualifier(QT, S);
4809dc8dab6fabf4bfd4f4b94bf572ac3342a5bbfcd7Bob Wilson  // Encode parameter type.
4810dc8dab6fabf4bfd4f4b94bf572ac3342a5bbfcd7Bob Wilson  getObjCEncodingForTypeImpl(T, S, true, true, 0,
4811dc8dab6fabf4bfd4f4b94bf572ac3342a5bbfcd7Bob Wilson                             true     /*OutermostType*/,
4812dc8dab6fabf4bfd4f4b94bf572ac3342a5bbfcd7Bob Wilson                             false    /*EncodingProperty*/,
4813dc8dab6fabf4bfd4f4b94bf572ac3342a5bbfcd7Bob Wilson                             false    /*StructField*/,
4814dc8dab6fabf4bfd4f4b94bf572ac3342a5bbfcd7Bob Wilson                             Extended /*EncodeBlockParameters*/,
4815dc8dab6fabf4bfd4f4b94bf572ac3342a5bbfcd7Bob Wilson                             Extended /*EncodeClassNames*/);
4816dc8dab6fabf4bfd4f4b94bf572ac3342a5bbfcd7Bob Wilson}
4817dc8dab6fabf4bfd4f4b94bf572ac3342a5bbfcd7Bob Wilson
4818a526c5c67e5a0473c340903ee542ce570119665fTed Kremenek/// getObjCEncodingForMethodDecl - Return the encoded type for this method
481933e1d64ab5cd5d27f8530ccd056191fe2c9f3f2eFariborz Jahanian/// declaration.
4820f968d8374791c37bc464efd9168c2d33dd73605fDouglas Gregorbool ASTContext::getObjCEncodingForMethodDecl(const ObjCMethodDecl *Decl,
4821dc8dab6fabf4bfd4f4b94bf572ac3342a5bbfcd7Bob Wilson                                              std::string& S,
4822dc8dab6fabf4bfd4f4b94bf572ac3342a5bbfcd7Bob Wilson                                              bool Extended) const {
4823c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar  // FIXME: This is not very efficient.
4824dc8dab6fabf4bfd4f4b94bf572ac3342a5bbfcd7Bob Wilson  // Encode return type.
4825dc8dab6fabf4bfd4f4b94bf572ac3342a5bbfcd7Bob Wilson  getObjCEncodingForMethodParameter(Decl->getObjCDeclQualifier(),
4826dc8dab6fabf4bfd4f4b94bf572ac3342a5bbfcd7Bob Wilson                                    Decl->getResultType(), S, Extended);
482733e1d64ab5cd5d27f8530ccd056191fe2c9f3f2eFariborz Jahanian  // Compute size of all parameters.
482833e1d64ab5cd5d27f8530ccd056191fe2c9f3f2eFariborz Jahanian  // Start with computing size of a pointer in number of bytes.
482933e1d64ab5cd5d27f8530ccd056191fe2c9f3f2eFariborz Jahanian  // FIXME: There might(should) be a better way of doing this computation!
483033e1d64ab5cd5d27f8530ccd056191fe2c9f3f2eFariborz Jahanian  SourceLocation Loc;
4831199c3d6cd16aebbb9c7f0d42af9d922c9628bf70Ken Dyck  CharUnits PtrSize = getTypeSizeInChars(VoidPtrTy);
483233e1d64ab5cd5d27f8530ccd056191fe2c9f3f2eFariborz Jahanian  // The first two arguments (self and _cmd) are pointers; account for
483333e1d64ab5cd5d27f8530ccd056191fe2c9f3f2eFariborz Jahanian  // their size.
4834199c3d6cd16aebbb9c7f0d42af9d922c9628bf70Ken Dyck  CharUnits ParmOffset = 2 * PtrSize;
4835491306a83c4f0f49f95a3bcbca8580cb98a91c7aArgyrios Kyrtzidis  for (ObjCMethodDecl::param_const_iterator PI = Decl->param_begin(),
48367732cc9c0fdc97a2f8cce4e5933d8103213d1aefFariborz Jahanian       E = Decl->sel_param_end(); PI != E; ++PI) {
483789951a86b594513c2a013532ed45d197413b1087Chris Lattner    QualType PType = (*PI)->getType();
4838aa8741a1db98eef05f09b1200dba94aa5dc3bc3dKen Dyck    CharUnits sz = getObjCEncodingTypeSize(PType);
4839f968d8374791c37bc464efd9168c2d33dd73605fDouglas Gregor    if (sz.isZero())
48407e68ba5660a9b2b854df4ae7a556e6b91d738c6cFariborz Jahanian      continue;
48417e68ba5660a9b2b854df4ae7a556e6b91d738c6cFariborz Jahanian
4842199c3d6cd16aebbb9c7f0d42af9d922c9628bf70Ken Dyck    assert (sz.isPositive() &&
4843199c3d6cd16aebbb9c7f0d42af9d922c9628bf70Ken Dyck        "getObjCEncodingForMethodDecl - Incomplete param type");
484433e1d64ab5cd5d27f8530ccd056191fe2c9f3f2eFariborz Jahanian    ParmOffset += sz;
484533e1d64ab5cd5d27f8530ccd056191fe2c9f3f2eFariborz Jahanian  }
4846199c3d6cd16aebbb9c7f0d42af9d922c9628bf70Ken Dyck  S += charUnitsToString(ParmOffset);
484733e1d64ab5cd5d27f8530ccd056191fe2c9f3f2eFariborz Jahanian  S += "@0:";
4848199c3d6cd16aebbb9c7f0d42af9d922c9628bf70Ken Dyck  S += charUnitsToString(PtrSize);
48491eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
485033e1d64ab5cd5d27f8530ccd056191fe2c9f3f2eFariborz Jahanian  // Argument types.
485133e1d64ab5cd5d27f8530ccd056191fe2c9f3f2eFariborz Jahanian  ParmOffset = 2 * PtrSize;
4852491306a83c4f0f49f95a3bcbca8580cb98a91c7aArgyrios Kyrtzidis  for (ObjCMethodDecl::param_const_iterator PI = Decl->param_begin(),
48537732cc9c0fdc97a2f8cce4e5933d8103213d1aefFariborz Jahanian       E = Decl->sel_param_end(); PI != E; ++PI) {
4854491306a83c4f0f49f95a3bcbca8580cb98a91c7aArgyrios Kyrtzidis    const ParmVarDecl *PVDecl = *PI;
48551eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    QualType PType = PVDecl->getOriginalType();
48564306d3cb9116605728252e2738df24b9f6ab53c3Fariborz Jahanian    if (const ArrayType *AT =
4857ab76d45e023fc5ae966968344e180cd09fdcc746Steve Naroff          dyn_cast<ArrayType>(PType->getCanonicalTypeInternal())) {
4858ab76d45e023fc5ae966968344e180cd09fdcc746Steve Naroff      // Use array's original type only if it has known number of
4859ab76d45e023fc5ae966968344e180cd09fdcc746Steve Naroff      // elements.
4860bb3fde337fb712c0e6da8790d431621be4793048Steve Naroff      if (!isa<ConstantArrayType>(AT))
4861ab76d45e023fc5ae966968344e180cd09fdcc746Steve Naroff        PType = PVDecl->getType();
4862ab76d45e023fc5ae966968344e180cd09fdcc746Steve Naroff    } else if (PType->isFunctionType())
4863ab76d45e023fc5ae966968344e180cd09fdcc746Steve Naroff      PType = PVDecl->getType();
4864dc8dab6fabf4bfd4f4b94bf572ac3342a5bbfcd7Bob Wilson    getObjCEncodingForMethodParameter(PVDecl->getObjCDeclQualifier(),
4865dc8dab6fabf4bfd4f4b94bf572ac3342a5bbfcd7Bob Wilson                                      PType, S, Extended);
4866199c3d6cd16aebbb9c7f0d42af9d922c9628bf70Ken Dyck    S += charUnitsToString(ParmOffset);
4867aa8741a1db98eef05f09b1200dba94aa5dc3bc3dKen Dyck    ParmOffset += getObjCEncodingTypeSize(PType);
486833e1d64ab5cd5d27f8530ccd056191fe2c9f3f2eFariborz Jahanian  }
4869f968d8374791c37bc464efd9168c2d33dd73605fDouglas Gregor
4870f968d8374791c37bc464efd9168c2d33dd73605fDouglas Gregor  return false;
487133e1d64ab5cd5d27f8530ccd056191fe2c9f3f2eFariborz Jahanian}
487233e1d64ab5cd5d27f8530ccd056191fe2c9f3f2eFariborz Jahanian
4873c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar/// getObjCEncodingForPropertyDecl - Return the encoded type for this
487483bccb85ff4b9981c4250c45494b439df8cbf983Fariborz Jahanian/// property declaration. If non-NULL, Container must be either an
4875c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar/// ObjCCategoryImplDecl or ObjCImplementationDecl; it should only be
4876c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar/// NULL when getting encodings for protocol properties.
48771eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// Property attributes are stored as a comma-delimited C string. The simple
48781eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// attributes readonly and bycopy are encoded as single characters. The
48791eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// parametrized attributes, getter=name, setter=name, and ivar=name, are
48801eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// encoded as single characters, followed by an identifier. Property types
48811eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// are also encoded as a parametrized attribute. The characters used to encode
488283bccb85ff4b9981c4250c45494b439df8cbf983Fariborz Jahanian/// these attributes are defined by the following enumeration:
488383bccb85ff4b9981c4250c45494b439df8cbf983Fariborz Jahanian/// @code
488483bccb85ff4b9981c4250c45494b439df8cbf983Fariborz Jahanian/// enum PropertyAttributes {
488583bccb85ff4b9981c4250c45494b439df8cbf983Fariborz Jahanian/// kPropertyReadOnly = 'R',   // property is read-only.
488683bccb85ff4b9981c4250c45494b439df8cbf983Fariborz Jahanian/// kPropertyBycopy = 'C',     // property is a copy of the value last assigned
488783bccb85ff4b9981c4250c45494b439df8cbf983Fariborz Jahanian/// kPropertyByref = '&',  // property is a reference to the value last assigned
488883bccb85ff4b9981c4250c45494b439df8cbf983Fariborz Jahanian/// kPropertyDynamic = 'D',    // property is dynamic
488983bccb85ff4b9981c4250c45494b439df8cbf983Fariborz Jahanian/// kPropertyGetter = 'G',     // followed by getter selector name
489083bccb85ff4b9981c4250c45494b439df8cbf983Fariborz Jahanian/// kPropertySetter = 'S',     // followed by setter selector name
489183bccb85ff4b9981c4250c45494b439df8cbf983Fariborz Jahanian/// kPropertyInstanceVariable = 'V'  // followed by instance variable  name
48920d4cb85130d91da61c45aecb9fd31c7097a7cfccBob Wilson/// kPropertyType = 'T'              // followed by old-style type encoding.
489383bccb85ff4b9981c4250c45494b439df8cbf983Fariborz Jahanian/// kPropertyWeak = 'W'              // 'weak' property
489483bccb85ff4b9981c4250c45494b439df8cbf983Fariborz Jahanian/// kPropertyStrong = 'P'            // property GC'able
489583bccb85ff4b9981c4250c45494b439df8cbf983Fariborz Jahanian/// kPropertyNonAtomic = 'N'         // property non-atomic
489683bccb85ff4b9981c4250c45494b439df8cbf983Fariborz Jahanian/// };
489783bccb85ff4b9981c4250c45494b439df8cbf983Fariborz Jahanian/// @endcode
48981eb4433ac451dc16f4133a88af2d002ac26c58efMike Stumpvoid ASTContext::getObjCEncodingForPropertyDecl(const ObjCPropertyDecl *PD,
4899c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar                                                const Decl *Container,
49004ba2a17694148e16eaa8d3917f657ffcd3667be4Jay Foad                                                std::string& S) const {
4901c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar  // Collect information from the property implementation decl(s).
4902c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar  bool Dynamic = false;
4903c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar  ObjCPropertyImplDecl *SynthesizePID = 0;
4904c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar
4905c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar  // FIXME: Duplicated code due to poor abstraction.
4906c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar  if (Container) {
49071eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    if (const ObjCCategoryImplDecl *CID =
4908c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar        dyn_cast<ObjCCategoryImplDecl>(Container)) {
4909c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar      for (ObjCCategoryImplDecl::propimpl_iterator
491017945a0f64fe03ff6ec0c2146005a87636e3ac12Argyrios Kyrtzidis             i = CID->propimpl_begin(), e = CID->propimpl_end();
4911653f1b1bf293a9bd96fd4dd6372e779cc7af1597Douglas Gregor           i != e; ++i) {
4912581deb3da481053c4993c7600f97acf7768caac5David Blaikie        ObjCPropertyImplDecl *PID = *i;
4913c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar        if (PID->getPropertyDecl() == PD) {
4914c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar          if (PID->getPropertyImplementation()==ObjCPropertyImplDecl::Dynamic) {
4915c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar            Dynamic = true;
4916c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar          } else {
4917c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar            SynthesizePID = PID;
4918c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar          }
4919c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar        }
4920c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar      }
4921c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar    } else {
492261710854be2b098428aff5316e64bd34b30fbcb7Chris Lattner      const ObjCImplementationDecl *OID=cast<ObjCImplementationDecl>(Container);
4923c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar      for (ObjCCategoryImplDecl::propimpl_iterator
492417945a0f64fe03ff6ec0c2146005a87636e3ac12Argyrios Kyrtzidis             i = OID->propimpl_begin(), e = OID->propimpl_end();
4925653f1b1bf293a9bd96fd4dd6372e779cc7af1597Douglas Gregor           i != e; ++i) {
4926581deb3da481053c4993c7600f97acf7768caac5David Blaikie        ObjCPropertyImplDecl *PID = *i;
4927c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar        if (PID->getPropertyDecl() == PD) {
4928c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar          if (PID->getPropertyImplementation()==ObjCPropertyImplDecl::Dynamic) {
4929c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar            Dynamic = true;
4930c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar          } else {
4931c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar            SynthesizePID = PID;
4932c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar          }
4933c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar        }
49341eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      }
4935c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar    }
4936c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar  }
4937c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar
4938c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar  // FIXME: This is not very efficient.
4939c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar  S = "T";
4940c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar
4941c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar  // Encode result type.
4942090b3f71702c5626d8520f9608d77c6f26dcfa15Fariborz Jahanian  // GCC has some special rules regarding encoding of properties which
4943090b3f71702c5626d8520f9608d77c6f26dcfa15Fariborz Jahanian  // closely resembles encoding of ivars.
49441eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  getObjCEncodingForTypeImpl(PD->getType(), S, true, true, 0,
4945090b3f71702c5626d8520f9608d77c6f26dcfa15Fariborz Jahanian                             true /* outermost type */,
4946090b3f71702c5626d8520f9608d77c6f26dcfa15Fariborz Jahanian                             true /* encoding for property */);
4947c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar
4948c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar  if (PD->isReadOnly()) {
4949c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar    S += ",R";
4950d7ceab3f311418fd77c2899e8fba254402fd0b5aNico Weber    if (PD->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_copy)
4951d7ceab3f311418fd77c2899e8fba254402fd0b5aNico Weber      S += ",C";
4952d7ceab3f311418fd77c2899e8fba254402fd0b5aNico Weber    if (PD->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_retain)
4953d7ceab3f311418fd77c2899e8fba254402fd0b5aNico Weber      S += ",&";
4954c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar  } else {
4955c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar    switch (PD->getSetterKind()) {
4956c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar    case ObjCPropertyDecl::Assign: break;
4957c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar    case ObjCPropertyDecl::Copy:   S += ",C"; break;
49581eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    case ObjCPropertyDecl::Retain: S += ",&"; break;
49593a02b44e3948f7762dbfba94b7961281ca29d022Fariborz Jahanian    case ObjCPropertyDecl::Weak:   S += ",W"; break;
4960c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar    }
4961c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar  }
4962c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar
4963c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar  // It really isn't clear at all what this means, since properties
4964c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar  // are "dynamic by default".
4965c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar  if (Dynamic)
4966c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar    S += ",D";
4967c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar
4968090b3f71702c5626d8520f9608d77c6f26dcfa15Fariborz Jahanian  if (PD->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_nonatomic)
4969090b3f71702c5626d8520f9608d77c6f26dcfa15Fariborz Jahanian    S += ",N";
49701eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
4971c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar  if (PD->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_getter) {
4972c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar    S += ",G";
4973077bf5e2f48acfa9e7d69429b6e4ba86ea14896dChris Lattner    S += PD->getGetterName().getAsString();
4974c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar  }
4975c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar
4976c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar  if (PD->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_setter) {
4977c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar    S += ",S";
4978077bf5e2f48acfa9e7d69429b6e4ba86ea14896dChris Lattner    S += PD->getSetterName().getAsString();
4979c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar  }
4980c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar
4981c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar  if (SynthesizePID) {
4982c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar    const ObjCIvarDecl *OID = SynthesizePID->getPropertyIvarDecl();
4983c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar    S += ",V";
498439f34e97d6a468f0a7dfa5664c61217cffc65b74Chris Lattner    S += OID->getNameAsString();
4985c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar  }
4986c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar
4987c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar  // FIXME: OBJCGC: weak & strong
4988c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar}
4989c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar
4990a1c033e9514865f3a7b0d8b3b20e6de926cfec6cFariborz Jahanian/// getLegacyIntegralTypeEncoding -
49911eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// Another legacy compatibility encoding: 32-bit longs are encoded as
49921eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// 'l' or 'L' , but not always.  For typedefs, we need to use
4993a1c033e9514865f3a7b0d8b3b20e6de926cfec6cFariborz Jahanian/// 'i' or 'I' instead if encoding a struct field, or a pointer!
4994a1c033e9514865f3a7b0d8b3b20e6de926cfec6cFariborz Jahanian///
4995a1c033e9514865f3a7b0d8b3b20e6de926cfec6cFariborz Jahanianvoid ASTContext::getLegacyIntegralTypeEncoding (QualType &PointeeTy) const {
49968e1fab243ab8023b7ee3899745386b3b3a4258f8Mike Stump  if (isa<TypedefType>(PointeeTy.getTypePtr())) {
4997183700f494ec9b6701b6efe82bcb25f4c79ba561John McCall    if (const BuiltinType *BT = PointeeTy->getAs<BuiltinType>()) {
49984ba2a17694148e16eaa8d3917f657ffcd3667be4Jay Foad      if (BT->getKind() == BuiltinType::ULong && getIntWidth(PointeeTy) == 32)
4999a1c033e9514865f3a7b0d8b3b20e6de926cfec6cFariborz Jahanian        PointeeTy = UnsignedIntTy;
50001eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      else
50014ba2a17694148e16eaa8d3917f657ffcd3667be4Jay Foad        if (BT->getKind() == BuiltinType::Long && getIntWidth(PointeeTy) == 32)
5002a1c033e9514865f3a7b0d8b3b20e6de926cfec6cFariborz Jahanian          PointeeTy = IntTy;
5003a1c033e9514865f3a7b0d8b3b20e6de926cfec6cFariborz Jahanian    }
5004a1c033e9514865f3a7b0d8b3b20e6de926cfec6cFariborz Jahanian  }
5005a1c033e9514865f3a7b0d8b3b20e6de926cfec6cFariborz Jahanian}
5006a1c033e9514865f3a7b0d8b3b20e6de926cfec6cFariborz Jahanian
50077d6b46d9a9d75dea8ef9f6973dd50633c1f37963Fariborz Jahanianvoid ASTContext::getObjCEncodingForType(QualType T, std::string& S,
50084ba2a17694148e16eaa8d3917f657ffcd3667be4Jay Foad                                        const FieldDecl *Field) const {
500982a6cfbc421cc99c5b7313271f399f7ef95056ecDaniel Dunbar  // We follow the behavior of gcc, expanding structures which are
501082a6cfbc421cc99c5b7313271f399f7ef95056ecDaniel Dunbar  // directly pointed to, and expanding embedded structures. Note that
501182a6cfbc421cc99c5b7313271f399f7ef95056ecDaniel Dunbar  // these rules are sufficient to prevent recursive encoding of the
501282a6cfbc421cc99c5b7313271f399f7ef95056ecDaniel Dunbar  // same type.
50131eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  getObjCEncodingForTypeImpl(T, S, true, true, Field,
50145b8c7d9fb620ba3a71e996d61e7b9bdf763b5c09Fariborz Jahanian                             true /* outermost type */);
501582a6cfbc421cc99c5b7313271f399f7ef95056ecDaniel Dunbar}
501682a6cfbc421cc99c5b7313271f399f7ef95056ecDaniel Dunbar
50173624e9e55d98507863d0731a0942cd874f2a1574John McCallstatic char getObjCEncodingForPrimitiveKind(const ASTContext *C,
50183624e9e55d98507863d0731a0942cd874f2a1574John McCall                                            BuiltinType::Kind kind) {
50193624e9e55d98507863d0731a0942cd874f2a1574John McCall    switch (kind) {
502064fd7e86c1a90d9ff78e4a0bd79f69499667a4e3David Chisnall    case BuiltinType::Void:       return 'v';
502164fd7e86c1a90d9ff78e4a0bd79f69499667a4e3David Chisnall    case BuiltinType::Bool:       return 'B';
502264fd7e86c1a90d9ff78e4a0bd79f69499667a4e3David Chisnall    case BuiltinType::Char_U:
502364fd7e86c1a90d9ff78e4a0bd79f69499667a4e3David Chisnall    case BuiltinType::UChar:      return 'C';
50243624e9e55d98507863d0731a0942cd874f2a1574John McCall    case BuiltinType::Char16:
502564fd7e86c1a90d9ff78e4a0bd79f69499667a4e3David Chisnall    case BuiltinType::UShort:     return 'S';
50263624e9e55d98507863d0731a0942cd874f2a1574John McCall    case BuiltinType::Char32:
502764fd7e86c1a90d9ff78e4a0bd79f69499667a4e3David Chisnall    case BuiltinType::UInt:       return 'I';
502864fd7e86c1a90d9ff78e4a0bd79f69499667a4e3David Chisnall    case BuiltinType::ULong:
50293624e9e55d98507863d0731a0942cd874f2a1574John McCall        return C->getTargetInfo().getLongWidth() == 32 ? 'L' : 'Q';
503064fd7e86c1a90d9ff78e4a0bd79f69499667a4e3David Chisnall    case BuiltinType::UInt128:    return 'T';
503164fd7e86c1a90d9ff78e4a0bd79f69499667a4e3David Chisnall    case BuiltinType::ULongLong:  return 'Q';
503264fd7e86c1a90d9ff78e4a0bd79f69499667a4e3David Chisnall    case BuiltinType::Char_S:
503364fd7e86c1a90d9ff78e4a0bd79f69499667a4e3David Chisnall    case BuiltinType::SChar:      return 'c';
503464fd7e86c1a90d9ff78e4a0bd79f69499667a4e3David Chisnall    case BuiltinType::Short:      return 's';
50353f59c975aa5d047f7edd1b900b5e885c38af0ef7Chris Lattner    case BuiltinType::WChar_S:
50363f59c975aa5d047f7edd1b900b5e885c38af0ef7Chris Lattner    case BuiltinType::WChar_U:
503764fd7e86c1a90d9ff78e4a0bd79f69499667a4e3David Chisnall    case BuiltinType::Int:        return 'i';
503864fd7e86c1a90d9ff78e4a0bd79f69499667a4e3David Chisnall    case BuiltinType::Long:
50393624e9e55d98507863d0731a0942cd874f2a1574John McCall      return C->getTargetInfo().getLongWidth() == 32 ? 'l' : 'q';
504064fd7e86c1a90d9ff78e4a0bd79f69499667a4e3David Chisnall    case BuiltinType::LongLong:   return 'q';
504164fd7e86c1a90d9ff78e4a0bd79f69499667a4e3David Chisnall    case BuiltinType::Int128:     return 't';
504264fd7e86c1a90d9ff78e4a0bd79f69499667a4e3David Chisnall    case BuiltinType::Float:      return 'f';
504364fd7e86c1a90d9ff78e4a0bd79f69499667a4e3David Chisnall    case BuiltinType::Double:     return 'd';
50443a0be84b2aed8563150cdbd976a98838afa261ebDaniel Dunbar    case BuiltinType::LongDouble: return 'D';
50453624e9e55d98507863d0731a0942cd874f2a1574John McCall    case BuiltinType::NullPtr:    return '*'; // like char*
50463624e9e55d98507863d0731a0942cd874f2a1574John McCall
50473624e9e55d98507863d0731a0942cd874f2a1574John McCall    case BuiltinType::Half:
50483624e9e55d98507863d0731a0942cd874f2a1574John McCall      // FIXME: potentially need @encodes for these!
50493624e9e55d98507863d0731a0942cd874f2a1574John McCall      return ' ';
50503624e9e55d98507863d0731a0942cd874f2a1574John McCall
50513624e9e55d98507863d0731a0942cd874f2a1574John McCall    case BuiltinType::ObjCId:
50523624e9e55d98507863d0731a0942cd874f2a1574John McCall    case BuiltinType::ObjCClass:
50533624e9e55d98507863d0731a0942cd874f2a1574John McCall    case BuiltinType::ObjCSel:
50543624e9e55d98507863d0731a0942cd874f2a1574John McCall      llvm_unreachable("@encoding ObjC primitive type");
50553624e9e55d98507863d0731a0942cd874f2a1574John McCall
50563624e9e55d98507863d0731a0942cd874f2a1574John McCall    // OpenCL and placeholder types don't need @encodings.
50573624e9e55d98507863d0731a0942cd874f2a1574John McCall    case BuiltinType::OCLImage1d:
50583624e9e55d98507863d0731a0942cd874f2a1574John McCall    case BuiltinType::OCLImage1dArray:
50593624e9e55d98507863d0731a0942cd874f2a1574John McCall    case BuiltinType::OCLImage1dBuffer:
50603624e9e55d98507863d0731a0942cd874f2a1574John McCall    case BuiltinType::OCLImage2d:
50613624e9e55d98507863d0731a0942cd874f2a1574John McCall    case BuiltinType::OCLImage2dArray:
50623624e9e55d98507863d0731a0942cd874f2a1574John McCall    case BuiltinType::OCLImage3d:
5063e6b9d802fb7b16d93474c4f1c179ab36202e8a8bGuy Benyei    case BuiltinType::OCLEvent:
506421f18c4fda167dc5f72feddbd6a7ac1b63200a0dGuy Benyei    case BuiltinType::OCLSampler:
50653624e9e55d98507863d0731a0942cd874f2a1574John McCall    case BuiltinType::Dependent:
50663624e9e55d98507863d0731a0942cd874f2a1574John McCall#define BUILTIN_TYPE(KIND, ID)
50673624e9e55d98507863d0731a0942cd874f2a1574John McCall#define PLACEHOLDER_TYPE(KIND, ID) \
50683624e9e55d98507863d0731a0942cd874f2a1574John McCall    case BuiltinType::KIND:
50693624e9e55d98507863d0731a0942cd874f2a1574John McCall#include "clang/AST/BuiltinTypes.def"
50703624e9e55d98507863d0731a0942cd874f2a1574John McCall      llvm_unreachable("invalid builtin type for @encode");
507164fd7e86c1a90d9ff78e4a0bd79f69499667a4e3David Chisnall    }
5072719e53f64637c5ac41aac0c711e1977487ffc0d7David Blaikie    llvm_unreachable("invalid BuiltinType::Kind value");
507364fd7e86c1a90d9ff78e4a0bd79f69499667a4e3David Chisnall}
507464fd7e86c1a90d9ff78e4a0bd79f69499667a4e3David Chisnall
50755471bc85b69912e3b448de004498a80c0de32296Douglas Gregorstatic char ObjCEncodingForEnumType(const ASTContext *C, const EnumType *ET) {
50765471bc85b69912e3b448de004498a80c0de32296Douglas Gregor  EnumDecl *Enum = ET->getDecl();
50775471bc85b69912e3b448de004498a80c0de32296Douglas Gregor
50785471bc85b69912e3b448de004498a80c0de32296Douglas Gregor  // The encoding of an non-fixed enum type is always 'i', regardless of size.
50795471bc85b69912e3b448de004498a80c0de32296Douglas Gregor  if (!Enum->isFixed())
50805471bc85b69912e3b448de004498a80c0de32296Douglas Gregor    return 'i';
50815471bc85b69912e3b448de004498a80c0de32296Douglas Gregor
50825471bc85b69912e3b448de004498a80c0de32296Douglas Gregor  // The encoding of a fixed enum type matches its fixed underlying type.
50833624e9e55d98507863d0731a0942cd874f2a1574John McCall  const BuiltinType *BT = Enum->getIntegerType()->castAs<BuiltinType>();
50843624e9e55d98507863d0731a0942cd874f2a1574John McCall  return getObjCEncodingForPrimitiveKind(C, BT->getKind());
50855471bc85b69912e3b448de004498a80c0de32296Douglas Gregor}
50865471bc85b69912e3b448de004498a80c0de32296Douglas Gregor
50874ba2a17694148e16eaa8d3917f657ffcd3667be4Jay Foadstatic void EncodeBitField(const ASTContext *Ctx, std::string& S,
508864fd7e86c1a90d9ff78e4a0bd79f69499667a4e3David Chisnall                           QualType T, const FieldDecl *FD) {
5089a6b8b2c09610b8bc4330e948ece8b940c2386406Richard Smith  assert(FD->isBitField() && "not a bitfield - getObjCEncodingForTypeImpl");
50908b4bf90eb6d3d08cf3bfb86705f0fdb20b9c5875Fariborz Jahanian  S += 'b';
509164fd7e86c1a90d9ff78e4a0bd79f69499667a4e3David Chisnall  // The NeXT runtime encodes bit fields as b followed by the number of bits.
509264fd7e86c1a90d9ff78e4a0bd79f69499667a4e3David Chisnall  // The GNU runtime requires more information; bitfields are encoded as b,
509364fd7e86c1a90d9ff78e4a0bd79f69499667a4e3David Chisnall  // then the offset (in bits) of the first element, then the type of the
509464fd7e86c1a90d9ff78e4a0bd79f69499667a4e3David Chisnall  // bitfield, then the size in bits.  For example, in this structure:
509564fd7e86c1a90d9ff78e4a0bd79f69499667a4e3David Chisnall  //
509664fd7e86c1a90d9ff78e4a0bd79f69499667a4e3David Chisnall  // struct
509764fd7e86c1a90d9ff78e4a0bd79f69499667a4e3David Chisnall  // {
509864fd7e86c1a90d9ff78e4a0bd79f69499667a4e3David Chisnall  //    int integer;
509964fd7e86c1a90d9ff78e4a0bd79f69499667a4e3David Chisnall  //    int flags:2;
510064fd7e86c1a90d9ff78e4a0bd79f69499667a4e3David Chisnall  // };
510164fd7e86c1a90d9ff78e4a0bd79f69499667a4e3David Chisnall  // On a 32-bit system, the encoding for flags would be b2 for the NeXT
510264fd7e86c1a90d9ff78e4a0bd79f69499667a4e3David Chisnall  // runtime, but b32i2 for the GNU runtime.  The reason for this extra
510364fd7e86c1a90d9ff78e4a0bd79f69499667a4e3David Chisnall  // information is not especially sensible, but we're stuck with it for
510464fd7e86c1a90d9ff78e4a0bd79f69499667a4e3David Chisnall  // compatibility with GCC, although providing it breaks anything that
510564fd7e86c1a90d9ff78e4a0bd79f69499667a4e3David Chisnall  // actually uses runtime introspection and wants to work on both runtimes...
5106260611a32535c851237926bfcf78869b13c07d5bJohn McCall  if (Ctx->getLangOpts().ObjCRuntime.isGNUFamily()) {
510764fd7e86c1a90d9ff78e4a0bd79f69499667a4e3David Chisnall    const RecordDecl *RD = FD->getParent();
510864fd7e86c1a90d9ff78e4a0bd79f69499667a4e3David Chisnall    const ASTRecordLayout &RL = Ctx->getASTRecordLayout(RD);
510982905749d5c8d8b4edec11de754a73349cb96603Eli Friedman    S += llvm::utostr(RL.getFieldOffset(FD->getFieldIndex()));
51105471bc85b69912e3b448de004498a80c0de32296Douglas Gregor    if (const EnumType *ET = T->getAs<EnumType>())
51115471bc85b69912e3b448de004498a80c0de32296Douglas Gregor      S += ObjCEncodingForEnumType(Ctx, ET);
51123624e9e55d98507863d0731a0942cd874f2a1574John McCall    else {
51133624e9e55d98507863d0731a0942cd874f2a1574John McCall      const BuiltinType *BT = T->castAs<BuiltinType>();
51143624e9e55d98507863d0731a0942cd874f2a1574John McCall      S += getObjCEncodingForPrimitiveKind(Ctx, BT->getKind());
51153624e9e55d98507863d0731a0942cd874f2a1574John McCall    }
511664fd7e86c1a90d9ff78e4a0bd79f69499667a4e3David Chisnall  }
5117a6b8b2c09610b8bc4330e948ece8b940c2386406Richard Smith  S += llvm::utostr(FD->getBitWidthValue(*Ctx));
51188b4bf90eb6d3d08cf3bfb86705f0fdb20b9c5875Fariborz Jahanian}
51198b4bf90eb6d3d08cf3bfb86705f0fdb20b9c5875Fariborz Jahanian
512001eb9b9683535d8a65c704ad2c545903409e2d36Daniel Dunbar// FIXME: Use SmallString for accumulating string.
512182a6cfbc421cc99c5b7313271f399f7ef95056ecDaniel Dunbarvoid ASTContext::getObjCEncodingForTypeImpl(QualType T, std::string& S,
512282a6cfbc421cc99c5b7313271f399f7ef95056ecDaniel Dunbar                                            bool ExpandPointedToStructures,
512382a6cfbc421cc99c5b7313271f399f7ef95056ecDaniel Dunbar                                            bool ExpandStructures,
5124153bfe5795e2c1a5a738e73d3784964e082237fcDaniel Dunbar                                            const FieldDecl *FD,
5125090b3f71702c5626d8520f9608d77c6f26dcfa15Fariborz Jahanian                                            bool OutermostType,
51262636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis                                            bool EncodingProperty,
5127dc8dab6fabf4bfd4f4b94bf572ac3342a5bbfcd7Bob Wilson                                            bool StructField,
5128dc8dab6fabf4bfd4f4b94bf572ac3342a5bbfcd7Bob Wilson                                            bool EncodeBlockParameters,
512917c1a2e748d49d293c9926eeeb85aa4890182106Fariborz Jahanian                                            bool EncodeClassNames,
513017c1a2e748d49d293c9926eeeb85aa4890182106Fariborz Jahanian                                            bool EncodePointerToObjCTypedef) const {
51313624e9e55d98507863d0731a0942cd874f2a1574John McCall  CanQualType CT = getCanonicalType(T);
51323624e9e55d98507863d0731a0942cd874f2a1574John McCall  switch (CT->getTypeClass()) {
51333624e9e55d98507863d0731a0942cd874f2a1574John McCall  case Type::Builtin:
51343624e9e55d98507863d0731a0942cd874f2a1574John McCall  case Type::Enum:
5135ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner    if (FD && FD->isBitField())
513664fd7e86c1a90d9ff78e4a0bd79f69499667a4e3David Chisnall      return EncodeBitField(this, S, T, FD);
51373624e9e55d98507863d0731a0942cd874f2a1574John McCall    if (const BuiltinType *BT = dyn_cast<BuiltinType>(CT))
51383624e9e55d98507863d0731a0942cd874f2a1574John McCall      S += getObjCEncodingForPrimitiveKind(this, BT->getKind());
51393624e9e55d98507863d0731a0942cd874f2a1574John McCall    else
51403624e9e55d98507863d0731a0942cd874f2a1574John McCall      S += ObjCEncodingForEnumType(this, cast<EnumType>(CT));
5141ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner    return;
51421eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
51433624e9e55d98507863d0731a0942cd874f2a1574John McCall  case Type::Complex: {
51443624e9e55d98507863d0731a0942cd874f2a1574John McCall    const ComplexType *CT = T->castAs<ComplexType>();
5145c612f7bc9a6379cd7e7c2dd306d05938e890051bAnders Carlsson    S += 'j';
51461eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    getObjCEncodingForTypeImpl(CT->getElementType(), S, false, false, 0, false,
5147c612f7bc9a6379cd7e7c2dd306d05938e890051bAnders Carlsson                               false);
5148ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner    return;
5149ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner  }
51503624e9e55d98507863d0731a0942cd874f2a1574John McCall
51513624e9e55d98507863d0731a0942cd874f2a1574John McCall  case Type::Atomic: {
51523624e9e55d98507863d0731a0942cd874f2a1574John McCall    const AtomicType *AT = T->castAs<AtomicType>();
51533624e9e55d98507863d0731a0942cd874f2a1574John McCall    S += 'A';
51543624e9e55d98507863d0731a0942cd874f2a1574John McCall    getObjCEncodingForTypeImpl(AT->getValueType(), S, false, false, 0,
51553624e9e55d98507863d0731a0942cd874f2a1574John McCall                               false, false);
51563624e9e55d98507863d0731a0942cd874f2a1574John McCall    return;
5157aa1d76163e4b0b1cc54e222be67379f8c02e8ffaFariborz Jahanian  }
51583624e9e55d98507863d0731a0942cd874f2a1574John McCall
51593624e9e55d98507863d0731a0942cd874f2a1574John McCall  // encoding for pointer or reference types.
51603624e9e55d98507863d0731a0942cd874f2a1574John McCall  case Type::Pointer:
51613624e9e55d98507863d0731a0942cd874f2a1574John McCall  case Type::LValueReference:
51623624e9e55d98507863d0731a0942cd874f2a1574John McCall  case Type::RValueReference: {
51633624e9e55d98507863d0731a0942cd874f2a1574John McCall    QualType PointeeTy;
51643624e9e55d98507863d0731a0942cd874f2a1574John McCall    if (isa<PointerType>(CT)) {
51653624e9e55d98507863d0731a0942cd874f2a1574John McCall      const PointerType *PT = T->castAs<PointerType>();
51663624e9e55d98507863d0731a0942cd874f2a1574John McCall      if (PT->isObjCSelType()) {
51673624e9e55d98507863d0731a0942cd874f2a1574John McCall        S += ':';
51683624e9e55d98507863d0731a0942cd874f2a1574John McCall        return;
51693624e9e55d98507863d0731a0942cd874f2a1574John McCall      }
51703624e9e55d98507863d0731a0942cd874f2a1574John McCall      PointeeTy = PT->getPointeeType();
51713624e9e55d98507863d0731a0942cd874f2a1574John McCall    } else {
51723624e9e55d98507863d0731a0942cd874f2a1574John McCall      PointeeTy = T->castAs<ReferenceType>()->getPointeeType();
51733624e9e55d98507863d0731a0942cd874f2a1574John McCall    }
51743624e9e55d98507863d0731a0942cd874f2a1574John McCall
5175a1c033e9514865f3a7b0d8b3b20e6de926cfec6cFariborz Jahanian    bool isReadOnly = false;
5176a1c033e9514865f3a7b0d8b3b20e6de926cfec6cFariborz Jahanian    // For historical/compatibility reasons, the read-only qualifier of the
5177a1c033e9514865f3a7b0d8b3b20e6de926cfec6cFariborz Jahanian    // pointee gets emitted _before_ the '^'.  The read-only qualifier of
5178a1c033e9514865f3a7b0d8b3b20e6de926cfec6cFariborz Jahanian    // the pointer itself gets ignored, _unless_ we are looking at a typedef!
51791eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    // Also, do not emit the 'r' for anything but the outermost type!
51808e1fab243ab8023b7ee3899745386b3b3a4258f8Mike Stump    if (isa<TypedefType>(T.getTypePtr())) {
5181a1c033e9514865f3a7b0d8b3b20e6de926cfec6cFariborz Jahanian      if (OutermostType && T.isConstQualified()) {
5182a1c033e9514865f3a7b0d8b3b20e6de926cfec6cFariborz Jahanian        isReadOnly = true;
5183a1c033e9514865f3a7b0d8b3b20e6de926cfec6cFariborz Jahanian        S += 'r';
5184a1c033e9514865f3a7b0d8b3b20e6de926cfec6cFariborz Jahanian      }
51859fdbab3cbc2fc04bcaf5768023d83707f3151144Mike Stump    } else if (OutermostType) {
5186a1c033e9514865f3a7b0d8b3b20e6de926cfec6cFariborz Jahanian      QualType P = PointeeTy;
51876217b80b7a1379b74cced1c076338262c3c980b3Ted Kremenek      while (P->getAs<PointerType>())
51886217b80b7a1379b74cced1c076338262c3c980b3Ted Kremenek        P = P->getAs<PointerType>()->getPointeeType();
5189a1c033e9514865f3a7b0d8b3b20e6de926cfec6cFariborz Jahanian      if (P.isConstQualified()) {
5190a1c033e9514865f3a7b0d8b3b20e6de926cfec6cFariborz Jahanian        isReadOnly = true;
5191a1c033e9514865f3a7b0d8b3b20e6de926cfec6cFariborz Jahanian        S += 'r';
5192a1c033e9514865f3a7b0d8b3b20e6de926cfec6cFariborz Jahanian      }
5193a1c033e9514865f3a7b0d8b3b20e6de926cfec6cFariborz Jahanian    }
5194a1c033e9514865f3a7b0d8b3b20e6de926cfec6cFariborz Jahanian    if (isReadOnly) {
5195a1c033e9514865f3a7b0d8b3b20e6de926cfec6cFariborz Jahanian      // Another legacy compatibility encoding. Some ObjC qualifier and type
5196a1c033e9514865f3a7b0d8b3b20e6de926cfec6cFariborz Jahanian      // combinations need to be rearranged.
5197a1c033e9514865f3a7b0d8b3b20e6de926cfec6cFariborz Jahanian      // Rewrite "in const" from "nr" to "rn"
51985f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner      if (StringRef(S).endswith("nr"))
51990237941e0beb0c929934b66ad29443b484d987feBenjamin Kramer        S.replace(S.end()-2, S.end(), "rn");
5200a1c033e9514865f3a7b0d8b3b20e6de926cfec6cFariborz Jahanian    }
52011eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
520285f9bceab1542aafff012d4d28e998f4ba16e362Anders Carlsson    if (PointeeTy->isCharType()) {
520385f9bceab1542aafff012d4d28e998f4ba16e362Anders Carlsson      // char pointer types should be encoded as '*' unless it is a
520485f9bceab1542aafff012d4d28e998f4ba16e362Anders Carlsson      // type that has been typedef'd to 'BOOL'.
5205e8c49533521c40643653f943d47229e62d277f88Anders Carlsson      if (!isTypeTypedefedAsBOOL(PointeeTy)) {
520685f9bceab1542aafff012d4d28e998f4ba16e362Anders Carlsson        S += '*';
520785f9bceab1542aafff012d4d28e998f4ba16e362Anders Carlsson        return;
520885f9bceab1542aafff012d4d28e998f4ba16e362Anders Carlsson      }
52096217b80b7a1379b74cced1c076338262c3c980b3Ted Kremenek    } else if (const RecordType *RTy = PointeeTy->getAs<RecordType>()) {
52109533a7fdb8397421f3be52e879442460a87389f6Steve Naroff      // GCC binary compat: Need to convert "struct objc_class *" to "#".
52119533a7fdb8397421f3be52e879442460a87389f6Steve Naroff      if (RTy->getDecl()->getIdentifier() == &Idents.get("objc_class")) {
52129533a7fdb8397421f3be52e879442460a87389f6Steve Naroff        S += '#';
52139533a7fdb8397421f3be52e879442460a87389f6Steve Naroff        return;
52149533a7fdb8397421f3be52e879442460a87389f6Steve Naroff      }
52159533a7fdb8397421f3be52e879442460a87389f6Steve Naroff      // GCC binary compat: Need to convert "struct objc_object *" to "@".
52169533a7fdb8397421f3be52e879442460a87389f6Steve Naroff      if (RTy->getDecl()->getIdentifier() == &Idents.get("objc_object")) {
52179533a7fdb8397421f3be52e879442460a87389f6Steve Naroff        S += '@';
52189533a7fdb8397421f3be52e879442460a87389f6Steve Naroff        return;
52199533a7fdb8397421f3be52e879442460a87389f6Steve Naroff      }
52209533a7fdb8397421f3be52e879442460a87389f6Steve Naroff      // fall through...
522185f9bceab1542aafff012d4d28e998f4ba16e362Anders Carlsson    }
522285f9bceab1542aafff012d4d28e998f4ba16e362Anders Carlsson    S += '^';
5223a1c033e9514865f3a7b0d8b3b20e6de926cfec6cFariborz Jahanian    getLegacyIntegralTypeEncoding(PointeeTy);
5224a1c033e9514865f3a7b0d8b3b20e6de926cfec6cFariborz Jahanian
52251eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    getObjCEncodingForTypeImpl(PointeeTy, S, false, ExpandPointedToStructures,
522643822eaeddeaa419b90f23c68af6b23c46788a58Fariborz Jahanian                               NULL);
5227ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner    return;
5228ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner  }
52293624e9e55d98507863d0731a0942cd874f2a1574John McCall
52303624e9e55d98507863d0731a0942cd874f2a1574John McCall  case Type::ConstantArray:
52313624e9e55d98507863d0731a0942cd874f2a1574John McCall  case Type::IncompleteArray:
52323624e9e55d98507863d0731a0942cd874f2a1574John McCall  case Type::VariableArray: {
52333624e9e55d98507863d0731a0942cd874f2a1574John McCall    const ArrayType *AT = cast<ArrayType>(CT);
52343624e9e55d98507863d0731a0942cd874f2a1574John McCall
52352636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis    if (isa<IncompleteArrayType>(AT) && !StructField) {
5236559a83330416affb0e341a2c53800cbf924a5178Anders Carlsson      // Incomplete arrays are encoded as a pointer to the array element.
5237559a83330416affb0e341a2c53800cbf924a5178Anders Carlsson      S += '^';
5238559a83330416affb0e341a2c53800cbf924a5178Anders Carlsson
52391eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      getObjCEncodingForTypeImpl(AT->getElementType(), S,
5240559a83330416affb0e341a2c53800cbf924a5178Anders Carlsson                                 false, ExpandStructures, FD);
5241559a83330416affb0e341a2c53800cbf924a5178Anders Carlsson    } else {
5242559a83330416affb0e341a2c53800cbf924a5178Anders Carlsson      S += '[';
52431eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
52442636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis      if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(AT)) {
52452636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis        if (getTypeSize(CAT->getElementType()) == 0)
52462636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis          S += '0';
52472636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis        else
52482636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis          S += llvm::utostr(CAT->getSize().getZExtValue());
52492636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis      } else {
5250559a83330416affb0e341a2c53800cbf924a5178Anders Carlsson        //Variable length arrays are encoded as a regular array with 0 elements.
52512636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis        assert((isa<VariableArrayType>(AT) || isa<IncompleteArrayType>(AT)) &&
52522636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis               "Unknown array type!");
5253559a83330416affb0e341a2c53800cbf924a5178Anders Carlsson        S += '0';
5254559a83330416affb0e341a2c53800cbf924a5178Anders Carlsson      }
52551eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
52561eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      getObjCEncodingForTypeImpl(AT->getElementType(), S,
5257559a83330416affb0e341a2c53800cbf924a5178Anders Carlsson                                 false, ExpandStructures, FD);
5258559a83330416affb0e341a2c53800cbf924a5178Anders Carlsson      S += ']';
5259559a83330416affb0e341a2c53800cbf924a5178Anders Carlsson    }
5260ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner    return;
5261ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner  }
52621eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
52633624e9e55d98507863d0731a0942cd874f2a1574John McCall  case Type::FunctionNoProto:
52643624e9e55d98507863d0731a0942cd874f2a1574John McCall  case Type::FunctionProto:
5265c0a87b7db06643178ad2cbce0767548c139ea387Anders Carlsson    S += '?';
5266ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner    return;
52671eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
52683624e9e55d98507863d0731a0942cd874f2a1574John McCall  case Type::Record: {
52693624e9e55d98507863d0731a0942cd874f2a1574John McCall    RecordDecl *RDecl = cast<RecordType>(CT)->getDecl();
5270d96b35bc6becf8db00d140c11e3d0e53f27567a1Daniel Dunbar    S += RDecl->isUnion() ? '(' : '{';
5271502a4a1ce4c34cf78c8182d9798da0a51d9b7302Daniel Dunbar    // Anonymous structures print as '?'
5272502a4a1ce4c34cf78c8182d9798da0a51d9b7302Daniel Dunbar    if (const IdentifierInfo *II = RDecl->getIdentifier()) {
5273502a4a1ce4c34cf78c8182d9798da0a51d9b7302Daniel Dunbar      S += II->getName();
52746fb94391dc7cb11fd4bbdb969bbab11b6b48c223Fariborz Jahanian      if (ClassTemplateSpecializationDecl *Spec
52756fb94391dc7cb11fd4bbdb969bbab11b6b48c223Fariborz Jahanian          = dyn_cast<ClassTemplateSpecializationDecl>(RDecl)) {
52766fb94391dc7cb11fd4bbdb969bbab11b6b48c223Fariborz Jahanian        const TemplateArgumentList &TemplateArgs = Spec->getTemplateArgs();
52775eada844fa70b6e2bc941dd7306f7a4fb1e8529dBenjamin Kramer        llvm::raw_string_ostream OS(S);
52785eada844fa70b6e2bc941dd7306f7a4fb1e8529dBenjamin Kramer        TemplateSpecializationType::PrintTemplateArgumentList(OS,
5279910f8008fea79120489a53593fe971b0b8a4a740Douglas Gregor                                            TemplateArgs.data(),
5280910f8008fea79120489a53593fe971b0b8a4a740Douglas Gregor                                            TemplateArgs.size(),
528130c42404202d2e2512e51efc6066bd614cfdb5a4Douglas Gregor                                            (*this).getPrintingPolicy());
52826fb94391dc7cb11fd4bbdb969bbab11b6b48c223Fariborz Jahanian      }
5283502a4a1ce4c34cf78c8182d9798da0a51d9b7302Daniel Dunbar    } else {
5284502a4a1ce4c34cf78c8182d9798da0a51d9b7302Daniel Dunbar      S += '?';
5285502a4a1ce4c34cf78c8182d9798da0a51d9b7302Daniel Dunbar    }
52860d504c1da852e58ff802545c823ecff3b6c654b8Daniel Dunbar    if (ExpandStructures) {
52877d6b46d9a9d75dea8ef9f6973dd50633c1f37963Fariborz Jahanian      S += '=';
52882636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis      if (!RDecl->isUnion()) {
52892636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis        getObjCEncodingForStructureImpl(RDecl, S, FD);
52902636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis      } else {
52912636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis        for (RecordDecl::field_iterator Field = RDecl->field_begin(),
52922636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis                                     FieldEnd = RDecl->field_end();
52932636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis             Field != FieldEnd; ++Field) {
52942636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis          if (FD) {
52952636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis            S += '"';
52962636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis            S += Field->getNameAsString();
52972636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis            S += '"';
52982636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis          }
52991eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
53002636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis          // Special case bit-fields.
53012636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis          if (Field->isBitField()) {
53022636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis            getObjCEncodingForTypeImpl(Field->getType(), S, false, true,
5303581deb3da481053c4993c7600f97acf7768caac5David Blaikie                                       *Field);
53042636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis          } else {
53052636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis            QualType qt = Field->getType();
53062636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis            getLegacyIntegralTypeEncoding(qt);
53072636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis            getObjCEncodingForTypeImpl(qt, S, false, true,
53082636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis                                       FD, /*OutermostType*/false,
53092636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis                                       /*EncodingProperty*/false,
53102636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis                                       /*StructField*/true);
53112636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis          }
5312d96b35bc6becf8db00d140c11e3d0e53f27567a1Daniel Dunbar        }
53137d6b46d9a9d75dea8ef9f6973dd50633c1f37963Fariborz Jahanian      }
53146de88a873a4cbe06d72602eef57d68006730a80bFariborz Jahanian    }
5315d96b35bc6becf8db00d140c11e3d0e53f27567a1Daniel Dunbar    S += RDecl->isUnion() ? ')' : '}';
5316ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner    return;
5317ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner  }
53181eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
53193624e9e55d98507863d0731a0942cd874f2a1574John McCall  case Type::BlockPointer: {
53203624e9e55d98507863d0731a0942cd874f2a1574John McCall    const BlockPointerType *BT = T->castAs<BlockPointerType>();
532121a98b188857d690aa4510c52ac4317ffa0908a8Steve Naroff    S += "@?"; // Unlike a pointer-to-function, which is "^?".
5322dc8dab6fabf4bfd4f4b94bf572ac3342a5bbfcd7Bob Wilson    if (EncodeBlockParameters) {
53233624e9e55d98507863d0731a0942cd874f2a1574John McCall      const FunctionType *FT = BT->getPointeeType()->castAs<FunctionType>();
5324dc8dab6fabf4bfd4f4b94bf572ac3342a5bbfcd7Bob Wilson
5325dc8dab6fabf4bfd4f4b94bf572ac3342a5bbfcd7Bob Wilson      S += '<';
5326dc8dab6fabf4bfd4f4b94bf572ac3342a5bbfcd7Bob Wilson      // Block return type
5327dc8dab6fabf4bfd4f4b94bf572ac3342a5bbfcd7Bob Wilson      getObjCEncodingForTypeImpl(FT->getResultType(), S,
5328dc8dab6fabf4bfd4f4b94bf572ac3342a5bbfcd7Bob Wilson                                 ExpandPointedToStructures, ExpandStructures,
5329dc8dab6fabf4bfd4f4b94bf572ac3342a5bbfcd7Bob Wilson                                 FD,
5330dc8dab6fabf4bfd4f4b94bf572ac3342a5bbfcd7Bob Wilson                                 false /* OutermostType */,
5331dc8dab6fabf4bfd4f4b94bf572ac3342a5bbfcd7Bob Wilson                                 EncodingProperty,
5332dc8dab6fabf4bfd4f4b94bf572ac3342a5bbfcd7Bob Wilson                                 false /* StructField */,
5333dc8dab6fabf4bfd4f4b94bf572ac3342a5bbfcd7Bob Wilson                                 EncodeBlockParameters,
5334dc8dab6fabf4bfd4f4b94bf572ac3342a5bbfcd7Bob Wilson                                 EncodeClassNames);
5335dc8dab6fabf4bfd4f4b94bf572ac3342a5bbfcd7Bob Wilson      // Block self
5336dc8dab6fabf4bfd4f4b94bf572ac3342a5bbfcd7Bob Wilson      S += "@?";
5337dc8dab6fabf4bfd4f4b94bf572ac3342a5bbfcd7Bob Wilson      // Block parameters
5338dc8dab6fabf4bfd4f4b94bf572ac3342a5bbfcd7Bob Wilson      if (const FunctionProtoType *FPT = dyn_cast<FunctionProtoType>(FT)) {
5339dc8dab6fabf4bfd4f4b94bf572ac3342a5bbfcd7Bob Wilson        for (FunctionProtoType::arg_type_iterator I = FPT->arg_type_begin(),
5340dc8dab6fabf4bfd4f4b94bf572ac3342a5bbfcd7Bob Wilson               E = FPT->arg_type_end(); I && (I != E); ++I) {
5341dc8dab6fabf4bfd4f4b94bf572ac3342a5bbfcd7Bob Wilson          getObjCEncodingForTypeImpl(*I, S,
5342dc8dab6fabf4bfd4f4b94bf572ac3342a5bbfcd7Bob Wilson                                     ExpandPointedToStructures,
5343dc8dab6fabf4bfd4f4b94bf572ac3342a5bbfcd7Bob Wilson                                     ExpandStructures,
5344dc8dab6fabf4bfd4f4b94bf572ac3342a5bbfcd7Bob Wilson                                     FD,
5345dc8dab6fabf4bfd4f4b94bf572ac3342a5bbfcd7Bob Wilson                                     false /* OutermostType */,
5346dc8dab6fabf4bfd4f4b94bf572ac3342a5bbfcd7Bob Wilson                                     EncodingProperty,
5347dc8dab6fabf4bfd4f4b94bf572ac3342a5bbfcd7Bob Wilson                                     false /* StructField */,
5348dc8dab6fabf4bfd4f4b94bf572ac3342a5bbfcd7Bob Wilson                                     EncodeBlockParameters,
5349dc8dab6fabf4bfd4f4b94bf572ac3342a5bbfcd7Bob Wilson                                     EncodeClassNames);
5350dc8dab6fabf4bfd4f4b94bf572ac3342a5bbfcd7Bob Wilson        }
5351dc8dab6fabf4bfd4f4b94bf572ac3342a5bbfcd7Bob Wilson      }
5352dc8dab6fabf4bfd4f4b94bf572ac3342a5bbfcd7Bob Wilson      S += '>';
5353dc8dab6fabf4bfd4f4b94bf572ac3342a5bbfcd7Bob Wilson    }
5354ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner    return;
5355ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner  }
53561eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
53573624e9e55d98507863d0731a0942cd874f2a1574John McCall  case Type::ObjCObject:
53583624e9e55d98507863d0731a0942cd874f2a1574John McCall  case Type::ObjCInterface: {
53593624e9e55d98507863d0731a0942cd874f2a1574John McCall    // Ignore protocol qualifiers when mangling at this level.
53603624e9e55d98507863d0731a0942cd874f2a1574John McCall    T = T->castAs<ObjCObjectType>()->getBaseType();
5361c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall
53623624e9e55d98507863d0731a0942cd874f2a1574John McCall    // The assumption seems to be that this assert will succeed
53633624e9e55d98507863d0731a0942cd874f2a1574John McCall    // because nested levels will have filtered out 'id' and 'Class'.
53643624e9e55d98507863d0731a0942cd874f2a1574John McCall    const ObjCInterfaceType *OIT = T->castAs<ObjCInterfaceType>();
536543822eaeddeaa419b90f23c68af6b23c46788a58Fariborz Jahanian    // @encode(class_name)
53660953e767ff7817f97b3ab20896b229891eeff45bJohn McCall    ObjCInterfaceDecl *OI = OIT->getDecl();
536743822eaeddeaa419b90f23c68af6b23c46788a58Fariborz Jahanian    S += '{';
536843822eaeddeaa419b90f23c68af6b23c46788a58Fariborz Jahanian    const IdentifierInfo *II = OI->getIdentifier();
536943822eaeddeaa419b90f23c68af6b23c46788a58Fariborz Jahanian    S += II->getName();
537043822eaeddeaa419b90f23c68af6b23c46788a58Fariborz Jahanian    S += '=';
5371db8264e4c5ffd7af6fbad4ca4306bd382bb02691Jordy Rose    SmallVector<const ObjCIvarDecl*, 32> Ivars;
53722c18bb7c9fca66c30b6eabbdcbc6399d24a54fa9Fariborz Jahanian    DeepCollectObjCIvars(OI, true, Ivars);
53732c18bb7c9fca66c30b6eabbdcbc6399d24a54fa9Fariborz Jahanian    for (unsigned i = 0, e = Ivars.size(); i != e; ++i) {
5374db8264e4c5ffd7af6fbad4ca4306bd382bb02691Jordy Rose      const FieldDecl *Field = cast<FieldDecl>(Ivars[i]);
53752c18bb7c9fca66c30b6eabbdcbc6399d24a54fa9Fariborz Jahanian      if (Field->isBitField())
53762c18bb7c9fca66c30b6eabbdcbc6399d24a54fa9Fariborz Jahanian        getObjCEncodingForTypeImpl(Field->getType(), S, false, true, Field);
537743822eaeddeaa419b90f23c68af6b23c46788a58Fariborz Jahanian      else
537817c1a2e748d49d293c9926eeeb85aa4890182106Fariborz Jahanian        getObjCEncodingForTypeImpl(Field->getType(), S, false, true, FD,
537917c1a2e748d49d293c9926eeeb85aa4890182106Fariborz Jahanian                                   false, false, false, false, false,
538017c1a2e748d49d293c9926eeeb85aa4890182106Fariborz Jahanian                                   EncodePointerToObjCTypedef);
538143822eaeddeaa419b90f23c68af6b23c46788a58Fariborz Jahanian    }
538243822eaeddeaa419b90f23c68af6b23c46788a58Fariborz Jahanian    S += '}';
5383ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner    return;
538443822eaeddeaa419b90f23c68af6b23c46788a58Fariborz Jahanian  }
53851eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
53863624e9e55d98507863d0731a0942cd874f2a1574John McCall  case Type::ObjCObjectPointer: {
53873624e9e55d98507863d0731a0942cd874f2a1574John McCall    const ObjCObjectPointerType *OPT = T->castAs<ObjCObjectPointerType>();
538814108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff    if (OPT->isObjCIdType()) {
538914108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff      S += '@';
539014108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff      return;
5391ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner    }
53921eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
539327d20a24a1b816cecbd142727d2c81af5a6a111bSteve Naroff    if (OPT->isObjCClassType() || OPT->isObjCQualifiedClassType()) {
539427d20a24a1b816cecbd142727d2c81af5a6a111bSteve Naroff      // FIXME: Consider if we need to output qualifiers for 'Class<p>'.
539527d20a24a1b816cecbd142727d2c81af5a6a111bSteve Naroff      // Since this is a binary compatibility issue, need to consult with runtime
539627d20a24a1b816cecbd142727d2c81af5a6a111bSteve Naroff      // folks. Fortunately, this is a *very* obsure construct.
539714108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff      S += '#';
539814108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff      return;
5399ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner    }
54001eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
5401ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner    if (OPT->isObjCQualifiedIdType()) {
54021eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      getObjCEncodingForTypeImpl(getObjCIdType(), S,
540314108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff                                 ExpandPointedToStructures,
540414108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff                                 ExpandStructures, FD);
5405dc8dab6fabf4bfd4f4b94bf572ac3342a5bbfcd7Bob Wilson      if (FD || EncodingProperty || EncodeClassNames) {
540614108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff        // Note that we do extended encoding of protocol qualifer list
540714108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff        // Only when doing ivar or property encoding.
540814108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff        S += '"';
540967ef8eaea8a0a2073147a8d863f0e3f30d525802Steve Naroff        for (ObjCObjectPointerType::qual_iterator I = OPT->qual_begin(),
541067ef8eaea8a0a2073147a8d863f0e3f30d525802Steve Naroff             E = OPT->qual_end(); I != E; ++I) {
541114108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff          S += '<';
541214108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff          S += (*I)->getNameAsString();
541314108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff          S += '>';
541414108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff        }
541514108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff        S += '"';
541614108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff      }
541714108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff      return;
5418ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner    }
54191eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
5420ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner    QualType PointeeTy = OPT->getPointeeType();
5421ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner    if (!EncodingProperty &&
542217c1a2e748d49d293c9926eeeb85aa4890182106Fariborz Jahanian        isa<TypedefType>(PointeeTy.getTypePtr()) &&
542317c1a2e748d49d293c9926eeeb85aa4890182106Fariborz Jahanian        !EncodePointerToObjCTypedef) {
5424ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner      // Another historical/compatibility reason.
54251eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      // We encode the underlying type which comes out as
5426ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner      // {...};
5427ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner      S += '^';
54281eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      getObjCEncodingForTypeImpl(PointeeTy, S,
54291eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump                                 false, ExpandPointedToStructures,
543017c1a2e748d49d293c9926eeeb85aa4890182106Fariborz Jahanian                                 NULL,
543117c1a2e748d49d293c9926eeeb85aa4890182106Fariborz Jahanian                                 false, false, false, false, false,
543217c1a2e748d49d293c9926eeeb85aa4890182106Fariborz Jahanian                                 /*EncodePointerToObjCTypedef*/true);
543314108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff      return;
543414108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff    }
5435ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner
5436ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner    S += '@';
5437dc8dab6fabf4bfd4f4b94bf572ac3342a5bbfcd7Bob Wilson    if (OPT->getInterfaceDecl() &&
5438dc8dab6fabf4bfd4f4b94bf572ac3342a5bbfcd7Bob Wilson        (FD || EncodingProperty || EncodeClassNames)) {
5439ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner      S += '"';
544027d20a24a1b816cecbd142727d2c81af5a6a111bSteve Naroff      S += OPT->getInterfaceDecl()->getIdentifier()->getName();
544167ef8eaea8a0a2073147a8d863f0e3f30d525802Steve Naroff      for (ObjCObjectPointerType::qual_iterator I = OPT->qual_begin(),
544267ef8eaea8a0a2073147a8d863f0e3f30d525802Steve Naroff           E = OPT->qual_end(); I != E; ++I) {
5443ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner        S += '<';
5444ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner        S += (*I)->getNameAsString();
5445ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner        S += '>';
54461eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      }
5447ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner      S += '"';
5448ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner    }
5449ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner    return;
5450ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner  }
54511eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
5452532ec7baf2d0791abc9551ef856a537711c5774aJohn McCall  // gcc just blithely ignores member pointers.
54533624e9e55d98507863d0731a0942cd874f2a1574John McCall  // FIXME: we shoul do better than that.  'M' is available.
54543624e9e55d98507863d0731a0942cd874f2a1574John McCall  case Type::MemberPointer:
5455532ec7baf2d0791abc9551ef856a537711c5774aJohn McCall    return;
5456e6012c7ecb9d848f4091c8c48e7d9946cc36b23fFariborz Jahanian
54573624e9e55d98507863d0731a0942cd874f2a1574John McCall  case Type::Vector:
54583624e9e55d98507863d0731a0942cd874f2a1574John McCall  case Type::ExtVector:
5459e6012c7ecb9d848f4091c8c48e7d9946cc36b23fFariborz Jahanian    // This matches gcc's encoding, even though technically it is
5460e6012c7ecb9d848f4091c8c48e7d9946cc36b23fFariborz Jahanian    // insufficient.
5461e6012c7ecb9d848f4091c8c48e7d9946cc36b23fFariborz Jahanian    // FIXME. We should do a better job than gcc.
5462e6012c7ecb9d848f4091c8c48e7d9946cc36b23fFariborz Jahanian    return;
54633624e9e55d98507863d0731a0942cd874f2a1574John McCall
5464dc7a4f5d7a7e3b60d4dc4a80338d7a2728540998Richard Smith  case Type::Auto:
5465dc7a4f5d7a7e3b60d4dc4a80338d7a2728540998Richard Smith    // We could see an undeduced auto type here during error recovery.
5466dc7a4f5d7a7e3b60d4dc4a80338d7a2728540998Richard Smith    // Just ignore it.
5467dc7a4f5d7a7e3b60d4dc4a80338d7a2728540998Richard Smith    return;
5468dc7a4f5d7a7e3b60d4dc4a80338d7a2728540998Richard Smith
54693624e9e55d98507863d0731a0942cd874f2a1574John McCall#define ABSTRACT_TYPE(KIND, BASE)
54703624e9e55d98507863d0731a0942cd874f2a1574John McCall#define TYPE(KIND, BASE)
54713624e9e55d98507863d0731a0942cd874f2a1574John McCall#define DEPENDENT_TYPE(KIND, BASE) \
54723624e9e55d98507863d0731a0942cd874f2a1574John McCall  case Type::KIND:
54733624e9e55d98507863d0731a0942cd874f2a1574John McCall#define NON_CANONICAL_TYPE(KIND, BASE) \
54743624e9e55d98507863d0731a0942cd874f2a1574John McCall  case Type::KIND:
54753624e9e55d98507863d0731a0942cd874f2a1574John McCall#define NON_CANONICAL_UNLESS_DEPENDENT_TYPE(KIND, BASE) \
54763624e9e55d98507863d0731a0942cd874f2a1574John McCall  case Type::KIND:
54773624e9e55d98507863d0731a0942cd874f2a1574John McCall#include "clang/AST/TypeNodes.def"
54783624e9e55d98507863d0731a0942cd874f2a1574John McCall    llvm_unreachable("@encode for dependent type!");
5479e6012c7ecb9d848f4091c8c48e7d9946cc36b23fFariborz Jahanian  }
54803624e9e55d98507863d0731a0942cd874f2a1574John McCall  llvm_unreachable("bad type kind!");
548185f9bceab1542aafff012d4d28e998f4ba16e362Anders Carlsson}
548285f9bceab1542aafff012d4d28e998f4ba16e362Anders Carlsson
54832636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidisvoid ASTContext::getObjCEncodingForStructureImpl(RecordDecl *RDecl,
54842636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis                                                 std::string &S,
54852636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis                                                 const FieldDecl *FD,
54862636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis                                                 bool includeVBases) const {
54872636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis  assert(RDecl && "Expected non-null RecordDecl");
54882636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis  assert(!RDecl->isUnion() && "Should not be called for unions");
54892636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis  if (!RDecl->getDefinition())
54902636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis    return;
54912636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis
54922636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis  CXXRecordDecl *CXXRec = dyn_cast<CXXRecordDecl>(RDecl);
54932636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis  std::multimap<uint64_t, NamedDecl *> FieldOrBaseOffsets;
54942636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis  const ASTRecordLayout &layout = getASTRecordLayout(RDecl);
54952636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis
54962636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis  if (CXXRec) {
54972636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis    for (CXXRecordDecl::base_class_iterator
54982636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis           BI = CXXRec->bases_begin(),
54992636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis           BE = CXXRec->bases_end(); BI != BE; ++BI) {
55002636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis      if (!BI->isVirtual()) {
55012636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis        CXXRecordDecl *base = BI->getType()->getAsCXXRecordDecl();
5502829f20097e030229fd4c234b84948f53312eaf55Argyrios Kyrtzidis        if (base->isEmpty())
5503829f20097e030229fd4c234b84948f53312eaf55Argyrios Kyrtzidis          continue;
5504d4f5198ae07d9a4958d8191bac694ded12173ad9Benjamin Kramer        uint64_t offs = toBits(layout.getBaseClassOffset(base));
55052636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis        FieldOrBaseOffsets.insert(FieldOrBaseOffsets.upper_bound(offs),
55062636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis                                  std::make_pair(offs, base));
55072636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis      }
55082636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis    }
55092636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis  }
55102636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis
55112636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis  unsigned i = 0;
55122636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis  for (RecordDecl::field_iterator Field = RDecl->field_begin(),
55132636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis                               FieldEnd = RDecl->field_end();
55142636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis       Field != FieldEnd; ++Field, ++i) {
55152636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis    uint64_t offs = layout.getFieldOffset(i);
55162636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis    FieldOrBaseOffsets.insert(FieldOrBaseOffsets.upper_bound(offs),
5517581deb3da481053c4993c7600f97acf7768caac5David Blaikie                              std::make_pair(offs, *Field));
55182636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis  }
55192636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis
55202636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis  if (CXXRec && includeVBases) {
55212636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis    for (CXXRecordDecl::base_class_iterator
55222636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis           BI = CXXRec->vbases_begin(),
55232636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis           BE = CXXRec->vbases_end(); BI != BE; ++BI) {
55242636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis      CXXRecordDecl *base = BI->getType()->getAsCXXRecordDecl();
5525829f20097e030229fd4c234b84948f53312eaf55Argyrios Kyrtzidis      if (base->isEmpty())
5526829f20097e030229fd4c234b84948f53312eaf55Argyrios Kyrtzidis        continue;
5527d4f5198ae07d9a4958d8191bac694ded12173ad9Benjamin Kramer      uint64_t offs = toBits(layout.getVBaseClassOffset(base));
552819aa8609a5d04eed9b81aa1c4608e8a4f3df6ccdArgyrios Kyrtzidis      if (FieldOrBaseOffsets.find(offs) == FieldOrBaseOffsets.end())
552919aa8609a5d04eed9b81aa1c4608e8a4f3df6ccdArgyrios Kyrtzidis        FieldOrBaseOffsets.insert(FieldOrBaseOffsets.end(),
553019aa8609a5d04eed9b81aa1c4608e8a4f3df6ccdArgyrios Kyrtzidis                                  std::make_pair(offs, base));
55312636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis    }
55322636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis  }
55332636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis
55342636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis  CharUnits size;
55352636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis  if (CXXRec) {
55362636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis    size = includeVBases ? layout.getSize() : layout.getNonVirtualSize();
55372636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis  } else {
55382636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis    size = layout.getSize();
55392636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis  }
55402636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis
55412636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis  uint64_t CurOffs = 0;
55422636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis  std::multimap<uint64_t, NamedDecl *>::iterator
55432636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis    CurLayObj = FieldOrBaseOffsets.begin();
55442636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis
554558db7a575efc9a2f35266fe240feac3cf317753dDouglas Gregor  if (CXXRec && CXXRec->isDynamicClass() &&
554658db7a575efc9a2f35266fe240feac3cf317753dDouglas Gregor      (CurLayObj == FieldOrBaseOffsets.end() || CurLayObj->first != 0)) {
55472636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis    if (FD) {
55482636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis      S += "\"_vptr$";
55492636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis      std::string recname = CXXRec->getNameAsString();
55502636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis      if (recname.empty()) recname = "?";
55512636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis      S += recname;
55522636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis      S += '"';
55532636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis    }
55542636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis    S += "^^?";
55552636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis    CurOffs += getTypeSize(VoidPtrTy);
55562636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis  }
55572636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis
55582636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis  if (!RDecl->hasFlexibleArrayMember()) {
55592636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis    // Mark the end of the structure.
55602636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis    uint64_t offs = toBits(size);
55612636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis    FieldOrBaseOffsets.insert(FieldOrBaseOffsets.upper_bound(offs),
55622636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis                              std::make_pair(offs, (NamedDecl*)0));
55632636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis  }
55642636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis
55652636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis  for (; CurLayObj != FieldOrBaseOffsets.end(); ++CurLayObj) {
55662636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis    assert(CurOffs <= CurLayObj->first);
55672636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis
55682636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis    if (CurOffs < CurLayObj->first) {
55692636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis      uint64_t padding = CurLayObj->first - CurOffs;
55702636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis      // FIXME: There doesn't seem to be a way to indicate in the encoding that
55712636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis      // packing/alignment of members is different that normal, in which case
55722636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis      // the encoding will be out-of-sync with the real layout.
55732636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis      // If the runtime switches to just consider the size of types without
55742636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis      // taking into account alignment, we could make padding explicit in the
55752636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis      // encoding (e.g. using arrays of chars). The encoding strings would be
55762636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis      // longer then though.
55772636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis      CurOffs += padding;
55782636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis    }
55792636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis
55802636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis    NamedDecl *dcl = CurLayObj->second;
55812636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis    if (dcl == 0)
55822636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis      break; // reached end of structure.
55832636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis
55842636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis    if (CXXRecordDecl *base = dyn_cast<CXXRecordDecl>(dcl)) {
55852636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis      // We expand the bases without their virtual bases since those are going
55862636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis      // in the initial structure. Note that this differs from gcc which
55872636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis      // expands virtual bases each time one is encountered in the hierarchy,
55882636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis      // making the encoding type bigger than it really is.
55892636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis      getObjCEncodingForStructureImpl(base, S, FD, /*includeVBases*/false);
5590829f20097e030229fd4c234b84948f53312eaf55Argyrios Kyrtzidis      assert(!base->isEmpty());
5591829f20097e030229fd4c234b84948f53312eaf55Argyrios Kyrtzidis      CurOffs += toBits(getASTRecordLayout(base).getNonVirtualSize());
55922636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis    } else {
55932636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis      FieldDecl *field = cast<FieldDecl>(dcl);
55942636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis      if (FD) {
55952636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis        S += '"';
55962636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis        S += field->getNameAsString();
55972636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis        S += '"';
55982636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis      }
55992636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis
56002636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis      if (field->isBitField()) {
56012636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis        EncodeBitField(this, S, field->getType(), field);
5602a6b8b2c09610b8bc4330e948ece8b940c2386406Richard Smith        CurOffs += field->getBitWidthValue(*this);
56032636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis      } else {
56042636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis        QualType qt = field->getType();
56052636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis        getLegacyIntegralTypeEncoding(qt);
56062636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis        getObjCEncodingForTypeImpl(qt, S, false, true, FD,
56072636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis                                   /*OutermostType*/false,
56082636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis                                   /*EncodingProperty*/false,
56092636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis                                   /*StructField*/true);
56102636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis        CurOffs += getTypeSize(field->getType());
56112636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis      }
56122636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis    }
56132636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis  }
56142636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis}
56152636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis
56161eb4433ac451dc16f4133a88af2d002ac26c58efMike Stumpvoid ASTContext::getObjCEncodingForTypeQualifier(Decl::ObjCDeclQualifier QT,
5617ecb01e666665efabd2aa76a76f6080e2a78965faFariborz Jahanian                                                 std::string& S) const {
5618ecb01e666665efabd2aa76a76f6080e2a78965faFariborz Jahanian  if (QT & Decl::OBJC_TQ_In)
5619ecb01e666665efabd2aa76a76f6080e2a78965faFariborz Jahanian    S += 'n';
5620ecb01e666665efabd2aa76a76f6080e2a78965faFariborz Jahanian  if (QT & Decl::OBJC_TQ_Inout)
5621ecb01e666665efabd2aa76a76f6080e2a78965faFariborz Jahanian    S += 'N';
5622ecb01e666665efabd2aa76a76f6080e2a78965faFariborz Jahanian  if (QT & Decl::OBJC_TQ_Out)
5623ecb01e666665efabd2aa76a76f6080e2a78965faFariborz Jahanian    S += 'o';
5624ecb01e666665efabd2aa76a76f6080e2a78965faFariborz Jahanian  if (QT & Decl::OBJC_TQ_Bycopy)
5625ecb01e666665efabd2aa76a76f6080e2a78965faFariborz Jahanian    S += 'O';
5626ecb01e666665efabd2aa76a76f6080e2a78965faFariborz Jahanian  if (QT & Decl::OBJC_TQ_Byref)
5627ecb01e666665efabd2aa76a76f6080e2a78965faFariborz Jahanian    S += 'R';
5628ecb01e666665efabd2aa76a76f6080e2a78965faFariborz Jahanian  if (QT & Decl::OBJC_TQ_Oneway)
5629ecb01e666665efabd2aa76a76f6080e2a78965faFariborz Jahanian    S += 'V';
5630ecb01e666665efabd2aa76a76f6080e2a78965faFariborz Jahanian}
5631ecb01e666665efabd2aa76a76f6080e2a78965faFariborz Jahanian
56324dfd02a17c6d604c72e6936527c5e1c56d3ecb7aDouglas GregorTypedefDecl *ASTContext::getObjCIdDecl() const {
56334dfd02a17c6d604c72e6936527c5e1c56d3ecb7aDouglas Gregor  if (!ObjCIdDecl) {
56344dfd02a17c6d604c72e6936527c5e1c56d3ecb7aDouglas Gregor    QualType T = getObjCObjectType(ObjCBuiltinIdTy, 0, 0);
56354dfd02a17c6d604c72e6936527c5e1c56d3ecb7aDouglas Gregor    T = getObjCObjectPointerType(T);
56364dfd02a17c6d604c72e6936527c5e1c56d3ecb7aDouglas Gregor    TypeSourceInfo *IdInfo = getTrivialTypeSourceInfo(T);
56374dfd02a17c6d604c72e6936527c5e1c56d3ecb7aDouglas Gregor    ObjCIdDecl = TypedefDecl::Create(const_cast<ASTContext &>(*this),
56384dfd02a17c6d604c72e6936527c5e1c56d3ecb7aDouglas Gregor                                     getTranslationUnitDecl(),
56394dfd02a17c6d604c72e6936527c5e1c56d3ecb7aDouglas Gregor                                     SourceLocation(), SourceLocation(),
56404dfd02a17c6d604c72e6936527c5e1c56d3ecb7aDouglas Gregor                                     &Idents.get("id"), IdInfo);
56414dfd02a17c6d604c72e6936527c5e1c56d3ecb7aDouglas Gregor  }
56424dfd02a17c6d604c72e6936527c5e1c56d3ecb7aDouglas Gregor
56434dfd02a17c6d604c72e6936527c5e1c56d3ecb7aDouglas Gregor  return ObjCIdDecl;
56447e219e47de26346885d667131977bd9ca2d7662aSteve Naroff}
56457e219e47de26346885d667131977bd9ca2d7662aSteve Naroff
56467a27ea52b7bd635c89bec5a9c521a3bf7d204238Douglas GregorTypedefDecl *ASTContext::getObjCSelDecl() const {
56477a27ea52b7bd635c89bec5a9c521a3bf7d204238Douglas Gregor  if (!ObjCSelDecl) {
56487a27ea52b7bd635c89bec5a9c521a3bf7d204238Douglas Gregor    QualType SelT = getPointerType(ObjCBuiltinSelTy);
56497a27ea52b7bd635c89bec5a9c521a3bf7d204238Douglas Gregor    TypeSourceInfo *SelInfo = getTrivialTypeSourceInfo(SelT);
56507a27ea52b7bd635c89bec5a9c521a3bf7d204238Douglas Gregor    ObjCSelDecl = TypedefDecl::Create(const_cast<ASTContext &>(*this),
56517a27ea52b7bd635c89bec5a9c521a3bf7d204238Douglas Gregor                                      getTranslationUnitDecl(),
56527a27ea52b7bd635c89bec5a9c521a3bf7d204238Douglas Gregor                                      SourceLocation(), SourceLocation(),
56537a27ea52b7bd635c89bec5a9c521a3bf7d204238Douglas Gregor                                      &Idents.get("SEL"), SelInfo);
56547a27ea52b7bd635c89bec5a9c521a3bf7d204238Douglas Gregor  }
56557a27ea52b7bd635c89bec5a9c521a3bf7d204238Douglas Gregor  return ObjCSelDecl;
5656b62f6813406a03bf8a371c4e46c9fad51d102121Fariborz Jahanian}
5657b62f6813406a03bf8a371c4e46c9fad51d102121Fariborz Jahanian
565879d6726921897811232554ed94c5d77b5b7b3fc0Douglas GregorTypedefDecl *ASTContext::getObjCClassDecl() const {
565979d6726921897811232554ed94c5d77b5b7b3fc0Douglas Gregor  if (!ObjCClassDecl) {
566079d6726921897811232554ed94c5d77b5b7b3fc0Douglas Gregor    QualType T = getObjCObjectType(ObjCBuiltinClassTy, 0, 0);
566179d6726921897811232554ed94c5d77b5b7b3fc0Douglas Gregor    T = getObjCObjectPointerType(T);
566279d6726921897811232554ed94c5d77b5b7b3fc0Douglas Gregor    TypeSourceInfo *ClassInfo = getTrivialTypeSourceInfo(T);
566379d6726921897811232554ed94c5d77b5b7b3fc0Douglas Gregor    ObjCClassDecl = TypedefDecl::Create(const_cast<ASTContext &>(*this),
566479d6726921897811232554ed94c5d77b5b7b3fc0Douglas Gregor                                        getTranslationUnitDecl(),
566579d6726921897811232554ed94c5d77b5b7b3fc0Douglas Gregor                                        SourceLocation(), SourceLocation(),
566679d6726921897811232554ed94c5d77b5b7b3fc0Douglas Gregor                                        &Idents.get("Class"), ClassInfo);
566779d6726921897811232554ed94c5d77b5b7b3fc0Douglas Gregor  }
566879d6726921897811232554ed94c5d77b5b7b3fc0Douglas Gregor
566979d6726921897811232554ed94c5d77b5b7b3fc0Douglas Gregor  return ObjCClassDecl;
5670a6ea10e22b600d92e084f6b11b9b9a92d0eb2412Douglas Gregor}
5671a6ea10e22b600d92e084f6b11b9b9a92d0eb2412Douglas Gregor
5672a6ea10e22b600d92e084f6b11b9b9a92d0eb2412Douglas GregorObjCInterfaceDecl *ASTContext::getObjCProtocolDecl() const {
5673a6ea10e22b600d92e084f6b11b9b9a92d0eb2412Douglas Gregor  if (!ObjCProtocolClassDecl) {
5674a6ea10e22b600d92e084f6b11b9b9a92d0eb2412Douglas Gregor    ObjCProtocolClassDecl
5675a6ea10e22b600d92e084f6b11b9b9a92d0eb2412Douglas Gregor      = ObjCInterfaceDecl::Create(*this, getTranslationUnitDecl(),
5676a6ea10e22b600d92e084f6b11b9b9a92d0eb2412Douglas Gregor                                  SourceLocation(),
5677a6ea10e22b600d92e084f6b11b9b9a92d0eb2412Douglas Gregor                                  &Idents.get("Protocol"),
5678a6ea10e22b600d92e084f6b11b9b9a92d0eb2412Douglas Gregor                                  /*PrevDecl=*/0,
5679a6ea10e22b600d92e084f6b11b9b9a92d0eb2412Douglas Gregor                                  SourceLocation(), true);
5680a6ea10e22b600d92e084f6b11b9b9a92d0eb2412Douglas Gregor  }
5681a6ea10e22b600d92e084f6b11b9b9a92d0eb2412Douglas Gregor
5682a6ea10e22b600d92e084f6b11b9b9a92d0eb2412Douglas Gregor  return ObjCProtocolClassDecl;
56838baaca50f07d0c10bba69c8d88c1b9078c92d06dAnders Carlsson}
56848baaca50f07d0c10bba69c8d88c1b9078c92d06dAnders Carlsson
5685c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge//===----------------------------------------------------------------------===//
5686c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge// __builtin_va_list Construction Functions
5687c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge//===----------------------------------------------------------------------===//
5688c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge
5689c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Ingestatic TypedefDecl *CreateCharPtrBuiltinVaListDecl(const ASTContext *Context) {
5690c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge  // typedef char* __builtin_va_list;
5691c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge  QualType CharPtrType = Context->getPointerType(Context->CharTy);
5692c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge  TypeSourceInfo *TInfo
5693c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge    = Context->getTrivialTypeSourceInfo(CharPtrType);
5694c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge
5695c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge  TypedefDecl *VaListTypeDecl
5696c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge    = TypedefDecl::Create(const_cast<ASTContext &>(*Context),
5697c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge                          Context->getTranslationUnitDecl(),
5698c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge                          SourceLocation(), SourceLocation(),
5699c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge                          &Context->Idents.get("__builtin_va_list"),
5700c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge                          TInfo);
5701c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge  return VaListTypeDecl;
5702c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge}
5703c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge
5704c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Ingestatic TypedefDecl *CreateVoidPtrBuiltinVaListDecl(const ASTContext *Context) {
5705c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge  // typedef void* __builtin_va_list;
5706c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge  QualType VoidPtrType = Context->getPointerType(Context->VoidTy);
5707c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge  TypeSourceInfo *TInfo
5708c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge    = Context->getTrivialTypeSourceInfo(VoidPtrType);
5709c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge
5710c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge  TypedefDecl *VaListTypeDecl
5711c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge    = TypedefDecl::Create(const_cast<ASTContext &>(*Context),
5712c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge                          Context->getTranslationUnitDecl(),
5713c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge                          SourceLocation(), SourceLocation(),
5714c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge                          &Context->Idents.get("__builtin_va_list"),
5715c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge                          TInfo);
5716c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge  return VaListTypeDecl;
5717c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge}
5718c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge
5719c264e16a42b3f6c36521857a29ea0949d9781c22Tim Northoverstatic TypedefDecl *
5720c264e16a42b3f6c36521857a29ea0949d9781c22Tim NorthoverCreateAArch64ABIBuiltinVaListDecl(const ASTContext *Context) {
5721c264e16a42b3f6c36521857a29ea0949d9781c22Tim Northover  RecordDecl *VaListTagDecl;
5722c264e16a42b3f6c36521857a29ea0949d9781c22Tim Northover  if (Context->getLangOpts().CPlusPlus) {
5723c264e16a42b3f6c36521857a29ea0949d9781c22Tim Northover    // namespace std { struct __va_list {
5724c264e16a42b3f6c36521857a29ea0949d9781c22Tim Northover    NamespaceDecl *NS;
5725c264e16a42b3f6c36521857a29ea0949d9781c22Tim Northover    NS = NamespaceDecl::Create(const_cast<ASTContext &>(*Context),
5726c264e16a42b3f6c36521857a29ea0949d9781c22Tim Northover                               Context->getTranslationUnitDecl(),
5727c264e16a42b3f6c36521857a29ea0949d9781c22Tim Northover                               /*Inline*/false, SourceLocation(),
5728c264e16a42b3f6c36521857a29ea0949d9781c22Tim Northover                               SourceLocation(), &Context->Idents.get("std"),
5729c264e16a42b3f6c36521857a29ea0949d9781c22Tim Northover                               /*PrevDecl*/0);
5730c264e16a42b3f6c36521857a29ea0949d9781c22Tim Northover
5731c264e16a42b3f6c36521857a29ea0949d9781c22Tim Northover    VaListTagDecl = CXXRecordDecl::Create(*Context, TTK_Struct,
5732c264e16a42b3f6c36521857a29ea0949d9781c22Tim Northover                                          Context->getTranslationUnitDecl(),
5733c264e16a42b3f6c36521857a29ea0949d9781c22Tim Northover                                          SourceLocation(), SourceLocation(),
5734c264e16a42b3f6c36521857a29ea0949d9781c22Tim Northover                                          &Context->Idents.get("__va_list"));
5735c264e16a42b3f6c36521857a29ea0949d9781c22Tim Northover    VaListTagDecl->setDeclContext(NS);
5736c264e16a42b3f6c36521857a29ea0949d9781c22Tim Northover  } else {
5737c264e16a42b3f6c36521857a29ea0949d9781c22Tim Northover    // struct __va_list
5738c264e16a42b3f6c36521857a29ea0949d9781c22Tim Northover    VaListTagDecl = CreateRecordDecl(*Context, TTK_Struct,
5739c264e16a42b3f6c36521857a29ea0949d9781c22Tim Northover                                   Context->getTranslationUnitDecl(),
5740c264e16a42b3f6c36521857a29ea0949d9781c22Tim Northover                                   &Context->Idents.get("__va_list"));
5741c264e16a42b3f6c36521857a29ea0949d9781c22Tim Northover  }
5742c264e16a42b3f6c36521857a29ea0949d9781c22Tim Northover
5743c264e16a42b3f6c36521857a29ea0949d9781c22Tim Northover  VaListTagDecl->startDefinition();
5744c264e16a42b3f6c36521857a29ea0949d9781c22Tim Northover
5745c264e16a42b3f6c36521857a29ea0949d9781c22Tim Northover  const size_t NumFields = 5;
5746c264e16a42b3f6c36521857a29ea0949d9781c22Tim Northover  QualType FieldTypes[NumFields];
5747c264e16a42b3f6c36521857a29ea0949d9781c22Tim Northover  const char *FieldNames[NumFields];
5748c264e16a42b3f6c36521857a29ea0949d9781c22Tim Northover
5749c264e16a42b3f6c36521857a29ea0949d9781c22Tim Northover  // void *__stack;
5750c264e16a42b3f6c36521857a29ea0949d9781c22Tim Northover  FieldTypes[0] = Context->getPointerType(Context->VoidTy);
5751c264e16a42b3f6c36521857a29ea0949d9781c22Tim Northover  FieldNames[0] = "__stack";
5752c264e16a42b3f6c36521857a29ea0949d9781c22Tim Northover
5753c264e16a42b3f6c36521857a29ea0949d9781c22Tim Northover  // void *__gr_top;
5754c264e16a42b3f6c36521857a29ea0949d9781c22Tim Northover  FieldTypes[1] = Context->getPointerType(Context->VoidTy);
5755c264e16a42b3f6c36521857a29ea0949d9781c22Tim Northover  FieldNames[1] = "__gr_top";
5756c264e16a42b3f6c36521857a29ea0949d9781c22Tim Northover
5757c264e16a42b3f6c36521857a29ea0949d9781c22Tim Northover  // void *__vr_top;
5758c264e16a42b3f6c36521857a29ea0949d9781c22Tim Northover  FieldTypes[2] = Context->getPointerType(Context->VoidTy);
5759c264e16a42b3f6c36521857a29ea0949d9781c22Tim Northover  FieldNames[2] = "__vr_top";
5760c264e16a42b3f6c36521857a29ea0949d9781c22Tim Northover
5761c264e16a42b3f6c36521857a29ea0949d9781c22Tim Northover  // int __gr_offs;
5762c264e16a42b3f6c36521857a29ea0949d9781c22Tim Northover  FieldTypes[3] = Context->IntTy;
5763c264e16a42b3f6c36521857a29ea0949d9781c22Tim Northover  FieldNames[3] = "__gr_offs";
5764c264e16a42b3f6c36521857a29ea0949d9781c22Tim Northover
5765c264e16a42b3f6c36521857a29ea0949d9781c22Tim Northover  // int __vr_offs;
5766c264e16a42b3f6c36521857a29ea0949d9781c22Tim Northover  FieldTypes[4] = Context->IntTy;
5767c264e16a42b3f6c36521857a29ea0949d9781c22Tim Northover  FieldNames[4] = "__vr_offs";
5768c264e16a42b3f6c36521857a29ea0949d9781c22Tim Northover
5769c264e16a42b3f6c36521857a29ea0949d9781c22Tim Northover  // Create fields
5770c264e16a42b3f6c36521857a29ea0949d9781c22Tim Northover  for (unsigned i = 0; i < NumFields; ++i) {
5771c264e16a42b3f6c36521857a29ea0949d9781c22Tim Northover    FieldDecl *Field = FieldDecl::Create(const_cast<ASTContext &>(*Context),
5772c264e16a42b3f6c36521857a29ea0949d9781c22Tim Northover                                         VaListTagDecl,
5773c264e16a42b3f6c36521857a29ea0949d9781c22Tim Northover                                         SourceLocation(),
5774c264e16a42b3f6c36521857a29ea0949d9781c22Tim Northover                                         SourceLocation(),
5775c264e16a42b3f6c36521857a29ea0949d9781c22Tim Northover                                         &Context->Idents.get(FieldNames[i]),
5776c264e16a42b3f6c36521857a29ea0949d9781c22Tim Northover                                         FieldTypes[i], /*TInfo=*/0,
5777c264e16a42b3f6c36521857a29ea0949d9781c22Tim Northover                                         /*BitWidth=*/0,
5778c264e16a42b3f6c36521857a29ea0949d9781c22Tim Northover                                         /*Mutable=*/false,
5779c264e16a42b3f6c36521857a29ea0949d9781c22Tim Northover                                         ICIS_NoInit);
5780c264e16a42b3f6c36521857a29ea0949d9781c22Tim Northover    Field->setAccess(AS_public);
5781c264e16a42b3f6c36521857a29ea0949d9781c22Tim Northover    VaListTagDecl->addDecl(Field);
5782c264e16a42b3f6c36521857a29ea0949d9781c22Tim Northover  }
5783c264e16a42b3f6c36521857a29ea0949d9781c22Tim Northover  VaListTagDecl->completeDefinition();
5784c264e16a42b3f6c36521857a29ea0949d9781c22Tim Northover  QualType VaListTagType = Context->getRecordType(VaListTagDecl);
5785c264e16a42b3f6c36521857a29ea0949d9781c22Tim Northover  Context->VaListTagTy = VaListTagType;
5786c264e16a42b3f6c36521857a29ea0949d9781c22Tim Northover
5787c264e16a42b3f6c36521857a29ea0949d9781c22Tim Northover  // } __builtin_va_list;
5788c264e16a42b3f6c36521857a29ea0949d9781c22Tim Northover  TypedefDecl *VaListTypedefDecl
5789c264e16a42b3f6c36521857a29ea0949d9781c22Tim Northover    = TypedefDecl::Create(const_cast<ASTContext &>(*Context),
5790c264e16a42b3f6c36521857a29ea0949d9781c22Tim Northover                          Context->getTranslationUnitDecl(),
5791c264e16a42b3f6c36521857a29ea0949d9781c22Tim Northover                          SourceLocation(), SourceLocation(),
5792c264e16a42b3f6c36521857a29ea0949d9781c22Tim Northover                          &Context->Idents.get("__builtin_va_list"),
5793c264e16a42b3f6c36521857a29ea0949d9781c22Tim Northover                          Context->getTrivialTypeSourceInfo(VaListTagType));
5794c264e16a42b3f6c36521857a29ea0949d9781c22Tim Northover
5795c264e16a42b3f6c36521857a29ea0949d9781c22Tim Northover  return VaListTypedefDecl;
5796c264e16a42b3f6c36521857a29ea0949d9781c22Tim Northover}
5797c264e16a42b3f6c36521857a29ea0949d9781c22Tim Northover
5798c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Ingestatic TypedefDecl *CreatePowerABIBuiltinVaListDecl(const ASTContext *Context) {
5799c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge  // typedef struct __va_list_tag {
5800c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge  RecordDecl *VaListTagDecl;
5801c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge
5802c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge  VaListTagDecl = CreateRecordDecl(*Context, TTK_Struct,
5803c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge                                   Context->getTranslationUnitDecl(),
5804c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge                                   &Context->Idents.get("__va_list_tag"));
5805c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge  VaListTagDecl->startDefinition();
5806c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge
5807c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge  const size_t NumFields = 5;
5808c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge  QualType FieldTypes[NumFields];
5809c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge  const char *FieldNames[NumFields];
5810c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge
5811c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge  //   unsigned char gpr;
5812c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge  FieldTypes[0] = Context->UnsignedCharTy;
5813c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge  FieldNames[0] = "gpr";
5814c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge
5815c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge  //   unsigned char fpr;
5816c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge  FieldTypes[1] = Context->UnsignedCharTy;
5817c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge  FieldNames[1] = "fpr";
5818c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge
5819c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge  //   unsigned short reserved;
5820c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge  FieldTypes[2] = Context->UnsignedShortTy;
5821c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge  FieldNames[2] = "reserved";
5822c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge
5823c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge  //   void* overflow_arg_area;
5824c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge  FieldTypes[3] = Context->getPointerType(Context->VoidTy);
5825c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge  FieldNames[3] = "overflow_arg_area";
5826c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge
5827c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge  //   void* reg_save_area;
5828c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge  FieldTypes[4] = Context->getPointerType(Context->VoidTy);
5829c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge  FieldNames[4] = "reg_save_area";
5830c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge
5831c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge  // Create fields
5832c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge  for (unsigned i = 0; i < NumFields; ++i) {
5833c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge    FieldDecl *Field = FieldDecl::Create(*Context, VaListTagDecl,
5834c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge                                         SourceLocation(),
5835c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge                                         SourceLocation(),
5836c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge                                         &Context->Idents.get(FieldNames[i]),
5837c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge                                         FieldTypes[i], /*TInfo=*/0,
5838c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge                                         /*BitWidth=*/0,
5839c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge                                         /*Mutable=*/false,
5840c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge                                         ICIS_NoInit);
5841c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge    Field->setAccess(AS_public);
5842c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge    VaListTagDecl->addDecl(Field);
5843c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge  }
5844c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge  VaListTagDecl->completeDefinition();
5845c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge  QualType VaListTagType = Context->getRecordType(VaListTagDecl);
5846fb40e3f10ccef93c4f8fb6bd4fe5a108fa6cd369Meador Inge  Context->VaListTagTy = VaListTagType;
5847c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge
5848c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge  // } __va_list_tag;
5849c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge  TypedefDecl *VaListTagTypedefDecl
5850c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge    = TypedefDecl::Create(const_cast<ASTContext &>(*Context),
5851c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge                          Context->getTranslationUnitDecl(),
5852c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge                          SourceLocation(), SourceLocation(),
5853c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge                          &Context->Idents.get("__va_list_tag"),
5854c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge                          Context->getTrivialTypeSourceInfo(VaListTagType));
5855c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge  QualType VaListTagTypedefType =
5856c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge    Context->getTypedefType(VaListTagTypedefDecl);
5857c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge
5858c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge  // typedef __va_list_tag __builtin_va_list[1];
5859c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge  llvm::APInt Size(Context->getTypeSize(Context->getSizeType()), 1);
5860c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge  QualType VaListTagArrayType
5861c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge    = Context->getConstantArrayType(VaListTagTypedefType,
5862c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge                                    Size, ArrayType::Normal, 0);
5863c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge  TypeSourceInfo *TInfo
5864c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge    = Context->getTrivialTypeSourceInfo(VaListTagArrayType);
5865c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge  TypedefDecl *VaListTypedefDecl
5866c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge    = TypedefDecl::Create(const_cast<ASTContext &>(*Context),
5867c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge                          Context->getTranslationUnitDecl(),
5868c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge                          SourceLocation(), SourceLocation(),
5869c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge                          &Context->Idents.get("__builtin_va_list"),
5870c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge                          TInfo);
5871c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge
5872c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge  return VaListTypedefDecl;
5873c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge}
5874c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge
5875c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Ingestatic TypedefDecl *
5876c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador IngeCreateX86_64ABIBuiltinVaListDecl(const ASTContext *Context) {
5877c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge  // typedef struct __va_list_tag {
5878c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge  RecordDecl *VaListTagDecl;
5879c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge  VaListTagDecl = CreateRecordDecl(*Context, TTK_Struct,
5880c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge                                   Context->getTranslationUnitDecl(),
5881c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge                                   &Context->Idents.get("__va_list_tag"));
5882c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge  VaListTagDecl->startDefinition();
5883c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge
5884c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge  const size_t NumFields = 4;
5885c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge  QualType FieldTypes[NumFields];
5886c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge  const char *FieldNames[NumFields];
5887c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge
5888c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge  //   unsigned gp_offset;
5889c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge  FieldTypes[0] = Context->UnsignedIntTy;
5890c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge  FieldNames[0] = "gp_offset";
5891c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge
5892c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge  //   unsigned fp_offset;
5893c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge  FieldTypes[1] = Context->UnsignedIntTy;
5894c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge  FieldNames[1] = "fp_offset";
5895c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge
5896c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge  //   void* overflow_arg_area;
5897c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge  FieldTypes[2] = Context->getPointerType(Context->VoidTy);
5898c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge  FieldNames[2] = "overflow_arg_area";
5899c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge
5900c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge  //   void* reg_save_area;
5901c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge  FieldTypes[3] = Context->getPointerType(Context->VoidTy);
5902c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge  FieldNames[3] = "reg_save_area";
5903c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge
5904c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge  // Create fields
5905c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge  for (unsigned i = 0; i < NumFields; ++i) {
5906c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge    FieldDecl *Field = FieldDecl::Create(const_cast<ASTContext &>(*Context),
5907c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge                                         VaListTagDecl,
5908c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge                                         SourceLocation(),
5909c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge                                         SourceLocation(),
5910c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge                                         &Context->Idents.get(FieldNames[i]),
5911c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge                                         FieldTypes[i], /*TInfo=*/0,
5912c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge                                         /*BitWidth=*/0,
5913c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge                                         /*Mutable=*/false,
5914c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge                                         ICIS_NoInit);
5915c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge    Field->setAccess(AS_public);
5916c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge    VaListTagDecl->addDecl(Field);
5917c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge  }
5918c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge  VaListTagDecl->completeDefinition();
5919c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge  QualType VaListTagType = Context->getRecordType(VaListTagDecl);
5920fb40e3f10ccef93c4f8fb6bd4fe5a108fa6cd369Meador Inge  Context->VaListTagTy = VaListTagType;
5921c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge
5922c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge  // } __va_list_tag;
5923c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge  TypedefDecl *VaListTagTypedefDecl
5924c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge    = TypedefDecl::Create(const_cast<ASTContext &>(*Context),
5925c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge                          Context->getTranslationUnitDecl(),
5926c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge                          SourceLocation(), SourceLocation(),
5927c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge                          &Context->Idents.get("__va_list_tag"),
5928c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge                          Context->getTrivialTypeSourceInfo(VaListTagType));
5929c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge  QualType VaListTagTypedefType =
5930c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge    Context->getTypedefType(VaListTagTypedefDecl);
5931c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge
5932c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge  // typedef __va_list_tag __builtin_va_list[1];
5933c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge  llvm::APInt Size(Context->getTypeSize(Context->getSizeType()), 1);
5934c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge  QualType VaListTagArrayType
5935c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge    = Context->getConstantArrayType(VaListTagTypedefType,
5936c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge                                      Size, ArrayType::Normal,0);
5937c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge  TypeSourceInfo *TInfo
5938c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge    = Context->getTrivialTypeSourceInfo(VaListTagArrayType);
5939c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge  TypedefDecl *VaListTypedefDecl
5940c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge    = TypedefDecl::Create(const_cast<ASTContext &>(*Context),
5941c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge                          Context->getTranslationUnitDecl(),
5942c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge                          SourceLocation(), SourceLocation(),
5943c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge                          &Context->Idents.get("__builtin_va_list"),
5944c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge                          TInfo);
5945c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge
5946c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge  return VaListTypedefDecl;
5947c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge}
5948c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge
5949c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Ingestatic TypedefDecl *CreatePNaClABIBuiltinVaListDecl(const ASTContext *Context) {
5950c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge  // typedef int __builtin_va_list[4];
5951c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge  llvm::APInt Size(Context->getTypeSize(Context->getSizeType()), 4);
5952c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge  QualType IntArrayType
5953c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge    = Context->getConstantArrayType(Context->IntTy,
5954c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge				    Size, ArrayType::Normal, 0);
5955c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge  TypedefDecl *VaListTypedefDecl
5956c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge    = TypedefDecl::Create(const_cast<ASTContext &>(*Context),
5957c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge                          Context->getTranslationUnitDecl(),
5958c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge                          SourceLocation(), SourceLocation(),
5959c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge                          &Context->Idents.get("__builtin_va_list"),
5960c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge                          Context->getTrivialTypeSourceInfo(IntArrayType));
5961c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge
5962c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge  return VaListTypedefDecl;
5963c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge}
5964c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge
5965eae5a820bced67465c8517793a1602dfaeed8a06Logan Chienstatic TypedefDecl *
5966eae5a820bced67465c8517793a1602dfaeed8a06Logan ChienCreateAAPCSABIBuiltinVaListDecl(const ASTContext *Context) {
5967eae5a820bced67465c8517793a1602dfaeed8a06Logan Chien  RecordDecl *VaListDecl;
5968eae5a820bced67465c8517793a1602dfaeed8a06Logan Chien  if (Context->getLangOpts().CPlusPlus) {
5969eae5a820bced67465c8517793a1602dfaeed8a06Logan Chien    // namespace std { struct __va_list {
5970eae5a820bced67465c8517793a1602dfaeed8a06Logan Chien    NamespaceDecl *NS;
5971eae5a820bced67465c8517793a1602dfaeed8a06Logan Chien    NS = NamespaceDecl::Create(const_cast<ASTContext &>(*Context),
5972eae5a820bced67465c8517793a1602dfaeed8a06Logan Chien                               Context->getTranslationUnitDecl(),
5973eae5a820bced67465c8517793a1602dfaeed8a06Logan Chien                               /*Inline*/false, SourceLocation(),
5974eae5a820bced67465c8517793a1602dfaeed8a06Logan Chien                               SourceLocation(), &Context->Idents.get("std"),
5975eae5a820bced67465c8517793a1602dfaeed8a06Logan Chien                               /*PrevDecl*/0);
5976eae5a820bced67465c8517793a1602dfaeed8a06Logan Chien
5977eae5a820bced67465c8517793a1602dfaeed8a06Logan Chien    VaListDecl = CXXRecordDecl::Create(*Context, TTK_Struct,
5978eae5a820bced67465c8517793a1602dfaeed8a06Logan Chien                                       Context->getTranslationUnitDecl(),
5979eae5a820bced67465c8517793a1602dfaeed8a06Logan Chien                                       SourceLocation(), SourceLocation(),
5980eae5a820bced67465c8517793a1602dfaeed8a06Logan Chien                                       &Context->Idents.get("__va_list"));
5981eae5a820bced67465c8517793a1602dfaeed8a06Logan Chien
5982eae5a820bced67465c8517793a1602dfaeed8a06Logan Chien    VaListDecl->setDeclContext(NS);
5983eae5a820bced67465c8517793a1602dfaeed8a06Logan Chien
5984eae5a820bced67465c8517793a1602dfaeed8a06Logan Chien  } else {
5985eae5a820bced67465c8517793a1602dfaeed8a06Logan Chien    // struct __va_list {
5986eae5a820bced67465c8517793a1602dfaeed8a06Logan Chien    VaListDecl = CreateRecordDecl(*Context, TTK_Struct,
5987eae5a820bced67465c8517793a1602dfaeed8a06Logan Chien                                  Context->getTranslationUnitDecl(),
5988eae5a820bced67465c8517793a1602dfaeed8a06Logan Chien                                  &Context->Idents.get("__va_list"));
5989eae5a820bced67465c8517793a1602dfaeed8a06Logan Chien  }
5990eae5a820bced67465c8517793a1602dfaeed8a06Logan Chien
5991eae5a820bced67465c8517793a1602dfaeed8a06Logan Chien  VaListDecl->startDefinition();
5992eae5a820bced67465c8517793a1602dfaeed8a06Logan Chien
5993eae5a820bced67465c8517793a1602dfaeed8a06Logan Chien  // void * __ap;
5994eae5a820bced67465c8517793a1602dfaeed8a06Logan Chien  FieldDecl *Field = FieldDecl::Create(const_cast<ASTContext &>(*Context),
5995eae5a820bced67465c8517793a1602dfaeed8a06Logan Chien                                       VaListDecl,
5996eae5a820bced67465c8517793a1602dfaeed8a06Logan Chien                                       SourceLocation(),
5997eae5a820bced67465c8517793a1602dfaeed8a06Logan Chien                                       SourceLocation(),
5998eae5a820bced67465c8517793a1602dfaeed8a06Logan Chien                                       &Context->Idents.get("__ap"),
5999eae5a820bced67465c8517793a1602dfaeed8a06Logan Chien                                       Context->getPointerType(Context->VoidTy),
6000eae5a820bced67465c8517793a1602dfaeed8a06Logan Chien                                       /*TInfo=*/0,
6001eae5a820bced67465c8517793a1602dfaeed8a06Logan Chien                                       /*BitWidth=*/0,
6002eae5a820bced67465c8517793a1602dfaeed8a06Logan Chien                                       /*Mutable=*/false,
6003eae5a820bced67465c8517793a1602dfaeed8a06Logan Chien                                       ICIS_NoInit);
6004eae5a820bced67465c8517793a1602dfaeed8a06Logan Chien  Field->setAccess(AS_public);
6005eae5a820bced67465c8517793a1602dfaeed8a06Logan Chien  VaListDecl->addDecl(Field);
6006eae5a820bced67465c8517793a1602dfaeed8a06Logan Chien
6007eae5a820bced67465c8517793a1602dfaeed8a06Logan Chien  // };
6008eae5a820bced67465c8517793a1602dfaeed8a06Logan Chien  VaListDecl->completeDefinition();
6009eae5a820bced67465c8517793a1602dfaeed8a06Logan Chien
6010eae5a820bced67465c8517793a1602dfaeed8a06Logan Chien  // typedef struct __va_list __builtin_va_list;
6011eae5a820bced67465c8517793a1602dfaeed8a06Logan Chien  TypeSourceInfo *TInfo
6012eae5a820bced67465c8517793a1602dfaeed8a06Logan Chien    = Context->getTrivialTypeSourceInfo(Context->getRecordType(VaListDecl));
6013eae5a820bced67465c8517793a1602dfaeed8a06Logan Chien
6014eae5a820bced67465c8517793a1602dfaeed8a06Logan Chien  TypedefDecl *VaListTypeDecl
6015eae5a820bced67465c8517793a1602dfaeed8a06Logan Chien    = TypedefDecl::Create(const_cast<ASTContext &>(*Context),
6016eae5a820bced67465c8517793a1602dfaeed8a06Logan Chien                          Context->getTranslationUnitDecl(),
6017eae5a820bced67465c8517793a1602dfaeed8a06Logan Chien                          SourceLocation(), SourceLocation(),
6018eae5a820bced67465c8517793a1602dfaeed8a06Logan Chien                          &Context->Idents.get("__builtin_va_list"),
6019eae5a820bced67465c8517793a1602dfaeed8a06Logan Chien                          TInfo);
6020eae5a820bced67465c8517793a1602dfaeed8a06Logan Chien
6021eae5a820bced67465c8517793a1602dfaeed8a06Logan Chien  return VaListTypeDecl;
6022eae5a820bced67465c8517793a1602dfaeed8a06Logan Chien}
6023eae5a820bced67465c8517793a1602dfaeed8a06Logan Chien
6024b8409215523e5478b8b0aa9cdcd10038cf7651feUlrich Weigandstatic TypedefDecl *
6025b8409215523e5478b8b0aa9cdcd10038cf7651feUlrich WeigandCreateSystemZBuiltinVaListDecl(const ASTContext *Context) {
6026b8409215523e5478b8b0aa9cdcd10038cf7651feUlrich Weigand  // typedef struct __va_list_tag {
6027b8409215523e5478b8b0aa9cdcd10038cf7651feUlrich Weigand  RecordDecl *VaListTagDecl;
6028b8409215523e5478b8b0aa9cdcd10038cf7651feUlrich Weigand  VaListTagDecl = CreateRecordDecl(*Context, TTK_Struct,
6029b8409215523e5478b8b0aa9cdcd10038cf7651feUlrich Weigand                                   Context->getTranslationUnitDecl(),
6030b8409215523e5478b8b0aa9cdcd10038cf7651feUlrich Weigand                                   &Context->Idents.get("__va_list_tag"));
6031b8409215523e5478b8b0aa9cdcd10038cf7651feUlrich Weigand  VaListTagDecl->startDefinition();
6032b8409215523e5478b8b0aa9cdcd10038cf7651feUlrich Weigand
6033b8409215523e5478b8b0aa9cdcd10038cf7651feUlrich Weigand  const size_t NumFields = 4;
6034b8409215523e5478b8b0aa9cdcd10038cf7651feUlrich Weigand  QualType FieldTypes[NumFields];
6035b8409215523e5478b8b0aa9cdcd10038cf7651feUlrich Weigand  const char *FieldNames[NumFields];
6036b8409215523e5478b8b0aa9cdcd10038cf7651feUlrich Weigand
6037b8409215523e5478b8b0aa9cdcd10038cf7651feUlrich Weigand  //   long __gpr;
6038b8409215523e5478b8b0aa9cdcd10038cf7651feUlrich Weigand  FieldTypes[0] = Context->LongTy;
6039b8409215523e5478b8b0aa9cdcd10038cf7651feUlrich Weigand  FieldNames[0] = "__gpr";
6040b8409215523e5478b8b0aa9cdcd10038cf7651feUlrich Weigand
6041b8409215523e5478b8b0aa9cdcd10038cf7651feUlrich Weigand  //   long __fpr;
6042b8409215523e5478b8b0aa9cdcd10038cf7651feUlrich Weigand  FieldTypes[1] = Context->LongTy;
6043b8409215523e5478b8b0aa9cdcd10038cf7651feUlrich Weigand  FieldNames[1] = "__fpr";
6044b8409215523e5478b8b0aa9cdcd10038cf7651feUlrich Weigand
6045b8409215523e5478b8b0aa9cdcd10038cf7651feUlrich Weigand  //   void *__overflow_arg_area;
6046b8409215523e5478b8b0aa9cdcd10038cf7651feUlrich Weigand  FieldTypes[2] = Context->getPointerType(Context->VoidTy);
6047b8409215523e5478b8b0aa9cdcd10038cf7651feUlrich Weigand  FieldNames[2] = "__overflow_arg_area";
6048b8409215523e5478b8b0aa9cdcd10038cf7651feUlrich Weigand
6049b8409215523e5478b8b0aa9cdcd10038cf7651feUlrich Weigand  //   void *__reg_save_area;
6050b8409215523e5478b8b0aa9cdcd10038cf7651feUlrich Weigand  FieldTypes[3] = Context->getPointerType(Context->VoidTy);
6051b8409215523e5478b8b0aa9cdcd10038cf7651feUlrich Weigand  FieldNames[3] = "__reg_save_area";
6052b8409215523e5478b8b0aa9cdcd10038cf7651feUlrich Weigand
6053b8409215523e5478b8b0aa9cdcd10038cf7651feUlrich Weigand  // Create fields
6054b8409215523e5478b8b0aa9cdcd10038cf7651feUlrich Weigand  for (unsigned i = 0; i < NumFields; ++i) {
6055b8409215523e5478b8b0aa9cdcd10038cf7651feUlrich Weigand    FieldDecl *Field = FieldDecl::Create(const_cast<ASTContext &>(*Context),
6056b8409215523e5478b8b0aa9cdcd10038cf7651feUlrich Weigand                                         VaListTagDecl,
6057b8409215523e5478b8b0aa9cdcd10038cf7651feUlrich Weigand                                         SourceLocation(),
6058b8409215523e5478b8b0aa9cdcd10038cf7651feUlrich Weigand                                         SourceLocation(),
6059b8409215523e5478b8b0aa9cdcd10038cf7651feUlrich Weigand                                         &Context->Idents.get(FieldNames[i]),
6060b8409215523e5478b8b0aa9cdcd10038cf7651feUlrich Weigand                                         FieldTypes[i], /*TInfo=*/0,
6061b8409215523e5478b8b0aa9cdcd10038cf7651feUlrich Weigand                                         /*BitWidth=*/0,
6062b8409215523e5478b8b0aa9cdcd10038cf7651feUlrich Weigand                                         /*Mutable=*/false,
6063b8409215523e5478b8b0aa9cdcd10038cf7651feUlrich Weigand                                         ICIS_NoInit);
6064b8409215523e5478b8b0aa9cdcd10038cf7651feUlrich Weigand    Field->setAccess(AS_public);
6065b8409215523e5478b8b0aa9cdcd10038cf7651feUlrich Weigand    VaListTagDecl->addDecl(Field);
6066b8409215523e5478b8b0aa9cdcd10038cf7651feUlrich Weigand  }
6067b8409215523e5478b8b0aa9cdcd10038cf7651feUlrich Weigand  VaListTagDecl->completeDefinition();
6068b8409215523e5478b8b0aa9cdcd10038cf7651feUlrich Weigand  QualType VaListTagType = Context->getRecordType(VaListTagDecl);
6069b8409215523e5478b8b0aa9cdcd10038cf7651feUlrich Weigand  Context->VaListTagTy = VaListTagType;
6070b8409215523e5478b8b0aa9cdcd10038cf7651feUlrich Weigand
6071b8409215523e5478b8b0aa9cdcd10038cf7651feUlrich Weigand  // } __va_list_tag;
6072b8409215523e5478b8b0aa9cdcd10038cf7651feUlrich Weigand  TypedefDecl *VaListTagTypedefDecl
6073b8409215523e5478b8b0aa9cdcd10038cf7651feUlrich Weigand    = TypedefDecl::Create(const_cast<ASTContext &>(*Context),
6074b8409215523e5478b8b0aa9cdcd10038cf7651feUlrich Weigand                          Context->getTranslationUnitDecl(),
6075b8409215523e5478b8b0aa9cdcd10038cf7651feUlrich Weigand                          SourceLocation(), SourceLocation(),
6076b8409215523e5478b8b0aa9cdcd10038cf7651feUlrich Weigand                          &Context->Idents.get("__va_list_tag"),
6077b8409215523e5478b8b0aa9cdcd10038cf7651feUlrich Weigand                          Context->getTrivialTypeSourceInfo(VaListTagType));
6078b8409215523e5478b8b0aa9cdcd10038cf7651feUlrich Weigand  QualType VaListTagTypedefType =
6079b8409215523e5478b8b0aa9cdcd10038cf7651feUlrich Weigand    Context->getTypedefType(VaListTagTypedefDecl);
6080b8409215523e5478b8b0aa9cdcd10038cf7651feUlrich Weigand
6081b8409215523e5478b8b0aa9cdcd10038cf7651feUlrich Weigand  // typedef __va_list_tag __builtin_va_list[1];
6082b8409215523e5478b8b0aa9cdcd10038cf7651feUlrich Weigand  llvm::APInt Size(Context->getTypeSize(Context->getSizeType()), 1);
6083b8409215523e5478b8b0aa9cdcd10038cf7651feUlrich Weigand  QualType VaListTagArrayType
6084b8409215523e5478b8b0aa9cdcd10038cf7651feUlrich Weigand    = Context->getConstantArrayType(VaListTagTypedefType,
6085b8409215523e5478b8b0aa9cdcd10038cf7651feUlrich Weigand                                      Size, ArrayType::Normal,0);
6086b8409215523e5478b8b0aa9cdcd10038cf7651feUlrich Weigand  TypeSourceInfo *TInfo
6087b8409215523e5478b8b0aa9cdcd10038cf7651feUlrich Weigand    = Context->getTrivialTypeSourceInfo(VaListTagArrayType);
6088b8409215523e5478b8b0aa9cdcd10038cf7651feUlrich Weigand  TypedefDecl *VaListTypedefDecl
6089b8409215523e5478b8b0aa9cdcd10038cf7651feUlrich Weigand    = TypedefDecl::Create(const_cast<ASTContext &>(*Context),
6090b8409215523e5478b8b0aa9cdcd10038cf7651feUlrich Weigand                          Context->getTranslationUnitDecl(),
6091b8409215523e5478b8b0aa9cdcd10038cf7651feUlrich Weigand                          SourceLocation(), SourceLocation(),
6092b8409215523e5478b8b0aa9cdcd10038cf7651feUlrich Weigand                          &Context->Idents.get("__builtin_va_list"),
6093b8409215523e5478b8b0aa9cdcd10038cf7651feUlrich Weigand                          TInfo);
6094b8409215523e5478b8b0aa9cdcd10038cf7651feUlrich Weigand
6095b8409215523e5478b8b0aa9cdcd10038cf7651feUlrich Weigand  return VaListTypedefDecl;
6096b8409215523e5478b8b0aa9cdcd10038cf7651feUlrich Weigand}
6097b8409215523e5478b8b0aa9cdcd10038cf7651feUlrich Weigand
6098c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Ingestatic TypedefDecl *CreateVaListDecl(const ASTContext *Context,
6099c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge                                     TargetInfo::BuiltinVaListKind Kind) {
6100c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge  switch (Kind) {
6101c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge  case TargetInfo::CharPtrBuiltinVaList:
6102c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge    return CreateCharPtrBuiltinVaListDecl(Context);
6103c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge  case TargetInfo::VoidPtrBuiltinVaList:
6104c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge    return CreateVoidPtrBuiltinVaListDecl(Context);
6105c264e16a42b3f6c36521857a29ea0949d9781c22Tim Northover  case TargetInfo::AArch64ABIBuiltinVaList:
6106c264e16a42b3f6c36521857a29ea0949d9781c22Tim Northover    return CreateAArch64ABIBuiltinVaListDecl(Context);
6107c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge  case TargetInfo::PowerABIBuiltinVaList:
6108c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge    return CreatePowerABIBuiltinVaListDecl(Context);
6109c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge  case TargetInfo::X86_64ABIBuiltinVaList:
6110c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge    return CreateX86_64ABIBuiltinVaListDecl(Context);
6111c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge  case TargetInfo::PNaClABIBuiltinVaList:
6112c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge    return CreatePNaClABIBuiltinVaListDecl(Context);
6113eae5a820bced67465c8517793a1602dfaeed8a06Logan Chien  case TargetInfo::AAPCSABIBuiltinVaList:
6114eae5a820bced67465c8517793a1602dfaeed8a06Logan Chien    return CreateAAPCSABIBuiltinVaListDecl(Context);
6115b8409215523e5478b8b0aa9cdcd10038cf7651feUlrich Weigand  case TargetInfo::SystemZBuiltinVaList:
6116b8409215523e5478b8b0aa9cdcd10038cf7651feUlrich Weigand    return CreateSystemZBuiltinVaListDecl(Context);
6117c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge  }
6118c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge
6119c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge  llvm_unreachable("Unhandled __builtin_va_list type kind");
6120c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge}
6121c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge
6122c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador IngeTypedefDecl *ASTContext::getBuiltinVaListDecl() const {
6123c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge  if (!BuiltinVaListDecl)
6124c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge    BuiltinVaListDecl = CreateVaListDecl(this, Target->getBuiltinVaListKind());
6125c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge
6126c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge  return BuiltinVaListDecl;
6127c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge}
6128c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge
6129fb40e3f10ccef93c4f8fb6bd4fe5a108fa6cd369Meador IngeQualType ASTContext::getVaListTagType() const {
6130fb40e3f10ccef93c4f8fb6bd4fe5a108fa6cd369Meador Inge  // Force the creation of VaListTagTy by building the __builtin_va_list
6131fb40e3f10ccef93c4f8fb6bd4fe5a108fa6cd369Meador Inge  // declaration.
6132fb40e3f10ccef93c4f8fb6bd4fe5a108fa6cd369Meador Inge  if (VaListTagTy.isNull())
6133fb40e3f10ccef93c4f8fb6bd4fe5a108fa6cd369Meador Inge    (void) getBuiltinVaListDecl();
6134fb40e3f10ccef93c4f8fb6bd4fe5a108fa6cd369Meador Inge
6135fb40e3f10ccef93c4f8fb6bd4fe5a108fa6cd369Meador Inge  return VaListTagTy;
6136fb40e3f10ccef93c4f8fb6bd4fe5a108fa6cd369Meador Inge}
6137fb40e3f10ccef93c4f8fb6bd4fe5a108fa6cd369Meador Inge
6138a526c5c67e5a0473c340903ee542ce570119665fTed Kremenekvoid ASTContext::setObjCConstantStringInterface(ObjCInterfaceDecl *Decl) {
61391eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  assert(ObjCConstantStringType.isNull() &&
61402198891824c38d45b2279de5d5e3ef9394eb457cSteve Naroff         "'NSConstantString' type already set!");
61411eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
6142a526c5c67e5a0473c340903ee542ce570119665fTed Kremenek  ObjCConstantStringType = getObjCInterfaceType(Decl);
61432198891824c38d45b2279de5d5e3ef9394eb457cSteve Naroff}
61442198891824c38d45b2279de5d5e3ef9394eb457cSteve Naroff
61450bd6feb9e9d40fc889fd47e899985125a43dfed8John McCall/// \brief Retrieve the template name that corresponds to a non-empty
61460bd6feb9e9d40fc889fd47e899985125a43dfed8John McCall/// lookup.
61474ba2a17694148e16eaa8d3917f657ffcd3667be4Jay FoadTemplateName
61484ba2a17694148e16eaa8d3917f657ffcd3667be4Jay FoadASTContext::getOverloadedTemplateName(UnresolvedSetIterator Begin,
61494ba2a17694148e16eaa8d3917f657ffcd3667be4Jay Foad                                      UnresolvedSetIterator End) const {
61500bd6feb9e9d40fc889fd47e899985125a43dfed8John McCall  unsigned size = End - Begin;
61510bd6feb9e9d40fc889fd47e899985125a43dfed8John McCall  assert(size > 1 && "set is not overloaded!");
61527532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor
61530bd6feb9e9d40fc889fd47e899985125a43dfed8John McCall  void *memory = Allocate(sizeof(OverloadedTemplateStorage) +
61540bd6feb9e9d40fc889fd47e899985125a43dfed8John McCall                          size * sizeof(FunctionTemplateDecl*));
61550bd6feb9e9d40fc889fd47e899985125a43dfed8John McCall  OverloadedTemplateStorage *OT = new(memory) OverloadedTemplateStorage(size);
61560bd6feb9e9d40fc889fd47e899985125a43dfed8John McCall
61570bd6feb9e9d40fc889fd47e899985125a43dfed8John McCall  NamedDecl **Storage = OT->getStorage();
6158eec51cf1ba5f0e62c9cdb81b5c63babdd6e649abJohn McCall  for (UnresolvedSetIterator I = Begin; I != End; ++I) {
61590bd6feb9e9d40fc889fd47e899985125a43dfed8John McCall    NamedDecl *D = *I;
61600bd6feb9e9d40fc889fd47e899985125a43dfed8John McCall    assert(isa<FunctionTemplateDecl>(D) ||
61610bd6feb9e9d40fc889fd47e899985125a43dfed8John McCall           (isa<UsingShadowDecl>(D) &&
61620bd6feb9e9d40fc889fd47e899985125a43dfed8John McCall            isa<FunctionTemplateDecl>(D->getUnderlyingDecl())));
61630bd6feb9e9d40fc889fd47e899985125a43dfed8John McCall    *Storage++ = D;
61647532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor  }
61657532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor
61660bd6feb9e9d40fc889fd47e899985125a43dfed8John McCall  return TemplateName(OT);
61677532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor}
61687532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor
6169d99cbe66403ee39c2ee58024b9582b95649a4fc5Douglas Gregor/// \brief Retrieve the template name that represents a qualified
6170d99cbe66403ee39c2ee58024b9582b95649a4fc5Douglas Gregor/// template name such as \c std::vector.
61714ba2a17694148e16eaa8d3917f657ffcd3667be4Jay FoadTemplateName
61724ba2a17694148e16eaa8d3917f657ffcd3667be4Jay FoadASTContext::getQualifiedTemplateName(NestedNameSpecifier *NNS,
61734ba2a17694148e16eaa8d3917f657ffcd3667be4Jay Foad                                     bool TemplateKeyword,
61744ba2a17694148e16eaa8d3917f657ffcd3667be4Jay Foad                                     TemplateDecl *Template) const {
61750f0ea2a96534c615ff5fdd81363989b23cf2164aDouglas Gregor  assert(NNS && "Missing nested-name-specifier in qualified template name");
61760f0ea2a96534c615ff5fdd81363989b23cf2164aDouglas Gregor
6177789b1f640205e81b5af250693246120f1ce9d147Douglas Gregor  // FIXME: Canonicalization?
6178d99cbe66403ee39c2ee58024b9582b95649a4fc5Douglas Gregor  llvm::FoldingSetNodeID ID;
6179d99cbe66403ee39c2ee58024b9582b95649a4fc5Douglas Gregor  QualifiedTemplateName::Profile(ID, NNS, TemplateKeyword, Template);
61801eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
6181d99cbe66403ee39c2ee58024b9582b95649a4fc5Douglas Gregor  void *InsertPos = 0;
6182d99cbe66403ee39c2ee58024b9582b95649a4fc5Douglas Gregor  QualifiedTemplateName *QTN =
61830bd6feb9e9d40fc889fd47e899985125a43dfed8John McCall    QualifiedTemplateNames.FindNodeOrInsertPos(ID, InsertPos);
6184d99cbe66403ee39c2ee58024b9582b95649a4fc5Douglas Gregor  if (!QTN) {
61852f47cab092cd2dcbfe7e003fa865499caf198dc2Richard Smith    QTN = new (*this, llvm::alignOf<QualifiedTemplateName>())
61862f47cab092cd2dcbfe7e003fa865499caf198dc2Richard Smith        QualifiedTemplateName(NNS, TemplateKeyword, Template);
6187d99cbe66403ee39c2ee58024b9582b95649a4fc5Douglas Gregor    QualifiedTemplateNames.InsertNode(QTN, InsertPos);
6188d99cbe66403ee39c2ee58024b9582b95649a4fc5Douglas Gregor  }
61891eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
6190d99cbe66403ee39c2ee58024b9582b95649a4fc5Douglas Gregor  return TemplateName(QTN);
6191d99cbe66403ee39c2ee58024b9582b95649a4fc5Douglas Gregor}
6192d99cbe66403ee39c2ee58024b9582b95649a4fc5Douglas Gregor
61937532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor/// \brief Retrieve the template name that represents a dependent
61947532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor/// template name such as \c MetaFun::template apply.
61954ba2a17694148e16eaa8d3917f657ffcd3667be4Jay FoadTemplateName
61964ba2a17694148e16eaa8d3917f657ffcd3667be4Jay FoadASTContext::getDependentTemplateName(NestedNameSpecifier *NNS,
61974ba2a17694148e16eaa8d3917f657ffcd3667be4Jay Foad                                     const IdentifierInfo *Name) const {
61981eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  assert((!NNS || NNS->isDependent()) &&
61993b6afbb99a1c44b4076f8e15fb7311405941b306Douglas Gregor         "Nested name specifier must be dependent");
62007532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor
62017532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor  llvm::FoldingSetNodeID ID;
62027532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor  DependentTemplateName::Profile(ID, NNS, Name);
62037532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor
62047532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor  void *InsertPos = 0;
62057532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor  DependentTemplateName *QTN =
62067532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor    DependentTemplateNames.FindNodeOrInsertPos(ID, InsertPos);
62077532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor
62087532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor  if (QTN)
62097532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor    return TemplateName(QTN);
62107532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor
62117532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor  NestedNameSpecifier *CanonNNS = getCanonicalNestedNameSpecifier(NNS);
62127532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor  if (CanonNNS == NNS) {
62132f47cab092cd2dcbfe7e003fa865499caf198dc2Richard Smith    QTN = new (*this, llvm::alignOf<DependentTemplateName>())
62142f47cab092cd2dcbfe7e003fa865499caf198dc2Richard Smith        DependentTemplateName(NNS, Name);
62157532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor  } else {
62167532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor    TemplateName Canon = getDependentTemplateName(CanonNNS, Name);
62172f47cab092cd2dcbfe7e003fa865499caf198dc2Richard Smith    QTN = new (*this, llvm::alignOf<DependentTemplateName>())
62182f47cab092cd2dcbfe7e003fa865499caf198dc2Richard Smith        DependentTemplateName(NNS, Name, Canon);
6219789b1f640205e81b5af250693246120f1ce9d147Douglas Gregor    DependentTemplateName *CheckQTN =
6220789b1f640205e81b5af250693246120f1ce9d147Douglas Gregor      DependentTemplateNames.FindNodeOrInsertPos(ID, InsertPos);
6221789b1f640205e81b5af250693246120f1ce9d147Douglas Gregor    assert(!CheckQTN && "Dependent type name canonicalization broken");
6222789b1f640205e81b5af250693246120f1ce9d147Douglas Gregor    (void)CheckQTN;
62237532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor  }
62247532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor
62257532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor  DependentTemplateNames.InsertNode(QTN, InsertPos);
62267532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor  return TemplateName(QTN);
62277532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor}
62287532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor
6229ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor/// \brief Retrieve the template name that represents a dependent
6230ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor/// template name such as \c MetaFun::template operator+.
6231ca1bdd7c269a2390d43c040a60511edd017ee130Douglas GregorTemplateName
6232ca1bdd7c269a2390d43c040a60511edd017ee130Douglas GregorASTContext::getDependentTemplateName(NestedNameSpecifier *NNS,
62334ba2a17694148e16eaa8d3917f657ffcd3667be4Jay Foad                                     OverloadedOperatorKind Operator) const {
6234ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor  assert((!NNS || NNS->isDependent()) &&
6235ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor         "Nested name specifier must be dependent");
6236ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor
6237ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor  llvm::FoldingSetNodeID ID;
6238ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor  DependentTemplateName::Profile(ID, NNS, Operator);
6239ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor
6240ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor  void *InsertPos = 0;
6241789b1f640205e81b5af250693246120f1ce9d147Douglas Gregor  DependentTemplateName *QTN
6242789b1f640205e81b5af250693246120f1ce9d147Douglas Gregor    = DependentTemplateNames.FindNodeOrInsertPos(ID, InsertPos);
6243ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor
6244ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor  if (QTN)
6245ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor    return TemplateName(QTN);
6246ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor
6247ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor  NestedNameSpecifier *CanonNNS = getCanonicalNestedNameSpecifier(NNS);
6248ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor  if (CanonNNS == NNS) {
62492f47cab092cd2dcbfe7e003fa865499caf198dc2Richard Smith    QTN = new (*this, llvm::alignOf<DependentTemplateName>())
62502f47cab092cd2dcbfe7e003fa865499caf198dc2Richard Smith        DependentTemplateName(NNS, Operator);
6251ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor  } else {
6252ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor    TemplateName Canon = getDependentTemplateName(CanonNNS, Operator);
62532f47cab092cd2dcbfe7e003fa865499caf198dc2Richard Smith    QTN = new (*this, llvm::alignOf<DependentTemplateName>())
62542f47cab092cd2dcbfe7e003fa865499caf198dc2Richard Smith        DependentTemplateName(NNS, Operator, Canon);
6255789b1f640205e81b5af250693246120f1ce9d147Douglas Gregor
6256789b1f640205e81b5af250693246120f1ce9d147Douglas Gregor    DependentTemplateName *CheckQTN
6257789b1f640205e81b5af250693246120f1ce9d147Douglas Gregor      = DependentTemplateNames.FindNodeOrInsertPos(ID, InsertPos);
6258789b1f640205e81b5af250693246120f1ce9d147Douglas Gregor    assert(!CheckQTN && "Dependent template name canonicalization broken");
6259789b1f640205e81b5af250693246120f1ce9d147Douglas Gregor    (void)CheckQTN;
6260ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor  }
6261ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor
6262ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor  DependentTemplateNames.InsertNode(QTN, InsertPos);
6263ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor  return TemplateName(QTN);
6264ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor}
6265ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor
62661aee05d08b2184acadeb36de300e216390780d6cDouglas GregorTemplateName
6267146060435c3efce95c95a092c7a1eb651cfb9ae0John McCallASTContext::getSubstTemplateTemplateParm(TemplateTemplateParmDecl *param,
6268146060435c3efce95c95a092c7a1eb651cfb9ae0John McCall                                         TemplateName replacement) const {
6269146060435c3efce95c95a092c7a1eb651cfb9ae0John McCall  llvm::FoldingSetNodeID ID;
6270146060435c3efce95c95a092c7a1eb651cfb9ae0John McCall  SubstTemplateTemplateParmStorage::Profile(ID, param, replacement);
6271146060435c3efce95c95a092c7a1eb651cfb9ae0John McCall
6272146060435c3efce95c95a092c7a1eb651cfb9ae0John McCall  void *insertPos = 0;
6273146060435c3efce95c95a092c7a1eb651cfb9ae0John McCall  SubstTemplateTemplateParmStorage *subst
6274146060435c3efce95c95a092c7a1eb651cfb9ae0John McCall    = SubstTemplateTemplateParms.FindNodeOrInsertPos(ID, insertPos);
6275146060435c3efce95c95a092c7a1eb651cfb9ae0John McCall
6276146060435c3efce95c95a092c7a1eb651cfb9ae0John McCall  if (!subst) {
6277146060435c3efce95c95a092c7a1eb651cfb9ae0John McCall    subst = new (*this) SubstTemplateTemplateParmStorage(param, replacement);
6278146060435c3efce95c95a092c7a1eb651cfb9ae0John McCall    SubstTemplateTemplateParms.InsertNode(subst, insertPos);
6279146060435c3efce95c95a092c7a1eb651cfb9ae0John McCall  }
6280146060435c3efce95c95a092c7a1eb651cfb9ae0John McCall
6281146060435c3efce95c95a092c7a1eb651cfb9ae0John McCall  return TemplateName(subst);
6282146060435c3efce95c95a092c7a1eb651cfb9ae0John McCall}
6283146060435c3efce95c95a092c7a1eb651cfb9ae0John McCall
6284146060435c3efce95c95a092c7a1eb651cfb9ae0John McCallTemplateName
62851aee05d08b2184acadeb36de300e216390780d6cDouglas GregorASTContext::getSubstTemplateTemplateParmPack(TemplateTemplateParmDecl *Param,
62861aee05d08b2184acadeb36de300e216390780d6cDouglas Gregor                                       const TemplateArgument &ArgPack) const {
62871aee05d08b2184acadeb36de300e216390780d6cDouglas Gregor  ASTContext &Self = const_cast<ASTContext &>(*this);
62881aee05d08b2184acadeb36de300e216390780d6cDouglas Gregor  llvm::FoldingSetNodeID ID;
62891aee05d08b2184acadeb36de300e216390780d6cDouglas Gregor  SubstTemplateTemplateParmPackStorage::Profile(ID, Self, Param, ArgPack);
62901aee05d08b2184acadeb36de300e216390780d6cDouglas Gregor
62911aee05d08b2184acadeb36de300e216390780d6cDouglas Gregor  void *InsertPos = 0;
62921aee05d08b2184acadeb36de300e216390780d6cDouglas Gregor  SubstTemplateTemplateParmPackStorage *Subst
62931aee05d08b2184acadeb36de300e216390780d6cDouglas Gregor    = SubstTemplateTemplateParmPacks.FindNodeOrInsertPos(ID, InsertPos);
62941aee05d08b2184acadeb36de300e216390780d6cDouglas Gregor
62951aee05d08b2184acadeb36de300e216390780d6cDouglas Gregor  if (!Subst) {
6296146060435c3efce95c95a092c7a1eb651cfb9ae0John McCall    Subst = new (*this) SubstTemplateTemplateParmPackStorage(Param,
62971aee05d08b2184acadeb36de300e216390780d6cDouglas Gregor                                                           ArgPack.pack_size(),
62981aee05d08b2184acadeb36de300e216390780d6cDouglas Gregor                                                         ArgPack.pack_begin());
62991aee05d08b2184acadeb36de300e216390780d6cDouglas Gregor    SubstTemplateTemplateParmPacks.InsertNode(Subst, InsertPos);
63001aee05d08b2184acadeb36de300e216390780d6cDouglas Gregor  }
63011aee05d08b2184acadeb36de300e216390780d6cDouglas Gregor
63021aee05d08b2184acadeb36de300e216390780d6cDouglas Gregor  return TemplateName(Subst);
63031aee05d08b2184acadeb36de300e216390780d6cDouglas Gregor}
63041aee05d08b2184acadeb36de300e216390780d6cDouglas Gregor
6305b4e66d5259f90e9aae4d40fc5de801e046c7df94Douglas Gregor/// getFromTargetType - Given one of the integer types provided by
6306d934112e6170b0fd940d8e40db6936cea2cdcf62Douglas Gregor/// TargetInfo, produce the corresponding type. The unsigned @p Type
6307d934112e6170b0fd940d8e40db6936cea2cdcf62Douglas Gregor/// is actually a value of type @c TargetInfo::IntType.
6308e27ec8ad56dbf1efb2de004b90fbbb86f740e3f1John McCallCanQualType ASTContext::getFromTargetType(unsigned Type) const {
6309b4e66d5259f90e9aae4d40fc5de801e046c7df94Douglas Gregor  switch (Type) {
6310e27ec8ad56dbf1efb2de004b90fbbb86f740e3f1John McCall  case TargetInfo::NoInt: return CanQualType();
6311b4e66d5259f90e9aae4d40fc5de801e046c7df94Douglas Gregor  case TargetInfo::SignedShort: return ShortTy;
6312b4e66d5259f90e9aae4d40fc5de801e046c7df94Douglas Gregor  case TargetInfo::UnsignedShort: return UnsignedShortTy;
6313b4e66d5259f90e9aae4d40fc5de801e046c7df94Douglas Gregor  case TargetInfo::SignedInt: return IntTy;
6314b4e66d5259f90e9aae4d40fc5de801e046c7df94Douglas Gregor  case TargetInfo::UnsignedInt: return UnsignedIntTy;
6315b4e66d5259f90e9aae4d40fc5de801e046c7df94Douglas Gregor  case TargetInfo::SignedLong: return LongTy;
6316b4e66d5259f90e9aae4d40fc5de801e046c7df94Douglas Gregor  case TargetInfo::UnsignedLong: return UnsignedLongTy;
6317b4e66d5259f90e9aae4d40fc5de801e046c7df94Douglas Gregor  case TargetInfo::SignedLongLong: return LongLongTy;
6318b4e66d5259f90e9aae4d40fc5de801e046c7df94Douglas Gregor  case TargetInfo::UnsignedLongLong: return UnsignedLongLongTy;
6319b4e66d5259f90e9aae4d40fc5de801e046c7df94Douglas Gregor  }
6320b4e66d5259f90e9aae4d40fc5de801e046c7df94Douglas Gregor
6321b219cfc4d75f0a03630b7c4509ef791b7e97b2c8David Blaikie  llvm_unreachable("Unhandled TargetInfo::IntType value");
6322b4e66d5259f90e9aae4d40fc5de801e046c7df94Douglas Gregor}
6323b6ccaac65ca72f72954eb3893bbd940bedd23f00Ted Kremenek
6324b6ccaac65ca72f72954eb3893bbd940bedd23f00Ted Kremenek//===----------------------------------------------------------------------===//
6325b6ccaac65ca72f72954eb3893bbd940bedd23f00Ted Kremenek//                        Type Predicates.
6326b6ccaac65ca72f72954eb3893bbd940bedd23f00Ted Kremenek//===----------------------------------------------------------------------===//
6327b6ccaac65ca72f72954eb3893bbd940bedd23f00Ted Kremenek
63284fd83ea566f4a0c083001c84b75da6cc8c99c1d6Fariborz Jahanian/// getObjCGCAttr - Returns one of GCNone, Weak or Strong objc's
63294fd83ea566f4a0c083001c84b75da6cc8c99c1d6Fariborz Jahanian/// garbage collection attribute.
63304fd83ea566f4a0c083001c84b75da6cc8c99c1d6Fariborz Jahanian///
6331ae278a3a57595349a411f6474938d4dd1b263a0eJohn McCallQualifiers::GC ASTContext::getObjCGCAttrKind(QualType Ty) const {
63324e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie  if (getLangOpts().getGC() == LangOptions::NonGC)
6333ae278a3a57595349a411f6474938d4dd1b263a0eJohn McCall    return Qualifiers::GCNone;
6334ae278a3a57595349a411f6474938d4dd1b263a0eJohn McCall
63354e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie  assert(getLangOpts().ObjC1);
6336ae278a3a57595349a411f6474938d4dd1b263a0eJohn McCall  Qualifiers::GC GCAttrs = Ty.getObjCGCAttr();
6337ae278a3a57595349a411f6474938d4dd1b263a0eJohn McCall
6338ae278a3a57595349a411f6474938d4dd1b263a0eJohn McCall  // Default behaviour under objective-C's gc is for ObjC pointers
6339ae278a3a57595349a411f6474938d4dd1b263a0eJohn McCall  // (or pointers to them) be treated as though they were declared
6340ae278a3a57595349a411f6474938d4dd1b263a0eJohn McCall  // as __strong.
6341ae278a3a57595349a411f6474938d4dd1b263a0eJohn McCall  if (GCAttrs == Qualifiers::GCNone) {
6342ae278a3a57595349a411f6474938d4dd1b263a0eJohn McCall    if (Ty->isObjCObjectPointerType() || Ty->isBlockPointerType())
6343ae278a3a57595349a411f6474938d4dd1b263a0eJohn McCall      return Qualifiers::Strong;
6344ae278a3a57595349a411f6474938d4dd1b263a0eJohn McCall    else if (Ty->isPointerType())
6345ae278a3a57595349a411f6474938d4dd1b263a0eJohn McCall      return getObjCGCAttrKind(Ty->getAs<PointerType>()->getPointeeType());
6346ae278a3a57595349a411f6474938d4dd1b263a0eJohn McCall  } else {
6347ae278a3a57595349a411f6474938d4dd1b263a0eJohn McCall    // It's not valid to set GC attributes on anything that isn't a
6348ae278a3a57595349a411f6474938d4dd1b263a0eJohn McCall    // pointer.
6349ae278a3a57595349a411f6474938d4dd1b263a0eJohn McCall#ifndef NDEBUG
6350ae278a3a57595349a411f6474938d4dd1b263a0eJohn McCall    QualType CT = Ty->getCanonicalTypeInternal();
6351ae278a3a57595349a411f6474938d4dd1b263a0eJohn McCall    while (const ArrayType *AT = dyn_cast<ArrayType>(CT))
6352ae278a3a57595349a411f6474938d4dd1b263a0eJohn McCall      CT = AT->getElementType();
6353ae278a3a57595349a411f6474938d4dd1b263a0eJohn McCall    assert(CT->isAnyPointerType() || CT->isBlockPointerType());
6354ae278a3a57595349a411f6474938d4dd1b263a0eJohn McCall#endif
63554fd83ea566f4a0c083001c84b75da6cc8c99c1d6Fariborz Jahanian  }
6356b7d2553edd2532d29b98b9e76bcf6a62bc48b417Chris Lattner  return GCAttrs;
63574fd83ea566f4a0c083001c84b75da6cc8c99c1d6Fariborz Jahanian}
63584fd83ea566f4a0c083001c84b75da6cc8c99c1d6Fariborz Jahanian
63596ac46a4a521366d7ab36ebe2ce4e624ab96b06f9Chris Lattner//===----------------------------------------------------------------------===//
63606ac46a4a521366d7ab36ebe2ce4e624ab96b06f9Chris Lattner//                        Type Compatibility Testing
63616ac46a4a521366d7ab36ebe2ce4e624ab96b06f9Chris Lattner//===----------------------------------------------------------------------===//
6362770951b5bb6028a8d326ddb4a13cef7d4a128162Chris Lattner
63631eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// areCompatVectorTypes - Return true if the two specified vector types are
63646ac46a4a521366d7ab36ebe2ce4e624ab96b06f9Chris Lattner/// compatible.
63656ac46a4a521366d7ab36ebe2ce4e624ab96b06f9Chris Lattnerstatic bool areCompatVectorTypes(const VectorType *LHS,
63666ac46a4a521366d7ab36ebe2ce4e624ab96b06f9Chris Lattner                                 const VectorType *RHS) {
6367467b27b9a24bdc823218ad1ad0e37673b6cc1e83John McCall  assert(LHS->isCanonicalUnqualified() && RHS->isCanonicalUnqualified());
63686ac46a4a521366d7ab36ebe2ce4e624ab96b06f9Chris Lattner  return LHS->getElementType() == RHS->getElementType() &&
636961710854be2b098428aff5316e64bd34b30fbcb7Chris Lattner         LHS->getNumElements() == RHS->getNumElements();
63706ac46a4a521366d7ab36ebe2ce4e624ab96b06f9Chris Lattner}
63716ac46a4a521366d7ab36ebe2ce4e624ab96b06f9Chris Lattner
6372255210ef415b9893f0e3794e8d9a704194c12f3cDouglas Gregorbool ASTContext::areCompatibleVectorTypes(QualType FirstVec,
6373255210ef415b9893f0e3794e8d9a704194c12f3cDouglas Gregor                                          QualType SecondVec) {
6374255210ef415b9893f0e3794e8d9a704194c12f3cDouglas Gregor  assert(FirstVec->isVectorType() && "FirstVec should be a vector type");
6375255210ef415b9893f0e3794e8d9a704194c12f3cDouglas Gregor  assert(SecondVec->isVectorType() && "SecondVec should be a vector type");
6376255210ef415b9893f0e3794e8d9a704194c12f3cDouglas Gregor
6377255210ef415b9893f0e3794e8d9a704194c12f3cDouglas Gregor  if (hasSameUnqualifiedType(FirstVec, SecondVec))
6378255210ef415b9893f0e3794e8d9a704194c12f3cDouglas Gregor    return true;
6379255210ef415b9893f0e3794e8d9a704194c12f3cDouglas Gregor
6380f69eb7cf8e616b5aad7911ec6f79b24b0a009227Bob Wilson  // Treat Neon vector types and most AltiVec vector types as if they are the
6381f69eb7cf8e616b5aad7911ec6f79b24b0a009227Bob Wilson  // equivalent GCC vector types.
6382255210ef415b9893f0e3794e8d9a704194c12f3cDouglas Gregor  const VectorType *First = FirstVec->getAs<VectorType>();
6383255210ef415b9893f0e3794e8d9a704194c12f3cDouglas Gregor  const VectorType *Second = SecondVec->getAs<VectorType>();
6384f69eb7cf8e616b5aad7911ec6f79b24b0a009227Bob Wilson  if (First->getNumElements() == Second->getNumElements() &&
6385255210ef415b9893f0e3794e8d9a704194c12f3cDouglas Gregor      hasSameType(First->getElementType(), Second->getElementType()) &&
6386f69eb7cf8e616b5aad7911ec6f79b24b0a009227Bob Wilson      First->getVectorKind() != VectorType::AltiVecPixel &&
6387f69eb7cf8e616b5aad7911ec6f79b24b0a009227Bob Wilson      First->getVectorKind() != VectorType::AltiVecBool &&
6388f69eb7cf8e616b5aad7911ec6f79b24b0a009227Bob Wilson      Second->getVectorKind() != VectorType::AltiVecPixel &&
6389f69eb7cf8e616b5aad7911ec6f79b24b0a009227Bob Wilson      Second->getVectorKind() != VectorType::AltiVecBool)
6390255210ef415b9893f0e3794e8d9a704194c12f3cDouglas Gregor    return true;
6391255210ef415b9893f0e3794e8d9a704194c12f3cDouglas Gregor
6392255210ef415b9893f0e3794e8d9a704194c12f3cDouglas Gregor  return false;
6393255210ef415b9893f0e3794e8d9a704194c12f3cDouglas Gregor}
6394255210ef415b9893f0e3794e8d9a704194c12f3cDouglas Gregor
63954084c306635b70f37029dca938444e6013f08684Steve Naroff//===----------------------------------------------------------------------===//
63964084c306635b70f37029dca938444e6013f08684Steve Naroff// ObjCQualifiedIdTypesAreCompatible - Compatibility testing for qualified id's.
63974084c306635b70f37029dca938444e6013f08684Steve Naroff//===----------------------------------------------------------------------===//
63984084c306635b70f37029dca938444e6013f08684Steve Naroff
63994084c306635b70f37029dca938444e6013f08684Steve Naroff/// ProtocolCompatibleWithProtocol - return 'true' if 'lProto' is in the
64004084c306635b70f37029dca938444e6013f08684Steve Naroff/// inheritance hierarchy of 'rProto'.
64014ba2a17694148e16eaa8d3917f657ffcd3667be4Jay Foadbool
64024ba2a17694148e16eaa8d3917f657ffcd3667be4Jay FoadASTContext::ProtocolCompatibleWithProtocol(ObjCProtocolDecl *lProto,
64034ba2a17694148e16eaa8d3917f657ffcd3667be4Jay Foad                                           ObjCProtocolDecl *rProto) const {
64043fc73ee0c613715ebce78e30b4d050ea715a007dDouglas Gregor  if (declaresSameEntity(lProto, rProto))
640514108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff    return true;
64064084c306635b70f37029dca938444e6013f08684Steve Naroff  for (ObjCProtocolDecl::protocol_iterator PI = rProto->protocol_begin(),
64074084c306635b70f37029dca938444e6013f08684Steve Naroff       E = rProto->protocol_end(); PI != E; ++PI)
64084084c306635b70f37029dca938444e6013f08684Steve Naroff    if (ProtocolCompatibleWithProtocol(lProto, *PI))
64094084c306635b70f37029dca938444e6013f08684Steve Naroff      return true;
64104084c306635b70f37029dca938444e6013f08684Steve Naroff  return false;
64114084c306635b70f37029dca938444e6013f08684Steve Naroff}
641214108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff
64134c3b8a3ed28c52dc56d3a1b9670d71e5f7070c62Dmitri Gribenko/// QualifiedIdConformsQualifiedId - compare id<pr,...> with id<pr1,...>
64144084c306635b70f37029dca938444e6013f08684Steve Naroff/// return true if lhs's protocols conform to rhs's protocol; false
64154084c306635b70f37029dca938444e6013f08684Steve Naroff/// otherwise.
64164084c306635b70f37029dca938444e6013f08684Steve Naroffbool ASTContext::QualifiedIdConformsQualifiedId(QualType lhs, QualType rhs) {
64174084c306635b70f37029dca938444e6013f08684Steve Naroff  if (lhs->isObjCQualifiedIdType() && rhs->isObjCQualifiedIdType())
64184084c306635b70f37029dca938444e6013f08684Steve Naroff    return ObjCQualifiedIdTypesAreCompatible(lhs, rhs, false);
64194084c306635b70f37029dca938444e6013f08684Steve Naroff  return false;
64204084c306635b70f37029dca938444e6013f08684Steve Naroff}
64214084c306635b70f37029dca938444e6013f08684Steve Naroff
64224c3b8a3ed28c52dc56d3a1b9670d71e5f7070c62Dmitri Gribenko/// ObjCQualifiedClassTypesAreCompatible - compare  Class<pr,...> and
64234c3b8a3ed28c52dc56d3a1b9670d71e5f7070c62Dmitri Gribenko/// Class<pr1, ...>.
6424a8f8dac6a29f6d33474a38a32ce9dd859b696da9Fariborz Jahanianbool ASTContext::ObjCQualifiedClassTypesAreCompatible(QualType lhs,
6425a8f8dac6a29f6d33474a38a32ce9dd859b696da9Fariborz Jahanian                                                      QualType rhs) {
6426a8f8dac6a29f6d33474a38a32ce9dd859b696da9Fariborz Jahanian  const ObjCObjectPointerType *lhsQID = lhs->getAs<ObjCObjectPointerType>();
6427a8f8dac6a29f6d33474a38a32ce9dd859b696da9Fariborz Jahanian  const ObjCObjectPointerType *rhsOPT = rhs->getAs<ObjCObjectPointerType>();
6428a8f8dac6a29f6d33474a38a32ce9dd859b696da9Fariborz Jahanian  assert ((lhsQID && rhsOPT) && "ObjCQualifiedClassTypesAreCompatible");
6429a8f8dac6a29f6d33474a38a32ce9dd859b696da9Fariborz Jahanian
6430a8f8dac6a29f6d33474a38a32ce9dd859b696da9Fariborz Jahanian  for (ObjCObjectPointerType::qual_iterator I = lhsQID->qual_begin(),
6431a8f8dac6a29f6d33474a38a32ce9dd859b696da9Fariborz Jahanian       E = lhsQID->qual_end(); I != E; ++I) {
6432a8f8dac6a29f6d33474a38a32ce9dd859b696da9Fariborz Jahanian    bool match = false;
6433a8f8dac6a29f6d33474a38a32ce9dd859b696da9Fariborz Jahanian    ObjCProtocolDecl *lhsProto = *I;
6434a8f8dac6a29f6d33474a38a32ce9dd859b696da9Fariborz Jahanian    for (ObjCObjectPointerType::qual_iterator J = rhsOPT->qual_begin(),
6435a8f8dac6a29f6d33474a38a32ce9dd859b696da9Fariborz Jahanian         E = rhsOPT->qual_end(); J != E; ++J) {
6436a8f8dac6a29f6d33474a38a32ce9dd859b696da9Fariborz Jahanian      ObjCProtocolDecl *rhsProto = *J;
6437a8f8dac6a29f6d33474a38a32ce9dd859b696da9Fariborz Jahanian      if (ProtocolCompatibleWithProtocol(lhsProto, rhsProto)) {
6438a8f8dac6a29f6d33474a38a32ce9dd859b696da9Fariborz Jahanian        match = true;
6439a8f8dac6a29f6d33474a38a32ce9dd859b696da9Fariborz Jahanian        break;
6440a8f8dac6a29f6d33474a38a32ce9dd859b696da9Fariborz Jahanian      }
6441a8f8dac6a29f6d33474a38a32ce9dd859b696da9Fariborz Jahanian    }
6442a8f8dac6a29f6d33474a38a32ce9dd859b696da9Fariborz Jahanian    if (!match)
6443a8f8dac6a29f6d33474a38a32ce9dd859b696da9Fariborz Jahanian      return false;
6444a8f8dac6a29f6d33474a38a32ce9dd859b696da9Fariborz Jahanian  }
6445a8f8dac6a29f6d33474a38a32ce9dd859b696da9Fariborz Jahanian  return true;
6446a8f8dac6a29f6d33474a38a32ce9dd859b696da9Fariborz Jahanian}
6447a8f8dac6a29f6d33474a38a32ce9dd859b696da9Fariborz Jahanian
64484084c306635b70f37029dca938444e6013f08684Steve Naroff/// ObjCQualifiedIdTypesAreCompatible - We know that one of lhs/rhs is an
64494084c306635b70f37029dca938444e6013f08684Steve Naroff/// ObjCQualifiedIDType.
64504084c306635b70f37029dca938444e6013f08684Steve Naroffbool ASTContext::ObjCQualifiedIdTypesAreCompatible(QualType lhs, QualType rhs,
64514084c306635b70f37029dca938444e6013f08684Steve Naroff                                                   bool compare) {
64524084c306635b70f37029dca938444e6013f08684Steve Naroff  // Allow id<P..> and an 'id' or void* type in all cases.
64531eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  if (lhs->isVoidPointerType() ||
64544084c306635b70f37029dca938444e6013f08684Steve Naroff      lhs->isObjCIdType() || lhs->isObjCClassType())
64554084c306635b70f37029dca938444e6013f08684Steve Naroff    return true;
64561eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  else if (rhs->isVoidPointerType() ||
64574084c306635b70f37029dca938444e6013f08684Steve Naroff           rhs->isObjCIdType() || rhs->isObjCClassType())
64584084c306635b70f37029dca938444e6013f08684Steve Naroff    return true;
64594084c306635b70f37029dca938444e6013f08684Steve Naroff
64604084c306635b70f37029dca938444e6013f08684Steve Naroff  if (const ObjCObjectPointerType *lhsQID = lhs->getAsObjCQualifiedIdType()) {
6461183700f494ec9b6701b6efe82bcb25f4c79ba561John McCall    const ObjCObjectPointerType *rhsOPT = rhs->getAs<ObjCObjectPointerType>();
64621eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
64634084c306635b70f37029dca938444e6013f08684Steve Naroff    if (!rhsOPT) return false;
64641eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
64654084c306635b70f37029dca938444e6013f08684Steve Naroff    if (rhsOPT->qual_empty()) {
64661eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      // If the RHS is a unqualified interface pointer "NSString*",
64674084c306635b70f37029dca938444e6013f08684Steve Naroff      // make sure we check the class hierarchy.
64684084c306635b70f37029dca938444e6013f08684Steve Naroff      if (ObjCInterfaceDecl *rhsID = rhsOPT->getInterfaceDecl()) {
64694084c306635b70f37029dca938444e6013f08684Steve Naroff        for (ObjCObjectPointerType::qual_iterator I = lhsQID->qual_begin(),
64704084c306635b70f37029dca938444e6013f08684Steve Naroff             E = lhsQID->qual_end(); I != E; ++I) {
64714084c306635b70f37029dca938444e6013f08684Steve Naroff          // when comparing an id<P> on lhs with a static type on rhs,
64724084c306635b70f37029dca938444e6013f08684Steve Naroff          // see if static class implements all of id's protocols, directly or
64734084c306635b70f37029dca938444e6013f08684Steve Naroff          // through its super class and categories.
64740fd8904c5f71a11d29f67716c3ebdf7ad1c855fbFariborz Jahanian          if (!rhsID->ClassImplementsProtocol(*I, true))
64754084c306635b70f37029dca938444e6013f08684Steve Naroff            return false;
64764084c306635b70f37029dca938444e6013f08684Steve Naroff        }
64774084c306635b70f37029dca938444e6013f08684Steve Naroff      }
64784084c306635b70f37029dca938444e6013f08684Steve Naroff      // If there are no qualifiers and no interface, we have an 'id'.
64794084c306635b70f37029dca938444e6013f08684Steve Naroff      return true;
64804084c306635b70f37029dca938444e6013f08684Steve Naroff    }
64811eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    // Both the right and left sides have qualifiers.
64824084c306635b70f37029dca938444e6013f08684Steve Naroff    for (ObjCObjectPointerType::qual_iterator I = lhsQID->qual_begin(),
64834084c306635b70f37029dca938444e6013f08684Steve Naroff         E = lhsQID->qual_end(); I != E; ++I) {
64844084c306635b70f37029dca938444e6013f08684Steve Naroff      ObjCProtocolDecl *lhsProto = *I;
64854084c306635b70f37029dca938444e6013f08684Steve Naroff      bool match = false;
6486de2e22d33afec98324a66a358dfe0951b3c7259aSteve Naroff
6487de2e22d33afec98324a66a358dfe0951b3c7259aSteve Naroff      // when comparing an id<P> on lhs with a static type on rhs,
6488de2e22d33afec98324a66a358dfe0951b3c7259aSteve Naroff      // see if static class implements all of id's protocols, directly or
6489de2e22d33afec98324a66a358dfe0951b3c7259aSteve Naroff      // through its super class and categories.
64904084c306635b70f37029dca938444e6013f08684Steve Naroff      for (ObjCObjectPointerType::qual_iterator J = rhsOPT->qual_begin(),
64914084c306635b70f37029dca938444e6013f08684Steve Naroff           E = rhsOPT->qual_end(); J != E; ++J) {
64924084c306635b70f37029dca938444e6013f08684Steve Naroff        ObjCProtocolDecl *rhsProto = *J;
64934084c306635b70f37029dca938444e6013f08684Steve Naroff        if (ProtocolCompatibleWithProtocol(lhsProto, rhsProto) ||
64944084c306635b70f37029dca938444e6013f08684Steve Naroff            (compare && ProtocolCompatibleWithProtocol(rhsProto, lhsProto))) {
64954084c306635b70f37029dca938444e6013f08684Steve Naroff          match = true;
64968f16756441450ed9fb39316e47d107fc2a1ef35bSteve Naroff          break;
64978f16756441450ed9fb39316e47d107fc2a1ef35bSteve Naroff        }
6498de2e22d33afec98324a66a358dfe0951b3c7259aSteve Naroff      }
64991eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      // If the RHS is a qualified interface pointer "NSString<P>*",
65004084c306635b70f37029dca938444e6013f08684Steve Naroff      // make sure we check the class hierarchy.
65014084c306635b70f37029dca938444e6013f08684Steve Naroff      if (ObjCInterfaceDecl *rhsID = rhsOPT->getInterfaceDecl()) {
65024084c306635b70f37029dca938444e6013f08684Steve Naroff        for (ObjCObjectPointerType::qual_iterator I = lhsQID->qual_begin(),
65034084c306635b70f37029dca938444e6013f08684Steve Naroff             E = lhsQID->qual_end(); I != E; ++I) {
65044084c306635b70f37029dca938444e6013f08684Steve Naroff          // when comparing an id<P> on lhs with a static type on rhs,
65054084c306635b70f37029dca938444e6013f08684Steve Naroff          // see if static class implements all of id's protocols, directly or
65064084c306635b70f37029dca938444e6013f08684Steve Naroff          // through its super class and categories.
65070fd8904c5f71a11d29f67716c3ebdf7ad1c855fbFariborz Jahanian          if (rhsID->ClassImplementsProtocol(*I, true)) {
65084084c306635b70f37029dca938444e6013f08684Steve Naroff            match = true;
65094084c306635b70f37029dca938444e6013f08684Steve Naroff            break;
65104084c306635b70f37029dca938444e6013f08684Steve Naroff          }
65114084c306635b70f37029dca938444e6013f08684Steve Naroff        }
65124084c306635b70f37029dca938444e6013f08684Steve Naroff      }
65134084c306635b70f37029dca938444e6013f08684Steve Naroff      if (!match)
6514de2e22d33afec98324a66a358dfe0951b3c7259aSteve Naroff        return false;
6515de2e22d33afec98324a66a358dfe0951b3c7259aSteve Naroff    }
65161eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
6517de2e22d33afec98324a66a358dfe0951b3c7259aSteve Naroff    return true;
6518de2e22d33afec98324a66a358dfe0951b3c7259aSteve Naroff  }
65191eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
65204084c306635b70f37029dca938444e6013f08684Steve Naroff  const ObjCObjectPointerType *rhsQID = rhs->getAsObjCQualifiedIdType();
65214084c306635b70f37029dca938444e6013f08684Steve Naroff  assert(rhsQID && "One of the LHS/RHS should be id<x>");
65224084c306635b70f37029dca938444e6013f08684Steve Naroff
65231eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  if (const ObjCObjectPointerType *lhsOPT =
65244084c306635b70f37029dca938444e6013f08684Steve Naroff        lhs->getAsObjCInterfacePointerType()) {
6525de5b17ea3c74d2844ed035a1edfa6479866139b5Fariborz Jahanian    // If both the right and left sides have qualifiers.
65264084c306635b70f37029dca938444e6013f08684Steve Naroff    for (ObjCObjectPointerType::qual_iterator I = lhsOPT->qual_begin(),
65274084c306635b70f37029dca938444e6013f08684Steve Naroff         E = lhsOPT->qual_end(); I != E; ++I) {
65284084c306635b70f37029dca938444e6013f08684Steve Naroff      ObjCProtocolDecl *lhsProto = *I;
65294084c306635b70f37029dca938444e6013f08684Steve Naroff      bool match = false;
65304084c306635b70f37029dca938444e6013f08684Steve Naroff
6531de5b17ea3c74d2844ed035a1edfa6479866139b5Fariborz Jahanian      // when comparing an id<P> on rhs with a static type on lhs,
65324084c306635b70f37029dca938444e6013f08684Steve Naroff      // see if static class implements all of id's protocols, directly or
65334084c306635b70f37029dca938444e6013f08684Steve Naroff      // through its super class and categories.
6534de5b17ea3c74d2844ed035a1edfa6479866139b5Fariborz Jahanian      // First, lhs protocols in the qualifier list must be found, direct
6535de5b17ea3c74d2844ed035a1edfa6479866139b5Fariborz Jahanian      // or indirect in rhs's qualifier list or it is a mismatch.
65364084c306635b70f37029dca938444e6013f08684Steve Naroff      for (ObjCObjectPointerType::qual_iterator J = rhsQID->qual_begin(),
65374084c306635b70f37029dca938444e6013f08684Steve Naroff           E = rhsQID->qual_end(); J != E; ++J) {
65384084c306635b70f37029dca938444e6013f08684Steve Naroff        ObjCProtocolDecl *rhsProto = *J;
65394084c306635b70f37029dca938444e6013f08684Steve Naroff        if (ProtocolCompatibleWithProtocol(lhsProto, rhsProto) ||
65404084c306635b70f37029dca938444e6013f08684Steve Naroff            (compare && ProtocolCompatibleWithProtocol(rhsProto, lhsProto))) {
65414084c306635b70f37029dca938444e6013f08684Steve Naroff          match = true;
65424084c306635b70f37029dca938444e6013f08684Steve Naroff          break;
65434084c306635b70f37029dca938444e6013f08684Steve Naroff        }
65444084c306635b70f37029dca938444e6013f08684Steve Naroff      }
65454084c306635b70f37029dca938444e6013f08684Steve Naroff      if (!match)
65464084c306635b70f37029dca938444e6013f08684Steve Naroff        return false;
65474084c306635b70f37029dca938444e6013f08684Steve Naroff    }
6548de5b17ea3c74d2844ed035a1edfa6479866139b5Fariborz Jahanian
6549de5b17ea3c74d2844ed035a1edfa6479866139b5Fariborz Jahanian    // Static class's protocols, or its super class or category protocols
6550de5b17ea3c74d2844ed035a1edfa6479866139b5Fariborz Jahanian    // must be found, direct or indirect in rhs's qualifier list or it is a mismatch.
6551de5b17ea3c74d2844ed035a1edfa6479866139b5Fariborz Jahanian    if (ObjCInterfaceDecl *lhsID = lhsOPT->getInterfaceDecl()) {
6552de5b17ea3c74d2844ed035a1edfa6479866139b5Fariborz Jahanian      llvm::SmallPtrSet<ObjCProtocolDecl *, 8> LHSInheritedProtocols;
6553de5b17ea3c74d2844ed035a1edfa6479866139b5Fariborz Jahanian      CollectInheritedProtocols(lhsID, LHSInheritedProtocols);
6554de5b17ea3c74d2844ed035a1edfa6479866139b5Fariborz Jahanian      // This is rather dubious but matches gcc's behavior. If lhs has
6555de5b17ea3c74d2844ed035a1edfa6479866139b5Fariborz Jahanian      // no type qualifier and its class has no static protocol(s)
6556de5b17ea3c74d2844ed035a1edfa6479866139b5Fariborz Jahanian      // assume that it is mismatch.
6557de5b17ea3c74d2844ed035a1edfa6479866139b5Fariborz Jahanian      if (LHSInheritedProtocols.empty() && lhsOPT->qual_empty())
6558de5b17ea3c74d2844ed035a1edfa6479866139b5Fariborz Jahanian        return false;
6559de5b17ea3c74d2844ed035a1edfa6479866139b5Fariborz Jahanian      for (llvm::SmallPtrSet<ObjCProtocolDecl*,8>::iterator I =
6560de5b17ea3c74d2844ed035a1edfa6479866139b5Fariborz Jahanian           LHSInheritedProtocols.begin(),
6561de5b17ea3c74d2844ed035a1edfa6479866139b5Fariborz Jahanian           E = LHSInheritedProtocols.end(); I != E; ++I) {
6562de5b17ea3c74d2844ed035a1edfa6479866139b5Fariborz Jahanian        bool match = false;
6563de5b17ea3c74d2844ed035a1edfa6479866139b5Fariborz Jahanian        ObjCProtocolDecl *lhsProto = (*I);
6564de5b17ea3c74d2844ed035a1edfa6479866139b5Fariborz Jahanian        for (ObjCObjectPointerType::qual_iterator J = rhsQID->qual_begin(),
6565de5b17ea3c74d2844ed035a1edfa6479866139b5Fariborz Jahanian             E = rhsQID->qual_end(); J != E; ++J) {
6566de5b17ea3c74d2844ed035a1edfa6479866139b5Fariborz Jahanian          ObjCProtocolDecl *rhsProto = *J;
6567de5b17ea3c74d2844ed035a1edfa6479866139b5Fariborz Jahanian          if (ProtocolCompatibleWithProtocol(lhsProto, rhsProto) ||
6568de5b17ea3c74d2844ed035a1edfa6479866139b5Fariborz Jahanian              (compare && ProtocolCompatibleWithProtocol(rhsProto, lhsProto))) {
6569de5b17ea3c74d2844ed035a1edfa6479866139b5Fariborz Jahanian            match = true;
6570de5b17ea3c74d2844ed035a1edfa6479866139b5Fariborz Jahanian            break;
6571de5b17ea3c74d2844ed035a1edfa6479866139b5Fariborz Jahanian          }
6572de5b17ea3c74d2844ed035a1edfa6479866139b5Fariborz Jahanian        }
6573de5b17ea3c74d2844ed035a1edfa6479866139b5Fariborz Jahanian        if (!match)
6574de5b17ea3c74d2844ed035a1edfa6479866139b5Fariborz Jahanian          return false;
6575de5b17ea3c74d2844ed035a1edfa6479866139b5Fariborz Jahanian      }
6576de5b17ea3c74d2844ed035a1edfa6479866139b5Fariborz Jahanian    }
65774084c306635b70f37029dca938444e6013f08684Steve Naroff    return true;
65784084c306635b70f37029dca938444e6013f08684Steve Naroff  }
65794084c306635b70f37029dca938444e6013f08684Steve Naroff  return false;
65804084c306635b70f37029dca938444e6013f08684Steve Naroff}
65814084c306635b70f37029dca938444e6013f08684Steve Naroff
65824084c306635b70f37029dca938444e6013f08684Steve Naroff/// canAssignObjCInterfaces - Return true if the two interface types are
65834084c306635b70f37029dca938444e6013f08684Steve Naroff/// compatible for assignment from RHS to LHS.  This handles validation of any
65844084c306635b70f37029dca938444e6013f08684Steve Naroff/// protocol qualifiers on the LHS or RHS.
65854084c306635b70f37029dca938444e6013f08684Steve Naroff///
65864084c306635b70f37029dca938444e6013f08684Steve Naroffbool ASTContext::canAssignObjCInterfaces(const ObjCObjectPointerType *LHSOPT,
65874084c306635b70f37029dca938444e6013f08684Steve Naroff                                         const ObjCObjectPointerType *RHSOPT) {
6588c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall  const ObjCObjectType* LHS = LHSOPT->getObjectType();
6589c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall  const ObjCObjectType* RHS = RHSOPT->getObjectType();
6590c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall
65914084c306635b70f37029dca938444e6013f08684Steve Naroff  // If either type represents the built-in 'id' or 'Class' types, return true.
6592c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall  if (LHS->isObjCUnqualifiedIdOrClass() ||
6593c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall      RHS->isObjCUnqualifiedIdOrClass())
65944084c306635b70f37029dca938444e6013f08684Steve Naroff    return true;
65954084c306635b70f37029dca938444e6013f08684Steve Naroff
6596c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall  if (LHS->isObjCQualifiedId() || RHS->isObjCQualifiedId())
65971eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    return ObjCQualifiedIdTypesAreCompatible(QualType(LHSOPT,0),
65981eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump                                             QualType(RHSOPT,0),
65994084c306635b70f37029dca938444e6013f08684Steve Naroff                                             false);
6600a8f8dac6a29f6d33474a38a32ce9dd859b696da9Fariborz Jahanian
6601a8f8dac6a29f6d33474a38a32ce9dd859b696da9Fariborz Jahanian  if (LHS->isObjCQualifiedClass() && RHS->isObjCQualifiedClass())
6602a8f8dac6a29f6d33474a38a32ce9dd859b696da9Fariborz Jahanian    return ObjCQualifiedClassTypesAreCompatible(QualType(LHSOPT,0),
6603a8f8dac6a29f6d33474a38a32ce9dd859b696da9Fariborz Jahanian                                                QualType(RHSOPT,0));
6604a8f8dac6a29f6d33474a38a32ce9dd859b696da9Fariborz Jahanian
6605c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall  // If we have 2 user-defined types, fall into that path.
6606c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall  if (LHS->getInterface() && RHS->getInterface())
66074084c306635b70f37029dca938444e6013f08684Steve Naroff    return canAssignObjCInterfaces(LHS, RHS);
66081eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
66094084c306635b70f37029dca938444e6013f08684Steve Naroff  return false;
661014108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff}
661114108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff
6612132f2a2da34f378fc675b9e174564b0f52c31d98Fariborz Jahanian/// canAssignObjCInterfacesInBlockPointer - This routine is specifically written
6613fc8f0e14ad142ed811e90fbd9a30e419e301c717Chris Lattner/// for providing type-safety for objective-c pointers used to pass/return
6614132f2a2da34f378fc675b9e174564b0f52c31d98Fariborz Jahanian/// arguments in block literals. When passed as arguments, passing 'A*' where
6615132f2a2da34f378fc675b9e174564b0f52c31d98Fariborz Jahanian/// 'id' is expected is not OK. Passing 'Sub *" where 'Super *" is expected is
6616132f2a2da34f378fc675b9e174564b0f52c31d98Fariborz Jahanian/// not OK. For the return type, the opposite is not OK.
6617132f2a2da34f378fc675b9e174564b0f52c31d98Fariborz Jahanianbool ASTContext::canAssignObjCInterfacesInBlockPointer(
6618132f2a2da34f378fc675b9e174564b0f52c31d98Fariborz Jahanian                                         const ObjCObjectPointerType *LHSOPT,
6619a4fdbfad150ae37bddaa4094d3925a27a1a1cf3fFariborz Jahanian                                         const ObjCObjectPointerType *RHSOPT,
6620a4fdbfad150ae37bddaa4094d3925a27a1a1cf3fFariborz Jahanian                                         bool BlockReturnType) {
6621a98344869c278295f38e106583906377520079b8Fariborz Jahanian  if (RHSOPT->isObjCBuiltinType() || LHSOPT->isObjCIdType())
6622132f2a2da34f378fc675b9e174564b0f52c31d98Fariborz Jahanian    return true;
6623132f2a2da34f378fc675b9e174564b0f52c31d98Fariborz Jahanian
6624132f2a2da34f378fc675b9e174564b0f52c31d98Fariborz Jahanian  if (LHSOPT->isObjCBuiltinType()) {
6625132f2a2da34f378fc675b9e174564b0f52c31d98Fariborz Jahanian    return RHSOPT->isObjCBuiltinType() || RHSOPT->isObjCQualifiedIdType();
6626132f2a2da34f378fc675b9e174564b0f52c31d98Fariborz Jahanian  }
6627132f2a2da34f378fc675b9e174564b0f52c31d98Fariborz Jahanian
6628a98344869c278295f38e106583906377520079b8Fariborz Jahanian  if (LHSOPT->isObjCQualifiedIdType() || RHSOPT->isObjCQualifiedIdType())
6629132f2a2da34f378fc675b9e174564b0f52c31d98Fariborz Jahanian    return ObjCQualifiedIdTypesAreCompatible(QualType(LHSOPT,0),
6630132f2a2da34f378fc675b9e174564b0f52c31d98Fariborz Jahanian                                             QualType(RHSOPT,0),
6631132f2a2da34f378fc675b9e174564b0f52c31d98Fariborz Jahanian                                             false);
6632132f2a2da34f378fc675b9e174564b0f52c31d98Fariborz Jahanian
6633132f2a2da34f378fc675b9e174564b0f52c31d98Fariborz Jahanian  const ObjCInterfaceType* LHS = LHSOPT->getInterfaceType();
6634132f2a2da34f378fc675b9e174564b0f52c31d98Fariborz Jahanian  const ObjCInterfaceType* RHS = RHSOPT->getInterfaceType();
6635132f2a2da34f378fc675b9e174564b0f52c31d98Fariborz Jahanian  if (LHS && RHS)  { // We have 2 user-defined types.
6636132f2a2da34f378fc675b9e174564b0f52c31d98Fariborz Jahanian    if (LHS != RHS) {
6637132f2a2da34f378fc675b9e174564b0f52c31d98Fariborz Jahanian      if (LHS->getDecl()->isSuperClassOf(RHS->getDecl()))
6638a4fdbfad150ae37bddaa4094d3925a27a1a1cf3fFariborz Jahanian        return BlockReturnType;
6639132f2a2da34f378fc675b9e174564b0f52c31d98Fariborz Jahanian      if (RHS->getDecl()->isSuperClassOf(LHS->getDecl()))
6640a4fdbfad150ae37bddaa4094d3925a27a1a1cf3fFariborz Jahanian        return !BlockReturnType;
6641132f2a2da34f378fc675b9e174564b0f52c31d98Fariborz Jahanian    }
6642132f2a2da34f378fc675b9e174564b0f52c31d98Fariborz Jahanian    else
6643132f2a2da34f378fc675b9e174564b0f52c31d98Fariborz Jahanian      return true;
6644132f2a2da34f378fc675b9e174564b0f52c31d98Fariborz Jahanian  }
6645132f2a2da34f378fc675b9e174564b0f52c31d98Fariborz Jahanian  return false;
6646132f2a2da34f378fc675b9e174564b0f52c31d98Fariborz Jahanian}
6647132f2a2da34f378fc675b9e174564b0f52c31d98Fariborz Jahanian
6648e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanian/// getIntersectionOfProtocols - This routine finds the intersection of set
6649e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanian/// of protocols inherited from two distinct objective-c pointer objects.
6650e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanian/// It is used to build composite qualifier list of the composite type of
6651e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanian/// the conditional expression involving two objective-c pointer objects.
6652e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanianstatic
6653e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanianvoid getIntersectionOfProtocols(ASTContext &Context,
6654e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanian                                const ObjCObjectPointerType *LHSOPT,
6655e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanian                                const ObjCObjectPointerType *RHSOPT,
66565f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner      SmallVectorImpl<ObjCProtocolDecl *> &IntersectionOfProtocols) {
6657e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanian
6658c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall  const ObjCObjectType* LHS = LHSOPT->getObjectType();
6659c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall  const ObjCObjectType* RHS = RHSOPT->getObjectType();
6660c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall  assert(LHS->getInterface() && "LHS must have an interface base");
6661c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall  assert(RHS->getInterface() && "RHS must have an interface base");
6662e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanian
6663e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanian  llvm::SmallPtrSet<ObjCProtocolDecl *, 8> InheritedProtocolSet;
6664e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanian  unsigned LHSNumProtocols = LHS->getNumProtocols();
6665e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanian  if (LHSNumProtocols > 0)
6666e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanian    InheritedProtocolSet.insert(LHS->qual_begin(), LHS->qual_end());
6667e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanian  else {
6668432a8893f7e30d141d7f279bd00b741a3cdac81fFariborz Jahanian    llvm::SmallPtrSet<ObjCProtocolDecl *, 8> LHSInheritedProtocols;
6669c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall    Context.CollectInheritedProtocols(LHS->getInterface(),
6670c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall                                      LHSInheritedProtocols);
6671e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanian    InheritedProtocolSet.insert(LHSInheritedProtocols.begin(),
6672e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanian                                LHSInheritedProtocols.end());
6673e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanian  }
6674e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanian
6675e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanian  unsigned RHSNumProtocols = RHS->getNumProtocols();
6676e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanian  if (RHSNumProtocols > 0) {
6677cb421fa690da545b58a720abe5f1c49b166dbde7Dan Gohman    ObjCProtocolDecl **RHSProtocols =
6678cb421fa690da545b58a720abe5f1c49b166dbde7Dan Gohman      const_cast<ObjCProtocolDecl **>(RHS->qual_begin());
6679e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanian    for (unsigned i = 0; i < RHSNumProtocols; ++i)
6680e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanian      if (InheritedProtocolSet.count(RHSProtocols[i]))
6681e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanian        IntersectionOfProtocols.push_back(RHSProtocols[i]);
66823060178ad9df29789505c1e6debcfc80a3a13587Chad Rosier  } else {
6683432a8893f7e30d141d7f279bd00b741a3cdac81fFariborz Jahanian    llvm::SmallPtrSet<ObjCProtocolDecl *, 8> RHSInheritedProtocols;
6684c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall    Context.CollectInheritedProtocols(RHS->getInterface(),
6685c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall                                      RHSInheritedProtocols);
6686432a8893f7e30d141d7f279bd00b741a3cdac81fFariborz Jahanian    for (llvm::SmallPtrSet<ObjCProtocolDecl*,8>::iterator I =
6687432a8893f7e30d141d7f279bd00b741a3cdac81fFariborz Jahanian         RHSInheritedProtocols.begin(),
6688432a8893f7e30d141d7f279bd00b741a3cdac81fFariborz Jahanian         E = RHSInheritedProtocols.end(); I != E; ++I)
6689432a8893f7e30d141d7f279bd00b741a3cdac81fFariborz Jahanian      if (InheritedProtocolSet.count((*I)))
6690432a8893f7e30d141d7f279bd00b741a3cdac81fFariborz Jahanian        IntersectionOfProtocols.push_back((*I));
6691e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanian  }
6692e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanian}
6693e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanian
6694db07b3f7cdcb505329c1280d7cf70791739a7cadFariborz Jahanian/// areCommonBaseCompatible - Returns common base class of the two classes if
6695db07b3f7cdcb505329c1280d7cf70791739a7cadFariborz Jahanian/// one found. Note that this is O'2 algorithm. But it will be called as the
6696db07b3f7cdcb505329c1280d7cf70791739a7cadFariborz Jahanian/// last type comparison in a ?-exp of ObjC pointer types before a
6697db07b3f7cdcb505329c1280d7cf70791739a7cadFariborz Jahanian/// warning is issued. So, its invokation is extremely rare.
6698db07b3f7cdcb505329c1280d7cf70791739a7cadFariborz JahanianQualType ASTContext::areCommonBaseCompatible(
6699c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall                                          const ObjCObjectPointerType *Lptr,
6700c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall                                          const ObjCObjectPointerType *Rptr) {
6701c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall  const ObjCObjectType *LHS = Lptr->getObjectType();
6702c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall  const ObjCObjectType *RHS = Rptr->getObjectType();
6703c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall  const ObjCInterfaceDecl* LDecl = LHS->getInterface();
6704c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall  const ObjCInterfaceDecl* RDecl = RHS->getInterface();
670560ef308e51c71b760d7f598c1b763ceb7b768148Douglas Gregor  if (!LDecl || !RDecl || (declaresSameEntity(LDecl, RDecl)))
6706db07b3f7cdcb505329c1280d7cf70791739a7cadFariborz Jahanian    return QualType();
6707db07b3f7cdcb505329c1280d7cf70791739a7cadFariborz Jahanian
67087c2bdcb4d30f2d370b4367664e6a11b075ce2cb3Fariborz Jahanian  do {
6709c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall    LHS = cast<ObjCInterfaceType>(getObjCInterfaceType(LDecl));
6710e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanian    if (canAssignObjCInterfaces(LHS, RHS)) {
67115f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner      SmallVector<ObjCProtocolDecl *, 8> Protocols;
6712c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall      getIntersectionOfProtocols(*this, Lptr, Rptr, Protocols);
6713c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall
6714c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall      QualType Result = QualType(LHS, 0);
6715c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall      if (!Protocols.empty())
6716c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall        Result = getObjCObjectType(Result, Protocols.data(), Protocols.size());
6717c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall      Result = getObjCObjectPointerType(Result);
6718c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall      return Result;
6719e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanian    }
67207c2bdcb4d30f2d370b4367664e6a11b075ce2cb3Fariborz Jahanian  } while ((LDecl = LDecl->getSuperClass()));
6721db07b3f7cdcb505329c1280d7cf70791739a7cadFariborz Jahanian
6722db07b3f7cdcb505329c1280d7cf70791739a7cadFariborz Jahanian  return QualType();
6723db07b3f7cdcb505329c1280d7cf70791739a7cadFariborz Jahanian}
6724db07b3f7cdcb505329c1280d7cf70791739a7cadFariborz Jahanian
6725c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCallbool ASTContext::canAssignObjCInterfaces(const ObjCObjectType *LHS,
6726c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall                                         const ObjCObjectType *RHS) {
6727c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall  assert(LHS->getInterface() && "LHS is not an interface type");
6728c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall  assert(RHS->getInterface() && "RHS is not an interface type");
6729c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall
67306ac46a4a521366d7ab36ebe2ce4e624ab96b06f9Chris Lattner  // Verify that the base decls are compatible: the RHS must be a subclass of
67316ac46a4a521366d7ab36ebe2ce4e624ab96b06f9Chris Lattner  // the LHS.
6732c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall  if (!LHS->getInterface()->isSuperClassOf(RHS->getInterface()))
67336ac46a4a521366d7ab36ebe2ce4e624ab96b06f9Chris Lattner    return false;
67341eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
67356ac46a4a521366d7ab36ebe2ce4e624ab96b06f9Chris Lattner  // RHS must have a superset of the protocols in the LHS.  If the LHS is not
67366ac46a4a521366d7ab36ebe2ce4e624ab96b06f9Chris Lattner  // protocol qualified at all, then we are good.
6737c15cb2af27514ecc879daba9aa01389c5203685dSteve Naroff  if (LHS->getNumProtocols() == 0)
67386ac46a4a521366d7ab36ebe2ce4e624ab96b06f9Chris Lattner    return true;
67391eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
6740b7bc34a83aff8af09f2a78aa6d1dcafe18ad8619Fariborz Jahanian  // Okay, we know the LHS has protocol qualifiers.  If the RHS doesn't,
6741b7bc34a83aff8af09f2a78aa6d1dcafe18ad8619Fariborz Jahanian  // more detailed analysis is required.
6742b7bc34a83aff8af09f2a78aa6d1dcafe18ad8619Fariborz Jahanian  if (RHS->getNumProtocols() == 0) {
6743b7bc34a83aff8af09f2a78aa6d1dcafe18ad8619Fariborz Jahanian    // OK, if LHS is a superclass of RHS *and*
6744b7bc34a83aff8af09f2a78aa6d1dcafe18ad8619Fariborz Jahanian    // this superclass is assignment compatible with LHS.
6745b7bc34a83aff8af09f2a78aa6d1dcafe18ad8619Fariborz Jahanian    // false otherwise.
6746627788c29976fbeb4ad47bcfcb3576889070e357Fariborz Jahanian    bool IsSuperClass =
6747627788c29976fbeb4ad47bcfcb3576889070e357Fariborz Jahanian      LHS->getInterface()->isSuperClassOf(RHS->getInterface());
6748627788c29976fbeb4ad47bcfcb3576889070e357Fariborz Jahanian    if (IsSuperClass) {
6749b7bc34a83aff8af09f2a78aa6d1dcafe18ad8619Fariborz Jahanian      // OK if conversion of LHS to SuperClass results in narrowing of types
6750b7bc34a83aff8af09f2a78aa6d1dcafe18ad8619Fariborz Jahanian      // ; i.e., SuperClass may implement at least one of the protocols
6751b7bc34a83aff8af09f2a78aa6d1dcafe18ad8619Fariborz Jahanian      // in LHS's protocol list. Example, SuperObj<P1> = lhs<P1,P2> is ok.
6752b7bc34a83aff8af09f2a78aa6d1dcafe18ad8619Fariborz Jahanian      // But not SuperObj<P1,P2,P3> = lhs<P1,P2>.
6753b7bc34a83aff8af09f2a78aa6d1dcafe18ad8619Fariborz Jahanian      llvm::SmallPtrSet<ObjCProtocolDecl *, 8> SuperClassInheritedProtocols;
6754627788c29976fbeb4ad47bcfcb3576889070e357Fariborz Jahanian      CollectInheritedProtocols(RHS->getInterface(), SuperClassInheritedProtocols);
6755b7bc34a83aff8af09f2a78aa6d1dcafe18ad8619Fariborz Jahanian      // If super class has no protocols, it is not a match.
6756b7bc34a83aff8af09f2a78aa6d1dcafe18ad8619Fariborz Jahanian      if (SuperClassInheritedProtocols.empty())
6757b7bc34a83aff8af09f2a78aa6d1dcafe18ad8619Fariborz Jahanian        return false;
6758b7bc34a83aff8af09f2a78aa6d1dcafe18ad8619Fariborz Jahanian
6759b7bc34a83aff8af09f2a78aa6d1dcafe18ad8619Fariborz Jahanian      for (ObjCObjectType::qual_iterator LHSPI = LHS->qual_begin(),
6760b7bc34a83aff8af09f2a78aa6d1dcafe18ad8619Fariborz Jahanian           LHSPE = LHS->qual_end();
6761b7bc34a83aff8af09f2a78aa6d1dcafe18ad8619Fariborz Jahanian           LHSPI != LHSPE; LHSPI++) {
6762b7bc34a83aff8af09f2a78aa6d1dcafe18ad8619Fariborz Jahanian        bool SuperImplementsProtocol = false;
6763b7bc34a83aff8af09f2a78aa6d1dcafe18ad8619Fariborz Jahanian        ObjCProtocolDecl *LHSProto = (*LHSPI);
6764b7bc34a83aff8af09f2a78aa6d1dcafe18ad8619Fariborz Jahanian
6765b7bc34a83aff8af09f2a78aa6d1dcafe18ad8619Fariborz Jahanian        for (llvm::SmallPtrSet<ObjCProtocolDecl*,8>::iterator I =
6766b7bc34a83aff8af09f2a78aa6d1dcafe18ad8619Fariborz Jahanian             SuperClassInheritedProtocols.begin(),
6767b7bc34a83aff8af09f2a78aa6d1dcafe18ad8619Fariborz Jahanian             E = SuperClassInheritedProtocols.end(); I != E; ++I) {
6768b7bc34a83aff8af09f2a78aa6d1dcafe18ad8619Fariborz Jahanian          ObjCProtocolDecl *SuperClassProto = (*I);
6769b7bc34a83aff8af09f2a78aa6d1dcafe18ad8619Fariborz Jahanian          if (SuperClassProto->lookupProtocolNamed(LHSProto->getIdentifier())) {
6770b7bc34a83aff8af09f2a78aa6d1dcafe18ad8619Fariborz Jahanian            SuperImplementsProtocol = true;
6771b7bc34a83aff8af09f2a78aa6d1dcafe18ad8619Fariborz Jahanian            break;
6772b7bc34a83aff8af09f2a78aa6d1dcafe18ad8619Fariborz Jahanian          }
6773b7bc34a83aff8af09f2a78aa6d1dcafe18ad8619Fariborz Jahanian        }
6774b7bc34a83aff8af09f2a78aa6d1dcafe18ad8619Fariborz Jahanian        if (!SuperImplementsProtocol)
6775b7bc34a83aff8af09f2a78aa6d1dcafe18ad8619Fariborz Jahanian          return false;
6776b7bc34a83aff8af09f2a78aa6d1dcafe18ad8619Fariborz Jahanian      }
6777b7bc34a83aff8af09f2a78aa6d1dcafe18ad8619Fariborz Jahanian      return true;
6778b7bc34a83aff8af09f2a78aa6d1dcafe18ad8619Fariborz Jahanian    }
6779b7bc34a83aff8af09f2a78aa6d1dcafe18ad8619Fariborz Jahanian    return false;
6780b7bc34a83aff8af09f2a78aa6d1dcafe18ad8619Fariborz Jahanian  }
67811eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
6782c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall  for (ObjCObjectType::qual_iterator LHSPI = LHS->qual_begin(),
6783c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall                                     LHSPE = LHS->qual_end();
678491b0b0cf6b537cbcbca0038c7032f87161a41d31Steve Naroff       LHSPI != LHSPE; LHSPI++) {
678591b0b0cf6b537cbcbca0038c7032f87161a41d31Steve Naroff    bool RHSImplementsProtocol = false;
678691b0b0cf6b537cbcbca0038c7032f87161a41d31Steve Naroff
678791b0b0cf6b537cbcbca0038c7032f87161a41d31Steve Naroff    // If the RHS doesn't implement the protocol on the left, the types
678891b0b0cf6b537cbcbca0038c7032f87161a41d31Steve Naroff    // are incompatible.
6789c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall    for (ObjCObjectType::qual_iterator RHSPI = RHS->qual_begin(),
6790c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall                                       RHSPE = RHS->qual_end();
67918f16756441450ed9fb39316e47d107fc2a1ef35bSteve Naroff         RHSPI != RHSPE; RHSPI++) {
67928f16756441450ed9fb39316e47d107fc2a1ef35bSteve Naroff      if ((*RHSPI)->lookupProtocolNamed((*LHSPI)->getIdentifier())) {
679391b0b0cf6b537cbcbca0038c7032f87161a41d31Steve Naroff        RHSImplementsProtocol = true;
67948f16756441450ed9fb39316e47d107fc2a1ef35bSteve Naroff        break;
67958f16756441450ed9fb39316e47d107fc2a1ef35bSteve Naroff      }
679691b0b0cf6b537cbcbca0038c7032f87161a41d31Steve Naroff    }
679791b0b0cf6b537cbcbca0038c7032f87161a41d31Steve Naroff    // FIXME: For better diagnostics, consider passing back the protocol name.
679891b0b0cf6b537cbcbca0038c7032f87161a41d31Steve Naroff    if (!RHSImplementsProtocol)
679991b0b0cf6b537cbcbca0038c7032f87161a41d31Steve Naroff      return false;
680091b0b0cf6b537cbcbca0038c7032f87161a41d31Steve Naroff  }
680191b0b0cf6b537cbcbca0038c7032f87161a41d31Steve Naroff  // The RHS implements all protocols listed on the LHS.
680291b0b0cf6b537cbcbca0038c7032f87161a41d31Steve Naroff  return true;
68036ac46a4a521366d7ab36ebe2ce4e624ab96b06f9Chris Lattner}
68046ac46a4a521366d7ab36ebe2ce4e624ab96b06f9Chris Lattner
6805389bf46ae41241a656ed71b00ac2177d7f385651Steve Naroffbool ASTContext::areComparableObjCPointerTypes(QualType LHS, QualType RHS) {
6806389bf46ae41241a656ed71b00ac2177d7f385651Steve Naroff  // get the "pointed to" types
6807183700f494ec9b6701b6efe82bcb25f4c79ba561John McCall  const ObjCObjectPointerType *LHSOPT = LHS->getAs<ObjCObjectPointerType>();
6808183700f494ec9b6701b6efe82bcb25f4c79ba561John McCall  const ObjCObjectPointerType *RHSOPT = RHS->getAs<ObjCObjectPointerType>();
68091eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
681014108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff  if (!LHSOPT || !RHSOPT)
6811389bf46ae41241a656ed71b00ac2177d7f385651Steve Naroff    return false;
681214108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff
681314108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff  return canAssignObjCInterfaces(LHSOPT, RHSOPT) ||
681414108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff         canAssignObjCInterfaces(RHSOPT, LHSOPT);
6815389bf46ae41241a656ed71b00ac2177d7f385651Steve Naroff}
6816389bf46ae41241a656ed71b00ac2177d7f385651Steve Naroff
6817569c3166874324c24011f8ade6978421f0d39b3cDouglas Gregorbool ASTContext::canBindObjCObjectType(QualType To, QualType From) {
6818569c3166874324c24011f8ade6978421f0d39b3cDouglas Gregor  return canAssignObjCInterfaces(
6819569c3166874324c24011f8ade6978421f0d39b3cDouglas Gregor                getObjCObjectPointerType(To)->getAs<ObjCObjectPointerType>(),
6820569c3166874324c24011f8ade6978421f0d39b3cDouglas Gregor                getObjCObjectPointerType(From)->getAs<ObjCObjectPointerType>());
6821569c3166874324c24011f8ade6978421f0d39b3cDouglas Gregor}
6822569c3166874324c24011f8ade6978421f0d39b3cDouglas Gregor
68231eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// typesAreCompatible - C99 6.7.3p9: For two qualified types to be compatible,
6824ec0550fa3653d46560bf4484a2e988329c228e39Steve Naroff/// both shall have the identically qualified version of a compatible type.
68251eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// C99 6.2.7p1: Two types have compatible types if their types are the
6826ec0550fa3653d46560bf4484a2e988329c228e39Steve Naroff/// same. See 6.7.[2,3,5] for additional rules.
6827447234dd459a00a5ed9b7c3e066162cd7a75bf2dDouglas Gregorbool ASTContext::typesAreCompatible(QualType LHS, QualType RHS,
6828447234dd459a00a5ed9b7c3e066162cd7a75bf2dDouglas Gregor                                    bool CompareUnqualified) {
68294e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie  if (getLangOpts().CPlusPlus)
68300e709abafbd939326850501f795cc7a92c88a354Douglas Gregor    return hasSameType(LHS, RHS);
68310e709abafbd939326850501f795cc7a92c88a354Douglas Gregor
6832447234dd459a00a5ed9b7c3e066162cd7a75bf2dDouglas Gregor  return !mergeTypes(LHS, RHS, false, CompareUnqualified).isNull();
68333d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman}
68343d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman
6835c286f3835eb6001c61664cef5d610dfaf80a6e9bFariborz Jahanianbool ASTContext::propertyTypesAreCompatible(QualType LHS, QualType RHS) {
683682378391dfc3b2fc22f63dbf1552bd9aed39dc42Fariborz Jahanian  return typesAreCompatible(LHS, RHS);
6837c286f3835eb6001c61664cef5d610dfaf80a6e9bFariborz Jahanian}
6838c286f3835eb6001c61664cef5d610dfaf80a6e9bFariborz Jahanian
6839132f2a2da34f378fc675b9e174564b0f52c31d98Fariborz Jahanianbool ASTContext::typesAreBlockPointerCompatible(QualType LHS, QualType RHS) {
6840132f2a2da34f378fc675b9e174564b0f52c31d98Fariborz Jahanian  return !mergeTypes(LHS, RHS, true).isNull();
6841132f2a2da34f378fc675b9e174564b0f52c31d98Fariborz Jahanian}
6842132f2a2da34f378fc675b9e174564b0f52c31d98Fariborz Jahanian
68434846675e0e42d1802b0ffd8972a45e72aeb3758dPeter Collingbourne/// mergeTransparentUnionType - if T is a transparent union type and a member
68444846675e0e42d1802b0ffd8972a45e72aeb3758dPeter Collingbourne/// of T is compatible with SubType, return the merged type, else return
68454846675e0e42d1802b0ffd8972a45e72aeb3758dPeter Collingbourne/// QualType()
68464846675e0e42d1802b0ffd8972a45e72aeb3758dPeter CollingbourneQualType ASTContext::mergeTransparentUnionType(QualType T, QualType SubType,
68474846675e0e42d1802b0ffd8972a45e72aeb3758dPeter Collingbourne                                               bool OfBlockPointer,
68484846675e0e42d1802b0ffd8972a45e72aeb3758dPeter Collingbourne                                               bool Unqualified) {
68494846675e0e42d1802b0ffd8972a45e72aeb3758dPeter Collingbourne  if (const RecordType *UT = T->getAsUnionType()) {
68504846675e0e42d1802b0ffd8972a45e72aeb3758dPeter Collingbourne    RecordDecl *UD = UT->getDecl();
68514846675e0e42d1802b0ffd8972a45e72aeb3758dPeter Collingbourne    if (UD->hasAttr<TransparentUnionAttr>()) {
68524846675e0e42d1802b0ffd8972a45e72aeb3758dPeter Collingbourne      for (RecordDecl::field_iterator it = UD->field_begin(),
68534846675e0e42d1802b0ffd8972a45e72aeb3758dPeter Collingbourne           itend = UD->field_end(); it != itend; ++it) {
6854f91d7572ee7fa6f2926c8e7e816039a1154a59f8Peter Collingbourne        QualType ET = it->getType().getUnqualifiedType();
68554846675e0e42d1802b0ffd8972a45e72aeb3758dPeter Collingbourne        QualType MT = mergeTypes(ET, SubType, OfBlockPointer, Unqualified);
68564846675e0e42d1802b0ffd8972a45e72aeb3758dPeter Collingbourne        if (!MT.isNull())
68574846675e0e42d1802b0ffd8972a45e72aeb3758dPeter Collingbourne          return MT;
68584846675e0e42d1802b0ffd8972a45e72aeb3758dPeter Collingbourne      }
68594846675e0e42d1802b0ffd8972a45e72aeb3758dPeter Collingbourne    }
68604846675e0e42d1802b0ffd8972a45e72aeb3758dPeter Collingbourne  }
68614846675e0e42d1802b0ffd8972a45e72aeb3758dPeter Collingbourne
68624846675e0e42d1802b0ffd8972a45e72aeb3758dPeter Collingbourne  return QualType();
68634846675e0e42d1802b0ffd8972a45e72aeb3758dPeter Collingbourne}
68644846675e0e42d1802b0ffd8972a45e72aeb3758dPeter Collingbourne
68654846675e0e42d1802b0ffd8972a45e72aeb3758dPeter Collingbourne/// mergeFunctionArgumentTypes - merge two types which appear as function
68664846675e0e42d1802b0ffd8972a45e72aeb3758dPeter Collingbourne/// argument types
68674846675e0e42d1802b0ffd8972a45e72aeb3758dPeter CollingbourneQualType ASTContext::mergeFunctionArgumentTypes(QualType lhs, QualType rhs,
68684846675e0e42d1802b0ffd8972a45e72aeb3758dPeter Collingbourne                                                bool OfBlockPointer,
68694846675e0e42d1802b0ffd8972a45e72aeb3758dPeter Collingbourne                                                bool Unqualified) {
68704846675e0e42d1802b0ffd8972a45e72aeb3758dPeter Collingbourne  // GNU extension: two types are compatible if they appear as a function
68714846675e0e42d1802b0ffd8972a45e72aeb3758dPeter Collingbourne  // argument, one of the types is a transparent union type and the other
68724846675e0e42d1802b0ffd8972a45e72aeb3758dPeter Collingbourne  // type is compatible with a union member
68734846675e0e42d1802b0ffd8972a45e72aeb3758dPeter Collingbourne  QualType lmerge = mergeTransparentUnionType(lhs, rhs, OfBlockPointer,
68744846675e0e42d1802b0ffd8972a45e72aeb3758dPeter Collingbourne                                              Unqualified);
68754846675e0e42d1802b0ffd8972a45e72aeb3758dPeter Collingbourne  if (!lmerge.isNull())
68764846675e0e42d1802b0ffd8972a45e72aeb3758dPeter Collingbourne    return lmerge;
68774846675e0e42d1802b0ffd8972a45e72aeb3758dPeter Collingbourne
68784846675e0e42d1802b0ffd8972a45e72aeb3758dPeter Collingbourne  QualType rmerge = mergeTransparentUnionType(rhs, lhs, OfBlockPointer,
68794846675e0e42d1802b0ffd8972a45e72aeb3758dPeter Collingbourne                                              Unqualified);
68804846675e0e42d1802b0ffd8972a45e72aeb3758dPeter Collingbourne  if (!rmerge.isNull())
68814846675e0e42d1802b0ffd8972a45e72aeb3758dPeter Collingbourne    return rmerge;
68824846675e0e42d1802b0ffd8972a45e72aeb3758dPeter Collingbourne
68834846675e0e42d1802b0ffd8972a45e72aeb3758dPeter Collingbourne  return mergeTypes(lhs, rhs, OfBlockPointer, Unqualified);
68844846675e0e42d1802b0ffd8972a45e72aeb3758dPeter Collingbourne}
68854846675e0e42d1802b0ffd8972a45e72aeb3758dPeter Collingbourne
6886132f2a2da34f378fc675b9e174564b0f52c31d98Fariborz JahanianQualType ASTContext::mergeFunctionTypes(QualType lhs, QualType rhs,
6887447234dd459a00a5ed9b7c3e066162cd7a75bf2dDouglas Gregor                                        bool OfBlockPointer,
6888447234dd459a00a5ed9b7c3e066162cd7a75bf2dDouglas Gregor                                        bool Unqualified) {
6889183700f494ec9b6701b6efe82bcb25f4c79ba561John McCall  const FunctionType *lbase = lhs->getAs<FunctionType>();
6890183700f494ec9b6701b6efe82bcb25f4c79ba561John McCall  const FunctionType *rbase = rhs->getAs<FunctionType>();
689172564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor  const FunctionProtoType *lproto = dyn_cast<FunctionProtoType>(lbase);
689272564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor  const FunctionProtoType *rproto = dyn_cast<FunctionProtoType>(rbase);
68933d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman  bool allLTypes = true;
68943d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman  bool allRTypes = true;
68953d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman
68963d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman  // Check return type
6897132f2a2da34f378fc675b9e174564b0f52c31d98Fariborz Jahanian  QualType retType;
6898d263fd1451299b1e5f5f1acb2bb13b0a4119aee8Fariborz Jahanian  if (OfBlockPointer) {
6899d263fd1451299b1e5f5f1acb2bb13b0a4119aee8Fariborz Jahanian    QualType RHS = rbase->getResultType();
6900d263fd1451299b1e5f5f1acb2bb13b0a4119aee8Fariborz Jahanian    QualType LHS = lbase->getResultType();
6901d263fd1451299b1e5f5f1acb2bb13b0a4119aee8Fariborz Jahanian    bool UnqualifiedResult = Unqualified;
6902d263fd1451299b1e5f5f1acb2bb13b0a4119aee8Fariborz Jahanian    if (!UnqualifiedResult)
6903d263fd1451299b1e5f5f1acb2bb13b0a4119aee8Fariborz Jahanian      UnqualifiedResult = (!RHS.hasQualifiers() && LHS.hasQualifiers());
6904a4fdbfad150ae37bddaa4094d3925a27a1a1cf3fFariborz Jahanian    retType = mergeTypes(LHS, RHS, true, UnqualifiedResult, true);
6905d263fd1451299b1e5f5f1acb2bb13b0a4119aee8Fariborz Jahanian  }
6906132f2a2da34f378fc675b9e174564b0f52c31d98Fariborz Jahanian  else
69078cc246c9a68c783a5b90d2e8b8927521cb3a49b7John McCall    retType = mergeTypes(lbase->getResultType(), rbase->getResultType(), false,
69088cc246c9a68c783a5b90d2e8b8927521cb3a49b7John McCall                         Unqualified);
69093d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman  if (retType.isNull()) return QualType();
6910447234dd459a00a5ed9b7c3e066162cd7a75bf2dDouglas Gregor
6911447234dd459a00a5ed9b7c3e066162cd7a75bf2dDouglas Gregor  if (Unqualified)
6912447234dd459a00a5ed9b7c3e066162cd7a75bf2dDouglas Gregor    retType = retType.getUnqualifiedType();
6913447234dd459a00a5ed9b7c3e066162cd7a75bf2dDouglas Gregor
6914447234dd459a00a5ed9b7c3e066162cd7a75bf2dDouglas Gregor  CanQualType LRetType = getCanonicalType(lbase->getResultType());
6915447234dd459a00a5ed9b7c3e066162cd7a75bf2dDouglas Gregor  CanQualType RRetType = getCanonicalType(rbase->getResultType());
6916447234dd459a00a5ed9b7c3e066162cd7a75bf2dDouglas Gregor  if (Unqualified) {
6917447234dd459a00a5ed9b7c3e066162cd7a75bf2dDouglas Gregor    LRetType = LRetType.getUnqualifiedType();
6918447234dd459a00a5ed9b7c3e066162cd7a75bf2dDouglas Gregor    RRetType = RRetType.getUnqualifiedType();
6919447234dd459a00a5ed9b7c3e066162cd7a75bf2dDouglas Gregor  }
6920447234dd459a00a5ed9b7c3e066162cd7a75bf2dDouglas Gregor
6921447234dd459a00a5ed9b7c3e066162cd7a75bf2dDouglas Gregor  if (getCanonicalType(retType) != LRetType)
692261710854be2b098428aff5316e64bd34b30fbcb7Chris Lattner    allLTypes = false;
6923447234dd459a00a5ed9b7c3e066162cd7a75bf2dDouglas Gregor  if (getCanonicalType(retType) != RRetType)
692461710854be2b098428aff5316e64bd34b30fbcb7Chris Lattner    allRTypes = false;
69258cc246c9a68c783a5b90d2e8b8927521cb3a49b7John McCall
69266a15c8586cc3e5d6e01c4c4e8b4374584569b58fDaniel Dunbar  // FIXME: double check this
69276a15c8586cc3e5d6e01c4c4e8b4374584569b58fDaniel Dunbar  // FIXME: should we error if lbase->getRegParmAttr() != 0 &&
69286a15c8586cc3e5d6e01c4c4e8b4374584569b58fDaniel Dunbar  //                           rbase->getRegParmAttr() != 0 &&
69296a15c8586cc3e5d6e01c4c4e8b4374584569b58fDaniel Dunbar  //                           lbase->getRegParmAttr() != rbase->getRegParmAttr()?
6930264ba48dc98f3f843935a485d5b086f7e0fdc4f1Rafael Espindola  FunctionType::ExtInfo lbaseInfo = lbase->getExtInfo();
6931264ba48dc98f3f843935a485d5b086f7e0fdc4f1Rafael Espindola  FunctionType::ExtInfo rbaseInfo = rbase->getExtInfo();
69328cc246c9a68c783a5b90d2e8b8927521cb3a49b7John McCall
69338cc246c9a68c783a5b90d2e8b8927521cb3a49b7John McCall  // Compatible functions must have compatible calling conventions
69348cc246c9a68c783a5b90d2e8b8927521cb3a49b7John McCall  if (!isSameCallConv(lbaseInfo.getCC(), rbaseInfo.getCC()))
69358cc246c9a68c783a5b90d2e8b8927521cb3a49b7John McCall    return QualType();
69368cc246c9a68c783a5b90d2e8b8927521cb3a49b7John McCall
69378cc246c9a68c783a5b90d2e8b8927521cb3a49b7John McCall  // Regparm is part of the calling convention.
6938a49218e17bcbb1acde0245773173e2c0c42f4f19Eli Friedman  if (lbaseInfo.getHasRegParm() != rbaseInfo.getHasRegParm())
6939a49218e17bcbb1acde0245773173e2c0c42f4f19Eli Friedman    return QualType();
69408cc246c9a68c783a5b90d2e8b8927521cb3a49b7John McCall  if (lbaseInfo.getRegParm() != rbaseInfo.getRegParm())
69418cc246c9a68c783a5b90d2e8b8927521cb3a49b7John McCall    return QualType();
69428cc246c9a68c783a5b90d2e8b8927521cb3a49b7John McCall
6943f85e193739c953358c865005855253af4f68a497John McCall  if (lbaseInfo.getProducesResult() != rbaseInfo.getProducesResult())
6944f85e193739c953358c865005855253af4f68a497John McCall    return QualType();
6945f85e193739c953358c865005855253af4f68a497John McCall
694653c8167d7a007daae87f342c0fedd03f1dcf1b62Fariborz Jahanian  // FIXME: some uses, e.g. conditional exprs, really want this to be 'both'.
694753c8167d7a007daae87f342c0fedd03f1dcf1b62Fariborz Jahanian  bool NoReturn = lbaseInfo.getNoReturn() || rbaseInfo.getNoReturn();
69481eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
69498b8a09e496fbed361d4c8e9e8cc259454a094258Rafael Espindola  if (lbaseInfo.getNoReturn() != NoReturn)
69508b8a09e496fbed361d4c8e9e8cc259454a094258Rafael Espindola    allLTypes = false;
69518b8a09e496fbed361d4c8e9e8cc259454a094258Rafael Espindola  if (rbaseInfo.getNoReturn() != NoReturn)
69528b8a09e496fbed361d4c8e9e8cc259454a094258Rafael Espindola    allRTypes = false;
69538b8a09e496fbed361d4c8e9e8cc259454a094258Rafael Espindola
6954f85e193739c953358c865005855253af4f68a497John McCall  FunctionType::ExtInfo einfo = lbaseInfo.withNoReturn(NoReturn);
6955e23cf437fe76b1ed02d63c3f61b456fd48a915f5John McCall
69563d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman  if (lproto && rproto) { // two C99 style function prototypes
6957465226e23a3008bd68973513dda1f9e3cd27dbddSebastian Redl    assert(!lproto->hasExceptionSpec() && !rproto->hasExceptionSpec() &&
6958465226e23a3008bd68973513dda1f9e3cd27dbddSebastian Redl           "C++ shouldn't be here");
69593d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    unsigned lproto_nargs = lproto->getNumArgs();
69603d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    unsigned rproto_nargs = rproto->getNumArgs();
69613d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman
69623d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    // Compatible functions must have the same number of arguments
69633d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    if (lproto_nargs != rproto_nargs)
69643d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman      return QualType();
69653d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman
69663d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    // Variadic and non-variadic functions aren't compatible
69673d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    if (lproto->isVariadic() != rproto->isVariadic())
69683d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman      return QualType();
69693d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman
69707fb5e4888221cd36652d078c6b171ac55e7f406dArgyrios Kyrtzidis    if (lproto->getTypeQuals() != rproto->getTypeQuals())
69717fb5e4888221cd36652d078c6b171ac55e7f406dArgyrios Kyrtzidis      return QualType();
69727fb5e4888221cd36652d078c6b171ac55e7f406dArgyrios Kyrtzidis
697378213e4aec6d8d22b5828dd2687fa19116ebaa26Fariborz Jahanian    if (LangOpts.ObjCAutoRefCount &&
697478213e4aec6d8d22b5828dd2687fa19116ebaa26Fariborz Jahanian        !FunctionTypesMatchOnNSConsumedAttrs(rproto, lproto))
697578213e4aec6d8d22b5828dd2687fa19116ebaa26Fariborz Jahanian      return QualType();
697678213e4aec6d8d22b5828dd2687fa19116ebaa26Fariborz Jahanian
69773d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    // Check argument compatibility
69785f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner    SmallVector<QualType, 10> types;
69793d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    for (unsigned i = 0; i < lproto_nargs; i++) {
69803d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman      QualType largtype = lproto->getArgType(i).getUnqualifiedType();
69813d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman      QualType rargtype = rproto->getArgType(i).getUnqualifiedType();
69824846675e0e42d1802b0ffd8972a45e72aeb3758dPeter Collingbourne      QualType argtype = mergeFunctionArgumentTypes(largtype, rargtype,
69834846675e0e42d1802b0ffd8972a45e72aeb3758dPeter Collingbourne                                                    OfBlockPointer,
69844846675e0e42d1802b0ffd8972a45e72aeb3758dPeter Collingbourne                                                    Unqualified);
69853d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman      if (argtype.isNull()) return QualType();
6986447234dd459a00a5ed9b7c3e066162cd7a75bf2dDouglas Gregor
6987447234dd459a00a5ed9b7c3e066162cd7a75bf2dDouglas Gregor      if (Unqualified)
6988447234dd459a00a5ed9b7c3e066162cd7a75bf2dDouglas Gregor        argtype = argtype.getUnqualifiedType();
6989447234dd459a00a5ed9b7c3e066162cd7a75bf2dDouglas Gregor
69903d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman      types.push_back(argtype);
6991447234dd459a00a5ed9b7c3e066162cd7a75bf2dDouglas Gregor      if (Unqualified) {
6992447234dd459a00a5ed9b7c3e066162cd7a75bf2dDouglas Gregor        largtype = largtype.getUnqualifiedType();
6993447234dd459a00a5ed9b7c3e066162cd7a75bf2dDouglas Gregor        rargtype = rargtype.getUnqualifiedType();
6994447234dd459a00a5ed9b7c3e066162cd7a75bf2dDouglas Gregor      }
6995447234dd459a00a5ed9b7c3e066162cd7a75bf2dDouglas Gregor
699661710854be2b098428aff5316e64bd34b30fbcb7Chris Lattner      if (getCanonicalType(argtype) != getCanonicalType(largtype))
699761710854be2b098428aff5316e64bd34b30fbcb7Chris Lattner        allLTypes = false;
699861710854be2b098428aff5316e64bd34b30fbcb7Chris Lattner      if (getCanonicalType(argtype) != getCanonicalType(rargtype))
699961710854be2b098428aff5316e64bd34b30fbcb7Chris Lattner        allRTypes = false;
70003d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    }
700178213e4aec6d8d22b5828dd2687fa19116ebaa26Fariborz Jahanian
70023d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    if (allLTypes) return lhs;
70033d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    if (allRTypes) return rhs;
7004e23cf437fe76b1ed02d63c3f61b456fd48a915f5John McCall
7005e23cf437fe76b1ed02d63c3f61b456fd48a915f5John McCall    FunctionProtoType::ExtProtoInfo EPI = lproto->getExtProtoInfo();
7006e23cf437fe76b1ed02d63c3f61b456fd48a915f5John McCall    EPI.ExtInfo = einfo;
7007bea522ff43a3f11c7a2bc7949119dbb9fce19e39Jordan Rose    return getFunctionType(retType, types, EPI);
70083d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman  }
70093d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman
70103d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman  if (lproto) allRTypes = false;
70113d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman  if (rproto) allLTypes = false;
70123d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman
701372564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor  const FunctionProtoType *proto = lproto ? lproto : rproto;
70143d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman  if (proto) {
7015465226e23a3008bd68973513dda1f9e3cd27dbddSebastian Redl    assert(!proto->hasExceptionSpec() && "C++ shouldn't be here");
70163d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    if (proto->isVariadic()) return QualType();
70173d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    // Check that the types are compatible with the types that
70183d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    // would result from default argument promotions (C99 6.7.5.3p15).
70193d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    // The only types actually affected are promotable integer
70203d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    // types and floats, which would be passed as a different
70213d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    // type depending on whether the prototype is visible.
70223d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    unsigned proto_nargs = proto->getNumArgs();
70233d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    for (unsigned i = 0; i < proto_nargs; ++i) {
70243d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman      QualType argTy = proto->getArgType(i);
7025b0f8eacfdcd0d43f51e669f2d723992d4af9f746Douglas Gregor
7026c586d5dd7a997321ea96d1d28213315f86778fc8Eli Friedman      // Look at the converted type of enum types, since that is the type used
7027b0f8eacfdcd0d43f51e669f2d723992d4af9f746Douglas Gregor      // to pass enum values.
7028c586d5dd7a997321ea96d1d28213315f86778fc8Eli Friedman      if (const EnumType *Enum = argTy->getAs<EnumType>()) {
7029c586d5dd7a997321ea96d1d28213315f86778fc8Eli Friedman        argTy = Enum->getDecl()->getIntegerType();
7030c586d5dd7a997321ea96d1d28213315f86778fc8Eli Friedman        if (argTy.isNull())
7031c586d5dd7a997321ea96d1d28213315f86778fc8Eli Friedman          return QualType();
7032c586d5dd7a997321ea96d1d28213315f86778fc8Eli Friedman      }
7033b0f8eacfdcd0d43f51e669f2d723992d4af9f746Douglas Gregor
70343d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman      if (argTy->isPromotableIntegerType() ||
70353d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman          getCanonicalType(argTy).getUnqualifiedType() == FloatTy)
70363d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman        return QualType();
70373d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    }
70383d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman
70393d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    if (allLTypes) return lhs;
70403d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    if (allRTypes) return rhs;
7041e23cf437fe76b1ed02d63c3f61b456fd48a915f5John McCall
7042e23cf437fe76b1ed02d63c3f61b456fd48a915f5John McCall    FunctionProtoType::ExtProtoInfo EPI = proto->getExtProtoInfo();
7043e23cf437fe76b1ed02d63c3f61b456fd48a915f5John McCall    EPI.ExtInfo = einfo;
7044bea522ff43a3f11c7a2bc7949119dbb9fce19e39Jordan Rose    return getFunctionType(retType,
7045bea522ff43a3f11c7a2bc7949119dbb9fce19e39Jordan Rose                           ArrayRef<QualType>(proto->arg_type_begin(),
7046bea522ff43a3f11c7a2bc7949119dbb9fce19e39Jordan Rose                                              proto->getNumArgs()),
7047bea522ff43a3f11c7a2bc7949119dbb9fce19e39Jordan Rose                           EPI);
70483d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman  }
70493d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman
70503d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman  if (allLTypes) return lhs;
70513d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman  if (allRTypes) return rhs;
70528cc246c9a68c783a5b90d2e8b8927521cb3a49b7John McCall  return getFunctionNoProtoType(retType, einfo);
70533d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman}
70543d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman
7055b9da713efb4277753211590953f433723908aadeJohn McCall/// Given that we have an enum type and a non-enum type, try to merge them.
7056b9da713efb4277753211590953f433723908aadeJohn McCallstatic QualType mergeEnumWithInteger(ASTContext &Context, const EnumType *ET,
7057b9da713efb4277753211590953f433723908aadeJohn McCall                                     QualType other, bool isBlockReturnType) {
7058b9da713efb4277753211590953f433723908aadeJohn McCall  // C99 6.7.2.2p4: Each enumerated type shall be compatible with char,
7059b9da713efb4277753211590953f433723908aadeJohn McCall  // a signed integer type, or an unsigned integer type.
7060b9da713efb4277753211590953f433723908aadeJohn McCall  // Compatibility is based on the underlying type, not the promotion
7061b9da713efb4277753211590953f433723908aadeJohn McCall  // type.
7062b9da713efb4277753211590953f433723908aadeJohn McCall  QualType underlyingType = ET->getDecl()->getIntegerType();
7063b9da713efb4277753211590953f433723908aadeJohn McCall  if (underlyingType.isNull()) return QualType();
7064b9da713efb4277753211590953f433723908aadeJohn McCall  if (Context.hasSameType(underlyingType, other))
7065b9da713efb4277753211590953f433723908aadeJohn McCall    return other;
7066b9da713efb4277753211590953f433723908aadeJohn McCall
7067b9da713efb4277753211590953f433723908aadeJohn McCall  // In block return types, we're more permissive and accept any
7068b9da713efb4277753211590953f433723908aadeJohn McCall  // integral type of the same size.
7069b9da713efb4277753211590953f433723908aadeJohn McCall  if (isBlockReturnType && other->isIntegerType() &&
7070b9da713efb4277753211590953f433723908aadeJohn McCall      Context.getTypeSize(underlyingType) == Context.getTypeSize(other))
7071b9da713efb4277753211590953f433723908aadeJohn McCall    return other;
7072b9da713efb4277753211590953f433723908aadeJohn McCall
7073b9da713efb4277753211590953f433723908aadeJohn McCall  return QualType();
7074b9da713efb4277753211590953f433723908aadeJohn McCall}
7075b9da713efb4277753211590953f433723908aadeJohn McCall
7076132f2a2da34f378fc675b9e174564b0f52c31d98Fariborz JahanianQualType ASTContext::mergeTypes(QualType LHS, QualType RHS,
7077447234dd459a00a5ed9b7c3e066162cd7a75bf2dDouglas Gregor                                bool OfBlockPointer,
7078a4fdbfad150ae37bddaa4094d3925a27a1a1cf3fFariborz Jahanian                                bool Unqualified, bool BlockReturnType) {
707943d69750e7f7b26076e7474dec8839bb777b260fBill Wendling  // C++ [expr]: If an expression initially has the type "reference to T", the
708043d69750e7f7b26076e7474dec8839bb777b260fBill Wendling  // type is adjusted to "T" prior to any further analysis, the expression
708143d69750e7f7b26076e7474dec8839bb777b260fBill Wendling  // designates the object or function denoted by the reference, and the
70827c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl  // expression is an lvalue unless the reference is an rvalue reference and
70837c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl  // the expression is a function call (possibly inside parentheses).
70840e709abafbd939326850501f795cc7a92c88a354Douglas Gregor  assert(!LHS->getAs<ReferenceType>() && "LHS is a reference type?");
70850e709abafbd939326850501f795cc7a92c88a354Douglas Gregor  assert(!RHS->getAs<ReferenceType>() && "RHS is a reference type?");
7086447234dd459a00a5ed9b7c3e066162cd7a75bf2dDouglas Gregor
7087447234dd459a00a5ed9b7c3e066162cd7a75bf2dDouglas Gregor  if (Unqualified) {
7088447234dd459a00a5ed9b7c3e066162cd7a75bf2dDouglas Gregor    LHS = LHS.getUnqualifiedType();
7089447234dd459a00a5ed9b7c3e066162cd7a75bf2dDouglas Gregor    RHS = RHS.getUnqualifiedType();
7090447234dd459a00a5ed9b7c3e066162cd7a75bf2dDouglas Gregor  }
70910e709abafbd939326850501f795cc7a92c88a354Douglas Gregor
70923d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman  QualType LHSCan = getCanonicalType(LHS),
70933d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman           RHSCan = getCanonicalType(RHS);
70943d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman
7095f3692dc4a47dc48d10cec0415c6e9e39b7a39707Chris Lattner  // If two types are identical, they are compatible.
70963d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman  if (LHSCan == RHSCan)
70973d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    return LHS;
70983d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman
70990953e767ff7817f97b3ab20896b229891eeff45bJohn McCall  // If the qualifiers are different, the types aren't compatible... mostly.
7100a4923eb7c4b04d360cb2747641a5e92818edf804Douglas Gregor  Qualifiers LQuals = LHSCan.getLocalQualifiers();
7101a4923eb7c4b04d360cb2747641a5e92818edf804Douglas Gregor  Qualifiers RQuals = RHSCan.getLocalQualifiers();
71020953e767ff7817f97b3ab20896b229891eeff45bJohn McCall  if (LQuals != RQuals) {
71030953e767ff7817f97b3ab20896b229891eeff45bJohn McCall    // If any of these qualifiers are different, we have a type
71040953e767ff7817f97b3ab20896b229891eeff45bJohn McCall    // mismatch.
71050953e767ff7817f97b3ab20896b229891eeff45bJohn McCall    if (LQuals.getCVRQualifiers() != RQuals.getCVRQualifiers() ||
7106f85e193739c953358c865005855253af4f68a497John McCall        LQuals.getAddressSpace() != RQuals.getAddressSpace() ||
7107f85e193739c953358c865005855253af4f68a497John McCall        LQuals.getObjCLifetime() != RQuals.getObjCLifetime())
71080953e767ff7817f97b3ab20896b229891eeff45bJohn McCall      return QualType();
71090953e767ff7817f97b3ab20896b229891eeff45bJohn McCall
71100953e767ff7817f97b3ab20896b229891eeff45bJohn McCall    // Exactly one GC qualifier difference is allowed: __strong is
71110953e767ff7817f97b3ab20896b229891eeff45bJohn McCall    // okay if the other type has no GC qualifier but is an Objective
71120953e767ff7817f97b3ab20896b229891eeff45bJohn McCall    // C object pointer (i.e. implicitly strong by default).  We fix
71130953e767ff7817f97b3ab20896b229891eeff45bJohn McCall    // this by pretending that the unqualified type was actually
71140953e767ff7817f97b3ab20896b229891eeff45bJohn McCall    // qualified __strong.
71150953e767ff7817f97b3ab20896b229891eeff45bJohn McCall    Qualifiers::GC GC_L = LQuals.getObjCGCAttr();
71160953e767ff7817f97b3ab20896b229891eeff45bJohn McCall    Qualifiers::GC GC_R = RQuals.getObjCGCAttr();
71170953e767ff7817f97b3ab20896b229891eeff45bJohn McCall    assert((GC_L != GC_R) && "unequal qualifier sets had only equal elements");
71180953e767ff7817f97b3ab20896b229891eeff45bJohn McCall
71190953e767ff7817f97b3ab20896b229891eeff45bJohn McCall    if (GC_L == Qualifiers::Weak || GC_R == Qualifiers::Weak)
71200953e767ff7817f97b3ab20896b229891eeff45bJohn McCall      return QualType();
71210953e767ff7817f97b3ab20896b229891eeff45bJohn McCall
71220953e767ff7817f97b3ab20896b229891eeff45bJohn McCall    if (GC_L == Qualifiers::Strong && RHSCan->isObjCObjectPointerType()) {
71230953e767ff7817f97b3ab20896b229891eeff45bJohn McCall      return mergeTypes(LHS, getObjCGCQualType(RHS, Qualifiers::Strong));
71240953e767ff7817f97b3ab20896b229891eeff45bJohn McCall    }
71250953e767ff7817f97b3ab20896b229891eeff45bJohn McCall    if (GC_R == Qualifiers::Strong && LHSCan->isObjCObjectPointerType()) {
71260953e767ff7817f97b3ab20896b229891eeff45bJohn McCall      return mergeTypes(getObjCGCQualType(LHS, Qualifiers::Strong), RHS);
71270953e767ff7817f97b3ab20896b229891eeff45bJohn McCall    }
71283d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    return QualType();
71290953e767ff7817f97b3ab20896b229891eeff45bJohn McCall  }
71300953e767ff7817f97b3ab20896b229891eeff45bJohn McCall
71310953e767ff7817f97b3ab20896b229891eeff45bJohn McCall  // Okay, qualifiers are equal.
71323d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman
7133852d63b806c5cbd730c6b9d696e2e27d02546b49Eli Friedman  Type::TypeClass LHSClass = LHSCan->getTypeClass();
7134852d63b806c5cbd730c6b9d696e2e27d02546b49Eli Friedman  Type::TypeClass RHSClass = RHSCan->getTypeClass();
7135f3692dc4a47dc48d10cec0415c6e9e39b7a39707Chris Lattner
71361adb88370beab45af2f065afe86b51ccd59ec50dChris Lattner  // We want to consider the two function types to be the same for these
71371adb88370beab45af2f065afe86b51ccd59ec50dChris Lattner  // comparisons, just force one to the other.
71381adb88370beab45af2f065afe86b51ccd59ec50dChris Lattner  if (LHSClass == Type::FunctionProto) LHSClass = Type::FunctionNoProto;
71391adb88370beab45af2f065afe86b51ccd59ec50dChris Lattner  if (RHSClass == Type::FunctionProto) RHSClass = Type::FunctionNoProto;
71404c721d381fb279899337d120edd4a24d405e56b2Eli Friedman
71414c721d381fb279899337d120edd4a24d405e56b2Eli Friedman  // Same as above for arrays
7142a36a61f218b9f7a97f2c0f511e0b29eb42e8f78bChris Lattner  if (LHSClass == Type::VariableArray || LHSClass == Type::IncompleteArray)
7143a36a61f218b9f7a97f2c0f511e0b29eb42e8f78bChris Lattner    LHSClass = Type::ConstantArray;
7144a36a61f218b9f7a97f2c0f511e0b29eb42e8f78bChris Lattner  if (RHSClass == Type::VariableArray || RHSClass == Type::IncompleteArray)
7145a36a61f218b9f7a97f2c0f511e0b29eb42e8f78bChris Lattner    RHSClass = Type::ConstantArray;
71461eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
7147c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall  // ObjCInterfaces are just specialized ObjCObjects.
7148c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall  if (LHSClass == Type::ObjCInterface) LHSClass = Type::ObjCObject;
7149c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall  if (RHSClass == Type::ObjCInterface) RHSClass = Type::ObjCObject;
7150c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall
7151213541a68a3e137d11d2cefb612c6cdb410d7e8eNate Begeman  // Canonicalize ExtVector -> Vector.
7152213541a68a3e137d11d2cefb612c6cdb410d7e8eNate Begeman  if (LHSClass == Type::ExtVector) LHSClass = Type::Vector;
7153213541a68a3e137d11d2cefb612c6cdb410d7e8eNate Begeman  if (RHSClass == Type::ExtVector) RHSClass = Type::Vector;
71541eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
71554e78fd0a960eaa7e97467f2e8f390f3a57da279bSteve Naroff  // If the canonical type classes don't match.
71564e78fd0a960eaa7e97467f2e8f390f3a57da279bSteve Naroff  if (LHSClass != RHSClass) {
7157b9da713efb4277753211590953f433723908aadeJohn McCall    // Note that we only have special rules for turning block enum
7158b9da713efb4277753211590953f433723908aadeJohn McCall    // returns into block int returns, not vice-versa.
7159183700f494ec9b6701b6efe82bcb25f4c79ba561John McCall    if (const EnumType* ETy = LHS->getAs<EnumType>()) {
7160b9da713efb4277753211590953f433723908aadeJohn McCall      return mergeEnumWithInteger(*this, ETy, RHS, false);
7161bab96968886f4b77083f4e26a28986ddb1e42d67Eli Friedman    }
7162183700f494ec9b6701b6efe82bcb25f4c79ba561John McCall    if (const EnumType* ETy = RHS->getAs<EnumType>()) {
7163b9da713efb4277753211590953f433723908aadeJohn McCall      return mergeEnumWithInteger(*this, ETy, LHS, BlockReturnType);
7164bab96968886f4b77083f4e26a28986ddb1e42d67Eli Friedman    }
7165e7cff2c00c3d74e38984a4db5c48d7e303d3ab8eFariborz Jahanian    // allow block pointer type to match an 'id' type.
7166419636316c04d570c183b4797fc811932c588358Fariborz Jahanian    if (OfBlockPointer && !BlockReturnType) {
7167419636316c04d570c183b4797fc811932c588358Fariborz Jahanian       if (LHS->isObjCIdType() && RHS->isBlockPointerType())
7168419636316c04d570c183b4797fc811932c588358Fariborz Jahanian         return LHS;
7169419636316c04d570c183b4797fc811932c588358Fariborz Jahanian      if (RHS->isObjCIdType() && LHS->isBlockPointerType())
7170419636316c04d570c183b4797fc811932c588358Fariborz Jahanian        return RHS;
7171419636316c04d570c183b4797fc811932c588358Fariborz Jahanian    }
7172e7cff2c00c3d74e38984a4db5c48d7e303d3ab8eFariborz Jahanian
71733d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    return QualType();
7174ec0550fa3653d46560bf4484a2e988329c228e39Steve Naroff  }
71753d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman
71764a74678ed6c3dedac05d02b1ee341f1db869f049Steve Naroff  // The canonical type classes match.
71771adb88370beab45af2f065afe86b51ccd59ec50dChris Lattner  switch (LHSClass) {
717872564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor#define TYPE(Class, Base)
717972564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor#define ABSTRACT_TYPE(Class, Base)
7180ad5e73887052193afda72db8efcb812bd083a4a8John McCall#define NON_CANONICAL_UNLESS_DEPENDENT_TYPE(Class, Base) case Type::Class:
718172564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor#define NON_CANONICAL_TYPE(Class, Base) case Type::Class:
718272564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor#define DEPENDENT_TYPE(Class, Base) case Type::Class:
718372564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor#include "clang/AST/TypeNodes.def"
7184b219cfc4d75f0a03630b7c4509ef791b7e97b2c8David Blaikie    llvm_unreachable("Non-canonical and dependent types shouldn't get here");
718572564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor
7186dc7a4f5d7a7e3b60d4dc4a80338d7a2728540998Richard Smith  case Type::Auto:
71877c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl  case Type::LValueReference:
71887c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl  case Type::RValueReference:
718972564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor  case Type::MemberPointer:
7190b219cfc4d75f0a03630b7c4509ef791b7e97b2c8David Blaikie    llvm_unreachable("C++ should never be in mergeTypes");
719172564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor
7192c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall  case Type::ObjCInterface:
719372564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor  case Type::IncompleteArray:
719472564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor  case Type::VariableArray:
719572564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor  case Type::FunctionProto:
719672564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor  case Type::ExtVector:
7197b219cfc4d75f0a03630b7c4509ef791b7e97b2c8David Blaikie    llvm_unreachable("Types are eliminated above");
719872564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor
71991adb88370beab45af2f065afe86b51ccd59ec50dChris Lattner  case Type::Pointer:
72003d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman  {
72013d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    // Merge two pointer types, while trying to preserve typedef info
72026217b80b7a1379b74cced1c076338262c3c980b3Ted Kremenek    QualType LHSPointee = LHS->getAs<PointerType>()->getPointeeType();
72036217b80b7a1379b74cced1c076338262c3c980b3Ted Kremenek    QualType RHSPointee = RHS->getAs<PointerType>()->getPointeeType();
7204447234dd459a00a5ed9b7c3e066162cd7a75bf2dDouglas Gregor    if (Unqualified) {
7205447234dd459a00a5ed9b7c3e066162cd7a75bf2dDouglas Gregor      LHSPointee = LHSPointee.getUnqualifiedType();
7206447234dd459a00a5ed9b7c3e066162cd7a75bf2dDouglas Gregor      RHSPointee = RHSPointee.getUnqualifiedType();
7207447234dd459a00a5ed9b7c3e066162cd7a75bf2dDouglas Gregor    }
7208447234dd459a00a5ed9b7c3e066162cd7a75bf2dDouglas Gregor    QualType ResultType = mergeTypes(LHSPointee, RHSPointee, false,
7209447234dd459a00a5ed9b7c3e066162cd7a75bf2dDouglas Gregor                                     Unqualified);
72103d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    if (ResultType.isNull()) return QualType();
721107d258756dc856c6987c394a0972884e6ed46765Eli Friedman    if (getCanonicalType(LHSPointee) == getCanonicalType(ResultType))
721261710854be2b098428aff5316e64bd34b30fbcb7Chris Lattner      return LHS;
721307d258756dc856c6987c394a0972884e6ed46765Eli Friedman    if (getCanonicalType(RHSPointee) == getCanonicalType(ResultType))
721461710854be2b098428aff5316e64bd34b30fbcb7Chris Lattner      return RHS;
72153d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    return getPointerType(ResultType);
72163d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman  }
7217c0febd58f5cbf4a93fd12f461863564dba0af76dSteve Naroff  case Type::BlockPointer:
7218c0febd58f5cbf4a93fd12f461863564dba0af76dSteve Naroff  {
7219c0febd58f5cbf4a93fd12f461863564dba0af76dSteve Naroff    // Merge two block pointer types, while trying to preserve typedef info
72206217b80b7a1379b74cced1c076338262c3c980b3Ted Kremenek    QualType LHSPointee = LHS->getAs<BlockPointerType>()->getPointeeType();
72216217b80b7a1379b74cced1c076338262c3c980b3Ted Kremenek    QualType RHSPointee = RHS->getAs<BlockPointerType>()->getPointeeType();
7222447234dd459a00a5ed9b7c3e066162cd7a75bf2dDouglas Gregor    if (Unqualified) {
7223447234dd459a00a5ed9b7c3e066162cd7a75bf2dDouglas Gregor      LHSPointee = LHSPointee.getUnqualifiedType();
7224447234dd459a00a5ed9b7c3e066162cd7a75bf2dDouglas Gregor      RHSPointee = RHSPointee.getUnqualifiedType();
7225447234dd459a00a5ed9b7c3e066162cd7a75bf2dDouglas Gregor    }
7226447234dd459a00a5ed9b7c3e066162cd7a75bf2dDouglas Gregor    QualType ResultType = mergeTypes(LHSPointee, RHSPointee, OfBlockPointer,
7227447234dd459a00a5ed9b7c3e066162cd7a75bf2dDouglas Gregor                                     Unqualified);
7228c0febd58f5cbf4a93fd12f461863564dba0af76dSteve Naroff    if (ResultType.isNull()) return QualType();
7229c0febd58f5cbf4a93fd12f461863564dba0af76dSteve Naroff    if (getCanonicalType(LHSPointee) == getCanonicalType(ResultType))
7230c0febd58f5cbf4a93fd12f461863564dba0af76dSteve Naroff      return LHS;
7231c0febd58f5cbf4a93fd12f461863564dba0af76dSteve Naroff    if (getCanonicalType(RHSPointee) == getCanonicalType(ResultType))
7232c0febd58f5cbf4a93fd12f461863564dba0af76dSteve Naroff      return RHS;
7233c0febd58f5cbf4a93fd12f461863564dba0af76dSteve Naroff    return getBlockPointerType(ResultType);
7234c0febd58f5cbf4a93fd12f461863564dba0af76dSteve Naroff  }
7235b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman  case Type::Atomic:
7236b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman  {
7237b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman    // Merge two pointer types, while trying to preserve typedef info
7238b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman    QualType LHSValue = LHS->getAs<AtomicType>()->getValueType();
7239b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman    QualType RHSValue = RHS->getAs<AtomicType>()->getValueType();
7240b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman    if (Unqualified) {
7241b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman      LHSValue = LHSValue.getUnqualifiedType();
7242b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman      RHSValue = RHSValue.getUnqualifiedType();
7243b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman    }
7244b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman    QualType ResultType = mergeTypes(LHSValue, RHSValue, false,
7245b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman                                     Unqualified);
7246b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman    if (ResultType.isNull()) return QualType();
7247b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman    if (getCanonicalType(LHSValue) == getCanonicalType(ResultType))
7248b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman      return LHS;
7249b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman    if (getCanonicalType(RHSValue) == getCanonicalType(ResultType))
7250b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman      return RHS;
7251b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman    return getAtomicType(ResultType);
7252b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman  }
72531adb88370beab45af2f065afe86b51ccd59ec50dChris Lattner  case Type::ConstantArray:
72543d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman  {
72553d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    const ConstantArrayType* LCAT = getAsConstantArrayType(LHS);
72563d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    const ConstantArrayType* RCAT = getAsConstantArrayType(RHS);
72573d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    if (LCAT && RCAT && RCAT->getSize() != LCAT->getSize())
72583d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman      return QualType();
72593d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman
72603d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    QualType LHSElem = getAsArrayType(LHS)->getElementType();
72613d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    QualType RHSElem = getAsArrayType(RHS)->getElementType();
7262447234dd459a00a5ed9b7c3e066162cd7a75bf2dDouglas Gregor    if (Unqualified) {
7263447234dd459a00a5ed9b7c3e066162cd7a75bf2dDouglas Gregor      LHSElem = LHSElem.getUnqualifiedType();
7264447234dd459a00a5ed9b7c3e066162cd7a75bf2dDouglas Gregor      RHSElem = RHSElem.getUnqualifiedType();
7265447234dd459a00a5ed9b7c3e066162cd7a75bf2dDouglas Gregor    }
7266447234dd459a00a5ed9b7c3e066162cd7a75bf2dDouglas Gregor
7267447234dd459a00a5ed9b7c3e066162cd7a75bf2dDouglas Gregor    QualType ResultType = mergeTypes(LHSElem, RHSElem, false, Unqualified);
72683d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    if (ResultType.isNull()) return QualType();
726961710854be2b098428aff5316e64bd34b30fbcb7Chris Lattner    if (LCAT && getCanonicalType(LHSElem) == getCanonicalType(ResultType))
727061710854be2b098428aff5316e64bd34b30fbcb7Chris Lattner      return LHS;
727161710854be2b098428aff5316e64bd34b30fbcb7Chris Lattner    if (RCAT && getCanonicalType(RHSElem) == getCanonicalType(ResultType))
727261710854be2b098428aff5316e64bd34b30fbcb7Chris Lattner      return RHS;
72733bc0f45a5e65814f42b22dcdf7249d1120d16f36Eli Friedman    if (LCAT) return getConstantArrayType(ResultType, LCAT->getSize(),
72743bc0f45a5e65814f42b22dcdf7249d1120d16f36Eli Friedman                                          ArrayType::ArraySizeModifier(), 0);
72753bc0f45a5e65814f42b22dcdf7249d1120d16f36Eli Friedman    if (RCAT) return getConstantArrayType(ResultType, RCAT->getSize(),
72763bc0f45a5e65814f42b22dcdf7249d1120d16f36Eli Friedman                                          ArrayType::ArraySizeModifier(), 0);
72773d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    const VariableArrayType* LVAT = getAsVariableArrayType(LHS);
72783d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    const VariableArrayType* RVAT = getAsVariableArrayType(RHS);
727961710854be2b098428aff5316e64bd34b30fbcb7Chris Lattner    if (LVAT && getCanonicalType(LHSElem) == getCanonicalType(ResultType))
728061710854be2b098428aff5316e64bd34b30fbcb7Chris Lattner      return LHS;
728161710854be2b098428aff5316e64bd34b30fbcb7Chris Lattner    if (RVAT && getCanonicalType(RHSElem) == getCanonicalType(ResultType))
728261710854be2b098428aff5316e64bd34b30fbcb7Chris Lattner      return RHS;
72833d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    if (LVAT) {
72843d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman      // FIXME: This isn't correct! But tricky to implement because
72853d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman      // the array's size has to be the size of LHS, but the type
72863d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman      // has to be different.
72873d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman      return LHS;
72883d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    }
72893d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    if (RVAT) {
72903d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman      // FIXME: This isn't correct! But tricky to implement because
72913d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman      // the array's size has to be the size of RHS, but the type
72923d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman      // has to be different.
72933d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman      return RHS;
72943d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    }
72953bc0f45a5e65814f42b22dcdf7249d1120d16f36Eli Friedman    if (getCanonicalType(LHSElem) == getCanonicalType(ResultType)) return LHS;
72963bc0f45a5e65814f42b22dcdf7249d1120d16f36Eli Friedman    if (getCanonicalType(RHSElem) == getCanonicalType(ResultType)) return RHS;
72977e7eb3da052a6d80ddf2377cab0384c798f73f75Douglas Gregor    return getIncompleteArrayType(ResultType,
72987e7eb3da052a6d80ddf2377cab0384c798f73f75Douglas Gregor                                  ArrayType::ArraySizeModifier(), 0);
72993d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman  }
73001adb88370beab45af2f065afe86b51ccd59ec50dChris Lattner  case Type::FunctionNoProto:
7301447234dd459a00a5ed9b7c3e066162cd7a75bf2dDouglas Gregor    return mergeFunctionTypes(LHS, RHS, OfBlockPointer, Unqualified);
730272564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor  case Type::Record:
730372564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor  case Type::Enum:
73043d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    return QualType();
73051adb88370beab45af2f065afe86b51ccd59ec50dChris Lattner  case Type::Builtin:
73063cc4c0c3058a788689b8fc73c0ac139544435c97Chris Lattner    // Only exactly equal builtin types are compatible, which is tested above.
73073d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    return QualType();
730864cfdb7da3cb744642fe8a99ad5c851ad3c930b2Daniel Dunbar  case Type::Complex:
730964cfdb7da3cb744642fe8a99ad5c851ad3c930b2Daniel Dunbar    // Distinct complex types are incompatible.
731064cfdb7da3cb744642fe8a99ad5c851ad3c930b2Daniel Dunbar    return QualType();
73113cc4c0c3058a788689b8fc73c0ac139544435c97Chris Lattner  case Type::Vector:
73125a61f0e5c5aaecd5713c3fa4b78be7167a7eeff2Eli Friedman    // FIXME: The merged type should be an ExtVector!
73131c471f3e1c0ec8cbc82447bb35908dfc55463e46John McCall    if (areCompatVectorTypes(LHSCan->getAs<VectorType>(),
73141c471f3e1c0ec8cbc82447bb35908dfc55463e46John McCall                             RHSCan->getAs<VectorType>()))
73153d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman      return LHS;
731661710854be2b098428aff5316e64bd34b30fbcb7Chris Lattner    return QualType();
7317c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall  case Type::ObjCObject: {
7318c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall    // Check if the types are assignment compatible.
73195a61f0e5c5aaecd5713c3fa4b78be7167a7eeff2Eli Friedman    // FIXME: This should be type compatibility, e.g. whether
73205a61f0e5c5aaecd5713c3fa4b78be7167a7eeff2Eli Friedman    // "LHS x; RHS x;" at global scope is legal.
7321c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall    const ObjCObjectType* LHSIface = LHS->getAs<ObjCObjectType>();
7322c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall    const ObjCObjectType* RHSIface = RHS->getAs<ObjCObjectType>();
7323c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall    if (canAssignObjCInterfaces(LHSIface, RHSIface))
73245fd659db11922fc12a58e478f7b745f9656b15a7Steve Naroff      return LHS;
73255fd659db11922fc12a58e478f7b745f9656b15a7Steve Naroff
73263d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    return QualType();
732761490e9a965cfee8a78c12c6802138844f04250dCedric Venet  }
732814108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff  case Type::ObjCObjectPointer: {
7329132f2a2da34f378fc675b9e174564b0f52c31d98Fariborz Jahanian    if (OfBlockPointer) {
7330132f2a2da34f378fc675b9e174564b0f52c31d98Fariborz Jahanian      if (canAssignObjCInterfacesInBlockPointer(
7331132f2a2da34f378fc675b9e174564b0f52c31d98Fariborz Jahanian                                          LHS->getAs<ObjCObjectPointerType>(),
7332a4fdbfad150ae37bddaa4094d3925a27a1a1cf3fFariborz Jahanian                                          RHS->getAs<ObjCObjectPointerType>(),
7333a4fdbfad150ae37bddaa4094d3925a27a1a1cf3fFariborz Jahanian                                          BlockReturnType))
73347530c034c0c71a64c5a9173206d9742ae847af8bDavid Blaikie        return LHS;
7335132f2a2da34f378fc675b9e174564b0f52c31d98Fariborz Jahanian      return QualType();
7336132f2a2da34f378fc675b9e174564b0f52c31d98Fariborz Jahanian    }
7337183700f494ec9b6701b6efe82bcb25f4c79ba561John McCall    if (canAssignObjCInterfaces(LHS->getAs<ObjCObjectPointerType>(),
7338183700f494ec9b6701b6efe82bcb25f4c79ba561John McCall                                RHS->getAs<ObjCObjectPointerType>()))
733914108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff      return LHS;
734014108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff
7341bc76dd06eb881c70c9775b74bab8b88cd747f173Steve Naroff    return QualType();
73427530c034c0c71a64c5a9173206d9742ae847af8bDavid Blaikie  }
7343ec0550fa3653d46560bf4484a2e988329c228e39Steve Naroff  }
734472564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor
73457530c034c0c71a64c5a9173206d9742ae847af8bDavid Blaikie  llvm_unreachable("Invalid Type::Class!");
7346ec0550fa3653d46560bf4484a2e988329c228e39Steve Naroff}
73477192f8e9592729882a09d84d77838db26e39ebd4Ted Kremenek
734878213e4aec6d8d22b5828dd2687fa19116ebaa26Fariborz Jahanianbool ASTContext::FunctionTypesMatchOnNSConsumedAttrs(
734978213e4aec6d8d22b5828dd2687fa19116ebaa26Fariborz Jahanian                   const FunctionProtoType *FromFunctionType,
735078213e4aec6d8d22b5828dd2687fa19116ebaa26Fariborz Jahanian                   const FunctionProtoType *ToFunctionType) {
735178213e4aec6d8d22b5828dd2687fa19116ebaa26Fariborz Jahanian  if (FromFunctionType->hasAnyConsumedArgs() !=
735278213e4aec6d8d22b5828dd2687fa19116ebaa26Fariborz Jahanian      ToFunctionType->hasAnyConsumedArgs())
735378213e4aec6d8d22b5828dd2687fa19116ebaa26Fariborz Jahanian    return false;
735478213e4aec6d8d22b5828dd2687fa19116ebaa26Fariborz Jahanian  FunctionProtoType::ExtProtoInfo FromEPI =
735578213e4aec6d8d22b5828dd2687fa19116ebaa26Fariborz Jahanian    FromFunctionType->getExtProtoInfo();
735678213e4aec6d8d22b5828dd2687fa19116ebaa26Fariborz Jahanian  FunctionProtoType::ExtProtoInfo ToEPI =
735778213e4aec6d8d22b5828dd2687fa19116ebaa26Fariborz Jahanian    ToFunctionType->getExtProtoInfo();
735878213e4aec6d8d22b5828dd2687fa19116ebaa26Fariborz Jahanian  if (FromEPI.ConsumedArguments && ToEPI.ConsumedArguments)
735978213e4aec6d8d22b5828dd2687fa19116ebaa26Fariborz Jahanian    for (unsigned ArgIdx = 0, NumArgs = FromFunctionType->getNumArgs();
736078213e4aec6d8d22b5828dd2687fa19116ebaa26Fariborz Jahanian         ArgIdx != NumArgs; ++ArgIdx)  {
736178213e4aec6d8d22b5828dd2687fa19116ebaa26Fariborz Jahanian      if (FromEPI.ConsumedArguments[ArgIdx] !=
736278213e4aec6d8d22b5828dd2687fa19116ebaa26Fariborz Jahanian          ToEPI.ConsumedArguments[ArgIdx])
736378213e4aec6d8d22b5828dd2687fa19116ebaa26Fariborz Jahanian        return false;
736478213e4aec6d8d22b5828dd2687fa19116ebaa26Fariborz Jahanian    }
736578213e4aec6d8d22b5828dd2687fa19116ebaa26Fariborz Jahanian  return true;
736678213e4aec6d8d22b5828dd2687fa19116ebaa26Fariborz Jahanian}
736778213e4aec6d8d22b5828dd2687fa19116ebaa26Fariborz Jahanian
73682390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian/// mergeObjCGCQualifiers - This routine merges ObjC's GC attribute of 'LHS' and
73692390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian/// 'RHS' attributes and returns the merged version; including for function
73702390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian/// return types.
73712390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz JahanianQualType ASTContext::mergeObjCGCQualifiers(QualType LHS, QualType RHS) {
73722390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian  QualType LHSCan = getCanonicalType(LHS),
73732390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian  RHSCan = getCanonicalType(RHS);
73742390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian  // If two types are identical, they are compatible.
73752390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian  if (LHSCan == RHSCan)
73762390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian    return LHS;
73772390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian  if (RHSCan->isFunctionType()) {
73782390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian    if (!LHSCan->isFunctionType())
73792390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian      return QualType();
73802390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian    QualType OldReturnType =
73812390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian      cast<FunctionType>(RHSCan.getTypePtr())->getResultType();
73822390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian    QualType NewReturnType =
73832390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian      cast<FunctionType>(LHSCan.getTypePtr())->getResultType();
73842390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian    QualType ResReturnType =
73852390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian      mergeObjCGCQualifiers(NewReturnType, OldReturnType);
73862390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian    if (ResReturnType.isNull())
73872390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian      return QualType();
73882390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian    if (ResReturnType == NewReturnType || ResReturnType == OldReturnType) {
73892390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian      // id foo(); ... __strong id foo(); or: __strong id foo(); ... id foo();
73902390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian      // In either case, use OldReturnType to build the new function type.
73912390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian      const FunctionType *F = LHS->getAs<FunctionType>();
73922390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian      if (const FunctionProtoType *FPT = cast<FunctionProtoType>(F)) {
7393e23cf437fe76b1ed02d63c3f61b456fd48a915f5John McCall        FunctionProtoType::ExtProtoInfo EPI = FPT->getExtProtoInfo();
7394e23cf437fe76b1ed02d63c3f61b456fd48a915f5John McCall        EPI.ExtInfo = getFunctionExtInfo(LHS);
73952390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian        QualType ResultType
7396bea522ff43a3f11c7a2bc7949119dbb9fce19e39Jordan Rose          = getFunctionType(OldReturnType,
7397bea522ff43a3f11c7a2bc7949119dbb9fce19e39Jordan Rose                            ArrayRef<QualType>(FPT->arg_type_begin(),
7398bea522ff43a3f11c7a2bc7949119dbb9fce19e39Jordan Rose                                               FPT->getNumArgs()),
7399bea522ff43a3f11c7a2bc7949119dbb9fce19e39Jordan Rose                            EPI);
74002390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian        return ResultType;
74012390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian      }
74022390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian    }
74032390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian    return QualType();
74042390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian  }
74052390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian
74062390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian  // If the qualifiers are different, the types can still be merged.
74072390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian  Qualifiers LQuals = LHSCan.getLocalQualifiers();
74082390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian  Qualifiers RQuals = RHSCan.getLocalQualifiers();
74092390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian  if (LQuals != RQuals) {
74102390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian    // If any of these qualifiers are different, we have a type mismatch.
74112390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian    if (LQuals.getCVRQualifiers() != RQuals.getCVRQualifiers() ||
74122390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian        LQuals.getAddressSpace() != RQuals.getAddressSpace())
74132390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian      return QualType();
74142390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian
74152390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian    // Exactly one GC qualifier difference is allowed: __strong is
74162390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian    // okay if the other type has no GC qualifier but is an Objective
74172390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian    // C object pointer (i.e. implicitly strong by default).  We fix
74182390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian    // this by pretending that the unqualified type was actually
74192390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian    // qualified __strong.
74202390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian    Qualifiers::GC GC_L = LQuals.getObjCGCAttr();
74212390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian    Qualifiers::GC GC_R = RQuals.getObjCGCAttr();
74222390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian    assert((GC_L != GC_R) && "unequal qualifier sets had only equal elements");
74232390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian
74242390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian    if (GC_L == Qualifiers::Weak || GC_R == Qualifiers::Weak)
74252390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian      return QualType();
74262390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian
74272390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian    if (GC_L == Qualifiers::Strong)
74282390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian      return LHS;
74292390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian    if (GC_R == Qualifiers::Strong)
74302390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian      return RHS;
74312390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian    return QualType();
74322390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian  }
74332390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian
74342390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian  if (LHSCan->isObjCObjectPointerType() && RHSCan->isObjCObjectPointerType()) {
74352390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian    QualType LHSBaseQT = LHS->getAs<ObjCObjectPointerType>()->getPointeeType();
74362390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian    QualType RHSBaseQT = RHS->getAs<ObjCObjectPointerType>()->getPointeeType();
74372390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian    QualType ResQT = mergeObjCGCQualifiers(LHSBaseQT, RHSBaseQT);
74382390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian    if (ResQT == LHSBaseQT)
74392390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian      return LHS;
74402390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian    if (ResQT == RHSBaseQT)
74412390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian      return RHS;
74422390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian  }
74432390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian  return QualType();
74442390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian}
74452390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian
74465426bf6456a5aeac416a9150de157904d101c819Chris Lattner//===----------------------------------------------------------------------===//
7447ad74a758189180b8ab8faea648e4766c3bfd7fcbEli Friedman//                         Integer Predicates
7448ad74a758189180b8ab8faea648e4766c3bfd7fcbEli Friedman//===----------------------------------------------------------------------===//
744988054dee0402e4d3c1f64e6b697acc47195c0d72Chris Lattner
74504ba2a17694148e16eaa8d3917f657ffcd3667be4Jay Foadunsigned ASTContext::getIntWidth(QualType T) const {
7451f4c7371fb1d3cebcfb40abad4537bb82515704eaJohn McCall  if (const EnumType *ET = dyn_cast<EnumType>(T))
745229a7f3342c3c6dd15d914c61ae22246c36d51ce7Eli Friedman    T = ET->getDecl()->getIntegerType();
74531274ccd90aec0b205fc838c3d504821ccfb55482Douglas Gregor  if (T->isBooleanType())
74541274ccd90aec0b205fc838c3d504821ccfb55482Douglas Gregor    return 1;
7455f98aba35e6c3da5aae61843fc01334939e4e12ecEli Friedman  // For builtin types, just use the standard type sizing method
7456ad74a758189180b8ab8faea648e4766c3bfd7fcbEli Friedman  return (unsigned)getTypeSize(T);
7457ad74a758189180b8ab8faea648e4766c3bfd7fcbEli Friedman}
7458ad74a758189180b8ab8faea648e4766c3bfd7fcbEli Friedman
7459762f159c3295552f1f3e5e1af8b66385bfaed786Abramo BagnaraQualType ASTContext::getCorrespondingUnsignedType(QualType T) const {
7460f60946222721d9ba3c059563935c17b84703187aDouglas Gregor  assert(T->hasSignedIntegerRepresentation() && "Unexpected type");
74616a2b9261bf9c973c7122d9d1febce24a38fa862dChris Lattner
74626a2b9261bf9c973c7122d9d1febce24a38fa862dChris Lattner  // Turn <4 x signed int> -> <4 x unsigned int>
74636a2b9261bf9c973c7122d9d1febce24a38fa862dChris Lattner  if (const VectorType *VTy = T->getAs<VectorType>())
74646a2b9261bf9c973c7122d9d1febce24a38fa862dChris Lattner    return getVectorType(getCorrespondingUnsignedType(VTy->getElementType()),
7465e86d78cf4754a6aef2cf9a33d847aa15338e276fBob Wilson                         VTy->getNumElements(), VTy->getVectorKind());
74666a2b9261bf9c973c7122d9d1febce24a38fa862dChris Lattner
74676a2b9261bf9c973c7122d9d1febce24a38fa862dChris Lattner  // For enums, we return the unsigned version of the base type.
74686a2b9261bf9c973c7122d9d1febce24a38fa862dChris Lattner  if (const EnumType *ETy = T->getAs<EnumType>())
7469ad74a758189180b8ab8faea648e4766c3bfd7fcbEli Friedman    T = ETy->getDecl()->getIntegerType();
74706a2b9261bf9c973c7122d9d1febce24a38fa862dChris Lattner
74716a2b9261bf9c973c7122d9d1febce24a38fa862dChris Lattner  const BuiltinType *BTy = T->getAs<BuiltinType>();
74726a2b9261bf9c973c7122d9d1febce24a38fa862dChris Lattner  assert(BTy && "Unexpected signed integer type");
7473ad74a758189180b8ab8faea648e4766c3bfd7fcbEli Friedman  switch (BTy->getKind()) {
7474ad74a758189180b8ab8faea648e4766c3bfd7fcbEli Friedman  case BuiltinType::Char_S:
7475ad74a758189180b8ab8faea648e4766c3bfd7fcbEli Friedman  case BuiltinType::SChar:
7476ad74a758189180b8ab8faea648e4766c3bfd7fcbEli Friedman    return UnsignedCharTy;
7477ad74a758189180b8ab8faea648e4766c3bfd7fcbEli Friedman  case BuiltinType::Short:
7478ad74a758189180b8ab8faea648e4766c3bfd7fcbEli Friedman    return UnsignedShortTy;
7479ad74a758189180b8ab8faea648e4766c3bfd7fcbEli Friedman  case BuiltinType::Int:
7480ad74a758189180b8ab8faea648e4766c3bfd7fcbEli Friedman    return UnsignedIntTy;
7481ad74a758189180b8ab8faea648e4766c3bfd7fcbEli Friedman  case BuiltinType::Long:
7482ad74a758189180b8ab8faea648e4766c3bfd7fcbEli Friedman    return UnsignedLongTy;
7483ad74a758189180b8ab8faea648e4766c3bfd7fcbEli Friedman  case BuiltinType::LongLong:
7484ad74a758189180b8ab8faea648e4766c3bfd7fcbEli Friedman    return UnsignedLongLongTy;
74852df9ced9fd1e8c7d7b38443db07e0e811de22571Chris Lattner  case BuiltinType::Int128:
74862df9ced9fd1e8c7d7b38443db07e0e811de22571Chris Lattner    return UnsignedInt128Ty;
7487ad74a758189180b8ab8faea648e4766c3bfd7fcbEli Friedman  default:
7488b219cfc4d75f0a03630b7c4509ef791b7e97b2c8David Blaikie    llvm_unreachable("Unexpected signed integer type");
7489ad74a758189180b8ab8faea648e4766c3bfd7fcbEli Friedman  }
7490ad74a758189180b8ab8faea648e4766c3bfd7fcbEli Friedman}
7491ad74a758189180b8ab8faea648e4766c3bfd7fcbEli Friedman
74927b90340c9c7d07aef4e301e72b5e8a30d5f4f0c8Argyrios KyrtzidisASTMutationListener::~ASTMutationListener() { }
74937b90340c9c7d07aef4e301e72b5e8a30d5f4f0c8Argyrios Kyrtzidis
74949dadfab2faebe40e7dbbfd0801c15174b69bd726Richard Smithvoid ASTMutationListener::DeducedReturnType(const FunctionDecl *FD,
74959dadfab2faebe40e7dbbfd0801c15174b69bd726Richard Smith                                            QualType ReturnType) {}
749686df27bbdbb98c39ec2184695c0561209f91beddChris Lattner
749786df27bbdbb98c39ec2184695c0561209f91beddChris Lattner//===----------------------------------------------------------------------===//
749886df27bbdbb98c39ec2184695c0561209f91beddChris Lattner//                          Builtin Type Computation
749986df27bbdbb98c39ec2184695c0561209f91beddChris Lattner//===----------------------------------------------------------------------===//
750086df27bbdbb98c39ec2184695c0561209f91beddChris Lattner
750186df27bbdbb98c39ec2184695c0561209f91beddChris Lattner/// DecodeTypeFromStr - This decodes one type descriptor from Str, advancing the
750233daae66462e8f51ee153463b32bdefd60c801aaChris Lattner/// pointer over the consumed characters.  This returns the resultant type.  If
750333daae66462e8f51ee153463b32bdefd60c801aaChris Lattner/// AllowTypeModifiers is false then modifier like * are not parsed, just basic
750433daae66462e8f51ee153463b32bdefd60c801aaChris Lattner/// types.  This allows "v2i*" to be parsed as a pointer to a v2i instead of
750533daae66462e8f51ee153463b32bdefd60c801aaChris Lattner/// a vector of "i*".
750614e0e7436cf6650a72052baea1f8ebe644cef489Chris Lattner///
750714e0e7436cf6650a72052baea1f8ebe644cef489Chris Lattner/// RequiresICE is filled in on return to indicate whether the value is required
750814e0e7436cf6650a72052baea1f8ebe644cef489Chris Lattner/// to be an Integer Constant Expression.
75094ba2a17694148e16eaa8d3917f657ffcd3667be4Jay Foadstatic QualType DecodeTypeFromStr(const char *&Str, const ASTContext &Context,
751086df27bbdbb98c39ec2184695c0561209f91beddChris Lattner                                  ASTContext::GetBuiltinTypeError &Error,
751114e0e7436cf6650a72052baea1f8ebe644cef489Chris Lattner                                  bool &RequiresICE,
751233daae66462e8f51ee153463b32bdefd60c801aaChris Lattner                                  bool AllowTypeModifiers) {
751386df27bbdbb98c39ec2184695c0561209f91beddChris Lattner  // Modifiers.
751486df27bbdbb98c39ec2184695c0561209f91beddChris Lattner  int HowLong = 0;
751586df27bbdbb98c39ec2184695c0561209f91beddChris Lattner  bool Signed = false, Unsigned = false;
751614e0e7436cf6650a72052baea1f8ebe644cef489Chris Lattner  RequiresICE = false;
7517393bd8e185692a451b2ba16bdfc9e7d3543b4217Chris Lattner
751833daae66462e8f51ee153463b32bdefd60c801aaChris Lattner  // Read the prefixed modifiers first.
751986df27bbdbb98c39ec2184695c0561209f91beddChris Lattner  bool Done = false;
752086df27bbdbb98c39ec2184695c0561209f91beddChris Lattner  while (!Done) {
752186df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    switch (*Str++) {
75221eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    default: Done = true; --Str; break;
7523393bd8e185692a451b2ba16bdfc9e7d3543b4217Chris Lattner    case 'I':
752414e0e7436cf6650a72052baea1f8ebe644cef489Chris Lattner      RequiresICE = true;
7525393bd8e185692a451b2ba16bdfc9e7d3543b4217Chris Lattner      break;
752686df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    case 'S':
752786df27bbdbb98c39ec2184695c0561209f91beddChris Lattner      assert(!Unsigned && "Can't use both 'S' and 'U' modifiers!");
752886df27bbdbb98c39ec2184695c0561209f91beddChris Lattner      assert(!Signed && "Can't use 'S' modifier multiple times!");
752986df27bbdbb98c39ec2184695c0561209f91beddChris Lattner      Signed = true;
753086df27bbdbb98c39ec2184695c0561209f91beddChris Lattner      break;
753186df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    case 'U':
753286df27bbdbb98c39ec2184695c0561209f91beddChris Lattner      assert(!Signed && "Can't use both 'S' and 'U' modifiers!");
753386df27bbdbb98c39ec2184695c0561209f91beddChris Lattner      assert(!Unsigned && "Can't use 'S' modifier multiple times!");
753486df27bbdbb98c39ec2184695c0561209f91beddChris Lattner      Unsigned = true;
753586df27bbdbb98c39ec2184695c0561209f91beddChris Lattner      break;
753686df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    case 'L':
753786df27bbdbb98c39ec2184695c0561209f91beddChris Lattner      assert(HowLong <= 2 && "Can't have LLLL modifier");
753886df27bbdbb98c39ec2184695c0561209f91beddChris Lattner      ++HowLong;
753986df27bbdbb98c39ec2184695c0561209f91beddChris Lattner      break;
754086df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    }
754186df27bbdbb98c39ec2184695c0561209f91beddChris Lattner  }
754286df27bbdbb98c39ec2184695c0561209f91beddChris Lattner
754386df27bbdbb98c39ec2184695c0561209f91beddChris Lattner  QualType Type;
75441eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
754586df27bbdbb98c39ec2184695c0561209f91beddChris Lattner  // Read the base type.
754686df27bbdbb98c39ec2184695c0561209f91beddChris Lattner  switch (*Str++) {
7547b219cfc4d75f0a03630b7c4509ef791b7e97b2c8David Blaikie  default: llvm_unreachable("Unknown builtin type letter!");
754886df27bbdbb98c39ec2184695c0561209f91beddChris Lattner  case 'v':
754986df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    assert(HowLong == 0 && !Signed && !Unsigned &&
755086df27bbdbb98c39ec2184695c0561209f91beddChris Lattner           "Bad modifiers used with 'v'!");
755186df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    Type = Context.VoidTy;
755286df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    break;
755386df27bbdbb98c39ec2184695c0561209f91beddChris Lattner  case 'f':
755486df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    assert(HowLong == 0 && !Signed && !Unsigned &&
755586df27bbdbb98c39ec2184695c0561209f91beddChris Lattner           "Bad modifiers used with 'f'!");
755686df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    Type = Context.FloatTy;
755786df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    break;
755886df27bbdbb98c39ec2184695c0561209f91beddChris Lattner  case 'd':
755986df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    assert(HowLong < 2 && !Signed && !Unsigned &&
756086df27bbdbb98c39ec2184695c0561209f91beddChris Lattner           "Bad modifiers used with 'd'!");
756186df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    if (HowLong)
756286df27bbdbb98c39ec2184695c0561209f91beddChris Lattner      Type = Context.LongDoubleTy;
756386df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    else
756486df27bbdbb98c39ec2184695c0561209f91beddChris Lattner      Type = Context.DoubleTy;
756586df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    break;
756686df27bbdbb98c39ec2184695c0561209f91beddChris Lattner  case 's':
756786df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    assert(HowLong == 0 && "Bad modifiers used with 's'!");
756886df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    if (Unsigned)
756986df27bbdbb98c39ec2184695c0561209f91beddChris Lattner      Type = Context.UnsignedShortTy;
757086df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    else
757186df27bbdbb98c39ec2184695c0561209f91beddChris Lattner      Type = Context.ShortTy;
757286df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    break;
757386df27bbdbb98c39ec2184695c0561209f91beddChris Lattner  case 'i':
757486df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    if (HowLong == 3)
757586df27bbdbb98c39ec2184695c0561209f91beddChris Lattner      Type = Unsigned ? Context.UnsignedInt128Ty : Context.Int128Ty;
757686df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    else if (HowLong == 2)
757786df27bbdbb98c39ec2184695c0561209f91beddChris Lattner      Type = Unsigned ? Context.UnsignedLongLongTy : Context.LongLongTy;
757886df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    else if (HowLong == 1)
757986df27bbdbb98c39ec2184695c0561209f91beddChris Lattner      Type = Unsigned ? Context.UnsignedLongTy : Context.LongTy;
758086df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    else
758186df27bbdbb98c39ec2184695c0561209f91beddChris Lattner      Type = Unsigned ? Context.UnsignedIntTy : Context.IntTy;
758286df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    break;
758386df27bbdbb98c39ec2184695c0561209f91beddChris Lattner  case 'c':
758486df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    assert(HowLong == 0 && "Bad modifiers used with 'c'!");
758586df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    if (Signed)
758686df27bbdbb98c39ec2184695c0561209f91beddChris Lattner      Type = Context.SignedCharTy;
758786df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    else if (Unsigned)
758886df27bbdbb98c39ec2184695c0561209f91beddChris Lattner      Type = Context.UnsignedCharTy;
758986df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    else
759086df27bbdbb98c39ec2184695c0561209f91beddChris Lattner      Type = Context.CharTy;
759186df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    break;
759286df27bbdbb98c39ec2184695c0561209f91beddChris Lattner  case 'b': // boolean
759386df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    assert(HowLong == 0 && !Signed && !Unsigned && "Bad modifiers for 'b'!");
759486df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    Type = Context.BoolTy;
759586df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    break;
759686df27bbdbb98c39ec2184695c0561209f91beddChris Lattner  case 'z':  // size_t.
759786df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    assert(HowLong == 0 && !Signed && !Unsigned && "Bad modifiers for 'z'!");
759886df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    Type = Context.getSizeType();
759986df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    break;
760086df27bbdbb98c39ec2184695c0561209f91beddChris Lattner  case 'F':
760186df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    Type = Context.getCFConstantStringType();
760286df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    break;
7603ba8bda05fefd3bb2f1ef201784b685f715bdde29Fariborz Jahanian  case 'G':
7604ba8bda05fefd3bb2f1ef201784b685f715bdde29Fariborz Jahanian    Type = Context.getObjCIdType();
7605ba8bda05fefd3bb2f1ef201784b685f715bdde29Fariborz Jahanian    break;
7606ba8bda05fefd3bb2f1ef201784b685f715bdde29Fariborz Jahanian  case 'H':
7607ba8bda05fefd3bb2f1ef201784b685f715bdde29Fariborz Jahanian    Type = Context.getObjCSelType();
7608ba8bda05fefd3bb2f1ef201784b685f715bdde29Fariborz Jahanian    break;
7609f7992132207d7ddc8810a135cec97ebe5805e500Fariborz Jahanian  case 'M':
7610f7992132207d7ddc8810a135cec97ebe5805e500Fariborz Jahanian    Type = Context.getObjCSuperType();
7611f7992132207d7ddc8810a135cec97ebe5805e500Fariborz Jahanian    break;
761286df27bbdbb98c39ec2184695c0561209f91beddChris Lattner  case 'a':
761386df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    Type = Context.getBuiltinVaListType();
761486df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    assert(!Type.isNull() && "builtin va list type not initialized!");
761586df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    break;
761686df27bbdbb98c39ec2184695c0561209f91beddChris Lattner  case 'A':
761786df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    // This is a "reference" to a va_list; however, what exactly
761886df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    // this means depends on how va_list is defined. There are two
761986df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    // different kinds of va_list: ones passed by value, and ones
762086df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    // passed by reference.  An example of a by-value va_list is
762186df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    // x86, where va_list is a char*. An example of by-ref va_list
762286df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    // is x86-64, where va_list is a __va_list_tag[1]. For x86,
762386df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    // we want this argument to be a char*&; for x86-64, we want
762486df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    // it to be a __va_list_tag*.
762586df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    Type = Context.getBuiltinVaListType();
762686df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    assert(!Type.isNull() && "builtin va list type not initialized!");
762714e0e7436cf6650a72052baea1f8ebe644cef489Chris Lattner    if (Type->isArrayType())
762886df27bbdbb98c39ec2184695c0561209f91beddChris Lattner      Type = Context.getArrayDecayedType(Type);
762914e0e7436cf6650a72052baea1f8ebe644cef489Chris Lattner    else
763086df27bbdbb98c39ec2184695c0561209f91beddChris Lattner      Type = Context.getLValueReferenceType(Type);
763186df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    break;
763286df27bbdbb98c39ec2184695c0561209f91beddChris Lattner  case 'V': {
763386df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    char *End;
763486df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    unsigned NumElements = strtoul(Str, &End, 10);
763586df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    assert(End != Str && "Missing vector size");
763686df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    Str = End;
76371eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
763814e0e7436cf6650a72052baea1f8ebe644cef489Chris Lattner    QualType ElementType = DecodeTypeFromStr(Str, Context, Error,
763914e0e7436cf6650a72052baea1f8ebe644cef489Chris Lattner                                             RequiresICE, false);
764014e0e7436cf6650a72052baea1f8ebe644cef489Chris Lattner    assert(!RequiresICE && "Can't require vector ICE");
764133daae66462e8f51ee153463b32bdefd60c801aaChris Lattner
764233daae66462e8f51ee153463b32bdefd60c801aaChris Lattner    // TODO: No way to make AltiVec vectors in builtins yet.
7643788b0fd67e1992f23555454efcdb16a19dfefac3Chris Lattner    Type = Context.getVectorType(ElementType, NumElements,
7644e86d78cf4754a6aef2cf9a33d847aa15338e276fBob Wilson                                 VectorType::GenericVector);
764586df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    break;
764686df27bbdbb98c39ec2184695c0561209f91beddChris Lattner  }
7647b4bc99ba5540a618c86c136c9112ead8fb1d9866Douglas Gregor  case 'E': {
7648b4bc99ba5540a618c86c136c9112ead8fb1d9866Douglas Gregor    char *End;
7649b4bc99ba5540a618c86c136c9112ead8fb1d9866Douglas Gregor
7650b4bc99ba5540a618c86c136c9112ead8fb1d9866Douglas Gregor    unsigned NumElements = strtoul(Str, &End, 10);
7651b4bc99ba5540a618c86c136c9112ead8fb1d9866Douglas Gregor    assert(End != Str && "Missing vector size");
7652b4bc99ba5540a618c86c136c9112ead8fb1d9866Douglas Gregor
7653b4bc99ba5540a618c86c136c9112ead8fb1d9866Douglas Gregor    Str = End;
7654b4bc99ba5540a618c86c136c9112ead8fb1d9866Douglas Gregor
7655b4bc99ba5540a618c86c136c9112ead8fb1d9866Douglas Gregor    QualType ElementType = DecodeTypeFromStr(Str, Context, Error, RequiresICE,
7656b4bc99ba5540a618c86c136c9112ead8fb1d9866Douglas Gregor                                             false);
7657b4bc99ba5540a618c86c136c9112ead8fb1d9866Douglas Gregor    Type = Context.getExtVectorType(ElementType, NumElements);
7658b4bc99ba5540a618c86c136c9112ead8fb1d9866Douglas Gregor    break;
7659b4bc99ba5540a618c86c136c9112ead8fb1d9866Douglas Gregor  }
7660d3a23b238a2b2c0f11e6ac4951c7410a8c5717bfDouglas Gregor  case 'X': {
766114e0e7436cf6650a72052baea1f8ebe644cef489Chris Lattner    QualType ElementType = DecodeTypeFromStr(Str, Context, Error, RequiresICE,
766214e0e7436cf6650a72052baea1f8ebe644cef489Chris Lattner                                             false);
766314e0e7436cf6650a72052baea1f8ebe644cef489Chris Lattner    assert(!RequiresICE && "Can't require complex ICE");
7664d3a23b238a2b2c0f11e6ac4951c7410a8c5717bfDouglas Gregor    Type = Context.getComplexType(ElementType);
7665d3a23b238a2b2c0f11e6ac4951c7410a8c5717bfDouglas Gregor    break;
7666cc075e4092900ac9eec9dc92e35096eae1ee29c7Fariborz Jahanian  }
7667cc075e4092900ac9eec9dc92e35096eae1ee29c7Fariborz Jahanian  case 'Y' : {
7668cc075e4092900ac9eec9dc92e35096eae1ee29c7Fariborz Jahanian    Type = Context.getPointerDiffType();
7669cc075e4092900ac9eec9dc92e35096eae1ee29c7Fariborz Jahanian    break;
7670cc075e4092900ac9eec9dc92e35096eae1ee29c7Fariborz Jahanian  }
76719a5a7e7351f78345a72c4956af25590f6d40ebcdChris Lattner  case 'P':
7672c29f77b769bcc5b6dc85e72c8e3cc2e348e5cf25Douglas Gregor    Type = Context.getFILEType();
7673c29f77b769bcc5b6dc85e72c8e3cc2e348e5cf25Douglas Gregor    if (Type.isNull()) {
7674f711c41dd9412a8182793259d355c4f6979ed5edMike Stump      Error = ASTContext::GE_Missing_stdio;
767586df27bbdbb98c39ec2184695c0561209f91beddChris Lattner      return QualType();
767686df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    }
7677fd612dbb23cd31c03c898ae53ff18d0dfd8488f9Mike Stump    break;
76789a5a7e7351f78345a72c4956af25590f6d40ebcdChris Lattner  case 'J':
7679f711c41dd9412a8182793259d355c4f6979ed5edMike Stump    if (Signed)
7680782fa308a765aeac2acb39c4e697c937ec21185bMike Stump      Type = Context.getsigjmp_bufType();
7681f711c41dd9412a8182793259d355c4f6979ed5edMike Stump    else
7682f711c41dd9412a8182793259d355c4f6979ed5edMike Stump      Type = Context.getjmp_bufType();
7683f711c41dd9412a8182793259d355c4f6979ed5edMike Stump
7684fd612dbb23cd31c03c898ae53ff18d0dfd8488f9Mike Stump    if (Type.isNull()) {
7685f711c41dd9412a8182793259d355c4f6979ed5edMike Stump      Error = ASTContext::GE_Missing_setjmp;
7686fd612dbb23cd31c03c898ae53ff18d0dfd8488f9Mike Stump      return QualType();
7687fd612dbb23cd31c03c898ae53ff18d0dfd8488f9Mike Stump    }
7688fd612dbb23cd31c03c898ae53ff18d0dfd8488f9Mike Stump    break;
7689e2d4f4ed44a32f179c6d48cd1dba8346ab2129d9Rafael Espindola  case 'K':
7690e2d4f4ed44a32f179c6d48cd1dba8346ab2129d9Rafael Espindola    assert(HowLong == 0 && !Signed && !Unsigned && "Bad modifiers for 'K'!");
7691e2d4f4ed44a32f179c6d48cd1dba8346ab2129d9Rafael Espindola    Type = Context.getucontext_tType();
7692e2d4f4ed44a32f179c6d48cd1dba8346ab2129d9Rafael Espindola
7693e2d4f4ed44a32f179c6d48cd1dba8346ab2129d9Rafael Espindola    if (Type.isNull()) {
7694e2d4f4ed44a32f179c6d48cd1dba8346ab2129d9Rafael Espindola      Error = ASTContext::GE_Missing_ucontext;
7695e2d4f4ed44a32f179c6d48cd1dba8346ab2129d9Rafael Espindola      return QualType();
7696e2d4f4ed44a32f179c6d48cd1dba8346ab2129d9Rafael Espindola    }
7697e2d4f4ed44a32f179c6d48cd1dba8346ab2129d9Rafael Espindola    break;
76986902e4146d426998ff6a94b16776c1b3f805a048Eli Friedman  case 'p':
76996902e4146d426998ff6a94b16776c1b3f805a048Eli Friedman    Type = Context.getProcessIDType();
77006902e4146d426998ff6a94b16776c1b3f805a048Eli Friedman    break;
7701782fa308a765aeac2acb39c4e697c937ec21185bMike Stump  }
77021eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
770333daae66462e8f51ee153463b32bdefd60c801aaChris Lattner  // If there are modifiers and if we're allowed to parse them, go for it.
770433daae66462e8f51ee153463b32bdefd60c801aaChris Lattner  Done = !AllowTypeModifiers;
770586df27bbdbb98c39ec2184695c0561209f91beddChris Lattner  while (!Done) {
7706187ab37a05b8f7015b9f39cc8cd9129a0c6d0b48John McCall    switch (char c = *Str++) {
770733daae66462e8f51ee153463b32bdefd60c801aaChris Lattner    default: Done = true; --Str; break;
770833daae66462e8f51ee153463b32bdefd60c801aaChris Lattner    case '*':
770933daae66462e8f51ee153463b32bdefd60c801aaChris Lattner    case '&': {
771033daae66462e8f51ee153463b32bdefd60c801aaChris Lattner      // Both pointers and references can have their pointee types
771133daae66462e8f51ee153463b32bdefd60c801aaChris Lattner      // qualified with an address space.
771233daae66462e8f51ee153463b32bdefd60c801aaChris Lattner      char *End;
771333daae66462e8f51ee153463b32bdefd60c801aaChris Lattner      unsigned AddrSpace = strtoul(Str, &End, 10);
771433daae66462e8f51ee153463b32bdefd60c801aaChris Lattner      if (End != Str && AddrSpace != 0) {
771533daae66462e8f51ee153463b32bdefd60c801aaChris Lattner        Type = Context.getAddrSpaceQualType(Type, AddrSpace);
771633daae66462e8f51ee153463b32bdefd60c801aaChris Lattner        Str = End;
771733daae66462e8f51ee153463b32bdefd60c801aaChris Lattner      }
771833daae66462e8f51ee153463b32bdefd60c801aaChris Lattner      if (c == '*')
771933daae66462e8f51ee153463b32bdefd60c801aaChris Lattner        Type = Context.getPointerType(Type);
772033daae66462e8f51ee153463b32bdefd60c801aaChris Lattner      else
772133daae66462e8f51ee153463b32bdefd60c801aaChris Lattner        Type = Context.getLValueReferenceType(Type);
772233daae66462e8f51ee153463b32bdefd60c801aaChris Lattner      break;
772333daae66462e8f51ee153463b32bdefd60c801aaChris Lattner    }
772433daae66462e8f51ee153463b32bdefd60c801aaChris Lattner    // FIXME: There's no way to have a built-in with an rvalue ref arg.
772533daae66462e8f51ee153463b32bdefd60c801aaChris Lattner    case 'C':
772633daae66462e8f51ee153463b32bdefd60c801aaChris Lattner      Type = Type.withConst();
772733daae66462e8f51ee153463b32bdefd60c801aaChris Lattner      break;
772833daae66462e8f51ee153463b32bdefd60c801aaChris Lattner    case 'D':
772933daae66462e8f51ee153463b32bdefd60c801aaChris Lattner      Type = Context.getVolatileType(Type);
773033daae66462e8f51ee153463b32bdefd60c801aaChris Lattner      break;
773118932a0f2a94a7813ec461d1118c39ecf8aa936fTed Kremenek    case 'R':
773218932a0f2a94a7813ec461d1118c39ecf8aa936fTed Kremenek      Type = Type.withRestrict();
773318932a0f2a94a7813ec461d1118c39ecf8aa936fTed Kremenek      break;
773486df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    }
773586df27bbdbb98c39ec2184695c0561209f91beddChris Lattner  }
7736393bd8e185692a451b2ba16bdfc9e7d3543b4217Chris Lattner
773714e0e7436cf6650a72052baea1f8ebe644cef489Chris Lattner  assert((!RequiresICE || Type->isIntegralOrEnumerationType()) &&
7738393bd8e185692a451b2ba16bdfc9e7d3543b4217Chris Lattner         "Integer constant 'I' type must be an integer");
77391eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
774086df27bbdbb98c39ec2184695c0561209f91beddChris Lattner  return Type;
774186df27bbdbb98c39ec2184695c0561209f91beddChris Lattner}
774286df27bbdbb98c39ec2184695c0561209f91beddChris Lattner
774386df27bbdbb98c39ec2184695c0561209f91beddChris Lattner/// GetBuiltinType - Return the type for the specified builtin.
774433daae66462e8f51ee153463b32bdefd60c801aaChris LattnerQualType ASTContext::GetBuiltinType(unsigned Id,
774514e0e7436cf6650a72052baea1f8ebe644cef489Chris Lattner                                    GetBuiltinTypeError &Error,
77464ba2a17694148e16eaa8d3917f657ffcd3667be4Jay Foad                                    unsigned *IntegerConstantArgs) const {
774733daae66462e8f51ee153463b32bdefd60c801aaChris Lattner  const char *TypeStr = BuiltinInfo.GetTypeString(Id);
77481eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
77495f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner  SmallVector<QualType, 8> ArgTypes;
77501eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
775114e0e7436cf6650a72052baea1f8ebe644cef489Chris Lattner  bool RequiresICE = false;
775286df27bbdbb98c39ec2184695c0561209f91beddChris Lattner  Error = GE_None;
775314e0e7436cf6650a72052baea1f8ebe644cef489Chris Lattner  QualType ResType = DecodeTypeFromStr(TypeStr, *this, Error,
775414e0e7436cf6650a72052baea1f8ebe644cef489Chris Lattner                                       RequiresICE, true);
775586df27bbdbb98c39ec2184695c0561209f91beddChris Lattner  if (Error != GE_None)
775686df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    return QualType();
775714e0e7436cf6650a72052baea1f8ebe644cef489Chris Lattner
775814e0e7436cf6650a72052baea1f8ebe644cef489Chris Lattner  assert(!RequiresICE && "Result of intrinsic cannot be required to be an ICE");
775914e0e7436cf6650a72052baea1f8ebe644cef489Chris Lattner
776086df27bbdbb98c39ec2184695c0561209f91beddChris Lattner  while (TypeStr[0] && TypeStr[0] != '.') {
776114e0e7436cf6650a72052baea1f8ebe644cef489Chris Lattner    QualType Ty = DecodeTypeFromStr(TypeStr, *this, Error, RequiresICE, true);
776286df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    if (Error != GE_None)
776386df27bbdbb98c39ec2184695c0561209f91beddChris Lattner      return QualType();
776486df27bbdbb98c39ec2184695c0561209f91beddChris Lattner
776514e0e7436cf6650a72052baea1f8ebe644cef489Chris Lattner    // If this argument is required to be an IntegerConstantExpression and the
776614e0e7436cf6650a72052baea1f8ebe644cef489Chris Lattner    // caller cares, fill in the bitmask we return.
776714e0e7436cf6650a72052baea1f8ebe644cef489Chris Lattner    if (RequiresICE && IntegerConstantArgs)
776814e0e7436cf6650a72052baea1f8ebe644cef489Chris Lattner      *IntegerConstantArgs |= 1 << ArgTypes.size();
776914e0e7436cf6650a72052baea1f8ebe644cef489Chris Lattner
777086df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    // Do array -> pointer decay.  The builtin should use the decayed type.
777186df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    if (Ty->isArrayType())
777286df27bbdbb98c39ec2184695c0561209f91beddChris Lattner      Ty = getArrayDecayedType(Ty);
77731eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
777486df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    ArgTypes.push_back(Ty);
777586df27bbdbb98c39ec2184695c0561209f91beddChris Lattner  }
777686df27bbdbb98c39ec2184695c0561209f91beddChris Lattner
777786df27bbdbb98c39ec2184695c0561209f91beddChris Lattner  assert((TypeStr[0] != '.' || TypeStr[1] == 0) &&
777886df27bbdbb98c39ec2184695c0561209f91beddChris Lattner         "'.' should only occur at end of builtin type list!");
777986df27bbdbb98c39ec2184695c0561209f91beddChris Lattner
778000ccbefcffeb88ea3e2e6323e594fa968753ad14John McCall  FunctionType::ExtInfo EI;
778100ccbefcffeb88ea3e2e6323e594fa968753ad14John McCall  if (BuiltinInfo.isNoReturn(Id)) EI = EI.withNoReturn(true);
778200ccbefcffeb88ea3e2e6323e594fa968753ad14John McCall
778300ccbefcffeb88ea3e2e6323e594fa968753ad14John McCall  bool Variadic = (TypeStr[0] == '.');
778400ccbefcffeb88ea3e2e6323e594fa968753ad14John McCall
778500ccbefcffeb88ea3e2e6323e594fa968753ad14John McCall  // We really shouldn't be making a no-proto type here, especially in C++.
778600ccbefcffeb88ea3e2e6323e594fa968753ad14John McCall  if (ArgTypes.empty() && Variadic)
778700ccbefcffeb88ea3e2e6323e594fa968753ad14John McCall    return getFunctionNoProtoType(ResType, EI);
7788ce056bcaa1c97b89a4b2de2112c62d060863be2bDouglas Gregor
7789e23cf437fe76b1ed02d63c3f61b456fd48a915f5John McCall  FunctionProtoType::ExtProtoInfo EPI;
779000ccbefcffeb88ea3e2e6323e594fa968753ad14John McCall  EPI.ExtInfo = EI;
779100ccbefcffeb88ea3e2e6323e594fa968753ad14John McCall  EPI.Variadic = Variadic;
7792e23cf437fe76b1ed02d63c3f61b456fd48a915f5John McCall
7793bea522ff43a3f11c7a2bc7949119dbb9fce19e39Jordan Rose  return getFunctionType(ResType, ArgTypes, EPI);
779486df27bbdbb98c39ec2184695c0561209f91beddChris Lattner}
7795a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman
779690e99a84ddd020e8fda79643748243725a2ed071Argyrios KyrtzidisGVALinkage ASTContext::GetGVALinkageForFunction(const FunctionDecl *FD) {
7797181e3ecc0907ae0103586a9f4db52241995a8267Rafael Espindola  if (!FD->isExternallyVisible())
779890e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis    return GVA_Internal;
779990e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis
7800181e3ecc0907ae0103586a9f4db52241995a8267Rafael Espindola  GVALinkage External = GVA_StrongExternal;
7801181e3ecc0907ae0103586a9f4db52241995a8267Rafael Espindola  switch (FD->getTemplateSpecializationKind()) {
7802181e3ecc0907ae0103586a9f4db52241995a8267Rafael Espindola  case TSK_Undeclared:
7803181e3ecc0907ae0103586a9f4db52241995a8267Rafael Espindola  case TSK_ExplicitSpecialization:
7804181e3ecc0907ae0103586a9f4db52241995a8267Rafael Espindola    External = GVA_StrongExternal;
7805181e3ecc0907ae0103586a9f4db52241995a8267Rafael Espindola    break;
780690e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis
7807181e3ecc0907ae0103586a9f4db52241995a8267Rafael Espindola  case TSK_ExplicitInstantiationDefinition:
7808181e3ecc0907ae0103586a9f4db52241995a8267Rafael Espindola    return GVA_ExplicitTemplateInstantiation;
7809181e3ecc0907ae0103586a9f4db52241995a8267Rafael Espindola
7810181e3ecc0907ae0103586a9f4db52241995a8267Rafael Espindola  case TSK_ExplicitInstantiationDeclaration:
7811181e3ecc0907ae0103586a9f4db52241995a8267Rafael Espindola  case TSK_ImplicitInstantiation:
7812181e3ecc0907ae0103586a9f4db52241995a8267Rafael Espindola    External = GVA_TemplateInstantiation;
7813181e3ecc0907ae0103586a9f4db52241995a8267Rafael Espindola    break;
781490e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis  }
781590e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis
781690e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis  if (!FD->isInlined())
781790e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis    return External;
781890e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis
78194e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie  if (!getLangOpts().CPlusPlus || FD->hasAttr<GNUInlineAttr>()) {
782090e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis    // GNU or C99 inline semantics. Determine whether this symbol should be
782190e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis    // externally visible.
782290e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis    if (FD->isInlineDefinitionExternallyVisible())
782390e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis      return External;
782490e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis
782590e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis    // C99 inline semantics, where the symbol is not externally visible.
782690e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis    return GVA_C99Inline;
782790e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis  }
782890e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis
782990e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis  // C++0x [temp.explicit]p9:
783090e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis  //   [ Note: The intent is that an inline function that is the subject of
783190e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis  //   an explicit instantiation declaration will still be implicitly
783290e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis  //   instantiated when used so that the body can be considered for
783390e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis  //   inlining, but that no out-of-line copy of the inline function would be
783490e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis  //   generated in the translation unit. -- end note ]
783590e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis  if (FD->getTemplateSpecializationKind()
783690e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis                                       == TSK_ExplicitInstantiationDeclaration)
783790e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis    return GVA_C99Inline;
783890e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis
783990e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis  return GVA_CXXInline;
784090e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis}
784190e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis
784290e99a84ddd020e8fda79643748243725a2ed071Argyrios KyrtzidisGVALinkage ASTContext::GetGVALinkageForVariable(const VarDecl *VD) {
7843181e3ecc0907ae0103586a9f4db52241995a8267Rafael Espindola  if (!VD->isExternallyVisible())
7844181e3ecc0907ae0103586a9f4db52241995a8267Rafael Espindola    return GVA_Internal;
7845181e3ecc0907ae0103586a9f4db52241995a8267Rafael Espindola
784690e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis  // If this is a static data member, compute the kind of template
784790e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis  // specialization. Otherwise, this variable is not part of a
784890e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis  // template.
784990e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis  TemplateSpecializationKind TSK = TSK_Undeclared;
785090e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis  if (VD->isStaticDataMember())
785190e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis    TSK = VD->getTemplateSpecializationKind();
785290e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis
7853181e3ecc0907ae0103586a9f4db52241995a8267Rafael Espindola  switch (TSK) {
7854181e3ecc0907ae0103586a9f4db52241995a8267Rafael Espindola  case TSK_Undeclared:
7855181e3ecc0907ae0103586a9f4db52241995a8267Rafael Espindola  case TSK_ExplicitSpecialization:
7856181e3ecc0907ae0103586a9f4db52241995a8267Rafael Espindola    return GVA_StrongExternal;
785790e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis
7858181e3ecc0907ae0103586a9f4db52241995a8267Rafael Espindola  case TSK_ExplicitInstantiationDeclaration:
7859181e3ecc0907ae0103586a9f4db52241995a8267Rafael Espindola    llvm_unreachable("Variable should not be instantiated");
7860181e3ecc0907ae0103586a9f4db52241995a8267Rafael Espindola  // Fall through to treat this like any other instantiation.
786190e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis
7862181e3ecc0907ae0103586a9f4db52241995a8267Rafael Espindola  case TSK_ExplicitInstantiationDefinition:
7863181e3ecc0907ae0103586a9f4db52241995a8267Rafael Espindola    return GVA_ExplicitTemplateInstantiation;
786490e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis
7865181e3ecc0907ae0103586a9f4db52241995a8267Rafael Espindola  case TSK_ImplicitInstantiation:
7866181e3ecc0907ae0103586a9f4db52241995a8267Rafael Espindola    return GVA_TemplateInstantiation;
786790e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis  }
786877b502580d2d3a04ee0a83e9e22eb6b8d8be3ea6Rafael Espindola
786977b502580d2d3a04ee0a83e9e22eb6b8d8be3ea6Rafael Espindola  llvm_unreachable("Invalid Linkage!");
787090e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis}
787190e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis
78724ac7c0bb39696e92fd220118fedc484c09a69870Argyrios Kyrtzidisbool ASTContext::DeclMustBeEmitted(const Decl *D) {
787390e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis  if (const VarDecl *VD = dyn_cast<VarDecl>(D)) {
787490e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis    if (!VD->isFileVarDecl())
787590e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis      return false;
7876f396ad9b1fa0c74c9db16a8158c3882c9db774e2Richard Smith  } else if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
7877f396ad9b1fa0c74c9db16a8158c3882c9db774e2Richard Smith    // We never need to emit an uninstantiated function template.
7878f396ad9b1fa0c74c9db16a8158c3882c9db774e2Richard Smith    if (FD->getTemplatedKind() == FunctionDecl::TK_FunctionTemplate)
7879f396ad9b1fa0c74c9db16a8158c3882c9db774e2Richard Smith      return false;
7880f396ad9b1fa0c74c9db16a8158c3882c9db774e2Richard Smith  } else
7881f396ad9b1fa0c74c9db16a8158c3882c9db774e2Richard Smith    return false;
7882f396ad9b1fa0c74c9db16a8158c3882c9db774e2Richard Smith
7883f396ad9b1fa0c74c9db16a8158c3882c9db774e2Richard Smith  // If this is a member of a class template, we do not need to emit it.
7884f396ad9b1fa0c74c9db16a8158c3882c9db774e2Richard Smith  if (D->getDeclContext()->isDependentContext())
788590e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis    return false;
788690e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis
7887ab411c8c2efed8f2403bf8596e780c0f2f905a19Argyrios Kyrtzidis  // Weak references don't produce any output by themselves.
7888ab411c8c2efed8f2403bf8596e780c0f2f905a19Argyrios Kyrtzidis  if (D->hasAttr<WeakRefAttr>())
7889ab411c8c2efed8f2403bf8596e780c0f2f905a19Argyrios Kyrtzidis    return false;
7890ab411c8c2efed8f2403bf8596e780c0f2f905a19Argyrios Kyrtzidis
789190e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis  // Aliases and used decls are required.
789290e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis  if (D->hasAttr<AliasAttr>() || D->hasAttr<UsedAttr>())
789390e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis    return true;
789490e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis
789590e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis  if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
789690e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis    // Forward declarations aren't required.
789710620eb5164e31208fcbf0437cd79ae535ed0559Sean Hunt    if (!FD->doesThisDeclarationHaveABody())
7898dce67a70a86db8758c926a76fdd980f5369d5746Nick Lewycky      return FD->doesDeclarationForceExternallyVisibleDefinition();
789990e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis
790090e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis    // Constructors and destructors are required.
790190e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis    if (FD->hasAttr<ConstructorAttr>() || FD->hasAttr<DestructorAttr>())
790290e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis      return true;
790390e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis
7904d5617eeafc93209a26b9f88276c88cf997c3a0a7John McCall    // The key function for a class is required.  This rule only comes
7905d5617eeafc93209a26b9f88276c88cf997c3a0a7John McCall    // into play when inline functions can be key functions, though.
7906d5617eeafc93209a26b9f88276c88cf997c3a0a7John McCall    if (getTargetInfo().getCXXABI().canKeyFunctionBeInline()) {
7907d5617eeafc93209a26b9f88276c88cf997c3a0a7John McCall      if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(FD)) {
7908d5617eeafc93209a26b9f88276c88cf997c3a0a7John McCall        const CXXRecordDecl *RD = MD->getParent();
7909d5617eeafc93209a26b9f88276c88cf997c3a0a7John McCall        if (MD->isOutOfLine() && RD->isDynamicClass()) {
7910d5617eeafc93209a26b9f88276c88cf997c3a0a7John McCall          const CXXMethodDecl *KeyFunc = getCurrentKeyFunction(RD);
7911d5617eeafc93209a26b9f88276c88cf997c3a0a7John McCall          if (KeyFunc && KeyFunc->getCanonicalDecl() == MD->getCanonicalDecl())
7912d5617eeafc93209a26b9f88276c88cf997c3a0a7John McCall            return true;
7913d5617eeafc93209a26b9f88276c88cf997c3a0a7John McCall        }
791490e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis      }
791590e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis    }
791690e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis
791790e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis    GVALinkage Linkage = GetGVALinkageForFunction(FD);
791890e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis
791990e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis    // static, static inline, always_inline, and extern inline functions can
792090e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis    // always be deferred.  Normal inline functions can be deferred in C99/C++.
792190e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis    // Implicit template instantiations can also be deferred in C++.
792290e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis    if (Linkage == GVA_Internal  || Linkage == GVA_C99Inline ||
79233a5aca8112e03814f78906dc5d032287adb272b5Anton Yartsev        Linkage == GVA_CXXInline || Linkage == GVA_TemplateInstantiation)
792490e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis      return false;
792590e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis    return true;
792690e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis  }
792794da1587f7d584fc61df793229d197969f204cd9Douglas Gregor
792890e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis  const VarDecl *VD = cast<VarDecl>(D);
792990e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis  assert(VD->isFileVarDecl() && "Expected file scoped var");
793090e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis
7931ab411c8c2efed8f2403bf8596e780c0f2f905a19Argyrios Kyrtzidis  if (VD->isThisDeclarationADefinition() == VarDecl::DeclarationOnly)
7932ab411c8c2efed8f2403bf8596e780c0f2f905a19Argyrios Kyrtzidis    return false;
7933ab411c8c2efed8f2403bf8596e780c0f2f905a19Argyrios Kyrtzidis
79345f9a7e31d038843f1d3a5e2544fc365ffd563951Richard Smith  // Variables that can be needed in other TUs are required.
793590e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis  GVALinkage L = GetGVALinkageForVariable(VD);
79365f9a7e31d038843f1d3a5e2544fc365ffd563951Richard Smith  if (L != GVA_Internal && L != GVA_TemplateInstantiation)
79375f9a7e31d038843f1d3a5e2544fc365ffd563951Richard Smith    return true;
793890e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis
79395f9a7e31d038843f1d3a5e2544fc365ffd563951Richard Smith  // Variables that have destruction with side-effects are required.
79405f9a7e31d038843f1d3a5e2544fc365ffd563951Richard Smith  if (VD->getType().isDestructedType())
79415f9a7e31d038843f1d3a5e2544fc365ffd563951Richard Smith    return true;
79425f9a7e31d038843f1d3a5e2544fc365ffd563951Richard Smith
79435f9a7e31d038843f1d3a5e2544fc365ffd563951Richard Smith  // Variables that have initialization with side-effects are required.
79445f9a7e31d038843f1d3a5e2544fc365ffd563951Richard Smith  if (VD->getInit() && VD->getInit()->HasSideEffects(*this))
79455f9a7e31d038843f1d3a5e2544fc365ffd563951Richard Smith    return true;
79465f9a7e31d038843f1d3a5e2544fc365ffd563951Richard Smith
79475f9a7e31d038843f1d3a5e2544fc365ffd563951Richard Smith  return false;
794890e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis}
7949071cc7deffad608165b1ddd5263e8bf181861520Charles Davis
79508f88a1dcc57cfe8580eb1558a783ad8499bfe8e0Timur IskhodzhanovCallingConv ASTContext::getDefaultCXXMethodCallConv(bool isVariadic) {
7951ee743f903858e337434ac0335f147f4de4ecae05Charles Davis  // Pass through to the C++ ABI object
79528f88a1dcc57cfe8580eb1558a783ad8499bfe8e0Timur Iskhodzhanov  return ABI->getDefaultMethodCallConv(isVariadic);
79538f88a1dcc57cfe8580eb1558a783ad8499bfe8e0Timur Iskhodzhanov}
79548f88a1dcc57cfe8580eb1558a783ad8499bfe8e0Timur Iskhodzhanov
79558f88a1dcc57cfe8580eb1558a783ad8499bfe8e0Timur IskhodzhanovCallingConv ASTContext::getCanonicalCallConv(CallingConv CC) const {
7956b8b2c9da87e7d70a1679db026f40548b3192b705John McCall  if (CC == CC_C && !LangOpts.MRTD &&
7957b8b2c9da87e7d70a1679db026f40548b3192b705John McCall      getTargetInfo().getCXXABI().isMemberFunctionCCDefault())
79588f88a1dcc57cfe8580eb1558a783ad8499bfe8e0Timur Iskhodzhanov    return CC_Default;
79598f88a1dcc57cfe8580eb1558a783ad8499bfe8e0Timur Iskhodzhanov  return CC;
7960ee743f903858e337434ac0335f147f4de4ecae05Charles Davis}
7961ee743f903858e337434ac0335f147f4de4ecae05Charles Davis
79624ba2a17694148e16eaa8d3917f657ffcd3667be4Jay Foadbool ASTContext::isNearlyEmpty(const CXXRecordDecl *RD) const {
7963dae0cb52e4e3d46bbfc9a4510909522197a92e54Anders Carlsson  // Pass through to the C++ ABI object
7964dae0cb52e4e3d46bbfc9a4510909522197a92e54Anders Carlsson  return ABI->isNearlyEmpty(RD);
7965dae0cb52e4e3d46bbfc9a4510909522197a92e54Anders Carlsson}
7966dae0cb52e4e3d46bbfc9a4510909522197a92e54Anders Carlsson
796714110477887e3dc168ffc6c191e72d705051f99ePeter CollingbourneMangleContext *ASTContext::createMangleContext() {
7968b8b2c9da87e7d70a1679db026f40548b3192b705John McCall  switch (Target->getCXXABI().getKind()) {
7969c264e16a42b3f6c36521857a29ea0949d9781c22Tim Northover  case TargetCXXABI::GenericAArch64:
7970b8b2c9da87e7d70a1679db026f40548b3192b705John McCall  case TargetCXXABI::GenericItanium:
7971b8b2c9da87e7d70a1679db026f40548b3192b705John McCall  case TargetCXXABI::GenericARM:
7972b8b2c9da87e7d70a1679db026f40548b3192b705John McCall  case TargetCXXABI::iOS:
797314110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne    return createItaniumMangleContext(*this, getDiagnostics());
7974b8b2c9da87e7d70a1679db026f40548b3192b705John McCall  case TargetCXXABI::Microsoft:
797514110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne    return createMicrosoftMangleContext(*this, getDiagnostics());
797614110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne  }
7977b219cfc4d75f0a03630b7c4509ef791b7e97b2c8David Blaikie  llvm_unreachable("Unsupported ABI");
797814110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne}
797914110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne
7980071cc7deffad608165b1ddd5263e8bf181861520Charles DavisCXXABI::~CXXABI() {}
7981ba29bd25515fbd99e98ba0fedb9d93617b27609eTed Kremenek
7982ba29bd25515fbd99e98ba0fedb9d93617b27609eTed Kremeneksize_t ASTContext::getSideTableAllocatedMemory() const {
79830c8cd1ac55720276ad41fa7b4f8785bead5afd32Ted Kremenek  return ASTRecordLayouts.getMemorySize()
79840c8cd1ac55720276ad41fa7b4f8785bead5afd32Ted Kremenek    + llvm::capacity_in_bytes(ObjCLayouts)
79850c8cd1ac55720276ad41fa7b4f8785bead5afd32Ted Kremenek    + llvm::capacity_in_bytes(KeyFunctions)
79860c8cd1ac55720276ad41fa7b4f8785bead5afd32Ted Kremenek    + llvm::capacity_in_bytes(ObjCImpls)
79870c8cd1ac55720276ad41fa7b4f8785bead5afd32Ted Kremenek    + llvm::capacity_in_bytes(BlockVarCopyInits)
79880c8cd1ac55720276ad41fa7b4f8785bead5afd32Ted Kremenek    + llvm::capacity_in_bytes(DeclAttrs)
79890c8cd1ac55720276ad41fa7b4f8785bead5afd32Ted Kremenek    + llvm::capacity_in_bytes(InstantiatedFromStaticDataMember)
79900c8cd1ac55720276ad41fa7b4f8785bead5afd32Ted Kremenek    + llvm::capacity_in_bytes(InstantiatedFromUsingDecl)
79910c8cd1ac55720276ad41fa7b4f8785bead5afd32Ted Kremenek    + llvm::capacity_in_bytes(InstantiatedFromUsingShadowDecl)
79920c8cd1ac55720276ad41fa7b4f8785bead5afd32Ted Kremenek    + llvm::capacity_in_bytes(InstantiatedFromUnnamedFieldDecl)
79930c8cd1ac55720276ad41fa7b4f8785bead5afd32Ted Kremenek    + llvm::capacity_in_bytes(OverriddenMethods)
79940c8cd1ac55720276ad41fa7b4f8785bead5afd32Ted Kremenek    + llvm::capacity_in_bytes(Types)
7995af0f4d0b2e38c810effc8b024ad2fb6604eec5d3Francois Pichet    + llvm::capacity_in_bytes(VariableArrayTypes)
79960d95f0d7b81110f77e99e833f766d19be7b7e072Francois Pichet    + llvm::capacity_in_bytes(ClassScopeSpecializationPattern);
7997ba29bd25515fbd99e98ba0fedb9d93617b27609eTed Kremenek}
7998d211cb709510fbe7e75167b9feee0050851d001aTed Kremenek
799966cff7257698d5528632917d38f9a3037bb1506dDavid Blaikievoid ASTContext::addUnnamedTag(const TagDecl *Tag) {
800066cff7257698d5528632917d38f9a3037bb1506dDavid Blaikie  // FIXME: This mangling should be applied to function local classes too
800166cff7257698d5528632917d38f9a3037bb1506dDavid Blaikie  if (!Tag->getName().empty() || Tag->getTypedefNameForAnonDecl() ||
8002181e3ecc0907ae0103586a9f4db52241995a8267Rafael Espindola      !isa<CXXRecordDecl>(Tag->getParent()) ||
8003181e3ecc0907ae0103586a9f4db52241995a8267Rafael Espindola      !Tag->isExternallyVisible())
800466cff7257698d5528632917d38f9a3037bb1506dDavid Blaikie    return;
800566cff7257698d5528632917d38f9a3037bb1506dDavid Blaikie
800666cff7257698d5528632917d38f9a3037bb1506dDavid Blaikie  std::pair<llvm::DenseMap<const DeclContext *, unsigned>::iterator, bool> P =
800766cff7257698d5528632917d38f9a3037bb1506dDavid Blaikie    UnnamedMangleContexts.insert(std::make_pair(Tag->getParent(), 0));
800866cff7257698d5528632917d38f9a3037bb1506dDavid Blaikie  UnnamedMangleNumbers.insert(std::make_pair(Tag, P.first->second++));
800966cff7257698d5528632917d38f9a3037bb1506dDavid Blaikie}
801066cff7257698d5528632917d38f9a3037bb1506dDavid Blaikie
801166cff7257698d5528632917d38f9a3037bb1506dDavid Blaikieint ASTContext::getUnnamedTagManglingNumber(const TagDecl *Tag) const {
801266cff7257698d5528632917d38f9a3037bb1506dDavid Blaikie  llvm::DenseMap<const TagDecl *, unsigned>::const_iterator I =
801366cff7257698d5528632917d38f9a3037bb1506dDavid Blaikie    UnnamedMangleNumbers.find(Tag);
801466cff7257698d5528632917d38f9a3037bb1506dDavid Blaikie  return I != UnnamedMangleNumbers.end() ? I->second : -1;
801566cff7257698d5528632917d38f9a3037bb1506dDavid Blaikie}
801666cff7257698d5528632917d38f9a3037bb1506dDavid Blaikie
80179e8c92a9c9b949bbb0408fbbd9a58e34894b6efcDouglas Gregorunsigned ASTContext::getLambdaManglingNumber(CXXMethodDecl *CallOperator) {
80189e8c92a9c9b949bbb0408fbbd9a58e34894b6efcDouglas Gregor  CXXRecordDecl *Lambda = CallOperator->getParent();
80199e8c92a9c9b949bbb0408fbbd9a58e34894b6efcDouglas Gregor  return LambdaMangleContexts[Lambda->getDeclContext()]
80209e8c92a9c9b949bbb0408fbbd9a58e34894b6efcDouglas Gregor           .getManglingNumber(CallOperator);
80219e8c92a9c9b949bbb0408fbbd9a58e34894b6efcDouglas Gregor}
80229e8c92a9c9b949bbb0408fbbd9a58e34894b6efcDouglas Gregor
80239e8c92a9c9b949bbb0408fbbd9a58e34894b6efcDouglas Gregor
8024d211cb709510fbe7e75167b9feee0050851d001aTed Kremenekvoid ASTContext::setParameterIndex(const ParmVarDecl *D, unsigned int index) {
8025d211cb709510fbe7e75167b9feee0050851d001aTed Kremenek  ParamIndices[D] = index;
8026d211cb709510fbe7e75167b9feee0050851d001aTed Kremenek}
8027d211cb709510fbe7e75167b9feee0050851d001aTed Kremenek
8028d211cb709510fbe7e75167b9feee0050851d001aTed Kremenekunsigned ASTContext::getParameterIndex(const ParmVarDecl *D) const {
8029d211cb709510fbe7e75167b9feee0050851d001aTed Kremenek  ParameterIndexTable::const_iterator I = ParamIndices.find(D);
8030d211cb709510fbe7e75167b9feee0050851d001aTed Kremenek  assert(I != ParamIndices.end() &&
8031d211cb709510fbe7e75167b9feee0050851d001aTed Kremenek         "ParmIndices lacks entry set by ParmVarDecl");
8032d211cb709510fbe7e75167b9feee0050851d001aTed Kremenek  return I->second;
8033d211cb709510fbe7e75167b9feee0050851d001aTed Kremenek}
8034