ClangASTSource.h revision 9b6898f3ec1dedbe1dfc8bd7cd1d82a5b10e1bb0
1//===-- ClangASTSource.h ----------------------------------------*- 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#ifndef liblldb_ClangASTSource_h_
11#define liblldb_ClangASTSource_h_
12
13#include <set>
14
15#include "clang/Basic/IdentifierTable.h"
16#include "clang/AST/ExternalASTSource.h"
17
18namespace lldb_private {
19
20class ClangExpressionDeclMap;
21
22//----------------------------------------------------------------------
23/// @class ClangASTSource ClangASTSource.h "lldb/Expression/ClangASTSource.h"
24/// @brief Provider for named objects defined in the debug info for Clang
25///
26/// As Clang parses an expression, it may encounter names that are not
27/// defined inside the expression, including variables, functions, and
28/// types.  Clang knows the name it is looking for, but nothing else.
29/// The ExternalSemaSource class provides Decls (VarDecl, FunDecl, TypeDecl)
30/// to Clang for these names, consulting the ClangExpressionDeclMap to do
31/// the actual lookups.
32//----------------------------------------------------------------------
33class ClangASTSource : public clang::ExternalASTSource
34{
35public:
36    //------------------------------------------------------------------
37    /// Constructor
38    ///
39    /// Initializes class variabes.
40    ///
41    /// @param[in] context
42    ///     A reference to the AST context provided by the parser.
43    ///
44    /// @param[in] declMap
45    ///     A reference to the LLDB object that handles entity lookup.
46    //------------------------------------------------------------------
47	ClangASTSource (clang::ASTContext &context,
48                    ClangExpressionDeclMap &decl_map) :
49        m_ast_context (context),
50        m_decl_map (decl_map),
51        m_active_lookups ()
52    {
53    }
54
55    //------------------------------------------------------------------
56    /// Destructor
57    //------------------------------------------------------------------
58	~ClangASTSource();
59
60    //------------------------------------------------------------------
61    /// Interface stub that returns NULL.
62    //------------------------------------------------------------------
63    virtual clang::Decl *
64    GetExternalDecl(uint32_t)
65    {
66        // These are only required for AST source that want to lazily load
67        // the declarations (or parts thereof) that they return.
68        return NULL;
69    }
70
71    //------------------------------------------------------------------
72    /// Interface stub that returns NULL.
73    //------------------------------------------------------------------
74    virtual clang::Stmt *
75    GetExternalDeclStmt(uint64_t)
76    {
77        // These are only required for AST source that want to lazily load
78        // the declarations (or parts thereof) that they return.
79        return NULL;
80    }
81
82    //------------------------------------------------------------------
83    /// Interface stub that returns an undifferentiated Selector.
84    //------------------------------------------------------------------
85    virtual clang::Selector
86    GetExternalSelector(uint32_t)
87    {
88        // These are also optional, although it might help with ObjC
89        // debugging if we have respectable signatures.  But a more
90        // efficient interface (that didn't require scanning all files
91        // for method signatures!) might help.
92        return clang::Selector();
93    }
94
95    //------------------------------------------------------------------
96    /// Interface stub that returns 0.
97    //------------------------------------------------------------------
98	virtual uint32_t
99    GetNumExternalSelectors()
100    {
101        // These are also optional, although it might help with ObjC
102        // debugging if we have respectable signatures.  But a more
103        // efficient interface (that didn't require scanning all files
104        // for method signatures!) might help.
105        return 0;
106    }
107
108    //------------------------------------------------------------------
109    /// Interface stub that returns NULL.
110    //------------------------------------------------------------------
111    virtual clang::CXXBaseSpecifier *
112    GetExternalCXXBaseSpecifiers(uint64_t Offset)
113    {
114        return NULL;
115    }
116
117    //------------------------------------------------------------------
118    /// Look up all Decls that match a particular name.  Only handles
119    /// Identifiers.  Passes the request on to DeclMap, and calls
120    /// SetExternalVisibleDeclsForName with the result.
121    ///
122    /// @param[in] DC
123    ///     The DeclContext to register the found Decls in.
124    ///
125    /// @param[in] Name
126    ///     The name to find entries for.
127    ///
128    /// @return
129    ///     Whatever SetExternalVisibleDeclsForName returns.
130    //------------------------------------------------------------------
131    virtual clang::DeclContextLookupResult
132    FindExternalVisibleDeclsByName (const clang::DeclContext *DC,
133                                    clang::DeclarationName Name);
134
135    //------------------------------------------------------------------
136    /// Interface stub.
137    //------------------------------------------------------------------
138    virtual void
139    MaterializeVisibleDecls (const clang::DeclContext *DC);
140
141    //------------------------------------------------------------------
142    /// Interface stub that returns true.
143    //------------------------------------------------------------------
144	virtual clang::ExternalLoadResult
145    FindExternalLexicalDecls (const clang::DeclContext *DC,
146                              bool (*isKindWeWant)(clang::Decl::Kind),
147                              llvm::SmallVectorImpl<clang::Decl*> &Decls);
148
149
150    virtual void
151    CompleteType (clang::TagDecl *Tag);
152
153    virtual void
154    CompleteType (clang::ObjCInterfaceDecl *Class);
155
156    //------------------------------------------------------------------
157    /// Called on entering a translation unit.  Tells Clang by calling
158    /// setHasExternalVisibleStorage() and setHasExternalLexicalStorage()
159    /// that this object has something to say about undefined names.
160    ///
161    /// @param[in] ASTConsumer
162    ///     Unused.
163    //------------------------------------------------------------------
164    void StartTranslationUnit (clang::ASTConsumer *Consumer);
165
166protected:
167    friend struct NameSearchContext;
168
169	clang::ASTContext &m_ast_context;   ///< The parser's AST context, for copying types into
170	ClangExpressionDeclMap &m_decl_map; ///< The object that looks up named entities in LLDB
171    std::set<const char *> m_active_lookups;
172};
173
174//----------------------------------------------------------------------
175/// @class NameSearchContext ClangASTSource.h "lldb/Expression/ClangASTSource.h"
176/// @brief Container for all objects relevant to a single name lookup
177///
178/// LLDB needs to create Decls for entities it finds.  This class communicates
179/// what name is being searched for and provides helper functions to construct
180/// Decls given appropriate type information.
181//----------------------------------------------------------------------
182struct NameSearchContext {
183    ClangASTSource &m_ast_source;                       ///< The AST source making the request
184    llvm::SmallVectorImpl<clang::NamedDecl*> &m_decls;  ///< The list of declarations already constructed
185    const clang::DeclarationName &m_decl_name;          ///< The name being looked for
186    const clang::DeclContext *m_decl_context;           ///< The DeclContext to put declarations into
187
188    //------------------------------------------------------------------
189    /// Constructor
190    ///
191    /// Initializes class variables.
192    ///
193    /// @param[in] astSource
194    ///     A reference to the AST source making a request.
195    ///
196    /// @param[in] decls
197    ///     A reference to a list into which new Decls will be placed.  This
198    ///     list is typically empty when the function is called.
199    ///
200    /// @param[in] name
201    ///     The name being searched for (always an Identifier).
202    ///
203    /// @param[in] dc
204    ///     The DeclContext to register Decls in.
205    //------------------------------------------------------------------
206    NameSearchContext (ClangASTSource &astSource,
207                       llvm::SmallVectorImpl<clang::NamedDecl*> &decls,
208                       clang::DeclarationName &name,
209                       const clang::DeclContext *dc) :
210        m_ast_source(astSource),
211        m_decls(decls),
212        m_decl_name(name),
213        m_decl_context(dc) {}
214
215    //------------------------------------------------------------------
216    /// Return the AST context for the current search.  Useful when copying
217    /// types.
218    //------------------------------------------------------------------
219    clang::ASTContext *GetASTContext();
220
221    //------------------------------------------------------------------
222    /// Create a VarDecl with the name being searched for and the provided
223    /// type and register it in the right places.
224    ///
225    /// @param[in] type
226    ///     The opaque QualType for the VarDecl being registered.
227    //------------------------------------------------------------------
228    clang::NamedDecl *AddVarDecl(void *type);
229
230    //------------------------------------------------------------------
231    /// Create a FunDecl with the name being searched for and the provided
232    /// type and register it in the right places.
233    ///
234    /// @param[in] type
235    ///     The opaque QualType for the FunDecl being registered.
236    //------------------------------------------------------------------
237    clang::NamedDecl *AddFunDecl(void *type);
238
239    //------------------------------------------------------------------
240    /// Create a FunDecl with the name being searched for and generic
241    /// type (i.e. intptr_t NAME_GOES_HERE(...)) and register it in the
242    /// right places.
243    //------------------------------------------------------------------
244    clang::NamedDecl *AddGenericFunDecl();
245
246    //------------------------------------------------------------------
247    /// Create a TypeDecl with the name being searched for and the provided
248    /// type and register it in the right places.
249    ///
250    /// @param[in] type
251    ///     The opaque QualType for the TypeDecl being registered.
252    //------------------------------------------------------------------
253    clang::NamedDecl *AddTypeDecl(void *type);
254
255
256    //------------------------------------------------------------------
257    /// Add Decls from the provided DeclContextLookupResult to the list
258    /// of results.
259    ///
260    /// @param[in] result
261    ///     The DeclContextLookupResult, usually returned as the result
262    ///     of querying a DeclContext.
263    //------------------------------------------------------------------
264    void AddLookupResult (clang::DeclContextLookupConstResult result);
265
266    //------------------------------------------------------------------
267    /// Add a NamedDecl to the list of results.
268    ///
269    /// @param[in] decl
270    ///     The NamedDecl, usually returned as the result
271    ///     of querying a DeclContext.
272    //------------------------------------------------------------------
273    void AddNamedDecl (clang::NamedDecl *decl);
274};
275
276}
277
278#endif
279