clang API Documentation

Entity.h
Go to the documentation of this file.
00001 //===--- Entity.h - Cross-translation-unit "token" for decls ----*- C++ -*-===//
00002 //
00003 //                     The LLVM Compiler Infrastructure
00004 //
00005 // This file is distributed under the University of Illinois Open Source
00006 // License. See LICENSE.TXT for details.
00007 //
00008 //===----------------------------------------------------------------------===//
00009 //
00010 //  Entity is a ASTContext-independent way to refer to declarations that are
00011 //  visible across translation units.
00012 //
00013 //===----------------------------------------------------------------------===//
00014 
00015 #ifndef LLVM_CLANG_INDEX_ENTITY_H
00016 #define LLVM_CLANG_INDEX_ENTITY_H
00017 
00018 #include "clang/Basic/LLVM.h"
00019 #include "llvm/ADT/PointerUnion.h"
00020 #include "llvm/ADT/DenseMap.h"
00021 #include "llvm/ADT/StringRef.h"
00022 #include <string>
00023 
00024 namespace clang {
00025   class ASTContext;
00026   class Decl;
00027 
00028 namespace idx {
00029   class Program;
00030   class EntityImpl;
00031 
00032 /// \brief A ASTContext-independent way to refer to declarations.
00033 ///
00034 /// Entity is basically the link for declarations that are semantically the same
00035 /// in multiple ASTContexts. A client will convert a Decl into an Entity and
00036 /// later use that Entity to find the "same" Decl into another ASTContext.
00037 /// Declarations that are semantically the same and visible across translation
00038 /// units will be associated with the same Entity.
00039 ///
00040 /// An Entity may also refer to declarations that cannot be visible across
00041 /// translation units, e.g. static functions with the same name in multiple
00042 /// translation units will be associated with different Entities.
00043 ///
00044 /// Entities can be checked for equality but note that the same Program object
00045 /// should be used when getting Entities.
00046 ///
00047 class Entity {
00048   /// \brief Stores the Decl directly if it is not visible outside of its own
00049   /// translation unit, otherwise it stores the associated EntityImpl.
00050   llvm::PointerUnion<Decl *, EntityImpl *> Val;
00051 
00052   explicit Entity(Decl *D);
00053   explicit Entity(EntityImpl *impl) : Val(impl) { }
00054   friend class EntityGetter;
00055 
00056 public:
00057   Entity() { }
00058 
00059   /// \brief Find the Decl that can be referred to by this entity.
00060   Decl *getDecl(ASTContext &AST) const;
00061 
00062   /// \brief If this Entity represents a declaration that is internal to its
00063   /// translation unit, getInternalDecl() returns it.
00064   Decl *getInternalDecl() const {
00065     assert(isInternalToTU() && "This Entity is not internal!");
00066     return Val.get<Decl *>();
00067   }
00068 
00069   /// \brief Get a printable name for debugging purpose.
00070   std::string getPrintableName() const;
00071 
00072   /// \brief Get an Entity associated with the given Decl.
00073   /// \returns invalid Entity if an Entity cannot refer to this Decl.
00074   static Entity get(Decl *D, Program &Prog);
00075 
00076   /// \brief Get an Entity associated with a name in the global namespace.
00077   static Entity get(StringRef Name, Program &Prog);
00078 
00079   /// \brief true if the Entity is not visible outside the trasnlation unit.
00080   bool isInternalToTU() const {
00081     assert(isValid() && "This Entity is not valid!");
00082     return Val.is<Decl *>();
00083   }
00084 
00085   bool isValid() const { return !Val.isNull(); }
00086   bool isInvalid() const { return !isValid(); }
00087 
00088   void *getAsOpaquePtr() const { return Val.getOpaqueValue(); }
00089   static Entity getFromOpaquePtr(void *Ptr) {
00090     Entity Ent;
00091     Ent.Val = llvm::PointerUnion<Decl *, EntityImpl *>::getFromOpaqueValue(Ptr);
00092     return Ent;
00093   }
00094 
00095   friend bool operator==(const Entity &LHS, const Entity &RHS) {
00096     return LHS.getAsOpaquePtr() == RHS.getAsOpaquePtr();
00097   }
00098 
00099   // For use in a std::map.
00100   friend bool operator < (const Entity &LHS, const Entity &RHS) {
00101     return LHS.getAsOpaquePtr() < RHS.getAsOpaquePtr();
00102   }
00103 
00104   // For use in DenseMap/DenseSet.
00105   static Entity getEmptyMarker() {
00106     Entity Ent;
00107     Ent.Val =
00108       llvm::PointerUnion<Decl *, EntityImpl *>::getFromOpaqueValue((void*)-1);
00109     return Ent;
00110   }
00111   static Entity getTombstoneMarker() {
00112     Entity Ent;
00113     Ent.Val =
00114       llvm::PointerUnion<Decl *, EntityImpl *>::getFromOpaqueValue((void*)-2);
00115     return Ent;
00116   }
00117 };
00118 
00119 } // namespace idx
00120 
00121 } // namespace clang
00122 
00123 namespace llvm {
00124 /// Define DenseMapInfo so that Entities can be used as keys in DenseMap and
00125 /// DenseSets.
00126 template<>
00127 struct DenseMapInfo<clang::idx::Entity> {
00128   static inline clang::idx::Entity getEmptyKey() {
00129     return clang::idx::Entity::getEmptyMarker();
00130   }
00131 
00132   static inline clang::idx::Entity getTombstoneKey() {
00133     return clang::idx::Entity::getTombstoneMarker();
00134   }
00135 
00136   static unsigned getHashValue(clang::idx::Entity);
00137 
00138   static inline bool
00139   isEqual(clang::idx::Entity LHS, clang::idx::Entity RHS) {
00140     return LHS == RHS;
00141   }
00142 };
00143   
00144 template <>
00145 struct isPodLike<clang::idx::Entity> { static const bool value = true; };
00146 
00147 }  // end namespace llvm
00148 
00149 #endif