ASTContext.cpp revision 91efca0fa2ef5e63b48692e3439f5c6e6bde350c
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
4091952354bd376062c3ab3d328c0fc6c36530c9309Dmitri Gribenkocomments::FullComment *ASTContext::getCommentForDecl(
4101952354bd376062c3ab3d328c0fc6c36530c9309Dmitri Gribenko                                              const Decl *D,
4111952354bd376062c3ab3d328c0fc6c36530c9309Dmitri Gribenko                                              const Preprocessor *PP) const {
412c41ace950dcf2254c9aa48e73647b89c35109f80Dmitri Gribenko  D = adjustDeclToTemplate(D);
413bf967be66ea8c51b66c61659c23240f762a56dbeFariborz Jahanian
414c41ace950dcf2254c9aa48e73647b89c35109f80Dmitri Gribenko  const Decl *Canonical = D->getCanonicalDecl();
415c41ace950dcf2254c9aa48e73647b89c35109f80Dmitri Gribenko  llvm::DenseMap<const Decl *, comments::FullComment *>::iterator Pos =
416c41ace950dcf2254c9aa48e73647b89c35109f80Dmitri Gribenko      ParsedComments.find(Canonical);
417bf967be66ea8c51b66c61659c23240f762a56dbeFariborz Jahanian
418bf967be66ea8c51b66c61659c23240f762a56dbeFariborz Jahanian  if (Pos != ParsedComments.end()) {
419749ace614b6ea1ae11d194a60b18e1e43e1db243Fariborz Jahanian    if (Canonical != D) {
420bf967be66ea8c51b66c61659c23240f762a56dbeFariborz Jahanian      comments::FullComment *FC = Pos->second;
421749ace614b6ea1ae11d194a60b18e1e43e1db243Fariborz Jahanian      comments::FullComment *CFC = cloneFullComment(FC, D);
422bf967be66ea8c51b66c61659c23240f762a56dbeFariborz Jahanian      return CFC;
423bf967be66ea8c51b66c61659c23240f762a56dbeFariborz Jahanian    }
424c41ace950dcf2254c9aa48e73647b89c35109f80Dmitri Gribenko    return Pos->second;
425bf967be66ea8c51b66c61659c23240f762a56dbeFariborz Jahanian  }
426bf967be66ea8c51b66c61659c23240f762a56dbeFariborz Jahanian
4271599eac40a3b28de0824013dc2fb90551dfa01b0Dmitri Gribenko  const Decl *OriginalDecl;
428bf967be66ea8c51b66c61659c23240f762a56dbeFariborz Jahanian
4291599eac40a3b28de0824013dc2fb90551dfa01b0Dmitri Gribenko  const RawComment *RC = getRawCommentForAnyRedecl(D, &OriginalDecl);
430bf967be66ea8c51b66c61659c23240f762a56dbeFariborz Jahanian  if (!RC) {
431bf967be66ea8c51b66c61659c23240f762a56dbeFariborz Jahanian    if (isa<ObjCMethodDecl>(D) || isa<FunctionDecl>(D)) {
4321e905da74dbfd353d77dfc548fc9d6ff420d515aDmitri Gribenko      SmallVector<const NamedDecl*, 8> Overridden;
433c328d9c22a4397dd7313d06be5b82d700297b246Fariborz Jahanian      const ObjCMethodDecl *OMD = dyn_cast<ObjCMethodDecl>(D);
43423799e3ec4b341753e4fb63a7e995cf4ac3b6066Fariborz Jahanian      if (OMD && OMD->isPropertyAccessor())
43523799e3ec4b341753e4fb63a7e995cf4ac3b6066Fariborz Jahanian        if (const ObjCPropertyDecl *PDecl = OMD->findPropertyDecl())
43623799e3ec4b341753e4fb63a7e995cf4ac3b6066Fariborz Jahanian          if (comments::FullComment *FC = getCommentForDecl(PDecl, PP))
43723799e3ec4b341753e4fb63a7e995cf4ac3b6066Fariborz Jahanian            return cloneFullComment(FC, D);
438c328d9c22a4397dd7313d06be5b82d700297b246Fariborz Jahanian      if (OMD)
4391e905da74dbfd353d77dfc548fc9d6ff420d515aDmitri Gribenko        addRedeclaredMethods(OMD, Overridden);
4401e905da74dbfd353d77dfc548fc9d6ff420d515aDmitri Gribenko      getOverriddenMethods(dyn_cast<NamedDecl>(D), Overridden);
44123799e3ec4b341753e4fb63a7e995cf4ac3b6066Fariborz Jahanian      for (unsigned i = 0, e = Overridden.size(); i < e; i++)
44223799e3ec4b341753e4fb63a7e995cf4ac3b6066Fariborz Jahanian        if (comments::FullComment *FC = getCommentForDecl(Overridden[i], PP))
44323799e3ec4b341753e4fb63a7e995cf4ac3b6066Fariborz Jahanian          return cloneFullComment(FC, D);
444bf967be66ea8c51b66c61659c23240f762a56dbeFariborz Jahanian    }
44541170b55ba635afb806394d44f2b7f1f6095df37Fariborz Jahanian    else if (const TypedefDecl *TD = dyn_cast<TypedefDecl>(D)) {
446d1e5c0df2acb01192eedda14534fe990e1e26c46Dmitri Gribenko      // Attach any tag type's documentation to its typedef if latter
44723799e3ec4b341753e4fb63a7e995cf4ac3b6066Fariborz Jahanian      // does not have one of its own.
44841170b55ba635afb806394d44f2b7f1f6095df37Fariborz Jahanian      QualType QT = TD->getUnderlyingType();
449d1e5c0df2acb01192eedda14534fe990e1e26c46Dmitri Gribenko      if (const TagType *TT = QT->getAs<TagType>())
450d1e5c0df2acb01192eedda14534fe990e1e26c46Dmitri Gribenko        if (const Decl *TD = TT->getDecl())
451d1e5c0df2acb01192eedda14534fe990e1e26c46Dmitri Gribenko          if (comments::FullComment *FC = getCommentForDecl(TD, PP))
45223799e3ec4b341753e4fb63a7e995cf4ac3b6066Fariborz Jahanian            return cloneFullComment(FC, D);
45341170b55ba635afb806394d44f2b7f1f6095df37Fariborz Jahanian    }
454622bb4acc00fd63bac545ca23f7c42fd909dc0e7Fariborz Jahanian    else if (const ObjCInterfaceDecl *IC = dyn_cast<ObjCInterfaceDecl>(D)) {
455622bb4acc00fd63bac545ca23f7c42fd909dc0e7Fariborz Jahanian      while (IC->getSuperClass()) {
456622bb4acc00fd63bac545ca23f7c42fd909dc0e7Fariborz Jahanian        IC = IC->getSuperClass();
457622bb4acc00fd63bac545ca23f7c42fd909dc0e7Fariborz Jahanian        if (comments::FullComment *FC = getCommentForDecl(IC, PP))
458622bb4acc00fd63bac545ca23f7c42fd909dc0e7Fariborz Jahanian          return cloneFullComment(FC, D);
459622bb4acc00fd63bac545ca23f7c42fd909dc0e7Fariborz Jahanian      }
460622bb4acc00fd63bac545ca23f7c42fd909dc0e7Fariborz Jahanian    }
461622bb4acc00fd63bac545ca23f7c42fd909dc0e7Fariborz Jahanian    else if (const ObjCCategoryDecl *CD = dyn_cast<ObjCCategoryDecl>(D)) {
462622bb4acc00fd63bac545ca23f7c42fd909dc0e7Fariborz Jahanian      if (const ObjCInterfaceDecl *IC = CD->getClassInterface())
463622bb4acc00fd63bac545ca23f7c42fd909dc0e7Fariborz Jahanian        if (comments::FullComment *FC = getCommentForDecl(IC, PP))
464622bb4acc00fd63bac545ca23f7c42fd909dc0e7Fariborz Jahanian          return cloneFullComment(FC, D);
465622bb4acc00fd63bac545ca23f7c42fd909dc0e7Fariborz Jahanian    }
466622bb4acc00fd63bac545ca23f7c42fd909dc0e7Fariborz Jahanian    else if (const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(D)) {
467622bb4acc00fd63bac545ca23f7c42fd909dc0e7Fariborz Jahanian      if (!(RD = RD->getDefinition()))
468622bb4acc00fd63bac545ca23f7c42fd909dc0e7Fariborz Jahanian        return NULL;
469622bb4acc00fd63bac545ca23f7c42fd909dc0e7Fariborz Jahanian      // Check non-virtual bases.
470622bb4acc00fd63bac545ca23f7c42fd909dc0e7Fariborz Jahanian      for (CXXRecordDecl::base_class_const_iterator I =
471622bb4acc00fd63bac545ca23f7c42fd909dc0e7Fariborz Jahanian           RD->bases_begin(), E = RD->bases_end(); I != E; ++I) {
47291efca0fa2ef5e63b48692e3439f5c6e6bde350cFariborz Jahanian        if (I->isVirtual() || (I->getAccessSpecifier() != AS_public))
473622bb4acc00fd63bac545ca23f7c42fd909dc0e7Fariborz Jahanian          continue;
474622bb4acc00fd63bac545ca23f7c42fd909dc0e7Fariborz Jahanian        QualType Ty = I->getType();
475622bb4acc00fd63bac545ca23f7c42fd909dc0e7Fariborz Jahanian        if (Ty.isNull())
476622bb4acc00fd63bac545ca23f7c42fd909dc0e7Fariborz Jahanian          continue;
477622bb4acc00fd63bac545ca23f7c42fd909dc0e7Fariborz Jahanian        if (const CXXRecordDecl *NonVirtualBase = Ty->getAsCXXRecordDecl()) {
478622bb4acc00fd63bac545ca23f7c42fd909dc0e7Fariborz Jahanian          if (!(NonVirtualBase= NonVirtualBase->getDefinition()))
479622bb4acc00fd63bac545ca23f7c42fd909dc0e7Fariborz Jahanian            continue;
480622bb4acc00fd63bac545ca23f7c42fd909dc0e7Fariborz Jahanian
481622bb4acc00fd63bac545ca23f7c42fd909dc0e7Fariborz Jahanian          if (comments::FullComment *FC = getCommentForDecl((NonVirtualBase), PP))
482622bb4acc00fd63bac545ca23f7c42fd909dc0e7Fariborz Jahanian            return cloneFullComment(FC, D);
483622bb4acc00fd63bac545ca23f7c42fd909dc0e7Fariborz Jahanian        }
484622bb4acc00fd63bac545ca23f7c42fd909dc0e7Fariborz Jahanian      }
485622bb4acc00fd63bac545ca23f7c42fd909dc0e7Fariborz Jahanian      // Check virtual bases.
486622bb4acc00fd63bac545ca23f7c42fd909dc0e7Fariborz Jahanian      for (CXXRecordDecl::base_class_const_iterator I =
487622bb4acc00fd63bac545ca23f7c42fd909dc0e7Fariborz Jahanian           RD->vbases_begin(), E = RD->vbases_end(); I != E; ++I) {
48891efca0fa2ef5e63b48692e3439f5c6e6bde350cFariborz Jahanian        if (I->getAccessSpecifier() != AS_public)
48991efca0fa2ef5e63b48692e3439f5c6e6bde350cFariborz Jahanian          continue;
490622bb4acc00fd63bac545ca23f7c42fd909dc0e7Fariborz Jahanian        QualType Ty = I->getType();
491622bb4acc00fd63bac545ca23f7c42fd909dc0e7Fariborz Jahanian        if (Ty.isNull())
492622bb4acc00fd63bac545ca23f7c42fd909dc0e7Fariborz Jahanian          continue;
493622bb4acc00fd63bac545ca23f7c42fd909dc0e7Fariborz Jahanian        if (const CXXRecordDecl *VirtualBase = Ty->getAsCXXRecordDecl()) {
494622bb4acc00fd63bac545ca23f7c42fd909dc0e7Fariborz Jahanian          if (!(VirtualBase= VirtualBase->getDefinition()))
495622bb4acc00fd63bac545ca23f7c42fd909dc0e7Fariborz Jahanian            continue;
496622bb4acc00fd63bac545ca23f7c42fd909dc0e7Fariborz Jahanian          if (comments::FullComment *FC = getCommentForDecl((VirtualBase), PP))
497622bb4acc00fd63bac545ca23f7c42fd909dc0e7Fariborz Jahanian            return cloneFullComment(FC, D);
498622bb4acc00fd63bac545ca23f7c42fd909dc0e7Fariborz Jahanian        }
499622bb4acc00fd63bac545ca23f7c42fd909dc0e7Fariborz Jahanian      }
500622bb4acc00fd63bac545ca23f7c42fd909dc0e7Fariborz Jahanian    }
5018d3ba23f2d9e6c87794d059412a0808c9cbacb25Dmitri Gribenko    return NULL;
502bf967be66ea8c51b66c61659c23240f762a56dbeFariborz Jahanian  }
503bf967be66ea8c51b66c61659c23240f762a56dbeFariborz Jahanian
5044b41c65feb93eeb6f6d27b49a2045ea1b72de9d1Dmitri Gribenko  // If the RawComment was attached to other redeclaration of this Decl, we
5054b41c65feb93eeb6f6d27b49a2045ea1b72de9d1Dmitri Gribenko  // should parse the comment in context of that other Decl.  This is important
5064b41c65feb93eeb6f6d27b49a2045ea1b72de9d1Dmitri Gribenko  // because comments can contain references to parameter names which can be
5074b41c65feb93eeb6f6d27b49a2045ea1b72de9d1Dmitri Gribenko  // different across redeclarations.
5081599eac40a3b28de0824013dc2fb90551dfa01b0Dmitri Gribenko  if (D != OriginalDecl)
5091952354bd376062c3ab3d328c0fc6c36530c9309Dmitri Gribenko    return getCommentForDecl(OriginalDecl, PP);
5101599eac40a3b28de0824013dc2fb90551dfa01b0Dmitri Gribenko
5111952354bd376062c3ab3d328c0fc6c36530c9309Dmitri Gribenko  comments::FullComment *FC = RC->parse(*this, PP, D);
512c41ace950dcf2254c9aa48e73647b89c35109f80Dmitri Gribenko  ParsedComments[Canonical] = FC;
513c41ace950dcf2254c9aa48e73647b89c35109f80Dmitri Gribenko  return FC;
5148d3ba23f2d9e6c87794d059412a0808c9cbacb25Dmitri Gribenko}
5158d3ba23f2d9e6c87794d059412a0808c9cbacb25Dmitri Gribenko
5163e1274f2b99cb99c03cc8e2c6517c37d330b597aDouglas Gregorvoid
5173e1274f2b99cb99c03cc8e2c6517c37d330b597aDouglas GregorASTContext::CanonicalTemplateTemplateParm::Profile(llvm::FoldingSetNodeID &ID,
5183e1274f2b99cb99c03cc8e2c6517c37d330b597aDouglas Gregor                                               TemplateTemplateParmDecl *Parm) {
5193e1274f2b99cb99c03cc8e2c6517c37d330b597aDouglas Gregor  ID.AddInteger(Parm->getDepth());
5203e1274f2b99cb99c03cc8e2c6517c37d330b597aDouglas Gregor  ID.AddInteger(Parm->getPosition());
52161c4d28e36cd3f1be392cb77f07436d1fa6b0f9fDouglas Gregor  ID.AddBoolean(Parm->isParameterPack());
5223e1274f2b99cb99c03cc8e2c6517c37d330b597aDouglas Gregor
5233e1274f2b99cb99c03cc8e2c6517c37d330b597aDouglas Gregor  TemplateParameterList *Params = Parm->getTemplateParameters();
5243e1274f2b99cb99c03cc8e2c6517c37d330b597aDouglas Gregor  ID.AddInteger(Params->size());
5253e1274f2b99cb99c03cc8e2c6517c37d330b597aDouglas Gregor  for (TemplateParameterList::const_iterator P = Params->begin(),
5263e1274f2b99cb99c03cc8e2c6517c37d330b597aDouglas Gregor                                          PEnd = Params->end();
5273e1274f2b99cb99c03cc8e2c6517c37d330b597aDouglas Gregor       P != PEnd; ++P) {
5283e1274f2b99cb99c03cc8e2c6517c37d330b597aDouglas Gregor    if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(*P)) {
5293e1274f2b99cb99c03cc8e2c6517c37d330b597aDouglas Gregor      ID.AddInteger(0);
5303e1274f2b99cb99c03cc8e2c6517c37d330b597aDouglas Gregor      ID.AddBoolean(TTP->isParameterPack());
5313e1274f2b99cb99c03cc8e2c6517c37d330b597aDouglas Gregor      continue;
5323e1274f2b99cb99c03cc8e2c6517c37d330b597aDouglas Gregor    }
5333e1274f2b99cb99c03cc8e2c6517c37d330b597aDouglas Gregor
5343e1274f2b99cb99c03cc8e2c6517c37d330b597aDouglas Gregor    if (NonTypeTemplateParmDecl *NTTP = dyn_cast<NonTypeTemplateParmDecl>(*P)) {
5353e1274f2b99cb99c03cc8e2c6517c37d330b597aDouglas Gregor      ID.AddInteger(1);
53661c4d28e36cd3f1be392cb77f07436d1fa6b0f9fDouglas Gregor      ID.AddBoolean(NTTP->isParameterPack());
5379e9c454b12671a624f666fc6fbf132fdf183effcEli Friedman      ID.AddPointer(NTTP->getType().getCanonicalType().getAsOpaquePtr());
5386952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor      if (NTTP->isExpandedParameterPack()) {
5396952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor        ID.AddBoolean(true);
5406952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor        ID.AddInteger(NTTP->getNumExpansionTypes());
5419e9c454b12671a624f666fc6fbf132fdf183effcEli Friedman        for (unsigned I = 0, N = NTTP->getNumExpansionTypes(); I != N; ++I) {
5429e9c454b12671a624f666fc6fbf132fdf183effcEli Friedman          QualType T = NTTP->getExpansionType(I);
5439e9c454b12671a624f666fc6fbf132fdf183effcEli Friedman          ID.AddPointer(T.getCanonicalType().getAsOpaquePtr());
5449e9c454b12671a624f666fc6fbf132fdf183effcEli Friedman        }
5456952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor      } else
5466952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor        ID.AddBoolean(false);
5473e1274f2b99cb99c03cc8e2c6517c37d330b597aDouglas Gregor      continue;
5483e1274f2b99cb99c03cc8e2c6517c37d330b597aDouglas Gregor    }
5493e1274f2b99cb99c03cc8e2c6517c37d330b597aDouglas Gregor
5503e1274f2b99cb99c03cc8e2c6517c37d330b597aDouglas Gregor    TemplateTemplateParmDecl *TTP = cast<TemplateTemplateParmDecl>(*P);
5513e1274f2b99cb99c03cc8e2c6517c37d330b597aDouglas Gregor    ID.AddInteger(2);
5523e1274f2b99cb99c03cc8e2c6517c37d330b597aDouglas Gregor    Profile(ID, TTP);
5533e1274f2b99cb99c03cc8e2c6517c37d330b597aDouglas Gregor  }
5543e1274f2b99cb99c03cc8e2c6517c37d330b597aDouglas Gregor}
5553e1274f2b99cb99c03cc8e2c6517c37d330b597aDouglas Gregor
5563e1274f2b99cb99c03cc8e2c6517c37d330b597aDouglas GregorTemplateTemplateParmDecl *
5573e1274f2b99cb99c03cc8e2c6517c37d330b597aDouglas GregorASTContext::getCanonicalTemplateTemplateParmDecl(
5584ba2a17694148e16eaa8d3917f657ffcd3667be4Jay Foad                                          TemplateTemplateParmDecl *TTP) const {
5593e1274f2b99cb99c03cc8e2c6517c37d330b597aDouglas Gregor  // Check if we already have a canonical template template parameter.
5603e1274f2b99cb99c03cc8e2c6517c37d330b597aDouglas Gregor  llvm::FoldingSetNodeID ID;
5613e1274f2b99cb99c03cc8e2c6517c37d330b597aDouglas Gregor  CanonicalTemplateTemplateParm::Profile(ID, TTP);
5623e1274f2b99cb99c03cc8e2c6517c37d330b597aDouglas Gregor  void *InsertPos = 0;
5633e1274f2b99cb99c03cc8e2c6517c37d330b597aDouglas Gregor  CanonicalTemplateTemplateParm *Canonical
5643e1274f2b99cb99c03cc8e2c6517c37d330b597aDouglas Gregor    = CanonTemplateTemplateParms.FindNodeOrInsertPos(ID, InsertPos);
5653e1274f2b99cb99c03cc8e2c6517c37d330b597aDouglas Gregor  if (Canonical)
5663e1274f2b99cb99c03cc8e2c6517c37d330b597aDouglas Gregor    return Canonical->getParam();
5673e1274f2b99cb99c03cc8e2c6517c37d330b597aDouglas Gregor
5683e1274f2b99cb99c03cc8e2c6517c37d330b597aDouglas Gregor  // Build a canonical template parameter list.
5693e1274f2b99cb99c03cc8e2c6517c37d330b597aDouglas Gregor  TemplateParameterList *Params = TTP->getTemplateParameters();
5705f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner  SmallVector<NamedDecl *, 4> CanonParams;
5713e1274f2b99cb99c03cc8e2c6517c37d330b597aDouglas Gregor  CanonParams.reserve(Params->size());
5723e1274f2b99cb99c03cc8e2c6517c37d330b597aDouglas Gregor  for (TemplateParameterList::const_iterator P = Params->begin(),
5733e1274f2b99cb99c03cc8e2c6517c37d330b597aDouglas Gregor                                          PEnd = Params->end();
5743e1274f2b99cb99c03cc8e2c6517c37d330b597aDouglas Gregor       P != PEnd; ++P) {
5753e1274f2b99cb99c03cc8e2c6517c37d330b597aDouglas Gregor    if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(*P))
5763e1274f2b99cb99c03cc8e2c6517c37d330b597aDouglas Gregor      CanonParams.push_back(
5773e1274f2b99cb99c03cc8e2c6517c37d330b597aDouglas Gregor                  TemplateTypeParmDecl::Create(*this, getTranslationUnitDecl(),
578344577e6b58f42d18dc8118c8903b49a85dc005eAbramo Bagnara                                               SourceLocation(),
579344577e6b58f42d18dc8118c8903b49a85dc005eAbramo Bagnara                                               SourceLocation(),
580344577e6b58f42d18dc8118c8903b49a85dc005eAbramo Bagnara                                               TTP->getDepth(),
5813e1274f2b99cb99c03cc8e2c6517c37d330b597aDouglas Gregor                                               TTP->getIndex(), 0, false,
5823e1274f2b99cb99c03cc8e2c6517c37d330b597aDouglas Gregor                                               TTP->isParameterPack()));
5833e1274f2b99cb99c03cc8e2c6517c37d330b597aDouglas Gregor    else if (NonTypeTemplateParmDecl *NTTP
5846952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor             = dyn_cast<NonTypeTemplateParmDecl>(*P)) {
5856952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor      QualType T = getCanonicalType(NTTP->getType());
5866952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor      TypeSourceInfo *TInfo = getTrivialTypeSourceInfo(T);
5876952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor      NonTypeTemplateParmDecl *Param;
5886952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor      if (NTTP->isExpandedParameterPack()) {
5895f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner        SmallVector<QualType, 2> ExpandedTypes;
5905f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner        SmallVector<TypeSourceInfo *, 2> ExpandedTInfos;
5916952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor        for (unsigned I = 0, N = NTTP->getNumExpansionTypes(); I != N; ++I) {
5926952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor          ExpandedTypes.push_back(getCanonicalType(NTTP->getExpansionType(I)));
5936952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor          ExpandedTInfos.push_back(
5946952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor                                getTrivialTypeSourceInfo(ExpandedTypes.back()));
5956952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor        }
5966952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor
5976952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor        Param = NonTypeTemplateParmDecl::Create(*this, getTranslationUnitDecl(),
598ff676cb48fe8bf7be2feaa251dc7c5fb15af4730Abramo Bagnara                                                SourceLocation(),
599ff676cb48fe8bf7be2feaa251dc7c5fb15af4730Abramo Bagnara                                                SourceLocation(),
6006952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor                                                NTTP->getDepth(),
6016952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor                                                NTTP->getPosition(), 0,
6026952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor                                                T,
6036952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor                                                TInfo,
6046952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor                                                ExpandedTypes.data(),
6056952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor                                                ExpandedTypes.size(),
6066952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor                                                ExpandedTInfos.data());
6076952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor      } else {
6086952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor        Param = NonTypeTemplateParmDecl::Create(*this, getTranslationUnitDecl(),
609ff676cb48fe8bf7be2feaa251dc7c5fb15af4730Abramo Bagnara                                                SourceLocation(),
610ff676cb48fe8bf7be2feaa251dc7c5fb15af4730Abramo Bagnara                                                SourceLocation(),
6116952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor                                                NTTP->getDepth(),
6126952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor                                                NTTP->getPosition(), 0,
6136952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor                                                T,
6146952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor                                                NTTP->isParameterPack(),
6156952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor                                                TInfo);
6166952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor      }
6176952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor      CanonParams.push_back(Param);
6186952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor
6196952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor    } else
6203e1274f2b99cb99c03cc8e2c6517c37d330b597aDouglas Gregor      CanonParams.push_back(getCanonicalTemplateTemplateParmDecl(
6213e1274f2b99cb99c03cc8e2c6517c37d330b597aDouglas Gregor                                           cast<TemplateTemplateParmDecl>(*P)));
6223e1274f2b99cb99c03cc8e2c6517c37d330b597aDouglas Gregor  }
6233e1274f2b99cb99c03cc8e2c6517c37d330b597aDouglas Gregor
6243e1274f2b99cb99c03cc8e2c6517c37d330b597aDouglas Gregor  TemplateTemplateParmDecl *CanonTTP
6253e1274f2b99cb99c03cc8e2c6517c37d330b597aDouglas Gregor    = TemplateTemplateParmDecl::Create(*this, getTranslationUnitDecl(),
6263e1274f2b99cb99c03cc8e2c6517c37d330b597aDouglas Gregor                                       SourceLocation(), TTP->getDepth(),
62761c4d28e36cd3f1be392cb77f07436d1fa6b0f9fDouglas Gregor                                       TTP->getPosition(),
62861c4d28e36cd3f1be392cb77f07436d1fa6b0f9fDouglas Gregor                                       TTP->isParameterPack(),
62961c4d28e36cd3f1be392cb77f07436d1fa6b0f9fDouglas Gregor                                       0,
6303e1274f2b99cb99c03cc8e2c6517c37d330b597aDouglas Gregor                         TemplateParameterList::Create(*this, SourceLocation(),
6313e1274f2b99cb99c03cc8e2c6517c37d330b597aDouglas Gregor                                                       SourceLocation(),
6323e1274f2b99cb99c03cc8e2c6517c37d330b597aDouglas Gregor                                                       CanonParams.data(),
6333e1274f2b99cb99c03cc8e2c6517c37d330b597aDouglas Gregor                                                       CanonParams.size(),
6343e1274f2b99cb99c03cc8e2c6517c37d330b597aDouglas Gregor                                                       SourceLocation()));
6353e1274f2b99cb99c03cc8e2c6517c37d330b597aDouglas Gregor
6363e1274f2b99cb99c03cc8e2c6517c37d330b597aDouglas Gregor  // Get the new insert position for the node we care about.
6373e1274f2b99cb99c03cc8e2c6517c37d330b597aDouglas Gregor  Canonical = CanonTemplateTemplateParms.FindNodeOrInsertPos(ID, InsertPos);
6383e1274f2b99cb99c03cc8e2c6517c37d330b597aDouglas Gregor  assert(Canonical == 0 && "Shouldn't be in the map!");
6393e1274f2b99cb99c03cc8e2c6517c37d330b597aDouglas Gregor  (void)Canonical;
6403e1274f2b99cb99c03cc8e2c6517c37d330b597aDouglas Gregor
6413e1274f2b99cb99c03cc8e2c6517c37d330b597aDouglas Gregor  // Create the canonical template template parameter entry.
6423e1274f2b99cb99c03cc8e2c6517c37d330b597aDouglas Gregor  Canonical = new (*this) CanonicalTemplateTemplateParm(CanonTTP);
6433e1274f2b99cb99c03cc8e2c6517c37d330b597aDouglas Gregor  CanonTemplateTemplateParms.InsertNode(Canonical, InsertPos);
6443e1274f2b99cb99c03cc8e2c6517c37d330b597aDouglas Gregor  return CanonTTP;
6453e1274f2b99cb99c03cc8e2c6517c37d330b597aDouglas Gregor}
6463e1274f2b99cb99c03cc8e2c6517c37d330b597aDouglas Gregor
647071cc7deffad608165b1ddd5263e8bf181861520Charles DavisCXXABI *ASTContext::createCXXABI(const TargetInfo &T) {
648ee79a4c30e5d1c5285551c9a25b8ec6d45d46aa7John McCall  if (!LangOpts.CPlusPlus) return 0;
649ee79a4c30e5d1c5285551c9a25b8ec6d45d46aa7John McCall
650b8b2c9da87e7d70a1679db026f40548b3192b705John McCall  switch (T.getCXXABI().getKind()) {
651b8b2c9da87e7d70a1679db026f40548b3192b705John McCall  case TargetCXXABI::GenericARM:
652b8b2c9da87e7d70a1679db026f40548b3192b705John McCall  case TargetCXXABI::iOS:
653ee79a4c30e5d1c5285551c9a25b8ec6d45d46aa7John McCall    return CreateARMCXXABI(*this);
654c264e16a42b3f6c36521857a29ea0949d9781c22Tim Northover  case TargetCXXABI::GenericAArch64: // Same as Itanium at this level
655b8b2c9da87e7d70a1679db026f40548b3192b705John McCall  case TargetCXXABI::GenericItanium:
656071cc7deffad608165b1ddd5263e8bf181861520Charles Davis    return CreateItaniumCXXABI(*this);
657b8b2c9da87e7d70a1679db026f40548b3192b705John McCall  case TargetCXXABI::Microsoft:
65820cf717034ba1f20fc47c025ecb72ed9b631ad13Charles Davis    return CreateMicrosoftCXXABI(*this);
65920cf717034ba1f20fc47c025ecb72ed9b631ad13Charles Davis  }
6607530c034c0c71a64c5a9173206d9742ae847af8bDavid Blaikie  llvm_unreachable("Invalid CXXABI type!");
661071cc7deffad608165b1ddd5263e8bf181861520Charles Davis}
662071cc7deffad608165b1ddd5263e8bf181861520Charles Davis
663bcfd1f55bfbb3e5944cd5e03d07b343e280838c4Douglas Gregorstatic const LangAS::Map *getAddressSpaceMap(const TargetInfo &T,
664207f4d8543529221932af82836016a2ef066c917Peter Collingbourne                                             const LangOptions &LOpts) {
665207f4d8543529221932af82836016a2ef066c917Peter Collingbourne  if (LOpts.FakeAddressSpaceMap) {
666207f4d8543529221932af82836016a2ef066c917Peter Collingbourne    // The fake address space map must have a distinct entry for each
667207f4d8543529221932af82836016a2ef066c917Peter Collingbourne    // language-specific address space.
668207f4d8543529221932af82836016a2ef066c917Peter Collingbourne    static const unsigned FakeAddrSpaceMap[] = {
669207f4d8543529221932af82836016a2ef066c917Peter Collingbourne      1, // opencl_global
670207f4d8543529221932af82836016a2ef066c917Peter Collingbourne      2, // opencl_local
6714dc34ebf2a0716bf77ba110dab6777a3fc4397ddPeter Collingbourne      3, // opencl_constant
6724dc34ebf2a0716bf77ba110dab6777a3fc4397ddPeter Collingbourne      4, // cuda_device
6734dc34ebf2a0716bf77ba110dab6777a3fc4397ddPeter Collingbourne      5, // cuda_constant
6744dc34ebf2a0716bf77ba110dab6777a3fc4397ddPeter Collingbourne      6  // cuda_shared
675207f4d8543529221932af82836016a2ef066c917Peter Collingbourne    };
676bcfd1f55bfbb3e5944cd5e03d07b343e280838c4Douglas Gregor    return &FakeAddrSpaceMap;
677207f4d8543529221932af82836016a2ef066c917Peter Collingbourne  } else {
678bcfd1f55bfbb3e5944cd5e03d07b343e280838c4Douglas Gregor    return &T.getAddressSpaceMap();
679207f4d8543529221932af82836016a2ef066c917Peter Collingbourne  }
680207f4d8543529221932af82836016a2ef066c917Peter Collingbourne}
681207f4d8543529221932af82836016a2ef066c917Peter Collingbourne
6823e3cd93b2fd9644e970c389e715c13883faf68b6Douglas GregorASTContext::ASTContext(LangOptions& LOpts, SourceManager &SM,
683bcfd1f55bfbb3e5944cd5e03d07b343e280838c4Douglas Gregor                       const TargetInfo *t,
684e91593ef084479340582b2ba177b44be50a717b7Daniel Dunbar                       IdentifierTable &idents, SelectorTable &sels,
6851b63e4f732dbc73d90abf886b4d21f8e3a165f6dChris Lattner                       Builtin::Context &builtins,
686bcfd1f55bfbb3e5944cd5e03d07b343e280838c4Douglas Gregor                       unsigned size_reserve,
687bcfd1f55bfbb3e5944cd5e03d07b343e280838c4Douglas Gregor                       bool DelayInitialization)
688bcfd1f55bfbb3e5944cd5e03d07b343e280838c4Douglas Gregor  : FunctionProtoTypes(this_()),
689bcfd1f55bfbb3e5944cd5e03d07b343e280838c4Douglas Gregor    TemplateSpecializationTypes(this_()),
690bcfd1f55bfbb3e5944cd5e03d07b343e280838c4Douglas Gregor    DependentTemplateSpecializationTypes(this_()),
691bcfd1f55bfbb3e5944cd5e03d07b343e280838c4Douglas Gregor    SubstTemplateTemplateParmPacks(this_()),
692bcfd1f55bfbb3e5944cd5e03d07b343e280838c4Douglas Gregor    GlobalNestedNameSpecifier(0),
693bcfd1f55bfbb3e5944cd5e03d07b343e280838c4Douglas Gregor    Int128Decl(0), UInt128Decl(0),
694c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge    BuiltinVaListDecl(0),
695a6ea10e22b600d92e084f6b11b9b9a92d0eb2412Douglas Gregor    ObjCIdDecl(0), ObjCSelDecl(0), ObjCClassDecl(0), ObjCProtocolClassDecl(0),
696961713055e636170da59d7006a878cb4ba518a5dFariborz Jahanian    BOOLDecl(0),
697e97179c675b341927807c718be215c8d1aab8acbDouglas Gregor    CFConstantStringTypeDecl(0), ObjCInstanceTypeDecl(0),
698bcfd1f55bfbb3e5944cd5e03d07b343e280838c4Douglas Gregor    FILEDecl(0),
699e2d4f4ed44a32f179c6d48cd1dba8346ab2129d9Rafael Espindola    jmp_bufDecl(0), sigjmp_bufDecl(0), ucontext_tDecl(0),
700e2d4f4ed44a32f179c6d48cd1dba8346ab2129d9Rafael Espindola    BlockDescriptorType(0), BlockDescriptorExtendedType(0),
701e2d4f4ed44a32f179c6d48cd1dba8346ab2129d9Rafael Espindola    cudaConfigureCallDecl(0),
702e664977aca2a05a77abab5a06dc0fb69e870cfb9Douglas Gregor    NullTypeSourceInfo(QualType()),
703e664977aca2a05a77abab5a06dc0fb69e870cfb9Douglas Gregor    FirstLocalImport(), LastLocalImport(),
704bcfd1f55bfbb3e5944cd5e03d07b343e280838c4Douglas Gregor    SourceMgr(SM), LangOpts(LOpts),
70530c42404202d2e2512e51efc6066bd614cfdb5a4Douglas Gregor    AddrSpaceMap(0), Target(t), PrintingPolicy(LOpts),
706bcfd1f55bfbb3e5944cd5e03d07b343e280838c4Douglas Gregor    Idents(idents), Selectors(sels),
707bcfd1f55bfbb3e5944cd5e03d07b343e280838c4Douglas Gregor    BuiltinInfo(builtins),
708bcfd1f55bfbb3e5944cd5e03d07b343e280838c4Douglas Gregor    DeclarationNames(*this),
70930c42404202d2e2512e51efc6066bd614cfdb5a4Douglas Gregor    ExternalSource(0), Listener(0),
710aa0cd85838f2a024e589ea4e8c2094130065af21Dmitri Gribenko    Comments(SM), CommentsLoaded(false),
7116ebf09130479bc7605aa09a3e6c4dc2ba3513495Dmitri Gribenko    CommentCommandTraits(BumpAlloc, LOpts.CommentOpts),
712bcfd1f55bfbb3e5944cd5e03d07b343e280838c4Douglas Gregor    LastSDM(0, 0),
713f7992132207d7ddc8810a135cec97ebe5805e500Fariborz Jahanian    UniqueBlockByRefTypeID(0)
714bcfd1f55bfbb3e5944cd5e03d07b343e280838c4Douglas Gregor{
7151eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  if (size_reserve > 0) Types.reserve(size_reserve);
716e91593ef084479340582b2ba177b44be50a717b7Daniel Dunbar  TUDecl = TranslationUnitDecl::Create(*this);
717bcfd1f55bfbb3e5944cd5e03d07b343e280838c4Douglas Gregor
718bcfd1f55bfbb3e5944cd5e03d07b343e280838c4Douglas Gregor  if (!DelayInitialization) {
719bcfd1f55bfbb3e5944cd5e03d07b343e280838c4Douglas Gregor    assert(t && "No target supplied for ASTContext initialization");
720bcfd1f55bfbb3e5944cd5e03d07b343e280838c4Douglas Gregor    InitBuiltinTypes(*t);
721bcfd1f55bfbb3e5944cd5e03d07b343e280838c4Douglas Gregor  }
722e91593ef084479340582b2ba177b44be50a717b7Daniel Dunbar}
723e91593ef084479340582b2ba177b44be50a717b7Daniel Dunbar
7245f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid SpencerASTContext::~ASTContext() {
7253478eb6872d836600caf45b0f81c2065d685d6e0Ted Kremenek  // Release the DenseMaps associated with DeclContext objects.
7263478eb6872d836600caf45b0f81c2065d685d6e0Ted Kremenek  // FIXME: Is this the ideal solution?
7273478eb6872d836600caf45b0f81c2065d685d6e0Ted Kremenek  ReleaseDeclContextMaps();
7287d10b7eb670b821741b4c96f6cf7afbc3bb39abeDouglas Gregor
72963fe86bee66fc145942c56b2cc564ea0b9b9ea12Douglas Gregor  // Call all of the deallocation functions.
73063fe86bee66fc145942c56b2cc564ea0b9b9ea12Douglas Gregor  for (unsigned I = 0, N = Deallocations.size(); I != N; ++I)
73163fe86bee66fc145942c56b2cc564ea0b9b9ea12Douglas Gregor    Deallocations[I].first(Deallocations[I].second);
7320054531488928a424666ac11fcdc6bcc5112de52Douglas Gregor
733dcfcfbec478f7ed96cd8d92f30c29bd4e30d5b9cTed Kremenek  // ASTRecordLayout objects in ASTRecordLayouts must always be destroyed
73463fe86bee66fc145942c56b2cc564ea0b9b9ea12Douglas Gregor  // because they can contain DenseMaps.
73563fe86bee66fc145942c56b2cc564ea0b9b9ea12Douglas Gregor  for (llvm::DenseMap<const ObjCContainerDecl*,
73663fe86bee66fc145942c56b2cc564ea0b9b9ea12Douglas Gregor       const ASTRecordLayout*>::iterator
73763fe86bee66fc145942c56b2cc564ea0b9b9ea12Douglas Gregor       I = ObjCLayouts.begin(), E = ObjCLayouts.end(); I != E; )
73863fe86bee66fc145942c56b2cc564ea0b9b9ea12Douglas Gregor    // Increment in loop to prevent using deallocated memory.
73963fe86bee66fc145942c56b2cc564ea0b9b9ea12Douglas Gregor    if (ASTRecordLayout *R = const_cast<ASTRecordLayout*>((I++)->second))
74063fe86bee66fc145942c56b2cc564ea0b9b9ea12Douglas Gregor      R->Destroy(*this);
74163fe86bee66fc145942c56b2cc564ea0b9b9ea12Douglas Gregor
742dcfcfbec478f7ed96cd8d92f30c29bd4e30d5b9cTed Kremenek  for (llvm::DenseMap<const RecordDecl*, const ASTRecordLayout*>::iterator
743dcfcfbec478f7ed96cd8d92f30c29bd4e30d5b9cTed Kremenek       I = ASTRecordLayouts.begin(), E = ASTRecordLayouts.end(); I != E; ) {
744dcfcfbec478f7ed96cd8d92f30c29bd4e30d5b9cTed Kremenek    // Increment in loop to prevent using deallocated memory.
745dcfcfbec478f7ed96cd8d92f30c29bd4e30d5b9cTed Kremenek    if (ASTRecordLayout *R = const_cast<ASTRecordLayout*>((I++)->second))
746dcfcfbec478f7ed96cd8d92f30c29bd4e30d5b9cTed Kremenek      R->Destroy(*this);
747dcfcfbec478f7ed96cd8d92f30c29bd4e30d5b9cTed Kremenek  }
7486320064d0c60fa8683f5623881c9394fd4aa7689Douglas Gregor
7496320064d0c60fa8683f5623881c9394fd4aa7689Douglas Gregor  for (llvm::DenseMap<const Decl*, AttrVec*>::iterator A = DeclAttrs.begin(),
7506320064d0c60fa8683f5623881c9394fd4aa7689Douglas Gregor                                                    AEnd = DeclAttrs.end();
7516320064d0c60fa8683f5623881c9394fd4aa7689Douglas Gregor       A != AEnd; ++A)
7526320064d0c60fa8683f5623881c9394fd4aa7689Douglas Gregor    A->second->~AttrVec();
7536320064d0c60fa8683f5623881c9394fd4aa7689Douglas Gregor}
754ab452ba8323d1985e08bade2bced588cddf2cc28Douglas Gregor
7550054531488928a424666ac11fcdc6bcc5112de52Douglas Gregorvoid ASTContext::AddDeallocation(void (*Callback)(void*), void *Data) {
7560054531488928a424666ac11fcdc6bcc5112de52Douglas Gregor  Deallocations.push_back(std::make_pair(Callback, Data));
7570054531488928a424666ac11fcdc6bcc5112de52Douglas Gregor}
7580054531488928a424666ac11fcdc6bcc5112de52Douglas Gregor
7591eb4433ac451dc16f4133a88af2d002ac26c58efMike Stumpvoid
7606f42b62b6194f53bcbc349f5d17388e1936535d7Dylan NoblesmithASTContext::setExternalSource(OwningPtr<ExternalASTSource> &Source) {
7612cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor  ExternalSource.reset(Source.take());
7622cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor}
7632cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor
7645f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencervoid ASTContext::PrintStats() const {
765cd92a65edc7cbbbb7e3aee8d31008594de90fa51Chandler Carruth  llvm::errs() << "\n*** AST Context Stats:\n";
766cd92a65edc7cbbbb7e3aee8d31008594de90fa51Chandler Carruth  llvm::errs() << "  " << Types.size() << " types total.\n";
7677c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl
768dbe833da54e1b6192991b64fc453cd50b5ee7909Douglas Gregor  unsigned counts[] = {
7691eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump#define TYPE(Name, Parent) 0,
770dbe833da54e1b6192991b64fc453cd50b5ee7909Douglas Gregor#define ABSTRACT_TYPE(Name, Parent)
771dbe833da54e1b6192991b64fc453cd50b5ee7909Douglas Gregor#include "clang/AST/TypeNodes.def"
772dbe833da54e1b6192991b64fc453cd50b5ee7909Douglas Gregor    0 // Extra
773dbe833da54e1b6192991b64fc453cd50b5ee7909Douglas Gregor  };
774c2ee10d79f70036af652a395ac1f8273f3d04e12Douglas Gregor
7755f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  for (unsigned i = 0, e = Types.size(); i != e; ++i) {
7765f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    Type *T = Types[i];
777dbe833da54e1b6192991b64fc453cd50b5ee7909Douglas Gregor    counts[(unsigned)T->getTypeClass()]++;
7785f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
7795f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
780dbe833da54e1b6192991b64fc453cd50b5ee7909Douglas Gregor  unsigned Idx = 0;
781dbe833da54e1b6192991b64fc453cd50b5ee7909Douglas Gregor  unsigned TotalBytes = 0;
782dbe833da54e1b6192991b64fc453cd50b5ee7909Douglas Gregor#define TYPE(Name, Parent)                                              \
783dbe833da54e1b6192991b64fc453cd50b5ee7909Douglas Gregor  if (counts[Idx])                                                      \
784cd92a65edc7cbbbb7e3aee8d31008594de90fa51Chandler Carruth    llvm::errs() << "    " << counts[Idx] << " " << #Name               \
785cd92a65edc7cbbbb7e3aee8d31008594de90fa51Chandler Carruth                 << " types\n";                                         \
786dbe833da54e1b6192991b64fc453cd50b5ee7909Douglas Gregor  TotalBytes += counts[Idx] * sizeof(Name##Type);                       \
787dbe833da54e1b6192991b64fc453cd50b5ee7909Douglas Gregor  ++Idx;
788dbe833da54e1b6192991b64fc453cd50b5ee7909Douglas Gregor#define ABSTRACT_TYPE(Name, Parent)
789dbe833da54e1b6192991b64fc453cd50b5ee7909Douglas Gregor#include "clang/AST/TypeNodes.def"
7901eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
791cd92a65edc7cbbbb7e3aee8d31008594de90fa51Chandler Carruth  llvm::errs() << "Total bytes = " << TotalBytes << "\n";
792cd92a65edc7cbbbb7e3aee8d31008594de90fa51Chandler Carruth
7934923aa25eb39d64106a5817c02d560a3aecf8b2cDouglas Gregor  // Implicit special member functions.
794cd92a65edc7cbbbb7e3aee8d31008594de90fa51Chandler Carruth  llvm::errs() << NumImplicitDefaultConstructorsDeclared << "/"
795cd92a65edc7cbbbb7e3aee8d31008594de90fa51Chandler Carruth               << NumImplicitDefaultConstructors
796cd92a65edc7cbbbb7e3aee8d31008594de90fa51Chandler Carruth               << " implicit default constructors created\n";
797cd92a65edc7cbbbb7e3aee8d31008594de90fa51Chandler Carruth  llvm::errs() << NumImplicitCopyConstructorsDeclared << "/"
798cd92a65edc7cbbbb7e3aee8d31008594de90fa51Chandler Carruth               << NumImplicitCopyConstructors
799cd92a65edc7cbbbb7e3aee8d31008594de90fa51Chandler Carruth               << " implicit copy constructors created\n";
8004e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie  if (getLangOpts().CPlusPlus)
801cd92a65edc7cbbbb7e3aee8d31008594de90fa51Chandler Carruth    llvm::errs() << NumImplicitMoveConstructorsDeclared << "/"
802cd92a65edc7cbbbb7e3aee8d31008594de90fa51Chandler Carruth                 << NumImplicitMoveConstructors
803cd92a65edc7cbbbb7e3aee8d31008594de90fa51Chandler Carruth                 << " implicit move constructors created\n";
804cd92a65edc7cbbbb7e3aee8d31008594de90fa51Chandler Carruth  llvm::errs() << NumImplicitCopyAssignmentOperatorsDeclared << "/"
805cd92a65edc7cbbbb7e3aee8d31008594de90fa51Chandler Carruth               << NumImplicitCopyAssignmentOperators
806cd92a65edc7cbbbb7e3aee8d31008594de90fa51Chandler Carruth               << " implicit copy assignment operators created\n";
8074e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie  if (getLangOpts().CPlusPlus)
808cd92a65edc7cbbbb7e3aee8d31008594de90fa51Chandler Carruth    llvm::errs() << NumImplicitMoveAssignmentOperatorsDeclared << "/"
809cd92a65edc7cbbbb7e3aee8d31008594de90fa51Chandler Carruth                 << NumImplicitMoveAssignmentOperators
810cd92a65edc7cbbbb7e3aee8d31008594de90fa51Chandler Carruth                 << " implicit move assignment operators created\n";
811cd92a65edc7cbbbb7e3aee8d31008594de90fa51Chandler Carruth  llvm::errs() << NumImplicitDestructorsDeclared << "/"
812cd92a65edc7cbbbb7e3aee8d31008594de90fa51Chandler Carruth               << NumImplicitDestructors
813cd92a65edc7cbbbb7e3aee8d31008594de90fa51Chandler Carruth               << " implicit destructors created\n";
814cd92a65edc7cbbbb7e3aee8d31008594de90fa51Chandler Carruth
8152cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor  if (ExternalSource.get()) {
816cd92a65edc7cbbbb7e3aee8d31008594de90fa51Chandler Carruth    llvm::errs() << "\n";
8172cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor    ExternalSource->PrintStats();
8182cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor  }
819cd92a65edc7cbbbb7e3aee8d31008594de90fa51Chandler Carruth
82063fe86bee66fc145942c56b2cc564ea0b9b9ea12Douglas Gregor  BumpAlloc.PrintStats();
8215f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
8225f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
823772eeaefef2c883aabe35caf4543e7e32d290183Douglas GregorTypedefDecl *ASTContext::getInt128Decl() const {
824772eeaefef2c883aabe35caf4543e7e32d290183Douglas Gregor  if (!Int128Decl) {
825772eeaefef2c883aabe35caf4543e7e32d290183Douglas Gregor    TypeSourceInfo *TInfo = getTrivialTypeSourceInfo(Int128Ty);
826772eeaefef2c883aabe35caf4543e7e32d290183Douglas Gregor    Int128Decl = TypedefDecl::Create(const_cast<ASTContext &>(*this),
827772eeaefef2c883aabe35caf4543e7e32d290183Douglas Gregor                                     getTranslationUnitDecl(),
828772eeaefef2c883aabe35caf4543e7e32d290183Douglas Gregor                                     SourceLocation(),
829772eeaefef2c883aabe35caf4543e7e32d290183Douglas Gregor                                     SourceLocation(),
830772eeaefef2c883aabe35caf4543e7e32d290183Douglas Gregor                                     &Idents.get("__int128_t"),
831772eeaefef2c883aabe35caf4543e7e32d290183Douglas Gregor                                     TInfo);
832772eeaefef2c883aabe35caf4543e7e32d290183Douglas Gregor  }
833772eeaefef2c883aabe35caf4543e7e32d290183Douglas Gregor
834772eeaefef2c883aabe35caf4543e7e32d290183Douglas Gregor  return Int128Decl;
835772eeaefef2c883aabe35caf4543e7e32d290183Douglas Gregor}
836772eeaefef2c883aabe35caf4543e7e32d290183Douglas Gregor
837772eeaefef2c883aabe35caf4543e7e32d290183Douglas GregorTypedefDecl *ASTContext::getUInt128Decl() const {
838772eeaefef2c883aabe35caf4543e7e32d290183Douglas Gregor  if (!UInt128Decl) {
839772eeaefef2c883aabe35caf4543e7e32d290183Douglas Gregor    TypeSourceInfo *TInfo = getTrivialTypeSourceInfo(UnsignedInt128Ty);
840772eeaefef2c883aabe35caf4543e7e32d290183Douglas Gregor    UInt128Decl = TypedefDecl::Create(const_cast<ASTContext &>(*this),
841772eeaefef2c883aabe35caf4543e7e32d290183Douglas Gregor                                     getTranslationUnitDecl(),
842772eeaefef2c883aabe35caf4543e7e32d290183Douglas Gregor                                     SourceLocation(),
843772eeaefef2c883aabe35caf4543e7e32d290183Douglas Gregor                                     SourceLocation(),
844772eeaefef2c883aabe35caf4543e7e32d290183Douglas Gregor                                     &Idents.get("__uint128_t"),
845772eeaefef2c883aabe35caf4543e7e32d290183Douglas Gregor                                     TInfo);
846772eeaefef2c883aabe35caf4543e7e32d290183Douglas Gregor  }
847772eeaefef2c883aabe35caf4543e7e32d290183Douglas Gregor
848772eeaefef2c883aabe35caf4543e7e32d290183Douglas Gregor  return UInt128Decl;
849772eeaefef2c883aabe35caf4543e7e32d290183Douglas Gregor}
8505f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
851e27ec8ad56dbf1efb2de004b90fbbb86f740e3f1John McCallvoid ASTContext::InitBuiltinType(CanQualType &R, BuiltinType::Kind K) {
8526b304a0254a13f42390b865ff5ba668a49cc58aeJohn McCall  BuiltinType *Ty = new (*this, TypeAlignment) BuiltinType(K);
853e27ec8ad56dbf1efb2de004b90fbbb86f740e3f1John McCall  R = CanQualType::CreateUnsafe(QualType(Ty, 0));
8546b304a0254a13f42390b865ff5ba668a49cc58aeJohn McCall  Types.push_back(Ty);
8555f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
8565f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
857bcfd1f55bfbb3e5944cd5e03d07b343e280838c4Douglas Gregorvoid ASTContext::InitBuiltinTypes(const TargetInfo &Target) {
858bcfd1f55bfbb3e5944cd5e03d07b343e280838c4Douglas Gregor  assert((!this->Target || this->Target == &Target) &&
859bcfd1f55bfbb3e5944cd5e03d07b343e280838c4Douglas Gregor         "Incorrect target reinitialization");
8605f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  assert(VoidTy.isNull() && "Context reinitialized?");
8611eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
862bcfd1f55bfbb3e5944cd5e03d07b343e280838c4Douglas Gregor  this->Target = &Target;
863bcfd1f55bfbb3e5944cd5e03d07b343e280838c4Douglas Gregor
864bcfd1f55bfbb3e5944cd5e03d07b343e280838c4Douglas Gregor  ABI.reset(createCXXABI(Target));
865bcfd1f55bfbb3e5944cd5e03d07b343e280838c4Douglas Gregor  AddrSpaceMap = getAddressSpaceMap(Target, LangOpts);
866bcfd1f55bfbb3e5944cd5e03d07b343e280838c4Douglas Gregor
8675f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // C99 6.2.5p19.
8685f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  InitBuiltinType(VoidTy,              BuiltinType::Void);
8691eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
8705f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // C99 6.2.5p2.
8715f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  InitBuiltinType(BoolTy,              BuiltinType::Bool);
8725f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // C99 6.2.5p3.
87315b91764d08e886391c865c4a444d7b51141c284Eli Friedman  if (LangOpts.CharIsSigned)
8745f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    InitBuiltinType(CharTy,            BuiltinType::Char_S);
8755f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  else
8765f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    InitBuiltinType(CharTy,            BuiltinType::Char_U);
8775f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // C99 6.2.5p4.
8785f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  InitBuiltinType(SignedCharTy,        BuiltinType::SChar);
8795f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  InitBuiltinType(ShortTy,             BuiltinType::Short);
8805f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  InitBuiltinType(IntTy,               BuiltinType::Int);
8815f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  InitBuiltinType(LongTy,              BuiltinType::Long);
8825f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  InitBuiltinType(LongLongTy,          BuiltinType::LongLong);
8831eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
8845f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // C99 6.2.5p6.
8855f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  InitBuiltinType(UnsignedCharTy,      BuiltinType::UChar);
8865f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  InitBuiltinType(UnsignedShortTy,     BuiltinType::UShort);
8875f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  InitBuiltinType(UnsignedIntTy,       BuiltinType::UInt);
8885f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  InitBuiltinType(UnsignedLongTy,      BuiltinType::ULong);
8895f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  InitBuiltinType(UnsignedLongLongTy,  BuiltinType::ULongLong);
8901eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
8915f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // C99 6.2.5p10.
8925f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  InitBuiltinType(FloatTy,             BuiltinType::Float);
8935f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  InitBuiltinType(DoubleTy,            BuiltinType::Double);
8945f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  InitBuiltinType(LongDoubleTy,        BuiltinType::LongDouble);
89564c438a4be2a871fa43c78264663ba1e9788b94dArgyrios Kyrtzidis
8962df9ced9fd1e8c7d7b38443db07e0e811de22571Chris Lattner  // GNU extension, 128-bit integers.
8972df9ced9fd1e8c7d7b38443db07e0e811de22571Chris Lattner  InitBuiltinType(Int128Ty,            BuiltinType::Int128);
8982df9ced9fd1e8c7d7b38443db07e0e811de22571Chris Lattner  InitBuiltinType(UnsignedInt128Ty,    BuiltinType::UInt128);
8992df9ced9fd1e8c7d7b38443db07e0e811de22571Chris Lattner
900e75bb61f1b876afaa6b2f4a2b860c2889ea1d050Abramo Bagnara  if (LangOpts.CPlusPlus && LangOpts.WChar) { // C++ 3.9.1p5
901d3d77cd138f8e830f6547b6f83fcd5721ccf5f5dEli Friedman    if (TargetInfo::isTypeSigned(Target.getWCharType()))
9023f59c975aa5d047f7edd1b900b5e885c38af0ef7Chris Lattner      InitBuiltinType(WCharTy,           BuiltinType::WChar_S);
9033f59c975aa5d047f7edd1b900b5e885c38af0ef7Chris Lattner    else  // -fshort-wchar makes wchar_t be unsigned.
9043f59c975aa5d047f7edd1b900b5e885c38af0ef7Chris Lattner      InitBuiltinType(WCharTy,           BuiltinType::WChar_U);
905e75bb61f1b876afaa6b2f4a2b860c2889ea1d050Abramo Bagnara  } else // C99 (or C++ using -fno-wchar)
9063a2503227c3db04a3619735127483263c1075ef7Chris Lattner    WCharTy = getFromTargetType(Target.getWCharType());
90764c438a4be2a871fa43c78264663ba1e9788b94dArgyrios Kyrtzidis
908392da48160bd92ceb486792780467cbfdb2d0e8cJames Molloy  WIntTy = getFromTargetType(Target.getWIntType());
909392da48160bd92ceb486792780467cbfdb2d0e8cJames Molloy
910f5c209d23b20ada4a9b6235db50317239cbf6ae1Alisdair Meredith  if (LangOpts.CPlusPlus) // C++0x 3.9.1p5, extension for C++
911f5c209d23b20ada4a9b6235db50317239cbf6ae1Alisdair Meredith    InitBuiltinType(Char16Ty,           BuiltinType::Char16);
912f5c209d23b20ada4a9b6235db50317239cbf6ae1Alisdair Meredith  else // C99
913f5c209d23b20ada4a9b6235db50317239cbf6ae1Alisdair Meredith    Char16Ty = getFromTargetType(Target.getChar16Type());
914f5c209d23b20ada4a9b6235db50317239cbf6ae1Alisdair Meredith
915f5c209d23b20ada4a9b6235db50317239cbf6ae1Alisdair Meredith  if (LangOpts.CPlusPlus) // C++0x 3.9.1p5, extension for C++
916f5c209d23b20ada4a9b6235db50317239cbf6ae1Alisdair Meredith    InitBuiltinType(Char32Ty,           BuiltinType::Char32);
917f5c209d23b20ada4a9b6235db50317239cbf6ae1Alisdair Meredith  else // C99
918f5c209d23b20ada4a9b6235db50317239cbf6ae1Alisdair Meredith    Char32Ty = getFromTargetType(Target.getChar32Type());
919f5c209d23b20ada4a9b6235db50317239cbf6ae1Alisdair Meredith
920898574e7496ba8fd76290079d3a9d06954992734Douglas Gregor  // Placeholder type for type-dependent expressions whose type is
921898574e7496ba8fd76290079d3a9d06954992734Douglas Gregor  // completely unknown. No code should ever check a type against
922898574e7496ba8fd76290079d3a9d06954992734Douglas Gregor  // DependentTy and users should never see it; however, it is here to
923898574e7496ba8fd76290079d3a9d06954992734Douglas Gregor  // help diagnose failures to properly check for type-dependent
924898574e7496ba8fd76290079d3a9d06954992734Douglas Gregor  // expressions.
925898574e7496ba8fd76290079d3a9d06954992734Douglas Gregor  InitBuiltinType(DependentTy,         BuiltinType::Dependent);
9268e9bebdea69c590dedfbf27374114cb76fe12fbdDouglas Gregor
9272a984cad5ac3fdceeff2bd99daa7b90979313475John McCall  // Placeholder type for functions.
9282a984cad5ac3fdceeff2bd99daa7b90979313475John McCall  InitBuiltinType(OverloadTy,          BuiltinType::Overload);
9292a984cad5ac3fdceeff2bd99daa7b90979313475John McCall
930864c041e118155c2b1ce0ba36942a3da5a4a055eJohn McCall  // Placeholder type for bound members.
931864c041e118155c2b1ce0ba36942a3da5a4a055eJohn McCall  InitBuiltinType(BoundMemberTy,       BuiltinType::BoundMember);
932864c041e118155c2b1ce0ba36942a3da5a4a055eJohn McCall
9333c3b7f90a863af43fa63043d396553ecf205351cJohn McCall  // Placeholder type for pseudo-objects.
9343c3b7f90a863af43fa63043d396553ecf205351cJohn McCall  InitBuiltinType(PseudoObjectTy,      BuiltinType::PseudoObject);
9353c3b7f90a863af43fa63043d396553ecf205351cJohn McCall
9361de4d4e8cb2e9c88809fea8092bc6e835a5473d2John McCall  // "any" type; useful for debugger-like clients.
9371de4d4e8cb2e9c88809fea8092bc6e835a5473d2John McCall  InitBuiltinType(UnknownAnyTy,        BuiltinType::UnknownAny);
9381de4d4e8cb2e9c88809fea8092bc6e835a5473d2John McCall
9390ddaeb9b031070ec64afe92d9892875ac44df427John McCall  // Placeholder type for unbridged ARC casts.
9400ddaeb9b031070ec64afe92d9892875ac44df427John McCall  InitBuiltinType(ARCUnbridgedCastTy,  BuiltinType::ARCUnbridgedCast);
9410ddaeb9b031070ec64afe92d9892875ac44df427John McCall
942a6c66cedc022c9e5d45a937d6b8cff491a6bf81bEli Friedman  // Placeholder type for builtin functions.
943a6c66cedc022c9e5d45a937d6b8cff491a6bf81bEli Friedman  InitBuiltinType(BuiltinFnTy,  BuiltinType::BuiltinFn);
944a6c66cedc022c9e5d45a937d6b8cff491a6bf81bEli Friedman
9455f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // C99 6.2.5p11.
9465f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  FloatComplexTy      = getComplexType(FloatTy);
9475f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  DoubleComplexTy     = getComplexType(DoubleTy);
9485f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  LongDoubleComplexTy = getComplexType(LongDoubleTy);
9498e9bebdea69c590dedfbf27374114cb76fe12fbdDouglas Gregor
95013dcd00615de5c4279d97bdf63cd5f0a14fd9dccFariborz Jahanian  // Builtin types for 'id', 'Class', and 'SEL'.
951de2e22d33afec98324a66a358dfe0951b3c7259aSteve Naroff  InitBuiltinType(ObjCBuiltinIdTy, BuiltinType::ObjCId);
952de2e22d33afec98324a66a358dfe0951b3c7259aSteve Naroff  InitBuiltinType(ObjCBuiltinClassTy, BuiltinType::ObjCClass);
95313dcd00615de5c4279d97bdf63cd5f0a14fd9dccFariborz Jahanian  InitBuiltinType(ObjCBuiltinSelTy, BuiltinType::ObjCSel);
954b13621d08e20ac7aa550e05896de8a57ee99c1e8Guy Benyei
955b13621d08e20ac7aa550e05896de8a57ee99c1e8Guy Benyei  if (LangOpts.OpenCL) {
956b13621d08e20ac7aa550e05896de8a57ee99c1e8Guy Benyei    InitBuiltinType(OCLImage1dTy, BuiltinType::OCLImage1d);
957b13621d08e20ac7aa550e05896de8a57ee99c1e8Guy Benyei    InitBuiltinType(OCLImage1dArrayTy, BuiltinType::OCLImage1dArray);
958b13621d08e20ac7aa550e05896de8a57ee99c1e8Guy Benyei    InitBuiltinType(OCLImage1dBufferTy, BuiltinType::OCLImage1dBuffer);
959b13621d08e20ac7aa550e05896de8a57ee99c1e8Guy Benyei    InitBuiltinType(OCLImage2dTy, BuiltinType::OCLImage2d);
960b13621d08e20ac7aa550e05896de8a57ee99c1e8Guy Benyei    InitBuiltinType(OCLImage2dArrayTy, BuiltinType::OCLImage2dArray);
961b13621d08e20ac7aa550e05896de8a57ee99c1e8Guy Benyei    InitBuiltinType(OCLImage3dTy, BuiltinType::OCLImage3d);
962e6b9d802fb7b16d93474c4f1c179ab36202e8a8bGuy Benyei
96321f18c4fda167dc5f72feddbd6a7ac1b63200a0dGuy Benyei    InitBuiltinType(OCLSamplerTy, BuiltinType::OCLSampler);
964e6b9d802fb7b16d93474c4f1c179ab36202e8a8bGuy Benyei    InitBuiltinType(OCLEventTy, BuiltinType::OCLEvent);
965b13621d08e20ac7aa550e05896de8a57ee99c1e8Guy Benyei  }
966ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek
967ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek  // Builtin type for __objc_yes and __objc_no
96893a49944e0e68e32bc22d45d44ee136b34beffb3Fariborz Jahanian  ObjCBuiltinBoolTy = (Target.useSignedCharForObjCBool() ?
96993a49944e0e68e32bc22d45d44ee136b34beffb3Fariborz Jahanian                       SignedCharTy : BoolTy);
970ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek
971a526c5c67e5a0473c340903ee542ce570119665fTed Kremenek  ObjCConstantStringType = QualType();
972f7992132207d7ddc8810a135cec97ebe5805e500Fariborz Jahanian
973f7992132207d7ddc8810a135cec97ebe5805e500Fariborz Jahanian  ObjCSuperType = QualType();
9741eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
97533e1d64ab5cd5d27f8530ccd056191fe2c9f3f2eFariborz Jahanian  // void * type
97633e1d64ab5cd5d27f8530ccd056191fe2c9f3f2eFariborz Jahanian  VoidPtrTy = getPointerType(VoidTy);
9776e8ed16ffef02b82995a90bdcf10ffff7d63839aSebastian Redl
9786e8ed16ffef02b82995a90bdcf10ffff7d63839aSebastian Redl  // nullptr type (C++0x 2.14.7)
9796e8ed16ffef02b82995a90bdcf10ffff7d63839aSebastian Redl  InitBuiltinType(NullPtrTy,           BuiltinType::NullPtr);
980aa4a99b4a62615db243f7a5c433169f2fc704420Anton Korobeynikov
981aa4a99b4a62615db243f7a5c433169f2fc704420Anton Korobeynikov  // half type (OpenCL 6.1.1.1) / ARM NEON __fp16
982aa4a99b4a62615db243f7a5c433169f2fc704420Anton Korobeynikov  InitBuiltinType(HalfTy, BuiltinType::Half);
983fb40e3f10ccef93c4f8fb6bd4fe5a108fa6cd369Meador Inge
984fb40e3f10ccef93c4f8fb6bd4fe5a108fa6cd369Meador Inge  // Builtin type used to help define __builtin_va_list.
985fb40e3f10ccef93c4f8fb6bd4fe5a108fa6cd369Meador Inge  VaListTagTy = QualType();
9865f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
9875f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
988d6471f7c1921c7802804ce3ff6fe9768310f72b9David BlaikieDiagnosticsEngine &ASTContext::getDiagnostics() const {
98978a916ec5ff5b66adec3c499e1b9af7b87668309Argyrios Kyrtzidis  return SourceMgr.getDiagnostics();
99078a916ec5ff5b66adec3c499e1b9af7b87668309Argyrios Kyrtzidis}
99178a916ec5ff5b66adec3c499e1b9af7b87668309Argyrios Kyrtzidis
9926320064d0c60fa8683f5623881c9394fd4aa7689Douglas GregorAttrVec& ASTContext::getDeclAttrs(const Decl *D) {
9936320064d0c60fa8683f5623881c9394fd4aa7689Douglas Gregor  AttrVec *&Result = DeclAttrs[D];
9946320064d0c60fa8683f5623881c9394fd4aa7689Douglas Gregor  if (!Result) {
9956320064d0c60fa8683f5623881c9394fd4aa7689Douglas Gregor    void *Mem = Allocate(sizeof(AttrVec));
9966320064d0c60fa8683f5623881c9394fd4aa7689Douglas Gregor    Result = new (Mem) AttrVec;
9976320064d0c60fa8683f5623881c9394fd4aa7689Douglas Gregor  }
9986320064d0c60fa8683f5623881c9394fd4aa7689Douglas Gregor
9996320064d0c60fa8683f5623881c9394fd4aa7689Douglas Gregor  return *Result;
10006320064d0c60fa8683f5623881c9394fd4aa7689Douglas Gregor}
10016320064d0c60fa8683f5623881c9394fd4aa7689Douglas Gregor
10026320064d0c60fa8683f5623881c9394fd4aa7689Douglas Gregor/// \brief Erase the attributes corresponding to the given declaration.
10036320064d0c60fa8683f5623881c9394fd4aa7689Douglas Gregorvoid ASTContext::eraseDeclAttrs(const Decl *D) {
10046320064d0c60fa8683f5623881c9394fd4aa7689Douglas Gregor  llvm::DenseMap<const Decl*, AttrVec*>::iterator Pos = DeclAttrs.find(D);
10056320064d0c60fa8683f5623881c9394fd4aa7689Douglas Gregor  if (Pos != DeclAttrs.end()) {
10066320064d0c60fa8683f5623881c9394fd4aa7689Douglas Gregor    Pos->second->~AttrVec();
10076320064d0c60fa8683f5623881c9394fd4aa7689Douglas Gregor    DeclAttrs.erase(Pos);
10086320064d0c60fa8683f5623881c9394fd4aa7689Douglas Gregor  }
10096320064d0c60fa8683f5623881c9394fd4aa7689Douglas Gregor}
10106320064d0c60fa8683f5623881c9394fd4aa7689Douglas Gregor
1011251b4ff2578e26959a4c036140ccd61c5e9292f2Douglas GregorMemberSpecializationInfo *
1012663b5a0be7261c29bc4c526a71cffcfa02d4153eDouglas GregorASTContext::getInstantiatedFromStaticDataMember(const VarDecl *Var) {
10137caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  assert(Var->isStaticDataMember() && "Not a static data member");
1014663b5a0be7261c29bc4c526a71cffcfa02d4153eDouglas Gregor  llvm::DenseMap<const VarDecl *, MemberSpecializationInfo *>::iterator Pos
10157caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor    = InstantiatedFromStaticDataMember.find(Var);
10167caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  if (Pos == InstantiatedFromStaticDataMember.end())
10177caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor    return 0;
10181eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
10197caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  return Pos->second;
10207caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor}
10217caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor
10221eb4433ac451dc16f4133a88af2d002ac26c58efMike Stumpvoid
1023251b4ff2578e26959a4c036140ccd61c5e9292f2Douglas GregorASTContext::setInstantiatedFromStaticDataMember(VarDecl *Inst, VarDecl *Tmpl,
10249421adc43891e272156fab640e5d5ee5054b779cArgyrios Kyrtzidis                                                TemplateSpecializationKind TSK,
10259421adc43891e272156fab640e5d5ee5054b779cArgyrios Kyrtzidis                                          SourceLocation PointOfInstantiation) {
10267caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  assert(Inst->isStaticDataMember() && "Not a static data member");
10277caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  assert(Tmpl->isStaticDataMember() && "Not a static data member");
10287caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor  assert(!InstantiatedFromStaticDataMember[Inst] &&
10297caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor         "Already noted what static data member was instantiated from");
1030251b4ff2578e26959a4c036140ccd61c5e9292f2Douglas Gregor  InstantiatedFromStaticDataMember[Inst]
10319421adc43891e272156fab640e5d5ee5054b779cArgyrios Kyrtzidis    = new (*this) MemberSpecializationInfo(Tmpl, TSK, PointOfInstantiation);
10327caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor}
10337caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor
1034af0f4d0b2e38c810effc8b024ad2fb6604eec5d3Francois PichetFunctionDecl *ASTContext::getClassScopeSpecializationPattern(
1035af0f4d0b2e38c810effc8b024ad2fb6604eec5d3Francois Pichet                                                     const FunctionDecl *FD){
1036af0f4d0b2e38c810effc8b024ad2fb6604eec5d3Francois Pichet  assert(FD && "Specialization is 0");
1037af0f4d0b2e38c810effc8b024ad2fb6604eec5d3Francois Pichet  llvm::DenseMap<const FunctionDecl*, FunctionDecl *>::const_iterator Pos
10380d95f0d7b81110f77e99e833f766d19be7b7e072Francois Pichet    = ClassScopeSpecializationPattern.find(FD);
10390d95f0d7b81110f77e99e833f766d19be7b7e072Francois Pichet  if (Pos == ClassScopeSpecializationPattern.end())
1040af0f4d0b2e38c810effc8b024ad2fb6604eec5d3Francois Pichet    return 0;
1041af0f4d0b2e38c810effc8b024ad2fb6604eec5d3Francois Pichet
1042af0f4d0b2e38c810effc8b024ad2fb6604eec5d3Francois Pichet  return Pos->second;
1043af0f4d0b2e38c810effc8b024ad2fb6604eec5d3Francois Pichet}
1044af0f4d0b2e38c810effc8b024ad2fb6604eec5d3Francois Pichet
1045af0f4d0b2e38c810effc8b024ad2fb6604eec5d3Francois Pichetvoid ASTContext::setClassScopeSpecializationPattern(FunctionDecl *FD,
1046af0f4d0b2e38c810effc8b024ad2fb6604eec5d3Francois Pichet                                        FunctionDecl *Pattern) {
1047af0f4d0b2e38c810effc8b024ad2fb6604eec5d3Francois Pichet  assert(FD && "Specialization is 0");
1048af0f4d0b2e38c810effc8b024ad2fb6604eec5d3Francois Pichet  assert(Pattern && "Class scope specialization pattern is 0");
10490d95f0d7b81110f77e99e833f766d19be7b7e072Francois Pichet  ClassScopeSpecializationPattern[FD] = Pattern;
1050af0f4d0b2e38c810effc8b024ad2fb6604eec5d3Francois Pichet}
1051af0f4d0b2e38c810effc8b024ad2fb6604eec5d3Francois Pichet
10527ba107a1863ddfa1664555854f0d7bdb3c491c92John McCallNamedDecl *
1053ed97649e9574b9d854fa4d6109c9333ae0993554John McCallASTContext::getInstantiatedFromUsingDecl(UsingDecl *UUD) {
10547ba107a1863ddfa1664555854f0d7bdb3c491c92John McCall  llvm::DenseMap<UsingDecl *, NamedDecl *>::const_iterator Pos
1055ed97649e9574b9d854fa4d6109c9333ae0993554John McCall    = InstantiatedFromUsingDecl.find(UUD);
1056ed97649e9574b9d854fa4d6109c9333ae0993554John McCall  if (Pos == InstantiatedFromUsingDecl.end())
10570d8df780aef1acda5962347a32591efc629b6748Anders Carlsson    return 0;
10581eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
10590d8df780aef1acda5962347a32591efc629b6748Anders Carlsson  return Pos->second;
10600d8df780aef1acda5962347a32591efc629b6748Anders Carlsson}
10610d8df780aef1acda5962347a32591efc629b6748Anders Carlsson
10620d8df780aef1acda5962347a32591efc629b6748Anders Carlssonvoid
1063ed97649e9574b9d854fa4d6109c9333ae0993554John McCallASTContext::setInstantiatedFromUsingDecl(UsingDecl *Inst, NamedDecl *Pattern) {
1064ed97649e9574b9d854fa4d6109c9333ae0993554John McCall  assert((isa<UsingDecl>(Pattern) ||
1065ed97649e9574b9d854fa4d6109c9333ae0993554John McCall          isa<UnresolvedUsingValueDecl>(Pattern) ||
1066ed97649e9574b9d854fa4d6109c9333ae0993554John McCall          isa<UnresolvedUsingTypenameDecl>(Pattern)) &&
1067ed97649e9574b9d854fa4d6109c9333ae0993554John McCall         "pattern decl is not a using decl");
1068ed97649e9574b9d854fa4d6109c9333ae0993554John McCall  assert(!InstantiatedFromUsingDecl[Inst] && "pattern already exists");
1069ed97649e9574b9d854fa4d6109c9333ae0993554John McCall  InstantiatedFromUsingDecl[Inst] = Pattern;
1070ed97649e9574b9d854fa4d6109c9333ae0993554John McCall}
1071ed97649e9574b9d854fa4d6109c9333ae0993554John McCall
1072ed97649e9574b9d854fa4d6109c9333ae0993554John McCallUsingShadowDecl *
1073ed97649e9574b9d854fa4d6109c9333ae0993554John McCallASTContext::getInstantiatedFromUsingShadowDecl(UsingShadowDecl *Inst) {
1074ed97649e9574b9d854fa4d6109c9333ae0993554John McCall  llvm::DenseMap<UsingShadowDecl*, UsingShadowDecl*>::const_iterator Pos
1075ed97649e9574b9d854fa4d6109c9333ae0993554John McCall    = InstantiatedFromUsingShadowDecl.find(Inst);
1076ed97649e9574b9d854fa4d6109c9333ae0993554John McCall  if (Pos == InstantiatedFromUsingShadowDecl.end())
1077ed97649e9574b9d854fa4d6109c9333ae0993554John McCall    return 0;
1078ed97649e9574b9d854fa4d6109c9333ae0993554John McCall
1079ed97649e9574b9d854fa4d6109c9333ae0993554John McCall  return Pos->second;
1080ed97649e9574b9d854fa4d6109c9333ae0993554John McCall}
1081ed97649e9574b9d854fa4d6109c9333ae0993554John McCall
1082ed97649e9574b9d854fa4d6109c9333ae0993554John McCallvoid
1083ed97649e9574b9d854fa4d6109c9333ae0993554John McCallASTContext::setInstantiatedFromUsingShadowDecl(UsingShadowDecl *Inst,
1084ed97649e9574b9d854fa4d6109c9333ae0993554John McCall                                               UsingShadowDecl *Pattern) {
1085ed97649e9574b9d854fa4d6109c9333ae0993554John McCall  assert(!InstantiatedFromUsingShadowDecl[Inst] && "pattern already exists");
1086ed97649e9574b9d854fa4d6109c9333ae0993554John McCall  InstantiatedFromUsingShadowDecl[Inst] = Pattern;
10870d8df780aef1acda5962347a32591efc629b6748Anders Carlsson}
10880d8df780aef1acda5962347a32591efc629b6748Anders Carlsson
1089d8b285fee4471f393da8ee30f552ceacdc362afaAnders CarlssonFieldDecl *ASTContext::getInstantiatedFromUnnamedFieldDecl(FieldDecl *Field) {
1090d8b285fee4471f393da8ee30f552ceacdc362afaAnders Carlsson  llvm::DenseMap<FieldDecl *, FieldDecl *>::iterator Pos
1091d8b285fee4471f393da8ee30f552ceacdc362afaAnders Carlsson    = InstantiatedFromUnnamedFieldDecl.find(Field);
1092d8b285fee4471f393da8ee30f552ceacdc362afaAnders Carlsson  if (Pos == InstantiatedFromUnnamedFieldDecl.end())
1093d8b285fee4471f393da8ee30f552ceacdc362afaAnders Carlsson    return 0;
10941eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1095d8b285fee4471f393da8ee30f552ceacdc362afaAnders Carlsson  return Pos->second;
1096d8b285fee4471f393da8ee30f552ceacdc362afaAnders Carlsson}
1097d8b285fee4471f393da8ee30f552ceacdc362afaAnders Carlsson
1098d8b285fee4471f393da8ee30f552ceacdc362afaAnders Carlssonvoid ASTContext::setInstantiatedFromUnnamedFieldDecl(FieldDecl *Inst,
1099d8b285fee4471f393da8ee30f552ceacdc362afaAnders Carlsson                                                     FieldDecl *Tmpl) {
1100d8b285fee4471f393da8ee30f552ceacdc362afaAnders Carlsson  assert(!Inst->getDeclName() && "Instantiated field decl is not unnamed");
1101d8b285fee4471f393da8ee30f552ceacdc362afaAnders Carlsson  assert(!Tmpl->getDeclName() && "Template field decl is not unnamed");
1102d8b285fee4471f393da8ee30f552ceacdc362afaAnders Carlsson  assert(!InstantiatedFromUnnamedFieldDecl[Inst] &&
1103d8b285fee4471f393da8ee30f552ceacdc362afaAnders Carlsson         "Already noted what unnamed field was instantiated from");
11041eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1105d8b285fee4471f393da8ee30f552ceacdc362afaAnders Carlsson  InstantiatedFromUnnamedFieldDecl[Inst] = Tmpl;
1106d8b285fee4471f393da8ee30f552ceacdc362afaAnders Carlsson}
1107d8b285fee4471f393da8ee30f552ceacdc362afaAnders Carlsson
110814d56ef43ff4921c6749f7340212fbb743fdbb9bFariborz Jahanianbool ASTContext::ZeroBitfieldFollowsNonBitfield(const FieldDecl *FD,
110914d56ef43ff4921c6749f7340212fbb743fdbb9bFariborz Jahanian                                    const FieldDecl *LastFD) const {
111014d56ef43ff4921c6749f7340212fbb743fdbb9bFariborz Jahanian  return (FD->isBitField() && LastFD && !LastFD->isBitField() &&
1111a6b8b2c09610b8bc4330e948ece8b940c2386406Richard Smith          FD->getBitWidthValue(*this) == 0);
111214d56ef43ff4921c6749f7340212fbb743fdbb9bFariborz Jahanian}
111314d56ef43ff4921c6749f7340212fbb743fdbb9bFariborz Jahanian
1114340fa242130c2d8d74c83edca0952e771aebe0e6Fariborz Jahanianbool ASTContext::ZeroBitfieldFollowsBitfield(const FieldDecl *FD,
1115340fa242130c2d8d74c83edca0952e771aebe0e6Fariborz Jahanian                                             const FieldDecl *LastFD) const {
1116340fa242130c2d8d74c83edca0952e771aebe0e6Fariborz Jahanian  return (FD->isBitField() && LastFD && LastFD->isBitField() &&
1117a6b8b2c09610b8bc4330e948ece8b940c2386406Richard Smith          FD->getBitWidthValue(*this) == 0 &&
1118a6b8b2c09610b8bc4330e948ece8b940c2386406Richard Smith          LastFD->getBitWidthValue(*this) != 0);
1119340fa242130c2d8d74c83edca0952e771aebe0e6Fariborz Jahanian}
1120340fa242130c2d8d74c83edca0952e771aebe0e6Fariborz Jahanian
11219b3acaa32548d0ce78b9c39a3911397f6738a47cFariborz Jahanianbool ASTContext::BitfieldFollowsBitfield(const FieldDecl *FD,
11229b3acaa32548d0ce78b9c39a3911397f6738a47cFariborz Jahanian                                         const FieldDecl *LastFD) const {
11239b3acaa32548d0ce78b9c39a3911397f6738a47cFariborz Jahanian  return (FD->isBitField() && LastFD && LastFD->isBitField() &&
1124a6b8b2c09610b8bc4330e948ece8b940c2386406Richard Smith          FD->getBitWidthValue(*this) &&
1125a6b8b2c09610b8bc4330e948ece8b940c2386406Richard Smith          LastFD->getBitWidthValue(*this));
11269b3acaa32548d0ce78b9c39a3911397f6738a47cFariborz Jahanian}
11279b3acaa32548d0ce78b9c39a3911397f6738a47cFariborz Jahanian
1128dd7fddb5b6883326e52b278a9b7e9cefea29aae0Chad Rosierbool ASTContext::NonBitfieldFollowsBitfield(const FieldDecl *FD,
112952bbe7a1133c3cb57e9246f1b96c12940ea3821aFariborz Jahanian                                         const FieldDecl *LastFD) const {
113052bbe7a1133c3cb57e9246f1b96c12940ea3821aFariborz Jahanian  return (!FD->isBitField() && LastFD && LastFD->isBitField() &&
1131a6b8b2c09610b8bc4330e948ece8b940c2386406Richard Smith          LastFD->getBitWidthValue(*this));
113252bbe7a1133c3cb57e9246f1b96c12940ea3821aFariborz Jahanian}
113352bbe7a1133c3cb57e9246f1b96c12940ea3821aFariborz Jahanian
1134dd7fddb5b6883326e52b278a9b7e9cefea29aae0Chad Rosierbool ASTContext::BitfieldFollowsNonBitfield(const FieldDecl *FD,
113552bbe7a1133c3cb57e9246f1b96c12940ea3821aFariborz Jahanian                                             const FieldDecl *LastFD) const {
113652bbe7a1133c3cb57e9246f1b96c12940ea3821aFariborz Jahanian  return (FD->isBitField() && LastFD && !LastFD->isBitField() &&
1137a6b8b2c09610b8bc4330e948ece8b940c2386406Richard Smith          FD->getBitWidthValue(*this));
113852bbe7a1133c3cb57e9246f1b96c12940ea3821aFariborz Jahanian}
113952bbe7a1133c3cb57e9246f1b96c12940ea3821aFariborz Jahanian
11407d10b7eb670b821741b4c96f6cf7afbc3bb39abeDouglas GregorASTContext::overridden_cxx_method_iterator
11417d10b7eb670b821741b4c96f6cf7afbc3bb39abeDouglas GregorASTContext::overridden_methods_begin(const CXXMethodDecl *Method) const {
11427d10b7eb670b821741b4c96f6cf7afbc3bb39abeDouglas Gregor  llvm::DenseMap<const CXXMethodDecl *, CXXMethodVector>::const_iterator Pos
114338eb1e161f602ee810dfb8a5a0d8462572f22689Argyrios Kyrtzidis    = OverriddenMethods.find(Method->getCanonicalDecl());
11447d10b7eb670b821741b4c96f6cf7afbc3bb39abeDouglas Gregor  if (Pos == OverriddenMethods.end())
11457d10b7eb670b821741b4c96f6cf7afbc3bb39abeDouglas Gregor    return 0;
11467d10b7eb670b821741b4c96f6cf7afbc3bb39abeDouglas Gregor
11477d10b7eb670b821741b4c96f6cf7afbc3bb39abeDouglas Gregor  return Pos->second.begin();
11487d10b7eb670b821741b4c96f6cf7afbc3bb39abeDouglas Gregor}
11497d10b7eb670b821741b4c96f6cf7afbc3bb39abeDouglas Gregor
11507d10b7eb670b821741b4c96f6cf7afbc3bb39abeDouglas GregorASTContext::overridden_cxx_method_iterator
11517d10b7eb670b821741b4c96f6cf7afbc3bb39abeDouglas GregorASTContext::overridden_methods_end(const CXXMethodDecl *Method) const {
11527d10b7eb670b821741b4c96f6cf7afbc3bb39abeDouglas Gregor  llvm::DenseMap<const CXXMethodDecl *, CXXMethodVector>::const_iterator Pos
115338eb1e161f602ee810dfb8a5a0d8462572f22689Argyrios Kyrtzidis    = OverriddenMethods.find(Method->getCanonicalDecl());
11547d10b7eb670b821741b4c96f6cf7afbc3bb39abeDouglas Gregor  if (Pos == OverriddenMethods.end())
11557d10b7eb670b821741b4c96f6cf7afbc3bb39abeDouglas Gregor    return 0;
11567d10b7eb670b821741b4c96f6cf7afbc3bb39abeDouglas Gregor
11577d10b7eb670b821741b4c96f6cf7afbc3bb39abeDouglas Gregor  return Pos->second.end();
11587d10b7eb670b821741b4c96f6cf7afbc3bb39abeDouglas Gregor}
11597d10b7eb670b821741b4c96f6cf7afbc3bb39abeDouglas Gregor
1160c91e9f439ae85d5f79a6b65672f1d7d1b55ccda0Argyrios Kyrtzidisunsigned
1161c91e9f439ae85d5f79a6b65672f1d7d1b55ccda0Argyrios KyrtzidisASTContext::overridden_methods_size(const CXXMethodDecl *Method) const {
1162c91e9f439ae85d5f79a6b65672f1d7d1b55ccda0Argyrios Kyrtzidis  llvm::DenseMap<const CXXMethodDecl *, CXXMethodVector>::const_iterator Pos
116338eb1e161f602ee810dfb8a5a0d8462572f22689Argyrios Kyrtzidis    = OverriddenMethods.find(Method->getCanonicalDecl());
1164c91e9f439ae85d5f79a6b65672f1d7d1b55ccda0Argyrios Kyrtzidis  if (Pos == OverriddenMethods.end())
1165c91e9f439ae85d5f79a6b65672f1d7d1b55ccda0Argyrios Kyrtzidis    return 0;
1166c91e9f439ae85d5f79a6b65672f1d7d1b55ccda0Argyrios Kyrtzidis
1167c91e9f439ae85d5f79a6b65672f1d7d1b55ccda0Argyrios Kyrtzidis  return Pos->second.size();
1168c91e9f439ae85d5f79a6b65672f1d7d1b55ccda0Argyrios Kyrtzidis}
1169c91e9f439ae85d5f79a6b65672f1d7d1b55ccda0Argyrios Kyrtzidis
11707d10b7eb670b821741b4c96f6cf7afbc3bb39abeDouglas Gregorvoid ASTContext::addOverriddenMethod(const CXXMethodDecl *Method,
11717d10b7eb670b821741b4c96f6cf7afbc3bb39abeDouglas Gregor                                     const CXXMethodDecl *Overridden) {
117238eb1e161f602ee810dfb8a5a0d8462572f22689Argyrios Kyrtzidis  assert(Method->isCanonicalDecl() && Overridden->isCanonicalDecl());
11737d10b7eb670b821741b4c96f6cf7afbc3bb39abeDouglas Gregor  OverriddenMethods[Method].push_back(Overridden);
11747d10b7eb670b821741b4c96f6cf7afbc3bb39abeDouglas Gregor}
11757d10b7eb670b821741b4c96f6cf7afbc3bb39abeDouglas Gregor
11761e905da74dbfd353d77dfc548fc9d6ff420d515aDmitri Gribenkovoid ASTContext::getOverriddenMethods(
11771e905da74dbfd353d77dfc548fc9d6ff420d515aDmitri Gribenko                      const NamedDecl *D,
11781e905da74dbfd353d77dfc548fc9d6ff420d515aDmitri Gribenko                      SmallVectorImpl<const NamedDecl *> &Overridden) const {
117921c3607282550779c9ae5fe784928597807fd110Argyrios Kyrtzidis  assert(D);
118021c3607282550779c9ae5fe784928597807fd110Argyrios Kyrtzidis
118121c3607282550779c9ae5fe784928597807fd110Argyrios Kyrtzidis  if (const CXXMethodDecl *CXXMethod = dyn_cast<CXXMethodDecl>(D)) {
1182685d10462b875f8c056d27488c0a1b4e13ef680fArgyrios Kyrtzidis    Overridden.append(overridden_methods_begin(CXXMethod),
1183685d10462b875f8c056d27488c0a1b4e13ef680fArgyrios Kyrtzidis                      overridden_methods_end(CXXMethod));
118421c3607282550779c9ae5fe784928597807fd110Argyrios Kyrtzidis    return;
118521c3607282550779c9ae5fe784928597807fd110Argyrios Kyrtzidis  }
118621c3607282550779c9ae5fe784928597807fd110Argyrios Kyrtzidis
118721c3607282550779c9ae5fe784928597807fd110Argyrios Kyrtzidis  const ObjCMethodDecl *Method = dyn_cast<ObjCMethodDecl>(D);
118821c3607282550779c9ae5fe784928597807fd110Argyrios Kyrtzidis  if (!Method)
118921c3607282550779c9ae5fe784928597807fd110Argyrios Kyrtzidis    return;
119021c3607282550779c9ae5fe784928597807fd110Argyrios Kyrtzidis
1191740ae67dbf8dac44dbc8d6593a60f4f37a0a2aa5Argyrios Kyrtzidis  SmallVector<const ObjCMethodDecl *, 8> OverDecls;
1192740ae67dbf8dac44dbc8d6593a60f4f37a0a2aa5Argyrios Kyrtzidis  Method->getOverriddenMethods(OverDecls);
1193bc0a2bb8dc122f8daae890ec82cecfe2054859ebArgyrios Kyrtzidis  Overridden.append(OverDecls.begin(), OverDecls.end());
119421c3607282550779c9ae5fe784928597807fd110Argyrios Kyrtzidis}
119521c3607282550779c9ae5fe784928597807fd110Argyrios Kyrtzidis
1196e664977aca2a05a77abab5a06dc0fb69e870cfb9Douglas Gregorvoid ASTContext::addedLocalImportDecl(ImportDecl *Import) {
1197e664977aca2a05a77abab5a06dc0fb69e870cfb9Douglas Gregor  assert(!Import->NextLocalImport && "Import declaration already in the chain");
1198e664977aca2a05a77abab5a06dc0fb69e870cfb9Douglas Gregor  assert(!Import->isFromASTFile() && "Non-local import declaration");
1199e664977aca2a05a77abab5a06dc0fb69e870cfb9Douglas Gregor  if (!FirstLocalImport) {
1200e664977aca2a05a77abab5a06dc0fb69e870cfb9Douglas Gregor    FirstLocalImport = Import;
1201e664977aca2a05a77abab5a06dc0fb69e870cfb9Douglas Gregor    LastLocalImport = Import;
1202e664977aca2a05a77abab5a06dc0fb69e870cfb9Douglas Gregor    return;
1203e664977aca2a05a77abab5a06dc0fb69e870cfb9Douglas Gregor  }
1204e664977aca2a05a77abab5a06dc0fb69e870cfb9Douglas Gregor
1205e664977aca2a05a77abab5a06dc0fb69e870cfb9Douglas Gregor  LastLocalImport->NextLocalImport = Import;
1206e664977aca2a05a77abab5a06dc0fb69e870cfb9Douglas Gregor  LastLocalImport = Import;
1207e664977aca2a05a77abab5a06dc0fb69e870cfb9Douglas Gregor}
1208e664977aca2a05a77abab5a06dc0fb69e870cfb9Douglas Gregor
1209464175bba1318bef7905122e9fda20cff926df78Chris Lattner//===----------------------------------------------------------------------===//
1210464175bba1318bef7905122e9fda20cff926df78Chris Lattner//                         Type Sizing and Analysis
1211464175bba1318bef7905122e9fda20cff926df78Chris Lattner//===----------------------------------------------------------------------===//
1212a7674d8a9a69f3f6fe16e70cf2a3b2b15fb7c43dChris Lattner
1213b7cfe88e88cb4f46308de89cf3f0c81bfe624128Chris Lattner/// getFloatTypeSemantics - Return the APFloat 'semantics' for the specified
1214b7cfe88e88cb4f46308de89cf3f0c81bfe624128Chris Lattner/// scalar floating point type.
1215b7cfe88e88cb4f46308de89cf3f0c81bfe624128Chris Lattnerconst llvm::fltSemantics &ASTContext::getFloatTypeSemantics(QualType T) const {
1216183700f494ec9b6701b6efe82bcb25f4c79ba561John McCall  const BuiltinType *BT = T->getAs<BuiltinType>();
1217b7cfe88e88cb4f46308de89cf3f0c81bfe624128Chris Lattner  assert(BT && "Not a floating point type!");
1218b7cfe88e88cb4f46308de89cf3f0c81bfe624128Chris Lattner  switch (BT->getKind()) {
1219b219cfc4d75f0a03630b7c4509ef791b7e97b2c8David Blaikie  default: llvm_unreachable("Not a floating point type!");
1220aa4a99b4a62615db243f7a5c433169f2fc704420Anton Korobeynikov  case BuiltinType::Half:       return Target->getHalfFormat();
1221bcfd1f55bfbb3e5944cd5e03d07b343e280838c4Douglas Gregor  case BuiltinType::Float:      return Target->getFloatFormat();
1222bcfd1f55bfbb3e5944cd5e03d07b343e280838c4Douglas Gregor  case BuiltinType::Double:     return Target->getDoubleFormat();
1223bcfd1f55bfbb3e5944cd5e03d07b343e280838c4Douglas Gregor  case BuiltinType::LongDouble: return Target->getLongDoubleFormat();
1224b7cfe88e88cb4f46308de89cf3f0c81bfe624128Chris Lattner  }
1225b7cfe88e88cb4f46308de89cf3f0c81bfe624128Chris Lattner}
1226b7cfe88e88cb4f46308de89cf3f0c81bfe624128Chris Lattner
12278b752f10c394b140f9ef89e049cbad1a7676fc25Ken Dyck/// getDeclAlign - Return a conservative estimate of the alignment of the
1228af707ab8fbb9451e8febb8d766f6c043628125c4Chris Lattner/// specified decl.  Note that bitfields do not have a valid alignment, so
1229af707ab8fbb9451e8febb8d766f6c043628125c4Chris Lattner/// this method will assert on them.
12305d484e8cf710207010720589d89602233de61d01Sebastian Redl/// If @p RefAsPointee, references are treated like their underlying type
12315d484e8cf710207010720589d89602233de61d01Sebastian Redl/// (for alignof), else they're treated like pointers (for CodeGen).
12324ba2a17694148e16eaa8d3917f657ffcd3667be4Jay FoadCharUnits ASTContext::getDeclAlign(const Decl *D, bool RefAsPointee) const {
1233bcfd1f55bfbb3e5944cd5e03d07b343e280838c4Douglas Gregor  unsigned Align = Target->getCharWidth();
1234dcdafb6a701aa9d81edcb088915f58933315dc05Eli Friedman
12354081a5c5f1381c4ec77f8ab3866693917e4329c4John McCall  bool UseAlignAttrOnly = false;
12364081a5c5f1381c4ec77f8ab3866693917e4329c4John McCall  if (unsigned AlignFromAttr = D->getMaxAlignment()) {
12374081a5c5f1381c4ec77f8ab3866693917e4329c4John McCall    Align = AlignFromAttr;
12384081a5c5f1381c4ec77f8ab3866693917e4329c4John McCall
12394081a5c5f1381c4ec77f8ab3866693917e4329c4John McCall    // __attribute__((aligned)) can increase or decrease alignment
12404081a5c5f1381c4ec77f8ab3866693917e4329c4John McCall    // *except* on a struct or struct member, where it only increases
12414081a5c5f1381c4ec77f8ab3866693917e4329c4John McCall    // alignment unless 'packed' is also specified.
12424081a5c5f1381c4ec77f8ab3866693917e4329c4John McCall    //
124382d0b0aab9088e977c2a44c4a5a90479c63149fePeter Collingbourne    // It is an error for alignas to decrease alignment, so we can
12444081a5c5f1381c4ec77f8ab3866693917e4329c4John McCall    // ignore that possibility;  Sema should diagnose it.
12454081a5c5f1381c4ec77f8ab3866693917e4329c4John McCall    if (isa<FieldDecl>(D)) {
12464081a5c5f1381c4ec77f8ab3866693917e4329c4John McCall      UseAlignAttrOnly = D->hasAttr<PackedAttr>() ||
12474081a5c5f1381c4ec77f8ab3866693917e4329c4John McCall        cast<FieldDecl>(D)->getParent()->hasAttr<PackedAttr>();
12484081a5c5f1381c4ec77f8ab3866693917e4329c4John McCall    } else {
12494081a5c5f1381c4ec77f8ab3866693917e4329c4John McCall      UseAlignAttrOnly = true;
12504081a5c5f1381c4ec77f8ab3866693917e4329c4John McCall    }
12514081a5c5f1381c4ec77f8ab3866693917e4329c4John McCall  }
125278a7d7d79964119a3f35b262eb154b5cbf1001edFariborz Jahanian  else if (isa<FieldDecl>(D))
125378a7d7d79964119a3f35b262eb154b5cbf1001edFariborz Jahanian      UseAlignAttrOnly =
125478a7d7d79964119a3f35b262eb154b5cbf1001edFariborz Jahanian        D->hasAttr<PackedAttr>() ||
125578a7d7d79964119a3f35b262eb154b5cbf1001edFariborz Jahanian        cast<FieldDecl>(D)->getParent()->hasAttr<PackedAttr>();
1256dcdafb6a701aa9d81edcb088915f58933315dc05Eli Friedman
1257ba4f5d5754c8291690d01ca9581926673d69b24cJohn McCall  // If we're using the align attribute only, just ignore everything
1258ba4f5d5754c8291690d01ca9581926673d69b24cJohn McCall  // else about the declaration and its type.
12594081a5c5f1381c4ec77f8ab3866693917e4329c4John McCall  if (UseAlignAttrOnly) {
1260ba4f5d5754c8291690d01ca9581926673d69b24cJohn McCall    // do nothing
1261ba4f5d5754c8291690d01ca9581926673d69b24cJohn McCall
12624081a5c5f1381c4ec77f8ab3866693917e4329c4John McCall  } else if (const ValueDecl *VD = dyn_cast<ValueDecl>(D)) {
1263af707ab8fbb9451e8febb8d766f6c043628125c4Chris Lattner    QualType T = VD->getType();
12646217b80b7a1379b74cced1c076338262c3c980b3Ted Kremenek    if (const ReferenceType* RT = T->getAs<ReferenceType>()) {
12655d484e8cf710207010720589d89602233de61d01Sebastian Redl      if (RefAsPointee)
12665d484e8cf710207010720589d89602233de61d01Sebastian Redl        T = RT->getPointeeType();
12675d484e8cf710207010720589d89602233de61d01Sebastian Redl      else
12685d484e8cf710207010720589d89602233de61d01Sebastian Redl        T = getPointerType(RT->getPointeeType());
12695d484e8cf710207010720589d89602233de61d01Sebastian Redl    }
12705d484e8cf710207010720589d89602233de61d01Sebastian Redl    if (!T->isIncompleteType() && !T->isFunctionType()) {
12713b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall      // Adjust alignments of declarations with array type by the
12723b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall      // large-array alignment on the target.
1273bcfd1f55bfbb3e5944cd5e03d07b343e280838c4Douglas Gregor      unsigned MinWidth = Target->getLargeArrayMinWidth();
12743b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall      const ArrayType *arrayType;
12753b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall      if (MinWidth && (arrayType = getAsArrayType(T))) {
12763b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall        if (isa<VariableArrayType>(arrayType))
1277bcfd1f55bfbb3e5944cd5e03d07b343e280838c4Douglas Gregor          Align = std::max(Align, Target->getLargeArrayAlign());
12783b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall        else if (isa<ConstantArrayType>(arrayType) &&
12793b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall                 MinWidth <= getTypeSize(cast<ConstantArrayType>(arrayType)))
1280bcfd1f55bfbb3e5944cd5e03d07b343e280838c4Douglas Gregor          Align = std::max(Align, Target->getLargeArrayAlign());
12813b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall
12823b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall        // Walk through any array types while we're at it.
12833b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall        T = getBaseElementType(arrayType);
12846deecb0d46bcfd048e651d2db7c4fb0d6407da96Rafael Espindola      }
12859f1210c3280104417a4ad30f0a00825ac8fa718aChad Rosier      Align = std::max(Align, getPreferredTypeAlign(T.getTypePtr()));
1286dcdafb6a701aa9d81edcb088915f58933315dc05Eli Friedman    }
1287ba4f5d5754c8291690d01ca9581926673d69b24cJohn McCall
1288ba4f5d5754c8291690d01ca9581926673d69b24cJohn McCall    // Fields can be subject to extra alignment constraints, like if
1289ba4f5d5754c8291690d01ca9581926673d69b24cJohn McCall    // the field is packed, the struct is packed, or the struct has a
1290ba4f5d5754c8291690d01ca9581926673d69b24cJohn McCall    // a max-field-alignment constraint (#pragma pack).  So calculate
1291ba4f5d5754c8291690d01ca9581926673d69b24cJohn McCall    // the actual alignment of the field within the struct, and then
1292ba4f5d5754c8291690d01ca9581926673d69b24cJohn McCall    // (as we're expected to) constrain that by the alignment of the type.
1293ba4f5d5754c8291690d01ca9581926673d69b24cJohn McCall    if (const FieldDecl *field = dyn_cast<FieldDecl>(VD)) {
1294ba4f5d5754c8291690d01ca9581926673d69b24cJohn McCall      // So calculate the alignment of the field.
1295ba4f5d5754c8291690d01ca9581926673d69b24cJohn McCall      const ASTRecordLayout &layout = getASTRecordLayout(field->getParent());
1296ba4f5d5754c8291690d01ca9581926673d69b24cJohn McCall
1297ba4f5d5754c8291690d01ca9581926673d69b24cJohn McCall      // Start with the record's overall alignment.
1298dac54c124e302d6f028ea5723c425b7f66fc7c71Ken Dyck      unsigned fieldAlign = toBits(layout.getAlignment());
1299ba4f5d5754c8291690d01ca9581926673d69b24cJohn McCall
1300ba4f5d5754c8291690d01ca9581926673d69b24cJohn McCall      // Use the GCD of that and the offset within the record.
1301ba4f5d5754c8291690d01ca9581926673d69b24cJohn McCall      uint64_t offset = layout.getFieldOffset(field->getFieldIndex());
1302ba4f5d5754c8291690d01ca9581926673d69b24cJohn McCall      if (offset > 0) {
1303ba4f5d5754c8291690d01ca9581926673d69b24cJohn McCall        // Alignment is always a power of 2, so the GCD will be a power of 2,
1304ba4f5d5754c8291690d01ca9581926673d69b24cJohn McCall        // which means we get to do this crazy thing instead of Euclid's.
1305ba4f5d5754c8291690d01ca9581926673d69b24cJohn McCall        uint64_t lowBitOfOffset = offset & (~offset + 1);
1306ba4f5d5754c8291690d01ca9581926673d69b24cJohn McCall        if (lowBitOfOffset < fieldAlign)
1307ba4f5d5754c8291690d01ca9581926673d69b24cJohn McCall          fieldAlign = static_cast<unsigned>(lowBitOfOffset);
1308ba4f5d5754c8291690d01ca9581926673d69b24cJohn McCall      }
1309ba4f5d5754c8291690d01ca9581926673d69b24cJohn McCall
1310ba4f5d5754c8291690d01ca9581926673d69b24cJohn McCall      Align = std::min(Align, fieldAlign);
131105f62474dd2b0f1cb69adbe0787f2868788aa949Charles Davis    }
1312af707ab8fbb9451e8febb8d766f6c043628125c4Chris Lattner  }
1313dcdafb6a701aa9d81edcb088915f58933315dc05Eli Friedman
1314eb6f5dc86531f794ba7746a2da4d28e37cf5da7eKen Dyck  return toCharUnitsFromBits(Align);
1315af707ab8fbb9451e8febb8d766f6c043628125c4Chris Lattner}
1316b7cfe88e88cb4f46308de89cf3f0c81bfe624128Chris Lattner
1317929bbfb0b69165b55da3c56abf22aa10e20dadc6John McCall// getTypeInfoDataSizeInChars - Return the size of a type, in
1318929bbfb0b69165b55da3c56abf22aa10e20dadc6John McCall// chars. If the type is a record, its data size is returned.  This is
1319929bbfb0b69165b55da3c56abf22aa10e20dadc6John McCall// the size of the memcpy that's performed when assigning this type
1320929bbfb0b69165b55da3c56abf22aa10e20dadc6John McCall// using a trivial copy/move assignment operator.
1321929bbfb0b69165b55da3c56abf22aa10e20dadc6John McCallstd::pair<CharUnits, CharUnits>
1322929bbfb0b69165b55da3c56abf22aa10e20dadc6John McCallASTContext::getTypeInfoDataSizeInChars(QualType T) const {
1323929bbfb0b69165b55da3c56abf22aa10e20dadc6John McCall  std::pair<CharUnits, CharUnits> sizeAndAlign = getTypeInfoInChars(T);
1324929bbfb0b69165b55da3c56abf22aa10e20dadc6John McCall
1325929bbfb0b69165b55da3c56abf22aa10e20dadc6John McCall  // In C++, objects can sometimes be allocated into the tail padding
1326929bbfb0b69165b55da3c56abf22aa10e20dadc6John McCall  // of a base-class subobject.  We decide whether that's possible
1327929bbfb0b69165b55da3c56abf22aa10e20dadc6John McCall  // during class layout, so here we can just trust the layout results.
1328929bbfb0b69165b55da3c56abf22aa10e20dadc6John McCall  if (getLangOpts().CPlusPlus) {
1329929bbfb0b69165b55da3c56abf22aa10e20dadc6John McCall    if (const RecordType *RT = T->getAs<RecordType>()) {
1330929bbfb0b69165b55da3c56abf22aa10e20dadc6John McCall      const ASTRecordLayout &layout = getASTRecordLayout(RT->getDecl());
1331929bbfb0b69165b55da3c56abf22aa10e20dadc6John McCall      sizeAndAlign.first = layout.getDataSize();
1332929bbfb0b69165b55da3c56abf22aa10e20dadc6John McCall    }
1333929bbfb0b69165b55da3c56abf22aa10e20dadc6John McCall  }
1334929bbfb0b69165b55da3c56abf22aa10e20dadc6John McCall
1335929bbfb0b69165b55da3c56abf22aa10e20dadc6John McCall  return sizeAndAlign;
1336929bbfb0b69165b55da3c56abf22aa10e20dadc6John McCall}
1337929bbfb0b69165b55da3c56abf22aa10e20dadc6John McCall
1338ea1471e0e967548c596a71469702f8846dbaf3c0John McCallstd::pair<CharUnits, CharUnits>
1339bee5a79fc95e3003d755031e3d2bb4410a71e1c1Ken DyckASTContext::getTypeInfoInChars(const Type *T) const {
1340ea1471e0e967548c596a71469702f8846dbaf3c0John McCall  std::pair<uint64_t, unsigned> Info = getTypeInfo(T);
1341eb6f5dc86531f794ba7746a2da4d28e37cf5da7eKen Dyck  return std::make_pair(toCharUnitsFromBits(Info.first),
1342eb6f5dc86531f794ba7746a2da4d28e37cf5da7eKen Dyck                        toCharUnitsFromBits(Info.second));
1343ea1471e0e967548c596a71469702f8846dbaf3c0John McCall}
1344ea1471e0e967548c596a71469702f8846dbaf3c0John McCall
1345ea1471e0e967548c596a71469702f8846dbaf3c0John McCallstd::pair<CharUnits, CharUnits>
1346bee5a79fc95e3003d755031e3d2bb4410a71e1c1Ken DyckASTContext::getTypeInfoInChars(QualType T) const {
1347ea1471e0e967548c596a71469702f8846dbaf3c0John McCall  return getTypeInfoInChars(T.getTypePtr());
1348ea1471e0e967548c596a71469702f8846dbaf3c0John McCall}
1349ea1471e0e967548c596a71469702f8846dbaf3c0John McCall
1350bc5419a2edc4030d1a623576fe339fbd3eed17a6Daniel Dunbarstd::pair<uint64_t, unsigned> ASTContext::getTypeInfo(const Type *T) const {
1351bc5419a2edc4030d1a623576fe339fbd3eed17a6Daniel Dunbar  TypeInfoMap::iterator it = MemoizedTypeInfo.find(T);
1352bc5419a2edc4030d1a623576fe339fbd3eed17a6Daniel Dunbar  if (it != MemoizedTypeInfo.end())
1353bc5419a2edc4030d1a623576fe339fbd3eed17a6Daniel Dunbar    return it->second;
1354bc5419a2edc4030d1a623576fe339fbd3eed17a6Daniel Dunbar
1355bc5419a2edc4030d1a623576fe339fbd3eed17a6Daniel Dunbar  std::pair<uint64_t, unsigned> Info = getTypeInfoImpl(T);
1356bc5419a2edc4030d1a623576fe339fbd3eed17a6Daniel Dunbar  MemoizedTypeInfo.insert(std::make_pair(T, Info));
1357bc5419a2edc4030d1a623576fe339fbd3eed17a6Daniel Dunbar  return Info;
1358bc5419a2edc4030d1a623576fe339fbd3eed17a6Daniel Dunbar}
1359bc5419a2edc4030d1a623576fe339fbd3eed17a6Daniel Dunbar
1360bc5419a2edc4030d1a623576fe339fbd3eed17a6Daniel Dunbar/// getTypeInfoImpl - Return the size of the specified type, in bits.  This
1361bc5419a2edc4030d1a623576fe339fbd3eed17a6Daniel Dunbar/// method does not work on incomplete types.
13620953e767ff7817f97b3ab20896b229891eeff45bJohn McCall///
13630953e767ff7817f97b3ab20896b229891eeff45bJohn McCall/// FIXME: Pointers into different addr spaces could have different sizes and
13640953e767ff7817f97b3ab20896b229891eeff45bJohn McCall/// alignment requirements: getPointerInfo should take an AddrSpace, this
13650953e767ff7817f97b3ab20896b229891eeff45bJohn McCall/// should take a QualType, &c.
1366d2d2a11a91d7ddf468bfb70f66362d24806ed601Chris Lattnerstd::pair<uint64_t, unsigned>
1367bc5419a2edc4030d1a623576fe339fbd3eed17a6Daniel DunbarASTContext::getTypeInfoImpl(const Type *T) const {
13685e301007e31e14c8ff647288e1b8bd8dbf8a5fe4Mike Stump  uint64_t Width=0;
13695e301007e31e14c8ff647288e1b8bd8dbf8a5fe4Mike Stump  unsigned Align=8;
1370a7674d8a9a69f3f6fe16e70cf2a3b2b15fb7c43dChris Lattner  switch (T->getTypeClass()) {
137172564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor#define TYPE(Class, Base)
137272564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor#define ABSTRACT_TYPE(Class, Base)
137318857644059c45da6776f1a288eec7b4cf3a844aDouglas Gregor#define NON_CANONICAL_TYPE(Class, Base)
137472564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor#define DEPENDENT_TYPE(Class, Base) case Type::Class:
137572564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor#include "clang/AST/TypeNodes.def"
1376d3d49bb27c7ffd9accc0a6c00e887111c0348845John McCall    llvm_unreachable("Should not see dependent types");
137772564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor
13785d2a6303467184b1f159bb6556efc434e50e3c28Chris Lattner  case Type::FunctionNoProto:
13795d2a6303467184b1f159bb6556efc434e50e3c28Chris Lattner  case Type::FunctionProto:
138018857644059c45da6776f1a288eec7b4cf3a844aDouglas Gregor    // GCC extension: alignof(function) = 32 bits
138118857644059c45da6776f1a288eec7b4cf3a844aDouglas Gregor    Width = 0;
138218857644059c45da6776f1a288eec7b4cf3a844aDouglas Gregor    Align = 32;
138318857644059c45da6776f1a288eec7b4cf3a844aDouglas Gregor    break;
138418857644059c45da6776f1a288eec7b4cf3a844aDouglas Gregor
138572564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor  case Type::IncompleteArray:
1386fb22d96692c5240fb8d611290dbf7eeed3759c73Steve Naroff  case Type::VariableArray:
138718857644059c45da6776f1a288eec7b4cf3a844aDouglas Gregor    Width = 0;
138818857644059c45da6776f1a288eec7b4cf3a844aDouglas Gregor    Align = getTypeAlign(cast<ArrayType>(T)->getElementType());
138918857644059c45da6776f1a288eec7b4cf3a844aDouglas Gregor    break;
139018857644059c45da6776f1a288eec7b4cf3a844aDouglas Gregor
1391fb22d96692c5240fb8d611290dbf7eeed3759c73Steve Naroff  case Type::ConstantArray: {
13921d75118af76cae2bfc06389cde410e14bd0a19fcDaniel Dunbar    const ConstantArrayType *CAT = cast<ConstantArrayType>(T);
13931eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
139498be4943e8dc4f3905629a7102668960873cf863Chris Lattner    std::pair<uint64_t, unsigned> EltInfo = getTypeInfo(CAT->getElementType());
1395fea966a4103ed9c018d1494b95e9d09b161f5a70Abramo Bagnara    uint64_t Size = CAT->getSize().getZExtValue();
1396bc5419a2edc4030d1a623576fe339fbd3eed17a6Daniel Dunbar    assert((Size == 0 || EltInfo.first <= (uint64_t)(-1)/Size) &&
1397bc5419a2edc4030d1a623576fe339fbd3eed17a6Daniel Dunbar           "Overflow in array type bit size evaluation");
1398fea966a4103ed9c018d1494b95e9d09b161f5a70Abramo Bagnara    Width = EltInfo.first*Size;
1399030d8846c7e520330007087e949f621989876e3aChris Lattner    Align = EltInfo.second;
1400cd88b4171753dcb2bc0a21d78f1597c796bb8a20Argyrios Kyrtzidis    Width = llvm::RoundUpToAlignment(Width, Align);
1401030d8846c7e520330007087e949f621989876e3aChris Lattner    break;
14025c09a02a5db85e08a432b6eeced9aa656349710dChristopher Lamb  }
1403213541a68a3e137d11d2cefb612c6cdb410d7e8eNate Begeman  case Type::ExtVector:
1404030d8846c7e520330007087e949f621989876e3aChris Lattner  case Type::Vector: {
14059fcfe926432f3c3f7e9a61219e55c352fd358e45Chris Lattner    const VectorType *VT = cast<VectorType>(T);
14069fcfe926432f3c3f7e9a61219e55c352fd358e45Chris Lattner    std::pair<uint64_t, unsigned> EltInfo = getTypeInfo(VT->getElementType());
14079fcfe926432f3c3f7e9a61219e55c352fd358e45Chris Lattner    Width = EltInfo.first*VT->getNumElements();
14084bd998bbc228915d2b9cae5b67879de48940d05eEli Friedman    Align = Width;
14096fe7c8aa8c7546743ecd0ac0138c2cf5d8155386Nate Begeman    // If the alignment is not a power of 2, round up to the next power of 2.
14106fe7c8aa8c7546743ecd0ac0138c2cf5d8155386Nate Begeman    // This happens for non-power-of-2 length vectors.
14118eefcd353c1d06a10104f69e5079ebab3183f9a3Dan Gohman    if (Align & (Align-1)) {
14129fcfe926432f3c3f7e9a61219e55c352fd358e45Chris Lattner      Align = llvm::NextPowerOf2(Align);
14139fcfe926432f3c3f7e9a61219e55c352fd358e45Chris Lattner      Width = llvm::RoundUpToAlignment(Width, Align);
14149fcfe926432f3c3f7e9a61219e55c352fd358e45Chris Lattner    }
1415f9e9af7df0cea6e997ac04131c7af6ca4384b0ccChad Rosier    // Adjust the alignment based on the target max.
1416f9e9af7df0cea6e997ac04131c7af6ca4384b0ccChad Rosier    uint64_t TargetVectorAlign = Target->getMaxVectorAlign();
1417f9e9af7df0cea6e997ac04131c7af6ca4384b0ccChad Rosier    if (TargetVectorAlign && TargetVectorAlign < Align)
1418f9e9af7df0cea6e997ac04131c7af6ca4384b0ccChad Rosier      Align = TargetVectorAlign;
1419030d8846c7e520330007087e949f621989876e3aChris Lattner    break;
1420030d8846c7e520330007087e949f621989876e3aChris Lattner  }
14215d2a6303467184b1f159bb6556efc434e50e3c28Chris Lattner
14229e9b6dc3fd413f5341fab54b681420eeb21cd169Chris Lattner  case Type::Builtin:
1423a7674d8a9a69f3f6fe16e70cf2a3b2b15fb7c43dChris Lattner    switch (cast<BuiltinType>(T)->getKind()) {
1424b219cfc4d75f0a03630b7c4509ef791b7e97b2c8David Blaikie    default: llvm_unreachable("Unknown builtin type!");
1425d2d2a11a91d7ddf468bfb70f66362d24806ed601Chris Lattner    case BuiltinType::Void:
142618857644059c45da6776f1a288eec7b4cf3a844aDouglas Gregor      // GCC extension: alignof(void) = 8 bits.
142718857644059c45da6776f1a288eec7b4cf3a844aDouglas Gregor      Width = 0;
142818857644059c45da6776f1a288eec7b4cf3a844aDouglas Gregor      Align = 8;
142918857644059c45da6776f1a288eec7b4cf3a844aDouglas Gregor      break;
143018857644059c45da6776f1a288eec7b4cf3a844aDouglas Gregor
14316f62c2abd8077bf70d2166d37e8caa426b34d8e4Chris Lattner    case BuiltinType::Bool:
1432bcfd1f55bfbb3e5944cd5e03d07b343e280838c4Douglas Gregor      Width = Target->getBoolWidth();
1433bcfd1f55bfbb3e5944cd5e03d07b343e280838c4Douglas Gregor      Align = Target->getBoolAlign();
14346f62c2abd8077bf70d2166d37e8caa426b34d8e4Chris Lattner      break;
1435692233e90a99c3a81dd04879d36eb9688f137c44Chris Lattner    case BuiltinType::Char_S:
1436692233e90a99c3a81dd04879d36eb9688f137c44Chris Lattner    case BuiltinType::Char_U:
1437692233e90a99c3a81dd04879d36eb9688f137c44Chris Lattner    case BuiltinType::UChar:
14386f62c2abd8077bf70d2166d37e8caa426b34d8e4Chris Lattner    case BuiltinType::SChar:
1439bcfd1f55bfbb3e5944cd5e03d07b343e280838c4Douglas Gregor      Width = Target->getCharWidth();
1440bcfd1f55bfbb3e5944cd5e03d07b343e280838c4Douglas Gregor      Align = Target->getCharAlign();
14416f62c2abd8077bf70d2166d37e8caa426b34d8e4Chris Lattner      break;
14423f59c975aa5d047f7edd1b900b5e885c38af0ef7Chris Lattner    case BuiltinType::WChar_S:
14433f59c975aa5d047f7edd1b900b5e885c38af0ef7Chris Lattner    case BuiltinType::WChar_U:
1444bcfd1f55bfbb3e5944cd5e03d07b343e280838c4Douglas Gregor      Width = Target->getWCharWidth();
1445bcfd1f55bfbb3e5944cd5e03d07b343e280838c4Douglas Gregor      Align = Target->getWCharAlign();
144664c438a4be2a871fa43c78264663ba1e9788b94dArgyrios Kyrtzidis      break;
1447f5c209d23b20ada4a9b6235db50317239cbf6ae1Alisdair Meredith    case BuiltinType::Char16:
1448bcfd1f55bfbb3e5944cd5e03d07b343e280838c4Douglas Gregor      Width = Target->getChar16Width();
1449bcfd1f55bfbb3e5944cd5e03d07b343e280838c4Douglas Gregor      Align = Target->getChar16Align();
1450f5c209d23b20ada4a9b6235db50317239cbf6ae1Alisdair Meredith      break;
1451f5c209d23b20ada4a9b6235db50317239cbf6ae1Alisdair Meredith    case BuiltinType::Char32:
1452bcfd1f55bfbb3e5944cd5e03d07b343e280838c4Douglas Gregor      Width = Target->getChar32Width();
1453bcfd1f55bfbb3e5944cd5e03d07b343e280838c4Douglas Gregor      Align = Target->getChar32Align();
1454f5c209d23b20ada4a9b6235db50317239cbf6ae1Alisdair Meredith      break;
1455692233e90a99c3a81dd04879d36eb9688f137c44Chris Lattner    case BuiltinType::UShort:
14566f62c2abd8077bf70d2166d37e8caa426b34d8e4Chris Lattner    case BuiltinType::Short:
1457bcfd1f55bfbb3e5944cd5e03d07b343e280838c4Douglas Gregor      Width = Target->getShortWidth();
1458bcfd1f55bfbb3e5944cd5e03d07b343e280838c4Douglas Gregor      Align = Target->getShortAlign();
14596f62c2abd8077bf70d2166d37e8caa426b34d8e4Chris Lattner      break;
1460692233e90a99c3a81dd04879d36eb9688f137c44Chris Lattner    case BuiltinType::UInt:
14616f62c2abd8077bf70d2166d37e8caa426b34d8e4Chris Lattner    case BuiltinType::Int:
1462bcfd1f55bfbb3e5944cd5e03d07b343e280838c4Douglas Gregor      Width = Target->getIntWidth();
1463bcfd1f55bfbb3e5944cd5e03d07b343e280838c4Douglas Gregor      Align = Target->getIntAlign();
14646f62c2abd8077bf70d2166d37e8caa426b34d8e4Chris Lattner      break;
1465692233e90a99c3a81dd04879d36eb9688f137c44Chris Lattner    case BuiltinType::ULong:
14666f62c2abd8077bf70d2166d37e8caa426b34d8e4Chris Lattner    case BuiltinType::Long:
1467bcfd1f55bfbb3e5944cd5e03d07b343e280838c4Douglas Gregor      Width = Target->getLongWidth();
1468bcfd1f55bfbb3e5944cd5e03d07b343e280838c4Douglas Gregor      Align = Target->getLongAlign();
14696f62c2abd8077bf70d2166d37e8caa426b34d8e4Chris Lattner      break;
1470692233e90a99c3a81dd04879d36eb9688f137c44Chris Lattner    case BuiltinType::ULongLong:
14716f62c2abd8077bf70d2166d37e8caa426b34d8e4Chris Lattner    case BuiltinType::LongLong:
1472bcfd1f55bfbb3e5944cd5e03d07b343e280838c4Douglas Gregor      Width = Target->getLongLongWidth();
1473bcfd1f55bfbb3e5944cd5e03d07b343e280838c4Douglas Gregor      Align = Target->getLongLongAlign();
14746f62c2abd8077bf70d2166d37e8caa426b34d8e4Chris Lattner      break;
1475ec16cb9b5a481d62a73ad47fa59034ced4d62022Chris Lattner    case BuiltinType::Int128:
1476ec16cb9b5a481d62a73ad47fa59034ced4d62022Chris Lattner    case BuiltinType::UInt128:
1477ec16cb9b5a481d62a73ad47fa59034ced4d62022Chris Lattner      Width = 128;
1478ec16cb9b5a481d62a73ad47fa59034ced4d62022Chris Lattner      Align = 128; // int128_t is 128-bit aligned on all targets.
1479ec16cb9b5a481d62a73ad47fa59034ced4d62022Chris Lattner      break;
1480aa4a99b4a62615db243f7a5c433169f2fc704420Anton Korobeynikov    case BuiltinType::Half:
1481aa4a99b4a62615db243f7a5c433169f2fc704420Anton Korobeynikov      Width = Target->getHalfWidth();
1482aa4a99b4a62615db243f7a5c433169f2fc704420Anton Korobeynikov      Align = Target->getHalfAlign();
1483aa4a99b4a62615db243f7a5c433169f2fc704420Anton Korobeynikov      break;
14846f62c2abd8077bf70d2166d37e8caa426b34d8e4Chris Lattner    case BuiltinType::Float:
1485bcfd1f55bfbb3e5944cd5e03d07b343e280838c4Douglas Gregor      Width = Target->getFloatWidth();
1486bcfd1f55bfbb3e5944cd5e03d07b343e280838c4Douglas Gregor      Align = Target->getFloatAlign();
14876f62c2abd8077bf70d2166d37e8caa426b34d8e4Chris Lattner      break;
14886f62c2abd8077bf70d2166d37e8caa426b34d8e4Chris Lattner    case BuiltinType::Double:
1489bcfd1f55bfbb3e5944cd5e03d07b343e280838c4Douglas Gregor      Width = Target->getDoubleWidth();
1490bcfd1f55bfbb3e5944cd5e03d07b343e280838c4Douglas Gregor      Align = Target->getDoubleAlign();
14916f62c2abd8077bf70d2166d37e8caa426b34d8e4Chris Lattner      break;
14926f62c2abd8077bf70d2166d37e8caa426b34d8e4Chris Lattner    case BuiltinType::LongDouble:
1493bcfd1f55bfbb3e5944cd5e03d07b343e280838c4Douglas Gregor      Width = Target->getLongDoubleWidth();
1494bcfd1f55bfbb3e5944cd5e03d07b343e280838c4Douglas Gregor      Align = Target->getLongDoubleAlign();
14956f62c2abd8077bf70d2166d37e8caa426b34d8e4Chris Lattner      break;
14966e8ed16ffef02b82995a90bdcf10ffff7d63839aSebastian Redl    case BuiltinType::NullPtr:
1497bcfd1f55bfbb3e5944cd5e03d07b343e280838c4Douglas Gregor      Width = Target->getPointerWidth(0); // C++ 3.9.1p11: sizeof(nullptr_t)
1498bcfd1f55bfbb3e5944cd5e03d07b343e280838c4Douglas Gregor      Align = Target->getPointerAlign(0); //   == sizeof(void*)
14991590d9c0fec4c710c2962e4bb71f76979b5163d3Sebastian Redl      break;
1500e04f5fc25cf49e8a5b836459d836c20dc3229a95Fariborz Jahanian    case BuiltinType::ObjCId:
1501e04f5fc25cf49e8a5b836459d836c20dc3229a95Fariborz Jahanian    case BuiltinType::ObjCClass:
1502e04f5fc25cf49e8a5b836459d836c20dc3229a95Fariborz Jahanian    case BuiltinType::ObjCSel:
1503bcfd1f55bfbb3e5944cd5e03d07b343e280838c4Douglas Gregor      Width = Target->getPointerWidth(0);
1504bcfd1f55bfbb3e5944cd5e03d07b343e280838c4Douglas Gregor      Align = Target->getPointerAlign(0);
1505e04f5fc25cf49e8a5b836459d836c20dc3229a95Fariborz Jahanian      break;
150621f18c4fda167dc5f72feddbd6a7ac1b63200a0dGuy Benyei    case BuiltinType::OCLSampler:
150721f18c4fda167dc5f72feddbd6a7ac1b63200a0dGuy Benyei      // Samplers are modeled as integers.
150821f18c4fda167dc5f72feddbd6a7ac1b63200a0dGuy Benyei      Width = Target->getIntWidth();
150921f18c4fda167dc5f72feddbd6a7ac1b63200a0dGuy Benyei      Align = Target->getIntAlign();
151021f18c4fda167dc5f72feddbd6a7ac1b63200a0dGuy Benyei      break;
1511e6b9d802fb7b16d93474c4f1c179ab36202e8a8bGuy Benyei    case BuiltinType::OCLEvent:
1512b13621d08e20ac7aa550e05896de8a57ee99c1e8Guy Benyei    case BuiltinType::OCLImage1d:
1513b13621d08e20ac7aa550e05896de8a57ee99c1e8Guy Benyei    case BuiltinType::OCLImage1dArray:
1514b13621d08e20ac7aa550e05896de8a57ee99c1e8Guy Benyei    case BuiltinType::OCLImage1dBuffer:
1515b13621d08e20ac7aa550e05896de8a57ee99c1e8Guy Benyei    case BuiltinType::OCLImage2d:
1516b13621d08e20ac7aa550e05896de8a57ee99c1e8Guy Benyei    case BuiltinType::OCLImage2dArray:
1517b13621d08e20ac7aa550e05896de8a57ee99c1e8Guy Benyei    case BuiltinType::OCLImage3d:
1518b13621d08e20ac7aa550e05896de8a57ee99c1e8Guy Benyei      // Currently these types are pointers to opaque types.
1519b13621d08e20ac7aa550e05896de8a57ee99c1e8Guy Benyei      Width = Target->getPointerWidth(0);
1520b13621d08e20ac7aa550e05896de8a57ee99c1e8Guy Benyei      Align = Target->getPointerAlign(0);
1521b13621d08e20ac7aa550e05896de8a57ee99c1e8Guy Benyei      break;
1522a7674d8a9a69f3f6fe16e70cf2a3b2b15fb7c43dChris Lattner    }
1523bfef6d7c67831a135d6ab79931f010f750a730adChris Lattner    break;
1524d1b3c2dd5bc1f3103bee6137957aa7c5f8f2f0bcSteve Naroff  case Type::ObjCObjectPointer:
1525bcfd1f55bfbb3e5944cd5e03d07b343e280838c4Douglas Gregor    Width = Target->getPointerWidth(0);
1526bcfd1f55bfbb3e5944cd5e03d07b343e280838c4Douglas Gregor    Align = Target->getPointerAlign(0);
15276f62c2abd8077bf70d2166d37e8caa426b34d8e4Chris Lattner    break;
1528485eeff9ba73376c8e01179bf1a501b1723446cbSteve Naroff  case Type::BlockPointer: {
1529207f4d8543529221932af82836016a2ef066c917Peter Collingbourne    unsigned AS = getTargetAddressSpace(
1530207f4d8543529221932af82836016a2ef066c917Peter Collingbourne        cast<BlockPointerType>(T)->getPointeeType());
1531bcfd1f55bfbb3e5944cd5e03d07b343e280838c4Douglas Gregor    Width = Target->getPointerWidth(AS);
1532bcfd1f55bfbb3e5944cd5e03d07b343e280838c4Douglas Gregor    Align = Target->getPointerAlign(AS);
1533485eeff9ba73376c8e01179bf1a501b1723446cbSteve Naroff    break;
1534485eeff9ba73376c8e01179bf1a501b1723446cbSteve Naroff  }
15355d484e8cf710207010720589d89602233de61d01Sebastian Redl  case Type::LValueReference:
15365d484e8cf710207010720589d89602233de61d01Sebastian Redl  case Type::RValueReference: {
15375d484e8cf710207010720589d89602233de61d01Sebastian Redl    // alignof and sizeof should never enter this code path here, so we go
15385d484e8cf710207010720589d89602233de61d01Sebastian Redl    // the pointer route.
1539207f4d8543529221932af82836016a2ef066c917Peter Collingbourne    unsigned AS = getTargetAddressSpace(
1540207f4d8543529221932af82836016a2ef066c917Peter Collingbourne        cast<ReferenceType>(T)->getPointeeType());
1541bcfd1f55bfbb3e5944cd5e03d07b343e280838c4Douglas Gregor    Width = Target->getPointerWidth(AS);
1542bcfd1f55bfbb3e5944cd5e03d07b343e280838c4Douglas Gregor    Align = Target->getPointerAlign(AS);
15435d484e8cf710207010720589d89602233de61d01Sebastian Redl    break;
15445d484e8cf710207010720589d89602233de61d01Sebastian Redl  }
1545f72a44330b9d9a4b2d93e9b91cfb8ab7bd4a0643Chris Lattner  case Type::Pointer: {
1546207f4d8543529221932af82836016a2ef066c917Peter Collingbourne    unsigned AS = getTargetAddressSpace(cast<PointerType>(T)->getPointeeType());
1547bcfd1f55bfbb3e5944cd5e03d07b343e280838c4Douglas Gregor    Width = Target->getPointerWidth(AS);
1548bcfd1f55bfbb3e5944cd5e03d07b343e280838c4Douglas Gregor    Align = Target->getPointerAlign(AS);
1549f72a44330b9d9a4b2d93e9b91cfb8ab7bd4a0643Chris Lattner    break;
1550f72a44330b9d9a4b2d93e9b91cfb8ab7bd4a0643Chris Lattner  }
1551f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl  case Type::MemberPointer: {
1552071cc7deffad608165b1ddd5263e8bf181861520Charles Davis    const MemberPointerType *MPT = cast<MemberPointerType>(T);
155384e9ab44af3a16f66d62590505db2036ef0aa03bReid Kleckner    llvm::tie(Width, Align) = ABI->getMemberPointerWidthAndAlign(MPT);
15541cca74ef3627a3a0ab14501d23e336548f6611b2Anders Carlsson    break;
1555f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl  }
15565d2a6303467184b1f159bb6556efc434e50e3c28Chris Lattner  case Type::Complex: {
15575d2a6303467184b1f159bb6556efc434e50e3c28Chris Lattner    // Complex types have the same alignment as their elements, but twice the
15585d2a6303467184b1f159bb6556efc434e50e3c28Chris Lattner    // size.
15591eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    std::pair<uint64_t, unsigned> EltInfo =
156098be4943e8dc4f3905629a7102668960873cf863Chris Lattner      getTypeInfo(cast<ComplexType>(T)->getElementType());
15619e9b6dc3fd413f5341fab54b681420eeb21cd169Chris Lattner    Width = EltInfo.first*2;
15625d2a6303467184b1f159bb6556efc434e50e3c28Chris Lattner    Align = EltInfo.second;
15635d2a6303467184b1f159bb6556efc434e50e3c28Chris Lattner    break;
15645d2a6303467184b1f159bb6556efc434e50e3c28Chris Lattner  }
1565c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall  case Type::ObjCObject:
1566c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall    return getTypeInfo(cast<ObjCObjectType>(T)->getBaseType().getTypePtr());
156744a3dded8080c5c9cfdad208ade8f8f7850d9a4fDevang Patel  case Type::ObjCInterface: {
15681d75118af76cae2bfc06389cde410e14bd0a19fcDaniel Dunbar    const ObjCInterfaceType *ObjCI = cast<ObjCInterfaceType>(T);
156944a3dded8080c5c9cfdad208ade8f8f7850d9a4fDevang Patel    const ASTRecordLayout &Layout = getASTObjCInterfaceLayout(ObjCI->getDecl());
1570dd76a9ab9ea675671200f94b18ce95766841952bKen Dyck    Width = toBits(Layout.getSize());
1571dac54c124e302d6f028ea5723c425b7f66fc7c71Ken Dyck    Align = toBits(Layout.getAlignment());
157244a3dded8080c5c9cfdad208ade8f8f7850d9a4fDevang Patel    break;
157344a3dded8080c5c9cfdad208ade8f8f7850d9a4fDevang Patel  }
157472564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor  case Type::Record:
157572564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor  case Type::Enum: {
15761d75118af76cae2bfc06389cde410e14bd0a19fcDaniel Dunbar    const TagType *TT = cast<TagType>(T);
15771d75118af76cae2bfc06389cde410e14bd0a19fcDaniel Dunbar
15781d75118af76cae2bfc06389cde410e14bd0a19fcDaniel Dunbar    if (TT->getDecl()->isInvalidDecl()) {
157922ce41d9fc8509da65858c75bf5b3c4dae2d8c04Douglas Gregor      Width = 8;
158022ce41d9fc8509da65858c75bf5b3c4dae2d8c04Douglas Gregor      Align = 8;
15818389eab190afef3462f6418b8d8fb70fb01c4005Chris Lattner      break;
15828389eab190afef3462f6418b8d8fb70fb01c4005Chris Lattner    }
15831eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
15841d75118af76cae2bfc06389cde410e14bd0a19fcDaniel Dunbar    if (const EnumType *ET = dyn_cast<EnumType>(TT))
15857176331b0f5cfaaa2b5aa487a6660e859e371119Chris Lattner      return getTypeInfo(ET->getDecl()->getIntegerType());
15867176331b0f5cfaaa2b5aa487a6660e859e371119Chris Lattner
15871d75118af76cae2bfc06389cde410e14bd0a19fcDaniel Dunbar    const RecordType *RT = cast<RecordType>(TT);
15887176331b0f5cfaaa2b5aa487a6660e859e371119Chris Lattner    const ASTRecordLayout &Layout = getASTRecordLayout(RT->getDecl());
1589dd76a9ab9ea675671200f94b18ce95766841952bKen Dyck    Width = toBits(Layout.getSize());
1590dac54c124e302d6f028ea5723c425b7f66fc7c71Ken Dyck    Align = toBits(Layout.getAlignment());
1591dc0d73e6495404418acf8548875aeaff07791a74Chris Lattner    break;
1592a7674d8a9a69f3f6fe16e70cf2a3b2b15fb7c43dChris Lattner  }
15937532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor
15949fcfe926432f3c3f7e9a61219e55c352fd358e45Chris Lattner  case Type::SubstTemplateTypeParm:
159549a832bd499d6f61c23655f1fac99f0dd229756eJohn McCall    return getTypeInfo(cast<SubstTemplateTypeParmType>(T)->
159649a832bd499d6f61c23655f1fac99f0dd229756eJohn McCall                       getReplacementType().getTypePtr());
159749a832bd499d6f61c23655f1fac99f0dd229756eJohn McCall
159834b41d939a1328f484511c6002ba2456db879a29Richard Smith  case Type::Auto: {
159934b41d939a1328f484511c6002ba2456db879a29Richard Smith    const AutoType *A = cast<AutoType>(T);
160034b41d939a1328f484511c6002ba2456db879a29Richard Smith    assert(A->isDeduced() && "Cannot request the size of a dependent type");
1601dc856aff4428380baa9afb5577ea04f8fb6beb13Matt Beaumont-Gay    return getTypeInfo(A->getDeducedType().getTypePtr());
160234b41d939a1328f484511c6002ba2456db879a29Richard Smith  }
160334b41d939a1328f484511c6002ba2456db879a29Richard Smith
1604075f8f1b6bed4d1b224c74f87508534cc6392ce6Abramo Bagnara  case Type::Paren:
1605075f8f1b6bed4d1b224c74f87508534cc6392ce6Abramo Bagnara    return getTypeInfo(cast<ParenType>(T)->getInnerType().getTypePtr());
1606075f8f1b6bed4d1b224c74f87508534cc6392ce6Abramo Bagnara
160718857644059c45da6776f1a288eec7b4cf3a844aDouglas Gregor  case Type::Typedef: {
1608162e1c1b487352434552147967c3dd296ebee2f7Richard Smith    const TypedefNameDecl *Typedef = cast<TypedefType>(T)->getDecl();
1609df1367af26cb2959775e9511108f12dcd2370a27Douglas Gregor    std::pair<uint64_t, unsigned> Info
1610df1367af26cb2959775e9511108f12dcd2370a27Douglas Gregor      = getTypeInfo(Typedef->getUnderlyingType().getTypePtr());
1611c1de52de64725945e5ae87e6f99ddedf161856e5Chris Lattner    // If the typedef has an aligned attribute on it, it overrides any computed
1612c1de52de64725945e5ae87e6f99ddedf161856e5Chris Lattner    // alignment we have.  This violates the GCC documentation (which says that
1613c1de52de64725945e5ae87e6f99ddedf161856e5Chris Lattner    // attribute(aligned) can only round up) but matches its implementation.
1614c1de52de64725945e5ae87e6f99ddedf161856e5Chris Lattner    if (unsigned AttrAlign = Typedef->getMaxAlignment())
1615c1de52de64725945e5ae87e6f99ddedf161856e5Chris Lattner      Align = AttrAlign;
1616c1de52de64725945e5ae87e6f99ddedf161856e5Chris Lattner    else
1617c1de52de64725945e5ae87e6f99ddedf161856e5Chris Lattner      Align = Info.second;
1618df1367af26cb2959775e9511108f12dcd2370a27Douglas Gregor    Width = Info.first;
16197532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor    break;
16207176331b0f5cfaaa2b5aa487a6660e859e371119Chris Lattner  }
162118857644059c45da6776f1a288eec7b4cf3a844aDouglas Gregor
162218857644059c45da6776f1a288eec7b4cf3a844aDouglas Gregor  case Type::TypeOfExpr:
162318857644059c45da6776f1a288eec7b4cf3a844aDouglas Gregor    return getTypeInfo(cast<TypeOfExprType>(T)->getUnderlyingExpr()->getType()
162418857644059c45da6776f1a288eec7b4cf3a844aDouglas Gregor                         .getTypePtr());
162518857644059c45da6776f1a288eec7b4cf3a844aDouglas Gregor
162618857644059c45da6776f1a288eec7b4cf3a844aDouglas Gregor  case Type::TypeOf:
162718857644059c45da6776f1a288eec7b4cf3a844aDouglas Gregor    return getTypeInfo(cast<TypeOfType>(T)->getUnderlyingType().getTypePtr());
162818857644059c45da6776f1a288eec7b4cf3a844aDouglas Gregor
1629395b475a4474f1c7574d927ad142ca0c7997cbcaAnders Carlsson  case Type::Decltype:
1630395b475a4474f1c7574d927ad142ca0c7997cbcaAnders Carlsson    return getTypeInfo(cast<DecltypeType>(T)->getUnderlyingExpr()->getType()
1631395b475a4474f1c7574d927ad142ca0c7997cbcaAnders Carlsson                        .getTypePtr());
1632395b475a4474f1c7574d927ad142ca0c7997cbcaAnders Carlsson
1633ca63c200346c0ca9e00194ec6e34a5a7b0ed9321Sean Hunt  case Type::UnaryTransform:
1634ca63c200346c0ca9e00194ec6e34a5a7b0ed9321Sean Hunt    return getTypeInfo(cast<UnaryTransformType>(T)->getUnderlyingType());
1635ca63c200346c0ca9e00194ec6e34a5a7b0ed9321Sean Hunt
1636465d41b92b2c862f3062c412a0538db65c6a2661Abramo Bagnara  case Type::Elaborated:
1637465d41b92b2c862f3062c412a0538db65c6a2661Abramo Bagnara    return getTypeInfo(cast<ElaboratedType>(T)->getNamedType().getTypePtr());
16381eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
16399d156a7b1b2771e191f2f5a45a7b7a694129463bJohn McCall  case Type::Attributed:
16409d156a7b1b2771e191f2f5a45a7b7a694129463bJohn McCall    return getTypeInfo(
16419d156a7b1b2771e191f2f5a45a7b7a694129463bJohn McCall                  cast<AttributedType>(T)->getEquivalentType().getTypePtr());
16429d156a7b1b2771e191f2f5a45a7b7a694129463bJohn McCall
16433e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith  case Type::TemplateSpecialization: {
16441eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    assert(getCanonicalType(T) != T &&
164518857644059c45da6776f1a288eec7b4cf3a844aDouglas Gregor           "Cannot request the size of a dependent type");
16463e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith    const TemplateSpecializationType *TST = cast<TemplateSpecializationType>(T);
16473e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith    // A type alias template specialization may refer to a typedef with the
16483e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith    // aligned attribute on it.
16493e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith    if (TST->isTypeAlias())
16503e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith      return getTypeInfo(TST->getAliasedType().getTypePtr());
16513e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith    else
16523e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith      return getTypeInfo(getCanonicalType(T));
16533e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith  }
16543e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith
1655b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman  case Type::Atomic: {
16569eda3abe7e183b05834947391c0cdc291f4ee0d8John McCall    // Start with the base type information.
16572be460723940f8184ec36529b6f6ddf59c04e411Eli Friedman    std::pair<uint64_t, unsigned> Info
16582be460723940f8184ec36529b6f6ddf59c04e411Eli Friedman      = getTypeInfo(cast<AtomicType>(T)->getValueType());
16592be460723940f8184ec36529b6f6ddf59c04e411Eli Friedman    Width = Info.first;
16602be460723940f8184ec36529b6f6ddf59c04e411Eli Friedman    Align = Info.second;
16619eda3abe7e183b05834947391c0cdc291f4ee0d8John McCall
16629eda3abe7e183b05834947391c0cdc291f4ee0d8John McCall    // If the size of the type doesn't exceed the platform's max
16639eda3abe7e183b05834947391c0cdc291f4ee0d8John McCall    // atomic promotion width, make the size and alignment more
16649eda3abe7e183b05834947391c0cdc291f4ee0d8John McCall    // favorable to atomic operations:
16659eda3abe7e183b05834947391c0cdc291f4ee0d8John McCall    if (Width != 0 && Width <= Target->getMaxAtomicPromoteWidth()) {
16669eda3abe7e183b05834947391c0cdc291f4ee0d8John McCall      // Round the size up to a power of 2.
16679eda3abe7e183b05834947391c0cdc291f4ee0d8John McCall      if (!llvm::isPowerOf2_64(Width))
16689eda3abe7e183b05834947391c0cdc291f4ee0d8John McCall        Width = llvm::NextPowerOf2(Width);
16699eda3abe7e183b05834947391c0cdc291f4ee0d8John McCall
16709eda3abe7e183b05834947391c0cdc291f4ee0d8John McCall      // Set the alignment equal to the size.
16712be460723940f8184ec36529b6f6ddf59c04e411Eli Friedman      Align = static_cast<unsigned>(Width);
16722be460723940f8184ec36529b6f6ddf59c04e411Eli Friedman    }
1673b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman  }
1674b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman
167518857644059c45da6776f1a288eec7b4cf3a844aDouglas Gregor  }
16761eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
16772be460723940f8184ec36529b6f6ddf59c04e411Eli Friedman  assert(llvm::isPowerOf2_32(Align) && "Alignment must be power of 2");
16789e9b6dc3fd413f5341fab54b681420eeb21cd169Chris Lattner  return std::make_pair(Width, Align);
1679a7674d8a9a69f3f6fe16e70cf2a3b2b15fb7c43dChris Lattner}
1680a7674d8a9a69f3f6fe16e70cf2a3b2b15fb7c43dChris Lattner
1681eb6f5dc86531f794ba7746a2da4d28e37cf5da7eKen Dyck/// toCharUnitsFromBits - Convert a size in bits to a size in characters.
1682eb6f5dc86531f794ba7746a2da4d28e37cf5da7eKen DyckCharUnits ASTContext::toCharUnitsFromBits(int64_t BitSize) const {
1683eb6f5dc86531f794ba7746a2da4d28e37cf5da7eKen Dyck  return CharUnits::fromQuantity(BitSize / getCharWidth());
1684eb6f5dc86531f794ba7746a2da4d28e37cf5da7eKen Dyck}
1685eb6f5dc86531f794ba7746a2da4d28e37cf5da7eKen Dyck
1686dd76a9ab9ea675671200f94b18ce95766841952bKen Dyck/// toBits - Convert a size in characters to a size in characters.
1687dd76a9ab9ea675671200f94b18ce95766841952bKen Dyckint64_t ASTContext::toBits(CharUnits CharSize) const {
1688dd76a9ab9ea675671200f94b18ce95766841952bKen Dyck  return CharSize.getQuantity() * getCharWidth();
1689dd76a9ab9ea675671200f94b18ce95766841952bKen Dyck}
1690dd76a9ab9ea675671200f94b18ce95766841952bKen Dyck
1691bdc601b196c48d4cd56a5ceb45d41ae4e87371abKen Dyck/// getTypeSizeInChars - Return the size of the specified type, in characters.
1692bdc601b196c48d4cd56a5ceb45d41ae4e87371abKen Dyck/// This method does not work on incomplete types.
16934ba2a17694148e16eaa8d3917f657ffcd3667be4Jay FoadCharUnits ASTContext::getTypeSizeInChars(QualType T) const {
1694eb6f5dc86531f794ba7746a2da4d28e37cf5da7eKen Dyck  return toCharUnitsFromBits(getTypeSize(T));
1695bdc601b196c48d4cd56a5ceb45d41ae4e87371abKen Dyck}
16964ba2a17694148e16eaa8d3917f657ffcd3667be4Jay FoadCharUnits ASTContext::getTypeSizeInChars(const Type *T) const {
1697eb6f5dc86531f794ba7746a2da4d28e37cf5da7eKen Dyck  return toCharUnitsFromBits(getTypeSize(T));
1698bdc601b196c48d4cd56a5ceb45d41ae4e87371abKen Dyck}
1699bdc601b196c48d4cd56a5ceb45d41ae4e87371abKen Dyck
170016e20cce43385001f33f8e3f90ee345609c805d1Ken Dyck/// getTypeAlignInChars - Return the ABI-specified alignment of a type, in
170186fa4311c8a330957ff5b765fbb0a7750ecd38c9Ken Dyck/// characters. This method does not work on incomplete types.
17024ba2a17694148e16eaa8d3917f657ffcd3667be4Jay FoadCharUnits ASTContext::getTypeAlignInChars(QualType T) const {
1703eb6f5dc86531f794ba7746a2da4d28e37cf5da7eKen Dyck  return toCharUnitsFromBits(getTypeAlign(T));
170486fa4311c8a330957ff5b765fbb0a7750ecd38c9Ken Dyck}
17054ba2a17694148e16eaa8d3917f657ffcd3667be4Jay FoadCharUnits ASTContext::getTypeAlignInChars(const Type *T) const {
1706eb6f5dc86531f794ba7746a2da4d28e37cf5da7eKen Dyck  return toCharUnitsFromBits(getTypeAlign(T));
170786fa4311c8a330957ff5b765fbb0a7750ecd38c9Ken Dyck}
170886fa4311c8a330957ff5b765fbb0a7750ecd38c9Ken Dyck
170934ebde404dc17d89487b07e6daaf1b47d5dfee39Chris Lattner/// getPreferredTypeAlign - Return the "preferred" alignment of the specified
171034ebde404dc17d89487b07e6daaf1b47d5dfee39Chris Lattner/// type for the current target in bits.  This can be different than the ABI
171134ebde404dc17d89487b07e6daaf1b47d5dfee39Chris Lattner/// alignment in cases where it is beneficial for performance to overalign
171234ebde404dc17d89487b07e6daaf1b47d5dfee39Chris Lattner/// a data type.
17134ba2a17694148e16eaa8d3917f657ffcd3667be4Jay Foadunsigned ASTContext::getPreferredTypeAlign(const Type *T) const {
171434ebde404dc17d89487b07e6daaf1b47d5dfee39Chris Lattner  unsigned ABIAlign = getTypeAlign(T);
17151eed60297ef4701b899c6a3b9680bf08f3403422Eli Friedman
17161eed60297ef4701b899c6a3b9680bf08f3403422Eli Friedman  // Double and long long should be naturally aligned if possible.
1717183700f494ec9b6701b6efe82bcb25f4c79ba561John McCall  if (const ComplexType* CT = T->getAs<ComplexType>())
17181eed60297ef4701b899c6a3b9680bf08f3403422Eli Friedman    T = CT->getElementType().getTypePtr();
17191eed60297ef4701b899c6a3b9680bf08f3403422Eli Friedman  if (T->isSpecificBuiltinType(BuiltinType::Double) ||
1720cde7a1dc68af2eb063a039b5a31c3b7dd92b1aa9Chad Rosier      T->isSpecificBuiltinType(BuiltinType::LongLong) ||
1721cde7a1dc68af2eb063a039b5a31c3b7dd92b1aa9Chad Rosier      T->isSpecificBuiltinType(BuiltinType::ULongLong))
17221eed60297ef4701b899c6a3b9680bf08f3403422Eli Friedman    return std::max(ABIAlign, (unsigned)getTypeSize(T));
17231eed60297ef4701b899c6a3b9680bf08f3403422Eli Friedman
172434ebde404dc17d89487b07e6daaf1b47d5dfee39Chris Lattner  return ABIAlign;
172534ebde404dc17d89487b07e6daaf1b47d5dfee39Chris Lattner}
172634ebde404dc17d89487b07e6daaf1b47d5dfee39Chris Lattner
17272c18bb7c9fca66c30b6eabbdcbc6399d24a54fa9Fariborz Jahanian/// DeepCollectObjCIvars -
17282c18bb7c9fca66c30b6eabbdcbc6399d24a54fa9Fariborz Jahanian/// This routine first collects all declared, but not synthesized, ivars in
17292c18bb7c9fca66c30b6eabbdcbc6399d24a54fa9Fariborz Jahanian/// super class and then collects all ivars, including those synthesized for
17302c18bb7c9fca66c30b6eabbdcbc6399d24a54fa9Fariborz Jahanian/// current class. This routine is used for implementation of current class
17312c18bb7c9fca66c30b6eabbdcbc6399d24a54fa9Fariborz Jahanian/// when all ivars, declared and synthesized are known.
17329820074dd47d37681085e964cd3392ac0b3e67b9Fariborz Jahanian///
17332c18bb7c9fca66c30b6eabbdcbc6399d24a54fa9Fariborz Jahanianvoid ASTContext::DeepCollectObjCIvars(const ObjCInterfaceDecl *OI,
17342c18bb7c9fca66c30b6eabbdcbc6399d24a54fa9Fariborz Jahanian                                      bool leafClass,
1735db8264e4c5ffd7af6fbad4ca4306bd382bb02691Jordy Rose                            SmallVectorImpl<const ObjCIvarDecl*> &Ivars) const {
17362c18bb7c9fca66c30b6eabbdcbc6399d24a54fa9Fariborz Jahanian  if (const ObjCInterfaceDecl *SuperClass = OI->getSuperClass())
17372c18bb7c9fca66c30b6eabbdcbc6399d24a54fa9Fariborz Jahanian    DeepCollectObjCIvars(SuperClass, false, Ivars);
17382c18bb7c9fca66c30b6eabbdcbc6399d24a54fa9Fariborz Jahanian  if (!leafClass) {
17392c18bb7c9fca66c30b6eabbdcbc6399d24a54fa9Fariborz Jahanian    for (ObjCInterfaceDecl::ivar_iterator I = OI->ivar_begin(),
17402c18bb7c9fca66c30b6eabbdcbc6399d24a54fa9Fariborz Jahanian         E = OI->ivar_end(); I != E; ++I)
1741581deb3da481053c4993c7600f97acf7768caac5David Blaikie      Ivars.push_back(*I);
17423060178ad9df29789505c1e6debcfc80a3a13587Chad Rosier  } else {
1743bf9eb88792e022e54a658657bf22e1925948e384Fariborz Jahanian    ObjCInterfaceDecl *IDecl = const_cast<ObjCInterfaceDecl *>(OI);
1744db8264e4c5ffd7af6fbad4ca4306bd382bb02691Jordy Rose    for (const ObjCIvarDecl *Iv = IDecl->all_declared_ivar_begin(); Iv;
1745bf9eb88792e022e54a658657bf22e1925948e384Fariborz Jahanian         Iv= Iv->getNextIvar())
1746bf9eb88792e022e54a658657bf22e1925948e384Fariborz Jahanian      Ivars.push_back(Iv);
1747bf9eb88792e022e54a658657bf22e1925948e384Fariborz Jahanian  }
17489820074dd47d37681085e964cd3392ac0b3e67b9Fariborz Jahanian}
17499820074dd47d37681085e964cd3392ac0b3e67b9Fariborz Jahanian
1750e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanian/// CollectInheritedProtocols - Collect all protocols in current class and
1751e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanian/// those inherited by it.
1752e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanianvoid ASTContext::CollectInheritedProtocols(const Decl *CDecl,
1753432a8893f7e30d141d7f279bd00b741a3cdac81fFariborz Jahanian                          llvm::SmallPtrSet<ObjCProtocolDecl*, 8> &Protocols) {
1754e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanian  if (const ObjCInterfaceDecl *OI = dyn_cast<ObjCInterfaceDecl>(CDecl)) {
175553b9441b5a81a24fa1f66f3f6416f1e36baa9c2fTed Kremenek    // We can use protocol_iterator here instead of
175653b9441b5a81a24fa1f66f3f6416f1e36baa9c2fTed Kremenek    // all_referenced_protocol_iterator since we are walking all categories.
175753b9441b5a81a24fa1f66f3f6416f1e36baa9c2fTed Kremenek    for (ObjCInterfaceDecl::all_protocol_iterator P = OI->all_referenced_protocol_begin(),
175853b9441b5a81a24fa1f66f3f6416f1e36baa9c2fTed Kremenek         PE = OI->all_referenced_protocol_end(); P != PE; ++P) {
1759e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanian      ObjCProtocolDecl *Proto = (*P);
17603fc73ee0c613715ebce78e30b4d050ea715a007dDouglas Gregor      Protocols.insert(Proto->getCanonicalDecl());
1761e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanian      for (ObjCProtocolDecl::protocol_iterator P = Proto->protocol_begin(),
1762b2f812165676230bce5d0215e49a4749c451ca9cFariborz Jahanian           PE = Proto->protocol_end(); P != PE; ++P) {
17633fc73ee0c613715ebce78e30b4d050ea715a007dDouglas Gregor        Protocols.insert((*P)->getCanonicalDecl());
1764e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanian        CollectInheritedProtocols(*P, Protocols);
1765e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanian      }
1766b2f812165676230bce5d0215e49a4749c451ca9cFariborz Jahanian    }
1767e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanian
1768e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanian    // Categories of this Interface.
1769d329724745b49f894b768d47275b7c2713106e89Douglas Gregor    for (ObjCInterfaceDecl::visible_categories_iterator
1770d329724745b49f894b768d47275b7c2713106e89Douglas Gregor           Cat = OI->visible_categories_begin(),
1771d329724745b49f894b768d47275b7c2713106e89Douglas Gregor           CatEnd = OI->visible_categories_end();
1772d329724745b49f894b768d47275b7c2713106e89Douglas Gregor         Cat != CatEnd; ++Cat) {
1773d329724745b49f894b768d47275b7c2713106e89Douglas Gregor      CollectInheritedProtocols(*Cat, Protocols);
1774d329724745b49f894b768d47275b7c2713106e89Douglas Gregor    }
1775d329724745b49f894b768d47275b7c2713106e89Douglas Gregor
1776e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanian    if (ObjCInterfaceDecl *SD = OI->getSuperClass())
1777e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanian      while (SD) {
1778e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanian        CollectInheritedProtocols(SD, Protocols);
1779e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanian        SD = SD->getSuperClass();
1780e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanian      }
1781b170ca5f4a8397c10e52050ff5df6885a3e6eca9Benjamin Kramer  } else if (const ObjCCategoryDecl *OC = dyn_cast<ObjCCategoryDecl>(CDecl)) {
178253b9441b5a81a24fa1f66f3f6416f1e36baa9c2fTed Kremenek    for (ObjCCategoryDecl::protocol_iterator P = OC->protocol_begin(),
1783e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanian         PE = OC->protocol_end(); P != PE; ++P) {
1784e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanian      ObjCProtocolDecl *Proto = (*P);
17853fc73ee0c613715ebce78e30b4d050ea715a007dDouglas Gregor      Protocols.insert(Proto->getCanonicalDecl());
1786e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanian      for (ObjCProtocolDecl::protocol_iterator P = Proto->protocol_begin(),
1787e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanian           PE = Proto->protocol_end(); P != PE; ++P)
1788e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanian        CollectInheritedProtocols(*P, Protocols);
1789e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanian    }
1790b170ca5f4a8397c10e52050ff5df6885a3e6eca9Benjamin Kramer  } else if (const ObjCProtocolDecl *OP = dyn_cast<ObjCProtocolDecl>(CDecl)) {
1791e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanian    for (ObjCProtocolDecl::protocol_iterator P = OP->protocol_begin(),
1792e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanian         PE = OP->protocol_end(); P != PE; ++P) {
1793e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanian      ObjCProtocolDecl *Proto = (*P);
17943fc73ee0c613715ebce78e30b4d050ea715a007dDouglas Gregor      Protocols.insert(Proto->getCanonicalDecl());
1795e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanian      for (ObjCProtocolDecl::protocol_iterator P = Proto->protocol_begin(),
1796e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanian           PE = Proto->protocol_end(); P != PE; ++P)
1797e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanian        CollectInheritedProtocols(*P, Protocols);
1798e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanian    }
1799e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanian  }
1800e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanian}
1801e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanian
18024ba2a17694148e16eaa8d3917f657ffcd3667be4Jay Foadunsigned ASTContext::CountNonClassIvars(const ObjCInterfaceDecl *OI) const {
18033bfacdf6f0706987c1b33c625cd68fb880881c22Fariborz Jahanian  unsigned count = 0;
18043bfacdf6f0706987c1b33c625cd68fb880881c22Fariborz Jahanian  // Count ivars declared in class extension.
1805d329724745b49f894b768d47275b7c2713106e89Douglas Gregor  for (ObjCInterfaceDecl::known_extensions_iterator
1806d329724745b49f894b768d47275b7c2713106e89Douglas Gregor         Ext = OI->known_extensions_begin(),
1807d329724745b49f894b768d47275b7c2713106e89Douglas Gregor         ExtEnd = OI->known_extensions_end();
1808d329724745b49f894b768d47275b7c2713106e89Douglas Gregor       Ext != ExtEnd; ++Ext) {
1809d329724745b49f894b768d47275b7c2713106e89Douglas Gregor    count += Ext->ivar_size();
1810d329724745b49f894b768d47275b7c2713106e89Douglas Gregor  }
1811d329724745b49f894b768d47275b7c2713106e89Douglas Gregor
18123bfacdf6f0706987c1b33c625cd68fb880881c22Fariborz Jahanian  // Count ivar defined in this class's implementation.  This
18133bfacdf6f0706987c1b33c625cd68fb880881c22Fariborz Jahanian  // includes synthesized ivars.
18143bfacdf6f0706987c1b33c625cd68fb880881c22Fariborz Jahanian  if (ObjCImplementationDecl *ImplDecl = OI->getImplementation())
1815b170ca5f4a8397c10e52050ff5df6885a3e6eca9Benjamin Kramer    count += ImplDecl->ivar_size();
1816b170ca5f4a8397c10e52050ff5df6885a3e6eca9Benjamin Kramer
18178e6ac1d80055fa37b9b84029c7e751624ba7f84cFariborz Jahanian  return count;
18188e6ac1d80055fa37b9b84029c7e751624ba7f84cFariborz Jahanian}
18198e6ac1d80055fa37b9b84029c7e751624ba7f84cFariborz Jahanian
18208deabc133c121f6c5561d0b2171a41cb2c29b2ceArgyrios Kyrtzidisbool ASTContext::isSentinelNullExpr(const Expr *E) {
18218deabc133c121f6c5561d0b2171a41cb2c29b2ceArgyrios Kyrtzidis  if (!E)
18228deabc133c121f6c5561d0b2171a41cb2c29b2ceArgyrios Kyrtzidis    return false;
18238deabc133c121f6c5561d0b2171a41cb2c29b2ceArgyrios Kyrtzidis
18248deabc133c121f6c5561d0b2171a41cb2c29b2ceArgyrios Kyrtzidis  // nullptr_t is always treated as null.
18258deabc133c121f6c5561d0b2171a41cb2c29b2ceArgyrios Kyrtzidis  if (E->getType()->isNullPtrType()) return true;
18268deabc133c121f6c5561d0b2171a41cb2c29b2ceArgyrios Kyrtzidis
18278deabc133c121f6c5561d0b2171a41cb2c29b2ceArgyrios Kyrtzidis  if (E->getType()->isAnyPointerType() &&
18288deabc133c121f6c5561d0b2171a41cb2c29b2ceArgyrios Kyrtzidis      E->IgnoreParenCasts()->isNullPointerConstant(*this,
18298deabc133c121f6c5561d0b2171a41cb2c29b2ceArgyrios Kyrtzidis                                                Expr::NPC_ValueDependentIsNull))
18308deabc133c121f6c5561d0b2171a41cb2c29b2ceArgyrios Kyrtzidis    return true;
18318deabc133c121f6c5561d0b2171a41cb2c29b2ceArgyrios Kyrtzidis
18328deabc133c121f6c5561d0b2171a41cb2c29b2ceArgyrios Kyrtzidis  // Unfortunately, __null has type 'int'.
18338deabc133c121f6c5561d0b2171a41cb2c29b2ceArgyrios Kyrtzidis  if (isa<GNUNullExpr>(E)) return true;
18348deabc133c121f6c5561d0b2171a41cb2c29b2ceArgyrios Kyrtzidis
18358deabc133c121f6c5561d0b2171a41cb2c29b2ceArgyrios Kyrtzidis  return false;
18368deabc133c121f6c5561d0b2171a41cb2c29b2ceArgyrios Kyrtzidis}
18378deabc133c121f6c5561d0b2171a41cb2c29b2ceArgyrios Kyrtzidis
18388a1d722f13df383600f36d77f842957c8adb5f1bArgyrios Kyrtzidis/// \brief Get the implementation of ObjCInterfaceDecl,or NULL if none exists.
18398a1d722f13df383600f36d77f842957c8adb5f1bArgyrios KyrtzidisObjCImplementationDecl *ASTContext::getObjCImplementation(ObjCInterfaceDecl *D) {
18408a1d722f13df383600f36d77f842957c8adb5f1bArgyrios Kyrtzidis  llvm::DenseMap<ObjCContainerDecl*, ObjCImplDecl*>::iterator
18418a1d722f13df383600f36d77f842957c8adb5f1bArgyrios Kyrtzidis    I = ObjCImpls.find(D);
18428a1d722f13df383600f36d77f842957c8adb5f1bArgyrios Kyrtzidis  if (I != ObjCImpls.end())
18438a1d722f13df383600f36d77f842957c8adb5f1bArgyrios Kyrtzidis    return cast<ObjCImplementationDecl>(I->second);
18448a1d722f13df383600f36d77f842957c8adb5f1bArgyrios Kyrtzidis  return 0;
18458a1d722f13df383600f36d77f842957c8adb5f1bArgyrios Kyrtzidis}
18468a1d722f13df383600f36d77f842957c8adb5f1bArgyrios Kyrtzidis/// \brief Get the implementation of ObjCCategoryDecl, or NULL if none exists.
18478a1d722f13df383600f36d77f842957c8adb5f1bArgyrios KyrtzidisObjCCategoryImplDecl *ASTContext::getObjCImplementation(ObjCCategoryDecl *D) {
18488a1d722f13df383600f36d77f842957c8adb5f1bArgyrios Kyrtzidis  llvm::DenseMap<ObjCContainerDecl*, ObjCImplDecl*>::iterator
18498a1d722f13df383600f36d77f842957c8adb5f1bArgyrios Kyrtzidis    I = ObjCImpls.find(D);
18508a1d722f13df383600f36d77f842957c8adb5f1bArgyrios Kyrtzidis  if (I != ObjCImpls.end())
18518a1d722f13df383600f36d77f842957c8adb5f1bArgyrios Kyrtzidis    return cast<ObjCCategoryImplDecl>(I->second);
18528a1d722f13df383600f36d77f842957c8adb5f1bArgyrios Kyrtzidis  return 0;
18538a1d722f13df383600f36d77f842957c8adb5f1bArgyrios Kyrtzidis}
18548a1d722f13df383600f36d77f842957c8adb5f1bArgyrios Kyrtzidis
18558a1d722f13df383600f36d77f842957c8adb5f1bArgyrios Kyrtzidis/// \brief Set the implementation of ObjCInterfaceDecl.
18568a1d722f13df383600f36d77f842957c8adb5f1bArgyrios Kyrtzidisvoid ASTContext::setObjCImplementation(ObjCInterfaceDecl *IFaceD,
18578a1d722f13df383600f36d77f842957c8adb5f1bArgyrios Kyrtzidis                           ObjCImplementationDecl *ImplD) {
18588a1d722f13df383600f36d77f842957c8adb5f1bArgyrios Kyrtzidis  assert(IFaceD && ImplD && "Passed null params");
18598a1d722f13df383600f36d77f842957c8adb5f1bArgyrios Kyrtzidis  ObjCImpls[IFaceD] = ImplD;
18608a1d722f13df383600f36d77f842957c8adb5f1bArgyrios Kyrtzidis}
18618a1d722f13df383600f36d77f842957c8adb5f1bArgyrios Kyrtzidis/// \brief Set the implementation of ObjCCategoryDecl.
18628a1d722f13df383600f36d77f842957c8adb5f1bArgyrios Kyrtzidisvoid ASTContext::setObjCImplementation(ObjCCategoryDecl *CatD,
18638a1d722f13df383600f36d77f842957c8adb5f1bArgyrios Kyrtzidis                           ObjCCategoryImplDecl *ImplD) {
18648a1d722f13df383600f36d77f842957c8adb5f1bArgyrios Kyrtzidis  assert(CatD && ImplD && "Passed null params");
18658a1d722f13df383600f36d77f842957c8adb5f1bArgyrios Kyrtzidis  ObjCImpls[CatD] = ImplD;
18668a1d722f13df383600f36d77f842957c8adb5f1bArgyrios Kyrtzidis}
18678a1d722f13df383600f36d77f842957c8adb5f1bArgyrios Kyrtzidis
1868b35cc2d46c4188a0b2d094b3104ce69092c34802Dmitri Gribenkoconst ObjCInterfaceDecl *ASTContext::getObjContainingInterface(
1869b35cc2d46c4188a0b2d094b3104ce69092c34802Dmitri Gribenko                                              const NamedDecl *ND) const {
1870b35cc2d46c4188a0b2d094b3104ce69092c34802Dmitri Gribenko  if (const ObjCInterfaceDecl *ID =
1871b35cc2d46c4188a0b2d094b3104ce69092c34802Dmitri Gribenko          dyn_cast<ObjCInterfaceDecl>(ND->getDeclContext()))
187287ec9c257c99b4136af6c7f5be5a2d486906ba84Argyrios Kyrtzidis    return ID;
1873b35cc2d46c4188a0b2d094b3104ce69092c34802Dmitri Gribenko  if (const ObjCCategoryDecl *CD =
1874b35cc2d46c4188a0b2d094b3104ce69092c34802Dmitri Gribenko          dyn_cast<ObjCCategoryDecl>(ND->getDeclContext()))
187587ec9c257c99b4136af6c7f5be5a2d486906ba84Argyrios Kyrtzidis    return CD->getClassInterface();
1876b35cc2d46c4188a0b2d094b3104ce69092c34802Dmitri Gribenko  if (const ObjCImplDecl *IMD =
1877b35cc2d46c4188a0b2d094b3104ce69092c34802Dmitri Gribenko          dyn_cast<ObjCImplDecl>(ND->getDeclContext()))
187887ec9c257c99b4136af6c7f5be5a2d486906ba84Argyrios Kyrtzidis    return IMD->getClassInterface();
187987ec9c257c99b4136af6c7f5be5a2d486906ba84Argyrios Kyrtzidis
188087ec9c257c99b4136af6c7f5be5a2d486906ba84Argyrios Kyrtzidis  return 0;
188187ec9c257c99b4136af6c7f5be5a2d486906ba84Argyrios Kyrtzidis}
188287ec9c257c99b4136af6c7f5be5a2d486906ba84Argyrios Kyrtzidis
18831ceee5c42d5c410217f67d384eecc6ea4a2bba9bFariborz Jahanian/// \brief Get the copy initialization expression of VarDecl,or NULL if
18841ceee5c42d5c410217f67d384eecc6ea4a2bba9bFariborz Jahanian/// none exists.
1885830937bc1100fba7682f7c32c40512085870f50cFariborz JahanianExpr *ASTContext::getBlockVarCopyInits(const VarDecl*VD) {
1886d016ec204c54d766c4561d43548551ffde0e6b43Fariborz Jahanian  assert(VD && "Passed null params");
1887d016ec204c54d766c4561d43548551ffde0e6b43Fariborz Jahanian  assert(VD->hasAttr<BlocksAttr>() &&
1888d016ec204c54d766c4561d43548551ffde0e6b43Fariborz Jahanian         "getBlockVarCopyInits - not __block var");
1889830937bc1100fba7682f7c32c40512085870f50cFariborz Jahanian  llvm::DenseMap<const VarDecl*, Expr*>::iterator
1890d016ec204c54d766c4561d43548551ffde0e6b43Fariborz Jahanian    I = BlockVarCopyInits.find(VD);
18911ceee5c42d5c410217f67d384eecc6ea4a2bba9bFariborz Jahanian  return (I != BlockVarCopyInits.end()) ? cast<Expr>(I->second) : 0;
18921ceee5c42d5c410217f67d384eecc6ea4a2bba9bFariborz Jahanian}
18931ceee5c42d5c410217f67d384eecc6ea4a2bba9bFariborz Jahanian
18941ceee5c42d5c410217f67d384eecc6ea4a2bba9bFariborz Jahanian/// \brief Set the copy inialization expression of a block var decl.
18951ceee5c42d5c410217f67d384eecc6ea4a2bba9bFariborz Jahanianvoid ASTContext::setBlockVarCopyInits(VarDecl*VD, Expr* Init) {
18961ceee5c42d5c410217f67d384eecc6ea4a2bba9bFariborz Jahanian  assert(VD && Init && "Passed null params");
1897d016ec204c54d766c4561d43548551ffde0e6b43Fariborz Jahanian  assert(VD->hasAttr<BlocksAttr>() &&
1898d016ec204c54d766c4561d43548551ffde0e6b43Fariborz Jahanian         "setBlockVarCopyInits - not __block var");
18991ceee5c42d5c410217f67d384eecc6ea4a2bba9bFariborz Jahanian  BlockVarCopyInits[VD] = Init;
19001ceee5c42d5c410217f67d384eecc6ea4a2bba9bFariborz Jahanian}
19011ceee5c42d5c410217f67d384eecc6ea4a2bba9bFariborz Jahanian
1902a93c934af4fbf97cbe8e649d82e68ccacfe57c95John McCallTypeSourceInfo *ASTContext::CreateTypeSourceInfo(QualType T,
19034ba2a17694148e16eaa8d3917f657ffcd3667be4Jay Foad                                                 unsigned DataSize) const {
1904109de5ead1dfcb3bc985cddb8cb3ed5bcecad88dJohn McCall  if (!DataSize)
1905109de5ead1dfcb3bc985cddb8cb3ed5bcecad88dJohn McCall    DataSize = TypeLoc::getFullDataSizeForType(T);
1906109de5ead1dfcb3bc985cddb8cb3ed5bcecad88dJohn McCall  else
1907109de5ead1dfcb3bc985cddb8cb3ed5bcecad88dJohn McCall    assert(DataSize == TypeLoc::getFullDataSizeForType(T) &&
1908a93c934af4fbf97cbe8e649d82e68ccacfe57c95John McCall           "incorrect data size provided to CreateTypeSourceInfo!");
1909109de5ead1dfcb3bc985cddb8cb3ed5bcecad88dJohn McCall
1910a93c934af4fbf97cbe8e649d82e68ccacfe57c95John McCall  TypeSourceInfo *TInfo =
1911a93c934af4fbf97cbe8e649d82e68ccacfe57c95John McCall    (TypeSourceInfo*)BumpAlloc.Allocate(sizeof(TypeSourceInfo) + DataSize, 8);
1912a93c934af4fbf97cbe8e649d82e68ccacfe57c95John McCall  new (TInfo) TypeSourceInfo(T);
1913a93c934af4fbf97cbe8e649d82e68ccacfe57c95John McCall  return TInfo;
1914b17166c8077cd900cca83a895c43b30ea6660598Argyrios Kyrtzidis}
1915b17166c8077cd900cca83a895c43b30ea6660598Argyrios Kyrtzidis
1916a93c934af4fbf97cbe8e649d82e68ccacfe57c95John McCallTypeSourceInfo *ASTContext::getTrivialTypeSourceInfo(QualType T,
19176952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor                                                     SourceLocation L) const {
1918a93c934af4fbf97cbe8e649d82e68ccacfe57c95John McCall  TypeSourceInfo *DI = CreateTypeSourceInfo(T);
1919c21c7e9c2cded68f91be15be6847c9649242dc17Douglas Gregor  DI->getTypeLoc().initialize(const_cast<ASTContext &>(*this), L);
1920a4eb74d4dfe126c686dc708fec444c85ffb73b47John McCall  return DI;
1921a4eb74d4dfe126c686dc708fec444c85ffb73b47John McCall}
1922a4eb74d4dfe126c686dc708fec444c85ffb73b47John McCall
1923b2dbbb99e12806eaaf53b7ccabc32f42b5719443Daniel Dunbarconst ASTRecordLayout &
19244ba2a17694148e16eaa8d3917f657ffcd3667be4Jay FoadASTContext::getASTObjCInterfaceLayout(const ObjCInterfaceDecl *D) const {
1925b2dbbb99e12806eaaf53b7ccabc32f42b5719443Daniel Dunbar  return getObjCLayout(D, 0);
1926b2dbbb99e12806eaaf53b7ccabc32f42b5719443Daniel Dunbar}
1927b2dbbb99e12806eaaf53b7ccabc32f42b5719443Daniel Dunbar
1928b2dbbb99e12806eaaf53b7ccabc32f42b5719443Daniel Dunbarconst ASTRecordLayout &
19294ba2a17694148e16eaa8d3917f657ffcd3667be4Jay FoadASTContext::getASTObjCImplementationLayout(
19304ba2a17694148e16eaa8d3917f657ffcd3667be4Jay Foad                                        const ObjCImplementationDecl *D) const {
1931b2dbbb99e12806eaaf53b7ccabc32f42b5719443Daniel Dunbar  return getObjCLayout(D->getClassInterface(), D);
1932b2dbbb99e12806eaaf53b7ccabc32f42b5719443Daniel Dunbar}
1933b2dbbb99e12806eaaf53b7ccabc32f42b5719443Daniel Dunbar
1934a7674d8a9a69f3f6fe16e70cf2a3b2b15fb7c43dChris Lattner//===----------------------------------------------------------------------===//
1935a7674d8a9a69f3f6fe16e70cf2a3b2b15fb7c43dChris Lattner//                   Type creation/memoization methods
1936a7674d8a9a69f3f6fe16e70cf2a3b2b15fb7c43dChris Lattner//===----------------------------------------------------------------------===//
1937a7674d8a9a69f3f6fe16e70cf2a3b2b15fb7c43dChris Lattner
19384ba2a17694148e16eaa8d3917f657ffcd3667be4Jay FoadQualType
19393b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCallASTContext::getExtQualType(const Type *baseType, Qualifiers quals) const {
19403b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall  unsigned fastQuals = quals.getFastQualifiers();
19413b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall  quals.removeFastQualifiers();
19420953e767ff7817f97b3ab20896b229891eeff45bJohn McCall
19430953e767ff7817f97b3ab20896b229891eeff45bJohn McCall  // Check if we've already instantiated this type.
19440953e767ff7817f97b3ab20896b229891eeff45bJohn McCall  llvm::FoldingSetNodeID ID;
19453b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall  ExtQuals::Profile(ID, baseType, quals);
19463b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall  void *insertPos = 0;
19473b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall  if (ExtQuals *eq = ExtQualNodes.FindNodeOrInsertPos(ID, insertPos)) {
19483b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall    assert(eq->getQualifiers() == quals);
19493b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall    return QualType(eq, fastQuals);
19500953e767ff7817f97b3ab20896b229891eeff45bJohn McCall  }
19510953e767ff7817f97b3ab20896b229891eeff45bJohn McCall
19523b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall  // If the base type is not canonical, make the appropriate canonical type.
19533b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall  QualType canon;
19543b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall  if (!baseType->isCanonicalUnqualified()) {
19553b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall    SplitQualType canonSplit = baseType->getCanonicalTypeInternal().split();
1956200fa53fd420aa8369586f569dbece04930ad6a3John McCall    canonSplit.Quals.addConsistentQualifiers(quals);
1957200fa53fd420aa8369586f569dbece04930ad6a3John McCall    canon = getExtQualType(canonSplit.Ty, canonSplit.Quals);
19583b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall
19593b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall    // Re-find the insert position.
19603b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall    (void) ExtQualNodes.FindNodeOrInsertPos(ID, insertPos);
19613b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall  }
19623b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall
19633b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall  ExtQuals *eq = new (*this, TypeAlignment) ExtQuals(baseType, canon, quals);
19643b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall  ExtQualNodes.InsertNode(eq, insertPos);
19653b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall  return QualType(eq, fastQuals);
19660953e767ff7817f97b3ab20896b229891eeff45bJohn McCall}
19670953e767ff7817f97b3ab20896b229891eeff45bJohn McCall
19684ba2a17694148e16eaa8d3917f657ffcd3667be4Jay FoadQualType
19694ba2a17694148e16eaa8d3917f657ffcd3667be4Jay FoadASTContext::getAddrSpaceQualType(QualType T, unsigned AddressSpace) const {
1970f52ab250ff92bc51a9ac9a8e19bd43b63a5f844fChris Lattner  QualType CanT = getCanonicalType(T);
1971f52ab250ff92bc51a9ac9a8e19bd43b63a5f844fChris Lattner  if (CanT.getAddressSpace() == AddressSpace)
1972f46699ce225811d8d9dbab9d00189a0e54469457Chris Lattner    return T;
1973b7d2553edd2532d29b98b9e76bcf6a62bc48b417Chris Lattner
19740953e767ff7817f97b3ab20896b229891eeff45bJohn McCall  // If we are composing extended qualifiers together, merge together
19750953e767ff7817f97b3ab20896b229891eeff45bJohn McCall  // into one ExtQuals node.
19760953e767ff7817f97b3ab20896b229891eeff45bJohn McCall  QualifierCollector Quals;
19770953e767ff7817f97b3ab20896b229891eeff45bJohn McCall  const Type *TypeNode = Quals.strip(T);
19781eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
19790953e767ff7817f97b3ab20896b229891eeff45bJohn McCall  // If this type already has an address space specified, it cannot get
19800953e767ff7817f97b3ab20896b229891eeff45bJohn McCall  // another one.
19810953e767ff7817f97b3ab20896b229891eeff45bJohn McCall  assert(!Quals.hasAddressSpace() &&
19820953e767ff7817f97b3ab20896b229891eeff45bJohn McCall         "Type cannot be in multiple addr spaces!");
19830953e767ff7817f97b3ab20896b229891eeff45bJohn McCall  Quals.addAddressSpace(AddressSpace);
19841eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
19850953e767ff7817f97b3ab20896b229891eeff45bJohn McCall  return getExtQualType(TypeNode, Quals);
1986ebb97e98c03f8d7034bd3748a10e35f39a95c289Christopher Lamb}
1987ebb97e98c03f8d7034bd3748a10e35f39a95c289Christopher Lamb
1988b7d2553edd2532d29b98b9e76bcf6a62bc48b417Chris LattnerQualType ASTContext::getObjCGCQualType(QualType T,
19894ba2a17694148e16eaa8d3917f657ffcd3667be4Jay Foad                                       Qualifiers::GC GCAttr) const {
1990d33d9c0cc0cfdcd0b10f35a6acdfb25da4a64f19Fariborz Jahanian  QualType CanT = getCanonicalType(T);
1991b7d2553edd2532d29b98b9e76bcf6a62bc48b417Chris Lattner  if (CanT.getObjCGCAttr() == GCAttr)
1992d33d9c0cc0cfdcd0b10f35a6acdfb25da4a64f19Fariborz Jahanian    return T;
19931eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
19947f040a9d817cd1c72b565e92abff473510bf9e1dJohn McCall  if (const PointerType *ptr = T->getAs<PointerType>()) {
19957f040a9d817cd1c72b565e92abff473510bf9e1dJohn McCall    QualType Pointee = ptr->getPointeeType();
199658f9f2c884af6b72d036b746a016d8031d31cb7aSteve Naroff    if (Pointee->isAnyPointerType()) {
19974027cd1b924e29784a49085b1717f35cdd719146Fariborz Jahanian      QualType ResultType = getObjCGCQualType(Pointee, GCAttr);
19984027cd1b924e29784a49085b1717f35cdd719146Fariborz Jahanian      return getPointerType(ResultType);
19994027cd1b924e29784a49085b1717f35cdd719146Fariborz Jahanian    }
20004027cd1b924e29784a49085b1717f35cdd719146Fariborz Jahanian  }
20011eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
20020953e767ff7817f97b3ab20896b229891eeff45bJohn McCall  // If we are composing extended qualifiers together, merge together
20030953e767ff7817f97b3ab20896b229891eeff45bJohn McCall  // into one ExtQuals node.
20040953e767ff7817f97b3ab20896b229891eeff45bJohn McCall  QualifierCollector Quals;
20050953e767ff7817f97b3ab20896b229891eeff45bJohn McCall  const Type *TypeNode = Quals.strip(T);
20061eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
20070953e767ff7817f97b3ab20896b229891eeff45bJohn McCall  // If this type already has an ObjCGC specified, it cannot get
20080953e767ff7817f97b3ab20896b229891eeff45bJohn McCall  // another one.
20090953e767ff7817f97b3ab20896b229891eeff45bJohn McCall  assert(!Quals.hasObjCGCAttr() &&
20100953e767ff7817f97b3ab20896b229891eeff45bJohn McCall         "Type cannot have multiple ObjCGCs!");
20110953e767ff7817f97b3ab20896b229891eeff45bJohn McCall  Quals.addObjCGCAttr(GCAttr);
20121eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
20130953e767ff7817f97b3ab20896b229891eeff45bJohn McCall  return getExtQualType(TypeNode, Quals);
2014d33d9c0cc0cfdcd0b10f35a6acdfb25da4a64f19Fariborz Jahanian}
2015a7674d8a9a69f3f6fe16e70cf2a3b2b15fb7c43dChris Lattner
2016e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCallconst FunctionType *ASTContext::adjustFunctionType(const FunctionType *T,
2017e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall                                                   FunctionType::ExtInfo Info) {
2018e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall  if (T->getExtInfo() == Info)
2019e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall    return T;
2020e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall
2021e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall  QualType Result;
2022e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall  if (const FunctionNoProtoType *FNPT = dyn_cast<FunctionNoProtoType>(T)) {
2023e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall    Result = getFunctionNoProtoType(FNPT->getResultType(), Info);
2024e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall  } else {
2025e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall    const FunctionProtoType *FPT = cast<FunctionProtoType>(T);
2026e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall    FunctionProtoType::ExtProtoInfo EPI = FPT->getExtProtoInfo();
2027e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall    EPI.ExtInfo = Info;
2028bea522ff43a3f11c7a2bc7949119dbb9fce19e39Jordan Rose    Result = getFunctionType(FPT->getResultType(),
2029bea522ff43a3f11c7a2bc7949119dbb9fce19e39Jordan Rose                             ArrayRef<QualType>(FPT->arg_type_begin(),
2030bea522ff43a3f11c7a2bc7949119dbb9fce19e39Jordan Rose                                                FPT->getNumArgs()),
2031bea522ff43a3f11c7a2bc7949119dbb9fce19e39Jordan Rose                             EPI);
2032e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall  }
2033e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall
2034e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall  return cast<FunctionType>(Result.getTypePtr());
2035e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall}
2036e6a365d772a6b455f1e23ac9ae5f40d65a55a18cJohn McCall
20375f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// getComplexType - Return the uniqued reference to the type for a complex
20385f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// number with the specified element type.
20394ba2a17694148e16eaa8d3917f657ffcd3667be4Jay FoadQualType ASTContext::getComplexType(QualType T) const {
20405f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // Unique pointers, to guarantee there is only one pointer of a particular
20415f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // structure.
20425f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  llvm::FoldingSetNodeID ID;
20435f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  ComplexType::Profile(ID, T);
20441eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
20455f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  void *InsertPos = 0;
20465f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  if (ComplexType *CT = ComplexTypes.FindNodeOrInsertPos(ID, InsertPos))
20475f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    return QualType(CT, 0);
20481eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
20495f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // If the pointee type isn't canonical, this won't be a canonical type either,
20505f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // so fill in the canonical type field.
20515f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  QualType Canonical;
2052467b27b9a24bdc823218ad1ad0e37673b6cc1e83John McCall  if (!T.isCanonical()) {
2053f52ab250ff92bc51a9ac9a8e19bd43b63a5f844fChris Lattner    Canonical = getComplexType(getCanonicalType(T));
20541eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
20555f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    // Get the new insert position for the node we care about.
20565f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    ComplexType *NewIP = ComplexTypes.FindNodeOrInsertPos(ID, InsertPos);
2057c6ed729f669044f5072a49d79041f455d971ece3Jeffrey Yasskin    assert(NewIP == 0 && "Shouldn't be in the map!"); (void)NewIP;
20585f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
20596b304a0254a13f42390b865ff5ba668a49cc58aeJohn McCall  ComplexType *New = new (*this, TypeAlignment) ComplexType(T, Canonical);
20605f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  Types.push_back(New);
20615f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  ComplexTypes.InsertNode(New, InsertPos);
20625f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  return QualType(New, 0);
20635f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
20645f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
20655f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// getPointerType - Return the uniqued reference to the type for a pointer to
20665f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// the specified type.
20674ba2a17694148e16eaa8d3917f657ffcd3667be4Jay FoadQualType ASTContext::getPointerType(QualType T) const {
20685f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // Unique pointers, to guarantee there is only one pointer of a particular
20695f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // structure.
20705f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  llvm::FoldingSetNodeID ID;
20715f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  PointerType::Profile(ID, T);
20721eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
20735f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  void *InsertPos = 0;
20745f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  if (PointerType *PT = PointerTypes.FindNodeOrInsertPos(ID, InsertPos))
20755f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    return QualType(PT, 0);
20761eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
20775f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // If the pointee type isn't canonical, this won't be a canonical type either,
20785f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // so fill in the canonical type field.
20795f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  QualType Canonical;
2080c90cc9374f18396251760b9fe6468fd1232e9f0eBob Wilson  if (!T.isCanonical()) {
2081f52ab250ff92bc51a9ac9a8e19bd43b63a5f844fChris Lattner    Canonical = getPointerType(getCanonicalType(T));
20821eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
2083c90cc9374f18396251760b9fe6468fd1232e9f0eBob Wilson    // Get the new insert position for the node we care about.
2084c90cc9374f18396251760b9fe6468fd1232e9f0eBob Wilson    PointerType *NewIP = PointerTypes.FindNodeOrInsertPos(ID, InsertPos);
2085c90cc9374f18396251760b9fe6468fd1232e9f0eBob Wilson    assert(NewIP == 0 && "Shouldn't be in the map!"); (void)NewIP;
2086c90cc9374f18396251760b9fe6468fd1232e9f0eBob Wilson  }
20876b304a0254a13f42390b865ff5ba668a49cc58aeJohn McCall  PointerType *New = new (*this, TypeAlignment) PointerType(T, Canonical);
20885f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  Types.push_back(New);
20895f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  PointerTypes.InsertNode(New, InsertPos);
20905f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  return QualType(New, 0);
20915f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
20925f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
20931eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// getBlockPointerType - Return the uniqued reference to the type for
20945618bd4a52c45fbbb605e3ba885663b2164db8a3Steve Naroff/// a pointer to the specified block.
20954ba2a17694148e16eaa8d3917f657ffcd3667be4Jay FoadQualType ASTContext::getBlockPointerType(QualType T) const {
2096296e8d5fdcf9946f51e866adc8d281379e51efe9Steve Naroff  assert(T->isFunctionType() && "block of function types only");
2097296e8d5fdcf9946f51e866adc8d281379e51efe9Steve Naroff  // Unique pointers, to guarantee there is only one block of a particular
20985618bd4a52c45fbbb605e3ba885663b2164db8a3Steve Naroff  // structure.
20995618bd4a52c45fbbb605e3ba885663b2164db8a3Steve Naroff  llvm::FoldingSetNodeID ID;
21005618bd4a52c45fbbb605e3ba885663b2164db8a3Steve Naroff  BlockPointerType::Profile(ID, T);
21011eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
21025618bd4a52c45fbbb605e3ba885663b2164db8a3Steve Naroff  void *InsertPos = 0;
21035618bd4a52c45fbbb605e3ba885663b2164db8a3Steve Naroff  if (BlockPointerType *PT =
21045618bd4a52c45fbbb605e3ba885663b2164db8a3Steve Naroff        BlockPointerTypes.FindNodeOrInsertPos(ID, InsertPos))
21055618bd4a52c45fbbb605e3ba885663b2164db8a3Steve Naroff    return QualType(PT, 0);
21061eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
21071eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  // If the block pointee type isn't canonical, this won't be a canonical
21085618bd4a52c45fbbb605e3ba885663b2164db8a3Steve Naroff  // type either so fill in the canonical type field.
21095618bd4a52c45fbbb605e3ba885663b2164db8a3Steve Naroff  QualType Canonical;
2110467b27b9a24bdc823218ad1ad0e37673b6cc1e83John McCall  if (!T.isCanonical()) {
21115618bd4a52c45fbbb605e3ba885663b2164db8a3Steve Naroff    Canonical = getBlockPointerType(getCanonicalType(T));
21121eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
21135618bd4a52c45fbbb605e3ba885663b2164db8a3Steve Naroff    // Get the new insert position for the node we care about.
21145618bd4a52c45fbbb605e3ba885663b2164db8a3Steve Naroff    BlockPointerType *NewIP =
21155618bd4a52c45fbbb605e3ba885663b2164db8a3Steve Naroff      BlockPointerTypes.FindNodeOrInsertPos(ID, InsertPos);
2116c6ed729f669044f5072a49d79041f455d971ece3Jeffrey Yasskin    assert(NewIP == 0 && "Shouldn't be in the map!"); (void)NewIP;
21175618bd4a52c45fbbb605e3ba885663b2164db8a3Steve Naroff  }
21186b304a0254a13f42390b865ff5ba668a49cc58aeJohn McCall  BlockPointerType *New
21196b304a0254a13f42390b865ff5ba668a49cc58aeJohn McCall    = new (*this, TypeAlignment) BlockPointerType(T, Canonical);
21205618bd4a52c45fbbb605e3ba885663b2164db8a3Steve Naroff  Types.push_back(New);
21215618bd4a52c45fbbb605e3ba885663b2164db8a3Steve Naroff  BlockPointerTypes.InsertNode(New, InsertPos);
21225618bd4a52c45fbbb605e3ba885663b2164db8a3Steve Naroff  return QualType(New, 0);
21235618bd4a52c45fbbb605e3ba885663b2164db8a3Steve Naroff}
21245618bd4a52c45fbbb605e3ba885663b2164db8a3Steve Naroff
21257c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl/// getLValueReferenceType - Return the uniqued reference to the type for an
21267c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl/// lvalue reference to the specified type.
21274ba2a17694148e16eaa8d3917f657ffcd3667be4Jay FoadQualType
21284ba2a17694148e16eaa8d3917f657ffcd3667be4Jay FoadASTContext::getLValueReferenceType(QualType T, bool SpelledAsLValue) const {
21299625e44c0252485277a340746ed8ac950686156fDouglas Gregor  assert(getCanonicalType(T) != OverloadTy &&
21309625e44c0252485277a340746ed8ac950686156fDouglas Gregor         "Unresolved overloaded function type");
21319625e44c0252485277a340746ed8ac950686156fDouglas Gregor
21325f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // Unique pointers, to guarantee there is only one pointer of a particular
21335f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // structure.
21345f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  llvm::FoldingSetNodeID ID;
213554e14c4db764c0636160d26c5bbf491637c83a76John McCall  ReferenceType::Profile(ID, T, SpelledAsLValue);
21365f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
21375f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  void *InsertPos = 0;
21387c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl  if (LValueReferenceType *RT =
21397c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl        LValueReferenceTypes.FindNodeOrInsertPos(ID, InsertPos))
21405f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    return QualType(RT, 0);
21417c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl
214254e14c4db764c0636160d26c5bbf491637c83a76John McCall  const ReferenceType *InnerRef = T->getAs<ReferenceType>();
214354e14c4db764c0636160d26c5bbf491637c83a76John McCall
21445f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // If the referencee type isn't canonical, this won't be a canonical type
21455f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // either, so fill in the canonical type field.
21465f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  QualType Canonical;
214754e14c4db764c0636160d26c5bbf491637c83a76John McCall  if (!SpelledAsLValue || InnerRef || !T.isCanonical()) {
214854e14c4db764c0636160d26c5bbf491637c83a76John McCall    QualType PointeeType = (InnerRef ? InnerRef->getPointeeType() : T);
214954e14c4db764c0636160d26c5bbf491637c83a76John McCall    Canonical = getLValueReferenceType(getCanonicalType(PointeeType));
21507c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl
21517c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl    // Get the new insert position for the node we care about.
21527c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl    LValueReferenceType *NewIP =
21537c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl      LValueReferenceTypes.FindNodeOrInsertPos(ID, InsertPos);
2154c6ed729f669044f5072a49d79041f455d971ece3Jeffrey Yasskin    assert(NewIP == 0 && "Shouldn't be in the map!"); (void)NewIP;
21557c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl  }
21567c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl
21576b304a0254a13f42390b865ff5ba668a49cc58aeJohn McCall  LValueReferenceType *New
215854e14c4db764c0636160d26c5bbf491637c83a76John McCall    = new (*this, TypeAlignment) LValueReferenceType(T, Canonical,
215954e14c4db764c0636160d26c5bbf491637c83a76John McCall                                                     SpelledAsLValue);
21607c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl  Types.push_back(New);
21617c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl  LValueReferenceTypes.InsertNode(New, InsertPos);
216254e14c4db764c0636160d26c5bbf491637c83a76John McCall
21637c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl  return QualType(New, 0);
21647c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl}
21657c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl
21667c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl/// getRValueReferenceType - Return the uniqued reference to the type for an
21677c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl/// rvalue reference to the specified type.
21684ba2a17694148e16eaa8d3917f657ffcd3667be4Jay FoadQualType ASTContext::getRValueReferenceType(QualType T) const {
21697c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl  // Unique pointers, to guarantee there is only one pointer of a particular
21707c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl  // structure.
21717c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl  llvm::FoldingSetNodeID ID;
217254e14c4db764c0636160d26c5bbf491637c83a76John McCall  ReferenceType::Profile(ID, T, false);
21737c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl
21747c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl  void *InsertPos = 0;
21757c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl  if (RValueReferenceType *RT =
21767c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl        RValueReferenceTypes.FindNodeOrInsertPos(ID, InsertPos))
21777c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl    return QualType(RT, 0);
21787c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl
217954e14c4db764c0636160d26c5bbf491637c83a76John McCall  const ReferenceType *InnerRef = T->getAs<ReferenceType>();
218054e14c4db764c0636160d26c5bbf491637c83a76John McCall
21817c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl  // If the referencee type isn't canonical, this won't be a canonical type
21827c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl  // either, so fill in the canonical type field.
21837c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl  QualType Canonical;
218454e14c4db764c0636160d26c5bbf491637c83a76John McCall  if (InnerRef || !T.isCanonical()) {
218554e14c4db764c0636160d26c5bbf491637c83a76John McCall    QualType PointeeType = (InnerRef ? InnerRef->getPointeeType() : T);
218654e14c4db764c0636160d26c5bbf491637c83a76John McCall    Canonical = getRValueReferenceType(getCanonicalType(PointeeType));
21877c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl
21885f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    // Get the new insert position for the node we care about.
21897c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl    RValueReferenceType *NewIP =
21907c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl      RValueReferenceTypes.FindNodeOrInsertPos(ID, InsertPos);
2191c6ed729f669044f5072a49d79041f455d971ece3Jeffrey Yasskin    assert(NewIP == 0 && "Shouldn't be in the map!"); (void)NewIP;
21925f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
21935f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
21946b304a0254a13f42390b865ff5ba668a49cc58aeJohn McCall  RValueReferenceType *New
21956b304a0254a13f42390b865ff5ba668a49cc58aeJohn McCall    = new (*this, TypeAlignment) RValueReferenceType(T, Canonical);
21965f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  Types.push_back(New);
21977c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl  RValueReferenceTypes.InsertNode(New, InsertPos);
21985f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  return QualType(New, 0);
21995f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
22005f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
2201f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl/// getMemberPointerType - Return the uniqued reference to the type for a
2202f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl/// member pointer to the specified type, in the specified class.
22034ba2a17694148e16eaa8d3917f657ffcd3667be4Jay FoadQualType ASTContext::getMemberPointerType(QualType T, const Type *Cls) const {
2204f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl  // Unique pointers, to guarantee there is only one pointer of a particular
2205f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl  // structure.
2206f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl  llvm::FoldingSetNodeID ID;
2207f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl  MemberPointerType::Profile(ID, T, Cls);
2208f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl
2209f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl  void *InsertPos = 0;
2210f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl  if (MemberPointerType *PT =
2211f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl      MemberPointerTypes.FindNodeOrInsertPos(ID, InsertPos))
2212f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl    return QualType(PT, 0);
2213f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl
2214f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl  // If the pointee or class type isn't canonical, this won't be a canonical
2215f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl  // type either, so fill in the canonical type field.
2216f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl  QualType Canonical;
221787c12c4a4667279dacb3d4a93c64b49148a0ff79Douglas Gregor  if (!T.isCanonical() || !Cls->isCanonicalUnqualified()) {
2218f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl    Canonical = getMemberPointerType(getCanonicalType(T),getCanonicalType(Cls));
2219f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl
2220f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl    // Get the new insert position for the node we care about.
2221f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl    MemberPointerType *NewIP =
2222f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl      MemberPointerTypes.FindNodeOrInsertPos(ID, InsertPos);
2223c6ed729f669044f5072a49d79041f455d971ece3Jeffrey Yasskin    assert(NewIP == 0 && "Shouldn't be in the map!"); (void)NewIP;
2224f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl  }
22256b304a0254a13f42390b865ff5ba668a49cc58aeJohn McCall  MemberPointerType *New
22266b304a0254a13f42390b865ff5ba668a49cc58aeJohn McCall    = new (*this, TypeAlignment) MemberPointerType(T, Cls, Canonical);
2227f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl  Types.push_back(New);
2228f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl  MemberPointerTypes.InsertNode(New, InsertPos);
2229f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl  return QualType(New, 0);
2230f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl}
2231f30208ad5b334e93582e846a2a0c92f38a607b8aSebastian Redl
22321eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// getConstantArrayType - Return the unique reference to the type for an
2233fb22d96692c5240fb8d611290dbf7eeed3759c73Steve Naroff/// array of the specified element type.
22341eb4433ac451dc16f4133a88af2d002ac26c58efMike StumpQualType ASTContext::getConstantArrayType(QualType EltTy,
223538aeec7299c48cb79523f7f89776fb258c84aeeaChris Lattner                                          const llvm::APInt &ArySizeIn,
2236c9406125e2cac9208098655ac8058c095c2c3a65Steve Naroff                                          ArrayType::ArraySizeModifier ASM,
223763e7d25d2e6036616b42f744fd4a39cd5f911960Abramo Bagnara                                          unsigned IndexTypeQuals) const {
2238923d56d436f750bc1f29db50e641078725558a1bSebastian Redl  assert((EltTy->isDependentType() ||
2239923d56d436f750bc1f29db50e641078725558a1bSebastian Redl          EltTy->isIncompleteType() || EltTy->isConstantSizeType()) &&
2240587cbdfd95f4b0aaccc14b31f5debe85d5daf7edEli Friedman         "Constant array of VLAs is illegal!");
2241587cbdfd95f4b0aaccc14b31f5debe85d5daf7edEli Friedman
224238aeec7299c48cb79523f7f89776fb258c84aeeaChris Lattner  // Convert the array size into a canonical width matching the pointer size for
224338aeec7299c48cb79523f7f89776fb258c84aeeaChris Lattner  // the target.
224438aeec7299c48cb79523f7f89776fb258c84aeeaChris Lattner  llvm::APInt ArySize(ArySizeIn);
22459f71a8f4c7a182a5236da9e747d57cc1d1bd24c2Jay Foad  ArySize =
2246bcfd1f55bfbb3e5944cd5e03d07b343e280838c4Douglas Gregor    ArySize.zextOrTrunc(Target->getPointerWidth(getTargetAddressSpace(EltTy)));
22471eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
22485f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  llvm::FoldingSetNodeID ID;
224963e7d25d2e6036616b42f744fd4a39cd5f911960Abramo Bagnara  ConstantArrayType::Profile(ID, EltTy, ArySize, ASM, IndexTypeQuals);
22501eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
22515f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  void *InsertPos = 0;
22521eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  if (ConstantArrayType *ATP =
22537192f8e9592729882a09d84d77838db26e39ebd4Ted Kremenek      ConstantArrayTypes.FindNodeOrInsertPos(ID, InsertPos))
22545f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    return QualType(ATP, 0);
22551eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
22563b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall  // If the element type isn't canonical or has qualifiers, this won't
22573b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall  // be a canonical type either, so fill in the canonical type field.
22583b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall  QualType Canon;
22593b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall  if (!EltTy.isCanonical() || EltTy.hasLocalQualifiers()) {
22603b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall    SplitQualType canonSplit = getCanonicalType(EltTy).split();
2261200fa53fd420aa8369586f569dbece04930ad6a3John McCall    Canon = getConstantArrayType(QualType(canonSplit.Ty, 0), ArySize,
226263e7d25d2e6036616b42f744fd4a39cd5f911960Abramo Bagnara                                 ASM, IndexTypeQuals);
2263200fa53fd420aa8369586f569dbece04930ad6a3John McCall    Canon = getQualifiedType(Canon, canonSplit.Quals);
22643b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall
22655f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    // Get the new insert position for the node we care about.
22661eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    ConstantArrayType *NewIP =
22677192f8e9592729882a09d84d77838db26e39ebd4Ted Kremenek      ConstantArrayTypes.FindNodeOrInsertPos(ID, InsertPos);
2268c6ed729f669044f5072a49d79041f455d971ece3Jeffrey Yasskin    assert(NewIP == 0 && "Shouldn't be in the map!"); (void)NewIP;
22695f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
22701eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
22716b304a0254a13f42390b865ff5ba668a49cc58aeJohn McCall  ConstantArrayType *New = new(*this,TypeAlignment)
227263e7d25d2e6036616b42f744fd4a39cd5f911960Abramo Bagnara    ConstantArrayType(EltTy, Canon, ArySize, ASM, IndexTypeQuals);
22737192f8e9592729882a09d84d77838db26e39ebd4Ted Kremenek  ConstantArrayTypes.InsertNode(New, InsertPos);
22745f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  Types.push_back(New);
22755f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  return QualType(New, 0);
22765f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
22775f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
2278ce8890371fcdb983ae487c87fa40606a34896ff7John McCall/// getVariableArrayDecayedType - Turns the given type, which may be
2279ce8890371fcdb983ae487c87fa40606a34896ff7John McCall/// variably-modified, into the corresponding type with all the known
2280ce8890371fcdb983ae487c87fa40606a34896ff7John McCall/// sizes replaced with [*].
2281ce8890371fcdb983ae487c87fa40606a34896ff7John McCallQualType ASTContext::getVariableArrayDecayedType(QualType type) const {
2282ce8890371fcdb983ae487c87fa40606a34896ff7John McCall  // Vastly most common case.
2283ce8890371fcdb983ae487c87fa40606a34896ff7John McCall  if (!type->isVariablyModifiedType()) return type;
2284ce8890371fcdb983ae487c87fa40606a34896ff7John McCall
2285ce8890371fcdb983ae487c87fa40606a34896ff7John McCall  QualType result;
2286ce8890371fcdb983ae487c87fa40606a34896ff7John McCall
2287ce8890371fcdb983ae487c87fa40606a34896ff7John McCall  SplitQualType split = type.getSplitDesugaredType();
2288200fa53fd420aa8369586f569dbece04930ad6a3John McCall  const Type *ty = split.Ty;
2289ce8890371fcdb983ae487c87fa40606a34896ff7John McCall  switch (ty->getTypeClass()) {
2290ce8890371fcdb983ae487c87fa40606a34896ff7John McCall#define TYPE(Class, Base)
2291ce8890371fcdb983ae487c87fa40606a34896ff7John McCall#define ABSTRACT_TYPE(Class, Base)
2292ce8890371fcdb983ae487c87fa40606a34896ff7John McCall#define NON_CANONICAL_TYPE(Class, Base) case Type::Class:
2293ce8890371fcdb983ae487c87fa40606a34896ff7John McCall#include "clang/AST/TypeNodes.def"
2294ce8890371fcdb983ae487c87fa40606a34896ff7John McCall    llvm_unreachable("didn't desugar past all non-canonical types?");
2295ce8890371fcdb983ae487c87fa40606a34896ff7John McCall
2296ce8890371fcdb983ae487c87fa40606a34896ff7John McCall  // These types should never be variably-modified.
2297ce8890371fcdb983ae487c87fa40606a34896ff7John McCall  case Type::Builtin:
2298ce8890371fcdb983ae487c87fa40606a34896ff7John McCall  case Type::Complex:
2299ce8890371fcdb983ae487c87fa40606a34896ff7John McCall  case Type::Vector:
2300ce8890371fcdb983ae487c87fa40606a34896ff7John McCall  case Type::ExtVector:
2301ce8890371fcdb983ae487c87fa40606a34896ff7John McCall  case Type::DependentSizedExtVector:
2302ce8890371fcdb983ae487c87fa40606a34896ff7John McCall  case Type::ObjCObject:
2303ce8890371fcdb983ae487c87fa40606a34896ff7John McCall  case Type::ObjCInterface:
2304ce8890371fcdb983ae487c87fa40606a34896ff7John McCall  case Type::ObjCObjectPointer:
2305ce8890371fcdb983ae487c87fa40606a34896ff7John McCall  case Type::Record:
2306ce8890371fcdb983ae487c87fa40606a34896ff7John McCall  case Type::Enum:
2307ce8890371fcdb983ae487c87fa40606a34896ff7John McCall  case Type::UnresolvedUsing:
2308ce8890371fcdb983ae487c87fa40606a34896ff7John McCall  case Type::TypeOfExpr:
2309ce8890371fcdb983ae487c87fa40606a34896ff7John McCall  case Type::TypeOf:
2310ce8890371fcdb983ae487c87fa40606a34896ff7John McCall  case Type::Decltype:
2311ca63c200346c0ca9e00194ec6e34a5a7b0ed9321Sean Hunt  case Type::UnaryTransform:
2312ce8890371fcdb983ae487c87fa40606a34896ff7John McCall  case Type::DependentName:
2313ce8890371fcdb983ae487c87fa40606a34896ff7John McCall  case Type::InjectedClassName:
2314ce8890371fcdb983ae487c87fa40606a34896ff7John McCall  case Type::TemplateSpecialization:
2315ce8890371fcdb983ae487c87fa40606a34896ff7John McCall  case Type::DependentTemplateSpecialization:
2316ce8890371fcdb983ae487c87fa40606a34896ff7John McCall  case Type::TemplateTypeParm:
2317ce8890371fcdb983ae487c87fa40606a34896ff7John McCall  case Type::SubstTemplateTypeParmPack:
231834b41d939a1328f484511c6002ba2456db879a29Richard Smith  case Type::Auto:
2319ce8890371fcdb983ae487c87fa40606a34896ff7John McCall  case Type::PackExpansion:
2320ce8890371fcdb983ae487c87fa40606a34896ff7John McCall    llvm_unreachable("type should never be variably-modified");
2321ce8890371fcdb983ae487c87fa40606a34896ff7John McCall
2322ce8890371fcdb983ae487c87fa40606a34896ff7John McCall  // These types can be variably-modified but should never need to
2323ce8890371fcdb983ae487c87fa40606a34896ff7John McCall  // further decay.
2324ce8890371fcdb983ae487c87fa40606a34896ff7John McCall  case Type::FunctionNoProto:
2325ce8890371fcdb983ae487c87fa40606a34896ff7John McCall  case Type::FunctionProto:
2326ce8890371fcdb983ae487c87fa40606a34896ff7John McCall  case Type::BlockPointer:
2327ce8890371fcdb983ae487c87fa40606a34896ff7John McCall  case Type::MemberPointer:
2328ce8890371fcdb983ae487c87fa40606a34896ff7John McCall    return type;
2329ce8890371fcdb983ae487c87fa40606a34896ff7John McCall
2330ce8890371fcdb983ae487c87fa40606a34896ff7John McCall  // These types can be variably-modified.  All these modifications
2331ce8890371fcdb983ae487c87fa40606a34896ff7John McCall  // preserve structure except as noted by comments.
2332ce8890371fcdb983ae487c87fa40606a34896ff7John McCall  // TODO: if we ever care about optimizing VLAs, there are no-op
2333ce8890371fcdb983ae487c87fa40606a34896ff7John McCall  // optimizations available here.
2334ce8890371fcdb983ae487c87fa40606a34896ff7John McCall  case Type::Pointer:
2335ce8890371fcdb983ae487c87fa40606a34896ff7John McCall    result = getPointerType(getVariableArrayDecayedType(
2336ce8890371fcdb983ae487c87fa40606a34896ff7John McCall                              cast<PointerType>(ty)->getPointeeType()));
2337ce8890371fcdb983ae487c87fa40606a34896ff7John McCall    break;
2338ce8890371fcdb983ae487c87fa40606a34896ff7John McCall
2339ce8890371fcdb983ae487c87fa40606a34896ff7John McCall  case Type::LValueReference: {
2340ce8890371fcdb983ae487c87fa40606a34896ff7John McCall    const LValueReferenceType *lv = cast<LValueReferenceType>(ty);
2341ce8890371fcdb983ae487c87fa40606a34896ff7John McCall    result = getLValueReferenceType(
2342ce8890371fcdb983ae487c87fa40606a34896ff7John McCall                 getVariableArrayDecayedType(lv->getPointeeType()),
2343ce8890371fcdb983ae487c87fa40606a34896ff7John McCall                                    lv->isSpelledAsLValue());
2344ce8890371fcdb983ae487c87fa40606a34896ff7John McCall    break;
2345745da3a5bb4ea35f93f50301e7fbbb7d78d3b6bbFariborz Jahanian  }
2346ce8890371fcdb983ae487c87fa40606a34896ff7John McCall
2347ce8890371fcdb983ae487c87fa40606a34896ff7John McCall  case Type::RValueReference: {
2348ce8890371fcdb983ae487c87fa40606a34896ff7John McCall    const RValueReferenceType *lv = cast<RValueReferenceType>(ty);
2349ce8890371fcdb983ae487c87fa40606a34896ff7John McCall    result = getRValueReferenceType(
2350ce8890371fcdb983ae487c87fa40606a34896ff7John McCall                 getVariableArrayDecayedType(lv->getPointeeType()));
2351ce8890371fcdb983ae487c87fa40606a34896ff7John McCall    break;
2352745da3a5bb4ea35f93f50301e7fbbb7d78d3b6bbFariborz Jahanian  }
2353745da3a5bb4ea35f93f50301e7fbbb7d78d3b6bbFariborz Jahanian
2354b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman  case Type::Atomic: {
2355b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman    const AtomicType *at = cast<AtomicType>(ty);
2356b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman    result = getAtomicType(getVariableArrayDecayedType(at->getValueType()));
2357b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman    break;
2358b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman  }
2359b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman
2360ce8890371fcdb983ae487c87fa40606a34896ff7John McCall  case Type::ConstantArray: {
2361ce8890371fcdb983ae487c87fa40606a34896ff7John McCall    const ConstantArrayType *cat = cast<ConstantArrayType>(ty);
2362ce8890371fcdb983ae487c87fa40606a34896ff7John McCall    result = getConstantArrayType(
2363ce8890371fcdb983ae487c87fa40606a34896ff7John McCall                 getVariableArrayDecayedType(cat->getElementType()),
2364ce8890371fcdb983ae487c87fa40606a34896ff7John McCall                                  cat->getSize(),
2365ce8890371fcdb983ae487c87fa40606a34896ff7John McCall                                  cat->getSizeModifier(),
2366ce8890371fcdb983ae487c87fa40606a34896ff7John McCall                                  cat->getIndexTypeCVRQualifiers());
2367ce8890371fcdb983ae487c87fa40606a34896ff7John McCall    break;
2368745da3a5bb4ea35f93f50301e7fbbb7d78d3b6bbFariborz Jahanian  }
2369745da3a5bb4ea35f93f50301e7fbbb7d78d3b6bbFariborz Jahanian
2370ce8890371fcdb983ae487c87fa40606a34896ff7John McCall  case Type::DependentSizedArray: {
2371ce8890371fcdb983ae487c87fa40606a34896ff7John McCall    const DependentSizedArrayType *dat = cast<DependentSizedArrayType>(ty);
2372ce8890371fcdb983ae487c87fa40606a34896ff7John McCall    result = getDependentSizedArrayType(
2373ce8890371fcdb983ae487c87fa40606a34896ff7John McCall                 getVariableArrayDecayedType(dat->getElementType()),
2374ce8890371fcdb983ae487c87fa40606a34896ff7John McCall                                        dat->getSizeExpr(),
2375ce8890371fcdb983ae487c87fa40606a34896ff7John McCall                                        dat->getSizeModifier(),
2376ce8890371fcdb983ae487c87fa40606a34896ff7John McCall                                        dat->getIndexTypeCVRQualifiers(),
2377ce8890371fcdb983ae487c87fa40606a34896ff7John McCall                                        dat->getBracketsRange());
2378ce8890371fcdb983ae487c87fa40606a34896ff7John McCall    break;
2379ce8890371fcdb983ae487c87fa40606a34896ff7John McCall  }
2380ce8890371fcdb983ae487c87fa40606a34896ff7John McCall
2381ce8890371fcdb983ae487c87fa40606a34896ff7John McCall  // Turn incomplete types into [*] types.
2382ce8890371fcdb983ae487c87fa40606a34896ff7John McCall  case Type::IncompleteArray: {
2383ce8890371fcdb983ae487c87fa40606a34896ff7John McCall    const IncompleteArrayType *iat = cast<IncompleteArrayType>(ty);
2384ce8890371fcdb983ae487c87fa40606a34896ff7John McCall    result = getVariableArrayType(
2385ce8890371fcdb983ae487c87fa40606a34896ff7John McCall                 getVariableArrayDecayedType(iat->getElementType()),
2386ce8890371fcdb983ae487c87fa40606a34896ff7John McCall                                  /*size*/ 0,
2387ce8890371fcdb983ae487c87fa40606a34896ff7John McCall                                  ArrayType::Normal,
2388ce8890371fcdb983ae487c87fa40606a34896ff7John McCall                                  iat->getIndexTypeCVRQualifiers(),
2389ce8890371fcdb983ae487c87fa40606a34896ff7John McCall                                  SourceRange());
2390ce8890371fcdb983ae487c87fa40606a34896ff7John McCall    break;
2391ce8890371fcdb983ae487c87fa40606a34896ff7John McCall  }
2392ce8890371fcdb983ae487c87fa40606a34896ff7John McCall
2393ce8890371fcdb983ae487c87fa40606a34896ff7John McCall  // Turn VLA types into [*] types.
2394ce8890371fcdb983ae487c87fa40606a34896ff7John McCall  case Type::VariableArray: {
2395ce8890371fcdb983ae487c87fa40606a34896ff7John McCall    const VariableArrayType *vat = cast<VariableArrayType>(ty);
2396ce8890371fcdb983ae487c87fa40606a34896ff7John McCall    result = getVariableArrayType(
2397ce8890371fcdb983ae487c87fa40606a34896ff7John McCall                 getVariableArrayDecayedType(vat->getElementType()),
2398ce8890371fcdb983ae487c87fa40606a34896ff7John McCall                                  /*size*/ 0,
2399ce8890371fcdb983ae487c87fa40606a34896ff7John McCall                                  ArrayType::Star,
2400ce8890371fcdb983ae487c87fa40606a34896ff7John McCall                                  vat->getIndexTypeCVRQualifiers(),
2401ce8890371fcdb983ae487c87fa40606a34896ff7John McCall                                  vat->getBracketsRange());
2402ce8890371fcdb983ae487c87fa40606a34896ff7John McCall    break;
2403ce8890371fcdb983ae487c87fa40606a34896ff7John McCall  }
2404ce8890371fcdb983ae487c87fa40606a34896ff7John McCall  }
2405ce8890371fcdb983ae487c87fa40606a34896ff7John McCall
2406ce8890371fcdb983ae487c87fa40606a34896ff7John McCall  // Apply the top-level qualifiers from the original.
2407200fa53fd420aa8369586f569dbece04930ad6a3John McCall  return getQualifiedType(result, split.Quals);
2408ce8890371fcdb983ae487c87fa40606a34896ff7John McCall}
2409745da3a5bb4ea35f93f50301e7fbbb7d78d3b6bbFariborz Jahanian
2410bdbf7b030a3e0ddb95240076683830e6f78c79a5Steve Naroff/// getVariableArrayType - Returns a non-unique reference to the type for a
2411bdbf7b030a3e0ddb95240076683830e6f78c79a5Steve Naroff/// variable array of the specified element type.
24127e7eb3da052a6d80ddf2377cab0384c798f73f75Douglas GregorQualType ASTContext::getVariableArrayType(QualType EltTy,
24137e7eb3da052a6d80ddf2377cab0384c798f73f75Douglas Gregor                                          Expr *NumElts,
2414c9406125e2cac9208098655ac8058c095c2c3a65Steve Naroff                                          ArrayType::ArraySizeModifier ASM,
241563e7d25d2e6036616b42f744fd4a39cd5f911960Abramo Bagnara                                          unsigned IndexTypeQuals,
24164ba2a17694148e16eaa8d3917f657ffcd3667be4Jay Foad                                          SourceRange Brackets) const {
2417c5773c4b8ce1ed6ed5c7112c9020c954a47dce96Eli Friedman  // Since we don't unique expressions, it isn't possible to unique VLA's
2418c5773c4b8ce1ed6ed5c7112c9020c954a47dce96Eli Friedman  // that have an expression provided for their size.
24193b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall  QualType Canon;
2420715e9c8a39437347e838aa108df443fe1086d359Douglas Gregor
24213b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall  // Be sure to pull qualifiers off the element type.
24223b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall  if (!EltTy.isCanonical() || EltTy.hasLocalQualifiers()) {
24233b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall    SplitQualType canonSplit = getCanonicalType(EltTy).split();
2424200fa53fd420aa8369586f569dbece04930ad6a3John McCall    Canon = getVariableArrayType(QualType(canonSplit.Ty, 0), NumElts, ASM,
242563e7d25d2e6036616b42f744fd4a39cd5f911960Abramo Bagnara                                 IndexTypeQuals, Brackets);
2426200fa53fd420aa8369586f569dbece04930ad6a3John McCall    Canon = getQualifiedType(Canon, canonSplit.Quals);
2427715e9c8a39437347e838aa108df443fe1086d359Douglas Gregor  }
2428715e9c8a39437347e838aa108df443fe1086d359Douglas Gregor
24296b304a0254a13f42390b865ff5ba668a49cc58aeJohn McCall  VariableArrayType *New = new(*this, TypeAlignment)
243063e7d25d2e6036616b42f744fd4a39cd5f911960Abramo Bagnara    VariableArrayType(EltTy, Canon, NumElts, ASM, IndexTypeQuals, Brackets);
2431c5773c4b8ce1ed6ed5c7112c9020c954a47dce96Eli Friedman
2432c5773c4b8ce1ed6ed5c7112c9020c954a47dce96Eli Friedman  VariableArrayTypes.push_back(New);
2433c5773c4b8ce1ed6ed5c7112c9020c954a47dce96Eli Friedman  Types.push_back(New);
2434c5773c4b8ce1ed6ed5c7112c9020c954a47dce96Eli Friedman  return QualType(New, 0);
2435c5773c4b8ce1ed6ed5c7112c9020c954a47dce96Eli Friedman}
2436c5773c4b8ce1ed6ed5c7112c9020c954a47dce96Eli Friedman
2437898574e7496ba8fd76290079d3a9d06954992734Douglas Gregor/// getDependentSizedArrayType - Returns a non-unique reference to
2438898574e7496ba8fd76290079d3a9d06954992734Douglas Gregor/// the type for a dependently-sized array of the specified element
243904d4beee4b86af20a9e4457023d3925cab8f9908Douglas Gregor/// type.
24403b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCallQualType ASTContext::getDependentSizedArrayType(QualType elementType,
24413b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall                                                Expr *numElements,
2442898574e7496ba8fd76290079d3a9d06954992734Douglas Gregor                                                ArrayType::ArraySizeModifier ASM,
24433b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall                                                unsigned elementTypeQuals,
24443b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall                                                SourceRange brackets) const {
24453b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall  assert((!numElements || numElements->isTypeDependent() ||
24463b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall          numElements->isValueDependent()) &&
2447898574e7496ba8fd76290079d3a9d06954992734Douglas Gregor         "Size must be type- or value-dependent!");
2448898574e7496ba8fd76290079d3a9d06954992734Douglas Gregor
24493b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall  // Dependently-sized array types that do not have a specified number
24503b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall  // of elements will have their sizes deduced from a dependent
24513b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall  // initializer.  We do no canonicalization here at all, which is okay
24523b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall  // because they can't be used in most locations.
24533b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall  if (!numElements) {
24543b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall    DependentSizedArrayType *newType
24553b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall      = new (*this, TypeAlignment)
24563b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall          DependentSizedArrayType(*this, elementType, QualType(),
24573b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall                                  numElements, ASM, elementTypeQuals,
24583b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall                                  brackets);
24593b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall    Types.push_back(newType);
24603b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall    return QualType(newType, 0);
2461cb78d8852a454684c987220132cdb5e54dd00121Douglas Gregor  }
2462cb78d8852a454684c987220132cdb5e54dd00121Douglas Gregor
24633b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall  // Otherwise, we actually build a new type every time, but we
24643b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall  // also build a canonical type.
24651eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
24663b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall  SplitQualType canonElementType = getCanonicalType(elementType).split();
2467898574e7496ba8fd76290079d3a9d06954992734Douglas Gregor
24683b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall  void *insertPos = 0;
24693b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall  llvm::FoldingSetNodeID ID;
24703b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall  DependentSizedArrayType::Profile(ID, *this,
2471200fa53fd420aa8369586f569dbece04930ad6a3John McCall                                   QualType(canonElementType.Ty, 0),
24723b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall                                   ASM, elementTypeQuals, numElements);
24733b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall
24743b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall  // Look for an existing type with these properties.
24753b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall  DependentSizedArrayType *canonTy =
24763b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall    DependentSizedArrayTypes.FindNodeOrInsertPos(ID, insertPos);
24773b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall
24783b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall  // If we don't have one, build one.
24793b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall  if (!canonTy) {
24803b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall    canonTy = new (*this, TypeAlignment)
2481200fa53fd420aa8369586f569dbece04930ad6a3John McCall      DependentSizedArrayType(*this, QualType(canonElementType.Ty, 0),
24823b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall                              QualType(), numElements, ASM, elementTypeQuals,
24833b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall                              brackets);
24843b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall    DependentSizedArrayTypes.InsertNode(canonTy, insertPos);
24853b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall    Types.push_back(canonTy);
24863b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall  }
24873b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall
24883b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall  // Apply qualifiers from the element type to the array.
24893b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall  QualType canon = getQualifiedType(QualType(canonTy,0),
2490200fa53fd420aa8369586f569dbece04930ad6a3John McCall                                    canonElementType.Quals);
24913b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall
24923b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall  // If we didn't need extra canonicalization for the element type,
24933b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall  // then just use that as our result.
2494200fa53fd420aa8369586f569dbece04930ad6a3John McCall  if (QualType(canonElementType.Ty, 0) == elementType)
24953b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall    return canon;
24963b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall
24973b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall  // Otherwise, we need to build a type which follows the spelling
24983b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall  // of the element type.
24993b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall  DependentSizedArrayType *sugaredType
25003b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall    = new (*this, TypeAlignment)
25013b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall        DependentSizedArrayType(*this, elementType, canon, numElements,
25023b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall                                ASM, elementTypeQuals, brackets);
25033b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall  Types.push_back(sugaredType);
25043b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall  return QualType(sugaredType, 0);
25053b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall}
25063b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall
25073b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCallQualType ASTContext::getIncompleteArrayType(QualType elementType,
2508c5773c4b8ce1ed6ed5c7112c9020c954a47dce96Eli Friedman                                            ArrayType::ArraySizeModifier ASM,
25093b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall                                            unsigned elementTypeQuals) const {
2510c5773c4b8ce1ed6ed5c7112c9020c954a47dce96Eli Friedman  llvm::FoldingSetNodeID ID;
25113b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall  IncompleteArrayType::Profile(ID, elementType, ASM, elementTypeQuals);
2512c5773c4b8ce1ed6ed5c7112c9020c954a47dce96Eli Friedman
25133b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall  void *insertPos = 0;
25143b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall  if (IncompleteArrayType *iat =
25153b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall       IncompleteArrayTypes.FindNodeOrInsertPos(ID, insertPos))
25163b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall    return QualType(iat, 0);
2517c5773c4b8ce1ed6ed5c7112c9020c954a47dce96Eli Friedman
2518c5773c4b8ce1ed6ed5c7112c9020c954a47dce96Eli Friedman  // If the element type isn't canonical, this won't be a canonical type
25193b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall  // either, so fill in the canonical type field.  We also have to pull
25203b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall  // qualifiers off the element type.
25213b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall  QualType canon;
2522c5773c4b8ce1ed6ed5c7112c9020c954a47dce96Eli Friedman
25233b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall  if (!elementType.isCanonical() || elementType.hasLocalQualifiers()) {
25243b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall    SplitQualType canonSplit = getCanonicalType(elementType).split();
2525200fa53fd420aa8369586f569dbece04930ad6a3John McCall    canon = getIncompleteArrayType(QualType(canonSplit.Ty, 0),
25263b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall                                   ASM, elementTypeQuals);
2527200fa53fd420aa8369586f569dbece04930ad6a3John McCall    canon = getQualifiedType(canon, canonSplit.Quals);
2528c5773c4b8ce1ed6ed5c7112c9020c954a47dce96Eli Friedman
2529c5773c4b8ce1ed6ed5c7112c9020c954a47dce96Eli Friedman    // Get the new insert position for the node we care about.
25303b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall    IncompleteArrayType *existing =
25313b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall      IncompleteArrayTypes.FindNodeOrInsertPos(ID, insertPos);
25323b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall    assert(!existing && "Shouldn't be in the map!"); (void) existing;
25332bd24ba6d10f8c811c8e2a57c8397e07082ba497Ted Kremenek  }
2534c5773c4b8ce1ed6ed5c7112c9020c954a47dce96Eli Friedman
25353b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall  IncompleteArrayType *newType = new (*this, TypeAlignment)
25363b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall    IncompleteArrayType(elementType, canon, ASM, elementTypeQuals);
2537c5773c4b8ce1ed6ed5c7112c9020c954a47dce96Eli Friedman
25383b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall  IncompleteArrayTypes.InsertNode(newType, insertPos);
25393b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall  Types.push_back(newType);
25403b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall  return QualType(newType, 0);
2541fb22d96692c5240fb8d611290dbf7eeed3759c73Steve Naroff}
2542fb22d96692c5240fb8d611290dbf7eeed3759c73Steve Naroff
254373322924127c873c13101b705dd823f5539ffa5fSteve Naroff/// getVectorType - Return the unique reference to a vector type of
254473322924127c873c13101b705dd823f5539ffa5fSteve Naroff/// the specified element type and size. VectorType must be a built-in type.
254582287d19ded35248c4ce6a425ce74116a13ce44eJohn ThompsonQualType ASTContext::getVectorType(QualType vecType, unsigned NumElts,
25464ba2a17694148e16eaa8d3917f657ffcd3667be4Jay Foad                                   VectorType::VectorKind VecKind) const {
25473b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall  assert(vecType->isBuiltinType());
25481eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
25495f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // Check if we've already instantiated a vector of this type.
25505f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  llvm::FoldingSetNodeID ID;
2551e86d78cf4754a6aef2cf9a33d847aa15338e276fBob Wilson  VectorType::Profile(ID, vecType, NumElts, Type::Vector, VecKind);
2552788b0fd67e1992f23555454efcdb16a19dfefac3Chris Lattner
25535f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  void *InsertPos = 0;
25545f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  if (VectorType *VTP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos))
25555f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    return QualType(VTP, 0);
25565f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
25575f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // If the element type isn't canonical, this won't be a canonical type either,
25585f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // so fill in the canonical type field.
25595f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  QualType Canonical;
2560255210ef415b9893f0e3794e8d9a704194c12f3cDouglas Gregor  if (!vecType.isCanonical()) {
2561231da7eb3dd13007e5e40fffe48998e5ef284e06Bob Wilson    Canonical = getVectorType(getCanonicalType(vecType), NumElts, VecKind);
25621eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
25635f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    // Get the new insert position for the node we care about.
25645f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    VectorType *NewIP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos);
2565c6ed729f669044f5072a49d79041f455d971ece3Jeffrey Yasskin    assert(NewIP == 0 && "Shouldn't be in the map!"); (void)NewIP;
25665f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
25676b304a0254a13f42390b865ff5ba668a49cc58aeJohn McCall  VectorType *New = new (*this, TypeAlignment)
2568e86d78cf4754a6aef2cf9a33d847aa15338e276fBob Wilson    VectorType(vecType, NumElts, Canonical, VecKind);
25695f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  VectorTypes.InsertNode(New, InsertPos);
25705f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  Types.push_back(New);
25715f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  return QualType(New, 0);
25725f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
25735f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
2574213541a68a3e137d11d2cefb612c6cdb410d7e8eNate Begeman/// getExtVectorType - Return the unique reference to an extended vector type of
257573322924127c873c13101b705dd823f5539ffa5fSteve Naroff/// the specified element type and size. VectorType must be a built-in type.
25764ba2a17694148e16eaa8d3917f657ffcd3667be4Jay FoadQualType
25774ba2a17694148e16eaa8d3917f657ffcd3667be4Jay FoadASTContext::getExtVectorType(QualType vecType, unsigned NumElts) const {
25784ac01401b1ec602a1f58c217544d3dcb5fcbd7f1Douglas Gregor  assert(vecType->isBuiltinType() || vecType->isDependentType());
25791eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
258073322924127c873c13101b705dd823f5539ffa5fSteve Naroff  // Check if we've already instantiated a vector of this type.
258173322924127c873c13101b705dd823f5539ffa5fSteve Naroff  llvm::FoldingSetNodeID ID;
2582788b0fd67e1992f23555454efcdb16a19dfefac3Chris Lattner  VectorType::Profile(ID, vecType, NumElts, Type::ExtVector,
2583e86d78cf4754a6aef2cf9a33d847aa15338e276fBob Wilson                      VectorType::GenericVector);
258473322924127c873c13101b705dd823f5539ffa5fSteve Naroff  void *InsertPos = 0;
258573322924127c873c13101b705dd823f5539ffa5fSteve Naroff  if (VectorType *VTP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos))
258673322924127c873c13101b705dd823f5539ffa5fSteve Naroff    return QualType(VTP, 0);
258773322924127c873c13101b705dd823f5539ffa5fSteve Naroff
258873322924127c873c13101b705dd823f5539ffa5fSteve Naroff  // If the element type isn't canonical, this won't be a canonical type either,
258973322924127c873c13101b705dd823f5539ffa5fSteve Naroff  // so fill in the canonical type field.
259073322924127c873c13101b705dd823f5539ffa5fSteve Naroff  QualType Canonical;
2591467b27b9a24bdc823218ad1ad0e37673b6cc1e83John McCall  if (!vecType.isCanonical()) {
2592213541a68a3e137d11d2cefb612c6cdb410d7e8eNate Begeman    Canonical = getExtVectorType(getCanonicalType(vecType), NumElts);
25931eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
259473322924127c873c13101b705dd823f5539ffa5fSteve Naroff    // Get the new insert position for the node we care about.
259573322924127c873c13101b705dd823f5539ffa5fSteve Naroff    VectorType *NewIP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos);
2596c6ed729f669044f5072a49d79041f455d971ece3Jeffrey Yasskin    assert(NewIP == 0 && "Shouldn't be in the map!"); (void)NewIP;
259773322924127c873c13101b705dd823f5539ffa5fSteve Naroff  }
25986b304a0254a13f42390b865ff5ba668a49cc58aeJohn McCall  ExtVectorType *New = new (*this, TypeAlignment)
25996b304a0254a13f42390b865ff5ba668a49cc58aeJohn McCall    ExtVectorType(vecType, NumElts, Canonical);
260073322924127c873c13101b705dd823f5539ffa5fSteve Naroff  VectorTypes.InsertNode(New, InsertPos);
260173322924127c873c13101b705dd823f5539ffa5fSteve Naroff  Types.push_back(New);
260273322924127c873c13101b705dd823f5539ffa5fSteve Naroff  return QualType(New, 0);
260373322924127c873c13101b705dd823f5539ffa5fSteve Naroff}
260473322924127c873c13101b705dd823f5539ffa5fSteve Naroff
26054ba2a17694148e16eaa8d3917f657ffcd3667be4Jay FoadQualType
26064ba2a17694148e16eaa8d3917f657ffcd3667be4Jay FoadASTContext::getDependentSizedExtVectorType(QualType vecType,
26074ba2a17694148e16eaa8d3917f657ffcd3667be4Jay Foad                                           Expr *SizeExpr,
26084ba2a17694148e16eaa8d3917f657ffcd3667be4Jay Foad                                           SourceLocation AttrLoc) const {
26092ec09f1dc123e1942ed756e8ee4fef86451eac9eDouglas Gregor  llvm::FoldingSetNodeID ID;
26101eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  DependentSizedExtVectorType::Profile(ID, *this, getCanonicalType(vecType),
26112ec09f1dc123e1942ed756e8ee4fef86451eac9eDouglas Gregor                                       SizeExpr);
26121eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
26132ec09f1dc123e1942ed756e8ee4fef86451eac9eDouglas Gregor  void *InsertPos = 0;
26142ec09f1dc123e1942ed756e8ee4fef86451eac9eDouglas Gregor  DependentSizedExtVectorType *Canon
26152ec09f1dc123e1942ed756e8ee4fef86451eac9eDouglas Gregor    = DependentSizedExtVectorTypes.FindNodeOrInsertPos(ID, InsertPos);
26162ec09f1dc123e1942ed756e8ee4fef86451eac9eDouglas Gregor  DependentSizedExtVectorType *New;
26172ec09f1dc123e1942ed756e8ee4fef86451eac9eDouglas Gregor  if (Canon) {
26182ec09f1dc123e1942ed756e8ee4fef86451eac9eDouglas Gregor    // We already have a canonical version of this array type; use it as
26192ec09f1dc123e1942ed756e8ee4fef86451eac9eDouglas Gregor    // the canonical type for a newly-built type.
26206b304a0254a13f42390b865ff5ba668a49cc58aeJohn McCall    New = new (*this, TypeAlignment)
26216b304a0254a13f42390b865ff5ba668a49cc58aeJohn McCall      DependentSizedExtVectorType(*this, vecType, QualType(Canon, 0),
26226b304a0254a13f42390b865ff5ba668a49cc58aeJohn McCall                                  SizeExpr, AttrLoc);
26232ec09f1dc123e1942ed756e8ee4fef86451eac9eDouglas Gregor  } else {
26242ec09f1dc123e1942ed756e8ee4fef86451eac9eDouglas Gregor    QualType CanonVecTy = getCanonicalType(vecType);
26252ec09f1dc123e1942ed756e8ee4fef86451eac9eDouglas Gregor    if (CanonVecTy == vecType) {
26266b304a0254a13f42390b865ff5ba668a49cc58aeJohn McCall      New = new (*this, TypeAlignment)
26276b304a0254a13f42390b865ff5ba668a49cc58aeJohn McCall        DependentSizedExtVectorType(*this, vecType, QualType(), SizeExpr,
26286b304a0254a13f42390b865ff5ba668a49cc58aeJohn McCall                                    AttrLoc);
2629789b1f640205e81b5af250693246120f1ce9d147Douglas Gregor
2630789b1f640205e81b5af250693246120f1ce9d147Douglas Gregor      DependentSizedExtVectorType *CanonCheck
2631789b1f640205e81b5af250693246120f1ce9d147Douglas Gregor        = DependentSizedExtVectorTypes.FindNodeOrInsertPos(ID, InsertPos);
2632789b1f640205e81b5af250693246120f1ce9d147Douglas Gregor      assert(!CanonCheck && "Dependent-sized ext_vector canonical type broken");
2633789b1f640205e81b5af250693246120f1ce9d147Douglas Gregor      (void)CanonCheck;
26342ec09f1dc123e1942ed756e8ee4fef86451eac9eDouglas Gregor      DependentSizedExtVectorTypes.InsertNode(New, InsertPos);
26352ec09f1dc123e1942ed756e8ee4fef86451eac9eDouglas Gregor    } else {
26362ec09f1dc123e1942ed756e8ee4fef86451eac9eDouglas Gregor      QualType Canon = getDependentSizedExtVectorType(CanonVecTy, SizeExpr,
26372ec09f1dc123e1942ed756e8ee4fef86451eac9eDouglas Gregor                                                      SourceLocation());
26386b304a0254a13f42390b865ff5ba668a49cc58aeJohn McCall      New = new (*this, TypeAlignment)
26396b304a0254a13f42390b865ff5ba668a49cc58aeJohn McCall        DependentSizedExtVectorType(*this, vecType, Canon, SizeExpr, AttrLoc);
26402ec09f1dc123e1942ed756e8ee4fef86451eac9eDouglas Gregor    }
26412ec09f1dc123e1942ed756e8ee4fef86451eac9eDouglas Gregor  }
26421eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
26439cdda0cf8528e3d595be9bfa002f0450074beb4dDouglas Gregor  Types.push_back(New);
26449cdda0cf8528e3d595be9bfa002f0450074beb4dDouglas Gregor  return QualType(New, 0);
26459cdda0cf8528e3d595be9bfa002f0450074beb4dDouglas Gregor}
26469cdda0cf8528e3d595be9bfa002f0450074beb4dDouglas Gregor
264772564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor/// getFunctionNoProtoType - Return a K&R style C function type like 'int()'.
26485f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer///
26494ba2a17694148e16eaa8d3917f657ffcd3667be4Jay FoadQualType
26504ba2a17694148e16eaa8d3917f657ffcd3667be4Jay FoadASTContext::getFunctionNoProtoType(QualType ResultTy,
26514ba2a17694148e16eaa8d3917f657ffcd3667be4Jay Foad                                   const FunctionType::ExtInfo &Info) const {
2652cfe9af250f466e7e38becea4428990448ae07737Roman Divacky  const CallingConv DefaultCC = Info.getCC();
2653cfe9af250f466e7e38becea4428990448ae07737Roman Divacky  const CallingConv CallConv = (LangOpts.MRTD && DefaultCC == CC_Default) ?
2654cfe9af250f466e7e38becea4428990448ae07737Roman Divacky                               CC_X86StdCall : DefaultCC;
26555f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // Unique functions, to guarantee there is only one function of a particular
26565f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // structure.
26575f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  llvm::FoldingSetNodeID ID;
2658264ba48dc98f3f843935a485d5b086f7e0fdc4f1Rafael Espindola  FunctionNoProtoType::Profile(ID, ResultTy, Info);
26591eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
26605f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  void *InsertPos = 0;
26611eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  if (FunctionNoProtoType *FT =
266272564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor        FunctionNoProtoTypes.FindNodeOrInsertPos(ID, InsertPos))
26635f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    return QualType(FT, 0);
26641eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
26655f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  QualType Canonical;
2666ab8bbf4ebd3e3e6eab913cb044772a62b7581941Douglas Gregor  if (!ResultTy.isCanonical() ||
266704a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall      getCanonicalCallConv(CallConv) != CallConv) {
2668264ba48dc98f3f843935a485d5b086f7e0fdc4f1Rafael Espindola    Canonical =
2669264ba48dc98f3f843935a485d5b086f7e0fdc4f1Rafael Espindola      getFunctionNoProtoType(getCanonicalType(ResultTy),
2670264ba48dc98f3f843935a485d5b086f7e0fdc4f1Rafael Espindola                     Info.withCallingConv(getCanonicalCallConv(CallConv)));
26711eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
26725f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    // Get the new insert position for the node we care about.
267372564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor    FunctionNoProtoType *NewIP =
267472564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor      FunctionNoProtoTypes.FindNodeOrInsertPos(ID, InsertPos);
2675c6ed729f669044f5072a49d79041f455d971ece3Jeffrey Yasskin    assert(NewIP == 0 && "Shouldn't be in the map!"); (void)NewIP;
26765f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
26771eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
2678cfe9af250f466e7e38becea4428990448ae07737Roman Divacky  FunctionProtoType::ExtInfo newInfo = Info.withCallingConv(CallConv);
26796b304a0254a13f42390b865ff5ba668a49cc58aeJohn McCall  FunctionNoProtoType *New = new (*this, TypeAlignment)
2680cfe9af250f466e7e38becea4428990448ae07737Roman Divacky    FunctionNoProtoType(ResultTy, Canonical, newInfo);
26815f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  Types.push_back(New);
268272564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor  FunctionNoProtoTypes.InsertNode(New, InsertPos);
26835f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  return QualType(New, 0);
26845f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
26855f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
268602dd79830979e6d83d4420377e8f4c9e4a77439bDouglas Gregor/// \brief Determine whether \p T is canonical as the result type of a function.
268702dd79830979e6d83d4420377e8f4c9e4a77439bDouglas Gregorstatic bool isCanonicalResultType(QualType T) {
268802dd79830979e6d83d4420377e8f4c9e4a77439bDouglas Gregor  return T.isCanonical() &&
268902dd79830979e6d83d4420377e8f4c9e4a77439bDouglas Gregor         (T.getObjCLifetime() == Qualifiers::OCL_None ||
269002dd79830979e6d83d4420377e8f4c9e4a77439bDouglas Gregor          T.getObjCLifetime() == Qualifiers::OCL_ExplicitNone);
269102dd79830979e6d83d4420377e8f4c9e4a77439bDouglas Gregor}
269202dd79830979e6d83d4420377e8f4c9e4a77439bDouglas Gregor
26935f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// getFunctionType - Return a normal function type with a typed argument
26945f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// list.  isVariadic indicates whether the argument list includes '...'.
26954ba2a17694148e16eaa8d3917f657ffcd3667be4Jay FoadQualType
2696bea522ff43a3f11c7a2bc7949119dbb9fce19e39Jordan RoseASTContext::getFunctionType(QualType ResultTy, ArrayRef<QualType> ArgArray,
26974ba2a17694148e16eaa8d3917f657ffcd3667be4Jay Foad                            const FunctionProtoType::ExtProtoInfo &EPI) const {
2698bea522ff43a3f11c7a2bc7949119dbb9fce19e39Jordan Rose  size_t NumArgs = ArgArray.size();
2699bea522ff43a3f11c7a2bc7949119dbb9fce19e39Jordan Rose
27005f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // Unique functions, to guarantee there is only one function of a particular
27015f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // structure.
27025f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  llvm::FoldingSetNodeID ID;
2703bea522ff43a3f11c7a2bc7949119dbb9fce19e39Jordan Rose  FunctionProtoType::Profile(ID, ResultTy, ArgArray.begin(), NumArgs, EPI,
2704bea522ff43a3f11c7a2bc7949119dbb9fce19e39Jordan Rose                             *this);
27055f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
27065f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  void *InsertPos = 0;
27071eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  if (FunctionProtoType *FTP =
270872564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor        FunctionProtoTypes.FindNodeOrInsertPos(ID, InsertPos))
27095f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    return QualType(FTP, 0);
2710465226e23a3008bd68973513dda1f9e3cd27dbddSebastian Redl
2711465226e23a3008bd68973513dda1f9e3cd27dbddSebastian Redl  // Determine whether the type being created is already canonical or not.
2712eefb3d5b49c844347f212073a7e975b8118fe8e9Richard Smith  bool isCanonical =
271302dd79830979e6d83d4420377e8f4c9e4a77439bDouglas Gregor    EPI.ExceptionSpecType == EST_None && isCanonicalResultType(ResultTy) &&
2714eefb3d5b49c844347f212073a7e975b8118fe8e9Richard Smith    !EPI.HasTrailingReturn;
27155f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  for (unsigned i = 0; i != NumArgs && isCanonical; ++i)
271654e14c4db764c0636160d26c5bbf491637c83a76John McCall    if (!ArgArray[i].isCanonicalAsParam())
27175f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      isCanonical = false;
27185f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
2719cfe9af250f466e7e38becea4428990448ae07737Roman Divacky  const CallingConv DefaultCC = EPI.ExtInfo.getCC();
2720cfe9af250f466e7e38becea4428990448ae07737Roman Divacky  const CallingConv CallConv = (LangOpts.MRTD && DefaultCC == CC_Default) ?
2721cfe9af250f466e7e38becea4428990448ae07737Roman Divacky                               CC_X86StdCall : DefaultCC;
2722e23cf437fe76b1ed02d63c3f61b456fd48a915f5John McCall
27235f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // If this type isn't canonical, get the canonical version of it.
2724465226e23a3008bd68973513dda1f9e3cd27dbddSebastian Redl  // The exception spec is not part of the canonical type.
27255f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  QualType Canonical;
272604a67a6aa3dfdc92d57f7f8d93ba397348c868a4John McCall  if (!isCanonical || getCanonicalCallConv(CallConv) != CallConv) {
27275f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner    SmallVector<QualType, 16> CanonicalArgs;
27285f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    CanonicalArgs.reserve(NumArgs);
27295f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    for (unsigned i = 0; i != NumArgs; ++i)
273054e14c4db764c0636160d26c5bbf491637c83a76John McCall      CanonicalArgs.push_back(getCanonicalParamType(ArgArray[i]));
2731465226e23a3008bd68973513dda1f9e3cd27dbddSebastian Redl
2732e23cf437fe76b1ed02d63c3f61b456fd48a915f5John McCall    FunctionProtoType::ExtProtoInfo CanonicalEPI = EPI;
2733eefb3d5b49c844347f212073a7e975b8118fe8e9Richard Smith    CanonicalEPI.HasTrailingReturn = false;
27348b5b4099c61a136e9a1714c4d8a593febe942268Sebastian Redl    CanonicalEPI.ExceptionSpecType = EST_None;
27358b5b4099c61a136e9a1714c4d8a593febe942268Sebastian Redl    CanonicalEPI.NumExceptions = 0;
2736e23cf437fe76b1ed02d63c3f61b456fd48a915f5John McCall    CanonicalEPI.ExtInfo
2737e23cf437fe76b1ed02d63c3f61b456fd48a915f5John McCall      = CanonicalEPI.ExtInfo.withCallingConv(getCanonicalCallConv(CallConv));
2738e23cf437fe76b1ed02d63c3f61b456fd48a915f5John McCall
273902dd79830979e6d83d4420377e8f4c9e4a77439bDouglas Gregor    // Result types do not have ARC lifetime qualifiers.
274002dd79830979e6d83d4420377e8f4c9e4a77439bDouglas Gregor    QualType CanResultTy = getCanonicalType(ResultTy);
274102dd79830979e6d83d4420377e8f4c9e4a77439bDouglas Gregor    if (ResultTy.getQualifiers().hasObjCLifetime()) {
274202dd79830979e6d83d4420377e8f4c9e4a77439bDouglas Gregor      Qualifiers Qs = CanResultTy.getQualifiers();
274302dd79830979e6d83d4420377e8f4c9e4a77439bDouglas Gregor      Qs.removeObjCLifetime();
274402dd79830979e6d83d4420377e8f4c9e4a77439bDouglas Gregor      CanResultTy = getQualifiedType(CanResultTy.getUnqualifiedType(), Qs);
274502dd79830979e6d83d4420377e8f4c9e4a77439bDouglas Gregor    }
274602dd79830979e6d83d4420377e8f4c9e4a77439bDouglas Gregor
2747bea522ff43a3f11c7a2bc7949119dbb9fce19e39Jordan Rose    Canonical = getFunctionType(CanResultTy, CanonicalArgs, CanonicalEPI);
2748465226e23a3008bd68973513dda1f9e3cd27dbddSebastian Redl
27495f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    // Get the new insert position for the node we care about.
275072564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor    FunctionProtoType *NewIP =
275172564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor      FunctionProtoTypes.FindNodeOrInsertPos(ID, InsertPos);
2752c6ed729f669044f5072a49d79041f455d971ece3Jeffrey Yasskin    assert(NewIP == 0 && "Shouldn't be in the map!"); (void)NewIP;
27535f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
2754465226e23a3008bd68973513dda1f9e3cd27dbddSebastian Redl
2755f85e193739c953358c865005855253af4f68a497John McCall  // FunctionProtoType objects are allocated with extra bytes after
2756f85e193739c953358c865005855253af4f68a497John McCall  // them for three variable size arrays at the end:
2757f85e193739c953358c865005855253af4f68a497John McCall  //  - parameter types
2758f85e193739c953358c865005855253af4f68a497John McCall  //  - exception types
2759f85e193739c953358c865005855253af4f68a497John McCall  //  - consumed-arguments flags
2760f85e193739c953358c865005855253af4f68a497John McCall  // Instead of the exception types, there could be a noexcept
2761b9d0b76e42fd2d4cdfd135220302458d03ad09feRichard Smith  // expression, or information used to resolve the exception
2762b9d0b76e42fd2d4cdfd135220302458d03ad09feRichard Smith  // specification.
2763e23cf437fe76b1ed02d63c3f61b456fd48a915f5John McCall  size_t Size = sizeof(FunctionProtoType) +
276460618fa7f88d5162bb5b40988b6b38d4d75d6fc6Sebastian Redl                NumArgs * sizeof(QualType);
2765b9d0b76e42fd2d4cdfd135220302458d03ad09feRichard Smith  if (EPI.ExceptionSpecType == EST_Dynamic) {
276660618fa7f88d5162bb5b40988b6b38d4d75d6fc6Sebastian Redl    Size += EPI.NumExceptions * sizeof(QualType);
2767b9d0b76e42fd2d4cdfd135220302458d03ad09feRichard Smith  } else if (EPI.ExceptionSpecType == EST_ComputedNoexcept) {
27688026f6d82f7fa544bc0453714fe94bca62a1196eSebastian Redl    Size += sizeof(Expr*);
2769e6975e9b0985ad7f7ff9187e38d95bfe9ac4181bRichard Smith  } else if (EPI.ExceptionSpecType == EST_Uninstantiated) {
277013bffc532bafd45d4a77867993c1afb83c7661beRichard Smith    Size += 2 * sizeof(FunctionDecl*);
2771b9d0b76e42fd2d4cdfd135220302458d03ad09feRichard Smith  } else if (EPI.ExceptionSpecType == EST_Unevaluated) {
2772b9d0b76e42fd2d4cdfd135220302458d03ad09feRichard Smith    Size += sizeof(FunctionDecl*);
277360618fa7f88d5162bb5b40988b6b38d4d75d6fc6Sebastian Redl  }
2774f85e193739c953358c865005855253af4f68a497John McCall  if (EPI.ConsumedArguments)
2775f85e193739c953358c865005855253af4f68a497John McCall    Size += NumArgs * sizeof(bool);
2776f85e193739c953358c865005855253af4f68a497John McCall
2777e23cf437fe76b1ed02d63c3f61b456fd48a915f5John McCall  FunctionProtoType *FTP = (FunctionProtoType*) Allocate(Size, TypeAlignment);
2778cfe9af250f466e7e38becea4428990448ae07737Roman Divacky  FunctionProtoType::ExtProtoInfo newEPI = EPI;
2779cfe9af250f466e7e38becea4428990448ae07737Roman Divacky  newEPI.ExtInfo = EPI.ExtInfo.withCallingConv(CallConv);
2780bea522ff43a3f11c7a2bc7949119dbb9fce19e39Jordan Rose  new (FTP) FunctionProtoType(ResultTy, ArgArray, Canonical, newEPI);
27815f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  Types.push_back(FTP);
278272564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor  FunctionProtoTypes.InsertNode(FTP, InsertPos);
27835f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  return QualType(FTP, 0);
27845f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
27855f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
27863cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall#ifndef NDEBUG
27873cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCallstatic bool NeedsInjectedClassNameType(const RecordDecl *D) {
27883cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall  if (!isa<CXXRecordDecl>(D)) return false;
27893cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall  const CXXRecordDecl *RD = cast<CXXRecordDecl>(D);
27903cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall  if (isa<ClassTemplatePartialSpecializationDecl>(RD))
27913cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall    return true;
27923cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall  if (RD->getDescribedClassTemplate() &&
27933cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall      !isa<ClassTemplateSpecializationDecl>(RD))
27943cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall    return true;
27953cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall  return false;
27963cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall}
27973cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall#endif
27983cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall
27993cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall/// getInjectedClassNameType - Return the unique reference to the
28003cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall/// injected class name type for the specified templated declaration.
28013cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCallQualType ASTContext::getInjectedClassNameType(CXXRecordDecl *Decl,
28024ba2a17694148e16eaa8d3917f657ffcd3667be4Jay Foad                                              QualType TST) const {
28033cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall  assert(NeedsInjectedClassNameType(Decl));
28043cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall  if (Decl->TypeForDecl) {
28053cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall    assert(isa<InjectedClassNameType>(Decl->TypeForDecl));
2806ef96ee0be5f100789f451641542a69cd719144d2Douglas Gregor  } else if (CXXRecordDecl *PrevDecl = Decl->getPreviousDecl()) {
28073cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall    assert(PrevDecl->TypeForDecl && "previous declaration has no type");
28083cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall    Decl->TypeForDecl = PrevDecl->TypeForDecl;
28093cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall    assert(isa<InjectedClassNameType>(Decl->TypeForDecl));
28103cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall  } else {
2811f4c7371fb1d3cebcfb40abad4537bb82515704eaJohn McCall    Type *newType =
281231f17ecbef57b5679c017c375db330546b7b5145John McCall      new (*this, TypeAlignment) InjectedClassNameType(Decl, TST);
2813f4c7371fb1d3cebcfb40abad4537bb82515704eaJohn McCall    Decl->TypeForDecl = newType;
2814f4c7371fb1d3cebcfb40abad4537bb82515704eaJohn McCall    Types.push_back(newType);
28153cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall  }
28163cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall  return QualType(Decl->TypeForDecl, 0);
28173cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall}
28183cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall
28192ce52f3fb95bf544db6bd3d91a72bce7d9cceb6cDouglas Gregor/// getTypeDeclType - Return the unique reference to the type for the
28202ce52f3fb95bf544db6bd3d91a72bce7d9cceb6cDouglas Gregor/// specified type declaration.
28214ba2a17694148e16eaa8d3917f657ffcd3667be4Jay FoadQualType ASTContext::getTypeDeclTypeSlow(const TypeDecl *Decl) const {
28221e6759e9e33dcaa73ce14c8a908ac9f87ac16463Argyrios Kyrtzidis  assert(Decl && "Passed null for Decl param");
2823becb8d5a6ab5103393eac5344ae69bcb860601ddJohn McCall  assert(!Decl->TypeForDecl && "TypeForDecl present in slow case");
28241eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
2825162e1c1b487352434552147967c3dd296ebee2f7Richard Smith  if (const TypedefNameDecl *Typedef = dyn_cast<TypedefNameDecl>(Decl))
28262ce52f3fb95bf544db6bd3d91a72bce7d9cceb6cDouglas Gregor    return getTypedefType(Typedef);
2827becb8d5a6ab5103393eac5344ae69bcb860601ddJohn McCall
2828becb8d5a6ab5103393eac5344ae69bcb860601ddJohn McCall  assert(!isa<TemplateTypeParmDecl>(Decl) &&
2829becb8d5a6ab5103393eac5344ae69bcb860601ddJohn McCall         "Template type parameter types are always available.");
2830becb8d5a6ab5103393eac5344ae69bcb860601ddJohn McCall
283119c8576b7328f4dc2d07682f5da552875c1912efJohn McCall  if (const RecordDecl *Record = dyn_cast<RecordDecl>(Decl)) {
2832ef96ee0be5f100789f451641542a69cd719144d2Douglas Gregor    assert(!Record->getPreviousDecl() &&
2833becb8d5a6ab5103393eac5344ae69bcb860601ddJohn McCall           "struct/union has previous declaration");
2834becb8d5a6ab5103393eac5344ae69bcb860601ddJohn McCall    assert(!NeedsInjectedClassNameType(Record));
2835400f5125e2432d648f2c8a31b36a7f318a880c47Argyrios Kyrtzidis    return getRecordType(Record);
283619c8576b7328f4dc2d07682f5da552875c1912efJohn McCall  } else if (const EnumDecl *Enum = dyn_cast<EnumDecl>(Decl)) {
2837ef96ee0be5f100789f451641542a69cd719144d2Douglas Gregor    assert(!Enum->getPreviousDecl() &&
2838becb8d5a6ab5103393eac5344ae69bcb860601ddJohn McCall           "enum has previous declaration");
2839400f5125e2432d648f2c8a31b36a7f318a880c47Argyrios Kyrtzidis    return getEnumType(Enum);
284019c8576b7328f4dc2d07682f5da552875c1912efJohn McCall  } else if (const UnresolvedUsingTypenameDecl *Using =
2841ed97649e9574b9d854fa4d6109c9333ae0993554John McCall               dyn_cast<UnresolvedUsingTypenameDecl>(Decl)) {
2842f4c7371fb1d3cebcfb40abad4537bb82515704eaJohn McCall    Type *newType = new (*this, TypeAlignment) UnresolvedUsingType(Using);
2843f4c7371fb1d3cebcfb40abad4537bb82515704eaJohn McCall    Decl->TypeForDecl = newType;
2844f4c7371fb1d3cebcfb40abad4537bb82515704eaJohn McCall    Types.push_back(newType);
28459fdbab3cbc2fc04bcaf5768023d83707f3151144Mike Stump  } else
2846becb8d5a6ab5103393eac5344ae69bcb860601ddJohn McCall    llvm_unreachable("TypeDecl without a type?");
284749aa7ff1245abd03e6e998e01302df31e4c6f8f6Argyrios Kyrtzidis
284849aa7ff1245abd03e6e998e01302df31e4c6f8f6Argyrios Kyrtzidis  return QualType(Decl->TypeForDecl, 0);
28492ce52f3fb95bf544db6bd3d91a72bce7d9cceb6cDouglas Gregor}
28502ce52f3fb95bf544db6bd3d91a72bce7d9cceb6cDouglas Gregor
28515f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// getTypedefType - Return the unique reference to the type for the
2852162e1c1b487352434552147967c3dd296ebee2f7Richard Smith/// specified typedef name decl.
28539763e221e16026ddf487d2564ed349d2c874a1a1Argyrios KyrtzidisQualType
2854162e1c1b487352434552147967c3dd296ebee2f7Richard SmithASTContext::getTypedefType(const TypedefNameDecl *Decl,
2855162e1c1b487352434552147967c3dd296ebee2f7Richard Smith                           QualType Canonical) const {
28565f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  if (Decl->TypeForDecl) return QualType(Decl->TypeForDecl, 0);
28571eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
28589763e221e16026ddf487d2564ed349d2c874a1a1Argyrios Kyrtzidis  if (Canonical.isNull())
28599763e221e16026ddf487d2564ed349d2c874a1a1Argyrios Kyrtzidis    Canonical = getCanonicalType(Decl->getUnderlyingType());
2860f4c7371fb1d3cebcfb40abad4537bb82515704eaJohn McCall  TypedefType *newType = new(*this, TypeAlignment)
28616b304a0254a13f42390b865ff5ba668a49cc58aeJohn McCall    TypedefType(Type::Typedef, Decl, Canonical);
2862f4c7371fb1d3cebcfb40abad4537bb82515704eaJohn McCall  Decl->TypeForDecl = newType;
2863f4c7371fb1d3cebcfb40abad4537bb82515704eaJohn McCall  Types.push_back(newType);
2864f4c7371fb1d3cebcfb40abad4537bb82515704eaJohn McCall  return QualType(newType, 0);
28655f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
28665f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
28674ba2a17694148e16eaa8d3917f657ffcd3667be4Jay FoadQualType ASTContext::getRecordType(const RecordDecl *Decl) const {
2868400f5125e2432d648f2c8a31b36a7f318a880c47Argyrios Kyrtzidis  if (Decl->TypeForDecl) return QualType(Decl->TypeForDecl, 0);
2869400f5125e2432d648f2c8a31b36a7f318a880c47Argyrios Kyrtzidis
2870ef96ee0be5f100789f451641542a69cd719144d2Douglas Gregor  if (const RecordDecl *PrevDecl = Decl->getPreviousDecl())
2871400f5125e2432d648f2c8a31b36a7f318a880c47Argyrios Kyrtzidis    if (PrevDecl->TypeForDecl)
2872400f5125e2432d648f2c8a31b36a7f318a880c47Argyrios Kyrtzidis      return QualType(Decl->TypeForDecl = PrevDecl->TypeForDecl, 0);
2873400f5125e2432d648f2c8a31b36a7f318a880c47Argyrios Kyrtzidis
2874f4c7371fb1d3cebcfb40abad4537bb82515704eaJohn McCall  RecordType *newType = new (*this, TypeAlignment) RecordType(Decl);
2875f4c7371fb1d3cebcfb40abad4537bb82515704eaJohn McCall  Decl->TypeForDecl = newType;
2876f4c7371fb1d3cebcfb40abad4537bb82515704eaJohn McCall  Types.push_back(newType);
2877f4c7371fb1d3cebcfb40abad4537bb82515704eaJohn McCall  return QualType(newType, 0);
2878400f5125e2432d648f2c8a31b36a7f318a880c47Argyrios Kyrtzidis}
2879400f5125e2432d648f2c8a31b36a7f318a880c47Argyrios Kyrtzidis
28804ba2a17694148e16eaa8d3917f657ffcd3667be4Jay FoadQualType ASTContext::getEnumType(const EnumDecl *Decl) const {
2881400f5125e2432d648f2c8a31b36a7f318a880c47Argyrios Kyrtzidis  if (Decl->TypeForDecl) return QualType(Decl->TypeForDecl, 0);
2882400f5125e2432d648f2c8a31b36a7f318a880c47Argyrios Kyrtzidis
2883ef96ee0be5f100789f451641542a69cd719144d2Douglas Gregor  if (const EnumDecl *PrevDecl = Decl->getPreviousDecl())
2884400f5125e2432d648f2c8a31b36a7f318a880c47Argyrios Kyrtzidis    if (PrevDecl->TypeForDecl)
2885400f5125e2432d648f2c8a31b36a7f318a880c47Argyrios Kyrtzidis      return QualType(Decl->TypeForDecl = PrevDecl->TypeForDecl, 0);
2886400f5125e2432d648f2c8a31b36a7f318a880c47Argyrios Kyrtzidis
2887f4c7371fb1d3cebcfb40abad4537bb82515704eaJohn McCall  EnumType *newType = new (*this, TypeAlignment) EnumType(Decl);
2888f4c7371fb1d3cebcfb40abad4537bb82515704eaJohn McCall  Decl->TypeForDecl = newType;
2889f4c7371fb1d3cebcfb40abad4537bb82515704eaJohn McCall  Types.push_back(newType);
2890f4c7371fb1d3cebcfb40abad4537bb82515704eaJohn McCall  return QualType(newType, 0);
2891400f5125e2432d648f2c8a31b36a7f318a880c47Argyrios Kyrtzidis}
2892400f5125e2432d648f2c8a31b36a7f318a880c47Argyrios Kyrtzidis
28939d156a7b1b2771e191f2f5a45a7b7a694129463bJohn McCallQualType ASTContext::getAttributedType(AttributedType::Kind attrKind,
28949d156a7b1b2771e191f2f5a45a7b7a694129463bJohn McCall                                       QualType modifiedType,
28959d156a7b1b2771e191f2f5a45a7b7a694129463bJohn McCall                                       QualType equivalentType) {
28969d156a7b1b2771e191f2f5a45a7b7a694129463bJohn McCall  llvm::FoldingSetNodeID id;
28979d156a7b1b2771e191f2f5a45a7b7a694129463bJohn McCall  AttributedType::Profile(id, attrKind, modifiedType, equivalentType);
28989d156a7b1b2771e191f2f5a45a7b7a694129463bJohn McCall
28999d156a7b1b2771e191f2f5a45a7b7a694129463bJohn McCall  void *insertPos = 0;
29009d156a7b1b2771e191f2f5a45a7b7a694129463bJohn McCall  AttributedType *type = AttributedTypes.FindNodeOrInsertPos(id, insertPos);
29019d156a7b1b2771e191f2f5a45a7b7a694129463bJohn McCall  if (type) return QualType(type, 0);
29029d156a7b1b2771e191f2f5a45a7b7a694129463bJohn McCall
29039d156a7b1b2771e191f2f5a45a7b7a694129463bJohn McCall  QualType canon = getCanonicalType(equivalentType);
29049d156a7b1b2771e191f2f5a45a7b7a694129463bJohn McCall  type = new (*this, TypeAlignment)
29059d156a7b1b2771e191f2f5a45a7b7a694129463bJohn McCall           AttributedType(canon, attrKind, modifiedType, equivalentType);
29069d156a7b1b2771e191f2f5a45a7b7a694129463bJohn McCall
29079d156a7b1b2771e191f2f5a45a7b7a694129463bJohn McCall  Types.push_back(type);
29089d156a7b1b2771e191f2f5a45a7b7a694129463bJohn McCall  AttributedTypes.InsertNode(type, insertPos);
29099d156a7b1b2771e191f2f5a45a7b7a694129463bJohn McCall
29109d156a7b1b2771e191f2f5a45a7b7a694129463bJohn McCall  return QualType(type, 0);
29119d156a7b1b2771e191f2f5a45a7b7a694129463bJohn McCall}
29129d156a7b1b2771e191f2f5a45a7b7a694129463bJohn McCall
29139d156a7b1b2771e191f2f5a45a7b7a694129463bJohn McCall
291449a832bd499d6f61c23655f1fac99f0dd229756eJohn McCall/// \brief Retrieve a substitution-result type.
291549a832bd499d6f61c23655f1fac99f0dd229756eJohn McCallQualType
291649a832bd499d6f61c23655f1fac99f0dd229756eJohn McCallASTContext::getSubstTemplateTypeParmType(const TemplateTypeParmType *Parm,
29174ba2a17694148e16eaa8d3917f657ffcd3667be4Jay Foad                                         QualType Replacement) const {
2918467b27b9a24bdc823218ad1ad0e37673b6cc1e83John McCall  assert(Replacement.isCanonical()
291949a832bd499d6f61c23655f1fac99f0dd229756eJohn McCall         && "replacement types must always be canonical");
292049a832bd499d6f61c23655f1fac99f0dd229756eJohn McCall
292149a832bd499d6f61c23655f1fac99f0dd229756eJohn McCall  llvm::FoldingSetNodeID ID;
292249a832bd499d6f61c23655f1fac99f0dd229756eJohn McCall  SubstTemplateTypeParmType::Profile(ID, Parm, Replacement);
292349a832bd499d6f61c23655f1fac99f0dd229756eJohn McCall  void *InsertPos = 0;
292449a832bd499d6f61c23655f1fac99f0dd229756eJohn McCall  SubstTemplateTypeParmType *SubstParm
292549a832bd499d6f61c23655f1fac99f0dd229756eJohn McCall    = SubstTemplateTypeParmTypes.FindNodeOrInsertPos(ID, InsertPos);
292649a832bd499d6f61c23655f1fac99f0dd229756eJohn McCall
292749a832bd499d6f61c23655f1fac99f0dd229756eJohn McCall  if (!SubstParm) {
292849a832bd499d6f61c23655f1fac99f0dd229756eJohn McCall    SubstParm = new (*this, TypeAlignment)
292949a832bd499d6f61c23655f1fac99f0dd229756eJohn McCall      SubstTemplateTypeParmType(Parm, Replacement);
293049a832bd499d6f61c23655f1fac99f0dd229756eJohn McCall    Types.push_back(SubstParm);
293149a832bd499d6f61c23655f1fac99f0dd229756eJohn McCall    SubstTemplateTypeParmTypes.InsertNode(SubstParm, InsertPos);
293249a832bd499d6f61c23655f1fac99f0dd229756eJohn McCall  }
293349a832bd499d6f61c23655f1fac99f0dd229756eJohn McCall
293449a832bd499d6f61c23655f1fac99f0dd229756eJohn McCall  return QualType(SubstParm, 0);
293549a832bd499d6f61c23655f1fac99f0dd229756eJohn McCall}
293649a832bd499d6f61c23655f1fac99f0dd229756eJohn McCall
2937c3069d618f4661d923cb1b5c4525b082fce73b04Douglas Gregor/// \brief Retrieve a
2938c3069d618f4661d923cb1b5c4525b082fce73b04Douglas GregorQualType ASTContext::getSubstTemplateTypeParmPackType(
2939c3069d618f4661d923cb1b5c4525b082fce73b04Douglas Gregor                                          const TemplateTypeParmType *Parm,
2940c3069d618f4661d923cb1b5c4525b082fce73b04Douglas Gregor                                              const TemplateArgument &ArgPack) {
2941c3069d618f4661d923cb1b5c4525b082fce73b04Douglas Gregor#ifndef NDEBUG
2942c3069d618f4661d923cb1b5c4525b082fce73b04Douglas Gregor  for (TemplateArgument::pack_iterator P = ArgPack.pack_begin(),
2943c3069d618f4661d923cb1b5c4525b082fce73b04Douglas Gregor                                    PEnd = ArgPack.pack_end();
2944c3069d618f4661d923cb1b5c4525b082fce73b04Douglas Gregor       P != PEnd; ++P) {
2945c3069d618f4661d923cb1b5c4525b082fce73b04Douglas Gregor    assert(P->getKind() == TemplateArgument::Type &&"Pack contains a non-type");
2946c3069d618f4661d923cb1b5c4525b082fce73b04Douglas Gregor    assert(P->getAsType().isCanonical() && "Pack contains non-canonical type");
2947c3069d618f4661d923cb1b5c4525b082fce73b04Douglas Gregor  }
2948c3069d618f4661d923cb1b5c4525b082fce73b04Douglas Gregor#endif
2949c3069d618f4661d923cb1b5c4525b082fce73b04Douglas Gregor
2950c3069d618f4661d923cb1b5c4525b082fce73b04Douglas Gregor  llvm::FoldingSetNodeID ID;
2951c3069d618f4661d923cb1b5c4525b082fce73b04Douglas Gregor  SubstTemplateTypeParmPackType::Profile(ID, Parm, ArgPack);
2952c3069d618f4661d923cb1b5c4525b082fce73b04Douglas Gregor  void *InsertPos = 0;
2953c3069d618f4661d923cb1b5c4525b082fce73b04Douglas Gregor  if (SubstTemplateTypeParmPackType *SubstParm
2954c3069d618f4661d923cb1b5c4525b082fce73b04Douglas Gregor        = SubstTemplateTypeParmPackTypes.FindNodeOrInsertPos(ID, InsertPos))
2955c3069d618f4661d923cb1b5c4525b082fce73b04Douglas Gregor    return QualType(SubstParm, 0);
2956c3069d618f4661d923cb1b5c4525b082fce73b04Douglas Gregor
2957c3069d618f4661d923cb1b5c4525b082fce73b04Douglas Gregor  QualType Canon;
2958c3069d618f4661d923cb1b5c4525b082fce73b04Douglas Gregor  if (!Parm->isCanonicalUnqualified()) {
2959c3069d618f4661d923cb1b5c4525b082fce73b04Douglas Gregor    Canon = getCanonicalType(QualType(Parm, 0));
2960c3069d618f4661d923cb1b5c4525b082fce73b04Douglas Gregor    Canon = getSubstTemplateTypeParmPackType(cast<TemplateTypeParmType>(Canon),
2961c3069d618f4661d923cb1b5c4525b082fce73b04Douglas Gregor                                             ArgPack);
2962c3069d618f4661d923cb1b5c4525b082fce73b04Douglas Gregor    SubstTemplateTypeParmPackTypes.FindNodeOrInsertPos(ID, InsertPos);
2963c3069d618f4661d923cb1b5c4525b082fce73b04Douglas Gregor  }
2964c3069d618f4661d923cb1b5c4525b082fce73b04Douglas Gregor
2965c3069d618f4661d923cb1b5c4525b082fce73b04Douglas Gregor  SubstTemplateTypeParmPackType *SubstParm
2966c3069d618f4661d923cb1b5c4525b082fce73b04Douglas Gregor    = new (*this, TypeAlignment) SubstTemplateTypeParmPackType(Parm, Canon,
2967c3069d618f4661d923cb1b5c4525b082fce73b04Douglas Gregor                                                               ArgPack);
2968c3069d618f4661d923cb1b5c4525b082fce73b04Douglas Gregor  Types.push_back(SubstParm);
2969c3069d618f4661d923cb1b5c4525b082fce73b04Douglas Gregor  SubstTemplateTypeParmTypes.InsertNode(SubstParm, InsertPos);
2970c3069d618f4661d923cb1b5c4525b082fce73b04Douglas Gregor  return QualType(SubstParm, 0);
2971c3069d618f4661d923cb1b5c4525b082fce73b04Douglas Gregor}
2972c3069d618f4661d923cb1b5c4525b082fce73b04Douglas Gregor
2973fab9d67cebb87be968e7ae31a3b549a5279b5d51Douglas Gregor/// \brief Retrieve the template type parameter type for a template
29741eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// parameter or parameter pack with the given depth, index, and (optionally)
297576e4ce42a30cee4dc40ce7c6014874fbc4f9baa7Anders Carlsson/// name.
29761eb4433ac451dc16f4133a88af2d002ac26c58efMike StumpQualType ASTContext::getTemplateTypeParmType(unsigned Depth, unsigned Index,
297776e4ce42a30cee4dc40ce7c6014874fbc4f9baa7Anders Carlsson                                             bool ParameterPack,
29784fb86f8c4585e53c21c847ad3de9e3b2de123cd9Chandler Carruth                                             TemplateTypeParmDecl *TTPDecl) const {
2979fab9d67cebb87be968e7ae31a3b549a5279b5d51Douglas Gregor  llvm::FoldingSetNodeID ID;
29804fb86f8c4585e53c21c847ad3de9e3b2de123cd9Chandler Carruth  TemplateTypeParmType::Profile(ID, Depth, Index, ParameterPack, TTPDecl);
2981fab9d67cebb87be968e7ae31a3b549a5279b5d51Douglas Gregor  void *InsertPos = 0;
29821eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  TemplateTypeParmType *TypeParm
2983fab9d67cebb87be968e7ae31a3b549a5279b5d51Douglas Gregor    = TemplateTypeParmTypes.FindNodeOrInsertPos(ID, InsertPos);
2984fab9d67cebb87be968e7ae31a3b549a5279b5d51Douglas Gregor
2985fab9d67cebb87be968e7ae31a3b549a5279b5d51Douglas Gregor  if (TypeParm)
2986fab9d67cebb87be968e7ae31a3b549a5279b5d51Douglas Gregor    return QualType(TypeParm, 0);
29871eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
29884fb86f8c4585e53c21c847ad3de9e3b2de123cd9Chandler Carruth  if (TTPDecl) {
298976e4ce42a30cee4dc40ce7c6014874fbc4f9baa7Anders Carlsson    QualType Canon = getTemplateTypeParmType(Depth, Index, ParameterPack);
29904fb86f8c4585e53c21c847ad3de9e3b2de123cd9Chandler Carruth    TypeParm = new (*this, TypeAlignment) TemplateTypeParmType(TTPDecl, Canon);
2991789b1f640205e81b5af250693246120f1ce9d147Douglas Gregor
2992789b1f640205e81b5af250693246120f1ce9d147Douglas Gregor    TemplateTypeParmType *TypeCheck
2993789b1f640205e81b5af250693246120f1ce9d147Douglas Gregor      = TemplateTypeParmTypes.FindNodeOrInsertPos(ID, InsertPos);
2994789b1f640205e81b5af250693246120f1ce9d147Douglas Gregor    assert(!TypeCheck && "Template type parameter canonical type broken");
2995789b1f640205e81b5af250693246120f1ce9d147Douglas Gregor    (void)TypeCheck;
299676e4ce42a30cee4dc40ce7c6014874fbc4f9baa7Anders Carlsson  } else
29976b304a0254a13f42390b865ff5ba668a49cc58aeJohn McCall    TypeParm = new (*this, TypeAlignment)
29986b304a0254a13f42390b865ff5ba668a49cc58aeJohn McCall      TemplateTypeParmType(Depth, Index, ParameterPack);
2999fab9d67cebb87be968e7ae31a3b549a5279b5d51Douglas Gregor
3000fab9d67cebb87be968e7ae31a3b549a5279b5d51Douglas Gregor  Types.push_back(TypeParm);
3001fab9d67cebb87be968e7ae31a3b549a5279b5d51Douglas Gregor  TemplateTypeParmTypes.InsertNode(TypeParm, InsertPos);
3002fab9d67cebb87be968e7ae31a3b549a5279b5d51Douglas Gregor
3003fab9d67cebb87be968e7ae31a3b549a5279b5d51Douglas Gregor  return QualType(TypeParm, 0);
3004fab9d67cebb87be968e7ae31a3b549a5279b5d51Douglas Gregor}
3005fab9d67cebb87be968e7ae31a3b549a5279b5d51Douglas Gregor
30063cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCallTypeSourceInfo *
30073cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCallASTContext::getTemplateSpecializationTypeInfo(TemplateName Name,
30083cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall                                              SourceLocation NameLoc,
30093cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall                                        const TemplateArgumentListInfo &Args,
30103e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith                                              QualType Underlying) const {
30117c3179cf463c3b3b8c21dbb955f933ba50b74f28Douglas Gregor  assert(!Name.getAsDependentTemplateName() &&
30127c3179cf463c3b3b8c21dbb955f933ba50b74f28Douglas Gregor         "No dependent template names here!");
30133e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith  QualType TST = getTemplateSpecializationType(Name, Args, Underlying);
30143cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall
30153cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall  TypeSourceInfo *DI = CreateTypeSourceInfo(TST);
301639e6ab4be93d9c5e729a578ddd9d415cd2d49872David Blaikie  TemplateSpecializationTypeLoc TL =
301739e6ab4be93d9c5e729a578ddd9d415cd2d49872David Blaikie      DI->getTypeLoc().castAs<TemplateSpecializationTypeLoc>();
301855d23c925b058be29b792008ddb7d68f6c4fa9a0Abramo Bagnara  TL.setTemplateKeywordLoc(SourceLocation());
30193cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall  TL.setTemplateNameLoc(NameLoc);
30203cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall  TL.setLAngleLoc(Args.getLAngleLoc());
30213cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall  TL.setRAngleLoc(Args.getRAngleLoc());
30223cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall  for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i)
30233cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall    TL.setArgLocInfo(i, Args[i].getLocInfo());
30243cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall  return DI;
30253cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall}
30263cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall
30271eb4433ac451dc16f4133a88af2d002ac26c58efMike StumpQualType
30287532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas GregorASTContext::getTemplateSpecializationType(TemplateName Template,
3029d5532b6cfff2977e0c59fa6ead7f7973984a620dJohn McCall                                          const TemplateArgumentListInfo &Args,
30303e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith                                          QualType Underlying) const {
30317c3179cf463c3b3b8c21dbb955f933ba50b74f28Douglas Gregor  assert(!Template.getAsDependentTemplateName() &&
30327c3179cf463c3b3b8c21dbb955f933ba50b74f28Douglas Gregor         "No dependent template names here!");
30337c3179cf463c3b3b8c21dbb955f933ba50b74f28Douglas Gregor
3034d5532b6cfff2977e0c59fa6ead7f7973984a620dJohn McCall  unsigned NumArgs = Args.size();
3035d5532b6cfff2977e0c59fa6ead7f7973984a620dJohn McCall
30365f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner  SmallVector<TemplateArgument, 4> ArgVec;
3037833ca991c1bfc967f0995974ca86f66ba1f666b5John McCall  ArgVec.reserve(NumArgs);
3038833ca991c1bfc967f0995974ca86f66ba1f666b5John McCall  for (unsigned i = 0; i != NumArgs; ++i)
3039833ca991c1bfc967f0995974ca86f66ba1f666b5John McCall    ArgVec.push_back(Args[i].getArgument());
3040833ca991c1bfc967f0995974ca86f66ba1f666b5John McCall
304131f17ecbef57b5679c017c375db330546b7b5145John McCall  return getTemplateSpecializationType(Template, ArgVec.data(), NumArgs,
30423e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith                                       Underlying);
3043833ca991c1bfc967f0995974ca86f66ba1f666b5John McCall}
3044833ca991c1bfc967f0995974ca86f66ba1f666b5John McCall
3045b70126a328f89937f46db42f9e3cba1592887c91Douglas Gregor#ifndef NDEBUG
3046b70126a328f89937f46db42f9e3cba1592887c91Douglas Gregorstatic bool hasAnyPackExpansions(const TemplateArgument *Args,
3047b70126a328f89937f46db42f9e3cba1592887c91Douglas Gregor                                 unsigned NumArgs) {
3048b70126a328f89937f46db42f9e3cba1592887c91Douglas Gregor  for (unsigned I = 0; I != NumArgs; ++I)
3049b70126a328f89937f46db42f9e3cba1592887c91Douglas Gregor    if (Args[I].isPackExpansion())
3050b70126a328f89937f46db42f9e3cba1592887c91Douglas Gregor      return true;
3051b70126a328f89937f46db42f9e3cba1592887c91Douglas Gregor
3052b70126a328f89937f46db42f9e3cba1592887c91Douglas Gregor  return true;
3053b70126a328f89937f46db42f9e3cba1592887c91Douglas Gregor}
3054b70126a328f89937f46db42f9e3cba1592887c91Douglas Gregor#endif
3055b70126a328f89937f46db42f9e3cba1592887c91Douglas Gregor
3056833ca991c1bfc967f0995974ca86f66ba1f666b5John McCallQualType
3057833ca991c1bfc967f0995974ca86f66ba1f666b5John McCallASTContext::getTemplateSpecializationType(TemplateName Template,
30587532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor                                          const TemplateArgument *Args,
30597532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor                                          unsigned NumArgs,
30603e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith                                          QualType Underlying) const {
30617c3179cf463c3b3b8c21dbb955f933ba50b74f28Douglas Gregor  assert(!Template.getAsDependentTemplateName() &&
30627c3179cf463c3b3b8c21dbb955f933ba50b74f28Douglas Gregor         "No dependent template names here!");
30630f0ea2a96534c615ff5fdd81363989b23cf2164aDouglas Gregor  // Look through qualified template names.
30640f0ea2a96534c615ff5fdd81363989b23cf2164aDouglas Gregor  if (QualifiedTemplateName *QTN = Template.getAsQualifiedTemplateName())
30650f0ea2a96534c615ff5fdd81363989b23cf2164aDouglas Gregor    Template = TemplateName(QTN->getTemplateDecl());
30667c3179cf463c3b3b8c21dbb955f933ba50b74f28Douglas Gregor
3067b70126a328f89937f46db42f9e3cba1592887c91Douglas Gregor  bool IsTypeAlias =
30683e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith    Template.getAsTemplateDecl() &&
30693e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith    isa<TypeAliasTemplateDecl>(Template.getAsTemplateDecl());
30703e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith  QualType CanonType;
30713e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith  if (!Underlying.isNull())
30723e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith    CanonType = getCanonicalType(Underlying);
30733e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith  else {
3074b70126a328f89937f46db42f9e3cba1592887c91Douglas Gregor    // We can get here with an alias template when the specialization contains
3075b70126a328f89937f46db42f9e3cba1592887c91Douglas Gregor    // a pack expansion that does not match up with a parameter pack.
3076b70126a328f89937f46db42f9e3cba1592887c91Douglas Gregor    assert((!IsTypeAlias || hasAnyPackExpansions(Args, NumArgs)) &&
3077b70126a328f89937f46db42f9e3cba1592887c91Douglas Gregor           "Caller must compute aliased type");
3078b70126a328f89937f46db42f9e3cba1592887c91Douglas Gregor    IsTypeAlias = false;
30793e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith    CanonType = getCanonicalTemplateSpecializationType(Template, Args,
30803e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith                                                       NumArgs);
30813e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith  }
3082fc705b84347e6fb4746a1a7e26949f64c2f2f358Douglas Gregor
30831275ae098acda31fe0e434510c729fcfed0458a1Douglas Gregor  // Allocate the (non-canonical) template specialization type, but don't
30841275ae098acda31fe0e434510c729fcfed0458a1Douglas Gregor  // try to unique it: these types typically have location information that
30851275ae098acda31fe0e434510c729fcfed0458a1Douglas Gregor  // we don't unique and don't want to lose.
30863e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith  void *Mem = Allocate(sizeof(TemplateSpecializationType) +
30873e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith                       sizeof(TemplateArgument) * NumArgs +
3088b70126a328f89937f46db42f9e3cba1592887c91Douglas Gregor                       (IsTypeAlias? sizeof(QualType) : 0),
30896b304a0254a13f42390b865ff5ba668a49cc58aeJohn McCall                       TypeAlignment);
30901eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  TemplateSpecializationType *Spec
3091b70126a328f89937f46db42f9e3cba1592887c91Douglas Gregor    = new (Mem) TemplateSpecializationType(Template, Args, NumArgs, CanonType,
3092b70126a328f89937f46db42f9e3cba1592887c91Douglas Gregor                                         IsTypeAlias ? Underlying : QualType());
30931eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
309455f6b14230c94272efbbcdd89a92224c8db9f225Douglas Gregor  Types.push_back(Spec);
30951eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  return QualType(Spec, 0);
309655f6b14230c94272efbbcdd89a92224c8db9f225Douglas Gregor}
309755f6b14230c94272efbbcdd89a92224c8db9f225Douglas Gregor
30981eb4433ac451dc16f4133a88af2d002ac26c58efMike StumpQualType
30999763e221e16026ddf487d2564ed349d2c874a1a1Argyrios KyrtzidisASTContext::getCanonicalTemplateSpecializationType(TemplateName Template,
31009763e221e16026ddf487d2564ed349d2c874a1a1Argyrios Kyrtzidis                                                   const TemplateArgument *Args,
31014ba2a17694148e16eaa8d3917f657ffcd3667be4Jay Foad                                                   unsigned NumArgs) const {
31027c3179cf463c3b3b8c21dbb955f933ba50b74f28Douglas Gregor  assert(!Template.getAsDependentTemplateName() &&
31037c3179cf463c3b3b8c21dbb955f933ba50b74f28Douglas Gregor         "No dependent template names here!");
31043e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith
31050f0ea2a96534c615ff5fdd81363989b23cf2164aDouglas Gregor  // Look through qualified template names.
31060f0ea2a96534c615ff5fdd81363989b23cf2164aDouglas Gregor  if (QualifiedTemplateName *QTN = Template.getAsQualifiedTemplateName())
31070f0ea2a96534c615ff5fdd81363989b23cf2164aDouglas Gregor    Template = TemplateName(QTN->getTemplateDecl());
31087c3179cf463c3b3b8c21dbb955f933ba50b74f28Douglas Gregor
31099763e221e16026ddf487d2564ed349d2c874a1a1Argyrios Kyrtzidis  // Build the canonical template specialization type.
31109763e221e16026ddf487d2564ed349d2c874a1a1Argyrios Kyrtzidis  TemplateName CanonTemplate = getCanonicalTemplateName(Template);
31115f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner  SmallVector<TemplateArgument, 4> CanonArgs;
31129763e221e16026ddf487d2564ed349d2c874a1a1Argyrios Kyrtzidis  CanonArgs.reserve(NumArgs);
31139763e221e16026ddf487d2564ed349d2c874a1a1Argyrios Kyrtzidis  for (unsigned I = 0; I != NumArgs; ++I)
31149763e221e16026ddf487d2564ed349d2c874a1a1Argyrios Kyrtzidis    CanonArgs.push_back(getCanonicalTemplateArgument(Args[I]));
31159763e221e16026ddf487d2564ed349d2c874a1a1Argyrios Kyrtzidis
31169763e221e16026ddf487d2564ed349d2c874a1a1Argyrios Kyrtzidis  // Determine whether this canonical template specialization type already
31179763e221e16026ddf487d2564ed349d2c874a1a1Argyrios Kyrtzidis  // exists.
31189763e221e16026ddf487d2564ed349d2c874a1a1Argyrios Kyrtzidis  llvm::FoldingSetNodeID ID;
31199763e221e16026ddf487d2564ed349d2c874a1a1Argyrios Kyrtzidis  TemplateSpecializationType::Profile(ID, CanonTemplate,
31209763e221e16026ddf487d2564ed349d2c874a1a1Argyrios Kyrtzidis                                      CanonArgs.data(), NumArgs, *this);
31219763e221e16026ddf487d2564ed349d2c874a1a1Argyrios Kyrtzidis
31229763e221e16026ddf487d2564ed349d2c874a1a1Argyrios Kyrtzidis  void *InsertPos = 0;
31239763e221e16026ddf487d2564ed349d2c874a1a1Argyrios Kyrtzidis  TemplateSpecializationType *Spec
31249763e221e16026ddf487d2564ed349d2c874a1a1Argyrios Kyrtzidis    = TemplateSpecializationTypes.FindNodeOrInsertPos(ID, InsertPos);
31259763e221e16026ddf487d2564ed349d2c874a1a1Argyrios Kyrtzidis
31269763e221e16026ddf487d2564ed349d2c874a1a1Argyrios Kyrtzidis  if (!Spec) {
31279763e221e16026ddf487d2564ed349d2c874a1a1Argyrios Kyrtzidis    // Allocate a new canonical template specialization type.
31289763e221e16026ddf487d2564ed349d2c874a1a1Argyrios Kyrtzidis    void *Mem = Allocate((sizeof(TemplateSpecializationType) +
31299763e221e16026ddf487d2564ed349d2c874a1a1Argyrios Kyrtzidis                          sizeof(TemplateArgument) * NumArgs),
31309763e221e16026ddf487d2564ed349d2c874a1a1Argyrios Kyrtzidis                         TypeAlignment);
31319763e221e16026ddf487d2564ed349d2c874a1a1Argyrios Kyrtzidis    Spec = new (Mem) TemplateSpecializationType(CanonTemplate,
31329763e221e16026ddf487d2564ed349d2c874a1a1Argyrios Kyrtzidis                                                CanonArgs.data(), NumArgs,
31333e4c6c4c79a03f5cb0c4671d7c282d623c6dc35eRichard Smith                                                QualType(), QualType());
31349763e221e16026ddf487d2564ed349d2c874a1a1Argyrios Kyrtzidis    Types.push_back(Spec);
31359763e221e16026ddf487d2564ed349d2c874a1a1Argyrios Kyrtzidis    TemplateSpecializationTypes.InsertNode(Spec, InsertPos);
31369763e221e16026ddf487d2564ed349d2c874a1a1Argyrios Kyrtzidis  }
31379763e221e16026ddf487d2564ed349d2c874a1a1Argyrios Kyrtzidis
31389763e221e16026ddf487d2564ed349d2c874a1a1Argyrios Kyrtzidis  assert(Spec->isDependentType() &&
31399763e221e16026ddf487d2564ed349d2c874a1a1Argyrios Kyrtzidis         "Non-dependent template-id type must have a canonical type");
31409763e221e16026ddf487d2564ed349d2c874a1a1Argyrios Kyrtzidis  return QualType(Spec, 0);
31419763e221e16026ddf487d2564ed349d2c874a1a1Argyrios Kyrtzidis}
31429763e221e16026ddf487d2564ed349d2c874a1a1Argyrios Kyrtzidis
31439763e221e16026ddf487d2564ed349d2c874a1a1Argyrios KyrtzidisQualType
3144465d41b92b2c862f3062c412a0538db65c6a2661Abramo BagnaraASTContext::getElaboratedType(ElaboratedTypeKeyword Keyword,
3145465d41b92b2c862f3062c412a0538db65c6a2661Abramo Bagnara                              NestedNameSpecifier *NNS,
31464ba2a17694148e16eaa8d3917f657ffcd3667be4Jay Foad                              QualType NamedType) const {
3147e4e5b054b4917f0ee493bb2fda5b1ec749bfb9a1Douglas Gregor  llvm::FoldingSetNodeID ID;
3148465d41b92b2c862f3062c412a0538db65c6a2661Abramo Bagnara  ElaboratedType::Profile(ID, Keyword, NNS, NamedType);
3149e4e5b054b4917f0ee493bb2fda5b1ec749bfb9a1Douglas Gregor
3150e4e5b054b4917f0ee493bb2fda5b1ec749bfb9a1Douglas Gregor  void *InsertPos = 0;
3151465d41b92b2c862f3062c412a0538db65c6a2661Abramo Bagnara  ElaboratedType *T = ElaboratedTypes.FindNodeOrInsertPos(ID, InsertPos);
3152e4e5b054b4917f0ee493bb2fda5b1ec749bfb9a1Douglas Gregor  if (T)
3153e4e5b054b4917f0ee493bb2fda5b1ec749bfb9a1Douglas Gregor    return QualType(T, 0);
3154e4e5b054b4917f0ee493bb2fda5b1ec749bfb9a1Douglas Gregor
3155789b1f640205e81b5af250693246120f1ce9d147Douglas Gregor  QualType Canon = NamedType;
3156789b1f640205e81b5af250693246120f1ce9d147Douglas Gregor  if (!Canon.isCanonical()) {
3157789b1f640205e81b5af250693246120f1ce9d147Douglas Gregor    Canon = getCanonicalType(NamedType);
3158465d41b92b2c862f3062c412a0538db65c6a2661Abramo Bagnara    ElaboratedType *CheckT = ElaboratedTypes.FindNodeOrInsertPos(ID, InsertPos);
3159465d41b92b2c862f3062c412a0538db65c6a2661Abramo Bagnara    assert(!CheckT && "Elaborated canonical type broken");
3160789b1f640205e81b5af250693246120f1ce9d147Douglas Gregor    (void)CheckT;
3161789b1f640205e81b5af250693246120f1ce9d147Douglas Gregor  }
3162789b1f640205e81b5af250693246120f1ce9d147Douglas Gregor
3163465d41b92b2c862f3062c412a0538db65c6a2661Abramo Bagnara  T = new (*this) ElaboratedType(Keyword, NNS, NamedType, Canon);
3164e4e5b054b4917f0ee493bb2fda5b1ec749bfb9a1Douglas Gregor  Types.push_back(T);
3165465d41b92b2c862f3062c412a0538db65c6a2661Abramo Bagnara  ElaboratedTypes.InsertNode(T, InsertPos);
3166e4e5b054b4917f0ee493bb2fda5b1ec749bfb9a1Douglas Gregor  return QualType(T, 0);
3167e4e5b054b4917f0ee493bb2fda5b1ec749bfb9a1Douglas Gregor}
3168e4e5b054b4917f0ee493bb2fda5b1ec749bfb9a1Douglas Gregor
3169075f8f1b6bed4d1b224c74f87508534cc6392ce6Abramo BagnaraQualType
31704ba2a17694148e16eaa8d3917f657ffcd3667be4Jay FoadASTContext::getParenType(QualType InnerType) const {
3171075f8f1b6bed4d1b224c74f87508534cc6392ce6Abramo Bagnara  llvm::FoldingSetNodeID ID;
3172075f8f1b6bed4d1b224c74f87508534cc6392ce6Abramo Bagnara  ParenType::Profile(ID, InnerType);
3173075f8f1b6bed4d1b224c74f87508534cc6392ce6Abramo Bagnara
3174075f8f1b6bed4d1b224c74f87508534cc6392ce6Abramo Bagnara  void *InsertPos = 0;
3175075f8f1b6bed4d1b224c74f87508534cc6392ce6Abramo Bagnara  ParenType *T = ParenTypes.FindNodeOrInsertPos(ID, InsertPos);
3176075f8f1b6bed4d1b224c74f87508534cc6392ce6Abramo Bagnara  if (T)
3177075f8f1b6bed4d1b224c74f87508534cc6392ce6Abramo Bagnara    return QualType(T, 0);
3178075f8f1b6bed4d1b224c74f87508534cc6392ce6Abramo Bagnara
3179075f8f1b6bed4d1b224c74f87508534cc6392ce6Abramo Bagnara  QualType Canon = InnerType;
3180075f8f1b6bed4d1b224c74f87508534cc6392ce6Abramo Bagnara  if (!Canon.isCanonical()) {
3181075f8f1b6bed4d1b224c74f87508534cc6392ce6Abramo Bagnara    Canon = getCanonicalType(InnerType);
3182075f8f1b6bed4d1b224c74f87508534cc6392ce6Abramo Bagnara    ParenType *CheckT = ParenTypes.FindNodeOrInsertPos(ID, InsertPos);
3183075f8f1b6bed4d1b224c74f87508534cc6392ce6Abramo Bagnara    assert(!CheckT && "Paren canonical type broken");
3184075f8f1b6bed4d1b224c74f87508534cc6392ce6Abramo Bagnara    (void)CheckT;
3185075f8f1b6bed4d1b224c74f87508534cc6392ce6Abramo Bagnara  }
3186075f8f1b6bed4d1b224c74f87508534cc6392ce6Abramo Bagnara
3187075f8f1b6bed4d1b224c74f87508534cc6392ce6Abramo Bagnara  T = new (*this) ParenType(InnerType, Canon);
3188075f8f1b6bed4d1b224c74f87508534cc6392ce6Abramo Bagnara  Types.push_back(T);
3189075f8f1b6bed4d1b224c74f87508534cc6392ce6Abramo Bagnara  ParenTypes.InsertNode(T, InsertPos);
3190075f8f1b6bed4d1b224c74f87508534cc6392ce6Abramo Bagnara  return QualType(T, 0);
3191075f8f1b6bed4d1b224c74f87508534cc6392ce6Abramo Bagnara}
3192075f8f1b6bed4d1b224c74f87508534cc6392ce6Abramo Bagnara
31934a2023f5014e82389d5980d307b89c545dbbac81Douglas GregorQualType ASTContext::getDependentNameType(ElaboratedTypeKeyword Keyword,
31944a2023f5014e82389d5980d307b89c545dbbac81Douglas Gregor                                          NestedNameSpecifier *NNS,
31954a2023f5014e82389d5980d307b89c545dbbac81Douglas Gregor                                          const IdentifierInfo *Name,
31964ba2a17694148e16eaa8d3917f657ffcd3667be4Jay Foad                                          QualType Canon) const {
3197d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor  assert(NNS->isDependent() && "nested-name-specifier must be dependent");
3198d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor
3199d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor  if (Canon.isNull()) {
3200d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor    NestedNameSpecifier *CanonNNS = getCanonicalNestedNameSpecifier(NNS);
32014a2023f5014e82389d5980d307b89c545dbbac81Douglas Gregor    ElaboratedTypeKeyword CanonKeyword = Keyword;
32024a2023f5014e82389d5980d307b89c545dbbac81Douglas Gregor    if (Keyword == ETK_None)
32034a2023f5014e82389d5980d307b89c545dbbac81Douglas Gregor      CanonKeyword = ETK_Typename;
32044a2023f5014e82389d5980d307b89c545dbbac81Douglas Gregor
32054a2023f5014e82389d5980d307b89c545dbbac81Douglas Gregor    if (CanonNNS != NNS || CanonKeyword != Keyword)
32064a2023f5014e82389d5980d307b89c545dbbac81Douglas Gregor      Canon = getDependentNameType(CanonKeyword, CanonNNS, Name);
3207d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor  }
3208d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor
3209d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor  llvm::FoldingSetNodeID ID;
32104a2023f5014e82389d5980d307b89c545dbbac81Douglas Gregor  DependentNameType::Profile(ID, Keyword, NNS, Name);
3211d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor
3212d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor  void *InsertPos = 0;
32134714c12a1ab759156b78be8f109ea4c12213af57Douglas Gregor  DependentNameType *T
32144714c12a1ab759156b78be8f109ea4c12213af57Douglas Gregor    = DependentNameTypes.FindNodeOrInsertPos(ID, InsertPos);
3215d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor  if (T)
3216d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor    return QualType(T, 0);
3217d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor
32184a2023f5014e82389d5980d307b89c545dbbac81Douglas Gregor  T = new (*this) DependentNameType(Keyword, NNS, Name, Canon);
3219d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor  Types.push_back(T);
32204714c12a1ab759156b78be8f109ea4c12213af57Douglas Gregor  DependentNameTypes.InsertNode(T, InsertPos);
32211eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  return QualType(T, 0);
3222d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor}
3223d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor
32241eb4433ac451dc16f4133a88af2d002ac26c58efMike StumpQualType
322533500955d731c73717af52088b7fc0e7a85681e7John McCallASTContext::getDependentTemplateSpecializationType(
322633500955d731c73717af52088b7fc0e7a85681e7John McCall                                 ElaboratedTypeKeyword Keyword,
32274a2023f5014e82389d5980d307b89c545dbbac81Douglas Gregor                                 NestedNameSpecifier *NNS,
322833500955d731c73717af52088b7fc0e7a85681e7John McCall                                 const IdentifierInfo *Name,
32294ba2a17694148e16eaa8d3917f657ffcd3667be4Jay Foad                                 const TemplateArgumentListInfo &Args) const {
323033500955d731c73717af52088b7fc0e7a85681e7John McCall  // TODO: avoid this copy
32315f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner  SmallVector<TemplateArgument, 16> ArgCopy;
323233500955d731c73717af52088b7fc0e7a85681e7John McCall  for (unsigned I = 0, E = Args.size(); I != E; ++I)
323333500955d731c73717af52088b7fc0e7a85681e7John McCall    ArgCopy.push_back(Args[I].getArgument());
323433500955d731c73717af52088b7fc0e7a85681e7John McCall  return getDependentTemplateSpecializationType(Keyword, NNS, Name,
323533500955d731c73717af52088b7fc0e7a85681e7John McCall                                                ArgCopy.size(),
323633500955d731c73717af52088b7fc0e7a85681e7John McCall                                                ArgCopy.data());
323733500955d731c73717af52088b7fc0e7a85681e7John McCall}
323833500955d731c73717af52088b7fc0e7a85681e7John McCall
323933500955d731c73717af52088b7fc0e7a85681e7John McCallQualType
324033500955d731c73717af52088b7fc0e7a85681e7John McCallASTContext::getDependentTemplateSpecializationType(
324133500955d731c73717af52088b7fc0e7a85681e7John McCall                                 ElaboratedTypeKeyword Keyword,
324233500955d731c73717af52088b7fc0e7a85681e7John McCall                                 NestedNameSpecifier *NNS,
324333500955d731c73717af52088b7fc0e7a85681e7John McCall                                 const IdentifierInfo *Name,
324433500955d731c73717af52088b7fc0e7a85681e7John McCall                                 unsigned NumArgs,
32454ba2a17694148e16eaa8d3917f657ffcd3667be4Jay Foad                                 const TemplateArgument *Args) const {
3246aa2187de137e5b809dcbbe14f3b61ae907a3d8aaDouglas Gregor  assert((!NNS || NNS->isDependent()) &&
3247aa2187de137e5b809dcbbe14f3b61ae907a3d8aaDouglas Gregor         "nested-name-specifier must be dependent");
32481734317845d60307d474b5da8a8d33adbaf5e723Douglas Gregor
3249789b1f640205e81b5af250693246120f1ce9d147Douglas Gregor  llvm::FoldingSetNodeID ID;
325033500955d731c73717af52088b7fc0e7a85681e7John McCall  DependentTemplateSpecializationType::Profile(ID, *this, Keyword, NNS,
325133500955d731c73717af52088b7fc0e7a85681e7John McCall                                               Name, NumArgs, Args);
3252789b1f640205e81b5af250693246120f1ce9d147Douglas Gregor
3253789b1f640205e81b5af250693246120f1ce9d147Douglas Gregor  void *InsertPos = 0;
325433500955d731c73717af52088b7fc0e7a85681e7John McCall  DependentTemplateSpecializationType *T
325533500955d731c73717af52088b7fc0e7a85681e7John McCall    = DependentTemplateSpecializationTypes.FindNodeOrInsertPos(ID, InsertPos);
3256789b1f640205e81b5af250693246120f1ce9d147Douglas Gregor  if (T)
3257789b1f640205e81b5af250693246120f1ce9d147Douglas Gregor    return QualType(T, 0);
3258789b1f640205e81b5af250693246120f1ce9d147Douglas Gregor
325933500955d731c73717af52088b7fc0e7a85681e7John McCall  NestedNameSpecifier *CanonNNS = getCanonicalNestedNameSpecifier(NNS);
326033500955d731c73717af52088b7fc0e7a85681e7John McCall
326133500955d731c73717af52088b7fc0e7a85681e7John McCall  ElaboratedTypeKeyword CanonKeyword = Keyword;
326233500955d731c73717af52088b7fc0e7a85681e7John McCall  if (Keyword == ETK_None) CanonKeyword = ETK_Typename;
32631734317845d60307d474b5da8a8d33adbaf5e723Douglas Gregor
326433500955d731c73717af52088b7fc0e7a85681e7John McCall  bool AnyNonCanonArgs = false;
32655f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner  SmallVector<TemplateArgument, 16> CanonArgs(NumArgs);
326633500955d731c73717af52088b7fc0e7a85681e7John McCall  for (unsigned I = 0; I != NumArgs; ++I) {
326733500955d731c73717af52088b7fc0e7a85681e7John McCall    CanonArgs[I] = getCanonicalTemplateArgument(Args[I]);
326833500955d731c73717af52088b7fc0e7a85681e7John McCall    if (!CanonArgs[I].structurallyEquals(Args[I]))
326933500955d731c73717af52088b7fc0e7a85681e7John McCall      AnyNonCanonArgs = true;
3270789b1f640205e81b5af250693246120f1ce9d147Douglas Gregor  }
32711734317845d60307d474b5da8a8d33adbaf5e723Douglas Gregor
327233500955d731c73717af52088b7fc0e7a85681e7John McCall  QualType Canon;
327333500955d731c73717af52088b7fc0e7a85681e7John McCall  if (AnyNonCanonArgs || CanonNNS != NNS || CanonKeyword != Keyword) {
327433500955d731c73717af52088b7fc0e7a85681e7John McCall    Canon = getDependentTemplateSpecializationType(CanonKeyword, CanonNNS,
327533500955d731c73717af52088b7fc0e7a85681e7John McCall                                                   Name, NumArgs,
327633500955d731c73717af52088b7fc0e7a85681e7John McCall                                                   CanonArgs.data());
327733500955d731c73717af52088b7fc0e7a85681e7John McCall
327833500955d731c73717af52088b7fc0e7a85681e7John McCall    // Find the insert position again.
327933500955d731c73717af52088b7fc0e7a85681e7John McCall    DependentTemplateSpecializationTypes.FindNodeOrInsertPos(ID, InsertPos);
328033500955d731c73717af52088b7fc0e7a85681e7John McCall  }
328133500955d731c73717af52088b7fc0e7a85681e7John McCall
328233500955d731c73717af52088b7fc0e7a85681e7John McCall  void *Mem = Allocate((sizeof(DependentTemplateSpecializationType) +
328333500955d731c73717af52088b7fc0e7a85681e7John McCall                        sizeof(TemplateArgument) * NumArgs),
328433500955d731c73717af52088b7fc0e7a85681e7John McCall                       TypeAlignment);
3285ef99001908e799c388f1363b1e607dad5f5b57d3John McCall  T = new (Mem) DependentTemplateSpecializationType(Keyword, NNS,
328633500955d731c73717af52088b7fc0e7a85681e7John McCall                                                    Name, NumArgs, Args, Canon);
32871734317845d60307d474b5da8a8d33adbaf5e723Douglas Gregor  Types.push_back(T);
328833500955d731c73717af52088b7fc0e7a85681e7John McCall  DependentTemplateSpecializationTypes.InsertNode(T, InsertPos);
32891eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  return QualType(T, 0);
32901734317845d60307d474b5da8a8d33adbaf5e723Douglas Gregor}
32911734317845d60307d474b5da8a8d33adbaf5e723Douglas Gregor
3292cded4f649cd4b7ba7d461c25c6482ef52b8d3a2aDouglas GregorQualType ASTContext::getPackExpansionType(QualType Pattern,
3293dc84cd5efdd3430efb22546b4ac656aa0540b210David Blaikie                                          Optional<unsigned> NumExpansions) {
32947536dd5e6c99584481b7dab68b7e7d8df9c54054Douglas Gregor  llvm::FoldingSetNodeID ID;
3295cded4f649cd4b7ba7d461c25c6482ef52b8d3a2aDouglas Gregor  PackExpansionType::Profile(ID, Pattern, NumExpansions);
32967536dd5e6c99584481b7dab68b7e7d8df9c54054Douglas Gregor
32977536dd5e6c99584481b7dab68b7e7d8df9c54054Douglas Gregor  assert(Pattern->containsUnexpandedParameterPack() &&
32987536dd5e6c99584481b7dab68b7e7d8df9c54054Douglas Gregor         "Pack expansions must expand one or more parameter packs");
32997536dd5e6c99584481b7dab68b7e7d8df9c54054Douglas Gregor  void *InsertPos = 0;
33007536dd5e6c99584481b7dab68b7e7d8df9c54054Douglas Gregor  PackExpansionType *T
33017536dd5e6c99584481b7dab68b7e7d8df9c54054Douglas Gregor    = PackExpansionTypes.FindNodeOrInsertPos(ID, InsertPos);
33027536dd5e6c99584481b7dab68b7e7d8df9c54054Douglas Gregor  if (T)
33037536dd5e6c99584481b7dab68b7e7d8df9c54054Douglas Gregor    return QualType(T, 0);
33047536dd5e6c99584481b7dab68b7e7d8df9c54054Douglas Gregor
33057536dd5e6c99584481b7dab68b7e7d8df9c54054Douglas Gregor  QualType Canon;
33067536dd5e6c99584481b7dab68b7e7d8df9c54054Douglas Gregor  if (!Pattern.isCanonical()) {
3307d8672ef2d343a0dbfe838724fb2d9fb4efea6041Richard Smith    Canon = getCanonicalType(Pattern);
3308d8672ef2d343a0dbfe838724fb2d9fb4efea6041Richard Smith    // The canonical type might not contain an unexpanded parameter pack, if it
3309d8672ef2d343a0dbfe838724fb2d9fb4efea6041Richard Smith    // contains an alias template specialization which ignores one of its
3310d8672ef2d343a0dbfe838724fb2d9fb4efea6041Richard Smith    // parameters.
3311d8672ef2d343a0dbfe838724fb2d9fb4efea6041Richard Smith    if (Canon->containsUnexpandedParameterPack()) {
3312d8672ef2d343a0dbfe838724fb2d9fb4efea6041Richard Smith      Canon = getPackExpansionType(getCanonicalType(Pattern), NumExpansions);
3313d8672ef2d343a0dbfe838724fb2d9fb4efea6041Richard Smith
3314d8672ef2d343a0dbfe838724fb2d9fb4efea6041Richard Smith      // Find the insert position again, in case we inserted an element into
3315d8672ef2d343a0dbfe838724fb2d9fb4efea6041Richard Smith      // PackExpansionTypes and invalidated our insert position.
3316d8672ef2d343a0dbfe838724fb2d9fb4efea6041Richard Smith      PackExpansionTypes.FindNodeOrInsertPos(ID, InsertPos);
3317d8672ef2d343a0dbfe838724fb2d9fb4efea6041Richard Smith    }
33187536dd5e6c99584481b7dab68b7e7d8df9c54054Douglas Gregor  }
33197536dd5e6c99584481b7dab68b7e7d8df9c54054Douglas Gregor
3320cded4f649cd4b7ba7d461c25c6482ef52b8d3a2aDouglas Gregor  T = new (*this) PackExpansionType(Pattern, Canon, NumExpansions);
33217536dd5e6c99584481b7dab68b7e7d8df9c54054Douglas Gregor  Types.push_back(T);
33227536dd5e6c99584481b7dab68b7e7d8df9c54054Douglas Gregor  PackExpansionTypes.InsertNode(T, InsertPos);
33237536dd5e6c99584481b7dab68b7e7d8df9c54054Douglas Gregor  return QualType(T, 0);
33247536dd5e6c99584481b7dab68b7e7d8df9c54054Douglas Gregor}
33257536dd5e6c99584481b7dab68b7e7d8df9c54054Douglas Gregor
332688cb27a160adc305783a44f922ee4b216006ebf9Chris Lattner/// CmpProtocolNames - Comparison predicate for sorting protocols
332788cb27a160adc305783a44f922ee4b216006ebf9Chris Lattner/// alphabetically.
332888cb27a160adc305783a44f922ee4b216006ebf9Chris Lattnerstatic bool CmpProtocolNames(const ObjCProtocolDecl *LHS,
332988cb27a160adc305783a44f922ee4b216006ebf9Chris Lattner                            const ObjCProtocolDecl *RHS) {
33302e1cd4264d363ca869bf37ef160902f211d21b8cDouglas Gregor  return LHS->getDeclName() < RHS->getDeclName();
333188cb27a160adc305783a44f922ee4b216006ebf9Chris Lattner}
333288cb27a160adc305783a44f922ee4b216006ebf9Chris Lattner
3333c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCallstatic bool areSortedAndUniqued(ObjCProtocolDecl * const *Protocols,
333454e14c4db764c0636160d26c5bbf491637c83a76John McCall                                unsigned NumProtocols) {
333554e14c4db764c0636160d26c5bbf491637c83a76John McCall  if (NumProtocols == 0) return true;
333654e14c4db764c0636160d26c5bbf491637c83a76John McCall
333761cc296de6c1f82fa84c0abb3ecd142a584838efDouglas Gregor  if (Protocols[0]->getCanonicalDecl() != Protocols[0])
333861cc296de6c1f82fa84c0abb3ecd142a584838efDouglas Gregor    return false;
333961cc296de6c1f82fa84c0abb3ecd142a584838efDouglas Gregor
334054e14c4db764c0636160d26c5bbf491637c83a76John McCall  for (unsigned i = 1; i != NumProtocols; ++i)
334161cc296de6c1f82fa84c0abb3ecd142a584838efDouglas Gregor    if (!CmpProtocolNames(Protocols[i-1], Protocols[i]) ||
334261cc296de6c1f82fa84c0abb3ecd142a584838efDouglas Gregor        Protocols[i]->getCanonicalDecl() != Protocols[i])
334354e14c4db764c0636160d26c5bbf491637c83a76John McCall      return false;
334454e14c4db764c0636160d26c5bbf491637c83a76John McCall  return true;
334554e14c4db764c0636160d26c5bbf491637c83a76John McCall}
334654e14c4db764c0636160d26c5bbf491637c83a76John McCall
334754e14c4db764c0636160d26c5bbf491637c83a76John McCallstatic void SortAndUniqueProtocols(ObjCProtocolDecl **Protocols,
334888cb27a160adc305783a44f922ee4b216006ebf9Chris Lattner                                   unsigned &NumProtocols) {
334988cb27a160adc305783a44f922ee4b216006ebf9Chris Lattner  ObjCProtocolDecl **ProtocolsEnd = Protocols+NumProtocols;
33501eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
335188cb27a160adc305783a44f922ee4b216006ebf9Chris Lattner  // Sort protocols, keyed by name.
335288cb27a160adc305783a44f922ee4b216006ebf9Chris Lattner  std::sort(Protocols, Protocols+NumProtocols, CmpProtocolNames);
335388cb27a160adc305783a44f922ee4b216006ebf9Chris Lattner
335461cc296de6c1f82fa84c0abb3ecd142a584838efDouglas Gregor  // Canonicalize.
335561cc296de6c1f82fa84c0abb3ecd142a584838efDouglas Gregor  for (unsigned I = 0, N = NumProtocols; I != N; ++I)
335661cc296de6c1f82fa84c0abb3ecd142a584838efDouglas Gregor    Protocols[I] = Protocols[I]->getCanonicalDecl();
335761cc296de6c1f82fa84c0abb3ecd142a584838efDouglas Gregor
335888cb27a160adc305783a44f922ee4b216006ebf9Chris Lattner  // Remove duplicates.
335988cb27a160adc305783a44f922ee4b216006ebf9Chris Lattner  ProtocolsEnd = std::unique(Protocols, ProtocolsEnd);
336088cb27a160adc305783a44f922ee4b216006ebf9Chris Lattner  NumProtocols = ProtocolsEnd-Protocols;
336188cb27a160adc305783a44f922ee4b216006ebf9Chris Lattner}
336288cb27a160adc305783a44f922ee4b216006ebf9Chris Lattner
3363c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCallQualType ASTContext::getObjCObjectType(QualType BaseType,
3364c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall                                       ObjCProtocolDecl * const *Protocols,
33654ba2a17694148e16eaa8d3917f657ffcd3667be4Jay Foad                                       unsigned NumProtocols) const {
3366c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall  // If the base type is an interface and there aren't any protocols
3367c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall  // to add, then the interface type will do just fine.
3368c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall  if (!NumProtocols && isa<ObjCInterfaceType>(BaseType))
3369c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall    return BaseType;
3370d1b3c2dd5bc1f3103bee6137957aa7c5f8f2f0bcSteve Naroff
3371c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall  // Look in the folding set for an existing type.
3372c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall  llvm::FoldingSetNodeID ID;
3373c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall  ObjCObjectTypeImpl::Profile(ID, BaseType, Protocols, NumProtocols);
3374d1b3c2dd5bc1f3103bee6137957aa7c5f8f2f0bcSteve Naroff  void *InsertPos = 0;
3375c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall  if (ObjCObjectType *QT = ObjCObjectTypes.FindNodeOrInsertPos(ID, InsertPos))
3376c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall    return QualType(QT, 0);
3377d1b3c2dd5bc1f3103bee6137957aa7c5f8f2f0bcSteve Naroff
3378c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall  // Build the canonical type, which has the canonical base type and
3379c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall  // a sorted-and-uniqued list of protocols.
338054e14c4db764c0636160d26c5bbf491637c83a76John McCall  QualType Canonical;
3381c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall  bool ProtocolsSorted = areSortedAndUniqued(Protocols, NumProtocols);
3382c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall  if (!ProtocolsSorted || !BaseType.isCanonical()) {
3383c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall    if (!ProtocolsSorted) {
33845f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner      SmallVector<ObjCProtocolDecl*, 8> Sorted(Protocols,
33850237941e0beb0c929934b66ad29443b484d987feBenjamin Kramer                                                     Protocols + NumProtocols);
338654e14c4db764c0636160d26c5bbf491637c83a76John McCall      unsigned UniqueCount = NumProtocols;
338754e14c4db764c0636160d26c5bbf491637c83a76John McCall
338854e14c4db764c0636160d26c5bbf491637c83a76John McCall      SortAndUniqueProtocols(&Sorted[0], UniqueCount);
3389c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall      Canonical = getObjCObjectType(getCanonicalType(BaseType),
3390c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall                                    &Sorted[0], UniqueCount);
339154e14c4db764c0636160d26c5bbf491637c83a76John McCall    } else {
3392c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall      Canonical = getObjCObjectType(getCanonicalType(BaseType),
3393c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall                                    Protocols, NumProtocols);
339454e14c4db764c0636160d26c5bbf491637c83a76John McCall    }
339554e14c4db764c0636160d26c5bbf491637c83a76John McCall
339654e14c4db764c0636160d26c5bbf491637c83a76John McCall    // Regenerate InsertPos.
3397c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall    ObjCObjectTypes.FindNodeOrInsertPos(ID, InsertPos);
339854e14c4db764c0636160d26c5bbf491637c83a76John McCall  }
339954e14c4db764c0636160d26c5bbf491637c83a76John McCall
3400c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall  unsigned Size = sizeof(ObjCObjectTypeImpl);
3401c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall  Size += NumProtocols * sizeof(ObjCProtocolDecl *);
3402fd6a0887a099256c35a5b23e9afd517ffe95fa0aDouglas Gregor  void *Mem = Allocate(Size, TypeAlignment);
3403c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall  ObjCObjectTypeImpl *T =
3404c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall    new (Mem) ObjCObjectTypeImpl(Canonical, BaseType, Protocols, NumProtocols);
34051eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
3406c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall  Types.push_back(T);
3407c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall  ObjCObjectTypes.InsertNode(T, InsertPos);
3408c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall  return QualType(T, 0);
3409d1b3c2dd5bc1f3103bee6137957aa7c5f8f2f0bcSteve Naroff}
341088cb27a160adc305783a44f922ee4b216006ebf9Chris Lattner
3411c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall/// getObjCObjectPointerType - Return a ObjCObjectPointerType type for
3412c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall/// the given object type.
34134ba2a17694148e16eaa8d3917f657ffcd3667be4Jay FoadQualType ASTContext::getObjCObjectPointerType(QualType ObjectT) const {
34144b6c9051c6522894978c9ba6a819a659d102db36Fariborz Jahanian  llvm::FoldingSetNodeID ID;
3415c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall  ObjCObjectPointerType::Profile(ID, ObjectT);
34161eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
34174b6c9051c6522894978c9ba6a819a659d102db36Fariborz Jahanian  void *InsertPos = 0;
3418c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall  if (ObjCObjectPointerType *QT =
3419c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall              ObjCObjectPointerTypes.FindNodeOrInsertPos(ID, InsertPos))
34204b6c9051c6522894978c9ba6a819a659d102db36Fariborz Jahanian    return QualType(QT, 0);
34211eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
3422c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall  // Find the canonical object type.
342354e14c4db764c0636160d26c5bbf491637c83a76John McCall  QualType Canonical;
3424c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall  if (!ObjectT.isCanonical()) {
3425c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall    Canonical = getObjCObjectPointerType(getCanonicalType(ObjectT));
342654e14c4db764c0636160d26c5bbf491637c83a76John McCall
3427c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall    // Regenerate InsertPos.
3428c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall    ObjCObjectPointerTypes.FindNodeOrInsertPos(ID, InsertPos);
342954e14c4db764c0636160d26c5bbf491637c83a76John McCall  }
343054e14c4db764c0636160d26c5bbf491637c83a76John McCall
3431c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall  // No match.
3432c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall  void *Mem = Allocate(sizeof(ObjCObjectPointerType), TypeAlignment);
3433c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall  ObjCObjectPointerType *QType =
3434c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall    new (Mem) ObjCObjectPointerType(Canonical, ObjectT);
343524fab41057e4b67ed69a6b4027d5ae0f2f6934dcArgyrios Kyrtzidis
343624fab41057e4b67ed69a6b4027d5ae0f2f6934dcArgyrios Kyrtzidis  Types.push_back(QType);
3437c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall  ObjCObjectPointerTypes.InsertNode(QType, InsertPos);
343824fab41057e4b67ed69a6b4027d5ae0f2f6934dcArgyrios Kyrtzidis  return QualType(QType, 0);
343924fab41057e4b67ed69a6b4027d5ae0f2f6934dcArgyrios Kyrtzidis}
344024fab41057e4b67ed69a6b4027d5ae0f2f6934dcArgyrios Kyrtzidis
3441deacbdca554298ccdf636f19c6094a8825ec6b34Douglas Gregor/// getObjCInterfaceType - Return the unique reference to the type for the
3442deacbdca554298ccdf636f19c6094a8825ec6b34Douglas Gregor/// specified ObjC interface decl. The list of protocols is optional.
34430af550115df1f57f17a4f125ff0e8b34820c65d1Douglas GregorQualType ASTContext::getObjCInterfaceType(const ObjCInterfaceDecl *Decl,
34440af550115df1f57f17a4f125ff0e8b34820c65d1Douglas Gregor                                          ObjCInterfaceDecl *PrevDecl) const {
3445deacbdca554298ccdf636f19c6094a8825ec6b34Douglas Gregor  if (Decl->TypeForDecl)
3446deacbdca554298ccdf636f19c6094a8825ec6b34Douglas Gregor    return QualType(Decl->TypeForDecl, 0);
344774c730ad1f6818b676b0bad46d806a9176950328Sebastian Redl
34480af550115df1f57f17a4f125ff0e8b34820c65d1Douglas Gregor  if (PrevDecl) {
34490af550115df1f57f17a4f125ff0e8b34820c65d1Douglas Gregor    assert(PrevDecl->TypeForDecl && "previous decl has no TypeForDecl");
34500af550115df1f57f17a4f125ff0e8b34820c65d1Douglas Gregor    Decl->TypeForDecl = PrevDecl->TypeForDecl;
34510af550115df1f57f17a4f125ff0e8b34820c65d1Douglas Gregor    return QualType(PrevDecl->TypeForDecl, 0);
34520af550115df1f57f17a4f125ff0e8b34820c65d1Douglas Gregor  }
34530af550115df1f57f17a4f125ff0e8b34820c65d1Douglas Gregor
34548d2dbbf9ddfd9d762a341d83f83d840ff68ce03dDouglas Gregor  // Prefer the definition, if there is one.
34558d2dbbf9ddfd9d762a341d83f83d840ff68ce03dDouglas Gregor  if (const ObjCInterfaceDecl *Def = Decl->getDefinition())
34568d2dbbf9ddfd9d762a341d83f83d840ff68ce03dDouglas Gregor    Decl = Def;
34578d2dbbf9ddfd9d762a341d83f83d840ff68ce03dDouglas Gregor
3458deacbdca554298ccdf636f19c6094a8825ec6b34Douglas Gregor  void *Mem = Allocate(sizeof(ObjCInterfaceType), TypeAlignment);
3459deacbdca554298ccdf636f19c6094a8825ec6b34Douglas Gregor  ObjCInterfaceType *T = new (Mem) ObjCInterfaceType(Decl);
3460deacbdca554298ccdf636f19c6094a8825ec6b34Douglas Gregor  Decl->TypeForDecl = T;
3461deacbdca554298ccdf636f19c6094a8825ec6b34Douglas Gregor  Types.push_back(T);
3462deacbdca554298ccdf636f19c6094a8825ec6b34Douglas Gregor  return QualType(T, 0);
3463c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall}
3464c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall
346572564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor/// getTypeOfExprType - Unlike many "get<Type>" functions, we can't unique
346672564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor/// TypeOfExprType AST's (since expression's are never shared). For example,
34679752f25748d954df99087d741ea35db37ff16beaSteve Naroff/// multiple declarations that refer to "typeof(x)" all contain different
34681eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// DeclRefExpr's. This doesn't effect the type checker, since it operates
34699752f25748d954df99087d741ea35db37ff16beaSteve Naroff/// on canonical type's (which are always unique).
34704ba2a17694148e16eaa8d3917f657ffcd3667be4Jay FoadQualType ASTContext::getTypeOfExprType(Expr *tofExpr) const {
3471dd0257c77719a13d4acd513df40b04300cbfc871Douglas Gregor  TypeOfExprType *toe;
3472b197572cf1cd70a817a1f546478cb2cb9112c48eDouglas Gregor  if (tofExpr->isTypeDependent()) {
3473b197572cf1cd70a817a1f546478cb2cb9112c48eDouglas Gregor    llvm::FoldingSetNodeID ID;
3474b197572cf1cd70a817a1f546478cb2cb9112c48eDouglas Gregor    DependentTypeOfExprType::Profile(ID, *this, tofExpr);
34751eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
3476b197572cf1cd70a817a1f546478cb2cb9112c48eDouglas Gregor    void *InsertPos = 0;
3477b197572cf1cd70a817a1f546478cb2cb9112c48eDouglas Gregor    DependentTypeOfExprType *Canon
3478b197572cf1cd70a817a1f546478cb2cb9112c48eDouglas Gregor      = DependentTypeOfExprTypes.FindNodeOrInsertPos(ID, InsertPos);
3479b197572cf1cd70a817a1f546478cb2cb9112c48eDouglas Gregor    if (Canon) {
3480b197572cf1cd70a817a1f546478cb2cb9112c48eDouglas Gregor      // We already have a "canonical" version of an identical, dependent
3481b197572cf1cd70a817a1f546478cb2cb9112c48eDouglas Gregor      // typeof(expr) type. Use that as our canonical type.
34826b304a0254a13f42390b865ff5ba668a49cc58aeJohn McCall      toe = new (*this, TypeAlignment) TypeOfExprType(tofExpr,
3483b197572cf1cd70a817a1f546478cb2cb9112c48eDouglas Gregor                                          QualType((TypeOfExprType*)Canon, 0));
34843060178ad9df29789505c1e6debcfc80a3a13587Chad Rosier    } else {
3485b197572cf1cd70a817a1f546478cb2cb9112c48eDouglas Gregor      // Build a new, canonical typeof(expr) type.
34866b304a0254a13f42390b865ff5ba668a49cc58aeJohn McCall      Canon
34876b304a0254a13f42390b865ff5ba668a49cc58aeJohn McCall        = new (*this, TypeAlignment) DependentTypeOfExprType(*this, tofExpr);
3488b197572cf1cd70a817a1f546478cb2cb9112c48eDouglas Gregor      DependentTypeOfExprTypes.InsertNode(Canon, InsertPos);
3489b197572cf1cd70a817a1f546478cb2cb9112c48eDouglas Gregor      toe = Canon;
3490b197572cf1cd70a817a1f546478cb2cb9112c48eDouglas Gregor    }
3491b197572cf1cd70a817a1f546478cb2cb9112c48eDouglas Gregor  } else {
3492dd0257c77719a13d4acd513df40b04300cbfc871Douglas Gregor    QualType Canonical = getCanonicalType(tofExpr->getType());
34936b304a0254a13f42390b865ff5ba668a49cc58aeJohn McCall    toe = new (*this, TypeAlignment) TypeOfExprType(tofExpr, Canonical);
3494dd0257c77719a13d4acd513df40b04300cbfc871Douglas Gregor  }
34959752f25748d954df99087d741ea35db37ff16beaSteve Naroff  Types.push_back(toe);
34969752f25748d954df99087d741ea35db37ff16beaSteve Naroff  return QualType(toe, 0);
3497d1861fd633d5096a00777c918eb8575ea7162fe7Steve Naroff}
3498d1861fd633d5096a00777c918eb8575ea7162fe7Steve Naroff
34999752f25748d954df99087d741ea35db37ff16beaSteve Naroff/// getTypeOfType -  Unlike many "get<Type>" functions, we don't unique
35009752f25748d954df99087d741ea35db37ff16beaSteve Naroff/// TypeOfType AST's. The only motivation to unique these nodes would be
35019752f25748d954df99087d741ea35db37ff16beaSteve Naroff/// memory savings. Since typeof(t) is fairly uncommon, space shouldn't be
35021eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// an issue. This doesn't effect the type checker, since it operates
35039752f25748d954df99087d741ea35db37ff16beaSteve Naroff/// on canonical type's (which are always unique).
35044ba2a17694148e16eaa8d3917f657ffcd3667be4Jay FoadQualType ASTContext::getTypeOfType(QualType tofType) const {
3505f52ab250ff92bc51a9ac9a8e19bd43b63a5f844fChris Lattner  QualType Canonical = getCanonicalType(tofType);
35066b304a0254a13f42390b865ff5ba668a49cc58aeJohn McCall  TypeOfType *tot = new (*this, TypeAlignment) TypeOfType(tofType, Canonical);
35079752f25748d954df99087d741ea35db37ff16beaSteve Naroff  Types.push_back(tot);
35089752f25748d954df99087d741ea35db37ff16beaSteve Naroff  return QualType(tot, 0);
3509d1861fd633d5096a00777c918eb8575ea7162fe7Steve Naroff}
3510d1861fd633d5096a00777c918eb8575ea7162fe7Steve Naroff
351160a9a2a404a4cf259d39133383e922aa00ca9043Anders Carlsson
3512395b475a4474f1c7574d927ad142ca0c7997cbcaAnders Carlsson/// getDecltypeType -  Unlike many "get<Type>" functions, we don't unique
3513395b475a4474f1c7574d927ad142ca0c7997cbcaAnders Carlsson/// DecltypeType AST's. The only motivation to unique these nodes would be
3514395b475a4474f1c7574d927ad142ca0c7997cbcaAnders Carlsson/// memory savings. Since decltype(t) is fairly uncommon, space shouldn't be
35151eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// an issue. This doesn't effect the type checker, since it operates
351639e02032b01874a0d02ba85a4cd3922adda81376David Blaikie/// on canonical types (which are always unique).
3517f8af98286022f72157d84951b48fde5fb369ab29Douglas GregorQualType ASTContext::getDecltypeType(Expr *e, QualType UnderlyingType) const {
3518dd0257c77719a13d4acd513df40b04300cbfc871Douglas Gregor  DecltypeType *dt;
3519561f81243f665cf2001caadc45df505f826b72d6Douglas Gregor
3520561f81243f665cf2001caadc45df505f826b72d6Douglas Gregor  // C++0x [temp.type]p2:
3521561f81243f665cf2001caadc45df505f826b72d6Douglas Gregor  //   If an expression e involves a template parameter, decltype(e) denotes a
3522561f81243f665cf2001caadc45df505f826b72d6Douglas Gregor  //   unique dependent type. Two such decltype-specifiers refer to the same
3523561f81243f665cf2001caadc45df505f826b72d6Douglas Gregor  //   type only if their expressions are equivalent (14.5.6.1).
3524561f81243f665cf2001caadc45df505f826b72d6Douglas Gregor  if (e->isInstantiationDependent()) {
35259d702ae1cd5cfa19d884cbef77e1df99395138bbDouglas Gregor    llvm::FoldingSetNodeID ID;
35269d702ae1cd5cfa19d884cbef77e1df99395138bbDouglas Gregor    DependentDecltypeType::Profile(ID, *this, e);
35271eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
35289d702ae1cd5cfa19d884cbef77e1df99395138bbDouglas Gregor    void *InsertPos = 0;
35299d702ae1cd5cfa19d884cbef77e1df99395138bbDouglas Gregor    DependentDecltypeType *Canon
35309d702ae1cd5cfa19d884cbef77e1df99395138bbDouglas Gregor      = DependentDecltypeTypes.FindNodeOrInsertPos(ID, InsertPos);
35319d702ae1cd5cfa19d884cbef77e1df99395138bbDouglas Gregor    if (Canon) {
35329d702ae1cd5cfa19d884cbef77e1df99395138bbDouglas Gregor      // We already have a "canonical" version of an equivalent, dependent
35339d702ae1cd5cfa19d884cbef77e1df99395138bbDouglas Gregor      // decltype type. Use that as our canonical type.
35340d729105ecb50a7e3cbe6e57c29149edfa5cf05aRichard Smith      dt = new (*this, TypeAlignment) DecltypeType(e, UnderlyingType,
35359d702ae1cd5cfa19d884cbef77e1df99395138bbDouglas Gregor                                       QualType((DecltypeType*)Canon, 0));
35363060178ad9df29789505c1e6debcfc80a3a13587Chad Rosier    } else {
35379d702ae1cd5cfa19d884cbef77e1df99395138bbDouglas Gregor      // Build a new, canonical typeof(expr) type.
35386b304a0254a13f42390b865ff5ba668a49cc58aeJohn McCall      Canon = new (*this, TypeAlignment) DependentDecltypeType(*this, e);
35399d702ae1cd5cfa19d884cbef77e1df99395138bbDouglas Gregor      DependentDecltypeTypes.InsertNode(Canon, InsertPos);
35409d702ae1cd5cfa19d884cbef77e1df99395138bbDouglas Gregor      dt = Canon;
35419d702ae1cd5cfa19d884cbef77e1df99395138bbDouglas Gregor    }
35429d702ae1cd5cfa19d884cbef77e1df99395138bbDouglas Gregor  } else {
3543f8af98286022f72157d84951b48fde5fb369ab29Douglas Gregor    dt = new (*this, TypeAlignment) DecltypeType(e, UnderlyingType,
3544f8af98286022f72157d84951b48fde5fb369ab29Douglas Gregor                                      getCanonicalType(UnderlyingType));
3545dd0257c77719a13d4acd513df40b04300cbfc871Douglas Gregor  }
3546395b475a4474f1c7574d927ad142ca0c7997cbcaAnders Carlsson  Types.push_back(dt);
3547395b475a4474f1c7574d927ad142ca0c7997cbcaAnders Carlsson  return QualType(dt, 0);
3548395b475a4474f1c7574d927ad142ca0c7997cbcaAnders Carlsson}
3549395b475a4474f1c7574d927ad142ca0c7997cbcaAnders Carlsson
3550ca63c200346c0ca9e00194ec6e34a5a7b0ed9321Sean Hunt/// getUnaryTransformationType - We don't unique these, since the memory
3551ca63c200346c0ca9e00194ec6e34a5a7b0ed9321Sean Hunt/// savings are minimal and these are rare.
3552ca63c200346c0ca9e00194ec6e34a5a7b0ed9321Sean HuntQualType ASTContext::getUnaryTransformType(QualType BaseType,
3553ca63c200346c0ca9e00194ec6e34a5a7b0ed9321Sean Hunt                                           QualType UnderlyingType,
3554ca63c200346c0ca9e00194ec6e34a5a7b0ed9321Sean Hunt                                           UnaryTransformType::UTTKind Kind)
3555ca63c200346c0ca9e00194ec6e34a5a7b0ed9321Sean Hunt    const {
3556ca63c200346c0ca9e00194ec6e34a5a7b0ed9321Sean Hunt  UnaryTransformType *Ty =
355769d9775da47a4b9f165dbc33277f02982928a94eDouglas Gregor    new (*this, TypeAlignment) UnaryTransformType (BaseType, UnderlyingType,
355869d9775da47a4b9f165dbc33277f02982928a94eDouglas Gregor                                                   Kind,
355969d9775da47a4b9f165dbc33277f02982928a94eDouglas Gregor                                 UnderlyingType->isDependentType() ?
356012fc4b0624706b474fa10308631fa8daf92f340fPeter Collingbourne                                 QualType() : getCanonicalType(UnderlyingType));
3561ca63c200346c0ca9e00194ec6e34a5a7b0ed9321Sean Hunt  Types.push_back(Ty);
3562ca63c200346c0ca9e00194ec6e34a5a7b0ed9321Sean Hunt  return QualType(Ty, 0);
3563ca63c200346c0ca9e00194ec6e34a5a7b0ed9321Sean Hunt}
3564ca63c200346c0ca9e00194ec6e34a5a7b0ed9321Sean Hunt
3565483b9f3bc05c5409e2c6643f1c9d91e21c8ff9d2Richard Smith/// getAutoType - We only unique auto types after they've been deduced.
3566a2c3646c35dd09d21b74826240aa916545b1873fRichard SmithQualType ASTContext::getAutoType(QualType DeducedType,
3567a2c3646c35dd09d21b74826240aa916545b1873fRichard Smith                                 bool IsDecltypeAuto) const {
3568483b9f3bc05c5409e2c6643f1c9d91e21c8ff9d2Richard Smith  void *InsertPos = 0;
3569483b9f3bc05c5409e2c6643f1c9d91e21c8ff9d2Richard Smith  if (!DeducedType.isNull()) {
3570483b9f3bc05c5409e2c6643f1c9d91e21c8ff9d2Richard Smith    // Look in the folding set for an existing type.
3571483b9f3bc05c5409e2c6643f1c9d91e21c8ff9d2Richard Smith    llvm::FoldingSetNodeID ID;
3572a2c3646c35dd09d21b74826240aa916545b1873fRichard Smith    AutoType::Profile(ID, DeducedType, IsDecltypeAuto);
3573483b9f3bc05c5409e2c6643f1c9d91e21c8ff9d2Richard Smith    if (AutoType *AT = AutoTypes.FindNodeOrInsertPos(ID, InsertPos))
3574483b9f3bc05c5409e2c6643f1c9d91e21c8ff9d2Richard Smith      return QualType(AT, 0);
3575483b9f3bc05c5409e2c6643f1c9d91e21c8ff9d2Richard Smith  }
3576483b9f3bc05c5409e2c6643f1c9d91e21c8ff9d2Richard Smith
3577a2c3646c35dd09d21b74826240aa916545b1873fRichard Smith  AutoType *AT = new (*this, TypeAlignment) AutoType(DeducedType,
3578a2c3646c35dd09d21b74826240aa916545b1873fRichard Smith                                                     IsDecltypeAuto);
3579483b9f3bc05c5409e2c6643f1c9d91e21c8ff9d2Richard Smith  Types.push_back(AT);
3580483b9f3bc05c5409e2c6643f1c9d91e21c8ff9d2Richard Smith  if (InsertPos)
3581483b9f3bc05c5409e2c6643f1c9d91e21c8ff9d2Richard Smith    AutoTypes.InsertNode(AT, InsertPos);
3582483b9f3bc05c5409e2c6643f1c9d91e21c8ff9d2Richard Smith  return QualType(AT, 0);
358334b41d939a1328f484511c6002ba2456db879a29Richard Smith}
358434b41d939a1328f484511c6002ba2456db879a29Richard Smith
3585b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman/// getAtomicType - Return the uniqued reference to the atomic type for
3586b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman/// the given value type.
3587b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli FriedmanQualType ASTContext::getAtomicType(QualType T) const {
3588b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman  // Unique pointers, to guarantee there is only one pointer of a particular
3589b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman  // structure.
3590b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman  llvm::FoldingSetNodeID ID;
3591b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman  AtomicType::Profile(ID, T);
3592b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman
3593b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman  void *InsertPos = 0;
3594b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman  if (AtomicType *AT = AtomicTypes.FindNodeOrInsertPos(ID, InsertPos))
3595b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman    return QualType(AT, 0);
3596b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman
3597b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman  // If the atomic value type isn't canonical, this won't be a canonical type
3598b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman  // either, so fill in the canonical type field.
3599b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman  QualType Canonical;
3600b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman  if (!T.isCanonical()) {
3601b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman    Canonical = getAtomicType(getCanonicalType(T));
3602b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman
3603b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman    // Get the new insert position for the node we care about.
3604b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman    AtomicType *NewIP = AtomicTypes.FindNodeOrInsertPos(ID, InsertPos);
3605b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman    assert(NewIP == 0 && "Shouldn't be in the map!"); (void)NewIP;
3606b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman  }
3607b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman  AtomicType *New = new (*this, TypeAlignment) AtomicType(T, Canonical);
3608b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman  Types.push_back(New);
3609b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman  AtomicTypes.InsertNode(New, InsertPos);
3610b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman  return QualType(New, 0);
3611b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman}
3612b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman
3613ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith/// getAutoDeductType - Get type pattern for deducing against 'auto'.
3614ad762fcdc16b9e4705b12b09d92b8c026212b906Richard SmithQualType ASTContext::getAutoDeductType() const {
3615ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith  if (AutoDeductTy.isNull())
3616a2c3646c35dd09d21b74826240aa916545b1873fRichard Smith    AutoDeductTy = getAutoType(QualType(), false);
3617ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith  assert(!AutoDeductTy.isNull() && "can't build 'auto' pattern");
3618ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith  return AutoDeductTy;
3619ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith}
3620ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith
3621ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith/// getAutoRRefDeductType - Get type pattern for deducing against 'auto &&'.
3622ad762fcdc16b9e4705b12b09d92b8c026212b906Richard SmithQualType ASTContext::getAutoRRefDeductType() const {
3623ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith  if (AutoRRefDeductTy.isNull())
3624ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith    AutoRRefDeductTy = getRValueReferenceType(getAutoDeductType());
3625ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith  assert(!AutoRRefDeductTy.isNull() && "can't build 'auto &&' pattern");
3626ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith  return AutoRRefDeductTy;
3627ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith}
3628ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith
36295f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// getTagDeclType - Return the unique reference to the type for the
36305f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// specified TagDecl (struct/union/class/enum) decl.
36314ba2a17694148e16eaa8d3917f657ffcd3667be4Jay FoadQualType ASTContext::getTagDeclType(const TagDecl *Decl) const {
3632d778f88d32b96a74c9edb7342c81357606a7cdc0Ted Kremenek  assert (Decl);
3633e607ed068334bacb8d7b093996b4671c6ca79e25Mike Stump  // FIXME: What is the design on getTagDeclType when it requires casting
3634e607ed068334bacb8d7b093996b4671c6ca79e25Mike Stump  // away const?  mutable?
3635e607ed068334bacb8d7b093996b4671c6ca79e25Mike Stump  return getTypeDeclType(const_cast<TagDecl*>(Decl));
36365f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
36375f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
36381eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// getSizeType - Return the unique type for "size_t" (C99 7.17), the result
36391eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// of the sizeof operator (C99 6.5.3.4p4). The value is target dependent and
36401eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// needs to agree with the definition in <stddef.h>.
3641a3ccda58913cc1a4b8564e349448b12acc462da7Anders CarlssonCanQualType ASTContext::getSizeType() const {
3642bcfd1f55bfbb3e5944cd5e03d07b343e280838c4Douglas Gregor  return getFromTargetType(Target->getSizeType());
36435f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
36445f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
364529e97cb35fab314388f62b68fefa78947e93c1dcHans Wennborg/// getIntMaxType - Return the unique type for "intmax_t" (C99 7.18.1.5).
364629e97cb35fab314388f62b68fefa78947e93c1dcHans WennborgCanQualType ASTContext::getIntMaxType() const {
364729e97cb35fab314388f62b68fefa78947e93c1dcHans Wennborg  return getFromTargetType(Target->getIntMaxType());
364829e97cb35fab314388f62b68fefa78947e93c1dcHans Wennborg}
364929e97cb35fab314388f62b68fefa78947e93c1dcHans Wennborg
365029e97cb35fab314388f62b68fefa78947e93c1dcHans Wennborg/// getUIntMaxType - Return the unique type for "uintmax_t" (C99 7.18.1.5).
365129e97cb35fab314388f62b68fefa78947e93c1dcHans WennborgCanQualType ASTContext::getUIntMaxType() const {
365229e97cb35fab314388f62b68fefa78947e93c1dcHans Wennborg  return getFromTargetType(Target->getUIntMaxType());
365329e97cb35fab314388f62b68fefa78947e93c1dcHans Wennborg}
365429e97cb35fab314388f62b68fefa78947e93c1dcHans Wennborg
365564c438a4be2a871fa43c78264663ba1e9788b94dArgyrios Kyrtzidis/// getSignedWCharType - Return the type of "signed wchar_t".
365664c438a4be2a871fa43c78264663ba1e9788b94dArgyrios Kyrtzidis/// Used when in C++, as a GCC extension.
365764c438a4be2a871fa43c78264663ba1e9788b94dArgyrios KyrtzidisQualType ASTContext::getSignedWCharType() const {
365864c438a4be2a871fa43c78264663ba1e9788b94dArgyrios Kyrtzidis  // FIXME: derive from "Target" ?
365964c438a4be2a871fa43c78264663ba1e9788b94dArgyrios Kyrtzidis  return WCharTy;
366064c438a4be2a871fa43c78264663ba1e9788b94dArgyrios Kyrtzidis}
366164c438a4be2a871fa43c78264663ba1e9788b94dArgyrios Kyrtzidis
366264c438a4be2a871fa43c78264663ba1e9788b94dArgyrios Kyrtzidis/// getUnsignedWCharType - Return the type of "unsigned wchar_t".
366364c438a4be2a871fa43c78264663ba1e9788b94dArgyrios Kyrtzidis/// Used when in C++, as a GCC extension.
366464c438a4be2a871fa43c78264663ba1e9788b94dArgyrios KyrtzidisQualType ASTContext::getUnsignedWCharType() const {
366564c438a4be2a871fa43c78264663ba1e9788b94dArgyrios Kyrtzidis  // FIXME: derive from "Target" ?
366664c438a4be2a871fa43c78264663ba1e9788b94dArgyrios Kyrtzidis  return UnsignedIntTy;
366764c438a4be2a871fa43c78264663ba1e9788b94dArgyrios Kyrtzidis}
366864c438a4be2a871fa43c78264663ba1e9788b94dArgyrios Kyrtzidis
36699677eb864f7354f140cd1bf59557843851e65de4Enea ZaffanellaQualType ASTContext::getIntPtrType() const {
36709677eb864f7354f140cd1bf59557843851e65de4Enea Zaffanella  return getFromTargetType(Target->getIntPtrType());
36719677eb864f7354f140cd1bf59557843851e65de4Enea Zaffanella}
36729677eb864f7354f140cd1bf59557843851e65de4Enea Zaffanella
36739677eb864f7354f140cd1bf59557843851e65de4Enea ZaffanellaQualType ASTContext::getUIntPtrType() const {
36749677eb864f7354f140cd1bf59557843851e65de4Enea Zaffanella  return getCorrespondingUnsignedType(getIntPtrType());
36759677eb864f7354f140cd1bf59557843851e65de4Enea Zaffanella}
36769677eb864f7354f140cd1bf59557843851e65de4Enea Zaffanella
367729e97cb35fab314388f62b68fefa78947e93c1dcHans Wennborg/// getPointerDiffType - Return the unique type for "ptrdiff_t" (C99 7.17)
36788b9023ba35a86838789e2c9034a6128728c547aaChris Lattner/// defined in <stddef.h>. Pointer - pointer requires this (C99 6.5.6p9).
36798b9023ba35a86838789e2c9034a6128728c547aaChris LattnerQualType ASTContext::getPointerDiffType() const {
3680bcfd1f55bfbb3e5944cd5e03d07b343e280838c4Douglas Gregor  return getFromTargetType(Target->getPtrDiffType(0));
36818b9023ba35a86838789e2c9034a6128728c547aaChris Lattner}
36828b9023ba35a86838789e2c9034a6128728c547aaChris Lattner
36836902e4146d426998ff6a94b16776c1b3f805a048Eli Friedman/// \brief Return the unique type for "pid_t" defined in
36846902e4146d426998ff6a94b16776c1b3f805a048Eli Friedman/// <sys/types.h>. We need this to compute the correct type for vfork().
36856902e4146d426998ff6a94b16776c1b3f805a048Eli FriedmanQualType ASTContext::getProcessIDType() const {
36866902e4146d426998ff6a94b16776c1b3f805a048Eli Friedman  return getFromTargetType(Target->getProcessIDType());
36876902e4146d426998ff6a94b16776c1b3f805a048Eli Friedman}
36886902e4146d426998ff6a94b16776c1b3f805a048Eli Friedman
3689e6327747b72bb687c948270f702ff53c30f411a6Chris Lattner//===----------------------------------------------------------------------===//
3690e6327747b72bb687c948270f702ff53c30f411a6Chris Lattner//                              Type Operators
3691e6327747b72bb687c948270f702ff53c30f411a6Chris Lattner//===----------------------------------------------------------------------===//
3692e6327747b72bb687c948270f702ff53c30f411a6Chris Lattner
36934ba2a17694148e16eaa8d3917f657ffcd3667be4Jay FoadCanQualType ASTContext::getCanonicalParamType(QualType T) const {
369454e14c4db764c0636160d26c5bbf491637c83a76John McCall  // Push qualifiers into arrays, and then discard any remaining
369554e14c4db764c0636160d26c5bbf491637c83a76John McCall  // qualifiers.
369654e14c4db764c0636160d26c5bbf491637c83a76John McCall  T = getCanonicalType(T);
3697745da3a5bb4ea35f93f50301e7fbbb7d78d3b6bbFariborz Jahanian  T = getVariableArrayDecayedType(T);
369854e14c4db764c0636160d26c5bbf491637c83a76John McCall  const Type *Ty = T.getTypePtr();
369954e14c4db764c0636160d26c5bbf491637c83a76John McCall  QualType Result;
370054e14c4db764c0636160d26c5bbf491637c83a76John McCall  if (isa<ArrayType>(Ty)) {
370154e14c4db764c0636160d26c5bbf491637c83a76John McCall    Result = getArrayDecayedType(QualType(Ty,0));
370254e14c4db764c0636160d26c5bbf491637c83a76John McCall  } else if (isa<FunctionType>(Ty)) {
370354e14c4db764c0636160d26c5bbf491637c83a76John McCall    Result = getPointerType(QualType(Ty, 0));
370454e14c4db764c0636160d26c5bbf491637c83a76John McCall  } else {
370554e14c4db764c0636160d26c5bbf491637c83a76John McCall    Result = QualType(Ty, 0);
370654e14c4db764c0636160d26c5bbf491637c83a76John McCall  }
370754e14c4db764c0636160d26c5bbf491637c83a76John McCall
370854e14c4db764c0636160d26c5bbf491637c83a76John McCall  return CanQualType::CreateUnsafe(Result);
370954e14c4db764c0636160d26c5bbf491637c83a76John McCall}
371054e14c4db764c0636160d26c5bbf491637c83a76John McCall
371162c28c831bbf207cc36e683e7c321fc33bf8928cJohn McCallQualType ASTContext::getUnqualifiedArrayType(QualType type,
371262c28c831bbf207cc36e683e7c321fc33bf8928cJohn McCall                                             Qualifiers &quals) {
371362c28c831bbf207cc36e683e7c321fc33bf8928cJohn McCall  SplitQualType splitType = type.getSplitUnqualifiedType();
371462c28c831bbf207cc36e683e7c321fc33bf8928cJohn McCall
371562c28c831bbf207cc36e683e7c321fc33bf8928cJohn McCall  // FIXME: getSplitUnqualifiedType() actually walks all the way to
371662c28c831bbf207cc36e683e7c321fc33bf8928cJohn McCall  // the unqualified desugared type and then drops it on the floor.
371762c28c831bbf207cc36e683e7c321fc33bf8928cJohn McCall  // We then have to strip that sugar back off with
371862c28c831bbf207cc36e683e7c321fc33bf8928cJohn McCall  // getUnqualifiedDesugaredType(), which is silly.
371962c28c831bbf207cc36e683e7c321fc33bf8928cJohn McCall  const ArrayType *AT =
3720200fa53fd420aa8369586f569dbece04930ad6a3John McCall    dyn_cast<ArrayType>(splitType.Ty->getUnqualifiedDesugaredType());
372162c28c831bbf207cc36e683e7c321fc33bf8928cJohn McCall
372262c28c831bbf207cc36e683e7c321fc33bf8928cJohn McCall  // If we don't have an array, just use the results in splitType.
37239dadd94e1c17fa030d1f88d8f2113ff59ccc6714Douglas Gregor  if (!AT) {
3724200fa53fd420aa8369586f569dbece04930ad6a3John McCall    quals = splitType.Quals;
3725200fa53fd420aa8369586f569dbece04930ad6a3John McCall    return QualType(splitType.Ty, 0);
372628e318cc6008c2bc008f0caee70dc736a03d6289Chandler Carruth  }
372728e318cc6008c2bc008f0caee70dc736a03d6289Chandler Carruth
372862c28c831bbf207cc36e683e7c321fc33bf8928cJohn McCall  // Otherwise, recurse on the array's element type.
372962c28c831bbf207cc36e683e7c321fc33bf8928cJohn McCall  QualType elementType = AT->getElementType();
373062c28c831bbf207cc36e683e7c321fc33bf8928cJohn McCall  QualType unqualElementType = getUnqualifiedArrayType(elementType, quals);
373162c28c831bbf207cc36e683e7c321fc33bf8928cJohn McCall
373262c28c831bbf207cc36e683e7c321fc33bf8928cJohn McCall  // If that didn't change the element type, AT has no qualifiers, so we
373362c28c831bbf207cc36e683e7c321fc33bf8928cJohn McCall  // can just use the results in splitType.
373462c28c831bbf207cc36e683e7c321fc33bf8928cJohn McCall  if (elementType == unqualElementType) {
373562c28c831bbf207cc36e683e7c321fc33bf8928cJohn McCall    assert(quals.empty()); // from the recursive call
3736200fa53fd420aa8369586f569dbece04930ad6a3John McCall    quals = splitType.Quals;
3737200fa53fd420aa8369586f569dbece04930ad6a3John McCall    return QualType(splitType.Ty, 0);
373862c28c831bbf207cc36e683e7c321fc33bf8928cJohn McCall  }
373962c28c831bbf207cc36e683e7c321fc33bf8928cJohn McCall
374062c28c831bbf207cc36e683e7c321fc33bf8928cJohn McCall  // Otherwise, add in the qualifiers from the outermost type, then
374162c28c831bbf207cc36e683e7c321fc33bf8928cJohn McCall  // build the type back up.
3742200fa53fd420aa8369586f569dbece04930ad6a3John McCall  quals.addConsistentQualifiers(splitType.Quals);
374328e318cc6008c2bc008f0caee70dc736a03d6289Chandler Carruth
37449dadd94e1c17fa030d1f88d8f2113ff59ccc6714Douglas Gregor  if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(AT)) {
374562c28c831bbf207cc36e683e7c321fc33bf8928cJohn McCall    return getConstantArrayType(unqualElementType, CAT->getSize(),
374628e318cc6008c2bc008f0caee70dc736a03d6289Chandler Carruth                                CAT->getSizeModifier(), 0);
374728e318cc6008c2bc008f0caee70dc736a03d6289Chandler Carruth  }
374828e318cc6008c2bc008f0caee70dc736a03d6289Chandler Carruth
37499dadd94e1c17fa030d1f88d8f2113ff59ccc6714Douglas Gregor  if (const IncompleteArrayType *IAT = dyn_cast<IncompleteArrayType>(AT)) {
375062c28c831bbf207cc36e683e7c321fc33bf8928cJohn McCall    return getIncompleteArrayType(unqualElementType, IAT->getSizeModifier(), 0);
375128e318cc6008c2bc008f0caee70dc736a03d6289Chandler Carruth  }
375228e318cc6008c2bc008f0caee70dc736a03d6289Chandler Carruth
37539dadd94e1c17fa030d1f88d8f2113ff59ccc6714Douglas Gregor  if (const VariableArrayType *VAT = dyn_cast<VariableArrayType>(AT)) {
375462c28c831bbf207cc36e683e7c321fc33bf8928cJohn McCall    return getVariableArrayType(unqualElementType,
37553fa5cae9b3812cab9fab6c042c3329bb70a3d046John McCall                                VAT->getSizeExpr(),
37569dadd94e1c17fa030d1f88d8f2113ff59ccc6714Douglas Gregor                                VAT->getSizeModifier(),
37579dadd94e1c17fa030d1f88d8f2113ff59ccc6714Douglas Gregor                                VAT->getIndexTypeCVRQualifiers(),
37589dadd94e1c17fa030d1f88d8f2113ff59ccc6714Douglas Gregor                                VAT->getBracketsRange());
37599dadd94e1c17fa030d1f88d8f2113ff59ccc6714Douglas Gregor  }
37609dadd94e1c17fa030d1f88d8f2113ff59ccc6714Douglas Gregor
37619dadd94e1c17fa030d1f88d8f2113ff59ccc6714Douglas Gregor  const DependentSizedArrayType *DSAT = cast<DependentSizedArrayType>(AT);
376262c28c831bbf207cc36e683e7c321fc33bf8928cJohn McCall  return getDependentSizedArrayType(unqualElementType, DSAT->getSizeExpr(),
376328e318cc6008c2bc008f0caee70dc736a03d6289Chandler Carruth                                    DSAT->getSizeModifier(), 0,
376428e318cc6008c2bc008f0caee70dc736a03d6289Chandler Carruth                                    SourceRange());
376528e318cc6008c2bc008f0caee70dc736a03d6289Chandler Carruth}
376628e318cc6008c2bc008f0caee70dc736a03d6289Chandler Carruth
37675a57efd7bf88a4a13018e0471ded8063a4abe8afDouglas Gregor/// UnwrapSimilarPointerTypes - If T1 and T2 are pointer types  that
37685a57efd7bf88a4a13018e0471ded8063a4abe8afDouglas Gregor/// may be similar (C++ 4.4), replaces T1 and T2 with the type that
37695a57efd7bf88a4a13018e0471ded8063a4abe8afDouglas Gregor/// they point to and return true. If T1 and T2 aren't pointer types
37705a57efd7bf88a4a13018e0471ded8063a4abe8afDouglas Gregor/// or pointer-to-member types, or if they are not similar at this
37715a57efd7bf88a4a13018e0471ded8063a4abe8afDouglas Gregor/// level, returns false and leaves T1 and T2 unchanged. Top-level
37725a57efd7bf88a4a13018e0471ded8063a4abe8afDouglas Gregor/// qualifiers on T1 and T2 are ignored. This function will typically
37735a57efd7bf88a4a13018e0471ded8063a4abe8afDouglas Gregor/// be called in a loop that successively "unwraps" pointer and
37745a57efd7bf88a4a13018e0471ded8063a4abe8afDouglas Gregor/// pointer-to-member types to compare them at each level.
37755a57efd7bf88a4a13018e0471ded8063a4abe8afDouglas Gregorbool ASTContext::UnwrapSimilarPointerTypes(QualType &T1, QualType &T2) {
37765a57efd7bf88a4a13018e0471ded8063a4abe8afDouglas Gregor  const PointerType *T1PtrType = T1->getAs<PointerType>(),
37775a57efd7bf88a4a13018e0471ded8063a4abe8afDouglas Gregor                    *T2PtrType = T2->getAs<PointerType>();
37785a57efd7bf88a4a13018e0471ded8063a4abe8afDouglas Gregor  if (T1PtrType && T2PtrType) {
37795a57efd7bf88a4a13018e0471ded8063a4abe8afDouglas Gregor    T1 = T1PtrType->getPointeeType();
37805a57efd7bf88a4a13018e0471ded8063a4abe8afDouglas Gregor    T2 = T2PtrType->getPointeeType();
37815a57efd7bf88a4a13018e0471ded8063a4abe8afDouglas Gregor    return true;
37825a57efd7bf88a4a13018e0471ded8063a4abe8afDouglas Gregor  }
37835a57efd7bf88a4a13018e0471ded8063a4abe8afDouglas Gregor
37845a57efd7bf88a4a13018e0471ded8063a4abe8afDouglas Gregor  const MemberPointerType *T1MPType = T1->getAs<MemberPointerType>(),
37855a57efd7bf88a4a13018e0471ded8063a4abe8afDouglas Gregor                          *T2MPType = T2->getAs<MemberPointerType>();
37865a57efd7bf88a4a13018e0471ded8063a4abe8afDouglas Gregor  if (T1MPType && T2MPType &&
37875a57efd7bf88a4a13018e0471ded8063a4abe8afDouglas Gregor      hasSameUnqualifiedType(QualType(T1MPType->getClass(), 0),
37885a57efd7bf88a4a13018e0471ded8063a4abe8afDouglas Gregor                             QualType(T2MPType->getClass(), 0))) {
37895a57efd7bf88a4a13018e0471ded8063a4abe8afDouglas Gregor    T1 = T1MPType->getPointeeType();
37905a57efd7bf88a4a13018e0471ded8063a4abe8afDouglas Gregor    T2 = T2MPType->getPointeeType();
37915a57efd7bf88a4a13018e0471ded8063a4abe8afDouglas Gregor    return true;
37925a57efd7bf88a4a13018e0471ded8063a4abe8afDouglas Gregor  }
37935a57efd7bf88a4a13018e0471ded8063a4abe8afDouglas Gregor
37944e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie  if (getLangOpts().ObjC1) {
37955a57efd7bf88a4a13018e0471ded8063a4abe8afDouglas Gregor    const ObjCObjectPointerType *T1OPType = T1->getAs<ObjCObjectPointerType>(),
37965a57efd7bf88a4a13018e0471ded8063a4abe8afDouglas Gregor                                *T2OPType = T2->getAs<ObjCObjectPointerType>();
37975a57efd7bf88a4a13018e0471ded8063a4abe8afDouglas Gregor    if (T1OPType && T2OPType) {
37985a57efd7bf88a4a13018e0471ded8063a4abe8afDouglas Gregor      T1 = T1OPType->getPointeeType();
37995a57efd7bf88a4a13018e0471ded8063a4abe8afDouglas Gregor      T2 = T2OPType->getPointeeType();
38005a57efd7bf88a4a13018e0471ded8063a4abe8afDouglas Gregor      return true;
38015a57efd7bf88a4a13018e0471ded8063a4abe8afDouglas Gregor    }
38025a57efd7bf88a4a13018e0471ded8063a4abe8afDouglas Gregor  }
38035a57efd7bf88a4a13018e0471ded8063a4abe8afDouglas Gregor
38045a57efd7bf88a4a13018e0471ded8063a4abe8afDouglas Gregor  // FIXME: Block pointers, too?
38055a57efd7bf88a4a13018e0471ded8063a4abe8afDouglas Gregor
38065a57efd7bf88a4a13018e0471ded8063a4abe8afDouglas Gregor  return false;
38075a57efd7bf88a4a13018e0471ded8063a4abe8afDouglas Gregor}
38085a57efd7bf88a4a13018e0471ded8063a4abe8afDouglas Gregor
38094ba2a17694148e16eaa8d3917f657ffcd3667be4Jay FoadDeclarationNameInfo
38104ba2a17694148e16eaa8d3917f657ffcd3667be4Jay FoadASTContext::getNameForTemplate(TemplateName Name,
38114ba2a17694148e16eaa8d3917f657ffcd3667be4Jay Foad                               SourceLocation NameLoc) const {
3812146060435c3efce95c95a092c7a1eb651cfb9ae0John McCall  switch (Name.getKind()) {
3813146060435c3efce95c95a092c7a1eb651cfb9ae0John McCall  case TemplateName::QualifiedTemplate:
3814146060435c3efce95c95a092c7a1eb651cfb9ae0John McCall  case TemplateName::Template:
38152577743c5650c646fb705df01403707e94f2df04Abramo Bagnara    // DNInfo work in progress: CHECKME: what about DNLoc?
3816146060435c3efce95c95a092c7a1eb651cfb9ae0John McCall    return DeclarationNameInfo(Name.getAsTemplateDecl()->getDeclName(),
3817146060435c3efce95c95a092c7a1eb651cfb9ae0John McCall                               NameLoc);
38182577743c5650c646fb705df01403707e94f2df04Abramo Bagnara
3819146060435c3efce95c95a092c7a1eb651cfb9ae0John McCall  case TemplateName::OverloadedTemplate: {
3820146060435c3efce95c95a092c7a1eb651cfb9ae0John McCall    OverloadedTemplateStorage *Storage = Name.getAsOverloadedTemplate();
3821146060435c3efce95c95a092c7a1eb651cfb9ae0John McCall    // DNInfo work in progress: CHECKME: what about DNLoc?
3822146060435c3efce95c95a092c7a1eb651cfb9ae0John McCall    return DeclarationNameInfo((*Storage->begin())->getDeclName(), NameLoc);
3823146060435c3efce95c95a092c7a1eb651cfb9ae0John McCall  }
3824146060435c3efce95c95a092c7a1eb651cfb9ae0John McCall
3825146060435c3efce95c95a092c7a1eb651cfb9ae0John McCall  case TemplateName::DependentTemplate: {
3826146060435c3efce95c95a092c7a1eb651cfb9ae0John McCall    DependentTemplateName *DTN = Name.getAsDependentTemplateName();
38272577743c5650c646fb705df01403707e94f2df04Abramo Bagnara    DeclarationName DName;
382880ad16f4b2b350ddbaae21a52975e63df5aafc2cJohn McCall    if (DTN->isIdentifier()) {
38292577743c5650c646fb705df01403707e94f2df04Abramo Bagnara      DName = DeclarationNames.getIdentifier(DTN->getIdentifier());
38302577743c5650c646fb705df01403707e94f2df04Abramo Bagnara      return DeclarationNameInfo(DName, NameLoc);
383180ad16f4b2b350ddbaae21a52975e63df5aafc2cJohn McCall    } else {
38322577743c5650c646fb705df01403707e94f2df04Abramo Bagnara      DName = DeclarationNames.getCXXOperatorName(DTN->getOperator());
38332577743c5650c646fb705df01403707e94f2df04Abramo Bagnara      // DNInfo work in progress: FIXME: source locations?
38342577743c5650c646fb705df01403707e94f2df04Abramo Bagnara      DeclarationNameLoc DNLoc;
38352577743c5650c646fb705df01403707e94f2df04Abramo Bagnara      DNLoc.CXXOperatorName.BeginOpNameLoc = SourceLocation().getRawEncoding();
38362577743c5650c646fb705df01403707e94f2df04Abramo Bagnara      DNLoc.CXXOperatorName.EndOpNameLoc = SourceLocation().getRawEncoding();
38372577743c5650c646fb705df01403707e94f2df04Abramo Bagnara      return DeclarationNameInfo(DName, NameLoc, DNLoc);
383880ad16f4b2b350ddbaae21a52975e63df5aafc2cJohn McCall    }
383980ad16f4b2b350ddbaae21a52975e63df5aafc2cJohn McCall  }
384080ad16f4b2b350ddbaae21a52975e63df5aafc2cJohn McCall
3841146060435c3efce95c95a092c7a1eb651cfb9ae0John McCall  case TemplateName::SubstTemplateTemplateParm: {
3842146060435c3efce95c95a092c7a1eb651cfb9ae0John McCall    SubstTemplateTemplateParmStorage *subst
3843146060435c3efce95c95a092c7a1eb651cfb9ae0John McCall      = Name.getAsSubstTemplateTemplateParm();
3844146060435c3efce95c95a092c7a1eb651cfb9ae0John McCall    return DeclarationNameInfo(subst->getParameter()->getDeclName(),
3845146060435c3efce95c95a092c7a1eb651cfb9ae0John McCall                               NameLoc);
3846146060435c3efce95c95a092c7a1eb651cfb9ae0John McCall  }
3847146060435c3efce95c95a092c7a1eb651cfb9ae0John McCall
3848146060435c3efce95c95a092c7a1eb651cfb9ae0John McCall  case TemplateName::SubstTemplateTemplateParmPack: {
3849146060435c3efce95c95a092c7a1eb651cfb9ae0John McCall    SubstTemplateTemplateParmPackStorage *subst
3850146060435c3efce95c95a092c7a1eb651cfb9ae0John McCall      = Name.getAsSubstTemplateTemplateParmPack();
3851146060435c3efce95c95a092c7a1eb651cfb9ae0John McCall    return DeclarationNameInfo(subst->getParameterPack()->getDeclName(),
3852146060435c3efce95c95a092c7a1eb651cfb9ae0John McCall                               NameLoc);
3853146060435c3efce95c95a092c7a1eb651cfb9ae0John McCall  }
3854146060435c3efce95c95a092c7a1eb651cfb9ae0John McCall  }
3855146060435c3efce95c95a092c7a1eb651cfb9ae0John McCall
3856146060435c3efce95c95a092c7a1eb651cfb9ae0John McCall  llvm_unreachable("bad template name kind!");
385780ad16f4b2b350ddbaae21a52975e63df5aafc2cJohn McCall}
385880ad16f4b2b350ddbaae21a52975e63df5aafc2cJohn McCall
38594ba2a17694148e16eaa8d3917f657ffcd3667be4Jay FoadTemplateName ASTContext::getCanonicalTemplateName(TemplateName Name) const {
3860146060435c3efce95c95a092c7a1eb651cfb9ae0John McCall  switch (Name.getKind()) {
3861146060435c3efce95c95a092c7a1eb651cfb9ae0John McCall  case TemplateName::QualifiedTemplate:
3862146060435c3efce95c95a092c7a1eb651cfb9ae0John McCall  case TemplateName::Template: {
3863146060435c3efce95c95a092c7a1eb651cfb9ae0John McCall    TemplateDecl *Template = Name.getAsTemplateDecl();
38643e1274f2b99cb99c03cc8e2c6517c37d330b597aDouglas Gregor    if (TemplateTemplateParmDecl *TTP
3865146060435c3efce95c95a092c7a1eb651cfb9ae0John McCall          = dyn_cast<TemplateTemplateParmDecl>(Template))
38663e1274f2b99cb99c03cc8e2c6517c37d330b597aDouglas Gregor      Template = getCanonicalTemplateTemplateParmDecl(TTP);
38673e1274f2b99cb99c03cc8e2c6517c37d330b597aDouglas Gregor
38683e1274f2b99cb99c03cc8e2c6517c37d330b597aDouglas Gregor    // The canonical template name is the canonical template declaration.
386997fbaa2a38804268a024f1a104b43fcf8b4411b0Argyrios Kyrtzidis    return TemplateName(cast<TemplateDecl>(Template->getCanonicalDecl()));
38703e1274f2b99cb99c03cc8e2c6517c37d330b597aDouglas Gregor  }
387125a3ef7cc5fd55dc8cc67c6e6770c8595657e082Douglas Gregor
3872146060435c3efce95c95a092c7a1eb651cfb9ae0John McCall  case TemplateName::OverloadedTemplate:
3873146060435c3efce95c95a092c7a1eb651cfb9ae0John McCall    llvm_unreachable("cannot canonicalize overloaded template");
3874146060435c3efce95c95a092c7a1eb651cfb9ae0John McCall
3875146060435c3efce95c95a092c7a1eb651cfb9ae0John McCall  case TemplateName::DependentTemplate: {
3876146060435c3efce95c95a092c7a1eb651cfb9ae0John McCall    DependentTemplateName *DTN = Name.getAsDependentTemplateName();
3877146060435c3efce95c95a092c7a1eb651cfb9ae0John McCall    assert(DTN && "Non-dependent template names must refer to template decls.");
3878146060435c3efce95c95a092c7a1eb651cfb9ae0John McCall    return DTN->CanonicalTemplateName;
3879146060435c3efce95c95a092c7a1eb651cfb9ae0John McCall  }
3880146060435c3efce95c95a092c7a1eb651cfb9ae0John McCall
3881146060435c3efce95c95a092c7a1eb651cfb9ae0John McCall  case TemplateName::SubstTemplateTemplateParm: {
3882146060435c3efce95c95a092c7a1eb651cfb9ae0John McCall    SubstTemplateTemplateParmStorage *subst
3883146060435c3efce95c95a092c7a1eb651cfb9ae0John McCall      = Name.getAsSubstTemplateTemplateParm();
3884146060435c3efce95c95a092c7a1eb651cfb9ae0John McCall    return getCanonicalTemplateName(subst->getReplacement());
38851aee05d08b2184acadeb36de300e216390780d6cDouglas Gregor  }
38861eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
3887146060435c3efce95c95a092c7a1eb651cfb9ae0John McCall  case TemplateName::SubstTemplateTemplateParmPack: {
3888146060435c3efce95c95a092c7a1eb651cfb9ae0John McCall    SubstTemplateTemplateParmPackStorage *subst
3889146060435c3efce95c95a092c7a1eb651cfb9ae0John McCall                                  = Name.getAsSubstTemplateTemplateParmPack();
3890146060435c3efce95c95a092c7a1eb651cfb9ae0John McCall    TemplateTemplateParmDecl *canonParameter
3891146060435c3efce95c95a092c7a1eb651cfb9ae0John McCall      = getCanonicalTemplateTemplateParmDecl(subst->getParameterPack());
3892146060435c3efce95c95a092c7a1eb651cfb9ae0John McCall    TemplateArgument canonArgPack
3893146060435c3efce95c95a092c7a1eb651cfb9ae0John McCall      = getCanonicalTemplateArgument(subst->getArgumentPack());
3894146060435c3efce95c95a092c7a1eb651cfb9ae0John McCall    return getSubstTemplateTemplateParmPack(canonParameter, canonArgPack);
3895146060435c3efce95c95a092c7a1eb651cfb9ae0John McCall  }
3896146060435c3efce95c95a092c7a1eb651cfb9ae0John McCall  }
3897146060435c3efce95c95a092c7a1eb651cfb9ae0John McCall
3898146060435c3efce95c95a092c7a1eb651cfb9ae0John McCall  llvm_unreachable("bad template name!");
389925a3ef7cc5fd55dc8cc67c6e6770c8595657e082Douglas Gregor}
390025a3ef7cc5fd55dc8cc67c6e6770c8595657e082Douglas Gregor
3901db0d4b751e83b8841b8f48f913f17e50467f13d4Douglas Gregorbool ASTContext::hasSameTemplateName(TemplateName X, TemplateName Y) {
3902db0d4b751e83b8841b8f48f913f17e50467f13d4Douglas Gregor  X = getCanonicalTemplateName(X);
3903db0d4b751e83b8841b8f48f913f17e50467f13d4Douglas Gregor  Y = getCanonicalTemplateName(Y);
3904db0d4b751e83b8841b8f48f913f17e50467f13d4Douglas Gregor  return X.getAsVoidPointer() == Y.getAsVoidPointer();
3905db0d4b751e83b8841b8f48f913f17e50467f13d4Douglas Gregor}
3906db0d4b751e83b8841b8f48f913f17e50467f13d4Douglas Gregor
39071eb4433ac451dc16f4133a88af2d002ac26c58efMike StumpTemplateArgument
39084ba2a17694148e16eaa8d3917f657ffcd3667be4Jay FoadASTContext::getCanonicalTemplateArgument(const TemplateArgument &Arg) const {
39091275ae098acda31fe0e434510c729fcfed0458a1Douglas Gregor  switch (Arg.getKind()) {
39101275ae098acda31fe0e434510c729fcfed0458a1Douglas Gregor    case TemplateArgument::Null:
39111275ae098acda31fe0e434510c729fcfed0458a1Douglas Gregor      return Arg;
39121eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
39131275ae098acda31fe0e434510c729fcfed0458a1Douglas Gregor    case TemplateArgument::Expression:
39141275ae098acda31fe0e434510c729fcfed0458a1Douglas Gregor      return Arg;
39151eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
3916d2008e2c80d6c9282044ec873a937a17a0f33579Douglas Gregor    case TemplateArgument::Declaration: {
3917d7a6b1640e565487d163023a6a2e83f55476ae96Eli Friedman      ValueDecl *D = cast<ValueDecl>(Arg.getAsDecl()->getCanonicalDecl());
3918d7a6b1640e565487d163023a6a2e83f55476ae96Eli Friedman      return TemplateArgument(D, Arg.isDeclForReferenceParam());
3919d2008e2c80d6c9282044ec873a937a17a0f33579Douglas Gregor    }
39201eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
3921d7a6b1640e565487d163023a6a2e83f55476ae96Eli Friedman    case TemplateArgument::NullPtr:
3922d7a6b1640e565487d163023a6a2e83f55476ae96Eli Friedman      return TemplateArgument(getCanonicalType(Arg.getNullPtrType()),
3923d7a6b1640e565487d163023a6a2e83f55476ae96Eli Friedman                              /*isNullPtr*/true);
3924d7a6b1640e565487d163023a6a2e83f55476ae96Eli Friedman
3925788cd06cf8e868a67158aafec5de3a1f408d14f3Douglas Gregor    case TemplateArgument::Template:
3926788cd06cf8e868a67158aafec5de3a1f408d14f3Douglas Gregor      return TemplateArgument(getCanonicalTemplateName(Arg.getAsTemplate()));
3927a7fc901a2e39bfe55bfcff5934b2d9fdf9656491Douglas Gregor
3928a7fc901a2e39bfe55bfcff5934b2d9fdf9656491Douglas Gregor    case TemplateArgument::TemplateExpansion:
3929a7fc901a2e39bfe55bfcff5934b2d9fdf9656491Douglas Gregor      return TemplateArgument(getCanonicalTemplateName(
3930a7fc901a2e39bfe55bfcff5934b2d9fdf9656491Douglas Gregor                                         Arg.getAsTemplateOrTemplatePattern()),
39312be29f423acad3bbe39099a78db2805acb5bdf17Douglas Gregor                              Arg.getNumTemplateExpansions());
3932a7fc901a2e39bfe55bfcff5934b2d9fdf9656491Douglas Gregor
39331275ae098acda31fe0e434510c729fcfed0458a1Douglas Gregor    case TemplateArgument::Integral:
3934855243789cb44799c03f4c7216d3d6308805f549Benjamin Kramer      return TemplateArgument(Arg, getCanonicalType(Arg.getIntegralType()));
39351eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
39361275ae098acda31fe0e434510c729fcfed0458a1Douglas Gregor    case TemplateArgument::Type:
3937833ca991c1bfc967f0995974ca86f66ba1f666b5John McCall      return TemplateArgument(getCanonicalType(Arg.getAsType()));
39381eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
39391275ae098acda31fe0e434510c729fcfed0458a1Douglas Gregor    case TemplateArgument::Pack: {
394087dd697dcc8ecb64df73ae64d61b8c80ff0c157cDouglas Gregor      if (Arg.pack_size() == 0)
394187dd697dcc8ecb64df73ae64d61b8c80ff0c157cDouglas Gregor        return Arg;
394287dd697dcc8ecb64df73ae64d61b8c80ff0c157cDouglas Gregor
3943910f8008fea79120489a53593fe971b0b8a4a740Douglas Gregor      TemplateArgument *CanonArgs
3944910f8008fea79120489a53593fe971b0b8a4a740Douglas Gregor        = new (*this) TemplateArgument[Arg.pack_size()];
39451275ae098acda31fe0e434510c729fcfed0458a1Douglas Gregor      unsigned Idx = 0;
39461eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      for (TemplateArgument::pack_iterator A = Arg.pack_begin(),
39471275ae098acda31fe0e434510c729fcfed0458a1Douglas Gregor                                        AEnd = Arg.pack_end();
39481275ae098acda31fe0e434510c729fcfed0458a1Douglas Gregor           A != AEnd; (void)++A, ++Idx)
39491275ae098acda31fe0e434510c729fcfed0458a1Douglas Gregor        CanonArgs[Idx] = getCanonicalTemplateArgument(*A);
39501eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
3951910f8008fea79120489a53593fe971b0b8a4a740Douglas Gregor      return TemplateArgument(CanonArgs, Arg.pack_size());
39521275ae098acda31fe0e434510c729fcfed0458a1Douglas Gregor    }
39531275ae098acda31fe0e434510c729fcfed0458a1Douglas Gregor  }
39541275ae098acda31fe0e434510c729fcfed0458a1Douglas Gregor
39551275ae098acda31fe0e434510c729fcfed0458a1Douglas Gregor  // Silence GCC warning
3956b219cfc4d75f0a03630b7c4509ef791b7e97b2c8David Blaikie  llvm_unreachable("Unhandled template argument kind");
39571275ae098acda31fe0e434510c729fcfed0458a1Douglas Gregor}
39581275ae098acda31fe0e434510c729fcfed0458a1Douglas Gregor
3959d57959af02b4af695276f4204443afe6e5d86bd8Douglas GregorNestedNameSpecifier *
39604ba2a17694148e16eaa8d3917f657ffcd3667be4Jay FoadASTContext::getCanonicalNestedNameSpecifier(NestedNameSpecifier *NNS) const {
39611eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  if (!NNS)
3962d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor    return 0;
3963d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor
3964d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor  switch (NNS->getKind()) {
3965d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor  case NestedNameSpecifier::Identifier:
3966d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor    // Canonicalize the prefix but keep the identifier the same.
39671eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    return NestedNameSpecifier::Create(*this,
3968d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor                         getCanonicalNestedNameSpecifier(NNS->getPrefix()),
3969d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor                                       NNS->getAsIdentifier());
3970d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor
3971d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor  case NestedNameSpecifier::Namespace:
3972d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor    // A namespace is canonical; build a nested-name-specifier with
3973d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor    // this namespace and no prefix.
397414aba76042e041b2c5e439bf4ae353a0a3c7fd73Douglas Gregor    return NestedNameSpecifier::Create(*this, 0,
397514aba76042e041b2c5e439bf4ae353a0a3c7fd73Douglas Gregor                                 NNS->getAsNamespace()->getOriginalNamespace());
397614aba76042e041b2c5e439bf4ae353a0a3c7fd73Douglas Gregor
397714aba76042e041b2c5e439bf4ae353a0a3c7fd73Douglas Gregor  case NestedNameSpecifier::NamespaceAlias:
397814aba76042e041b2c5e439bf4ae353a0a3c7fd73Douglas Gregor    // A namespace is canonical; build a nested-name-specifier with
397914aba76042e041b2c5e439bf4ae353a0a3c7fd73Douglas Gregor    // this namespace and no prefix.
398014aba76042e041b2c5e439bf4ae353a0a3c7fd73Douglas Gregor    return NestedNameSpecifier::Create(*this, 0,
398114aba76042e041b2c5e439bf4ae353a0a3c7fd73Douglas Gregor                                    NNS->getAsNamespaceAlias()->getNamespace()
398214aba76042e041b2c5e439bf4ae353a0a3c7fd73Douglas Gregor                                                      ->getOriginalNamespace());
3983d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor
3984d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor  case NestedNameSpecifier::TypeSpec:
3985d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor  case NestedNameSpecifier::TypeSpecWithTemplate: {
3986d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor    QualType T = getCanonicalType(QualType(NNS->getAsType(), 0));
3987264bf66d55563dd86a3d7e06738aa427de512d2cDouglas Gregor
3988264bf66d55563dd86a3d7e06738aa427de512d2cDouglas Gregor    // If we have some kind of dependent-named type (e.g., "typename T::type"),
3989264bf66d55563dd86a3d7e06738aa427de512d2cDouglas Gregor    // break it apart into its prefix and identifier, then reconsititute those
3990264bf66d55563dd86a3d7e06738aa427de512d2cDouglas Gregor    // as the canonical nested-name-specifier. This is required to canonicalize
3991264bf66d55563dd86a3d7e06738aa427de512d2cDouglas Gregor    // a dependent nested-name-specifier involving typedefs of dependent-name
3992264bf66d55563dd86a3d7e06738aa427de512d2cDouglas Gregor    // types, e.g.,
3993264bf66d55563dd86a3d7e06738aa427de512d2cDouglas Gregor    //   typedef typename T::type T1;
3994264bf66d55563dd86a3d7e06738aa427de512d2cDouglas Gregor    //   typedef typename T1::type T2;
399516412ef25a2203b7066d0db2b41f944631e5cf79Eli Friedman    if (const DependentNameType *DNT = T->getAs<DependentNameType>())
399616412ef25a2203b7066d0db2b41f944631e5cf79Eli Friedman      return NestedNameSpecifier::Create(*this, DNT->getQualifier(),
3997264bf66d55563dd86a3d7e06738aa427de512d2cDouglas Gregor                           const_cast<IdentifierInfo *>(DNT->getIdentifier()));
3998264bf66d55563dd86a3d7e06738aa427de512d2cDouglas Gregor
399916412ef25a2203b7066d0db2b41f944631e5cf79Eli Friedman    // Otherwise, just canonicalize the type, and force it to be a TypeSpec.
400016412ef25a2203b7066d0db2b41f944631e5cf79Eli Friedman    // FIXME: Why are TypeSpec and TypeSpecWithTemplate distinct in the
400116412ef25a2203b7066d0db2b41f944631e5cf79Eli Friedman    // first place?
40023b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall    return NestedNameSpecifier::Create(*this, 0, false,
40033b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall                                       const_cast<Type*>(T.getTypePtr()));
4004d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor  }
4005d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor
4006d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor  case NestedNameSpecifier::Global:
4007d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor    // The global specifier is canonical and unique.
4008d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor    return NNS;
4009d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor  }
4010d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor
40117530c034c0c71a64c5a9173206d9742ae847af8bDavid Blaikie  llvm_unreachable("Invalid NestedNameSpecifier::Kind!");
4012d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor}
4013d57959af02b4af695276f4204443afe6e5d86bd8Douglas Gregor
4014c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner
40154ba2a17694148e16eaa8d3917f657ffcd3667be4Jay Foadconst ArrayType *ASTContext::getAsArrayType(QualType T) const {
4016c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner  // Handle the non-qualified case efficiently.
4017a4923eb7c4b04d360cb2747641a5e92818edf804Douglas Gregor  if (!T.hasLocalQualifiers()) {
4018c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner    // Handle the common positive case fast.
4019c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner    if (const ArrayType *AT = dyn_cast<ArrayType>(T))
4020c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner      return AT;
4021c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner  }
40221eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
40230953e767ff7817f97b3ab20896b229891eeff45bJohn McCall  // Handle the common negative case fast.
40243b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall  if (!isa<ArrayType>(T.getCanonicalType()))
4025c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner    return 0;
40261eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
40270953e767ff7817f97b3ab20896b229891eeff45bJohn McCall  // Apply any qualifiers from the array type to the element type.  This
4028c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner  // implements C99 6.7.3p8: "If the specification of an array type includes
4029c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner  // any type qualifiers, the element type is so qualified, not the array type."
40301eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
4031c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner  // If we get here, we either have type qualifiers on the type, or we have
4032c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner  // sugar such as a typedef in the way.  If we have type qualifiers on the type
403350d62d1b4a98adbc83de8f8cd1379ea1c25656f7Douglas Gregor  // we must propagate them down into the element type.
40340953e767ff7817f97b3ab20896b229891eeff45bJohn McCall
40353b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall  SplitQualType split = T.getSplitDesugaredType();
4036200fa53fd420aa8369586f569dbece04930ad6a3John McCall  Qualifiers qs = split.Quals;
40371eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
4038c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner  // If we have a simple case, just return now.
4039200fa53fd420aa8369586f569dbece04930ad6a3John McCall  const ArrayType *ATy = dyn_cast<ArrayType>(split.Ty);
40403b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall  if (ATy == 0 || qs.empty())
4041c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner    return ATy;
40421eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
4043c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner  // Otherwise, we have an array and we have qualifiers on it.  Push the
4044c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner  // qualifiers into the array element type and return a new array type.
40453b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall  QualType NewEltTy = getQualifiedType(ATy->getElementType(), qs);
40461eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
4047c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner  if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(ATy))
4048c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner    return cast<ArrayType>(getConstantArrayType(NewEltTy, CAT->getSize(),
4049c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner                                                CAT->getSizeModifier(),
40500953e767ff7817f97b3ab20896b229891eeff45bJohn McCall                                           CAT->getIndexTypeCVRQualifiers()));
4051c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner  if (const IncompleteArrayType *IAT = dyn_cast<IncompleteArrayType>(ATy))
4052c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner    return cast<ArrayType>(getIncompleteArrayType(NewEltTy,
4053c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner                                                  IAT->getSizeModifier(),
40540953e767ff7817f97b3ab20896b229891eeff45bJohn McCall                                           IAT->getIndexTypeCVRQualifiers()));
4055898574e7496ba8fd76290079d3a9d06954992734Douglas Gregor
40561eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  if (const DependentSizedArrayType *DSAT
4057898574e7496ba8fd76290079d3a9d06954992734Douglas Gregor        = dyn_cast<DependentSizedArrayType>(ATy))
4058898574e7496ba8fd76290079d3a9d06954992734Douglas Gregor    return cast<ArrayType>(
40591eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump                     getDependentSizedArrayType(NewEltTy,
40603fa5cae9b3812cab9fab6c042c3329bb70a3d046John McCall                                                DSAT->getSizeExpr(),
4061898574e7496ba8fd76290079d3a9d06954992734Douglas Gregor                                                DSAT->getSizeModifier(),
40620953e767ff7817f97b3ab20896b229891eeff45bJohn McCall                                              DSAT->getIndexTypeCVRQualifiers(),
40637e7eb3da052a6d80ddf2377cab0384c798f73f75Douglas Gregor                                                DSAT->getBracketsRange()));
40641eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
4065c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner  const VariableArrayType *VAT = cast<VariableArrayType>(ATy);
40667e7eb3da052a6d80ddf2377cab0384c798f73f75Douglas Gregor  return cast<ArrayType>(getVariableArrayType(NewEltTy,
40673fa5cae9b3812cab9fab6c042c3329bb70a3d046John McCall                                              VAT->getSizeExpr(),
4068c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner                                              VAT->getSizeModifier(),
40690953e767ff7817f97b3ab20896b229891eeff45bJohn McCall                                              VAT->getIndexTypeCVRQualifiers(),
40707e7eb3da052a6d80ddf2377cab0384c798f73f75Douglas Gregor                                              VAT->getBracketsRange()));
407177c9647cae939104c6cb2b6a4dd8ca859d2e5770Chris Lattner}
407277c9647cae939104c6cb2b6a4dd8ca859d2e5770Chris Lattner
4073ad9689f3531c49e4bff467d9469993606800068cAbramo BagnaraQualType ASTContext::getAdjustedParameterType(QualType T) const {
407479e6bd379773447a74cc3e579d9081e4c5cb6d63Douglas Gregor  // C99 6.7.5.3p7:
407579e6bd379773447a74cc3e579d9081e4c5cb6d63Douglas Gregor  //   A declaration of a parameter as "array of type" shall be
407679e6bd379773447a74cc3e579d9081e4c5cb6d63Douglas Gregor  //   adjusted to "qualified pointer to type", where the type
407779e6bd379773447a74cc3e579d9081e4c5cb6d63Douglas Gregor  //   qualifiers (if any) are those specified within the [ and ] of
407879e6bd379773447a74cc3e579d9081e4c5cb6d63Douglas Gregor  //   the array type derivation.
407979e6bd379773447a74cc3e579d9081e4c5cb6d63Douglas Gregor  if (T->isArrayType())
408079e6bd379773447a74cc3e579d9081e4c5cb6d63Douglas Gregor    return getArrayDecayedType(T);
408179e6bd379773447a74cc3e579d9081e4c5cb6d63Douglas Gregor
408279e6bd379773447a74cc3e579d9081e4c5cb6d63Douglas Gregor  // C99 6.7.5.3p8:
408379e6bd379773447a74cc3e579d9081e4c5cb6d63Douglas Gregor  //   A declaration of a parameter as "function returning type"
408479e6bd379773447a74cc3e579d9081e4c5cb6d63Douglas Gregor  //   shall be adjusted to "pointer to function returning type", as
408579e6bd379773447a74cc3e579d9081e4c5cb6d63Douglas Gregor  //   in 6.3.2.1.
408679e6bd379773447a74cc3e579d9081e4c5cb6d63Douglas Gregor  if (T->isFunctionType())
408779e6bd379773447a74cc3e579d9081e4c5cb6d63Douglas Gregor    return getPointerType(T);
408879e6bd379773447a74cc3e579d9081e4c5cb6d63Douglas Gregor
408979e6bd379773447a74cc3e579d9081e4c5cb6d63Douglas Gregor  return T;
409079e6bd379773447a74cc3e579d9081e4c5cb6d63Douglas Gregor}
409179e6bd379773447a74cc3e579d9081e4c5cb6d63Douglas Gregor
4092ad9689f3531c49e4bff467d9469993606800068cAbramo BagnaraQualType ASTContext::getSignatureParameterType(QualType T) const {
409379e6bd379773447a74cc3e579d9081e4c5cb6d63Douglas Gregor  T = getVariableArrayDecayedType(T);
409479e6bd379773447a74cc3e579d9081e4c5cb6d63Douglas Gregor  T = getAdjustedParameterType(T);
409579e6bd379773447a74cc3e579d9081e4c5cb6d63Douglas Gregor  return T.getUnqualifiedType();
409679e6bd379773447a74cc3e579d9081e4c5cb6d63Douglas Gregor}
409779e6bd379773447a74cc3e579d9081e4c5cb6d63Douglas Gregor
4098e6327747b72bb687c948270f702ff53c30f411a6Chris Lattner/// getArrayDecayedType - Return the properly qualified result of decaying the
4099e6327747b72bb687c948270f702ff53c30f411a6Chris Lattner/// specified array type to a pointer.  This operation is non-trivial when
4100e6327747b72bb687c948270f702ff53c30f411a6Chris Lattner/// handling typedefs etc.  The canonical type of "T" must be an array type,
4101e6327747b72bb687c948270f702ff53c30f411a6Chris Lattner/// this returns a pointer to a properly qualified element of the array.
4102e6327747b72bb687c948270f702ff53c30f411a6Chris Lattner///
4103e6327747b72bb687c948270f702ff53c30f411a6Chris Lattner/// See C99 6.7.5.3p7 and C99 6.3.2.1p3.
41044ba2a17694148e16eaa8d3917f657ffcd3667be4Jay FoadQualType ASTContext::getArrayDecayedType(QualType Ty) const {
4105c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner  // Get the element type with 'getAsArrayType' so that we don't lose any
4106c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner  // typedefs in the element type of the array.  This also handles propagation
4107c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner  // of type qualifiers from the array type into the element type if present
4108c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner  // (C99 6.7.3p8).
4109c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner  const ArrayType *PrettyArrayType = getAsArrayType(Ty);
4110c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner  assert(PrettyArrayType && "Not an array type!");
41111eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
4112c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner  QualType PtrTy = getPointerType(PrettyArrayType->getElementType());
4113e6327747b72bb687c948270f702ff53c30f411a6Chris Lattner
4114e6327747b72bb687c948270f702ff53c30f411a6Chris Lattner  // int x[restrict 4] ->  int *restrict
41150953e767ff7817f97b3ab20896b229891eeff45bJohn McCall  return getQualifiedType(PtrTy, PrettyArrayType->getIndexTypeQualifiers());
4116e6327747b72bb687c948270f702ff53c30f411a6Chris Lattner}
4117e6327747b72bb687c948270f702ff53c30f411a6Chris Lattner
41183b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCallQualType ASTContext::getBaseElementType(const ArrayType *array) const {
41193b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall  return getBaseElementType(array->getElementType());
41205e03f9ea8174ae588c5e69ec6b5ef4c68f8fd766Douglas Gregor}
41215e03f9ea8174ae588c5e69ec6b5ef4c68f8fd766Douglas Gregor
41223b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCallQualType ASTContext::getBaseElementType(QualType type) const {
41233b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall  Qualifiers qs;
41243b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall  while (true) {
41253b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall    SplitQualType split = type.getSplitDesugaredType();
4126200fa53fd420aa8369586f569dbece04930ad6a3John McCall    const ArrayType *array = split.Ty->getAsArrayTypeUnsafe();
41273b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall    if (!array) break;
41281eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
41293b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall    type = array->getElementType();
4130200fa53fd420aa8369586f569dbece04930ad6a3John McCall    qs.addConsistentQualifiers(split.Quals);
41313b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall  }
41321eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
41333b6575108a5b6d8b92ac3a9a7794bf6c3a210907John McCall  return getQualifiedType(type, qs);
41346183a99b064b397d98297904fbd6cf00fe1f453dAnders Carlsson}
41356183a99b064b397d98297904fbd6cf00fe1f453dAnders Carlsson
41360de78998e7bda473b408437053e48661b510d453Fariborz Jahanian/// getConstantArrayElementCount - Returns number of constant array elements.
41371eb4433ac451dc16f4133a88af2d002ac26c58efMike Stumpuint64_t
41380de78998e7bda473b408437053e48661b510d453Fariborz JahanianASTContext::getConstantArrayElementCount(const ConstantArrayType *CA)  const {
41390de78998e7bda473b408437053e48661b510d453Fariborz Jahanian  uint64_t ElementCount = 1;
41400de78998e7bda473b408437053e48661b510d453Fariborz Jahanian  do {
41410de78998e7bda473b408437053e48661b510d453Fariborz Jahanian    ElementCount *= CA->getSize().getZExtValue();
4142d5e839429771ad4d1a8b3db598cbbc6d93621f75Richard Smith    CA = dyn_cast_or_null<ConstantArrayType>(
4143d5e839429771ad4d1a8b3db598cbbc6d93621f75Richard Smith      CA->getElementType()->getAsArrayTypeUnsafe());
41440de78998e7bda473b408437053e48661b510d453Fariborz Jahanian  } while (CA);
41450de78998e7bda473b408437053e48661b510d453Fariborz Jahanian  return ElementCount;
41460de78998e7bda473b408437053e48661b510d453Fariborz Jahanian}
41470de78998e7bda473b408437053e48661b510d453Fariborz Jahanian
41485f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// getFloatingRank - Return a relative rank for floating point types.
41495f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// This routine will assert if passed a built-in type that isn't a float.
4150a75cea3f6be0daa8054d36af81a6ffda1713f82dChris Lattnerstatic FloatingRank getFloatingRank(QualType T) {
4151183700f494ec9b6701b6efe82bcb25f4c79ba561John McCall  if (const ComplexType *CT = T->getAs<ComplexType>())
41525f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    return getFloatingRank(CT->getElementType());
4153a75cea3f6be0daa8054d36af81a6ffda1713f82dChris Lattner
4154183700f494ec9b6701b6efe82bcb25f4c79ba561John McCall  assert(T->getAs<BuiltinType>() && "getFloatingRank(): not a floating type");
4155183700f494ec9b6701b6efe82bcb25f4c79ba561John McCall  switch (T->getAs<BuiltinType>()->getKind()) {
4156b219cfc4d75f0a03630b7c4509ef791b7e97b2c8David Blaikie  default: llvm_unreachable("getFloatingRank(): not a floating type");
4157aa4a99b4a62615db243f7a5c433169f2fc704420Anton Korobeynikov  case BuiltinType::Half:       return HalfRank;
41585f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case BuiltinType::Float:      return FloatRank;
41595f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case BuiltinType::Double:     return DoubleRank;
41605f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case BuiltinType::LongDouble: return LongDoubleRank;
41615f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
41625f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
41635f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
41641eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// getFloatingTypeOfSizeWithinDomain - Returns a real floating
41651eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// point or a complex type (based on typeDomain/typeSize).
4166716c7304ff5d27a95e1e7823acd1d09d5ec3e37fSteve Naroff/// 'typeDomain' is a real floating point or complex type.
4167716c7304ff5d27a95e1e7823acd1d09d5ec3e37fSteve Naroff/// 'typeSize' is a real floating point or complex type.
41681361b11066239ea15764a2a844405352d87296b3Chris LattnerQualType ASTContext::getFloatingTypeOfSizeWithinDomain(QualType Size,
41691361b11066239ea15764a2a844405352d87296b3Chris Lattner                                                       QualType Domain) const {
41701361b11066239ea15764a2a844405352d87296b3Chris Lattner  FloatingRank EltRank = getFloatingRank(Size);
41711361b11066239ea15764a2a844405352d87296b3Chris Lattner  if (Domain->isComplexType()) {
41721361b11066239ea15764a2a844405352d87296b3Chris Lattner    switch (EltRank) {
4173561d3abc881033776ece385a01a510e1cbc1fa92David Blaikie    case HalfRank: llvm_unreachable("Complex half is not supported");
4174f1448a0e4a1e868ff873a8530a61a09cb68666ccSteve Naroff    case FloatRank:      return FloatComplexTy;
4175f1448a0e4a1e868ff873a8530a61a09cb68666ccSteve Naroff    case DoubleRank:     return DoubleComplexTy;
4176f1448a0e4a1e868ff873a8530a61a09cb68666ccSteve Naroff    case LongDoubleRank: return LongDoubleComplexTy;
4177f1448a0e4a1e868ff873a8530a61a09cb68666ccSteve Naroff    }
4178f1448a0e4a1e868ff873a8530a61a09cb68666ccSteve Naroff  }
41791361b11066239ea15764a2a844405352d87296b3Chris Lattner
41801361b11066239ea15764a2a844405352d87296b3Chris Lattner  assert(Domain->isRealFloatingType() && "Unknown domain!");
41811361b11066239ea15764a2a844405352d87296b3Chris Lattner  switch (EltRank) {
418219dbb20ac4371fae3190379a7e7bd467af3c00aaJoey Gouly  case HalfRank:       return HalfTy;
41831361b11066239ea15764a2a844405352d87296b3Chris Lattner  case FloatRank:      return FloatTy;
41841361b11066239ea15764a2a844405352d87296b3Chris Lattner  case DoubleRank:     return DoubleTy;
41851361b11066239ea15764a2a844405352d87296b3Chris Lattner  case LongDoubleRank: return LongDoubleTy;
41865f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
4187561d3abc881033776ece385a01a510e1cbc1fa92David Blaikie  llvm_unreachable("getFloatingRank(): illegal value for rank");
41885f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
41895f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
41907cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner/// getFloatingTypeOrder - Compare the rank of the two specified floating
41917cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner/// point types, ignoring the domain of the type (i.e. 'double' ==
41927cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner/// '_Complex double').  If LHS > RHS, return 1.  If LHS == RHS, return 0. If
41931eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// LHS < RHS, return -1.
41944ba2a17694148e16eaa8d3917f657ffcd3667be4Jay Foadint ASTContext::getFloatingTypeOrder(QualType LHS, QualType RHS) const {
4195a75cea3f6be0daa8054d36af81a6ffda1713f82dChris Lattner  FloatingRank LHSR = getFloatingRank(LHS);
4196a75cea3f6be0daa8054d36af81a6ffda1713f82dChris Lattner  FloatingRank RHSR = getFloatingRank(RHS);
41971eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
4198a75cea3f6be0daa8054d36af81a6ffda1713f82dChris Lattner  if (LHSR == RHSR)
4199fb0d49669aa370b4c0993c5cee60275ef9fd6518Steve Naroff    return 0;
4200a75cea3f6be0daa8054d36af81a6ffda1713f82dChris Lattner  if (LHSR > RHSR)
4201fb0d49669aa370b4c0993c5cee60275ef9fd6518Steve Naroff    return 1;
4202fb0d49669aa370b4c0993c5cee60275ef9fd6518Steve Naroff  return -1;
42035f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
42045f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
4205f52ab250ff92bc51a9ac9a8e19bd43b63a5f844fChris Lattner/// getIntegerRank - Return an integer conversion rank (C99 6.3.1.1p1). This
4206f52ab250ff92bc51a9ac9a8e19bd43b63a5f844fChris Lattner/// routine will assert if passed a built-in type that isn't an integer or enum,
4207f52ab250ff92bc51a9ac9a8e19bd43b63a5f844fChris Lattner/// or if it is not canonicalized.
4208f4c7371fb1d3cebcfb40abad4537bb82515704eaJohn McCallunsigned ASTContext::getIntegerRank(const Type *T) const {
4209467b27b9a24bdc823218ad1ad0e37673b6cc1e83John McCall  assert(T->isCanonicalUnqualified() && "T should be canonicalized");
4210f5c209d23b20ada4a9b6235db50317239cbf6ae1Alisdair Meredith
4211f52ab250ff92bc51a9ac9a8e19bd43b63a5f844fChris Lattner  switch (cast<BuiltinType>(T)->getKind()) {
4212b219cfc4d75f0a03630b7c4509ef791b7e97b2c8David Blaikie  default: llvm_unreachable("getIntegerRank(): not a built-in integer");
42137cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner  case BuiltinType::Bool:
4214f98aba35e6c3da5aae61843fc01334939e4e12ecEli Friedman    return 1 + (getIntWidth(BoolTy) << 3);
42157cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner  case BuiltinType::Char_S:
42167cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner  case BuiltinType::Char_U:
42177cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner  case BuiltinType::SChar:
42187cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner  case BuiltinType::UChar:
4219f98aba35e6c3da5aae61843fc01334939e4e12ecEli Friedman    return 2 + (getIntWidth(CharTy) << 3);
42207cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner  case BuiltinType::Short:
42217cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner  case BuiltinType::UShort:
4222f98aba35e6c3da5aae61843fc01334939e4e12ecEli Friedman    return 3 + (getIntWidth(ShortTy) << 3);
42237cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner  case BuiltinType::Int:
42247cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner  case BuiltinType::UInt:
4225f98aba35e6c3da5aae61843fc01334939e4e12ecEli Friedman    return 4 + (getIntWidth(IntTy) << 3);
42267cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner  case BuiltinType::Long:
42277cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner  case BuiltinType::ULong:
4228f98aba35e6c3da5aae61843fc01334939e4e12ecEli Friedman    return 5 + (getIntWidth(LongTy) << 3);
42297cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner  case BuiltinType::LongLong:
42307cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner  case BuiltinType::ULongLong:
4231f98aba35e6c3da5aae61843fc01334939e4e12ecEli Friedman    return 6 + (getIntWidth(LongLongTy) << 3);
42322df9ced9fd1e8c7d7b38443db07e0e811de22571Chris Lattner  case BuiltinType::Int128:
42332df9ced9fd1e8c7d7b38443db07e0e811de22571Chris Lattner  case BuiltinType::UInt128:
42342df9ced9fd1e8c7d7b38443db07e0e811de22571Chris Lattner    return 7 + (getIntWidth(Int128Ty) << 3);
4235f52ab250ff92bc51a9ac9a8e19bd43b63a5f844fChris Lattner  }
4236f52ab250ff92bc51a9ac9a8e19bd43b63a5f844fChris Lattner}
4237f52ab250ff92bc51a9ac9a8e19bd43b63a5f844fChris Lattner
423804e8357f6801e9ff52673e7e899a67bbabf9de93Eli Friedman/// \brief Whether this is a promotable bitfield reference according
423904e8357f6801e9ff52673e7e899a67bbabf9de93Eli Friedman/// to C99 6.3.1.1p2, bullet 2 (and GCC extensions).
424004e8357f6801e9ff52673e7e899a67bbabf9de93Eli Friedman///
424104e8357f6801e9ff52673e7e899a67bbabf9de93Eli Friedman/// \returns the type this bit-field will promote to, or NULL if no
424204e8357f6801e9ff52673e7e899a67bbabf9de93Eli Friedman/// promotion occurs.
42434ba2a17694148e16eaa8d3917f657ffcd3667be4Jay FoadQualType ASTContext::isPromotableBitField(Expr *E) const {
4244ceafbdeb93ecf323cca74e660bf54504c86f3b71Douglas Gregor  if (E->isTypeDependent() || E->isValueDependent())
4245ceafbdeb93ecf323cca74e660bf54504c86f3b71Douglas Gregor    return QualType();
4246ceafbdeb93ecf323cca74e660bf54504c86f3b71Douglas Gregor
424704e8357f6801e9ff52673e7e899a67bbabf9de93Eli Friedman  FieldDecl *Field = E->getBitField();
424804e8357f6801e9ff52673e7e899a67bbabf9de93Eli Friedman  if (!Field)
424904e8357f6801e9ff52673e7e899a67bbabf9de93Eli Friedman    return QualType();
425004e8357f6801e9ff52673e7e899a67bbabf9de93Eli Friedman
425104e8357f6801e9ff52673e7e899a67bbabf9de93Eli Friedman  QualType FT = Field->getType();
425204e8357f6801e9ff52673e7e899a67bbabf9de93Eli Friedman
4253a6b8b2c09610b8bc4330e948ece8b940c2386406Richard Smith  uint64_t BitWidth = Field->getBitWidthValue(*this);
425404e8357f6801e9ff52673e7e899a67bbabf9de93Eli Friedman  uint64_t IntSize = getTypeSize(IntTy);
425504e8357f6801e9ff52673e7e899a67bbabf9de93Eli Friedman  // GCC extension compatibility: if the bit-field size is less than or equal
425604e8357f6801e9ff52673e7e899a67bbabf9de93Eli Friedman  // to the size of int, it gets promoted no matter what its type is.
425704e8357f6801e9ff52673e7e899a67bbabf9de93Eli Friedman  // For instance, unsigned long bf : 4 gets promoted to signed int.
425804e8357f6801e9ff52673e7e899a67bbabf9de93Eli Friedman  if (BitWidth < IntSize)
425904e8357f6801e9ff52673e7e899a67bbabf9de93Eli Friedman    return IntTy;
426004e8357f6801e9ff52673e7e899a67bbabf9de93Eli Friedman
426104e8357f6801e9ff52673e7e899a67bbabf9de93Eli Friedman  if (BitWidth == IntSize)
426204e8357f6801e9ff52673e7e899a67bbabf9de93Eli Friedman    return FT->isSignedIntegerType() ? IntTy : UnsignedIntTy;
426304e8357f6801e9ff52673e7e899a67bbabf9de93Eli Friedman
426404e8357f6801e9ff52673e7e899a67bbabf9de93Eli Friedman  // Types bigger than int are not subject to promotions, and therefore act
426504e8357f6801e9ff52673e7e899a67bbabf9de93Eli Friedman  // like the base type.
426604e8357f6801e9ff52673e7e899a67bbabf9de93Eli Friedman  // FIXME: This doesn't quite match what gcc does, but what gcc does here
426704e8357f6801e9ff52673e7e899a67bbabf9de93Eli Friedman  // is ridiculous.
426804e8357f6801e9ff52673e7e899a67bbabf9de93Eli Friedman  return QualType();
426904e8357f6801e9ff52673e7e899a67bbabf9de93Eli Friedman}
427004e8357f6801e9ff52673e7e899a67bbabf9de93Eli Friedman
4271a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman/// getPromotedIntegerType - Returns the type that Promotable will
4272a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman/// promote to: C99 6.3.1.1p2, assuming that Promotable is a promotable
4273a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman/// integer type.
42744ba2a17694148e16eaa8d3917f657ffcd3667be4Jay FoadQualType ASTContext::getPromotedIntegerType(QualType Promotable) const {
4275a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman  assert(!Promotable.isNull());
4276a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman  assert(Promotable->isPromotableIntegerType());
4277842aef8d942a880eeb9535d40de31a86838264cbJohn McCall  if (const EnumType *ET = Promotable->getAs<EnumType>())
4278842aef8d942a880eeb9535d40de31a86838264cbJohn McCall    return ET->getDecl()->getPromotionType();
427968a2dc446fe6d32d5da3557902100ed06b21b12bEli Friedman
428068a2dc446fe6d32d5da3557902100ed06b21b12bEli Friedman  if (const BuiltinType *BT = Promotable->getAs<BuiltinType>()) {
428168a2dc446fe6d32d5da3557902100ed06b21b12bEli Friedman    // C++ [conv.prom]: A prvalue of type char16_t, char32_t, or wchar_t
428268a2dc446fe6d32d5da3557902100ed06b21b12bEli Friedman    // (3.9.1) can be converted to a prvalue of the first of the following
428368a2dc446fe6d32d5da3557902100ed06b21b12bEli Friedman    // types that can represent all the values of its underlying type:
428468a2dc446fe6d32d5da3557902100ed06b21b12bEli Friedman    // int, unsigned int, long int, unsigned long int, long long int, or
428568a2dc446fe6d32d5da3557902100ed06b21b12bEli Friedman    // unsigned long long int [...]
428668a2dc446fe6d32d5da3557902100ed06b21b12bEli Friedman    // FIXME: Is there some better way to compute this?
428768a2dc446fe6d32d5da3557902100ed06b21b12bEli Friedman    if (BT->getKind() == BuiltinType::WChar_S ||
428868a2dc446fe6d32d5da3557902100ed06b21b12bEli Friedman        BT->getKind() == BuiltinType::WChar_U ||
428968a2dc446fe6d32d5da3557902100ed06b21b12bEli Friedman        BT->getKind() == BuiltinType::Char16 ||
429068a2dc446fe6d32d5da3557902100ed06b21b12bEli Friedman        BT->getKind() == BuiltinType::Char32) {
429168a2dc446fe6d32d5da3557902100ed06b21b12bEli Friedman      bool FromIsSigned = BT->getKind() == BuiltinType::WChar_S;
429268a2dc446fe6d32d5da3557902100ed06b21b12bEli Friedman      uint64_t FromSize = getTypeSize(BT);
429368a2dc446fe6d32d5da3557902100ed06b21b12bEli Friedman      QualType PromoteTypes[] = { IntTy, UnsignedIntTy, LongTy, UnsignedLongTy,
429468a2dc446fe6d32d5da3557902100ed06b21b12bEli Friedman                                  LongLongTy, UnsignedLongLongTy };
429568a2dc446fe6d32d5da3557902100ed06b21b12bEli Friedman      for (size_t Idx = 0; Idx < llvm::array_lengthof(PromoteTypes); ++Idx) {
429668a2dc446fe6d32d5da3557902100ed06b21b12bEli Friedman        uint64_t ToSize = getTypeSize(PromoteTypes[Idx]);
429768a2dc446fe6d32d5da3557902100ed06b21b12bEli Friedman        if (FromSize < ToSize ||
429868a2dc446fe6d32d5da3557902100ed06b21b12bEli Friedman            (FromSize == ToSize &&
429968a2dc446fe6d32d5da3557902100ed06b21b12bEli Friedman             FromIsSigned == PromoteTypes[Idx]->isSignedIntegerType()))
430068a2dc446fe6d32d5da3557902100ed06b21b12bEli Friedman          return PromoteTypes[Idx];
430168a2dc446fe6d32d5da3557902100ed06b21b12bEli Friedman      }
430268a2dc446fe6d32d5da3557902100ed06b21b12bEli Friedman      llvm_unreachable("char type should fit into long long");
430368a2dc446fe6d32d5da3557902100ed06b21b12bEli Friedman    }
430468a2dc446fe6d32d5da3557902100ed06b21b12bEli Friedman  }
430568a2dc446fe6d32d5da3557902100ed06b21b12bEli Friedman
430668a2dc446fe6d32d5da3557902100ed06b21b12bEli Friedman  // At this point, we should have a signed or unsigned integer type.
4307a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman  if (Promotable->isSignedIntegerType())
4308a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman    return IntTy;
43095b64e77c2d51ca3e9313ed4107d3c4d927895cd6Eli Friedman  uint64_t PromotableSize = getIntWidth(Promotable);
43105b64e77c2d51ca3e9313ed4107d3c4d927895cd6Eli Friedman  uint64_t IntSize = getIntWidth(IntTy);
4311a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman  assert(Promotable->isUnsignedIntegerType() && PromotableSize <= IntSize);
4312a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman  return (PromotableSize != IntSize) ? IntTy : UnsignedIntTy;
4313a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman}
4314a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman
431531862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis/// \brief Recurses in pointer/array types until it finds an objc retainable
431631862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis/// type and returns its ownership.
431731862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios KyrtzidisQualifiers::ObjCLifetime ASTContext::getInnerObjCOwnership(QualType T) const {
431831862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis  while (!T.isNull()) {
431931862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis    if (T.getObjCLifetime() != Qualifiers::OCL_None)
432031862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis      return T.getObjCLifetime();
432131862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis    if (T->isArrayType())
432231862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis      T = getBaseElementType(T);
432331862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis    else if (const PointerType *PT = T->getAs<PointerType>())
432431862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis      T = PT->getPointeeType();
432531862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis    else if (const ReferenceType *RT = T->getAs<ReferenceType>())
432628445f0b62f6aed851ff87ce64d9b19200d3211fArgyrios Kyrtzidis      T = RT->getPointeeType();
432731862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis    else
432831862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis      break;
432931862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis  }
433031862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis
433131862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis  return Qualifiers::OCL_None;
433231862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis}
433331862ba5ea70b1f2c81d03f8a0100b61cd6f06f6Argyrios Kyrtzidis
43341eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// getIntegerTypeOrder - Returns the highest ranked integer type:
43357cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner/// C99 6.3.1.8p1.  If LHS > RHS, return 1.  If LHS == RHS, return 0. If
43361eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// LHS < RHS, return -1.
43374ba2a17694148e16eaa8d3917f657ffcd3667be4Jay Foadint ASTContext::getIntegerTypeOrder(QualType LHS, QualType RHS) const {
4338f4c7371fb1d3cebcfb40abad4537bb82515704eaJohn McCall  const Type *LHSC = getCanonicalType(LHS).getTypePtr();
4339f4c7371fb1d3cebcfb40abad4537bb82515704eaJohn McCall  const Type *RHSC = getCanonicalType(RHS).getTypePtr();
43407cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner  if (LHSC == RHSC) return 0;
43411eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
4342f52ab250ff92bc51a9ac9a8e19bd43b63a5f844fChris Lattner  bool LHSUnsigned = LHSC->isUnsignedIntegerType();
4343f52ab250ff92bc51a9ac9a8e19bd43b63a5f844fChris Lattner  bool RHSUnsigned = RHSC->isUnsignedIntegerType();
43441eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
43457cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner  unsigned LHSRank = getIntegerRank(LHSC);
43467cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner  unsigned RHSRank = getIntegerRank(RHSC);
43471eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
43487cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner  if (LHSUnsigned == RHSUnsigned) {  // Both signed or both unsigned.
43497cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner    if (LHSRank == RHSRank) return 0;
43507cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner    return LHSRank > RHSRank ? 1 : -1;
43517cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner  }
43521eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
43537cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner  // Otherwise, the LHS is signed and the RHS is unsigned or visa versa.
43547cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner  if (LHSUnsigned) {
43557cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner    // If the unsigned [LHS] type is larger, return it.
43567cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner    if (LHSRank >= RHSRank)
43577cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner      return 1;
43581eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
43597cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner    // If the signed type can represent all values of the unsigned type, it
43607cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner    // wins.  Because we are dealing with 2's complement and types that are
43611eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    // powers of two larger than each other, this is always safe.
43627cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner    return -1;
43637cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner  }
43647cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner
43657cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner  // If the unsigned [RHS] type is larger, return it.
43667cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner  if (RHSRank >= LHSRank)
43677cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner    return -1;
43681eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
43697cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner  // If the signed type can represent all values of the unsigned type, it
43707cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner  // wins.  Because we are dealing with 2's complement and types that are
43711eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  // powers of two larger than each other, this is always safe.
43727cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner  return 1;
43735f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
437471993dd85eed9cc42c6b2fa61ee5c53026b74817Anders Carlsson
437579cbc7dd2aacd85a28f469b5dc73c4ea296e7072Anders Carlssonstatic RecordDecl *
4376ba877adeb49ed6dc17f27fa3a3bcd0cca713fd68Abramo BagnaraCreateRecordDecl(const ASTContext &Ctx, RecordDecl::TagKind TK,
4377ba877adeb49ed6dc17f27fa3a3bcd0cca713fd68Abramo Bagnara                 DeclContext *DC, IdentifierInfo *Id) {
4378ba877adeb49ed6dc17f27fa3a3bcd0cca713fd68Abramo Bagnara  SourceLocation Loc;
43794e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie  if (Ctx.getLangOpts().CPlusPlus)
4380ba877adeb49ed6dc17f27fa3a3bcd0cca713fd68Abramo Bagnara    return CXXRecordDecl::Create(Ctx, TK, DC, Loc, Loc, Id);
438179cbc7dd2aacd85a28f469b5dc73c4ea296e7072Anders Carlsson  else
4382ba877adeb49ed6dc17f27fa3a3bcd0cca713fd68Abramo Bagnara    return RecordDecl::Create(Ctx, TK, DC, Loc, Loc, Id);
438379cbc7dd2aacd85a28f469b5dc73c4ea296e7072Anders Carlsson}
4384ba877adeb49ed6dc17f27fa3a3bcd0cca713fd68Abramo Bagnara
43851eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump// getCFConstantStringType - Return the type used for constant CFStrings.
43864ba2a17694148e16eaa8d3917f657ffcd3667be4Jay FoadQualType ASTContext::getCFConstantStringType() const {
438771993dd85eed9cc42c6b2fa61ee5c53026b74817Anders Carlsson  if (!CFConstantStringTypeDecl) {
43881eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    CFConstantStringTypeDecl =
4389ba877adeb49ed6dc17f27fa3a3bcd0cca713fd68Abramo Bagnara      CreateRecordDecl(*this, TTK_Struct, TUDecl,
439079cbc7dd2aacd85a28f469b5dc73c4ea296e7072Anders Carlsson                       &Idents.get("NSConstantString"));
43915cfa011e61e14e6f2e1659047d809706c0e4c6a3John McCall    CFConstantStringTypeDecl->startDefinition();
439279cbc7dd2aacd85a28f469b5dc73c4ea296e7072Anders Carlsson
4393f06273f8bbacb086a46bde456429c8d08f6d07eeAnders Carlsson    QualType FieldTypes[4];
43941eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
439571993dd85eed9cc42c6b2fa61ee5c53026b74817Anders Carlsson    // const int *isa;
43960953e767ff7817f97b3ab20896b229891eeff45bJohn McCall    FieldTypes[0] = getPointerType(IntTy.withConst());
4397f06273f8bbacb086a46bde456429c8d08f6d07eeAnders Carlsson    // int flags;
4398f06273f8bbacb086a46bde456429c8d08f6d07eeAnders Carlsson    FieldTypes[1] = IntTy;
439971993dd85eed9cc42c6b2fa61ee5c53026b74817Anders Carlsson    // const char *str;
44000953e767ff7817f97b3ab20896b229891eeff45bJohn McCall    FieldTypes[2] = getPointerType(CharTy.withConst());
440171993dd85eed9cc42c6b2fa61ee5c53026b74817Anders Carlsson    // long length;
44021eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    FieldTypes[3] = LongTy;
44031eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
440444b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor    // Create fields
440544b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor    for (unsigned i = 0; i < 4; ++i) {
44061eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      FieldDecl *Field = FieldDecl::Create(*this, CFConstantStringTypeDecl,
4407ff676cb48fe8bf7be2feaa251dc7c5fb15af4730Abramo Bagnara                                           SourceLocation(),
440844b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor                                           SourceLocation(), 0,
4409a93c934af4fbf97cbe8e649d82e68ccacfe57c95John McCall                                           FieldTypes[i], /*TInfo=*/0,
44101eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump                                           /*BitWidth=*/0,
44117a614d8380297fcd2bc23986241905d97222948cRichard Smith                                           /*Mutable=*/false,
4412ca5233044ef679840d1ad1c46a36b16e2ee8a6e1Richard Smith                                           ICIS_NoInit);
44132888b65aae768f54062505330df7be230a0510c7John McCall      Field->setAccess(AS_public);
441417945a0f64fe03ff6ec0c2146005a87636e3ac12Argyrios Kyrtzidis      CFConstantStringTypeDecl->addDecl(Field);
441544b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor    }
441644b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor
4417838db383b69b9fb55f55c8e9546477df198a4faaDouglas Gregor    CFConstantStringTypeDecl->completeDefinition();
441871993dd85eed9cc42c6b2fa61ee5c53026b74817Anders Carlsson  }
44191eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
442071993dd85eed9cc42c6b2fa61ee5c53026b74817Anders Carlsson  return getTagDeclType(CFConstantStringTypeDecl);
44218467583c2704e7a9691ea56939a029015f0ade0aGabor Greif}
4422b2cf3573d7351094f6247fcca94703ce88eb9ee0Anders Carlsson
4423f7992132207d7ddc8810a135cec97ebe5805e500Fariborz JahanianQualType ASTContext::getObjCSuperType() const {
4424f7992132207d7ddc8810a135cec97ebe5805e500Fariborz Jahanian  if (ObjCSuperType.isNull()) {
4425f7992132207d7ddc8810a135cec97ebe5805e500Fariborz Jahanian    RecordDecl *ObjCSuperTypeDecl  =
4426f7992132207d7ddc8810a135cec97ebe5805e500Fariborz Jahanian      CreateRecordDecl(*this, TTK_Struct, TUDecl, &Idents.get("objc_super"));
4427f7992132207d7ddc8810a135cec97ebe5805e500Fariborz Jahanian    TUDecl->addDecl(ObjCSuperTypeDecl);
4428f7992132207d7ddc8810a135cec97ebe5805e500Fariborz Jahanian    ObjCSuperType = getTagDeclType(ObjCSuperTypeDecl);
4429f7992132207d7ddc8810a135cec97ebe5805e500Fariborz Jahanian  }
4430f7992132207d7ddc8810a135cec97ebe5805e500Fariborz Jahanian  return ObjCSuperType;
4431f7992132207d7ddc8810a135cec97ebe5805e500Fariborz Jahanian}
4432f7992132207d7ddc8810a135cec97ebe5805e500Fariborz Jahanian
4433319ac896a0fef7365d5589b8021db7e41207fe42Douglas Gregorvoid ASTContext::setCFConstantStringType(QualType T) {
44346217b80b7a1379b74cced1c076338262c3c980b3Ted Kremenek  const RecordType *Rec = T->getAs<RecordType>();
4435319ac896a0fef7365d5589b8021db7e41207fe42Douglas Gregor  assert(Rec && "Invalid CFConstantStringType");
4436319ac896a0fef7365d5589b8021db7e41207fe42Douglas Gregor  CFConstantStringTypeDecl = Rec->getDecl();
4437319ac896a0fef7365d5589b8021db7e41207fe42Douglas Gregor}
4438319ac896a0fef7365d5589b8021db7e41207fe42Douglas Gregor
44394ba2a17694148e16eaa8d3917f657ffcd3667be4Jay FoadQualType ASTContext::getBlockDescriptorType() const {
4440adaaad3715c9c26cdcfdfe3401a13d7b4423ddcfMike Stump  if (BlockDescriptorType)
4441adaaad3715c9c26cdcfdfe3401a13d7b4423ddcfMike Stump    return getTagDeclType(BlockDescriptorType);
4442adaaad3715c9c26cdcfdfe3401a13d7b4423ddcfMike Stump
4443adaaad3715c9c26cdcfdfe3401a13d7b4423ddcfMike Stump  RecordDecl *T;
4444adaaad3715c9c26cdcfdfe3401a13d7b4423ddcfMike Stump  // FIXME: Needs the FlagAppleBlock bit.
4445ba877adeb49ed6dc17f27fa3a3bcd0cca713fd68Abramo Bagnara  T = CreateRecordDecl(*this, TTK_Struct, TUDecl,
444679cbc7dd2aacd85a28f469b5dc73c4ea296e7072Anders Carlsson                       &Idents.get("__block_descriptor"));
44475cfa011e61e14e6f2e1659047d809706c0e4c6a3John McCall  T->startDefinition();
4448adaaad3715c9c26cdcfdfe3401a13d7b4423ddcfMike Stump
4449adaaad3715c9c26cdcfdfe3401a13d7b4423ddcfMike Stump  QualType FieldTypes[] = {
4450adaaad3715c9c26cdcfdfe3401a13d7b4423ddcfMike Stump    UnsignedLongTy,
4451adaaad3715c9c26cdcfdfe3401a13d7b4423ddcfMike Stump    UnsignedLongTy,
4452adaaad3715c9c26cdcfdfe3401a13d7b4423ddcfMike Stump  };
4453adaaad3715c9c26cdcfdfe3401a13d7b4423ddcfMike Stump
4454adaaad3715c9c26cdcfdfe3401a13d7b4423ddcfMike Stump  const char *FieldNames[] = {
4455adaaad3715c9c26cdcfdfe3401a13d7b4423ddcfMike Stump    "reserved",
4456083c25eea14bb4cc4ecc3ec763c60e2e609e22bdMike Stump    "Size"
4457adaaad3715c9c26cdcfdfe3401a13d7b4423ddcfMike Stump  };
4458adaaad3715c9c26cdcfdfe3401a13d7b4423ddcfMike Stump
4459adaaad3715c9c26cdcfdfe3401a13d7b4423ddcfMike Stump  for (size_t i = 0; i < 2; ++i) {
4460ff676cb48fe8bf7be2feaa251dc7c5fb15af4730Abramo Bagnara    FieldDecl *Field = FieldDecl::Create(*this, T, SourceLocation(),
4461adaaad3715c9c26cdcfdfe3401a13d7b4423ddcfMike Stump                                         SourceLocation(),
4462adaaad3715c9c26cdcfdfe3401a13d7b4423ddcfMike Stump                                         &Idents.get(FieldNames[i]),
4463a93c934af4fbf97cbe8e649d82e68ccacfe57c95John McCall                                         FieldTypes[i], /*TInfo=*/0,
4464adaaad3715c9c26cdcfdfe3401a13d7b4423ddcfMike Stump                                         /*BitWidth=*/0,
44657a614d8380297fcd2bc23986241905d97222948cRichard Smith                                         /*Mutable=*/false,
4466ca5233044ef679840d1ad1c46a36b16e2ee8a6e1Richard Smith                                         ICIS_NoInit);
44672888b65aae768f54062505330df7be230a0510c7John McCall    Field->setAccess(AS_public);
4468adaaad3715c9c26cdcfdfe3401a13d7b4423ddcfMike Stump    T->addDecl(Field);
4469adaaad3715c9c26cdcfdfe3401a13d7b4423ddcfMike Stump  }
4470adaaad3715c9c26cdcfdfe3401a13d7b4423ddcfMike Stump
4471838db383b69b9fb55f55c8e9546477df198a4faaDouglas Gregor  T->completeDefinition();
4472adaaad3715c9c26cdcfdfe3401a13d7b4423ddcfMike Stump
4473adaaad3715c9c26cdcfdfe3401a13d7b4423ddcfMike Stump  BlockDescriptorType = T;
4474adaaad3715c9c26cdcfdfe3401a13d7b4423ddcfMike Stump
4475adaaad3715c9c26cdcfdfe3401a13d7b4423ddcfMike Stump  return getTagDeclType(BlockDescriptorType);
4476adaaad3715c9c26cdcfdfe3401a13d7b4423ddcfMike Stump}
4477adaaad3715c9c26cdcfdfe3401a13d7b4423ddcfMike Stump
44784ba2a17694148e16eaa8d3917f657ffcd3667be4Jay FoadQualType ASTContext::getBlockDescriptorExtendedType() const {
4479083c25eea14bb4cc4ecc3ec763c60e2e609e22bdMike Stump  if (BlockDescriptorExtendedType)
4480083c25eea14bb4cc4ecc3ec763c60e2e609e22bdMike Stump    return getTagDeclType(BlockDescriptorExtendedType);
4481083c25eea14bb4cc4ecc3ec763c60e2e609e22bdMike Stump
4482083c25eea14bb4cc4ecc3ec763c60e2e609e22bdMike Stump  RecordDecl *T;
4483083c25eea14bb4cc4ecc3ec763c60e2e609e22bdMike Stump  // FIXME: Needs the FlagAppleBlock bit.
4484ba877adeb49ed6dc17f27fa3a3bcd0cca713fd68Abramo Bagnara  T = CreateRecordDecl(*this, TTK_Struct, TUDecl,
448579cbc7dd2aacd85a28f469b5dc73c4ea296e7072Anders Carlsson                       &Idents.get("__block_descriptor_withcopydispose"));
44865cfa011e61e14e6f2e1659047d809706c0e4c6a3John McCall  T->startDefinition();
4487083c25eea14bb4cc4ecc3ec763c60e2e609e22bdMike Stump
4488083c25eea14bb4cc4ecc3ec763c60e2e609e22bdMike Stump  QualType FieldTypes[] = {
4489083c25eea14bb4cc4ecc3ec763c60e2e609e22bdMike Stump    UnsignedLongTy,
4490083c25eea14bb4cc4ecc3ec763c60e2e609e22bdMike Stump    UnsignedLongTy,
4491083c25eea14bb4cc4ecc3ec763c60e2e609e22bdMike Stump    getPointerType(VoidPtrTy),
4492083c25eea14bb4cc4ecc3ec763c60e2e609e22bdMike Stump    getPointerType(VoidPtrTy)
4493083c25eea14bb4cc4ecc3ec763c60e2e609e22bdMike Stump  };
4494083c25eea14bb4cc4ecc3ec763c60e2e609e22bdMike Stump
4495083c25eea14bb4cc4ecc3ec763c60e2e609e22bdMike Stump  const char *FieldNames[] = {
4496083c25eea14bb4cc4ecc3ec763c60e2e609e22bdMike Stump    "reserved",
4497083c25eea14bb4cc4ecc3ec763c60e2e609e22bdMike Stump    "Size",
4498083c25eea14bb4cc4ecc3ec763c60e2e609e22bdMike Stump    "CopyFuncPtr",
4499083c25eea14bb4cc4ecc3ec763c60e2e609e22bdMike Stump    "DestroyFuncPtr"
4500083c25eea14bb4cc4ecc3ec763c60e2e609e22bdMike Stump  };
4501083c25eea14bb4cc4ecc3ec763c60e2e609e22bdMike Stump
4502083c25eea14bb4cc4ecc3ec763c60e2e609e22bdMike Stump  for (size_t i = 0; i < 4; ++i) {
4503ff676cb48fe8bf7be2feaa251dc7c5fb15af4730Abramo Bagnara    FieldDecl *Field = FieldDecl::Create(*this, T, SourceLocation(),
4504083c25eea14bb4cc4ecc3ec763c60e2e609e22bdMike Stump                                         SourceLocation(),
4505083c25eea14bb4cc4ecc3ec763c60e2e609e22bdMike Stump                                         &Idents.get(FieldNames[i]),
4506a93c934af4fbf97cbe8e649d82e68ccacfe57c95John McCall                                         FieldTypes[i], /*TInfo=*/0,
4507083c25eea14bb4cc4ecc3ec763c60e2e609e22bdMike Stump                                         /*BitWidth=*/0,
45087a614d8380297fcd2bc23986241905d97222948cRichard Smith                                         /*Mutable=*/false,
4509ca5233044ef679840d1ad1c46a36b16e2ee8a6e1Richard Smith                                         ICIS_NoInit);
45102888b65aae768f54062505330df7be230a0510c7John McCall    Field->setAccess(AS_public);
4511083c25eea14bb4cc4ecc3ec763c60e2e609e22bdMike Stump    T->addDecl(Field);
4512083c25eea14bb4cc4ecc3ec763c60e2e609e22bdMike Stump  }
4513083c25eea14bb4cc4ecc3ec763c60e2e609e22bdMike Stump
4514838db383b69b9fb55f55c8e9546477df198a4faaDouglas Gregor  T->completeDefinition();
4515083c25eea14bb4cc4ecc3ec763c60e2e609e22bdMike Stump
4516083c25eea14bb4cc4ecc3ec763c60e2e609e22bdMike Stump  BlockDescriptorExtendedType = T;
4517083c25eea14bb4cc4ecc3ec763c60e2e609e22bdMike Stump
4518083c25eea14bb4cc4ecc3ec763c60e2e609e22bdMike Stump  return getTagDeclType(BlockDescriptorExtendedType);
4519083c25eea14bb4cc4ecc3ec763c60e2e609e22bdMike Stump}
4520083c25eea14bb4cc4ecc3ec763c60e2e609e22bdMike Stump
4521b15c8984ea300624fbbde385d3907667ce1043faFariborz Jahanian/// BlockRequiresCopying - Returns true if byref variable "D" of type "Ty"
4522b15c8984ea300624fbbde385d3907667ce1043faFariborz Jahanian/// requires copy/dispose. Note that this must match the logic
4523b15c8984ea300624fbbde385d3907667ce1043faFariborz Jahanian/// in buildByrefHelpers.
4524b15c8984ea300624fbbde385d3907667ce1043faFariborz Jahanianbool ASTContext::BlockRequiresCopying(QualType Ty,
4525b15c8984ea300624fbbde385d3907667ce1043faFariborz Jahanian                                      const VarDecl *D) {
4526b15c8984ea300624fbbde385d3907667ce1043faFariborz Jahanian  if (const CXXRecordDecl *record = Ty->getAsCXXRecordDecl()) {
4527b15c8984ea300624fbbde385d3907667ce1043faFariborz Jahanian    const Expr *copyExpr = getBlockVarCopyInits(D);
4528b15c8984ea300624fbbde385d3907667ce1043faFariborz Jahanian    if (!copyExpr && record->hasTrivialDestructor()) return false;
4529b15c8984ea300624fbbde385d3907667ce1043faFariborz Jahanian
4530af7b44d847d3e44c43346d508b2e55a6254b6e9dMike Stump    return true;
4531b15c8984ea300624fbbde385d3907667ce1043faFariborz Jahanian  }
4532b15c8984ea300624fbbde385d3907667ce1043faFariborz Jahanian
4533b15c8984ea300624fbbde385d3907667ce1043faFariborz Jahanian  if (!Ty->isObjCRetainableType()) return false;
4534b15c8984ea300624fbbde385d3907667ce1043faFariborz Jahanian
4535b15c8984ea300624fbbde385d3907667ce1043faFariborz Jahanian  Qualifiers qs = Ty.getQualifiers();
4536b15c8984ea300624fbbde385d3907667ce1043faFariborz Jahanian
4537b15c8984ea300624fbbde385d3907667ce1043faFariborz Jahanian  // If we have lifetime, that dominates.
4538b15c8984ea300624fbbde385d3907667ce1043faFariborz Jahanian  if (Qualifiers::ObjCLifetime lifetime = qs.getObjCLifetime()) {
4539b15c8984ea300624fbbde385d3907667ce1043faFariborz Jahanian    assert(getLangOpts().ObjCAutoRefCount);
4540b15c8984ea300624fbbde385d3907667ce1043faFariborz Jahanian
4541b15c8984ea300624fbbde385d3907667ce1043faFariborz Jahanian    switch (lifetime) {
4542b15c8984ea300624fbbde385d3907667ce1043faFariborz Jahanian      case Qualifiers::OCL_None: llvm_unreachable("impossible");
4543b15c8984ea300624fbbde385d3907667ce1043faFariborz Jahanian
4544b15c8984ea300624fbbde385d3907667ce1043faFariborz Jahanian      // These are just bits as far as the runtime is concerned.
4545b15c8984ea300624fbbde385d3907667ce1043faFariborz Jahanian      case Qualifiers::OCL_ExplicitNone:
4546b15c8984ea300624fbbde385d3907667ce1043faFariborz Jahanian      case Qualifiers::OCL_Autoreleasing:
4547b15c8984ea300624fbbde385d3907667ce1043faFariborz Jahanian        return false;
4548b15c8984ea300624fbbde385d3907667ce1043faFariborz Jahanian
4549b15c8984ea300624fbbde385d3907667ce1043faFariborz Jahanian      // Tell the runtime that this is ARC __weak, called by the
4550b15c8984ea300624fbbde385d3907667ce1043faFariborz Jahanian      // byref routines.
4551b15c8984ea300624fbbde385d3907667ce1043faFariborz Jahanian      case Qualifiers::OCL_Weak:
4552b15c8984ea300624fbbde385d3907667ce1043faFariborz Jahanian      // ARC __strong __block variables need to be retained.
4553b15c8984ea300624fbbde385d3907667ce1043faFariborz Jahanian      case Qualifiers::OCL_Strong:
4554b15c8984ea300624fbbde385d3907667ce1043faFariborz Jahanian        return true;
4555e38be617437ccdcc180c5a49e447cbcd07539292Fariborz Jahanian    }
4556b15c8984ea300624fbbde385d3907667ce1043faFariborz Jahanian    llvm_unreachable("fell out of lifetime switch!");
4557e38be617437ccdcc180c5a49e447cbcd07539292Fariborz Jahanian  }
4558b15c8984ea300624fbbde385d3907667ce1043faFariborz Jahanian  return (Ty->isBlockPointerType() || isObjCNSObjectType(Ty) ||
4559b15c8984ea300624fbbde385d3907667ce1043faFariborz Jahanian          Ty->isObjCObjectPointerType());
4560af7b44d847d3e44c43346d508b2e55a6254b6e9dMike Stump}
4561af7b44d847d3e44c43346d508b2e55a6254b6e9dMike Stump
45623ca23d7dc6cb61e6f363a58d9256d548199d120cFariborz Jahanianbool ASTContext::getByrefLifetime(QualType Ty,
45633ca23d7dc6cb61e6f363a58d9256d548199d120cFariborz Jahanian                              Qualifiers::ObjCLifetime &LifeTime,
45643ca23d7dc6cb61e6f363a58d9256d548199d120cFariborz Jahanian                              bool &HasByrefExtendedLayout) const {
45653ca23d7dc6cb61e6f363a58d9256d548199d120cFariborz Jahanian
45663ca23d7dc6cb61e6f363a58d9256d548199d120cFariborz Jahanian  if (!getLangOpts().ObjC1 ||
45673ca23d7dc6cb61e6f363a58d9256d548199d120cFariborz Jahanian      getLangOpts().getGC() != LangOptions::NonGC)
45683ca23d7dc6cb61e6f363a58d9256d548199d120cFariborz Jahanian    return false;
45693ca23d7dc6cb61e6f363a58d9256d548199d120cFariborz Jahanian
45703ca23d7dc6cb61e6f363a58d9256d548199d120cFariborz Jahanian  HasByrefExtendedLayout = false;
457134db84fdb092f89ea3678a0792074a5b9253829aFariborz Jahanian  if (Ty->isRecordType()) {
45723ca23d7dc6cb61e6f363a58d9256d548199d120cFariborz Jahanian    HasByrefExtendedLayout = true;
45733ca23d7dc6cb61e6f363a58d9256d548199d120cFariborz Jahanian    LifeTime = Qualifiers::OCL_None;
45743ca23d7dc6cb61e6f363a58d9256d548199d120cFariborz Jahanian  }
45753ca23d7dc6cb61e6f363a58d9256d548199d120cFariborz Jahanian  else if (getLangOpts().ObjCAutoRefCount)
45763ca23d7dc6cb61e6f363a58d9256d548199d120cFariborz Jahanian    LifeTime = Ty.getObjCLifetime();
45773ca23d7dc6cb61e6f363a58d9256d548199d120cFariborz Jahanian  // MRR.
45783ca23d7dc6cb61e6f363a58d9256d548199d120cFariborz Jahanian  else if (Ty->isObjCObjectPointerType() || Ty->isBlockPointerType())
45793ca23d7dc6cb61e6f363a58d9256d548199d120cFariborz Jahanian    LifeTime = Qualifiers::OCL_ExplicitNone;
45803ca23d7dc6cb61e6f363a58d9256d548199d120cFariborz Jahanian  else
45813ca23d7dc6cb61e6f363a58d9256d548199d120cFariborz Jahanian    LifeTime = Qualifiers::OCL_None;
45823ca23d7dc6cb61e6f363a58d9256d548199d120cFariborz Jahanian  return true;
45833ca23d7dc6cb61e6f363a58d9256d548199d120cFariborz Jahanian}
45843ca23d7dc6cb61e6f363a58d9256d548199d120cFariborz Jahanian
4585e97179c675b341927807c718be215c8d1aab8acbDouglas GregorTypedefDecl *ASTContext::getObjCInstanceTypeDecl() {
4586e97179c675b341927807c718be215c8d1aab8acbDouglas Gregor  if (!ObjCInstanceTypeDecl)
4587e97179c675b341927807c718be215c8d1aab8acbDouglas Gregor    ObjCInstanceTypeDecl = TypedefDecl::Create(*this,
4588e97179c675b341927807c718be215c8d1aab8acbDouglas Gregor                                               getTranslationUnitDecl(),
4589e97179c675b341927807c718be215c8d1aab8acbDouglas Gregor                                               SourceLocation(),
4590e97179c675b341927807c718be215c8d1aab8acbDouglas Gregor                                               SourceLocation(),
4591e97179c675b341927807c718be215c8d1aab8acbDouglas Gregor                                               &Idents.get("instancetype"),
4592e97179c675b341927807c718be215c8d1aab8acbDouglas Gregor                                     getTrivialTypeSourceInfo(getObjCIdType()));
4593e97179c675b341927807c718be215c8d1aab8acbDouglas Gregor  return ObjCInstanceTypeDecl;
4594e97179c675b341927807c718be215c8d1aab8acbDouglas Gregor}
4595e97179c675b341927807c718be215c8d1aab8acbDouglas Gregor
4596e8c49533521c40643653f943d47229e62d277f88Anders Carlsson// This returns true if a type has been typedefed to BOOL:
4597e8c49533521c40643653f943d47229e62d277f88Anders Carlsson// typedef <type> BOOL;
45982d99833e8c956775f2183601cd120b65b569c867Chris Lattnerstatic bool isTypeTypedefedAsBOOL(QualType T) {
4599e8c49533521c40643653f943d47229e62d277f88Anders Carlsson  if (const TypedefType *TT = dyn_cast<TypedefType>(T))
4600bb49c3ee5d270485f4b273691fd14bc97403fa5dChris Lattner    if (IdentifierInfo *II = TT->getDecl()->getIdentifier())
4601bb49c3ee5d270485f4b273691fd14bc97403fa5dChris Lattner      return II->isStr("BOOL");
46021eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
460385f9bceab1542aafff012d4d28e998f4ba16e362Anders Carlsson  return false;
460485f9bceab1542aafff012d4d28e998f4ba16e362Anders Carlsson}
460585f9bceab1542aafff012d4d28e998f4ba16e362Anders Carlsson
4606a526c5c67e5a0473c340903ee542ce570119665fTed Kremenek/// getObjCEncodingTypeSize returns size of type for objective-c encoding
460733e1d64ab5cd5d27f8530ccd056191fe2c9f3f2eFariborz Jahanian/// purpose.
46084ba2a17694148e16eaa8d3917f657ffcd3667be4Jay FoadCharUnits ASTContext::getObjCEncodingTypeSize(QualType type) const {
4609f968d8374791c37bc464efd9168c2d33dd73605fDouglas Gregor  if (!type->isIncompleteArrayType() && type->isIncompleteType())
4610f968d8374791c37bc464efd9168c2d33dd73605fDouglas Gregor    return CharUnits::Zero();
4611f968d8374791c37bc464efd9168c2d33dd73605fDouglas Gregor
4612199c3d6cd16aebbb9c7f0d42af9d922c9628bf70Ken Dyck  CharUnits sz = getTypeSizeInChars(type);
46131eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
461433e1d64ab5cd5d27f8530ccd056191fe2c9f3f2eFariborz Jahanian  // Make all integer and enum types at least as large as an int
46152ade35e2cfd554e49d35a52047cea98a82787af9Douglas Gregor  if (sz.isPositive() && type->isIntegralOrEnumerationType())
4616199c3d6cd16aebbb9c7f0d42af9d922c9628bf70Ken Dyck    sz = std::max(sz, getTypeSizeInChars(IntTy));
461733e1d64ab5cd5d27f8530ccd056191fe2c9f3f2eFariborz Jahanian  // Treat arrays as pointers, since that's how they're passed in.
461833e1d64ab5cd5d27f8530ccd056191fe2c9f3f2eFariborz Jahanian  else if (type->isArrayType())
4619199c3d6cd16aebbb9c7f0d42af9d922c9628bf70Ken Dyck    sz = getTypeSizeInChars(VoidPtrTy);
4620aa8741a1db98eef05f09b1200dba94aa5dc3bc3dKen Dyck  return sz;
4621199c3d6cd16aebbb9c7f0d42af9d922c9628bf70Ken Dyck}
4622199c3d6cd16aebbb9c7f0d42af9d922c9628bf70Ken Dyck
4623199c3d6cd16aebbb9c7f0d42af9d922c9628bf70Ken Dyckstatic inline
4624199c3d6cd16aebbb9c7f0d42af9d922c9628bf70Ken Dyckstd::string charUnitsToString(const CharUnits &CU) {
4625199c3d6cd16aebbb9c7f0d42af9d922c9628bf70Ken Dyck  return llvm::itostr(CU.getQuantity());
462633e1d64ab5cd5d27f8530ccd056191fe2c9f3f2eFariborz Jahanian}
462733e1d64ab5cd5d27f8530ccd056191fe2c9f3f2eFariborz Jahanian
46286b5a61b6dc400027fd793dcadceeb9da944a37eaJohn McCall/// getObjCEncodingForBlock - Return the encoded type for this block
46295e530af5d51572a0ed5dbe50da54bd333840c63dDavid Chisnall/// declaration.
46306b5a61b6dc400027fd793dcadceeb9da944a37eaJohn McCallstd::string ASTContext::getObjCEncodingForBlock(const BlockExpr *Expr) const {
46316b5a61b6dc400027fd793dcadceeb9da944a37eaJohn McCall  std::string S;
46326b5a61b6dc400027fd793dcadceeb9da944a37eaJohn McCall
46335e530af5d51572a0ed5dbe50da54bd333840c63dDavid Chisnall  const BlockDecl *Decl = Expr->getBlockDecl();
46345e530af5d51572a0ed5dbe50da54bd333840c63dDavid Chisnall  QualType BlockTy =
46355e530af5d51572a0ed5dbe50da54bd333840c63dDavid Chisnall      Expr->getType()->getAs<BlockPointerType>()->getPointeeType();
46365e530af5d51572a0ed5dbe50da54bd333840c63dDavid Chisnall  // Encode result type.
46373d145f660a23d5bdabdd1ead83c51f3df1489b09Fariborz Jahanian  if (getLangOpts().EncodeExtendedBlockSig)
463806cffc0511ba76d979fad9ef6944e685936b62f0Fariborz Jahanian    getObjCEncodingForMethodParameter(Decl::OBJC_TQ_None,
463906cffc0511ba76d979fad9ef6944e685936b62f0Fariborz Jahanian                            BlockTy->getAs<FunctionType>()->getResultType(),
464006cffc0511ba76d979fad9ef6944e685936b62f0Fariborz Jahanian                            S, true /*Extended*/);
464106cffc0511ba76d979fad9ef6944e685936b62f0Fariborz Jahanian  else
464206cffc0511ba76d979fad9ef6944e685936b62f0Fariborz Jahanian    getObjCEncodingForType(BlockTy->getAs<FunctionType>()->getResultType(),
464306cffc0511ba76d979fad9ef6944e685936b62f0Fariborz Jahanian                           S);
46445e530af5d51572a0ed5dbe50da54bd333840c63dDavid Chisnall  // Compute size of all parameters.
46455e530af5d51572a0ed5dbe50da54bd333840c63dDavid Chisnall  // Start with computing size of a pointer in number of bytes.
46465e530af5d51572a0ed5dbe50da54bd333840c63dDavid Chisnall  // FIXME: There might(should) be a better way of doing this computation!
46475e530af5d51572a0ed5dbe50da54bd333840c63dDavid Chisnall  SourceLocation Loc;
4648199c3d6cd16aebbb9c7f0d42af9d922c9628bf70Ken Dyck  CharUnits PtrSize = getTypeSizeInChars(VoidPtrTy);
4649199c3d6cd16aebbb9c7f0d42af9d922c9628bf70Ken Dyck  CharUnits ParmOffset = PtrSize;
46506f46c2653c1545cc3fef0c0df996d18160160ce8Fariborz Jahanian  for (BlockDecl::param_const_iterator PI = Decl->param_begin(),
46515e530af5d51572a0ed5dbe50da54bd333840c63dDavid Chisnall       E = Decl->param_end(); PI != E; ++PI) {
46525e530af5d51572a0ed5dbe50da54bd333840c63dDavid Chisnall    QualType PType = (*PI)->getType();
4653aa8741a1db98eef05f09b1200dba94aa5dc3bc3dKen Dyck    CharUnits sz = getObjCEncodingTypeSize(PType);
4654075a54354dc6e3644b12206e5127855091783fd6Fariborz Jahanian    if (sz.isZero())
4655075a54354dc6e3644b12206e5127855091783fd6Fariborz Jahanian      continue;
4656199c3d6cd16aebbb9c7f0d42af9d922c9628bf70Ken Dyck    assert (sz.isPositive() && "BlockExpr - Incomplete param type");
46575e530af5d51572a0ed5dbe50da54bd333840c63dDavid Chisnall    ParmOffset += sz;
46585e530af5d51572a0ed5dbe50da54bd333840c63dDavid Chisnall  }
46595e530af5d51572a0ed5dbe50da54bd333840c63dDavid Chisnall  // Size of the argument frame
4660199c3d6cd16aebbb9c7f0d42af9d922c9628bf70Ken Dyck  S += charUnitsToString(ParmOffset);
46615e530af5d51572a0ed5dbe50da54bd333840c63dDavid Chisnall  // Block pointer and offset.
46625e530af5d51572a0ed5dbe50da54bd333840c63dDavid Chisnall  S += "@?0";
46635e530af5d51572a0ed5dbe50da54bd333840c63dDavid Chisnall
46645e530af5d51572a0ed5dbe50da54bd333840c63dDavid Chisnall  // Argument types.
46655e530af5d51572a0ed5dbe50da54bd333840c63dDavid Chisnall  ParmOffset = PtrSize;
46665e530af5d51572a0ed5dbe50da54bd333840c63dDavid Chisnall  for (BlockDecl::param_const_iterator PI = Decl->param_begin(), E =
46675e530af5d51572a0ed5dbe50da54bd333840c63dDavid Chisnall       Decl->param_end(); PI != E; ++PI) {
46685e530af5d51572a0ed5dbe50da54bd333840c63dDavid Chisnall    ParmVarDecl *PVDecl = *PI;
46695e530af5d51572a0ed5dbe50da54bd333840c63dDavid Chisnall    QualType PType = PVDecl->getOriginalType();
46705e530af5d51572a0ed5dbe50da54bd333840c63dDavid Chisnall    if (const ArrayType *AT =
46715e530af5d51572a0ed5dbe50da54bd333840c63dDavid Chisnall          dyn_cast<ArrayType>(PType->getCanonicalTypeInternal())) {
46725e530af5d51572a0ed5dbe50da54bd333840c63dDavid Chisnall      // Use array's original type only if it has known number of
46735389f48b24937ad7b4093307128b3cbf25235654David Chisnall      // elements.
46745389f48b24937ad7b4093307128b3cbf25235654David Chisnall      if (!isa<ConstantArrayType>(AT))
46755389f48b24937ad7b4093307128b3cbf25235654David Chisnall        PType = PVDecl->getType();
46765389f48b24937ad7b4093307128b3cbf25235654David Chisnall    } else if (PType->isFunctionType())
46775389f48b24937ad7b4093307128b3cbf25235654David Chisnall      PType = PVDecl->getType();
46783d145f660a23d5bdabdd1ead83c51f3df1489b09Fariborz Jahanian    if (getLangOpts().EncodeExtendedBlockSig)
467906cffc0511ba76d979fad9ef6944e685936b62f0Fariborz Jahanian      getObjCEncodingForMethodParameter(Decl::OBJC_TQ_None, PType,
468006cffc0511ba76d979fad9ef6944e685936b62f0Fariborz Jahanian                                      S, true /*Extended*/);
468106cffc0511ba76d979fad9ef6944e685936b62f0Fariborz Jahanian    else
468206cffc0511ba76d979fad9ef6944e685936b62f0Fariborz Jahanian      getObjCEncodingForType(PType, S);
46835389f48b24937ad7b4093307128b3cbf25235654David Chisnall    S += charUnitsToString(ParmOffset);
46845389f48b24937ad7b4093307128b3cbf25235654David Chisnall    ParmOffset += getObjCEncodingTypeSize(PType);
46855389f48b24937ad7b4093307128b3cbf25235654David Chisnall  }
46866b5a61b6dc400027fd793dcadceeb9da944a37eaJohn McCall
46876b5a61b6dc400027fd793dcadceeb9da944a37eaJohn McCall  return S;
46885389f48b24937ad7b4093307128b3cbf25235654David Chisnall}
46895389f48b24937ad7b4093307128b3cbf25235654David Chisnall
4690f968d8374791c37bc464efd9168c2d33dd73605fDouglas Gregorbool ASTContext::getObjCEncodingForFunctionDecl(const FunctionDecl *Decl,
46915389f48b24937ad7b4093307128b3cbf25235654David Chisnall                                                std::string& S) {
46925389f48b24937ad7b4093307128b3cbf25235654David Chisnall  // Encode result type.
46935389f48b24937ad7b4093307128b3cbf25235654David Chisnall  getObjCEncodingForType(Decl->getResultType(), S);
46945389f48b24937ad7b4093307128b3cbf25235654David Chisnall  CharUnits ParmOffset;
46955389f48b24937ad7b4093307128b3cbf25235654David Chisnall  // Compute size of all parameters.
46965389f48b24937ad7b4093307128b3cbf25235654David Chisnall  for (FunctionDecl::param_const_iterator PI = Decl->param_begin(),
46975389f48b24937ad7b4093307128b3cbf25235654David Chisnall       E = Decl->param_end(); PI != E; ++PI) {
46985389f48b24937ad7b4093307128b3cbf25235654David Chisnall    QualType PType = (*PI)->getType();
46995389f48b24937ad7b4093307128b3cbf25235654David Chisnall    CharUnits sz = getObjCEncodingTypeSize(PType);
4700f968d8374791c37bc464efd9168c2d33dd73605fDouglas Gregor    if (sz.isZero())
47017e68ba5660a9b2b854df4ae7a556e6b91d738c6cFariborz Jahanian      continue;
47027e68ba5660a9b2b854df4ae7a556e6b91d738c6cFariborz Jahanian
47035389f48b24937ad7b4093307128b3cbf25235654David Chisnall    assert (sz.isPositive() &&
4704f968d8374791c37bc464efd9168c2d33dd73605fDouglas Gregor        "getObjCEncodingForFunctionDecl - Incomplete param type");
47055389f48b24937ad7b4093307128b3cbf25235654David Chisnall    ParmOffset += sz;
47065389f48b24937ad7b4093307128b3cbf25235654David Chisnall  }
47075389f48b24937ad7b4093307128b3cbf25235654David Chisnall  S += charUnitsToString(ParmOffset);
47085389f48b24937ad7b4093307128b3cbf25235654David Chisnall  ParmOffset = CharUnits::Zero();
47095389f48b24937ad7b4093307128b3cbf25235654David Chisnall
47105389f48b24937ad7b4093307128b3cbf25235654David Chisnall  // Argument types.
47115389f48b24937ad7b4093307128b3cbf25235654David Chisnall  for (FunctionDecl::param_const_iterator PI = Decl->param_begin(),
47125389f48b24937ad7b4093307128b3cbf25235654David Chisnall       E = Decl->param_end(); PI != E; ++PI) {
47135389f48b24937ad7b4093307128b3cbf25235654David Chisnall    ParmVarDecl *PVDecl = *PI;
47145389f48b24937ad7b4093307128b3cbf25235654David Chisnall    QualType PType = PVDecl->getOriginalType();
47155389f48b24937ad7b4093307128b3cbf25235654David Chisnall    if (const ArrayType *AT =
47165389f48b24937ad7b4093307128b3cbf25235654David Chisnall          dyn_cast<ArrayType>(PType->getCanonicalTypeInternal())) {
47175389f48b24937ad7b4093307128b3cbf25235654David Chisnall      // Use array's original type only if it has known number of
47185e530af5d51572a0ed5dbe50da54bd333840c63dDavid Chisnall      // elements.
47195e530af5d51572a0ed5dbe50da54bd333840c63dDavid Chisnall      if (!isa<ConstantArrayType>(AT))
47205e530af5d51572a0ed5dbe50da54bd333840c63dDavid Chisnall        PType = PVDecl->getType();
47215e530af5d51572a0ed5dbe50da54bd333840c63dDavid Chisnall    } else if (PType->isFunctionType())
47225e530af5d51572a0ed5dbe50da54bd333840c63dDavid Chisnall      PType = PVDecl->getType();
47235e530af5d51572a0ed5dbe50da54bd333840c63dDavid Chisnall    getObjCEncodingForType(PType, S);
4724199c3d6cd16aebbb9c7f0d42af9d922c9628bf70Ken Dyck    S += charUnitsToString(ParmOffset);
4725aa8741a1db98eef05f09b1200dba94aa5dc3bc3dKen Dyck    ParmOffset += getObjCEncodingTypeSize(PType);
47265e530af5d51572a0ed5dbe50da54bd333840c63dDavid Chisnall  }
4727f968d8374791c37bc464efd9168c2d33dd73605fDouglas Gregor
4728f968d8374791c37bc464efd9168c2d33dd73605fDouglas Gregor  return false;
47295e530af5d51572a0ed5dbe50da54bd333840c63dDavid Chisnall}
47305e530af5d51572a0ed5dbe50da54bd333840c63dDavid Chisnall
4731dc8dab6fabf4bfd4f4b94bf572ac3342a5bbfcd7Bob Wilson/// getObjCEncodingForMethodParameter - Return the encoded type for a single
4732dc8dab6fabf4bfd4f4b94bf572ac3342a5bbfcd7Bob Wilson/// method parameter or return type. If Extended, include class names and
4733dc8dab6fabf4bfd4f4b94bf572ac3342a5bbfcd7Bob Wilson/// block object types.
4734dc8dab6fabf4bfd4f4b94bf572ac3342a5bbfcd7Bob Wilsonvoid ASTContext::getObjCEncodingForMethodParameter(Decl::ObjCDeclQualifier QT,
4735dc8dab6fabf4bfd4f4b94bf572ac3342a5bbfcd7Bob Wilson                                                   QualType T, std::string& S,
4736dc8dab6fabf4bfd4f4b94bf572ac3342a5bbfcd7Bob Wilson                                                   bool Extended) const {
4737dc8dab6fabf4bfd4f4b94bf572ac3342a5bbfcd7Bob Wilson  // Encode type qualifer, 'in', 'inout', etc. for the parameter.
4738dc8dab6fabf4bfd4f4b94bf572ac3342a5bbfcd7Bob Wilson  getObjCEncodingForTypeQualifier(QT, S);
4739dc8dab6fabf4bfd4f4b94bf572ac3342a5bbfcd7Bob Wilson  // Encode parameter type.
4740dc8dab6fabf4bfd4f4b94bf572ac3342a5bbfcd7Bob Wilson  getObjCEncodingForTypeImpl(T, S, true, true, 0,
4741dc8dab6fabf4bfd4f4b94bf572ac3342a5bbfcd7Bob Wilson                             true     /*OutermostType*/,
4742dc8dab6fabf4bfd4f4b94bf572ac3342a5bbfcd7Bob Wilson                             false    /*EncodingProperty*/,
4743dc8dab6fabf4bfd4f4b94bf572ac3342a5bbfcd7Bob Wilson                             false    /*StructField*/,
4744dc8dab6fabf4bfd4f4b94bf572ac3342a5bbfcd7Bob Wilson                             Extended /*EncodeBlockParameters*/,
4745dc8dab6fabf4bfd4f4b94bf572ac3342a5bbfcd7Bob Wilson                             Extended /*EncodeClassNames*/);
4746dc8dab6fabf4bfd4f4b94bf572ac3342a5bbfcd7Bob Wilson}
4747dc8dab6fabf4bfd4f4b94bf572ac3342a5bbfcd7Bob Wilson
4748a526c5c67e5a0473c340903ee542ce570119665fTed Kremenek/// getObjCEncodingForMethodDecl - Return the encoded type for this method
474933e1d64ab5cd5d27f8530ccd056191fe2c9f3f2eFariborz Jahanian/// declaration.
4750f968d8374791c37bc464efd9168c2d33dd73605fDouglas Gregorbool ASTContext::getObjCEncodingForMethodDecl(const ObjCMethodDecl *Decl,
4751dc8dab6fabf4bfd4f4b94bf572ac3342a5bbfcd7Bob Wilson                                              std::string& S,
4752dc8dab6fabf4bfd4f4b94bf572ac3342a5bbfcd7Bob Wilson                                              bool Extended) const {
4753c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar  // FIXME: This is not very efficient.
4754dc8dab6fabf4bfd4f4b94bf572ac3342a5bbfcd7Bob Wilson  // Encode return type.
4755dc8dab6fabf4bfd4f4b94bf572ac3342a5bbfcd7Bob Wilson  getObjCEncodingForMethodParameter(Decl->getObjCDeclQualifier(),
4756dc8dab6fabf4bfd4f4b94bf572ac3342a5bbfcd7Bob Wilson                                    Decl->getResultType(), S, Extended);
475733e1d64ab5cd5d27f8530ccd056191fe2c9f3f2eFariborz Jahanian  // Compute size of all parameters.
475833e1d64ab5cd5d27f8530ccd056191fe2c9f3f2eFariborz Jahanian  // Start with computing size of a pointer in number of bytes.
475933e1d64ab5cd5d27f8530ccd056191fe2c9f3f2eFariborz Jahanian  // FIXME: There might(should) be a better way of doing this computation!
476033e1d64ab5cd5d27f8530ccd056191fe2c9f3f2eFariborz Jahanian  SourceLocation Loc;
4761199c3d6cd16aebbb9c7f0d42af9d922c9628bf70Ken Dyck  CharUnits PtrSize = getTypeSizeInChars(VoidPtrTy);
476233e1d64ab5cd5d27f8530ccd056191fe2c9f3f2eFariborz Jahanian  // The first two arguments (self and _cmd) are pointers; account for
476333e1d64ab5cd5d27f8530ccd056191fe2c9f3f2eFariborz Jahanian  // their size.
4764199c3d6cd16aebbb9c7f0d42af9d922c9628bf70Ken Dyck  CharUnits ParmOffset = 2 * PtrSize;
4765491306a83c4f0f49f95a3bcbca8580cb98a91c7aArgyrios Kyrtzidis  for (ObjCMethodDecl::param_const_iterator PI = Decl->param_begin(),
47667732cc9c0fdc97a2f8cce4e5933d8103213d1aefFariborz Jahanian       E = Decl->sel_param_end(); PI != E; ++PI) {
476789951a86b594513c2a013532ed45d197413b1087Chris Lattner    QualType PType = (*PI)->getType();
4768aa8741a1db98eef05f09b1200dba94aa5dc3bc3dKen Dyck    CharUnits sz = getObjCEncodingTypeSize(PType);
4769f968d8374791c37bc464efd9168c2d33dd73605fDouglas Gregor    if (sz.isZero())
47707e68ba5660a9b2b854df4ae7a556e6b91d738c6cFariborz Jahanian      continue;
47717e68ba5660a9b2b854df4ae7a556e6b91d738c6cFariborz Jahanian
4772199c3d6cd16aebbb9c7f0d42af9d922c9628bf70Ken Dyck    assert (sz.isPositive() &&
4773199c3d6cd16aebbb9c7f0d42af9d922c9628bf70Ken Dyck        "getObjCEncodingForMethodDecl - Incomplete param type");
477433e1d64ab5cd5d27f8530ccd056191fe2c9f3f2eFariborz Jahanian    ParmOffset += sz;
477533e1d64ab5cd5d27f8530ccd056191fe2c9f3f2eFariborz Jahanian  }
4776199c3d6cd16aebbb9c7f0d42af9d922c9628bf70Ken Dyck  S += charUnitsToString(ParmOffset);
477733e1d64ab5cd5d27f8530ccd056191fe2c9f3f2eFariborz Jahanian  S += "@0:";
4778199c3d6cd16aebbb9c7f0d42af9d922c9628bf70Ken Dyck  S += charUnitsToString(PtrSize);
47791eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
478033e1d64ab5cd5d27f8530ccd056191fe2c9f3f2eFariborz Jahanian  // Argument types.
478133e1d64ab5cd5d27f8530ccd056191fe2c9f3f2eFariborz Jahanian  ParmOffset = 2 * PtrSize;
4782491306a83c4f0f49f95a3bcbca8580cb98a91c7aArgyrios Kyrtzidis  for (ObjCMethodDecl::param_const_iterator PI = Decl->param_begin(),
47837732cc9c0fdc97a2f8cce4e5933d8103213d1aefFariborz Jahanian       E = Decl->sel_param_end(); PI != E; ++PI) {
4784491306a83c4f0f49f95a3bcbca8580cb98a91c7aArgyrios Kyrtzidis    const ParmVarDecl *PVDecl = *PI;
47851eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    QualType PType = PVDecl->getOriginalType();
47864306d3cb9116605728252e2738df24b9f6ab53c3Fariborz Jahanian    if (const ArrayType *AT =
4787ab76d45e023fc5ae966968344e180cd09fdcc746Steve Naroff          dyn_cast<ArrayType>(PType->getCanonicalTypeInternal())) {
4788ab76d45e023fc5ae966968344e180cd09fdcc746Steve Naroff      // Use array's original type only if it has known number of
4789ab76d45e023fc5ae966968344e180cd09fdcc746Steve Naroff      // elements.
4790bb3fde337fb712c0e6da8790d431621be4793048Steve Naroff      if (!isa<ConstantArrayType>(AT))
4791ab76d45e023fc5ae966968344e180cd09fdcc746Steve Naroff        PType = PVDecl->getType();
4792ab76d45e023fc5ae966968344e180cd09fdcc746Steve Naroff    } else if (PType->isFunctionType())
4793ab76d45e023fc5ae966968344e180cd09fdcc746Steve Naroff      PType = PVDecl->getType();
4794dc8dab6fabf4bfd4f4b94bf572ac3342a5bbfcd7Bob Wilson    getObjCEncodingForMethodParameter(PVDecl->getObjCDeclQualifier(),
4795dc8dab6fabf4bfd4f4b94bf572ac3342a5bbfcd7Bob Wilson                                      PType, S, Extended);
4796199c3d6cd16aebbb9c7f0d42af9d922c9628bf70Ken Dyck    S += charUnitsToString(ParmOffset);
4797aa8741a1db98eef05f09b1200dba94aa5dc3bc3dKen Dyck    ParmOffset += getObjCEncodingTypeSize(PType);
479833e1d64ab5cd5d27f8530ccd056191fe2c9f3f2eFariborz Jahanian  }
4799f968d8374791c37bc464efd9168c2d33dd73605fDouglas Gregor
4800f968d8374791c37bc464efd9168c2d33dd73605fDouglas Gregor  return false;
480133e1d64ab5cd5d27f8530ccd056191fe2c9f3f2eFariborz Jahanian}
480233e1d64ab5cd5d27f8530ccd056191fe2c9f3f2eFariborz Jahanian
4803c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar/// getObjCEncodingForPropertyDecl - Return the encoded type for this
480483bccb85ff4b9981c4250c45494b439df8cbf983Fariborz Jahanian/// property declaration. If non-NULL, Container must be either an
4805c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar/// ObjCCategoryImplDecl or ObjCImplementationDecl; it should only be
4806c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar/// NULL when getting encodings for protocol properties.
48071eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// Property attributes are stored as a comma-delimited C string. The simple
48081eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// attributes readonly and bycopy are encoded as single characters. The
48091eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// parametrized attributes, getter=name, setter=name, and ivar=name, are
48101eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// encoded as single characters, followed by an identifier. Property types
48111eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// are also encoded as a parametrized attribute. The characters used to encode
481283bccb85ff4b9981c4250c45494b439df8cbf983Fariborz Jahanian/// these attributes are defined by the following enumeration:
481383bccb85ff4b9981c4250c45494b439df8cbf983Fariborz Jahanian/// @code
481483bccb85ff4b9981c4250c45494b439df8cbf983Fariborz Jahanian/// enum PropertyAttributes {
481583bccb85ff4b9981c4250c45494b439df8cbf983Fariborz Jahanian/// kPropertyReadOnly = 'R',   // property is read-only.
481683bccb85ff4b9981c4250c45494b439df8cbf983Fariborz Jahanian/// kPropertyBycopy = 'C',     // property is a copy of the value last assigned
481783bccb85ff4b9981c4250c45494b439df8cbf983Fariborz Jahanian/// kPropertyByref = '&',  // property is a reference to the value last assigned
481883bccb85ff4b9981c4250c45494b439df8cbf983Fariborz Jahanian/// kPropertyDynamic = 'D',    // property is dynamic
481983bccb85ff4b9981c4250c45494b439df8cbf983Fariborz Jahanian/// kPropertyGetter = 'G',     // followed by getter selector name
482083bccb85ff4b9981c4250c45494b439df8cbf983Fariborz Jahanian/// kPropertySetter = 'S',     // followed by setter selector name
482183bccb85ff4b9981c4250c45494b439df8cbf983Fariborz Jahanian/// kPropertyInstanceVariable = 'V'  // followed by instance variable  name
48220d4cb85130d91da61c45aecb9fd31c7097a7cfccBob Wilson/// kPropertyType = 'T'              // followed by old-style type encoding.
482383bccb85ff4b9981c4250c45494b439df8cbf983Fariborz Jahanian/// kPropertyWeak = 'W'              // 'weak' property
482483bccb85ff4b9981c4250c45494b439df8cbf983Fariborz Jahanian/// kPropertyStrong = 'P'            // property GC'able
482583bccb85ff4b9981c4250c45494b439df8cbf983Fariborz Jahanian/// kPropertyNonAtomic = 'N'         // property non-atomic
482683bccb85ff4b9981c4250c45494b439df8cbf983Fariborz Jahanian/// };
482783bccb85ff4b9981c4250c45494b439df8cbf983Fariborz Jahanian/// @endcode
48281eb4433ac451dc16f4133a88af2d002ac26c58efMike Stumpvoid ASTContext::getObjCEncodingForPropertyDecl(const ObjCPropertyDecl *PD,
4829c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar                                                const Decl *Container,
48304ba2a17694148e16eaa8d3917f657ffcd3667be4Jay Foad                                                std::string& S) const {
4831c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar  // Collect information from the property implementation decl(s).
4832c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar  bool Dynamic = false;
4833c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar  ObjCPropertyImplDecl *SynthesizePID = 0;
4834c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar
4835c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar  // FIXME: Duplicated code due to poor abstraction.
4836c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar  if (Container) {
48371eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    if (const ObjCCategoryImplDecl *CID =
4838c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar        dyn_cast<ObjCCategoryImplDecl>(Container)) {
4839c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar      for (ObjCCategoryImplDecl::propimpl_iterator
484017945a0f64fe03ff6ec0c2146005a87636e3ac12Argyrios Kyrtzidis             i = CID->propimpl_begin(), e = CID->propimpl_end();
4841653f1b1bf293a9bd96fd4dd6372e779cc7af1597Douglas Gregor           i != e; ++i) {
4842581deb3da481053c4993c7600f97acf7768caac5David Blaikie        ObjCPropertyImplDecl *PID = *i;
4843c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar        if (PID->getPropertyDecl() == PD) {
4844c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar          if (PID->getPropertyImplementation()==ObjCPropertyImplDecl::Dynamic) {
4845c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar            Dynamic = true;
4846c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar          } else {
4847c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar            SynthesizePID = PID;
4848c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar          }
4849c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar        }
4850c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar      }
4851c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar    } else {
485261710854be2b098428aff5316e64bd34b30fbcb7Chris Lattner      const ObjCImplementationDecl *OID=cast<ObjCImplementationDecl>(Container);
4853c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar      for (ObjCCategoryImplDecl::propimpl_iterator
485417945a0f64fe03ff6ec0c2146005a87636e3ac12Argyrios Kyrtzidis             i = OID->propimpl_begin(), e = OID->propimpl_end();
4855653f1b1bf293a9bd96fd4dd6372e779cc7af1597Douglas Gregor           i != e; ++i) {
4856581deb3da481053c4993c7600f97acf7768caac5David Blaikie        ObjCPropertyImplDecl *PID = *i;
4857c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar        if (PID->getPropertyDecl() == PD) {
4858c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar          if (PID->getPropertyImplementation()==ObjCPropertyImplDecl::Dynamic) {
4859c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar            Dynamic = true;
4860c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar          } else {
4861c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar            SynthesizePID = PID;
4862c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar          }
4863c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar        }
48641eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      }
4865c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar    }
4866c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar  }
4867c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar
4868c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar  // FIXME: This is not very efficient.
4869c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar  S = "T";
4870c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar
4871c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar  // Encode result type.
4872090b3f71702c5626d8520f9608d77c6f26dcfa15Fariborz Jahanian  // GCC has some special rules regarding encoding of properties which
4873090b3f71702c5626d8520f9608d77c6f26dcfa15Fariborz Jahanian  // closely resembles encoding of ivars.
48741eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  getObjCEncodingForTypeImpl(PD->getType(), S, true, true, 0,
4875090b3f71702c5626d8520f9608d77c6f26dcfa15Fariborz Jahanian                             true /* outermost type */,
4876090b3f71702c5626d8520f9608d77c6f26dcfa15Fariborz Jahanian                             true /* encoding for property */);
4877c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar
4878c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar  if (PD->isReadOnly()) {
4879c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar    S += ",R";
4880c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar  } else {
4881c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar    switch (PD->getSetterKind()) {
4882c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar    case ObjCPropertyDecl::Assign: break;
4883c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar    case ObjCPropertyDecl::Copy:   S += ",C"; break;
48841eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    case ObjCPropertyDecl::Retain: S += ",&"; break;
48853a02b44e3948f7762dbfba94b7961281ca29d022Fariborz Jahanian    case ObjCPropertyDecl::Weak:   S += ",W"; break;
4886c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar    }
4887c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar  }
4888c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar
4889c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar  // It really isn't clear at all what this means, since properties
4890c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar  // are "dynamic by default".
4891c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar  if (Dynamic)
4892c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar    S += ",D";
4893c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar
4894090b3f71702c5626d8520f9608d77c6f26dcfa15Fariborz Jahanian  if (PD->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_nonatomic)
4895090b3f71702c5626d8520f9608d77c6f26dcfa15Fariborz Jahanian    S += ",N";
48961eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
4897c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar  if (PD->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_getter) {
4898c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar    S += ",G";
4899077bf5e2f48acfa9e7d69429b6e4ba86ea14896dChris Lattner    S += PD->getGetterName().getAsString();
4900c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar  }
4901c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar
4902c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar  if (PD->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_setter) {
4903c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar    S += ",S";
4904077bf5e2f48acfa9e7d69429b6e4ba86ea14896dChris Lattner    S += PD->getSetterName().getAsString();
4905c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar  }
4906c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar
4907c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar  if (SynthesizePID) {
4908c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar    const ObjCIvarDecl *OID = SynthesizePID->getPropertyIvarDecl();
4909c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar    S += ",V";
491039f34e97d6a468f0a7dfa5664c61217cffc65b74Chris Lattner    S += OID->getNameAsString();
4911c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar  }
4912c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar
4913c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar  // FIXME: OBJCGC: weak & strong
4914c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar}
4915c56f34a1c1779de15330bdb3eec39b3418802d47Daniel Dunbar
4916a1c033e9514865f3a7b0d8b3b20e6de926cfec6cFariborz Jahanian/// getLegacyIntegralTypeEncoding -
49171eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// Another legacy compatibility encoding: 32-bit longs are encoded as
49181eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// 'l' or 'L' , but not always.  For typedefs, we need to use
4919a1c033e9514865f3a7b0d8b3b20e6de926cfec6cFariborz Jahanian/// 'i' or 'I' instead if encoding a struct field, or a pointer!
4920a1c033e9514865f3a7b0d8b3b20e6de926cfec6cFariborz Jahanian///
4921a1c033e9514865f3a7b0d8b3b20e6de926cfec6cFariborz Jahanianvoid ASTContext::getLegacyIntegralTypeEncoding (QualType &PointeeTy) const {
49228e1fab243ab8023b7ee3899745386b3b3a4258f8Mike Stump  if (isa<TypedefType>(PointeeTy.getTypePtr())) {
4923183700f494ec9b6701b6efe82bcb25f4c79ba561John McCall    if (const BuiltinType *BT = PointeeTy->getAs<BuiltinType>()) {
49244ba2a17694148e16eaa8d3917f657ffcd3667be4Jay Foad      if (BT->getKind() == BuiltinType::ULong && getIntWidth(PointeeTy) == 32)
4925a1c033e9514865f3a7b0d8b3b20e6de926cfec6cFariborz Jahanian        PointeeTy = UnsignedIntTy;
49261eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      else
49274ba2a17694148e16eaa8d3917f657ffcd3667be4Jay Foad        if (BT->getKind() == BuiltinType::Long && getIntWidth(PointeeTy) == 32)
4928a1c033e9514865f3a7b0d8b3b20e6de926cfec6cFariborz Jahanian          PointeeTy = IntTy;
4929a1c033e9514865f3a7b0d8b3b20e6de926cfec6cFariborz Jahanian    }
4930a1c033e9514865f3a7b0d8b3b20e6de926cfec6cFariborz Jahanian  }
4931a1c033e9514865f3a7b0d8b3b20e6de926cfec6cFariborz Jahanian}
4932a1c033e9514865f3a7b0d8b3b20e6de926cfec6cFariborz Jahanian
49337d6b46d9a9d75dea8ef9f6973dd50633c1f37963Fariborz Jahanianvoid ASTContext::getObjCEncodingForType(QualType T, std::string& S,
49344ba2a17694148e16eaa8d3917f657ffcd3667be4Jay Foad                                        const FieldDecl *Field) const {
493582a6cfbc421cc99c5b7313271f399f7ef95056ecDaniel Dunbar  // We follow the behavior of gcc, expanding structures which are
493682a6cfbc421cc99c5b7313271f399f7ef95056ecDaniel Dunbar  // directly pointed to, and expanding embedded structures. Note that
493782a6cfbc421cc99c5b7313271f399f7ef95056ecDaniel Dunbar  // these rules are sufficient to prevent recursive encoding of the
493882a6cfbc421cc99c5b7313271f399f7ef95056ecDaniel Dunbar  // same type.
49391eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  getObjCEncodingForTypeImpl(T, S, true, true, Field,
49405b8c7d9fb620ba3a71e996d61e7b9bdf763b5c09Fariborz Jahanian                             true /* outermost type */);
494182a6cfbc421cc99c5b7313271f399f7ef95056ecDaniel Dunbar}
494282a6cfbc421cc99c5b7313271f399f7ef95056ecDaniel Dunbar
49433624e9e55d98507863d0731a0942cd874f2a1574John McCallstatic char getObjCEncodingForPrimitiveKind(const ASTContext *C,
49443624e9e55d98507863d0731a0942cd874f2a1574John McCall                                            BuiltinType::Kind kind) {
49453624e9e55d98507863d0731a0942cd874f2a1574John McCall    switch (kind) {
494664fd7e86c1a90d9ff78e4a0bd79f69499667a4e3David Chisnall    case BuiltinType::Void:       return 'v';
494764fd7e86c1a90d9ff78e4a0bd79f69499667a4e3David Chisnall    case BuiltinType::Bool:       return 'B';
494864fd7e86c1a90d9ff78e4a0bd79f69499667a4e3David Chisnall    case BuiltinType::Char_U:
494964fd7e86c1a90d9ff78e4a0bd79f69499667a4e3David Chisnall    case BuiltinType::UChar:      return 'C';
49503624e9e55d98507863d0731a0942cd874f2a1574John McCall    case BuiltinType::Char16:
495164fd7e86c1a90d9ff78e4a0bd79f69499667a4e3David Chisnall    case BuiltinType::UShort:     return 'S';
49523624e9e55d98507863d0731a0942cd874f2a1574John McCall    case BuiltinType::Char32:
495364fd7e86c1a90d9ff78e4a0bd79f69499667a4e3David Chisnall    case BuiltinType::UInt:       return 'I';
495464fd7e86c1a90d9ff78e4a0bd79f69499667a4e3David Chisnall    case BuiltinType::ULong:
49553624e9e55d98507863d0731a0942cd874f2a1574John McCall        return C->getTargetInfo().getLongWidth() == 32 ? 'L' : 'Q';
495664fd7e86c1a90d9ff78e4a0bd79f69499667a4e3David Chisnall    case BuiltinType::UInt128:    return 'T';
495764fd7e86c1a90d9ff78e4a0bd79f69499667a4e3David Chisnall    case BuiltinType::ULongLong:  return 'Q';
495864fd7e86c1a90d9ff78e4a0bd79f69499667a4e3David Chisnall    case BuiltinType::Char_S:
495964fd7e86c1a90d9ff78e4a0bd79f69499667a4e3David Chisnall    case BuiltinType::SChar:      return 'c';
496064fd7e86c1a90d9ff78e4a0bd79f69499667a4e3David Chisnall    case BuiltinType::Short:      return 's';
49613f59c975aa5d047f7edd1b900b5e885c38af0ef7Chris Lattner    case BuiltinType::WChar_S:
49623f59c975aa5d047f7edd1b900b5e885c38af0ef7Chris Lattner    case BuiltinType::WChar_U:
496364fd7e86c1a90d9ff78e4a0bd79f69499667a4e3David Chisnall    case BuiltinType::Int:        return 'i';
496464fd7e86c1a90d9ff78e4a0bd79f69499667a4e3David Chisnall    case BuiltinType::Long:
49653624e9e55d98507863d0731a0942cd874f2a1574John McCall      return C->getTargetInfo().getLongWidth() == 32 ? 'l' : 'q';
496664fd7e86c1a90d9ff78e4a0bd79f69499667a4e3David Chisnall    case BuiltinType::LongLong:   return 'q';
496764fd7e86c1a90d9ff78e4a0bd79f69499667a4e3David Chisnall    case BuiltinType::Int128:     return 't';
496864fd7e86c1a90d9ff78e4a0bd79f69499667a4e3David Chisnall    case BuiltinType::Float:      return 'f';
496964fd7e86c1a90d9ff78e4a0bd79f69499667a4e3David Chisnall    case BuiltinType::Double:     return 'd';
49703a0be84b2aed8563150cdbd976a98838afa261ebDaniel Dunbar    case BuiltinType::LongDouble: return 'D';
49713624e9e55d98507863d0731a0942cd874f2a1574John McCall    case BuiltinType::NullPtr:    return '*'; // like char*
49723624e9e55d98507863d0731a0942cd874f2a1574John McCall
49733624e9e55d98507863d0731a0942cd874f2a1574John McCall    case BuiltinType::Half:
49743624e9e55d98507863d0731a0942cd874f2a1574John McCall      // FIXME: potentially need @encodes for these!
49753624e9e55d98507863d0731a0942cd874f2a1574John McCall      return ' ';
49763624e9e55d98507863d0731a0942cd874f2a1574John McCall
49773624e9e55d98507863d0731a0942cd874f2a1574John McCall    case BuiltinType::ObjCId:
49783624e9e55d98507863d0731a0942cd874f2a1574John McCall    case BuiltinType::ObjCClass:
49793624e9e55d98507863d0731a0942cd874f2a1574John McCall    case BuiltinType::ObjCSel:
49803624e9e55d98507863d0731a0942cd874f2a1574John McCall      llvm_unreachable("@encoding ObjC primitive type");
49813624e9e55d98507863d0731a0942cd874f2a1574John McCall
49823624e9e55d98507863d0731a0942cd874f2a1574John McCall    // OpenCL and placeholder types don't need @encodings.
49833624e9e55d98507863d0731a0942cd874f2a1574John McCall    case BuiltinType::OCLImage1d:
49843624e9e55d98507863d0731a0942cd874f2a1574John McCall    case BuiltinType::OCLImage1dArray:
49853624e9e55d98507863d0731a0942cd874f2a1574John McCall    case BuiltinType::OCLImage1dBuffer:
49863624e9e55d98507863d0731a0942cd874f2a1574John McCall    case BuiltinType::OCLImage2d:
49873624e9e55d98507863d0731a0942cd874f2a1574John McCall    case BuiltinType::OCLImage2dArray:
49883624e9e55d98507863d0731a0942cd874f2a1574John McCall    case BuiltinType::OCLImage3d:
4989e6b9d802fb7b16d93474c4f1c179ab36202e8a8bGuy Benyei    case BuiltinType::OCLEvent:
499021f18c4fda167dc5f72feddbd6a7ac1b63200a0dGuy Benyei    case BuiltinType::OCLSampler:
49913624e9e55d98507863d0731a0942cd874f2a1574John McCall    case BuiltinType::Dependent:
49923624e9e55d98507863d0731a0942cd874f2a1574John McCall#define BUILTIN_TYPE(KIND, ID)
49933624e9e55d98507863d0731a0942cd874f2a1574John McCall#define PLACEHOLDER_TYPE(KIND, ID) \
49943624e9e55d98507863d0731a0942cd874f2a1574John McCall    case BuiltinType::KIND:
49953624e9e55d98507863d0731a0942cd874f2a1574John McCall#include "clang/AST/BuiltinTypes.def"
49963624e9e55d98507863d0731a0942cd874f2a1574John McCall      llvm_unreachable("invalid builtin type for @encode");
499764fd7e86c1a90d9ff78e4a0bd79f69499667a4e3David Chisnall    }
4998719e53f64637c5ac41aac0c711e1977487ffc0d7David Blaikie    llvm_unreachable("invalid BuiltinType::Kind value");
499964fd7e86c1a90d9ff78e4a0bd79f69499667a4e3David Chisnall}
500064fd7e86c1a90d9ff78e4a0bd79f69499667a4e3David Chisnall
50015471bc85b69912e3b448de004498a80c0de32296Douglas Gregorstatic char ObjCEncodingForEnumType(const ASTContext *C, const EnumType *ET) {
50025471bc85b69912e3b448de004498a80c0de32296Douglas Gregor  EnumDecl *Enum = ET->getDecl();
50035471bc85b69912e3b448de004498a80c0de32296Douglas Gregor
50045471bc85b69912e3b448de004498a80c0de32296Douglas Gregor  // The encoding of an non-fixed enum type is always 'i', regardless of size.
50055471bc85b69912e3b448de004498a80c0de32296Douglas Gregor  if (!Enum->isFixed())
50065471bc85b69912e3b448de004498a80c0de32296Douglas Gregor    return 'i';
50075471bc85b69912e3b448de004498a80c0de32296Douglas Gregor
50085471bc85b69912e3b448de004498a80c0de32296Douglas Gregor  // The encoding of a fixed enum type matches its fixed underlying type.
50093624e9e55d98507863d0731a0942cd874f2a1574John McCall  const BuiltinType *BT = Enum->getIntegerType()->castAs<BuiltinType>();
50103624e9e55d98507863d0731a0942cd874f2a1574John McCall  return getObjCEncodingForPrimitiveKind(C, BT->getKind());
50115471bc85b69912e3b448de004498a80c0de32296Douglas Gregor}
50125471bc85b69912e3b448de004498a80c0de32296Douglas Gregor
50134ba2a17694148e16eaa8d3917f657ffcd3667be4Jay Foadstatic void EncodeBitField(const ASTContext *Ctx, std::string& S,
501464fd7e86c1a90d9ff78e4a0bd79f69499667a4e3David Chisnall                           QualType T, const FieldDecl *FD) {
5015a6b8b2c09610b8bc4330e948ece8b940c2386406Richard Smith  assert(FD->isBitField() && "not a bitfield - getObjCEncodingForTypeImpl");
50168b4bf90eb6d3d08cf3bfb86705f0fdb20b9c5875Fariborz Jahanian  S += 'b';
501764fd7e86c1a90d9ff78e4a0bd79f69499667a4e3David Chisnall  // The NeXT runtime encodes bit fields as b followed by the number of bits.
501864fd7e86c1a90d9ff78e4a0bd79f69499667a4e3David Chisnall  // The GNU runtime requires more information; bitfields are encoded as b,
501964fd7e86c1a90d9ff78e4a0bd79f69499667a4e3David Chisnall  // then the offset (in bits) of the first element, then the type of the
502064fd7e86c1a90d9ff78e4a0bd79f69499667a4e3David Chisnall  // bitfield, then the size in bits.  For example, in this structure:
502164fd7e86c1a90d9ff78e4a0bd79f69499667a4e3David Chisnall  //
502264fd7e86c1a90d9ff78e4a0bd79f69499667a4e3David Chisnall  // struct
502364fd7e86c1a90d9ff78e4a0bd79f69499667a4e3David Chisnall  // {
502464fd7e86c1a90d9ff78e4a0bd79f69499667a4e3David Chisnall  //    int integer;
502564fd7e86c1a90d9ff78e4a0bd79f69499667a4e3David Chisnall  //    int flags:2;
502664fd7e86c1a90d9ff78e4a0bd79f69499667a4e3David Chisnall  // };
502764fd7e86c1a90d9ff78e4a0bd79f69499667a4e3David Chisnall  // On a 32-bit system, the encoding for flags would be b2 for the NeXT
502864fd7e86c1a90d9ff78e4a0bd79f69499667a4e3David Chisnall  // runtime, but b32i2 for the GNU runtime.  The reason for this extra
502964fd7e86c1a90d9ff78e4a0bd79f69499667a4e3David Chisnall  // information is not especially sensible, but we're stuck with it for
503064fd7e86c1a90d9ff78e4a0bd79f69499667a4e3David Chisnall  // compatibility with GCC, although providing it breaks anything that
503164fd7e86c1a90d9ff78e4a0bd79f69499667a4e3David Chisnall  // actually uses runtime introspection and wants to work on both runtimes...
5032260611a32535c851237926bfcf78869b13c07d5bJohn McCall  if (Ctx->getLangOpts().ObjCRuntime.isGNUFamily()) {
503364fd7e86c1a90d9ff78e4a0bd79f69499667a4e3David Chisnall    const RecordDecl *RD = FD->getParent();
503464fd7e86c1a90d9ff78e4a0bd79f69499667a4e3David Chisnall    const ASTRecordLayout &RL = Ctx->getASTRecordLayout(RD);
503582905749d5c8d8b4edec11de754a73349cb96603Eli Friedman    S += llvm::utostr(RL.getFieldOffset(FD->getFieldIndex()));
50365471bc85b69912e3b448de004498a80c0de32296Douglas Gregor    if (const EnumType *ET = T->getAs<EnumType>())
50375471bc85b69912e3b448de004498a80c0de32296Douglas Gregor      S += ObjCEncodingForEnumType(Ctx, ET);
50383624e9e55d98507863d0731a0942cd874f2a1574John McCall    else {
50393624e9e55d98507863d0731a0942cd874f2a1574John McCall      const BuiltinType *BT = T->castAs<BuiltinType>();
50403624e9e55d98507863d0731a0942cd874f2a1574John McCall      S += getObjCEncodingForPrimitiveKind(Ctx, BT->getKind());
50413624e9e55d98507863d0731a0942cd874f2a1574John McCall    }
504264fd7e86c1a90d9ff78e4a0bd79f69499667a4e3David Chisnall  }
5043a6b8b2c09610b8bc4330e948ece8b940c2386406Richard Smith  S += llvm::utostr(FD->getBitWidthValue(*Ctx));
50448b4bf90eb6d3d08cf3bfb86705f0fdb20b9c5875Fariborz Jahanian}
50458b4bf90eb6d3d08cf3bfb86705f0fdb20b9c5875Fariborz Jahanian
504601eb9b9683535d8a65c704ad2c545903409e2d36Daniel Dunbar// FIXME: Use SmallString for accumulating string.
504782a6cfbc421cc99c5b7313271f399f7ef95056ecDaniel Dunbarvoid ASTContext::getObjCEncodingForTypeImpl(QualType T, std::string& S,
504882a6cfbc421cc99c5b7313271f399f7ef95056ecDaniel Dunbar                                            bool ExpandPointedToStructures,
504982a6cfbc421cc99c5b7313271f399f7ef95056ecDaniel Dunbar                                            bool ExpandStructures,
5050153bfe5795e2c1a5a738e73d3784964e082237fcDaniel Dunbar                                            const FieldDecl *FD,
5051090b3f71702c5626d8520f9608d77c6f26dcfa15Fariborz Jahanian                                            bool OutermostType,
50522636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis                                            bool EncodingProperty,
5053dc8dab6fabf4bfd4f4b94bf572ac3342a5bbfcd7Bob Wilson                                            bool StructField,
5054dc8dab6fabf4bfd4f4b94bf572ac3342a5bbfcd7Bob Wilson                                            bool EncodeBlockParameters,
505517c1a2e748d49d293c9926eeeb85aa4890182106Fariborz Jahanian                                            bool EncodeClassNames,
505617c1a2e748d49d293c9926eeeb85aa4890182106Fariborz Jahanian                                            bool EncodePointerToObjCTypedef) const {
50573624e9e55d98507863d0731a0942cd874f2a1574John McCall  CanQualType CT = getCanonicalType(T);
50583624e9e55d98507863d0731a0942cd874f2a1574John McCall  switch (CT->getTypeClass()) {
50593624e9e55d98507863d0731a0942cd874f2a1574John McCall  case Type::Builtin:
50603624e9e55d98507863d0731a0942cd874f2a1574John McCall  case Type::Enum:
5061ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner    if (FD && FD->isBitField())
506264fd7e86c1a90d9ff78e4a0bd79f69499667a4e3David Chisnall      return EncodeBitField(this, S, T, FD);
50633624e9e55d98507863d0731a0942cd874f2a1574John McCall    if (const BuiltinType *BT = dyn_cast<BuiltinType>(CT))
50643624e9e55d98507863d0731a0942cd874f2a1574John McCall      S += getObjCEncodingForPrimitiveKind(this, BT->getKind());
50653624e9e55d98507863d0731a0942cd874f2a1574John McCall    else
50663624e9e55d98507863d0731a0942cd874f2a1574John McCall      S += ObjCEncodingForEnumType(this, cast<EnumType>(CT));
5067ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner    return;
50681eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
50693624e9e55d98507863d0731a0942cd874f2a1574John McCall  case Type::Complex: {
50703624e9e55d98507863d0731a0942cd874f2a1574John McCall    const ComplexType *CT = T->castAs<ComplexType>();
5071c612f7bc9a6379cd7e7c2dd306d05938e890051bAnders Carlsson    S += 'j';
50721eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    getObjCEncodingForTypeImpl(CT->getElementType(), S, false, false, 0, false,
5073c612f7bc9a6379cd7e7c2dd306d05938e890051bAnders Carlsson                               false);
5074ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner    return;
5075ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner  }
50763624e9e55d98507863d0731a0942cd874f2a1574John McCall
50773624e9e55d98507863d0731a0942cd874f2a1574John McCall  case Type::Atomic: {
50783624e9e55d98507863d0731a0942cd874f2a1574John McCall    const AtomicType *AT = T->castAs<AtomicType>();
50793624e9e55d98507863d0731a0942cd874f2a1574John McCall    S += 'A';
50803624e9e55d98507863d0731a0942cd874f2a1574John McCall    getObjCEncodingForTypeImpl(AT->getValueType(), S, false, false, 0,
50813624e9e55d98507863d0731a0942cd874f2a1574John McCall                               false, false);
50823624e9e55d98507863d0731a0942cd874f2a1574John McCall    return;
5083aa1d76163e4b0b1cc54e222be67379f8c02e8ffaFariborz Jahanian  }
50843624e9e55d98507863d0731a0942cd874f2a1574John McCall
50853624e9e55d98507863d0731a0942cd874f2a1574John McCall  // encoding for pointer or reference types.
50863624e9e55d98507863d0731a0942cd874f2a1574John McCall  case Type::Pointer:
50873624e9e55d98507863d0731a0942cd874f2a1574John McCall  case Type::LValueReference:
50883624e9e55d98507863d0731a0942cd874f2a1574John McCall  case Type::RValueReference: {
50893624e9e55d98507863d0731a0942cd874f2a1574John McCall    QualType PointeeTy;
50903624e9e55d98507863d0731a0942cd874f2a1574John McCall    if (isa<PointerType>(CT)) {
50913624e9e55d98507863d0731a0942cd874f2a1574John McCall      const PointerType *PT = T->castAs<PointerType>();
50923624e9e55d98507863d0731a0942cd874f2a1574John McCall      if (PT->isObjCSelType()) {
50933624e9e55d98507863d0731a0942cd874f2a1574John McCall        S += ':';
50943624e9e55d98507863d0731a0942cd874f2a1574John McCall        return;
50953624e9e55d98507863d0731a0942cd874f2a1574John McCall      }
50963624e9e55d98507863d0731a0942cd874f2a1574John McCall      PointeeTy = PT->getPointeeType();
50973624e9e55d98507863d0731a0942cd874f2a1574John McCall    } else {
50983624e9e55d98507863d0731a0942cd874f2a1574John McCall      PointeeTy = T->castAs<ReferenceType>()->getPointeeType();
50993624e9e55d98507863d0731a0942cd874f2a1574John McCall    }
51003624e9e55d98507863d0731a0942cd874f2a1574John McCall
5101a1c033e9514865f3a7b0d8b3b20e6de926cfec6cFariborz Jahanian    bool isReadOnly = false;
5102a1c033e9514865f3a7b0d8b3b20e6de926cfec6cFariborz Jahanian    // For historical/compatibility reasons, the read-only qualifier of the
5103a1c033e9514865f3a7b0d8b3b20e6de926cfec6cFariborz Jahanian    // pointee gets emitted _before_ the '^'.  The read-only qualifier of
5104a1c033e9514865f3a7b0d8b3b20e6de926cfec6cFariborz Jahanian    // the pointer itself gets ignored, _unless_ we are looking at a typedef!
51051eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    // Also, do not emit the 'r' for anything but the outermost type!
51068e1fab243ab8023b7ee3899745386b3b3a4258f8Mike Stump    if (isa<TypedefType>(T.getTypePtr())) {
5107a1c033e9514865f3a7b0d8b3b20e6de926cfec6cFariborz Jahanian      if (OutermostType && T.isConstQualified()) {
5108a1c033e9514865f3a7b0d8b3b20e6de926cfec6cFariborz Jahanian        isReadOnly = true;
5109a1c033e9514865f3a7b0d8b3b20e6de926cfec6cFariborz Jahanian        S += 'r';
5110a1c033e9514865f3a7b0d8b3b20e6de926cfec6cFariborz Jahanian      }
51119fdbab3cbc2fc04bcaf5768023d83707f3151144Mike Stump    } else if (OutermostType) {
5112a1c033e9514865f3a7b0d8b3b20e6de926cfec6cFariborz Jahanian      QualType P = PointeeTy;
51136217b80b7a1379b74cced1c076338262c3c980b3Ted Kremenek      while (P->getAs<PointerType>())
51146217b80b7a1379b74cced1c076338262c3c980b3Ted Kremenek        P = P->getAs<PointerType>()->getPointeeType();
5115a1c033e9514865f3a7b0d8b3b20e6de926cfec6cFariborz Jahanian      if (P.isConstQualified()) {
5116a1c033e9514865f3a7b0d8b3b20e6de926cfec6cFariborz Jahanian        isReadOnly = true;
5117a1c033e9514865f3a7b0d8b3b20e6de926cfec6cFariborz Jahanian        S += 'r';
5118a1c033e9514865f3a7b0d8b3b20e6de926cfec6cFariborz Jahanian      }
5119a1c033e9514865f3a7b0d8b3b20e6de926cfec6cFariborz Jahanian    }
5120a1c033e9514865f3a7b0d8b3b20e6de926cfec6cFariborz Jahanian    if (isReadOnly) {
5121a1c033e9514865f3a7b0d8b3b20e6de926cfec6cFariborz Jahanian      // Another legacy compatibility encoding. Some ObjC qualifier and type
5122a1c033e9514865f3a7b0d8b3b20e6de926cfec6cFariborz Jahanian      // combinations need to be rearranged.
5123a1c033e9514865f3a7b0d8b3b20e6de926cfec6cFariborz Jahanian      // Rewrite "in const" from "nr" to "rn"
51245f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner      if (StringRef(S).endswith("nr"))
51250237941e0beb0c929934b66ad29443b484d987feBenjamin Kramer        S.replace(S.end()-2, S.end(), "rn");
5126a1c033e9514865f3a7b0d8b3b20e6de926cfec6cFariborz Jahanian    }
51271eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
512885f9bceab1542aafff012d4d28e998f4ba16e362Anders Carlsson    if (PointeeTy->isCharType()) {
512985f9bceab1542aafff012d4d28e998f4ba16e362Anders Carlsson      // char pointer types should be encoded as '*' unless it is a
513085f9bceab1542aafff012d4d28e998f4ba16e362Anders Carlsson      // type that has been typedef'd to 'BOOL'.
5131e8c49533521c40643653f943d47229e62d277f88Anders Carlsson      if (!isTypeTypedefedAsBOOL(PointeeTy)) {
513285f9bceab1542aafff012d4d28e998f4ba16e362Anders Carlsson        S += '*';
513385f9bceab1542aafff012d4d28e998f4ba16e362Anders Carlsson        return;
513485f9bceab1542aafff012d4d28e998f4ba16e362Anders Carlsson      }
51356217b80b7a1379b74cced1c076338262c3c980b3Ted Kremenek    } else if (const RecordType *RTy = PointeeTy->getAs<RecordType>()) {
51369533a7fdb8397421f3be52e879442460a87389f6Steve Naroff      // GCC binary compat: Need to convert "struct objc_class *" to "#".
51379533a7fdb8397421f3be52e879442460a87389f6Steve Naroff      if (RTy->getDecl()->getIdentifier() == &Idents.get("objc_class")) {
51389533a7fdb8397421f3be52e879442460a87389f6Steve Naroff        S += '#';
51399533a7fdb8397421f3be52e879442460a87389f6Steve Naroff        return;
51409533a7fdb8397421f3be52e879442460a87389f6Steve Naroff      }
51419533a7fdb8397421f3be52e879442460a87389f6Steve Naroff      // GCC binary compat: Need to convert "struct objc_object *" to "@".
51429533a7fdb8397421f3be52e879442460a87389f6Steve Naroff      if (RTy->getDecl()->getIdentifier() == &Idents.get("objc_object")) {
51439533a7fdb8397421f3be52e879442460a87389f6Steve Naroff        S += '@';
51449533a7fdb8397421f3be52e879442460a87389f6Steve Naroff        return;
51459533a7fdb8397421f3be52e879442460a87389f6Steve Naroff      }
51469533a7fdb8397421f3be52e879442460a87389f6Steve Naroff      // fall through...
514785f9bceab1542aafff012d4d28e998f4ba16e362Anders Carlsson    }
514885f9bceab1542aafff012d4d28e998f4ba16e362Anders Carlsson    S += '^';
5149a1c033e9514865f3a7b0d8b3b20e6de926cfec6cFariborz Jahanian    getLegacyIntegralTypeEncoding(PointeeTy);
5150a1c033e9514865f3a7b0d8b3b20e6de926cfec6cFariborz Jahanian
51511eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    getObjCEncodingForTypeImpl(PointeeTy, S, false, ExpandPointedToStructures,
515243822eaeddeaa419b90f23c68af6b23c46788a58Fariborz Jahanian                               NULL);
5153ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner    return;
5154ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner  }
51553624e9e55d98507863d0731a0942cd874f2a1574John McCall
51563624e9e55d98507863d0731a0942cd874f2a1574John McCall  case Type::ConstantArray:
51573624e9e55d98507863d0731a0942cd874f2a1574John McCall  case Type::IncompleteArray:
51583624e9e55d98507863d0731a0942cd874f2a1574John McCall  case Type::VariableArray: {
51593624e9e55d98507863d0731a0942cd874f2a1574John McCall    const ArrayType *AT = cast<ArrayType>(CT);
51603624e9e55d98507863d0731a0942cd874f2a1574John McCall
51612636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis    if (isa<IncompleteArrayType>(AT) && !StructField) {
5162559a83330416affb0e341a2c53800cbf924a5178Anders Carlsson      // Incomplete arrays are encoded as a pointer to the array element.
5163559a83330416affb0e341a2c53800cbf924a5178Anders Carlsson      S += '^';
5164559a83330416affb0e341a2c53800cbf924a5178Anders Carlsson
51651eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      getObjCEncodingForTypeImpl(AT->getElementType(), S,
5166559a83330416affb0e341a2c53800cbf924a5178Anders Carlsson                                 false, ExpandStructures, FD);
5167559a83330416affb0e341a2c53800cbf924a5178Anders Carlsson    } else {
5168559a83330416affb0e341a2c53800cbf924a5178Anders Carlsson      S += '[';
51691eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
51702636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis      if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(AT)) {
51712636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis        if (getTypeSize(CAT->getElementType()) == 0)
51722636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis          S += '0';
51732636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis        else
51742636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis          S += llvm::utostr(CAT->getSize().getZExtValue());
51752636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis      } else {
5176559a83330416affb0e341a2c53800cbf924a5178Anders Carlsson        //Variable length arrays are encoded as a regular array with 0 elements.
51772636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis        assert((isa<VariableArrayType>(AT) || isa<IncompleteArrayType>(AT)) &&
51782636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis               "Unknown array type!");
5179559a83330416affb0e341a2c53800cbf924a5178Anders Carlsson        S += '0';
5180559a83330416affb0e341a2c53800cbf924a5178Anders Carlsson      }
51811eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
51821eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      getObjCEncodingForTypeImpl(AT->getElementType(), S,
5183559a83330416affb0e341a2c53800cbf924a5178Anders Carlsson                                 false, ExpandStructures, FD);
5184559a83330416affb0e341a2c53800cbf924a5178Anders Carlsson      S += ']';
5185559a83330416affb0e341a2c53800cbf924a5178Anders Carlsson    }
5186ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner    return;
5187ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner  }
51881eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
51893624e9e55d98507863d0731a0942cd874f2a1574John McCall  case Type::FunctionNoProto:
51903624e9e55d98507863d0731a0942cd874f2a1574John McCall  case Type::FunctionProto:
5191c0a87b7db06643178ad2cbce0767548c139ea387Anders Carlsson    S += '?';
5192ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner    return;
51931eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
51943624e9e55d98507863d0731a0942cd874f2a1574John McCall  case Type::Record: {
51953624e9e55d98507863d0731a0942cd874f2a1574John McCall    RecordDecl *RDecl = cast<RecordType>(CT)->getDecl();
5196d96b35bc6becf8db00d140c11e3d0e53f27567a1Daniel Dunbar    S += RDecl->isUnion() ? '(' : '{';
5197502a4a1ce4c34cf78c8182d9798da0a51d9b7302Daniel Dunbar    // Anonymous structures print as '?'
5198502a4a1ce4c34cf78c8182d9798da0a51d9b7302Daniel Dunbar    if (const IdentifierInfo *II = RDecl->getIdentifier()) {
5199502a4a1ce4c34cf78c8182d9798da0a51d9b7302Daniel Dunbar      S += II->getName();
52006fb94391dc7cb11fd4bbdb969bbab11b6b48c223Fariborz Jahanian      if (ClassTemplateSpecializationDecl *Spec
52016fb94391dc7cb11fd4bbdb969bbab11b6b48c223Fariborz Jahanian          = dyn_cast<ClassTemplateSpecializationDecl>(RDecl)) {
52026fb94391dc7cb11fd4bbdb969bbab11b6b48c223Fariborz Jahanian        const TemplateArgumentList &TemplateArgs = Spec->getTemplateArgs();
52035eada844fa70b6e2bc941dd7306f7a4fb1e8529dBenjamin Kramer        llvm::raw_string_ostream OS(S);
52045eada844fa70b6e2bc941dd7306f7a4fb1e8529dBenjamin Kramer        TemplateSpecializationType::PrintTemplateArgumentList(OS,
5205910f8008fea79120489a53593fe971b0b8a4a740Douglas Gregor                                            TemplateArgs.data(),
5206910f8008fea79120489a53593fe971b0b8a4a740Douglas Gregor                                            TemplateArgs.size(),
520730c42404202d2e2512e51efc6066bd614cfdb5a4Douglas Gregor                                            (*this).getPrintingPolicy());
52086fb94391dc7cb11fd4bbdb969bbab11b6b48c223Fariborz Jahanian      }
5209502a4a1ce4c34cf78c8182d9798da0a51d9b7302Daniel Dunbar    } else {
5210502a4a1ce4c34cf78c8182d9798da0a51d9b7302Daniel Dunbar      S += '?';
5211502a4a1ce4c34cf78c8182d9798da0a51d9b7302Daniel Dunbar    }
52120d504c1da852e58ff802545c823ecff3b6c654b8Daniel Dunbar    if (ExpandStructures) {
52137d6b46d9a9d75dea8ef9f6973dd50633c1f37963Fariborz Jahanian      S += '=';
52142636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis      if (!RDecl->isUnion()) {
52152636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis        getObjCEncodingForStructureImpl(RDecl, S, FD);
52162636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis      } else {
52172636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis        for (RecordDecl::field_iterator Field = RDecl->field_begin(),
52182636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis                                     FieldEnd = RDecl->field_end();
52192636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis             Field != FieldEnd; ++Field) {
52202636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis          if (FD) {
52212636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis            S += '"';
52222636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis            S += Field->getNameAsString();
52232636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis            S += '"';
52242636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis          }
52251eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
52262636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis          // Special case bit-fields.
52272636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis          if (Field->isBitField()) {
52282636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis            getObjCEncodingForTypeImpl(Field->getType(), S, false, true,
5229581deb3da481053c4993c7600f97acf7768caac5David Blaikie                                       *Field);
52302636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis          } else {
52312636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis            QualType qt = Field->getType();
52322636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis            getLegacyIntegralTypeEncoding(qt);
52332636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis            getObjCEncodingForTypeImpl(qt, S, false, true,
52342636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis                                       FD, /*OutermostType*/false,
52352636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis                                       /*EncodingProperty*/false,
52362636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis                                       /*StructField*/true);
52372636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis          }
5238d96b35bc6becf8db00d140c11e3d0e53f27567a1Daniel Dunbar        }
52397d6b46d9a9d75dea8ef9f6973dd50633c1f37963Fariborz Jahanian      }
52406de88a873a4cbe06d72602eef57d68006730a80bFariborz Jahanian    }
5241d96b35bc6becf8db00d140c11e3d0e53f27567a1Daniel Dunbar    S += RDecl->isUnion() ? ')' : '}';
5242ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner    return;
5243ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner  }
52441eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
52453624e9e55d98507863d0731a0942cd874f2a1574John McCall  case Type::BlockPointer: {
52463624e9e55d98507863d0731a0942cd874f2a1574John McCall    const BlockPointerType *BT = T->castAs<BlockPointerType>();
524721a98b188857d690aa4510c52ac4317ffa0908a8Steve Naroff    S += "@?"; // Unlike a pointer-to-function, which is "^?".
5248dc8dab6fabf4bfd4f4b94bf572ac3342a5bbfcd7Bob Wilson    if (EncodeBlockParameters) {
52493624e9e55d98507863d0731a0942cd874f2a1574John McCall      const FunctionType *FT = BT->getPointeeType()->castAs<FunctionType>();
5250dc8dab6fabf4bfd4f4b94bf572ac3342a5bbfcd7Bob Wilson
5251dc8dab6fabf4bfd4f4b94bf572ac3342a5bbfcd7Bob Wilson      S += '<';
5252dc8dab6fabf4bfd4f4b94bf572ac3342a5bbfcd7Bob Wilson      // Block return type
5253dc8dab6fabf4bfd4f4b94bf572ac3342a5bbfcd7Bob Wilson      getObjCEncodingForTypeImpl(FT->getResultType(), S,
5254dc8dab6fabf4bfd4f4b94bf572ac3342a5bbfcd7Bob Wilson                                 ExpandPointedToStructures, ExpandStructures,
5255dc8dab6fabf4bfd4f4b94bf572ac3342a5bbfcd7Bob Wilson                                 FD,
5256dc8dab6fabf4bfd4f4b94bf572ac3342a5bbfcd7Bob Wilson                                 false /* OutermostType */,
5257dc8dab6fabf4bfd4f4b94bf572ac3342a5bbfcd7Bob Wilson                                 EncodingProperty,
5258dc8dab6fabf4bfd4f4b94bf572ac3342a5bbfcd7Bob Wilson                                 false /* StructField */,
5259dc8dab6fabf4bfd4f4b94bf572ac3342a5bbfcd7Bob Wilson                                 EncodeBlockParameters,
5260dc8dab6fabf4bfd4f4b94bf572ac3342a5bbfcd7Bob Wilson                                 EncodeClassNames);
5261dc8dab6fabf4bfd4f4b94bf572ac3342a5bbfcd7Bob Wilson      // Block self
5262dc8dab6fabf4bfd4f4b94bf572ac3342a5bbfcd7Bob Wilson      S += "@?";
5263dc8dab6fabf4bfd4f4b94bf572ac3342a5bbfcd7Bob Wilson      // Block parameters
5264dc8dab6fabf4bfd4f4b94bf572ac3342a5bbfcd7Bob Wilson      if (const FunctionProtoType *FPT = dyn_cast<FunctionProtoType>(FT)) {
5265dc8dab6fabf4bfd4f4b94bf572ac3342a5bbfcd7Bob Wilson        for (FunctionProtoType::arg_type_iterator I = FPT->arg_type_begin(),
5266dc8dab6fabf4bfd4f4b94bf572ac3342a5bbfcd7Bob Wilson               E = FPT->arg_type_end(); I && (I != E); ++I) {
5267dc8dab6fabf4bfd4f4b94bf572ac3342a5bbfcd7Bob Wilson          getObjCEncodingForTypeImpl(*I, S,
5268dc8dab6fabf4bfd4f4b94bf572ac3342a5bbfcd7Bob Wilson                                     ExpandPointedToStructures,
5269dc8dab6fabf4bfd4f4b94bf572ac3342a5bbfcd7Bob Wilson                                     ExpandStructures,
5270dc8dab6fabf4bfd4f4b94bf572ac3342a5bbfcd7Bob Wilson                                     FD,
5271dc8dab6fabf4bfd4f4b94bf572ac3342a5bbfcd7Bob Wilson                                     false /* OutermostType */,
5272dc8dab6fabf4bfd4f4b94bf572ac3342a5bbfcd7Bob Wilson                                     EncodingProperty,
5273dc8dab6fabf4bfd4f4b94bf572ac3342a5bbfcd7Bob Wilson                                     false /* StructField */,
5274dc8dab6fabf4bfd4f4b94bf572ac3342a5bbfcd7Bob Wilson                                     EncodeBlockParameters,
5275dc8dab6fabf4bfd4f4b94bf572ac3342a5bbfcd7Bob Wilson                                     EncodeClassNames);
5276dc8dab6fabf4bfd4f4b94bf572ac3342a5bbfcd7Bob Wilson        }
5277dc8dab6fabf4bfd4f4b94bf572ac3342a5bbfcd7Bob Wilson      }
5278dc8dab6fabf4bfd4f4b94bf572ac3342a5bbfcd7Bob Wilson      S += '>';
5279dc8dab6fabf4bfd4f4b94bf572ac3342a5bbfcd7Bob Wilson    }
5280ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner    return;
5281ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner  }
52821eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
52833624e9e55d98507863d0731a0942cd874f2a1574John McCall  case Type::ObjCObject:
52843624e9e55d98507863d0731a0942cd874f2a1574John McCall  case Type::ObjCInterface: {
52853624e9e55d98507863d0731a0942cd874f2a1574John McCall    // Ignore protocol qualifiers when mangling at this level.
52863624e9e55d98507863d0731a0942cd874f2a1574John McCall    T = T->castAs<ObjCObjectType>()->getBaseType();
5287c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall
52883624e9e55d98507863d0731a0942cd874f2a1574John McCall    // The assumption seems to be that this assert will succeed
52893624e9e55d98507863d0731a0942cd874f2a1574John McCall    // because nested levels will have filtered out 'id' and 'Class'.
52903624e9e55d98507863d0731a0942cd874f2a1574John McCall    const ObjCInterfaceType *OIT = T->castAs<ObjCInterfaceType>();
529143822eaeddeaa419b90f23c68af6b23c46788a58Fariborz Jahanian    // @encode(class_name)
52920953e767ff7817f97b3ab20896b229891eeff45bJohn McCall    ObjCInterfaceDecl *OI = OIT->getDecl();
529343822eaeddeaa419b90f23c68af6b23c46788a58Fariborz Jahanian    S += '{';
529443822eaeddeaa419b90f23c68af6b23c46788a58Fariborz Jahanian    const IdentifierInfo *II = OI->getIdentifier();
529543822eaeddeaa419b90f23c68af6b23c46788a58Fariborz Jahanian    S += II->getName();
529643822eaeddeaa419b90f23c68af6b23c46788a58Fariborz Jahanian    S += '=';
5297db8264e4c5ffd7af6fbad4ca4306bd382bb02691Jordy Rose    SmallVector<const ObjCIvarDecl*, 32> Ivars;
52982c18bb7c9fca66c30b6eabbdcbc6399d24a54fa9Fariborz Jahanian    DeepCollectObjCIvars(OI, true, Ivars);
52992c18bb7c9fca66c30b6eabbdcbc6399d24a54fa9Fariborz Jahanian    for (unsigned i = 0, e = Ivars.size(); i != e; ++i) {
5300db8264e4c5ffd7af6fbad4ca4306bd382bb02691Jordy Rose      const FieldDecl *Field = cast<FieldDecl>(Ivars[i]);
53012c18bb7c9fca66c30b6eabbdcbc6399d24a54fa9Fariborz Jahanian      if (Field->isBitField())
53022c18bb7c9fca66c30b6eabbdcbc6399d24a54fa9Fariborz Jahanian        getObjCEncodingForTypeImpl(Field->getType(), S, false, true, Field);
530343822eaeddeaa419b90f23c68af6b23c46788a58Fariborz Jahanian      else
530417c1a2e748d49d293c9926eeeb85aa4890182106Fariborz Jahanian        getObjCEncodingForTypeImpl(Field->getType(), S, false, true, FD,
530517c1a2e748d49d293c9926eeeb85aa4890182106Fariborz Jahanian                                   false, false, false, false, false,
530617c1a2e748d49d293c9926eeeb85aa4890182106Fariborz Jahanian                                   EncodePointerToObjCTypedef);
530743822eaeddeaa419b90f23c68af6b23c46788a58Fariborz Jahanian    }
530843822eaeddeaa419b90f23c68af6b23c46788a58Fariborz Jahanian    S += '}';
5309ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner    return;
531043822eaeddeaa419b90f23c68af6b23c46788a58Fariborz Jahanian  }
53111eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
53123624e9e55d98507863d0731a0942cd874f2a1574John McCall  case Type::ObjCObjectPointer: {
53133624e9e55d98507863d0731a0942cd874f2a1574John McCall    const ObjCObjectPointerType *OPT = T->castAs<ObjCObjectPointerType>();
531414108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff    if (OPT->isObjCIdType()) {
531514108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff      S += '@';
531614108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff      return;
5317ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner    }
53181eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
531927d20a24a1b816cecbd142727d2c81af5a6a111bSteve Naroff    if (OPT->isObjCClassType() || OPT->isObjCQualifiedClassType()) {
532027d20a24a1b816cecbd142727d2c81af5a6a111bSteve Naroff      // FIXME: Consider if we need to output qualifiers for 'Class<p>'.
532127d20a24a1b816cecbd142727d2c81af5a6a111bSteve Naroff      // Since this is a binary compatibility issue, need to consult with runtime
532227d20a24a1b816cecbd142727d2c81af5a6a111bSteve Naroff      // folks. Fortunately, this is a *very* obsure construct.
532314108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff      S += '#';
532414108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff      return;
5325ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner    }
53261eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
5327ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner    if (OPT->isObjCQualifiedIdType()) {
53281eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      getObjCEncodingForTypeImpl(getObjCIdType(), S,
532914108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff                                 ExpandPointedToStructures,
533014108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff                                 ExpandStructures, FD);
5331dc8dab6fabf4bfd4f4b94bf572ac3342a5bbfcd7Bob Wilson      if (FD || EncodingProperty || EncodeClassNames) {
533214108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff        // Note that we do extended encoding of protocol qualifer list
533314108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff        // Only when doing ivar or property encoding.
533414108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff        S += '"';
533567ef8eaea8a0a2073147a8d863f0e3f30d525802Steve Naroff        for (ObjCObjectPointerType::qual_iterator I = OPT->qual_begin(),
533667ef8eaea8a0a2073147a8d863f0e3f30d525802Steve Naroff             E = OPT->qual_end(); I != E; ++I) {
533714108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff          S += '<';
533814108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff          S += (*I)->getNameAsString();
533914108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff          S += '>';
534014108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff        }
534114108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff        S += '"';
534214108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff      }
534314108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff      return;
5344ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner    }
53451eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
5346ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner    QualType PointeeTy = OPT->getPointeeType();
5347ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner    if (!EncodingProperty &&
534817c1a2e748d49d293c9926eeeb85aa4890182106Fariborz Jahanian        isa<TypedefType>(PointeeTy.getTypePtr()) &&
534917c1a2e748d49d293c9926eeeb85aa4890182106Fariborz Jahanian        !EncodePointerToObjCTypedef) {
5350ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner      // Another historical/compatibility reason.
53511eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      // We encode the underlying type which comes out as
5352ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner      // {...};
5353ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner      S += '^';
53541eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      getObjCEncodingForTypeImpl(PointeeTy, S,
53551eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump                                 false, ExpandPointedToStructures,
535617c1a2e748d49d293c9926eeeb85aa4890182106Fariborz Jahanian                                 NULL,
535717c1a2e748d49d293c9926eeeb85aa4890182106Fariborz Jahanian                                 false, false, false, false, false,
535817c1a2e748d49d293c9926eeeb85aa4890182106Fariborz Jahanian                                 /*EncodePointerToObjCTypedef*/true);
535914108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff      return;
536014108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff    }
5361ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner
5362ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner    S += '@';
5363dc8dab6fabf4bfd4f4b94bf572ac3342a5bbfcd7Bob Wilson    if (OPT->getInterfaceDecl() &&
5364dc8dab6fabf4bfd4f4b94bf572ac3342a5bbfcd7Bob Wilson        (FD || EncodingProperty || EncodeClassNames)) {
5365ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner      S += '"';
536627d20a24a1b816cecbd142727d2c81af5a6a111bSteve Naroff      S += OPT->getInterfaceDecl()->getIdentifier()->getName();
536767ef8eaea8a0a2073147a8d863f0e3f30d525802Steve Naroff      for (ObjCObjectPointerType::qual_iterator I = OPT->qual_begin(),
536867ef8eaea8a0a2073147a8d863f0e3f30d525802Steve Naroff           E = OPT->qual_end(); I != E; ++I) {
5369ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner        S += '<';
5370ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner        S += (*I)->getNameAsString();
5371ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner        S += '>';
53721eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      }
5373ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner      S += '"';
5374ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner    }
5375ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner    return;
5376ce7b38c4f1ea9c51e2f46a82e3f57456b74269d5Chris Lattner  }
53771eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
5378532ec7baf2d0791abc9551ef856a537711c5774aJohn McCall  // gcc just blithely ignores member pointers.
53793624e9e55d98507863d0731a0942cd874f2a1574John McCall  // FIXME: we shoul do better than that.  'M' is available.
53803624e9e55d98507863d0731a0942cd874f2a1574John McCall  case Type::MemberPointer:
5381532ec7baf2d0791abc9551ef856a537711c5774aJohn McCall    return;
5382e6012c7ecb9d848f4091c8c48e7d9946cc36b23fFariborz Jahanian
53833624e9e55d98507863d0731a0942cd874f2a1574John McCall  case Type::Vector:
53843624e9e55d98507863d0731a0942cd874f2a1574John McCall  case Type::ExtVector:
5385e6012c7ecb9d848f4091c8c48e7d9946cc36b23fFariborz Jahanian    // This matches gcc's encoding, even though technically it is
5386e6012c7ecb9d848f4091c8c48e7d9946cc36b23fFariborz Jahanian    // insufficient.
5387e6012c7ecb9d848f4091c8c48e7d9946cc36b23fFariborz Jahanian    // FIXME. We should do a better job than gcc.
5388e6012c7ecb9d848f4091c8c48e7d9946cc36b23fFariborz Jahanian    return;
53893624e9e55d98507863d0731a0942cd874f2a1574John McCall
53903624e9e55d98507863d0731a0942cd874f2a1574John McCall#define ABSTRACT_TYPE(KIND, BASE)
53913624e9e55d98507863d0731a0942cd874f2a1574John McCall#define TYPE(KIND, BASE)
53923624e9e55d98507863d0731a0942cd874f2a1574John McCall#define DEPENDENT_TYPE(KIND, BASE) \
53933624e9e55d98507863d0731a0942cd874f2a1574John McCall  case Type::KIND:
53943624e9e55d98507863d0731a0942cd874f2a1574John McCall#define NON_CANONICAL_TYPE(KIND, BASE) \
53953624e9e55d98507863d0731a0942cd874f2a1574John McCall  case Type::KIND:
53963624e9e55d98507863d0731a0942cd874f2a1574John McCall#define NON_CANONICAL_UNLESS_DEPENDENT_TYPE(KIND, BASE) \
53973624e9e55d98507863d0731a0942cd874f2a1574John McCall  case Type::KIND:
53983624e9e55d98507863d0731a0942cd874f2a1574John McCall#include "clang/AST/TypeNodes.def"
53993624e9e55d98507863d0731a0942cd874f2a1574John McCall    llvm_unreachable("@encode for dependent type!");
5400e6012c7ecb9d848f4091c8c48e7d9946cc36b23fFariborz Jahanian  }
54013624e9e55d98507863d0731a0942cd874f2a1574John McCall  llvm_unreachable("bad type kind!");
540285f9bceab1542aafff012d4d28e998f4ba16e362Anders Carlsson}
540385f9bceab1542aafff012d4d28e998f4ba16e362Anders Carlsson
54042636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidisvoid ASTContext::getObjCEncodingForStructureImpl(RecordDecl *RDecl,
54052636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis                                                 std::string &S,
54062636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis                                                 const FieldDecl *FD,
54072636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis                                                 bool includeVBases) const {
54082636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis  assert(RDecl && "Expected non-null RecordDecl");
54092636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis  assert(!RDecl->isUnion() && "Should not be called for unions");
54102636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis  if (!RDecl->getDefinition())
54112636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis    return;
54122636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis
54132636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis  CXXRecordDecl *CXXRec = dyn_cast<CXXRecordDecl>(RDecl);
54142636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis  std::multimap<uint64_t, NamedDecl *> FieldOrBaseOffsets;
54152636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis  const ASTRecordLayout &layout = getASTRecordLayout(RDecl);
54162636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis
54172636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis  if (CXXRec) {
54182636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis    for (CXXRecordDecl::base_class_iterator
54192636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis           BI = CXXRec->bases_begin(),
54202636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis           BE = CXXRec->bases_end(); BI != BE; ++BI) {
54212636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis      if (!BI->isVirtual()) {
54222636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis        CXXRecordDecl *base = BI->getType()->getAsCXXRecordDecl();
5423829f20097e030229fd4c234b84948f53312eaf55Argyrios Kyrtzidis        if (base->isEmpty())
5424829f20097e030229fd4c234b84948f53312eaf55Argyrios Kyrtzidis          continue;
5425d4f5198ae07d9a4958d8191bac694ded12173ad9Benjamin Kramer        uint64_t offs = toBits(layout.getBaseClassOffset(base));
54262636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis        FieldOrBaseOffsets.insert(FieldOrBaseOffsets.upper_bound(offs),
54272636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis                                  std::make_pair(offs, base));
54282636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis      }
54292636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis    }
54302636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis  }
54312636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis
54322636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis  unsigned i = 0;
54332636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis  for (RecordDecl::field_iterator Field = RDecl->field_begin(),
54342636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis                               FieldEnd = RDecl->field_end();
54352636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis       Field != FieldEnd; ++Field, ++i) {
54362636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis    uint64_t offs = layout.getFieldOffset(i);
54372636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis    FieldOrBaseOffsets.insert(FieldOrBaseOffsets.upper_bound(offs),
5438581deb3da481053c4993c7600f97acf7768caac5David Blaikie                              std::make_pair(offs, *Field));
54392636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis  }
54402636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis
54412636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis  if (CXXRec && includeVBases) {
54422636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis    for (CXXRecordDecl::base_class_iterator
54432636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis           BI = CXXRec->vbases_begin(),
54442636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis           BE = CXXRec->vbases_end(); BI != BE; ++BI) {
54452636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis      CXXRecordDecl *base = BI->getType()->getAsCXXRecordDecl();
5446829f20097e030229fd4c234b84948f53312eaf55Argyrios Kyrtzidis      if (base->isEmpty())
5447829f20097e030229fd4c234b84948f53312eaf55Argyrios Kyrtzidis        continue;
5448d4f5198ae07d9a4958d8191bac694ded12173ad9Benjamin Kramer      uint64_t offs = toBits(layout.getVBaseClassOffset(base));
544919aa8609a5d04eed9b81aa1c4608e8a4f3df6ccdArgyrios Kyrtzidis      if (FieldOrBaseOffsets.find(offs) == FieldOrBaseOffsets.end())
545019aa8609a5d04eed9b81aa1c4608e8a4f3df6ccdArgyrios Kyrtzidis        FieldOrBaseOffsets.insert(FieldOrBaseOffsets.end(),
545119aa8609a5d04eed9b81aa1c4608e8a4f3df6ccdArgyrios Kyrtzidis                                  std::make_pair(offs, base));
54522636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis    }
54532636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis  }
54542636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis
54552636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis  CharUnits size;
54562636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis  if (CXXRec) {
54572636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis    size = includeVBases ? layout.getSize() : layout.getNonVirtualSize();
54582636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis  } else {
54592636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis    size = layout.getSize();
54602636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis  }
54612636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis
54622636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis  uint64_t CurOffs = 0;
54632636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis  std::multimap<uint64_t, NamedDecl *>::iterator
54642636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis    CurLayObj = FieldOrBaseOffsets.begin();
54652636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis
546658db7a575efc9a2f35266fe240feac3cf317753dDouglas Gregor  if (CXXRec && CXXRec->isDynamicClass() &&
546758db7a575efc9a2f35266fe240feac3cf317753dDouglas Gregor      (CurLayObj == FieldOrBaseOffsets.end() || CurLayObj->first != 0)) {
54682636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis    if (FD) {
54692636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis      S += "\"_vptr$";
54702636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis      std::string recname = CXXRec->getNameAsString();
54712636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis      if (recname.empty()) recname = "?";
54722636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis      S += recname;
54732636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis      S += '"';
54742636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis    }
54752636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis    S += "^^?";
54762636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis    CurOffs += getTypeSize(VoidPtrTy);
54772636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis  }
54782636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis
54792636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis  if (!RDecl->hasFlexibleArrayMember()) {
54802636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis    // Mark the end of the structure.
54812636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis    uint64_t offs = toBits(size);
54822636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis    FieldOrBaseOffsets.insert(FieldOrBaseOffsets.upper_bound(offs),
54832636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis                              std::make_pair(offs, (NamedDecl*)0));
54842636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis  }
54852636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis
54862636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis  for (; CurLayObj != FieldOrBaseOffsets.end(); ++CurLayObj) {
54872636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis    assert(CurOffs <= CurLayObj->first);
54882636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis
54892636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis    if (CurOffs < CurLayObj->first) {
54902636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis      uint64_t padding = CurLayObj->first - CurOffs;
54912636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis      // FIXME: There doesn't seem to be a way to indicate in the encoding that
54922636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis      // packing/alignment of members is different that normal, in which case
54932636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis      // the encoding will be out-of-sync with the real layout.
54942636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis      // If the runtime switches to just consider the size of types without
54952636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis      // taking into account alignment, we could make padding explicit in the
54962636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis      // encoding (e.g. using arrays of chars). The encoding strings would be
54972636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis      // longer then though.
54982636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis      CurOffs += padding;
54992636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis    }
55002636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis
55012636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis    NamedDecl *dcl = CurLayObj->second;
55022636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis    if (dcl == 0)
55032636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis      break; // reached end of structure.
55042636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis
55052636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis    if (CXXRecordDecl *base = dyn_cast<CXXRecordDecl>(dcl)) {
55062636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis      // We expand the bases without their virtual bases since those are going
55072636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis      // in the initial structure. Note that this differs from gcc which
55082636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis      // expands virtual bases each time one is encountered in the hierarchy,
55092636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis      // making the encoding type bigger than it really is.
55102636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis      getObjCEncodingForStructureImpl(base, S, FD, /*includeVBases*/false);
5511829f20097e030229fd4c234b84948f53312eaf55Argyrios Kyrtzidis      assert(!base->isEmpty());
5512829f20097e030229fd4c234b84948f53312eaf55Argyrios Kyrtzidis      CurOffs += toBits(getASTRecordLayout(base).getNonVirtualSize());
55132636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis    } else {
55142636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis      FieldDecl *field = cast<FieldDecl>(dcl);
55152636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis      if (FD) {
55162636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis        S += '"';
55172636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis        S += field->getNameAsString();
55182636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis        S += '"';
55192636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis      }
55202636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis
55212636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis      if (field->isBitField()) {
55222636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis        EncodeBitField(this, S, field->getType(), field);
5523a6b8b2c09610b8bc4330e948ece8b940c2386406Richard Smith        CurOffs += field->getBitWidthValue(*this);
55242636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis      } else {
55252636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis        QualType qt = field->getType();
55262636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis        getLegacyIntegralTypeEncoding(qt);
55272636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis        getObjCEncodingForTypeImpl(qt, S, false, true, FD,
55282636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis                                   /*OutermostType*/false,
55292636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis                                   /*EncodingProperty*/false,
55302636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis                                   /*StructField*/true);
55312636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis        CurOffs += getTypeSize(field->getType());
55322636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis      }
55332636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis    }
55342636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis  }
55352636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis}
55362636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis
55371eb4433ac451dc16f4133a88af2d002ac26c58efMike Stumpvoid ASTContext::getObjCEncodingForTypeQualifier(Decl::ObjCDeclQualifier QT,
5538ecb01e666665efabd2aa76a76f6080e2a78965faFariborz Jahanian                                                 std::string& S) const {
5539ecb01e666665efabd2aa76a76f6080e2a78965faFariborz Jahanian  if (QT & Decl::OBJC_TQ_In)
5540ecb01e666665efabd2aa76a76f6080e2a78965faFariborz Jahanian    S += 'n';
5541ecb01e666665efabd2aa76a76f6080e2a78965faFariborz Jahanian  if (QT & Decl::OBJC_TQ_Inout)
5542ecb01e666665efabd2aa76a76f6080e2a78965faFariborz Jahanian    S += 'N';
5543ecb01e666665efabd2aa76a76f6080e2a78965faFariborz Jahanian  if (QT & Decl::OBJC_TQ_Out)
5544ecb01e666665efabd2aa76a76f6080e2a78965faFariborz Jahanian    S += 'o';
5545ecb01e666665efabd2aa76a76f6080e2a78965faFariborz Jahanian  if (QT & Decl::OBJC_TQ_Bycopy)
5546ecb01e666665efabd2aa76a76f6080e2a78965faFariborz Jahanian    S += 'O';
5547ecb01e666665efabd2aa76a76f6080e2a78965faFariborz Jahanian  if (QT & Decl::OBJC_TQ_Byref)
5548ecb01e666665efabd2aa76a76f6080e2a78965faFariborz Jahanian    S += 'R';
5549ecb01e666665efabd2aa76a76f6080e2a78965faFariborz Jahanian  if (QT & Decl::OBJC_TQ_Oneway)
5550ecb01e666665efabd2aa76a76f6080e2a78965faFariborz Jahanian    S += 'V';
5551ecb01e666665efabd2aa76a76f6080e2a78965faFariborz Jahanian}
5552ecb01e666665efabd2aa76a76f6080e2a78965faFariborz Jahanian
55534dfd02a17c6d604c72e6936527c5e1c56d3ecb7aDouglas GregorTypedefDecl *ASTContext::getObjCIdDecl() const {
55544dfd02a17c6d604c72e6936527c5e1c56d3ecb7aDouglas Gregor  if (!ObjCIdDecl) {
55554dfd02a17c6d604c72e6936527c5e1c56d3ecb7aDouglas Gregor    QualType T = getObjCObjectType(ObjCBuiltinIdTy, 0, 0);
55564dfd02a17c6d604c72e6936527c5e1c56d3ecb7aDouglas Gregor    T = getObjCObjectPointerType(T);
55574dfd02a17c6d604c72e6936527c5e1c56d3ecb7aDouglas Gregor    TypeSourceInfo *IdInfo = getTrivialTypeSourceInfo(T);
55584dfd02a17c6d604c72e6936527c5e1c56d3ecb7aDouglas Gregor    ObjCIdDecl = TypedefDecl::Create(const_cast<ASTContext &>(*this),
55594dfd02a17c6d604c72e6936527c5e1c56d3ecb7aDouglas Gregor                                     getTranslationUnitDecl(),
55604dfd02a17c6d604c72e6936527c5e1c56d3ecb7aDouglas Gregor                                     SourceLocation(), SourceLocation(),
55614dfd02a17c6d604c72e6936527c5e1c56d3ecb7aDouglas Gregor                                     &Idents.get("id"), IdInfo);
55624dfd02a17c6d604c72e6936527c5e1c56d3ecb7aDouglas Gregor  }
55634dfd02a17c6d604c72e6936527c5e1c56d3ecb7aDouglas Gregor
55644dfd02a17c6d604c72e6936527c5e1c56d3ecb7aDouglas Gregor  return ObjCIdDecl;
55657e219e47de26346885d667131977bd9ca2d7662aSteve Naroff}
55667e219e47de26346885d667131977bd9ca2d7662aSteve Naroff
55677a27ea52b7bd635c89bec5a9c521a3bf7d204238Douglas GregorTypedefDecl *ASTContext::getObjCSelDecl() const {
55687a27ea52b7bd635c89bec5a9c521a3bf7d204238Douglas Gregor  if (!ObjCSelDecl) {
55697a27ea52b7bd635c89bec5a9c521a3bf7d204238Douglas Gregor    QualType SelT = getPointerType(ObjCBuiltinSelTy);
55707a27ea52b7bd635c89bec5a9c521a3bf7d204238Douglas Gregor    TypeSourceInfo *SelInfo = getTrivialTypeSourceInfo(SelT);
55717a27ea52b7bd635c89bec5a9c521a3bf7d204238Douglas Gregor    ObjCSelDecl = TypedefDecl::Create(const_cast<ASTContext &>(*this),
55727a27ea52b7bd635c89bec5a9c521a3bf7d204238Douglas Gregor                                      getTranslationUnitDecl(),
55737a27ea52b7bd635c89bec5a9c521a3bf7d204238Douglas Gregor                                      SourceLocation(), SourceLocation(),
55747a27ea52b7bd635c89bec5a9c521a3bf7d204238Douglas Gregor                                      &Idents.get("SEL"), SelInfo);
55757a27ea52b7bd635c89bec5a9c521a3bf7d204238Douglas Gregor  }
55767a27ea52b7bd635c89bec5a9c521a3bf7d204238Douglas Gregor  return ObjCSelDecl;
5577b62f6813406a03bf8a371c4e46c9fad51d102121Fariborz Jahanian}
5578b62f6813406a03bf8a371c4e46c9fad51d102121Fariborz Jahanian
557979d6726921897811232554ed94c5d77b5b7b3fc0Douglas GregorTypedefDecl *ASTContext::getObjCClassDecl() const {
558079d6726921897811232554ed94c5d77b5b7b3fc0Douglas Gregor  if (!ObjCClassDecl) {
558179d6726921897811232554ed94c5d77b5b7b3fc0Douglas Gregor    QualType T = getObjCObjectType(ObjCBuiltinClassTy, 0, 0);
558279d6726921897811232554ed94c5d77b5b7b3fc0Douglas Gregor    T = getObjCObjectPointerType(T);
558379d6726921897811232554ed94c5d77b5b7b3fc0Douglas Gregor    TypeSourceInfo *ClassInfo = getTrivialTypeSourceInfo(T);
558479d6726921897811232554ed94c5d77b5b7b3fc0Douglas Gregor    ObjCClassDecl = TypedefDecl::Create(const_cast<ASTContext &>(*this),
558579d6726921897811232554ed94c5d77b5b7b3fc0Douglas Gregor                                        getTranslationUnitDecl(),
558679d6726921897811232554ed94c5d77b5b7b3fc0Douglas Gregor                                        SourceLocation(), SourceLocation(),
558779d6726921897811232554ed94c5d77b5b7b3fc0Douglas Gregor                                        &Idents.get("Class"), ClassInfo);
558879d6726921897811232554ed94c5d77b5b7b3fc0Douglas Gregor  }
558979d6726921897811232554ed94c5d77b5b7b3fc0Douglas Gregor
559079d6726921897811232554ed94c5d77b5b7b3fc0Douglas Gregor  return ObjCClassDecl;
5591a6ea10e22b600d92e084f6b11b9b9a92d0eb2412Douglas Gregor}
5592a6ea10e22b600d92e084f6b11b9b9a92d0eb2412Douglas Gregor
5593a6ea10e22b600d92e084f6b11b9b9a92d0eb2412Douglas GregorObjCInterfaceDecl *ASTContext::getObjCProtocolDecl() const {
5594a6ea10e22b600d92e084f6b11b9b9a92d0eb2412Douglas Gregor  if (!ObjCProtocolClassDecl) {
5595a6ea10e22b600d92e084f6b11b9b9a92d0eb2412Douglas Gregor    ObjCProtocolClassDecl
5596a6ea10e22b600d92e084f6b11b9b9a92d0eb2412Douglas Gregor      = ObjCInterfaceDecl::Create(*this, getTranslationUnitDecl(),
5597a6ea10e22b600d92e084f6b11b9b9a92d0eb2412Douglas Gregor                                  SourceLocation(),
5598a6ea10e22b600d92e084f6b11b9b9a92d0eb2412Douglas Gregor                                  &Idents.get("Protocol"),
5599a6ea10e22b600d92e084f6b11b9b9a92d0eb2412Douglas Gregor                                  /*PrevDecl=*/0,
5600a6ea10e22b600d92e084f6b11b9b9a92d0eb2412Douglas Gregor                                  SourceLocation(), true);
5601a6ea10e22b600d92e084f6b11b9b9a92d0eb2412Douglas Gregor  }
5602a6ea10e22b600d92e084f6b11b9b9a92d0eb2412Douglas Gregor
5603a6ea10e22b600d92e084f6b11b9b9a92d0eb2412Douglas Gregor  return ObjCProtocolClassDecl;
56048baaca50f07d0c10bba69c8d88c1b9078c92d06dAnders Carlsson}
56058baaca50f07d0c10bba69c8d88c1b9078c92d06dAnders Carlsson
5606c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge//===----------------------------------------------------------------------===//
5607c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge// __builtin_va_list Construction Functions
5608c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge//===----------------------------------------------------------------------===//
5609c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge
5610c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Ingestatic TypedefDecl *CreateCharPtrBuiltinVaListDecl(const ASTContext *Context) {
5611c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge  // typedef char* __builtin_va_list;
5612c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge  QualType CharPtrType = Context->getPointerType(Context->CharTy);
5613c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge  TypeSourceInfo *TInfo
5614c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge    = Context->getTrivialTypeSourceInfo(CharPtrType);
5615c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge
5616c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge  TypedefDecl *VaListTypeDecl
5617c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge    = TypedefDecl::Create(const_cast<ASTContext &>(*Context),
5618c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge                          Context->getTranslationUnitDecl(),
5619c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge                          SourceLocation(), SourceLocation(),
5620c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge                          &Context->Idents.get("__builtin_va_list"),
5621c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge                          TInfo);
5622c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge  return VaListTypeDecl;
5623c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge}
5624c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge
5625c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Ingestatic TypedefDecl *CreateVoidPtrBuiltinVaListDecl(const ASTContext *Context) {
5626c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge  // typedef void* __builtin_va_list;
5627c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge  QualType VoidPtrType = Context->getPointerType(Context->VoidTy);
5628c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge  TypeSourceInfo *TInfo
5629c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge    = Context->getTrivialTypeSourceInfo(VoidPtrType);
5630c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge
5631c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge  TypedefDecl *VaListTypeDecl
5632c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge    = TypedefDecl::Create(const_cast<ASTContext &>(*Context),
5633c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge                          Context->getTranslationUnitDecl(),
5634c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge                          SourceLocation(), SourceLocation(),
5635c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge                          &Context->Idents.get("__builtin_va_list"),
5636c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge                          TInfo);
5637c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge  return VaListTypeDecl;
5638c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge}
5639c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge
5640c264e16a42b3f6c36521857a29ea0949d9781c22Tim Northoverstatic TypedefDecl *
5641c264e16a42b3f6c36521857a29ea0949d9781c22Tim NorthoverCreateAArch64ABIBuiltinVaListDecl(const ASTContext *Context) {
5642c264e16a42b3f6c36521857a29ea0949d9781c22Tim Northover  RecordDecl *VaListTagDecl;
5643c264e16a42b3f6c36521857a29ea0949d9781c22Tim Northover  if (Context->getLangOpts().CPlusPlus) {
5644c264e16a42b3f6c36521857a29ea0949d9781c22Tim Northover    // namespace std { struct __va_list {
5645c264e16a42b3f6c36521857a29ea0949d9781c22Tim Northover    NamespaceDecl *NS;
5646c264e16a42b3f6c36521857a29ea0949d9781c22Tim Northover    NS = NamespaceDecl::Create(const_cast<ASTContext &>(*Context),
5647c264e16a42b3f6c36521857a29ea0949d9781c22Tim Northover                               Context->getTranslationUnitDecl(),
5648c264e16a42b3f6c36521857a29ea0949d9781c22Tim Northover                               /*Inline*/false, SourceLocation(),
5649c264e16a42b3f6c36521857a29ea0949d9781c22Tim Northover                               SourceLocation(), &Context->Idents.get("std"),
5650c264e16a42b3f6c36521857a29ea0949d9781c22Tim Northover                               /*PrevDecl*/0);
5651c264e16a42b3f6c36521857a29ea0949d9781c22Tim Northover
5652c264e16a42b3f6c36521857a29ea0949d9781c22Tim Northover    VaListTagDecl = CXXRecordDecl::Create(*Context, TTK_Struct,
5653c264e16a42b3f6c36521857a29ea0949d9781c22Tim Northover                                          Context->getTranslationUnitDecl(),
5654c264e16a42b3f6c36521857a29ea0949d9781c22Tim Northover                                          SourceLocation(), SourceLocation(),
5655c264e16a42b3f6c36521857a29ea0949d9781c22Tim Northover                                          &Context->Idents.get("__va_list"));
5656c264e16a42b3f6c36521857a29ea0949d9781c22Tim Northover    VaListTagDecl->setDeclContext(NS);
5657c264e16a42b3f6c36521857a29ea0949d9781c22Tim Northover  } else {
5658c264e16a42b3f6c36521857a29ea0949d9781c22Tim Northover    // struct __va_list
5659c264e16a42b3f6c36521857a29ea0949d9781c22Tim Northover    VaListTagDecl = CreateRecordDecl(*Context, TTK_Struct,
5660c264e16a42b3f6c36521857a29ea0949d9781c22Tim Northover                                   Context->getTranslationUnitDecl(),
5661c264e16a42b3f6c36521857a29ea0949d9781c22Tim Northover                                   &Context->Idents.get("__va_list"));
5662c264e16a42b3f6c36521857a29ea0949d9781c22Tim Northover  }
5663c264e16a42b3f6c36521857a29ea0949d9781c22Tim Northover
5664c264e16a42b3f6c36521857a29ea0949d9781c22Tim Northover  VaListTagDecl->startDefinition();
5665c264e16a42b3f6c36521857a29ea0949d9781c22Tim Northover
5666c264e16a42b3f6c36521857a29ea0949d9781c22Tim Northover  const size_t NumFields = 5;
5667c264e16a42b3f6c36521857a29ea0949d9781c22Tim Northover  QualType FieldTypes[NumFields];
5668c264e16a42b3f6c36521857a29ea0949d9781c22Tim Northover  const char *FieldNames[NumFields];
5669c264e16a42b3f6c36521857a29ea0949d9781c22Tim Northover
5670c264e16a42b3f6c36521857a29ea0949d9781c22Tim Northover  // void *__stack;
5671c264e16a42b3f6c36521857a29ea0949d9781c22Tim Northover  FieldTypes[0] = Context->getPointerType(Context->VoidTy);
5672c264e16a42b3f6c36521857a29ea0949d9781c22Tim Northover  FieldNames[0] = "__stack";
5673c264e16a42b3f6c36521857a29ea0949d9781c22Tim Northover
5674c264e16a42b3f6c36521857a29ea0949d9781c22Tim Northover  // void *__gr_top;
5675c264e16a42b3f6c36521857a29ea0949d9781c22Tim Northover  FieldTypes[1] = Context->getPointerType(Context->VoidTy);
5676c264e16a42b3f6c36521857a29ea0949d9781c22Tim Northover  FieldNames[1] = "__gr_top";
5677c264e16a42b3f6c36521857a29ea0949d9781c22Tim Northover
5678c264e16a42b3f6c36521857a29ea0949d9781c22Tim Northover  // void *__vr_top;
5679c264e16a42b3f6c36521857a29ea0949d9781c22Tim Northover  FieldTypes[2] = Context->getPointerType(Context->VoidTy);
5680c264e16a42b3f6c36521857a29ea0949d9781c22Tim Northover  FieldNames[2] = "__vr_top";
5681c264e16a42b3f6c36521857a29ea0949d9781c22Tim Northover
5682c264e16a42b3f6c36521857a29ea0949d9781c22Tim Northover  // int __gr_offs;
5683c264e16a42b3f6c36521857a29ea0949d9781c22Tim Northover  FieldTypes[3] = Context->IntTy;
5684c264e16a42b3f6c36521857a29ea0949d9781c22Tim Northover  FieldNames[3] = "__gr_offs";
5685c264e16a42b3f6c36521857a29ea0949d9781c22Tim Northover
5686c264e16a42b3f6c36521857a29ea0949d9781c22Tim Northover  // int __vr_offs;
5687c264e16a42b3f6c36521857a29ea0949d9781c22Tim Northover  FieldTypes[4] = Context->IntTy;
5688c264e16a42b3f6c36521857a29ea0949d9781c22Tim Northover  FieldNames[4] = "__vr_offs";
5689c264e16a42b3f6c36521857a29ea0949d9781c22Tim Northover
5690c264e16a42b3f6c36521857a29ea0949d9781c22Tim Northover  // Create fields
5691c264e16a42b3f6c36521857a29ea0949d9781c22Tim Northover  for (unsigned i = 0; i < NumFields; ++i) {
5692c264e16a42b3f6c36521857a29ea0949d9781c22Tim Northover    FieldDecl *Field = FieldDecl::Create(const_cast<ASTContext &>(*Context),
5693c264e16a42b3f6c36521857a29ea0949d9781c22Tim Northover                                         VaListTagDecl,
5694c264e16a42b3f6c36521857a29ea0949d9781c22Tim Northover                                         SourceLocation(),
5695c264e16a42b3f6c36521857a29ea0949d9781c22Tim Northover                                         SourceLocation(),
5696c264e16a42b3f6c36521857a29ea0949d9781c22Tim Northover                                         &Context->Idents.get(FieldNames[i]),
5697c264e16a42b3f6c36521857a29ea0949d9781c22Tim Northover                                         FieldTypes[i], /*TInfo=*/0,
5698c264e16a42b3f6c36521857a29ea0949d9781c22Tim Northover                                         /*BitWidth=*/0,
5699c264e16a42b3f6c36521857a29ea0949d9781c22Tim Northover                                         /*Mutable=*/false,
5700c264e16a42b3f6c36521857a29ea0949d9781c22Tim Northover                                         ICIS_NoInit);
5701c264e16a42b3f6c36521857a29ea0949d9781c22Tim Northover    Field->setAccess(AS_public);
5702c264e16a42b3f6c36521857a29ea0949d9781c22Tim Northover    VaListTagDecl->addDecl(Field);
5703c264e16a42b3f6c36521857a29ea0949d9781c22Tim Northover  }
5704c264e16a42b3f6c36521857a29ea0949d9781c22Tim Northover  VaListTagDecl->completeDefinition();
5705c264e16a42b3f6c36521857a29ea0949d9781c22Tim Northover  QualType VaListTagType = Context->getRecordType(VaListTagDecl);
5706c264e16a42b3f6c36521857a29ea0949d9781c22Tim Northover  Context->VaListTagTy = VaListTagType;
5707c264e16a42b3f6c36521857a29ea0949d9781c22Tim Northover
5708c264e16a42b3f6c36521857a29ea0949d9781c22Tim Northover  // } __builtin_va_list;
5709c264e16a42b3f6c36521857a29ea0949d9781c22Tim Northover  TypedefDecl *VaListTypedefDecl
5710c264e16a42b3f6c36521857a29ea0949d9781c22Tim Northover    = TypedefDecl::Create(const_cast<ASTContext &>(*Context),
5711c264e16a42b3f6c36521857a29ea0949d9781c22Tim Northover                          Context->getTranslationUnitDecl(),
5712c264e16a42b3f6c36521857a29ea0949d9781c22Tim Northover                          SourceLocation(), SourceLocation(),
5713c264e16a42b3f6c36521857a29ea0949d9781c22Tim Northover                          &Context->Idents.get("__builtin_va_list"),
5714c264e16a42b3f6c36521857a29ea0949d9781c22Tim Northover                          Context->getTrivialTypeSourceInfo(VaListTagType));
5715c264e16a42b3f6c36521857a29ea0949d9781c22Tim Northover
5716c264e16a42b3f6c36521857a29ea0949d9781c22Tim Northover  return VaListTypedefDecl;
5717c264e16a42b3f6c36521857a29ea0949d9781c22Tim Northover}
5718c264e16a42b3f6c36521857a29ea0949d9781c22Tim Northover
5719c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Ingestatic TypedefDecl *CreatePowerABIBuiltinVaListDecl(const ASTContext *Context) {
5720c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge  // typedef struct __va_list_tag {
5721c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge  RecordDecl *VaListTagDecl;
5722c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge
5723c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge  VaListTagDecl = CreateRecordDecl(*Context, TTK_Struct,
5724c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge                                   Context->getTranslationUnitDecl(),
5725c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge                                   &Context->Idents.get("__va_list_tag"));
5726c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge  VaListTagDecl->startDefinition();
5727c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge
5728c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge  const size_t NumFields = 5;
5729c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge  QualType FieldTypes[NumFields];
5730c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge  const char *FieldNames[NumFields];
5731c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge
5732c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge  //   unsigned char gpr;
5733c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge  FieldTypes[0] = Context->UnsignedCharTy;
5734c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge  FieldNames[0] = "gpr";
5735c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge
5736c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge  //   unsigned char fpr;
5737c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge  FieldTypes[1] = Context->UnsignedCharTy;
5738c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge  FieldNames[1] = "fpr";
5739c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge
5740c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge  //   unsigned short reserved;
5741c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge  FieldTypes[2] = Context->UnsignedShortTy;
5742c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge  FieldNames[2] = "reserved";
5743c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge
5744c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge  //   void* overflow_arg_area;
5745c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge  FieldTypes[3] = Context->getPointerType(Context->VoidTy);
5746c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge  FieldNames[3] = "overflow_arg_area";
5747c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge
5748c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge  //   void* reg_save_area;
5749c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge  FieldTypes[4] = Context->getPointerType(Context->VoidTy);
5750c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge  FieldNames[4] = "reg_save_area";
5751c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge
5752c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge  // Create fields
5753c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge  for (unsigned i = 0; i < NumFields; ++i) {
5754c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge    FieldDecl *Field = FieldDecl::Create(*Context, VaListTagDecl,
5755c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge                                         SourceLocation(),
5756c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge                                         SourceLocation(),
5757c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge                                         &Context->Idents.get(FieldNames[i]),
5758c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge                                         FieldTypes[i], /*TInfo=*/0,
5759c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge                                         /*BitWidth=*/0,
5760c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge                                         /*Mutable=*/false,
5761c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge                                         ICIS_NoInit);
5762c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge    Field->setAccess(AS_public);
5763c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge    VaListTagDecl->addDecl(Field);
5764c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge  }
5765c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge  VaListTagDecl->completeDefinition();
5766c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge  QualType VaListTagType = Context->getRecordType(VaListTagDecl);
5767fb40e3f10ccef93c4f8fb6bd4fe5a108fa6cd369Meador Inge  Context->VaListTagTy = VaListTagType;
5768c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge
5769c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge  // } __va_list_tag;
5770c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge  TypedefDecl *VaListTagTypedefDecl
5771c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge    = TypedefDecl::Create(const_cast<ASTContext &>(*Context),
5772c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge                          Context->getTranslationUnitDecl(),
5773c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge                          SourceLocation(), SourceLocation(),
5774c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge                          &Context->Idents.get("__va_list_tag"),
5775c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge                          Context->getTrivialTypeSourceInfo(VaListTagType));
5776c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge  QualType VaListTagTypedefType =
5777c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge    Context->getTypedefType(VaListTagTypedefDecl);
5778c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge
5779c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge  // typedef __va_list_tag __builtin_va_list[1];
5780c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge  llvm::APInt Size(Context->getTypeSize(Context->getSizeType()), 1);
5781c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge  QualType VaListTagArrayType
5782c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge    = Context->getConstantArrayType(VaListTagTypedefType,
5783c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge                                    Size, ArrayType::Normal, 0);
5784c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge  TypeSourceInfo *TInfo
5785c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge    = Context->getTrivialTypeSourceInfo(VaListTagArrayType);
5786c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge  TypedefDecl *VaListTypedefDecl
5787c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge    = TypedefDecl::Create(const_cast<ASTContext &>(*Context),
5788c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge                          Context->getTranslationUnitDecl(),
5789c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge                          SourceLocation(), SourceLocation(),
5790c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge                          &Context->Idents.get("__builtin_va_list"),
5791c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge                          TInfo);
5792c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge
5793c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge  return VaListTypedefDecl;
5794c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge}
5795c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge
5796c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Ingestatic TypedefDecl *
5797c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador IngeCreateX86_64ABIBuiltinVaListDecl(const ASTContext *Context) {
5798c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge  // typedef struct __va_list_tag {
5799c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge  RecordDecl *VaListTagDecl;
5800c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge  VaListTagDecl = CreateRecordDecl(*Context, TTK_Struct,
5801c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge                                   Context->getTranslationUnitDecl(),
5802c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge                                   &Context->Idents.get("__va_list_tag"));
5803c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge  VaListTagDecl->startDefinition();
5804c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge
5805c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge  const size_t NumFields = 4;
5806c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge  QualType FieldTypes[NumFields];
5807c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge  const char *FieldNames[NumFields];
5808c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge
5809c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge  //   unsigned gp_offset;
5810c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge  FieldTypes[0] = Context->UnsignedIntTy;
5811c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge  FieldNames[0] = "gp_offset";
5812c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge
5813c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge  //   unsigned fp_offset;
5814c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge  FieldTypes[1] = Context->UnsignedIntTy;
5815c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge  FieldNames[1] = "fp_offset";
5816c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge
5817c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge  //   void* overflow_arg_area;
5818c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge  FieldTypes[2] = Context->getPointerType(Context->VoidTy);
5819c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge  FieldNames[2] = "overflow_arg_area";
5820c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge
5821c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge  //   void* reg_save_area;
5822c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge  FieldTypes[3] = Context->getPointerType(Context->VoidTy);
5823c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge  FieldNames[3] = "reg_save_area";
5824c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge
5825c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge  // Create fields
5826c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge  for (unsigned i = 0; i < NumFields; ++i) {
5827c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge    FieldDecl *Field = FieldDecl::Create(const_cast<ASTContext &>(*Context),
5828c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge                                         VaListTagDecl,
5829c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge                                         SourceLocation(),
5830c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge                                         SourceLocation(),
5831c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge                                         &Context->Idents.get(FieldNames[i]),
5832c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge                                         FieldTypes[i], /*TInfo=*/0,
5833c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge                                         /*BitWidth=*/0,
5834c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge                                         /*Mutable=*/false,
5835c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge                                         ICIS_NoInit);
5836c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge    Field->setAccess(AS_public);
5837c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge    VaListTagDecl->addDecl(Field);
5838c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge  }
5839c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge  VaListTagDecl->completeDefinition();
5840c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge  QualType VaListTagType = Context->getRecordType(VaListTagDecl);
5841fb40e3f10ccef93c4f8fb6bd4fe5a108fa6cd369Meador Inge  Context->VaListTagTy = VaListTagType;
5842c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge
5843c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge  // } __va_list_tag;
5844c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge  TypedefDecl *VaListTagTypedefDecl
5845c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge    = TypedefDecl::Create(const_cast<ASTContext &>(*Context),
5846c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge                          Context->getTranslationUnitDecl(),
5847c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge                          SourceLocation(), SourceLocation(),
5848c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge                          &Context->Idents.get("__va_list_tag"),
5849c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge                          Context->getTrivialTypeSourceInfo(VaListTagType));
5850c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge  QualType VaListTagTypedefType =
5851c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge    Context->getTypedefType(VaListTagTypedefDecl);
5852c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge
5853c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge  // typedef __va_list_tag __builtin_va_list[1];
5854c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge  llvm::APInt Size(Context->getTypeSize(Context->getSizeType()), 1);
5855c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge  QualType VaListTagArrayType
5856c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge    = Context->getConstantArrayType(VaListTagTypedefType,
5857c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge                                      Size, ArrayType::Normal,0);
5858c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge  TypeSourceInfo *TInfo
5859c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge    = Context->getTrivialTypeSourceInfo(VaListTagArrayType);
5860c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge  TypedefDecl *VaListTypedefDecl
5861c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge    = TypedefDecl::Create(const_cast<ASTContext &>(*Context),
5862c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge                          Context->getTranslationUnitDecl(),
5863c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge                          SourceLocation(), SourceLocation(),
5864c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge                          &Context->Idents.get("__builtin_va_list"),
5865c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge                          TInfo);
5866c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge
5867c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge  return VaListTypedefDecl;
5868c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge}
5869c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge
5870c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Ingestatic TypedefDecl *CreatePNaClABIBuiltinVaListDecl(const ASTContext *Context) {
5871c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge  // typedef int __builtin_va_list[4];
5872c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge  llvm::APInt Size(Context->getTypeSize(Context->getSizeType()), 4);
5873c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge  QualType IntArrayType
5874c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge    = Context->getConstantArrayType(Context->IntTy,
5875c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge				    Size, ArrayType::Normal, 0);
5876c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge  TypedefDecl *VaListTypedefDecl
5877c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge    = TypedefDecl::Create(const_cast<ASTContext &>(*Context),
5878c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge                          Context->getTranslationUnitDecl(),
5879c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge                          SourceLocation(), SourceLocation(),
5880c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge                          &Context->Idents.get("__builtin_va_list"),
5881c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge                          Context->getTrivialTypeSourceInfo(IntArrayType));
5882c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge
5883c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge  return VaListTypedefDecl;
5884c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge}
5885c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge
5886eae5a820bced67465c8517793a1602dfaeed8a06Logan Chienstatic TypedefDecl *
5887eae5a820bced67465c8517793a1602dfaeed8a06Logan ChienCreateAAPCSABIBuiltinVaListDecl(const ASTContext *Context) {
5888eae5a820bced67465c8517793a1602dfaeed8a06Logan Chien  RecordDecl *VaListDecl;
5889eae5a820bced67465c8517793a1602dfaeed8a06Logan Chien  if (Context->getLangOpts().CPlusPlus) {
5890eae5a820bced67465c8517793a1602dfaeed8a06Logan Chien    // namespace std { struct __va_list {
5891eae5a820bced67465c8517793a1602dfaeed8a06Logan Chien    NamespaceDecl *NS;
5892eae5a820bced67465c8517793a1602dfaeed8a06Logan Chien    NS = NamespaceDecl::Create(const_cast<ASTContext &>(*Context),
5893eae5a820bced67465c8517793a1602dfaeed8a06Logan Chien                               Context->getTranslationUnitDecl(),
5894eae5a820bced67465c8517793a1602dfaeed8a06Logan Chien                               /*Inline*/false, SourceLocation(),
5895eae5a820bced67465c8517793a1602dfaeed8a06Logan Chien                               SourceLocation(), &Context->Idents.get("std"),
5896eae5a820bced67465c8517793a1602dfaeed8a06Logan Chien                               /*PrevDecl*/0);
5897eae5a820bced67465c8517793a1602dfaeed8a06Logan Chien
5898eae5a820bced67465c8517793a1602dfaeed8a06Logan Chien    VaListDecl = CXXRecordDecl::Create(*Context, TTK_Struct,
5899eae5a820bced67465c8517793a1602dfaeed8a06Logan Chien                                       Context->getTranslationUnitDecl(),
5900eae5a820bced67465c8517793a1602dfaeed8a06Logan Chien                                       SourceLocation(), SourceLocation(),
5901eae5a820bced67465c8517793a1602dfaeed8a06Logan Chien                                       &Context->Idents.get("__va_list"));
5902eae5a820bced67465c8517793a1602dfaeed8a06Logan Chien
5903eae5a820bced67465c8517793a1602dfaeed8a06Logan Chien    VaListDecl->setDeclContext(NS);
5904eae5a820bced67465c8517793a1602dfaeed8a06Logan Chien
5905eae5a820bced67465c8517793a1602dfaeed8a06Logan Chien  } else {
5906eae5a820bced67465c8517793a1602dfaeed8a06Logan Chien    // struct __va_list {
5907eae5a820bced67465c8517793a1602dfaeed8a06Logan Chien    VaListDecl = CreateRecordDecl(*Context, TTK_Struct,
5908eae5a820bced67465c8517793a1602dfaeed8a06Logan Chien                                  Context->getTranslationUnitDecl(),
5909eae5a820bced67465c8517793a1602dfaeed8a06Logan Chien                                  &Context->Idents.get("__va_list"));
5910eae5a820bced67465c8517793a1602dfaeed8a06Logan Chien  }
5911eae5a820bced67465c8517793a1602dfaeed8a06Logan Chien
5912eae5a820bced67465c8517793a1602dfaeed8a06Logan Chien  VaListDecl->startDefinition();
5913eae5a820bced67465c8517793a1602dfaeed8a06Logan Chien
5914eae5a820bced67465c8517793a1602dfaeed8a06Logan Chien  // void * __ap;
5915eae5a820bced67465c8517793a1602dfaeed8a06Logan Chien  FieldDecl *Field = FieldDecl::Create(const_cast<ASTContext &>(*Context),
5916eae5a820bced67465c8517793a1602dfaeed8a06Logan Chien                                       VaListDecl,
5917eae5a820bced67465c8517793a1602dfaeed8a06Logan Chien                                       SourceLocation(),
5918eae5a820bced67465c8517793a1602dfaeed8a06Logan Chien                                       SourceLocation(),
5919eae5a820bced67465c8517793a1602dfaeed8a06Logan Chien                                       &Context->Idents.get("__ap"),
5920eae5a820bced67465c8517793a1602dfaeed8a06Logan Chien                                       Context->getPointerType(Context->VoidTy),
5921eae5a820bced67465c8517793a1602dfaeed8a06Logan Chien                                       /*TInfo=*/0,
5922eae5a820bced67465c8517793a1602dfaeed8a06Logan Chien                                       /*BitWidth=*/0,
5923eae5a820bced67465c8517793a1602dfaeed8a06Logan Chien                                       /*Mutable=*/false,
5924eae5a820bced67465c8517793a1602dfaeed8a06Logan Chien                                       ICIS_NoInit);
5925eae5a820bced67465c8517793a1602dfaeed8a06Logan Chien  Field->setAccess(AS_public);
5926eae5a820bced67465c8517793a1602dfaeed8a06Logan Chien  VaListDecl->addDecl(Field);
5927eae5a820bced67465c8517793a1602dfaeed8a06Logan Chien
5928eae5a820bced67465c8517793a1602dfaeed8a06Logan Chien  // };
5929eae5a820bced67465c8517793a1602dfaeed8a06Logan Chien  VaListDecl->completeDefinition();
5930eae5a820bced67465c8517793a1602dfaeed8a06Logan Chien
5931eae5a820bced67465c8517793a1602dfaeed8a06Logan Chien  // typedef struct __va_list __builtin_va_list;
5932eae5a820bced67465c8517793a1602dfaeed8a06Logan Chien  TypeSourceInfo *TInfo
5933eae5a820bced67465c8517793a1602dfaeed8a06Logan Chien    = Context->getTrivialTypeSourceInfo(Context->getRecordType(VaListDecl));
5934eae5a820bced67465c8517793a1602dfaeed8a06Logan Chien
5935eae5a820bced67465c8517793a1602dfaeed8a06Logan Chien  TypedefDecl *VaListTypeDecl
5936eae5a820bced67465c8517793a1602dfaeed8a06Logan Chien    = TypedefDecl::Create(const_cast<ASTContext &>(*Context),
5937eae5a820bced67465c8517793a1602dfaeed8a06Logan Chien                          Context->getTranslationUnitDecl(),
5938eae5a820bced67465c8517793a1602dfaeed8a06Logan Chien                          SourceLocation(), SourceLocation(),
5939eae5a820bced67465c8517793a1602dfaeed8a06Logan Chien                          &Context->Idents.get("__builtin_va_list"),
5940eae5a820bced67465c8517793a1602dfaeed8a06Logan Chien                          TInfo);
5941eae5a820bced67465c8517793a1602dfaeed8a06Logan Chien
5942eae5a820bced67465c8517793a1602dfaeed8a06Logan Chien  return VaListTypeDecl;
5943eae5a820bced67465c8517793a1602dfaeed8a06Logan Chien}
5944eae5a820bced67465c8517793a1602dfaeed8a06Logan Chien
5945c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Ingestatic TypedefDecl *CreateVaListDecl(const ASTContext *Context,
5946c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge                                     TargetInfo::BuiltinVaListKind Kind) {
5947c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge  switch (Kind) {
5948c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge  case TargetInfo::CharPtrBuiltinVaList:
5949c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge    return CreateCharPtrBuiltinVaListDecl(Context);
5950c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge  case TargetInfo::VoidPtrBuiltinVaList:
5951c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge    return CreateVoidPtrBuiltinVaListDecl(Context);
5952c264e16a42b3f6c36521857a29ea0949d9781c22Tim Northover  case TargetInfo::AArch64ABIBuiltinVaList:
5953c264e16a42b3f6c36521857a29ea0949d9781c22Tim Northover    return CreateAArch64ABIBuiltinVaListDecl(Context);
5954c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge  case TargetInfo::PowerABIBuiltinVaList:
5955c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge    return CreatePowerABIBuiltinVaListDecl(Context);
5956c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge  case TargetInfo::X86_64ABIBuiltinVaList:
5957c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge    return CreateX86_64ABIBuiltinVaListDecl(Context);
5958c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge  case TargetInfo::PNaClABIBuiltinVaList:
5959c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge    return CreatePNaClABIBuiltinVaListDecl(Context);
5960eae5a820bced67465c8517793a1602dfaeed8a06Logan Chien  case TargetInfo::AAPCSABIBuiltinVaList:
5961eae5a820bced67465c8517793a1602dfaeed8a06Logan Chien    return CreateAAPCSABIBuiltinVaListDecl(Context);
5962c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge  }
5963c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge
5964c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge  llvm_unreachable("Unhandled __builtin_va_list type kind");
5965c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge}
5966c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge
5967c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador IngeTypedefDecl *ASTContext::getBuiltinVaListDecl() const {
5968c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge  if (!BuiltinVaListDecl)
5969c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge    BuiltinVaListDecl = CreateVaListDecl(this, Target->getBuiltinVaListKind());
5970c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge
5971c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge  return BuiltinVaListDecl;
5972c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge}
5973c5613b26a24a33d7450e3d0bf315c6ccc920ce7bMeador Inge
5974fb40e3f10ccef93c4f8fb6bd4fe5a108fa6cd369Meador IngeQualType ASTContext::getVaListTagType() const {
5975fb40e3f10ccef93c4f8fb6bd4fe5a108fa6cd369Meador Inge  // Force the creation of VaListTagTy by building the __builtin_va_list
5976fb40e3f10ccef93c4f8fb6bd4fe5a108fa6cd369Meador Inge  // declaration.
5977fb40e3f10ccef93c4f8fb6bd4fe5a108fa6cd369Meador Inge  if (VaListTagTy.isNull())
5978fb40e3f10ccef93c4f8fb6bd4fe5a108fa6cd369Meador Inge    (void) getBuiltinVaListDecl();
5979fb40e3f10ccef93c4f8fb6bd4fe5a108fa6cd369Meador Inge
5980fb40e3f10ccef93c4f8fb6bd4fe5a108fa6cd369Meador Inge  return VaListTagTy;
5981fb40e3f10ccef93c4f8fb6bd4fe5a108fa6cd369Meador Inge}
5982fb40e3f10ccef93c4f8fb6bd4fe5a108fa6cd369Meador Inge
5983a526c5c67e5a0473c340903ee542ce570119665fTed Kremenekvoid ASTContext::setObjCConstantStringInterface(ObjCInterfaceDecl *Decl) {
59841eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  assert(ObjCConstantStringType.isNull() &&
59852198891824c38d45b2279de5d5e3ef9394eb457cSteve Naroff         "'NSConstantString' type already set!");
59861eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
5987a526c5c67e5a0473c340903ee542ce570119665fTed Kremenek  ObjCConstantStringType = getObjCInterfaceType(Decl);
59882198891824c38d45b2279de5d5e3ef9394eb457cSteve Naroff}
59892198891824c38d45b2279de5d5e3ef9394eb457cSteve Naroff
59900bd6feb9e9d40fc889fd47e899985125a43dfed8John McCall/// \brief Retrieve the template name that corresponds to a non-empty
59910bd6feb9e9d40fc889fd47e899985125a43dfed8John McCall/// lookup.
59924ba2a17694148e16eaa8d3917f657ffcd3667be4Jay FoadTemplateName
59934ba2a17694148e16eaa8d3917f657ffcd3667be4Jay FoadASTContext::getOverloadedTemplateName(UnresolvedSetIterator Begin,
59944ba2a17694148e16eaa8d3917f657ffcd3667be4Jay Foad                                      UnresolvedSetIterator End) const {
59950bd6feb9e9d40fc889fd47e899985125a43dfed8John McCall  unsigned size = End - Begin;
59960bd6feb9e9d40fc889fd47e899985125a43dfed8John McCall  assert(size > 1 && "set is not overloaded!");
59977532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor
59980bd6feb9e9d40fc889fd47e899985125a43dfed8John McCall  void *memory = Allocate(sizeof(OverloadedTemplateStorage) +
59990bd6feb9e9d40fc889fd47e899985125a43dfed8John McCall                          size * sizeof(FunctionTemplateDecl*));
60000bd6feb9e9d40fc889fd47e899985125a43dfed8John McCall  OverloadedTemplateStorage *OT = new(memory) OverloadedTemplateStorage(size);
60010bd6feb9e9d40fc889fd47e899985125a43dfed8John McCall
60020bd6feb9e9d40fc889fd47e899985125a43dfed8John McCall  NamedDecl **Storage = OT->getStorage();
6003eec51cf1ba5f0e62c9cdb81b5c63babdd6e649abJohn McCall  for (UnresolvedSetIterator I = Begin; I != End; ++I) {
60040bd6feb9e9d40fc889fd47e899985125a43dfed8John McCall    NamedDecl *D = *I;
60050bd6feb9e9d40fc889fd47e899985125a43dfed8John McCall    assert(isa<FunctionTemplateDecl>(D) ||
60060bd6feb9e9d40fc889fd47e899985125a43dfed8John McCall           (isa<UsingShadowDecl>(D) &&
60070bd6feb9e9d40fc889fd47e899985125a43dfed8John McCall            isa<FunctionTemplateDecl>(D->getUnderlyingDecl())));
60080bd6feb9e9d40fc889fd47e899985125a43dfed8John McCall    *Storage++ = D;
60097532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor  }
60107532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor
60110bd6feb9e9d40fc889fd47e899985125a43dfed8John McCall  return TemplateName(OT);
60127532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor}
60137532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor
6014d99cbe66403ee39c2ee58024b9582b95649a4fc5Douglas Gregor/// \brief Retrieve the template name that represents a qualified
6015d99cbe66403ee39c2ee58024b9582b95649a4fc5Douglas Gregor/// template name such as \c std::vector.
60164ba2a17694148e16eaa8d3917f657ffcd3667be4Jay FoadTemplateName
60174ba2a17694148e16eaa8d3917f657ffcd3667be4Jay FoadASTContext::getQualifiedTemplateName(NestedNameSpecifier *NNS,
60184ba2a17694148e16eaa8d3917f657ffcd3667be4Jay Foad                                     bool TemplateKeyword,
60194ba2a17694148e16eaa8d3917f657ffcd3667be4Jay Foad                                     TemplateDecl *Template) const {
60200f0ea2a96534c615ff5fdd81363989b23cf2164aDouglas Gregor  assert(NNS && "Missing nested-name-specifier in qualified template name");
60210f0ea2a96534c615ff5fdd81363989b23cf2164aDouglas Gregor
6022789b1f640205e81b5af250693246120f1ce9d147Douglas Gregor  // FIXME: Canonicalization?
6023d99cbe66403ee39c2ee58024b9582b95649a4fc5Douglas Gregor  llvm::FoldingSetNodeID ID;
6024d99cbe66403ee39c2ee58024b9582b95649a4fc5Douglas Gregor  QualifiedTemplateName::Profile(ID, NNS, TemplateKeyword, Template);
60251eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
6026d99cbe66403ee39c2ee58024b9582b95649a4fc5Douglas Gregor  void *InsertPos = 0;
6027d99cbe66403ee39c2ee58024b9582b95649a4fc5Douglas Gregor  QualifiedTemplateName *QTN =
60280bd6feb9e9d40fc889fd47e899985125a43dfed8John McCall    QualifiedTemplateNames.FindNodeOrInsertPos(ID, InsertPos);
6029d99cbe66403ee39c2ee58024b9582b95649a4fc5Douglas Gregor  if (!QTN) {
60302f47cab092cd2dcbfe7e003fa865499caf198dc2Richard Smith    QTN = new (*this, llvm::alignOf<QualifiedTemplateName>())
60312f47cab092cd2dcbfe7e003fa865499caf198dc2Richard Smith        QualifiedTemplateName(NNS, TemplateKeyword, Template);
6032d99cbe66403ee39c2ee58024b9582b95649a4fc5Douglas Gregor    QualifiedTemplateNames.InsertNode(QTN, InsertPos);
6033d99cbe66403ee39c2ee58024b9582b95649a4fc5Douglas Gregor  }
60341eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
6035d99cbe66403ee39c2ee58024b9582b95649a4fc5Douglas Gregor  return TemplateName(QTN);
6036d99cbe66403ee39c2ee58024b9582b95649a4fc5Douglas Gregor}
6037d99cbe66403ee39c2ee58024b9582b95649a4fc5Douglas Gregor
60387532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor/// \brief Retrieve the template name that represents a dependent
60397532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor/// template name such as \c MetaFun::template apply.
60404ba2a17694148e16eaa8d3917f657ffcd3667be4Jay FoadTemplateName
60414ba2a17694148e16eaa8d3917f657ffcd3667be4Jay FoadASTContext::getDependentTemplateName(NestedNameSpecifier *NNS,
60424ba2a17694148e16eaa8d3917f657ffcd3667be4Jay Foad                                     const IdentifierInfo *Name) const {
60431eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  assert((!NNS || NNS->isDependent()) &&
60443b6afbb99a1c44b4076f8e15fb7311405941b306Douglas Gregor         "Nested name specifier must be dependent");
60457532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor
60467532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor  llvm::FoldingSetNodeID ID;
60477532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor  DependentTemplateName::Profile(ID, NNS, Name);
60487532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor
60497532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor  void *InsertPos = 0;
60507532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor  DependentTemplateName *QTN =
60517532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor    DependentTemplateNames.FindNodeOrInsertPos(ID, InsertPos);
60527532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor
60537532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor  if (QTN)
60547532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor    return TemplateName(QTN);
60557532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor
60567532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor  NestedNameSpecifier *CanonNNS = getCanonicalNestedNameSpecifier(NNS);
60577532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor  if (CanonNNS == NNS) {
60582f47cab092cd2dcbfe7e003fa865499caf198dc2Richard Smith    QTN = new (*this, llvm::alignOf<DependentTemplateName>())
60592f47cab092cd2dcbfe7e003fa865499caf198dc2Richard Smith        DependentTemplateName(NNS, Name);
60607532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor  } else {
60617532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor    TemplateName Canon = getDependentTemplateName(CanonNNS, Name);
60622f47cab092cd2dcbfe7e003fa865499caf198dc2Richard Smith    QTN = new (*this, llvm::alignOf<DependentTemplateName>())
60632f47cab092cd2dcbfe7e003fa865499caf198dc2Richard Smith        DependentTemplateName(NNS, Name, Canon);
6064789b1f640205e81b5af250693246120f1ce9d147Douglas Gregor    DependentTemplateName *CheckQTN =
6065789b1f640205e81b5af250693246120f1ce9d147Douglas Gregor      DependentTemplateNames.FindNodeOrInsertPos(ID, InsertPos);
6066789b1f640205e81b5af250693246120f1ce9d147Douglas Gregor    assert(!CheckQTN && "Dependent type name canonicalization broken");
6067789b1f640205e81b5af250693246120f1ce9d147Douglas Gregor    (void)CheckQTN;
60687532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor  }
60697532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor
60707532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor  DependentTemplateNames.InsertNode(QTN, InsertPos);
60717532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor  return TemplateName(QTN);
60727532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor}
60737532dc66648cfe7432c9fe66dec5225f0ab301c6Douglas Gregor
6074ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor/// \brief Retrieve the template name that represents a dependent
6075ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor/// template name such as \c MetaFun::template operator+.
6076ca1bdd7c269a2390d43c040a60511edd017ee130Douglas GregorTemplateName
6077ca1bdd7c269a2390d43c040a60511edd017ee130Douglas GregorASTContext::getDependentTemplateName(NestedNameSpecifier *NNS,
60784ba2a17694148e16eaa8d3917f657ffcd3667be4Jay Foad                                     OverloadedOperatorKind Operator) const {
6079ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor  assert((!NNS || NNS->isDependent()) &&
6080ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor         "Nested name specifier must be dependent");
6081ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor
6082ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor  llvm::FoldingSetNodeID ID;
6083ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor  DependentTemplateName::Profile(ID, NNS, Operator);
6084ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor
6085ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor  void *InsertPos = 0;
6086789b1f640205e81b5af250693246120f1ce9d147Douglas Gregor  DependentTemplateName *QTN
6087789b1f640205e81b5af250693246120f1ce9d147Douglas Gregor    = DependentTemplateNames.FindNodeOrInsertPos(ID, InsertPos);
6088ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor
6089ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor  if (QTN)
6090ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor    return TemplateName(QTN);
6091ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor
6092ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor  NestedNameSpecifier *CanonNNS = getCanonicalNestedNameSpecifier(NNS);
6093ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor  if (CanonNNS == NNS) {
60942f47cab092cd2dcbfe7e003fa865499caf198dc2Richard Smith    QTN = new (*this, llvm::alignOf<DependentTemplateName>())
60952f47cab092cd2dcbfe7e003fa865499caf198dc2Richard Smith        DependentTemplateName(NNS, Operator);
6096ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor  } else {
6097ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor    TemplateName Canon = getDependentTemplateName(CanonNNS, Operator);
60982f47cab092cd2dcbfe7e003fa865499caf198dc2Richard Smith    QTN = new (*this, llvm::alignOf<DependentTemplateName>())
60992f47cab092cd2dcbfe7e003fa865499caf198dc2Richard Smith        DependentTemplateName(NNS, Operator, Canon);
6100789b1f640205e81b5af250693246120f1ce9d147Douglas Gregor
6101789b1f640205e81b5af250693246120f1ce9d147Douglas Gregor    DependentTemplateName *CheckQTN
6102789b1f640205e81b5af250693246120f1ce9d147Douglas Gregor      = DependentTemplateNames.FindNodeOrInsertPos(ID, InsertPos);
6103789b1f640205e81b5af250693246120f1ce9d147Douglas Gregor    assert(!CheckQTN && "Dependent template name canonicalization broken");
6104789b1f640205e81b5af250693246120f1ce9d147Douglas Gregor    (void)CheckQTN;
6105ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor  }
6106ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor
6107ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor  DependentTemplateNames.InsertNode(QTN, InsertPos);
6108ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor  return TemplateName(QTN);
6109ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor}
6110ca1bdd7c269a2390d43c040a60511edd017ee130Douglas Gregor
61111aee05d08b2184acadeb36de300e216390780d6cDouglas GregorTemplateName
6112146060435c3efce95c95a092c7a1eb651cfb9ae0John McCallASTContext::getSubstTemplateTemplateParm(TemplateTemplateParmDecl *param,
6113146060435c3efce95c95a092c7a1eb651cfb9ae0John McCall                                         TemplateName replacement) const {
6114146060435c3efce95c95a092c7a1eb651cfb9ae0John McCall  llvm::FoldingSetNodeID ID;
6115146060435c3efce95c95a092c7a1eb651cfb9ae0John McCall  SubstTemplateTemplateParmStorage::Profile(ID, param, replacement);
6116146060435c3efce95c95a092c7a1eb651cfb9ae0John McCall
6117146060435c3efce95c95a092c7a1eb651cfb9ae0John McCall  void *insertPos = 0;
6118146060435c3efce95c95a092c7a1eb651cfb9ae0John McCall  SubstTemplateTemplateParmStorage *subst
6119146060435c3efce95c95a092c7a1eb651cfb9ae0John McCall    = SubstTemplateTemplateParms.FindNodeOrInsertPos(ID, insertPos);
6120146060435c3efce95c95a092c7a1eb651cfb9ae0John McCall
6121146060435c3efce95c95a092c7a1eb651cfb9ae0John McCall  if (!subst) {
6122146060435c3efce95c95a092c7a1eb651cfb9ae0John McCall    subst = new (*this) SubstTemplateTemplateParmStorage(param, replacement);
6123146060435c3efce95c95a092c7a1eb651cfb9ae0John McCall    SubstTemplateTemplateParms.InsertNode(subst, insertPos);
6124146060435c3efce95c95a092c7a1eb651cfb9ae0John McCall  }
6125146060435c3efce95c95a092c7a1eb651cfb9ae0John McCall
6126146060435c3efce95c95a092c7a1eb651cfb9ae0John McCall  return TemplateName(subst);
6127146060435c3efce95c95a092c7a1eb651cfb9ae0John McCall}
6128146060435c3efce95c95a092c7a1eb651cfb9ae0John McCall
6129146060435c3efce95c95a092c7a1eb651cfb9ae0John McCallTemplateName
61301aee05d08b2184acadeb36de300e216390780d6cDouglas GregorASTContext::getSubstTemplateTemplateParmPack(TemplateTemplateParmDecl *Param,
61311aee05d08b2184acadeb36de300e216390780d6cDouglas Gregor                                       const TemplateArgument &ArgPack) const {
61321aee05d08b2184acadeb36de300e216390780d6cDouglas Gregor  ASTContext &Self = const_cast<ASTContext &>(*this);
61331aee05d08b2184acadeb36de300e216390780d6cDouglas Gregor  llvm::FoldingSetNodeID ID;
61341aee05d08b2184acadeb36de300e216390780d6cDouglas Gregor  SubstTemplateTemplateParmPackStorage::Profile(ID, Self, Param, ArgPack);
61351aee05d08b2184acadeb36de300e216390780d6cDouglas Gregor
61361aee05d08b2184acadeb36de300e216390780d6cDouglas Gregor  void *InsertPos = 0;
61371aee05d08b2184acadeb36de300e216390780d6cDouglas Gregor  SubstTemplateTemplateParmPackStorage *Subst
61381aee05d08b2184acadeb36de300e216390780d6cDouglas Gregor    = SubstTemplateTemplateParmPacks.FindNodeOrInsertPos(ID, InsertPos);
61391aee05d08b2184acadeb36de300e216390780d6cDouglas Gregor
61401aee05d08b2184acadeb36de300e216390780d6cDouglas Gregor  if (!Subst) {
6141146060435c3efce95c95a092c7a1eb651cfb9ae0John McCall    Subst = new (*this) SubstTemplateTemplateParmPackStorage(Param,
61421aee05d08b2184acadeb36de300e216390780d6cDouglas Gregor                                                           ArgPack.pack_size(),
61431aee05d08b2184acadeb36de300e216390780d6cDouglas Gregor                                                         ArgPack.pack_begin());
61441aee05d08b2184acadeb36de300e216390780d6cDouglas Gregor    SubstTemplateTemplateParmPacks.InsertNode(Subst, InsertPos);
61451aee05d08b2184acadeb36de300e216390780d6cDouglas Gregor  }
61461aee05d08b2184acadeb36de300e216390780d6cDouglas Gregor
61471aee05d08b2184acadeb36de300e216390780d6cDouglas Gregor  return TemplateName(Subst);
61481aee05d08b2184acadeb36de300e216390780d6cDouglas Gregor}
61491aee05d08b2184acadeb36de300e216390780d6cDouglas Gregor
6150b4e66d5259f90e9aae4d40fc5de801e046c7df94Douglas Gregor/// getFromTargetType - Given one of the integer types provided by
6151d934112e6170b0fd940d8e40db6936cea2cdcf62Douglas Gregor/// TargetInfo, produce the corresponding type. The unsigned @p Type
6152d934112e6170b0fd940d8e40db6936cea2cdcf62Douglas Gregor/// is actually a value of type @c TargetInfo::IntType.
6153e27ec8ad56dbf1efb2de004b90fbbb86f740e3f1John McCallCanQualType ASTContext::getFromTargetType(unsigned Type) const {
6154b4e66d5259f90e9aae4d40fc5de801e046c7df94Douglas Gregor  switch (Type) {
6155e27ec8ad56dbf1efb2de004b90fbbb86f740e3f1John McCall  case TargetInfo::NoInt: return CanQualType();
6156b4e66d5259f90e9aae4d40fc5de801e046c7df94Douglas Gregor  case TargetInfo::SignedShort: return ShortTy;
6157b4e66d5259f90e9aae4d40fc5de801e046c7df94Douglas Gregor  case TargetInfo::UnsignedShort: return UnsignedShortTy;
6158b4e66d5259f90e9aae4d40fc5de801e046c7df94Douglas Gregor  case TargetInfo::SignedInt: return IntTy;
6159b4e66d5259f90e9aae4d40fc5de801e046c7df94Douglas Gregor  case TargetInfo::UnsignedInt: return UnsignedIntTy;
6160b4e66d5259f90e9aae4d40fc5de801e046c7df94Douglas Gregor  case TargetInfo::SignedLong: return LongTy;
6161b4e66d5259f90e9aae4d40fc5de801e046c7df94Douglas Gregor  case TargetInfo::UnsignedLong: return UnsignedLongTy;
6162b4e66d5259f90e9aae4d40fc5de801e046c7df94Douglas Gregor  case TargetInfo::SignedLongLong: return LongLongTy;
6163b4e66d5259f90e9aae4d40fc5de801e046c7df94Douglas Gregor  case TargetInfo::UnsignedLongLong: return UnsignedLongLongTy;
6164b4e66d5259f90e9aae4d40fc5de801e046c7df94Douglas Gregor  }
6165b4e66d5259f90e9aae4d40fc5de801e046c7df94Douglas Gregor
6166b219cfc4d75f0a03630b7c4509ef791b7e97b2c8David Blaikie  llvm_unreachable("Unhandled TargetInfo::IntType value");
6167b4e66d5259f90e9aae4d40fc5de801e046c7df94Douglas Gregor}
6168b6ccaac65ca72f72954eb3893bbd940bedd23f00Ted Kremenek
6169b6ccaac65ca72f72954eb3893bbd940bedd23f00Ted Kremenek//===----------------------------------------------------------------------===//
6170b6ccaac65ca72f72954eb3893bbd940bedd23f00Ted Kremenek//                        Type Predicates.
6171b6ccaac65ca72f72954eb3893bbd940bedd23f00Ted Kremenek//===----------------------------------------------------------------------===//
6172b6ccaac65ca72f72954eb3893bbd940bedd23f00Ted Kremenek
61734fd83ea566f4a0c083001c84b75da6cc8c99c1d6Fariborz Jahanian/// getObjCGCAttr - Returns one of GCNone, Weak or Strong objc's
61744fd83ea566f4a0c083001c84b75da6cc8c99c1d6Fariborz Jahanian/// garbage collection attribute.
61754fd83ea566f4a0c083001c84b75da6cc8c99c1d6Fariborz Jahanian///
6176ae278a3a57595349a411f6474938d4dd1b263a0eJohn McCallQualifiers::GC ASTContext::getObjCGCAttrKind(QualType Ty) const {
61774e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie  if (getLangOpts().getGC() == LangOptions::NonGC)
6178ae278a3a57595349a411f6474938d4dd1b263a0eJohn McCall    return Qualifiers::GCNone;
6179ae278a3a57595349a411f6474938d4dd1b263a0eJohn McCall
61804e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie  assert(getLangOpts().ObjC1);
6181ae278a3a57595349a411f6474938d4dd1b263a0eJohn McCall  Qualifiers::GC GCAttrs = Ty.getObjCGCAttr();
6182ae278a3a57595349a411f6474938d4dd1b263a0eJohn McCall
6183ae278a3a57595349a411f6474938d4dd1b263a0eJohn McCall  // Default behaviour under objective-C's gc is for ObjC pointers
6184ae278a3a57595349a411f6474938d4dd1b263a0eJohn McCall  // (or pointers to them) be treated as though they were declared
6185ae278a3a57595349a411f6474938d4dd1b263a0eJohn McCall  // as __strong.
6186ae278a3a57595349a411f6474938d4dd1b263a0eJohn McCall  if (GCAttrs == Qualifiers::GCNone) {
6187ae278a3a57595349a411f6474938d4dd1b263a0eJohn McCall    if (Ty->isObjCObjectPointerType() || Ty->isBlockPointerType())
6188ae278a3a57595349a411f6474938d4dd1b263a0eJohn McCall      return Qualifiers::Strong;
6189ae278a3a57595349a411f6474938d4dd1b263a0eJohn McCall    else if (Ty->isPointerType())
6190ae278a3a57595349a411f6474938d4dd1b263a0eJohn McCall      return getObjCGCAttrKind(Ty->getAs<PointerType>()->getPointeeType());
6191ae278a3a57595349a411f6474938d4dd1b263a0eJohn McCall  } else {
6192ae278a3a57595349a411f6474938d4dd1b263a0eJohn McCall    // It's not valid to set GC attributes on anything that isn't a
6193ae278a3a57595349a411f6474938d4dd1b263a0eJohn McCall    // pointer.
6194ae278a3a57595349a411f6474938d4dd1b263a0eJohn McCall#ifndef NDEBUG
6195ae278a3a57595349a411f6474938d4dd1b263a0eJohn McCall    QualType CT = Ty->getCanonicalTypeInternal();
6196ae278a3a57595349a411f6474938d4dd1b263a0eJohn McCall    while (const ArrayType *AT = dyn_cast<ArrayType>(CT))
6197ae278a3a57595349a411f6474938d4dd1b263a0eJohn McCall      CT = AT->getElementType();
6198ae278a3a57595349a411f6474938d4dd1b263a0eJohn McCall    assert(CT->isAnyPointerType() || CT->isBlockPointerType());
6199ae278a3a57595349a411f6474938d4dd1b263a0eJohn McCall#endif
62004fd83ea566f4a0c083001c84b75da6cc8c99c1d6Fariborz Jahanian  }
6201b7d2553edd2532d29b98b9e76bcf6a62bc48b417Chris Lattner  return GCAttrs;
62024fd83ea566f4a0c083001c84b75da6cc8c99c1d6Fariborz Jahanian}
62034fd83ea566f4a0c083001c84b75da6cc8c99c1d6Fariborz Jahanian
62046ac46a4a521366d7ab36ebe2ce4e624ab96b06f9Chris Lattner//===----------------------------------------------------------------------===//
62056ac46a4a521366d7ab36ebe2ce4e624ab96b06f9Chris Lattner//                        Type Compatibility Testing
62066ac46a4a521366d7ab36ebe2ce4e624ab96b06f9Chris Lattner//===----------------------------------------------------------------------===//
6207770951b5bb6028a8d326ddb4a13cef7d4a128162Chris Lattner
62081eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// areCompatVectorTypes - Return true if the two specified vector types are
62096ac46a4a521366d7ab36ebe2ce4e624ab96b06f9Chris Lattner/// compatible.
62106ac46a4a521366d7ab36ebe2ce4e624ab96b06f9Chris Lattnerstatic bool areCompatVectorTypes(const VectorType *LHS,
62116ac46a4a521366d7ab36ebe2ce4e624ab96b06f9Chris Lattner                                 const VectorType *RHS) {
6212467b27b9a24bdc823218ad1ad0e37673b6cc1e83John McCall  assert(LHS->isCanonicalUnqualified() && RHS->isCanonicalUnqualified());
62136ac46a4a521366d7ab36ebe2ce4e624ab96b06f9Chris Lattner  return LHS->getElementType() == RHS->getElementType() &&
621461710854be2b098428aff5316e64bd34b30fbcb7Chris Lattner         LHS->getNumElements() == RHS->getNumElements();
62156ac46a4a521366d7ab36ebe2ce4e624ab96b06f9Chris Lattner}
62166ac46a4a521366d7ab36ebe2ce4e624ab96b06f9Chris Lattner
6217255210ef415b9893f0e3794e8d9a704194c12f3cDouglas Gregorbool ASTContext::areCompatibleVectorTypes(QualType FirstVec,
6218255210ef415b9893f0e3794e8d9a704194c12f3cDouglas Gregor                                          QualType SecondVec) {
6219255210ef415b9893f0e3794e8d9a704194c12f3cDouglas Gregor  assert(FirstVec->isVectorType() && "FirstVec should be a vector type");
6220255210ef415b9893f0e3794e8d9a704194c12f3cDouglas Gregor  assert(SecondVec->isVectorType() && "SecondVec should be a vector type");
6221255210ef415b9893f0e3794e8d9a704194c12f3cDouglas Gregor
6222255210ef415b9893f0e3794e8d9a704194c12f3cDouglas Gregor  if (hasSameUnqualifiedType(FirstVec, SecondVec))
6223255210ef415b9893f0e3794e8d9a704194c12f3cDouglas Gregor    return true;
6224255210ef415b9893f0e3794e8d9a704194c12f3cDouglas Gregor
6225f69eb7cf8e616b5aad7911ec6f79b24b0a009227Bob Wilson  // Treat Neon vector types and most AltiVec vector types as if they are the
6226f69eb7cf8e616b5aad7911ec6f79b24b0a009227Bob Wilson  // equivalent GCC vector types.
6227255210ef415b9893f0e3794e8d9a704194c12f3cDouglas Gregor  const VectorType *First = FirstVec->getAs<VectorType>();
6228255210ef415b9893f0e3794e8d9a704194c12f3cDouglas Gregor  const VectorType *Second = SecondVec->getAs<VectorType>();
6229f69eb7cf8e616b5aad7911ec6f79b24b0a009227Bob Wilson  if (First->getNumElements() == Second->getNumElements() &&
6230255210ef415b9893f0e3794e8d9a704194c12f3cDouglas Gregor      hasSameType(First->getElementType(), Second->getElementType()) &&
6231f69eb7cf8e616b5aad7911ec6f79b24b0a009227Bob Wilson      First->getVectorKind() != VectorType::AltiVecPixel &&
6232f69eb7cf8e616b5aad7911ec6f79b24b0a009227Bob Wilson      First->getVectorKind() != VectorType::AltiVecBool &&
6233f69eb7cf8e616b5aad7911ec6f79b24b0a009227Bob Wilson      Second->getVectorKind() != VectorType::AltiVecPixel &&
6234f69eb7cf8e616b5aad7911ec6f79b24b0a009227Bob Wilson      Second->getVectorKind() != VectorType::AltiVecBool)
6235255210ef415b9893f0e3794e8d9a704194c12f3cDouglas Gregor    return true;
6236255210ef415b9893f0e3794e8d9a704194c12f3cDouglas Gregor
6237255210ef415b9893f0e3794e8d9a704194c12f3cDouglas Gregor  return false;
6238255210ef415b9893f0e3794e8d9a704194c12f3cDouglas Gregor}
6239255210ef415b9893f0e3794e8d9a704194c12f3cDouglas Gregor
62404084c306635b70f37029dca938444e6013f08684Steve Naroff//===----------------------------------------------------------------------===//
62414084c306635b70f37029dca938444e6013f08684Steve Naroff// ObjCQualifiedIdTypesAreCompatible - Compatibility testing for qualified id's.
62424084c306635b70f37029dca938444e6013f08684Steve Naroff//===----------------------------------------------------------------------===//
62434084c306635b70f37029dca938444e6013f08684Steve Naroff
62444084c306635b70f37029dca938444e6013f08684Steve Naroff/// ProtocolCompatibleWithProtocol - return 'true' if 'lProto' is in the
62454084c306635b70f37029dca938444e6013f08684Steve Naroff/// inheritance hierarchy of 'rProto'.
62464ba2a17694148e16eaa8d3917f657ffcd3667be4Jay Foadbool
62474ba2a17694148e16eaa8d3917f657ffcd3667be4Jay FoadASTContext::ProtocolCompatibleWithProtocol(ObjCProtocolDecl *lProto,
62484ba2a17694148e16eaa8d3917f657ffcd3667be4Jay Foad                                           ObjCProtocolDecl *rProto) const {
62493fc73ee0c613715ebce78e30b4d050ea715a007dDouglas Gregor  if (declaresSameEntity(lProto, rProto))
625014108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff    return true;
62514084c306635b70f37029dca938444e6013f08684Steve Naroff  for (ObjCProtocolDecl::protocol_iterator PI = rProto->protocol_begin(),
62524084c306635b70f37029dca938444e6013f08684Steve Naroff       E = rProto->protocol_end(); PI != E; ++PI)
62534084c306635b70f37029dca938444e6013f08684Steve Naroff    if (ProtocolCompatibleWithProtocol(lProto, *PI))
62544084c306635b70f37029dca938444e6013f08684Steve Naroff      return true;
62554084c306635b70f37029dca938444e6013f08684Steve Naroff  return false;
62564084c306635b70f37029dca938444e6013f08684Steve Naroff}
625714108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff
62584c3b8a3ed28c52dc56d3a1b9670d71e5f7070c62Dmitri Gribenko/// QualifiedIdConformsQualifiedId - compare id<pr,...> with id<pr1,...>
62594084c306635b70f37029dca938444e6013f08684Steve Naroff/// return true if lhs's protocols conform to rhs's protocol; false
62604084c306635b70f37029dca938444e6013f08684Steve Naroff/// otherwise.
62614084c306635b70f37029dca938444e6013f08684Steve Naroffbool ASTContext::QualifiedIdConformsQualifiedId(QualType lhs, QualType rhs) {
62624084c306635b70f37029dca938444e6013f08684Steve Naroff  if (lhs->isObjCQualifiedIdType() && rhs->isObjCQualifiedIdType())
62634084c306635b70f37029dca938444e6013f08684Steve Naroff    return ObjCQualifiedIdTypesAreCompatible(lhs, rhs, false);
62644084c306635b70f37029dca938444e6013f08684Steve Naroff  return false;
62654084c306635b70f37029dca938444e6013f08684Steve Naroff}
62664084c306635b70f37029dca938444e6013f08684Steve Naroff
62674c3b8a3ed28c52dc56d3a1b9670d71e5f7070c62Dmitri Gribenko/// ObjCQualifiedClassTypesAreCompatible - compare  Class<pr,...> and
62684c3b8a3ed28c52dc56d3a1b9670d71e5f7070c62Dmitri Gribenko/// Class<pr1, ...>.
6269a8f8dac6a29f6d33474a38a32ce9dd859b696da9Fariborz Jahanianbool ASTContext::ObjCQualifiedClassTypesAreCompatible(QualType lhs,
6270a8f8dac6a29f6d33474a38a32ce9dd859b696da9Fariborz Jahanian                                                      QualType rhs) {
6271a8f8dac6a29f6d33474a38a32ce9dd859b696da9Fariborz Jahanian  const ObjCObjectPointerType *lhsQID = lhs->getAs<ObjCObjectPointerType>();
6272a8f8dac6a29f6d33474a38a32ce9dd859b696da9Fariborz Jahanian  const ObjCObjectPointerType *rhsOPT = rhs->getAs<ObjCObjectPointerType>();
6273a8f8dac6a29f6d33474a38a32ce9dd859b696da9Fariborz Jahanian  assert ((lhsQID && rhsOPT) && "ObjCQualifiedClassTypesAreCompatible");
6274a8f8dac6a29f6d33474a38a32ce9dd859b696da9Fariborz Jahanian
6275a8f8dac6a29f6d33474a38a32ce9dd859b696da9Fariborz Jahanian  for (ObjCObjectPointerType::qual_iterator I = lhsQID->qual_begin(),
6276a8f8dac6a29f6d33474a38a32ce9dd859b696da9Fariborz Jahanian       E = lhsQID->qual_end(); I != E; ++I) {
6277a8f8dac6a29f6d33474a38a32ce9dd859b696da9Fariborz Jahanian    bool match = false;
6278a8f8dac6a29f6d33474a38a32ce9dd859b696da9Fariborz Jahanian    ObjCProtocolDecl *lhsProto = *I;
6279a8f8dac6a29f6d33474a38a32ce9dd859b696da9Fariborz Jahanian    for (ObjCObjectPointerType::qual_iterator J = rhsOPT->qual_begin(),
6280a8f8dac6a29f6d33474a38a32ce9dd859b696da9Fariborz Jahanian         E = rhsOPT->qual_end(); J != E; ++J) {
6281a8f8dac6a29f6d33474a38a32ce9dd859b696da9Fariborz Jahanian      ObjCProtocolDecl *rhsProto = *J;
6282a8f8dac6a29f6d33474a38a32ce9dd859b696da9Fariborz Jahanian      if (ProtocolCompatibleWithProtocol(lhsProto, rhsProto)) {
6283a8f8dac6a29f6d33474a38a32ce9dd859b696da9Fariborz Jahanian        match = true;
6284a8f8dac6a29f6d33474a38a32ce9dd859b696da9Fariborz Jahanian        break;
6285a8f8dac6a29f6d33474a38a32ce9dd859b696da9Fariborz Jahanian      }
6286a8f8dac6a29f6d33474a38a32ce9dd859b696da9Fariborz Jahanian    }
6287a8f8dac6a29f6d33474a38a32ce9dd859b696da9Fariborz Jahanian    if (!match)
6288a8f8dac6a29f6d33474a38a32ce9dd859b696da9Fariborz Jahanian      return false;
6289a8f8dac6a29f6d33474a38a32ce9dd859b696da9Fariborz Jahanian  }
6290a8f8dac6a29f6d33474a38a32ce9dd859b696da9Fariborz Jahanian  return true;
6291a8f8dac6a29f6d33474a38a32ce9dd859b696da9Fariborz Jahanian}
6292a8f8dac6a29f6d33474a38a32ce9dd859b696da9Fariborz Jahanian
62934084c306635b70f37029dca938444e6013f08684Steve Naroff/// ObjCQualifiedIdTypesAreCompatible - We know that one of lhs/rhs is an
62944084c306635b70f37029dca938444e6013f08684Steve Naroff/// ObjCQualifiedIDType.
62954084c306635b70f37029dca938444e6013f08684Steve Naroffbool ASTContext::ObjCQualifiedIdTypesAreCompatible(QualType lhs, QualType rhs,
62964084c306635b70f37029dca938444e6013f08684Steve Naroff                                                   bool compare) {
62974084c306635b70f37029dca938444e6013f08684Steve Naroff  // Allow id<P..> and an 'id' or void* type in all cases.
62981eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  if (lhs->isVoidPointerType() ||
62994084c306635b70f37029dca938444e6013f08684Steve Naroff      lhs->isObjCIdType() || lhs->isObjCClassType())
63004084c306635b70f37029dca938444e6013f08684Steve Naroff    return true;
63011eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  else if (rhs->isVoidPointerType() ||
63024084c306635b70f37029dca938444e6013f08684Steve Naroff           rhs->isObjCIdType() || rhs->isObjCClassType())
63034084c306635b70f37029dca938444e6013f08684Steve Naroff    return true;
63044084c306635b70f37029dca938444e6013f08684Steve Naroff
63054084c306635b70f37029dca938444e6013f08684Steve Naroff  if (const ObjCObjectPointerType *lhsQID = lhs->getAsObjCQualifiedIdType()) {
6306183700f494ec9b6701b6efe82bcb25f4c79ba561John McCall    const ObjCObjectPointerType *rhsOPT = rhs->getAs<ObjCObjectPointerType>();
63071eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
63084084c306635b70f37029dca938444e6013f08684Steve Naroff    if (!rhsOPT) return false;
63091eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
63104084c306635b70f37029dca938444e6013f08684Steve Naroff    if (rhsOPT->qual_empty()) {
63111eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      // If the RHS is a unqualified interface pointer "NSString*",
63124084c306635b70f37029dca938444e6013f08684Steve Naroff      // make sure we check the class hierarchy.
63134084c306635b70f37029dca938444e6013f08684Steve Naroff      if (ObjCInterfaceDecl *rhsID = rhsOPT->getInterfaceDecl()) {
63144084c306635b70f37029dca938444e6013f08684Steve Naroff        for (ObjCObjectPointerType::qual_iterator I = lhsQID->qual_begin(),
63154084c306635b70f37029dca938444e6013f08684Steve Naroff             E = lhsQID->qual_end(); I != E; ++I) {
63164084c306635b70f37029dca938444e6013f08684Steve Naroff          // when comparing an id<P> on lhs with a static type on rhs,
63174084c306635b70f37029dca938444e6013f08684Steve Naroff          // see if static class implements all of id's protocols, directly or
63184084c306635b70f37029dca938444e6013f08684Steve Naroff          // through its super class and categories.
63190fd8904c5f71a11d29f67716c3ebdf7ad1c855fbFariborz Jahanian          if (!rhsID->ClassImplementsProtocol(*I, true))
63204084c306635b70f37029dca938444e6013f08684Steve Naroff            return false;
63214084c306635b70f37029dca938444e6013f08684Steve Naroff        }
63224084c306635b70f37029dca938444e6013f08684Steve Naroff      }
63234084c306635b70f37029dca938444e6013f08684Steve Naroff      // If there are no qualifiers and no interface, we have an 'id'.
63244084c306635b70f37029dca938444e6013f08684Steve Naroff      return true;
63254084c306635b70f37029dca938444e6013f08684Steve Naroff    }
63261eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    // Both the right and left sides have qualifiers.
63274084c306635b70f37029dca938444e6013f08684Steve Naroff    for (ObjCObjectPointerType::qual_iterator I = lhsQID->qual_begin(),
63284084c306635b70f37029dca938444e6013f08684Steve Naroff         E = lhsQID->qual_end(); I != E; ++I) {
63294084c306635b70f37029dca938444e6013f08684Steve Naroff      ObjCProtocolDecl *lhsProto = *I;
63304084c306635b70f37029dca938444e6013f08684Steve Naroff      bool match = false;
6331de2e22d33afec98324a66a358dfe0951b3c7259aSteve Naroff
6332de2e22d33afec98324a66a358dfe0951b3c7259aSteve Naroff      // when comparing an id<P> on lhs with a static type on rhs,
6333de2e22d33afec98324a66a358dfe0951b3c7259aSteve Naroff      // see if static class implements all of id's protocols, directly or
6334de2e22d33afec98324a66a358dfe0951b3c7259aSteve Naroff      // through its super class and categories.
63354084c306635b70f37029dca938444e6013f08684Steve Naroff      for (ObjCObjectPointerType::qual_iterator J = rhsOPT->qual_begin(),
63364084c306635b70f37029dca938444e6013f08684Steve Naroff           E = rhsOPT->qual_end(); J != E; ++J) {
63374084c306635b70f37029dca938444e6013f08684Steve Naroff        ObjCProtocolDecl *rhsProto = *J;
63384084c306635b70f37029dca938444e6013f08684Steve Naroff        if (ProtocolCompatibleWithProtocol(lhsProto, rhsProto) ||
63394084c306635b70f37029dca938444e6013f08684Steve Naroff            (compare && ProtocolCompatibleWithProtocol(rhsProto, lhsProto))) {
63404084c306635b70f37029dca938444e6013f08684Steve Naroff          match = true;
63418f16756441450ed9fb39316e47d107fc2a1ef35bSteve Naroff          break;
63428f16756441450ed9fb39316e47d107fc2a1ef35bSteve Naroff        }
6343de2e22d33afec98324a66a358dfe0951b3c7259aSteve Naroff      }
63441eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      // If the RHS is a qualified interface pointer "NSString<P>*",
63454084c306635b70f37029dca938444e6013f08684Steve Naroff      // make sure we check the class hierarchy.
63464084c306635b70f37029dca938444e6013f08684Steve Naroff      if (ObjCInterfaceDecl *rhsID = rhsOPT->getInterfaceDecl()) {
63474084c306635b70f37029dca938444e6013f08684Steve Naroff        for (ObjCObjectPointerType::qual_iterator I = lhsQID->qual_begin(),
63484084c306635b70f37029dca938444e6013f08684Steve Naroff             E = lhsQID->qual_end(); I != E; ++I) {
63494084c306635b70f37029dca938444e6013f08684Steve Naroff          // when comparing an id<P> on lhs with a static type on rhs,
63504084c306635b70f37029dca938444e6013f08684Steve Naroff          // see if static class implements all of id's protocols, directly or
63514084c306635b70f37029dca938444e6013f08684Steve Naroff          // through its super class and categories.
63520fd8904c5f71a11d29f67716c3ebdf7ad1c855fbFariborz Jahanian          if (rhsID->ClassImplementsProtocol(*I, true)) {
63534084c306635b70f37029dca938444e6013f08684Steve Naroff            match = true;
63544084c306635b70f37029dca938444e6013f08684Steve Naroff            break;
63554084c306635b70f37029dca938444e6013f08684Steve Naroff          }
63564084c306635b70f37029dca938444e6013f08684Steve Naroff        }
63574084c306635b70f37029dca938444e6013f08684Steve Naroff      }
63584084c306635b70f37029dca938444e6013f08684Steve Naroff      if (!match)
6359de2e22d33afec98324a66a358dfe0951b3c7259aSteve Naroff        return false;
6360de2e22d33afec98324a66a358dfe0951b3c7259aSteve Naroff    }
63611eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
6362de2e22d33afec98324a66a358dfe0951b3c7259aSteve Naroff    return true;
6363de2e22d33afec98324a66a358dfe0951b3c7259aSteve Naroff  }
63641eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
63654084c306635b70f37029dca938444e6013f08684Steve Naroff  const ObjCObjectPointerType *rhsQID = rhs->getAsObjCQualifiedIdType();
63664084c306635b70f37029dca938444e6013f08684Steve Naroff  assert(rhsQID && "One of the LHS/RHS should be id<x>");
63674084c306635b70f37029dca938444e6013f08684Steve Naroff
63681eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  if (const ObjCObjectPointerType *lhsOPT =
63694084c306635b70f37029dca938444e6013f08684Steve Naroff        lhs->getAsObjCInterfacePointerType()) {
6370de5b17ea3c74d2844ed035a1edfa6479866139b5Fariborz Jahanian    // If both the right and left sides have qualifiers.
63714084c306635b70f37029dca938444e6013f08684Steve Naroff    for (ObjCObjectPointerType::qual_iterator I = lhsOPT->qual_begin(),
63724084c306635b70f37029dca938444e6013f08684Steve Naroff         E = lhsOPT->qual_end(); I != E; ++I) {
63734084c306635b70f37029dca938444e6013f08684Steve Naroff      ObjCProtocolDecl *lhsProto = *I;
63744084c306635b70f37029dca938444e6013f08684Steve Naroff      bool match = false;
63754084c306635b70f37029dca938444e6013f08684Steve Naroff
6376de5b17ea3c74d2844ed035a1edfa6479866139b5Fariborz Jahanian      // when comparing an id<P> on rhs with a static type on lhs,
63774084c306635b70f37029dca938444e6013f08684Steve Naroff      // see if static class implements all of id's protocols, directly or
63784084c306635b70f37029dca938444e6013f08684Steve Naroff      // through its super class and categories.
6379de5b17ea3c74d2844ed035a1edfa6479866139b5Fariborz Jahanian      // First, lhs protocols in the qualifier list must be found, direct
6380de5b17ea3c74d2844ed035a1edfa6479866139b5Fariborz Jahanian      // or indirect in rhs's qualifier list or it is a mismatch.
63814084c306635b70f37029dca938444e6013f08684Steve Naroff      for (ObjCObjectPointerType::qual_iterator J = rhsQID->qual_begin(),
63824084c306635b70f37029dca938444e6013f08684Steve Naroff           E = rhsQID->qual_end(); J != E; ++J) {
63834084c306635b70f37029dca938444e6013f08684Steve Naroff        ObjCProtocolDecl *rhsProto = *J;
63844084c306635b70f37029dca938444e6013f08684Steve Naroff        if (ProtocolCompatibleWithProtocol(lhsProto, rhsProto) ||
63854084c306635b70f37029dca938444e6013f08684Steve Naroff            (compare && ProtocolCompatibleWithProtocol(rhsProto, lhsProto))) {
63864084c306635b70f37029dca938444e6013f08684Steve Naroff          match = true;
63874084c306635b70f37029dca938444e6013f08684Steve Naroff          break;
63884084c306635b70f37029dca938444e6013f08684Steve Naroff        }
63894084c306635b70f37029dca938444e6013f08684Steve Naroff      }
63904084c306635b70f37029dca938444e6013f08684Steve Naroff      if (!match)
63914084c306635b70f37029dca938444e6013f08684Steve Naroff        return false;
63924084c306635b70f37029dca938444e6013f08684Steve Naroff    }
6393de5b17ea3c74d2844ed035a1edfa6479866139b5Fariborz Jahanian
6394de5b17ea3c74d2844ed035a1edfa6479866139b5Fariborz Jahanian    // Static class's protocols, or its super class or category protocols
6395de5b17ea3c74d2844ed035a1edfa6479866139b5Fariborz Jahanian    // must be found, direct or indirect in rhs's qualifier list or it is a mismatch.
6396de5b17ea3c74d2844ed035a1edfa6479866139b5Fariborz Jahanian    if (ObjCInterfaceDecl *lhsID = lhsOPT->getInterfaceDecl()) {
6397de5b17ea3c74d2844ed035a1edfa6479866139b5Fariborz Jahanian      llvm::SmallPtrSet<ObjCProtocolDecl *, 8> LHSInheritedProtocols;
6398de5b17ea3c74d2844ed035a1edfa6479866139b5Fariborz Jahanian      CollectInheritedProtocols(lhsID, LHSInheritedProtocols);
6399de5b17ea3c74d2844ed035a1edfa6479866139b5Fariborz Jahanian      // This is rather dubious but matches gcc's behavior. If lhs has
6400de5b17ea3c74d2844ed035a1edfa6479866139b5Fariborz Jahanian      // no type qualifier and its class has no static protocol(s)
6401de5b17ea3c74d2844ed035a1edfa6479866139b5Fariborz Jahanian      // assume that it is mismatch.
6402de5b17ea3c74d2844ed035a1edfa6479866139b5Fariborz Jahanian      if (LHSInheritedProtocols.empty() && lhsOPT->qual_empty())
6403de5b17ea3c74d2844ed035a1edfa6479866139b5Fariborz Jahanian        return false;
6404de5b17ea3c74d2844ed035a1edfa6479866139b5Fariborz Jahanian      for (llvm::SmallPtrSet<ObjCProtocolDecl*,8>::iterator I =
6405de5b17ea3c74d2844ed035a1edfa6479866139b5Fariborz Jahanian           LHSInheritedProtocols.begin(),
6406de5b17ea3c74d2844ed035a1edfa6479866139b5Fariborz Jahanian           E = LHSInheritedProtocols.end(); I != E; ++I) {
6407de5b17ea3c74d2844ed035a1edfa6479866139b5Fariborz Jahanian        bool match = false;
6408de5b17ea3c74d2844ed035a1edfa6479866139b5Fariborz Jahanian        ObjCProtocolDecl *lhsProto = (*I);
6409de5b17ea3c74d2844ed035a1edfa6479866139b5Fariborz Jahanian        for (ObjCObjectPointerType::qual_iterator J = rhsQID->qual_begin(),
6410de5b17ea3c74d2844ed035a1edfa6479866139b5Fariborz Jahanian             E = rhsQID->qual_end(); J != E; ++J) {
6411de5b17ea3c74d2844ed035a1edfa6479866139b5Fariborz Jahanian          ObjCProtocolDecl *rhsProto = *J;
6412de5b17ea3c74d2844ed035a1edfa6479866139b5Fariborz Jahanian          if (ProtocolCompatibleWithProtocol(lhsProto, rhsProto) ||
6413de5b17ea3c74d2844ed035a1edfa6479866139b5Fariborz Jahanian              (compare && ProtocolCompatibleWithProtocol(rhsProto, lhsProto))) {
6414de5b17ea3c74d2844ed035a1edfa6479866139b5Fariborz Jahanian            match = true;
6415de5b17ea3c74d2844ed035a1edfa6479866139b5Fariborz Jahanian            break;
6416de5b17ea3c74d2844ed035a1edfa6479866139b5Fariborz Jahanian          }
6417de5b17ea3c74d2844ed035a1edfa6479866139b5Fariborz Jahanian        }
6418de5b17ea3c74d2844ed035a1edfa6479866139b5Fariborz Jahanian        if (!match)
6419de5b17ea3c74d2844ed035a1edfa6479866139b5Fariborz Jahanian          return false;
6420de5b17ea3c74d2844ed035a1edfa6479866139b5Fariborz Jahanian      }
6421de5b17ea3c74d2844ed035a1edfa6479866139b5Fariborz Jahanian    }
64224084c306635b70f37029dca938444e6013f08684Steve Naroff    return true;
64234084c306635b70f37029dca938444e6013f08684Steve Naroff  }
64244084c306635b70f37029dca938444e6013f08684Steve Naroff  return false;
64254084c306635b70f37029dca938444e6013f08684Steve Naroff}
64264084c306635b70f37029dca938444e6013f08684Steve Naroff
64274084c306635b70f37029dca938444e6013f08684Steve Naroff/// canAssignObjCInterfaces - Return true if the two interface types are
64284084c306635b70f37029dca938444e6013f08684Steve Naroff/// compatible for assignment from RHS to LHS.  This handles validation of any
64294084c306635b70f37029dca938444e6013f08684Steve Naroff/// protocol qualifiers on the LHS or RHS.
64304084c306635b70f37029dca938444e6013f08684Steve Naroff///
64314084c306635b70f37029dca938444e6013f08684Steve Naroffbool ASTContext::canAssignObjCInterfaces(const ObjCObjectPointerType *LHSOPT,
64324084c306635b70f37029dca938444e6013f08684Steve Naroff                                         const ObjCObjectPointerType *RHSOPT) {
6433c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall  const ObjCObjectType* LHS = LHSOPT->getObjectType();
6434c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall  const ObjCObjectType* RHS = RHSOPT->getObjectType();
6435c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall
64364084c306635b70f37029dca938444e6013f08684Steve Naroff  // If either type represents the built-in 'id' or 'Class' types, return true.
6437c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall  if (LHS->isObjCUnqualifiedIdOrClass() ||
6438c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall      RHS->isObjCUnqualifiedIdOrClass())
64394084c306635b70f37029dca938444e6013f08684Steve Naroff    return true;
64404084c306635b70f37029dca938444e6013f08684Steve Naroff
6441c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall  if (LHS->isObjCQualifiedId() || RHS->isObjCQualifiedId())
64421eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    return ObjCQualifiedIdTypesAreCompatible(QualType(LHSOPT,0),
64431eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump                                             QualType(RHSOPT,0),
64444084c306635b70f37029dca938444e6013f08684Steve Naroff                                             false);
6445a8f8dac6a29f6d33474a38a32ce9dd859b696da9Fariborz Jahanian
6446a8f8dac6a29f6d33474a38a32ce9dd859b696da9Fariborz Jahanian  if (LHS->isObjCQualifiedClass() && RHS->isObjCQualifiedClass())
6447a8f8dac6a29f6d33474a38a32ce9dd859b696da9Fariborz Jahanian    return ObjCQualifiedClassTypesAreCompatible(QualType(LHSOPT,0),
6448a8f8dac6a29f6d33474a38a32ce9dd859b696da9Fariborz Jahanian                                                QualType(RHSOPT,0));
6449a8f8dac6a29f6d33474a38a32ce9dd859b696da9Fariborz Jahanian
6450c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall  // If we have 2 user-defined types, fall into that path.
6451c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall  if (LHS->getInterface() && RHS->getInterface())
64524084c306635b70f37029dca938444e6013f08684Steve Naroff    return canAssignObjCInterfaces(LHS, RHS);
64531eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
64544084c306635b70f37029dca938444e6013f08684Steve Naroff  return false;
645514108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff}
645614108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff
6457132f2a2da34f378fc675b9e174564b0f52c31d98Fariborz Jahanian/// canAssignObjCInterfacesInBlockPointer - This routine is specifically written
6458fc8f0e14ad142ed811e90fbd9a30e419e301c717Chris Lattner/// for providing type-safety for objective-c pointers used to pass/return
6459132f2a2da34f378fc675b9e174564b0f52c31d98Fariborz Jahanian/// arguments in block literals. When passed as arguments, passing 'A*' where
6460132f2a2da34f378fc675b9e174564b0f52c31d98Fariborz Jahanian/// 'id' is expected is not OK. Passing 'Sub *" where 'Super *" is expected is
6461132f2a2da34f378fc675b9e174564b0f52c31d98Fariborz Jahanian/// not OK. For the return type, the opposite is not OK.
6462132f2a2da34f378fc675b9e174564b0f52c31d98Fariborz Jahanianbool ASTContext::canAssignObjCInterfacesInBlockPointer(
6463132f2a2da34f378fc675b9e174564b0f52c31d98Fariborz Jahanian                                         const ObjCObjectPointerType *LHSOPT,
6464a4fdbfad150ae37bddaa4094d3925a27a1a1cf3fFariborz Jahanian                                         const ObjCObjectPointerType *RHSOPT,
6465a4fdbfad150ae37bddaa4094d3925a27a1a1cf3fFariborz Jahanian                                         bool BlockReturnType) {
6466a98344869c278295f38e106583906377520079b8Fariborz Jahanian  if (RHSOPT->isObjCBuiltinType() || LHSOPT->isObjCIdType())
6467132f2a2da34f378fc675b9e174564b0f52c31d98Fariborz Jahanian    return true;
6468132f2a2da34f378fc675b9e174564b0f52c31d98Fariborz Jahanian
6469132f2a2da34f378fc675b9e174564b0f52c31d98Fariborz Jahanian  if (LHSOPT->isObjCBuiltinType()) {
6470132f2a2da34f378fc675b9e174564b0f52c31d98Fariborz Jahanian    return RHSOPT->isObjCBuiltinType() || RHSOPT->isObjCQualifiedIdType();
6471132f2a2da34f378fc675b9e174564b0f52c31d98Fariborz Jahanian  }
6472132f2a2da34f378fc675b9e174564b0f52c31d98Fariborz Jahanian
6473a98344869c278295f38e106583906377520079b8Fariborz Jahanian  if (LHSOPT->isObjCQualifiedIdType() || RHSOPT->isObjCQualifiedIdType())
6474132f2a2da34f378fc675b9e174564b0f52c31d98Fariborz Jahanian    return ObjCQualifiedIdTypesAreCompatible(QualType(LHSOPT,0),
6475132f2a2da34f378fc675b9e174564b0f52c31d98Fariborz Jahanian                                             QualType(RHSOPT,0),
6476132f2a2da34f378fc675b9e174564b0f52c31d98Fariborz Jahanian                                             false);
6477132f2a2da34f378fc675b9e174564b0f52c31d98Fariborz Jahanian
6478132f2a2da34f378fc675b9e174564b0f52c31d98Fariborz Jahanian  const ObjCInterfaceType* LHS = LHSOPT->getInterfaceType();
6479132f2a2da34f378fc675b9e174564b0f52c31d98Fariborz Jahanian  const ObjCInterfaceType* RHS = RHSOPT->getInterfaceType();
6480132f2a2da34f378fc675b9e174564b0f52c31d98Fariborz Jahanian  if (LHS && RHS)  { // We have 2 user-defined types.
6481132f2a2da34f378fc675b9e174564b0f52c31d98Fariborz Jahanian    if (LHS != RHS) {
6482132f2a2da34f378fc675b9e174564b0f52c31d98Fariborz Jahanian      if (LHS->getDecl()->isSuperClassOf(RHS->getDecl()))
6483a4fdbfad150ae37bddaa4094d3925a27a1a1cf3fFariborz Jahanian        return BlockReturnType;
6484132f2a2da34f378fc675b9e174564b0f52c31d98Fariborz Jahanian      if (RHS->getDecl()->isSuperClassOf(LHS->getDecl()))
6485a4fdbfad150ae37bddaa4094d3925a27a1a1cf3fFariborz Jahanian        return !BlockReturnType;
6486132f2a2da34f378fc675b9e174564b0f52c31d98Fariborz Jahanian    }
6487132f2a2da34f378fc675b9e174564b0f52c31d98Fariborz Jahanian    else
6488132f2a2da34f378fc675b9e174564b0f52c31d98Fariborz Jahanian      return true;
6489132f2a2da34f378fc675b9e174564b0f52c31d98Fariborz Jahanian  }
6490132f2a2da34f378fc675b9e174564b0f52c31d98Fariborz Jahanian  return false;
6491132f2a2da34f378fc675b9e174564b0f52c31d98Fariborz Jahanian}
6492132f2a2da34f378fc675b9e174564b0f52c31d98Fariborz Jahanian
6493e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanian/// getIntersectionOfProtocols - This routine finds the intersection of set
6494e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanian/// of protocols inherited from two distinct objective-c pointer objects.
6495e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanian/// It is used to build composite qualifier list of the composite type of
6496e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanian/// the conditional expression involving two objective-c pointer objects.
6497e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanianstatic
6498e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanianvoid getIntersectionOfProtocols(ASTContext &Context,
6499e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanian                                const ObjCObjectPointerType *LHSOPT,
6500e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanian                                const ObjCObjectPointerType *RHSOPT,
65015f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner      SmallVectorImpl<ObjCProtocolDecl *> &IntersectionOfProtocols) {
6502e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanian
6503c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall  const ObjCObjectType* LHS = LHSOPT->getObjectType();
6504c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall  const ObjCObjectType* RHS = RHSOPT->getObjectType();
6505c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall  assert(LHS->getInterface() && "LHS must have an interface base");
6506c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall  assert(RHS->getInterface() && "RHS must have an interface base");
6507e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanian
6508e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanian  llvm::SmallPtrSet<ObjCProtocolDecl *, 8> InheritedProtocolSet;
6509e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanian  unsigned LHSNumProtocols = LHS->getNumProtocols();
6510e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanian  if (LHSNumProtocols > 0)
6511e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanian    InheritedProtocolSet.insert(LHS->qual_begin(), LHS->qual_end());
6512e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanian  else {
6513432a8893f7e30d141d7f279bd00b741a3cdac81fFariborz Jahanian    llvm::SmallPtrSet<ObjCProtocolDecl *, 8> LHSInheritedProtocols;
6514c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall    Context.CollectInheritedProtocols(LHS->getInterface(),
6515c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall                                      LHSInheritedProtocols);
6516e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanian    InheritedProtocolSet.insert(LHSInheritedProtocols.begin(),
6517e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanian                                LHSInheritedProtocols.end());
6518e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanian  }
6519e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanian
6520e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanian  unsigned RHSNumProtocols = RHS->getNumProtocols();
6521e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanian  if (RHSNumProtocols > 0) {
6522cb421fa690da545b58a720abe5f1c49b166dbde7Dan Gohman    ObjCProtocolDecl **RHSProtocols =
6523cb421fa690da545b58a720abe5f1c49b166dbde7Dan Gohman      const_cast<ObjCProtocolDecl **>(RHS->qual_begin());
6524e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanian    for (unsigned i = 0; i < RHSNumProtocols; ++i)
6525e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanian      if (InheritedProtocolSet.count(RHSProtocols[i]))
6526e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanian        IntersectionOfProtocols.push_back(RHSProtocols[i]);
65273060178ad9df29789505c1e6debcfc80a3a13587Chad Rosier  } else {
6528432a8893f7e30d141d7f279bd00b741a3cdac81fFariborz Jahanian    llvm::SmallPtrSet<ObjCProtocolDecl *, 8> RHSInheritedProtocols;
6529c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall    Context.CollectInheritedProtocols(RHS->getInterface(),
6530c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall                                      RHSInheritedProtocols);
6531432a8893f7e30d141d7f279bd00b741a3cdac81fFariborz Jahanian    for (llvm::SmallPtrSet<ObjCProtocolDecl*,8>::iterator I =
6532432a8893f7e30d141d7f279bd00b741a3cdac81fFariborz Jahanian         RHSInheritedProtocols.begin(),
6533432a8893f7e30d141d7f279bd00b741a3cdac81fFariborz Jahanian         E = RHSInheritedProtocols.end(); I != E; ++I)
6534432a8893f7e30d141d7f279bd00b741a3cdac81fFariborz Jahanian      if (InheritedProtocolSet.count((*I)))
6535432a8893f7e30d141d7f279bd00b741a3cdac81fFariborz Jahanian        IntersectionOfProtocols.push_back((*I));
6536e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanian  }
6537e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanian}
6538e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanian
6539db07b3f7cdcb505329c1280d7cf70791739a7cadFariborz Jahanian/// areCommonBaseCompatible - Returns common base class of the two classes if
6540db07b3f7cdcb505329c1280d7cf70791739a7cadFariborz Jahanian/// one found. Note that this is O'2 algorithm. But it will be called as the
6541db07b3f7cdcb505329c1280d7cf70791739a7cadFariborz Jahanian/// last type comparison in a ?-exp of ObjC pointer types before a
6542db07b3f7cdcb505329c1280d7cf70791739a7cadFariborz Jahanian/// warning is issued. So, its invokation is extremely rare.
6543db07b3f7cdcb505329c1280d7cf70791739a7cadFariborz JahanianQualType ASTContext::areCommonBaseCompatible(
6544c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall                                          const ObjCObjectPointerType *Lptr,
6545c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall                                          const ObjCObjectPointerType *Rptr) {
6546c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall  const ObjCObjectType *LHS = Lptr->getObjectType();
6547c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall  const ObjCObjectType *RHS = Rptr->getObjectType();
6548c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall  const ObjCInterfaceDecl* LDecl = LHS->getInterface();
6549c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall  const ObjCInterfaceDecl* RDecl = RHS->getInterface();
655060ef308e51c71b760d7f598c1b763ceb7b768148Douglas Gregor  if (!LDecl || !RDecl || (declaresSameEntity(LDecl, RDecl)))
6551db07b3f7cdcb505329c1280d7cf70791739a7cadFariborz Jahanian    return QualType();
6552db07b3f7cdcb505329c1280d7cf70791739a7cadFariborz Jahanian
65537c2bdcb4d30f2d370b4367664e6a11b075ce2cb3Fariborz Jahanian  do {
6554c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall    LHS = cast<ObjCInterfaceType>(getObjCInterfaceType(LDecl));
6555e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanian    if (canAssignObjCInterfaces(LHS, RHS)) {
65565f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner      SmallVector<ObjCProtocolDecl *, 8> Protocols;
6557c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall      getIntersectionOfProtocols(*this, Lptr, Rptr, Protocols);
6558c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall
6559c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall      QualType Result = QualType(LHS, 0);
6560c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall      if (!Protocols.empty())
6561c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall        Result = getObjCObjectType(Result, Protocols.data(), Protocols.size());
6562c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall      Result = getObjCObjectPointerType(Result);
6563c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall      return Result;
6564e23fa2d0e84d1b878e012442a726c664216a9adfFariborz Jahanian    }
65657c2bdcb4d30f2d370b4367664e6a11b075ce2cb3Fariborz Jahanian  } while ((LDecl = LDecl->getSuperClass()));
6566db07b3f7cdcb505329c1280d7cf70791739a7cadFariborz Jahanian
6567db07b3f7cdcb505329c1280d7cf70791739a7cadFariborz Jahanian  return QualType();
6568db07b3f7cdcb505329c1280d7cf70791739a7cadFariborz Jahanian}
6569db07b3f7cdcb505329c1280d7cf70791739a7cadFariborz Jahanian
6570c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCallbool ASTContext::canAssignObjCInterfaces(const ObjCObjectType *LHS,
6571c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall                                         const ObjCObjectType *RHS) {
6572c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall  assert(LHS->getInterface() && "LHS is not an interface type");
6573c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall  assert(RHS->getInterface() && "RHS is not an interface type");
6574c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall
65756ac46a4a521366d7ab36ebe2ce4e624ab96b06f9Chris Lattner  // Verify that the base decls are compatible: the RHS must be a subclass of
65766ac46a4a521366d7ab36ebe2ce4e624ab96b06f9Chris Lattner  // the LHS.
6577c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall  if (!LHS->getInterface()->isSuperClassOf(RHS->getInterface()))
65786ac46a4a521366d7ab36ebe2ce4e624ab96b06f9Chris Lattner    return false;
65791eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
65806ac46a4a521366d7ab36ebe2ce4e624ab96b06f9Chris Lattner  // RHS must have a superset of the protocols in the LHS.  If the LHS is not
65816ac46a4a521366d7ab36ebe2ce4e624ab96b06f9Chris Lattner  // protocol qualified at all, then we are good.
6582c15cb2af27514ecc879daba9aa01389c5203685dSteve Naroff  if (LHS->getNumProtocols() == 0)
65836ac46a4a521366d7ab36ebe2ce4e624ab96b06f9Chris Lattner    return true;
65841eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
6585b7bc34a83aff8af09f2a78aa6d1dcafe18ad8619Fariborz Jahanian  // Okay, we know the LHS has protocol qualifiers.  If the RHS doesn't,
6586b7bc34a83aff8af09f2a78aa6d1dcafe18ad8619Fariborz Jahanian  // more detailed analysis is required.
6587b7bc34a83aff8af09f2a78aa6d1dcafe18ad8619Fariborz Jahanian  if (RHS->getNumProtocols() == 0) {
6588b7bc34a83aff8af09f2a78aa6d1dcafe18ad8619Fariborz Jahanian    // OK, if LHS is a superclass of RHS *and*
6589b7bc34a83aff8af09f2a78aa6d1dcafe18ad8619Fariborz Jahanian    // this superclass is assignment compatible with LHS.
6590b7bc34a83aff8af09f2a78aa6d1dcafe18ad8619Fariborz Jahanian    // false otherwise.
6591627788c29976fbeb4ad47bcfcb3576889070e357Fariborz Jahanian    bool IsSuperClass =
6592627788c29976fbeb4ad47bcfcb3576889070e357Fariborz Jahanian      LHS->getInterface()->isSuperClassOf(RHS->getInterface());
6593627788c29976fbeb4ad47bcfcb3576889070e357Fariborz Jahanian    if (IsSuperClass) {
6594b7bc34a83aff8af09f2a78aa6d1dcafe18ad8619Fariborz Jahanian      // OK if conversion of LHS to SuperClass results in narrowing of types
6595b7bc34a83aff8af09f2a78aa6d1dcafe18ad8619Fariborz Jahanian      // ; i.e., SuperClass may implement at least one of the protocols
6596b7bc34a83aff8af09f2a78aa6d1dcafe18ad8619Fariborz Jahanian      // in LHS's protocol list. Example, SuperObj<P1> = lhs<P1,P2> is ok.
6597b7bc34a83aff8af09f2a78aa6d1dcafe18ad8619Fariborz Jahanian      // But not SuperObj<P1,P2,P3> = lhs<P1,P2>.
6598b7bc34a83aff8af09f2a78aa6d1dcafe18ad8619Fariborz Jahanian      llvm::SmallPtrSet<ObjCProtocolDecl *, 8> SuperClassInheritedProtocols;
6599627788c29976fbeb4ad47bcfcb3576889070e357Fariborz Jahanian      CollectInheritedProtocols(RHS->getInterface(), SuperClassInheritedProtocols);
6600b7bc34a83aff8af09f2a78aa6d1dcafe18ad8619Fariborz Jahanian      // If super class has no protocols, it is not a match.
6601b7bc34a83aff8af09f2a78aa6d1dcafe18ad8619Fariborz Jahanian      if (SuperClassInheritedProtocols.empty())
6602b7bc34a83aff8af09f2a78aa6d1dcafe18ad8619Fariborz Jahanian        return false;
6603b7bc34a83aff8af09f2a78aa6d1dcafe18ad8619Fariborz Jahanian
6604b7bc34a83aff8af09f2a78aa6d1dcafe18ad8619Fariborz Jahanian      for (ObjCObjectType::qual_iterator LHSPI = LHS->qual_begin(),
6605b7bc34a83aff8af09f2a78aa6d1dcafe18ad8619Fariborz Jahanian           LHSPE = LHS->qual_end();
6606b7bc34a83aff8af09f2a78aa6d1dcafe18ad8619Fariborz Jahanian           LHSPI != LHSPE; LHSPI++) {
6607b7bc34a83aff8af09f2a78aa6d1dcafe18ad8619Fariborz Jahanian        bool SuperImplementsProtocol = false;
6608b7bc34a83aff8af09f2a78aa6d1dcafe18ad8619Fariborz Jahanian        ObjCProtocolDecl *LHSProto = (*LHSPI);
6609b7bc34a83aff8af09f2a78aa6d1dcafe18ad8619Fariborz Jahanian
6610b7bc34a83aff8af09f2a78aa6d1dcafe18ad8619Fariborz Jahanian        for (llvm::SmallPtrSet<ObjCProtocolDecl*,8>::iterator I =
6611b7bc34a83aff8af09f2a78aa6d1dcafe18ad8619Fariborz Jahanian             SuperClassInheritedProtocols.begin(),
6612b7bc34a83aff8af09f2a78aa6d1dcafe18ad8619Fariborz Jahanian             E = SuperClassInheritedProtocols.end(); I != E; ++I) {
6613b7bc34a83aff8af09f2a78aa6d1dcafe18ad8619Fariborz Jahanian          ObjCProtocolDecl *SuperClassProto = (*I);
6614b7bc34a83aff8af09f2a78aa6d1dcafe18ad8619Fariborz Jahanian          if (SuperClassProto->lookupProtocolNamed(LHSProto->getIdentifier())) {
6615b7bc34a83aff8af09f2a78aa6d1dcafe18ad8619Fariborz Jahanian            SuperImplementsProtocol = true;
6616b7bc34a83aff8af09f2a78aa6d1dcafe18ad8619Fariborz Jahanian            break;
6617b7bc34a83aff8af09f2a78aa6d1dcafe18ad8619Fariborz Jahanian          }
6618b7bc34a83aff8af09f2a78aa6d1dcafe18ad8619Fariborz Jahanian        }
6619b7bc34a83aff8af09f2a78aa6d1dcafe18ad8619Fariborz Jahanian        if (!SuperImplementsProtocol)
6620b7bc34a83aff8af09f2a78aa6d1dcafe18ad8619Fariborz Jahanian          return false;
6621b7bc34a83aff8af09f2a78aa6d1dcafe18ad8619Fariborz Jahanian      }
6622b7bc34a83aff8af09f2a78aa6d1dcafe18ad8619Fariborz Jahanian      return true;
6623b7bc34a83aff8af09f2a78aa6d1dcafe18ad8619Fariborz Jahanian    }
6624b7bc34a83aff8af09f2a78aa6d1dcafe18ad8619Fariborz Jahanian    return false;
6625b7bc34a83aff8af09f2a78aa6d1dcafe18ad8619Fariborz Jahanian  }
66261eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
6627c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall  for (ObjCObjectType::qual_iterator LHSPI = LHS->qual_begin(),
6628c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall                                     LHSPE = LHS->qual_end();
662991b0b0cf6b537cbcbca0038c7032f87161a41d31Steve Naroff       LHSPI != LHSPE; LHSPI++) {
663091b0b0cf6b537cbcbca0038c7032f87161a41d31Steve Naroff    bool RHSImplementsProtocol = false;
663191b0b0cf6b537cbcbca0038c7032f87161a41d31Steve Naroff
663291b0b0cf6b537cbcbca0038c7032f87161a41d31Steve Naroff    // If the RHS doesn't implement the protocol on the left, the types
663391b0b0cf6b537cbcbca0038c7032f87161a41d31Steve Naroff    // are incompatible.
6634c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall    for (ObjCObjectType::qual_iterator RHSPI = RHS->qual_begin(),
6635c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall                                       RHSPE = RHS->qual_end();
66368f16756441450ed9fb39316e47d107fc2a1ef35bSteve Naroff         RHSPI != RHSPE; RHSPI++) {
66378f16756441450ed9fb39316e47d107fc2a1ef35bSteve Naroff      if ((*RHSPI)->lookupProtocolNamed((*LHSPI)->getIdentifier())) {
663891b0b0cf6b537cbcbca0038c7032f87161a41d31Steve Naroff        RHSImplementsProtocol = true;
66398f16756441450ed9fb39316e47d107fc2a1ef35bSteve Naroff        break;
66408f16756441450ed9fb39316e47d107fc2a1ef35bSteve Naroff      }
664191b0b0cf6b537cbcbca0038c7032f87161a41d31Steve Naroff    }
664291b0b0cf6b537cbcbca0038c7032f87161a41d31Steve Naroff    // FIXME: For better diagnostics, consider passing back the protocol name.
664391b0b0cf6b537cbcbca0038c7032f87161a41d31Steve Naroff    if (!RHSImplementsProtocol)
664491b0b0cf6b537cbcbca0038c7032f87161a41d31Steve Naroff      return false;
664591b0b0cf6b537cbcbca0038c7032f87161a41d31Steve Naroff  }
664691b0b0cf6b537cbcbca0038c7032f87161a41d31Steve Naroff  // The RHS implements all protocols listed on the LHS.
664791b0b0cf6b537cbcbca0038c7032f87161a41d31Steve Naroff  return true;
66486ac46a4a521366d7ab36ebe2ce4e624ab96b06f9Chris Lattner}
66496ac46a4a521366d7ab36ebe2ce4e624ab96b06f9Chris Lattner
6650389bf46ae41241a656ed71b00ac2177d7f385651Steve Naroffbool ASTContext::areComparableObjCPointerTypes(QualType LHS, QualType RHS) {
6651389bf46ae41241a656ed71b00ac2177d7f385651Steve Naroff  // get the "pointed to" types
6652183700f494ec9b6701b6efe82bcb25f4c79ba561John McCall  const ObjCObjectPointerType *LHSOPT = LHS->getAs<ObjCObjectPointerType>();
6653183700f494ec9b6701b6efe82bcb25f4c79ba561John McCall  const ObjCObjectPointerType *RHSOPT = RHS->getAs<ObjCObjectPointerType>();
66541eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
665514108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff  if (!LHSOPT || !RHSOPT)
6656389bf46ae41241a656ed71b00ac2177d7f385651Steve Naroff    return false;
665714108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff
665814108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff  return canAssignObjCInterfaces(LHSOPT, RHSOPT) ||
665914108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff         canAssignObjCInterfaces(RHSOPT, LHSOPT);
6660389bf46ae41241a656ed71b00ac2177d7f385651Steve Naroff}
6661389bf46ae41241a656ed71b00ac2177d7f385651Steve Naroff
6662569c3166874324c24011f8ade6978421f0d39b3cDouglas Gregorbool ASTContext::canBindObjCObjectType(QualType To, QualType From) {
6663569c3166874324c24011f8ade6978421f0d39b3cDouglas Gregor  return canAssignObjCInterfaces(
6664569c3166874324c24011f8ade6978421f0d39b3cDouglas Gregor                getObjCObjectPointerType(To)->getAs<ObjCObjectPointerType>(),
6665569c3166874324c24011f8ade6978421f0d39b3cDouglas Gregor                getObjCObjectPointerType(From)->getAs<ObjCObjectPointerType>());
6666569c3166874324c24011f8ade6978421f0d39b3cDouglas Gregor}
6667569c3166874324c24011f8ade6978421f0d39b3cDouglas Gregor
66681eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// typesAreCompatible - C99 6.7.3p9: For two qualified types to be compatible,
6669ec0550fa3653d46560bf4484a2e988329c228e39Steve Naroff/// both shall have the identically qualified version of a compatible type.
66701eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// C99 6.2.7p1: Two types have compatible types if their types are the
6671ec0550fa3653d46560bf4484a2e988329c228e39Steve Naroff/// same. See 6.7.[2,3,5] for additional rules.
6672447234dd459a00a5ed9b7c3e066162cd7a75bf2dDouglas Gregorbool ASTContext::typesAreCompatible(QualType LHS, QualType RHS,
6673447234dd459a00a5ed9b7c3e066162cd7a75bf2dDouglas Gregor                                    bool CompareUnqualified) {
66744e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie  if (getLangOpts().CPlusPlus)
66750e709abafbd939326850501f795cc7a92c88a354Douglas Gregor    return hasSameType(LHS, RHS);
66760e709abafbd939326850501f795cc7a92c88a354Douglas Gregor
6677447234dd459a00a5ed9b7c3e066162cd7a75bf2dDouglas Gregor  return !mergeTypes(LHS, RHS, false, CompareUnqualified).isNull();
66783d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman}
66793d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman
6680c286f3835eb6001c61664cef5d610dfaf80a6e9bFariborz Jahanianbool ASTContext::propertyTypesAreCompatible(QualType LHS, QualType RHS) {
668182378391dfc3b2fc22f63dbf1552bd9aed39dc42Fariborz Jahanian  return typesAreCompatible(LHS, RHS);
6682c286f3835eb6001c61664cef5d610dfaf80a6e9bFariborz Jahanian}
6683c286f3835eb6001c61664cef5d610dfaf80a6e9bFariborz Jahanian
6684132f2a2da34f378fc675b9e174564b0f52c31d98Fariborz Jahanianbool ASTContext::typesAreBlockPointerCompatible(QualType LHS, QualType RHS) {
6685132f2a2da34f378fc675b9e174564b0f52c31d98Fariborz Jahanian  return !mergeTypes(LHS, RHS, true).isNull();
6686132f2a2da34f378fc675b9e174564b0f52c31d98Fariborz Jahanian}
6687132f2a2da34f378fc675b9e174564b0f52c31d98Fariborz Jahanian
66884846675e0e42d1802b0ffd8972a45e72aeb3758dPeter Collingbourne/// mergeTransparentUnionType - if T is a transparent union type and a member
66894846675e0e42d1802b0ffd8972a45e72aeb3758dPeter Collingbourne/// of T is compatible with SubType, return the merged type, else return
66904846675e0e42d1802b0ffd8972a45e72aeb3758dPeter Collingbourne/// QualType()
66914846675e0e42d1802b0ffd8972a45e72aeb3758dPeter CollingbourneQualType ASTContext::mergeTransparentUnionType(QualType T, QualType SubType,
66924846675e0e42d1802b0ffd8972a45e72aeb3758dPeter Collingbourne                                               bool OfBlockPointer,
66934846675e0e42d1802b0ffd8972a45e72aeb3758dPeter Collingbourne                                               bool Unqualified) {
66944846675e0e42d1802b0ffd8972a45e72aeb3758dPeter Collingbourne  if (const RecordType *UT = T->getAsUnionType()) {
66954846675e0e42d1802b0ffd8972a45e72aeb3758dPeter Collingbourne    RecordDecl *UD = UT->getDecl();
66964846675e0e42d1802b0ffd8972a45e72aeb3758dPeter Collingbourne    if (UD->hasAttr<TransparentUnionAttr>()) {
66974846675e0e42d1802b0ffd8972a45e72aeb3758dPeter Collingbourne      for (RecordDecl::field_iterator it = UD->field_begin(),
66984846675e0e42d1802b0ffd8972a45e72aeb3758dPeter Collingbourne           itend = UD->field_end(); it != itend; ++it) {
6699f91d7572ee7fa6f2926c8e7e816039a1154a59f8Peter Collingbourne        QualType ET = it->getType().getUnqualifiedType();
67004846675e0e42d1802b0ffd8972a45e72aeb3758dPeter Collingbourne        QualType MT = mergeTypes(ET, SubType, OfBlockPointer, Unqualified);
67014846675e0e42d1802b0ffd8972a45e72aeb3758dPeter Collingbourne        if (!MT.isNull())
67024846675e0e42d1802b0ffd8972a45e72aeb3758dPeter Collingbourne          return MT;
67034846675e0e42d1802b0ffd8972a45e72aeb3758dPeter Collingbourne      }
67044846675e0e42d1802b0ffd8972a45e72aeb3758dPeter Collingbourne    }
67054846675e0e42d1802b0ffd8972a45e72aeb3758dPeter Collingbourne  }
67064846675e0e42d1802b0ffd8972a45e72aeb3758dPeter Collingbourne
67074846675e0e42d1802b0ffd8972a45e72aeb3758dPeter Collingbourne  return QualType();
67084846675e0e42d1802b0ffd8972a45e72aeb3758dPeter Collingbourne}
67094846675e0e42d1802b0ffd8972a45e72aeb3758dPeter Collingbourne
67104846675e0e42d1802b0ffd8972a45e72aeb3758dPeter Collingbourne/// mergeFunctionArgumentTypes - merge two types which appear as function
67114846675e0e42d1802b0ffd8972a45e72aeb3758dPeter Collingbourne/// argument types
67124846675e0e42d1802b0ffd8972a45e72aeb3758dPeter CollingbourneQualType ASTContext::mergeFunctionArgumentTypes(QualType lhs, QualType rhs,
67134846675e0e42d1802b0ffd8972a45e72aeb3758dPeter Collingbourne                                                bool OfBlockPointer,
67144846675e0e42d1802b0ffd8972a45e72aeb3758dPeter Collingbourne                                                bool Unqualified) {
67154846675e0e42d1802b0ffd8972a45e72aeb3758dPeter Collingbourne  // GNU extension: two types are compatible if they appear as a function
67164846675e0e42d1802b0ffd8972a45e72aeb3758dPeter Collingbourne  // argument, one of the types is a transparent union type and the other
67174846675e0e42d1802b0ffd8972a45e72aeb3758dPeter Collingbourne  // type is compatible with a union member
67184846675e0e42d1802b0ffd8972a45e72aeb3758dPeter Collingbourne  QualType lmerge = mergeTransparentUnionType(lhs, rhs, OfBlockPointer,
67194846675e0e42d1802b0ffd8972a45e72aeb3758dPeter Collingbourne                                              Unqualified);
67204846675e0e42d1802b0ffd8972a45e72aeb3758dPeter Collingbourne  if (!lmerge.isNull())
67214846675e0e42d1802b0ffd8972a45e72aeb3758dPeter Collingbourne    return lmerge;
67224846675e0e42d1802b0ffd8972a45e72aeb3758dPeter Collingbourne
67234846675e0e42d1802b0ffd8972a45e72aeb3758dPeter Collingbourne  QualType rmerge = mergeTransparentUnionType(rhs, lhs, OfBlockPointer,
67244846675e0e42d1802b0ffd8972a45e72aeb3758dPeter Collingbourne                                              Unqualified);
67254846675e0e42d1802b0ffd8972a45e72aeb3758dPeter Collingbourne  if (!rmerge.isNull())
67264846675e0e42d1802b0ffd8972a45e72aeb3758dPeter Collingbourne    return rmerge;
67274846675e0e42d1802b0ffd8972a45e72aeb3758dPeter Collingbourne
67284846675e0e42d1802b0ffd8972a45e72aeb3758dPeter Collingbourne  return mergeTypes(lhs, rhs, OfBlockPointer, Unqualified);
67294846675e0e42d1802b0ffd8972a45e72aeb3758dPeter Collingbourne}
67304846675e0e42d1802b0ffd8972a45e72aeb3758dPeter Collingbourne
6731132f2a2da34f378fc675b9e174564b0f52c31d98Fariborz JahanianQualType ASTContext::mergeFunctionTypes(QualType lhs, QualType rhs,
6732447234dd459a00a5ed9b7c3e066162cd7a75bf2dDouglas Gregor                                        bool OfBlockPointer,
6733447234dd459a00a5ed9b7c3e066162cd7a75bf2dDouglas Gregor                                        bool Unqualified) {
6734183700f494ec9b6701b6efe82bcb25f4c79ba561John McCall  const FunctionType *lbase = lhs->getAs<FunctionType>();
6735183700f494ec9b6701b6efe82bcb25f4c79ba561John McCall  const FunctionType *rbase = rhs->getAs<FunctionType>();
673672564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor  const FunctionProtoType *lproto = dyn_cast<FunctionProtoType>(lbase);
673772564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor  const FunctionProtoType *rproto = dyn_cast<FunctionProtoType>(rbase);
67383d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman  bool allLTypes = true;
67393d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman  bool allRTypes = true;
67403d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman
67413d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman  // Check return type
6742132f2a2da34f378fc675b9e174564b0f52c31d98Fariborz Jahanian  QualType retType;
6743d263fd1451299b1e5f5f1acb2bb13b0a4119aee8Fariborz Jahanian  if (OfBlockPointer) {
6744d263fd1451299b1e5f5f1acb2bb13b0a4119aee8Fariborz Jahanian    QualType RHS = rbase->getResultType();
6745d263fd1451299b1e5f5f1acb2bb13b0a4119aee8Fariborz Jahanian    QualType LHS = lbase->getResultType();
6746d263fd1451299b1e5f5f1acb2bb13b0a4119aee8Fariborz Jahanian    bool UnqualifiedResult = Unqualified;
6747d263fd1451299b1e5f5f1acb2bb13b0a4119aee8Fariborz Jahanian    if (!UnqualifiedResult)
6748d263fd1451299b1e5f5f1acb2bb13b0a4119aee8Fariborz Jahanian      UnqualifiedResult = (!RHS.hasQualifiers() && LHS.hasQualifiers());
6749a4fdbfad150ae37bddaa4094d3925a27a1a1cf3fFariborz Jahanian    retType = mergeTypes(LHS, RHS, true, UnqualifiedResult, true);
6750d263fd1451299b1e5f5f1acb2bb13b0a4119aee8Fariborz Jahanian  }
6751132f2a2da34f378fc675b9e174564b0f52c31d98Fariborz Jahanian  else
67528cc246c9a68c783a5b90d2e8b8927521cb3a49b7John McCall    retType = mergeTypes(lbase->getResultType(), rbase->getResultType(), false,
67538cc246c9a68c783a5b90d2e8b8927521cb3a49b7John McCall                         Unqualified);
67543d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman  if (retType.isNull()) return QualType();
6755447234dd459a00a5ed9b7c3e066162cd7a75bf2dDouglas Gregor
6756447234dd459a00a5ed9b7c3e066162cd7a75bf2dDouglas Gregor  if (Unqualified)
6757447234dd459a00a5ed9b7c3e066162cd7a75bf2dDouglas Gregor    retType = retType.getUnqualifiedType();
6758447234dd459a00a5ed9b7c3e066162cd7a75bf2dDouglas Gregor
6759447234dd459a00a5ed9b7c3e066162cd7a75bf2dDouglas Gregor  CanQualType LRetType = getCanonicalType(lbase->getResultType());
6760447234dd459a00a5ed9b7c3e066162cd7a75bf2dDouglas Gregor  CanQualType RRetType = getCanonicalType(rbase->getResultType());
6761447234dd459a00a5ed9b7c3e066162cd7a75bf2dDouglas Gregor  if (Unqualified) {
6762447234dd459a00a5ed9b7c3e066162cd7a75bf2dDouglas Gregor    LRetType = LRetType.getUnqualifiedType();
6763447234dd459a00a5ed9b7c3e066162cd7a75bf2dDouglas Gregor    RRetType = RRetType.getUnqualifiedType();
6764447234dd459a00a5ed9b7c3e066162cd7a75bf2dDouglas Gregor  }
6765447234dd459a00a5ed9b7c3e066162cd7a75bf2dDouglas Gregor
6766447234dd459a00a5ed9b7c3e066162cd7a75bf2dDouglas Gregor  if (getCanonicalType(retType) != LRetType)
676761710854be2b098428aff5316e64bd34b30fbcb7Chris Lattner    allLTypes = false;
6768447234dd459a00a5ed9b7c3e066162cd7a75bf2dDouglas Gregor  if (getCanonicalType(retType) != RRetType)
676961710854be2b098428aff5316e64bd34b30fbcb7Chris Lattner    allRTypes = false;
67708cc246c9a68c783a5b90d2e8b8927521cb3a49b7John McCall
67716a15c8586cc3e5d6e01c4c4e8b4374584569b58fDaniel Dunbar  // FIXME: double check this
67726a15c8586cc3e5d6e01c4c4e8b4374584569b58fDaniel Dunbar  // FIXME: should we error if lbase->getRegParmAttr() != 0 &&
67736a15c8586cc3e5d6e01c4c4e8b4374584569b58fDaniel Dunbar  //                           rbase->getRegParmAttr() != 0 &&
67746a15c8586cc3e5d6e01c4c4e8b4374584569b58fDaniel Dunbar  //                           lbase->getRegParmAttr() != rbase->getRegParmAttr()?
6775264ba48dc98f3f843935a485d5b086f7e0fdc4f1Rafael Espindola  FunctionType::ExtInfo lbaseInfo = lbase->getExtInfo();
6776264ba48dc98f3f843935a485d5b086f7e0fdc4f1Rafael Espindola  FunctionType::ExtInfo rbaseInfo = rbase->getExtInfo();
67778cc246c9a68c783a5b90d2e8b8927521cb3a49b7John McCall
67788cc246c9a68c783a5b90d2e8b8927521cb3a49b7John McCall  // Compatible functions must have compatible calling conventions
67798cc246c9a68c783a5b90d2e8b8927521cb3a49b7John McCall  if (!isSameCallConv(lbaseInfo.getCC(), rbaseInfo.getCC()))
67808cc246c9a68c783a5b90d2e8b8927521cb3a49b7John McCall    return QualType();
67818cc246c9a68c783a5b90d2e8b8927521cb3a49b7John McCall
67828cc246c9a68c783a5b90d2e8b8927521cb3a49b7John McCall  // Regparm is part of the calling convention.
6783a49218e17bcbb1acde0245773173e2c0c42f4f19Eli Friedman  if (lbaseInfo.getHasRegParm() != rbaseInfo.getHasRegParm())
6784a49218e17bcbb1acde0245773173e2c0c42f4f19Eli Friedman    return QualType();
67858cc246c9a68c783a5b90d2e8b8927521cb3a49b7John McCall  if (lbaseInfo.getRegParm() != rbaseInfo.getRegParm())
67868cc246c9a68c783a5b90d2e8b8927521cb3a49b7John McCall    return QualType();
67878cc246c9a68c783a5b90d2e8b8927521cb3a49b7John McCall
6788f85e193739c953358c865005855253af4f68a497John McCall  if (lbaseInfo.getProducesResult() != rbaseInfo.getProducesResult())
6789f85e193739c953358c865005855253af4f68a497John McCall    return QualType();
6790f85e193739c953358c865005855253af4f68a497John McCall
679153c8167d7a007daae87f342c0fedd03f1dcf1b62Fariborz Jahanian  // FIXME: some uses, e.g. conditional exprs, really want this to be 'both'.
679253c8167d7a007daae87f342c0fedd03f1dcf1b62Fariborz Jahanian  bool NoReturn = lbaseInfo.getNoReturn() || rbaseInfo.getNoReturn();
67931eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
67948b8a09e496fbed361d4c8e9e8cc259454a094258Rafael Espindola  if (lbaseInfo.getNoReturn() != NoReturn)
67958b8a09e496fbed361d4c8e9e8cc259454a094258Rafael Espindola    allLTypes = false;
67968b8a09e496fbed361d4c8e9e8cc259454a094258Rafael Espindola  if (rbaseInfo.getNoReturn() != NoReturn)
67978b8a09e496fbed361d4c8e9e8cc259454a094258Rafael Espindola    allRTypes = false;
67988b8a09e496fbed361d4c8e9e8cc259454a094258Rafael Espindola
6799f85e193739c953358c865005855253af4f68a497John McCall  FunctionType::ExtInfo einfo = lbaseInfo.withNoReturn(NoReturn);
6800e23cf437fe76b1ed02d63c3f61b456fd48a915f5John McCall
68013d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman  if (lproto && rproto) { // two C99 style function prototypes
6802465226e23a3008bd68973513dda1f9e3cd27dbddSebastian Redl    assert(!lproto->hasExceptionSpec() && !rproto->hasExceptionSpec() &&
6803465226e23a3008bd68973513dda1f9e3cd27dbddSebastian Redl           "C++ shouldn't be here");
68043d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    unsigned lproto_nargs = lproto->getNumArgs();
68053d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    unsigned rproto_nargs = rproto->getNumArgs();
68063d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman
68073d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    // Compatible functions must have the same number of arguments
68083d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    if (lproto_nargs != rproto_nargs)
68093d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman      return QualType();
68103d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman
68113d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    // Variadic and non-variadic functions aren't compatible
68123d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    if (lproto->isVariadic() != rproto->isVariadic())
68133d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman      return QualType();
68143d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman
68157fb5e4888221cd36652d078c6b171ac55e7f406dArgyrios Kyrtzidis    if (lproto->getTypeQuals() != rproto->getTypeQuals())
68167fb5e4888221cd36652d078c6b171ac55e7f406dArgyrios Kyrtzidis      return QualType();
68177fb5e4888221cd36652d078c6b171ac55e7f406dArgyrios Kyrtzidis
681878213e4aec6d8d22b5828dd2687fa19116ebaa26Fariborz Jahanian    if (LangOpts.ObjCAutoRefCount &&
681978213e4aec6d8d22b5828dd2687fa19116ebaa26Fariborz Jahanian        !FunctionTypesMatchOnNSConsumedAttrs(rproto, lproto))
682078213e4aec6d8d22b5828dd2687fa19116ebaa26Fariborz Jahanian      return QualType();
682178213e4aec6d8d22b5828dd2687fa19116ebaa26Fariborz Jahanian
68223d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    // Check argument compatibility
68235f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner    SmallVector<QualType, 10> types;
68243d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    for (unsigned i = 0; i < lproto_nargs; i++) {
68253d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman      QualType largtype = lproto->getArgType(i).getUnqualifiedType();
68263d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman      QualType rargtype = rproto->getArgType(i).getUnqualifiedType();
68274846675e0e42d1802b0ffd8972a45e72aeb3758dPeter Collingbourne      QualType argtype = mergeFunctionArgumentTypes(largtype, rargtype,
68284846675e0e42d1802b0ffd8972a45e72aeb3758dPeter Collingbourne                                                    OfBlockPointer,
68294846675e0e42d1802b0ffd8972a45e72aeb3758dPeter Collingbourne                                                    Unqualified);
68303d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman      if (argtype.isNull()) return QualType();
6831447234dd459a00a5ed9b7c3e066162cd7a75bf2dDouglas Gregor
6832447234dd459a00a5ed9b7c3e066162cd7a75bf2dDouglas Gregor      if (Unqualified)
6833447234dd459a00a5ed9b7c3e066162cd7a75bf2dDouglas Gregor        argtype = argtype.getUnqualifiedType();
6834447234dd459a00a5ed9b7c3e066162cd7a75bf2dDouglas Gregor
68353d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman      types.push_back(argtype);
6836447234dd459a00a5ed9b7c3e066162cd7a75bf2dDouglas Gregor      if (Unqualified) {
6837447234dd459a00a5ed9b7c3e066162cd7a75bf2dDouglas Gregor        largtype = largtype.getUnqualifiedType();
6838447234dd459a00a5ed9b7c3e066162cd7a75bf2dDouglas Gregor        rargtype = rargtype.getUnqualifiedType();
6839447234dd459a00a5ed9b7c3e066162cd7a75bf2dDouglas Gregor      }
6840447234dd459a00a5ed9b7c3e066162cd7a75bf2dDouglas Gregor
684161710854be2b098428aff5316e64bd34b30fbcb7Chris Lattner      if (getCanonicalType(argtype) != getCanonicalType(largtype))
684261710854be2b098428aff5316e64bd34b30fbcb7Chris Lattner        allLTypes = false;
684361710854be2b098428aff5316e64bd34b30fbcb7Chris Lattner      if (getCanonicalType(argtype) != getCanonicalType(rargtype))
684461710854be2b098428aff5316e64bd34b30fbcb7Chris Lattner        allRTypes = false;
68453d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    }
684678213e4aec6d8d22b5828dd2687fa19116ebaa26Fariborz Jahanian
68473d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    if (allLTypes) return lhs;
68483d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    if (allRTypes) return rhs;
6849e23cf437fe76b1ed02d63c3f61b456fd48a915f5John McCall
6850e23cf437fe76b1ed02d63c3f61b456fd48a915f5John McCall    FunctionProtoType::ExtProtoInfo EPI = lproto->getExtProtoInfo();
6851e23cf437fe76b1ed02d63c3f61b456fd48a915f5John McCall    EPI.ExtInfo = einfo;
6852bea522ff43a3f11c7a2bc7949119dbb9fce19e39Jordan Rose    return getFunctionType(retType, types, EPI);
68533d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman  }
68543d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman
68553d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman  if (lproto) allRTypes = false;
68563d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman  if (rproto) allLTypes = false;
68573d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman
685872564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor  const FunctionProtoType *proto = lproto ? lproto : rproto;
68593d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman  if (proto) {
6860465226e23a3008bd68973513dda1f9e3cd27dbddSebastian Redl    assert(!proto->hasExceptionSpec() && "C++ shouldn't be here");
68613d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    if (proto->isVariadic()) return QualType();
68623d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    // Check that the types are compatible with the types that
68633d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    // would result from default argument promotions (C99 6.7.5.3p15).
68643d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    // The only types actually affected are promotable integer
68653d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    // types and floats, which would be passed as a different
68663d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    // type depending on whether the prototype is visible.
68673d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    unsigned proto_nargs = proto->getNumArgs();
68683d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    for (unsigned i = 0; i < proto_nargs; ++i) {
68693d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman      QualType argTy = proto->getArgType(i);
6870b0f8eacfdcd0d43f51e669f2d723992d4af9f746Douglas Gregor
6871c586d5dd7a997321ea96d1d28213315f86778fc8Eli Friedman      // Look at the converted type of enum types, since that is the type used
6872b0f8eacfdcd0d43f51e669f2d723992d4af9f746Douglas Gregor      // to pass enum values.
6873c586d5dd7a997321ea96d1d28213315f86778fc8Eli Friedman      if (const EnumType *Enum = argTy->getAs<EnumType>()) {
6874c586d5dd7a997321ea96d1d28213315f86778fc8Eli Friedman        argTy = Enum->getDecl()->getIntegerType();
6875c586d5dd7a997321ea96d1d28213315f86778fc8Eli Friedman        if (argTy.isNull())
6876c586d5dd7a997321ea96d1d28213315f86778fc8Eli Friedman          return QualType();
6877c586d5dd7a997321ea96d1d28213315f86778fc8Eli Friedman      }
6878b0f8eacfdcd0d43f51e669f2d723992d4af9f746Douglas Gregor
68793d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman      if (argTy->isPromotableIntegerType() ||
68803d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman          getCanonicalType(argTy).getUnqualifiedType() == FloatTy)
68813d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman        return QualType();
68823d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    }
68833d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman
68843d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    if (allLTypes) return lhs;
68853d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    if (allRTypes) return rhs;
6886e23cf437fe76b1ed02d63c3f61b456fd48a915f5John McCall
6887e23cf437fe76b1ed02d63c3f61b456fd48a915f5John McCall    FunctionProtoType::ExtProtoInfo EPI = proto->getExtProtoInfo();
6888e23cf437fe76b1ed02d63c3f61b456fd48a915f5John McCall    EPI.ExtInfo = einfo;
6889bea522ff43a3f11c7a2bc7949119dbb9fce19e39Jordan Rose    return getFunctionType(retType,
6890bea522ff43a3f11c7a2bc7949119dbb9fce19e39Jordan Rose                           ArrayRef<QualType>(proto->arg_type_begin(),
6891bea522ff43a3f11c7a2bc7949119dbb9fce19e39Jordan Rose                                              proto->getNumArgs()),
6892bea522ff43a3f11c7a2bc7949119dbb9fce19e39Jordan Rose                           EPI);
68933d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman  }
68943d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman
68953d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman  if (allLTypes) return lhs;
68963d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman  if (allRTypes) return rhs;
68978cc246c9a68c783a5b90d2e8b8927521cb3a49b7John McCall  return getFunctionNoProtoType(retType, einfo);
68983d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman}
68993d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman
6900b9da713efb4277753211590953f433723908aadeJohn McCall/// Given that we have an enum type and a non-enum type, try to merge them.
6901b9da713efb4277753211590953f433723908aadeJohn McCallstatic QualType mergeEnumWithInteger(ASTContext &Context, const EnumType *ET,
6902b9da713efb4277753211590953f433723908aadeJohn McCall                                     QualType other, bool isBlockReturnType) {
6903b9da713efb4277753211590953f433723908aadeJohn McCall  // C99 6.7.2.2p4: Each enumerated type shall be compatible with char,
6904b9da713efb4277753211590953f433723908aadeJohn McCall  // a signed integer type, or an unsigned integer type.
6905b9da713efb4277753211590953f433723908aadeJohn McCall  // Compatibility is based on the underlying type, not the promotion
6906b9da713efb4277753211590953f433723908aadeJohn McCall  // type.
6907b9da713efb4277753211590953f433723908aadeJohn McCall  QualType underlyingType = ET->getDecl()->getIntegerType();
6908b9da713efb4277753211590953f433723908aadeJohn McCall  if (underlyingType.isNull()) return QualType();
6909b9da713efb4277753211590953f433723908aadeJohn McCall  if (Context.hasSameType(underlyingType, other))
6910b9da713efb4277753211590953f433723908aadeJohn McCall    return other;
6911b9da713efb4277753211590953f433723908aadeJohn McCall
6912b9da713efb4277753211590953f433723908aadeJohn McCall  // In block return types, we're more permissive and accept any
6913b9da713efb4277753211590953f433723908aadeJohn McCall  // integral type of the same size.
6914b9da713efb4277753211590953f433723908aadeJohn McCall  if (isBlockReturnType && other->isIntegerType() &&
6915b9da713efb4277753211590953f433723908aadeJohn McCall      Context.getTypeSize(underlyingType) == Context.getTypeSize(other))
6916b9da713efb4277753211590953f433723908aadeJohn McCall    return other;
6917b9da713efb4277753211590953f433723908aadeJohn McCall
6918b9da713efb4277753211590953f433723908aadeJohn McCall  return QualType();
6919b9da713efb4277753211590953f433723908aadeJohn McCall}
6920b9da713efb4277753211590953f433723908aadeJohn McCall
6921132f2a2da34f378fc675b9e174564b0f52c31d98Fariborz JahanianQualType ASTContext::mergeTypes(QualType LHS, QualType RHS,
6922447234dd459a00a5ed9b7c3e066162cd7a75bf2dDouglas Gregor                                bool OfBlockPointer,
6923a4fdbfad150ae37bddaa4094d3925a27a1a1cf3fFariborz Jahanian                                bool Unqualified, bool BlockReturnType) {
692443d69750e7f7b26076e7474dec8839bb777b260fBill Wendling  // C++ [expr]: If an expression initially has the type "reference to T", the
692543d69750e7f7b26076e7474dec8839bb777b260fBill Wendling  // type is adjusted to "T" prior to any further analysis, the expression
692643d69750e7f7b26076e7474dec8839bb777b260fBill Wendling  // designates the object or function denoted by the reference, and the
69277c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl  // expression is an lvalue unless the reference is an rvalue reference and
69287c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl  // the expression is a function call (possibly inside parentheses).
69290e709abafbd939326850501f795cc7a92c88a354Douglas Gregor  assert(!LHS->getAs<ReferenceType>() && "LHS is a reference type?");
69300e709abafbd939326850501f795cc7a92c88a354Douglas Gregor  assert(!RHS->getAs<ReferenceType>() && "RHS is a reference type?");
6931447234dd459a00a5ed9b7c3e066162cd7a75bf2dDouglas Gregor
6932447234dd459a00a5ed9b7c3e066162cd7a75bf2dDouglas Gregor  if (Unqualified) {
6933447234dd459a00a5ed9b7c3e066162cd7a75bf2dDouglas Gregor    LHS = LHS.getUnqualifiedType();
6934447234dd459a00a5ed9b7c3e066162cd7a75bf2dDouglas Gregor    RHS = RHS.getUnqualifiedType();
6935447234dd459a00a5ed9b7c3e066162cd7a75bf2dDouglas Gregor  }
69360e709abafbd939326850501f795cc7a92c88a354Douglas Gregor
69373d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman  QualType LHSCan = getCanonicalType(LHS),
69383d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman           RHSCan = getCanonicalType(RHS);
69393d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman
6940f3692dc4a47dc48d10cec0415c6e9e39b7a39707Chris Lattner  // If two types are identical, they are compatible.
69413d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman  if (LHSCan == RHSCan)
69423d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    return LHS;
69433d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman
69440953e767ff7817f97b3ab20896b229891eeff45bJohn McCall  // If the qualifiers are different, the types aren't compatible... mostly.
6945a4923eb7c4b04d360cb2747641a5e92818edf804Douglas Gregor  Qualifiers LQuals = LHSCan.getLocalQualifiers();
6946a4923eb7c4b04d360cb2747641a5e92818edf804Douglas Gregor  Qualifiers RQuals = RHSCan.getLocalQualifiers();
69470953e767ff7817f97b3ab20896b229891eeff45bJohn McCall  if (LQuals != RQuals) {
69480953e767ff7817f97b3ab20896b229891eeff45bJohn McCall    // If any of these qualifiers are different, we have a type
69490953e767ff7817f97b3ab20896b229891eeff45bJohn McCall    // mismatch.
69500953e767ff7817f97b3ab20896b229891eeff45bJohn McCall    if (LQuals.getCVRQualifiers() != RQuals.getCVRQualifiers() ||
6951f85e193739c953358c865005855253af4f68a497John McCall        LQuals.getAddressSpace() != RQuals.getAddressSpace() ||
6952f85e193739c953358c865005855253af4f68a497John McCall        LQuals.getObjCLifetime() != RQuals.getObjCLifetime())
69530953e767ff7817f97b3ab20896b229891eeff45bJohn McCall      return QualType();
69540953e767ff7817f97b3ab20896b229891eeff45bJohn McCall
69550953e767ff7817f97b3ab20896b229891eeff45bJohn McCall    // Exactly one GC qualifier difference is allowed: __strong is
69560953e767ff7817f97b3ab20896b229891eeff45bJohn McCall    // okay if the other type has no GC qualifier but is an Objective
69570953e767ff7817f97b3ab20896b229891eeff45bJohn McCall    // C object pointer (i.e. implicitly strong by default).  We fix
69580953e767ff7817f97b3ab20896b229891eeff45bJohn McCall    // this by pretending that the unqualified type was actually
69590953e767ff7817f97b3ab20896b229891eeff45bJohn McCall    // qualified __strong.
69600953e767ff7817f97b3ab20896b229891eeff45bJohn McCall    Qualifiers::GC GC_L = LQuals.getObjCGCAttr();
69610953e767ff7817f97b3ab20896b229891eeff45bJohn McCall    Qualifiers::GC GC_R = RQuals.getObjCGCAttr();
69620953e767ff7817f97b3ab20896b229891eeff45bJohn McCall    assert((GC_L != GC_R) && "unequal qualifier sets had only equal elements");
69630953e767ff7817f97b3ab20896b229891eeff45bJohn McCall
69640953e767ff7817f97b3ab20896b229891eeff45bJohn McCall    if (GC_L == Qualifiers::Weak || GC_R == Qualifiers::Weak)
69650953e767ff7817f97b3ab20896b229891eeff45bJohn McCall      return QualType();
69660953e767ff7817f97b3ab20896b229891eeff45bJohn McCall
69670953e767ff7817f97b3ab20896b229891eeff45bJohn McCall    if (GC_L == Qualifiers::Strong && RHSCan->isObjCObjectPointerType()) {
69680953e767ff7817f97b3ab20896b229891eeff45bJohn McCall      return mergeTypes(LHS, getObjCGCQualType(RHS, Qualifiers::Strong));
69690953e767ff7817f97b3ab20896b229891eeff45bJohn McCall    }
69700953e767ff7817f97b3ab20896b229891eeff45bJohn McCall    if (GC_R == Qualifiers::Strong && LHSCan->isObjCObjectPointerType()) {
69710953e767ff7817f97b3ab20896b229891eeff45bJohn McCall      return mergeTypes(getObjCGCQualType(LHS, Qualifiers::Strong), RHS);
69720953e767ff7817f97b3ab20896b229891eeff45bJohn McCall    }
69733d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    return QualType();
69740953e767ff7817f97b3ab20896b229891eeff45bJohn McCall  }
69750953e767ff7817f97b3ab20896b229891eeff45bJohn McCall
69760953e767ff7817f97b3ab20896b229891eeff45bJohn McCall  // Okay, qualifiers are equal.
69773d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman
6978852d63b806c5cbd730c6b9d696e2e27d02546b49Eli Friedman  Type::TypeClass LHSClass = LHSCan->getTypeClass();
6979852d63b806c5cbd730c6b9d696e2e27d02546b49Eli Friedman  Type::TypeClass RHSClass = RHSCan->getTypeClass();
6980f3692dc4a47dc48d10cec0415c6e9e39b7a39707Chris Lattner
69811adb88370beab45af2f065afe86b51ccd59ec50dChris Lattner  // We want to consider the two function types to be the same for these
69821adb88370beab45af2f065afe86b51ccd59ec50dChris Lattner  // comparisons, just force one to the other.
69831adb88370beab45af2f065afe86b51ccd59ec50dChris Lattner  if (LHSClass == Type::FunctionProto) LHSClass = Type::FunctionNoProto;
69841adb88370beab45af2f065afe86b51ccd59ec50dChris Lattner  if (RHSClass == Type::FunctionProto) RHSClass = Type::FunctionNoProto;
69854c721d381fb279899337d120edd4a24d405e56b2Eli Friedman
69864c721d381fb279899337d120edd4a24d405e56b2Eli Friedman  // Same as above for arrays
6987a36a61f218b9f7a97f2c0f511e0b29eb42e8f78bChris Lattner  if (LHSClass == Type::VariableArray || LHSClass == Type::IncompleteArray)
6988a36a61f218b9f7a97f2c0f511e0b29eb42e8f78bChris Lattner    LHSClass = Type::ConstantArray;
6989a36a61f218b9f7a97f2c0f511e0b29eb42e8f78bChris Lattner  if (RHSClass == Type::VariableArray || RHSClass == Type::IncompleteArray)
6990a36a61f218b9f7a97f2c0f511e0b29eb42e8f78bChris Lattner    RHSClass = Type::ConstantArray;
69911eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
6992c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall  // ObjCInterfaces are just specialized ObjCObjects.
6993c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall  if (LHSClass == Type::ObjCInterface) LHSClass = Type::ObjCObject;
6994c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall  if (RHSClass == Type::ObjCInterface) RHSClass = Type::ObjCObject;
6995c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall
6996213541a68a3e137d11d2cefb612c6cdb410d7e8eNate Begeman  // Canonicalize ExtVector -> Vector.
6997213541a68a3e137d11d2cefb612c6cdb410d7e8eNate Begeman  if (LHSClass == Type::ExtVector) LHSClass = Type::Vector;
6998213541a68a3e137d11d2cefb612c6cdb410d7e8eNate Begeman  if (RHSClass == Type::ExtVector) RHSClass = Type::Vector;
69991eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
70004e78fd0a960eaa7e97467f2e8f390f3a57da279bSteve Naroff  // If the canonical type classes don't match.
70014e78fd0a960eaa7e97467f2e8f390f3a57da279bSteve Naroff  if (LHSClass != RHSClass) {
7002b9da713efb4277753211590953f433723908aadeJohn McCall    // Note that we only have special rules for turning block enum
7003b9da713efb4277753211590953f433723908aadeJohn McCall    // returns into block int returns, not vice-versa.
7004183700f494ec9b6701b6efe82bcb25f4c79ba561John McCall    if (const EnumType* ETy = LHS->getAs<EnumType>()) {
7005b9da713efb4277753211590953f433723908aadeJohn McCall      return mergeEnumWithInteger(*this, ETy, RHS, false);
7006bab96968886f4b77083f4e26a28986ddb1e42d67Eli Friedman    }
7007183700f494ec9b6701b6efe82bcb25f4c79ba561John McCall    if (const EnumType* ETy = RHS->getAs<EnumType>()) {
7008b9da713efb4277753211590953f433723908aadeJohn McCall      return mergeEnumWithInteger(*this, ETy, LHS, BlockReturnType);
7009bab96968886f4b77083f4e26a28986ddb1e42d67Eli Friedman    }
7010e7cff2c00c3d74e38984a4db5c48d7e303d3ab8eFariborz Jahanian    // allow block pointer type to match an 'id' type.
7011419636316c04d570c183b4797fc811932c588358Fariborz Jahanian    if (OfBlockPointer && !BlockReturnType) {
7012419636316c04d570c183b4797fc811932c588358Fariborz Jahanian       if (LHS->isObjCIdType() && RHS->isBlockPointerType())
7013419636316c04d570c183b4797fc811932c588358Fariborz Jahanian         return LHS;
7014419636316c04d570c183b4797fc811932c588358Fariborz Jahanian      if (RHS->isObjCIdType() && LHS->isBlockPointerType())
7015419636316c04d570c183b4797fc811932c588358Fariborz Jahanian        return RHS;
7016419636316c04d570c183b4797fc811932c588358Fariborz Jahanian    }
7017e7cff2c00c3d74e38984a4db5c48d7e303d3ab8eFariborz Jahanian
70183d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    return QualType();
7019ec0550fa3653d46560bf4484a2e988329c228e39Steve Naroff  }
70203d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman
70214a74678ed6c3dedac05d02b1ee341f1db869f049Steve Naroff  // The canonical type classes match.
70221adb88370beab45af2f065afe86b51ccd59ec50dChris Lattner  switch (LHSClass) {
702372564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor#define TYPE(Class, Base)
702472564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor#define ABSTRACT_TYPE(Class, Base)
7025ad5e73887052193afda72db8efcb812bd083a4a8John McCall#define NON_CANONICAL_UNLESS_DEPENDENT_TYPE(Class, Base) case Type::Class:
702672564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor#define NON_CANONICAL_TYPE(Class, Base) case Type::Class:
702772564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor#define DEPENDENT_TYPE(Class, Base) case Type::Class:
702872564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor#include "clang/AST/TypeNodes.def"
7029b219cfc4d75f0a03630b7c4509ef791b7e97b2c8David Blaikie    llvm_unreachable("Non-canonical and dependent types shouldn't get here");
703072564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor
70317c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl  case Type::LValueReference:
70327c80bd64032e610c0dbd74fc0ef6ea334447f2fdSebastian Redl  case Type::RValueReference:
703372564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor  case Type::MemberPointer:
7034b219cfc4d75f0a03630b7c4509ef791b7e97b2c8David Blaikie    llvm_unreachable("C++ should never be in mergeTypes");
703572564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor
7036c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall  case Type::ObjCInterface:
703772564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor  case Type::IncompleteArray:
703872564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor  case Type::VariableArray:
703972564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor  case Type::FunctionProto:
704072564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor  case Type::ExtVector:
7041b219cfc4d75f0a03630b7c4509ef791b7e97b2c8David Blaikie    llvm_unreachable("Types are eliminated above");
704272564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor
70431adb88370beab45af2f065afe86b51ccd59ec50dChris Lattner  case Type::Pointer:
70443d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman  {
70453d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    // Merge two pointer types, while trying to preserve typedef info
70466217b80b7a1379b74cced1c076338262c3c980b3Ted Kremenek    QualType LHSPointee = LHS->getAs<PointerType>()->getPointeeType();
70476217b80b7a1379b74cced1c076338262c3c980b3Ted Kremenek    QualType RHSPointee = RHS->getAs<PointerType>()->getPointeeType();
7048447234dd459a00a5ed9b7c3e066162cd7a75bf2dDouglas Gregor    if (Unqualified) {
7049447234dd459a00a5ed9b7c3e066162cd7a75bf2dDouglas Gregor      LHSPointee = LHSPointee.getUnqualifiedType();
7050447234dd459a00a5ed9b7c3e066162cd7a75bf2dDouglas Gregor      RHSPointee = RHSPointee.getUnqualifiedType();
7051447234dd459a00a5ed9b7c3e066162cd7a75bf2dDouglas Gregor    }
7052447234dd459a00a5ed9b7c3e066162cd7a75bf2dDouglas Gregor    QualType ResultType = mergeTypes(LHSPointee, RHSPointee, false,
7053447234dd459a00a5ed9b7c3e066162cd7a75bf2dDouglas Gregor                                     Unqualified);
70543d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    if (ResultType.isNull()) return QualType();
705507d258756dc856c6987c394a0972884e6ed46765Eli Friedman    if (getCanonicalType(LHSPointee) == getCanonicalType(ResultType))
705661710854be2b098428aff5316e64bd34b30fbcb7Chris Lattner      return LHS;
705707d258756dc856c6987c394a0972884e6ed46765Eli Friedman    if (getCanonicalType(RHSPointee) == getCanonicalType(ResultType))
705861710854be2b098428aff5316e64bd34b30fbcb7Chris Lattner      return RHS;
70593d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    return getPointerType(ResultType);
70603d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman  }
7061c0febd58f5cbf4a93fd12f461863564dba0af76dSteve Naroff  case Type::BlockPointer:
7062c0febd58f5cbf4a93fd12f461863564dba0af76dSteve Naroff  {
7063c0febd58f5cbf4a93fd12f461863564dba0af76dSteve Naroff    // Merge two block pointer types, while trying to preserve typedef info
70646217b80b7a1379b74cced1c076338262c3c980b3Ted Kremenek    QualType LHSPointee = LHS->getAs<BlockPointerType>()->getPointeeType();
70656217b80b7a1379b74cced1c076338262c3c980b3Ted Kremenek    QualType RHSPointee = RHS->getAs<BlockPointerType>()->getPointeeType();
7066447234dd459a00a5ed9b7c3e066162cd7a75bf2dDouglas Gregor    if (Unqualified) {
7067447234dd459a00a5ed9b7c3e066162cd7a75bf2dDouglas Gregor      LHSPointee = LHSPointee.getUnqualifiedType();
7068447234dd459a00a5ed9b7c3e066162cd7a75bf2dDouglas Gregor      RHSPointee = RHSPointee.getUnqualifiedType();
7069447234dd459a00a5ed9b7c3e066162cd7a75bf2dDouglas Gregor    }
7070447234dd459a00a5ed9b7c3e066162cd7a75bf2dDouglas Gregor    QualType ResultType = mergeTypes(LHSPointee, RHSPointee, OfBlockPointer,
7071447234dd459a00a5ed9b7c3e066162cd7a75bf2dDouglas Gregor                                     Unqualified);
7072c0febd58f5cbf4a93fd12f461863564dba0af76dSteve Naroff    if (ResultType.isNull()) return QualType();
7073c0febd58f5cbf4a93fd12f461863564dba0af76dSteve Naroff    if (getCanonicalType(LHSPointee) == getCanonicalType(ResultType))
7074c0febd58f5cbf4a93fd12f461863564dba0af76dSteve Naroff      return LHS;
7075c0febd58f5cbf4a93fd12f461863564dba0af76dSteve Naroff    if (getCanonicalType(RHSPointee) == getCanonicalType(ResultType))
7076c0febd58f5cbf4a93fd12f461863564dba0af76dSteve Naroff      return RHS;
7077c0febd58f5cbf4a93fd12f461863564dba0af76dSteve Naroff    return getBlockPointerType(ResultType);
7078c0febd58f5cbf4a93fd12f461863564dba0af76dSteve Naroff  }
7079b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman  case Type::Atomic:
7080b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman  {
7081b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman    // Merge two pointer types, while trying to preserve typedef info
7082b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman    QualType LHSValue = LHS->getAs<AtomicType>()->getValueType();
7083b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman    QualType RHSValue = RHS->getAs<AtomicType>()->getValueType();
7084b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman    if (Unqualified) {
7085b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman      LHSValue = LHSValue.getUnqualifiedType();
7086b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman      RHSValue = RHSValue.getUnqualifiedType();
7087b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman    }
7088b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman    QualType ResultType = mergeTypes(LHSValue, RHSValue, false,
7089b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman                                     Unqualified);
7090b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman    if (ResultType.isNull()) return QualType();
7091b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman    if (getCanonicalType(LHSValue) == getCanonicalType(ResultType))
7092b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman      return LHS;
7093b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman    if (getCanonicalType(RHSValue) == getCanonicalType(ResultType))
7094b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman      return RHS;
7095b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman    return getAtomicType(ResultType);
7096b001de7458d17c17e6d8b8034c7cfcefd3b70c00Eli Friedman  }
70971adb88370beab45af2f065afe86b51ccd59ec50dChris Lattner  case Type::ConstantArray:
70983d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman  {
70993d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    const ConstantArrayType* LCAT = getAsConstantArrayType(LHS);
71003d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    const ConstantArrayType* RCAT = getAsConstantArrayType(RHS);
71013d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    if (LCAT && RCAT && RCAT->getSize() != LCAT->getSize())
71023d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman      return QualType();
71033d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman
71043d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    QualType LHSElem = getAsArrayType(LHS)->getElementType();
71053d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    QualType RHSElem = getAsArrayType(RHS)->getElementType();
7106447234dd459a00a5ed9b7c3e066162cd7a75bf2dDouglas Gregor    if (Unqualified) {
7107447234dd459a00a5ed9b7c3e066162cd7a75bf2dDouglas Gregor      LHSElem = LHSElem.getUnqualifiedType();
7108447234dd459a00a5ed9b7c3e066162cd7a75bf2dDouglas Gregor      RHSElem = RHSElem.getUnqualifiedType();
7109447234dd459a00a5ed9b7c3e066162cd7a75bf2dDouglas Gregor    }
7110447234dd459a00a5ed9b7c3e066162cd7a75bf2dDouglas Gregor
7111447234dd459a00a5ed9b7c3e066162cd7a75bf2dDouglas Gregor    QualType ResultType = mergeTypes(LHSElem, RHSElem, false, Unqualified);
71123d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    if (ResultType.isNull()) return QualType();
711361710854be2b098428aff5316e64bd34b30fbcb7Chris Lattner    if (LCAT && getCanonicalType(LHSElem) == getCanonicalType(ResultType))
711461710854be2b098428aff5316e64bd34b30fbcb7Chris Lattner      return LHS;
711561710854be2b098428aff5316e64bd34b30fbcb7Chris Lattner    if (RCAT && getCanonicalType(RHSElem) == getCanonicalType(ResultType))
711661710854be2b098428aff5316e64bd34b30fbcb7Chris Lattner      return RHS;
71173bc0f45a5e65814f42b22dcdf7249d1120d16f36Eli Friedman    if (LCAT) return getConstantArrayType(ResultType, LCAT->getSize(),
71183bc0f45a5e65814f42b22dcdf7249d1120d16f36Eli Friedman                                          ArrayType::ArraySizeModifier(), 0);
71193bc0f45a5e65814f42b22dcdf7249d1120d16f36Eli Friedman    if (RCAT) return getConstantArrayType(ResultType, RCAT->getSize(),
71203bc0f45a5e65814f42b22dcdf7249d1120d16f36Eli Friedman                                          ArrayType::ArraySizeModifier(), 0);
71213d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    const VariableArrayType* LVAT = getAsVariableArrayType(LHS);
71223d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    const VariableArrayType* RVAT = getAsVariableArrayType(RHS);
712361710854be2b098428aff5316e64bd34b30fbcb7Chris Lattner    if (LVAT && getCanonicalType(LHSElem) == getCanonicalType(ResultType))
712461710854be2b098428aff5316e64bd34b30fbcb7Chris Lattner      return LHS;
712561710854be2b098428aff5316e64bd34b30fbcb7Chris Lattner    if (RVAT && getCanonicalType(RHSElem) == getCanonicalType(ResultType))
712661710854be2b098428aff5316e64bd34b30fbcb7Chris Lattner      return RHS;
71273d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    if (LVAT) {
71283d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman      // FIXME: This isn't correct! But tricky to implement because
71293d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman      // the array's size has to be the size of LHS, but the type
71303d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman      // has to be different.
71313d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman      return LHS;
71323d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    }
71333d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    if (RVAT) {
71343d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman      // FIXME: This isn't correct! But tricky to implement because
71353d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman      // the array's size has to be the size of RHS, but the type
71363d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman      // has to be different.
71373d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman      return RHS;
71383d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    }
71393bc0f45a5e65814f42b22dcdf7249d1120d16f36Eli Friedman    if (getCanonicalType(LHSElem) == getCanonicalType(ResultType)) return LHS;
71403bc0f45a5e65814f42b22dcdf7249d1120d16f36Eli Friedman    if (getCanonicalType(RHSElem) == getCanonicalType(ResultType)) return RHS;
71417e7eb3da052a6d80ddf2377cab0384c798f73f75Douglas Gregor    return getIncompleteArrayType(ResultType,
71427e7eb3da052a6d80ddf2377cab0384c798f73f75Douglas Gregor                                  ArrayType::ArraySizeModifier(), 0);
71433d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman  }
71441adb88370beab45af2f065afe86b51ccd59ec50dChris Lattner  case Type::FunctionNoProto:
7145447234dd459a00a5ed9b7c3e066162cd7a75bf2dDouglas Gregor    return mergeFunctionTypes(LHS, RHS, OfBlockPointer, Unqualified);
714672564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor  case Type::Record:
714772564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor  case Type::Enum:
71483d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    return QualType();
71491adb88370beab45af2f065afe86b51ccd59ec50dChris Lattner  case Type::Builtin:
71503cc4c0c3058a788689b8fc73c0ac139544435c97Chris Lattner    // Only exactly equal builtin types are compatible, which is tested above.
71513d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    return QualType();
715264cfdb7da3cb744642fe8a99ad5c851ad3c930b2Daniel Dunbar  case Type::Complex:
715364cfdb7da3cb744642fe8a99ad5c851ad3c930b2Daniel Dunbar    // Distinct complex types are incompatible.
715464cfdb7da3cb744642fe8a99ad5c851ad3c930b2Daniel Dunbar    return QualType();
71553cc4c0c3058a788689b8fc73c0ac139544435c97Chris Lattner  case Type::Vector:
71565a61f0e5c5aaecd5713c3fa4b78be7167a7eeff2Eli Friedman    // FIXME: The merged type should be an ExtVector!
71571c471f3e1c0ec8cbc82447bb35908dfc55463e46John McCall    if (areCompatVectorTypes(LHSCan->getAs<VectorType>(),
71581c471f3e1c0ec8cbc82447bb35908dfc55463e46John McCall                             RHSCan->getAs<VectorType>()))
71593d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman      return LHS;
716061710854be2b098428aff5316e64bd34b30fbcb7Chris Lattner    return QualType();
7161c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall  case Type::ObjCObject: {
7162c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall    // Check if the types are assignment compatible.
71635a61f0e5c5aaecd5713c3fa4b78be7167a7eeff2Eli Friedman    // FIXME: This should be type compatibility, e.g. whether
71645a61f0e5c5aaecd5713c3fa4b78be7167a7eeff2Eli Friedman    // "LHS x; RHS x;" at global scope is legal.
7165c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall    const ObjCObjectType* LHSIface = LHS->getAs<ObjCObjectType>();
7166c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall    const ObjCObjectType* RHSIface = RHS->getAs<ObjCObjectType>();
7167c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall    if (canAssignObjCInterfaces(LHSIface, RHSIface))
71685fd659db11922fc12a58e478f7b745f9656b15a7Steve Naroff      return LHS;
71695fd659db11922fc12a58e478f7b745f9656b15a7Steve Naroff
71703d815e7eb56c25d7ed812eced32e41df43039f9aEli Friedman    return QualType();
717161490e9a965cfee8a78c12c6802138844f04250dCedric Venet  }
717214108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff  case Type::ObjCObjectPointer: {
7173132f2a2da34f378fc675b9e174564b0f52c31d98Fariborz Jahanian    if (OfBlockPointer) {
7174132f2a2da34f378fc675b9e174564b0f52c31d98Fariborz Jahanian      if (canAssignObjCInterfacesInBlockPointer(
7175132f2a2da34f378fc675b9e174564b0f52c31d98Fariborz Jahanian                                          LHS->getAs<ObjCObjectPointerType>(),
7176a4fdbfad150ae37bddaa4094d3925a27a1a1cf3fFariborz Jahanian                                          RHS->getAs<ObjCObjectPointerType>(),
7177a4fdbfad150ae37bddaa4094d3925a27a1a1cf3fFariborz Jahanian                                          BlockReturnType))
71787530c034c0c71a64c5a9173206d9742ae847af8bDavid Blaikie        return LHS;
7179132f2a2da34f378fc675b9e174564b0f52c31d98Fariborz Jahanian      return QualType();
7180132f2a2da34f378fc675b9e174564b0f52c31d98Fariborz Jahanian    }
7181183700f494ec9b6701b6efe82bcb25f4c79ba561John McCall    if (canAssignObjCInterfaces(LHS->getAs<ObjCObjectPointerType>(),
7182183700f494ec9b6701b6efe82bcb25f4c79ba561John McCall                                RHS->getAs<ObjCObjectPointerType>()))
718314108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff      return LHS;
718414108da7f7fc059772711e4ffee1322a27b152a7Steve Naroff
7185bc76dd06eb881c70c9775b74bab8b88cd747f173Steve Naroff    return QualType();
71867530c034c0c71a64c5a9173206d9742ae847af8bDavid Blaikie  }
7187ec0550fa3653d46560bf4484a2e988329c228e39Steve Naroff  }
718872564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor
71897530c034c0c71a64c5a9173206d9742ae847af8bDavid Blaikie  llvm_unreachable("Invalid Type::Class!");
7190ec0550fa3653d46560bf4484a2e988329c228e39Steve Naroff}
71917192f8e9592729882a09d84d77838db26e39ebd4Ted Kremenek
719278213e4aec6d8d22b5828dd2687fa19116ebaa26Fariborz Jahanianbool ASTContext::FunctionTypesMatchOnNSConsumedAttrs(
719378213e4aec6d8d22b5828dd2687fa19116ebaa26Fariborz Jahanian                   const FunctionProtoType *FromFunctionType,
719478213e4aec6d8d22b5828dd2687fa19116ebaa26Fariborz Jahanian                   const FunctionProtoType *ToFunctionType) {
719578213e4aec6d8d22b5828dd2687fa19116ebaa26Fariborz Jahanian  if (FromFunctionType->hasAnyConsumedArgs() !=
719678213e4aec6d8d22b5828dd2687fa19116ebaa26Fariborz Jahanian      ToFunctionType->hasAnyConsumedArgs())
719778213e4aec6d8d22b5828dd2687fa19116ebaa26Fariborz Jahanian    return false;
719878213e4aec6d8d22b5828dd2687fa19116ebaa26Fariborz Jahanian  FunctionProtoType::ExtProtoInfo FromEPI =
719978213e4aec6d8d22b5828dd2687fa19116ebaa26Fariborz Jahanian    FromFunctionType->getExtProtoInfo();
720078213e4aec6d8d22b5828dd2687fa19116ebaa26Fariborz Jahanian  FunctionProtoType::ExtProtoInfo ToEPI =
720178213e4aec6d8d22b5828dd2687fa19116ebaa26Fariborz Jahanian    ToFunctionType->getExtProtoInfo();
720278213e4aec6d8d22b5828dd2687fa19116ebaa26Fariborz Jahanian  if (FromEPI.ConsumedArguments && ToEPI.ConsumedArguments)
720378213e4aec6d8d22b5828dd2687fa19116ebaa26Fariborz Jahanian    for (unsigned ArgIdx = 0, NumArgs = FromFunctionType->getNumArgs();
720478213e4aec6d8d22b5828dd2687fa19116ebaa26Fariborz Jahanian         ArgIdx != NumArgs; ++ArgIdx)  {
720578213e4aec6d8d22b5828dd2687fa19116ebaa26Fariborz Jahanian      if (FromEPI.ConsumedArguments[ArgIdx] !=
720678213e4aec6d8d22b5828dd2687fa19116ebaa26Fariborz Jahanian          ToEPI.ConsumedArguments[ArgIdx])
720778213e4aec6d8d22b5828dd2687fa19116ebaa26Fariborz Jahanian        return false;
720878213e4aec6d8d22b5828dd2687fa19116ebaa26Fariborz Jahanian    }
720978213e4aec6d8d22b5828dd2687fa19116ebaa26Fariborz Jahanian  return true;
721078213e4aec6d8d22b5828dd2687fa19116ebaa26Fariborz Jahanian}
721178213e4aec6d8d22b5828dd2687fa19116ebaa26Fariborz Jahanian
72122390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian/// mergeObjCGCQualifiers - This routine merges ObjC's GC attribute of 'LHS' and
72132390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian/// 'RHS' attributes and returns the merged version; including for function
72142390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian/// return types.
72152390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz JahanianQualType ASTContext::mergeObjCGCQualifiers(QualType LHS, QualType RHS) {
72162390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian  QualType LHSCan = getCanonicalType(LHS),
72172390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian  RHSCan = getCanonicalType(RHS);
72182390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian  // If two types are identical, they are compatible.
72192390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian  if (LHSCan == RHSCan)
72202390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian    return LHS;
72212390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian  if (RHSCan->isFunctionType()) {
72222390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian    if (!LHSCan->isFunctionType())
72232390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian      return QualType();
72242390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian    QualType OldReturnType =
72252390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian      cast<FunctionType>(RHSCan.getTypePtr())->getResultType();
72262390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian    QualType NewReturnType =
72272390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian      cast<FunctionType>(LHSCan.getTypePtr())->getResultType();
72282390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian    QualType ResReturnType =
72292390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian      mergeObjCGCQualifiers(NewReturnType, OldReturnType);
72302390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian    if (ResReturnType.isNull())
72312390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian      return QualType();
72322390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian    if (ResReturnType == NewReturnType || ResReturnType == OldReturnType) {
72332390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian      // id foo(); ... __strong id foo(); or: __strong id foo(); ... id foo();
72342390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian      // In either case, use OldReturnType to build the new function type.
72352390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian      const FunctionType *F = LHS->getAs<FunctionType>();
72362390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian      if (const FunctionProtoType *FPT = cast<FunctionProtoType>(F)) {
7237e23cf437fe76b1ed02d63c3f61b456fd48a915f5John McCall        FunctionProtoType::ExtProtoInfo EPI = FPT->getExtProtoInfo();
7238e23cf437fe76b1ed02d63c3f61b456fd48a915f5John McCall        EPI.ExtInfo = getFunctionExtInfo(LHS);
72392390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian        QualType ResultType
7240bea522ff43a3f11c7a2bc7949119dbb9fce19e39Jordan Rose          = getFunctionType(OldReturnType,
7241bea522ff43a3f11c7a2bc7949119dbb9fce19e39Jordan Rose                            ArrayRef<QualType>(FPT->arg_type_begin(),
7242bea522ff43a3f11c7a2bc7949119dbb9fce19e39Jordan Rose                                               FPT->getNumArgs()),
7243bea522ff43a3f11c7a2bc7949119dbb9fce19e39Jordan Rose                            EPI);
72442390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian        return ResultType;
72452390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian      }
72462390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian    }
72472390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian    return QualType();
72482390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian  }
72492390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian
72502390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian  // If the qualifiers are different, the types can still be merged.
72512390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian  Qualifiers LQuals = LHSCan.getLocalQualifiers();
72522390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian  Qualifiers RQuals = RHSCan.getLocalQualifiers();
72532390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian  if (LQuals != RQuals) {
72542390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian    // If any of these qualifiers are different, we have a type mismatch.
72552390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian    if (LQuals.getCVRQualifiers() != RQuals.getCVRQualifiers() ||
72562390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian        LQuals.getAddressSpace() != RQuals.getAddressSpace())
72572390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian      return QualType();
72582390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian
72592390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian    // Exactly one GC qualifier difference is allowed: __strong is
72602390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian    // okay if the other type has no GC qualifier but is an Objective
72612390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian    // C object pointer (i.e. implicitly strong by default).  We fix
72622390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian    // this by pretending that the unqualified type was actually
72632390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian    // qualified __strong.
72642390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian    Qualifiers::GC GC_L = LQuals.getObjCGCAttr();
72652390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian    Qualifiers::GC GC_R = RQuals.getObjCGCAttr();
72662390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian    assert((GC_L != GC_R) && "unequal qualifier sets had only equal elements");
72672390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian
72682390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian    if (GC_L == Qualifiers::Weak || GC_R == Qualifiers::Weak)
72692390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian      return QualType();
72702390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian
72712390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian    if (GC_L == Qualifiers::Strong)
72722390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian      return LHS;
72732390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian    if (GC_R == Qualifiers::Strong)
72742390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian      return RHS;
72752390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian    return QualType();
72762390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian  }
72772390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian
72782390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian  if (LHSCan->isObjCObjectPointerType() && RHSCan->isObjCObjectPointerType()) {
72792390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian    QualType LHSBaseQT = LHS->getAs<ObjCObjectPointerType>()->getPointeeType();
72802390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian    QualType RHSBaseQT = RHS->getAs<ObjCObjectPointerType>()->getPointeeType();
72812390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian    QualType ResQT = mergeObjCGCQualifiers(LHSBaseQT, RHSBaseQT);
72822390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian    if (ResQT == LHSBaseQT)
72832390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian      return LHS;
72842390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian    if (ResQT == RHSBaseQT)
72852390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian      return RHS;
72862390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian  }
72872390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian  return QualType();
72882390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian}
72892390a72a3ebd37737fec5ba1385db9c3bb22fc59Fariborz Jahanian
72905426bf6456a5aeac416a9150de157904d101c819Chris Lattner//===----------------------------------------------------------------------===//
7291ad74a758189180b8ab8faea648e4766c3bfd7fcbEli Friedman//                         Integer Predicates
7292ad74a758189180b8ab8faea648e4766c3bfd7fcbEli Friedman//===----------------------------------------------------------------------===//
729388054dee0402e4d3c1f64e6b697acc47195c0d72Chris Lattner
72944ba2a17694148e16eaa8d3917f657ffcd3667be4Jay Foadunsigned ASTContext::getIntWidth(QualType T) const {
7295f4c7371fb1d3cebcfb40abad4537bb82515704eaJohn McCall  if (const EnumType *ET = dyn_cast<EnumType>(T))
729629a7f3342c3c6dd15d914c61ae22246c36d51ce7Eli Friedman    T = ET->getDecl()->getIntegerType();
72971274ccd90aec0b205fc838c3d504821ccfb55482Douglas Gregor  if (T->isBooleanType())
72981274ccd90aec0b205fc838c3d504821ccfb55482Douglas Gregor    return 1;
7299f98aba35e6c3da5aae61843fc01334939e4e12ecEli Friedman  // For builtin types, just use the standard type sizing method
7300ad74a758189180b8ab8faea648e4766c3bfd7fcbEli Friedman  return (unsigned)getTypeSize(T);
7301ad74a758189180b8ab8faea648e4766c3bfd7fcbEli Friedman}
7302ad74a758189180b8ab8faea648e4766c3bfd7fcbEli Friedman
7303762f159c3295552f1f3e5e1af8b66385bfaed786Abramo BagnaraQualType ASTContext::getCorrespondingUnsignedType(QualType T) const {
7304f60946222721d9ba3c059563935c17b84703187aDouglas Gregor  assert(T->hasSignedIntegerRepresentation() && "Unexpected type");
73056a2b9261bf9c973c7122d9d1febce24a38fa862dChris Lattner
73066a2b9261bf9c973c7122d9d1febce24a38fa862dChris Lattner  // Turn <4 x signed int> -> <4 x unsigned int>
73076a2b9261bf9c973c7122d9d1febce24a38fa862dChris Lattner  if (const VectorType *VTy = T->getAs<VectorType>())
73086a2b9261bf9c973c7122d9d1febce24a38fa862dChris Lattner    return getVectorType(getCorrespondingUnsignedType(VTy->getElementType()),
7309e86d78cf4754a6aef2cf9a33d847aa15338e276fBob Wilson                         VTy->getNumElements(), VTy->getVectorKind());
73106a2b9261bf9c973c7122d9d1febce24a38fa862dChris Lattner
73116a2b9261bf9c973c7122d9d1febce24a38fa862dChris Lattner  // For enums, we return the unsigned version of the base type.
73126a2b9261bf9c973c7122d9d1febce24a38fa862dChris Lattner  if (const EnumType *ETy = T->getAs<EnumType>())
7313ad74a758189180b8ab8faea648e4766c3bfd7fcbEli Friedman    T = ETy->getDecl()->getIntegerType();
73146a2b9261bf9c973c7122d9d1febce24a38fa862dChris Lattner
73156a2b9261bf9c973c7122d9d1febce24a38fa862dChris Lattner  const BuiltinType *BTy = T->getAs<BuiltinType>();
73166a2b9261bf9c973c7122d9d1febce24a38fa862dChris Lattner  assert(BTy && "Unexpected signed integer type");
7317ad74a758189180b8ab8faea648e4766c3bfd7fcbEli Friedman  switch (BTy->getKind()) {
7318ad74a758189180b8ab8faea648e4766c3bfd7fcbEli Friedman  case BuiltinType::Char_S:
7319ad74a758189180b8ab8faea648e4766c3bfd7fcbEli Friedman  case BuiltinType::SChar:
7320ad74a758189180b8ab8faea648e4766c3bfd7fcbEli Friedman    return UnsignedCharTy;
7321ad74a758189180b8ab8faea648e4766c3bfd7fcbEli Friedman  case BuiltinType::Short:
7322ad74a758189180b8ab8faea648e4766c3bfd7fcbEli Friedman    return UnsignedShortTy;
7323ad74a758189180b8ab8faea648e4766c3bfd7fcbEli Friedman  case BuiltinType::Int:
7324ad74a758189180b8ab8faea648e4766c3bfd7fcbEli Friedman    return UnsignedIntTy;
7325ad74a758189180b8ab8faea648e4766c3bfd7fcbEli Friedman  case BuiltinType::Long:
7326ad74a758189180b8ab8faea648e4766c3bfd7fcbEli Friedman    return UnsignedLongTy;
7327ad74a758189180b8ab8faea648e4766c3bfd7fcbEli Friedman  case BuiltinType::LongLong:
7328ad74a758189180b8ab8faea648e4766c3bfd7fcbEli Friedman    return UnsignedLongLongTy;
73292df9ced9fd1e8c7d7b38443db07e0e811de22571Chris Lattner  case BuiltinType::Int128:
73302df9ced9fd1e8c7d7b38443db07e0e811de22571Chris Lattner    return UnsignedInt128Ty;
7331ad74a758189180b8ab8faea648e4766c3bfd7fcbEli Friedman  default:
7332b219cfc4d75f0a03630b7c4509ef791b7e97b2c8David Blaikie    llvm_unreachable("Unexpected signed integer type");
7333ad74a758189180b8ab8faea648e4766c3bfd7fcbEli Friedman  }
7334ad74a758189180b8ab8faea648e4766c3bfd7fcbEli Friedman}
7335ad74a758189180b8ab8faea648e4766c3bfd7fcbEli Friedman
73367b90340c9c7d07aef4e301e72b5e8a30d5f4f0c8Argyrios KyrtzidisASTMutationListener::~ASTMutationListener() { }
73377b90340c9c7d07aef4e301e72b5e8a30d5f4f0c8Argyrios Kyrtzidis
733886df27bbdbb98c39ec2184695c0561209f91beddChris Lattner
733986df27bbdbb98c39ec2184695c0561209f91beddChris Lattner//===----------------------------------------------------------------------===//
734086df27bbdbb98c39ec2184695c0561209f91beddChris Lattner//                          Builtin Type Computation
734186df27bbdbb98c39ec2184695c0561209f91beddChris Lattner//===----------------------------------------------------------------------===//
734286df27bbdbb98c39ec2184695c0561209f91beddChris Lattner
734386df27bbdbb98c39ec2184695c0561209f91beddChris Lattner/// DecodeTypeFromStr - This decodes one type descriptor from Str, advancing the
734433daae66462e8f51ee153463b32bdefd60c801aaChris Lattner/// pointer over the consumed characters.  This returns the resultant type.  If
734533daae66462e8f51ee153463b32bdefd60c801aaChris Lattner/// AllowTypeModifiers is false then modifier like * are not parsed, just basic
734633daae66462e8f51ee153463b32bdefd60c801aaChris Lattner/// types.  This allows "v2i*" to be parsed as a pointer to a v2i instead of
734733daae66462e8f51ee153463b32bdefd60c801aaChris Lattner/// a vector of "i*".
734814e0e7436cf6650a72052baea1f8ebe644cef489Chris Lattner///
734914e0e7436cf6650a72052baea1f8ebe644cef489Chris Lattner/// RequiresICE is filled in on return to indicate whether the value is required
735014e0e7436cf6650a72052baea1f8ebe644cef489Chris Lattner/// to be an Integer Constant Expression.
73514ba2a17694148e16eaa8d3917f657ffcd3667be4Jay Foadstatic QualType DecodeTypeFromStr(const char *&Str, const ASTContext &Context,
735286df27bbdbb98c39ec2184695c0561209f91beddChris Lattner                                  ASTContext::GetBuiltinTypeError &Error,
735314e0e7436cf6650a72052baea1f8ebe644cef489Chris Lattner                                  bool &RequiresICE,
735433daae66462e8f51ee153463b32bdefd60c801aaChris Lattner                                  bool AllowTypeModifiers) {
735586df27bbdbb98c39ec2184695c0561209f91beddChris Lattner  // Modifiers.
735686df27bbdbb98c39ec2184695c0561209f91beddChris Lattner  int HowLong = 0;
735786df27bbdbb98c39ec2184695c0561209f91beddChris Lattner  bool Signed = false, Unsigned = false;
735814e0e7436cf6650a72052baea1f8ebe644cef489Chris Lattner  RequiresICE = false;
7359393bd8e185692a451b2ba16bdfc9e7d3543b4217Chris Lattner
736033daae66462e8f51ee153463b32bdefd60c801aaChris Lattner  // Read the prefixed modifiers first.
736186df27bbdbb98c39ec2184695c0561209f91beddChris Lattner  bool Done = false;
736286df27bbdbb98c39ec2184695c0561209f91beddChris Lattner  while (!Done) {
736386df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    switch (*Str++) {
73641eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    default: Done = true; --Str; break;
7365393bd8e185692a451b2ba16bdfc9e7d3543b4217Chris Lattner    case 'I':
736614e0e7436cf6650a72052baea1f8ebe644cef489Chris Lattner      RequiresICE = true;
7367393bd8e185692a451b2ba16bdfc9e7d3543b4217Chris Lattner      break;
736886df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    case 'S':
736986df27bbdbb98c39ec2184695c0561209f91beddChris Lattner      assert(!Unsigned && "Can't use both 'S' and 'U' modifiers!");
737086df27bbdbb98c39ec2184695c0561209f91beddChris Lattner      assert(!Signed && "Can't use 'S' modifier multiple times!");
737186df27bbdbb98c39ec2184695c0561209f91beddChris Lattner      Signed = true;
737286df27bbdbb98c39ec2184695c0561209f91beddChris Lattner      break;
737386df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    case 'U':
737486df27bbdbb98c39ec2184695c0561209f91beddChris Lattner      assert(!Signed && "Can't use both 'S' and 'U' modifiers!");
737586df27bbdbb98c39ec2184695c0561209f91beddChris Lattner      assert(!Unsigned && "Can't use 'S' modifier multiple times!");
737686df27bbdbb98c39ec2184695c0561209f91beddChris Lattner      Unsigned = true;
737786df27bbdbb98c39ec2184695c0561209f91beddChris Lattner      break;
737886df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    case 'L':
737986df27bbdbb98c39ec2184695c0561209f91beddChris Lattner      assert(HowLong <= 2 && "Can't have LLLL modifier");
738086df27bbdbb98c39ec2184695c0561209f91beddChris Lattner      ++HowLong;
738186df27bbdbb98c39ec2184695c0561209f91beddChris Lattner      break;
738286df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    }
738386df27bbdbb98c39ec2184695c0561209f91beddChris Lattner  }
738486df27bbdbb98c39ec2184695c0561209f91beddChris Lattner
738586df27bbdbb98c39ec2184695c0561209f91beddChris Lattner  QualType Type;
73861eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
738786df27bbdbb98c39ec2184695c0561209f91beddChris Lattner  // Read the base type.
738886df27bbdbb98c39ec2184695c0561209f91beddChris Lattner  switch (*Str++) {
7389b219cfc4d75f0a03630b7c4509ef791b7e97b2c8David Blaikie  default: llvm_unreachable("Unknown builtin type letter!");
739086df27bbdbb98c39ec2184695c0561209f91beddChris Lattner  case 'v':
739186df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    assert(HowLong == 0 && !Signed && !Unsigned &&
739286df27bbdbb98c39ec2184695c0561209f91beddChris Lattner           "Bad modifiers used with 'v'!");
739386df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    Type = Context.VoidTy;
739486df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    break;
739586df27bbdbb98c39ec2184695c0561209f91beddChris Lattner  case 'f':
739686df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    assert(HowLong == 0 && !Signed && !Unsigned &&
739786df27bbdbb98c39ec2184695c0561209f91beddChris Lattner           "Bad modifiers used with 'f'!");
739886df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    Type = Context.FloatTy;
739986df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    break;
740086df27bbdbb98c39ec2184695c0561209f91beddChris Lattner  case 'd':
740186df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    assert(HowLong < 2 && !Signed && !Unsigned &&
740286df27bbdbb98c39ec2184695c0561209f91beddChris Lattner           "Bad modifiers used with 'd'!");
740386df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    if (HowLong)
740486df27bbdbb98c39ec2184695c0561209f91beddChris Lattner      Type = Context.LongDoubleTy;
740586df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    else
740686df27bbdbb98c39ec2184695c0561209f91beddChris Lattner      Type = Context.DoubleTy;
740786df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    break;
740886df27bbdbb98c39ec2184695c0561209f91beddChris Lattner  case 's':
740986df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    assert(HowLong == 0 && "Bad modifiers used with 's'!");
741086df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    if (Unsigned)
741186df27bbdbb98c39ec2184695c0561209f91beddChris Lattner      Type = Context.UnsignedShortTy;
741286df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    else
741386df27bbdbb98c39ec2184695c0561209f91beddChris Lattner      Type = Context.ShortTy;
741486df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    break;
741586df27bbdbb98c39ec2184695c0561209f91beddChris Lattner  case 'i':
741686df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    if (HowLong == 3)
741786df27bbdbb98c39ec2184695c0561209f91beddChris Lattner      Type = Unsigned ? Context.UnsignedInt128Ty : Context.Int128Ty;
741886df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    else if (HowLong == 2)
741986df27bbdbb98c39ec2184695c0561209f91beddChris Lattner      Type = Unsigned ? Context.UnsignedLongLongTy : Context.LongLongTy;
742086df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    else if (HowLong == 1)
742186df27bbdbb98c39ec2184695c0561209f91beddChris Lattner      Type = Unsigned ? Context.UnsignedLongTy : Context.LongTy;
742286df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    else
742386df27bbdbb98c39ec2184695c0561209f91beddChris Lattner      Type = Unsigned ? Context.UnsignedIntTy : Context.IntTy;
742486df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    break;
742586df27bbdbb98c39ec2184695c0561209f91beddChris Lattner  case 'c':
742686df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    assert(HowLong == 0 && "Bad modifiers used with 'c'!");
742786df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    if (Signed)
742886df27bbdbb98c39ec2184695c0561209f91beddChris Lattner      Type = Context.SignedCharTy;
742986df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    else if (Unsigned)
743086df27bbdbb98c39ec2184695c0561209f91beddChris Lattner      Type = Context.UnsignedCharTy;
743186df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    else
743286df27bbdbb98c39ec2184695c0561209f91beddChris Lattner      Type = Context.CharTy;
743386df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    break;
743486df27bbdbb98c39ec2184695c0561209f91beddChris Lattner  case 'b': // boolean
743586df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    assert(HowLong == 0 && !Signed && !Unsigned && "Bad modifiers for 'b'!");
743686df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    Type = Context.BoolTy;
743786df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    break;
743886df27bbdbb98c39ec2184695c0561209f91beddChris Lattner  case 'z':  // size_t.
743986df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    assert(HowLong == 0 && !Signed && !Unsigned && "Bad modifiers for 'z'!");
744086df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    Type = Context.getSizeType();
744186df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    break;
744286df27bbdbb98c39ec2184695c0561209f91beddChris Lattner  case 'F':
744386df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    Type = Context.getCFConstantStringType();
744486df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    break;
7445ba8bda05fefd3bb2f1ef201784b685f715bdde29Fariborz Jahanian  case 'G':
7446ba8bda05fefd3bb2f1ef201784b685f715bdde29Fariborz Jahanian    Type = Context.getObjCIdType();
7447ba8bda05fefd3bb2f1ef201784b685f715bdde29Fariborz Jahanian    break;
7448ba8bda05fefd3bb2f1ef201784b685f715bdde29Fariborz Jahanian  case 'H':
7449ba8bda05fefd3bb2f1ef201784b685f715bdde29Fariborz Jahanian    Type = Context.getObjCSelType();
7450ba8bda05fefd3bb2f1ef201784b685f715bdde29Fariborz Jahanian    break;
7451f7992132207d7ddc8810a135cec97ebe5805e500Fariborz Jahanian  case 'M':
7452f7992132207d7ddc8810a135cec97ebe5805e500Fariborz Jahanian    Type = Context.getObjCSuperType();
7453f7992132207d7ddc8810a135cec97ebe5805e500Fariborz Jahanian    break;
745486df27bbdbb98c39ec2184695c0561209f91beddChris Lattner  case 'a':
745586df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    Type = Context.getBuiltinVaListType();
745686df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    assert(!Type.isNull() && "builtin va list type not initialized!");
745786df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    break;
745886df27bbdbb98c39ec2184695c0561209f91beddChris Lattner  case 'A':
745986df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    // This is a "reference" to a va_list; however, what exactly
746086df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    // this means depends on how va_list is defined. There are two
746186df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    // different kinds of va_list: ones passed by value, and ones
746286df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    // passed by reference.  An example of a by-value va_list is
746386df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    // x86, where va_list is a char*. An example of by-ref va_list
746486df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    // is x86-64, where va_list is a __va_list_tag[1]. For x86,
746586df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    // we want this argument to be a char*&; for x86-64, we want
746686df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    // it to be a __va_list_tag*.
746786df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    Type = Context.getBuiltinVaListType();
746886df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    assert(!Type.isNull() && "builtin va list type not initialized!");
746914e0e7436cf6650a72052baea1f8ebe644cef489Chris Lattner    if (Type->isArrayType())
747086df27bbdbb98c39ec2184695c0561209f91beddChris Lattner      Type = Context.getArrayDecayedType(Type);
747114e0e7436cf6650a72052baea1f8ebe644cef489Chris Lattner    else
747286df27bbdbb98c39ec2184695c0561209f91beddChris Lattner      Type = Context.getLValueReferenceType(Type);
747386df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    break;
747486df27bbdbb98c39ec2184695c0561209f91beddChris Lattner  case 'V': {
747586df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    char *End;
747686df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    unsigned NumElements = strtoul(Str, &End, 10);
747786df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    assert(End != Str && "Missing vector size");
747886df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    Str = End;
74791eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
748014e0e7436cf6650a72052baea1f8ebe644cef489Chris Lattner    QualType ElementType = DecodeTypeFromStr(Str, Context, Error,
748114e0e7436cf6650a72052baea1f8ebe644cef489Chris Lattner                                             RequiresICE, false);
748214e0e7436cf6650a72052baea1f8ebe644cef489Chris Lattner    assert(!RequiresICE && "Can't require vector ICE");
748333daae66462e8f51ee153463b32bdefd60c801aaChris Lattner
748433daae66462e8f51ee153463b32bdefd60c801aaChris Lattner    // TODO: No way to make AltiVec vectors in builtins yet.
7485788b0fd67e1992f23555454efcdb16a19dfefac3Chris Lattner    Type = Context.getVectorType(ElementType, NumElements,
7486e86d78cf4754a6aef2cf9a33d847aa15338e276fBob Wilson                                 VectorType::GenericVector);
748786df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    break;
748886df27bbdbb98c39ec2184695c0561209f91beddChris Lattner  }
7489b4bc99ba5540a618c86c136c9112ead8fb1d9866Douglas Gregor  case 'E': {
7490b4bc99ba5540a618c86c136c9112ead8fb1d9866Douglas Gregor    char *End;
7491b4bc99ba5540a618c86c136c9112ead8fb1d9866Douglas Gregor
7492b4bc99ba5540a618c86c136c9112ead8fb1d9866Douglas Gregor    unsigned NumElements = strtoul(Str, &End, 10);
7493b4bc99ba5540a618c86c136c9112ead8fb1d9866Douglas Gregor    assert(End != Str && "Missing vector size");
7494b4bc99ba5540a618c86c136c9112ead8fb1d9866Douglas Gregor
7495b4bc99ba5540a618c86c136c9112ead8fb1d9866Douglas Gregor    Str = End;
7496b4bc99ba5540a618c86c136c9112ead8fb1d9866Douglas Gregor
7497b4bc99ba5540a618c86c136c9112ead8fb1d9866Douglas Gregor    QualType ElementType = DecodeTypeFromStr(Str, Context, Error, RequiresICE,
7498b4bc99ba5540a618c86c136c9112ead8fb1d9866Douglas Gregor                                             false);
7499b4bc99ba5540a618c86c136c9112ead8fb1d9866Douglas Gregor    Type = Context.getExtVectorType(ElementType, NumElements);
7500b4bc99ba5540a618c86c136c9112ead8fb1d9866Douglas Gregor    break;
7501b4bc99ba5540a618c86c136c9112ead8fb1d9866Douglas Gregor  }
7502d3a23b238a2b2c0f11e6ac4951c7410a8c5717bfDouglas Gregor  case 'X': {
750314e0e7436cf6650a72052baea1f8ebe644cef489Chris Lattner    QualType ElementType = DecodeTypeFromStr(Str, Context, Error, RequiresICE,
750414e0e7436cf6650a72052baea1f8ebe644cef489Chris Lattner                                             false);
750514e0e7436cf6650a72052baea1f8ebe644cef489Chris Lattner    assert(!RequiresICE && "Can't require complex ICE");
7506d3a23b238a2b2c0f11e6ac4951c7410a8c5717bfDouglas Gregor    Type = Context.getComplexType(ElementType);
7507d3a23b238a2b2c0f11e6ac4951c7410a8c5717bfDouglas Gregor    break;
7508cc075e4092900ac9eec9dc92e35096eae1ee29c7Fariborz Jahanian  }
7509cc075e4092900ac9eec9dc92e35096eae1ee29c7Fariborz Jahanian  case 'Y' : {
7510cc075e4092900ac9eec9dc92e35096eae1ee29c7Fariborz Jahanian    Type = Context.getPointerDiffType();
7511cc075e4092900ac9eec9dc92e35096eae1ee29c7Fariborz Jahanian    break;
7512cc075e4092900ac9eec9dc92e35096eae1ee29c7Fariborz Jahanian  }
75139a5a7e7351f78345a72c4956af25590f6d40ebcdChris Lattner  case 'P':
7514c29f77b769bcc5b6dc85e72c8e3cc2e348e5cf25Douglas Gregor    Type = Context.getFILEType();
7515c29f77b769bcc5b6dc85e72c8e3cc2e348e5cf25Douglas Gregor    if (Type.isNull()) {
7516f711c41dd9412a8182793259d355c4f6979ed5edMike Stump      Error = ASTContext::GE_Missing_stdio;
751786df27bbdbb98c39ec2184695c0561209f91beddChris Lattner      return QualType();
751886df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    }
7519fd612dbb23cd31c03c898ae53ff18d0dfd8488f9Mike Stump    break;
75209a5a7e7351f78345a72c4956af25590f6d40ebcdChris Lattner  case 'J':
7521f711c41dd9412a8182793259d355c4f6979ed5edMike Stump    if (Signed)
7522782fa308a765aeac2acb39c4e697c937ec21185bMike Stump      Type = Context.getsigjmp_bufType();
7523f711c41dd9412a8182793259d355c4f6979ed5edMike Stump    else
7524f711c41dd9412a8182793259d355c4f6979ed5edMike Stump      Type = Context.getjmp_bufType();
7525f711c41dd9412a8182793259d355c4f6979ed5edMike Stump
7526fd612dbb23cd31c03c898ae53ff18d0dfd8488f9Mike Stump    if (Type.isNull()) {
7527f711c41dd9412a8182793259d355c4f6979ed5edMike Stump      Error = ASTContext::GE_Missing_setjmp;
7528fd612dbb23cd31c03c898ae53ff18d0dfd8488f9Mike Stump      return QualType();
7529fd612dbb23cd31c03c898ae53ff18d0dfd8488f9Mike Stump    }
7530fd612dbb23cd31c03c898ae53ff18d0dfd8488f9Mike Stump    break;
7531e2d4f4ed44a32f179c6d48cd1dba8346ab2129d9Rafael Espindola  case 'K':
7532e2d4f4ed44a32f179c6d48cd1dba8346ab2129d9Rafael Espindola    assert(HowLong == 0 && !Signed && !Unsigned && "Bad modifiers for 'K'!");
7533e2d4f4ed44a32f179c6d48cd1dba8346ab2129d9Rafael Espindola    Type = Context.getucontext_tType();
7534e2d4f4ed44a32f179c6d48cd1dba8346ab2129d9Rafael Espindola
7535e2d4f4ed44a32f179c6d48cd1dba8346ab2129d9Rafael Espindola    if (Type.isNull()) {
7536e2d4f4ed44a32f179c6d48cd1dba8346ab2129d9Rafael Espindola      Error = ASTContext::GE_Missing_ucontext;
7537e2d4f4ed44a32f179c6d48cd1dba8346ab2129d9Rafael Espindola      return QualType();
7538e2d4f4ed44a32f179c6d48cd1dba8346ab2129d9Rafael Espindola    }
7539e2d4f4ed44a32f179c6d48cd1dba8346ab2129d9Rafael Espindola    break;
75406902e4146d426998ff6a94b16776c1b3f805a048Eli Friedman  case 'p':
75416902e4146d426998ff6a94b16776c1b3f805a048Eli Friedman    Type = Context.getProcessIDType();
75426902e4146d426998ff6a94b16776c1b3f805a048Eli Friedman    break;
7543782fa308a765aeac2acb39c4e697c937ec21185bMike Stump  }
75441eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
754533daae66462e8f51ee153463b32bdefd60c801aaChris Lattner  // If there are modifiers and if we're allowed to parse them, go for it.
754633daae66462e8f51ee153463b32bdefd60c801aaChris Lattner  Done = !AllowTypeModifiers;
754786df27bbdbb98c39ec2184695c0561209f91beddChris Lattner  while (!Done) {
7548187ab37a05b8f7015b9f39cc8cd9129a0c6d0b48John McCall    switch (char c = *Str++) {
754933daae66462e8f51ee153463b32bdefd60c801aaChris Lattner    default: Done = true; --Str; break;
755033daae66462e8f51ee153463b32bdefd60c801aaChris Lattner    case '*':
755133daae66462e8f51ee153463b32bdefd60c801aaChris Lattner    case '&': {
755233daae66462e8f51ee153463b32bdefd60c801aaChris Lattner      // Both pointers and references can have their pointee types
755333daae66462e8f51ee153463b32bdefd60c801aaChris Lattner      // qualified with an address space.
755433daae66462e8f51ee153463b32bdefd60c801aaChris Lattner      char *End;
755533daae66462e8f51ee153463b32bdefd60c801aaChris Lattner      unsigned AddrSpace = strtoul(Str, &End, 10);
755633daae66462e8f51ee153463b32bdefd60c801aaChris Lattner      if (End != Str && AddrSpace != 0) {
755733daae66462e8f51ee153463b32bdefd60c801aaChris Lattner        Type = Context.getAddrSpaceQualType(Type, AddrSpace);
755833daae66462e8f51ee153463b32bdefd60c801aaChris Lattner        Str = End;
755933daae66462e8f51ee153463b32bdefd60c801aaChris Lattner      }
756033daae66462e8f51ee153463b32bdefd60c801aaChris Lattner      if (c == '*')
756133daae66462e8f51ee153463b32bdefd60c801aaChris Lattner        Type = Context.getPointerType(Type);
756233daae66462e8f51ee153463b32bdefd60c801aaChris Lattner      else
756333daae66462e8f51ee153463b32bdefd60c801aaChris Lattner        Type = Context.getLValueReferenceType(Type);
756433daae66462e8f51ee153463b32bdefd60c801aaChris Lattner      break;
756533daae66462e8f51ee153463b32bdefd60c801aaChris Lattner    }
756633daae66462e8f51ee153463b32bdefd60c801aaChris Lattner    // FIXME: There's no way to have a built-in with an rvalue ref arg.
756733daae66462e8f51ee153463b32bdefd60c801aaChris Lattner    case 'C':
756833daae66462e8f51ee153463b32bdefd60c801aaChris Lattner      Type = Type.withConst();
756933daae66462e8f51ee153463b32bdefd60c801aaChris Lattner      break;
757033daae66462e8f51ee153463b32bdefd60c801aaChris Lattner    case 'D':
757133daae66462e8f51ee153463b32bdefd60c801aaChris Lattner      Type = Context.getVolatileType(Type);
757233daae66462e8f51ee153463b32bdefd60c801aaChris Lattner      break;
757318932a0f2a94a7813ec461d1118c39ecf8aa936fTed Kremenek    case 'R':
757418932a0f2a94a7813ec461d1118c39ecf8aa936fTed Kremenek      Type = Type.withRestrict();
757518932a0f2a94a7813ec461d1118c39ecf8aa936fTed Kremenek      break;
757686df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    }
757786df27bbdbb98c39ec2184695c0561209f91beddChris Lattner  }
7578393bd8e185692a451b2ba16bdfc9e7d3543b4217Chris Lattner
757914e0e7436cf6650a72052baea1f8ebe644cef489Chris Lattner  assert((!RequiresICE || Type->isIntegralOrEnumerationType()) &&
7580393bd8e185692a451b2ba16bdfc9e7d3543b4217Chris Lattner         "Integer constant 'I' type must be an integer");
75811eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
758286df27bbdbb98c39ec2184695c0561209f91beddChris Lattner  return Type;
758386df27bbdbb98c39ec2184695c0561209f91beddChris Lattner}
758486df27bbdbb98c39ec2184695c0561209f91beddChris Lattner
758586df27bbdbb98c39ec2184695c0561209f91beddChris Lattner/// GetBuiltinType - Return the type for the specified builtin.
758633daae66462e8f51ee153463b32bdefd60c801aaChris LattnerQualType ASTContext::GetBuiltinType(unsigned Id,
758714e0e7436cf6650a72052baea1f8ebe644cef489Chris Lattner                                    GetBuiltinTypeError &Error,
75884ba2a17694148e16eaa8d3917f657ffcd3667be4Jay Foad                                    unsigned *IntegerConstantArgs) const {
758933daae66462e8f51ee153463b32bdefd60c801aaChris Lattner  const char *TypeStr = BuiltinInfo.GetTypeString(Id);
75901eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
75915f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner  SmallVector<QualType, 8> ArgTypes;
75921eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
759314e0e7436cf6650a72052baea1f8ebe644cef489Chris Lattner  bool RequiresICE = false;
759486df27bbdbb98c39ec2184695c0561209f91beddChris Lattner  Error = GE_None;
759514e0e7436cf6650a72052baea1f8ebe644cef489Chris Lattner  QualType ResType = DecodeTypeFromStr(TypeStr, *this, Error,
759614e0e7436cf6650a72052baea1f8ebe644cef489Chris Lattner                                       RequiresICE, true);
759786df27bbdbb98c39ec2184695c0561209f91beddChris Lattner  if (Error != GE_None)
759886df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    return QualType();
759914e0e7436cf6650a72052baea1f8ebe644cef489Chris Lattner
760014e0e7436cf6650a72052baea1f8ebe644cef489Chris Lattner  assert(!RequiresICE && "Result of intrinsic cannot be required to be an ICE");
760114e0e7436cf6650a72052baea1f8ebe644cef489Chris Lattner
760286df27bbdbb98c39ec2184695c0561209f91beddChris Lattner  while (TypeStr[0] && TypeStr[0] != '.') {
760314e0e7436cf6650a72052baea1f8ebe644cef489Chris Lattner    QualType Ty = DecodeTypeFromStr(TypeStr, *this, Error, RequiresICE, true);
760486df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    if (Error != GE_None)
760586df27bbdbb98c39ec2184695c0561209f91beddChris Lattner      return QualType();
760686df27bbdbb98c39ec2184695c0561209f91beddChris Lattner
760714e0e7436cf6650a72052baea1f8ebe644cef489Chris Lattner    // If this argument is required to be an IntegerConstantExpression and the
760814e0e7436cf6650a72052baea1f8ebe644cef489Chris Lattner    // caller cares, fill in the bitmask we return.
760914e0e7436cf6650a72052baea1f8ebe644cef489Chris Lattner    if (RequiresICE && IntegerConstantArgs)
761014e0e7436cf6650a72052baea1f8ebe644cef489Chris Lattner      *IntegerConstantArgs |= 1 << ArgTypes.size();
761114e0e7436cf6650a72052baea1f8ebe644cef489Chris Lattner
761286df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    // Do array -> pointer decay.  The builtin should use the decayed type.
761386df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    if (Ty->isArrayType())
761486df27bbdbb98c39ec2184695c0561209f91beddChris Lattner      Ty = getArrayDecayedType(Ty);
76151eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
761686df27bbdbb98c39ec2184695c0561209f91beddChris Lattner    ArgTypes.push_back(Ty);
761786df27bbdbb98c39ec2184695c0561209f91beddChris Lattner  }
761886df27bbdbb98c39ec2184695c0561209f91beddChris Lattner
761986df27bbdbb98c39ec2184695c0561209f91beddChris Lattner  assert((TypeStr[0] != '.' || TypeStr[1] == 0) &&
762086df27bbdbb98c39ec2184695c0561209f91beddChris Lattner         "'.' should only occur at end of builtin type list!");
762186df27bbdbb98c39ec2184695c0561209f91beddChris Lattner
762200ccbefcffeb88ea3e2e6323e594fa968753ad14John McCall  FunctionType::ExtInfo EI;
762300ccbefcffeb88ea3e2e6323e594fa968753ad14John McCall  if (BuiltinInfo.isNoReturn(Id)) EI = EI.withNoReturn(true);
762400ccbefcffeb88ea3e2e6323e594fa968753ad14John McCall
762500ccbefcffeb88ea3e2e6323e594fa968753ad14John McCall  bool Variadic = (TypeStr[0] == '.');
762600ccbefcffeb88ea3e2e6323e594fa968753ad14John McCall
762700ccbefcffeb88ea3e2e6323e594fa968753ad14John McCall  // We really shouldn't be making a no-proto type here, especially in C++.
762800ccbefcffeb88ea3e2e6323e594fa968753ad14John McCall  if (ArgTypes.empty() && Variadic)
762900ccbefcffeb88ea3e2e6323e594fa968753ad14John McCall    return getFunctionNoProtoType(ResType, EI);
7630ce056bcaa1c97b89a4b2de2112c62d060863be2bDouglas Gregor
7631e23cf437fe76b1ed02d63c3f61b456fd48a915f5John McCall  FunctionProtoType::ExtProtoInfo EPI;
763200ccbefcffeb88ea3e2e6323e594fa968753ad14John McCall  EPI.ExtInfo = EI;
763300ccbefcffeb88ea3e2e6323e594fa968753ad14John McCall  EPI.Variadic = Variadic;
7634e23cf437fe76b1ed02d63c3f61b456fd48a915f5John McCall
7635bea522ff43a3f11c7a2bc7949119dbb9fce19e39Jordan Rose  return getFunctionType(ResType, ArgTypes, EPI);
763686df27bbdbb98c39ec2184695c0561209f91beddChris Lattner}
7637a95d75769edae299816ec7fd9bbcdf1ef617c5c9Eli Friedman
763890e99a84ddd020e8fda79643748243725a2ed071Argyrios KyrtzidisGVALinkage ASTContext::GetGVALinkageForFunction(const FunctionDecl *FD) {
763990e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis  GVALinkage External = GVA_StrongExternal;
764090e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis
764190e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis  Linkage L = FD->getLinkage();
764290e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis  switch (L) {
764390e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis  case NoLinkage:
764490e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis  case InternalLinkage:
764590e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis  case UniqueExternalLinkage:
764690e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis    return GVA_Internal;
764790e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis
764890e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis  case ExternalLinkage:
764990e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis    switch (FD->getTemplateSpecializationKind()) {
765090e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis    case TSK_Undeclared:
765190e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis    case TSK_ExplicitSpecialization:
765290e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis      External = GVA_StrongExternal;
765390e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis      break;
765490e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis
765590e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis    case TSK_ExplicitInstantiationDefinition:
765690e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis      return GVA_ExplicitTemplateInstantiation;
765790e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis
765890e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis    case TSK_ExplicitInstantiationDeclaration:
765990e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis    case TSK_ImplicitInstantiation:
766090e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis      External = GVA_TemplateInstantiation;
766190e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis      break;
766290e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis    }
766390e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis  }
766490e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis
766590e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis  if (!FD->isInlined())
766690e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis    return External;
766790e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis
76684e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie  if (!getLangOpts().CPlusPlus || FD->hasAttr<GNUInlineAttr>()) {
766990e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis    // GNU or C99 inline semantics. Determine whether this symbol should be
767090e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis    // externally visible.
767190e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis    if (FD->isInlineDefinitionExternallyVisible())
767290e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis      return External;
767390e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis
767490e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis    // C99 inline semantics, where the symbol is not externally visible.
767590e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis    return GVA_C99Inline;
767690e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis  }
767790e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis
767890e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis  // C++0x [temp.explicit]p9:
767990e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis  //   [ Note: The intent is that an inline function that is the subject of
768090e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis  //   an explicit instantiation declaration will still be implicitly
768190e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis  //   instantiated when used so that the body can be considered for
768290e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis  //   inlining, but that no out-of-line copy of the inline function would be
768390e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis  //   generated in the translation unit. -- end note ]
768490e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis  if (FD->getTemplateSpecializationKind()
768590e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis                                       == TSK_ExplicitInstantiationDeclaration)
768690e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis    return GVA_C99Inline;
768790e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis
768890e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis  return GVA_CXXInline;
768990e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis}
769090e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis
769190e99a84ddd020e8fda79643748243725a2ed071Argyrios KyrtzidisGVALinkage ASTContext::GetGVALinkageForVariable(const VarDecl *VD) {
769290e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis  // If this is a static data member, compute the kind of template
769390e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis  // specialization. Otherwise, this variable is not part of a
769490e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis  // template.
769590e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis  TemplateSpecializationKind TSK = TSK_Undeclared;
769690e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis  if (VD->isStaticDataMember())
769790e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis    TSK = VD->getTemplateSpecializationKind();
769890e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis
769990e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis  Linkage L = VD->getLinkage();
770090e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis
770190e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis  switch (L) {
770290e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis  case NoLinkage:
770390e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis  case InternalLinkage:
770490e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis  case UniqueExternalLinkage:
770590e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis    return GVA_Internal;
770690e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis
770790e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis  case ExternalLinkage:
770890e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis    switch (TSK) {
770990e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis    case TSK_Undeclared:
771090e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis    case TSK_ExplicitSpecialization:
771190e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis      return GVA_StrongExternal;
771290e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis
771390e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis    case TSK_ExplicitInstantiationDeclaration:
771490e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis      llvm_unreachable("Variable should not be instantiated");
771590e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis      // Fall through to treat this like any other instantiation.
771690e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis
771790e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis    case TSK_ExplicitInstantiationDefinition:
771890e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis      return GVA_ExplicitTemplateInstantiation;
771990e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis
772090e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis    case TSK_ImplicitInstantiation:
772190e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis      return GVA_TemplateInstantiation;
772290e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis    }
772390e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis  }
772490e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis
77257530c034c0c71a64c5a9173206d9742ae847af8bDavid Blaikie  llvm_unreachable("Invalid Linkage!");
772690e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis}
772790e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis
77284ac7c0bb39696e92fd220118fedc484c09a69870Argyrios Kyrtzidisbool ASTContext::DeclMustBeEmitted(const Decl *D) {
772990e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis  if (const VarDecl *VD = dyn_cast<VarDecl>(D)) {
773090e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis    if (!VD->isFileVarDecl())
773190e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis      return false;
7732f396ad9b1fa0c74c9db16a8158c3882c9db774e2Richard Smith  } else if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
7733f396ad9b1fa0c74c9db16a8158c3882c9db774e2Richard Smith    // We never need to emit an uninstantiated function template.
7734f396ad9b1fa0c74c9db16a8158c3882c9db774e2Richard Smith    if (FD->getTemplatedKind() == FunctionDecl::TK_FunctionTemplate)
7735f396ad9b1fa0c74c9db16a8158c3882c9db774e2Richard Smith      return false;
7736f396ad9b1fa0c74c9db16a8158c3882c9db774e2Richard Smith  } else
7737f396ad9b1fa0c74c9db16a8158c3882c9db774e2Richard Smith    return false;
7738f396ad9b1fa0c74c9db16a8158c3882c9db774e2Richard Smith
7739f396ad9b1fa0c74c9db16a8158c3882c9db774e2Richard Smith  // If this is a member of a class template, we do not need to emit it.
7740f396ad9b1fa0c74c9db16a8158c3882c9db774e2Richard Smith  if (D->getDeclContext()->isDependentContext())
774190e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis    return false;
774290e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis
7743ab411c8c2efed8f2403bf8596e780c0f2f905a19Argyrios Kyrtzidis  // Weak references don't produce any output by themselves.
7744ab411c8c2efed8f2403bf8596e780c0f2f905a19Argyrios Kyrtzidis  if (D->hasAttr<WeakRefAttr>())
7745ab411c8c2efed8f2403bf8596e780c0f2f905a19Argyrios Kyrtzidis    return false;
7746ab411c8c2efed8f2403bf8596e780c0f2f905a19Argyrios Kyrtzidis
774790e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis  // Aliases and used decls are required.
774890e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis  if (D->hasAttr<AliasAttr>() || D->hasAttr<UsedAttr>())
774990e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis    return true;
775090e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis
775190e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis  if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
775290e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis    // Forward declarations aren't required.
775310620eb5164e31208fcbf0437cd79ae535ed0559Sean Hunt    if (!FD->doesThisDeclarationHaveABody())
7754dce67a70a86db8758c926a76fdd980f5369d5746Nick Lewycky      return FD->doesDeclarationForceExternallyVisibleDefinition();
775590e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis
775690e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis    // Constructors and destructors are required.
775790e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis    if (FD->hasAttr<ConstructorAttr>() || FD->hasAttr<DestructorAttr>())
775890e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis      return true;
775990e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis
7760d5617eeafc93209a26b9f88276c88cf997c3a0a7John McCall    // The key function for a class is required.  This rule only comes
7761d5617eeafc93209a26b9f88276c88cf997c3a0a7John McCall    // into play when inline functions can be key functions, though.
7762d5617eeafc93209a26b9f88276c88cf997c3a0a7John McCall    if (getTargetInfo().getCXXABI().canKeyFunctionBeInline()) {
7763d5617eeafc93209a26b9f88276c88cf997c3a0a7John McCall      if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(FD)) {
7764d5617eeafc93209a26b9f88276c88cf997c3a0a7John McCall        const CXXRecordDecl *RD = MD->getParent();
7765d5617eeafc93209a26b9f88276c88cf997c3a0a7John McCall        if (MD->isOutOfLine() && RD->isDynamicClass()) {
7766d5617eeafc93209a26b9f88276c88cf997c3a0a7John McCall          const CXXMethodDecl *KeyFunc = getCurrentKeyFunction(RD);
7767d5617eeafc93209a26b9f88276c88cf997c3a0a7John McCall          if (KeyFunc && KeyFunc->getCanonicalDecl() == MD->getCanonicalDecl())
7768d5617eeafc93209a26b9f88276c88cf997c3a0a7John McCall            return true;
7769d5617eeafc93209a26b9f88276c88cf997c3a0a7John McCall        }
777090e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis      }
777190e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis    }
777290e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis
777390e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis    GVALinkage Linkage = GetGVALinkageForFunction(FD);
777490e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis
777590e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis    // static, static inline, always_inline, and extern inline functions can
777690e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis    // always be deferred.  Normal inline functions can be deferred in C99/C++.
777790e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis    // Implicit template instantiations can also be deferred in C++.
777890e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis    if (Linkage == GVA_Internal  || Linkage == GVA_C99Inline ||
77793a5aca8112e03814f78906dc5d032287adb272b5Anton Yartsev        Linkage == GVA_CXXInline || Linkage == GVA_TemplateInstantiation)
778090e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis      return false;
778190e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis    return true;
778290e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis  }
778394da1587f7d584fc61df793229d197969f204cd9Douglas Gregor
778490e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis  const VarDecl *VD = cast<VarDecl>(D);
778590e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis  assert(VD->isFileVarDecl() && "Expected file scoped var");
778690e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis
7787ab411c8c2efed8f2403bf8596e780c0f2f905a19Argyrios Kyrtzidis  if (VD->isThisDeclarationADefinition() == VarDecl::DeclarationOnly)
7788ab411c8c2efed8f2403bf8596e780c0f2f905a19Argyrios Kyrtzidis    return false;
7789ab411c8c2efed8f2403bf8596e780c0f2f905a19Argyrios Kyrtzidis
77905f9a7e31d038843f1d3a5e2544fc365ffd563951Richard Smith  // Variables that can be needed in other TUs are required.
779190e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis  GVALinkage L = GetGVALinkageForVariable(VD);
77925f9a7e31d038843f1d3a5e2544fc365ffd563951Richard Smith  if (L != GVA_Internal && L != GVA_TemplateInstantiation)
77935f9a7e31d038843f1d3a5e2544fc365ffd563951Richard Smith    return true;
779490e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis
77955f9a7e31d038843f1d3a5e2544fc365ffd563951Richard Smith  // Variables that have destruction with side-effects are required.
77965f9a7e31d038843f1d3a5e2544fc365ffd563951Richard Smith  if (VD->getType().isDestructedType())
77975f9a7e31d038843f1d3a5e2544fc365ffd563951Richard Smith    return true;
77985f9a7e31d038843f1d3a5e2544fc365ffd563951Richard Smith
77995f9a7e31d038843f1d3a5e2544fc365ffd563951Richard Smith  // Variables that have initialization with side-effects are required.
78005f9a7e31d038843f1d3a5e2544fc365ffd563951Richard Smith  if (VD->getInit() && VD->getInit()->HasSideEffects(*this))
78015f9a7e31d038843f1d3a5e2544fc365ffd563951Richard Smith    return true;
78025f9a7e31d038843f1d3a5e2544fc365ffd563951Richard Smith
78035f9a7e31d038843f1d3a5e2544fc365ffd563951Richard Smith  return false;
780490e99a84ddd020e8fda79643748243725a2ed071Argyrios Kyrtzidis}
7805071cc7deffad608165b1ddd5263e8bf181861520Charles Davis
78068f88a1dcc57cfe8580eb1558a783ad8499bfe8e0Timur IskhodzhanovCallingConv ASTContext::getDefaultCXXMethodCallConv(bool isVariadic) {
7807ee743f903858e337434ac0335f147f4de4ecae05Charles Davis  // Pass through to the C++ ABI object
78088f88a1dcc57cfe8580eb1558a783ad8499bfe8e0Timur Iskhodzhanov  return ABI->getDefaultMethodCallConv(isVariadic);
78098f88a1dcc57cfe8580eb1558a783ad8499bfe8e0Timur Iskhodzhanov}
78108f88a1dcc57cfe8580eb1558a783ad8499bfe8e0Timur Iskhodzhanov
78118f88a1dcc57cfe8580eb1558a783ad8499bfe8e0Timur IskhodzhanovCallingConv ASTContext::getCanonicalCallConv(CallingConv CC) const {
7812b8b2c9da87e7d70a1679db026f40548b3192b705John McCall  if (CC == CC_C && !LangOpts.MRTD &&
7813b8b2c9da87e7d70a1679db026f40548b3192b705John McCall      getTargetInfo().getCXXABI().isMemberFunctionCCDefault())
78148f88a1dcc57cfe8580eb1558a783ad8499bfe8e0Timur Iskhodzhanov    return CC_Default;
78158f88a1dcc57cfe8580eb1558a783ad8499bfe8e0Timur Iskhodzhanov  return CC;
7816ee743f903858e337434ac0335f147f4de4ecae05Charles Davis}
7817ee743f903858e337434ac0335f147f4de4ecae05Charles Davis
78184ba2a17694148e16eaa8d3917f657ffcd3667be4Jay Foadbool ASTContext::isNearlyEmpty(const CXXRecordDecl *RD) const {
7819dae0cb52e4e3d46bbfc9a4510909522197a92e54Anders Carlsson  // Pass through to the C++ ABI object
7820dae0cb52e4e3d46bbfc9a4510909522197a92e54Anders Carlsson  return ABI->isNearlyEmpty(RD);
7821dae0cb52e4e3d46bbfc9a4510909522197a92e54Anders Carlsson}
7822dae0cb52e4e3d46bbfc9a4510909522197a92e54Anders Carlsson
782314110477887e3dc168ffc6c191e72d705051f99ePeter CollingbourneMangleContext *ASTContext::createMangleContext() {
7824b8b2c9da87e7d70a1679db026f40548b3192b705John McCall  switch (Target->getCXXABI().getKind()) {
7825c264e16a42b3f6c36521857a29ea0949d9781c22Tim Northover  case TargetCXXABI::GenericAArch64:
7826b8b2c9da87e7d70a1679db026f40548b3192b705John McCall  case TargetCXXABI::GenericItanium:
7827b8b2c9da87e7d70a1679db026f40548b3192b705John McCall  case TargetCXXABI::GenericARM:
7828b8b2c9da87e7d70a1679db026f40548b3192b705John McCall  case TargetCXXABI::iOS:
782914110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne    return createItaniumMangleContext(*this, getDiagnostics());
7830b8b2c9da87e7d70a1679db026f40548b3192b705John McCall  case TargetCXXABI::Microsoft:
783114110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne    return createMicrosoftMangleContext(*this, getDiagnostics());
783214110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne  }
7833b219cfc4d75f0a03630b7c4509ef791b7e97b2c8David Blaikie  llvm_unreachable("Unsupported ABI");
783414110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne}
783514110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne
7836071cc7deffad608165b1ddd5263e8bf181861520Charles DavisCXXABI::~CXXABI() {}
7837ba29bd25515fbd99e98ba0fedb9d93617b27609eTed Kremenek
7838ba29bd25515fbd99e98ba0fedb9d93617b27609eTed Kremeneksize_t ASTContext::getSideTableAllocatedMemory() const {
78390c8cd1ac55720276ad41fa7b4f8785bead5afd32Ted Kremenek  return ASTRecordLayouts.getMemorySize()
78400c8cd1ac55720276ad41fa7b4f8785bead5afd32Ted Kremenek    + llvm::capacity_in_bytes(ObjCLayouts)
78410c8cd1ac55720276ad41fa7b4f8785bead5afd32Ted Kremenek    + llvm::capacity_in_bytes(KeyFunctions)
78420c8cd1ac55720276ad41fa7b4f8785bead5afd32Ted Kremenek    + llvm::capacity_in_bytes(ObjCImpls)
78430c8cd1ac55720276ad41fa7b4f8785bead5afd32Ted Kremenek    + llvm::capacity_in_bytes(BlockVarCopyInits)
78440c8cd1ac55720276ad41fa7b4f8785bead5afd32Ted Kremenek    + llvm::capacity_in_bytes(DeclAttrs)
78450c8cd1ac55720276ad41fa7b4f8785bead5afd32Ted Kremenek    + llvm::capacity_in_bytes(InstantiatedFromStaticDataMember)
78460c8cd1ac55720276ad41fa7b4f8785bead5afd32Ted Kremenek    + llvm::capacity_in_bytes(InstantiatedFromUsingDecl)
78470c8cd1ac55720276ad41fa7b4f8785bead5afd32Ted Kremenek    + llvm::capacity_in_bytes(InstantiatedFromUsingShadowDecl)
78480c8cd1ac55720276ad41fa7b4f8785bead5afd32Ted Kremenek    + llvm::capacity_in_bytes(InstantiatedFromUnnamedFieldDecl)
78490c8cd1ac55720276ad41fa7b4f8785bead5afd32Ted Kremenek    + llvm::capacity_in_bytes(OverriddenMethods)
78500c8cd1ac55720276ad41fa7b4f8785bead5afd32Ted Kremenek    + llvm::capacity_in_bytes(Types)
7851af0f4d0b2e38c810effc8b024ad2fb6604eec5d3Francois Pichet    + llvm::capacity_in_bytes(VariableArrayTypes)
78520d95f0d7b81110f77e99e833f766d19be7b7e072Francois Pichet    + llvm::capacity_in_bytes(ClassScopeSpecializationPattern);
7853ba29bd25515fbd99e98ba0fedb9d93617b27609eTed Kremenek}
7854d211cb709510fbe7e75167b9feee0050851d001aTed Kremenek
785566cff7257698d5528632917d38f9a3037bb1506dDavid Blaikievoid ASTContext::addUnnamedTag(const TagDecl *Tag) {
785666cff7257698d5528632917d38f9a3037bb1506dDavid Blaikie  // FIXME: This mangling should be applied to function local classes too
785766cff7257698d5528632917d38f9a3037bb1506dDavid Blaikie  if (!Tag->getName().empty() || Tag->getTypedefNameForAnonDecl() ||
785866cff7257698d5528632917d38f9a3037bb1506dDavid Blaikie      !isa<CXXRecordDecl>(Tag->getParent()) || Tag->getLinkage() != ExternalLinkage)
785966cff7257698d5528632917d38f9a3037bb1506dDavid Blaikie    return;
786066cff7257698d5528632917d38f9a3037bb1506dDavid Blaikie
786166cff7257698d5528632917d38f9a3037bb1506dDavid Blaikie  std::pair<llvm::DenseMap<const DeclContext *, unsigned>::iterator, bool> P =
786266cff7257698d5528632917d38f9a3037bb1506dDavid Blaikie    UnnamedMangleContexts.insert(std::make_pair(Tag->getParent(), 0));
786366cff7257698d5528632917d38f9a3037bb1506dDavid Blaikie  UnnamedMangleNumbers.insert(std::make_pair(Tag, P.first->second++));
786466cff7257698d5528632917d38f9a3037bb1506dDavid Blaikie}
786566cff7257698d5528632917d38f9a3037bb1506dDavid Blaikie
786666cff7257698d5528632917d38f9a3037bb1506dDavid Blaikieint ASTContext::getUnnamedTagManglingNumber(const TagDecl *Tag) const {
786766cff7257698d5528632917d38f9a3037bb1506dDavid Blaikie  llvm::DenseMap<const TagDecl *, unsigned>::const_iterator I =
786866cff7257698d5528632917d38f9a3037bb1506dDavid Blaikie    UnnamedMangleNumbers.find(Tag);
786966cff7257698d5528632917d38f9a3037bb1506dDavid Blaikie  return I != UnnamedMangleNumbers.end() ? I->second : -1;
787066cff7257698d5528632917d38f9a3037bb1506dDavid Blaikie}
787166cff7257698d5528632917d38f9a3037bb1506dDavid Blaikie
78729e8c92a9c9b949bbb0408fbbd9a58e34894b6efcDouglas Gregorunsigned ASTContext::getLambdaManglingNumber(CXXMethodDecl *CallOperator) {
78739e8c92a9c9b949bbb0408fbbd9a58e34894b6efcDouglas Gregor  CXXRecordDecl *Lambda = CallOperator->getParent();
78749e8c92a9c9b949bbb0408fbbd9a58e34894b6efcDouglas Gregor  return LambdaMangleContexts[Lambda->getDeclContext()]
78759e8c92a9c9b949bbb0408fbbd9a58e34894b6efcDouglas Gregor           .getManglingNumber(CallOperator);
78769e8c92a9c9b949bbb0408fbbd9a58e34894b6efcDouglas Gregor}
78779e8c92a9c9b949bbb0408fbbd9a58e34894b6efcDouglas Gregor
78789e8c92a9c9b949bbb0408fbbd9a58e34894b6efcDouglas Gregor
7879d211cb709510fbe7e75167b9feee0050851d001aTed Kremenekvoid ASTContext::setParameterIndex(const ParmVarDecl *D, unsigned int index) {
7880d211cb709510fbe7e75167b9feee0050851d001aTed Kremenek  ParamIndices[D] = index;
7881d211cb709510fbe7e75167b9feee0050851d001aTed Kremenek}
7882d211cb709510fbe7e75167b9feee0050851d001aTed Kremenek
7883d211cb709510fbe7e75167b9feee0050851d001aTed Kremenekunsigned ASTContext::getParameterIndex(const ParmVarDecl *D) const {
7884d211cb709510fbe7e75167b9feee0050851d001aTed Kremenek  ParameterIndexTable::const_iterator I = ParamIndices.find(D);
7885d211cb709510fbe7e75167b9feee0050851d001aTed Kremenek  assert(I != ParamIndices.end() &&
7886d211cb709510fbe7e75167b9feee0050851d001aTed Kremenek         "ParmIndices lacks entry set by ParmVarDecl");
7887d211cb709510fbe7e75167b9feee0050851d001aTed Kremenek  return I->second;
7888d211cb709510fbe7e75167b9feee0050851d001aTed Kremenek}
7889