1 //===-- ASTStructExtractor.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_ASTStructExtractor_h_
11 #define liblldb_ASTStructExtractor_h_
13 #include "ClangExpressionVariable.h"
14 #include "ClangFunctionCaller.h"
16 #include "lldb/Core/ClangForward.h"
17 #include "clang/Sema/SemaConsumer.h"
19 namespace lldb_private {
21 //----------------------------------------------------------------------
22 /// @class ASTStructExtractor ASTStructExtractor.h
23 /// "lldb/Expression/ASTStructExtractor.h"
24 /// @brief Extracts and describes the argument structure for a wrapped function.
26 /// This pass integrates with ClangFunctionCaller, which calls functions with
28 /// sets of arguments. To avoid having to implement the full calling convention
29 /// for the target's architecture, ClangFunctionCaller writes a simple wrapper
30 /// function that takes a pointer to an argument structure that contains room
31 /// for the address of the function to be called, the values of all its
32 /// arguments, and room for the function's return value.
34 /// The definition of this struct is itself in the body of the wrapper function,
35 /// so Clang does the structure layout itself. ASTStructExtractor reads through
36 /// the AST for the wrapper function and finds the struct.
37 //----------------------------------------------------------------------
38 class ASTStructExtractor : public clang::SemaConsumer {
40 //----------------------------------------------------------------------
43 /// @param[in] passthrough
44 /// Since the ASTs must typically go through to the Clang code generator
45 /// in order to produce LLVM IR, this SemaConsumer must allow them to
46 /// pass to the next step in the chain after processing. Passthrough is
47 /// the next ASTConsumer, or NULL if none is required.
49 /// @param[in] struct_name
50 /// The name of the structure to extract from the wrapper function.
52 /// @param[in] function
53 /// The caller object whose members should be populated with information
54 /// about the argument struct. ClangFunctionCaller friends
55 /// ASTStructExtractor
57 //----------------------------------------------------------------------
58 ASTStructExtractor(clang::ASTConsumer *passthrough, const char *struct_name,
59 ClangFunctionCaller &function);
61 //----------------------------------------------------------------------
63 //----------------------------------------------------------------------
64 ~ASTStructExtractor() override;
66 //----------------------------------------------------------------------
67 /// Link this consumer with a particular AST context
69 /// @param[in] Context
70 /// This AST context will be used for types and identifiers, and also
71 /// forwarded to the passthrough consumer, if one exists.
72 //----------------------------------------------------------------------
73 void Initialize(clang::ASTContext &Context) override;
75 //----------------------------------------------------------------------
76 /// Examine a list of Decls to find the function $__lldb_expr and
77 /// transform its code
80 /// The list of Decls to search. These may contain LinkageSpecDecls,
81 /// which need to be searched recursively. That job falls to
82 /// TransformTopLevelDecl.
83 //----------------------------------------------------------------------
84 bool HandleTopLevelDecl(clang::DeclGroupRef D) override;
86 //----------------------------------------------------------------------
88 //----------------------------------------------------------------------
89 void HandleTranslationUnit(clang::ASTContext &Ctx) override;
91 //----------------------------------------------------------------------
93 //----------------------------------------------------------------------
94 void HandleTagDeclDefinition(clang::TagDecl *D) override;
96 //----------------------------------------------------------------------
98 //----------------------------------------------------------------------
99 void CompleteTentativeDefinition(clang::VarDecl *D) override;
101 //----------------------------------------------------------------------
103 //----------------------------------------------------------------------
104 void HandleVTable(clang::CXXRecordDecl *RD) override;
106 //----------------------------------------------------------------------
108 //----------------------------------------------------------------------
109 void PrintStats() override;
111 //----------------------------------------------------------------------
112 /// Set the Sema object to use when performing transforms, and pass it on
115 /// The Sema to use. Because Sema isn't externally visible, this class
116 /// casts it to an Action for actual use.
117 //----------------------------------------------------------------------
118 void InitializeSema(clang::Sema &S) override;
120 //----------------------------------------------------------------------
121 /// Reset the Sema to NULL now that transformations are done
122 //----------------------------------------------------------------------
123 void ForgetSema() override;
126 //----------------------------------------------------------------------
127 /// Hunt the given FunctionDecl for the argument struct and place
128 /// information about it into m_function
131 /// The FunctionDecl to hunt.
132 //----------------------------------------------------------------------
133 void ExtractFromFunctionDecl(clang::FunctionDecl *F);
135 //----------------------------------------------------------------------
136 /// Hunt the given Decl for FunctionDecls named the same as the wrapper
137 /// function name, recursing as necessary through LinkageSpecDecls, and
138 /// calling ExtractFromFunctionDecl on anything that was found
141 /// The Decl to hunt.
142 //----------------------------------------------------------------------
143 void ExtractFromTopLevelDecl(clang::Decl *D);
146 *m_ast_context; ///< The AST context to use for identifiers and types.
147 clang::ASTConsumer *m_passthrough; ///< The ASTConsumer down the chain, for
148 ///passthrough. NULL if it's a
150 clang::SemaConsumer *m_passthrough_sema; ///< The SemaConsumer down the chain,
151 ///for passthrough. NULL if it's an
153 clang::Sema *m_sema; ///< The Sema to use.
155 *m_action; ///< The Sema to use, cast to an Action so it's usable.
157 ClangFunctionCaller &m_function; ///< The function to populate with
158 ///information about the argument structure.
159 std::string m_struct_name; ///< The name of the structure to extract.
162 } // namespace lldb_private
164 #endif // liblldb_ASTStructExtractor_h_