c-client.xsl 56.6 KB
Newer Older
Josh Triplett's avatar
Josh Triplett committed
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
<?xml version="1.0" encoding="utf-8"?>
<!--
Copyright (C) 2004 Josh Triplett.  All Rights Reserved.

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Except as contained in this notice, the names of the authors or their
institutions shall not be used in advertising or otherwise to promote the
sale, use or other dealings in this Software without prior written
authorization from the authors.
-->
<xsl:transform xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
               version="1.0"
Josh Triplett's avatar
Josh Triplett committed
29
30
31
32
33
               xmlns:e="http://exslt.org/common"
               xmlns:func="http://exslt.org/functions"
               xmlns:str="http://exslt.org/strings"
               xmlns:xcb="http://xcb.freedesktop.org"
               extension-element-prefixes="func str xcb">
Josh Triplett's avatar
Josh Triplett committed
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
  
  <xsl:output method="text" />

  <xsl:strip-space elements="*" />

  <!-- "header" or "source" -->
  <xsl:param name="mode" />

  <!-- Path to the core protocol descriptions. -->
  <xsl:param name="base-path" />

  <!-- Path to the extension protocol descriptions. -->
  <xsl:param name="extension-path" select="$base-path" />

  <xsl:variable name="h" select="$mode = 'header'" />
  <xsl:variable name="c" select="$mode = 'source'" />
50
51

  <xsl:variable name="need-string-h" select="//request/pad[@bytes != 1]" />
Josh Triplett's avatar
Josh Triplett committed
52
53
54
55
56
57
  
  <!-- String used to indent lines of code. -->
  <xsl:variable name="indent-string" select="'    '" />

  <xsl:variable name="ucase" select="'ABCDEFGHIJKLMNOPQRSTUVWXYZ'" />
  <xsl:variable name="lcase" select="'abcdefghijklmnopqrstuvwxyz'" />
Josh Triplett's avatar
Josh Triplett committed
58
59
  <xsl:variable name="letters" select="concat($ucase, $lcase)" />
  <xsl:variable name="digits" select="'0123456789'" />
Josh Triplett's avatar
Josh Triplett committed
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

  <xsl:variable name="header" select="/xcb/@header" />
  <xsl:variable name="ucase-header"
                select="translate($header,$lcase,$ucase)" />

  <xsl:variable name="ext" select="/xcb/@extension-name" />

  <!-- Other protocol descriptions to search for types in, after checking the
       current protocol description. -->
  <xsl:variable name="search-path-rtf">
    <xsl:for-each select="/xcb/import">
      <path><xsl:value-of select="concat($extension-path, ., '.xml')" /></path>
    </xsl:for-each>
  </xsl:variable>
  <xsl:variable name="search-path" select="e:node-set($search-path-rtf)/path"/>

  <xsl:variable name="root" select="/" />
  
  <!-- First pass: Store everything in a variable. -->
  <xsl:variable name="pass1-rtf">
    <xsl:apply-templates select="/" mode="pass1" />
  </xsl:variable>
  <xsl:variable name="pass1" select="e:node-set($pass1-rtf)" />
  
  <xsl:template match="xcb" mode="pass1">
    <xcb>
      <xsl:copy-of select="@*" />
      <xsl:if test="$ext">
Josh Triplett's avatar
Josh Triplett committed
88
        <constant type="xcb_extension_t" name="{xcb:xcb-prefix()}_id">
Josh Triplett's avatar
Josh Triplett committed
89
90
91
92
93
94
95
          <xsl:attribute name="value">{ "<xsl:value-of select="@extension-xname" />" }</xsl:attribute>
        </constant>
      </xsl:if>
      <xsl:apply-templates mode="pass1" />
    </xcb>
  </xsl:template>

Josh Triplett's avatar
Josh Triplett committed
96
97
98
99
  <func:function name="xcb:xcb-prefix">
    <xsl:param name="name" />
    <func:result>
      <xsl:text>xcb</xsl:text>
100
101
102
      <xsl:if test="/xcb/@extension-name">
	<xsl:text>_</xsl:text>
	<xsl:choose>
103
	  <xsl:when test="/xcb/@extension-multiword = 'true' or /xcb/@extension-multiword = '1'">
104
105
106
	    <xsl:call-template name="camelcase-to-underscore">
	      <xsl:with-param name="camelcase" select="/xcb/@extension-name" />
	    </xsl:call-template>
107
108
109
	  </xsl:when>
	  <xsl:otherwise>
	    <xsl:value-of select="translate(/xcb/@extension-name, $ucase, $lcase)"/>
110
111
112
	  </xsl:otherwise>
	</xsl:choose>
      </xsl:if>
Josh Triplett's avatar
Josh Triplett committed
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
      <xsl:if test="$name">
        <xsl:text>_</xsl:text>
        <xsl:call-template name="camelcase-to-underscore">
          <xsl:with-param name="camelcase" select="$name" />
        </xsl:call-template>
      </xsl:if>
    </func:result>
  </func:function>

  <func:function name="xcb:lowercase">
    <xsl:param name="name" />
    <func:result>
      <xsl:call-template name="camelcase-to-underscore">
        <xsl:with-param name="camelcase" select="$name" />
      </xsl:call-template>
    </func:result>
  </func:function>

  <func:function name="xcb:get-char-void">
    <xsl:param name="name" />
    <xsl:variable name="ctype" select="substring-before($name, '_t')" />
    <func:result>
      <xsl:choose>
        <xsl:when test="$ctype = 'char' or $ctype = 'void' or $ctype = 'float' or $ctype = 'double'">
          <xsl:value-of select="$ctype" />    
        </xsl:when>
        <xsl:otherwise>
          <xsl:value-of select="$name" />
        </xsl:otherwise>
      </xsl:choose>
    </func:result>
  </func:function>

  <func:function name="xcb:remove-void">
    <xsl:param name="name" />
    <xsl:variable name="ctype" select="substring-before($name, '_t')" />
    <func:result>
      <xsl:choose>
        <xsl:when test="$ctype = 'char' or $ctype = 'void' or $ctype = 'float' or $ctype = 'double'">
          <xsl:choose>
            <xsl:when test="$ctype = 'void'">
              <xsl:text>char</xsl:text>
            </xsl:when>
            <xsl:otherwise>
              <xsl:value-of select="$ctype" />
            </xsl:otherwise>
          </xsl:choose>
        </xsl:when>
        <xsl:otherwise>
          <xsl:value-of select="$name" />
        </xsl:otherwise>
      </xsl:choose>
    </func:result>
  </func:function>

  <!-- split camel case into words and insert underscore -->
  <xsl:template name="camelcase-to-underscore">
    <xsl:param name="camelcase"/>
    <xsl:choose>
      <xsl:when test="$camelcase='CHAR2B' or $camelcase='INT64'
                      or $camelcase='FLOAT32' or $camelcase='FLOAT64'
                      or $camelcase='BOOL32' or $camelcase='STRING8'
                      or $camelcase='Family_DECnet'">
        <xsl:value-of select="translate($camelcase, $ucase, $lcase)"/>
      </xsl:when>
      <xsl:otherwise>
        <xsl:for-each select="str:split($camelcase, '')">
          <xsl:variable name="a" select="."/>
          <xsl:variable name="b" select="following::*[1]"/>
          <xsl:variable name="c" select="following::*[2]"/>
          <xsl:value-of select="translate(., $ucase, $lcase)"/>
          <xsl:if test="($b and contains($lcase, $a) and contains($ucase, $b))
                        or ($b and contains($digits, $a)
                            and contains($letters, $b))
                        or ($b and contains($letters, $a)
                            and contains($digits, $b))
                        or ($c and contains($ucase, $a)
                            and contains($ucase, $b)
                            and contains($lcase, $c))">
            <xsl:text>_</xsl:text>
          </xsl:if>
        </xsl:for-each>
      </xsl:otherwise>
    </xsl:choose>
  </xsl:template>

Josh Triplett's avatar
Josh Triplett committed
199
200
201
202
203
204
205
206
207
208
209
210
211
212
  <!-- Modify names that conflict with C++ keywords by prefixing them with an
       underscore.  If the name parameter is not specified, it defaults to the
       value of the name attribute on the context node. -->
  <xsl:template name="canonical-var-name">
    <xsl:param name="name" select="@name" />
    <xsl:if test="$name='new' or $name='delete'
                  or $name='class' or $name='operator'">
      <xsl:text>_</xsl:text>
    </xsl:if>
    <xsl:value-of select="$name" />
  </xsl:template>

  <!-- List of core types, for use in canonical-type-name. -->
  <xsl:variable name="core-types-rtf">
Josh Triplett's avatar
Josh Triplett committed
213
214
215
216
217
218
219
220
221
222
223
224
225
    <type name="BOOL" newname="uint8" />
    <type name="BYTE" newname="uint8" />
    <type name="CARD8" newname="uint8" />
    <type name="CARD16" newname="uint16" />
    <type name="CARD32" newname="uint32" />
    <type name="INT8" newname="int8" />
    <type name="INT16" newname="int16" />
    <type name="INT32" newname="int32" />

    <type name="char" newname="char" />
    <type name="void" newname="void" />
    <type name="float" newname="float" />
    <type name="double" newname="double" />
Josh Triplett's avatar
Josh Triplett committed
226
227
228
229
230
  </xsl:variable>
  <xsl:variable name="core-types" select="e:node-set($core-types-rtf)" />

  <!--
    Output the canonical name for a type.  This will be
Josh Triplett's avatar
Josh Triplett committed
231
232
    xcb_{extension-containing-type-if-any}_type, wherever the type is found in
    the search path, or just type if not found.  If the type parameter is not
Josh Triplett's avatar
Josh Triplett committed
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
    specified, it defaults to the value of the type attribute on the context
    node.
  -->
  <xsl:template name="canonical-type-name">
    <xsl:param name="type" select="string(@type)" />

    <xsl:variable name="is-unqualified" select="not(contains($type, ':'))"/>
    <xsl:variable name="namespace" select="substring-before($type, ':')" />
    <xsl:variable name="unqualified-type">
      <xsl:choose>
        <xsl:when test="$is-unqualified">
          <xsl:value-of select="$type" />
        </xsl:when>
        <xsl:otherwise>
          <xsl:value-of select="substring-after($type, ':')" />
        </xsl:otherwise>
      </xsl:choose>
    </xsl:variable>

    <xsl:choose>
      <xsl:when test="$is-unqualified and $core-types/type[@name=$type]">
Josh Triplett's avatar
Josh Triplett committed
254
        <xsl:value-of select="$core-types/type[@name=$type]/@newname" />
Josh Triplett's avatar
Josh Triplett committed
255
256
257
258
259
      </xsl:when>
      <xsl:otherwise>
        <xsl:variable name="type-definitions"
                      select="(/xcb|document($search-path)/xcb
                              )[$is-unqualified or @header=$namespace]
260
261
                               /*[((self::struct or self::union or self::enum
                                    or self::xidtype or self::xidunion
Josh Triplett's avatar
Josh Triplett committed
262
263
264
265
266
267
268
269
                                    or self::event or self::eventcopy
                                    or self::error or self::errorcopy)
                                   and @name=$unqualified-type)
                                  or (self::typedef
                                      and @newname=$unqualified-type)]" />
        <xsl:choose>
          <xsl:when test="count($type-definitions) = 1">
            <xsl:for-each select="$type-definitions">
Josh Triplett's avatar
Josh Triplett committed
270
              <xsl:value-of select="xcb:xcb-prefix($unqualified-type)" />
Josh Triplett's avatar
Josh Triplett committed
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
            </xsl:for-each>
          </xsl:when>
          <xsl:when test="count($type-definitions) > 1">
            <xsl:message terminate="yes">
              <xsl:text>Multiple definitions of type "</xsl:text>
              <xsl:value-of select="$type" />
              <xsl:text>" found.</xsl:text>
              <xsl:if test="$is-unqualified">
                <xsl:for-each select="$type-definitions">
                  <xsl:text>
    </xsl:text>
                  <xsl:value-of select="concat(/xcb/@header, ':', $type)" />
                </xsl:for-each>
              </xsl:if>
            </xsl:message>
          </xsl:when>
          <xsl:otherwise>
            <xsl:message terminate="yes">
              <xsl:text>No definitions of type "</xsl:text>
              <xsl:value-of select="$type" />
              <xsl:text>" found</xsl:text>
              <xsl:if test="$is-unqualified">
                <xsl:text>, and it is not a known core type</xsl:text>
              </xsl:if>
              <xsl:text>.</xsl:text>
            </xsl:message>
          </xsl:otherwise>
        </xsl:choose> 	
      </xsl:otherwise>
    </xsl:choose>
  </xsl:template>
  
  <!-- Helper template for requests, that outputs the cookie type.  The
304
305
       parameter "request" must be the request node, which defaults to the
       context node. -->
Josh Triplett's avatar
Josh Triplett committed
306
  <xsl:template name="cookie-type">
307
    <xsl:param name="request" select="." />
Josh Triplett's avatar
Josh Triplett committed
308
    <xsl:choose>
309
      <xsl:when test="$request/reply">
Josh Triplett's avatar
Josh Triplett committed
310
        <xsl:value-of select="xcb:xcb-prefix($request/@name)" />
Josh Triplett's avatar
Josh Triplett committed
311
312
      </xsl:when>
      <xsl:otherwise>
Josh Triplett's avatar
Josh Triplett committed
313
        <xsl:text>xcb_void</xsl:text>
Josh Triplett's avatar
Josh Triplett committed
314
315
      </xsl:otherwise>
    </xsl:choose>
Josh Triplett's avatar
Josh Triplett committed
316
    <xsl:text>_cookie_t</xsl:text>
Josh Triplett's avatar
Josh Triplett committed
317
318
  </xsl:template>

319
320
321
322
323
  <xsl:template name="request-function">
    <xsl:param name="checked" />
    <xsl:param name="req" />
    <function>
      <xsl:attribute name="name">
Josh Triplett's avatar
Josh Triplett committed
324
325
326
        <xsl:value-of select="xcb:xcb-prefix($req/@name)" />
        <xsl:if test="$checked='true' and not($req/reply)">_checked</xsl:if>
        <xsl:if test="$checked='false' and $req/reply">_unchecked</xsl:if>
327
328
329
330
331
332
      </xsl:attribute>
      <xsl:attribute name="type">
        <xsl:call-template name="cookie-type">
          <xsl:with-param name="request" select="$req" />
        </xsl:call-template>
      </xsl:attribute>
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
      <doc>/**</doc>
      <doc> * Delivers a request to the X server</doc>
      <doc> * @param c The connection</doc>
      <doc> * @return A cookie</doc>
      <doc> *</doc>
      <doc> * Delivers a request to the X server.</doc>
      <doc> * </doc>
      <xsl:if test="$checked='true' and not($req/reply)">
        <doc> * This form can be used only if the request will not cause</doc>
        <doc> * a reply to be generated. Any returned error will be</doc>
        <doc> * saved for handling by xcb_request_check().</doc>
      </xsl:if>
      <xsl:if test="$checked='false' and $req/reply">
        <doc> * This form can be used only if the request will cause</doc>
        <doc> * a reply to be generated. Any returned error will be</doc>
        <doc> * placed in the event queue.</doc>
      </xsl:if>
      <doc> */</doc>
Josh Triplett's avatar
Josh Triplett committed
351
      <field type="xcb_connection_t *" name="c" />
352
      <xsl:apply-templates select="$req/*[not(self::reply)]" mode="param" />
353
      <do-request ref="{xcb:xcb-prefix($req/@name)}_request_t" opcode="{translate(xcb:xcb-prefix($req/@name), $lcase, $ucase)}"
354
355
356
357
358
359
360
361
                  checked="{$checked}">
        <xsl:if test="$req/reply">
          <xsl:attribute name="has-reply">true</xsl:attribute>
        </xsl:if>
      </do-request>
    </function>
  </xsl:template>
  
Josh Triplett's avatar
Josh Triplett committed
362
  <xsl:template match="request" mode="pass1">
363
    <xsl:variable name="req" select="." />
Josh Triplett's avatar
Josh Triplett committed
364
    <xsl:if test="reply">
Josh Triplett's avatar
Josh Triplett committed
365
      <struct name="{xcb:xcb-prefix(@name)}_cookie_t">
Josh Triplett's avatar
Josh Triplett committed
366
367
368
        <field type="unsigned int" name="sequence" />
      </struct>
    </xsl:if>
369
    <constant type="number" name="{xcb:xcb-prefix($req/@name)}" value="{$req/@opcode}" />
Josh Triplett's avatar
Josh Triplett committed
370
371
    <struct name="{xcb:xcb-prefix(@name)}_request_t">
      <field type="uint8_t" name="major_opcode" no-assign="true" />
Josh Triplett's avatar
Josh Triplett committed
372
      <xsl:if test="$ext">
Josh Triplett's avatar
Josh Triplett committed
373
        <field type="uint8_t" name="minor_opcode" no-assign="true" />
Josh Triplett's avatar
Josh Triplett committed
374
375
376
      </xsl:if>
      <xsl:apply-templates select="*[not(self::reply)]" mode="field" />
      <middle>
Josh Triplett's avatar
Josh Triplett committed
377
        <field type="uint16_t" name="length" no-assign="true" />
Josh Triplett's avatar
Josh Triplett committed
378
379
      </middle>
    </struct>
380
381
382
383
384
385
386
387
    <xsl:call-template name="request-function">
      <xsl:with-param name="checked" select="'true'" />
      <xsl:with-param name="req" select="$req" />
    </xsl:call-template>
    <xsl:call-template name="request-function">
      <xsl:with-param name="checked" select="'false'" />
      <xsl:with-param name="req" select="$req" />
    </xsl:call-template>
Josh Triplett's avatar
Josh Triplett committed
388
    <xsl:if test="reply">
Josh Triplett's avatar
Josh Triplett committed
389
390
      <struct name="{xcb:xcb-prefix(@name)}_reply_t">
        <field type="uint8_t" name="response_type" />
Josh Triplett's avatar
Josh Triplett committed
391
392
        <xsl:apply-templates select="reply/*" mode="field" />
        <middle>
Josh Triplett's avatar
Josh Triplett committed
393
394
          <field type="uint16_t" name="sequence" />
          <field type="uint32_t" name="length" />
Josh Triplett's avatar
Josh Triplett committed
395
396
        </middle>
      </struct>
Josh Triplett's avatar
Josh Triplett committed
397
398
      <iterator-functions ref="{xcb:xcb-prefix(@name)}" kind="_reply" />
      <function type="{xcb:xcb-prefix(@name)}_reply_t *" name="{xcb:xcb-prefix(@name)}_reply">
399
400
401
402
403
404
405
406
407
408
409
        <doc>/**</doc>
        <doc> * Return the reply</doc>
        <doc> * @param c      The connection</doc>
        <doc> * @param cookie The cookie</doc>
        <doc> * @param e      The xcb_generic_error_t supplied</doc>
        <doc> *</doc>
        <doc> * Returns the reply of the request asked by</doc>
        <doc> * </doc>
        <doc> * The parameter @p e supplied to this function must be NULL if</doc>
        <doc> * <xsl:value-of select="xcb:xcb-prefix(@name)" />_unchecked(). is used.</doc>
        <doc> * Otherwise, it stores the error if any.</doc>
410
411
        <doc> *</doc>
        <doc> * The returned value must be freed by the caller using free().</doc>
412
        <doc> */</doc>
Josh Triplett's avatar
Josh Triplett committed
413
        <field type="xcb_connection_t *" name="c" />
Josh Triplett's avatar
Josh Triplett committed
414
415
416
417
418
        <field name="cookie">
          <xsl:attribute name="type">
            <xsl:call-template name="cookie-type" />
          </xsl:attribute>
        </field>
Josh Triplett's avatar
Josh Triplett committed
419
420
421
        <field type="xcb_generic_error_t **" name="e" />
        <l>return (<xsl:value-of select="xcb:xcb-prefix(@name)" />_reply_t *)<!--
        --> xcb_wait_for_reply(c, cookie.sequence, e);</l>
Josh Triplett's avatar
Josh Triplett committed
422
423
424
425
      </function>
    </xsl:if>
  </xsl:template>

426
  <xsl:template match="xidtype|xidunion" mode="pass1">
427
    <typedef oldname="uint32_t" newname="{xcb:xcb-prefix(@name)}_t" />
Josh Triplett's avatar
Josh Triplett committed
428
429
    <iterator ref="{xcb:xcb-prefix(@name)}" />
    <iterator-functions ref="{xcb:xcb-prefix(@name)}" />
Josh Triplett's avatar
Josh Triplett committed
430
431
432
  </xsl:template>

  <xsl:template match="struct|union" mode="pass1">
Josh Triplett's avatar
Josh Triplett committed
433
    <struct name="{xcb:xcb-prefix(@name)}_t">
Josh Triplett's avatar
Josh Triplett committed
434
435
436
437
438
      <xsl:if test="self::union">
        <xsl:attribute name="kind">union</xsl:attribute>
      </xsl:if>
      <xsl:apply-templates select="*" mode="field" />
    </struct>
Josh Triplett's avatar
Josh Triplett committed
439
440
    <iterator ref="{xcb:xcb-prefix(@name)}" />
    <iterator-functions ref="{xcb:xcb-prefix(@name)}" />
Josh Triplett's avatar
Josh Triplett committed
441
442
443
444
445
446
  </xsl:template>

  <xsl:template match="event|eventcopy|error|errorcopy" mode="pass1">
    <xsl:variable name="suffix">
      <xsl:choose>
        <xsl:when test="self::event|self::eventcopy">
Josh Triplett's avatar
Josh Triplett committed
447
          <xsl:text>_event_t</xsl:text>
Josh Triplett's avatar
Josh Triplett committed
448
449
        </xsl:when>
        <xsl:when test="self::error|self::errorcopy">
Josh Triplett's avatar
Josh Triplett committed
450
          <xsl:text>_error_t</xsl:text>
Josh Triplett's avatar
Josh Triplett committed
451
452
453
        </xsl:when>
      </xsl:choose>
    </xsl:variable>
Josh Triplett's avatar
Josh Triplett committed
454
    <constant type="number" name="{xcb:xcb-prefix(@name)}" value="{@number}" />
Josh Triplett's avatar
Josh Triplett committed
455
456
    <xsl:choose>
      <xsl:when test="self::event|self::error">
Josh Triplett's avatar
Josh Triplett committed
457
458
        <struct name="{xcb:xcb-prefix(@name)}{$suffix}">
          <field type="uint8_t" name="response_type" />
Josh Triplett's avatar
Josh Triplett committed
459
          <xsl:if test="self::error">
Josh Triplett's avatar
Josh Triplett committed
460
            <field type="uint8_t" name="error_code" />
Josh Triplett's avatar
Josh Triplett committed
461
462
463
464
          </xsl:if>
          <xsl:apply-templates select="*" mode="field" />
          <xsl:if test="not(self::event and boolean(@no-sequence-number))">
            <middle>
Josh Triplett's avatar
Josh Triplett committed
465
              <field type="uint16_t" name="sequence" />
Josh Triplett's avatar
Josh Triplett committed
466
467
468
469
470
            </middle>
          </xsl:if>
        </struct>
      </xsl:when>
      <xsl:when test="self::eventcopy|self::errorcopy">
Josh Triplett's avatar
Josh Triplett committed
471
        <typedef newname="{xcb:xcb-prefix(@name)}{$suffix}">
Josh Triplett's avatar
Josh Triplett committed
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
          <xsl:attribute name="oldname">
            <xsl:call-template name="canonical-type-name">
              <xsl:with-param name="type" select="@ref" />
            </xsl:call-template>
            <xsl:value-of select="$suffix" />
          </xsl:attribute>
        </typedef>
      </xsl:when>
    </xsl:choose>
  </xsl:template>

  <xsl:template match="typedef" mode="pass1">
    <typedef>
      <xsl:attribute name="oldname">
        <xsl:call-template name="canonical-type-name">
          <xsl:with-param name="type" select="@oldname" />
        </xsl:call-template>
Josh Triplett's avatar
Josh Triplett committed
489
        <xsl:text>_t</xsl:text>
Josh Triplett's avatar
Josh Triplett committed
490
491
492
493
494
      </xsl:attribute>
      <xsl:attribute name="newname">
        <xsl:call-template name="canonical-type-name">
          <xsl:with-param name="type" select="@newname" />
        </xsl:call-template>
Josh Triplett's avatar
Josh Triplett committed
495
        <xsl:text>_t</xsl:text>
Josh Triplett's avatar
Josh Triplett committed
496
497
      </xsl:attribute>
    </typedef>
Josh Triplett's avatar
Josh Triplett committed
498
499
    <iterator ref="{xcb:xcb-prefix(@newname)}" />
    <iterator-functions ref="{xcb:xcb-prefix(@newname)}" />
Josh Triplett's avatar
Josh Triplett committed
500
501
502
  </xsl:template>

  <xsl:template match="enum" mode="pass1">
Josh Triplett's avatar
Josh Triplett committed
503
    <enum name="{xcb:xcb-prefix(@name)}_t">
Josh Triplett's avatar
Josh Triplett committed
504
      <xsl:for-each select="item">
Josh Triplett's avatar
Josh Triplett committed
505
        <item name="{translate(xcb:xcb-prefix(concat(../@name, concat('_', @name))), $lcase, $ucase)}">
Josh Triplett's avatar
Josh Triplett committed
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
          <xsl:copy-of select="*" />
        </item>
      </xsl:for-each>
    </enum>
  </xsl:template>

  <!--
    Templates for processing fields.
  -->

  <xsl:template match="pad" mode="field">
    <xsl:copy-of select="." />
  </xsl:template>
  
  <xsl:template match="field|exprfield" mode="field">
    <xsl:copy>
      <xsl:attribute name="type">
        <xsl:call-template name="canonical-type-name" />
Josh Triplett's avatar
Josh Triplett committed
524
        <xsl:text>_t</xsl:text>
Josh Triplett's avatar
Josh Triplett committed
525
526
527
528
529
530
531
532
533
534
      </xsl:attribute>
      <xsl:attribute name="name">
        <xsl:call-template name="canonical-var-name" />
      </xsl:attribute>
      <xsl:copy-of select="*" />
    </xsl:copy>
  </xsl:template>

  <xsl:template match="list" mode="field">
    <xsl:variable name="type"><!--
Josh Triplett's avatar
Josh Triplett committed
535
536
      --><xsl:call-template name="canonical-type-name" />
        <xsl:text>_t</xsl:text><!--
Josh Triplett's avatar
Josh Triplett committed
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
    --></xsl:variable>
    <list type="{$type}">
      <xsl:attribute name="name">
        <xsl:call-template name="canonical-var-name" />
      </xsl:attribute>
      <xsl:if test="not(parent::request) and node()
                    and not(.//*[not(self::value or self::op)])">
        <xsl:attribute name="fixed">true</xsl:attribute>
      </xsl:if>
      <!-- Handle lists with no length expressions. -->
      <xsl:if test="not(node())">
        <xsl:choose>
          <!-- In a request, refer to an implicit localparam for length. -->
          <xsl:when test="parent::request">
            <fieldref>
              <xsl:value-of select="concat(@name, '_len')" />
            </fieldref>
          </xsl:when>
          <!-- In a reply, use the length of the reply to determine the length
               of the list. -->
          <xsl:when test="parent::reply">
            <op op="/">
              <op op="&lt;&lt;">
                <fieldref>length</fieldref>
                <value>2</value>
              </op>
              <function-call name="sizeof">
                <param><xsl:value-of select="$type" /></param>
              </function-call>
            </op>
          </xsl:when>
          <!-- Other cases generate an error. -->
          <xsl:otherwise>
            <xsl:message terminate="yes"><!--
              -->Encountered a list with no length expresssion outside a<!--
              --> request or reply.<!--
            --></xsl:message>
          </xsl:otherwise>
        </xsl:choose>
      </xsl:if>
      <xsl:copy-of select="*" />
    </list>
  </xsl:template>

  <xsl:template match="valueparam" mode="field">
    <field>
      <xsl:attribute name="type">
        <xsl:call-template name="canonical-type-name">
          <xsl:with-param name="type" select="@value-mask-type" />
        </xsl:call-template>
Josh Triplett's avatar
Josh Triplett committed
587
        <xsl:text>_t</xsl:text>
Josh Triplett's avatar
Josh Triplett committed
588
589
590
591
592
593
594
      </xsl:attribute>
      <xsl:attribute name="name">
        <xsl:call-template name="canonical-var-name">
          <xsl:with-param name="name" select="@value-mask-name" />
        </xsl:call-template>
      </xsl:attribute>
    </field>
Josh Triplett's avatar
Josh Triplett committed
595
    <list type="uint32_t">
Josh Triplett's avatar
Josh Triplett committed
596
597
598
599
600
      <xsl:attribute name="name">
        <xsl:call-template name="canonical-var-name">
          <xsl:with-param name="name" select="@value-list-name" />
        </xsl:call-template>
      </xsl:attribute>
Josh Triplett's avatar
Josh Triplett committed
601
      <function-call name="xcb_popcount">
Josh Triplett's avatar
Josh Triplett committed
602
603
604
605
606
607
608
609
610
611
612
        <param>
          <fieldref>
            <xsl:call-template name="canonical-var-name">
              <xsl:with-param name="name" select="@value-mask-name" />
            </xsl:call-template>
          </fieldref>
        </param>
      </function-call>
    </list>
  </xsl:template>

613
  <xsl:template match="field" mode="param">
Josh Triplett's avatar
Josh Triplett committed
614
615
616
    <field>
      <xsl:attribute name="type">
        <xsl:call-template name="canonical-type-name" />
Josh Triplett's avatar
Josh Triplett committed
617
        <xsl:text>_t</xsl:text>
Josh Triplett's avatar
Josh Triplett committed
618
619
620
621
622
623
624
625
626
627
      </xsl:attribute>
      <xsl:attribute name="name">
        <xsl:call-template name="canonical-var-name" />
      </xsl:attribute>
    </field>
  </xsl:template>

  <xsl:template match="list" mode="param">
    <!-- If no length expression is provided, use a CARD32 localfield. -->
    <xsl:if test="not(node())">
Josh Triplett's avatar
Josh Triplett committed
628
      <field type="uint32_t" name="{@name}_len" />
Josh Triplett's avatar
Josh Triplett committed
629
630
    </xsl:if>
    <field>
Josh Triplett's avatar
Josh Triplett committed
631
632
633
      <xsl:variable name="ctype">
        <xsl:call-template name="canonical-type-name" />
      </xsl:variable>
Josh Triplett's avatar
Josh Triplett committed
634
635
636
      <xsl:attribute name="type">
        <xsl:text>const </xsl:text>
        <xsl:call-template name="canonical-type-name" />
Josh Triplett's avatar
Josh Triplett committed
637
638
639
        <xsl:if test="not($ctype='char') and not($ctype='void')">
          <xsl:text>_t</xsl:text>
        </xsl:if>
Josh Triplett's avatar
Josh Triplett committed
640
641
642
643
644
645
646
647
648
649
650
651
652
653
        <xsl:text> *</xsl:text>
      </xsl:attribute>
      <xsl:attribute name="name">
        <xsl:call-template name="canonical-var-name" />
      </xsl:attribute>
    </field>
  </xsl:template>

  <xsl:template match="valueparam" mode="param">
    <field>
      <xsl:attribute name="type">
        <xsl:call-template name="canonical-type-name">
          <xsl:with-param name="type" select="@value-mask-type" />
        </xsl:call-template>
Josh Triplett's avatar
Josh Triplett committed
654
        <xsl:text>_t</xsl:text>
Josh Triplett's avatar
Josh Triplett committed
655
656
657
658
659
660
661
      </xsl:attribute>
      <xsl:attribute name="name">
        <xsl:call-template name="canonical-var-name">
          <xsl:with-param name="name" select="@value-mask-name" />
        </xsl:call-template>
      </xsl:attribute>
    </field>
Josh Triplett's avatar
Josh Triplett committed
662
    <field type="const uint32_t *">
Josh Triplett's avatar
Josh Triplett committed
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
      <xsl:attribute name="name">
        <xsl:call-template name="canonical-var-name">
          <xsl:with-param name="name" select="@value-list-name" />
        </xsl:call-template>
      </xsl:attribute>
    </field>
  </xsl:template>

  <!-- Second pass: Process the variable. -->
  <xsl:variable name="result-rtf">
    <xsl:apply-templates select="$pass1/*" mode="pass2" />
  </xsl:variable>
  <xsl:variable name="result" select="e:node-set($result-rtf)" />

  <xsl:template match="xcb" mode="pass2">
    <xcb>
      <xsl:copy-of select="@*" />
      <xsl:apply-templates mode="pass2"
                           select="constant|enum|struct|typedef|iterator" />
      <xsl:apply-templates mode="pass2"
                           select="function|iterator-functions" />
    </xcb>
  </xsl:template>

  <!-- Generic rules for nodes that don't need further processing: copy node
       with attributes, and recursively process the child nodes. -->
  <xsl:template match="*" mode="pass2">
    <xsl:copy>
      <xsl:copy-of select="@*" />
      <xsl:apply-templates mode="pass2" />
    </xsl:copy>
  </xsl:template>

  <xsl:template match="struct" mode="pass2">
    <xsl:if test="@kind='union' and list[not(@fixed)]">
      <xsl:message terminate="yes">Unions must be fixed length.</xsl:message>
    </xsl:if>
    <struct name="{@name}">
      <xsl:if test="@kind">
        <xsl:attribute name="kind">
          <xsl:value-of select="@kind" />
        </xsl:attribute>
      </xsl:if>
      <!-- FIXME: This should go by size, not number of fields. -->
      <xsl:copy-of select="node()[not(self::middle)
                   and position() &lt; 3]" />
      <xsl:if test="middle and (count(*[not(self::middle)]) &lt; 2)">
        <pad bytes="{2 - count(*[not(self::middle)])}" />
      </xsl:if>
      <xsl:copy-of select="middle/*" />
      <xsl:copy-of select="node()[not(self::middle) and (position() > 2)]" />
    </struct>
  </xsl:template>

  <xsl:template match="do-request" mode="pass2">
    <xsl:variable name="struct"
                  select="$pass1/xcb/struct[@name=current()/@ref]" />

721
    <xsl:variable name="num-parts" select="(1+count($struct/list))*2" />
Josh Triplett's avatar
Josh Triplett committed
722

Josh Triplett's avatar
Josh Triplett committed
723
    <l>static const xcb_protocol_request_t xcb_req = {</l>
Josh Triplett's avatar
Josh Triplett committed
724
725
726
727
    <indent>
      <l>/* count */ <xsl:value-of select="$num-parts" />,</l>
      <l>/* ext */ <xsl:choose>
                     <xsl:when test="$ext">
Josh Triplett's avatar
Josh Triplett committed
728
729
730
                       <xsl:text>&amp;</xsl:text>
                       <xsl:value-of select="xcb:xcb-prefix()" />
                       <xsl:text>_id</xsl:text>
Josh Triplett's avatar
Josh Triplett committed
731
732
733
734
735
736
737
738
739
                     </xsl:when>
                     <xsl:otherwise>0</xsl:otherwise>
                   </xsl:choose>,</l>
      <l>/* opcode */ <xsl:value-of select="@opcode" />,</l>
      <l>/* isvoid */ <xsl:value-of select="1-boolean(@has-reply)" /></l>
    </indent>
    <l>};</l>

    <l />
740
    <l>struct iovec xcb_parts[<xsl:value-of select="$num-parts+2" />];</l>
Josh Triplett's avatar
Josh Triplett committed
741
742
743
744
    <l><xsl:value-of select="../@type" /> xcb_ret;</l>
    <l><xsl:value-of select="@ref" /> xcb_out;</l>

    <l />
745
746
747
748
    <xsl:if test="not ($ext) and not($struct//*[(self::field or self::exprfield or self::pad)
                                                and not(boolean(@no-assign))])">
      <l>xcb_out.pad0 = 0;</l>
    </xsl:if>
749
    <xsl:apply-templates select="$struct//*[(self::field or self::exprfield or self::pad)
Josh Triplett's avatar
Josh Triplett committed
750
751
752
753
                                            and not(boolean(@no-assign))]"
                         mode="assign" />

    <l />
754
    <l>xcb_parts[2].iov_base = (char *) &amp;xcb_out;</l>
755
756
757
    <l>xcb_parts[2].iov_len = sizeof(xcb_out);</l>
    <l>xcb_parts[3].iov_base = 0;</l>
    <l>xcb_parts[3].iov_len = -xcb_parts[2].iov_len &amp; 3;</l>
Josh Triplett's avatar
Josh Triplett committed
758
759

    <xsl:for-each select="$struct/list">
760
      <l>xcb_parts[<xsl:value-of select="2 + position() * 2"/>].iov_base = (char *) <!--
Josh Triplett's avatar
Josh Triplett committed
761
      --><xsl:value-of select="@name" />;</l>
762
      <l>xcb_parts[<xsl:value-of select="2 + position() * 2"/>].iov_len = <!--
Josh Triplett's avatar
Josh Triplett committed
763
764
      --><xsl:apply-templates mode="output-expression" />
      <xsl:if test="not(@type = 'void_t')">
Josh Triplett's avatar
Josh Triplett committed
765
        <xsl:text> * sizeof(</xsl:text>
Josh Triplett's avatar
Josh Triplett committed
766
767
768
769
770
771
772
773
          <xsl:choose>
          <xsl:when test="@type='char_t'">
            <xsl:text>char</xsl:text>
          </xsl:when>
          <xsl:otherwise>
            <xsl:value-of select="@type" />
          </xsl:otherwise>
        </xsl:choose>
Josh Triplett's avatar
Josh Triplett committed
774
775
        <xsl:text>)</xsl:text>
      </xsl:if>;</l>
776
777
      <l>xcb_parts[<xsl:value-of select="3 + position() * 2"/>].iov_base = 0;</l>
      <l>xcb_parts[<xsl:value-of select="3 + position() * 2"/>].iov_len = -xcb_parts[<xsl:value-of select="2 + position() * 2"/>].iov_len &amp; 3;</l>
Josh Triplett's avatar
Josh Triplett committed
778
779
    </xsl:for-each>

Josh Triplett's avatar
Josh Triplett committed
780
    <l>xcb_ret.sequence = xcb_send_request(c, <!--
781
    --><xsl:choose>
782
         <xsl:when test="@checked='true'">XCB_REQUEST_CHECKED</xsl:when>
783
         <xsl:otherwise>0</xsl:otherwise>
784
       </xsl:choose>, xcb_parts + 2, &amp;xcb_req);</l>
Josh Triplett's avatar
Josh Triplett committed
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
    <l>return xcb_ret;</l>
  </xsl:template>

  <xsl:template match="field" mode="assign">
    <l>
      <xsl:text>xcb_out.</xsl:text>
      <xsl:value-of select="@name" />
      <xsl:text> = </xsl:text>
      <xsl:value-of select="@name" />
      <xsl:text>;</xsl:text>
    </l>
  </xsl:template>

  <xsl:template match="exprfield" mode="assign">
    <l>
      <xsl:text>xcb_out.</xsl:text>
      <xsl:value-of select="@name" />
      <xsl:text> = </xsl:text>
      <xsl:apply-templates mode="output-expression" />
      <xsl:text>;</xsl:text>
    </l>
  </xsl:template>

808
809
810
811
812
813
814
815
  <xsl:template match="pad" mode="assign">
    <xsl:variable name="padnum"><xsl:number /></xsl:variable>
    <l><xsl:choose>
        <xsl:when test="@bytes = 1">xcb_out.pad<xsl:value-of select="$padnum - 1" /> = 0;</xsl:when>
        <xsl:otherwise>memset(xcb_out.pad<xsl:value-of select="$padnum - 1" />, 0, <xsl:value-of select="@bytes" />);</xsl:otherwise>
    </xsl:choose></l>
  </xsl:template>

Josh Triplett's avatar
Josh Triplett committed
816
  <xsl:template match="iterator" mode="pass2">
Josh Triplett's avatar
Josh Triplett committed
817
818
    <struct name="{@ref}_iterator_t">
      <field type="{@ref}_t *" name="data" />
Josh Triplett's avatar
Josh Triplett committed
819
820
821
822
823
824
825
826
827
      <field type="int" name="rem" />
      <field type="int" name="index" />
    </struct>
  </xsl:template>

  <xsl:template match="iterator-functions" mode="pass2">
    <xsl:variable name="ref" select="@ref" />
    <xsl:variable name="kind" select="@kind" />
    <xsl:variable name="struct"
Josh Triplett's avatar
Josh Triplett committed
828
                  select="$pass1/xcb/struct[@name=concat($ref, $kind, '_t')]" />
Josh Triplett's avatar
Josh Triplett committed
829
830
831
832
    <xsl:variable name="nextfields-rtf">
      <nextfield>R + 1</nextfield>
      <xsl:for-each select="$struct/list[not(@fixed)]">
        <xsl:choose>
Josh Triplett's avatar
Josh Triplett committed
833
834
835
836
          <xsl:when test="substring(@type, 1, 3) = 'xcb'">
            <nextfield><xsl:value-of select="substring(@type, 1, string-length(@type)-2)" />_end(<!--
            --><xsl:value-of select="$ref" />_<!--
            --><xsl:value-of select="string(@name)" />_iterator(R))</nextfield>
Josh Triplett's avatar
Josh Triplett committed
837
838
          </xsl:when>
          <xsl:otherwise>
Josh Triplett's avatar
Josh Triplett committed
839
840
            <nextfield><xsl:value-of select="$ref" />_<!--
            --><xsl:value-of select="string(@name)" />_end(R)</nextfield>
Josh Triplett's avatar
Josh Triplett committed
841
842
843
844
845
846
847
848
849
850
851
          </xsl:otherwise>
        </xsl:choose>
      </xsl:for-each>
    </xsl:variable>
    <xsl:variable name="nextfields" select="e:node-set($nextfields-rtf)" />
    <xsl:for-each select="$struct/list[not(@fixed)]">
      <xsl:variable name="number"
                    select="1+count(preceding-sibling::list[not(@fixed)])" />
      <xsl:variable name="nextfield" select="$nextfields/nextfield[$number]" />
      <xsl:variable name="is-first"
                    select="not(preceding-sibling::list[not(@fixed)])" />
Josh Triplett's avatar
Josh Triplett committed
852
      <xsl:variable name="field-name" select="@name" />
Josh Triplett's avatar
Josh Triplett committed
853
854
855
      <xsl:variable name="is-variable"
                    select="$pass1/xcb/struct[@name=current()/@type]/list
                            or document($search-path)/xcb
Josh Triplett's avatar
Josh Triplett committed
856
857
858
859
860
                               /struct[concat(xcb:xcb-prefix(@name), '_t')
                                       = current()/@type]
                               /*[self::valueparam
                                  or self::list[.//*[not(self::value
                                                         or self::op)]]]" />
Josh Triplett's avatar
Josh Triplett committed
861
      <xsl:if test="not($is-variable)">
Josh Triplett's avatar
Josh Triplett committed
862
863
        <function type="{xcb:get-char-void(@type)} *" name="{$ref}_{xcb:lowercase($field-name)}">
          <field type="const {$ref}{$kind}_t *" name="R" />
Josh Triplett's avatar
Josh Triplett committed
864
865
          <xsl:choose>
            <xsl:when test="$is-first">
Josh Triplett's avatar
Josh Triplett committed
866
              <l>return (<xsl:value-of select="xcb:get-char-void(@type)" /> *) <!--
Josh Triplett's avatar
Josh Triplett committed
867
868
869
              -->(<xsl:value-of select="$nextfield" />);</l>
            </xsl:when>
            <xsl:otherwise>
Josh Triplett's avatar
Josh Triplett committed
870
              <l>xcb_generic_iterator_t prev = <!--
Josh Triplett's avatar
Josh Triplett committed
871
              --><xsl:value-of select="$nextfield" />;</l>
Josh Triplett's avatar
Josh Triplett committed
872
              <l>return (<xsl:value-of select="xcb:get-char-void(@type)" /> *) <!--
Josh Triplett's avatar
Josh Triplett committed
873
              -->((char *) prev.data + XCB_TYPE_PAD(<!--
Josh Triplett's avatar
Josh Triplett committed
874
              --><xsl:value-of select="xcb:get-char-void(@type)" />, prev.index));</l>
Josh Triplett's avatar
Josh Triplett committed
875
876
877
878
            </xsl:otherwise>
          </xsl:choose>
        </function>
      </xsl:if>
Josh Triplett's avatar
Josh Triplett committed
879
880
      <function type="int" name="{$ref}_{xcb:lowercase($field-name)}_length">
        <field type="const {$ref}{$kind}_t *" name="R" />
Josh Triplett's avatar
Josh Triplett committed
881
882
883
884
885
        <l>return <xsl:apply-templates mode="output-expression">
                    <xsl:with-param name="field-prefix" select="'R->'" />
                  </xsl:apply-templates>;</l>
      </function>
      <xsl:choose>
Josh Triplett's avatar
Josh Triplett committed
886
887
888
889
        <xsl:when test="substring(@type, 1, 3) = 'xcb'">
          <function type="{substring(@type, 1, string-length(@type)-2)}_iterator_t" name="{$ref}_{xcb:lowercase($field-name)}_iterator">
            <field type="const {$ref}{$kind}_t *" name="R" />
            <l><xsl:value-of select="substring(@type, 1, string-length(@type)-2)" />_iterator_t i;</l>
Josh Triplett's avatar
Josh Triplett committed
890
891
892
893
894
895
            <xsl:choose>
              <xsl:when test="$is-first">
                <l>i.data = (<xsl:value-of select="@type" /> *) <!--
                -->(<xsl:value-of select="$nextfield" />);</l>
              </xsl:when>
              <xsl:otherwise>
Josh Triplett's avatar
Josh Triplett committed
896
                <l>xcb_generic_iterator_t prev = <!--
Josh Triplett's avatar
Josh Triplett committed
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
                --><xsl:value-of select="$nextfield" />;</l>
                <l>i.data = (<xsl:value-of select="@type" /> *) <!--
                -->((char *) prev.data + XCB_TYPE_PAD(<!--
                --><xsl:value-of select="@type" />, prev.index));</l>
              </xsl:otherwise>
            </xsl:choose>
            <l>i.rem = <xsl:apply-templates mode="output-expression">
                         <xsl:with-param name="field-prefix" select="'R->'" />
                       </xsl:apply-templates>;</l>
            <l>i.index = (char *) i.data - (char *) R;</l>
            <l>return i;</l>
          </function>
        </xsl:when>
        <xsl:otherwise>
          <xsl:variable name="cast">
            <xsl:choose>
              <xsl:when test="@type='void'">char</xsl:when>
              <xsl:otherwise><xsl:value-of select="@type" /></xsl:otherwise>
            </xsl:choose>
          </xsl:variable>
Josh Triplett's avatar
Josh Triplett committed
917
918
919
          <function type="xcb_generic_iterator_t" name="{$ref}_{xcb:lowercase($field-name)}_end">
            <field type="const {$ref}{$kind}_t *" name="R" />
            <l>xcb_generic_iterator_t i;</l>
Josh Triplett's avatar
Josh Triplett committed
920
921
            <xsl:choose>
              <xsl:when test="$is-first">
Josh Triplett's avatar
Josh Triplett committed
922
                <l>i.data = ((<xsl:value-of select="xcb:remove-void($cast)" /> *) <!--
Josh Triplett's avatar
Josh Triplett committed
923
924
925
926
927
928
                -->(<xsl:value-of select="$nextfield" />)) + (<!--
                --><xsl:apply-templates mode="output-expression">
                     <xsl:with-param name="field-prefix" select="'R->'" />
                   </xsl:apply-templates>);</l>
              </xsl:when>
              <xsl:otherwise>
Josh Triplett's avatar
Josh Triplett committed
929
                <l>xcb_generic_iterator_t child = <!--
Josh Triplett's avatar
Josh Triplett committed
930
                --><xsl:value-of select="$nextfield" />;</l>
Josh Triplett's avatar
Josh Triplett committed
931
                <l>i.data = ((<xsl:value-of select="xcb:get-char-void($cast)" /> *) <!--
Josh Triplett's avatar
Josh Triplett committed
932
933
934
935
936
937
938
939
940
941
942
943
944
945
                -->child.data) + (<!--
                --><xsl:apply-templates mode="output-expression">
                     <xsl:with-param name="field-prefix" select="'R->'" />
                   </xsl:apply-templates>);</l>
              </xsl:otherwise>
            </xsl:choose>
            <l>i.rem = 0;</l>
            <l>i.index = (char *) i.data - (char *) R;</l>
            <l>return i;</l>
          </function>
        </xsl:otherwise>
      </xsl:choose>
    </xsl:for-each>
    <xsl:if test="not($kind)">
Josh Triplett's avatar
Josh Triplett committed
946
      <function type="void" name="{$ref}_next">
947
948
949
950
951
952
953
954
        <doc>/**</doc>
        <doc> * Get the next element of the iterator</doc>
        <doc> * @param i Pointer to a <xsl:value-of select="$ref" />_iterator_t</doc>
        <doc> *</doc>
        <doc> * Get the next element in the iterator. The member rem is</doc>
        <doc> * decreased by one. The member data points to the next</doc>
        <doc> * element. The member index is increased by sizeof(<xsl:value-of select="$ref" />_t)</doc>
        <doc> */</doc>
Josh Triplett's avatar
Josh Triplett committed
955
        <field type="{$ref}_iterator_t *" name="i" />
Josh Triplett's avatar
Josh Triplett committed
956
957
        <xsl:choose>
          <xsl:when test="$struct/list[not(@fixed)]">
Josh Triplett's avatar
Josh Triplett committed
958
959
            <l><xsl:value-of select="$ref" />_t *R = i->data;</l>
            <l>xcb_generic_iterator_t child = <!--
Josh Triplett's avatar
Josh Triplett committed
960
961
            --><xsl:value-of select="$nextfields/nextfield[last()]" />;</l>
            <l>--i->rem;</l>
Josh Triplett's avatar
Josh Triplett committed
962
            <l>i->data = (<xsl:value-of select="$ref" />_t *) child.data;</l>
Josh Triplett's avatar
Josh Triplett committed
963
964
965
966
967
            <l>i->index = child.index;</l>
          </xsl:when>
          <xsl:otherwise>
            <l>--i->rem;</l>
            <l>++i->data;</l>
Josh Triplett's avatar
Josh Triplett committed
968
            <l>i->index += sizeof(<xsl:value-of select="$ref" />_t);</l>
Josh Triplett's avatar
Josh Triplett committed
969
970
971
          </xsl:otherwise>
        </xsl:choose>
      </function>
Josh Triplett's avatar
Josh Triplett committed
972
      <function type="xcb_generic_iterator_t" name="{$ref}_end">
973
974
975
976
977
978
979
980
981
        <doc>/**</doc>
        <doc> * Return the iterator pointing to the last element</doc>
        <doc> * @param i An <xsl:value-of select="$ref" />_iterator_t</doc>
        <doc> * @return  The iterator pointing to the last element</doc>
        <doc> *</doc>
        <doc> * Set the current element in the iterator to the last element.</doc>
        <doc> * The member rem is set to 0. The member data points to the</doc>
        <doc> * last element.</doc>
        <doc> */</doc>
Josh Triplett's avatar
Josh Triplett committed
982
983
        <field type="{$ref}_iterator_t" name="i" />
        <l>xcb_generic_iterator_t ret;</l>
Josh Triplett's avatar
Josh Triplett committed
984
985
986
987
        <xsl:choose>
          <xsl:when test="$struct/list[not(@fixed)]">
            <l>while(i.rem > 0)</l>
            <indent>
Josh Triplett's avatar
Josh Triplett committed
988
              <l><xsl:value-of select="$ref" />_next(&amp;i);</l>
Josh Triplett's avatar
Josh Triplett committed
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
            </indent>
            <l>ret.data = i.data;</l>
            <l>ret.rem = i.rem;</l>
            <l>ret.index = i.index;</l>
          </xsl:when>
          <xsl:otherwise>
            <l>ret.data = i.data + i.rem;</l>
            <l>ret.index = i.index + ((char *) ret.data - (char *) i.data);</l>
            <l>ret.rem = 0;</l>
          </xsl:otherwise>
        </xsl:choose>
        <l>return ret;</l>
      </function>
    </xsl:if>
  </xsl:template>

  <!-- Output the results. -->
  <xsl:template match="/">
    <xsl:if test="not(function-available('e:node-set'))">
      <xsl:message terminate="yes"><!--
        -->Error: This stylesheet requires the EXSL node-set extension.<!--
      --></xsl:message>
    </xsl:if>

    <xsl:if test="not($h) and not($c)">
      <xsl:message terminate="yes"><!--
        -->Error: Parameter "mode" must be "header" or "source".<!--
      --></xsl:message>
    </xsl:if>

    <xsl:apply-templates select="$result/*" mode="output" />
  </xsl:template>

  <xsl:template match="xcb" mode="output">
    <xsl:variable name="guard"><!--
      -->__<xsl:value-of select="$ucase-header" />_H<!--
    --></xsl:variable>

Jamey Sharp's avatar
Jamey Sharp committed
1027
1028
1029
1030
1031
<xsl:text>/*
 * This file generated automatically from </xsl:text>
<xsl:value-of select="$header" /><xsl:text>.xml by c-client.xsl using XSLT.
 * Edit at your peril.
 */
1032
</xsl:text>
Jamey Sharp's avatar
Jamey Sharp committed
1033
<xsl:if test="$h"><xsl:text>
1034
1035
1036
1037
1038
1039
1040
/**
 * @defgroup XCB_</xsl:text><xsl:value-of select="$ext" /><xsl:text>_API XCB </xsl:text><xsl:value-of select="$ext" /><xsl:text> API
 * @brief </xsl:text><xsl:value-of select="$ext" /><xsl:text> XCB Protocol Implementation.</xsl:text>
<xsl:text>
 * @{
 **/
</xsl:text>
Josh Triplett's avatar
Josh Triplett committed
1041

Jamey Sharp's avatar
Jamey Sharp committed
1042
<xsl:text>
Josh Triplett's avatar
Josh Triplett committed
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
#ifndef </xsl:text><xsl:value-of select="$guard" /><xsl:text>
#define </xsl:text><xsl:value-of select="$guard" /><xsl:text>
</xsl:text>
#include "xcb.h"
<xsl:for-each select="$root/xcb/import">
<xsl:text>#include "</xsl:text><xsl:value-of select="." /><xsl:text>.h"
</xsl:text>
</xsl:for-each>
<xsl:text>
</xsl:text>
</xsl:if>
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
<xsl:if test="$h">
    <xsl:choose>
        <xsl:when test="string($ext)">
  <xsl:text>#define XCB_</xsl:text><xsl:value-of select="translate($ext, $lcase, $ucase)"/><xsl:text>_MAJOR_VERSION </xsl:text><xsl:value-of select="/xcb/@major-version" /><xsl:text>
</xsl:text>
  <xsl:text>#define XCB_</xsl:text><xsl:value-of select="translate($ext, $lcase, $ucase)"/><xsl:text>_MINOR_VERSION </xsl:text><xsl:value-of select="/xcb/@minor-version" />
  <xsl:text>
  
</xsl:text>
    </xsl:when>
  </xsl:choose>
</xsl:if>
Josh Triplett's avatar
Josh Triplett committed
1066

1067
1068
1069
1070
<xsl:if test="$c">
<xsl:if test="$need-string-h">
#include &lt;string.h&gt;</xsl:if>
<xsl:text>
Josh Triplett's avatar
Josh Triplett committed
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
#include &lt;assert.h&gt;
#include "xcbext.h"
#include "</xsl:text><xsl:value-of select="$header" /><xsl:text>.h"

</xsl:text></xsl:if>

    <xsl:apply-templates mode="output" />

<xsl:if test="$h">
<xsl:text>
Jamey Sharp's avatar
Jamey Sharp committed
1081
1082
#endif

1083
1084
1085
/**
 * @}
 */
Josh Triplett's avatar
Josh Triplett committed
1086
1087
1088
1089
1090
1091
1092
1093
</xsl:text>
</xsl:if>
  </xsl:template>

  <xsl:template match="constant" mode="output">
    <xsl:choose>
      <xsl:when test="@type = 'number'">
        <xsl:if test="$h">
1094
1095
          <xsl:text>/** Opcode for </xsl:text><xsl:value-of select="@name"/><xsl:text>. */
</xsl:text>
Josh Triplett's avatar
Josh Triplett committed
1096
          <xsl:text>#define </xsl:text>
Josh Triplett's avatar
Josh Triplett committed
1097
          <xsl:value-of select="translate(@name, $lcase, $ucase)" />
Josh Triplett's avatar
Josh Triplett committed
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
          <xsl:text> </xsl:text>
          <xsl:value-of select="@value" />
          <xsl:text>

</xsl:text>
        </xsl:if>
      </xsl:when>
      <xsl:when test="@type = 'string'">
        <xsl:if test="$h">
          <xsl:text>extern </xsl:text>
        </xsl:if>
        <xsl:text>const char </xsl:text>
        <xsl:value-of select="@name" />
        <xsl:text>[]</xsl:text>
        <xsl:if test="$c">
          <xsl:text> = "</xsl:text>
          <xsl:value-of select="@value" />
          <xsl:text>"</xsl:text>
        </xsl:if>
        <xsl:text>;

</xsl:text>
      </xsl:when>
      <xsl:otherwise>
        <xsl:if test="$h">
          <xsl:text>extern </xsl:text>
        </xsl:if>
        <xsl:call-template name="type-and-name" />
        <xsl:if test="$c">
          <xsl:text> = </xsl:text>
          <xsl:value-of select="@value" />
        </xsl:if>
        <xsl:text>;

</xsl:text>
      </xsl:otherwise>
    </xsl:choose>
  </xsl:template>

  <xsl:template match="typedef" mode="output">
    <xsl:if test="$h">
      <xsl:text>typedef </xsl:text>
Josh Triplett's avatar
Josh Triplett committed
1140
      <xsl:value-of select="xcb:get-char-void(@oldname)" />
Josh Triplett's avatar
Josh Triplett committed
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
      <xsl:text> </xsl:text>
      <xsl:value-of select="@newname" />
      <xsl:text>;

</xsl:text>
    </xsl:if>
  </xsl:template>

  <xsl:template match="struct" mode="output">
    <xsl:if test="$h">
      <xsl:variable name="type-lengths">
        <xsl:call-template name="type-lengths">
          <xsl:with-param name="items" select="field/@type" />
        </xsl:call-template>
      </xsl:variable>
1156
1157
1158
1159
      <xsl:text>/**
 * @brief </xsl:text><xsl:value-of select="@name" /><xsl:text>
 **/
</xsl:text>
Josh Triplett's avatar
Josh Triplett committed
1160
1161
      <xsl:text>typedef </xsl:text>
      <xsl:if test="not(@kind)">struct</xsl:if><xsl:value-of select="@kind" />
1162
1163
      <xsl:text> </xsl:text>
      <xsl:value-of select="@name" />
Josh Triplett's avatar
Josh Triplett committed
1164
1165
1166
1167
1168
1169
1170
      <xsl:text> {
</xsl:text>
      <xsl:for-each select="exprfield|field|list[@fixed]|pad">
        <xsl:text>    </xsl:text>
        <xsl:apply-templates select=".">
          <xsl:with-param name="type-lengths" select="$type-lengths" />
        </xsl:apply-templates>
1171
        <xsl:text>; /**&lt; </xsl:text><xsl:text> */
Josh Triplett's avatar
Josh Triplett committed
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
</xsl:text>
      </xsl:for-each>
      <xsl:text>} </xsl:text>
      <xsl:value-of select="@name" />
      <xsl:text>;

</xsl:text>
    </xsl:if>
  </xsl:template>

  <xsl:template match="enum" mode="output">
    <xsl:if test="$h">
1184
1185
1186
      <xsl:text>typedef enum </xsl:text>
      <xsl:value-of select="@name" />
      <xsl:text> {
Josh Triplett's avatar
Josh Triplett committed
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
    </xsl:text>
      <xsl:call-template name="list">
        <xsl:with-param name="separator"><xsl:text>,
    </xsl:text></xsl:with-param>
        <xsl:with-param name="items">
          <xsl:for-each select="item">
            <item>
              <xsl:value-of select="@name" />
              <xsl:if test="node()"> <!-- If there is an expression -->
                <xsl:text> = </xsl:text>
                <xsl:apply-templates mode="output-expression" />
              </xsl:if>
            </item>
          </xsl:for-each>
        </xsl:with-param>
      </xsl:call-template>
      <xsl:text>
} </xsl:text><xsl:value-of select="@name" /><xsl:text>;

</xsl:text>
    </xsl:if>
  </xsl:template>

  <xsl:template match="function" mode="output">
    <xsl:variable name="decl-open" select="concat(@name, ' (')" />
    <xsl:variable name="type-lengths">
      <xsl:call-template name="type-lengths">
        <xsl:with-param name="items" select="field/@type" />
      </xsl:call-template>
1216
1217
  </xsl:variable>
  <!-- Doxygen for functions in header. -->
1218
1219
1220
1221
    <xsl:if test="$h">
      <xsl:apply-templates select="doc" mode="function-doc">
      </xsl:apply-templates>
    </xsl:if>
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
/*****************************************************************************
 **
 ** <xsl:value-of select="@type" />
 <xsl:text> </xsl:text>
 <xsl:value-of select="@name" />
 ** <xsl:call-template name="list">
     <xsl:with-param name="items">
         <xsl:for-each select="field">
             <item>
                 <xsl:text>
 ** @param </xsl:text>
                 <xsl:apply-templates select=".">
                     <xsl:with-param name="type-lengths" select="$type-lengths" />
                 </xsl:apply-templates>
             </item>
         </xsl:for-each>
     </xsl:with-param>
 </xsl:call-template>
 ** @returns <xsl:value-of select="@type" />
 **
 *****************************************************************************/
 
<xsl:value-of select="@type" />
Josh Triplett's avatar
Josh Triplett committed
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
    <xsl:text>
</xsl:text>
    <xsl:value-of select="$decl-open" />
    <xsl:call-template name="list">
      <xsl:with-param name="separator">
        <xsl:text>,
</xsl:text>
        <xsl:call-template name="repeat">
          <xsl:with-param name="count" select="string-length($decl-open)" />
        </xsl:call-template>
      </xsl:with-param>
      <xsl:with-param name="items">
        <xsl:for-each select="field">
          <item>
            <xsl:apply-templates select=".">
              <xsl:with-param name="type-lengths" select="$type-lengths" />
            </xsl:apply-templates>
1262
            <xsl:text>  /**&lt; */</xsl:text>
Josh Triplett's avatar
Josh Triplett committed
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
          </item>
        </xsl:for-each>
      </xsl:with-param>
    </xsl:call-template>
    <xsl:text>)</xsl:text>

    <xsl:if test="$h"><xsl:text>;

</xsl:text></xsl:if>

    <xsl:if test="$c">
      <xsl:text>
{
</xsl:text>
      <xsl:apply-templates select="l|indent" mode="function-body">
        <xsl:with-param name="indent" select="$indent-string" />
      </xsl:apply-templates>
      <xsl:text>}

</xsl:text>
    </xsl:if>
  </xsl:template>

1286
1287
1288
1289
1290
  <xsl:template match="doc" mode="function-doc">
    <xsl:value-of select="." /><xsl:text>
</xsl:text>
  </xsl:template>

Josh Triplett's avatar
Josh Triplett committed
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
  <xsl:template match="l" mode="function-body">
    <xsl:param name="indent" />
    <xsl:value-of select="concat($indent, .)" /><xsl:text>
</xsl:text>
  </xsl:template>

  <xsl:template match="indent" mode="function-body">
    <xsl:param name="indent" />
    <xsl:apply-templates select="l|indent" mode="function-body">
      <xsl:with-param name="indent" select="concat($indent, $indent-string)" />
    </xsl:apply-templates>
  </xsl:template>

  <xsl:template match="value" mode="output-expression">
    <xsl:value-of select="." />
  </xsl:template>

  <xsl:template match="fieldref" mode="output-expression">
    <xsl:param name="field-prefix" />
    <xsl:value-of select="concat($field-prefix, .)" />
  </xsl:template>

  <xsl:template match="op" mode="output-expression">
    <xsl:param name="field-prefix" />
    <xsl:text>(</xsl:text>
    <xsl:apply-templates select="node()[1]" mode="output-expression">
      <xsl:with-param name="field-prefix" select="$field-prefix" />
    </xsl:apply-templates>
    <xsl:text> </xsl:text>
    <xsl:value-of select="@op" />
    <xsl:text> </xsl:text>
    <xsl:apply-templates select="node()[2]" mode="output-expression">
      <xsl:with-param name="field-prefix" select="$field-prefix" />
    </xsl:apply-templates>
    <xsl:text>)</xsl:text>
  </xsl:template>

1328
1329
  <xsl:template match="bit" mode="output-expression">
    <xsl:text>(1 &lt;&lt; </xsl:text>
1330
    <xsl:value-of select="." />
1331
1332
1333
    <xsl:text>)</xsl:text>
  </xsl:template>

Josh Triplett's avatar
Josh Triplett committed
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
  <xsl:template match="function-call" mode="output-expression">
    <xsl:param name="field-prefix" />
    <xsl:value-of select="@name" />
    <xsl:text>(</xsl:text>
    <xsl:call-template name="list">
      <xsl:with-param name="separator" select="', '" />
      <xsl:with-param name="items">
        <xsl:for-each select="param">
          <item><xsl:apply-templates mode="output-expression">
            <xsl:with-param name="field-prefix" select="$field-prefix" />
          </xsl:apply-templates></item>
        </xsl:for-each>
      </xsl:with-param>
    </xsl:call-template>
    <xsl:text>)</xsl:text>
  </xsl:template>

  <!-- Catch invalid elements in expressions. -->
  <xsl:template match="*" mode="output-expression">
    <xsl:message terminate="yes">
      <xsl:text>Invalid element in expression: </xsl:text>
      <xsl:value-of select="name()" />
    </xsl:message>
  </xsl:template>

  <xsl:template match="field|exprfield">
    <xsl:param name="type-lengths" select="0" />
    <xsl:call-template name="type-and-name">
      <xsl:with-param name="type-lengths" select="$type-lengths" />
    </xsl:call-template>
  </xsl:template>

  <xsl:template match="list[@fixed]">
    <xsl:param name="type-lengths" select="0" />
    <xsl:call-template name="type-and-name">
      <xsl:with-param name="type-lengths" select="$type-lengths" />
    </xsl:call-template>
    <xsl:text>[</xsl:text>
    <xsl:apply-templates mode="output-expression" />
    <xsl:text>]</xsl:text>
  </xsl:template>

  <xsl:template match="pad">
    <xsl:param name="type-lengths" select="0" />

    <xsl:variable name="padnum"><xsl:number /></xsl:variable>

    <xsl:call-template name="type-and-name">
Josh Triplett's avatar
Josh Triplett committed
1382
      <xsl:with-param name="type" select="'uint8_t'" />
Josh Triplett's avatar
Josh Triplett committed
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
      <xsl:with-param name="name">
        <xsl:text>pad</xsl:text>
        <xsl:value-of select="$padnum - 1" />
      </xsl:with-param>
      <xsl:with-param name="type-lengths" select="$type-lengths" />
    </xsl:call-template>
    <xsl:if test="@bytes > 1">
      <xsl:text>[</xsl:text>
      <xsl:value-of select="@bytes" />
      <xsl:text>]</xsl:text>
    </xsl:if>
  </xsl:template>

  <!-- Output the given type and name (defaulting to the corresponding
       attributes of the context node), with the appropriate spacing.  The
       type must consist of a base type (which may contain spaces), then
       optionally a single space and a suffix of one or more '*' characters.
       If the type-lengths parameter is provided, use it to line up the base
       types and suffixs of the type declarations. -->
  <xsl:template name="type-and-name">
    <xsl:param name="type" select="@type" />
    <xsl:param name="name" select="@name" />
    <xsl:param name="type-lengths">
      <max-type-length>0</max-type-length>
      <max-suffix-length>0</max-suffix-length>
    </xsl:param>
    
    <xsl:variable name="type-lengths-ns" select="e:node-set($type-lengths)" />
    <xsl:variable name="min-type-length"
                  select="$type-lengths-ns/max-type-length" />
    <xsl:variable name="min-suffix-length"
                  select="$type-lengths-ns/max-suffix-length" />

    <xsl:variable name="base-type">
      <xsl:choose>
        <xsl:when test="contains($type, ' *')">
          <xsl:value-of select="substring-before($type, ' *')" />
        </xsl:when>
        <xsl:otherwise>
          <xsl:value-of select="$type" />
        </xsl:otherwise>
      </xsl:choose>
    </xsl:variable>
    <xsl:variable name="suffix">
      <xsl:if test="contains($type, ' *')">
        <xsl:text>*</xsl:text>
        <xsl:value-of select="substring-after($type, ' *')" />
      </xsl:if>
    </xsl:variable>

    <xsl:value-of select="$base-type" />
    <xsl:if test="string-length($base-type) &lt; $min-type-length">
      <xsl:call-template name="repeat">
        <xsl:with-param name="count" select="$min-type-length
                                             - string-length($base-type)" />
      </xsl:call-template>
    </xsl:if>
    <xsl:text> </xsl:text>
    <xsl:if test="string-length($suffix) &lt; $min-suffix-length">
      <xsl:call-template name="repeat">
        <xsl:with-param name="count" select="$min-suffix-length
                                             - string-length($suffix)" />
      </xsl:call-template>
    </xsl:if>
    <xsl:value-of select="$suffix" />
    <xsl:value-of select="$name" />
  </xsl:template>

  <!-- Output a list with a given separator.  Empty items are skipped. -->
  <xsl:template name="list">
    <xsl:param name="separator" />
    <xsl:param name="items" />

    <xsl:for-each select="e:node-set($items)/*">
      <xsl:value-of select="." />
      <xsl:if test="not(position() = last())">
        <xsl:value-of select="$separator" />
      </xsl:if>
    </xsl:for-each>
  </xsl:template>

  <!-- Repeat a string (space by default) a given number of times. -->
  <xsl:template name="repeat">
    <xsl:param name="str" select="' '" />
    <xsl:param name="count" />

    <xsl:if test="$count &gt; 0">
      <xsl:value-of select="$str" />
      <xsl:call-template name="repeat">
        <xsl:with-param name="str"   select="$str" />
        <xsl:with-param name="count" select="$count - 1" />
      </xsl:call-template>
    </xsl:if>
  </xsl:template>

  <!-- Record the maximum type lengths of a set of types for use as the
       max-type-lengths parameter of type-and-name. -->
  <xsl:template name="type-lengths">
    <xsl:param name="items" />
    <xsl:variable name="type-lengths-rtf">
      <xsl:for-each select="$items">
        <item>
          <xsl:choose>
            <xsl:when test="contains(., ' *')">
              <xsl:value-of select="string-length(
                                    substring-before(., ' *'))" />
            </xsl:when>
            <xsl:otherwise>
              <xsl:value-of select="string-length(.)" />
            </xsl:otherwise>
          </xsl:choose>
        </item>
      </xsl:for-each>
    </xsl:variable>
    <xsl:variable name="suffix-lengths-rtf">
      <xsl:for-each select="$items">
        <item>
          <xsl:choose>
            <xsl:when test="contains(., ' *')">
              <xsl:value-of select="string-length(substring-after(., ' *'))
                                    + 1" />
            </xsl:when>
            <xsl:otherwise>
              <xsl:text>0</xsl:text>
            </xsl:otherwise>
          </xsl:choose>
        </item>
      </xsl:for-each>
    </xsl:variable>
    <max-type-length>
      <xsl:call-template name="max">
        <xsl:with-param name="items"
                        select="e:node-set($type-lengths-rtf)/*" />
      </xsl:call-template>
    </max-type-length>
    <max-suffix-length>
      <xsl:call-template name="max">
        <xsl:with-param name="items"
                        select="e:node-set($suffix-lengths-rtf)/*" />
      </xsl:call-template>
    </max-suffix-length>
  </xsl:template>

  <!-- Return the maximum number in a set of numbers. -->
  <xsl:template name="max">
    <xsl:param name="items" />
    <xsl:choose>
      <xsl:when test="count($items) = 0">
        <xsl:text>0</xsl:text>
      </xsl:when>
      <xsl:otherwise>
        <xsl:variable name="head" select="number($items[1])" />
        <xsl:variable name="tail-max">
          <xsl:call-template name="max">
            <xsl:with-param name="items" select="$items[position() > 1]" />
          </xsl:call-template>
        </xsl:variable>
        <xsl:choose>
          <xsl:when test="$head > number($tail-max)">
            <xsl:value-of select="$head" />
          </xsl:when>
          <xsl:otherwise>
            <xsl:value-of select="$tail-max" />
          </xsl:otherwise>
        </xsl:choose>
      </xsl:otherwise>
    </xsl:choose>
  </xsl:template>
</xsl:transform>