Coverage Report

Created: 2019-02-15 18:59

/Users/buildslave/jenkins/workspace/clang-stage2-coverage-R/llvm/tools/clang/include/clang/AST/ASTImporter.h
Line
Count
Source (jump to first uncovered line)
1
//===- ASTImporter.h - Importing ASTs from other Contexts -------*- C++ -*-===//
2
//
3
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4
// See https://llvm.org/LICENSE.txt for license information.
5
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6
//
7
//===----------------------------------------------------------------------===//
8
//
9
//  This file defines the ASTImporter class which imports AST nodes from one
10
//  context into another context.
11
//
12
//===----------------------------------------------------------------------===//
13
14
#ifndef LLVM_CLANG_AST_ASTIMPORTER_H
15
#define LLVM_CLANG_AST_ASTIMPORTER_H
16
17
#include "clang/AST/DeclarationName.h"
18
#include "clang/AST/NestedNameSpecifier.h"
19
#include "clang/AST/TemplateName.h"
20
#include "clang/AST/Type.h"
21
#include "clang/Basic/Diagnostic.h"
22
#include "clang/Basic/IdentifierTable.h"
23
#include "clang/Basic/LLVM.h"
24
#include "clang/Basic/SourceLocation.h"
25
#include "llvm/ADT/DenseMap.h"
26
#include "llvm/ADT/DenseSet.h"
27
#include "llvm/ADT/Optional.h"
28
#include "llvm/ADT/SmallVector.h"
29
#include "llvm/Support/Error.h"
30
#include <utility>
31
32
namespace clang {
33
34
class ASTContext;
35
class Attr;
36
class ASTImporterLookupTable;
37
class CXXBaseSpecifier;
38
class CXXCtorInitializer;
39
class Decl;
40
class DeclContext;
41
class Expr;
42
class FileManager;
43
class NamedDecl;
44
class Stmt;
45
class TagDecl;
46
class TranslationUnitDecl;
47
class TypeSourceInfo;
48
49
  class ImportError : public llvm::ErrorInfo<ImportError> {
50
  public:
51
    /// \brief Kind of error when importing an AST component.
52
    enum ErrorKind {
53
        NameConflict, /// Naming ambiguity (likely ODR violation).
54
        UnsupportedConstruct, /// Not supported node or case.
55
        Unknown /// Other error.
56
    };
57
58
    ErrorKind Error;
59
60
    static char ID;
61
62
81
    ImportError() : Error(Unknown) { }
63
    ImportError(const ImportError &Other) : Error(Other.Error) { }
64
27
    ImportError(ErrorKind Error) : Error(Error) { }
65
66
    std::string toString() const;
67
68
    void log(raw_ostream &OS) const override;
69
    std::error_code convertToErrorCode() const override;
70
  };
71
72
  // \brief Returns with a list of declarations started from the canonical decl
73
  // then followed by subsequent decls in the translation unit.
74
  // This gives a canonical list for each entry in the redecl chain.
75
  // `Decl::redecls()` gives a list of decls which always start from the
76
  // previous decl and the next item is actually the previous item in the order
77
  // of source locations.  Thus, `Decl::redecls()` gives different lists for
78
  // the different entries in a given redecl chain.
79
  llvm::SmallVector<Decl*, 2> getCanonicalForwardRedeclChain(Decl* D);
80
81
  /// Imports selected nodes from one AST context into another context,
82
  /// merging AST nodes where appropriate.
83
  class ASTImporter {
84
    friend class ASTNodeImporter;
85
  public:
86
    using NonEquivalentDeclSet = llvm::DenseSet<std::pair<Decl *, Decl *>>;
87
    using ImportedCXXBaseSpecifierMap =
88
        llvm::DenseMap<const CXXBaseSpecifier *, CXXBaseSpecifier *>;
89
90
  private:
91
92
    /// Pointer to the import specific lookup table, which may be shared
93
    /// amongst several ASTImporter objects.
94
    /// This is an externally managed resource (and should exist during the
95
    /// lifetime of the ASTImporter object)
96
    /// If not set then the original C/C++ lookup is used.
97
    ASTImporterLookupTable *LookupTable = nullptr;
98
99
    /// The contexts we're importing to and from.
100
    ASTContext &ToContext, &FromContext;
101
102
    /// The file managers we're importing to and from.
103
    FileManager &ToFileManager, &FromFileManager;
104
105
    /// Whether to perform a minimal import.
106
    bool Minimal;
107
108
    /// Whether the last diagnostic came from the "from" context.
109
    bool LastDiagFromFrom = false;
110
111
    /// Mapping from the already-imported types in the "from" context
112
    /// to the corresponding types in the "to" context.
113
    llvm::DenseMap<const Type *, const Type *> ImportedTypes;
114
115
    /// Mapping from the already-imported declarations in the "from"
116
    /// context to the corresponding declarations in the "to" context.
117
    llvm::DenseMap<Decl *, Decl *> ImportedDecls;
118
119
    /// Mapping from the already-imported declarations in the "to"
120
    /// context to the corresponding declarations in the "from" context.
121
    llvm::DenseMap<Decl *, Decl *> ImportedFromDecls;
122
123
    /// Mapping from the already-imported statements in the "from"
124
    /// context to the corresponding statements in the "to" context.
125
    llvm::DenseMap<Stmt *, Stmt *> ImportedStmts;
126
127
    /// Mapping from the already-imported FileIDs in the "from" source
128
    /// manager to the corresponding FileIDs in the "to" source manager.
129
    llvm::DenseMap<FileID, FileID> ImportedFileIDs;
130
131
    /// Mapping from the already-imported CXXBasesSpecifier in
132
    ///  the "from" source manager to the corresponding CXXBasesSpecifier
133
    ///  in the "to" source manager.
134
    ImportedCXXBaseSpecifierMap ImportedCXXBaseSpecifiers;
135
136
    /// Declaration (from, to) pairs that are known not to be equivalent
137
    /// (which we have already complained about).
138
    NonEquivalentDeclSet NonEquivalentDecls;
139
140
    using FoundDeclsTy = SmallVector<NamedDecl *, 2>;
141
    FoundDeclsTy findDeclsInToCtx(DeclContext *DC, DeclarationName Name);
142
143
    void AddToLookupTable(Decl *ToD);
144
145
  public:
146
147
    /// \param ToContext The context we'll be importing into.
148
    ///
149
    /// \param ToFileManager The file manager we'll be importing into.
150
    ///
151
    /// \param FromContext The context we'll be importing from.
152
    ///
153
    /// \param FromFileManager The file manager we'll be importing into.
154
    ///
155
    /// \param MinimalImport If true, the importer will attempt to import
156
    /// as little as it can, e.g., by importing declarations as forward
157
    /// declarations that can be completed at a later point.
158
    ///
159
    /// \param LookupTable The importer specific lookup table which may be
160
    /// shared amongst several ASTImporter objects.
161
    /// If not set then the original C/C++ lookup is used.
162
    ASTImporter(ASTContext &ToContext, FileManager &ToFileManager,
163
                ASTContext &FromContext, FileManager &FromFileManager,
164
                bool MinimalImport,
165
                ASTImporterLookupTable *LookupTable = nullptr);
166
167
    virtual ~ASTImporter();
168
169
    /// Whether the importer will perform a minimal import, creating
170
    /// to-be-completed forward declarations when possible.
171
4.13k
    bool isMinimalImport() const { return Minimal; }
172
173
    /// \brief Import the given object, returns the result.
174
    ///
175
    /// \param To Import the object into this variable.
176
    /// \param From Object to import.
177
    /// \return Error information (success or error).
178
    template <typename ImportT>
179
7.43k
    LLVM_NODISCARD llvm::Error importInto(ImportT &To, const ImportT &From) {
180
7.43k
      To = Import(From);
181
7.43k
      if (From && 
!To7.14k
)
182
0
          return llvm::make_error<ImportError>();
183
7.43k
      return llvm::Error::success();
184
7.43k
      // FIXME: this should be the final code
185
7.43k
      //auto ToOrErr = Import(From);
186
7.43k
      //if (ToOrErr)
187
7.43k
      //  To = *ToOrErr;
188
7.43k
      //return ToOrErr.takeError();
189
7.43k
    }
190
191
    /// Import the given type from the "from" context into the "to"
192
    /// context. A null type is imported as a null type (no error).
193
    ///
194
    /// \returns The equivalent type in the "to" context, or the import error.
195
    llvm::Expected<QualType> Import_New(QualType FromT);
196
    // FIXME: Remove this version.
197
    QualType Import(QualType FromT);
198
199
    /// Import the given type source information from the
200
    /// "from" context into the "to" context.
201
    ///
202
    /// \returns The equivalent type source information in the "to"
203
    /// context, or the import error.
204
    llvm::Expected<TypeSourceInfo *> Import_New(TypeSourceInfo *FromTSI);
205
    // FIXME: Remove this version.
206
    TypeSourceInfo *Import(TypeSourceInfo *FromTSI);
207
208
    /// Import the given attribute from the "from" context into the
209
    /// "to" context.
210
    ///
211
    /// \returns The equivalent attribute in the "to" context, or the import
212
    /// error.
213
    llvm::Expected<Attr *> Import_New(const Attr *FromAttr);
214
    // FIXME: Remove this version.
215
    Attr *Import(const Attr *FromAttr);
216
217
    /// Import the given declaration from the "from" context into the
218
    /// "to" context.
219
    ///
220
    /// \returns The equivalent declaration in the "to" context, or the import
221
    /// error.
222
    llvm::Expected<Decl *> Import_New(Decl *FromD);
223
0
    llvm::Expected<Decl *> Import_New(const Decl *FromD) {
224
0
      return Import_New(const_cast<Decl *>(FromD));
225
0
    }
226
    // FIXME: Remove this version.
227
    Decl *Import(Decl *FromD);
228
    Decl *Import(const Decl *FromD) {
229
      return Import(const_cast<Decl *>(FromD));
230
    }
231
232
    /// Return the copy of the given declaration in the "to" context if
233
    /// it has already been imported from the "from" context.  Otherwise return
234
    /// nullptr.
235
    Decl *GetAlreadyImportedOrNull(const Decl *FromD) const;
236
237
    /// Return the translation unit from where the declaration was
238
    /// imported. If it does not exist nullptr is returned.
239
    TranslationUnitDecl *GetFromTU(Decl *ToD);
240
241
    /// Import the given declaration context from the "from"
242
    /// AST context into the "to" AST context.
243
    ///
244
    /// \returns the equivalent declaration context in the "to"
245
    /// context, or error value.
246
    llvm::Expected<DeclContext *> ImportContext(DeclContext *FromDC);
247
248
    /// Import the given expression from the "from" context into the
249
    /// "to" context.
250
    ///
251
    /// \returns The equivalent expression in the "to" context, or the import
252
    /// error.
253
    llvm::Expected<Expr *> Import_New(Expr *FromE);
254
    // FIXME: Remove this version.
255
    Expr *Import(Expr *FromE);
256
257
    /// Import the given statement from the "from" context into the
258
    /// "to" context.
259
    ///
260
    /// \returns The equivalent statement in the "to" context, or the import
261
    /// error.
262
    llvm::Expected<Stmt *> Import_New(Stmt *FromS);
263
    // FIXME: Remove this version.
264
    Stmt *Import(Stmt *FromS);
265
266
    /// Import the given nested-name-specifier from the "from"
267
    /// context into the "to" context.
268
    ///
269
    /// \returns The equivalent nested-name-specifier in the "to"
270
    /// context, or the import error.
271
    llvm::Expected<NestedNameSpecifier *>
272
    Import_New(NestedNameSpecifier *FromNNS);
273
    // FIXME: Remove this version.
274
    NestedNameSpecifier *Import(NestedNameSpecifier *FromNNS);
275
276
    /// Import the given nested-name-specifier-loc from the "from"
277
    /// context into the "to" context.
278
    ///
279
    /// \returns The equivalent nested-name-specifier-loc in the "to"
280
    /// context, or the import error.
281
    llvm::Expected<NestedNameSpecifierLoc>
282
    Import_New(NestedNameSpecifierLoc FromNNS);
283
    // FIXME: Remove this version.
284
    NestedNameSpecifierLoc Import(NestedNameSpecifierLoc FromNNS);
285
286
    /// Import the given template name from the "from" context into the
287
    /// "to" context, or the import error.
288
    llvm::Expected<TemplateName> Import_New(TemplateName From);
289
    // FIXME: Remove this version.
290
    TemplateName Import(TemplateName From);
291
292
    /// Import the given source location from the "from" context into
293
    /// the "to" context.
294
    ///
295
    /// \returns The equivalent source location in the "to" context, or the
296
    /// import error.
297
    llvm::Expected<SourceLocation> Import_New(SourceLocation FromLoc);
298
    // FIXME: Remove this version.
299
    SourceLocation Import(SourceLocation FromLoc);
300
301
    /// Import the given source range from the "from" context into
302
    /// the "to" context.
303
    ///
304
    /// \returns The equivalent source range in the "to" context, or the import
305
    /// error.
306
    llvm::Expected<SourceRange> Import_New(SourceRange FromRange);
307
    // FIXME: Remove this version.
308
    SourceRange Import(SourceRange FromRange);
309
310
    /// Import the given declaration name from the "from"
311
    /// context into the "to" context.
312
    ///
313
    /// \returns The equivalent declaration name in the "to" context, or the
314
    /// import error.
315
    llvm::Expected<DeclarationName> Import_New(DeclarationName FromName);
316
    // FIXME: Remove this version.
317
    DeclarationName Import(DeclarationName FromName);
318
319
    /// Import the given identifier from the "from" context
320
    /// into the "to" context.
321
    ///
322
    /// \returns The equivalent identifier in the "to" context. Note: It
323
    /// returns nullptr only if the FromId was nullptr.
324
    IdentifierInfo *Import(const IdentifierInfo *FromId);
325
326
    /// Import the given Objective-C selector from the "from"
327
    /// context into the "to" context.
328
    ///
329
    /// \returns The equivalent selector in the "to" context, or the import
330
    /// error.
331
    llvm::Expected<Selector> Import_New(Selector FromSel);
332
    // FIXME: Remove this version.
333
    Selector Import(Selector FromSel);
334
335
    /// Import the given file ID from the "from" context into the
336
    /// "to" context.
337
    ///
338
    /// \returns The equivalent file ID in the source manager of the "to"
339
    /// context, or the import error.
340
    llvm::Expected<FileID> Import_New(FileID);
341
    // FIXME: Remove this version.
342
    FileID Import(FileID);
343
344
    /// Import the given C++ constructor initializer from the "from"
345
    /// context into the "to" context.
346
    ///
347
    /// \returns The equivalent initializer in the "to" context, or the import
348
    /// error.
349
    llvm::Expected<CXXCtorInitializer *>
350
    Import_New(CXXCtorInitializer *FromInit);
351
    // FIXME: Remove this version.
352
    CXXCtorInitializer *Import(CXXCtorInitializer *FromInit);
353
354
    /// Import the given CXXBaseSpecifier from the "from" context into
355
    /// the "to" context.
356
    ///
357
    /// \returns The equivalent CXXBaseSpecifier in the source manager of the
358
    /// "to" context, or the import error.
359
    llvm::Expected<CXXBaseSpecifier *>
360
    Import_New(const CXXBaseSpecifier *FromSpec);
361
    // FIXME: Remove this version.
362
    CXXBaseSpecifier *Import(const CXXBaseSpecifier *FromSpec);
363
364
    /// Import the definition of the given declaration, including all of
365
    /// the declarations it contains.
366
    LLVM_NODISCARD llvm::Error ImportDefinition_New(Decl *From);
367
368
    // FIXME: Compatibility function.
369
    // Usages of this should be changed to ImportDefinition_New.
370
    void ImportDefinition(Decl *From);
371
372
    /// Cope with a name conflict when importing a declaration into the
373
    /// given context.
374
    ///
375
    /// This routine is invoked whenever there is a name conflict while
376
    /// importing a declaration. The returned name will become the name of the
377
    /// imported declaration. By default, the returned name is the same as the
378
    /// original name, leaving the conflict unresolve such that name lookup
379
    /// for this name is likely to find an ambiguity later.
380
    ///
381
    /// Subclasses may override this routine to resolve the conflict, e.g., by
382
    /// renaming the declaration being imported.
383
    ///
384
    /// \param Name the name of the declaration being imported, which conflicts
385
    /// with other declarations.
386
    ///
387
    /// \param DC the declaration context (in the "to" AST context) in which
388
    /// the name is being imported.
389
    ///
390
    /// \param IDNS the identifier namespace in which the name will be found.
391
    ///
392
    /// \param Decls the set of declarations with the same name as the
393
    /// declaration being imported.
394
    ///
395
    /// \param NumDecls the number of conflicting declarations in \p Decls.
396
    ///
397
    /// \returns the name that the newly-imported declaration should have.
398
    virtual DeclarationName HandleNameConflict(DeclarationName Name,
399
                                               DeclContext *DC,
400
                                               unsigned IDNS,
401
                                               NamedDecl **Decls,
402
                                               unsigned NumDecls);
403
404
    /// Retrieve the context that AST nodes are being imported into.
405
28.5k
    ASTContext &getToContext() const { return ToContext; }
406
407
    /// Retrieve the context that AST nodes are being imported from.
408
4.69k
    ASTContext &getFromContext() const { return FromContext; }
409
410
    /// Retrieve the file manager that AST nodes are being imported into.
411
0
    FileManager &getToFileManager() const { return ToFileManager; }
412
413
    /// Retrieve the file manager that AST nodes are being imported from.
414
0
    FileManager &getFromFileManager() const { return FromFileManager; }
415
416
    /// Report a diagnostic in the "to" context.
417
    DiagnosticBuilder ToDiag(SourceLocation Loc, unsigned DiagID);
418
419
    /// Report a diagnostic in the "from" context.
420
    DiagnosticBuilder FromDiag(SourceLocation Loc, unsigned DiagID);
421
422
    /// Return the set of declarations that we know are not equivalent.
423
1.44k
    NonEquivalentDeclSet &getNonEquivalentDecls() { return NonEquivalentDecls; }
424
425
    /// Called for ObjCInterfaceDecl, ObjCProtocolDecl, and TagDecl.
426
    /// Mark the Decl as complete, filling it in as much as possible.
427
    ///
428
    /// \param D A declaration in the "to" context.
429
    virtual void CompleteDecl(Decl* D);
430
431
    /// Subclasses can override this function to observe all of the \c From ->
432
    /// \c To declaration mappings as they are imported.
433
7.22k
    virtual Decl *Imported(Decl *From, Decl *To) { return To; }
434
435
    /// Store and assign the imported declaration to its counterpart.
436
    Decl *MapImported(Decl *From, Decl *To);
437
438
    /// Called by StructuralEquivalenceContext.  If a RecordDecl is
439
    /// being compared to another RecordDecl as part of import, completing the
440
    /// other RecordDecl may trigger importation of the first RecordDecl. This
441
    /// happens especially for anonymous structs.  If the original of the second
442
    /// RecordDecl can be found, we can complete it without the need for
443
    /// importation, eliminating this loop.
444
388
    virtual Decl *GetOriginalDecl(Decl *To) { return nullptr; }
445
446
    /// Determine whether the given types are structurally
447
    /// equivalent.
448
    bool IsStructurallyEquivalent(QualType From, QualType To,
449
                                  bool Complain = true);
450
451
    /// Determine the index of a field in its parent record.
452
    /// F should be a field (or indirect field) declaration.
453
    /// \returns The index of the field in its parent context (starting from 0).
454
    /// On error `None` is returned (parent context is non-record).
455
    static llvm::Optional<unsigned> getFieldIndex(Decl *F);
456
457
  };
458
459
} // namespace clang
460
461
#endif // LLVM_CLANG_AST_ASTIMPORTER_H