blob: 65f4b00a865120f2a31f901946a4c64d856f497d [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
Adrian Prantld8f460e2018-05-02 16:55:16 +000023/// "lldb/Expression/ASTStructExtractor.h" Extracts and describes the argument
24/// structure for a wrapped function.
Sean Callanan1a8d4092010-08-27 01:01:44 +000025///
Kate Stoneb9c1b512016-09-06 20:57:50 +000026/// This pass integrates with ClangFunctionCaller, which calls functions with
Adrian Prantld8f460e2018-05-02 16:55:16 +000027/// 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.
Sean Callanan1a8d4092010-08-27 01:01:44 +000032///
Adrian Prantld8f460e2018-05-02 16:55:16 +000033/// 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.
Sean Callanan1a8d4092010-08-27 01:01:44 +000036//----------------------------------------------------------------------
Kate Stoneb9c1b512016-09-06 20:57:50 +000037class ASTStructExtractor : public clang::SemaConsumer {
Sean Callanan1a8d4092010-08-27 01:01:44 +000038public:
Kate Stoneb9c1b512016-09-06 20:57:50 +000039 //----------------------------------------------------------------------
40 /// Constructor
41 ///
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.
47 ///
48 /// @param[in] struct_name
49 /// The name of the structure to extract from the wrapper function.
50 ///
51 /// @param[in] function
52 /// The caller object whose members should be populated with information
53 /// about the argument struct. ClangFunctionCaller friends
54 /// ASTStructExtractor
55 /// for this purpose.
56 //----------------------------------------------------------------------
57 ASTStructExtractor(clang::ASTConsumer *passthrough, const char *struct_name,
58 ClangFunctionCaller &function);
59
60 //----------------------------------------------------------------------
61 /// Destructor
62 //----------------------------------------------------------------------
63 ~ASTStructExtractor() override;
64
65 //----------------------------------------------------------------------
66 /// Link this consumer with a particular AST context
67 ///
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;
73
74 //----------------------------------------------------------------------
Adrian Prantld8f460e2018-05-02 16:55:16 +000075 /// Examine a list of Decls to find the function $__lldb_expr and transform
76 /// its code
Kate Stoneb9c1b512016-09-06 20:57:50 +000077 ///
78 /// @param[in] D
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;
84
85 //----------------------------------------------------------------------
86 /// Passthrough stub
87 //----------------------------------------------------------------------
88 void HandleTranslationUnit(clang::ASTContext &Ctx) override;
89
90 //----------------------------------------------------------------------
91 /// Passthrough stub
92 //----------------------------------------------------------------------
93 void HandleTagDeclDefinition(clang::TagDecl *D) override;
94
95 //----------------------------------------------------------------------
96 /// Passthrough stub
97 //----------------------------------------------------------------------
98 void CompleteTentativeDefinition(clang::VarDecl *D) override;
99
100 //----------------------------------------------------------------------
101 /// Passthrough stub
102 //----------------------------------------------------------------------
103 void HandleVTable(clang::CXXRecordDecl *RD) override;
104
105 //----------------------------------------------------------------------
106 /// Passthrough stub
107 //----------------------------------------------------------------------
108 void PrintStats() override;
109
110 //----------------------------------------------------------------------
111 /// Set the Sema object to use when performing transforms, and pass it on
112 ///
113 /// @param[in] S
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;
118
119 //----------------------------------------------------------------------
120 /// Reset the Sema to NULL now that transformations are done
121 //----------------------------------------------------------------------
122 void ForgetSema() override;
Pavel Labath083645b2015-08-18 09:18:19 +0000123
Sean Callanan1a8d4092010-08-27 01:01:44 +0000124private:
Kate Stoneb9c1b512016-09-06 20:57:50 +0000125 //----------------------------------------------------------------------
126 /// Hunt the given FunctionDecl for the argument struct and place
127 /// information about it into m_function
128 ///
129 /// @param[in] F
130 /// The FunctionDecl to hunt.
131 //----------------------------------------------------------------------
132 void ExtractFromFunctionDecl(clang::FunctionDecl *F);
133
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
138 ///
139 /// @param[in] D
140 /// The Decl to hunt.
141 //----------------------------------------------------------------------
142 void ExtractFromTopLevelDecl(clang::Decl *D);
143
144 clang::ASTContext
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
148 ///SemaConsumer.
149 clang::SemaConsumer *m_passthrough_sema; ///< The SemaConsumer down the chain,
150 ///for passthrough. NULL if it's an
151 ///ASTConsumer.
152 clang::Sema *m_sema; ///< The Sema to use.
153 clang::Action
154 *m_action; ///< The Sema to use, cast to an Action so it's usable.
155
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.
Sean Callanan1a8d4092010-08-27 01:01:44 +0000159};
Kate Stoneb9c1b512016-09-06 20:57:50 +0000160
Pavel Labath083645b2015-08-18 09:18:19 +0000161} // namespace lldb_private
Sean Callanan1a8d4092010-08-27 01:01:44 +0000162
Pavel Labath083645b2015-08-18 09:18:19 +0000163#endif // liblldb_ASTStructExtractor_h_