Home | History | Annotate | Download | only in Sema
      1 //===- IdentifierResolver.h - Lexical Scope Name 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 IdentifierResolver class, which is used for lexical
     11 // scoped lookup, based on declaration names.
     12 //
     13 //===----------------------------------------------------------------------===//
     14 
     15 #ifndef LLVM_CLANG_SEMA_IDENTIFIERRESOLVER_H
     16 #define LLVM_CLANG_SEMA_IDENTIFIERRESOLVER_H
     17 
     18 #include "clang/Basic/IdentifierTable.h"
     19 #include "llvm/ADT/SmallVector.h"
     20 
     21 namespace clang {
     22 
     23 class ASTContext;
     24 class Decl;
     25 class DeclContext;
     26 class DeclarationName;
     27 class ExternalPreprocessorSource;
     28 class NamedDecl;
     29 class Preprocessor;
     30 class Scope;
     31 
     32 /// IdentifierResolver - Keeps track of shadowed decls on enclosing
     33 /// scopes.  It manages the shadowing chains of declaration names and
     34 /// implements efficient decl lookup based on a declaration name.
     35 class IdentifierResolver {
     36 
     37   /// IdDeclInfo - Keeps track of information about decls associated
     38   /// to a particular declaration name. IdDeclInfos are lazily
     39   /// constructed and assigned to a declaration name the first time a
     40   /// decl with that declaration name is shadowed in some scope.
     41   class IdDeclInfo {
     42   public:
     43     typedef SmallVector<NamedDecl*, 2> DeclsTy;
     44 
     45     inline DeclsTy::iterator decls_begin() { return Decls.begin(); }
     46     inline DeclsTy::iterator decls_end() { return Decls.end(); }
     47 
     48     void AddDecl(NamedDecl *D) { Decls.push_back(D); }
     49 
     50     /// RemoveDecl - Remove the decl from the scope chain.
     51     /// The decl must already be part of the decl chain.
     52     void RemoveDecl(NamedDecl *D);
     53 
     54     /// \brief Insert the given declaration at the given position in the list.
     55     void InsertDecl(DeclsTy::iterator Pos, NamedDecl *D) {
     56       Decls.insert(Pos, D);
     57     }
     58 
     59   private:
     60     DeclsTy Decls;
     61   };
     62 
     63 public:
     64 
     65   /// iterator - Iterate over the decls of a specified declaration name.
     66   /// It will walk or not the parent declaration contexts depending on how
     67   /// it was instantiated.
     68   class iterator {
     69   public:
     70     typedef NamedDecl *             value_type;
     71     typedef NamedDecl *             reference;
     72     typedef NamedDecl *             pointer;
     73     typedef std::input_iterator_tag iterator_category;
     74     typedef std::ptrdiff_t          difference_type;
     75 
     76     /// Ptr - There are 3 forms that 'Ptr' represents:
     77     /// 1) A single NamedDecl. (Ptr & 0x1 == 0)
     78     /// 2) A IdDeclInfo::DeclsTy::iterator that traverses only the decls of the
     79     ///    same declaration context. (Ptr & 0x3 == 0x1)
     80     /// 3) A IdDeclInfo::DeclsTy::iterator that traverses the decls of parent
     81     ///    declaration contexts too. (Ptr & 0x3 == 0x3)
     82     uintptr_t Ptr;
     83     typedef IdDeclInfo::DeclsTy::iterator BaseIter;
     84 
     85     /// A single NamedDecl. (Ptr & 0x1 == 0)
     86     iterator(NamedDecl *D) {
     87       Ptr = reinterpret_cast<uintptr_t>(D);
     88       assert((Ptr & 0x1) == 0 && "Invalid Ptr!");
     89     }
     90     /// A IdDeclInfo::DeclsTy::iterator that walks or not the parent declaration
     91     /// contexts depending on 'LookInParentCtx'.
     92     iterator(BaseIter I) {
     93       Ptr = reinterpret_cast<uintptr_t>(I) | 0x1;
     94     }
     95 
     96     bool isIterator() const { return (Ptr & 0x1); }
     97 
     98     BaseIter getIterator() const {
     99       assert(isIterator() && "Ptr not an iterator!");
    100       return reinterpret_cast<BaseIter>(Ptr & ~0x3);
    101     }
    102 
    103     friend class IdentifierResolver;
    104 
    105     void incrementSlowCase();
    106   public:
    107     iterator() : Ptr(0) {}
    108 
    109     NamedDecl *operator*() const {
    110       if (isIterator())
    111         return *getIterator();
    112       else
    113         return reinterpret_cast<NamedDecl*>(Ptr);
    114     }
    115 
    116     bool operator==(const iterator &RHS) const {
    117       return Ptr == RHS.Ptr;
    118     }
    119     bool operator!=(const iterator &RHS) const {
    120       return Ptr != RHS.Ptr;
    121     }
    122 
    123     // Preincrement.
    124     iterator& operator++() {
    125       if (!isIterator()) // common case.
    126         Ptr = 0;
    127       else
    128         incrementSlowCase();
    129       return *this;
    130     }
    131 
    132     uintptr_t getAsOpaqueValue() const { return Ptr; }
    133 
    134     static iterator getFromOpaqueValue(uintptr_t P) {
    135       iterator Result;
    136       Result.Ptr = P;
    137       return Result;
    138     }
    139   };
    140 
    141   /// begin - Returns an iterator for decls with the name 'Name'.
    142   iterator begin(DeclarationName Name);
    143 
    144   /// end - Returns an iterator that has 'finished'.
    145   iterator end() {
    146     return iterator();
    147   }
    148 
    149   /// isDeclInScope - If 'Ctx' is a function/method, isDeclInScope returns true
    150   /// if 'D' is in Scope 'S', otherwise 'S' is ignored and isDeclInScope returns
    151   /// true if 'D' belongs to the given declaration context.
    152   ///
    153   /// \param AllowInlineNamespace If \c true, we are checking whether a prior
    154   ///        declaration is in scope in a declaration that requires a prior
    155   ///        declaration (because it is either explicitly qualified or is a
    156   ///        template instantiation or specialization). In this case, a
    157   ///        declaration is in scope if it's in the inline namespace set of the
    158   ///        context.
    159   bool isDeclInScope(Decl *D, DeclContext *Ctx, Scope *S = nullptr,
    160                      bool AllowInlineNamespace = false) const;
    161 
    162   /// AddDecl - Link the decl to its shadowed decl chain.
    163   void AddDecl(NamedDecl *D);
    164 
    165   /// RemoveDecl - Unlink the decl from its shadowed decl chain.
    166   /// The decl must already be part of the decl chain.
    167   void RemoveDecl(NamedDecl *D);
    168 
    169   /// \brief Insert the given declaration after the given iterator
    170   /// position.
    171   void InsertDeclAfter(iterator Pos, NamedDecl *D);
    172 
    173   /// \brief Try to add the given declaration to the top level scope, if it
    174   /// (or a redeclaration of it) hasn't already been added.
    175   ///
    176   /// \param D The externally-produced declaration to add.
    177   ///
    178   /// \param Name The name of the externally-produced declaration.
    179   ///
    180   /// \returns true if the declaration was added, false otherwise.
    181   bool tryAddTopLevelDecl(NamedDecl *D, DeclarationName Name);
    182 
    183   explicit IdentifierResolver(Preprocessor &PP);
    184   ~IdentifierResolver();
    185 
    186 private:
    187   const LangOptions &LangOpt;
    188   Preprocessor &PP;
    189 
    190   class IdDeclInfoMap;
    191   IdDeclInfoMap *IdDeclInfos;
    192 
    193   void updatingIdentifier(IdentifierInfo &II);
    194   void readingIdentifier(IdentifierInfo &II);
    195 
    196   /// FETokenInfo contains a Decl pointer if lower bit == 0.
    197   static inline bool isDeclPtr(void *Ptr) {
    198     return (reinterpret_cast<uintptr_t>(Ptr) & 0x1) == 0;
    199   }
    200 
    201   /// FETokenInfo contains a IdDeclInfo pointer if lower bit == 1.
    202   static inline IdDeclInfo *toIdDeclInfo(void *Ptr) {
    203     assert((reinterpret_cast<uintptr_t>(Ptr) & 0x1) == 1
    204           && "Ptr not a IdDeclInfo* !");
    205     return reinterpret_cast<IdDeclInfo*>(
    206                     reinterpret_cast<uintptr_t>(Ptr) & ~0x1
    207                                                             );
    208   }
    209 };
    210 
    211 } // end namespace clang
    212 
    213 #endif
    214