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" Extracts and describes the argument 24 /// structure for a wrapped function. 25 /// 26 /// This pass integrates with ClangFunctionCaller, which calls functions with 27 /// 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. 32 /// 33 /// 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. 36 //---------------------------------------------------------------------- 37 class ASTStructExtractor : public clang::SemaConsumer { 38 public: 39 //---------------------------------------------------------------------- 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 //---------------------------------------------------------------------- 75 /// Examine a list of Decls to find the function $__lldb_expr and transform 76 /// its code 77 /// 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; 123 124 private: 125 //---------------------------------------------------------------------- 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. 159 }; 160 161 } // namespace lldb_private 162 163 #endif // liblldb_ASTStructExtractor_h_ 164