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" Extracts and describes the argument
24 /// structure for a wrapped function.
26 /// This pass integrates with ClangFunctionCaller, which calls functions with
27 /// custom sets of arguments. To avoid having to implement the full calling
28 /// convention for the target's architecture, ClangFunctionCaller writes a
29 /// simple wrapper function that takes a pointer to an argument structure that
30 /// contains room for the address of the function to be called, the values of
31 /// all its arguments, and room for the function's return value.
33 /// The definition of this struct is itself in the body of the wrapper
34 /// function, so Clang does the structure layout itself. ASTStructExtractor
35 /// reads through the AST for the wrapper function and finds the struct.
36 //----------------------------------------------------------------------
37 class ASTStructExtractor : public clang::SemaConsumer {
39 //----------------------------------------------------------------------
42 /// @param[in] passthrough
43 /// Since the ASTs must typically go through to the Clang code generator
44 /// in order to produce LLVM IR, this SemaConsumer must allow them to
45 /// pass to the next step in the chain after processing. Passthrough is
46 /// the next ASTConsumer, or NULL if none is required.
48 /// @param[in] struct_name
49 /// The name of the structure to extract from the wrapper function.
51 /// @param[in] function
52 /// The caller object whose members should be populated with information
53 /// about the argument struct. ClangFunctionCaller friends
54 /// ASTStructExtractor
56 //----------------------------------------------------------------------
57 ASTStructExtractor(clang::ASTConsumer *passthrough, const char *struct_name,
58 ClangFunctionCaller &function);
60 //----------------------------------------------------------------------
62 //----------------------------------------------------------------------
63 ~ASTStructExtractor() override;
65 //----------------------------------------------------------------------
66 /// Link this consumer with a particular AST context
68 /// @param[in] Context
69 /// This AST context will be used for types and identifiers, and also
70 /// forwarded to the passthrough consumer, if one exists.
71 //----------------------------------------------------------------------
72 void Initialize(clang::ASTContext &Context) override;
74 //----------------------------------------------------------------------
75 /// Examine a list of Decls to find the function $__lldb_expr and transform
79 /// The list of Decls to search. These may contain LinkageSpecDecls,
80 /// which need to be searched recursively. That job falls to
81 /// TransformTopLevelDecl.
82 //----------------------------------------------------------------------
83 bool HandleTopLevelDecl(clang::DeclGroupRef D) override;
85 //----------------------------------------------------------------------
87 //----------------------------------------------------------------------
88 void HandleTranslationUnit(clang::ASTContext &Ctx) override;
90 //----------------------------------------------------------------------
92 //----------------------------------------------------------------------
93 void HandleTagDeclDefinition(clang::TagDecl *D) override;
95 //----------------------------------------------------------------------
97 //----------------------------------------------------------------------
98 void CompleteTentativeDefinition(clang::VarDecl *D) override;
100 //----------------------------------------------------------------------
102 //----------------------------------------------------------------------
103 void HandleVTable(clang::CXXRecordDecl *RD) override;
105 //----------------------------------------------------------------------
107 //----------------------------------------------------------------------
108 void PrintStats() override;
110 //----------------------------------------------------------------------
111 /// Set the Sema object to use when performing transforms, and pass it on
114 /// The Sema to use. Because Sema isn't externally visible, this class
115 /// casts it to an Action for actual use.
116 //----------------------------------------------------------------------
117 void InitializeSema(clang::Sema &S) override;
119 //----------------------------------------------------------------------
120 /// Reset the Sema to NULL now that transformations are done
121 //----------------------------------------------------------------------
122 void ForgetSema() override;
125 //----------------------------------------------------------------------
126 /// Hunt the given FunctionDecl for the argument struct and place
127 /// information about it into m_function
130 /// The FunctionDecl to hunt.
131 //----------------------------------------------------------------------
132 void ExtractFromFunctionDecl(clang::FunctionDecl *F);
134 //----------------------------------------------------------------------
135 /// Hunt the given Decl for FunctionDecls named the same as the wrapper
136 /// function name, recursing as necessary through LinkageSpecDecls, and
137 /// calling ExtractFromFunctionDecl on anything that was found
140 /// The Decl to hunt.
141 //----------------------------------------------------------------------
142 void ExtractFromTopLevelDecl(clang::Decl *D);
145 *m_ast_context; ///< The AST context to use for identifiers and types.
146 clang::ASTConsumer *m_passthrough; ///< The ASTConsumer down the chain, for
147 ///passthrough. NULL if it's a
149 clang::SemaConsumer *m_passthrough_sema; ///< The SemaConsumer down the chain,
150 ///for passthrough. NULL if it's an
152 clang::Sema *m_sema; ///< The Sema to use.
154 *m_action; ///< The Sema to use, cast to an Action so it's usable.
156 ClangFunctionCaller &m_function; ///< The function to populate with
157 ///information about the argument structure.
158 std::string m_struct_name; ///< The name of the structure to extract.
161 } // namespace lldb_private
163 #endif // liblldb_ASTStructExtractor_h_