Sean Callanan | 1a8d409 | 2010-08-27 01:01:44 +0000 | [diff] [blame] | 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 | |
Sean Callanan | 4dbb271 | 2015-09-25 20:35:58 +0000 | [diff] [blame] | 13 | #include "ClangExpressionVariable.h" |
| 14 | #include "ClangFunctionCaller.h" |
| 15 | |
Sean Callanan | 1a8d409 | 2010-08-27 01:01:44 +0000 | [diff] [blame] | 16 | #include "lldb/Core/ClangForward.h" |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 17 | #include "clang/Sema/SemaConsumer.h" |
Sean Callanan | 1a8d409 | 2010-08-27 01:01:44 +0000 | [diff] [blame] | 18 | |
| 19 | namespace lldb_private { |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 20 | |
Sean Callanan | 1a8d409 | 2010-08-27 01:01:44 +0000 | [diff] [blame] | 21 | //---------------------------------------------------------------------- |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 22 | /// @class ASTStructExtractor ASTStructExtractor.h |
Adrian Prantl | d8f460e | 2018-05-02 16:55:16 +0000 | [diff] [blame] | 23 | /// "lldb/Expression/ASTStructExtractor.h" Extracts and describes the argument |
| 24 | /// structure for a wrapped function. |
Sean Callanan | 1a8d409 | 2010-08-27 01:01:44 +0000 | [diff] [blame] | 25 | /// |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 26 | /// This pass integrates with ClangFunctionCaller, which calls functions with |
Adrian Prantl | d8f460e | 2018-05-02 16:55:16 +0000 | [diff] [blame] | 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. |
Sean Callanan | 1a8d409 | 2010-08-27 01:01:44 +0000 | [diff] [blame] | 32 | /// |
Adrian Prantl | d8f460e | 2018-05-02 16:55:16 +0000 | [diff] [blame] | 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. |
Sean Callanan | 1a8d409 | 2010-08-27 01:01:44 +0000 | [diff] [blame] | 36 | //---------------------------------------------------------------------- |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 37 | class ASTStructExtractor : public clang::SemaConsumer { |
Sean Callanan | 1a8d409 | 2010-08-27 01:01:44 +0000 | [diff] [blame] | 38 | public: |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 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 | //---------------------------------------------------------------------- |
Adrian Prantl | d8f460e | 2018-05-02 16:55:16 +0000 | [diff] [blame] | 75 | /// Examine a list of Decls to find the function $__lldb_expr and transform |
| 76 | /// its code |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 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; |
Pavel Labath | 083645b | 2015-08-18 09:18:19 +0000 | [diff] [blame] | 123 | |
Sean Callanan | 1a8d409 | 2010-08-27 01:01:44 +0000 | [diff] [blame] | 124 | private: |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 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. |
Sean Callanan | 1a8d409 | 2010-08-27 01:01:44 +0000 | [diff] [blame] | 159 | }; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 160 | |
Pavel Labath | 083645b | 2015-08-18 09:18:19 +0000 | [diff] [blame] | 161 | } // namespace lldb_private |
Sean Callanan | 1a8d409 | 2010-08-27 01:01:44 +0000 | [diff] [blame] | 162 | |
Pavel Labath | 083645b | 2015-08-18 09:18:19 +0000 | [diff] [blame] | 163 | #endif // liblldb_ASTStructExtractor_h_ |