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