annotate clang/lib/AST/ASTImporterLookupTable.cpp @ 176:de4ac79aef9d

...
author Shinji KONO <kono@ie.u-ryukyu.ac.jp>
date Mon, 25 May 2020 17:13:11 +0900
parents 1d019706d866
children 2e18cbf3894f
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
150
anatofuz
parents:
diff changeset
1 //===- ASTImporterLookupTable.cpp - ASTImporter specific lookup -----------===//
anatofuz
parents:
diff changeset
2 //
anatofuz
parents:
diff changeset
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
anatofuz
parents:
diff changeset
4 // See https://llvm.org/LICENSE.txt for license information.
anatofuz
parents:
diff changeset
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
anatofuz
parents:
diff changeset
6 //
anatofuz
parents:
diff changeset
7 //===----------------------------------------------------------------------===//
anatofuz
parents:
diff changeset
8 //
anatofuz
parents:
diff changeset
9 // This file defines the ASTImporterLookupTable class which implements a
anatofuz
parents:
diff changeset
10 // lookup procedure for the import mechanism.
anatofuz
parents:
diff changeset
11 //
anatofuz
parents:
diff changeset
12 //===----------------------------------------------------------------------===//
anatofuz
parents:
diff changeset
13
anatofuz
parents:
diff changeset
14 #include "clang/AST/ASTImporterLookupTable.h"
anatofuz
parents:
diff changeset
15 #include "clang/AST/Decl.h"
anatofuz
parents:
diff changeset
16 #include "clang/AST/RecursiveASTVisitor.h"
anatofuz
parents:
diff changeset
17
anatofuz
parents:
diff changeset
18 namespace clang {
anatofuz
parents:
diff changeset
19
anatofuz
parents:
diff changeset
20 namespace {
anatofuz
parents:
diff changeset
21
anatofuz
parents:
diff changeset
22 struct Builder : RecursiveASTVisitor<Builder> {
anatofuz
parents:
diff changeset
23 ASTImporterLookupTable &LT;
anatofuz
parents:
diff changeset
24 Builder(ASTImporterLookupTable &LT) : LT(LT) {}
anatofuz
parents:
diff changeset
25 bool VisitNamedDecl(NamedDecl *D) {
anatofuz
parents:
diff changeset
26 LT.add(D);
anatofuz
parents:
diff changeset
27 return true;
anatofuz
parents:
diff changeset
28 }
anatofuz
parents:
diff changeset
29 // In most cases the FriendDecl contains the declaration of the befriended
anatofuz
parents:
diff changeset
30 // class as a child node, so it is discovered during the recursive
anatofuz
parents:
diff changeset
31 // visitation. However, there are cases when the befriended class is not a
anatofuz
parents:
diff changeset
32 // child, thus it must be fetched explicitly from the FriendDecl, and only
anatofuz
parents:
diff changeset
33 // then can we add it to the lookup table.
anatofuz
parents:
diff changeset
34 bool VisitFriendDecl(FriendDecl *D) {
anatofuz
parents:
diff changeset
35 if (D->getFriendType()) {
anatofuz
parents:
diff changeset
36 QualType Ty = D->getFriendType()->getType();
anatofuz
parents:
diff changeset
37 if (isa<ElaboratedType>(Ty))
anatofuz
parents:
diff changeset
38 Ty = cast<ElaboratedType>(Ty)->getNamedType();
anatofuz
parents:
diff changeset
39 // A FriendDecl with a dependent type (e.g. ClassTemplateSpecialization)
anatofuz
parents:
diff changeset
40 // always has that decl as child node.
anatofuz
parents:
diff changeset
41 // However, there are non-dependent cases which does not have the
anatofuz
parents:
diff changeset
42 // type as a child node. We have to dig up that type now.
anatofuz
parents:
diff changeset
43 if (!Ty->isDependentType()) {
anatofuz
parents:
diff changeset
44 if (const auto *RTy = dyn_cast<RecordType>(Ty))
anatofuz
parents:
diff changeset
45 LT.add(RTy->getAsCXXRecordDecl());
anatofuz
parents:
diff changeset
46 else if (const auto *SpecTy = dyn_cast<TemplateSpecializationType>(Ty))
anatofuz
parents:
diff changeset
47 LT.add(SpecTy->getAsCXXRecordDecl());
anatofuz
parents:
diff changeset
48 else if (isa<TypedefType>(Ty)) {
anatofuz
parents:
diff changeset
49 // We do not put friend typedefs to the lookup table because
anatofuz
parents:
diff changeset
50 // ASTImporter does not organize typedefs into redecl chains.
anatofuz
parents:
diff changeset
51 } else {
anatofuz
parents:
diff changeset
52 llvm_unreachable("Unhandled type of friend class");
anatofuz
parents:
diff changeset
53 }
anatofuz
parents:
diff changeset
54 }
anatofuz
parents:
diff changeset
55 }
anatofuz
parents:
diff changeset
56 return true;
anatofuz
parents:
diff changeset
57 }
anatofuz
parents:
diff changeset
58
anatofuz
parents:
diff changeset
59 // Override default settings of base.
anatofuz
parents:
diff changeset
60 bool shouldVisitTemplateInstantiations() const { return true; }
anatofuz
parents:
diff changeset
61 bool shouldVisitImplicitCode() const { return true; }
anatofuz
parents:
diff changeset
62 };
anatofuz
parents:
diff changeset
63
anatofuz
parents:
diff changeset
64 } // anonymous namespace
anatofuz
parents:
diff changeset
65
anatofuz
parents:
diff changeset
66 ASTImporterLookupTable::ASTImporterLookupTable(TranslationUnitDecl &TU) {
anatofuz
parents:
diff changeset
67 Builder B(*this);
anatofuz
parents:
diff changeset
68 B.TraverseDecl(&TU);
anatofuz
parents:
diff changeset
69 }
anatofuz
parents:
diff changeset
70
anatofuz
parents:
diff changeset
71 void ASTImporterLookupTable::add(DeclContext *DC, NamedDecl *ND) {
anatofuz
parents:
diff changeset
72 DeclList &Decls = LookupTable[DC][ND->getDeclName()];
anatofuz
parents:
diff changeset
73 // Inserts if and only if there is no element in the container equal to it.
anatofuz
parents:
diff changeset
74 Decls.insert(ND);
anatofuz
parents:
diff changeset
75 }
anatofuz
parents:
diff changeset
76
anatofuz
parents:
diff changeset
77 void ASTImporterLookupTable::remove(DeclContext *DC, NamedDecl *ND) {
anatofuz
parents:
diff changeset
78 DeclList &Decls = LookupTable[DC][ND->getDeclName()];
anatofuz
parents:
diff changeset
79 bool EraseResult = Decls.remove(ND);
anatofuz
parents:
diff changeset
80 (void)EraseResult;
anatofuz
parents:
diff changeset
81 assert(EraseResult == true && "Trying to remove not contained Decl");
anatofuz
parents:
diff changeset
82 }
anatofuz
parents:
diff changeset
83
anatofuz
parents:
diff changeset
84 void ASTImporterLookupTable::add(NamedDecl *ND) {
anatofuz
parents:
diff changeset
85 assert(ND);
anatofuz
parents:
diff changeset
86 DeclContext *DC = ND->getDeclContext()->getPrimaryContext();
anatofuz
parents:
diff changeset
87 add(DC, ND);
anatofuz
parents:
diff changeset
88 DeclContext *ReDC = DC->getRedeclContext()->getPrimaryContext();
anatofuz
parents:
diff changeset
89 if (DC != ReDC)
anatofuz
parents:
diff changeset
90 add(ReDC, ND);
anatofuz
parents:
diff changeset
91 }
anatofuz
parents:
diff changeset
92
anatofuz
parents:
diff changeset
93 void ASTImporterLookupTable::remove(NamedDecl *ND) {
anatofuz
parents:
diff changeset
94 assert(ND);
anatofuz
parents:
diff changeset
95 DeclContext *DC = ND->getDeclContext()->getPrimaryContext();
anatofuz
parents:
diff changeset
96 remove(DC, ND);
anatofuz
parents:
diff changeset
97 DeclContext *ReDC = DC->getRedeclContext()->getPrimaryContext();
anatofuz
parents:
diff changeset
98 if (DC != ReDC)
anatofuz
parents:
diff changeset
99 remove(ReDC, ND);
anatofuz
parents:
diff changeset
100 }
anatofuz
parents:
diff changeset
101
anatofuz
parents:
diff changeset
102 ASTImporterLookupTable::LookupResult
anatofuz
parents:
diff changeset
103 ASTImporterLookupTable::lookup(DeclContext *DC, DeclarationName Name) const {
anatofuz
parents:
diff changeset
104 auto DCI = LookupTable.find(DC->getPrimaryContext());
anatofuz
parents:
diff changeset
105 if (DCI == LookupTable.end())
anatofuz
parents:
diff changeset
106 return {};
anatofuz
parents:
diff changeset
107
anatofuz
parents:
diff changeset
108 const auto &FoundNameMap = DCI->second;
anatofuz
parents:
diff changeset
109 auto NamesI = FoundNameMap.find(Name);
anatofuz
parents:
diff changeset
110 if (NamesI == FoundNameMap.end())
anatofuz
parents:
diff changeset
111 return {};
anatofuz
parents:
diff changeset
112
anatofuz
parents:
diff changeset
113 return NamesI->second;
anatofuz
parents:
diff changeset
114 }
anatofuz
parents:
diff changeset
115
anatofuz
parents:
diff changeset
116 void ASTImporterLookupTable::dump(DeclContext *DC) const {
anatofuz
parents:
diff changeset
117 auto DCI = LookupTable.find(DC->getPrimaryContext());
anatofuz
parents:
diff changeset
118 if (DCI == LookupTable.end())
anatofuz
parents:
diff changeset
119 llvm::errs() << "empty\n";
anatofuz
parents:
diff changeset
120 const auto &FoundNameMap = DCI->second;
anatofuz
parents:
diff changeset
121 for (const auto &Entry : FoundNameMap) {
anatofuz
parents:
diff changeset
122 DeclarationName Name = Entry.first;
anatofuz
parents:
diff changeset
123 llvm::errs() << "==== Name: ";
anatofuz
parents:
diff changeset
124 Name.dump();
anatofuz
parents:
diff changeset
125 const DeclList& List = Entry.second;
anatofuz
parents:
diff changeset
126 for (NamedDecl *ND : List) {
anatofuz
parents:
diff changeset
127 ND->dump();
anatofuz
parents:
diff changeset
128 }
anatofuz
parents:
diff changeset
129 }
anatofuz
parents:
diff changeset
130 }
anatofuz
parents:
diff changeset
131
anatofuz
parents:
diff changeset
132 void ASTImporterLookupTable::dump() const {
anatofuz
parents:
diff changeset
133 for (const auto &Entry : LookupTable) {
anatofuz
parents:
diff changeset
134 DeclContext *DC = Entry.first;
anatofuz
parents:
diff changeset
135 StringRef Primary = DC->getPrimaryContext() ? " primary" : "";
anatofuz
parents:
diff changeset
136 llvm::errs() << "== DC:" << cast<Decl>(DC) << Primary << "\n";
anatofuz
parents:
diff changeset
137 dump(DC);
anatofuz
parents:
diff changeset
138 }
anatofuz
parents:
diff changeset
139 }
anatofuz
parents:
diff changeset
140
anatofuz
parents:
diff changeset
141 } // namespace clang