forked from DFHack/df-structures
-
Notifications
You must be signed in to change notification settings - Fork 0
/
StructType.pm
293 lines (244 loc) · 9.88 KB
/
StructType.pm
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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
package StructType;
use utf8;
use strict;
use warnings;
BEGIN {
use Exporter ();
our $VERSION = 1.00;
our @ISA = qw(Exporter);
our @EXPORT = qw(
&render_struct_type
);
our %EXPORT_TAGS = ( ); # eg: TAG => [ qw!name1 name2! ],
our @EXPORT_OK = qw( );
}
END { }
use XML::LibXML;
use Common;
use StructFields;
# MISC
sub translate_lookup($) {
my ($str) = @_;
return undef unless $str && $str =~ /^\$global((\.[_a-zA-Z0-9]+)+)$/;
my @fields = split /\./, substr($1,1);
my $expr = "df::global::".shift(@fields);
for my $fn (@fields) {
$expr = "_fieldptr($expr, $fn)";
}
return $expr;
}
sub emit_find_instance(\%$) {
my ($rinfo, $tag) = @_;
my $keyfield = $tag->getAttribute('key-field');
my $keyfield_tag = find_subfield $tag, $keyfield;
my $keytype = 'int';
my $vectype = "std::vector<$typename*>";
if ($keyfield) {
die "Could not find field $keyfield in $typename\n" unless $keyfield_tag;
my $type = get_struct_field_type($keyfield_tag);
$keytype = 'key_field_type';
emit "typedef $typename* key_pointer_type;";
emit "typedef $type key_field_type;";
emit "static int binsearch_index(const $vectype &vec, key_field_type key, bool exact = true);";
emit "static int binsearch_index(const $vectype &vec, key_pointer_type key, bool exact = true);";
with_emit_static {
emit_block {
emit "return ::binsearch_index(vec, &${typename}::$keyfield, key, exact);";
} "int ${typename}::binsearch_index(const $vectype &vec, key_field_type key, bool exact) ";
emit_block {
emit "return binsearch_index(vec, key->$keyfield, exact);";
} "int ${typename}::binsearch_index(const $vectype &vec, key_pointer_type key, bool exact) ";
};
}
my $instance_vector = translate_lookup $tag->getAttribute('instance-vector');
if ($instance_vector) {
emit "static $vectype &get_vector();";
emit "static $vectype *get_vector_ptr();";
emit "static $typename *find($keytype id);";
with_emit_static {
emit_block {
emit "return *get_vector_ptr();";
} "$vectype& ${typename}::get_vector() ";
emit_block {
emit "return $instance_vector;";
} "$vectype* ${typename}::get_vector_ptr() ";
emit_block {
emit "std::vector<$typename*> *vec_ = get_vector_ptr();";
emit "if (!vec_) return NULL;";
if ($keyfield) {
emit "return binsearch_in_vector(*vec_, id_);";
} else {
emit "return (size_t(id_) < vec_->size()) ? (*vec_)[id_] : NULL;";
}
} "$typename *${typename}::find($keytype id_) ";
};
push @{$rinfo->{statics}}, {'get_vector' => 'get_vector_ptr'};
push @{$rinfo->{statics}}, 'find';
}
}
sub render_virtual_methods {
my ($tag) = @_;
# Collect all parent classes
my @parents = ( $tag );
for (;;) {
my $inherits = $parents[0]->getAttribute('inherits-from') or last;
my $parent = $types{$inherits} || die "Unknown parent: $inherits\n";
unshift @parents, $parent;
}
# Build the vtable array
my %name_index;
my @vtable;
my @starts;
my $dtor_id = '~destructor';
for my $type (@parents) {
push @starts, scalar(@vtable);
for my $method ($type->findnodes('virtual-methods/vmethod')) {
my $is_destructor = is_attr_true($method, 'is-destructor');
my $name = $is_destructor ? $dtor_id : $method->getAttribute('name');
if ($name) {
die "Duplicate virtual method: $name in ".$type->getAttribute('type-name')."\n"
if exists $name_index{$name};
$name_index{$name} = scalar(@vtable);
}
push @vtable, $method;
}
}
my $dtor_idx = $name_index{$dtor_id};
my $no_dtor = 0;
# If the vtable is empty, conjure up a destructor
unless (@vtable) {
$no_dtor = 1;
push @vtable, undef;
$dtor_idx = $#vtable;
}
my @our_vmethods;
# Generate the methods
my $min_id = $starts[-1];
my $cur_mode = '';
for (my $idx = $min_id; $idx <= $#vtable; $idx++) {
my $method = $vtable[$idx];
my $is_destructor = 1;
my $name = $typename;
my $is_anon = 1;
if ($method) {
$is_destructor = is_attr_true($method, 'is-destructor');
$name = $method->getAttribute('name') unless $is_destructor;
$is_anon = 0 if $name;
}
push @our_vmethods, $method unless ($is_anon || $is_destructor);
my $rq_mode = $is_anon ? 'protected' : 'public';
unless ($rq_mode eq $cur_mode) {
$cur_mode = $rq_mode;
outdent { emit "$cur_mode:"; }
}
with_anon {
$name = ensure_name $name;
$method->setAttribute('ld:anon-name', $name) if $method && $is_anon;
my @ret_type = $is_destructor ? () : $method->findnodes('ret-type');
my @arg_types = $is_destructor ? () : $method->findnodes('ld:field');
my $ret_type = $ret_type[0] ? get_struct_field_type($ret_type[0], -local => 1, -rettype => 1) : 'void';
my @arg_strs = map { scalar get_struct_field_type($_, -local => 1, -funcarg => 1) } @arg_types;
my $ret_stmt = '';
unless ($ret_type eq 'void') {
$ret_stmt = ' return '.($ret_type =~ /\*$/ ? '0' : "$ret_type()").';';
}
emit_comment $method;
emit 'virtual ', ($is_destructor?'~':$ret_type.' '), $name,
'(', join(', ', @arg_strs), ') {', $ret_stmt, ' /*', $idx, '*/ };',
get_comment($method);
} "anon_vmethod_$idx";
}
return ($no_dtor, \@our_vmethods);
}
sub render_struct_type {
my ($tag) = @_;
my $tag_name = $tag->getAttribute('ld:meta');
my $is_class = ($tag_name eq 'class-type');
my $is_linked_list = (($tag->getAttribute('ld:subtype') or '') eq 'df-linked-list-type');
my $item_type = $tag->getAttribute('item-type');
my $list_link_type = $tag->getAttribute('df-list-link-type');
my $list_link_field = $tag->getAttribute('df-list-link-field');
my $is_other_vectors = (($tag->getAttribute('ld:subtype') or '') eq 'df-other-vectors-type');
my $index_enum = $tag->getAttribute('index-enum');
my $custom_methods = is_attr_true($tag, 'custom-methods') || $tag->findnodes('custom-methods/cmethod');
my $has_methods = $is_class || is_attr_true($tag, 'has-methods');
my $inherits = $tag->getAttribute('inherits-from');
my $original_name = $tag->getAttribute('original-name');
my $ispec = '';
for my $extra ($tag->findnodes('extra-include')) {
my $tname = $extra->getAttribute('type-name');
if ($tname) {
register_ref $tname, 1;
} else {
header_ref $extra->getAttribute('filename');
}
}
if ($inherits) {
register_ref $inherits, 1;
$ispec = ' : '.$inherits;
} elsif ($is_class) {
$ispec = ' : virtual_class';
} elsif ($is_linked_list) {
register_ref $item_type, 1;
$ispec = ' : DfLinkedList<'.$typename.', '.$item_type.'>';
} elsif ($is_other_vectors) {
register_ref $item_type, 0;
register_ref $index_enum, 1;
$ispec = ' : DfOtherVectors<'.$typename.', '.$index_enum.', '.$item_type.'>';
}
if ($list_link_type) {
register_ref $list_link_type, 0;
}
with_struct_block {
my $vmethod_emit = sub {
my %info;
emit_find_instance(%info, $tag);
if ($list_link_type) {
emit $list_link_type," *dfhack_get_list_link();";
emit "void dfhack_set_list_link(",$list_link_type," *);";
with_emit_static {
emit_block {
if ($list_link_field) {
emit "return ",$list_link_field,";";
} else {
emit "return nullptr;";
}
} "$list_link_type *${typename}::dfhack_get_list_link()";
emit_block {
if ($list_link_field) {
emit "this->",$list_link_field," = l;";
}
} "void ${typename}::dfhack_set_list_link($list_link_type *l)";
};
}
if ($has_methods || $custom_methods) {
if ($custom_methods) {
local $indentation = 0;
emit '#include "custom/', $typename, '.methods.inc"';
my %name_index;
$info{cmethods} = [];
for my $method ($tag->findnodes('custom-methods/cmethod')) {
my $name = $method->getAttribute('name');
die "Custom method has no name in ".$typename."\n"
if not $name;
die "Duplicate custom method: $name in ".$typename."\n"
if exists $name_index{$name};
$name_index{$name} = 1;
push @{$info{cmethods}}, $method;
}
}
if ($is_class) {
my ($no_dtor, $vmethods) = render_virtual_methods $tag;
$info{nodtor} = $no_dtor;
$info{vmethods} = $vmethods;
} elsif (!$custom_methods) {
emit "~",$typename,"() {}";
}
}
return %info;
};
emit_struct_fields($tag, $typename, -class => $is_class, -inherits => $inherits,
-addmethods => $vmethod_emit);
} $tag, "$typename$ispec", -export => 1;
}
1;