Coverage Report

Created: 2018-11-16 02:38

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