ClangASTSource.h revision 9ceed1e2b872d12a1c01ba148855db07b193649e
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/Sema/ExternalSemaSource.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::ExternalSemaSource
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    clang::Decl *GetExternalDecl(uint32_t);
64
65    //------------------------------------------------------------------
66    /// Interface stub that returns NULL.
67    //------------------------------------------------------------------
68    clang::Stmt *GetExternalDeclStmt(uint64_t);
69
70    //------------------------------------------------------------------
71    /// Interface stub that returns an undifferentiated Selector.
72    //------------------------------------------------------------------
73    clang::Selector GetExternalSelector(uint32_t);
74
75    //------------------------------------------------------------------
76    /// Interface stub that returns 0.
77    //------------------------------------------------------------------
78	uint32_t GetNumExternalSelectors();
79
80    //------------------------------------------------------------------
81    /// Look up all Decls that match a particular name.  Only handles
82    /// Identifiers.  Passes the request on to DeclMap, and calls
83    /// SetExternalVisibleDeclsForName with the result.
84    ///
85    /// @param[in] DC
86    ///     The DeclContext to register the found Decls in.
87    ///
88    /// @param[in] Name
89    ///     The name to find entries for.
90    ///
91    /// @return
92    ///     Whatever SetExternalVisibleDeclsForName returns.
93    //------------------------------------------------------------------
94    clang::DeclContextLookupResult
95    FindExternalVisibleDeclsByName (const clang::DeclContext *DC,
96                                    clang::DeclarationName Name);
97
98    //------------------------------------------------------------------
99    /// Interface stub.
100    //------------------------------------------------------------------
101    void MaterializeVisibleDecls (const clang::DeclContext *DC);
102
103    //------------------------------------------------------------------
104    /// Interface stub that returns true.
105    //------------------------------------------------------------------
106	bool FindExternalLexicalDecls (const clang::DeclContext *DC,
107                                   bool (*isKindWeWant)(clang::Decl::Kind),
108                                   llvm::SmallVectorImpl<clang::Decl*> &Decls);
109
110    //------------------------------------------------------------------
111    /// Called on entering a translation unit.  Tells Clang by calling
112    /// setHasExternalVisibleStorage() and setHasExternalLexicalStorage()
113    /// that this object has something to say about undefined names.
114    ///
115    /// @param[in] ASTConsumer
116    ///     Unused.
117    //------------------------------------------------------------------
118    void StartTranslationUnit (clang::ASTConsumer *Consumer);
119
120protected:
121    friend struct NameSearchContext;
122
123	clang::ASTContext &m_ast_context;   ///< The parser's AST context, for copying types into
124	ClangExpressionDeclMap &m_decl_map; ///< The object that looks up named entities in LLDB
125    std::set<const char *> m_active_lookups;
126};
127
128//----------------------------------------------------------------------
129/// @class NameSearchContext ClangASTSource.h "lldb/Expression/ClangASTSource.h"
130/// @brief Container for all objects relevant to a single name lookup
131///
132/// LLDB needs to create Decls for entities it finds.  This class communicates
133/// what name is being searched for and provides helper functions to construct
134/// Decls given appropriate type information.
135//----------------------------------------------------------------------
136struct NameSearchContext {
137    ClangASTSource &m_ast_source;                       ///< The AST source making the request
138    llvm::SmallVectorImpl<clang::NamedDecl*> &m_decls;  ///< The list of declarations already constructed
139    const clang::DeclarationName &m_decl_name;          ///< The name being looked for
140    const clang::DeclContext *m_decl_context;           ///< The DeclContext to put declarations into
141
142    //------------------------------------------------------------------
143    /// Constructor
144    ///
145    /// Initializes class variables.
146    ///
147    /// @param[in] astSource
148    ///     A reference to the AST source making a request.
149    ///
150    /// @param[in] decls
151    ///     A reference to a list into which new Decls will be placed.  This
152    ///     list is typically empty when the function is called.
153    ///
154    /// @param[in] name
155    ///     The name being searched for (always an Identifier).
156    ///
157    /// @param[in] dc
158    ///     The DeclContext to register Decls in.
159    //------------------------------------------------------------------
160    NameSearchContext (ClangASTSource &astSource,
161                       llvm::SmallVectorImpl<clang::NamedDecl*> &decls,
162                       clang::DeclarationName &name,
163                       const clang::DeclContext *dc) :
164        m_ast_source(astSource),
165        m_decls(decls),
166        m_decl_name(name),
167        m_decl_context(dc) {}
168
169    //------------------------------------------------------------------
170    /// Return the AST context for the current search.  Useful when copying
171    /// types.
172    //------------------------------------------------------------------
173    clang::ASTContext *GetASTContext();
174
175    //------------------------------------------------------------------
176    /// Create a VarDecl with the name being searched for and the provided
177    /// type and register it in the right places.
178    ///
179    /// @param[in] type
180    ///     The opaque QualType for the VarDecl being registered.
181    //------------------------------------------------------------------
182    clang::NamedDecl *AddVarDecl(void *type);
183
184    //------------------------------------------------------------------
185    /// Create a FunDecl with the name being searched for and the provided
186    /// type and register it in the right places.
187    ///
188    /// @param[in] type
189    ///     The opaque QualType for the FunDecl being registered.
190    //------------------------------------------------------------------
191    clang::NamedDecl *AddFunDecl(void *type);
192
193    //------------------------------------------------------------------
194    /// Create a FunDecl with the name being searched for and generic
195    /// type (i.e. intptr_t NAME_GOES_HERE(...)) and register it in the
196    /// right places.
197    //------------------------------------------------------------------
198    clang::NamedDecl *AddGenericFunDecl();
199
200    //------------------------------------------------------------------
201    /// Create a TypeDecl with the name being searched for and the provided
202    /// type and register it in the right places.
203    ///
204    /// @param[in] type
205    ///     The opaque QualType for the TypeDecl being registered.
206    //------------------------------------------------------------------
207    clang::NamedDecl *AddTypeDecl(void *type);
208};
209
210}
211
212#endif