1 //===-- ASTStructExtractor.h ------------------------------------*- C++ -*-===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
9 #ifndef liblldb_ASTStructExtractor_h_
10 #define liblldb_ASTStructExtractor_h_
12 #include "ClangExpressionVariable.h"
13 #include "ClangFunctionCaller.h"
15 #include "lldb/Core/ClangForward.h"
16 #include "clang/Sema/SemaConsumer.h"
18 namespace lldb_private {
20 /// \class ASTStructExtractor ASTStructExtractor.h
21 /// "lldb/Expression/ASTStructExtractor.h" Extracts and describes the argument
22 /// structure for a wrapped function.
24 /// This pass integrates with ClangFunctionCaller, which calls functions with
25 /// custom sets of arguments. To avoid having to implement the full calling
26 /// convention for the target's architecture, ClangFunctionCaller writes a
27 /// simple wrapper function that takes a pointer to an argument structure that
28 /// contains room for the address of the function to be called, the values of
29 /// all its arguments, and room for the function's return value.
31 /// The definition of this struct is itself in the body of the wrapper
32 /// function, so Clang does the structure layout itself. ASTStructExtractor
33 /// reads through the AST for the wrapper function and finds the struct.
34 class ASTStructExtractor : public clang::SemaConsumer {
38 /// \param[in] passthrough
39 /// Since the ASTs must typically go through to the Clang code generator
40 /// in order to produce LLVM IR, this SemaConsumer must allow them to
41 /// pass to the next step in the chain after processing. Passthrough is
42 /// the next ASTConsumer, or NULL if none is required.
44 /// \param[in] struct_name
45 /// The name of the structure to extract from the wrapper function.
47 /// \param[in] function
48 /// The caller object whose members should be populated with information
49 /// about the argument struct. ClangFunctionCaller friends
50 /// ASTStructExtractor
52 ASTStructExtractor(clang::ASTConsumer *passthrough, const char *struct_name,
53 ClangFunctionCaller &function);
56 ~ASTStructExtractor() override;
58 /// Link this consumer with a particular AST context
60 /// \param[in] Context
61 /// This AST context will be used for types and identifiers, and also
62 /// forwarded to the passthrough consumer, if one exists.
63 void Initialize(clang::ASTContext &Context) override;
65 /// Examine a list of Decls to find the function $__lldb_expr and transform
69 /// The list of Decls to search. These may contain LinkageSpecDecls,
70 /// which need to be searched recursively. That job falls to
71 /// TransformTopLevelDecl.
72 bool HandleTopLevelDecl(clang::DeclGroupRef D) override;
75 void HandleTranslationUnit(clang::ASTContext &Ctx) override;
78 void HandleTagDeclDefinition(clang::TagDecl *D) override;
81 void CompleteTentativeDefinition(clang::VarDecl *D) override;
84 void HandleVTable(clang::CXXRecordDecl *RD) override;
87 void PrintStats() override;
89 /// Set the Sema object to use when performing transforms, and pass it on
92 /// The Sema to use. Because Sema isn't externally visible, this class
93 /// casts it to an Action for actual use.
94 void InitializeSema(clang::Sema &S) override;
96 /// Reset the Sema to NULL now that transformations are done
97 void ForgetSema() override;
100 /// Hunt the given FunctionDecl for the argument struct and place
101 /// information about it into m_function
104 /// The FunctionDecl to hunt.
105 void ExtractFromFunctionDecl(clang::FunctionDecl *F);
107 /// Hunt the given Decl for FunctionDecls named the same as the wrapper
108 /// function name, recursing as necessary through LinkageSpecDecls, and
109 /// calling ExtractFromFunctionDecl on anything that was found
112 /// The Decl to hunt.
113 void ExtractFromTopLevelDecl(clang::Decl *D);
116 *m_ast_context; ///< The AST context to use for identifiers and types.
117 clang::ASTConsumer *m_passthrough; ///< The ASTConsumer down the chain, for
118 ///passthrough. NULL if it's a
120 clang::SemaConsumer *m_passthrough_sema; ///< The SemaConsumer down the chain,
121 ///for passthrough. NULL if it's an
123 clang::Sema *m_sema; ///< The Sema to use.
125 ClangFunctionCaller &m_function; ///< The function to populate with
126 ///information about the argument structure.
127 std::string m_struct_name; ///< The name of the structure to extract.
130 } // namespace lldb_private
132 #endif // liblldb_ASTStructExtractor_h_