-
Notifications
You must be signed in to change notification settings - Fork 20
Expand file tree
/
Copy pathclassconstruction.cpp
More file actions
134 lines (99 loc) · 7.39 KB
/
classconstruction.cpp
File metadata and controls
134 lines (99 loc) · 7.39 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
#include "classconstruction.h"
std::string ExtractPrefix(const std::string& input){
size_t firstPos = input.find('#');
if (firstPos != std::string::npos) {
size_t secondPos = input.find('#', firstPos + 1);
if (secondPos != std::string::npos) {
return input.substr(firstPos + 1, secondPos - firstPos - 1);
}
}
return "";
}
bool IsInstanceMethod(std::string func_name){
auto prefix = ExtractPrefix(func_name);
return prefix.find('~') != std::string::npos && prefix.find('>') != std::string::npos;
}
bool ConstructClasses(std::map<uint32_t, std::set<uint32_t>> &class2memberfuns, panda::es2panda::parser::Program *parser_program, BytecodeOptIrInterface *ir_interface,
std::map<uint32_t, panda::es2panda::ir::Expression*> &class2father, std::map<uint32_t, panda::es2panda::ir::ScriptFunction *> &method2scriptfunast,
std::map<uint32_t, panda::es2panda::ir::ClassDeclaration *> &ctor2classdeclast, std::map<std::string, std::string>& raw2newname
){
for(const auto & pair : class2memberfuns){
auto constructor_offset = pair.first;
std::cout << constructor_offset << std::endl;
auto member_funcs = pair.second;
//std::cout << "constructor_offset: " << constructor_offset << std::endl;
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
auto methodid = ir_interface->GetMethodIdByOffset(constructor_offset);
auto constructor_offset_name = RemoveArgumentsOfFunc(methodid);
std::string newname_constructor_offset_name;
if(raw2newname.find(constructor_offset_name) != raw2newname.end()){
newname_constructor_offset_name = raw2newname[constructor_offset_name];
}else{
HandleError("#ConstructClasses: find constructor_offset_name newname error");
}
panda::es2panda::util::StringView name_view1 = panda::es2panda::util::StringView(*(new std::string(RemoveArgumentsOfFunc(newname_constructor_offset_name))));
auto identNode = AllocNode<panda::es2panda::ir::Identifier>(parser_program, name_view1);
ArenaVector<es2panda::ir::TSClassImplements *> implements(parser_program->Allocator()->Adapter());
ArenaVector<es2panda::ir::TSIndexSignature *> indexSignatures(parser_program->Allocator()->Adapter());
ArenaVector<es2panda::ir::Statement *> properties(parser_program->Allocator()->Adapter());
//////////////////////////////////////////////////////////////////////////////////////////////
ArenaVector<es2panda::ir::Decorator *> decorators(parser_program->Allocator()->Adapter());
ArenaVector<es2panda::ir::Annotation *> annotations(parser_program->Allocator()->Adapter());
ArenaVector<es2panda::ir::ParamDecorators> paramDecorators(parser_program->Allocator()->Adapter());
panda::es2panda::util::StringView name_view2 = panda::es2panda::util::StringView("constructor");
auto keyNode = AllocNode<panda::es2panda::ir::Identifier>(parser_program, name_view2);
auto func = method2scriptfunast[constructor_offset];
method2scriptfunast.erase(constructor_offset);
if(func == nullptr){
HandleError("#DecompilePandaFile: find constructor function fail!");
}
auto funcExpr = AllocNode<panda::es2panda::ir::FunctionExpression>(parser_program, func);
auto ctor = AllocNode<es2panda::ir::MethodDefinition>(parser_program, es2panda::ir::MethodDefinitionKind::CONSTRUCTOR, keyNode, funcExpr,
es2panda::ir::ModifierFlags::PUBLIC, parser_program->Allocator(),
std::move(decorators), std::move(annotations),
std::move(paramDecorators), false);
///////////////////////////////////////////////////////////////////////////////////////////////
auto father = class2father[constructor_offset];
auto classDefinition = AllocNode<es2panda::ir::ClassDefinition>(parser_program, nullptr, identNode, nullptr, nullptr,
std::move(implements), ctor, nullptr,
nullptr, father, std::move(properties),
std::move(indexSignatures), false, false);
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
for (const auto& member_func_offset : member_funcs) {
if(constructor_offset == member_func_offset){
continue;
}
auto func = method2scriptfunast[member_func_offset];
method2scriptfunast.erase(member_func_offset);
if(func == nullptr){
std::cout << "member function offset: " << member_func_offset << std::endl;
HandleError("#ConstructClasses: find member function fail!");
}
auto funcExpr = AllocNode<es2panda::ir::FunctionExpression>(parser_program, func);
auto raw_member_name = RemoveArgumentsOfFunc(ir_interface->GetMethodIdByOffset(member_func_offset));
std::string new_member_name;
if(raw2newname.find(raw_member_name) != raw2newname.end()){
new_member_name = raw2newname[raw_member_name];
}else{
HandleError("#ConstructClasses: find new_member_name newname error");
}
panda::es2panda::util::StringView name_view3 = panda::es2panda::util::StringView(*(new std::string(new_member_name)));
auto keyNode = AllocNode<es2panda::ir::Identifier>(parser_program, name_view3);
ArenaVector<es2panda::ir::Decorator *> decorators(parser_program->Allocator()->Adapter());
ArenaVector<es2panda::ir::Annotation *> annotations(parser_program->Allocator()->Adapter());
ArenaVector<es2panda::ir::ParamDecorators> paramDecorators(parser_program->Allocator()->Adapter());
auto method = AllocNode<es2panda::ir::MethodDefinition>(parser_program, es2panda::ir::MethodDefinitionKind::METHOD, keyNode, funcExpr,
es2panda::ir::ModifierFlags::PUBLIC, parser_program->Allocator(),
std::move(decorators), std::move(annotations),
std::move(paramDecorators), false);
classDefinition->AddToBody(method);
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
ArenaVector<es2panda::ir::Decorator *> decorators1(parser_program->Allocator()->Adapter());
ArenaVector<es2panda::ir::Annotation *> annotations1(parser_program->Allocator()->Adapter());
auto classDecl = AllocNode<es2panda::ir::ClassDeclaration>(parser_program, classDefinition,
std::move(decorators1), std::move(annotations1), false);
ctor2classdeclast[constructor_offset] = classDecl;
}
return true;
}