1dda28197Spatrick //===-- ASTStructExtractor.cpp --------------------------------------------===//
2061da546Spatrick //
3061da546Spatrick // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4061da546Spatrick // See https://llvm.org/LICENSE.txt for license information.
5061da546Spatrick // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6061da546Spatrick //
7061da546Spatrick //===----------------------------------------------------------------------===//
8061da546Spatrick
9061da546Spatrick #include "ASTStructExtractor.h"
10061da546Spatrick
11061da546Spatrick #include "lldb/Utility/Log.h"
12061da546Spatrick #include "clang/AST/ASTContext.h"
13061da546Spatrick #include "clang/AST/Decl.h"
14061da546Spatrick #include "clang/AST/DeclCXX.h"
15061da546Spatrick #include "clang/AST/DeclGroup.h"
16061da546Spatrick #include "clang/AST/Expr.h"
17061da546Spatrick #include "clang/AST/RecordLayout.h"
18061da546Spatrick #include "clang/AST/Stmt.h"
19061da546Spatrick #include "clang/Parse/Parser.h"
20061da546Spatrick #include "clang/Sema/Sema.h"
21061da546Spatrick #include "llvm/Support/Casting.h"
22061da546Spatrick #include "llvm/Support/raw_ostream.h"
23*be691f3bSpatrick #include <cstdlib>
24061da546Spatrick
25061da546Spatrick using namespace llvm;
26061da546Spatrick using namespace clang;
27061da546Spatrick using namespace lldb_private;
28061da546Spatrick
ASTStructExtractor(ASTConsumer * passthrough,const char * struct_name,ClangFunctionCaller & function)29061da546Spatrick ASTStructExtractor::ASTStructExtractor(ASTConsumer *passthrough,
30061da546Spatrick const char *struct_name,
31061da546Spatrick ClangFunctionCaller &function)
32061da546Spatrick : m_ast_context(nullptr), m_passthrough(passthrough),
33061da546Spatrick m_passthrough_sema(nullptr), m_sema(nullptr), m_function(function),
34061da546Spatrick m_struct_name(struct_name) {
35061da546Spatrick if (!m_passthrough)
36061da546Spatrick return;
37061da546Spatrick
38061da546Spatrick m_passthrough_sema = dyn_cast<SemaConsumer>(passthrough);
39061da546Spatrick }
40061da546Spatrick
41*be691f3bSpatrick ASTStructExtractor::~ASTStructExtractor() = default;
42061da546Spatrick
Initialize(ASTContext & Context)43061da546Spatrick void ASTStructExtractor::Initialize(ASTContext &Context) {
44061da546Spatrick m_ast_context = &Context;
45061da546Spatrick
46061da546Spatrick if (m_passthrough)
47061da546Spatrick m_passthrough->Initialize(Context);
48061da546Spatrick }
49061da546Spatrick
ExtractFromFunctionDecl(FunctionDecl * F)50061da546Spatrick void ASTStructExtractor::ExtractFromFunctionDecl(FunctionDecl *F) {
51061da546Spatrick if (!F->hasBody())
52061da546Spatrick return;
53061da546Spatrick
54061da546Spatrick Stmt *body_stmt = F->getBody();
55061da546Spatrick CompoundStmt *body_compound_stmt = dyn_cast<CompoundStmt>(body_stmt);
56061da546Spatrick
57061da546Spatrick if (!body_compound_stmt)
58061da546Spatrick return; // do we have to handle this?
59061da546Spatrick
60061da546Spatrick RecordDecl *struct_decl = nullptr;
61061da546Spatrick
62061da546Spatrick StringRef desired_name(m_struct_name);
63061da546Spatrick
64061da546Spatrick for (CompoundStmt::const_body_iterator bi = body_compound_stmt->body_begin(),
65061da546Spatrick be = body_compound_stmt->body_end();
66061da546Spatrick bi != be; ++bi) {
67061da546Spatrick Stmt *curr_stmt = *bi;
68061da546Spatrick DeclStmt *curr_decl_stmt = dyn_cast<DeclStmt>(curr_stmt);
69061da546Spatrick if (!curr_decl_stmt)
70061da546Spatrick continue;
71061da546Spatrick DeclGroupRef decl_group = curr_decl_stmt->getDeclGroup();
72061da546Spatrick for (Decl *candidate_decl : decl_group) {
73061da546Spatrick RecordDecl *candidate_record_decl = dyn_cast<RecordDecl>(candidate_decl);
74061da546Spatrick if (!candidate_record_decl)
75061da546Spatrick continue;
76061da546Spatrick if (candidate_record_decl->getName() == desired_name) {
77061da546Spatrick struct_decl = candidate_record_decl;
78061da546Spatrick break;
79061da546Spatrick }
80061da546Spatrick }
81061da546Spatrick if (struct_decl)
82061da546Spatrick break;
83061da546Spatrick }
84061da546Spatrick
85061da546Spatrick if (!struct_decl)
86061da546Spatrick return;
87061da546Spatrick
88061da546Spatrick const ASTRecordLayout *struct_layout(
89061da546Spatrick &m_ast_context->getASTRecordLayout(struct_decl));
90061da546Spatrick
91061da546Spatrick if (!struct_layout)
92061da546Spatrick return;
93061da546Spatrick
94061da546Spatrick m_function.m_struct_size =
95061da546Spatrick struct_layout->getSize()
96061da546Spatrick .getQuantity(); // TODO Store m_struct_size as CharUnits
97061da546Spatrick m_function.m_return_offset =
98061da546Spatrick struct_layout->getFieldOffset(struct_layout->getFieldCount() - 1) / 8;
99061da546Spatrick m_function.m_return_size =
100061da546Spatrick struct_layout->getDataSize().getQuantity() - m_function.m_return_offset;
101061da546Spatrick
102061da546Spatrick for (unsigned field_index = 0, num_fields = struct_layout->getFieldCount();
103061da546Spatrick field_index < num_fields; ++field_index) {
104061da546Spatrick m_function.m_member_offsets.push_back(
105061da546Spatrick struct_layout->getFieldOffset(field_index) / 8);
106061da546Spatrick }
107061da546Spatrick
108061da546Spatrick m_function.m_struct_valid = true;
109061da546Spatrick }
110061da546Spatrick
ExtractFromTopLevelDecl(Decl * D)111061da546Spatrick void ASTStructExtractor::ExtractFromTopLevelDecl(Decl *D) {
112061da546Spatrick LinkageSpecDecl *linkage_spec_decl = dyn_cast<LinkageSpecDecl>(D);
113061da546Spatrick
114061da546Spatrick if (linkage_spec_decl) {
115061da546Spatrick RecordDecl::decl_iterator decl_iterator;
116061da546Spatrick
117061da546Spatrick for (decl_iterator = linkage_spec_decl->decls_begin();
118061da546Spatrick decl_iterator != linkage_spec_decl->decls_end(); ++decl_iterator) {
119061da546Spatrick ExtractFromTopLevelDecl(*decl_iterator);
120061da546Spatrick }
121061da546Spatrick }
122061da546Spatrick
123061da546Spatrick FunctionDecl *function_decl = dyn_cast<FunctionDecl>(D);
124061da546Spatrick
125061da546Spatrick if (m_ast_context && function_decl &&
126061da546Spatrick !m_function.m_wrapper_function_name.compare(
127061da546Spatrick function_decl->getNameAsString())) {
128061da546Spatrick ExtractFromFunctionDecl(function_decl);
129061da546Spatrick }
130061da546Spatrick }
131061da546Spatrick
HandleTopLevelDecl(DeclGroupRef D)132061da546Spatrick bool ASTStructExtractor::HandleTopLevelDecl(DeclGroupRef D) {
133061da546Spatrick DeclGroupRef::iterator decl_iterator;
134061da546Spatrick
135061da546Spatrick for (decl_iterator = D.begin(); decl_iterator != D.end(); ++decl_iterator) {
136061da546Spatrick Decl *decl = *decl_iterator;
137061da546Spatrick
138061da546Spatrick ExtractFromTopLevelDecl(decl);
139061da546Spatrick }
140061da546Spatrick
141061da546Spatrick if (m_passthrough)
142061da546Spatrick return m_passthrough->HandleTopLevelDecl(D);
143061da546Spatrick return true;
144061da546Spatrick }
145061da546Spatrick
HandleTranslationUnit(ASTContext & Ctx)146061da546Spatrick void ASTStructExtractor::HandleTranslationUnit(ASTContext &Ctx) {
147061da546Spatrick if (m_passthrough)
148061da546Spatrick m_passthrough->HandleTranslationUnit(Ctx);
149061da546Spatrick }
150061da546Spatrick
HandleTagDeclDefinition(TagDecl * D)151061da546Spatrick void ASTStructExtractor::HandleTagDeclDefinition(TagDecl *D) {
152061da546Spatrick if (m_passthrough)
153061da546Spatrick m_passthrough->HandleTagDeclDefinition(D);
154061da546Spatrick }
155061da546Spatrick
CompleteTentativeDefinition(VarDecl * D)156061da546Spatrick void ASTStructExtractor::CompleteTentativeDefinition(VarDecl *D) {
157061da546Spatrick if (m_passthrough)
158061da546Spatrick m_passthrough->CompleteTentativeDefinition(D);
159061da546Spatrick }
160061da546Spatrick
HandleVTable(CXXRecordDecl * RD)161061da546Spatrick void ASTStructExtractor::HandleVTable(CXXRecordDecl *RD) {
162061da546Spatrick if (m_passthrough)
163061da546Spatrick m_passthrough->HandleVTable(RD);
164061da546Spatrick }
165061da546Spatrick
PrintStats()166061da546Spatrick void ASTStructExtractor::PrintStats() {
167061da546Spatrick if (m_passthrough)
168061da546Spatrick m_passthrough->PrintStats();
169061da546Spatrick }
170061da546Spatrick
InitializeSema(Sema & S)171061da546Spatrick void ASTStructExtractor::InitializeSema(Sema &S) {
172061da546Spatrick m_sema = &S;
173061da546Spatrick
174061da546Spatrick if (m_passthrough_sema)
175061da546Spatrick m_passthrough_sema->InitializeSema(S);
176061da546Spatrick }
177061da546Spatrick
ForgetSema()178061da546Spatrick void ASTStructExtractor::ForgetSema() {
179061da546Spatrick m_sema = nullptr;
180061da546Spatrick
181061da546Spatrick if (m_passthrough_sema)
182061da546Spatrick m_passthrough_sema->ForgetSema();
183061da546Spatrick }
184