Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-05-10 08:36:28

0001 //===- ASTImporterLookupTable.h - ASTImporter specific lookup--*- C++ -*---===//
0002 //
0003 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
0004 // See https://llvm.org/LICENSE.txt for license information.
0005 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
0006 //
0007 //===----------------------------------------------------------------------===//
0008 //
0009 //  This file defines the ASTImporterLookupTable class which implements a
0010 //  lookup procedure for the import mechanism.
0011 //
0012 //===----------------------------------------------------------------------===//
0013 
0014 #ifndef LLVM_CLANG_AST_ASTIMPORTERLOOKUPTABLE_H
0015 #define LLVM_CLANG_AST_ASTIMPORTERLOOKUPTABLE_H
0016 
0017 #include "clang/AST/DeclBase.h" // lookup_result
0018 #include "clang/AST/DeclarationName.h"
0019 #include "llvm/ADT/DenseMap.h"
0020 #include "llvm/ADT/SetVector.h"
0021 
0022 namespace clang {
0023 
0024 class NamedDecl;
0025 class DeclContext;
0026 
0027 // There are certain cases when normal C/C++ lookup (localUncachedLookup)
0028 // does not find AST nodes. E.g.:
0029 // Example 1:
0030 //   template <class T>
0031 //   struct X {
0032 //     friend void foo(); // this is never found in the DC of the TU.
0033 //   };
0034 // Example 2:
0035 //   // The fwd decl to Foo is not found in the lookupPtr of the DC of the
0036 //   // translation unit decl.
0037 //   // Here we could find the node by doing a traverse throught the list of
0038 //   // the Decls in the DC, but that would not scale.
0039 //   struct A { struct Foo *p; };
0040 // This is a severe problem because the importer decides if it has to create a
0041 // new Decl or not based on the lookup results.
0042 // To overcome these cases we need an importer specific lookup table which
0043 // holds every node and we are not interested in any C/C++ specific visibility
0044 // considerations. Simply, we must know if there is an existing Decl in a
0045 // given DC. Once we found it then we can handle any visibility related tasks.
0046 class ASTImporterLookupTable {
0047 
0048   // We store a list of declarations for each name.
0049   // And we collect these lists for each DeclContext.
0050   // We could have a flat map with (DeclContext, Name) tuple as key, but a two
0051   // level map seems easier to handle.
0052   using DeclList = llvm::SmallSetVector<NamedDecl *, 2>;
0053   using NameMap = llvm::SmallDenseMap<DeclarationName, DeclList, 4>;
0054   using DCMap = llvm::DenseMap<DeclContext *, NameMap>;
0055 
0056   void add(DeclContext *DC, NamedDecl *ND);
0057   void remove(DeclContext *DC, NamedDecl *ND);
0058 
0059   DCMap LookupTable;
0060 
0061 public:
0062   ASTImporterLookupTable(TranslationUnitDecl &TU);
0063   void add(NamedDecl *ND);
0064   void remove(NamedDecl *ND);
0065   // Sometimes a declaration is created first with a temporarily value of decl
0066   // context (often the translation unit) and later moved to the final context.
0067   // This happens for declarations that are created before the final declaration
0068   // context. In such cases the lookup table needs to be updated.
0069   // (The declaration is in these cases not added to the temporary decl context,
0070   // only its parent is set.)
0071   // FIXME: It would be better to not add the declaration to the temporary
0072   // context at all in the lookup table, but this requires big change in
0073   // ASTImporter.
0074   // The function should be called when the old context is definitely different
0075   // from the new.
0076   void update(NamedDecl *ND, DeclContext *OldDC);
0077   // Same as 'update' but allow if 'ND' is not in the table or the old context
0078   // is the same as the new.
0079   // FIXME: The old redeclaration context is not handled.
0080   void updateForced(NamedDecl *ND, DeclContext *OldDC);
0081   using LookupResult = DeclList;
0082   LookupResult lookup(DeclContext *DC, DeclarationName Name) const;
0083   // Check if the `ND` is within the lookup table (with its current name) in
0084   // context `DC`. This is intended for debug purposes when the DeclContext of a
0085   // NamedDecl is changed.
0086   bool contains(DeclContext *DC, NamedDecl *ND) const;
0087   void dump(DeclContext *DC) const;
0088   void dump() const;
0089 };
0090 
0091 } // namespace clang
0092 
0093 #endif // LLVM_CLANG_AST_ASTIMPORTERLOOKUPTABLE_H