]> CyberLeo.Net >> Repos - FreeBSD/releng/9.0.git/blob - contrib/llvm/tools/clang/include/clang/Index/ASTLocation.h
Copy stable/9 to releng/9.0 as part of the FreeBSD 9.0-RELEASE release
[FreeBSD/releng/9.0.git] / contrib / llvm / tools / clang / include / clang / Index / ASTLocation.h
1 //===--- ASTLocation.h - A <Decl, Stmt> pair --------------------*- C++ -*-===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 //  ASTLocation is Decl or a Stmt and its immediate Decl parent.
11 //
12 //===----------------------------------------------------------------------===//
13
14 #ifndef LLVM_CLANG_INDEX_ASTLOCATION_H
15 #define LLVM_CLANG_INDEX_ASTLOCATION_H
16
17 #include "clang/AST/TypeLoc.h"
18 #include "llvm/ADT/PointerIntPair.h"
19
20 namespace clang {
21   class Decl;
22   class Stmt;
23   class NamedDecl;
24
25 namespace idx {
26   class TranslationUnit;
27
28 /// \brief Represents a Decl or a Stmt and its immediate Decl parent. It's
29 /// immutable.
30 ///
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.
35 ///
36 class ASTLocation {
37 public:
38   enum NodeKind {
39     N_Decl, N_NamedRef, N_Stmt, N_Type
40   };
41
42   struct NamedRef {
43     NamedDecl *ND;
44     SourceLocation Loc;
45     
46     NamedRef() : ND(0) { }
47     NamedRef(NamedDecl *nd, SourceLocation loc) : ND(nd), Loc(loc) { }
48   };
49
50 private:
51   llvm::PointerIntPair<Decl *, 2, NodeKind> ParentDecl;
52
53   union {
54     Decl *D;
55     Stmt *Stm;
56     struct {
57       NamedDecl *ND;
58       unsigned RawLoc;
59     } NDRef;
60     struct {
61       void *TyPtr;
62       void *Data;
63     } Ty;
64   };
65
66 public:
67   ASTLocation() { }
68
69   explicit ASTLocation(const Decl *d)
70     : ParentDecl(const_cast<Decl*>(d), N_Decl), D(const_cast<Decl*>(d)) { }
71
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);
76   }
77
78   ASTLocation(const Decl *parentDecl, NamedDecl *ndRef, SourceLocation loc)
79     : ParentDecl(const_cast<Decl*>(parentDecl), N_NamedRef) {
80     if (ndRef) {
81       NDRef.ND = ndRef;
82       NDRef.RawLoc = loc.getRawEncoding();
83     } else
84       ParentDecl.setPointer(0);
85   }
86
87   ASTLocation(const Decl *parentDecl, TypeLoc tyLoc)
88     : ParentDecl(const_cast<Decl*>(parentDecl), N_Type) {
89     if (tyLoc) {
90       Ty.TyPtr = tyLoc.getType().getAsOpaquePtr();
91       Ty.Data = tyLoc.getOpaqueData();
92     } else
93       ParentDecl.setPointer(0);
94   }
95
96   bool isValid() const { return ParentDecl.getPointer() != 0; }
97   bool isInvalid() const { return !isValid(); }
98   
99   NodeKind getKind() const {
100     assert(isValid());
101     return (NodeKind)ParentDecl.getInt();
102   }
103   
104   Decl *getParentDecl() const { return ParentDecl.getPointer(); }
105   
106   Decl *AsDecl() const {
107     assert(getKind() == N_Decl);
108     return D;
109   }
110   Stmt *AsStmt() const {
111     assert(getKind() == N_Stmt);
112     return Stm;
113   }
114   NamedRef AsNamedRef() const {
115     assert(getKind() == N_NamedRef);
116     return NamedRef(NDRef.ND, SourceLocation::getFromRawEncoding(NDRef.RawLoc));
117   }
118   TypeLoc AsTypeLoc() const {
119     assert(getKind() == N_Type);
120     return TypeLoc(QualType::getFromOpaquePtr(Ty.TyPtr), Ty.Data);
121   }
122
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();
127   }
128   TypeLoc dyn_AsTypeLoc() const {
129     return getKind() == N_Type ? AsTypeLoc() : TypeLoc();
130   }
131   
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; }
136
137   /// \brief Returns the declaration that this ASTLocation references.
138   ///
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();
145   }
146
147   SourceRange getSourceRange() const;
148
149   void print(raw_ostream &OS) const;
150 };
151
152 /// \brief Like ASTLocation but also contains the TranslationUnit that the
153 /// ASTLocation originated from.
154 class TULocation : public ASTLocation {
155   TranslationUnit *TU;
156
157 public:
158   TULocation(TranslationUnit *tu, ASTLocation astLoc)
159     : ASTLocation(astLoc), TU(tu) {
160     assert(tu && "Passed null translation unit");
161   }
162
163   TranslationUnit *getTU() const { return TU; }
164 };
165
166 } // namespace idx
167
168 } // namespace clang
169
170 #endif