1 //===-- 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 13 #include "ClangExpressionVariable.h" 14 #include "ClangFunctionCaller.h" 15 16 #include "lldb/Core/ClangForward.h" 17 #include "clang/Sema/SemaConsumer.h" 18 19 namespace lldb_private { 20 21 //---------------------------------------------------------------------- 22 /// @class ASTStructExtractor ASTStructExtractor.h 23 /// "lldb/Expression/ASTStructExtractor.h" 24 /// @brief Extracts and describes the argument structure for a wrapped function. 25 /// 26 /// This pass integrates with ClangFunctionCaller, which calls functions with 27 /// custom 28 /// sets of arguments. To avoid having to implement the full calling convention 29 /// for the target's architecture, ClangFunctionCaller writes a simple wrapper 30 /// 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 36 /// the AST for the wrapper function and finds the struct. 37 //---------------------------------------------------------------------- 38 class ASTStructExtractor : public clang::SemaConsumer { 39 public: 40 //---------------------------------------------------------------------- 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; 124 125 private: 126 //---------------------------------------------------------------------- 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. 160 }; 161 162 } // namespace lldb_private 163 164 #endif // liblldb_ASTStructExtractor_h_ 165