forked from DFHack/df-structures
-
Notifications
You must be signed in to change notification settings - Fork 0
/
lower-1.xslt
402 lines (361 loc) · 17.9 KB
/
lower-1.xslt
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
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
<?xml version="1.0" encoding="ISO-8859-1"?>
<!--
The original XML format is good for human use, but
difficult to interpret during code generation. This
lowers it to more repetitive & verbose, but easier
for the programs to interpret.
This is the first pass that folds all field tags into ld:field.
-->
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:ld="http://github.com/peterix/dfhack/lowered-data-definition">
<!--
Global templates:
- Copy attributes and simple tags
- Bail out on unexpected tags
-->
<xsl:template match="@*">
<xsl:copy-of select='.'/>
</xsl:template>
<xsl:template match="*">
<xsl:message terminate="yes">
Error: Unexpected tag: <xsl:value-of select='name(.)'/>
</xsl:message>
</xsl:template>
<xsl:template match="/data-definition">
<ld:data-definition>
<xsl:apply-templates select='@*|node()'/>
</ld:data-definition>
</xsl:template>
<xsl:template match="comment|code-helper|enum-attr|enum-item|item-attr|extra-include">
<xsl:copy>
<xsl:apply-templates select='@*|node()'/>
</xsl:copy>
</xsl:template>
<xsl:template match="virtual-methods|custom-methods|cond-if|cond-else">
<xsl:param name='level' select='-1'/>
<xsl:copy>
<xsl:apply-templates select='@*|node()'>
<xsl:with-param name='level' select="$level"/>
</xsl:apply-templates>
</xsl:copy>
</xsl:template>
<!-- Type defs: convert to one common 'global-type' tag name. -->
<xsl:template match='enum-type|bitfield-type|class-type|struct-type'>
<ld:global-type>
<xsl:attribute name='ld:meta'><xsl:value-of select='name(.)'/></xsl:attribute>
<xsl:attribute name='ld:level'>0</xsl:attribute>
<xsl:apply-templates select='@*|node()'>
<xsl:with-param name='level' select="1"/>
</xsl:apply-templates>
</ld:global-type>
</xsl:template>
<xsl:template match='df-linked-list-type'>
<ld:global-type ld:meta='struct-type' ld:subtype='df-linked-list-type' ld:level='0' type-name='{@type-name}' item-type='{@item-type}'>
<code-helper name="describe"><xsl:text>(describe-obj $.item)</xsl:text></code-helper>
<ld:field name='item' type-name='{@item-type}' ld:level='1' ld:meta='pointer' ld:is-container='true'>
<ld:item ld:level='2' ld:meta='global' type-name='{@item-type}'/>
</ld:field>
<ld:field name='prev' type-name='{@type-name}' ld:level='1' ld:meta='pointer' ld:is-container='true'>
<ld:item ld:level='2' ld:meta='global' type-name='{@type-name}'/>
</ld:field>
<ld:field name='next' type-name='{@type-name}' ld:level='1' ld:meta='pointer' ld:is-container='true'>
<ld:item ld:level='2' ld:meta='global' type-name='{@type-name}'/>
</ld:field>
</ld:global-type>
</xsl:template>
<xsl:template match='df-other-vectors-type'>
<xsl:variable name='vectors' select='./stl-vector'/>
<xsl:variable name='enum-name' select='@index-enum'/>
<xsl:variable name='item-type' select='@item-type'/>
<xsl:for-each select="*[not(name()='stl-vector' or name()='comment' or name()='code-helper')]">
<xsl:message terminate="yes">
Error: df-other-vectors-type may only contain stl-vector fields. (<xsl:value-of select='name(.)'/>)
</xsl:message>
</xsl:for-each>
<xsl:for-each select="$vectors">
<xsl:variable name='enum-key' select='@name'/>
<xsl:variable name='enum-item' select='/data-definition/enum-type[@type-name=($enum-name)]/enum-item[@name=($enum-key)]'/>
<xsl:choose>
<xsl:when test='count($enum-item) = 0'>
<xsl:message terminate="yes">
Error: field <xsl:value-of select='$enum-key'/> is not a member of enum <xsl:value-of select='$enum-name'/>
</xsl:message>
</xsl:when>
<xsl:when test="starts-with(($enum-item/@value), '-')">
<xsl:message terminate="yes">
Error: field <xsl:value-of select='$enum-key'/> corresponds to an enum value of <xsl:value-of select='$enum-item/@value'/>
</xsl:message>
</xsl:when>
</xsl:choose>
</xsl:for-each>
<ld:global-type ld:meta='struct-type' ld:subtype='df-other-vectors-type' ld:level='0' type-name='{@type-name}' index-enum='{$enum-name}' item-type='{$item-type}'>
<xsl:apply-templates select="@comment|@since"/>
<xsl:apply-templates select="comment|code-helper">
<xsl:with-param name='level' select="1"/>
</xsl:apply-templates>
<xsl:for-each select="/data-definition/enum-type[@type-name=($enum-name)]/enum-item[not(starts-with(@value, '-'))]">
<xsl:variable name='enum-key' select='@name'/>
<xsl:variable name='defined' select='$vectors[@name=($enum-key)]'/>
<xsl:choose>
<xsl:when test='count($defined)'>
<xsl:apply-templates select='$defined'>
<xsl:with-param name='level' select='1'/>
</xsl:apply-templates>
</xsl:when>
<xsl:otherwise>
<ld:field ld:meta='container' ld:level='1' ld:subtype='stl-vector' name='{$enum-key}' pointer-type='{$item-type}'>
<ld:item ld:meta='pointer' ld:is-container='true' ld:level='2' type-name='{$item-type}'>
<ld:item>
<xsl:call-template name='lookup-type-ref'>
<xsl:with-param name='name' select='$item-type'/>
<xsl:with-param name='level' select='3'/>
</xsl:call-template>
</ld:item>
</ld:item>
</ld:field>
</xsl:otherwise>
</xsl:choose>
</xsl:for-each>
</ld:global-type>
</xsl:template>
<!-- Code to properly annotate references to types by name -->
<xsl:key name="primitive-type-lookup" match="prim-type" use="@ld:subtype"/>
<xsl:variable name="primitive-types-top" select="document('')/*/ld:primitive-types"/>
<xsl:template match="ld:primitive-types">
<xsl:param name="name"/>
<xsl:param name="level"/>
<xsl:param name="rq_global"/>
<xsl:variable name='item' select="key('primitive-type-lookup', $name)"/>
<xsl:choose>
<xsl:when test="$item">
<xsl:if test='$rq_global'>
<xsl:message terminate="yes">
Error: Cannot refer to primitive types from <xsl:value-of select='$rq_global'/>
</xsl:message>
</xsl:if>
<xsl:apply-templates select="$item/@*"/>
<xsl:apply-templates select="$item/*">
<xsl:with-param name='level' select="$level+1"/>
</xsl:apply-templates>
</xsl:when>
<xsl:otherwise>
<xsl:attribute name='ld:meta'>global</xsl:attribute>
<xsl:attribute name='type-name'><xsl:value-of select='$name'/></xsl:attribute>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
<xsl:template name="lookup-type-ref">
<xsl:param name='name'/>
<xsl:param name='level' select='-1'/>
<xsl:param name='rq_global'/>
<xsl:attribute name='ld:level'><xsl:value-of select='$level'/></xsl:attribute>
<xsl:apply-templates select="$primitive-types-top">
<xsl:with-param name="name" select="$name"/>
<xsl:with-param name="level" select="$level"/>
<xsl:with-param name="rq_global" select="$rq_global"/>
</xsl:apply-templates>
</xsl:template>
<!--
Fields:
- Fold into one generic 'field' tag.
- Add a 'level' attribute to assist in searching by name.
-->
<!-- Primitive types: -->
<ld:primitive-types>
<prim-type ld:meta='number' ld:subtype='int8_t' ld:bits='8'/>
<prim-type ld:meta='number' ld:subtype='uint8_t' ld:unsigned='true' ld:bits='8'/>
<prim-type ld:meta='number' ld:subtype='int16_t' ld:bits='16'/>
<prim-type ld:meta='number' ld:subtype='uint16_t' ld:unsigned='true' ld:bits='16'/>
<prim-type ld:meta='number' ld:subtype='int32_t' ld:bits='32'/>
<prim-type ld:meta='number' ld:subtype='uint32_t' ld:unsigned='true' ld:bits='32'/>
<prim-type ld:meta='number' ld:subtype='int64_t' ld:bits='64'/>
<prim-type ld:meta='number' ld:subtype='uint64_t' ld:unsigned='true' ld:bits='64'/>
<prim-type ld:meta='number' ld:subtype='ssize_t' ld:bits=''/>
<prim-type ld:meta='number' ld:subtype='size_t' ld:unsigned='true' ld:bits=''/>
<prim-type ld:meta='number' ld:subtype='long' ld:bits=''/>
<prim-type ld:meta='number' ld:subtype='ulong' ld:bits=''/>
<prim-type ld:meta='number' ld:subtype='bool' ld:bits='8'/>
<prim-type ld:meta='number' ld:subtype='s-float' ld:bits='32'/>
<prim-type ld:meta='number' ld:subtype='d-float' ld:bits='64'/>
<prim-type ld:meta='number' ld:subtype='flag-bit' ld:bits='1'/>
<prim-type ld:meta='bytes' ld:subtype='padding'/>
<prim-type ld:meta='bytes' ld:subtype='static-string'/>
<prim-type ld:meta='pointer' ld:subtype='pointer'/>
<prim-type ld:meta='pointer' ld:subtype='ptr-string' ld:is-container='true'>
<static-string/>
</prim-type>
<prim-type ld:meta='primitive' ld:subtype='stl-string'/>
<prim-type ld:meta='primitive' ld:subtype='stl-fstream'/>
<prim-type ld:meta='primitive' ld:subtype='stl-mutex'/>
<prim-type ld:meta='primitive' ld:subtype='stl-condition-variable'/>
<prim-type ld:meta='primitive' ld:subtype='stl-future'/>
</ld:primitive-types>
<xsl:template match='int8_t|uint8_t|int16_t|uint16_t|int32_t|uint32_t|int64_t|uint64_t|size_t|ssize_t|long|ulong|bool|flag-bit|s-float|d-float|padding|static-string|ptr-string|stl-string|stl-fstream|stl-mutex|stl-condition-variable|stl-future'>
<xsl:param name='level' select='-1'/>
<ld:field>
<xsl:apply-templates select='@*'/>
<xsl:call-template name='lookup-type-ref'>
<xsl:with-param name="name" select="name(.)"/>
<xsl:with-param name="level" select="$level"/>
</xsl:call-template>
<xsl:apply-templates select='node()'/>
</ld:field>
</xsl:template>
<!--
Compound, enum or bitfield:
- When a proxy: meta='global' subtype='$tag' type-name='blah'
- When an ad-hoc compound: meta='compound' subtype='$tag'
- Level not incremented unless it has a name.
-->
<xsl:template name='compound'>
<xsl:param name='level' select='-1'/>
<xsl:param name='level_shift' select='1'/>
<xsl:param name='rq_global'/>
<xsl:apply-templates select='@*'/>
<xsl:choose>
<xsl:when test='@type-name'>
<xsl:call-template name='lookup-type-ref'>
<xsl:with-param name='name' select="@type-name"/>
<xsl:with-param name="level" select="$level"/>
<xsl:with-param name='rq_global' select="$rq_global"/>
</xsl:call-template>
<xsl:apply-templates select='node()'/>
</xsl:when>
<xsl:otherwise>
<xsl:attribute name='ld:level'><xsl:value-of select='$level'/></xsl:attribute>
<xsl:attribute name='ld:meta'>compound</xsl:attribute>
<xsl:apply-templates select='node()'>
<xsl:with-param name='level' select="$level+$level_shift"/>
</xsl:apply-templates>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
<xsl:template match='compound'>
<xsl:param name='level' select='-1'/>
<ld:field>
<xsl:if test="not(@name|@type-name)">
<xsl:attribute name='ld:anon-compound'>true</xsl:attribute>
</xsl:if>
<xsl:call-template name='compound'>
<xsl:with-param name='level' select="$level"/>
<xsl:with-param name='level_shift' select="count(@name)"/>
</xsl:call-template>
</ld:field>
</xsl:template>
<xsl:template match='bitfield|enum'>
<xsl:param name='level' select='-1'/>
<ld:field>
<xsl:attribute name='ld:subtype'><xsl:value-of select='name(.)'/></xsl:attribute>
<xsl:call-template name='compound'>
<xsl:with-param name='level' select="$level"/>
<xsl:with-param name='rq_global' select="name(.)"/>
</xsl:call-template>
</ld:field>
</xsl:template>
<!-- Generic container helper: resolve type-name to a field, then process subtags. -->
<xsl:template name='container'>
<xsl:param name='level' select='-1'/>
<xsl:attribute name='ld:is-container'>true</xsl:attribute>
<xsl:choose>
<xsl:when test='@pointer-type'>
<ld:field ld:meta='pointer' ld:is-container='true'>
<xsl:attribute name='ld:level'><xsl:value-of select='$level'/></xsl:attribute>
<xsl:attribute name='type-name'><xsl:value-of select='@pointer-type'/></xsl:attribute>
<xsl:apply-templates select='@refers-to|@ref-target|@aux-value'/>
<ld:field>
<xsl:apply-templates select='@refers-to|@ref-target|@aux-value'/>
<xsl:call-template name='lookup-type-ref'>
<xsl:with-param name='name' select="@pointer-type"/>
<xsl:with-param name="level" select="$level+1"/>
</xsl:call-template>
</ld:field>
</ld:field>
<xsl:apply-templates select='node()'/>
</xsl:when>
<xsl:when test='@type-name'>
<ld:field>
<xsl:apply-templates select='@refers-to|@ref-target|@aux-value'/>
<xsl:call-template name='lookup-type-ref'>
<xsl:with-param name='name' select="@type-name"/>
<xsl:with-param name="level" select="$level"/>
</xsl:call-template>
</ld:field>
<xsl:apply-templates select='node()'/>
</xsl:when>
<xsl:otherwise>
<xsl:apply-templates select='node()'>
<xsl:with-param name='level' select="$level"/>
</xsl:apply-templates>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
<!-- Special containers: meta='$tag' -->
<xsl:template match='static-array|pointer'>
<xsl:param name='level' select='-1'/>
<ld:field>
<xsl:attribute name='ld:level'><xsl:value-of select='$level'/></xsl:attribute>
<xsl:attribute name='ld:meta'><xsl:value-of select='name(.)'/></xsl:attribute>
<xsl:apply-templates select='@*'/>
<xsl:call-template name='container'>
<xsl:with-param name='level' select="$level+1"/>
</xsl:call-template>
</ld:field>
</xsl:template>
<!-- Misc containers: meta='container' subtype='$tag' -->
<xsl:template match='stl-vector|stl-deque|stl-set|stl-bit-vector|stl-map|stl-unordered-map|stl-optional|stl-variant|stl-shared-ptr|stl-weak-ptr|stl-function|df-flagarray|df-static-flagarray|df-array|df-linked-list'>
<xsl:param name='level' select='-1'/>
<ld:field ld:meta='container'>
<xsl:attribute name='ld:level'><xsl:value-of select='$level'/></xsl:attribute>
<xsl:attribute name='ld:subtype'><xsl:value-of select='name(.)'/></xsl:attribute>
<xsl:apply-templates select='@*'/>
<xsl:call-template name='container'>
<xsl:with-param name='level' select="$level+1"/>
</xsl:call-template>
</ld:field>
</xsl:template>
<!-- Virtual methods -->
<xsl:template match='vmethod|cmethod'>
<xsl:param name='level' select='-1'/>
<xsl:copy>
<xsl:attribute name='ld:level'><xsl:value-of select='$level'/></xsl:attribute>
<xsl:apply-templates select='@*'/>
<xsl:if test='@ret-type'>
<xsl:copy-of select='text()[1]'/>
<ret-type>
<xsl:call-template name='lookup-type-ref'>
<xsl:with-param name='name' select="@ret-type"/>
<xsl:with-param name="level" select="$level+1"/>
</xsl:call-template>
</ret-type>
</xsl:if>
<xsl:apply-templates select='node()'>
<xsl:with-param name='level' select="$level+1"/>
</xsl:apply-templates>
</xsl:copy>
</xsl:template>
<xsl:template match='ret-type'>
<xsl:param name='level' select='-1'/>
<xsl:copy>
<xsl:call-template name='compound'>
<xsl:with-param name='level' select="$level"/>
</xsl:call-template>
</xsl:copy>
</xsl:template>
<!-- Global objects: treat as container fields -->
<xsl:template match='global-object'>
<ld:global-object>
<xsl:attribute name='ld:level'>0</xsl:attribute>
<xsl:apply-templates select='@*'/>
<xsl:call-template name='container'>
<xsl:with-param name='level' select="1"/>
</xsl:call-template>
</ld:global-object>
</xsl:template>
</xsl:stylesheet>
<!--
Local Variables:
indent-tabs-mode: nil
nxml-child-indent: 4
End:
-->