ASTResultSynthesizer.h revision 65dafa8344c8c018e346dd331a7782081a896239
1//===-- ASTResultSynthesizer.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_ASTResultSynthesizer_h_
11#define liblldb_ASTResultSynthesizer_h_
12
13#include "clang/Sema/SemaConsumer.h"
14#include "lldb/Core/ClangForward.h"
15
16namespace lldb_private {
17
18//----------------------------------------------------------------------
19/// @class ASTResultSynthesizer ASTResultSynthesizer.h "lldb/Expression/ASTResultSynthesizer.h"
20/// @brief Adds a result variable declaration to the ASTs for an expression.
21///
22/// Users expect the expression "i + 3" to return a result, even if a result
23/// variable wasn't specifically declared.  To fulfil this requirement, LLDB adds
24/// a result variable to the expression, transforming it to
25/// "int ___clang_expr_result = i + 3."  The IR transformers ensure that the
26/// resulting variable is mapped to the right piece of memory.
27/// ASTResultSynthesizer's job is to add the variable and its initialization to
28/// the ASTs for the expression, and it does so by acting as a SemaConsumer for
29/// Clang.
30//----------------------------------------------------------------------
31class ASTResultSynthesizer : public clang::SemaConsumer
32{
33public:
34    //----------------------------------------------------------------------
35    /// Constructor
36    ///
37    /// @param[in] passthrough
38    ///     Since the ASTs must typically go through to the Clang code generator
39    ///     in order to produce LLVM IR, this SemaConsumer must allow them to
40    ///     pass to the next step in the chain after processing.  Passthrough is
41    ///     the next ASTConsumer, or NULL if none is required.
42    //----------------------------------------------------------------------
43    ASTResultSynthesizer(clang::ASTConsumer *passthrough);
44
45    //----------------------------------------------------------------------
46    /// Destructor
47    //----------------------------------------------------------------------
48    ~ASTResultSynthesizer();
49
50    //----------------------------------------------------------------------
51    /// Link this consumer with a particular AST context
52    ///
53    /// @param[in] Context
54    ///     This AST context will be used for types and identifiers, and also
55    ///     forwarded to the passthrough consumer, if one exists.
56    //----------------------------------------------------------------------
57    void Initialize(clang::ASTContext &Context);
58
59    //----------------------------------------------------------------------
60    /// Examine a list of Decls to find the function ___clang_expr and
61    /// transform its code
62    ///
63    /// @param[in] D
64    ///     The list of Decls to search.  These may contain LinkageSpecDecls,
65    ///     which need to be searched recursively.  That job falls to
66    ///     TransformTopLevelDecl.
67    //----------------------------------------------------------------------
68    void HandleTopLevelDecl(clang::DeclGroupRef D);
69
70    //----------------------------------------------------------------------
71    /// Passthrough stub
72    //----------------------------------------------------------------------
73    void HandleTranslationUnit(clang::ASTContext &Ctx);
74
75    //----------------------------------------------------------------------
76    /// Passthrough stub
77    //----------------------------------------------------------------------
78    void HandleTagDeclDefinition(clang::TagDecl *D);
79
80    //----------------------------------------------------------------------
81    /// Passthrough stub
82    //----------------------------------------------------------------------
83    void CompleteTentativeDefinition(clang::VarDecl *D);
84
85    //----------------------------------------------------------------------
86    /// Passthrough stub
87    //----------------------------------------------------------------------
88    void HandleVTable(clang::CXXRecordDecl *RD, bool DefinitionRequired);
89
90    //----------------------------------------------------------------------
91    /// Passthrough stub
92    //----------------------------------------------------------------------
93    void PrintStats();
94
95    //----------------------------------------------------------------------
96    /// Set the Sema object to use when performing transforms, and pass it on
97    ///
98    /// @param[in] S
99    ///     The Sema to use.  Because Sema isn't externally visible, this class
100    ///     casts it to an Action for actual use.
101    //----------------------------------------------------------------------
102    void InitializeSema(clang::Sema &S);
103
104    //----------------------------------------------------------------------
105    /// Reset the Sema to NULL now that transformations are done
106    //----------------------------------------------------------------------
107    void ForgetSema();
108private:
109    //----------------------------------------------------------------------
110    /// Hunt the given Decl for FunctionDecls named ___clang_expr, recursing
111    /// as necessary through LinkageSpecDecls, and calling SynthesizeResult on
112    /// anything that was found
113    ///
114    /// @param[in] D
115    ///     The Decl to hunt.
116    //----------------------------------------------------------------------
117    void TransformTopLevelDecl(clang::Decl *D);
118
119    //----------------------------------------------------------------------
120    /// Process a function and produce the result variable and initialization
121    ///
122    /// @param[in] FunDecl
123    ///     The function to process.
124    //----------------------------------------------------------------------
125    bool SynthesizeResult(clang::FunctionDecl *FunDecl);
126
127    clang::ASTContext *m_ast_context;           ///< The AST context to use for identifiers and types.
128    clang::ASTConsumer *m_passthrough;          ///< The ASTConsumer down the chain, for passthrough.  NULL if it's a SemaConsumer.
129    clang::SemaConsumer *m_passthrough_sema;    ///< The SemaConsumer down the chain, for passthrough.  NULL if it's an ASTConsumer.
130    clang::Sema *m_sema;                        ///< The Sema to use.
131    clang::Action *m_action;                    ///< The Sema to use, cast to an Action so it's usable.
132};
133
134}
135
136#endif