This repository was archived by the owner on Jul 29, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvariable.cpp
More file actions
121 lines (92 loc) · 4.16 KB
/
variable.cpp
File metadata and controls
121 lines (92 loc) · 4.16 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
#ifndef JDECOMPILER_VARIABLE_CPP
#define JDECOMPILER_VARIABLE_CPP
#include "variable.h"
namespace jdecompiler {
string Variable::getRawNameByType(const Type* type, bool* unchecked) {
if(const ClassType* classType = dynamic_cast<const ClassType*>(type)) {
if(classType->simpleName == "Object") return "obj";
if(classType->simpleName == "Boolean") return "bool";
if(classType->simpleName == "Byte") return "b";
if(classType->simpleName == "Character") return "ch";
if(classType->simpleName == "Short") return "sh";
if(classType->simpleName == "Integer") return "n";
if(classType->simpleName == "Long") return "l";
if(classType->simpleName == "Float") return "f";
if(classType->simpleName == "Double") return "d";
if(classType->simpleName == "String") return "str";
if(classType->simpleName == "StringBuilder") return "str";
if(classType->simpleName == "BigInteger") return "bigint";
if(classType->simpleName == "BigDemical") return "bigdem";
}
*unchecked = true;
return type->getVarName();
}
string Variable::getNameByType(const Type* type) {
bool unchecked = false;
const string name = getRawNameByType(type, &unchecked);
if(unchecked) {
static const map<const char*, const char*> keywords {
{"boolean", "bool"}, {"byte", "b"}, {"char", "ch"}, {"short", "sh"}, {"int", "n"}, {"long", "l"},
{"float", "f"}, {"double", "d"}, {"void", "v"},
{"public", "pub"}, {"protected", "prot"}, {"private", "priv"}, {"static", "stat"}, {"final", "f"}, {"abstract", "abs"},
{"transient", "trans"}, {"volatile", "vol"}, {"native", "nat"}, {"synchronized", "sync"},
{"class", "clazz"}, {"interface", "interf"}, {"enum", "en"}, {"this", "t"}, {"super", "sup"}, {"extends", "ext"}, {"implements", "impl"},
{"import", "imp"}, {"package", "pack"}, {"instanceof", "inst"}, {"new", "n"},
{"if", "cond"}, {"else", "el"}, {"while", "whl"}, {"do", "d"}, {"for", "f"}, {"switch", "sw"}, {"case", "cs"}, {"default", "def"},
{"break", "brk"}, {"continue", "cont"}, {"return", "ret"},
{"try", "tr"}, {"catch", "c"}, {"finally", "f"}, {"throw", "thr"}, {"throws", "thrs"}, {"assert", "assrt"},
{"true", "tr"}, {"false", "fls"}, {"null", "nul"},
{"strictfp", "strict"}, {"const", "cnst"}, {"goto", "gt"}
};
for(const auto& keyword : keywords)
if(name == keyword.first)
return keyword.second;
}
return name;
}
Variable::Variable(const Type* type, bool declared, bool isFixedType): type(type), declared(declared), isFixedType(isFixedType) {}
template<bool widest = true>
const Type* Variable::setType(const Type* newType) const {
if(isFixedType) {
if(type->isSubtypeOf(newType))
return this->type;
else
throw IncopatibleTypesException(type, newType);
}
if(typeSettingLocked) // To avoid infinite recursion
return this->type;
typeSettingLocked = true;
for(const Operation* operation : bindedOperations) {
newType = widest ? operation->getReturnTypeAsWidest(newType) : operation->getReturnTypeAs(newType);
}
typeSettingLocked = false;
return this->type = newType;//(instanceof<const PrimitiveType*>(newType) ? static_cast<const PrimitiveType*>(newType)->toVariableCapacityIntegralType() : newType);
}
NamedVariable::NamedVariable(const Type* type, bool declared, const string& name, bool isFixedType):
Variable(type, declared, isFixedType), name(name) {}
void NamedVariable::makeCounter() const {}
bool NamedVariable::isCounter() const {
return false;
}
string NamedVariable::getName() const {
return name;
}
void NamedVariable::addName(const string&) const {}
UnnamedVariable::UnnamedVariable(const Type* type, bool declared, bool isFixedType):
Variable(type, declared, isFixedType) {}
UnnamedVariable::UnnamedVariable(const Type* type, bool declared, const string& name, bool isFixedType):
Variable(type, declared, isFixedType), names({name}) {}
void UnnamedVariable::makeCounter() const {
counter = true;
}
bool UnnamedVariable::isCounter() const {
return counter;
}
string UnnamedVariable::getName() const {
return names.size() == 1 ? *names.begin() : getNameByType(type);
}
void UnnamedVariable::addName(const string& name) const {
names.insert(name);
}
}
#endif