diff options
Diffstat (limited to 'include/clang/Index')
-rw-r--r-- | include/clang/Index/ASTLocation.h | 174 | ||||
-rw-r--r-- | include/clang/Index/Analyzer.h | 56 | ||||
-rw-r--r-- | include/clang/Index/DeclReferenceMap.h | 50 | ||||
-rw-r--r-- | include/clang/Index/Entity.h | 143 | ||||
-rw-r--r-- | include/clang/Index/GlobalSelector.h | 99 | ||||
-rw-r--r-- | include/clang/Index/Handlers.h | 81 | ||||
-rw-r--r-- | include/clang/Index/IndexProvider.h | 38 | ||||
-rw-r--r-- | include/clang/Index/Indexer.h | 75 | ||||
-rw-r--r-- | include/clang/Index/Program.h | 45 | ||||
-rw-r--r-- | include/clang/Index/STLExtras.h | 63 | ||||
-rw-r--r-- | include/clang/Index/SelectorMap.h | 57 | ||||
-rw-r--r-- | include/clang/Index/TranslationUnit.h | 37 | ||||
-rw-r--r-- | include/clang/Index/Utils.h | 35 |
13 files changed, 953 insertions, 0 deletions
diff --git a/include/clang/Index/ASTLocation.h b/include/clang/Index/ASTLocation.h new file mode 100644 index 0000000000000..9620ec5dbd4fd --- /dev/null +++ b/include/clang/Index/ASTLocation.h @@ -0,0 +1,174 @@ +//===--- ASTLocation.h - A <Decl, Stmt> pair --------------------*- C++ -*-===// +// +// The LLVM Compiler Infrastructure +// +// This file is distributed under the University of Illinois Open Source +// License. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// +// +// ASTLocation is Decl or a Stmt and its immediate Decl parent. +// +//===----------------------------------------------------------------------===// + +#ifndef LLVM_CLANG_INDEX_ASTLOCATION_H +#define LLVM_CLANG_INDEX_ASTLOCATION_H + +#include "clang/AST/TypeLoc.h" +#include "llvm/ADT/PointerIntPair.h" + +namespace llvm { + class raw_ostream; +} + +namespace clang { + class Decl; + class Stmt; + class NamedDecl; + +namespace idx { + class TranslationUnit; + +/// \brief Represents a Decl or a Stmt and its immediate Decl parent. It's +/// immutable. +/// +/// ASTLocation is intended to be used as a "pointer" into the AST. It is either +/// just a Decl, or a Stmt and its Decl parent. Since a single Stmt is devoid +/// of context, its parent Decl provides all the additional missing information +/// like the declaration context, ASTContext, etc. +/// +class ASTLocation { +public: + enum NodeKind { + N_Decl, N_NamedRef, N_Stmt, N_Type + }; + + struct NamedRef { + NamedDecl *ND; + SourceLocation Loc; + + NamedRef() : ND(0) { } + NamedRef(NamedDecl *nd, SourceLocation loc) : ND(nd), Loc(loc) { } + }; + +private: + llvm::PointerIntPair<Decl *, 2, NodeKind> ParentDecl; + + union { + Decl *D; + Stmt *Stm; + struct { + NamedDecl *ND; + unsigned RawLoc; + } NDRef; + struct { + void *TyPtr; + void *Data; + } Ty; + }; + +public: + ASTLocation() { } + + explicit ASTLocation(const Decl *d) + : ParentDecl(const_cast<Decl*>(d), N_Decl), D(const_cast<Decl*>(d)) { } + + ASTLocation(const Decl *parentDecl, const Stmt *stm) + : ParentDecl(const_cast<Decl*>(parentDecl), N_Stmt), + Stm(const_cast<Stmt*>(stm)) { + if (!stm) ParentDecl.setPointer(0); + } + + ASTLocation(const Decl *parentDecl, NamedDecl *ndRef, SourceLocation loc) + : ParentDecl(const_cast<Decl*>(parentDecl), N_NamedRef) { + if (ndRef) { + NDRef.ND = ndRef; + NDRef.RawLoc = loc.getRawEncoding(); + } else + ParentDecl.setPointer(0); + } + + ASTLocation(const Decl *parentDecl, TypeLoc tyLoc) + : ParentDecl(const_cast<Decl*>(parentDecl), N_Type) { + if (tyLoc) { + Ty.TyPtr = tyLoc.getSourceType().getAsOpaquePtr(); + Ty.Data = tyLoc.getOpaqueData(); + } else + ParentDecl.setPointer(0); + } + + bool isValid() const { return ParentDecl.getPointer() != 0; } + bool isInvalid() const { return !isValid(); } + + NodeKind getKind() const { + assert(isValid()); + return (NodeKind)ParentDecl.getInt(); + } + + Decl *getParentDecl() const { return ParentDecl.getPointer(); } + + Decl *AsDecl() const { + assert(getKind() == N_Decl); + return D; + } + Stmt *AsStmt() const { + assert(getKind() == N_Stmt); + return Stm; + } + NamedRef AsNamedRef() const { + assert(getKind() == N_NamedRef); + return NamedRef(NDRef.ND, SourceLocation::getFromRawEncoding(NDRef.RawLoc)); + } + TypeLoc AsTypeLoc() const { + assert(getKind() == N_Type); + return TypeLoc(QualType::getFromOpaquePtr(Ty.TyPtr), Ty.Data); + } + + Decl *dyn_AsDecl() const { return getKind() == N_Decl ? D : 0; } + Stmt *dyn_AsStmt() const { return getKind() == N_Stmt ? Stm : 0; } + NamedRef dyn_AsNamedRef() const { + return getKind() == N_Type ? AsNamedRef() : NamedRef(); + } + TypeLoc dyn_AsTypeLoc() const { + return getKind() == N_Type ? AsTypeLoc() : TypeLoc(); + } + + bool isDecl() const { return isValid() && getKind() == N_Decl; } + bool isStmt() const { return isValid() && getKind() == N_Stmt; } + bool isNamedRef() const { return isValid() && getKind() == N_NamedRef; } + bool isType() const { return isValid() && getKind() == N_Type; } + + /// \brief Returns the declaration that this ASTLocation references. + /// + /// If this points to a Decl, that Decl is returned. + /// If this points to an Expr that references a Decl, that Decl is returned, + /// otherwise it returns NULL. + Decl *getReferencedDecl(); + const Decl *getReferencedDecl() const { + return const_cast<ASTLocation*>(this)->getReferencedDecl(); + } + + SourceRange getSourceRange() const; + + void print(llvm::raw_ostream &OS) const; +}; + +/// \brief Like ASTLocation but also contains the TranslationUnit that the +/// ASTLocation originated from. +class TULocation : public ASTLocation { + TranslationUnit *TU; + +public: + TULocation(TranslationUnit *tu, ASTLocation astLoc) + : ASTLocation(astLoc), TU(tu) { + assert(tu && "Passed null translation unit"); + } + + TranslationUnit *getTU() const { return TU; } +}; + +} // namespace idx + +} // namespace clang + +#endif diff --git a/include/clang/Index/Analyzer.h b/include/clang/Index/Analyzer.h new file mode 100644 index 0000000000000..f6b5465148e68 --- /dev/null +++ b/include/clang/Index/Analyzer.h @@ -0,0 +1,56 @@ +//===--- Analyzer.h - Analysis for indexing information ---------*- C++ -*-===// +// +// The LLVM Compiler Infrastructure +// +// This file is distributed under the University of Illinois Open Source +// License. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// +// +// This file declares the Analyzer interface. +// +//===----------------------------------------------------------------------===// + +#ifndef LLVM_CLANG_INDEX_ANALYZER_H +#define LLVM_CLANG_INDEX_ANALYZER_H + +namespace clang { + class Decl; + class ObjCMessageExpr; + +namespace idx { + class Program; + class IndexProvider; + class TULocationHandler; + +/// \brief Provides indexing information, like finding all references of an +/// Entity across translation units. +class Analyzer { + Program &Prog; + IndexProvider &Idxer; + + Analyzer(const Analyzer&); // do not implement + Analyzer &operator=(const Analyzer &); // do not implement + +public: + explicit Analyzer(Program &prog, IndexProvider &idxer) + : Prog(prog), Idxer(idxer) { } + + /// \brief Find all TULocations for declarations of the given Decl and pass + /// them to Handler. + void FindDeclarations(Decl *D, TULocationHandler &Handler); + + /// \brief Find all TULocations for references of the given Decl and pass + /// them to Handler. + void FindReferences(Decl *D, TULocationHandler &Handler); + + /// \brief Find methods that may respond to the given message and pass them + /// to Handler. + void FindObjCMethods(ObjCMessageExpr *MsgE, TULocationHandler &Handler); +}; + +} // namespace idx + +} // namespace clang + +#endif diff --git a/include/clang/Index/DeclReferenceMap.h b/include/clang/Index/DeclReferenceMap.h new file mode 100644 index 0000000000000..73f2fe50b3b64 --- /dev/null +++ b/include/clang/Index/DeclReferenceMap.h @@ -0,0 +1,50 @@ +//===--- DeclReferenceMap.h - Map Decls to their references -----*- C++ -*-===// +// +// The LLVM Compiler Infrastructure +// +// This file is distributed under the University of Illinois Open Source +// License. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// +// +// DeclReferenceMap creates a mapping from Decls to the ASTLocations that +// reference them. +// +//===----------------------------------------------------------------------===// + +#ifndef LLVM_CLANG_INDEX_DECLREFERENCEMAP_H +#define LLVM_CLANG_INDEX_DECLREFERENCEMAP_H + +#include "clang/Index/ASTLocation.h" +#include "clang/Index/STLExtras.h" +#include <map> + +namespace clang { + class ASTContext; + class NamedDecl; + +namespace idx { + +/// \brief Maps NamedDecls with the ASTLocations that reference them. +/// +/// References are mapped and retrieved using the canonical decls. +class DeclReferenceMap { +public: + explicit DeclReferenceMap(ASTContext &Ctx); + + typedef std::multimap<NamedDecl*, ASTLocation> MapTy; + typedef pair_value_iterator<MapTy::iterator> astlocation_iterator; + + astlocation_iterator refs_begin(NamedDecl *D) const; + astlocation_iterator refs_end(NamedDecl *D) const; + bool refs_empty(NamedDecl *D) const; + +private: + mutable MapTy Map; +}; + +} // end idx namespace + +} // end clang namespace + +#endif diff --git a/include/clang/Index/Entity.h b/include/clang/Index/Entity.h new file mode 100644 index 0000000000000..4533a1a0ac082 --- /dev/null +++ b/include/clang/Index/Entity.h @@ -0,0 +1,143 @@ +//===--- Entity.h - Cross-translation-unit "token" for decls ----*- C++ -*-===// +// +// The LLVM Compiler Infrastructure +// +// This file is distributed under the University of Illinois Open Source +// License. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// +// +// Entity is a ASTContext-independent way to refer to declarations that are +// visible across translation units. +// +//===----------------------------------------------------------------------===// + +#ifndef LLVM_CLANG_INDEX_ENTITY_H +#define LLVM_CLANG_INDEX_ENTITY_H + +#include "llvm/ADT/PointerUnion.h" +#include "llvm/ADT/DenseMap.h" +#include <string> + +namespace clang { + class ASTContext; + class Decl; + +namespace idx { + class Program; + class EntityImpl; + +/// \brief A ASTContext-independent way to refer to declarations. +/// +/// Entity is basically the link for declarations that are semantically the same +/// in multiple ASTContexts. A client will convert a Decl into an Entity and +/// later use that Entity to find the "same" Decl into another ASTContext. +/// Declarations that are semantically the same and visible across translation +/// units will be associated with the same Entity. +/// +/// An Entity may also refer to declarations that cannot be visible across +/// translation units, e.g. static functions with the same name in multiple +/// translation units will be associated with different Entities. +/// +/// Entities can be checked for equality but note that the same Program object +/// should be used when getting Entities. +/// +class Entity { + /// \brief Stores the Decl directly if it is not visible outside of its own + /// translation unit, otherwise it stores the associated EntityImpl. + llvm::PointerUnion<Decl *, EntityImpl *> Val; + + explicit Entity(Decl *D); + explicit Entity(EntityImpl *impl) : Val(impl) { } + friend class EntityGetter; + +public: + Entity() { } + + /// \brief Find the Decl that can be referred to by this entity. + Decl *getDecl(ASTContext &AST) const; + + /// \brief If this Entity represents a declaration that is internal to its + /// translation unit, getInternalDecl() returns it. + Decl *getInternalDecl() const { + assert(isInternalToTU() && "This Entity is not internal!"); + return Val.get<Decl *>(); + } + + /// \brief Get a printable name for debugging purpose. + std::string getPrintableName() const; + + /// \brief Get an Entity associated with the given Decl. + /// \returns invalid Entity if an Entity cannot refer to this Decl. + static Entity get(Decl *D, Program &Prog); + + /// \brief true if the Entity is not visible outside the trasnlation unit. + bool isInternalToTU() const { + assert(isValid() && "This Entity is not valid!"); + return Val.is<Decl *>(); + } + + bool isValid() const { return !Val.isNull(); } + bool isInvalid() const { return !isValid(); } + + void *getAsOpaquePtr() const { return Val.getOpaqueValue(); } + static Entity getFromOpaquePtr(void *Ptr) { + Entity Ent; + Ent.Val = llvm::PointerUnion<Decl *, EntityImpl *>::getFromOpaqueValue(Ptr); + return Ent; + } + + friend bool operator==(const Entity &LHS, const Entity &RHS) { + return LHS.getAsOpaquePtr() == RHS.getAsOpaquePtr(); + } + + // For use in a std::map. + friend bool operator < (const Entity &LHS, const Entity &RHS) { + return LHS.getAsOpaquePtr() < RHS.getAsOpaquePtr(); + } + + // For use in DenseMap/DenseSet. + static Entity getEmptyMarker() { + Entity Ent; + Ent.Val = + llvm::PointerUnion<Decl *, EntityImpl *>::getFromOpaqueValue((void*)-1); + return Ent; + } + static Entity getTombstoneMarker() { + Entity Ent; + Ent.Val = + llvm::PointerUnion<Decl *, EntityImpl *>::getFromOpaqueValue((void*)-2); + return Ent; + } +}; + +} // namespace idx + +} // namespace clang + +namespace llvm { +/// Define DenseMapInfo so that Entities can be used as keys in DenseMap and +/// DenseSets. +template<> +struct DenseMapInfo<clang::idx::Entity> { + static inline clang::idx::Entity getEmptyKey() { + return clang::idx::Entity::getEmptyMarker(); + } + + static inline clang::idx::Entity getTombstoneKey() { + return clang::idx::Entity::getTombstoneMarker(); + } + + static unsigned getHashValue(clang::idx::Entity); + + static inline bool + isEqual(clang::idx::Entity LHS, clang::idx::Entity RHS) { + return LHS == RHS; + } + + static inline bool isPod() { return true; } +}; + +} // end namespace llvm + +#endif diff --git a/include/clang/Index/GlobalSelector.h b/include/clang/Index/GlobalSelector.h new file mode 100644 index 0000000000000..51f98267f3562 --- /dev/null +++ b/include/clang/Index/GlobalSelector.h @@ -0,0 +1,99 @@ +//===--- GlobalSelector.h - Cross-translation-unit "token" for selectors --===// +// +// The LLVM Compiler Infrastructure +// +// This file is distributed under the University of Illinois Open Source +// License. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// +// +// GlobalSelector is a ASTContext-independent way to refer to selectors. +// +//===----------------------------------------------------------------------===// + +#ifndef LLVM_CLANG_INDEX_GLOBALSELECTOR_H +#define LLVM_CLANG_INDEX_GLOBALSELECTOR_H + +#include "llvm/ADT/DenseMap.h" +#include <string> + +namespace clang { + class ASTContext; + class Selector; + +namespace idx { + class Program; + +/// \brief A ASTContext-independent way to refer to selectors. +class GlobalSelector { + void *Val; + + explicit GlobalSelector(void *val) : Val(val) { } + +public: + GlobalSelector() : Val(0) { } + + /// \brief Get the ASTContext-specific selector. + Selector getSelector(ASTContext &AST) const; + + bool isValid() const { return Val != 0; } + bool isInvalid() const { return !isValid(); } + + /// \brief Get a printable name for debugging purpose. + std::string getPrintableName() const; + + /// \brief Get a GlobalSelector for the ASTContext-specific selector. + static GlobalSelector get(Selector Sel, Program &Prog); + + void *getAsOpaquePtr() const { return Val; } + + static GlobalSelector getFromOpaquePtr(void *Ptr) { + return GlobalSelector(Ptr); + } + + friend bool operator==(const GlobalSelector &LHS, const GlobalSelector &RHS) { + return LHS.getAsOpaquePtr() == RHS.getAsOpaquePtr(); + } + + // For use in a std::map. + friend bool operator< (const GlobalSelector &LHS, const GlobalSelector &RHS) { + return LHS.getAsOpaquePtr() < RHS.getAsOpaquePtr(); + } + + // For use in DenseMap/DenseSet. + static GlobalSelector getEmptyMarker() { return GlobalSelector((void*)-1); } + static GlobalSelector getTombstoneMarker() { + return GlobalSelector((void*)-2); + } +}; + +} // namespace idx + +} // namespace clang + +namespace llvm { +/// Define DenseMapInfo so that GlobalSelectors can be used as keys in DenseMap +/// and DenseSets. +template<> +struct DenseMapInfo<clang::idx::GlobalSelector> { + static inline clang::idx::GlobalSelector getEmptyKey() { + return clang::idx::GlobalSelector::getEmptyMarker(); + } + + static inline clang::idx::GlobalSelector getTombstoneKey() { + return clang::idx::GlobalSelector::getTombstoneMarker(); + } + + static unsigned getHashValue(clang::idx::GlobalSelector); + + static inline bool + isEqual(clang::idx::GlobalSelector LHS, clang::idx::GlobalSelector RHS) { + return LHS == RHS; + } + + static inline bool isPod() { return true; } +}; + +} // end namespace llvm + +#endif diff --git a/include/clang/Index/Handlers.h b/include/clang/Index/Handlers.h new file mode 100644 index 0000000000000..655aef901cd16 --- /dev/null +++ b/include/clang/Index/Handlers.h @@ -0,0 +1,81 @@ +//===--- Handlers.h - Interfaces for receiving information ------*- C++ -*-===// +// +// The LLVM Compiler Infrastructure +// +// This file is distributed under the University of Illinois Open Source +// License. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// +// +// Abstract interfaces for receiving information. +// +//===----------------------------------------------------------------------===// + +#ifndef LLVM_CLANG_INDEX_HANDLERS_H +#define LLVM_CLANG_INDEX_HANDLERS_H + +#include "llvm/ADT/SmallVector.h" + +namespace clang { + +namespace idx { + class Entity; + class TranslationUnit; + class TULocation; + +/// \brief Abstract interface for receiving Entities. +class EntityHandler { +public: + typedef Entity receiving_type; + + virtual ~EntityHandler(); + virtual void Handle(Entity Ent) = 0; +}; + +/// \brief Abstract interface for receiving TranslationUnits. +class TranslationUnitHandler { +public: + typedef TranslationUnit* receiving_type; + + virtual ~TranslationUnitHandler(); + virtual void Handle(TranslationUnit *TU) = 0; +}; + +/// \brief Abstract interface for receiving TULocations. +class TULocationHandler { +public: + typedef TULocation receiving_type; + + virtual ~TULocationHandler(); + virtual void Handle(TULocation TULoc) = 0; +}; + +/// \brief Helper for the Handler classes. Stores the objects into a vector. +/// example: +/// @code +/// Storing<TranslationUnitHandler> TURes; +/// IndexProvider.GetTranslationUnitsFor(Entity, TURes); +/// for (Storing<TranslationUnitHandler>::iterator +/// I = TURes.begin(), E = TURes.end(); I != E; ++I) { .... +/// @endcode +template <typename handler_type> +class Storing : public handler_type { + typedef typename handler_type::receiving_type receiving_type; + typedef llvm::SmallVector<receiving_type, 8> StoreTy; + StoreTy Store; + +public: + virtual void Handle(receiving_type Obj) { + Store.push_back(Obj); + } + + typedef typename StoreTy::const_iterator iterator; + iterator begin() const { return Store.begin(); } + iterator end() const { return Store.end(); } +}; + +} // namespace idx + +} // namespace clang + +#endif diff --git a/include/clang/Index/IndexProvider.h b/include/clang/Index/IndexProvider.h new file mode 100644 index 0000000000000..187dd9393cbb9 --- /dev/null +++ b/include/clang/Index/IndexProvider.h @@ -0,0 +1,38 @@ +//===--- IndexProvider.h - Maps information to translation units -*- C++ -*-==// +// +// The LLVM Compiler Infrastructure +// +// This file is distributed under the University of Illinois Open Source +// License. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// +// +// Maps information to TranslationUnits. +// +//===----------------------------------------------------------------------===// + +#ifndef LLVM_CLANG_INDEX_INDEXPROVIDER_H +#define LLVM_CLANG_INDEX_INDEXPROVIDER_H + +namespace clang { + +namespace idx { + class Entity; + class TranslationUnitHandler; + class GlobalSelector; + +/// \brief Maps information to TranslationUnits. +class IndexProvider { +public: + virtual ~IndexProvider(); + virtual void GetTranslationUnitsFor(Entity Ent, + TranslationUnitHandler &Handler) = 0; + virtual void GetTranslationUnitsFor(GlobalSelector Sel, + TranslationUnitHandler &Handler) = 0; +}; + +} // namespace idx + +} // namespace clang + +#endif diff --git a/include/clang/Index/Indexer.h b/include/clang/Index/Indexer.h new file mode 100644 index 0000000000000..8b1d2dd38bff8 --- /dev/null +++ b/include/clang/Index/Indexer.h @@ -0,0 +1,75 @@ +//===--- Indexer.h - IndexProvider implementation ---------------*- C++ -*-===// +// +// The LLVM Compiler Infrastructure +// +// This file is distributed under the University of Illinois Open Source +// License. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// +// +// IndexProvider implementation. +// +//===----------------------------------------------------------------------===// + +#ifndef LLVM_CLANG_INDEX_INDEXER_H +#define LLVM_CLANG_INDEX_INDEXER_H + +#include "clang/Frontend/TextDiagnosticBuffer.h" +#include "clang/Index/IndexProvider.h" +#include "clang/Index/Entity.h" +#include "clang/Index/GlobalSelector.h" +#include "llvm/ADT/SmallPtrSet.h" +#include "llvm/ADT/DenseMap.h" +#include "clang/Basic/FileManager.h" +#include <map> + +namespace clang { + class ASTContext; + +namespace idx { + class Program; + class TranslationUnit; + +/// \brief Maps information to TranslationUnits. +class Indexer : public IndexProvider { +public: + typedef llvm::SmallPtrSet<TranslationUnit *, 4> TUSetTy; + typedef llvm::DenseMap<ASTContext *, TranslationUnit *> CtxTUMapTy; + typedef std::map<Entity, TUSetTy> MapTy; + typedef std::map<GlobalSelector, TUSetTy> SelMapTy; + + explicit Indexer(Program &prog) : + Prog(prog), Diags(&DiagClient) { } + + Program &getProgram() const { return Prog; } + + Diagnostic &getDiagnostics() { return Diags; } + const Diagnostic &getDiagnostics() const { return Diags; } + + FileManager &getFileManager() { return FileMgr; } + const FileManager &getFileManager() const { return FileMgr; } + + /// \brief Find all Entities and map them to the given translation unit. + void IndexAST(TranslationUnit *TU); + + virtual void GetTranslationUnitsFor(Entity Ent, + TranslationUnitHandler &Handler); + virtual void GetTranslationUnitsFor(GlobalSelector Sel, + TranslationUnitHandler &Handler); + +private: + Program &Prog; + TextDiagnosticBuffer DiagClient; + Diagnostic Diags; + FileManager FileMgr; + + MapTy Map; + CtxTUMapTy CtxTUMap; + SelMapTy SelMap; +}; + +} // namespace idx + +} // namespace clang + +#endif diff --git a/include/clang/Index/Program.h b/include/clang/Index/Program.h new file mode 100644 index 0000000000000..8039192512d62 --- /dev/null +++ b/include/clang/Index/Program.h @@ -0,0 +1,45 @@ +//===--- Program.h - Cross-translation unit information ---------*- C++ -*-===// +// +// The LLVM Compiler Infrastructure +// +// This file is distributed under the University of Illinois Open Source +// License. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// +// +// This file declares the idx::Program interface. +// +//===----------------------------------------------------------------------===// + +#ifndef LLVM_CLANG_INDEX_PROGRAM_H +#define LLVM_CLANG_INDEX_PROGRAM_H + +namespace clang { + class ASTContext; + +namespace idx { + class EntityHandler; + +/// \brief Top level object that owns and maintains information +/// that is common across translation units. +class Program { + void *Impl; + + Program(const Program&); // do not implement + Program &operator=(const Program &); // do not implement + friend class Entity; + friend class GlobalSelector; + +public: + Program(); + ~Program(); + + /// \brief Traverses the AST and passes all the entities to the Handler. + void FindEntities(ASTContext &Ctx, EntityHandler &Handler); +}; + +} // namespace idx + +} // namespace clang + +#endif diff --git a/include/clang/Index/STLExtras.h b/include/clang/Index/STLExtras.h new file mode 100644 index 0000000000000..a3693c6c79a62 --- /dev/null +++ b/include/clang/Index/STLExtras.h @@ -0,0 +1,63 @@ +//===--- STLExtras.h - Helper STL related templates -------------*- C++ -*-===// +// +// The LLVM Compiler Infrastructure +// +// This file is distributed under the University of Illinois Open Source +// License. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// +// +// Helper templates for using with the STL. +// +//===----------------------------------------------------------------------===// + +#ifndef LLVM_CLANG_INDEX_STLEXTRAS_H +#define LLVM_CLANG_INDEX_STLEXTRAS_H + +namespace clang { + +namespace idx { + +/// \brief Wraps an iterator whose value_type is a pair, and provides +/// pair's second object as the value. +template <typename iter_type> +class pair_value_iterator { + iter_type I; + +public: + typedef typename iter_type::value_type::second_type value_type; + typedef value_type& reference; + typedef value_type* pointer; + typedef typename iter_type::iterator_category iterator_category; + typedef typename iter_type::difference_type difference_type; + + pair_value_iterator() { } + pair_value_iterator(iter_type i) : I(i) { } + + reference operator*() const { return I->second; } + pointer operator->() const { return &I->second; } + + pair_value_iterator& operator++() { + ++I; + return *this; + } + + pair_value_iterator operator++(int) { + pair_value_iterator tmp(*this); + ++(*this); + return tmp; + } + + friend bool operator==(pair_value_iterator L, pair_value_iterator R) { + return L.I == R.I; + } + friend bool operator!=(pair_value_iterator L, pair_value_iterator R) { + return L.I != R.I; + } +}; + +} // end idx namespace + +} // end clang namespace + +#endif diff --git a/include/clang/Index/SelectorMap.h b/include/clang/Index/SelectorMap.h new file mode 100644 index 0000000000000..be01702fcbdbe --- /dev/null +++ b/include/clang/Index/SelectorMap.h @@ -0,0 +1,57 @@ +//===--- SelectorMap.h - Maps selectors to methods and messages -*- C++ -*-===// +// +// The LLVM Compiler Infrastructure +// +// This file is distributed under the University of Illinois Open Source +// License. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// +// +// SelectorMap creates a mapping from selectors to ObjC method declarations +// and ObjC message expressions. +// +//===----------------------------------------------------------------------===// + +#ifndef LLVM_CLANG_INDEX_SELECTORMAP_H +#define LLVM_CLANG_INDEX_SELECTORMAP_H + +#include "clang/Index/ASTLocation.h" +#include "clang/Index/STLExtras.h" +#include "clang/Basic/IdentifierTable.h" +#include <map> + +namespace clang { + class ASTContext; + class ObjCMethodDecl; + +namespace idx { + +/// \brief Maps NamedDecls with the ASTLocations that reference them. +/// +/// References are mapped and retrieved using the canonical decls. +class SelectorMap { +public: + explicit SelectorMap(ASTContext &Ctx); + + typedef std::multimap<Selector, ObjCMethodDecl *> SelMethMapTy; + typedef std::multimap<Selector, ASTLocation> SelRefMapTy; + + typedef pair_value_iterator<SelMethMapTy::iterator> method_iterator; + typedef pair_value_iterator<SelRefMapTy::iterator> astlocation_iterator; + + method_iterator methods_begin(Selector Sel) const; + method_iterator methods_end(Selector Sel) const; + + astlocation_iterator refs_begin(Selector Sel) const; + astlocation_iterator refs_end(Selector Sel) const; + +private: + mutable SelMethMapTy SelMethMap; + mutable SelRefMapTy SelRefMap; +}; + +} // end idx namespace + +} // end clang namespace + +#endif diff --git a/include/clang/Index/TranslationUnit.h b/include/clang/Index/TranslationUnit.h new file mode 100644 index 0000000000000..bf9e78f72892c --- /dev/null +++ b/include/clang/Index/TranslationUnit.h @@ -0,0 +1,37 @@ +//===--- TranslationUnit.h - Interface for a translation unit ---*- C++ -*-===// +// +// The LLVM Compiler Infrastructure +// +// This file is distributed under the University of Illinois Open Source +// License. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// +// +// Abstract interface for a translation unit. +// +//===----------------------------------------------------------------------===// + +#ifndef LLVM_CLANG_INDEX_TRANSLATIONUNIT_H +#define LLVM_CLANG_INDEX_TRANSLATIONUNIT_H + +namespace clang { + class ASTContext; + +namespace idx { + class DeclReferenceMap; + class SelectorMap; + +/// \brief Abstract interface for a translation unit. +class TranslationUnit { +public: + virtual ~TranslationUnit(); + virtual ASTContext &getASTContext() = 0; + virtual DeclReferenceMap &getDeclReferenceMap() = 0; + virtual SelectorMap &getSelectorMap() = 0; +}; + +} // namespace idx + +} // namespace clang + +#endif diff --git a/include/clang/Index/Utils.h b/include/clang/Index/Utils.h new file mode 100644 index 0000000000000..e78ef8a155630 --- /dev/null +++ b/include/clang/Index/Utils.h @@ -0,0 +1,35 @@ +//===--- Utils.h - Misc utilities for indexing-----------------------------===// +// +// The LLVM Compiler Infrastructure +// +// This file is distributed under the University of Illinois Open Source +// License. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// +// +// This header contains miscellaneous utilities for indexing related +// functionality. +// +//===----------------------------------------------------------------------===// + +#ifndef LLVM_CLANG_INDEX_UTILS_H +#define LLVM_CLANG_INDEX_UTILS_H + +namespace clang { + class ASTContext; + class SourceLocation; + +namespace idx { + class ASTLocation; + +/// \brief Returns the ASTLocation that a source location points to. +/// +/// \returns the resolved ASTLocation or an invalid ASTLocation if the source +/// location could not be resolved. +ASTLocation ResolveLocationInAST(ASTContext &Ctx, SourceLocation Loc); + +} // end namespace idx + +} // end namespace clang + +#endif |