-
Notifications
You must be signed in to change notification settings - Fork 10
/
Enum.cpp
179 lines (177 loc) · 6.21 KB
/
Enum.cpp
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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
/* Software License Agreement
*
* Copyright(C) 1994-2023 David Lindauer, (LADSoft)
*
* This file is part of the Orange C Compiler package.
*
* The Orange C Compiler package is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* The Orange C Compiler package is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Orange C. If not, see <http://www.gnu.org/licenses/>.
*
* contact information:
* email: [email protected] <David Lindauer>
*
*/
#include "DotNetPELib.h"
#include "PEFile.h"
namespace DotNetPELib
{
Field* Enum::AddValue(Allocator& allocator, const std::string& Name, longlong Value)
{
Type* type = allocator.AllocateType(this);
Field* field = allocator.AllocateField(Name, type, Qualifiers(Qualifiers::EnumField));
field->AddEnumValue(Value, size);
Add(field);
return field;
}
bool Enum::ILSrcDump(PELib& peLib) const
{
ILSrcDumpClassHeader(peLib);
peLib.Out() << " {" << std::endl;
DataContainer::ILSrcDump(peLib);
peLib.Out() << " .field public specialname rtspecialname ";
Field::ILSrcDumpTypeName(peLib, size);
peLib.Out() << " value__" << std::endl;
peLib.Out() << "}" << std::endl;
return true;
}
void Enum::ObjOut(PELib& peLib, int pass) const
{
if (pass == -1)
{
peLib.Out() << std::endl << "$Eb" << peLib.FormatName(Qualifiers::GetObjName(name_, parent_));
peLib.Out() << std::endl << "$Ee";
}
else
{
peLib.Out() << std::endl << "$Eb" << peLib.FormatName(name_) << size_ << "," << flags_.Flags();
DataContainer::ObjOut(peLib, pass);
peLib.Out() << std::endl << "$Ee";
}
}
Enum* Enum::ObjIn(PELib& peLib, bool definition)
{
Enum* rv = nullptr;
std::string name = peLib.UnformatName();
if (definition)
{
// as a definition
Field::ValueSize size = (Field::ValueSize)peLib.ObjInt();
char ch;
ch = peLib.ObjChar();
if (ch != ',')
peLib.ObjError(oe_syntax);
Qualifiers flags;
flags.ObjIn(peLib);
DataContainer* temp;
Enum* e;
temp = peLib.GetContainer()->FindContainer(name);
if (temp && typeid(*temp) != typeid(Enum))
peLib.ObjError(oe_noenum);
if (!temp)
rv = e = peLib.AllocateEnum(name, flags, size);
else
e = static_cast<Enum*>(temp);
((DataContainer*)e)->ObjIn(peLib);
}
else
{
// if we get here it is as an operand
void* result;
if (peLib.Find(name, &result) == PELib::s_enum)
{
rv = static_cast<Enum*>(result);
}
else
{
peLib.ObjError(oe_noenum);
}
}
if (peLib.ObjEnd() != 'E')
peLib.ObjError(oe_syntax);
return rv;
}
bool Enum::PEDump(PELib& peLib)
{
if (!InAssemblyRef())
{
int peflags = TransferFlags();
size_t typenameIndex = peLib.PEOut().HashString(Name());
size_t namespaceIndex = ParentNamespace(peLib);
size_t extends = peLib.PEOut().EnumBaseClass();
size_t fieldIndex = peLib.PEOut().NextTableIndex(tField);
size_t methodIndex = peLib.PEOut().NextTableIndex(tMethodDef);
TypeDefOrRef extendsClass(TypeDefOrRef::TypeRef, extends);
DataContainer* parent = Parent();
if (parent && typeid(*parent) == typeid(Class))
namespaceIndex = 0;
TableEntryBase* table =
new TypeDefTableEntry(peflags, typenameIndex, namespaceIndex, extendsClass, fieldIndex, methodIndex);
peIndex_ = peLib.PEOut().AddTableEntry(table);
if (parent && typeid(*parent) == typeid(Class))
{
size_t enclosing = ParentClass(peLib);
table = new NestedClassTableEntry(peIndex_, enclosing);
peLib.PEOut().AddTableEntry(table);
}
DataContainer::PEDump(peLib); // should only be the enumerations
size_t sz;
Type::BasicType tsize;
switch (size)
{
case Field::i8:
tsize = Type::i8;
break;
case Field::i16:
tsize = Type::i16;
break;
case Field::i32:
default:
tsize = Type::i32;
break;
case Field::i64:
tsize = Type::i64;
break;
}
// add the value member
Type type(tsize, 0);
Field field("value__", &type, Qualifiers(0));
Byte* sig = SignatureGenerator::FieldSig(&field, sz);
size_t sigindex = peLib.PEOut().HashBlob(sig, sz);
size_t nameindex = peLib.PEOut().HashString(field.Name());
table = new FieldTableEntry(FieldTableEntry::Public | FieldTableEntry::SpecialName | FieldTableEntry::RTSpecialName,
nameindex, sigindex);
peIndex_ = peLib.PEOut().AddTableEntry(table);
delete[] sig;
}
else if (!peIndex_)
{
if (typeid(*parent_) == typeid(Class))
{
parent_->PEDump(peLib);
ResolutionScope resolution(ResolutionScope::TypeRef, parent_->PEIndex());
size_t typenameIndex = peLib.PEOut().HashString(Name());
TableEntryBase* table = new TypeRefTableEntry(resolution, typenameIndex, 0);
peIndex_ = peLib.PEOut().AddTableEntry(table);
}
else
{
ResolutionScope resolution(ResolutionScope::AssemblyRef, ParentAssembly(peLib));
size_t typenameIndex = peLib.PEOut().HashString(Name());
size_t namespaceIndex = ParentNamespace(peLib);
TableEntryBase* table = new TypeRefTableEntry(resolution, typenameIndex, namespaceIndex);
peIndex_ = peLib.PEOut().AddTableEntry(table);
}
}
return true;
}
} // namespace DotNetPELib