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 liblldb_ASTStructExtractor_h_ 10 #define liblldb_ASTStructExtractor_h_ 11 12 #include "ClangExpressionVariable.h" 13 #include "ClangFunctionCaller.h" 14 15 #include "lldb/Core/ClangForward.h" 16 #include "clang/Sema/SemaConsumer.h" 17 18 namespace lldb_private { 19 20 /// \class ASTStructExtractor ASTStructExtractor.h 21 /// "lldb/Expression/ASTStructExtractor.h" Extracts and describes the argument 22 /// structure for a wrapped function. 23 /// 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. 30 /// 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 { 35 public: 36 /// Constructor 37 /// 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. 43 /// 44 /// \param[in] struct_name 45 /// The name of the structure to extract from the wrapper function. 46 /// 47 /// \param[in] function 48 /// The caller object whose members should be populated with information 49 /// about the argument struct. ClangFunctionCaller friends 50 /// ASTStructExtractor 51 /// for this purpose. 52 ASTStructExtractor(clang::ASTConsumer *passthrough, const char *struct_name, 53 ClangFunctionCaller &function); 54 55 /// Destructor 56 ~ASTStructExtractor() override; 57 58 /// Link this consumer with a particular AST context 59 /// 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; 64 65 /// Examine a list of Decls to find the function $__lldb_expr and transform 66 /// its code 67 /// 68 /// \param[in] D 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; 73 74 /// Passthrough stub 75 void HandleTranslationUnit(clang::ASTContext &Ctx) override; 76 77 /// Passthrough stub 78 void HandleTagDeclDefinition(clang::TagDecl *D) override; 79 80 /// Passthrough stub 81 void CompleteTentativeDefinition(clang::VarDecl *D) override; 82 83 /// Passthrough stub 84 void HandleVTable(clang::CXXRecordDecl *RD) override; 85 86 /// Passthrough stub 87 void PrintStats() override; 88 89 /// Set the Sema object to use when performing transforms, and pass it on 90 /// 91 /// \param[in] S 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; 95 96 /// Reset the Sema to NULL now that transformations are done 97 void ForgetSema() override; 98 99 private: 100 /// Hunt the given FunctionDecl for the argument struct and place 101 /// information about it into m_function 102 /// 103 /// \param[in] F 104 /// The FunctionDecl to hunt. 105 void ExtractFromFunctionDecl(clang::FunctionDecl *F); 106 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 110 /// 111 /// \param[in] D 112 /// The Decl to hunt. 113 void ExtractFromTopLevelDecl(clang::Decl *D); 114 115 clang::ASTContext 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 119 ///SemaConsumer. 120 clang::SemaConsumer *m_passthrough_sema; ///< The SemaConsumer down the chain, 121 ///for passthrough. NULL if it's an 122 ///ASTConsumer. 123 clang::Sema *m_sema; ///< The Sema to use. 124 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. 128 }; 129 130 } // namespace lldb_private 131 132 #endif // liblldb_ASTStructExtractor_h_ 133