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 //===----------------------------------------------------------------------===//
10 #include "ASTStructExtractor.h"
12 #include "lldb/Core/Log.h"
14 #include "clang/AST/ASTContext.h"
15 #include "clang/AST/Decl.h"
16 #include "clang/AST/DeclCXX.h"
17 #include "clang/AST/DeclGroup.h"
18 #include "clang/AST/Expr.h"
19 #include "clang/AST/RecordLayout.h"
20 #include "clang/AST/Stmt.h"
21 #include "clang/Parse/Parser.h"
22 #include "clang/Sema/Sema.h"
23 #include "llvm/Support/Casting.h"
24 #include "llvm/Support/raw_ostream.h"
27 using namespace clang;
28 using namespace lldb_private;
30 ASTStructExtractor::ASTStructExtractor(ASTConsumer *passthrough,
31 const char *struct_name,
32 ClangFunctionCaller &function)
33 : m_ast_context(NULL), m_passthrough(passthrough), m_passthrough_sema(NULL),
34 m_sema(NULL), m_action(NULL), m_function(function),
35 m_struct_name(struct_name) {
39 m_passthrough_sema = dyn_cast<SemaConsumer>(passthrough);
42 ASTStructExtractor::~ASTStructExtractor() {}
44 void ASTStructExtractor::Initialize(ASTContext &Context) {
45 m_ast_context = &Context;
48 m_passthrough->Initialize(Context);
51 void ASTStructExtractor::ExtractFromFunctionDecl(FunctionDecl *F) {
55 Stmt *body_stmt = F->getBody();
56 CompoundStmt *body_compound_stmt = dyn_cast<CompoundStmt>(body_stmt);
58 if (!body_compound_stmt)
59 return; // do we have to handle this?
61 RecordDecl *struct_decl = NULL;
63 StringRef desired_name(m_struct_name);
65 for (CompoundStmt::const_body_iterator bi = body_compound_stmt->body_begin(),
66 be = body_compound_stmt->body_end();
68 Stmt *curr_stmt = *bi;
69 DeclStmt *curr_decl_stmt = dyn_cast<DeclStmt>(curr_stmt);
72 DeclGroupRef decl_group = curr_decl_stmt->getDeclGroup();
73 for (Decl *candidate_decl : decl_group) {
74 RecordDecl *candidate_record_decl = dyn_cast<RecordDecl>(candidate_decl);
75 if (!candidate_record_decl)
77 if (candidate_record_decl->getName() == desired_name) {
78 struct_decl = candidate_record_decl;
89 const ASTRecordLayout *struct_layout(
90 &m_ast_context->getASTRecordLayout(struct_decl));
95 m_function.m_struct_size =
96 struct_layout->getSize()
97 .getQuantity(); // TODO Store m_struct_size as CharUnits
98 m_function.m_return_offset =
99 struct_layout->getFieldOffset(struct_layout->getFieldCount() - 1) / 8;
100 m_function.m_return_size =
101 struct_layout->getDataSize().getQuantity() - m_function.m_return_offset;
103 for (unsigned field_index = 0, num_fields = struct_layout->getFieldCount();
104 field_index < num_fields; ++field_index) {
105 m_function.m_member_offsets.push_back(
106 struct_layout->getFieldOffset(field_index) / 8);
109 m_function.m_struct_valid = true;
112 void ASTStructExtractor::ExtractFromTopLevelDecl(Decl *D) {
113 LinkageSpecDecl *linkage_spec_decl = dyn_cast<LinkageSpecDecl>(D);
115 if (linkage_spec_decl) {
116 RecordDecl::decl_iterator decl_iterator;
118 for (decl_iterator = linkage_spec_decl->decls_begin();
119 decl_iterator != linkage_spec_decl->decls_end(); ++decl_iterator) {
120 ExtractFromTopLevelDecl(*decl_iterator);
124 FunctionDecl *function_decl = dyn_cast<FunctionDecl>(D);
126 if (m_ast_context && function_decl &&
127 !m_function.m_wrapper_function_name.compare(
128 function_decl->getNameAsString())) {
129 ExtractFromFunctionDecl(function_decl);
133 bool ASTStructExtractor::HandleTopLevelDecl(DeclGroupRef D) {
134 DeclGroupRef::iterator decl_iterator;
136 for (decl_iterator = D.begin(); decl_iterator != D.end(); ++decl_iterator) {
137 Decl *decl = *decl_iterator;
139 ExtractFromTopLevelDecl(decl);
143 return m_passthrough->HandleTopLevelDecl(D);
147 void ASTStructExtractor::HandleTranslationUnit(ASTContext &Ctx) {
149 m_passthrough->HandleTranslationUnit(Ctx);
152 void ASTStructExtractor::HandleTagDeclDefinition(TagDecl *D) {
154 m_passthrough->HandleTagDeclDefinition(D);
157 void ASTStructExtractor::CompleteTentativeDefinition(VarDecl *D) {
159 m_passthrough->CompleteTentativeDefinition(D);
162 void ASTStructExtractor::HandleVTable(CXXRecordDecl *RD) {
164 m_passthrough->HandleVTable(RD);
167 void ASTStructExtractor::PrintStats() {
169 m_passthrough->PrintStats();
172 void ASTStructExtractor::InitializeSema(Sema &S) {
174 m_action = reinterpret_cast<Action *>(m_sema);
176 if (m_passthrough_sema)
177 m_passthrough_sema->InitializeSema(S);
180 void ASTStructExtractor::ForgetSema() {
184 if (m_passthrough_sema)
185 m_passthrough_sema->ForgetSema();