1 //===--- ASTLocation.h - A <Decl, Stmt> pair --------------------*- 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 // ASTLocation is Decl or a Stmt and its immediate Decl parent.
12 //===----------------------------------------------------------------------===//
14 #ifndef LLVM_CLANG_INDEX_ASTLOCATION_H
15 #define LLVM_CLANG_INDEX_ASTLOCATION_H
17 #include "clang/AST/TypeLoc.h"
18 #include "llvm/ADT/PointerIntPair.h"
26 class TranslationUnit;
28 /// \brief Represents a Decl or a Stmt and its immediate Decl parent. It's
31 /// ASTLocation is intended to be used as a "pointer" into the AST. It is either
32 /// just a Decl, or a Stmt and its Decl parent. Since a single Stmt is devoid
33 /// of context, its parent Decl provides all the additional missing information
34 /// like the declaration context, ASTContext, etc.
39 N_Decl, N_NamedRef, N_Stmt, N_Type
46 NamedRef() : ND(0) { }
47 NamedRef(NamedDecl *nd, SourceLocation loc) : ND(nd), Loc(loc) { }
51 llvm::PointerIntPair<Decl *, 2, NodeKind> ParentDecl;
69 explicit ASTLocation(const Decl *d)
70 : ParentDecl(const_cast<Decl*>(d), N_Decl), D(const_cast<Decl*>(d)) { }
72 ASTLocation(const Decl *parentDecl, const Stmt *stm)
73 : ParentDecl(const_cast<Decl*>(parentDecl), N_Stmt),
74 Stm(const_cast<Stmt*>(stm)) {
75 if (!stm) ParentDecl.setPointer(0);
78 ASTLocation(const Decl *parentDecl, NamedDecl *ndRef, SourceLocation loc)
79 : ParentDecl(const_cast<Decl*>(parentDecl), N_NamedRef) {
82 NDRef.RawLoc = loc.getRawEncoding();
84 ParentDecl.setPointer(0);
87 ASTLocation(const Decl *parentDecl, TypeLoc tyLoc)
88 : ParentDecl(const_cast<Decl*>(parentDecl), N_Type) {
90 Ty.TyPtr = tyLoc.getType().getAsOpaquePtr();
91 Ty.Data = tyLoc.getOpaqueData();
93 ParentDecl.setPointer(0);
96 bool isValid() const { return ParentDecl.getPointer() != 0; }
97 bool isInvalid() const { return !isValid(); }
99 NodeKind getKind() const {
101 return (NodeKind)ParentDecl.getInt();
104 Decl *getParentDecl() const { return ParentDecl.getPointer(); }
106 Decl *AsDecl() const {
107 assert(getKind() == N_Decl);
110 Stmt *AsStmt() const {
111 assert(getKind() == N_Stmt);
114 NamedRef AsNamedRef() const {
115 assert(getKind() == N_NamedRef);
116 return NamedRef(NDRef.ND, SourceLocation::getFromRawEncoding(NDRef.RawLoc));
118 TypeLoc AsTypeLoc() const {
119 assert(getKind() == N_Type);
120 return TypeLoc(QualType::getFromOpaquePtr(Ty.TyPtr), Ty.Data);
123 Decl *dyn_AsDecl() const { return isValid() && getKind() == N_Decl ? D : 0; }
124 Stmt *dyn_AsStmt() const { return isValid() && getKind() == N_Stmt ? Stm : 0; }
125 NamedRef dyn_AsNamedRef() const {
126 return getKind() == N_Type ? AsNamedRef() : NamedRef();
128 TypeLoc dyn_AsTypeLoc() const {
129 return getKind() == N_Type ? AsTypeLoc() : TypeLoc();
132 bool isDecl() const { return isValid() && getKind() == N_Decl; }
133 bool isStmt() const { return isValid() && getKind() == N_Stmt; }
134 bool isNamedRef() const { return isValid() && getKind() == N_NamedRef; }
135 bool isType() const { return isValid() && getKind() == N_Type; }
137 /// \brief Returns the declaration that this ASTLocation references.
139 /// If this points to a Decl, that Decl is returned.
140 /// If this points to an Expr that references a Decl, that Decl is returned,
141 /// otherwise it returns NULL.
142 Decl *getReferencedDecl();
143 const Decl *getReferencedDecl() const {
144 return const_cast<ASTLocation*>(this)->getReferencedDecl();
147 SourceRange getSourceRange() const;
149 void print(raw_ostream &OS) const;
152 /// \brief Like ASTLocation but also contains the TranslationUnit that the
153 /// ASTLocation originated from.
154 class TULocation : public ASTLocation {
158 TULocation(TranslationUnit *tu, ASTLocation astLoc)
159 : ASTLocation(astLoc), TU(tu) {
160 assert(tu && "Passed null translation unit");
163 TranslationUnit *getTU() const { return TU; }