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"
30 class TranslationUnit;
32 /// \brief Represents a Decl or a Stmt and its immediate Decl parent. It's
35 /// ASTLocation is intended to be used as a "pointer" into the AST. It is either
36 /// just a Decl, or a Stmt and its Decl parent. Since a single Stmt is devoid
37 /// of context, its parent Decl provides all the additional missing information
38 /// like the declaration context, ASTContext, etc.
43 N_Decl, N_NamedRef, N_Stmt, N_Type
50 NamedRef() : ND(0) { }
51 NamedRef(NamedDecl *nd, SourceLocation loc) : ND(nd), Loc(loc) { }
55 llvm::PointerIntPair<Decl *, 2, NodeKind> ParentDecl;
73 explicit ASTLocation(const Decl *d)
74 : ParentDecl(const_cast<Decl*>(d), N_Decl), D(const_cast<Decl*>(d)) { }
76 ASTLocation(const Decl *parentDecl, const Stmt *stm)
77 : ParentDecl(const_cast<Decl*>(parentDecl), N_Stmt),
78 Stm(const_cast<Stmt*>(stm)) {
79 if (!stm) ParentDecl.setPointer(0);
82 ASTLocation(const Decl *parentDecl, NamedDecl *ndRef, SourceLocation loc)
83 : ParentDecl(const_cast<Decl*>(parentDecl), N_NamedRef) {
86 NDRef.RawLoc = loc.getRawEncoding();
88 ParentDecl.setPointer(0);
91 ASTLocation(const Decl *parentDecl, TypeLoc tyLoc)
92 : ParentDecl(const_cast<Decl*>(parentDecl), N_Type) {
94 Ty.TyPtr = tyLoc.getType().getAsOpaquePtr();
95 Ty.Data = tyLoc.getOpaqueData();
97 ParentDecl.setPointer(0);
100 bool isValid() const { return ParentDecl.getPointer() != 0; }
101 bool isInvalid() const { return !isValid(); }
103 NodeKind getKind() const {
105 return (NodeKind)ParentDecl.getInt();
108 Decl *getParentDecl() const { return ParentDecl.getPointer(); }
110 Decl *AsDecl() const {
111 assert(getKind() == N_Decl);
114 Stmt *AsStmt() const {
115 assert(getKind() == N_Stmt);
118 NamedRef AsNamedRef() const {
119 assert(getKind() == N_NamedRef);
120 return NamedRef(NDRef.ND, SourceLocation::getFromRawEncoding(NDRef.RawLoc));
122 TypeLoc AsTypeLoc() const {
123 assert(getKind() == N_Type);
124 return TypeLoc(QualType::getFromOpaquePtr(Ty.TyPtr), Ty.Data);
127 Decl *dyn_AsDecl() const { return isValid() && getKind() == N_Decl ? D : 0; }
128 Stmt *dyn_AsStmt() const { return isValid() && getKind() == N_Stmt ? Stm : 0; }
129 NamedRef dyn_AsNamedRef() const {
130 return getKind() == N_Type ? AsNamedRef() : NamedRef();
132 TypeLoc dyn_AsTypeLoc() const {
133 return getKind() == N_Type ? AsTypeLoc() : TypeLoc();
136 bool isDecl() const { return isValid() && getKind() == N_Decl; }
137 bool isStmt() const { return isValid() && getKind() == N_Stmt; }
138 bool isNamedRef() const { return isValid() && getKind() == N_NamedRef; }
139 bool isType() const { return isValid() && getKind() == N_Type; }
141 /// \brief Returns the declaration that this ASTLocation references.
143 /// If this points to a Decl, that Decl is returned.
144 /// If this points to an Expr that references a Decl, that Decl is returned,
145 /// otherwise it returns NULL.
146 Decl *getReferencedDecl();
147 const Decl *getReferencedDecl() const {
148 return const_cast<ASTLocation*>(this)->getReferencedDecl();
151 SourceRange getSourceRange() const;
153 void print(llvm::raw_ostream &OS) const;
156 /// \brief Like ASTLocation but also contains the TranslationUnit that the
157 /// ASTLocation originated from.
158 class TULocation : public ASTLocation {
162 TULocation(TranslationUnit *tu, ASTLocation astLoc)
163 : ASTLocation(astLoc), TU(tu) {
164 assert(tu && "Passed null translation unit");
167 TranslationUnit *getTU() const { return TU; }