]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/llvm/tools/clang/include/clang/AST/ASTImporterLookupTable.h
Merge clang trunk r351319, resolve conflicts, and update FREEBSD-Xlist.
[FreeBSD/FreeBSD.git] / contrib / llvm / tools / clang / include / clang / AST / ASTImporterLookupTable.h
1 //===- ASTImporterLookupTable.h - ASTImporter specific lookup--*- 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 ASTImporterLookupTable class which implements a
11 //  lookup procedure for the import mechanism.
12 //
13 //===----------------------------------------------------------------------===//
14
15 #ifndef LLVM_CLANG_AST_ASTIMPORTERLOOKUPTABLE_H
16 #define LLVM_CLANG_AST_ASTIMPORTERLOOKUPTABLE_H
17
18 #include "clang/AST/DeclBase.h" // lookup_result
19 #include "clang/AST/DeclarationName.h"
20 #include "llvm/ADT/DenseMap.h"
21 #include "llvm/ADT/SetVector.h"
22
23 namespace clang {
24
25 class ASTContext;
26 class NamedDecl;
27 class DeclContext;
28
29 // There are certain cases when normal C/C++ lookup (localUncachedLookup)
30 // does not find AST nodes. E.g.:
31 // Example 1:
32 //   template <class T>
33 //   struct X {
34 //     friend void foo(); // this is never found in the DC of the TU.
35 //   };
36 // Example 2:
37 //   // The fwd decl to Foo is not found in the lookupPtr of the DC of the
38 //   // translation unit decl.
39 //   // Here we could find the node by doing a traverse throught the list of
40 //   // the Decls in the DC, but that would not scale.
41 //   struct A { struct Foo *p; };
42 // This is a severe problem because the importer decides if it has to create a
43 // new Decl or not based on the lookup results.
44 // To overcome these cases we need an importer specific lookup table which
45 // holds every node and we are not interested in any C/C++ specific visibility
46 // considerations. Simply, we must know if there is an existing Decl in a
47 // given DC. Once we found it then we can handle any visibility related tasks.
48 class ASTImporterLookupTable {
49
50   // We store a list of declarations for each name.
51   // And we collect these lists for each DeclContext.
52   // We could have a flat map with (DeclContext, Name) tuple as key, but a two
53   // level map seems easier to handle.
54   using DeclList = llvm::SmallSetVector<NamedDecl *, 2>;
55   using NameMap = llvm::SmallDenseMap<DeclarationName, DeclList, 4>;
56   using DCMap = llvm::DenseMap<DeclContext *, NameMap>;
57
58   void add(DeclContext *DC, NamedDecl *ND);
59   void remove(DeclContext *DC, NamedDecl *ND);
60
61   DCMap LookupTable;
62
63 public:
64   ASTImporterLookupTable(TranslationUnitDecl &TU);
65   void add(NamedDecl *ND);
66   void remove(NamedDecl *ND);
67   using LookupResult = DeclList;
68   LookupResult lookup(DeclContext *DC, DeclarationName Name) const;
69   void dump(DeclContext *DC) const;
70   void dump() const;
71 };
72
73 } // namespace clang
74
75 #endif // LLVM_CLANG_AST_ASTIMPORTERLOOKUPTABLE_H