blob: 63e3161cae85aa754aee29fe48030b10511d4817 [file] [log] [blame]
Sean Callanan1a8d4092010-08-27 01:01:44 +00001//===-- ASTStructExtractor.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_ASTStructExtractor_h_
11#define liblldb_ASTStructExtractor_h_
12
Sean Callanan4dbb2712015-09-25 20:35:58 +000013#include "ClangExpressionVariable.h"
14#include "ClangFunctionCaller.h"
15
Sean Callanan1a8d4092010-08-27 01:01:44 +000016#include "lldb/Core/ClangForward.h"
Kate Stoneb9c1b512016-09-06 20:57:50 +000017#include "clang/Sema/SemaConsumer.h"
Sean Callanan1a8d4092010-08-27 01:01:44 +000018
19namespace lldb_private {
Kate Stoneb9c1b512016-09-06 20:57:50 +000020
Sean Callanan1a8d4092010-08-27 01:01:44 +000021//----------------------------------------------------------------------
Kate Stoneb9c1b512016-09-06 20:57:50 +000022/// @class ASTStructExtractor ASTStructExtractor.h
23/// "lldb/Expression/ASTStructExtractor.h"
Sean Callanan1a8d4092010-08-27 01:01:44 +000024/// @brief Extracts and describes the argument structure for a wrapped function.
25///
Kate Stoneb9c1b512016-09-06 20:57:50 +000026/// This pass integrates with ClangFunctionCaller, which calls functions with
27/// custom
Sean Callanan1a8d4092010-08-27 01:01:44 +000028/// sets of arguments. To avoid having to implement the full calling convention
Jim Ingham151c0322015-09-15 21:13:50 +000029/// for the target's architecture, ClangFunctionCaller writes a simple wrapper
Sean Callanan1a8d4092010-08-27 01:01:44 +000030/// 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.
33///
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
Bruce Mitchenerd93c4a32014-07-01 21:22:11 +000036/// the AST for the wrapper function and finds the struct.
Sean Callanan1a8d4092010-08-27 01:01:44 +000037//----------------------------------------------------------------------
Kate Stoneb9c1b512016-09-06 20:57:50 +000038class ASTStructExtractor : public clang::SemaConsumer {
Sean Callanan1a8d4092010-08-27 01:01:44 +000039public:
Kate Stoneb9c1b512016-09-06 20:57:50 +000040 //----------------------------------------------------------------------
41 /// Constructor
42 ///
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.
48 ///
49 /// @param[in] struct_name
50 /// The name of the structure to extract from the wrapper function.
51 ///
52 /// @param[in] function
53 /// The caller object whose members should be populated with information
54 /// about the argument struct. ClangFunctionCaller friends
55 /// ASTStructExtractor
56 /// for this purpose.
57 //----------------------------------------------------------------------
58 ASTStructExtractor(clang::ASTConsumer *passthrough, const char *struct_name,
59 ClangFunctionCaller &function);
60
61 //----------------------------------------------------------------------
62 /// Destructor
63 //----------------------------------------------------------------------
64 ~ASTStructExtractor() override;
65
66 //----------------------------------------------------------------------
67 /// Link this consumer with a particular AST context
68 ///
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;
74
75 //----------------------------------------------------------------------
76 /// Examine a list of Decls to find the function $__lldb_expr and
77 /// transform its code
78 ///
79 /// @param[in] D
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;
85
86 //----------------------------------------------------------------------
87 /// Passthrough stub
88 //----------------------------------------------------------------------
89 void HandleTranslationUnit(clang::ASTContext &Ctx) override;
90
91 //----------------------------------------------------------------------
92 /// Passthrough stub
93 //----------------------------------------------------------------------
94 void HandleTagDeclDefinition(clang::TagDecl *D) override;
95
96 //----------------------------------------------------------------------
97 /// Passthrough stub
98 //----------------------------------------------------------------------
99 void CompleteTentativeDefinition(clang::VarDecl *D) override;
100
101 //----------------------------------------------------------------------
102 /// Passthrough stub
103 //----------------------------------------------------------------------
104 void HandleVTable(clang::CXXRecordDecl *RD) override;
105
106 //----------------------------------------------------------------------
107 /// Passthrough stub
108 //----------------------------------------------------------------------
109 void PrintStats() override;
110
111 //----------------------------------------------------------------------
112 /// Set the Sema object to use when performing transforms, and pass it on
113 ///
114 /// @param[in] S
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;
119
120 //----------------------------------------------------------------------
121 /// Reset the Sema to NULL now that transformations are done
122 //----------------------------------------------------------------------
123 void ForgetSema() override;
Pavel Labath083645b2015-08-18 09:18:19 +0000124
Sean Callanan1a8d4092010-08-27 01:01:44 +0000125private:
Kate Stoneb9c1b512016-09-06 20:57:50 +0000126 //----------------------------------------------------------------------
127 /// Hunt the given FunctionDecl for the argument struct and place
128 /// information about it into m_function
129 ///
130 /// @param[in] F
131 /// The FunctionDecl to hunt.
132 //----------------------------------------------------------------------
133 void ExtractFromFunctionDecl(clang::FunctionDecl *F);
134
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
139 ///
140 /// @param[in] D
141 /// The Decl to hunt.
142 //----------------------------------------------------------------------
143 void ExtractFromTopLevelDecl(clang::Decl *D);
144
145 clang::ASTContext
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
149 ///SemaConsumer.
150 clang::SemaConsumer *m_passthrough_sema; ///< The SemaConsumer down the chain,
151 ///for passthrough. NULL if it's an
152 ///ASTConsumer.
153 clang::Sema *m_sema; ///< The Sema to use.
154 clang::Action
155 *m_action; ///< The Sema to use, cast to an Action so it's usable.
156
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.
Sean Callanan1a8d4092010-08-27 01:01:44 +0000160};
Kate Stoneb9c1b512016-09-06 20:57:50 +0000161
Pavel Labath083645b2015-08-18 09:18:19 +0000162} // namespace lldb_private
Sean Callanan1a8d4092010-08-27 01:01:44 +0000163
Pavel Labath083645b2015-08-18 09:18:19 +0000164#endif // liblldb_ASTStructExtractor_h_