1 //===-- ASTStructExtractor.h ------------------------------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #ifndef liblldb_ASTStructExtractor_h_
11 #define liblldb_ASTStructExtractor_h_
13 #include "clang/Sema/SemaConsumer.h"
14 #include "lldb/Core/ClangForward.h"
15 #include "lldb/Expression/ClangExpressionVariable.h"
16 #include "lldb/Expression/ClangFunction.h"
18 namespace lldb_private {
20 //----------------------------------------------------------------------
21 /// @class ASTStructExtractor ASTStructExtractor.h "lldb/Expression/ASTStructExtractor.h"
22 /// @brief Extracts and describes the argument structure for a wrapped function.
24 /// This pass integrates with ClangFunction, which calls functions with custom
25 /// sets of arguments. To avoid having to implement the full calling convention
26 /// for the target's architecture, ClangFunction writes a simple wrapper
27 /// function that takes a pointer to an argument structure that contains room
28 /// for the address of the function to be called, the values of all its
29 /// arguments, and room for the function's return value.
31 /// The definition of this struct is itself in the body of the wrapper function,
32 /// so Clang does the structure layout itself. ASTStructExtractor reads through
33 /// the AST for the wrapper funtion and finds the struct.
34 //----------------------------------------------------------------------
35 class ASTStructExtractor : public clang::SemaConsumer
38 //----------------------------------------------------------------------
41 /// @param[in] passthrough
42 /// Since the ASTs must typically go through to the Clang code generator
43 /// in order to produce LLVM IR, this SemaConsumer must allow them to
44 /// pass to the next step in the chain after processing. Passthrough is
45 /// the next ASTConsumer, or NULL if none is required.
47 /// @param[in] struct_name
48 /// The name of the structure to extract from the wrapper function.
50 /// @param[in] function
51 /// The caller object whose members should be populated with information
52 /// about the argument struct. ClangFunction friends ASTStructExtractor
54 //----------------------------------------------------------------------
55 ASTStructExtractor(clang::ASTConsumer *passthrough,
56 const char *struct_name,
57 ClangFunction &function);
59 //----------------------------------------------------------------------
61 //----------------------------------------------------------------------
62 virtual ~ASTStructExtractor();
64 //----------------------------------------------------------------------
65 /// Link this consumer with a particular AST context
67 /// @param[in] Context
68 /// This AST context will be used for types and identifiers, and also
69 /// forwarded to the passthrough consumer, if one exists.
70 //----------------------------------------------------------------------
71 void Initialize(clang::ASTContext &Context);
73 //----------------------------------------------------------------------
74 /// Examine a list of Decls to find the function $__lldb_expr and
75 /// transform its code
78 /// The list of Decls to search. These may contain LinkageSpecDecls,
79 /// which need to be searched recursively. That job falls to
80 /// TransformTopLevelDecl.
81 //----------------------------------------------------------------------
82 bool HandleTopLevelDecl(clang::DeclGroupRef D);
84 //----------------------------------------------------------------------
86 //----------------------------------------------------------------------
87 void HandleTranslationUnit(clang::ASTContext &Ctx);
89 //----------------------------------------------------------------------
91 //----------------------------------------------------------------------
92 void HandleTagDeclDefinition(clang::TagDecl *D);
94 //----------------------------------------------------------------------
96 //----------------------------------------------------------------------
97 void CompleteTentativeDefinition(clang::VarDecl *D);
99 //----------------------------------------------------------------------
101 //----------------------------------------------------------------------
102 void HandleVTable(clang::CXXRecordDecl *RD, bool DefinitionRequired);
104 //----------------------------------------------------------------------
106 //----------------------------------------------------------------------
109 //----------------------------------------------------------------------
110 /// Set the Sema object to use when performing transforms, and pass it on
113 /// The Sema to use. Because Sema isn't externally visible, this class
114 /// casts it to an Action for actual use.
115 //----------------------------------------------------------------------
116 void InitializeSema(clang::Sema &S);
118 //----------------------------------------------------------------------
119 /// Reset the Sema to NULL now that transformations are done
120 //----------------------------------------------------------------------
123 //----------------------------------------------------------------------
124 /// Hunt the given FunctionDecl for the argument struct and place
125 /// information about it into m_function
128 /// The FunctionDecl to hunt.
129 //----------------------------------------------------------------------
131 ExtractFromFunctionDecl(clang::FunctionDecl* F);
133 //----------------------------------------------------------------------
134 /// Hunt the given Decl for FunctionDecls named the same as the wrapper
135 /// function name, recursing as necessary through LinkageSpecDecls, and
136 /// calling ExtractFromFunctionDecl on anything that was found
139 /// The Decl to hunt.
140 //----------------------------------------------------------------------
142 ExtractFromTopLevelDecl(clang::Decl* D);
144 clang::ASTContext *m_ast_context; ///< The AST context to use for identifiers and types.
145 clang::ASTConsumer *m_passthrough; ///< The ASTConsumer down the chain, for passthrough. NULL if it's a SemaConsumer.
146 clang::SemaConsumer *m_passthrough_sema; ///< The SemaConsumer down the chain, for passthrough. NULL if it's an ASTConsumer.
147 clang::Sema *m_sema; ///< The Sema to use.
148 clang::Action *m_action; ///< The Sema to use, cast to an Action so it's usable.
150 ClangFunction &m_function; ///< The function to populate with information about the argument structure.
151 std::string m_struct_name; ///< The name of the structure to extract.