1 //===-- ASTStructExtractor.cpp ----------------------------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
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/RecordLayout.h"
17 #include "clang/AST/Stmt.h"
18 #include "clang/Parse/Parser.h"
19 #include "clang/Sema/Sema.h"
20 #include "llvm/Support/Casting.h"
21 #include "llvm/Support/raw_ostream.h"
22 #include "lldb/Core/Log.h"
23 #include "lldb/Expression/ASTStructExtractor.h"
26 using namespace clang;
27 using namespace lldb_private;
29 ASTStructExtractor::ASTStructExtractor(ASTConsumer *passthrough,
30 const char *struct_name,
31 ClangFunction &function) :
33 m_passthrough (passthrough),
34 m_passthrough_sema (NULL),
37 m_function (function),
38 m_struct_name (struct_name)
43 m_passthrough_sema = dyn_cast<SemaConsumer>(passthrough);
46 ASTStructExtractor::~ASTStructExtractor()
51 ASTStructExtractor::Initialize(ASTContext &Context)
53 m_ast_context = &Context;
56 m_passthrough->Initialize(Context);
60 ASTStructExtractor::ExtractFromFunctionDecl(FunctionDecl *F)
65 Stmt *body_stmt = F->getBody();
66 CompoundStmt *body_compound_stmt = dyn_cast<CompoundStmt>(body_stmt);
68 if (!body_compound_stmt)
69 return; // do we have to handle this?
71 RecordDecl *struct_decl = NULL;
73 StringRef desired_name(m_struct_name.c_str());
75 for (CompoundStmt::const_body_iterator bi = body_compound_stmt->body_begin(), be = body_compound_stmt->body_end();
79 Stmt *curr_stmt = *bi;
80 DeclStmt *curr_decl_stmt = dyn_cast<DeclStmt>(curr_stmt);
83 DeclGroupRef decl_group = curr_decl_stmt->getDeclGroup();
84 for (Decl *candidate_decl : decl_group)
86 RecordDecl *candidate_record_decl = dyn_cast<RecordDecl>(candidate_decl);
87 if (!candidate_record_decl)
89 if (candidate_record_decl->getName() == desired_name)
91 struct_decl = candidate_record_decl;
102 const ASTRecordLayout* struct_layout(&m_ast_context->getASTRecordLayout (struct_decl));
107 m_function.m_struct_size = struct_layout->getSize().getQuantity(); // TODO Store m_struct_size as CharUnits
108 m_function.m_return_offset = struct_layout->getFieldOffset(struct_layout->getFieldCount() - 1) / 8;
109 m_function.m_return_size = struct_layout->getDataSize().getQuantity() - m_function.m_return_offset;
111 for (unsigned field_index = 0, num_fields = struct_layout->getFieldCount();
112 field_index < num_fields;
115 m_function.m_member_offsets.push_back(struct_layout->getFieldOffset(field_index) / 8);
118 m_function.m_struct_valid = true;
122 ASTStructExtractor::ExtractFromTopLevelDecl(Decl* D)
124 LinkageSpecDecl *linkage_spec_decl = dyn_cast<LinkageSpecDecl>(D);
126 if (linkage_spec_decl)
128 RecordDecl::decl_iterator decl_iterator;
130 for (decl_iterator = linkage_spec_decl->decls_begin();
131 decl_iterator != linkage_spec_decl->decls_end();
134 ExtractFromTopLevelDecl(*decl_iterator);
138 FunctionDecl *function_decl = dyn_cast<FunctionDecl>(D);
142 !m_function.m_wrapper_function_name.compare(function_decl->getNameAsString().c_str()))
144 ExtractFromFunctionDecl(function_decl);
149 ASTStructExtractor::HandleTopLevelDecl(DeclGroupRef D)
151 DeclGroupRef::iterator decl_iterator;
153 for (decl_iterator = D.begin();
154 decl_iterator != D.end();
157 Decl *decl = *decl_iterator;
159 ExtractFromTopLevelDecl(decl);
163 return m_passthrough->HandleTopLevelDecl(D);
168 ASTStructExtractor::HandleTranslationUnit(ASTContext &Ctx)
171 m_passthrough->HandleTranslationUnit(Ctx);
175 ASTStructExtractor::HandleTagDeclDefinition(TagDecl *D)
178 m_passthrough->HandleTagDeclDefinition(D);
182 ASTStructExtractor::CompleteTentativeDefinition(VarDecl *D)
185 m_passthrough->CompleteTentativeDefinition(D);
189 ASTStructExtractor::HandleVTable(CXXRecordDecl *RD, bool DefinitionRequired)
192 m_passthrough->HandleVTable(RD, DefinitionRequired);
196 ASTStructExtractor::PrintStats()
199 m_passthrough->PrintStats();
203 ASTStructExtractor::InitializeSema(Sema &S)
206 m_action = reinterpret_cast<Action*>(m_sema);
208 if (m_passthrough_sema)
209 m_passthrough_sema->InitializeSema(S);
213 ASTStructExtractor::ForgetSema()
218 if (m_passthrough_sema)
219 m_passthrough_sema->ForgetSema();