]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/llvm/tools/lldb/source/Plugins/ExpressionParser/Clang/ASTResultSynthesizer.h
Import DTS files from Linux 4.18
[FreeBSD/FreeBSD.git] / contrib / llvm / tools / lldb / source / Plugins / ExpressionParser / Clang / ASTResultSynthesizer.h
1 //===-- ASTResultSynthesizer.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_ASTResultSynthesizer_h_
11 #define liblldb_ASTResultSynthesizer_h_
12
13 #include "lldb/Core/ClangForward.h"
14 #include "lldb/Target/Target.h"
15 #include "clang/Sema/SemaConsumer.h"
16
17 namespace lldb_private {
18
19 //----------------------------------------------------------------------
20 /// @class ASTResultSynthesizer ASTResultSynthesizer.h
21 /// "lldb/Expression/ASTResultSynthesizer.h"
22 /// @brief Adds a result variable declaration to the ASTs for an expression.
23 ///
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
26 /// adds
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
32 /// Clang.
33 //----------------------------------------------------------------------
34 class ASTResultSynthesizer : public clang::SemaConsumer {
35 public:
36   //----------------------------------------------------------------------
37   /// Constructor
38   ///
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.
44   ///
45   /// @param[in] top_level
46   ///     If true, register all top-level Decls and don't try to handle the
47   ///     main function.
48   ///
49   /// @param[in] target
50   ///     The target, which contains the persistent variable store and the
51   ///     AST importer.
52   //----------------------------------------------------------------------
53   ASTResultSynthesizer(clang::ASTConsumer *passthrough, bool top_level,
54                        Target &target);
55
56   //----------------------------------------------------------------------
57   /// Destructor
58   //----------------------------------------------------------------------
59   ~ASTResultSynthesizer() override;
60
61   //----------------------------------------------------------------------
62   /// Link this consumer with a particular AST context
63   ///
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;
69
70   //----------------------------------------------------------------------
71   /// Examine a list of Decls to find the function $__lldb_expr and
72   /// transform its code
73   ///
74   /// @param[in] D
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;
80
81   //----------------------------------------------------------------------
82   /// Passthrough stub
83   //----------------------------------------------------------------------
84   void HandleTranslationUnit(clang::ASTContext &Ctx) override;
85
86   //----------------------------------------------------------------------
87   /// Passthrough stub
88   //----------------------------------------------------------------------
89   void HandleTagDeclDefinition(clang::TagDecl *D) override;
90
91   //----------------------------------------------------------------------
92   /// Passthrough stub
93   //----------------------------------------------------------------------
94   void CompleteTentativeDefinition(clang::VarDecl *D) override;
95
96   //----------------------------------------------------------------------
97   /// Passthrough stub
98   //----------------------------------------------------------------------
99   void HandleVTable(clang::CXXRecordDecl *RD) override;
100
101   //----------------------------------------------------------------------
102   /// Passthrough stub
103   //----------------------------------------------------------------------
104   void PrintStats() override;
105
106   //----------------------------------------------------------------------
107   /// Set the Sema object to use when performing transforms, and pass it on
108   ///
109   /// @param[in] S
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;
114
115   //----------------------------------------------------------------------
116   /// Reset the Sema to NULL now that transformations are done
117   //----------------------------------------------------------------------
118   void ForgetSema() override;
119
120   //----------------------------------------------------------------------
121   /// The parse has succeeded, so record its persistent decls
122   //----------------------------------------------------------------------
123   void CommitPersistentDecls();
124
125 private:
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
130   ///
131   /// @param[in] D
132   ///     The Decl to hunt.
133   //----------------------------------------------------------------------
134   void TransformTopLevelDecl(clang::Decl *D);
135
136   //----------------------------------------------------------------------
137   /// Process an Objective-C method and produce the result variable and
138   /// initialization
139   ///
140   /// @param[in] MethodDecl
141   ///     The method to process.
142   //----------------------------------------------------------------------
143   bool SynthesizeObjCMethodResult(clang::ObjCMethodDecl *MethodDecl);
144
145   //----------------------------------------------------------------------
146   /// Process a function and produce the result variable and initialization
147   ///
148   /// @param[in] FunDecl
149   ///     The function to process.
150   //----------------------------------------------------------------------
151   bool SynthesizeFunctionResult(clang::FunctionDecl *FunDecl);
152
153   //----------------------------------------------------------------------
154   /// Process a function body and produce the result variable and
155   /// initialization
156   ///
157   /// @param[in] Body
158   ///     The body of the function.
159   ///
160   /// @param[in] DC
161   ///     The DeclContext of the function, into which the result variable
162   ///     is inserted.
163   //----------------------------------------------------------------------
164   bool SynthesizeBodyResult(clang::CompoundStmt *Body, clang::DeclContext *DC);
165
166   //----------------------------------------------------------------------
167   /// Given a DeclContext for a function or method, find all types
168   /// declared in the context and record any persistent types found.
169   ///
170   /// @param[in] FunDeclCtx
171   ///     The context for the function to process.
172   //----------------------------------------------------------------------
173   void RecordPersistentTypes(clang::DeclContext *FunDeclCtx);
174
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
178   /// AST context.
179   ///
180   /// @param[in] Body
181   ///     The body of the function.
182   //----------------------------------------------------------------------
183   void MaybeRecordPersistentType(clang::TypeDecl *D);
184
185   //----------------------------------------------------------------------
186   /// Given a NamedDecl, register it as a pointer type in the target's scratch
187   /// AST context.
188   ///
189   /// @param[in] Body
190   ///     The body of the function.
191   //----------------------------------------------------------------------
192   void RecordPersistentDecl(clang::NamedDecl *D);
193
194   clang::ASTContext
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
198                                      ///SemaConsumer.
199   clang::SemaConsumer *m_passthrough_sema; ///< The SemaConsumer down the chain,
200                                            ///for passthrough.  NULL if it's an
201                                            ///ASTConsumer.
202
203   std::vector<clang::NamedDecl *> m_decls; ///< Persistent declarations to
204                                            ///register assuming the expression
205                                            ///succeeds.
206
207   Target &m_target;    ///< The target, which contains the persistent variable
208                        ///store and the
209   clang::Sema *m_sema; ///< The Sema to use.
210   bool m_top_level;
211 };
212
213 } // namespace lldb_private
214
215 #endif // liblldb_ASTResultSynthesizer_h_