-
Notifications
You must be signed in to change notification settings - Fork 311
Expand file tree
/
Copy pathExternalInterpreterSource.cpp
More file actions
336 lines (283 loc) · 13.8 KB
/
ExternalInterpreterSource.cpp
File metadata and controls
336 lines (283 loc) · 13.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
//--------------------------------------------------------------------*- C++ -*-
// CLING - the C++ LLVM-based InterpreterG :)
// author: Elisavet Sakellari <elisavet.sakellari@cern.ch>
//
// This file is dual-licensed: you can choose to license it under the University
// of Illinois Open Source License or the GNU Lesser General Public License. See
// LICENSE.TXT for details.
//------------------------------------------------------------------------------
#include "ExternalInterpreterSource.h"
#include "cling/Interpreter/Interpreter.h"
#include "cling/Utils/Diagnostics.h"
#include "clang/AST/ASTContext.h"
#include "clang/AST/ASTDiagnostic.h"
#include "clang/AST/ASTImporter.h"
#include "clang/AST/DeclContextInternals.h"
#include "clang/Frontend/CompilerInstance.h"
#include "clang/Lex/Preprocessor.h"
#include "clang/Sema/Sema.h"
using namespace clang;
namespace {
class ClingASTImporter : public ASTImporter {
private:
cling::ExternalInterpreterSource &m_Source;
public:
ClingASTImporter(ASTContext &ToContext, FileManager &ToFileManager,
ASTContext &FromContext, FileManager &FromFileManager,
bool MinimalImport,
cling::ExternalInterpreterSource& source):
ASTImporter(ToContext, ToFileManager, FromContext, FromFileManager,
MinimalImport), m_Source(source) {}
virtual ~ClingASTImporter() = default;
Decl *Imported(Decl *From, Decl *To) override {
ASTImporter::Imported(From, To);
if (clang::TagDecl* toTagDecl = dyn_cast<TagDecl>(To)) {
toTagDecl->setHasExternalLexicalStorage();
toTagDecl->setMustBuildLookupTable();
toTagDecl->setHasExternalVisibleStorage();
}
if (NamespaceDecl *toNamespaceDecl = dyn_cast<NamespaceDecl>(To)) {
toNamespaceDecl->setHasExternalVisibleStorage();
}
if (ObjCContainerDecl *toContainerDecl = dyn_cast<ObjCContainerDecl>(To)) {
toContainerDecl->setHasExternalLexicalStorage();
toContainerDecl->setHasExternalVisibleStorage();
}
// Put the name of the Decl imported with the
// DeclarationName coming from the parent, in my map.
if (NamedDecl *toNamedDecl = llvm::dyn_cast<NamedDecl>(To)) {
NamedDecl *fromNamedDecl = llvm::dyn_cast<NamedDecl>(From);
m_Source.addToImportedDecls(toNamedDecl->getDeclName(),
fromNamedDecl->getDeclName());
}
if (DeclContext *toDeclContext = llvm::dyn_cast<DeclContext>(To)) {
DeclContext *fromDeclContext = llvm::dyn_cast<DeclContext>(From);
m_Source.addToImportedDeclContexts(toDeclContext, fromDeclContext);
}
return To;
}
};
}
namespace cling {
ExternalInterpreterSource::ExternalInterpreterSource(
const cling::Interpreter *parent, cling::Interpreter *child) :
m_ParentInterpreter(parent), m_ChildInterpreter(child) {
clang::DeclContext *parentTUDeclContext =
m_ParentInterpreter->getCI()->getASTContext().getTranslationUnitDecl();
clang::DeclContext *childTUDeclContext =
m_ChildInterpreter->getCI()->getASTContext().getTranslationUnitDecl();
// Also keep in the map of Decl Contexts the Translation Unit Decl Context
m_ImportedDeclContexts.emplace(childTUDeclContext, parentTUDeclContext);
FileManager &childFM = m_ChildInterpreter->getCI()->getFileManager();
FileManager &parentFM = m_ParentInterpreter->getCI()->getFileManager();
ASTContext &fromASTContext = m_ParentInterpreter->getCI()->getASTContext();
ASTContext &toASTContext = m_ChildInterpreter->getCI()->getASTContext();
ClingASTImporter* importer
= new ClingASTImporter(toASTContext, childFM, fromASTContext, parentFM,
/*MinimalImport : ON*/ true, *this);
m_Importer.reset(llvm::dyn_cast<ASTImporter>(importer));
}
ExternalInterpreterSource::~ExternalInterpreterSource() {}
void ExternalInterpreterSource::ImportDecl(Decl *declToImport,
DeclarationName &childDeclName,
const DeclarationName &parentDeclName,
const DeclContext *childCurrentDeclContext) {
// Don't do the import if we have a Function Template or using decls. They
// are not supported by clang.
// FIXME: These are temporary checks and should be de-activated once clang
// supports their import.
if ((declToImport->isFunctionOrFunctionTemplate()
&& declToImport->isTemplateDecl()) || dyn_cast<UsingDecl>(declToImport)
|| dyn_cast<UsingDirectiveDecl>(declToImport)
|| dyn_cast<UsingShadowDecl>(declToImport)) {
#ifndef NDEBUG
utils::DiagnosticsStore DS(
m_Importer->getFromContext().getDiagnostics(), false, false, true);
assert((m_Importer->Import(declToImport)==nullptr) && "Import worked!");
assert(!DS.empty() &&
DS[0].getID() == clang::diag::err_unsupported_ast_node &&
"Import may be supported");
#endif
return;
}
if (Decl *importedDecl = m_Importer->Import(declToImport)) {
if (NamedDecl *importedNamedDecl = llvm::dyn_cast<NamedDecl>(importedDecl)) {
SetExternalVisibleDeclsForName(childCurrentDeclContext,
importedNamedDecl->getDeclName(),
importedNamedDecl);
}
// Put the name of the Decl imported with the
// DeclarationName coming from the parent, in my map.
m_ImportedDecls.emplace(childDeclName, parentDeclName);
}
}
void ExternalInterpreterSource::ImportDeclContext(
DeclContext *declContextToImport,
DeclarationName &childDeclName,
const DeclarationName &parentDeclName,
const DeclContext *childCurrentDeclContext) {
if (DeclContext *importedDeclContext =
m_Importer->ImportContext(declContextToImport)) {
importedDeclContext->setHasExternalVisibleStorage(true);
if (NamedDecl *importedNamedDecl =
llvm::dyn_cast<NamedDecl>(importedDeclContext)) {
SetExternalVisibleDeclsForName(childCurrentDeclContext,
importedNamedDecl->getDeclName(),
importedNamedDecl);
}
// Put the name of the DeclContext imported with the
// DeclarationName coming from the parent, in my map.
m_ImportedDecls.emplace(childDeclName, parentDeclName);
// And also put the declaration context I found from the parent Interpreter
// in the map of the child Interpreter to have it for the future.
m_ImportedDeclContexts.emplace(importedDeclContext, declContextToImport);
}
}
bool ExternalInterpreterSource::Import(DeclContext::lookup_result lookup_result,
const DeclContext *childCurrentDeclContext,
DeclarationName &childDeclName,
const DeclarationName &parentDeclName) {
for (NamedDecl* ND : lookup_result) {
// Check if this Name we are looking for is
// a DeclContext (for example a Namespace, function etc.).
if (DeclContext *declContextToImport = llvm::dyn_cast<DeclContext>(ND)) {
ImportDeclContext(declContextToImport, childDeclName,
parentDeclName, childCurrentDeclContext);
}
ImportDecl(ND, childDeclName, parentDeclName, childCurrentDeclContext);
}
return true;
}
///\brief This is the one of the most important function of the class
/// since from here initiates the lookup and import part of the missing
/// Decl(s) (Contexts).
///
bool ExternalInterpreterSource::FindExternalVisibleDeclsByName(
const DeclContext *childCurrentDeclContext, DeclarationName childDeclName) {
assert(childDeclName && "Child Decl name is empty");
assert(childCurrentDeclContext->hasExternalVisibleStorage() &&
"DeclContext has no visible decls in storage");
// Search in the map of the stored Decl Contexts for this
// Decl Context.
auto IDeclContext = m_ImportedDeclContexts.find(childCurrentDeclContext);
// If childCurrentDeclContext was found before and is already in the map,
// then do the lookup using the stored pointer.
if (IDeclContext == m_ImportedDeclContexts.end())
return false;
DeclContext *parentDC = IDeclContext->second;
//Check if we have already found this declaration Name before
DeclarationName parentDeclName;
auto IDecl = m_ImportedDecls.find(childDeclName);
if (IDecl != m_ImportedDecls.end()) {
parentDeclName = IDecl->second;
} else {
// Get the identifier info from the parent interpreter
// for this Name.
const std::string name = childDeclName.getAsString();
IdentifierTable &parentIdentifierTable =
m_ParentInterpreter->getCI()->getASTContext().Idents;
IdentifierInfo &parentIdentifierInfo =
parentIdentifierTable.get(name);
parentDeclName = DeclarationName(&parentIdentifierInfo);
// Make sure then lookup name is right, this is an issue looking to import
// a constructor, where lookup_result can hold the injected class name.
// FIXME: Pre-filter childDeclName.getNameKind() and just drop import
// of any unsupported types (i.e. CXXUsingDirective)
const DeclarationName::NameKind ChildKind = childDeclName.getNameKind();
if (parentDeclName.getNameKind() != ChildKind) {
(void)parentDC->lookup(parentDeclName);
const auto* DM = parentDC->getPrimaryContext()->getLookupPtr();
assert(DM && "No lookup map");
for (auto&& Entry : *DM) {
const DeclarationName& DN = Entry.first;
if (DN.getNameKind() == ChildKind) {
if (DN.getAsString() == name) {
parentDeclName = DN;
break;
}
}
}
}
}
DeclContext::lookup_result lookup_result = parentDC->lookup(parentDeclName);
// Check if we found this Name in the parent interpreter
if (lookup_result.empty())
return false;
if (!Import(lookup_result, childCurrentDeclContext, childDeclName,
parentDeclName))
return false;
// FIXME: The failure of this to work out of the box seems like a deeper
// issue (in ASTImporter::ImportContext or
// CXXRecordDecl::getVisibleConversionFunctions for example).
// Constructing or importing a variable of type CXXRecordDecl.
// Import the all constructors, conversion routines, and the destructor.
const CXXRecordDecl* CXD = dyn_cast<CXXRecordDecl>(childCurrentDeclContext);
if (!CXD && isa<VarDecl>(*lookup_result.begin())) {
assert(lookup_result.size() == 1 && "More than one VarDecl?!");
CXD = cast<VarDecl>(*lookup_result.begin())
->getType()
->getAsCXXRecordDecl();
if (CXD)
parentDC = cast<DeclContext>(const_cast<CXXRecordDecl*>(CXD));
}
if (!CXD)
return true;
ASTContext& AST = m_ChildInterpreter->getCI()->getASTContext();
const auto CanonQT = CXD->getCanonicalDecl()
->getTypeForDecl()
->getCanonicalTypeUnqualified();
const auto* DM = parentDC->getPrimaryContext()->getLookupPtr();
assert(DM && "No lookup map");
for (auto&& Entry : *DM) {
const DeclarationName& ParentDN = Entry.first;
const auto ParentKind = ParentDN.getNameKind();
if (ParentKind < DeclarationName::CXXConstructorName ||
ParentKind > DeclarationName::CXXConversionFunctionName)
continue;
if (m_ImportedDecls.find(childDeclName) == m_ImportedDecls.end())
continue;
DeclarationName ChildDN =
AST.DeclarationNames.getCXXSpecialName(ParentDN.getNameKind(),
CanonQT);
// FIXME: DeclContext::Import checks if the decl is a DeclContext.
// Is that neccessary?
DeclContext::lookup_result LR = parentDC->lookup(ParentDN);
if (!LR.empty() &&
!Import(LR, childCurrentDeclContext, ChildDN, ParentDN))
return false;
}
return true;
}
///\brief Make available to child all decls in parent's decl context
/// that corresponds to child decl context.
void ExternalInterpreterSource::completeVisibleDeclsMap(
const clang::DeclContext *childDeclContext) {
assert (childDeclContext && "No child decl context!");
if (!childDeclContext->hasExternalVisibleStorage())
return;
// Search in the map of the stored Decl Contexts for this
// Decl Context.
auto IDeclContext = m_ImportedDeclContexts.find(childDeclContext);
// If childCurrentDeclContext was found before and is already in the map,
// then do the lookup using the stored pointer.
if (IDeclContext == m_ImportedDeclContexts.end()) return ;
DeclContext *parentDeclContext = IDeclContext->second;
// Filter the decls from the external source using the stem information
// stored in Sema.
StringRef filter =
m_ChildInterpreter->getCI()->getPreprocessor().getCodeCompletionFilter();
for (Decl* D : parentDeclContext->decls()) {
if (NamedDecl* parentDecl = llvm::dyn_cast<NamedDecl>(D)) {
DeclarationName childDeclName = parentDecl->getDeclName();
if (auto II = childDeclName.getAsIdentifierInfo()) {
StringRef name = II->getName();
if (!name.empty() && name.startswith(filter))
ImportDecl(parentDecl, childDeclName, childDeclName,
childDeclContext);
}
}
}
const_cast<DeclContext *>(childDeclContext)->
setHasExternalVisibleStorage(false);
}
} // end namespace cling