blob: 1125d6272f9f0d8b98e50220092c883acdaa3075 [file] [log] [blame]
Sean Callanan1a8d4092010-08-27 01:01:44 +00001//===-- ASTResultSynthesizer.cpp --------------------------------*- C++ -*-===//
Sean Callanan116be532010-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 Callanan17827832010-12-13 22:46:15 +000015#include "clang/AST/DeclObjC.h"
Sean Callanan116be532010-07-01 20:08:22 +000016#include "clang/AST/Expr.h"
17#include "clang/AST/Stmt.h"
Sean Callanan116be532010-07-01 20:08:22 +000018#include "clang/Parse/Parser.h"
Sean Callanan116be532010-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 Callanan1a8d4092010-08-27 01:01:44 +000022#include "lldb/Expression/ASTResultSynthesizer.h"
Sean Callanan116be532010-07-01 20:08:22 +000023
24using namespace llvm;
25using namespace clang;
26using namespace lldb_private;
27
Sean Callananf7c3e272010-11-19 02:52:21 +000028ASTResultSynthesizer::ASTResultSynthesizer(ASTConsumer *passthrough,
29 TypeFromUser desired_type) :
Greg Claytonc8e11e12010-07-12 23:14:00 +000030 m_ast_context (NULL),
31 m_passthrough (passthrough),
32 m_passthrough_sema (NULL),
Sean Callananf7c3e272010-11-19 02:52:21 +000033 m_sema (NULL),
34 m_desired_type (desired_type)
Sean Callanan116be532010-07-01 20:08:22 +000035{
36 if (!m_passthrough)
37 return;
38
39 m_passthrough_sema = dyn_cast<SemaConsumer>(passthrough);
40}
41
Sean Callanan1a8d4092010-08-27 01:01:44 +000042ASTResultSynthesizer::~ASTResultSynthesizer()
Sean Callanan116be532010-07-01 20:08:22 +000043{
44}
45
46void
Sean Callanan1a8d4092010-08-27 01:01:44 +000047ASTResultSynthesizer::Initialize(ASTContext &Context)
Sean Callanan116be532010-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 Callanan1a8d4092010-08-27 01:01:44 +000056ASTResultSynthesizer::TransformTopLevelDecl(Decl* D)
Sean Callanan116be532010-07-01 20:08:22 +000057{
Sean Callanan17827832010-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 Callanan116be532010-07-01 20:08:22 +000073
Sean Callanan17827832010-12-13 22:46:15 +000074 if (LinkageSpecDecl *linkage_spec_decl = dyn_cast<LinkageSpecDecl>(D))
Sean Callanan116be532010-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 Callanan17827832010-12-13 22:46:15 +000085 else if (ObjCMethodDecl *method_decl = dyn_cast<ObjCMethodDecl>(D))
Sean Callanan116be532010-07-01 20:08:22 +000086 {
Sean Callanan17827832010-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 Callanan116be532010-07-01 20:08:22 +0000100 }
101}
102
103void
Sean Callanan1a8d4092010-08-27 01:01:44 +0000104ASTResultSynthesizer::HandleTopLevelDecl(DeclGroupRef D)
Sean Callanan116be532010-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 Callanan17827832010-12-13 22:46:15 +0000122ASTResultSynthesizer::SynthesizeFunctionResult (FunctionDecl *FunDecl)
Sean Callanan116be532010-07-01 20:08:22 +0000123{
Greg Clayton2d4edfb2010-11-06 01:53:30 +0000124 lldb::LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
Sean Callanan116be532010-07-01 20:08:22 +0000125
Sean Callanan17827832010-12-13 22:46:15 +0000126 ASTContext &Ctx(*m_ast_context);
127
Sean Callanan116be532010-07-01 20:08:22 +0000128 if (!m_sema)
129 return false;
Sean Callanan17827832010-12-13 22:46:15 +0000130
Sean Callanan116be532010-07-01 20:08:22 +0000131 FunctionDecl *function_decl = FunDecl;
132
133 if (!function_decl)
134 return false;
135
Jim Inghame3be0c52011-01-22 01:25:40 +0000136 if (log && log->GetVerbose())
Sean Callanan9e6ed532010-09-13 21:34:21 +0000137 {
138 std::string s;
139 raw_string_ostream os(s);
140
Sean Callananc31ba262010-09-22 00:33:31 +0000141 Ctx.getTranslationUnitDecl()->print(os);
Sean Callanan9e6ed532010-09-13 21:34:21 +0000142
143 os.flush();
144
Sean Callananc31ba262010-09-22 00:33:31 +0000145 log->Printf("AST context before transforming:\n%s", s.c_str());
Sean Callanan9e6ed532010-09-13 21:34:21 +0000146 }
147
Sean Callanan116be532010-07-01 20:08:22 +0000148 Stmt *function_body = function_decl->getBody();
149 CompoundStmt *compound_stmt = dyn_cast<CompoundStmt>(function_body);
150
Sean Callanan17827832010-12-13 22:46:15 +0000151 bool ret = SynthesizeBodyResult (compound_stmt,
152 function_decl);
Jim Inghame3be0c52011-01-22 01:25:40 +0000153
154 if (log && log->GetVerbose())
Sean Callanan17827832010-12-13 22:46:15 +0000155 {
156 std::string s;
157 raw_string_ostream os(s);
158
159 function_decl->print(os);
160
161 os.flush();
162
Jim Inghame3be0c52011-01-22 01:25:40 +0000163 log->Printf ("Transformed function AST:\n%s", s.c_str());
Sean Callanan17827832010-12-13 22:46:15 +0000164 }
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
Jim Inghame3be0c52011-01-22 01:25:40 +0000182 if (log && log->GetVerbose())
Sean Callanan17827832010-12-13 22:46:15 +0000183 {
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);
Sean Callanan22c52d92011-07-18 21:30:18 +0000222
223 if (!Body)
Sean Callanan116be532010-07-01 20:08:22 +0000224 return false;
225
Sean Callanan22c52d92011-07-18 21:30:18 +0000226 if (Body->body_empty())
Sean Callanan116be532010-07-01 20:08:22 +0000227 return false;
228
Sean Callanan22c52d92011-07-18 21:30:18 +0000229 Stmt **last_stmt_ptr = Body->body_end() - 1;
Sean Callanan116be532010-07-01 20:08:22 +0000230 Stmt *last_stmt = *last_stmt_ptr;
231
Sean Callanan9e6ed532010-09-13 21:34:21 +0000232 while (dyn_cast<NullStmt>(last_stmt))
233 {
Sean Callanan22c52d92011-07-18 21:30:18 +0000234 if (last_stmt_ptr != Body->body_begin())
Sean Callanan9e6ed532010-09-13 21:34:21 +0000235 {
236 last_stmt_ptr--;
237 last_stmt = *last_stmt_ptr;
238 }
Sean Callanan2d1f4be2011-02-22 21:52:56 +0000239 else
240 {
241 return false;
242 }
Sean Callanan9e6ed532010-09-13 21:34:21 +0000243 }
244
Sean Callanan116be532010-07-01 20:08:22 +0000245 Expr *last_expr = dyn_cast<Expr>(last_stmt);
246
247 if (!last_expr)
248 // No auxiliary variable necessary; expression returns void
249 return true;
250
Sean Callanan92adcac2011-01-13 08:53:35 +0000251 // is_lvalue is used to record whether the expression returns an assignable Lvalue or an
252 // Rvalue. This is relevant because they are handled differently.
253 //
254 // For Lvalues
255 //
256 // - In AST result synthesis (here!) the expression E is transformed into an initialization
257 // T *$__lldb_expr_result_ptr = &E.
258 //
259 // - In structure allocation, a pointer-sized slot is allocated in the struct that is to be
260 // passed into the expression.
261 //
262 // - In IR transformations, reads and writes to $__lldb_expr_result_ptr are redirected at
263 // an entry in the struct ($__lldb_arg) passed into the expression. (Other persistent
264 // variables are treated similarly, having been materialized as references, but in those
265 // cases the value of the reference itself is never modified.)
266 //
267 // - During materialization, $0 (the result persistent variable) is ignored.
268 //
269 // - During dematerialization, $0 is marked up as a load address with value equal to the
270 // contents of the structure entry.
271 //
272 // For Rvalues
273 //
274 // - In AST result synthesis the expression E is transformed into an initialization
275 // static T $__lldb_expr_result = E.
276 //
277 // - In structure allocation, a pointer-sized slot is allocated in the struct that is to be
278 // passed into the expression.
279 //
280 // - In IR transformations, an instruction is inserted at the beginning of the function to
281 // dereference the pointer resident in the slot. Reads and writes to $__lldb_expr_result
282 // are redirected at that dereferenced version. Guard variables for the static variable
283 // are excised.
284 //
285 // - During materialization, $0 (the result persistent variable) is populated with the location
286 // of a newly-allocated area of memory.
287 //
288 // - During dematerialization, $0 is ignored.
289
290 bool is_lvalue =
291 (last_expr->getValueKind() == VK_LValue || last_expr->getValueKind() == VK_XValue) &&
292 (last_expr->getObjectKind() == OK_Ordinary);
293
Sean Callanan116be532010-07-01 20:08:22 +0000294 QualType expr_qual_type = last_expr->getType();
Sean Callanan78e37602011-01-27 04:42:51 +0000295 const clang::Type *expr_type = expr_qual_type.getTypePtr();
Sean Callanan116be532010-07-01 20:08:22 +0000296
297 if (!expr_type)
298 return false;
299
300 if (expr_type->isVoidType())
301 return true;
302
303 if (log)
304 {
305 std::string s = expr_qual_type.getAsString();
306
Sean Callanan92adcac2011-01-13 08:53:35 +0000307 log->Printf("Last statement is an %s with type: %s", (is_lvalue ? "lvalue" : "rvalue"), s.c_str());
Sean Callanan116be532010-07-01 20:08:22 +0000308 }
309
Sean Callanan77eaf442011-07-08 00:39:14 +0000310 clang::VarDecl *result_decl = NULL;
Sean Callanan116be532010-07-01 20:08:22 +0000311
Sean Callanan92adcac2011-01-13 08:53:35 +0000312 if (is_lvalue)
313 {
Sean Callanan0c4d8d22011-08-04 21:37:47 +0000314 IdentifierInfo *result_ptr_id;
315
316 if (expr_type->isFunctionType())
317 result_ptr_id = &Ctx.Idents.get("$__lldb_expr_result"); // functions actually should be treated like function pointers
318 else
319 result_ptr_id = &Ctx.Idents.get("$__lldb_expr_result_ptr");
Sean Callanan92adcac2011-01-13 08:53:35 +0000320
321 QualType ptr_qual_type = Ctx.getPointerType(expr_qual_type);
322
323 result_decl = VarDecl::Create(Ctx,
324 DC,
325 SourceLocation(),
Sean Callananfb0b7582011-03-15 00:17:19 +0000326 SourceLocation(),
Sean Callanan0c4d8d22011-08-04 21:37:47 +0000327 result_ptr_id,
Sean Callanan92adcac2011-01-13 08:53:35 +0000328 ptr_qual_type,
329 NULL,
330 SC_Static,
331 SC_Static);
332
333 if (!result_decl)
334 return false;
335
336 ExprResult address_of_expr = m_sema->CreateBuiltinUnaryOp(SourceLocation(), UO_AddrOf, last_expr);
337
Sean Callananfb0b7582011-03-15 00:17:19 +0000338 m_sema->AddInitializerToDecl(result_decl, address_of_expr.take(), true, true);
Sean Callanan92adcac2011-01-13 08:53:35 +0000339 }
340 else
341 {
342 IdentifierInfo &result_id = Ctx.Idents.get("$__lldb_expr_result");
343
344 result_decl = VarDecl::Create(Ctx,
345 DC,
Sean Callananfb0b7582011-03-15 00:17:19 +0000346 SourceLocation(),
347 SourceLocation(),
Sean Callanan92adcac2011-01-13 08:53:35 +0000348 &result_id,
349 expr_qual_type,
350 NULL,
351 SC_Static,
352 SC_Static);
353
354 if (!result_decl)
355 return false;
356
Sean Callananfb0b7582011-03-15 00:17:19 +0000357 m_sema->AddInitializerToDecl(result_decl, last_expr, true, true);
Sean Callanan92adcac2011-01-13 08:53:35 +0000358 }
Sean Callanan116be532010-07-01 20:08:22 +0000359
Sean Callanan17827832010-12-13 22:46:15 +0000360 DC->addDecl(result_decl);
Sean Callanan116be532010-07-01 20:08:22 +0000361
362 ///////////////////////////////
363 // call AddInitializerToDecl
364 //
Sean Callanane2ef6e32010-09-23 03:01:22 +0000365
Sean Callanan92adcac2011-01-13 08:53:35 +0000366 //m_sema->AddInitializerToDecl(result_decl, last_expr);
Sean Callanan116be532010-07-01 20:08:22 +0000367
368 /////////////////////////////////
369 // call ConvertDeclToDeclGroup
370 //
371
Sean Callanane2ef6e32010-09-23 03:01:22 +0000372 Sema::DeclGroupPtrTy result_decl_group_ptr;
Sean Callanan116be532010-07-01 20:08:22 +0000373
Sean Callanane2ef6e32010-09-23 03:01:22 +0000374 result_decl_group_ptr = m_sema->ConvertDeclToDeclGroup(result_decl);
Sean Callanan116be532010-07-01 20:08:22 +0000375
376 ////////////////////////
377 // call ActOnDeclStmt
378 //
379
Sean Callanane2ef6e32010-09-23 03:01:22 +0000380 StmtResult result_initialization_stmt_result(m_sema->ActOnDeclStmt(result_decl_group_ptr,
381 SourceLocation(),
382 SourceLocation()));
Sean Callanan116be532010-07-01 20:08:22 +0000383
Sean Callanan116be532010-07-01 20:08:22 +0000384 ////////////////////////////////////////////////
385 // replace the old statement with the new one
386 //
387
Sean Callananddb46ef2010-07-24 01:37:44 +0000388 *last_stmt_ptr = reinterpret_cast<Stmt*>(result_initialization_stmt_result.take());
Sean Callanan116be532010-07-01 20:08:22 +0000389
Sean Callanan116be532010-07-01 20:08:22 +0000390 return true;
391}
392
393void
Sean Callanan1a8d4092010-08-27 01:01:44 +0000394ASTResultSynthesizer::HandleTranslationUnit(ASTContext &Ctx)
Sean Callanan116be532010-07-01 20:08:22 +0000395{
396 if (m_passthrough)
397 m_passthrough->HandleTranslationUnit(Ctx);
398}
399
400void
Sean Callanan1a8d4092010-08-27 01:01:44 +0000401ASTResultSynthesizer::HandleTagDeclDefinition(TagDecl *D)
Sean Callanan116be532010-07-01 20:08:22 +0000402{
403 if (m_passthrough)
404 m_passthrough->HandleTagDeclDefinition(D);
405}
406
407void
Sean Callanan1a8d4092010-08-27 01:01:44 +0000408ASTResultSynthesizer::CompleteTentativeDefinition(VarDecl *D)
Sean Callanan116be532010-07-01 20:08:22 +0000409{
410 if (m_passthrough)
411 m_passthrough->CompleteTentativeDefinition(D);
412}
413
414void
Sean Callanan1a8d4092010-08-27 01:01:44 +0000415ASTResultSynthesizer::HandleVTable(CXXRecordDecl *RD, bool DefinitionRequired)
Sean Callanan116be532010-07-01 20:08:22 +0000416{
417 if (m_passthrough)
418 m_passthrough->HandleVTable(RD, DefinitionRequired);
419}
420
421void
Sean Callanan1a8d4092010-08-27 01:01:44 +0000422ASTResultSynthesizer::PrintStats()
Sean Callanan116be532010-07-01 20:08:22 +0000423{
424 if (m_passthrough)
425 m_passthrough->PrintStats();
426}
427
428void
Sean Callanan1a8d4092010-08-27 01:01:44 +0000429ASTResultSynthesizer::InitializeSema(Sema &S)
Sean Callanan116be532010-07-01 20:08:22 +0000430{
431 m_sema = &S;
Sean Callanan116be532010-07-01 20:08:22 +0000432
433 if (m_passthrough_sema)
434 m_passthrough_sema->InitializeSema(S);
435}
436
437void
Sean Callanan1a8d4092010-08-27 01:01:44 +0000438ASTResultSynthesizer::ForgetSema()
Sean Callanan116be532010-07-01 20:08:22 +0000439{
440 m_sema = NULL;
Sean Callanan116be532010-07-01 20:08:22 +0000441
442 if (m_passthrough_sema)
443 m_passthrough_sema->ForgetSema();
444}