1 //===-- ASTResultSynthesizer.h ----------------------------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #ifndef liblldb_ASTResultSynthesizer_h_
11 #define liblldb_ASTResultSynthesizer_h_
13 #include "lldb/Core/ClangForward.h"
14 #include "lldb/Target/Target.h"
15 #include "clang/Sema/SemaConsumer.h"
17 namespace lldb_private {
19 //----------------------------------------------------------------------
20 /// @class ASTResultSynthesizer ASTResultSynthesizer.h
21 /// "lldb/Expression/ASTResultSynthesizer.h"
22 /// @brief Adds a result variable declaration to the ASTs for an expression.
24 /// Users expect the expression "i + 3" to return a result, even if a result
25 /// variable wasn't specifically declared. To fulfil this requirement, LLDB
27 /// a result variable to the expression, transforming it to
28 /// "int $__lldb_expr_result = i + 3." The IR transformers ensure that the
29 /// resulting variable is mapped to the right piece of memory.
30 /// ASTResultSynthesizer's job is to add the variable and its initialization to
31 /// the ASTs for the expression, and it does so by acting as a SemaConsumer for
33 //----------------------------------------------------------------------
34 class ASTResultSynthesizer : public clang::SemaConsumer {
36 //----------------------------------------------------------------------
39 /// @param[in] passthrough
40 /// Since the ASTs must typically go through to the Clang code generator
41 /// in order to produce LLVM IR, this SemaConsumer must allow them to
42 /// pass to the next step in the chain after processing. Passthrough is
43 /// the next ASTConsumer, or NULL if none is required.
45 /// @param[in] top_level
46 /// If true, register all top-level Decls and don't try to handle the
50 /// The target, which contains the persistent variable store and the
52 //----------------------------------------------------------------------
53 ASTResultSynthesizer(clang::ASTConsumer *passthrough, bool top_level,
56 //----------------------------------------------------------------------
58 //----------------------------------------------------------------------
59 ~ASTResultSynthesizer() override;
61 //----------------------------------------------------------------------
62 /// Link this consumer with a particular AST context
64 /// @param[in] Context
65 /// This AST context will be used for types and identifiers, and also
66 /// forwarded to the passthrough consumer, if one exists.
67 //----------------------------------------------------------------------
68 void Initialize(clang::ASTContext &Context) override;
70 //----------------------------------------------------------------------
71 /// Examine a list of Decls to find the function $__lldb_expr and
72 /// transform its code
75 /// The list of Decls to search. These may contain LinkageSpecDecls,
76 /// which need to be searched recursively. That job falls to
77 /// TransformTopLevelDecl.
78 //----------------------------------------------------------------------
79 bool HandleTopLevelDecl(clang::DeclGroupRef D) override;
81 //----------------------------------------------------------------------
83 //----------------------------------------------------------------------
84 void HandleTranslationUnit(clang::ASTContext &Ctx) override;
86 //----------------------------------------------------------------------
88 //----------------------------------------------------------------------
89 void HandleTagDeclDefinition(clang::TagDecl *D) override;
91 //----------------------------------------------------------------------
93 //----------------------------------------------------------------------
94 void CompleteTentativeDefinition(clang::VarDecl *D) override;
96 //----------------------------------------------------------------------
98 //----------------------------------------------------------------------
99 void HandleVTable(clang::CXXRecordDecl *RD) override;
101 //----------------------------------------------------------------------
103 //----------------------------------------------------------------------
104 void PrintStats() override;
106 //----------------------------------------------------------------------
107 /// Set the Sema object to use when performing transforms, and pass it on
110 /// The Sema to use. Because Sema isn't externally visible, this class
111 /// casts it to an Action for actual use.
112 //----------------------------------------------------------------------
113 void InitializeSema(clang::Sema &S) override;
115 //----------------------------------------------------------------------
116 /// Reset the Sema to NULL now that transformations are done
117 //----------------------------------------------------------------------
118 void ForgetSema() override;
120 //----------------------------------------------------------------------
121 /// The parse has succeeded, so record its persistent decls
122 //----------------------------------------------------------------------
123 void CommitPersistentDecls();
126 //----------------------------------------------------------------------
127 /// Hunt the given Decl for FunctionDecls named $__lldb_expr, recursing
128 /// as necessary through LinkageSpecDecls, and calling SynthesizeResult on
129 /// anything that was found
132 /// The Decl to hunt.
133 //----------------------------------------------------------------------
134 void TransformTopLevelDecl(clang::Decl *D);
136 //----------------------------------------------------------------------
137 /// Process an Objective-C method and produce the result variable and
140 /// @param[in] MethodDecl
141 /// The method to process.
142 //----------------------------------------------------------------------
143 bool SynthesizeObjCMethodResult(clang::ObjCMethodDecl *MethodDecl);
145 //----------------------------------------------------------------------
146 /// Process a function and produce the result variable and initialization
148 /// @param[in] FunDecl
149 /// The function to process.
150 //----------------------------------------------------------------------
151 bool SynthesizeFunctionResult(clang::FunctionDecl *FunDecl);
153 //----------------------------------------------------------------------
154 /// Process a function body and produce the result variable and
158 /// The body of the function.
161 /// The DeclContext of the function, into which the result variable
163 //----------------------------------------------------------------------
164 bool SynthesizeBodyResult(clang::CompoundStmt *Body, clang::DeclContext *DC);
166 //----------------------------------------------------------------------
167 /// Given a DeclContext for a function or method, find all types
168 /// declared in the context and record any persistent types found.
170 /// @param[in] FunDeclCtx
171 /// The context for the function to process.
172 //----------------------------------------------------------------------
173 void RecordPersistentTypes(clang::DeclContext *FunDeclCtx);
175 //----------------------------------------------------------------------
176 /// Given a TypeDecl, if it declares a type whose name starts with a
177 /// dollar sign, register it as a pointer type in the target's scratch
181 /// The body of the function.
182 //----------------------------------------------------------------------
183 void MaybeRecordPersistentType(clang::TypeDecl *D);
185 //----------------------------------------------------------------------
186 /// Given a NamedDecl, register it as a pointer type in the target's scratch
190 /// The body of the function.
191 //----------------------------------------------------------------------
192 void RecordPersistentDecl(clang::NamedDecl *D);
195 *m_ast_context; ///< The AST context to use for identifiers and types.
196 clang::ASTConsumer *m_passthrough; ///< The ASTConsumer down the chain, for
197 ///passthrough. NULL if it's a
199 clang::SemaConsumer *m_passthrough_sema; ///< The SemaConsumer down the chain,
200 ///for passthrough. NULL if it's an
203 std::vector<clang::NamedDecl *> m_decls; ///< Persistent declarations to
204 ///register assuming the expression
207 Target &m_target; ///< The target, which contains the persistent variable
209 clang::Sema *m_sema; ///< The Sema to use.
213 } // namespace lldb_private
215 #endif // liblldb_ASTResultSynthesizer_h_