blob: 36f78b3120fe4b51d2beeff08d1c6d9f5e4d7f31 [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"
Sean Callanan3aa7da52010-12-13 22:46:15 +000015#include "clang/AST/DeclObjC.h"
Sean Callanan8c6934d2010-07-01 20:08:22 +000016#include "clang/AST/Expr.h"
17#include "clang/AST/Stmt.h"
Sean Callanan8c6934d2010-07-01 20:08:22 +000018#include "clang/Parse/Parser.h"
Sean Callanan8c6934d2010-07-01 20:08:22 +000019#include "llvm/Support/Casting.h"
20#include "llvm/Support/raw_ostream.h"
21#include "lldb/Core/Log.h"
Sean Callanan65dafa82010-08-27 01:01:44 +000022#include "lldb/Expression/ASTResultSynthesizer.h"
Sean Callanan8c6934d2010-07-01 20:08:22 +000023
24using namespace llvm;
25using namespace clang;
26using namespace lldb_private;
27
Sean Callanana91dd992010-11-19 02:52:21 +000028ASTResultSynthesizer::ASTResultSynthesizer(ASTConsumer *passthrough,
29 TypeFromUser desired_type) :
Greg Clayton980d0672010-07-12 23:14:00 +000030 m_ast_context (NULL),
31 m_passthrough (passthrough),
32 m_passthrough_sema (NULL),
Sean Callanana91dd992010-11-19 02:52:21 +000033 m_sema (NULL),
34 m_desired_type (desired_type)
Sean Callanan8c6934d2010-07-01 20:08:22 +000035{
36 if (!m_passthrough)
37 return;
38
39 m_passthrough_sema = dyn_cast<SemaConsumer>(passthrough);
40}
41
Sean Callanan65dafa82010-08-27 01:01:44 +000042ASTResultSynthesizer::~ASTResultSynthesizer()
Sean Callanan8c6934d2010-07-01 20:08:22 +000043{
44}
45
46void
Sean Callanan65dafa82010-08-27 01:01:44 +000047ASTResultSynthesizer::Initialize(ASTContext &Context)
Sean Callanan8c6934d2010-07-01 20:08:22 +000048{
49 m_ast_context = &Context;
50
51 if (m_passthrough)
52 m_passthrough->Initialize(Context);
53}
54
55void
Sean Callanan65dafa82010-08-27 01:01:44 +000056ASTResultSynthesizer::TransformTopLevelDecl(Decl* D)
Sean Callanan8c6934d2010-07-01 20:08:22 +000057{
Sean Callanan3aa7da52010-12-13 22:46:15 +000058 lldb::LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
59
60 if (NamedDecl *named_decl = dyn_cast<NamedDecl>(D))
61 {
62 if (log)
63 {
64 if (named_decl->getIdentifier())
65 log->Printf("TransformTopLevelDecl(%s)", named_decl->getIdentifier()->getNameStart());
66 else if (ObjCMethodDecl *method_decl = dyn_cast<ObjCMethodDecl>(D))
67 log->Printf("TransformTopLevelDecl(%s)", method_decl->getSelector().getAsString().c_str());
68 else
69 log->Printf("TransformTopLevelDecl(<complex>)");
70 }
71
72 }
Sean Callanan8c6934d2010-07-01 20:08:22 +000073
Sean Callanan3aa7da52010-12-13 22:46:15 +000074 if (LinkageSpecDecl *linkage_spec_decl = dyn_cast<LinkageSpecDecl>(D))
Sean Callanan8c6934d2010-07-01 20:08:22 +000075 {
76 RecordDecl::decl_iterator decl_iterator;
77
78 for (decl_iterator = linkage_spec_decl->decls_begin();
79 decl_iterator != linkage_spec_decl->decls_end();
80 ++decl_iterator)
81 {
82 TransformTopLevelDecl(*decl_iterator);
83 }
84 }
Sean Callanan3aa7da52010-12-13 22:46:15 +000085 else if (ObjCMethodDecl *method_decl = dyn_cast<ObjCMethodDecl>(D))
Sean Callanan8c6934d2010-07-01 20:08:22 +000086 {
Sean Callanan3aa7da52010-12-13 22:46:15 +000087 if (m_ast_context &&
88 !method_decl->getSelector().getAsString().compare("$__lldb_expr:"))
89 {
90 SynthesizeObjCMethodResult(method_decl);
91 }
92 }
93 else if (FunctionDecl *function_decl = dyn_cast<FunctionDecl>(D))
94 {
95 if (m_ast_context &&
96 !function_decl->getNameInfo().getAsString().compare("$__lldb_expr"))
97 {
98 SynthesizeFunctionResult(function_decl);
99 }
Sean Callanan8c6934d2010-07-01 20:08:22 +0000100 }
101}
102
103void
Sean Callanan65dafa82010-08-27 01:01:44 +0000104ASTResultSynthesizer::HandleTopLevelDecl(DeclGroupRef D)
Sean Callanan8c6934d2010-07-01 20:08:22 +0000105{
106 DeclGroupRef::iterator decl_iterator;
107
108 for (decl_iterator = D.begin();
109 decl_iterator != D.end();
110 ++decl_iterator)
111 {
112 Decl *decl = *decl_iterator;
113
114 TransformTopLevelDecl(decl);
115 }
116
117 if (m_passthrough)
118 m_passthrough->HandleTopLevelDecl(D);
119}
120
121bool
Sean Callanan3aa7da52010-12-13 22:46:15 +0000122ASTResultSynthesizer::SynthesizeFunctionResult (FunctionDecl *FunDecl)
Sean Callanan8c6934d2010-07-01 20:08:22 +0000123{
Greg Claytone005f2c2010-11-06 01:53:30 +0000124 lldb::LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
Sean Callanan8c6934d2010-07-01 20:08:22 +0000125
Sean Callanan3aa7da52010-12-13 22:46:15 +0000126 ASTContext &Ctx(*m_ast_context);
127
Sean Callanan8c6934d2010-07-01 20:08:22 +0000128 if (!m_sema)
129 return false;
Sean Callanan3aa7da52010-12-13 22:46:15 +0000130
Sean Callanan8c6934d2010-07-01 20:08:22 +0000131 FunctionDecl *function_decl = FunDecl;
132
133 if (!function_decl)
134 return false;
135
Sean Callanane8a59a82010-09-13 21:34:21 +0000136 if (log)
137 {
138 std::string s;
139 raw_string_ostream os(s);
140
Sean Callanan6288a652010-09-22 00:33:31 +0000141 Ctx.getTranslationUnitDecl()->print(os);
Sean Callanane8a59a82010-09-13 21:34:21 +0000142
143 os.flush();
144
Sean Callanan6288a652010-09-22 00:33:31 +0000145 log->Printf("AST context before transforming:\n%s", s.c_str());
Sean Callanane8a59a82010-09-13 21:34:21 +0000146 }
147
Sean Callanan8c6934d2010-07-01 20:08:22 +0000148 Stmt *function_body = function_decl->getBody();
149 CompoundStmt *compound_stmt = dyn_cast<CompoundStmt>(function_body);
150
Sean Callanan3aa7da52010-12-13 22:46:15 +0000151 bool ret = SynthesizeBodyResult (compound_stmt,
152 function_decl);
153
154 if (log)
155 {
156 std::string s;
157 raw_string_ostream os(s);
158
159 function_decl->print(os);
160
161 os.flush();
162
163 log->Printf("Transformed function AST:\n%s", s.c_str());
164 }
165
166 return ret;
167}
168
169bool
170ASTResultSynthesizer::SynthesizeObjCMethodResult (ObjCMethodDecl *MethodDecl)
171{
172 lldb::LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
173
174 ASTContext &Ctx(*m_ast_context);
175
176 if (!m_sema)
177 return false;
178
179 if (!MethodDecl)
180 return false;
181
182 if (log)
183 {
184 std::string s;
185 raw_string_ostream os(s);
186
187 Ctx.getTranslationUnitDecl()->print(os);
188
189 os.flush();
190
191 log->Printf("AST context before transforming:\n%s", s.c_str());
192 }
193
194 Stmt *method_body = MethodDecl->getBody();
195 CompoundStmt *compound_stmt = dyn_cast<CompoundStmt>(method_body);
196
197 bool ret = SynthesizeBodyResult (compound_stmt,
198 MethodDecl);
199
200 if (log)
201 {
202 std::string s;
203 raw_string_ostream os(s);
204
205 MethodDecl->print(os);
206
207 os.flush();
208
209 log->Printf("Transformed function AST:\n%s", s.c_str());
210 }
211
212 return ret;
213}
214
215bool
216ASTResultSynthesizer::SynthesizeBodyResult (CompoundStmt *Body,
217 DeclContext *DC)
218{
219 lldb::LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
220
221 ASTContext &Ctx(*m_ast_context);
222
223 CompoundStmt *compound_stmt = dyn_cast<CompoundStmt>(Body);
224
Sean Callanan8c6934d2010-07-01 20:08:22 +0000225 if (!compound_stmt)
226 return false;
227
228 if (compound_stmt->body_empty())
229 return false;
230
231 Stmt **last_stmt_ptr = compound_stmt->body_end() - 1;
232 Stmt *last_stmt = *last_stmt_ptr;
233
Sean Callanane8a59a82010-09-13 21:34:21 +0000234 while (dyn_cast<NullStmt>(last_stmt))
235 {
236 if (last_stmt_ptr != compound_stmt->body_begin())
237 {
238 last_stmt_ptr--;
239 last_stmt = *last_stmt_ptr;
240 }
241 }
242
Sean Callanan8c6934d2010-07-01 20:08:22 +0000243 Expr *last_expr = dyn_cast<Expr>(last_stmt);
244
245 if (!last_expr)
246 // No auxiliary variable necessary; expression returns void
247 return true;
248
249 QualType expr_qual_type = last_expr->getType();
250 clang::Type *expr_type = expr_qual_type.getTypePtr();
251
252 if (!expr_type)
253 return false;
254
255 if (expr_type->isVoidType())
256 return true;
257
258 if (log)
259 {
260 std::string s = expr_qual_type.getAsString();
261
262 log->Printf("Last statement's type: %s", s.c_str());
263 }
264
Greg Clayton8de27c72010-10-15 22:48:33 +0000265 IdentifierInfo &result_id = Ctx.Idents.get("$__lldb_expr_result");
Sean Callanan45839272010-07-24 01:37:44 +0000266
Sean Callanan8c6934d2010-07-01 20:08:22 +0000267 clang::VarDecl *result_decl = VarDecl::Create(Ctx,
Sean Callanan3aa7da52010-12-13 22:46:15 +0000268 DC,
Sean Callanan8c6934d2010-07-01 20:08:22 +0000269 SourceLocation(),
270 &result_id,
271 expr_qual_type,
272 NULL,
Sean Callanan47a5c4c2010-09-23 03:01:22 +0000273 SC_Static,
274 SC_Static);
Sean Callanan8c6934d2010-07-01 20:08:22 +0000275
276 if (!result_decl)
277 return false;
278
Sean Callanan3aa7da52010-12-13 22:46:15 +0000279 DC->addDecl(result_decl);
Sean Callanan8c6934d2010-07-01 20:08:22 +0000280
281 ///////////////////////////////
282 // call AddInitializerToDecl
283 //
Sean Callanan47a5c4c2010-09-23 03:01:22 +0000284
285 m_sema->AddInitializerToDecl(result_decl, last_expr);
Sean Callanan8c6934d2010-07-01 20:08:22 +0000286
287 /////////////////////////////////
288 // call ConvertDeclToDeclGroup
289 //
290
Sean Callanan47a5c4c2010-09-23 03:01:22 +0000291 Sema::DeclGroupPtrTy result_decl_group_ptr;
Sean Callanan8c6934d2010-07-01 20:08:22 +0000292
Sean Callanan47a5c4c2010-09-23 03:01:22 +0000293 result_decl_group_ptr = m_sema->ConvertDeclToDeclGroup(result_decl);
Sean Callanan8c6934d2010-07-01 20:08:22 +0000294
295 ////////////////////////
296 // call ActOnDeclStmt
297 //
298
Sean Callanan47a5c4c2010-09-23 03:01:22 +0000299 StmtResult result_initialization_stmt_result(m_sema->ActOnDeclStmt(result_decl_group_ptr,
300 SourceLocation(),
301 SourceLocation()));
Sean Callanan8c6934d2010-07-01 20:08:22 +0000302
Sean Callanan8c6934d2010-07-01 20:08:22 +0000303 ////////////////////////////////////////////////
304 // replace the old statement with the new one
305 //
306
Sean Callanan45839272010-07-24 01:37:44 +0000307 *last_stmt_ptr = reinterpret_cast<Stmt*>(result_initialization_stmt_result.take());
Sean Callanan8c6934d2010-07-01 20:08:22 +0000308
Sean Callanan8c6934d2010-07-01 20:08:22 +0000309 return true;
310}
311
312void
Sean Callanan65dafa82010-08-27 01:01:44 +0000313ASTResultSynthesizer::HandleTranslationUnit(ASTContext &Ctx)
Sean Callanan8c6934d2010-07-01 20:08:22 +0000314{
315 if (m_passthrough)
316 m_passthrough->HandleTranslationUnit(Ctx);
317}
318
319void
Sean Callanan65dafa82010-08-27 01:01:44 +0000320ASTResultSynthesizer::HandleTagDeclDefinition(TagDecl *D)
Sean Callanan8c6934d2010-07-01 20:08:22 +0000321{
322 if (m_passthrough)
323 m_passthrough->HandleTagDeclDefinition(D);
324}
325
326void
Sean Callanan65dafa82010-08-27 01:01:44 +0000327ASTResultSynthesizer::CompleteTentativeDefinition(VarDecl *D)
Sean Callanan8c6934d2010-07-01 20:08:22 +0000328{
329 if (m_passthrough)
330 m_passthrough->CompleteTentativeDefinition(D);
331}
332
333void
Sean Callanan65dafa82010-08-27 01:01:44 +0000334ASTResultSynthesizer::HandleVTable(CXXRecordDecl *RD, bool DefinitionRequired)
Sean Callanan8c6934d2010-07-01 20:08:22 +0000335{
336 if (m_passthrough)
337 m_passthrough->HandleVTable(RD, DefinitionRequired);
338}
339
340void
Sean Callanan65dafa82010-08-27 01:01:44 +0000341ASTResultSynthesizer::PrintStats()
Sean Callanan8c6934d2010-07-01 20:08:22 +0000342{
343 if (m_passthrough)
344 m_passthrough->PrintStats();
345}
346
347void
Sean Callanan65dafa82010-08-27 01:01:44 +0000348ASTResultSynthesizer::InitializeSema(Sema &S)
Sean Callanan8c6934d2010-07-01 20:08:22 +0000349{
350 m_sema = &S;
Sean Callanan8c6934d2010-07-01 20:08:22 +0000351
352 if (m_passthrough_sema)
353 m_passthrough_sema->InitializeSema(S);
354}
355
356void
Sean Callanan65dafa82010-08-27 01:01:44 +0000357ASTResultSynthesizer::ForgetSema()
Sean Callanan8c6934d2010-07-01 20:08:22 +0000358{
359 m_sema = NULL;
Sean Callanan8c6934d2010-07-01 20:08:22 +0000360
361 if (m_passthrough_sema)
362 m_passthrough_sema->ForgetSema();
363}