1 //===--- Entity.h - Cross-translation-unit "token" for decls ----*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // Entity is a ASTContext-independent way to refer to declarations that are
11 // visible across translation units.
13 //===----------------------------------------------------------------------===//
15 #ifndef LLVM_CLANG_INDEX_ENTITY_H
16 #define LLVM_CLANG_INDEX_ENTITY_H
18 #include "llvm/ADT/PointerUnion.h"
19 #include "llvm/ADT/DenseMap.h"
20 #include "llvm/ADT/StringRef.h"
31 /// \brief A ASTContext-independent way to refer to declarations.
33 /// Entity is basically the link for declarations that are semantically the same
34 /// in multiple ASTContexts. A client will convert a Decl into an Entity and
35 /// later use that Entity to find the "same" Decl into another ASTContext.
36 /// Declarations that are semantically the same and visible across translation
37 /// units will be associated with the same Entity.
39 /// An Entity may also refer to declarations that cannot be visible across
40 /// translation units, e.g. static functions with the same name in multiple
41 /// translation units will be associated with different Entities.
43 /// Entities can be checked for equality but note that the same Program object
44 /// should be used when getting Entities.
47 /// \brief Stores the Decl directly if it is not visible outside of its own
48 /// translation unit, otherwise it stores the associated EntityImpl.
49 llvm::PointerUnion<Decl *, EntityImpl *> Val;
51 explicit Entity(Decl *D);
52 explicit Entity(EntityImpl *impl) : Val(impl) { }
53 friend class EntityGetter;
58 /// \brief Find the Decl that can be referred to by this entity.
59 Decl *getDecl(ASTContext &AST) const;
61 /// \brief If this Entity represents a declaration that is internal to its
62 /// translation unit, getInternalDecl() returns it.
63 Decl *getInternalDecl() const {
64 assert(isInternalToTU() && "This Entity is not internal!");
65 return Val.get<Decl *>();
68 /// \brief Get a printable name for debugging purpose.
69 std::string getPrintableName() const;
71 /// \brief Get an Entity associated with the given Decl.
72 /// \returns invalid Entity if an Entity cannot refer to this Decl.
73 static Entity get(Decl *D, Program &Prog);
75 /// \brief Get an Entity associated with a name in the global namespace.
76 static Entity get(llvm::StringRef Name, Program &Prog);
78 /// \brief true if the Entity is not visible outside the trasnlation unit.
79 bool isInternalToTU() const {
80 assert(isValid() && "This Entity is not valid!");
81 return Val.is<Decl *>();
84 bool isValid() const { return !Val.isNull(); }
85 bool isInvalid() const { return !isValid(); }
87 void *getAsOpaquePtr() const { return Val.getOpaqueValue(); }
88 static Entity getFromOpaquePtr(void *Ptr) {
90 Ent.Val = llvm::PointerUnion<Decl *, EntityImpl *>::getFromOpaqueValue(Ptr);
94 friend bool operator==(const Entity &LHS, const Entity &RHS) {
95 return LHS.getAsOpaquePtr() == RHS.getAsOpaquePtr();
98 // For use in a std::map.
99 friend bool operator < (const Entity &LHS, const Entity &RHS) {
100 return LHS.getAsOpaquePtr() < RHS.getAsOpaquePtr();
103 // For use in DenseMap/DenseSet.
104 static Entity getEmptyMarker() {
107 llvm::PointerUnion<Decl *, EntityImpl *>::getFromOpaqueValue((void*)-1);
110 static Entity getTombstoneMarker() {
113 llvm::PointerUnion<Decl *, EntityImpl *>::getFromOpaqueValue((void*)-2);
123 /// Define DenseMapInfo so that Entities can be used as keys in DenseMap and
126 struct DenseMapInfo<clang::idx::Entity> {
127 static inline clang::idx::Entity getEmptyKey() {
128 return clang::idx::Entity::getEmptyMarker();
131 static inline clang::idx::Entity getTombstoneKey() {
132 return clang::idx::Entity::getTombstoneMarker();
135 static unsigned getHashValue(clang::idx::Entity);
138 isEqual(clang::idx::Entity LHS, clang::idx::Entity RHS) {
144 struct isPodLike<clang::idx::Entity> { static const bool value = true; };
146 } // end namespace llvm