1cfca06d7SDimitry Andric //===-- ASTStructExtractor.cpp --------------------------------------------===//
2f034231aSEd Maste //
35f29bb8aSDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
45f29bb8aSDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
55f29bb8aSDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6f034231aSEd Maste //
7f034231aSEd Maste //===----------------------------------------------------------------------===//
8f034231aSEd Maste
9e81d9d49SDimitry Andric #include "ASTStructExtractor.h"
10e81d9d49SDimitry Andric
1174a628f7SDimitry Andric #include "lldb/Utility/Log.h"
12f034231aSEd Maste #include "clang/AST/ASTContext.h"
13f034231aSEd Maste #include "clang/AST/Decl.h"
14f034231aSEd Maste #include "clang/AST/DeclCXX.h"
15f034231aSEd Maste #include "clang/AST/DeclGroup.h"
16f034231aSEd Maste #include "clang/AST/Expr.h"
17f034231aSEd Maste #include "clang/AST/RecordLayout.h"
18f034231aSEd Maste #include "clang/AST/Stmt.h"
19f034231aSEd Maste #include "clang/Parse/Parser.h"
20f034231aSEd Maste #include "clang/Sema/Sema.h"
21f034231aSEd Maste #include "llvm/Support/Casting.h"
22f034231aSEd Maste #include "llvm/Support/raw_ostream.h"
23344a3780SDimitry Andric #include <cstdlib>
24f034231aSEd Maste
25f034231aSEd Maste using namespace llvm;
26f034231aSEd Maste using namespace clang;
27f034231aSEd Maste using namespace lldb_private;
28f034231aSEd Maste
ASTStructExtractor(ASTConsumer * passthrough,const char * struct_name,ClangFunctionCaller & function)29f034231aSEd Maste ASTStructExtractor::ASTStructExtractor(ASTConsumer *passthrough,
30f034231aSEd Maste const char *struct_name,
3114f1b3e8SDimitry Andric ClangFunctionCaller &function)
325f29bb8aSDimitry Andric : m_ast_context(nullptr), m_passthrough(passthrough),
33706b4fc4SDimitry Andric m_passthrough_sema(nullptr), m_sema(nullptr), m_function(function),
34706b4fc4SDimitry Andric m_struct_name(struct_name) {
35f034231aSEd Maste if (!m_passthrough)
36f034231aSEd Maste return;
37f034231aSEd Maste
38f034231aSEd Maste m_passthrough_sema = dyn_cast<SemaConsumer>(passthrough);
39f034231aSEd Maste }
40f034231aSEd Maste
41344a3780SDimitry Andric ASTStructExtractor::~ASTStructExtractor() = default;
42f034231aSEd Maste
Initialize(ASTContext & Context)4314f1b3e8SDimitry Andric void ASTStructExtractor::Initialize(ASTContext &Context) {
44f034231aSEd Maste m_ast_context = &Context;
45f034231aSEd Maste
46f034231aSEd Maste if (m_passthrough)
47f034231aSEd Maste m_passthrough->Initialize(Context);
48f034231aSEd Maste }
49f034231aSEd Maste
ExtractFromFunctionDecl(FunctionDecl * F)5014f1b3e8SDimitry Andric void ASTStructExtractor::ExtractFromFunctionDecl(FunctionDecl *F) {
51f034231aSEd Maste if (!F->hasBody())
52f034231aSEd Maste return;
53f034231aSEd Maste
54f034231aSEd Maste Stmt *body_stmt = F->getBody();
55f034231aSEd Maste CompoundStmt *body_compound_stmt = dyn_cast<CompoundStmt>(body_stmt);
56f034231aSEd Maste
57f034231aSEd Maste if (!body_compound_stmt)
58f034231aSEd Maste return; // do we have to handle this?
59f034231aSEd Maste
605f29bb8aSDimitry Andric RecordDecl *struct_decl = nullptr;
61f034231aSEd Maste
6214f1b3e8SDimitry Andric StringRef desired_name(m_struct_name);
63f034231aSEd Maste
6414f1b3e8SDimitry Andric for (CompoundStmt::const_body_iterator bi = body_compound_stmt->body_begin(),
6514f1b3e8SDimitry Andric be = body_compound_stmt->body_end();
6614f1b3e8SDimitry Andric bi != be; ++bi) {
67f034231aSEd Maste Stmt *curr_stmt = *bi;
68f034231aSEd Maste DeclStmt *curr_decl_stmt = dyn_cast<DeclStmt>(curr_stmt);
69f034231aSEd Maste if (!curr_decl_stmt)
70f034231aSEd Maste continue;
71f034231aSEd Maste DeclGroupRef decl_group = curr_decl_stmt->getDeclGroup();
7214f1b3e8SDimitry Andric for (Decl *candidate_decl : decl_group) {
73f034231aSEd Maste RecordDecl *candidate_record_decl = dyn_cast<RecordDecl>(candidate_decl);
74f034231aSEd Maste if (!candidate_record_decl)
75f034231aSEd Maste continue;
7614f1b3e8SDimitry Andric if (candidate_record_decl->getName() == desired_name) {
77f034231aSEd Maste struct_decl = candidate_record_decl;
78f034231aSEd Maste break;
79f034231aSEd Maste }
80f034231aSEd Maste }
81f034231aSEd Maste if (struct_decl)
82f034231aSEd Maste break;
83f034231aSEd Maste }
84f034231aSEd Maste
85f034231aSEd Maste if (!struct_decl)
86f034231aSEd Maste return;
87f034231aSEd Maste
8814f1b3e8SDimitry Andric const ASTRecordLayout *struct_layout(
8914f1b3e8SDimitry Andric &m_ast_context->getASTRecordLayout(struct_decl));
90f034231aSEd Maste
91f034231aSEd Maste if (!struct_layout)
92f034231aSEd Maste return;
93f034231aSEd Maste
9414f1b3e8SDimitry Andric m_function.m_struct_size =
9514f1b3e8SDimitry Andric struct_layout->getSize()
9614f1b3e8SDimitry Andric .getQuantity(); // TODO Store m_struct_size as CharUnits
9714f1b3e8SDimitry Andric m_function.m_return_offset =
9814f1b3e8SDimitry Andric struct_layout->getFieldOffset(struct_layout->getFieldCount() - 1) / 8;
9914f1b3e8SDimitry Andric m_function.m_return_size =
10014f1b3e8SDimitry Andric struct_layout->getDataSize().getQuantity() - m_function.m_return_offset;
101f034231aSEd Maste
102f034231aSEd Maste for (unsigned field_index = 0, num_fields = struct_layout->getFieldCount();
10314f1b3e8SDimitry Andric field_index < num_fields; ++field_index) {
10414f1b3e8SDimitry Andric m_function.m_member_offsets.push_back(
10514f1b3e8SDimitry Andric struct_layout->getFieldOffset(field_index) / 8);
106f034231aSEd Maste }
107f034231aSEd Maste
108f034231aSEd Maste m_function.m_struct_valid = true;
109f034231aSEd Maste }
110f034231aSEd Maste
ExtractFromTopLevelDecl(Decl * D)11114f1b3e8SDimitry Andric void ASTStructExtractor::ExtractFromTopLevelDecl(Decl *D) {
112f034231aSEd Maste LinkageSpecDecl *linkage_spec_decl = dyn_cast<LinkageSpecDecl>(D);
113f034231aSEd Maste
11414f1b3e8SDimitry Andric if (linkage_spec_decl) {
115f034231aSEd Maste RecordDecl::decl_iterator decl_iterator;
116f034231aSEd Maste
117f034231aSEd Maste for (decl_iterator = linkage_spec_decl->decls_begin();
11814f1b3e8SDimitry Andric decl_iterator != linkage_spec_decl->decls_end(); ++decl_iterator) {
119f034231aSEd Maste ExtractFromTopLevelDecl(*decl_iterator);
120f034231aSEd Maste }
121f034231aSEd Maste }
122f034231aSEd Maste
123f034231aSEd Maste FunctionDecl *function_decl = dyn_cast<FunctionDecl>(D);
124f034231aSEd Maste
12514f1b3e8SDimitry Andric if (m_ast_context && function_decl &&
12614f1b3e8SDimitry Andric !m_function.m_wrapper_function_name.compare(
12714f1b3e8SDimitry Andric function_decl->getNameAsString())) {
128f034231aSEd Maste ExtractFromFunctionDecl(function_decl);
129f034231aSEd Maste }
130f034231aSEd Maste }
131f034231aSEd Maste
HandleTopLevelDecl(DeclGroupRef D)13214f1b3e8SDimitry Andric bool ASTStructExtractor::HandleTopLevelDecl(DeclGroupRef D) {
133f034231aSEd Maste DeclGroupRef::iterator decl_iterator;
134f034231aSEd Maste
13514f1b3e8SDimitry Andric for (decl_iterator = D.begin(); decl_iterator != D.end(); ++decl_iterator) {
136f034231aSEd Maste Decl *decl = *decl_iterator;
137f034231aSEd Maste
138f034231aSEd Maste ExtractFromTopLevelDecl(decl);
139f034231aSEd Maste }
140f034231aSEd Maste
141f034231aSEd Maste if (m_passthrough)
142f034231aSEd Maste return m_passthrough->HandleTopLevelDecl(D);
143f034231aSEd Maste return true;
144f034231aSEd Maste }
145f034231aSEd Maste
HandleTranslationUnit(ASTContext & Ctx)14614f1b3e8SDimitry Andric void ASTStructExtractor::HandleTranslationUnit(ASTContext &Ctx) {
147f034231aSEd Maste if (m_passthrough)
148f034231aSEd Maste m_passthrough->HandleTranslationUnit(Ctx);
149f034231aSEd Maste }
150f034231aSEd Maste
HandleTagDeclDefinition(TagDecl * D)15114f1b3e8SDimitry Andric void ASTStructExtractor::HandleTagDeclDefinition(TagDecl *D) {
152f034231aSEd Maste if (m_passthrough)
153f034231aSEd Maste m_passthrough->HandleTagDeclDefinition(D);
154f034231aSEd Maste }
155f034231aSEd Maste
CompleteTentativeDefinition(VarDecl * D)15614f1b3e8SDimitry Andric void ASTStructExtractor::CompleteTentativeDefinition(VarDecl *D) {
157f034231aSEd Maste if (m_passthrough)
158f034231aSEd Maste m_passthrough->CompleteTentativeDefinition(D);
159f034231aSEd Maste }
160f034231aSEd Maste
HandleVTable(CXXRecordDecl * RD)16114f1b3e8SDimitry Andric void ASTStructExtractor::HandleVTable(CXXRecordDecl *RD) {
162f034231aSEd Maste if (m_passthrough)
16312bd4897SEd Maste m_passthrough->HandleVTable(RD);
164f034231aSEd Maste }
165f034231aSEd Maste
PrintStats()16614f1b3e8SDimitry Andric void ASTStructExtractor::PrintStats() {
167f034231aSEd Maste if (m_passthrough)
168f034231aSEd Maste m_passthrough->PrintStats();
169f034231aSEd Maste }
170f034231aSEd Maste
InitializeSema(Sema & S)17114f1b3e8SDimitry Andric void ASTStructExtractor::InitializeSema(Sema &S) {
172f034231aSEd Maste m_sema = &S;
173f034231aSEd Maste
174f034231aSEd Maste if (m_passthrough_sema)
175f034231aSEd Maste m_passthrough_sema->InitializeSema(S);
176f034231aSEd Maste }
177f034231aSEd Maste
ForgetSema()17814f1b3e8SDimitry Andric void ASTStructExtractor::ForgetSema() {
1795f29bb8aSDimitry Andric m_sema = nullptr;
180f034231aSEd Maste
181f034231aSEd Maste if (m_passthrough_sema)
182f034231aSEd Maste m_passthrough_sema->ForgetSema();
183f034231aSEd Maste }
184