]> granicus.if.org Git - clang/blob - include/clang/AST/ExternalASTSource.h
A couple more small changes which are probably required for Cygwin
[clang] / include / clang / AST / ExternalASTSource.h
1 //===--- ExternalASTSource.h - Abstract External AST Interface --*- 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 //  This file defines the ExternalASTSource interface, which enables
11 //  construction of AST nodes from some external source.x
12 //
13 //===----------------------------------------------------------------------===//
14 #ifndef LLVM_CLANG_AST_EXTERNAL_AST_SOURCE_H
15 #define LLVM_CLANG_AST_EXTERNAL_AST_SOURCE_H
16
17 #include "clang/AST/DeclarationName.h"
18 #include "clang/AST/Type.h"
19 #include "llvm/ADT/SmallVector.h"
20 #include <cassert>
21 namespace clang {
22
23 class ASTConsumer;
24 class Decl;
25 class DeclContext;
26 class ExternalSemaSource; // layering violation required for downcasting
27 class Stmt;
28
29 /// \brief The deserialized representation of a set of declarations
30 /// with the same name that are visible in a given context.
31 struct VisibleDeclaration {
32   /// \brief The name of the declarations.
33   DeclarationName Name;
34
35   /// \brief The ID numbers of all of the declarations with this name. 
36   ///
37   /// These declarations have not necessarily been de-serialized.
38   llvm::SmallVector<unsigned, 4> Declarations;
39 };
40
41 /// \brief Abstract interface for external sources of AST nodes.
42 ///
43 /// External AST sources provide AST nodes constructed from some
44 /// external source, such as a precompiled header. External AST
45 /// sources can resolve types and declarations from abstract IDs into
46 /// actual type and declaration nodes, and read parts of declaration
47 /// contexts.
48 class ExternalASTSource {
49   /// \brief Whether this AST source also provides information for
50   /// semantic analysis.
51   bool SemaSource;
52
53   friend class ExternalSemaSource;
54
55 public:
56   ExternalASTSource() : SemaSource(false) { }
57
58   virtual ~ExternalASTSource();
59
60   /// \brief Resolve a type ID into a type, potentially building a new
61   /// type.
62   virtual QualType GetType(unsigned ID) = 0;
63
64   /// \brief Resolve a declaration ID into a declaration, potentially
65   /// building a new declaration.
66   virtual Decl *GetDecl(unsigned ID) = 0;
67
68   /// \brief Resolve the offset of a statement in the decl stream into a
69   /// statement.
70   ///
71   /// This operation will read a new statement from the external
72   /// source each time it is called, and is meant to be used via a
73   /// LazyOffsetPtr.
74   virtual Stmt *GetDeclStmt(uint64_t Offset) = 0;
75
76   /// \brief Read all of the declarations lexically stored in a
77   /// declaration context.
78   ///
79   /// \param DC The declaration context whose declarations will be
80   /// read.
81   ///
82   /// \param Decls Vector that will contain the declarations loaded
83   /// from the external source. The caller is responsible for merging
84   /// these declarations with any declarations already stored in the
85   /// declaration context.
86   ///
87   /// \returns true if there was an error while reading the
88   /// declarations for this declaration context.
89   virtual bool ReadDeclsLexicallyInContext(DeclContext *DC,
90                                   llvm::SmallVectorImpl<uint32_t> &Decls) = 0;
91
92   /// \brief Read all of the declarations visible from a declaration
93   /// context.
94   ///
95   /// \param DC The declaration context whose visible declarations
96   /// will be read.
97   ///
98   /// \param Decls A vector of visible declaration structures,
99   /// providing the mapping from each name visible in the declaration
100   /// context to the declaration IDs of declarations with that name.
101   ///
102   /// \returns true if there was an error while reading the
103   /// declarations for this declaration context.
104   virtual bool ReadDeclsVisibleInContext(DeclContext *DC,
105                        llvm::SmallVectorImpl<VisibleDeclaration> & Decls) = 0;
106
107   /// \brief Function that will be invoked when we begin parsing a new
108   /// translation unit involving this external AST source.
109   virtual void StartTranslationUnit(ASTConsumer *Consumer) { }
110
111   /// \brief Print any statistics that have been gathered regarding
112   /// the external AST source.
113   virtual void PrintStats();
114 };
115
116 /// \brief A lazy pointer to an AST node (of base type T) that resides
117 /// within an external AST source.
118 ///
119 /// The AST node is identified within the external AST source by a
120 /// 63-bit offset, and can be retrieved via an operation on the
121 /// external AST source itself.
122 template<typename T, T* (ExternalASTSource::*Get)(uint64_t Offset)>
123 struct LazyOffsetPtr {
124   /// \brief Either a pointer to an AST node or the offset within the
125   /// external AST source where the AST node can be found.
126   ///
127   /// If the low bit is clear, a pointer to the AST node. If the low
128   /// bit is set, the upper 63 bits are the offset.
129   mutable uint64_t Ptr;
130
131 public:
132   LazyOffsetPtr() : Ptr(0) { }
133
134   explicit LazyOffsetPtr(T *Ptr) : Ptr(reinterpret_cast<uint64_t>(Ptr)) { }
135   explicit LazyOffsetPtr(uint64_t Offset) : Ptr((Offset << 1) | 0x01) {
136     assert((Offset << 1 >> 1) == Offset && "Offsets must require < 63 bits");
137     if (Offset == 0)
138       Ptr = 0;
139   }
140
141   LazyOffsetPtr &operator=(T *Ptr) {
142     this->Ptr = reinterpret_cast<uint64_t>(Ptr);
143     return *this;
144   }
145   
146   LazyOffsetPtr &operator=(uint64_t Offset) {
147     assert((Offset << 1 >> 1) == Offset && "Offsets must require < 63 bits");
148     if (Offset == 0)
149       Ptr = 0;
150     else
151       Ptr = (Offset << 1) | 0x01;
152
153     return *this;
154   }
155
156   /// \brief Whether this pointer is non-NULL.
157   ///
158   /// This operation does not require the AST node to be deserialized.
159   operator bool() const { return Ptr != 0; }
160
161   /// \brief Whether this pointer is currently stored as an offset.
162   bool isOffset() const { return Ptr & 0x01; }
163
164   /// \brief Retrieve the pointer to the AST node that this lazy pointer
165   ///
166   /// \param Source the external AST source.
167   ///
168   /// \returns a pointer to the AST node.
169   T* get(ExternalASTSource *Source) const {
170     if (isOffset()) {
171       assert(Source && 
172              "Cannot deserialize a lazy pointer without an AST source");
173       Ptr = reinterpret_cast<uint64_t>((Source->*Get)(Ptr >> 1));
174     }
175     return reinterpret_cast<T*>(Ptr);
176   }
177 };
178
179 /// \brief A lazy pointer to a statement.
180 typedef LazyOffsetPtr<Stmt, &ExternalASTSource::GetDeclStmt> LazyDeclStmtPtr;
181
182 } // end namespace clang
183
184 #endif // LLVM_CLANG_AST_EXTERNAL_AST_SOURCE_H