aboutsummaryrefslogtreecommitdiff
path: root/include/lldb/Expression/ASTStructExtractor.h
blob: a1518de83d6d1a70e6aa99ec74b24b2f7b9bf628 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
//===-- ASTStructExtractor.h ------------------------------------*- C++ -*-===//
//
//                     The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//

#ifndef liblldb_ASTStructExtractor_h_
#define liblldb_ASTStructExtractor_h_

#include "clang/Sema/SemaConsumer.h"
#include "lldb/Core/ClangForward.h"
#include "lldb/Expression/ClangExpressionVariable.h"
#include "lldb/Expression/ClangFunction.h"

namespace lldb_private {
    
//----------------------------------------------------------------------
/// @class ASTStructExtractor ASTStructExtractor.h "lldb/Expression/ASTStructExtractor.h"
/// @brief Extracts and describes the argument structure for a wrapped function.
///
/// This pass integrates with ClangFunction, which calls functions with custom
/// sets of arguments.  To avoid having to implement the full calling convention
/// for the target's architecture, ClangFunction writes a simple wrapper
/// function that takes a pointer to an argument structure that contains room
/// for the address of the function to be called, the values of all its
/// arguments, and room for the function's return value.
///
/// The definition of this struct is itself in the body of the wrapper function,
/// so Clang does the structure layout itself.  ASTStructExtractor reads through
/// the AST for the wrapper funtion and finds the struct.
//----------------------------------------------------------------------
class ASTStructExtractor : public clang::SemaConsumer
{
public:
    //----------------------------------------------------------------------
    /// Constructor
    ///
    /// @param[in] passthrough
    ///     Since the ASTs must typically go through to the Clang code generator
    ///     in order to produce LLVM IR, this SemaConsumer must allow them to
    ///     pass to the next step in the chain after processing.  Passthrough is
    ///     the next ASTConsumer, or NULL if none is required.
    ///
    /// @param[in] struct_name
    ///     The name of the structure to extract from the wrapper function.
    ///
    /// @param[in] function
    ///     The caller object whose members should be populated with information
    ///     about the argument struct.  ClangFunction friends ASTStructExtractor
    ///     for this purpose.
    //----------------------------------------------------------------------
    ASTStructExtractor(clang::ASTConsumer *passthrough,
                       const char *struct_name,
                       ClangFunction &function);
    
    //----------------------------------------------------------------------
    /// Destructor
    //----------------------------------------------------------------------
    virtual ~ASTStructExtractor();
    
    //----------------------------------------------------------------------
    /// Link this consumer with a particular AST context
    ///
    /// @param[in] Context
    ///     This AST context will be used for types and identifiers, and also
    ///     forwarded to the passthrough consumer, if one exists.
    //----------------------------------------------------------------------
    void Initialize(clang::ASTContext &Context);
    
    //----------------------------------------------------------------------
    /// Examine a list of Decls to find the function $__lldb_expr and 
    /// transform its code
    ///
    /// @param[in] D
    ///     The list of Decls to search.  These may contain LinkageSpecDecls,
    ///     which need to be searched recursively.  That job falls to
    ///     TransformTopLevelDecl.
    //----------------------------------------------------------------------
    bool HandleTopLevelDecl(clang::DeclGroupRef D);
    
    //----------------------------------------------------------------------
    /// Passthrough stub
    //----------------------------------------------------------------------
    void HandleTranslationUnit(clang::ASTContext &Ctx);
    
    //----------------------------------------------------------------------
    /// Passthrough stub
    //----------------------------------------------------------------------
    void HandleTagDeclDefinition(clang::TagDecl *D);
    
    //----------------------------------------------------------------------
    /// Passthrough stub
    //----------------------------------------------------------------------
    void CompleteTentativeDefinition(clang::VarDecl *D);
    
    //----------------------------------------------------------------------
    /// Passthrough stub
    //----------------------------------------------------------------------
    void HandleVTable(clang::CXXRecordDecl *RD, bool DefinitionRequired);
    
    //----------------------------------------------------------------------
    /// Passthrough stub
    //----------------------------------------------------------------------
    void PrintStats();
    
    //----------------------------------------------------------------------
    /// Set the Sema object to use when performing transforms, and pass it on
    ///
    /// @param[in] S
    ///     The Sema to use.  Because Sema isn't externally visible, this class
    ///     casts it to an Action for actual use.
    //----------------------------------------------------------------------
    void InitializeSema(clang::Sema &S);
    
    //----------------------------------------------------------------------
    /// Reset the Sema to NULL now that transformations are done
    //----------------------------------------------------------------------
    void ForgetSema();
private:
    //----------------------------------------------------------------------
    /// Hunt the given FunctionDecl for the argument struct and place
    /// information about it into m_function
    ///
    /// @param[in] F
    ///     The FunctionDecl to hunt.
    //----------------------------------------------------------------------
    void
    ExtractFromFunctionDecl(clang::FunctionDecl* F);
    
    //----------------------------------------------------------------------
    /// Hunt the given Decl for FunctionDecls named the same as the wrapper
    /// function name, recursing as necessary through LinkageSpecDecls, and 
    /// calling ExtractFromFunctionDecl on anything that was found
    ///
    /// @param[in] D
    ///     The Decl to hunt.
    //----------------------------------------------------------------------
    void
    ExtractFromTopLevelDecl(clang::Decl* D);
    
    clang::ASTContext              *m_ast_context;          ///< The AST context to use for identifiers and types.
    clang::ASTConsumer             *m_passthrough;          ///< The ASTConsumer down the chain, for passthrough.  NULL if it's a SemaConsumer.
    clang::SemaConsumer            *m_passthrough_sema;     ///< The SemaConsumer down the chain, for passthrough.  NULL if it's an ASTConsumer.
    clang::Sema                    *m_sema;                 ///< The Sema to use.
    clang::Action                  *m_action;               ///< The Sema to use, cast to an Action so it's usable.
    
    ClangFunction                  &m_function;             ///< The function to populate with information about the argument structure.
    std::string                     m_struct_name;          ///< The name of the structure to extract.
};
    
}

#endif