forked from PlatONnetwork/pWASM-abigen
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAbiGenerator.cpp
More file actions
93 lines (83 loc) · 3.9 KB
/
AbiGenerator.cpp
File metadata and controls
93 lines (83 loc) · 3.9 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
#include <algorithm>
#include <regex>
#include "AbiGenerator.h"
#include "Exception.h"
#include "Common.h"
#include "Log.h"
using namespace std;
namespace cl = llvm::cl;
using namespace clang;
namespace platon {
static string convertBuildinType(const string &typeName){
regex r(R"(char\s*\[\d+\])");
smatch smatch;
if (regex_search(typeName, smatch, r)) {
return "char *";
}
return typeName;
}
void ABIGenerator::getRealName(clang::QualType &type, clang::ASTContext* astContext, std::string &typeName, std::string &realTypeName) {
if (isa<TypedefType>(type.getTypePtr())) {
const auto* tdDecl = type->getAs<clang::TypedefType>()->getDecl();
auto underlyingType = tdDecl->getUnderlyingType().getUnqualifiedType();
LOGDEBUG << "xxx:" << tdDecl->getName().str();
if (typeName.empty()) {
LOGDEBUG << "set typeName" << typeName;
typeName = tdDecl->getName().str();
}
if (isa<ArrayType>(underlyingType.getTypePtr())){
LOGDEBUG << "typeName:" << typeName << " is array";
}
if (isa<TypedefType>(underlyingType.getTypePtr())){
LOGDEBUG << "get typename again type:" << typeName << " realTypeName:" << realTypeName;
getRealName(underlyingType, astContext, typeName, realTypeName);
} else {
realTypeName = convertBuildinType(clang::TypeName::getFullyQualifiedName(underlyingType, *astContext));
}
} else {
realTypeName = convertBuildinType(clang::TypeName::getFullyQualifiedName(type, *astContext));
}
LOGDEBUG << "typeName:" << typeName << " realTypeName:" << realTypeName;
}
void ABIGenerator::handleTagDeclDefinition(TagDecl* tagDecl) {
clang::ASTContext* astContext = &tagDecl->getASTContext();
if (tagDecl->getName().str() == contract) {
if (contract == "Token"){
LOGDEBUG << "contract == Token";
} else {
LOGDEBUG << "contract != Token";
}
LOGDEBUG << "decl name:[" << tagDecl->getName().str() << "] contract:[" <<contract << "]";
const auto* recDecl = dyn_cast<CXXRecordDecl>(tagDecl);
if (recDecl == nullptr) return;
for (const CXXMethodDecl* method : recDecl->methods()) {
string methodName = method->getNameAsString();
if (std::find_if(actions.begin(), actions.end(), [methodName] (const string& o) -> bool {return o == methodName;}) == actions.end()) {
continue;
}
LOGDEBUG << "method :" << methodName;
ABI abi;
abi.methodName = method->getNameAsString();
if (method->isConst()){
abi.isConst = true;
}
clang::QualType returnType = method->getReturnType();
getRealName(returnType, astContext, abi.returnType.typeName, abi.returnType.realTypeName);
for (const auto *p : method->parameters()) {
clang::QualType qt = p->getOriginalType().getNonReferenceType();
abi.args.push_back(p->getNameAsString());
LOGDEBUG << "parame name:" << p->getNameAsString();
string typeName;
string realTypeName;
getRealName(qt, astContext, typeName, realTypeName);
if (!isBuildinType(realTypeName)) {
throw Exception() << ErrStr(methodName + ":" + p->getNameAsString() + ":" + realTypeName + " is not buildin type");
}
abi.types.push_back({typeName, realTypeName});
}
abiDef.abis.push_back(abi);
}
LOGDEBUG << "abis size:" << abiDef.abis.size();
}
}
}