blob: 0cdcf2a110d7a073e0e88634fc195f155b2a09c7 [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 Callanan65dafa82010-08-27 01:01:44 +000027ASTResultSynthesizer::ASTResultSynthesizer(ASTConsumer *passthrough) :
Greg Clayton980d0672010-07-12 23:14:00 +000028 m_ast_context (NULL),
29 m_passthrough (passthrough),
30 m_passthrough_sema (NULL),
Sean Callanan47a5c4c2010-09-23 03:01:22 +000031 m_sema (NULL)
Sean Callanan8c6934d2010-07-01 20:08:22 +000032{
33 if (!m_passthrough)
34 return;
35
36 m_passthrough_sema = dyn_cast<SemaConsumer>(passthrough);
37}
38
Sean Callanan65dafa82010-08-27 01:01:44 +000039ASTResultSynthesizer::~ASTResultSynthesizer()
Sean Callanan8c6934d2010-07-01 20:08:22 +000040{
41}
42
43void
Sean Callanan65dafa82010-08-27 01:01:44 +000044ASTResultSynthesizer::Initialize(ASTContext &Context)
Sean Callanan8c6934d2010-07-01 20:08:22 +000045{
46 m_ast_context = &Context;
47
48 if (m_passthrough)
49 m_passthrough->Initialize(Context);
50}
51
52void
Sean Callanan65dafa82010-08-27 01:01:44 +000053ASTResultSynthesizer::TransformTopLevelDecl(Decl* D)
Sean Callanan8c6934d2010-07-01 20:08:22 +000054{
55 LinkageSpecDecl *linkage_spec_decl = dyn_cast<LinkageSpecDecl>(D);
56
57 if (linkage_spec_decl)
58 {
59 RecordDecl::decl_iterator decl_iterator;
60
61 for (decl_iterator = linkage_spec_decl->decls_begin();
62 decl_iterator != linkage_spec_decl->decls_end();
63 ++decl_iterator)
64 {
65 TransformTopLevelDecl(*decl_iterator);
66 }
67 }
68
69 FunctionDecl *function_decl = dyn_cast<FunctionDecl>(D);
70
71 if (m_ast_context &&
72 function_decl &&
Sean Callanan47a5c4c2010-09-23 03:01:22 +000073 !function_decl->getNameInfo().getAsString().compare("___clang_expr"))
Sean Callanan8c6934d2010-07-01 20:08:22 +000074 {
Sean Callananbfc33ce2010-08-16 23:01:35 +000075 SynthesizeResult(function_decl);
Sean Callanan8c6934d2010-07-01 20:08:22 +000076 }
77}
78
79void
Sean Callanan65dafa82010-08-27 01:01:44 +000080ASTResultSynthesizer::HandleTopLevelDecl(DeclGroupRef D)
Sean Callanan8c6934d2010-07-01 20:08:22 +000081{
82 DeclGroupRef::iterator decl_iterator;
83
84 for (decl_iterator = D.begin();
85 decl_iterator != D.end();
86 ++decl_iterator)
87 {
88 Decl *decl = *decl_iterator;
89
90 TransformTopLevelDecl(decl);
91 }
92
93 if (m_passthrough)
94 m_passthrough->HandleTopLevelDecl(D);
95}
96
97bool
Sean Callanan65dafa82010-08-27 01:01:44 +000098ASTResultSynthesizer::SynthesizeResult (FunctionDecl *FunDecl)
Sean Callanan8c6934d2010-07-01 20:08:22 +000099{
Sean Callananbfc33ce2010-08-16 23:01:35 +0000100 ASTContext &Ctx(*m_ast_context);
101
Sean Callanan8c6934d2010-07-01 20:08:22 +0000102 Log *log = lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS);
103
104 if (!m_sema)
105 return false;
106
107 FunctionDecl *function_decl = FunDecl;
108
109 if (!function_decl)
110 return false;
111
Sean Callanane8a59a82010-09-13 21:34:21 +0000112 if (log)
113 {
114 std::string s;
115 raw_string_ostream os(s);
116
Sean Callanan6288a652010-09-22 00:33:31 +0000117 Ctx.getTranslationUnitDecl()->print(os);
Sean Callanane8a59a82010-09-13 21:34:21 +0000118
119 os.flush();
120
Sean Callanan6288a652010-09-22 00:33:31 +0000121 log->Printf("AST context before transforming:\n%s", s.c_str());
Sean Callanane8a59a82010-09-13 21:34:21 +0000122 }
123
Sean Callanan8c6934d2010-07-01 20:08:22 +0000124 Stmt *function_body = function_decl->getBody();
125 CompoundStmt *compound_stmt = dyn_cast<CompoundStmt>(function_body);
126
127 if (!compound_stmt)
128 return false;
129
130 if (compound_stmt->body_empty())
131 return false;
132
133 Stmt **last_stmt_ptr = compound_stmt->body_end() - 1;
134 Stmt *last_stmt = *last_stmt_ptr;
135
Sean Callanane8a59a82010-09-13 21:34:21 +0000136 while (dyn_cast<NullStmt>(last_stmt))
137 {
138 if (last_stmt_ptr != compound_stmt->body_begin())
139 {
140 last_stmt_ptr--;
141 last_stmt = *last_stmt_ptr;
142 }
143 }
144
Sean Callanan8c6934d2010-07-01 20:08:22 +0000145 Expr *last_expr = dyn_cast<Expr>(last_stmt);
146
147 if (!last_expr)
148 // No auxiliary variable necessary; expression returns void
149 return true;
150
151 QualType expr_qual_type = last_expr->getType();
152 clang::Type *expr_type = expr_qual_type.getTypePtr();
153
154 if (!expr_type)
155 return false;
156
157 if (expr_type->isVoidType())
158 return true;
159
160 if (log)
161 {
162 std::string s = expr_qual_type.getAsString();
163
164 log->Printf("Last statement's type: %s", s.c_str());
165 }
166
167 IdentifierInfo &result_id = Ctx.Idents.get("___clang_expr_result");
Sean Callanan45839272010-07-24 01:37:44 +0000168
Sean Callanan8c6934d2010-07-01 20:08:22 +0000169 clang::VarDecl *result_decl = VarDecl::Create(Ctx,
170 function_decl,
171 SourceLocation(),
172 &result_id,
173 expr_qual_type,
174 NULL,
Sean Callanan47a5c4c2010-09-23 03:01:22 +0000175 SC_Static,
176 SC_Static);
Sean Callanan8c6934d2010-07-01 20:08:22 +0000177
178 if (!result_decl)
179 return false;
180
181 function_decl->addDecl(result_decl);
182
183 ///////////////////////////////
184 // call AddInitializerToDecl
185 //
Sean Callanan47a5c4c2010-09-23 03:01:22 +0000186
187 m_sema->AddInitializerToDecl(result_decl, last_expr);
Sean Callanan8c6934d2010-07-01 20:08:22 +0000188
189 /////////////////////////////////
190 // call ConvertDeclToDeclGroup
191 //
192
Sean Callanan47a5c4c2010-09-23 03:01:22 +0000193 Sema::DeclGroupPtrTy result_decl_group_ptr;
Sean Callanan8c6934d2010-07-01 20:08:22 +0000194
Sean Callanan47a5c4c2010-09-23 03:01:22 +0000195 result_decl_group_ptr = m_sema->ConvertDeclToDeclGroup(result_decl);
Sean Callanan8c6934d2010-07-01 20:08:22 +0000196
197 ////////////////////////
198 // call ActOnDeclStmt
199 //
200
Sean Callanan47a5c4c2010-09-23 03:01:22 +0000201 StmtResult result_initialization_stmt_result(m_sema->ActOnDeclStmt(result_decl_group_ptr,
202 SourceLocation(),
203 SourceLocation()));
Sean Callanan8c6934d2010-07-01 20:08:22 +0000204
Sean Callanan8c6934d2010-07-01 20:08:22 +0000205 ////////////////////////////////////////////////
206 // replace the old statement with the new one
207 //
208
Sean Callanan45839272010-07-24 01:37:44 +0000209 *last_stmt_ptr = reinterpret_cast<Stmt*>(result_initialization_stmt_result.take());
Sean Callanan8c6934d2010-07-01 20:08:22 +0000210
211 if (log)
212 {
213 std::string s;
214 raw_string_ostream os(s);
215
216 function_decl->print(os);
217
218 os.flush();
219
220 log->Printf("Transformed function AST:\n%s", s.c_str());
221 }
222
223 return true;
224}
225
226void
Sean Callanan65dafa82010-08-27 01:01:44 +0000227ASTResultSynthesizer::HandleTranslationUnit(ASTContext &Ctx)
Sean Callanan8c6934d2010-07-01 20:08:22 +0000228{
229 if (m_passthrough)
230 m_passthrough->HandleTranslationUnit(Ctx);
231}
232
233void
Sean Callanan65dafa82010-08-27 01:01:44 +0000234ASTResultSynthesizer::HandleTagDeclDefinition(TagDecl *D)
Sean Callanan8c6934d2010-07-01 20:08:22 +0000235{
236 if (m_passthrough)
237 m_passthrough->HandleTagDeclDefinition(D);
238}
239
240void
Sean Callanan65dafa82010-08-27 01:01:44 +0000241ASTResultSynthesizer::CompleteTentativeDefinition(VarDecl *D)
Sean Callanan8c6934d2010-07-01 20:08:22 +0000242{
243 if (m_passthrough)
244 m_passthrough->CompleteTentativeDefinition(D);
245}
246
247void
Sean Callanan65dafa82010-08-27 01:01:44 +0000248ASTResultSynthesizer::HandleVTable(CXXRecordDecl *RD, bool DefinitionRequired)
Sean Callanan8c6934d2010-07-01 20:08:22 +0000249{
250 if (m_passthrough)
251 m_passthrough->HandleVTable(RD, DefinitionRequired);
252}
253
254void
Sean Callanan65dafa82010-08-27 01:01:44 +0000255ASTResultSynthesizer::PrintStats()
Sean Callanan8c6934d2010-07-01 20:08:22 +0000256{
257 if (m_passthrough)
258 m_passthrough->PrintStats();
259}
260
261void
Sean Callanan65dafa82010-08-27 01:01:44 +0000262ASTResultSynthesizer::InitializeSema(Sema &S)
Sean Callanan8c6934d2010-07-01 20:08:22 +0000263{
264 m_sema = &S;
Sean Callanan8c6934d2010-07-01 20:08:22 +0000265
266 if (m_passthrough_sema)
267 m_passthrough_sema->InitializeSema(S);
268}
269
270void
Sean Callanan65dafa82010-08-27 01:01:44 +0000271ASTResultSynthesizer::ForgetSema()
Sean Callanan8c6934d2010-07-01 20:08:22 +0000272{
273 m_sema = NULL;
Sean Callanan8c6934d2010-07-01 20:08:22 +0000274
275 if (m_passthrough_sema)
276 m_passthrough_sema->ForgetSema();
277}