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 "clang/Sema/SemaConsumer.h"
14 #include "lldb/Core/ClangForward.h"
15 #include "lldb/Symbol/TaggedASTType.h"
17 namespace lldb_private {
19 //----------------------------------------------------------------------
20 /// @class ASTResultSynthesizer ASTResultSynthesizer.h "lldb/Expression/ASTResultSynthesizer.h"
21 /// @brief Adds a result variable declaration to the ASTs for an expression.
23 /// Users expect the expression "i + 3" to return a result, even if a result
24 /// variable wasn't specifically declared. To fulfil this requirement, LLDB adds
25 /// a result variable to the expression, transforming it to
26 /// "int $__lldb_expr_result = i + 3." The IR transformers ensure that the
27 /// resulting variable is mapped to the right piece of memory.
28 /// ASTResultSynthesizer's job is to add the variable and its initialization to
29 /// the ASTs for the expression, and it does so by acting as a SemaConsumer for
31 //----------------------------------------------------------------------
32 class ASTResultSynthesizer : public clang::SemaConsumer
35 //----------------------------------------------------------------------
38 /// @param[in] passthrough
39 /// Since the ASTs must typically go through to the Clang code generator
40 /// in order to produce LLVM IR, this SemaConsumer must allow them to
41 /// pass to the next step in the chain after processing. Passthrough is
42 /// the next ASTConsumer, or NULL if none is required.
45 /// The target, which contains the persistent variable store and the
47 //----------------------------------------------------------------------
48 ASTResultSynthesizer(clang::ASTConsumer *passthrough,
51 //----------------------------------------------------------------------
53 //----------------------------------------------------------------------
54 ~ASTResultSynthesizer();
56 //----------------------------------------------------------------------
57 /// Link this consumer with a particular AST context
59 /// @param[in] Context
60 /// This AST context will be used for types and identifiers, and also
61 /// forwarded to the passthrough consumer, if one exists.
62 //----------------------------------------------------------------------
63 void Initialize(clang::ASTContext &Context);
65 //----------------------------------------------------------------------
66 /// Examine a list of Decls to find the function $__lldb_expr and
67 /// transform its code
70 /// The list of Decls to search. These may contain LinkageSpecDecls,
71 /// which need to be searched recursively. That job falls to
72 /// TransformTopLevelDecl.
73 //----------------------------------------------------------------------
74 bool HandleTopLevelDecl(clang::DeclGroupRef D);
76 //----------------------------------------------------------------------
78 //----------------------------------------------------------------------
79 void HandleTranslationUnit(clang::ASTContext &Ctx);
81 //----------------------------------------------------------------------
83 //----------------------------------------------------------------------
84 void HandleTagDeclDefinition(clang::TagDecl *D);
86 //----------------------------------------------------------------------
88 //----------------------------------------------------------------------
89 void CompleteTentativeDefinition(clang::VarDecl *D);
91 //----------------------------------------------------------------------
93 //----------------------------------------------------------------------
94 void HandleVTable(clang::CXXRecordDecl *RD, bool DefinitionRequired);
96 //----------------------------------------------------------------------
98 //----------------------------------------------------------------------
101 //----------------------------------------------------------------------
102 /// Set the Sema object to use when performing transforms, and pass it on
105 /// The Sema to use. Because Sema isn't externally visible, this class
106 /// casts it to an Action for actual use.
107 //----------------------------------------------------------------------
108 void InitializeSema(clang::Sema &S);
110 //----------------------------------------------------------------------
111 /// Reset the Sema to NULL now that transformations are done
112 //----------------------------------------------------------------------
115 //----------------------------------------------------------------------
116 /// Hunt the given Decl for FunctionDecls named $__lldb_expr, recursing
117 /// as necessary through LinkageSpecDecls, and calling SynthesizeResult on
118 /// anything that was found
121 /// The Decl to hunt.
122 //----------------------------------------------------------------------
123 void TransformTopLevelDecl(clang::Decl *D);
125 //----------------------------------------------------------------------
126 /// Process an Objective-C method and produce the result variable and
129 /// @param[in] MethodDecl
130 /// The method to process.
131 //----------------------------------------------------------------------
132 bool SynthesizeObjCMethodResult(clang::ObjCMethodDecl *MethodDecl);
134 //----------------------------------------------------------------------
135 /// Process a function and produce the result variable and initialization
137 /// @param[in] FunDecl
138 /// The function to process.
139 //----------------------------------------------------------------------
140 bool SynthesizeFunctionResult(clang::FunctionDecl *FunDecl);
142 //----------------------------------------------------------------------
143 /// Process a function body and produce the result variable and
147 /// The body of the function.
150 /// The DeclContext of the function, into which the result variable
152 //----------------------------------------------------------------------
153 bool SynthesizeBodyResult(clang::CompoundStmt *Body,
154 clang::DeclContext *DC);
156 //----------------------------------------------------------------------
157 /// Given a DeclContext for a function or method, find all types
158 /// declared in the context and record any persistent types found.
160 /// @param[in] FunDeclCtx
161 /// The context for the function to process.
162 //----------------------------------------------------------------------
163 void RecordPersistentTypes(clang::DeclContext *FunDeclCtx);
165 //----------------------------------------------------------------------
166 /// Given a TypeDecl, if it declares a type whose name starts with a
167 /// dollar sign, register it as a pointer type in the target's scratch
171 /// The body of the function.
172 //----------------------------------------------------------------------
173 void MaybeRecordPersistentType(clang::TypeDecl *D);
175 clang::ASTContext *m_ast_context; ///< The AST context to use for identifiers and types.
176 clang::ASTConsumer *m_passthrough; ///< The ASTConsumer down the chain, for passthrough. NULL if it's a SemaConsumer.
177 clang::SemaConsumer *m_passthrough_sema; ///< The SemaConsumer down the chain, for passthrough. NULL if it's an ASTConsumer.
178 Target &m_target; ///< The target, which contains the persistent variable store and the
179 clang::Sema *m_sema; ///< The Sema to use.