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