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"
28942f9fe11d3a9583eef6bc4ca2549b1f0d1694daReid Kleckner#include "clang/AST/MangleNumberingContext.h"
292fa67efeaf66a9332c30a026dc1c21bef6c33a6cBenjamin Kramer#include "clang/AST/RecordLayout.h"
30cff15128c6c089bd6fae841b80680e6f5afbf0bfReid Kleckner#include "clang/AST/RecursiveASTVisitor.h"
312fa67efeaf66a9332c30a026dc1c21bef6c33a6cBenjamin Kramer#include "clang/AST/TypeLoc.h"
32651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines#include "clang/AST/VTableBuilder.h"
331b63e4f732dbc73d90abf886b4d21f8e3a165f6dChris Lattner#include "clang/Basic/Builtins.h"
34a9376d470ccb0eac74fe09a6b2a18a890f1d17c4Chris Lattner#include "clang/Basic/SourceManager.h"
355f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer#include "clang/Basic/TargetInfo.h"
36f5942a44880be26878592eb052b737579349411eBenjamin Kramer#include "llvm/ADT/SmallString.h"
3785f9bceab1542aafff012d4d28e998f4ba16e362Anders Carlsson#include "llvm/ADT/StringExtras.h"
38e39ff50fd72e9ffaa28f141a5c9bb22f1b8f2844Robert Lytton#include "llvm/ADT/Triple.h"
392fa67efeaf66a9332c30a026dc1c21bef6c33a6cBenjamin Kramer#include "llvm/Support/Capacity.h"
406fe7c8aa8c7546743ecd0ac0138c2cf5d8155386Nate Begeman#include "llvm/Support/MathExtras.h"
41f5942a44880be26878592eb052b737579349411eBenjamin Kramer#include "llvm/Support/raw_ostream.h"
422636197098e02fd7c90f9496056b8ab886dcbff0Argyrios Kyrtzidis#include <map>
4329445a0542d128cd7ee587ee52229670b9b54a12Anders Carlsson
445f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencerusing namespace clang;
455f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
461827403a7138946305c0058f262e02b595cf882fDouglas Gregorunsigned ASTContext::NumImplicitDefaultConstructors;
471827403a7138946305c0058f262e02b595cf882fDouglas Gregorunsigned ASTContext::NumImplicitDefaultConstructorsDeclared;
48225843186e3972ce798d3de00f86da9008b15a0eDouglas Gregorunsigned ASTContext::NumImplicitCopyConstructors;
49225843186e3972ce798d3de00f86da9008b15a0eDouglas Gregorunsigned ASTContext::NumImplicitCopyConstructorsDeclared;
50ffe37fdda5b4b4f162a45155c30d9f60ce110c12Sean Huntunsigned ASTContext::NumImplicitMoveConstructors;
51ffe37fdda5b4b4f162a45155c30d9f60ce110c12Sean Huntunsigned ASTContext::NumImplicitMoveConstructorsDeclared;
52a376d10acfacf19d6dfa41069f7929739a18dd7aDouglas Gregorunsigned ASTContext::NumImplicitCopyAssignmentOperators;
53a376d10acfacf19d6dfa41069f7929739a18dd7aDouglas Gregorunsigned ASTContext::NumImplicitCopyAssignmentOperatorsDeclared;
54ffe37fdda5b4b4f162a45155c30d9f60ce110c12Sean Huntunsigned ASTContext::NumImplicitMoveAssignmentOperators;
55ffe37fdda5b4b4f162a45155c30d9f60ce110c12Sean Huntunsigned ASTContext::NumImplicitMoveAssignmentOperatorsDeclared;
564923aa25eb39d64106a5817c02d560a3aecf8b2cDouglas Gregorunsigned ASTContext::NumImplicitDestructors;
574923aa25eb39d64106a5817c02d560a3aecf8b2cDouglas Gregorunsigned ASTContext::NumImplicitDestructorsDeclared;
584923aa25eb39d64106a5817c02d560a3aecf8b2cDouglas Gregor
595f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencerenum FloatingRank {
60aa4a99b4a62615db243f7a5c433169f2fc704420Anton Korobeynikov  HalfRank, FloatRank, DoubleRank, LongDoubleRank
615f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer};
625f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
63a5ef44ff5d93a3be6ca67782828157a71894cf0cDmitri GribenkoRawComment *ASTContext::getRawCommentForDeclNoCache(const Decl *D) const {
64aa0cd85838f2a024e589ea4e8c2094130065af21Dmitri Gribenko  if (!CommentsLoaded && ExternalSource) {
65aa0cd85838f2a024e589ea4e8c2094130065af21Dmitri Gribenko    ExternalSource->ReadComments();
66651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines
67651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines#ifndef NDEBUG
68651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines    ArrayRef<RawComment *> RawComments = Comments.getComments();
69651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines    assert(std::is_sorted(RawComments.begin(), RawComments.end(),
70651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines                          BeforeThanCompare<RawComment>(SourceMgr)));
71651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines#endif
72651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines
73aa0cd85838f2a024e589ea4e8c2094130065af21Dmitri Gribenko    CommentsLoaded = true;
74aa0cd85838f2a024e589ea4e8c2094130065af21Dmitri Gribenko  }
75aa0cd85838f2a024e589ea4e8c2094130065af21Dmitri Gribenko
76aa0cd85838f2a024e589ea4e8c2094130065af21Dmitri Gribenko  assert(D);
77aa0cd85838f2a024e589ea4e8c2094130065af21Dmitri Gribenko
78c3fee3539fd00a6ce21dc1f574baf76686640072Dmitri Gribenko  // User can not attach documentation to implicit declarations.
79c3fee3539fd00a6ce21dc1f574baf76686640072Dmitri Gribenko  if (D->isImplicit())
806bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines    return nullptr;
81c3fee3539fd00a6ce21dc1f574baf76686640072Dmitri Gribenko
82c41ace950dcf2254c9aa48e73647b89c35109f80Dmitri Gribenko  // User can not attach documentation to implicit instantiations.
83c41ace950dcf2254c9aa48e73647b89c35109f80Dmitri Gribenko  if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
84c41ace950dcf2254c9aa48e73647b89c35109f80Dmitri Gribenko    if (FD->getTemplateSpecializationKind() == TSK_ImplicitInstantiation)
856bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines      return nullptr;
86c41ace950dcf2254c9aa48e73647b89c35109f80Dmitri Gribenko  }
87c41ace950dcf2254c9aa48e73647b89c35109f80Dmitri Gribenko
88dce750b15eb5eb797ac9bbea118333d7d1896831Dmitri Gribenko  if (const VarDecl *VD = dyn_cast<VarDecl>(D)) {
89dce750b15eb5eb797ac9bbea118333d7d1896831Dmitri Gribenko    if (VD->isStaticDataMember() &&
90dce750b15eb5eb797ac9bbea118333d7d1896831Dmitri Gribenko        VD->getTemplateSpecializationKind() == TSK_ImplicitInstantiation)
916bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines      return nullptr;
92dce750b15eb5eb797ac9bbea118333d7d1896831Dmitri Gribenko  }
93dce750b15eb5eb797ac9bbea118333d7d1896831Dmitri Gribenko
94dce750b15eb5eb797ac9bbea118333d7d1896831Dmitri Gribenko  if (const CXXRecordDecl *CRD = dyn_cast<CXXRecordDecl>(D)) {
95dce750b15eb5eb797ac9bbea118333d7d1896831Dmitri Gribenko    if (CRD->getTemplateSpecializationKind() == TSK_ImplicitInstantiation)
966bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines      return nullptr;
97dce750b15eb5eb797ac9bbea118333d7d1896831Dmitri Gribenko  }
98dce750b15eb5eb797ac9bbea118333d7d1896831Dmitri Gribenko
99d1e5c0df2acb01192eedda14534fe990e1e26c46Dmitri Gribenko  if (const ClassTemplateSpecializationDecl *CTSD =
100d1e5c0df2acb01192eedda14534fe990e1e26c46Dmitri Gribenko          dyn_cast<ClassTemplateSpecializationDecl>(D)) {
101d1e5c0df2acb01192eedda14534fe990e1e26c46Dmitri Gribenko    TemplateSpecializationKind TSK = CTSD->getSpecializationKind();
102d1e5c0df2acb01192eedda14534fe990e1e26c46Dmitri Gribenko    if (TSK == TSK_ImplicitInstantiation ||
103d1e5c0df2acb01192eedda14534fe990e1e26c46Dmitri Gribenko        TSK == TSK_Undeclared)
1046bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines      return nullptr;
105d1e5c0df2acb01192eedda14534fe990e1e26c46Dmitri Gribenko  }
106d1e5c0df2acb01192eedda14534fe990e1e26c46Dmitri Gribenko
107dce750b15eb5eb797ac9bbea118333d7d1896831Dmitri Gribenko  if (const EnumDecl *ED = dyn_cast<EnumDecl>(D)) {
108dce750b15eb5eb797ac9bbea118333d7d1896831Dmitri Gribenko    if (ED->getTemplateSpecializationKind() == TSK_ImplicitInstantiation)
1096bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines      return nullptr;
110dce750b15eb5eb797ac9bbea118333d7d1896831Dmitri Gribenko  }
111099ecfb0ed137665a3394187030d8fd7183fd9d4Fariborz Jahanian  if (const TagDecl *TD = dyn_cast<TagDecl>(D)) {
112099ecfb0ed137665a3394187030d8fd7183fd9d4Fariborz Jahanian    // When tag declaration (but not definition!) is part of the
113099ecfb0ed137665a3394187030d8fd7183fd9d4Fariborz Jahanian    // decl-specifier-seq of some other declaration, it doesn't get comment
114099ecfb0ed137665a3394187030d8fd7183fd9d4Fariborz Jahanian    if (TD->isEmbeddedInDeclarator() && !TD->isCompleteDefinition())
1156bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines      return nullptr;
116099ecfb0ed137665a3394187030d8fd7183fd9d4Fariborz Jahanian  }
117aa0cd85838f2a024e589ea4e8c2094130065af21Dmitri Gribenko  // TODO: handle comments for function parameters properly.
118aa0cd85838f2a024e589ea4e8c2094130065af21Dmitri Gribenko  if (isa<ParmVarDecl>(D))
1196bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines    return nullptr;
120aa0cd85838f2a024e589ea4e8c2094130065af21Dmitri Gribenko
12196b098674908eaa59a9128f3305cda6fbbdad563Dmitri Gribenko  // TODO: we could look up template parameter documentation in the template
12296b098674908eaa59a9128f3305cda6fbbdad563Dmitri Gribenko  // documentation.
12396b098674908eaa59a9128f3305cda6fbbdad563Dmitri Gribenko  if (isa<TemplateTypeParmDecl>(D) ||
12496b098674908eaa59a9128f3305cda6fbbdad563Dmitri Gribenko      isa<NonTypeTemplateParmDecl>(D) ||
12596b098674908eaa59a9128f3305cda6fbbdad563Dmitri Gribenko      isa<TemplateTemplateParmDecl>(D))
1266bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines    return nullptr;
12796b098674908eaa59a9128f3305cda6fbbdad563Dmitri Gribenko
128811c820257746b1799b790b6adc7804f44154011Dmitri Gribenko  ArrayRef<RawComment *> RawComments = Comments.getComments();
129aa0cd85838f2a024e589ea4e8c2094130065af21Dmitri Gribenko
130aa0cd85838f2a024e589ea4e8c2094130065af21Dmitri Gribenko  // If there are no comments anywhere, we won't find anything.
131aa0cd85838f2a024e589ea4e8c2094130065af21Dmitri Gribenko  if (RawComments.empty())
1326bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines    return nullptr;
133aa0cd85838f2a024e589ea4e8c2094130065af21Dmitri Gribenko
134abd56c816e9164b17bb3e7154a511b0c9896ffdbDmitri Gribenko  // Find declaration location.
135abd56c816e9164b17bb3e7154a511b0c9896ffdbDmitri Gribenko  // For Objective-C declarations we generally don't expect to have multiple
136abd56c816e9164b17bb3e7154a511b0c9896ffdbDmitri Gribenko  // declarators, thus use declaration starting location as the "declaration
137abd56c816e9164b17bb3e7154a511b0c9896ffdbDmitri Gribenko  // location".
138abd56c816e9164b17bb3e7154a511b0c9896ffdbDmitri Gribenko  // For all other declarations multiple declarators are used quite frequently,
139abd56c816e9164b17bb3e7154a511b0c9896ffdbDmitri Gribenko  // so we use the location of the identifier as the "declaration location".
140abd56c816e9164b17bb3e7154a511b0c9896ffdbDmitri Gribenko  SourceLocation DeclLoc;
141abd56c816e9164b17bb3e7154a511b0c9896ffdbDmitri Gribenko  if (isa<ObjCMethodDecl>(D) || isa<ObjCContainerDecl>(D) ||
14296b098674908eaa59a9128f3305cda6fbbdad563Dmitri Gribenko      isa<ObjCPropertyDecl>(D) ||
143c27bc80a98b9558513b50956c930eedc9e461ae0Dmitri Gribenko      isa<RedeclarableTemplateDecl>(D) ||
144c27bc80a98b9558513b50956c930eedc9e461ae0Dmitri Gribenko      isa<ClassTemplateSpecializationDecl>(D))
145abd56c816e9164b17bb3e7154a511b0c9896ffdbDmitri Gribenko    DeclLoc = D->getLocStart();
146ceaa1ecfa3a8c2cd851bda94b135318e83ba560aFariborz Jahanian  else {
147abd56c816e9164b17bb3e7154a511b0c9896ffdbDmitri Gribenko    DeclLoc = D->getLocation();
148651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines    if (DeclLoc.isMacroID()) {
149651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines      if (isa<TypedefDecl>(D)) {
150651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines        // If location of the typedef name is in a macro, it is because being
151651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines        // declared via a macro. Try using declaration's starting location as
152651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines        // the "declaration location".
153651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines        DeclLoc = D->getLocStart();
154651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines      } else if (const TagDecl *TD = dyn_cast<TagDecl>(D)) {
155651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines        // If location of the tag decl is inside a macro, but the spelling of
156651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines        // the tag name comes from a macro argument, it looks like a special
157651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines        // macro like NS_ENUM is being used to define the tag decl.  In that
158651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines        // case, adjust the source location to the expansion loc so that we can
159651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines        // attach the comment to the tag decl.
160651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines        if (SourceMgr.isMacroArgExpansion(DeclLoc) &&
161651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines            TD->isCompleteDefinition())
162651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines          DeclLoc = SourceMgr.getExpansionLoc(DeclLoc);
163651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines      }
164651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines    }
165ceaa1ecfa3a8c2cd851bda94b135318e83ba560aFariborz Jahanian  }
166abd56c816e9164b17bb3e7154a511b0c9896ffdbDmitri Gribenko
167aa0cd85838f2a024e589ea4e8c2094130065af21Dmitri Gribenko  // If the declaration doesn't map directly to a location in a file, we
168aa0cd85838f2a024e589ea4e8c2094130065af21Dmitri Gribenko  // can't find the comment.
169aa0cd85838f2a024e589ea4e8c2094130065af21Dmitri Gribenko  if (DeclLoc.isInvalid() || !DeclLoc.isFileID())
1706bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines    return nullptr;
171aa0cd85838f2a024e589ea4e8c2094130065af21Dmitri Gribenko
172aa0cd85838f2a024e589ea4e8c2094130065af21Dmitri Gribenko  // Find the comment that occurs just after this declaration.
173a444f1856459130bd3a1bb8995331c9e367db04fDmitri Gribenko  ArrayRef<RawComment *>::iterator Comment;
174a444f1856459130bd3a1bb8995331c9e367db04fDmitri Gribenko  {
175a444f1856459130bd3a1bb8995331c9e367db04fDmitri Gribenko    // When searching for comments during parsing, the comment we are looking
176a444f1856459130bd3a1bb8995331c9e367db04fDmitri Gribenko    // for is usually among the last two comments we parsed -- check them
177a444f1856459130bd3a1bb8995331c9e367db04fDmitri Gribenko    // first.
1786fd7d3067dd06584ef3940e88e31fea1a0e83588Dmitri Gribenko    RawComment CommentAtDeclLoc(
1796fd7d3067dd06584ef3940e88e31fea1a0e83588Dmitri Gribenko        SourceMgr, SourceRange(DeclLoc), false,
1806fd7d3067dd06584ef3940e88e31fea1a0e83588Dmitri Gribenko        LangOpts.CommentOpts.ParseAllComments);
181a444f1856459130bd3a1bb8995331c9e367db04fDmitri Gribenko    BeforeThanCompare<RawComment> Compare(SourceMgr);
182a444f1856459130bd3a1bb8995331c9e367db04fDmitri Gribenko    ArrayRef<RawComment *>::iterator MaybeBeforeDecl = RawComments.end() - 1;
183a444f1856459130bd3a1bb8995331c9e367db04fDmitri Gribenko    bool Found = Compare(*MaybeBeforeDecl, &CommentAtDeclLoc);
184a444f1856459130bd3a1bb8995331c9e367db04fDmitri Gribenko    if (!Found && RawComments.size() >= 2) {
185a444f1856459130bd3a1bb8995331c9e367db04fDmitri Gribenko      MaybeBeforeDecl--;
186a444f1856459130bd3a1bb8995331c9e367db04fDmitri Gribenko      Found = Compare(*MaybeBeforeDecl, &CommentAtDeclLoc);
187a444f1856459130bd3a1bb8995331c9e367db04fDmitri Gribenko    }
188a444f1856459130bd3a1bb8995331c9e367db04fDmitri Gribenko
189a444f1856459130bd3a1bb8995331c9e367db04fDmitri Gribenko    if (Found) {
190a444f1856459130bd3a1bb8995331c9e367db04fDmitri Gribenko      Comment = MaybeBeforeDecl + 1;
191a444f1856459130bd3a1bb8995331c9e367db04fDmitri Gribenko      assert(Comment == std::lower_bound(RawComments.begin(), RawComments.end(),
192a444f1856459130bd3a1bb8995331c9e367db04fDmitri Gribenko                                         &CommentAtDeclLoc, Compare));
193a444f1856459130bd3a1bb8995331c9e367db04fDmitri Gribenko    } else {
194a444f1856459130bd3a1bb8995331c9e367db04fDmitri Gribenko      // Slow path.
195a444f1856459130bd3a1bb8995331c9e367db04fDmitri Gribenko      Comment = std::lower_bound(RawComments.begin(), RawComments.end(),
196a444f1856459130bd3a1bb8995331c9e367db04fDmitri Gribenko                                 &CommentAtDeclLoc, Compare);
197a444f1856459130bd3a1bb8995331c9e367db04fDmitri Gribenko    }
198a444f1856459130bd3a1bb8995331c9e367db04fDmitri Gribenko  }
199aa0cd85838f2a024e589ea4e8c2094130065af21Dmitri Gribenko
200aa0cd85838f2a024e589ea4e8c2094130065af21Dmitri Gribenko  // Decompose the location for the declaration and find the beginning of the
201aa0cd85838f2a024e589ea4e8c2094130065af21Dmitri Gribenko  // file buffer.
202aa0cd85838f2a024e589ea4e8c2094130065af21Dmitri Gribenko  std::pair<FileID, unsigned> DeclLocDecomp = SourceMgr.getDecomposedLoc(DeclLoc);
203aa0cd85838f2a024e589ea4e8c2094130065af21Dmitri Gribenko
204aa0cd85838f2a024e589ea4e8c2094130065af21Dmitri Gribenko  // First check whether we have a trailing comment.
205aa0cd85838f2a024e589ea4e8c2094130065af21Dmitri Gribenko  if (Comment != RawComments.end() &&
206811c820257746b1799b790b6adc7804f44154011Dmitri Gribenko      (*Comment)->isDocumentation() && (*Comment)->isTrailingComment() &&
2078c238be45e2ae6fa00fb5a2bc27a46d3c16d47eeFariborz Jahanian      (isa<FieldDecl>(D) || isa<EnumConstantDecl>(D) || isa<VarDecl>(D) ||
20815c8e56e44a5fb6d298ae5bad791f9ea5bb883f6Fariborz Jahanian       isa<ObjCMethodDecl>(D) || isa<ObjCPropertyDecl>(D))) {
209aa0cd85838f2a024e589ea4e8c2094130065af21Dmitri Gribenko    std::pair<FileID, unsigned> CommentBeginDecomp
210811c820257746b1799b790b6adc7804f44154011Dmitri Gribenko      = SourceMgr.getDecomposedLoc((*Comment)->getSourceRange().getBegin());
211aa0cd85838f2a024e589ea4e8c2094130065af21Dmitri Gribenko    // Check that Doxygen trailing comment comes after the declaration, starts
212aa0cd85838f2a024e589ea4e8c2094130065af21Dmitri Gribenko    // on the same line and in the same file as the declaration.
213aa0cd85838f2a024e589ea4e8c2094130065af21Dmitri Gribenko    if (DeclLocDecomp.first == CommentBeginDecomp.first &&
214aa0cd85838f2a024e589ea4e8c2094130065af21Dmitri Gribenko        SourceMgr.getLineNumber(DeclLocDecomp.first, DeclLocDecomp.second)
215aa0cd85838f2a024e589ea4e8c2094130065af21Dmitri Gribenko          == SourceMgr.getLineNumber(CommentBeginDecomp.first,
216aa0cd85838f2a024e589ea4e8c2094130065af21Dmitri Gribenko                                     CommentBeginDecomp.second)) {
217811c820257746b1799b790b6adc7804f44154011Dmitri Gribenko      return *Comment;
218aa0cd85838f2a024e589ea4e8c2094130065af21Dmitri Gribenko    }
219aa0cd85838f2a024e589ea4e8c2094130065af21Dmitri Gribenko  }
220aa0cd85838f2a024e589ea4e8c2094130065af21Dmitri Gribenko
221aa0cd85838f2a024e589ea4e8c2094130065af21Dmitri Gribenko  // The comment just after the declaration was not a trailing comment.
222aa0cd85838f2a024e589ea4e8c2094130065af21Dmitri Gribenko  // Let's look at the previous comment.
223aa0cd85838f2a024e589ea4e8c2094130065af21Dmitri Gribenko  if (Comment == RawComments.begin())
2246bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines    return nullptr;
225aa0cd85838f2a024e589ea4e8c2094130065af21Dmitri Gribenko  --Comment;
226aa0cd85838f2a024e589ea4e8c2094130065af21Dmitri Gribenko
227aa0cd85838f2a024e589ea4e8c2094130065af21Dmitri Gribenko  // Check that we actually have a non-member Doxygen comment.
228811c820257746b1799b790b6adc7804f44154011Dmitri Gribenko  if (!(*Comment)->isDocumentation() || (*Comment)->isTrailingComment())
2296bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines    return nullptr;
230aa0cd85838f2a024e589ea4e8c2094130065af21Dmitri Gribenko
231aa0cd85838f2a024e589ea4e8c2094130065af21Dmitri Gribenko  // Decompose the end of the comment.
232aa0cd85838f2a024e589ea4e8c2094130065af21Dmitri Gribenko  std::pair<FileID, unsigned> CommentEndDecomp
233811c820257746b1799b790b6adc7804f44154011Dmitri Gribenko    = SourceMgr.getDecomposedLoc((*Comment)->getSourceRange().getEnd());
234aa0cd85838f2a024e589ea4e8c2094130065af21Dmitri Gribenko
235aa0cd85838f2a024e589ea4e8c2094130065af21Dmitri Gribenko  // If the comment and the declaration aren't in the same file, then they
236aa0cd85838f2a024e589ea4e8c2094130065af21Dmitri Gribenko  // aren't related.
237aa0cd85838f2a024e589ea4e8c2094130065af21Dmitri Gribenko  if (DeclLocDecomp.first != CommentEndDecomp.first)
2386bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines    return nullptr;
239aa0cd85838f2a024e589ea4e8c2094130065af21Dmitri Gribenko
240aa0cd85838f2a024e589ea4e8c2094130065af21Dmitri Gribenko  // Get the corresponding buffer.
241aa0cd85838f2a024e589ea4e8c2094130065af21Dmitri Gribenko  bool Invalid = false;
242aa0cd85838f2a024e589ea4e8c2094130065af21Dmitri Gribenko  const char *Buffer = SourceMgr.getBufferData(DeclLocDecomp.first,
243aa0cd85838f2a024e589ea4e8c2094130065af21Dmitri Gribenko                                               &Invalid).data();
244aa0cd85838f2a024e589ea4e8c2094130065af21Dmitri Gribenko  if (Invalid)
2456bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines    return nullptr;
246aa0cd85838f2a024e589ea4e8c2094130065af21Dmitri Gribenko
247aa0cd85838f2a024e589ea4e8c2094130065af21Dmitri Gribenko  // Extract text between the comment and declaration.
248aa0cd85838f2a024e589ea4e8c2094130065af21Dmitri Gribenko  StringRef Text(Buffer + CommentEndDecomp.second,
249aa0cd85838f2a024e589ea4e8c2094130065af21Dmitri Gribenko                 DeclLocDecomp.second - CommentEndDecomp.second);
250aa0cd85838f2a024e589ea4e8c2094130065af21Dmitri Gribenko
2518bdb58a7835a9a90dd9b9791fccf269cbc1dcef3Dmitri Gribenko  // There should be no other declarations or preprocessor directives between
2528bdb58a7835a9a90dd9b9791fccf269cbc1dcef3Dmitri Gribenko  // comment and declaration.
253dc663264c56e587ce9210a7c3b049645e92526b9Argyrios Kyrtzidis  if (Text.find_first_of(";{}#@") != StringRef::npos)
2546bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines    return nullptr;
255aa0cd85838f2a024e589ea4e8c2094130065af21Dmitri Gribenko
256811c820257746b1799b790b6adc7804f44154011Dmitri Gribenko  return *Comment;
257aa0cd85838f2a024e589ea4e8c2094130065af21Dmitri Gribenko}
258aa0cd85838f2a024e589ea4e8c2094130065af21Dmitri Gribenko
259c41ace950dcf2254c9aa48e73647b89c35109f80Dmitri Gribenkonamespace {
260c41ace950dcf2254c9aa48e73647b89c35109f80Dmitri Gribenko/// If we have a 'templated' declaration for a template, adjust 'D' to
261c41ace950dcf2254c9aa48e73647b89c35109f80Dmitri Gribenko/// refer to the actual template.
2622125c9010e259548a8c476fa998a561889555c95Dmitri Gribenko/// If we have an implicit instantiation, adjust 'D' to refer to template.
263c41ace950dcf2254c9aa48e73647b89c35109f80Dmitri Gribenkoconst Decl *adjustDeclToTemplate(const Decl *D) {
264cd81df2dcff4e13eea6edfbfd52a4458d978d174Douglas Gregor  if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
2652125c9010e259548a8c476fa998a561889555c95Dmitri Gribenko    // Is this function declaration part of a function template?
266cd81df2dcff4e13eea6edfbfd52a4458d978d174Douglas Gregor    if (const FunctionTemplateDecl *FTD = FD->getDescribedFunctionTemplate())
2672125c9010e259548a8c476fa998a561889555c95Dmitri Gribenko      return FTD;
2682125c9010e259548a8c476fa998a561889555c95Dmitri Gribenko
2692125c9010e259548a8c476fa998a561889555c95Dmitri Gribenko    // Nothing to do if function is not an implicit instantiation.
2702125c9010e259548a8c476fa998a561889555c95Dmitri Gribenko    if (FD->getTemplateSpecializationKind() != TSK_ImplicitInstantiation)
2712125c9010e259548a8c476fa998a561889555c95Dmitri Gribenko      return D;
2722125c9010e259548a8c476fa998a561889555c95Dmitri Gribenko
2732125c9010e259548a8c476fa998a561889555c95Dmitri Gribenko    // Function is an implicit instantiation of a function template?
2742125c9010e259548a8c476fa998a561889555c95Dmitri Gribenko    if (const FunctionTemplateDecl *FTD = FD->getPrimaryTemplate())
2752125c9010e259548a8c476fa998a561889555c95Dmitri Gribenko      return FTD;
2762125c9010e259548a8c476fa998a561889555c95Dmitri Gribenko
2772125c9010e259548a8c476fa998a561889555c95Dmitri Gribenko    // Function is instantiated from a member definition of a class template?
2782125c9010e259548a8c476fa998a561889555c95Dmitri Gribenko    if (const FunctionDecl *MemberDecl =
2792125c9010e259548a8c476fa998a561889555c95Dmitri Gribenko            FD->getInstantiatedFromMemberFunction())
2802125c9010e259548a8c476fa998a561889555c95Dmitri Gribenko      return MemberDecl;
2812125c9010e259548a8c476fa998a561889555c95Dmitri Gribenko
2822125c9010e259548a8c476fa998a561889555c95Dmitri Gribenko    return D;
2832125c9010e259548a8c476fa998a561889555c95Dmitri Gribenko  }
2842125c9010e259548a8c476fa998a561889555c95Dmitri Gribenko  if (const VarDecl *VD = dyn_cast<VarDecl>(D)) {
2852125c9010e259548a8c476fa998a561889555c95Dmitri Gribenko    // Static data member is instantiated from a member definition of a class
2862125c9010e259548a8c476fa998a561889555c95Dmitri Gribenko    // template?
2872125c9010e259548a8c476fa998a561889555c95Dmitri Gribenko    if (VD->isStaticDataMember())
2882125c9010e259548a8c476fa998a561889555c95Dmitri Gribenko      if (const VarDecl *MemberDecl = VD->getInstantiatedFromStaticDataMember())
2892125c9010e259548a8c476fa998a561889555c95Dmitri Gribenko        return MemberDecl;
2902125c9010e259548a8c476fa998a561889555c95Dmitri Gribenko
2912125c9010e259548a8c476fa998a561889555c95Dmitri Gribenko    return D;
2922125c9010e259548a8c476fa998a561889555c95Dmitri Gribenko  }
2932125c9010e259548a8c476fa998a561889555c95Dmitri Gribenko  if (const CXXRecordDecl *CRD = dyn_cast<CXXRecordDecl>(D)) {
2942125c9010e259548a8c476fa998a561889555c95Dmitri Gribenko    // Is this class declaration part of a class template?
2952125c9010e259548a8c476fa998a561889555c95Dmitri Gribenko    if (const ClassTemplateDecl *CTD = CRD->getDescribedClassTemplate())
2962125c9010e259548a8c476fa998a561889555c95Dmitri Gribenko      return CTD;
2972125c9010e259548a8c476fa998a561889555c95Dmitri Gribenko
2982125c9010e259548a8c476fa998a561889555c95Dmitri Gribenko    // Class is an implicit instantiation of a class template or partial
2992125c9010e259548a8c476fa998a561889555c95Dmitri Gribenko    // specialization?
3002125c9010e259548a8c476fa998a561889555c95Dmitri Gribenko    if (const ClassTemplateSpecializationDecl *CTSD =
3012125c9010e259548a8c476fa998a561889555c95Dmitri Gribenko            dyn_cast<ClassTemplateSpecializationDecl>(CRD)) {
3022125c9010e259548a8c476fa998a561889555c95Dmitri Gribenko      if (CTSD->getSpecializationKind() != TSK_ImplicitInstantiation)
3032125c9010e259548a8c476fa998a561889555c95Dmitri Gribenko        return D;
3042125c9010e259548a8c476fa998a561889555c95Dmitri Gribenko      llvm::PointerUnion<ClassTemplateDecl *,
3052125c9010e259548a8c476fa998a561889555c95Dmitri Gribenko                         ClassTemplatePartialSpecializationDecl *>
3062125c9010e259548a8c476fa998a561889555c95Dmitri Gribenko          PU = CTSD->getSpecializedTemplateOrPartial();
3072125c9010e259548a8c476fa998a561889555c95Dmitri Gribenko      return PU.is<ClassTemplateDecl*>() ?
3082125c9010e259548a8c476fa998a561889555c95Dmitri Gribenko          static_cast<const Decl*>(PU.get<ClassTemplateDecl *>()) :
3092125c9010e259548a8c476fa998a561889555c95Dmitri Gribenko          static_cast<const Decl*>(
3102125c9010e259548a8c476fa998a561889555c95Dmitri Gribenko              PU.get<ClassTemplatePartialSpecializationDecl *>());
3112125c9010e259548a8c476fa998a561889555c95Dmitri Gribenko    }
3122125c9010e259548a8c476fa998a561889555c95Dmitri Gribenko
3132125c9010e259548a8c476fa998a561889555c95Dmitri Gribenko    // Class is instantiated from a member definition of a class template?
3142125c9010e259548a8c476fa998a561889555c95Dmitri Gribenko    if (const MemberSpecializationInfo *Info =
3152125c9010e259548a8c476fa998a561889555c95Dmitri Gribenko                   CRD->getMemberSpecializationInfo())
3162125c9010e259548a8c476fa998a561889555c95Dmitri Gribenko      return Info->getInstantiatedFrom();
3172125c9010e259548a8c476fa998a561889555c95Dmitri Gribenko
3182125c9010e259548a8c476fa998a561889555c95Dmitri Gribenko    return D;
3192125c9010e259548a8c476fa998a561889555c95Dmitri Gribenko  }
3202125c9010e259548a8c476fa998a561889555c95Dmitri Gribenko  if (const EnumDecl *ED = dyn_cast<EnumDecl>(D)) {
3212125c9010e259548a8c476fa998a561889555c95Dmitri Gribenko    // Enum is instantiated from a member definition of a class template?
3222125c9010e259548a8c476fa998a561889555c95Dmitri Gribenko    if (const EnumDecl *MemberDecl = ED->getInstantiatedFromMemberEnum())
3232125c9010e259548a8c476fa998a561889555c95Dmitri Gribenko      return MemberDecl;
3242125c9010e259548a8c476fa998a561889555c95Dmitri Gribenko
3252125c9010e259548a8c476fa998a561889555c95Dmitri Gribenko    return D;
326cd81df2dcff4e13eea6edfbfd52a4458d978d174Douglas Gregor  }
3272125c9010e259548a8c476fa998a561889555c95Dmitri Gribenko  // FIXME: Adjust alias templates?
328c41ace950dcf2254c9aa48e73647b89c35109f80Dmitri Gribenko  return D;
329c41ace950dcf2254c9aa48e73647b89c35109f80Dmitri Gribenko}
330c41ace950dcf2254c9aa48e73647b89c35109f80Dmitri Gribenko} // unnamed namespace
331c41ace950dcf2254c9aa48e73647b89c35109f80Dmitri Gribenko
3321599eac40a3b28de0824013dc2fb90551dfa01b0Dmitri Gribenkoconst RawComment *ASTContext::getRawCommentForAnyRedecl(
3331599eac40a3b28de0824013dc2fb90551dfa01b0Dmitri Gribenko                                                const Decl *D,
3341599eac40a3b28de0824013dc2fb90551dfa01b0Dmitri Gribenko                                                const Decl **OriginalDecl) const {
335c41ace950dcf2254c9aa48e73647b89c35109f80Dmitri Gribenko  D = adjustDeclToTemplate(D);
336cd81df2dcff4e13eea6edfbfd52a4458d978d174Douglas Gregor
337f50555eedef33fd5a67d369aa0ae8a6f1d201543Dmitri Gribenko  // Check whether we have cached a comment for this declaration already.
338f50555eedef33fd5a67d369aa0ae8a6f1d201543Dmitri Gribenko  {
339f50555eedef33fd5a67d369aa0ae8a6f1d201543Dmitri Gribenko    llvm::DenseMap<const Decl *, RawCommentAndCacheFlags>::iterator Pos =
340f50555eedef33fd5a67d369aa0ae8a6f1d201543Dmitri Gribenko        RedeclComments.find(D);
341f50555eedef33fd5a67d369aa0ae8a6f1d201543Dmitri Gribenko    if (Pos != RedeclComments.end()) {
342f50555eedef33fd5a67d369aa0ae8a6f1d201543Dmitri Gribenko      const RawCommentAndCacheFlags &Raw = Pos->second;
3431599eac40a3b28de0824013dc2fb90551dfa01b0Dmitri Gribenko      if (Raw.getKind() != RawCommentAndCacheFlags::NoCommentInDecl) {
3441599eac40a3b28de0824013dc2fb90551dfa01b0Dmitri Gribenko        if (OriginalDecl)
3451599eac40a3b28de0824013dc2fb90551dfa01b0Dmitri Gribenko          *OriginalDecl = Raw.getOriginalDecl();
346f50555eedef33fd5a67d369aa0ae8a6f1d201543Dmitri Gribenko        return Raw.getRaw();
3471599eac40a3b28de0824013dc2fb90551dfa01b0Dmitri Gribenko      }
348f50555eedef33fd5a67d369aa0ae8a6f1d201543Dmitri Gribenko    }
349f50555eedef33fd5a67d369aa0ae8a6f1d201543Dmitri Gribenko  }
350f50555eedef33fd5a67d369aa0ae8a6f1d201543Dmitri Gribenko
351f50555eedef33fd5a67d369aa0ae8a6f1d201543Dmitri Gribenko  // Search for comments attached to declarations in the redeclaration chain.
3526bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines  const RawComment *RC = nullptr;
3536bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines  const Decl *OriginalDeclForRC = nullptr;
354651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  for (auto I : D->redecls()) {
355f50555eedef33fd5a67d369aa0ae8a6f1d201543Dmitri Gribenko    llvm::DenseMap<const Decl *, RawCommentAndCacheFlags>::iterator Pos =
356651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines        RedeclComments.find(I);
357f50555eedef33fd5a67d369aa0ae8a6f1d201543Dmitri Gribenko    if (Pos != RedeclComments.end()) {
358f50555eedef33fd5a67d369aa0ae8a6f1d201543Dmitri Gribenko      const RawCommentAndCacheFlags &Raw = Pos->second;
359f50555eedef33fd5a67d369aa0ae8a6f1d201543Dmitri Gribenko      if (Raw.getKind() != RawCommentAndCacheFlags::NoCommentInDecl) {
360f50555eedef33fd5a67d369aa0ae8a6f1d201543Dmitri Gribenko        RC = Raw.getRaw();
3611599eac40a3b28de0824013dc2fb90551dfa01b0Dmitri Gribenko        OriginalDeclForRC = Raw.getOriginalDecl();
362f50555eedef33fd5a67d369aa0ae8a6f1d201543Dmitri Gribenko        break;
363f50555eedef33fd5a67d369aa0ae8a6f1d201543Dmitri Gribenko      }
364f50555eedef33fd5a67d369aa0ae8a6f1d201543Dmitri Gribenko    } else {
365651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines      RC = getRawCommentForDeclNoCache(I);
366651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines      OriginalDeclForRC = I;
367f50555eedef33fd5a67d369aa0ae8a6f1d201543Dmitri Gribenko      RawCommentAndCacheFlags Raw;
368f50555eedef33fd5a67d369aa0ae8a6f1d201543Dmitri Gribenko      if (RC) {
369f50555eedef33fd5a67d369aa0ae8a6f1d201543Dmitri Gribenko        Raw.setRaw(RC);
370f50555eedef33fd5a67d369aa0ae8a6f1d201543Dmitri Gribenko        Raw.setKind(RawCommentAndCacheFlags::FromDecl);
371f50555eedef33fd5a67d369aa0ae8a6f1d201543Dmitri Gribenko      } else
372f50555eedef33fd5a67d369aa0ae8a6f1d201543Dmitri Gribenko        Raw.setKind(RawCommentAndCacheFlags::NoCommentInDecl);
373651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines      Raw.setOriginalDecl(I);
374651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines      RedeclComments[I] = Raw;
375f50555eedef33fd5a67d369aa0ae8a6f1d201543Dmitri Gribenko      if (RC)
376f50555eedef33fd5a67d369aa0ae8a6f1d201543Dmitri Gribenko        break;
377f50555eedef33fd5a67d369aa0ae8a6f1d201543Dmitri Gribenko    }
3788d3ba23f2d9e6c87794d059412a0808c9cbacb25Dmitri Gribenko  }
379aa0cd85838f2a024e589ea4e8c2094130065af21Dmitri Gribenko
3808376f5934a18b950ac7323d8a38ed231623010faDmitri Gribenko  // If we found a comment, it should be a documentation comment.
3818376f5934a18b950ac7323d8a38ed231623010faDmitri Gribenko  assert(!RC || RC->isDocumentation());
382f50555eedef33fd5a67d369aa0ae8a6f1d201543Dmitri Gribenko
3831599eac40a3b28de0824013dc2fb90551dfa01b0Dmitri Gribenko  if (OriginalDecl)
3841599eac40a3b28de0824013dc2fb90551dfa01b0Dmitri Gribenko    *OriginalDecl = OriginalDeclForRC;
3851599eac40a3b28de0824013dc2fb90551dfa01b0Dmitri Gribenko
386f50555eedef33fd5a67d369aa0ae8a6f1d201543Dmitri Gribenko  // Update cache for every declaration in the redeclaration chain.
387f50555eedef33fd5a67d369aa0ae8a6f1d201543Dmitri Gribenko  RawCommentAndCacheFlags Raw;
388f50555eedef33fd5a67d369aa0ae8a6f1d201543Dmitri Gribenko  Raw.setRaw(RC);
389f50555eedef33fd5a67d369aa0ae8a6f1d201543Dmitri Gribenko  Raw.setKind(RawCommentAndCacheFlags::FromRedecl);
3901599eac40a3b28de0824013dc2fb90551dfa01b0Dmitri Gribenko  Raw.setOriginalDecl(OriginalDeclForRC);
391f50555eedef33fd5a67d369aa0ae8a6f1d201543Dmitri Gribenko
392651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  for (auto I : D->redecls()) {
393651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines    RawCommentAndCacheFlags &R = RedeclComments[I];
394f50555eedef33fd5a67d369aa0ae8a6f1d201543Dmitri Gribenko    if (R.getKind() == RawCommentAndCacheFlags::NoCommentInDecl)
395f50555eedef33fd5a67d369aa0ae8a6f1d201543Dmitri Gribenko      R = Raw;
396f50555eedef33fd5a67d369aa0ae8a6f1d201543Dmitri Gribenko  }
397f50555eedef33fd5a67d369aa0ae8a6f1d201543Dmitri Gribenko
398aa0cd85838f2a024e589ea4e8c2094130065af21Dmitri Gribenko  return RC;
399aa0cd85838f2a024e589ea4e8c2094130065af21Dmitri Gribenko}
400aa0cd85838f2a024e589ea4e8c2094130065af21Dmitri Gribenko
401bf967be66ea8c51b66c61659c23240f762a56dbeFariborz Jahanianstatic void addRedeclaredMethods(const ObjCMethodDecl *ObjCMethod,
402bf967be66ea8c51b66c61659c23240f762a56dbeFariborz Jahanian                   SmallVectorImpl<const NamedDecl *> &Redeclared) {
403bf967be66ea8c51b66c61659c23240f762a56dbeFariborz Jahanian  const DeclContext *DC = ObjCMethod->getDeclContext();
404bf967be66ea8c51b66c61659c23240f762a56dbeFariborz Jahanian  if (const ObjCImplDecl *IMD = dyn_cast<ObjCImplDecl>(DC)) {
405bf967be66ea8c51b66c61659c23240f762a56dbeFariborz Jahanian    const ObjCInterfaceDecl *ID = IMD->getClassInterface();
406bf967be66ea8c51b66c61659c23240f762a56dbeFariborz Jahanian    if (!ID)
407bf967be66ea8c51b66c61659c23240f762a56dbeFariborz Jahanian      return;
408bf967be66ea8c51b66c61659c23240f762a56dbeFariborz Jahanian    // Add redeclared method here.
409651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines    for (const auto *Ext : ID->known_extensions()) {
410bf967be66ea8c51b66c61659c23240f762a56dbeFariborz Jahanian      if (ObjCMethodDecl *RedeclaredMethod =
411d329724745b49f894b768d47275b7c2713106e89Douglas Gregor            Ext->getMethod(ObjCMethod->getSelector(),
412bf967be66ea8c51b66c61659c23240f762a56dbeFariborz Jahanian                                  ObjCMethod->isInstanceMethod()))
413bf967be66ea8c51b66c61659c23240f762a56dbeFariborz Jahanian        Redeclared.push_back(RedeclaredMethod);
414bf967be66ea8c51b66c61659c23240f762a56dbeFariborz Jahanian    }
415bf967be66ea8c51b66c61659c23240f762a56dbeFariborz Jahanian  }
416bf967be66ea8c51b66c61659c23240f762a56dbeFariborz Jahanian}
417bf967be66ea8c51b66c61659c23240f762a56dbeFariborz Jahanian
418749ace614b6ea1ae11d194a60b18e1e43e1db243Fariborz Jahaniancomments::FullComment *ASTContext::cloneFullComment(comments::FullComment *FC,
419749ace614b6ea1ae11d194a60b18e1e43e1db243Fariborz Jahanian                                                    const Decl *D) const {
420749ace614b6ea1ae11d194a60b18e1e43e1db243Fariborz Jahanian  comments::DeclInfo *ThisDeclInfo = new (*this) comments::DeclInfo;
421749ace614b6ea1ae11d194a60b18e1e43e1db243Fariborz Jahanian  ThisDeclInfo->CommentDecl = D;
422749ace614b6ea1ae11d194a60b18e1e43e1db243Fariborz Jahanian  ThisDeclInfo->IsFilled = false;
423749ace614b6ea1ae11d194a60b18e1e43e1db243Fariborz Jahanian  ThisDeclInfo->fill();
424749ace614b6ea1ae11d194a60b18e1e43e1db243Fariborz Jahanian  ThisDeclInfo->CommentDecl = FC->getDecl();
4256bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines  if (!ThisDeclInfo->TemplateParameters)
4266bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines    ThisDeclInfo->TemplateParameters = FC->getDeclInfo()->TemplateParameters;
427749ace614b6ea1ae11d194a60b18e1e43e1db243Fariborz Jahanian  comments::FullComment *CFC =
428749ace614b6ea1ae11d194a60b18e1e43e1db243Fariborz Jahanian    new (*this) comments::FullComment(FC->getBlocks(),
429749ace614b6ea1ae11d194a60b18e1e43e1db243Fariborz Jahanian                                      ThisDeclInfo);
430749ace614b6ea1ae11d194a60b18e1e43e1db243Fariborz Jahanian  return CFC;
431749ace614b6ea1ae11d194a60b18e1e43e1db243Fariborz Jahanian
432749ace614b6ea1ae11d194a60b18e1e43e1db243Fariborz Jahanian}
433749ace614b6ea1ae11d194a60b18e1e43e1db243Fariborz Jahanian
4340a74a4ccc4ecfe1a2792ab72c83815323d8fc914Richard Smithcomments::FullComment *ASTContext::getLocalCommentForDeclUncached(const Decl *D) const {
4350a74a4ccc4ecfe1a2792ab72c83815323d8fc914Richard Smith  const RawComment *RC = getRawCommentForDeclNoCache(D);
4366bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines  return RC ? RC->parse(*this, nullptr, D) : nullptr;
4370a74a4ccc4ecfe1a2792ab72c83815323d8fc914Richard Smith}
4380a74a4ccc4ecfe1a2792ab72c83815323d8fc914Richard Smith
4391952354bd376062c3ab3d328c0fc6c36530c9309Dmitri Gribenkocomments::FullComment *ASTContext::getCommentForDecl(
4401952354bd376062c3ab3d328c0fc6c36530c9309Dmitri Gribenko                                              const Decl *D,
4411952354bd376062c3ab3d328c0fc6c36530c9309Dmitri Gribenko                                              const Preprocessor *PP) const {
442fbff0c4510c7f0e0f30a005960e434b973f5bd21Fariborz Jahanian  if (D->isInvalidDecl())
4436bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines    return nullptr;
444c41ace950dcf2254c9aa48e73647b89c35109f80Dmitri Gribenko  D = adjustDeclToTemplate(D);
445bf967be66ea8c51b66c61659c23240f762a56dbeFariborz Jahanian
446c41ace950dcf2254c9aa48e73647b89c35109f80Dmitri Gribenko  const Decl *Canonical = D->getCanonicalDecl();
447c41ace950dcf2254c9aa48e73647b89c35109f80Dmitri Gribenko  llvm::DenseMap<const Decl *, comments::FullComment *>::iterator Pos =
448c41ace950dcf2254c9aa48e73647b89c35109f80Dmitri Gribenko      ParsedComments.find(Canonical);
449bf967be66ea8c51b66c61659c23240f762a56dbeFariborz Jahanian
450bf967be66ea8c51b66c61659c23240f762a56dbeFariborz Jahanian  if (Pos != ParsedComments.end()) {
451749ace614b6ea1ae11d194a60b18e1e43e1db243Fariborz Jahanian    if (Canonical != D) {
452bf967be66ea8c51b66c61659c23240f762a56dbeFariborz Jahanian      comments::FullComment *FC = Pos->second;
453749ace614b6ea1ae11d194a60b18e1e43e1db243Fariborz Jahanian      comments::FullComment *CFC = cloneFullComment(FC, D);
454bf967be66ea8c51b66c61659c23240f762a56dbeFariborz Jahanian      return CFC;
455bf967be66ea8c51b66c61659c23240f762a56dbeFariborz Jahanian    }
456c41ace950dcf2254c9aa48e73647b89c35109f80Dmitri Gribenko    return Pos->second;
457bf967be66ea8c51b66c61659c23240f762a56dbeFariborz Jahanian  }
458bf967be66ea8c51b66c61659c23240f762a56dbeFariborz Jahanian
4591599eac40a3b28de0824013dc2fb90551dfa01b0Dmitri Gribenko  const Decl *OriginalDecl;
460bf967be66ea8c51b66c61659c23240f762a56dbeFariborz Jahanian
4611599eac40a3b28de0824013dc2fb90551dfa01b0Dmitri Gribenko  const RawComment *RC = getRawCommentForAnyRedecl(D, &OriginalDecl);
462bf967be66ea8c51b66c61659c23240f762a56dbeFariborz Jahanian  if (!RC) {
463bf967be66ea8c51b66c61659c23240f762a56dbeFariborz Jahanian    if (isa<ObjCMethodDecl>(D) || isa<FunctionDecl>(D)) {
4641e905da74dbfd353d77dfc548fc9d6ff420d515aDmitri Gribenko      SmallVector<const NamedDecl*, 8> Overridden;
465c328d9c22a4397dd7313d06be5b82d700297b246Fariborz Jahanian      const ObjCMethodDecl *OMD = dyn_cast<ObjCMethodDecl>(D);
46623799e3ec4b341753e4fb63a7e995cf4ac3b6066Fariborz Jahanian      if (OMD && OMD->isPropertyAccessor())
46723799e3ec4b341753e4fb63a7e995cf4ac3b6066Fariborz Jahanian        if (const ObjCPropertyDecl *PDecl = OMD->findPropertyDecl())
46823799e3ec4b341753e4fb63a7e995cf4ac3b6066Fariborz Jahanian          if (comments::FullComment *FC = getCommentForDecl(PDecl, PP))
46923799e3ec4b341753e4fb63a7e995cf4ac3b6066Fariborz Jahanian            return cloneFullComment(FC, D);
470c328d9c22a4397dd7313d06be5b82d700297b246Fariborz Jahanian      if (OMD)
4711e905da74dbfd353d77dfc548fc9d6ff420d515aDmitri Gribenko        addRedeclaredMethods(OMD, Overridden);
4721e905da74dbfd353d77dfc548fc9d6ff420d515aDmitri Gribenko      getOverriddenMethods(dyn_cast<NamedDecl>(D), Overridden);
47323799e3ec4b341753e4fb63a7e995cf4ac3b6066Fariborz Jahanian      for (unsigned i = 0, e = Overridden.size(); i < e; i++)
47423799e3ec4b341753e4fb63a7e995cf4ac3b6066Fariborz Jahanian        if (comments::FullComment *FC = getCommentForDecl(Overridden[i], PP))
47523799e3ec4b341753e4fb63a7e995cf4ac3b6066Fariborz Jahanian          return cloneFullComment(FC, D);
476bf967be66ea8c51b66c61659c23240f762a56dbeFariborz Jahanian    }
4774857fdcdcb61f314a53ea45be3cbfaf60bb9255bFariborz Jahanian    else if (const TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D)) {
478d1e5c0df2acb01192eedda14534fe990e1e26c46Dmitri Gribenko      // Attach any tag type's documentation to its typedef if latter
47923799e3ec4b341753e4fb63a7e995cf4ac3b6066Fariborz Jahanian      // does not have one of its own.
48041170b55ba635afb806394d44f2b7f1f6095df37Fariborz Jahanian      QualType QT = TD->getUnderlyingType();
481d1e5c0df2acb01192eedda14534fe990e1e26c46Dmitri Gribenko      if (const TagType *TT = QT->getAs<TagType>())
482d1e5c0df2acb01192eedda14534fe990e1e26c46Dmitri Gribenko        if (const Decl *TD = TT->getDecl())
483d1e5c0df2acb01192eedda14534fe990e1e26c46Dmitri Gribenko          if (comments::FullComment *FC = getCommentForDecl(TD, PP))
48423799e3ec4b341753e4fb63a7e995cf4ac3b6066Fariborz Jahanian            return cloneFullComment(FC, D);
48541170b55ba635afb806394d44f2b7f1f6095df37Fariborz Jahanian    }
486622bb4acc00fd63bac545ca23f7c42fd909dc0e7Fariborz Jahanian    else if (const ObjCInterfaceDecl *IC = dyn_cast<ObjCInterfaceDecl>(D)) {
487622bb4acc00fd63bac545ca23f7c42fd909dc0e7Fariborz Jahanian      while (IC->getSuperClass()) {
488622bb4acc00fd63bac545ca23f7c42fd909dc0e7Fariborz Jahanian        IC = IC->getSuperClass();
489622bb4acc00fd63bac545ca23f7c42fd909dc0e7Fariborz Jahanian        if (comments::FullComment *FC = getCommentForDecl(IC, PP))
490622bb4acc00fd63bac545ca23f7c42fd909dc0e7Fariborz Jahanian          return cloneFullComment(FC, D);
491622bb4acc00fd63bac545ca23f7c42fd909dc0e7Fariborz Jahanian      }
492622bb4acc00fd63bac545ca23f7c42fd909dc0e7Fariborz Jahanian    }
493622bb4acc00fd63bac545ca23f7c42fd909dc0e7Fariborz Jahanian    else if (const ObjCCategoryDecl *CD = dyn_cast<ObjCCategoryDecl>(D)) {
494622bb4acc00fd63bac545ca23f7c42fd909dc0e7Fariborz Jahanian      if (const ObjCInterfaceDecl *IC = CD->getClassInterface())
495622bb4acc00fd63bac545ca23f7c42fd909dc0e7Fariborz Jahanian        if (comments::FullComment *FC = getCommentForDecl(IC, PP))
496622bb4acc00fd63bac545ca23f7c42fd909dc0e7Fariborz Jahanian          return cloneFullComment(FC, D);
497622bb4acc00fd63bac545ca23f7c42fd909dc0e7Fariborz Jahanian    }
498622bb4acc00fd63bac545ca23f7c42fd909dc0e7Fariborz Jahanian    else if (const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(D)) {
499622bb4acc00fd63bac545ca23f7c42fd909dc0e7Fariborz Jahanian      if (!(RD = RD->getDefinition()))
5006bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines        return nullptr;
501622bb4acc00fd63bac545ca23f7c42fd909dc0e7Fariborz Jahanian      // Check non-virtual bases.
502651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines      for (const auto &I : RD->bases()) {
503651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines        if (I.isVirtual() || (I.getAccessSpecifier() != AS_public))
504622bb4acc00fd63bac545ca23f7c42fd909dc0e7Fariborz Jahanian          continue;
505651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines        QualType Ty = I.getType();
506622bb4acc00fd63bac545ca23f7c42fd909dc0e7Fariborz Jahanian        if (Ty.isNull())
507622bb4acc00fd63bac545ca23f7c42fd909dc0e7Fariborz Jahanian          continue;
508622bb4acc00fd63bac545ca23f7c42fd909dc0e7Fariborz Jahanian        if (const CXXRecordDecl *NonVirtualBase = Ty->getAsCXXRecordDecl()) {
509622bb4acc00fd63bac545ca23f7c42fd909dc0e7Fariborz Jahanian          if (!(NonVirtualBase= NonVirtualBase->getDefinition()))
510622bb4acc00fd63bac545ca23f7c42fd909dc0e7Fariborz Jahanian            continue;
511622bb4acc00fd63bac545ca23f7c42fd909dc0e7Fariborz Jahanian
512622bb4acc00fd63bac545ca23f7c42fd909dc0e7Fariborz Jahanian          if (comments::FullComment *FC = getCommentForDecl((NonVirtualBase), PP))
513622bb4acc00fd63bac545ca23f7c42fd909dc0e7Fariborz Jahanian            return cloneFullComment(FC, D);
514622bb4acc00fd63bac545ca23f7c42fd909dc0e7Fariborz Jahanian        }
515622bb4acc00fd63bac545ca23f7c42fd909dc0e7Fariborz Jahanian      }
516622bb4acc00fd63bac545ca23f7c42fd909dc0e7Fariborz Jahanian      // Check virtual bases.
517651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines      for (const auto &I : RD->vbases()) {
518651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines        if (I.getAccessSpecifier() != AS_public)
51991efca0fa2ef5e63b48692e3439f5c6e6bde350cFariborz Jahanian          continue;
520651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines        QualType Ty = I.getType();
521622bb4acc00fd63bac545ca23f7c42fd909dc0e7Fariborz Jahanian        if (Ty.isNull())
522622bb4acc00fd63bac545ca23f7c42fd909dc0e7Fariborz Jahanian          continue;
523622bb4acc00fd63bac545ca23f7c42fd909dc0e7Fariborz Jahanian        if (const CXXRecordDecl *VirtualBase = Ty->getAsCXXRecordDecl()) {
524622bb4acc00fd63bac545ca23f7c42fd909dc0e7Fariborz Jahanian          if (!(VirtualBase= VirtualBase->getDefinition()))
525622bb4acc00fd63bac545ca23f7c42fd909dc0e7Fariborz Jahanian            continue;
526622bb4acc00fd63bac545ca23f7c42fd909dc0e7Fariborz Jahanian          if (comments::FullComment *FC = getCommentForDecl((VirtualBase), PP))
527622bb4acc00fd63bac545ca23f7c42fd909dc0e7Fariborz Jahanian            return cloneFullComment(FC, D);
528622bb4acc00fd63bac545ca23f7c42fd909dc0e7Fariborz Jahanian        }
529622bb4acc00fd63bac545ca23f7c42fd909dc0e7Fariborz Jahanian      }
530622bb4acc00fd63bac545ca23f7c42fd909dc0e7Fariborz Jahanian    }
5316bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines    return nullptr;
532bf967be66ea8c51b66c61659c23240f762a56dbeFariborz Jahanian  }
533bf967be66ea8c51b66c61659c23240f762a56dbeFariborz Jahanian
5344b41c65feb93eeb6f6d27b49a2045ea1b72de9d1Dmitri Gribenko  // If the RawComment was attached to other redeclaration of this Decl, we
5354b41c65feb93eeb6f6d27b49a2045ea1b72de9d1Dmitri Gribenko  // should parse the comment in context of that other Decl.  This is important
5364b41c65feb93eeb6f6d27b49a2045ea1b72de9d1Dmitri Gribenko  // because comments can contain references to parameter names which can be
5374b41c65feb93eeb6f6d27b49a2045ea1b72de9d1Dmitri Gribenko  // different across redeclarations.
5381599eac40a3b28de0824013dc2fb90551dfa01b0Dmitri Gribenko  if (D != OriginalDecl)
5391952354bd376062c3ab3d328c0fc6c36530c9309Dmitri Gribenko    return getCommentForDecl(OriginalDecl, PP);
5401599eac40a3b28de0824013dc2fb90551dfa01b0Dmitri Gribenko
5411952354bd376062c3ab3d328c0fc6c36530c9309Dmitri Gribenko  comments::FullComment *FC = RC->parse(*this, PP, D);
542c41ace950dcf2254c9aa48e73647b89c35109f80Dmitri Gribenko  ParsedComments[Canonical] = FC;
543c41ace950dcf2254c9aa48e73647b89c35109f80Dmitri Gribenko  return FC;
5448d3ba23f2d9e6c87794d059412a0808c9cbacb25Dmitri Gribenko}
5458d3ba23f2d9e6c87794d059412a0808c9cbacb25Dmitri Gribenko
5463e1274f2b99cb99c03cc8e2c6517c37d330b597aDouglas Gregorvoid
5473e1274f2b99cb99c03cc8e2c6517c37d330b597aDouglas GregorASTContext::CanonicalTemplateTemplateParm::Profile(llvm::FoldingSetNodeID &ID,
5483e1274f2b99cb99c03cc8e2c6517c37d330b597aDouglas Gregor                                               TemplateTemplateParmDecl *Parm) {
5493e1274f2b99cb99c03cc8e2c6517c37d330b597aDouglas Gregor  ID.AddInteger(Parm->getDepth());
5503e1274f2b99cb99c03cc8e2c6517c37d330b597aDouglas Gregor  ID.AddInteger(Parm->getPosition());
55161c4d28e36cd3f1be392cb77f07436d1fa6b0f9fDouglas Gregor  ID.AddBoolean(Parm->isParameterPack());
5523e1274f2b99cb99c03cc8e2c6517c37d330b597aDouglas Gregor
5533e1274f2b99cb99c03cc8e2c6517c37d330b597aDouglas Gregor  TemplateParameterList *Params = Parm->getTemplateParameters();
5543e1274f2b99cb99c03cc8e2c6517c37d330b597aDouglas Gregor  ID.AddInteger(Params->size());
5553e1274f2b99cb99c03cc8e2c6517c37d330b597aDouglas Gregor  for (TemplateParameterList::const_iterator P = Params->begin(),
5563e1274f2b99cb99c03cc8e2c6517c37d330b597aDouglas Gregor                                          PEnd = Params->end();
5573e1274f2b99cb99c03cc8e2c6517c37d330b597aDouglas Gregor       P != PEnd; ++P) {
5583e1274f2b99cb99c03cc8e2c6517c37d330b597aDouglas Gregor    if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(*P)) {
5593e1274f2b99cb99c03cc8e2c6517c37d330b597aDouglas Gregor      ID.AddInteger(0);
5603e1274f2b99cb99c03cc8e2c6517c37d330b597aDouglas Gregor      ID.AddBoolean(TTP->isParameterPack());
5613e1274f2b99cb99c03cc8e2c6517c37d330b597aDouglas Gregor      continue;
5623e1274f2b99cb99c03cc8e2c6517c37d330b597aDouglas Gregor    }
5633e1274f2b99cb99c03cc8e2c6517c37d330b597aDouglas Gregor
5643e1274f2b99cb99c03cc8e2c6517c37d330b597aDouglas Gregor    if (NonTypeTemplateParmDecl *NTTP = dyn_cast<NonTypeTemplateParmDecl>(*P)) {
5653e1274f2b99cb99c03cc8e2c6517c37d330b597aDouglas Gregor      ID.AddInteger(1);
56661c4d28e36cd3f1be392cb77f07436d1fa6b0f9fDouglas Gregor      ID.AddBoolean(NTTP->isParameterPack());
5679e9c454b12671a624f666fc6fbf132fdf183effcEli Friedman      ID.AddPointer(NTTP->getType().getCanonicalType().getAsOpaquePtr());
5686952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor      if (NTTP->isExpandedParameterPack()) {
5696952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor        ID.AddBoolean(true);
5706952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor        ID.AddInteger(NTTP->getNumExpansionTypes());
5719e9c454b12671a624f666fc6fbf132fdf183effcEli Friedman        for (unsigned I = 0, N = NTTP->getNumExpansionTypes(); I != N; ++I) {
5729e9c454b12671a624f666fc6fbf132fdf183effcEli Friedman          QualType T = NTTP->getExpansionType(I);
5739e9c454b12671a624f666fc6fbf132fdf183effcEli Friedman          ID.AddPointer(T.getCanonicalType().getAsOpaquePtr());
5749e9c454b12671a624f666fc6fbf132fdf183effcEli Friedman        }
5756952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor      } else
5766952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor        ID.AddBoolean(false);
5773e1274f2b99cb99c03cc8e2c6517c37d330b597aDouglas Gregor      continue;
5783e1274f2b99cb99c03cc8e2c6517c37d330b597aDouglas Gregor    }
5793e1274f2b99cb99c03cc8e2c6517c37d330b597aDouglas Gregor
5803e1274f2b99cb99c03cc8e2c6517c37d330b597aDouglas Gregor    TemplateTemplateParmDecl *TTP = cast<TemplateTemplateParmDecl>(*P);
5813e1274f2b99cb99c03cc8e2c6517c37d330b597aDouglas Gregor    ID.AddInteger(2);
5823e1274f2b99cb99c03cc8e2c6517c37d330b597aDouglas Gregor    Profile(ID, TTP);
5833e1274f2b99cb99c03cc8e2c6517c37d330b597aDouglas Gregor  }
5843e1274f2b99cb99c03cc8e2c6517c37d330b597aDouglas Gregor}
5853e1274f2b99cb99c03cc8e2c6517c37d330b597aDouglas Gregor
5863e1274f2b99cb99c03cc8e2c6517c37d330b597aDouglas GregorTemplateTemplateParmDecl *
5873e1274f2b99cb99c03cc8e2c6517c37d330b597aDouglas GregorASTContext::getCanonicalTemplateTemplateParmDecl(
5884ba2a17694148e16eaa8d3917f657ffcd3667be4Jay Foad                                          TemplateTemplateParmDecl *TTP) const {
5893e1274f2b99cb99c03cc8e2c6517c37d330b597aDouglas Gregor  // Check if we already have a canonical template template parameter.
5903e1274f2b99cb99c03cc8e2c6517c37d330b597aDouglas Gregor  llvm::FoldingSetNodeID ID;
5913e1274f2b99cb99c03cc8e2c6517c37d330b597aDouglas Gregor  CanonicalTemplateTemplateParm::Profile(ID, TTP);
5926bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines  void *InsertPos = nullptr;
5933e1274f2b99cb99c03cc8e2c6517c37d330b597aDouglas Gregor  CanonicalTemplateTemplateParm *Canonical
5943e1274f2b99cb99c03cc8e2c6517c37d330b597aDouglas Gregor    = CanonTemplateTemplateParms.FindNodeOrInsertPos(ID, InsertPos);
5953e1274f2b99cb99c03cc8e2c6517c37d330b597aDouglas Gregor  if (Canonical)
5963e1274f2b99cb99c03cc8e2c6517c37d330b597aDouglas Gregor    return Canonical->getParam();
5973e1274f2b99cb99c03cc8e2c6517c37d330b597aDouglas Gregor
5983e1274f2b99cb99c03cc8e2c6517c37d330b597aDouglas Gregor  // Build a canonical template parameter list.
5993e1274f2b99cb99c03cc8e2c6517c37d330b597aDouglas Gregor  TemplateParameterList *Params = TTP->getTemplateParameters();
6005f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner  SmallVector<NamedDecl *, 4> CanonParams;
6013e1274f2b99cb99c03cc8e2c6517c37d330b597aDouglas Gregor  CanonParams.reserve(Params->size());
6023e1274f2b99cb99c03cc8e2c6517c37d330b597aDouglas Gregor  for (TemplateParameterList::const_iterator P = Params->begin(),
6033e1274f2b99cb99c03cc8e2c6517c37d330b597aDouglas Gregor                                          PEnd = Params->end();
6043e1274f2b99cb99c03cc8e2c6517c37d330b597aDouglas Gregor       P != PEnd; ++P) {
6053e1274f2b99cb99c03cc8e2c6517c37d330b597aDouglas Gregor    if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(*P))
6063e1274f2b99cb99c03cc8e2c6517c37d330b597aDouglas Gregor      CanonParams.push_back(
6073e1274f2b99cb99c03cc8e2c6517c37d330b597aDouglas Gregor                  TemplateTypeParmDecl::Create(*this, getTranslationUnitDecl(),
608344577e6b58f42d18dc8118c8903b49a85dc005eAbramo Bagnara                                               SourceLocation(),
609344577e6b58f42d18dc8118c8903b49a85dc005eAbramo Bagnara                                               SourceLocation(),
610344577e6b58f42d18dc8118c8903b49a85dc005eAbramo Bagnara                                               TTP->getDepth(),
6116bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines                                               TTP->getIndex(), nullptr, false,
6123e1274f2b99cb99c03cc8e2c6517c37d330b597aDouglas Gregor                                               TTP->isParameterPack()));
6133e1274f2b99cb99c03cc8e2c6517c37d330b597aDouglas Gregor    else if (NonTypeTemplateParmDecl *NTTP
6146952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor             = dyn_cast<NonTypeTemplateParmDecl>(*P)) {
6156952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor      QualType T = getCanonicalType(NTTP->getType());
6166952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor      TypeSourceInfo *TInfo = getTrivialTypeSourceInfo(T);
6176952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor      NonTypeTemplateParmDecl *Param;
6186952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor      if (NTTP->isExpandedParameterPack()) {
6195f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner        SmallVector<QualType, 2> ExpandedTypes;
6205f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner        SmallVector<TypeSourceInfo *, 2> ExpandedTInfos;
6216952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor        for (unsigned I = 0, N = NTTP->getNumExpansionTypes(); I != N; ++I) {
6226952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor          ExpandedTypes.push_back(getCanonicalType(NTTP->getExpansionType(I)));
6236952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor          ExpandedTInfos.push_back(
6246952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor                                getTrivialTypeSourceInfo(ExpandedTypes.back()));
6256952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor        }
6266952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor
6276952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor        Param = NonTypeTemplateParmDecl::Create(*this, getTranslationUnitDecl(),
628ff676cb48fe8bf7be2feaa251dc7c5fb15af4730Abramo Bagnara                                                SourceLocation(),
629ff676cb48fe8bf7be2feaa251dc7c5fb15af4730Abramo Bagnara                                                SourceLocation(),
6306952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor                                                NTTP->getDepth(),
6316bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines                                                NTTP->getPosition(), nullptr,
6326952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor                                                T,
6336952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor                                                TInfo,
6346952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor                                                ExpandedTypes.data(),
6356952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor                                                ExpandedTypes.size(),
6366952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor                                                ExpandedTInfos.data());
6376952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor      } else {
6386952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor        Param = NonTypeTemplateParmDecl::Create(*this, getTranslationUnitDecl(),
639ff676cb48fe8bf7be2feaa251dc7c5fb15af4730Abramo Bagnara                                                SourceLocation(),
640ff676cb48fe8bf7be2feaa251dc7c5fb15af4730Abramo Bagnara                                                SourceLocation(),
6416952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor                                                NTTP->getDepth(),
6426bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines                                                NTTP->getPosition(), nullptr,
6436952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor                                                T,
6446952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor                                                NTTP->isParameterPack(),
6456952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor                                                TInfo);
6466952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor      }
6476952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor      CanonParams.push_back(Param);
6486952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor
6496952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor    } else
6503e1274f2b99cb99c03cc8e2c6517c37d330b597aDouglas Gregor      CanonParams.push_back(getCanonicalTemplateTemplateParmDecl(
6513e1274f2b99cb99c03cc8e2c6517c37d330b597aDouglas Gregor                                           cast<TemplateTemplateParmDecl>(*P)));
6523e1274f2b99cb99c03cc8e2c6517c37d330b597aDouglas Gregor  }
6533e1274f2b99cb99c03cc8e2c6517c37d330b597aDouglas Gregor
6543e1274f2b99cb99c03cc8e2c6517c37d330b597aDouglas Gregor  TemplateTemplateParmDecl *CanonTTP
6553e1274f2b99cb99c03cc8e2c6517c37d330b597aDouglas Gregor    = TemplateTemplateParmDecl::Create(*this, getTranslationUnitDecl(),
6563e1274f2b99cb99c03cc8e2c6517c37d330b597aDouglas Gregor                                       SourceLocation(), TTP->getDepth(),
65761c4d28e36cd3f1be392cb77f07436d1fa6b0f9fDouglas Gregor                                       TTP->getPosition(),
65861c4d28e36cd3f1be392cb77f07436d1fa6b0f9fDouglas Gregor                                       TTP->isParameterPack(),
6596bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines                                       nullptr,
6603e1274f2b99cb99c03cc8e2c6517c37d330b597aDouglas Gregor                         TemplateParameterList::Create(*this, SourceLocation(),
6613e1274f2b99cb99c03cc8e2c6517c37d330b597aDouglas Gregor                                                       SourceLocation(),
6623e1274f2b99cb99c03cc8e2c6517c37d330b597aDouglas Gregor                                                       CanonParams.data(),
6633e1274f2b99cb99c03cc8e2c6517c37d330b597aDouglas Gregor                                                       CanonParams.size(),
6643e1274f2b99cb99c03cc8e2c6517c37d330b597aDouglas Gregor                                                       SourceLocation()));
6653e1274f2b99cb99c03cc8e2c6517c37d330b597aDouglas Gregor
6663e1274f2b99cb99c03cc8e2c6517c37d330b597aDouglas Gregor  // Get the new insert position for the node we care about.
6673e1274f2b99cb99c03cc8e2c6517c37d330b597aDouglas Gregor  Canonical = CanonTemplateTemplateParms.FindNodeOrInsertPos(ID, InsertPos);
6686bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines  assert(!Canonical && "Shouldn't be in the map!");
6693e1274f2b99cb99c03cc8e2c6517c37d330b597aDouglas Gregor  (void)Canonical;
6703e1274f2b99cb99c03cc8e2c6517c37d330b597aDouglas Gregor
6713e1274f2b99cb99c03cc8e2c6517c37d330b597aDouglas Gregor  // Create the canonical template template parameter entry.
6723e1274f2b99cb99c03cc8e2c6517c37d330b597aDouglas Gregor  Canonical = new (*this) CanonicalTemplateTemplateParm(CanonTTP);
6733e1274f2b99cb99c03cc8e2c6517c37d330b597aDouglas Gregor  CanonTemplateTemplateParms.InsertNode(Canonical, InsertPos);
6743e1274f2b99cb99c03cc8e2c6517c37d330b597aDouglas Gregor  return CanonTTP;
6753e1274f2b99cb99c03cc8e2c6517c37d330b597aDouglas Gregor}
6763e1274f2b99cb99c03cc8e2c6517c37d330b597aDouglas Gregor
677071cc7deffad608165b1ddd5263e8bf181861520Charles DavisCXXABI *ASTContext::createCXXABI(const TargetInfo &T) {
6786bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines  if (!LangOpts.CPlusPlus) return nullptr;
679ee79a4c30e5d1c5285551c9a25b8ec6d45d46aa7John McCall
680b8b2c9da87e7d70a1679db026f40548b3192b705John McCall  switch (T.getCXXABI().getKind()) {
6816bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines  case TargetCXXABI::GenericARM: // Same as Itanium at this level
682b8b2c9da87e7d70a1679db026f40548b3192b705John McCall  case TargetCXXABI::iOS:
683651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  case TargetCXXABI::iOS64:
6846bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines  case TargetCXXABI::GenericAArch64:
685b8b2c9da87e7d70a1679db026f40548b3192b705John McCall  case TargetCXXABI::GenericItanium:
686071cc7deffad608165b1ddd5263e8bf181861520Charles Davis    return CreateItaniumCXXABI(*this);
687b8b2c9da87e7d70a1679db026f40548b3192b705John McCall  case TargetCXXABI::Microsoft:
68820cf717034ba1f20fc47c025ecb72ed9b631ad13Charles Davis    return CreateMicrosoftCXXABI(*this);
68920cf717034ba1f20fc47c025ecb72ed9b631ad13Charles Davis  }
6907530c034c0c71a64c5a9173206d9742ae847af8bDavid Blaikie  llvm_unreachable("Invalid CXXABI type!");
691071cc7deffad608165b1ddd5263e8bf181861520Charles Davis}
692071cc7deffad608165b1ddd5263e8bf181861520Charles Davis
693bcfd1f55bfbb3e5944cd5e03d07b343e280838c4Douglas Gregorstatic const LangAS::Map *getAddressSpaceMap(const TargetInfo &T,
694207f4d8543529221932af82836016a2ef066c917Peter Collingbourne                                             const LangOptions &LOpts) {
695207f4d8543529221932af82836016a2ef066c917Peter Collingbourne  if (LOpts.FakeAddressSpaceMap) {
696207f4d8543529221932af82836016a2ef066c917Peter Collingbourne    // The fake address space map must have a distinct entry for each
697207f4d8543529221932af82836016a2ef066c917Peter Collingbourne    // language-specific address space.
698207f4d8543529221932af82836016a2ef066c917Peter Collingbourne    static const unsigned FakeAddrSpaceMap[] = {
699207f4d8543529221932af82836016a2ef066c917Peter Collingbourne      1, // opencl_global
700207f4d8543529221932af82836016a2ef066c917Peter Collingbourne      2, // opencl_local
7014dc34ebf2a0716bf77ba110dab6777a3fc4397ddPeter Collingbourne      3, // opencl_constant
7024dc34ebf2a0716bf77ba110dab6777a3fc4397ddPeter Collingbourne      4, // cuda_device
7034dc34ebf2a0716bf77ba110dab6777a3fc4397ddPeter Collingbourne      5, // cuda_constant
7044dc34ebf2a0716bf77ba110dab6777a3fc4397ddPeter Collingbourne      6  // cuda_shared
705207f4d8543529221932af82836016a2ef066c917Peter Collingbourne    };
706bcfd1f55bfbb3e5944cd5e03d07b343e280838c4Douglas Gregor    return &FakeAddrSpaceMap;
707207f4d8543529221932af82836016a2ef066c917Peter Collingbourne  } else {
708bcfd1f55bfbb3e5944cd5e03d07b343e280838c4Douglas Gregor    return &T.getAddressSpaceMap();
709207f4d8543529221932af82836016a2ef066c917Peter Collingbourne  }
710207f4d8543529221932af82836016a2ef066c917Peter Collingbourne}
711207f4d8543529221932af82836016a2ef066c917Peter Collingbourne
7121eef85246b411b55c493098266746d0d83c241eaDavid Tweedstatic bool isAddrSpaceMapManglingEnabled(const TargetInfo &TI,
7131eef85246b411b55c493098266746d0d83c241eaDavid Tweed                                          const LangOptions &LangOpts) {
7141eef85246b411b55c493098266746d0d83c241eaDavid Tweed  switch (LangOpts.getAddressSpaceMapMangling()) {
7151eef85246b411b55c493098266746d0d83c241eaDavid Tweed  case LangOptions::ASMM_Target:
7161eef85246b411b55c493098266746d0d83c241eaDavid Tweed    return TI.useAddressSpaceMapMangling();
7171eef85246b411b55c493098266746d0d83c241eaDavid Tweed  case LangOptions::ASMM_On:
7181eef85246b411b55c493098266746d0d83c241eaDavid Tweed    return true;
7191eef85246b411b55c493098266746d0d83c241eaDavid Tweed  case LangOptions::ASMM_Off:
7201eef85246b411b55c493098266746d0d83c241eaDavid Tweed    return false;
7211eef85246b411b55c493098266746d0d83c241eaDavid Tweed  }
722cb3b4001d4b9492df3e88227282cec2764e7e76fNAKAMURA Takumi  llvm_unreachable("getAddressSpaceMapMangling() doesn't cover anything.");
7231eef85246b411b55c493098266746d0d83c241eaDavid Tweed}
7241eef85246b411b55c493098266746d0d83c241eaDavid Tweed
7253e3cd93b2fd9644e970c389e715c13883faf68b6Douglas GregorASTContext::ASTContext(LangOptions& LOpts, SourceManager &SM,
726e91593ef084479340582b2ba177b44be50a717b7Daniel Dunbar                       IdentifierTable &idents, SelectorTable &sels,
7276bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines                       Builtin::Context &builtins)
728bcfd1f55bfbb3e5944cd5e03d07b343e280838c4Douglas Gregor  : FunctionProtoTypes(this_()),
729bcfd1f55bfbb3e5944cd5e03d07b343e280838c4Douglas Gregor    TemplateSpecializationTypes(this_()),
730bcfd1f55bfbb3e5944cd5e03d07b343e280838c4Douglas Gregor    DependentTemplateSpecializationTypes(this_()),
731bcfd1f55bfbb3e5944cd5e03d07b343e280838c4Douglas Gregor    SubstTemplateTemplateParmPacks(this_()),
7326bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines    GlobalNestedNameSpecifier(nullptr),
7336bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines    Int128Decl(nullptr), UInt128Decl(nullptr), Float128StubDecl(nullptr),
7346bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines    BuiltinVaListDecl(nullptr),
7356bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines    ObjCIdDecl(nullptr), ObjCSelDecl(nullptr), ObjCClassDecl(nullptr),
7366bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines    ObjCProtocolClassDecl(nullptr), BOOLDecl(nullptr),
7376bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines    CFConstantStringTypeDecl(nullptr), ObjCInstanceTypeDecl(nullptr),
7386bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines    FILEDecl(nullptr),
7396bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines    jmp_bufDecl(nullptr), sigjmp_bufDecl(nullptr), ucontext_tDecl(nullptr),
7406bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines    BlockDescriptorType(nullptr), BlockDescriptorExtendedType(nullptr),
7416bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines    cudaConfigureCallDecl(nullptr),
742e664977aca2a05a77abab5a06dc0fb69e870cfb9Douglas Gregor    NullTypeSourceInfo(QualType()),
743e664977aca2a05a77abab5a06dc0fb69e870cfb9Douglas Gregor    FirstLocalImport(), LastLocalImport(),
744bcfd1f55bfbb3e5944cd5e03d07b343e280838c4Douglas Gregor    SourceMgr(SM), LangOpts(LOpts),
7456bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines    AddrSpaceMap(nullptr), Target(nullptr), PrintingPolicy(LOpts),
746bcfd1f55bfbb3e5944cd5e03d07b343e280838c4Douglas Gregor    Idents(idents), Selectors(sels),
747bcfd1f55bfbb3e5944cd5e03d07b343e280838c4Douglas Gregor    BuiltinInfo(builtins),
748