blob: bcabfe3badc109a3be6aff0b7d9716befa754225 [file] [log] [blame]
Sean Callanan65dafa82010-08-27 01:01:44 +00001//===-- ASTResultSynthesizer.cpp --------------------------------*- C++ -*-===//
Sean Callanan8c6934d2010-07-01 20:08:22 +00002//
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#include "stdlib.h"
11#include "clang/AST/ASTContext.h"
12#include "clang/AST/Decl.h"
13#include "clang/AST/DeclCXX.h"
14#include "clang/AST/DeclGroup.h"
15#include "clang/AST/Expr.h"
16#include "clang/AST/Stmt.h"
Sean Callanan8c6934d2010-07-01 20:08:22 +000017#include "clang/Parse/Parser.h"
Sean Callanan8c6934d2010-07-01 20:08:22 +000018#include "llvm/Support/Casting.h"
19#include "llvm/Support/raw_ostream.h"
20#include "lldb/Core/Log.h"
Sean Callanan65dafa82010-08-27 01:01:44 +000021#include "lldb/Expression/ASTResultSynthesizer.h"
Sean Callanan8c6934d2010-07-01 20:08:22 +000022
23using namespace llvm;
24using namespace clang;
25using namespace lldb_private;
26
Sean Callanana91dd992010-11-19 02:52:21 +000027ASTResultSynthesizer::ASTResultSynthesizer(ASTConsumer *passthrough,
28 TypeFromUser desired_type) :
Greg Clayton980d0672010-07-12 23:14:00 +000029 m_ast_context (NULL),
30 m_passthrough (passthrough),
31 m_passthrough_sema (NULL),
Sean Callanana91dd992010-11-19 02:52:21 +000032 m_sema (NULL),
33 m_desired_type (desired_type)
Sean Callanan8c6934d2010-07-01 20:08:22 +000034{
35 if (!m_passthrough)
36 return;
37
38 m_passthrough_sema = dyn_cast<SemaConsumer>(passthrough);
39}
40
Sean Callanan65dafa82010-08-27 01:01:44 +000041ASTResultSynthesizer::~ASTResultSynthesizer()
Sean Callanan8c6934d2010-07-01 20:08:22 +000042{
43}
44
45void
Sean Callanan65dafa82010-08-27 01:01:44 +000046ASTResultSynthesizer::Initialize(ASTContext &Context)
Sean Callanan8c6934d2010-07-01 20:08:22 +000047{
48 m_ast_context = &Context;
49
50 if (m_passthrough)
51 m_passthrough->Initialize(Context);
52}
53
54void
Sean Callanan65dafa82010-08-27 01:01:44 +000055ASTResultSynthesizer::TransformTopLevelDecl(Decl* D)
Sean Callanan8c6934d2010-07-01 20:08:22 +000056{
57 LinkageSpecDecl *linkage_spec_decl = dyn_cast<LinkageSpecDecl>(D);
58
59 if (linkage_spec_decl)
60 {
61 RecordDecl::decl_iterator decl_iterator;
62
63 for (decl_iterator = linkage_spec_decl->decls_begin();
64 decl_iterator != linkage_spec_decl->decls_end();
65 ++decl_iterator)
66 {
67 TransformTopLevelDecl(*decl_iterator);
68 }
69 }
70
71 FunctionDecl *function_decl = dyn_cast<FunctionDecl>(D);
72
73 if (m_ast_context &&
74 function_decl &&
Greg Clayton8de27c72010-10-15 22:48:33 +000075 !function_decl->getNameInfo().getAsString().compare("$__lldb_expr"))
Sean Callanan8c6934d2010-07-01 20:08:22 +000076 {
Sean Callananbfc33ce2010-08-16 23:01:35 +000077 SynthesizeResult(function_decl);
Sean Callanan8c6934d2010-07-01 20:08:22 +000078 }
79}
80
81void
Sean Callanan65dafa82010-08-27 01:01:44 +000082ASTResultSynthesizer::HandleTopLevelDecl(DeclGroupRef D)
Sean Callanan8c6934d2010-07-01 20:08:22 +000083{
84 DeclGroupRef::iterator decl_iterator;
85
86 for (decl_iterator = D.begin();
87 decl_iterator != D.end();
88 ++decl_iterator)
89 {
90 Decl *decl = *decl_iterator;
91
92 TransformTopLevelDecl(decl);
93 }
94
95 if (m_passthrough)
96 m_passthrough->HandleTopLevelDecl(D);
97}
98
99bool
Sean Callanan65dafa82010-08-27 01:01:44 +0000100ASTResultSynthesizer::SynthesizeResult (FunctionDecl *FunDecl)
Sean Callanan8c6934d2010-07-01 20:08:22 +0000101{
Sean Callananbfc33ce2010-08-16 23:01:35 +0000102 ASTContext &Ctx(*m_ast_context);
103
Greg Claytone005f2c2010-11-06 01:53:30 +0000104 lldb::LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
Sean Callanan8c6934d2010-07-01 20:08:22 +0000105
106 if (!m_sema)
107 return false;
108
109 FunctionDecl *function_decl = FunDecl;
110
111 if (!function_decl)
112 return false;
113
Sean Callanane8a59a82010-09-13 21:34:21 +0000114 if (log)
115 {
116 std::string s;
117 raw_string_ostream os(s);
118
Sean Callanan6288a652010-09-22 00:33:31 +0000119 Ctx.getTranslationUnitDecl()->print(os);
Sean Callanane8a59a82010-09-13 21:34:21 +0000120
121 os.flush();
122
Sean Callanan6288a652010-09-22 00:33:31 +0000123 log->Printf("AST context before transforming:\n%s", s.c_str());
Sean Callanane8a59a82010-09-13 21:34:21 +0000124 }
125
Sean Callanan8c6934d2010-07-01 20:08:22 +0000126 Stmt *function_body = function_decl->getBody();
127 CompoundStmt *compound_stmt = dyn_cast<CompoundStmt>(function_body);
128
129 if (!compound_stmt)
130 return false;
131
132 if (compound_stmt->body_empty())
133 return false;
134
135 Stmt **last_stmt_ptr = compound_stmt->body_end() - 1;
136 Stmt *last_stmt = *last_stmt_ptr;
137
Sean Callanane8a59a82010-09-13 21:34:21 +0000138 while (dyn_cast<NullStmt>(last_stmt))
139 {
140 if (last_stmt_ptr != compound_stmt->body_begin())
141 {
142 last_stmt_ptr--;
143 last_stmt = *last_stmt_ptr;
144 }
145 }
146
Sean Callanan8c6934d2010-07-01 20:08:22 +0000147 Expr *last_expr = dyn_cast<Expr>(last_stmt);
148
149 if (!last_expr)
150 // No auxiliary variable necessary; expression returns void
151 return true;
152
153 QualType expr_qual_type = last_expr->getType();
154 clang::Type *expr_type = expr_qual_type.getTypePtr();
155
156 if (!expr_type)
157 return false;
158
159 if (expr_type->isVoidType())
160 return true;
161
162 if (log)
163 {
164 std::string s = expr_qual_type.getAsString();
165
166 log->Printf("Last statement's type: %s", s.c_str());
167 }
168
Greg Clayton8de27c72010-10-15 22:48:33 +0000169 IdentifierInfo &result_id = Ctx.Idents.get("$__lldb_expr_result");
Sean Callanan45839272010-07-24 01:37:44 +0000170
Sean Callanan8c6934d2010-07-01 20:08:22 +0000171 clang::VarDecl *result_decl = VarDecl::Create(Ctx,
172 function_decl,
173 SourceLocation(),
174 &result_id,
175 expr_qual_type,
176 NULL,
Sean Callanan47a5c4c2010-09-23 03:01:22 +0000177 SC_Static,
178 SC_Static);
Sean Callanan8c6934d2010-07-01 20:08:22 +0000179
180 if (!result_decl)
181 return false;
182
183 function_decl->addDecl(result_decl);
184
185 ///////////////////////////////
186 // call AddInitializerToDecl
187 //
Sean Callanan47a5c4c2010-09-23 03:01:22 +0000188
189 m_sema->AddInitializerToDecl(result_decl, last_expr);
Sean Callanan8c6934d2010-07-01 20:08:22 +0000190
191 /////////////////////////////////
192 // call ConvertDeclToDeclGroup
193 //
194
Sean Callanan47a5c4c2010-09-23 03:01:22 +0000195 Sema::DeclGroupPtrTy result_decl_group_ptr;
Sean Callanan8c6934d2010-07-01 20:08:22 +0000196
Sean Callanan47a5c4c2010-09-23 03:01:22 +0000197 result_decl_group_ptr = m_sema->ConvertDeclToDeclGroup(result_decl);
Sean Callanan8c6934d2010-07-01 20:08:22 +0000198
199 ////////////////////////
200 // call ActOnDeclStmt
201 //
202
Sean Callanan47a5c4c2010-09-23 03:01:22 +0000203 StmtResult result_initialization_stmt_result(m_sema->ActOnDeclStmt(result_decl_group_ptr,
204 SourceLocation(),
205 SourceLocation()));
Sean Callanan8c6934d2010-07-01 20:08:22 +0000206
Sean Callanan8c6934d2010-07-01 20:08:22 +0000207 ////////////////////////////////////////////////
208 // replace the old statement with the new one
209 //
210
Sean Callanan45839272010-07-24 01:37:44 +0000211 *last_stmt_ptr = reinterpret_cast<Stmt*>(result_initialization_stmt_result.take());
Sean Callanan8c6934d2010-07-01 20:08:22 +0000212
213 if (log)
214 {
215 std::string s;
216 raw_string_ostream os(s);
217
218 function_decl->print(os);
219
220 os.flush();
221
222 log->Printf("Transformed function AST:\n%s", s.c_str());
223 }
224
225 return true;
226}
227
228void
Sean Callanan65dafa82010-08-27 01:01:44 +0000229ASTResultSynthesizer::HandleTranslationUnit(ASTContext &Ctx)
Sean Callanan8c6934d2010-07-01 20:08:22 +0000230{
231 if (m_passthrough)
232 m_passthrough->HandleTranslationUnit(Ctx);
233}
234
235void
Sean Callanan65dafa82010-08-27 01:01:44 +0000236ASTResultSynthesizer::HandleTagDeclDefinition(TagDecl *D)
Sean Callanan8c6934d2010-07-01 20:08:22 +0000237{
238 if (m_passthrough)
239 m_passthrough->HandleTagDeclDefinition(D);
240}
241
242void
Sean Callanan65dafa82010-08-27 01:01:44 +0000243ASTResultSynthesizer::CompleteTentativeDefinition(VarDecl *D)
Sean Callanan8c6934d2010-07-01 20:08:22 +0000244{
245 if (m_passthrough)
246 m_passthrough->CompleteTentativeDefinition(D);
247}
248
249void
Sean Callanan65dafa82010-08-27 01:01:44 +0000250ASTResultSynthesizer::HandleVTable(CXXRecordDecl *RD, bool DefinitionRequired)
Sean Callanan8c6934d2010-07-01 20:08:22 +0000251{
252 if (m_passthrough)
253 m_passthrough->HandleVTable(RD, DefinitionRequired);
254}
255
256void
Sean Callanan65dafa82010-08-27 01:01:44 +0000257ASTResultSynthesizer::PrintStats()
Sean Callanan8c6934d2010-07-01 20:08:22 +0000258{
259 if (m_passthrough)
260 m_passthrough->PrintStats();
261}
262
263void
Sean Callanan65dafa82010-08-27 01:01:44 +0000264ASTResultSynthesizer::InitializeSema(Sema &S)
Sean Callanan8c6934d2010-07-01 20:08:22 +0000265{
266 m_sema = &S;
Sean Callanan8c6934d2010-07-01 20:08:22 +0000267
268 if (m_passthrough_sema)
269 m_passthrough_sema->InitializeSema(S);
270}
271
272void
Sean Callanan65dafa82010-08-27 01:01:44 +0000273ASTResultSynthesizer::ForgetSema()
Sean Callanan8c6934d2010-07-01 20:08:22 +0000274{
275 m_sema = NULL;
Sean Callanan8c6934d2010-07-01 20:08:22 +0000276
277 if (m_passthrough_sema)
278 m_passthrough_sema->ForgetSema();
279}