Handle "xidunion" instead of "union" for XID unions like DRAWABLE and FONTABLE
[free-sw/xcb/libxcb] / src / c-client.xsl
1 <?xml version="1.0" encoding="utf-8"?>
2 <!--
3 Copyright (C) 2004 Josh Triplett.  All Rights Reserved.
4
5 Permission is hereby granted, free of charge, to any person obtaining a copy
6 of this software and associated documentation files (the "Software"), to deal
7 in the Software without restriction, including without limitation the rights
8 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 copies of the Software, and to permit persons to whom the Software is
10 furnished to do so, subject to the following conditions:
11
12 The above copyright notice and this permission notice shall be included in all
13 copies or substantial portions of the Software.
14
15 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
19 ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20 WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
21
22 Except as contained in this notice, the names of the authors or their
23 institutions shall not be used in advertising or otherwise to promote the
24 sale, use or other dealings in this Software without prior written
25 authorization from the authors.
26 -->
27 <xsl:transform xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
28                version="1.0"
29                xmlns:e="http://exslt.org/common"
30                xmlns:func="http://exslt.org/functions"
31                xmlns:str="http://exslt.org/strings"
32                xmlns:xcb="http://xcb.freedesktop.org"
33                extension-element-prefixes="func str xcb">
34   
35   <xsl:output method="text" />
36
37   <xsl:strip-space elements="*" />
38
39   <!-- "header" or "source" -->
40   <xsl:param name="mode" />
41
42   <!-- Path to the core protocol descriptions. -->
43   <xsl:param name="base-path" />
44
45   <!-- Path to the extension protocol descriptions. -->
46   <xsl:param name="extension-path" select="$base-path" />
47
48   <xsl:variable name="h" select="$mode = 'header'" />
49   <xsl:variable name="c" select="$mode = 'source'" />
50   
51   <!-- String used to indent lines of code. -->
52   <xsl:variable name="indent-string" select="'    '" />
53
54   <xsl:variable name="ucase" select="'ABCDEFGHIJKLMNOPQRSTUVWXYZ'" />
55   <xsl:variable name="lcase" select="'abcdefghijklmnopqrstuvwxyz'" />
56   <xsl:variable name="letters" select="concat($ucase, $lcase)" />
57   <xsl:variable name="digits" select="'0123456789'" />
58
59   <xsl:variable name="header" select="/xcb/@header" />
60   <xsl:variable name="ucase-header"
61                 select="translate($header,$lcase,$ucase)" />
62
63   <xsl:variable name="ext" select="/xcb/@extension-name" />
64
65   <!-- Other protocol descriptions to search for types in, after checking the
66        current protocol description. -->
67   <xsl:variable name="search-path-rtf">
68     <xsl:for-each select="/xcb/import">
69       <path><xsl:value-of select="concat($extension-path, ., '.xml')" /></path>
70     </xsl:for-each>
71     <xsl:if test="not($header='xproto')">
72       <path><xsl:value-of select="concat($base-path, 'xproto.xml')" /></path>
73     </xsl:if>
74   </xsl:variable>
75   <xsl:variable name="search-path" select="e:node-set($search-path-rtf)/path"/>
76
77   <xsl:variable name="root" select="/" />
78   
79   <!-- First pass: Store everything in a variable. -->
80   <xsl:variable name="pass1-rtf">
81     <xsl:apply-templates select="/" mode="pass1" />
82   </xsl:variable>
83   <xsl:variable name="pass1" select="e:node-set($pass1-rtf)" />
84   
85   <xsl:template match="xcb" mode="pass1">
86     <xcb>
87       <xsl:copy-of select="@*" />
88       <xsl:if test="$ext">
89         <constant type="xcb_extension_t" name="{xcb:xcb-prefix()}_id">
90           <xsl:attribute name="value">{ "<xsl:value-of select="@extension-xname" />" }</xsl:attribute>
91         </constant>
92       </xsl:if>
93       <xsl:apply-templates mode="pass1" />
94     </xcb>
95   </xsl:template>
96
97   <func:function name="xcb:xcb-prefix">
98     <xsl:param name="name" />
99     <func:result>
100       <xsl:text>xcb</xsl:text>
101       <xsl:choose>
102         <xsl:when test="/xcb/@extension-name = 'RandR'">
103           <xsl:text>_randr</xsl:text>
104         </xsl:when>
105         <xsl:when test="/xcb/@extension-name = 'ScreenSaver'">
106           <xsl:text>_screensaver</xsl:text>
107         </xsl:when>
108         <xsl:when test="/xcb/@extension-name = 'XF86Dri'">
109           <xsl:text>_xf86dri</xsl:text>
110         </xsl:when>
111         <xsl:when test="/xcb/@extension-name = 'XFixes'">
112           <xsl:text>_xfixes</xsl:text>
113         </xsl:when>
114         <xsl:when test="/xcb/@extension-name = 'XvMC'">
115           <xsl:text>_xvmc</xsl:text>
116         </xsl:when>
117         <xsl:when test="/xcb/@extension-name">
118           <xsl:text>_</xsl:text>
119           <xsl:call-template name="camelcase-to-underscore">
120             <xsl:with-param name="camelcase" select="/xcb/@extension-name" />
121           </xsl:call-template>
122         </xsl:when>
123         <xsl:otherwise>
124         </xsl:otherwise>
125       </xsl:choose>
126       <xsl:if test="$name">
127         <xsl:text>_</xsl:text>
128         <xsl:call-template name="camelcase-to-underscore">
129           <xsl:with-param name="camelcase" select="$name" />
130         </xsl:call-template>
131       </xsl:if>
132     </func:result>
133   </func:function>
134
135   <func:function name="xcb:lowercase">
136     <xsl:param name="name" />
137     <func:result>
138       <xsl:call-template name="camelcase-to-underscore">
139         <xsl:with-param name="camelcase" select="$name" />
140       </xsl:call-template>
141     </func:result>
142   </func:function>
143
144   <func:function name="xcb:get-char-void">
145     <xsl:param name="name" />
146     <xsl:variable name="ctype" select="substring-before($name, '_t')" />
147     <func:result>
148       <xsl:choose>
149         <xsl:when test="$ctype = 'char' or $ctype = 'void' or $ctype = 'float' or $ctype = 'double'">
150           <xsl:value-of select="$ctype" />    
151         </xsl:when>
152         <xsl:otherwise>
153           <xsl:value-of select="$name" />
154         </xsl:otherwise>
155       </xsl:choose>
156     </func:result>
157   </func:function>
158
159   <func:function name="xcb:remove-void">
160     <xsl:param name="name" />
161     <xsl:variable name="ctype" select="substring-before($name, '_t')" />
162     <func:result>
163       <xsl:choose>
164         <xsl:when test="$ctype = 'char' or $ctype = 'void' or $ctype = 'float' or $ctype = 'double'">
165           <xsl:choose>
166             <xsl:when test="$ctype = 'void'">
167               <xsl:text>char</xsl:text>
168             </xsl:when>
169             <xsl:otherwise>
170               <xsl:value-of select="$ctype" />
171             </xsl:otherwise>
172           </xsl:choose>
173         </xsl:when>
174         <xsl:otherwise>
175           <xsl:value-of select="$name" />
176         </xsl:otherwise>
177       </xsl:choose>
178     </func:result>
179   </func:function>
180
181   <!-- split camel case into words and insert underscore -->
182   <xsl:template name="camelcase-to-underscore">
183     <xsl:param name="camelcase"/>
184     <xsl:choose>
185       <xsl:when test="$camelcase='CHAR2B' or $camelcase='INT64'
186                       or $camelcase='FLOAT32' or $camelcase='FLOAT64'
187                       or $camelcase='BOOL32' or $camelcase='STRING8'
188                       or $camelcase='Family_DECnet'">
189         <xsl:value-of select="translate($camelcase, $ucase, $lcase)"/>
190       </xsl:when>
191       <xsl:otherwise>
192         <xsl:for-each select="str:split($camelcase, '')">
193           <xsl:variable name="a" select="."/>
194           <xsl:variable name="b" select="following::*[1]"/>
195           <xsl:variable name="c" select="following::*[2]"/>
196           <xsl:value-of select="translate(., $ucase, $lcase)"/>
197           <xsl:if test="($b and contains($lcase, $a) and contains($ucase, $b))
198                         or ($b and contains($digits, $a)
199                             and contains($letters, $b))
200                         or ($b and contains($letters, $a)
201                             and contains($digits, $b))
202                         or ($c and contains($ucase, $a)
203                             and contains($ucase, $b)
204                             and contains($lcase, $c))">
205             <xsl:text>_</xsl:text>
206           </xsl:if>
207         </xsl:for-each>
208       </xsl:otherwise>
209     </xsl:choose>
210   </xsl:template>
211
212   <!-- Modify names that conflict with C++ keywords by prefixing them with an
213        underscore.  If the name parameter is not specified, it defaults to the
214        value of the name attribute on the context node. -->
215   <xsl:template name="canonical-var-name">
216     <xsl:param name="name" select="@name" />
217     <xsl:if test="$name='new' or $name='delete'
218                   or $name='class' or $name='operator'">
219       <xsl:text>_</xsl:text>
220     </xsl:if>
221     <xsl:value-of select="$name" />
222   </xsl:template>
223
224   <!-- List of core types, for use in canonical-type-name. -->
225   <xsl:variable name="core-types-rtf">
226     <type name="BOOL" newname="uint8" />
227     <type name="BYTE" newname="uint8" />
228     <type name="CARD8" newname="uint8" />
229     <type name="CARD16" newname="uint16" />
230     <type name="CARD32" newname="uint32" />
231     <type name="INT8" newname="int8" />
232     <type name="INT16" newname="int16" />
233     <type name="INT32" newname="int32" />
234
235     <type name="char" newname="char" />
236     <type name="void" newname="void" />
237     <type name="float" newname="float" />
238     <type name="double" newname="double" />
239   </xsl:variable>
240   <xsl:variable name="core-types" select="e:node-set($core-types-rtf)" />
241
242   <!--
243     Output the canonical name for a type.  This will be
244     xcb_{extension-containing-type-if-any}_type, wherever the type is found in
245     the search path, or just type if not found.  If the type parameter is not
246     specified, it defaults to the value of the type attribute on the context
247     node.
248   -->
249   <xsl:template name="canonical-type-name">
250     <xsl:param name="type" select="string(@type)" />
251
252     <xsl:variable name="is-unqualified" select="not(contains($type, ':'))"/>
253     <xsl:variable name="namespace" select="substring-before($type, ':')" />
254     <xsl:variable name="unqualified-type">
255       <xsl:choose>
256         <xsl:when test="$is-unqualified">
257           <xsl:value-of select="$type" />
258         </xsl:when>
259         <xsl:otherwise>
260           <xsl:value-of select="substring-after($type, ':')" />
261         </xsl:otherwise>
262       </xsl:choose>
263     </xsl:variable>
264
265     <xsl:choose>
266       <xsl:when test="$is-unqualified and $core-types/type[@name=$type]">
267         <xsl:value-of select="$core-types/type[@name=$type]/@newname" />
268       </xsl:when>
269       <xsl:otherwise>
270         <xsl:variable name="type-definitions"
271                       select="(/xcb|document($search-path)/xcb
272                               )[$is-unqualified or @header=$namespace]
273                                /*[((self::struct or self::union or self::enum
274                                     or self::xidtype or self::xidunion
275                                     or self::event or self::eventcopy
276                                     or self::error or self::errorcopy)
277                                    and @name=$unqualified-type)
278                                   or (self::typedef
279                                       and @newname=$unqualified-type)]" />
280         <xsl:choose>
281           <xsl:when test="count($type-definitions) = 1">
282             <xsl:for-each select="$type-definitions">
283               <xsl:value-of select="xcb:xcb-prefix($unqualified-type)" />
284             </xsl:for-each>
285           </xsl:when>
286           <xsl:when test="count($type-definitions) > 1">
287             <xsl:message terminate="yes">
288               <xsl:text>Multiple definitions of type "</xsl:text>
289               <xsl:value-of select="$type" />
290               <xsl:text>" found.</xsl:text>
291               <xsl:if test="$is-unqualified">
292                 <xsl:for-each select="$type-definitions">
293                   <xsl:text>
294     </xsl:text>
295                   <xsl:value-of select="concat(/xcb/@header, ':', $type)" />
296                 </xsl:for-each>
297               </xsl:if>
298             </xsl:message>
299           </xsl:when>
300           <xsl:otherwise>
301             <xsl:message terminate="yes">
302               <xsl:text>No definitions of type "</xsl:text>
303               <xsl:value-of select="$type" />
304               <xsl:text>" found</xsl:text>
305               <xsl:if test="$is-unqualified">
306                 <xsl:text>, and it is not a known core type</xsl:text>
307               </xsl:if>
308               <xsl:text>.</xsl:text>
309             </xsl:message>
310           </xsl:otherwise>
311         </xsl:choose>   
312       </xsl:otherwise>
313     </xsl:choose>
314   </xsl:template>
315   
316   <!-- Helper template for requests, that outputs the cookie type.  The
317        parameter "request" must be the request node, which defaults to the
318        context node. -->
319   <xsl:template name="cookie-type">
320     <xsl:param name="request" select="." />
321     <xsl:choose>
322       <xsl:when test="$request/reply">
323         <xsl:value-of select="xcb:xcb-prefix($request/@name)" />
324       </xsl:when>
325       <xsl:otherwise>
326         <xsl:text>xcb_void</xsl:text>
327       </xsl:otherwise>
328     </xsl:choose>
329     <xsl:text>_cookie_t</xsl:text>
330   </xsl:template>
331
332   <xsl:template name="request-function">
333     <xsl:param name="checked" />
334     <xsl:param name="req" />
335     <function>
336       <xsl:attribute name="name">
337         <xsl:value-of select="xcb:xcb-prefix($req/@name)" />
338         <xsl:if test="$checked='true' and not($req/reply)">_checked</xsl:if>
339         <xsl:if test="$checked='false' and $req/reply">_unchecked</xsl:if>
340       </xsl:attribute>
341       <xsl:attribute name="type">
342         <xsl:call-template name="cookie-type">
343           <xsl:with-param name="request" select="$req" />
344         </xsl:call-template>
345       </xsl:attribute>
346       <field type="xcb_connection_t *" name="c" />
347       <xsl:apply-templates select="$req/*[not(self::reply)]" mode="param" />
348       <do-request ref="{xcb:xcb-prefix($req/@name)}_request_t" opcode="{$req/@opcode}"
349                   checked="{$checked}">
350         <xsl:if test="$req/reply">
351           <xsl:attribute name="has-reply">true</xsl:attribute>
352         </xsl:if>
353       </do-request>
354     </function>
355   </xsl:template>
356   
357   <xsl:template match="request" mode="pass1">
358     <xsl:variable name="req" select="." />
359     <xsl:if test="reply">
360       <struct name="{xcb:xcb-prefix(@name)}_cookie_t">
361         <field type="unsigned int" name="sequence" />
362       </struct>
363     </xsl:if>
364     <struct name="{xcb:xcb-prefix(@name)}_request_t">
365       <field type="uint8_t" name="major_opcode" no-assign="true" />
366       <xsl:if test="$ext">
367         <field type="uint8_t" name="minor_opcode" no-assign="true" />
368       </xsl:if>
369       <xsl:apply-templates select="*[not(self::reply)]" mode="field" />
370       <middle>
371         <field type="uint16_t" name="length" no-assign="true" />
372       </middle>
373     </struct>
374     <xsl:call-template name="request-function">
375       <xsl:with-param name="checked" select="'true'" />
376       <xsl:with-param name="req" select="$req" />
377     </xsl:call-template>
378     <xsl:call-template name="request-function">
379       <xsl:with-param name="checked" select="'false'" />
380       <xsl:with-param name="req" select="$req" />
381     </xsl:call-template>
382     <xsl:if test="reply">
383       <struct name="{xcb:xcb-prefix(@name)}_reply_t">
384         <field type="uint8_t" name="response_type" />
385         <xsl:apply-templates select="reply/*" mode="field" />
386         <middle>
387           <field type="uint16_t" name="sequence" />
388           <field type="uint32_t" name="length" />
389         </middle>
390       </struct>
391       <iterator-functions ref="{xcb:xcb-prefix(@name)}" kind="_reply" />
392       <function type="{xcb:xcb-prefix(@name)}_reply_t *" name="{xcb:xcb-prefix(@name)}_reply">
393         <field type="xcb_connection_t *" name="c" />
394         <field name="cookie">
395           <xsl:attribute name="type">
396             <xsl:call-template name="cookie-type" />
397           </xsl:attribute>
398         </field>
399         <field type="xcb_generic_error_t **" name="e" />
400         <l>return (<xsl:value-of select="xcb:xcb-prefix(@name)" />_reply_t *)<!--
401         --> xcb_wait_for_reply(c, cookie.sequence, e);</l>
402       </function>
403     </xsl:if>
404   </xsl:template>
405
406   <xsl:template match="xidtype|xidunion" mode="pass1">
407     <typedef oldname="uint32_t" newname="{xcb:xcb-prefix(@name)}_t" />
408     <iterator ref="{xcb:xcb-prefix(@name)}" />
409     <iterator-functions ref="{xcb:xcb-prefix(@name)}" />
410   </xsl:template>
411
412   <xsl:template match="struct|union" mode="pass1">
413     <struct name="{xcb:xcb-prefix(@name)}_t">
414       <xsl:if test="self::union">
415         <xsl:attribute name="kind">union</xsl:attribute>
416       </xsl:if>
417       <xsl:apply-templates select="*" mode="field" />
418     </struct>
419     <iterator ref="{xcb:xcb-prefix(@name)}" />
420     <iterator-functions ref="{xcb:xcb-prefix(@name)}" />
421   </xsl:template>
422
423   <xsl:template match="event|eventcopy|error|errorcopy" mode="pass1">
424     <xsl:variable name="suffix">
425       <xsl:choose>
426         <xsl:when test="self::event|self::eventcopy">
427           <xsl:text>_event_t</xsl:text>
428         </xsl:when>
429         <xsl:when test="self::error|self::errorcopy">
430           <xsl:text>_error_t</xsl:text>
431         </xsl:when>
432       </xsl:choose>
433     </xsl:variable>
434     <constant type="number" name="{xcb:xcb-prefix(@name)}" value="{@number}" />
435     <xsl:choose>
436       <xsl:when test="self::event|self::error">
437         <struct name="{xcb:xcb-prefix(@name)}{$suffix}">
438           <field type="uint8_t" name="response_type" />
439           <xsl:if test="self::error">
440             <field type="uint8_t" name="error_code" />
441           </xsl:if>
442           <xsl:apply-templates select="*" mode="field" />
443           <xsl:if test="not(self::event and boolean(@no-sequence-number))">
444             <middle>
445               <field type="uint16_t" name="sequence" />
446             </middle>
447           </xsl:if>
448         </struct>
449       </xsl:when>
450       <xsl:when test="self::eventcopy|self::errorcopy">
451         <typedef newname="{xcb:xcb-prefix(@name)}{$suffix}">
452           <xsl:attribute name="oldname">
453             <xsl:call-template name="canonical-type-name">
454               <xsl:with-param name="type" select="@ref" />
455             </xsl:call-template>
456             <xsl:value-of select="$suffix" />
457           </xsl:attribute>
458         </typedef>
459       </xsl:when>
460     </xsl:choose>
461   </xsl:template>
462
463   <xsl:template match="typedef" mode="pass1">
464     <typedef>
465       <xsl:attribute name="oldname">
466         <xsl:call-template name="canonical-type-name">
467           <xsl:with-param name="type" select="@oldname" />
468         </xsl:call-template>
469         <xsl:text>_t</xsl:text>
470       </xsl:attribute>
471       <xsl:attribute name="newname">
472         <xsl:call-template name="canonical-type-name">
473           <xsl:with-param name="type" select="@newname" />
474         </xsl:call-template>
475         <xsl:text>_t</xsl:text>
476       </xsl:attribute>
477     </typedef>
478     <iterator ref="{xcb:xcb-prefix(@newname)}" />
479     <iterator-functions ref="{xcb:xcb-prefix(@newname)}" />
480   </xsl:template>
481
482   <xsl:template match="enum" mode="pass1">
483     <enum name="{xcb:xcb-prefix(@name)}_t">
484       <xsl:for-each select="item">
485         <item name="{translate(xcb:xcb-prefix(concat(../@name, concat('_', @name))), $lcase, $ucase)}">
486           <xsl:copy-of select="*" />
487         </item>
488       </xsl:for-each>
489     </enum>
490   </xsl:template>
491
492   <!--
493     Templates for processing fields.
494   -->
495
496   <xsl:template match="pad" mode="field">
497     <xsl:copy-of select="." />
498   </xsl:template>
499   
500   <xsl:template match="field|exprfield" mode="field">
501     <xsl:copy>
502       <xsl:attribute name="type">
503         <xsl:call-template name="canonical-type-name" />
504         <xsl:text>_t</xsl:text>
505       </xsl:attribute>
506       <xsl:attribute name="name">
507         <xsl:call-template name="canonical-var-name" />
508       </xsl:attribute>
509       <xsl:copy-of select="*" />
510     </xsl:copy>
511   </xsl:template>
512
513   <xsl:template match="list" mode="field">
514     <xsl:variable name="type"><!--
515       --><xsl:call-template name="canonical-type-name" />
516         <xsl:text>_t</xsl:text><!--
517     --></xsl:variable>
518     <list type="{$type}">
519       <xsl:attribute name="name">
520         <xsl:call-template name="canonical-var-name" />
521       </xsl:attribute>
522       <xsl:if test="not(parent::request) and node()
523                     and not(.//*[not(self::value or self::op)])">
524         <xsl:attribute name="fixed">true</xsl:attribute>
525       </xsl:if>
526       <!-- Handle lists with no length expressions. -->
527       <xsl:if test="not(node())">
528         <xsl:choose>
529           <!-- In a request, refer to an implicit localparam for length. -->
530           <xsl:when test="parent::request">
531             <fieldref>
532               <xsl:value-of select="concat(@name, '_len')" />
533             </fieldref>
534           </xsl:when>
535           <!-- In a reply, use the length of the reply to determine the length
536                of the list. -->
537           <xsl:when test="parent::reply">
538             <op op="/">
539               <op op="&lt;&lt;">
540                 <fieldref>length</fieldref>
541                 <value>2</value>
542               </op>
543               <function-call name="sizeof">
544                 <param><xsl:value-of select="$type" /></param>
545               </function-call>
546             </op>
547           </xsl:when>
548           <!-- Other cases generate an error. -->
549           <xsl:otherwise>
550             <xsl:message terminate="yes"><!--
551               -->Encountered a list with no length expresssion outside a<!--
552               --> request or reply.<!--
553             --></xsl:message>
554           </xsl:otherwise>
555         </xsl:choose>
556       </xsl:if>
557       <xsl:copy-of select="*" />
558     </list>
559   </xsl:template>
560
561   <xsl:template match="valueparam" mode="field">
562     <field>
563       <xsl:attribute name="type">
564         <xsl:call-template name="canonical-type-name">
565           <xsl:with-param name="type" select="@value-mask-type" />
566         </xsl:call-template>
567         <xsl:text>_t</xsl:text>
568       </xsl:attribute>
569       <xsl:attribute name="name">
570         <xsl:call-template name="canonical-var-name">
571           <xsl:with-param name="name" select="@value-mask-name" />
572         </xsl:call-template>
573       </xsl:attribute>
574     </field>
575     <list type="uint32_t">
576       <xsl:attribute name="name">
577         <xsl:call-template name="canonical-var-name">
578           <xsl:with-param name="name" select="@value-list-name" />
579         </xsl:call-template>
580       </xsl:attribute>
581       <function-call name="xcb_popcount">
582         <param>
583           <fieldref>
584             <xsl:call-template name="canonical-var-name">
585               <xsl:with-param name="name" select="@value-mask-name" />
586             </xsl:call-template>
587           </fieldref>
588         </param>
589       </function-call>
590     </list>
591   </xsl:template>
592
593   <xsl:template match="field|localfield" mode="param">
594     <field>
595       <xsl:attribute name="type">
596         <xsl:call-template name="canonical-type-name" />
597         <xsl:text>_t</xsl:text>
598       </xsl:attribute>
599       <xsl:attribute name="name">
600         <xsl:call-template name="canonical-var-name" />
601       </xsl:attribute>
602     </field>
603   </xsl:template>
604
605   <xsl:template match="list" mode="param">
606     <!-- If no length expression is provided, use a CARD32 localfield. -->
607     <xsl:if test="not(node())">
608       <field type="uint32_t" name="{@name}_len" />
609     </xsl:if>
610     <field>
611       <xsl:variable name="ctype">
612         <xsl:call-template name="canonical-type-name" />
613       </xsl:variable>
614       <xsl:attribute name="type">
615         <xsl:text>const </xsl:text>
616         <xsl:call-template name="canonical-type-name" />
617         <xsl:if test="not($ctype='char') and not($ctype='void')">
618           <xsl:text>_t</xsl:text>
619         </xsl:if>
620         <xsl:text> *</xsl:text>
621       </xsl:attribute>
622       <xsl:attribute name="name">
623         <xsl:call-template name="canonical-var-name" />
624       </xsl:attribute>
625     </field>
626   </xsl:template>
627
628   <xsl:template match="valueparam" mode="param">
629     <field>
630       <xsl:attribute name="type">
631         <xsl:call-template name="canonical-type-name">
632           <xsl:with-param name="type" select="@value-mask-type" />
633         </xsl:call-template>
634         <xsl:text>_t</xsl:text>
635       </xsl:attribute>
636       <xsl:attribute name="name">
637         <xsl:call-template name="canonical-var-name">
638           <xsl:with-param name="name" select="@value-mask-name" />
639         </xsl:call-template>
640       </xsl:attribute>
641     </field>
642     <field type="const uint32_t *">
643       <xsl:attribute name="name">
644         <xsl:call-template name="canonical-var-name">
645           <xsl:with-param name="name" select="@value-list-name" />
646         </xsl:call-template>
647       </xsl:attribute>
648     </field>
649   </xsl:template>
650
651   <!-- Second pass: Process the variable. -->
652   <xsl:variable name="result-rtf">
653     <xsl:apply-templates select="$pass1/*" mode="pass2" />
654   </xsl:variable>
655   <xsl:variable name="result" select="e:node-set($result-rtf)" />
656
657   <xsl:template match="xcb" mode="pass2">
658     <xcb>
659       <xsl:copy-of select="@*" />
660       <xsl:apply-templates mode="pass2"
661                            select="constant|enum|struct|typedef|iterator" />
662       <xsl:apply-templates mode="pass2"
663                            select="function|iterator-functions" />
664     </xcb>
665   </xsl:template>
666
667   <!-- Generic rules for nodes that don't need further processing: copy node
668        with attributes, and recursively process the child nodes. -->
669   <xsl:template match="*" mode="pass2">
670     <xsl:copy>
671       <xsl:copy-of select="@*" />
672       <xsl:apply-templates mode="pass2" />
673     </xsl:copy>
674   </xsl:template>
675
676   <xsl:template match="struct" mode="pass2">
677     <xsl:if test="@kind='union' and list[not(@fixed)]">
678       <xsl:message terminate="yes">Unions must be fixed length.</xsl:message>
679     </xsl:if>
680     <struct name="{@name}">
681       <xsl:if test="@kind">
682         <xsl:attribute name="kind">
683           <xsl:value-of select="@kind" />
684         </xsl:attribute>
685       </xsl:if>
686       <!-- FIXME: This should go by size, not number of fields. -->
687       <xsl:copy-of select="node()[not(self::middle)
688                    and position() &lt; 3]" />
689       <xsl:if test="middle and (count(*[not(self::middle)]) &lt; 2)">
690         <pad bytes="{2 - count(*[not(self::middle)])}" />
691       </xsl:if>
692       <xsl:copy-of select="middle/*" />
693       <xsl:copy-of select="node()[not(self::middle) and (position() > 2)]" />
694     </struct>
695   </xsl:template>
696
697   <xsl:template match="do-request" mode="pass2">
698     <xsl:variable name="struct"
699                   select="$pass1/xcb/struct[@name=current()/@ref]" />
700
701     <xsl:variable name="num-parts" select="(1+count($struct/list))*2" />
702
703     <l>static const xcb_protocol_request_t xcb_req = {</l>
704     <indent>
705       <l>/* count */ <xsl:value-of select="$num-parts" />,</l>
706       <l>/* ext */ <xsl:choose>
707                      <xsl:when test="$ext">
708                        <xsl:text>&amp;</xsl:text>
709                        <xsl:value-of select="xcb:xcb-prefix()" />
710                        <xsl:text>_id</xsl:text>
711                      </xsl:when>
712                      <xsl:otherwise>0</xsl:otherwise>
713                    </xsl:choose>,</l>
714       <l>/* opcode */ <xsl:value-of select="@opcode" />,</l>
715       <l>/* isvoid */ <xsl:value-of select="1-boolean(@has-reply)" /></l>
716     </indent>
717     <l>};</l>
718
719     <l />
720     <l>struct iovec xcb_parts[<xsl:value-of select="$num-parts+2" />];</l>
721     <l><xsl:value-of select="../@type" /> xcb_ret;</l>
722     <l><xsl:value-of select="@ref" /> xcb_out;</l>
723
724     <l />
725     <xsl:apply-templates select="$struct//*[(self::field or self::exprfield)
726                                             and not(boolean(@no-assign))]"
727                          mode="assign" />
728
729     <l />
730     <l>xcb_parts[2].iov_base = (char *) &amp;xcb_out;</l>
731     <l>xcb_parts[2].iov_len = sizeof(xcb_out);</l>
732     <l>xcb_parts[3].iov_base = 0;</l>
733     <l>xcb_parts[3].iov_len = -xcb_parts[2].iov_len &amp; 3;</l>
734
735     <xsl:for-each select="$struct/list">
736       <l>xcb_parts[<xsl:value-of select="2 + position() * 2"/>].iov_base = (char *) <!--
737       --><xsl:value-of select="@name" />;</l>
738       <l>xcb_parts[<xsl:value-of select="2 + position() * 2"/>].iov_len = <!--
739       --><xsl:apply-templates mode="output-expression" />
740       <xsl:if test="not(@type = 'void_t')">
741         <xsl:text> * sizeof(</xsl:text>
742           <xsl:choose>
743           <xsl:when test="@type='char_t'">
744             <xsl:text>char</xsl:text>
745           </xsl:when>
746           <xsl:otherwise>
747             <xsl:value-of select="@type" />
748           </xsl:otherwise>
749         </xsl:choose>
750         <xsl:text>)</xsl:text>
751       </xsl:if>;</l>
752       <l>xcb_parts[<xsl:value-of select="3 + position() * 2"/>].iov_base = 0;</l>
753       <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>
754     </xsl:for-each>
755
756     <l>xcb_ret.sequence = xcb_send_request(c, <!--
757     --><xsl:choose>
758          <xsl:when test="@checked='true'">XCB_REQUEST_CHECKED</xsl:when>
759          <xsl:otherwise>0</xsl:otherwise>
760        </xsl:choose>, xcb_parts + 2, &amp;xcb_req);</l>
761     <l>return xcb_ret;</l>
762   </xsl:template>
763
764   <xsl:template match="field" mode="assign">
765     <l>
766       <xsl:text>xcb_out.</xsl:text>
767       <xsl:value-of select="@name" />
768       <xsl:text> = </xsl:text>
769       <xsl:value-of select="@name" />
770       <xsl:text>;</xsl:text>
771     </l>
772   </xsl:template>
773
774   <xsl:template match="exprfield" mode="assign">
775     <l>
776       <xsl:text>xcb_out.</xsl:text>
777       <xsl:value-of select="@name" />
778       <xsl:text> = </xsl:text>
779       <xsl:apply-templates mode="output-expression" />
780       <xsl:text>;</xsl:text>
781     </l>
782   </xsl:template>
783
784   <xsl:template match="iterator" mode="pass2">
785     <struct name="{@ref}_iterator_t">
786       <field type="{@ref}_t *" name="data" />
787       <field type="int" name="rem" />
788       <field type="int" name="index" />
789     </struct>
790   </xsl:template>
791
792   <xsl:template match="iterator-functions" mode="pass2">
793     <xsl:variable name="ref" select="@ref" />
794     <xsl:variable name="kind" select="@kind" />
795     <xsl:variable name="struct"
796                   select="$pass1/xcb/struct[@name=concat($ref, $kind, '_t')]" />
797     <xsl:variable name="nextfields-rtf">
798       <nextfield>R + 1</nextfield>
799       <xsl:for-each select="$struct/list[not(@fixed)]">
800         <xsl:choose>
801           <xsl:when test="substring(@type, 1, 3) = 'xcb'">
802             <nextfield><xsl:value-of select="substring(@type, 1, string-length(@type)-2)" />_end(<!--
803             --><xsl:value-of select="$ref" />_<!--
804             --><xsl:value-of select="string(@name)" />_iterator(R))</nextfield>
805           </xsl:when>
806           <xsl:otherwise>
807             <nextfield><xsl:value-of select="$ref" />_<!--
808             --><xsl:value-of select="string(@name)" />_end(R)</nextfield>
809           </xsl:otherwise>
810         </xsl:choose>
811       </xsl:for-each>
812     </xsl:variable>
813     <xsl:variable name="nextfields" select="e:node-set($nextfields-rtf)" />
814     <xsl:for-each select="$struct/list[not(@fixed)]">
815       <xsl:variable name="number"
816                     select="1+count(preceding-sibling::list[not(@fixed)])" />
817       <xsl:variable name="nextfield" select="$nextfields/nextfield[$number]" />
818       <xsl:variable name="is-first"
819                     select="not(preceding-sibling::list[not(@fixed)])" />
820       <xsl:variable name="field-name" select="@name" />
821       <xsl:variable name="is-variable"
822                     select="$pass1/xcb/struct[@name=current()/@type]/list
823                             or document($search-path)/xcb
824                                /struct[concat(xcb:xcb-prefix(@name), '_t')
825                                        = current()/@type]
826                                /*[self::valueparam
827                                   or self::list[.//*[not(self::value
828                                                          or self::op)]]]" />
829       <xsl:if test="not($is-variable)">
830         <function type="{xcb:get-char-void(@type)} *" name="{$ref}_{xcb:lowercase($field-name)}">
831           <field type="const {$ref}{$kind}_t *" name="R" />
832           <xsl:choose>
833             <xsl:when test="$is-first">
834               <l>return (<xsl:value-of select="xcb:get-char-void(@type)" /> *) <!--
835               -->(<xsl:value-of select="$nextfield" />);</l>
836             </xsl:when>
837             <xsl:otherwise>
838               <l>xcb_generic_iterator_t prev = <!--
839               --><xsl:value-of select="$nextfield" />;</l>
840               <l>return (<xsl:value-of select="xcb:get-char-void(@type)" /> *) <!--
841               -->((char *) prev.data + XCB_TYPE_PAD(<!--
842               --><xsl:value-of select="xcb:get-char-void(@type)" />, prev.index));</l>
843             </xsl:otherwise>
844           </xsl:choose>
845         </function>
846       </xsl:if>
847       <function type="int" name="{$ref}_{xcb:lowercase($field-name)}_length">
848         <field type="const {$ref}{$kind}_t *" name="R" />
849         <l>return <xsl:apply-templates mode="output-expression">
850                     <xsl:with-param name="field-prefix" select="'R->'" />
851                   </xsl:apply-templates>;</l>
852       </function>
853       <xsl:choose>
854         <xsl:when test="substring(@type, 1, 3) = 'xcb'">
855           <function type="{substring(@type, 1, string-length(@type)-2)}_iterator_t" name="{$ref}_{xcb:lowercase($field-name)}_iterator">
856             <field type="const {$ref}{$kind}_t *" name="R" />
857             <l><xsl:value-of select="substring(@type, 1, string-length(@type)-2)" />_iterator_t i;</l>
858             <xsl:choose>
859               <xsl:when test="$is-first">
860                 <l>i.data = (<xsl:value-of select="@type" /> *) <!--
861                 -->(<xsl:value-of select="$nextfield" />);</l>
862               </xsl:when>
863               <xsl:otherwise>
864                 <l>xcb_generic_iterator_t prev = <!--
865                 --><xsl:value-of select="$nextfield" />;</l>
866                 <l>i.data = (<xsl:value-of select="@type" /> *) <!--
867                 -->((char *) prev.data + XCB_TYPE_PAD(<!--
868                 --><xsl:value-of select="@type" />, prev.index));</l>
869               </xsl:otherwise>
870             </xsl:choose>
871             <l>i.rem = <xsl:apply-templates mode="output-expression">
872                          <xsl:with-param name="field-prefix" select="'R->'" />
873                        </xsl:apply-templates>;</l>
874             <l>i.index = (char *) i.data - (char *) R;</l>
875             <l>return i;</l>
876           </function>
877         </xsl:when>
878         <xsl:otherwise>
879           <xsl:variable name="cast">
880             <xsl:choose>
881               <xsl:when test="@type='void'">char</xsl:when>
882               <xsl:otherwise><xsl:value-of select="@type" /></xsl:otherwise>
883             </xsl:choose>
884           </xsl:variable>
885           <function type="xcb_generic_iterator_t" name="{$ref}_{xcb:lowercase($field-name)}_end">
886             <field type="const {$ref}{$kind}_t *" name="R" />
887             <l>xcb_generic_iterator_t i;</l>
888             <xsl:choose>
889               <xsl:when test="$is-first">
890                 <l>i.data = ((<xsl:value-of select="xcb:remove-void($cast)" /> *) <!--
891                 -->(<xsl:value-of select="$nextfield" />)) + (<!--
892                 --><xsl:apply-templates mode="output-expression">
893                      <xsl:with-param name="field-prefix" select="'R->'" />
894                    </xsl:apply-templates>);</l>
895               </xsl:when>
896               <xsl:otherwise>
897                 <l>xcb_generic_iterator_t child = <!--
898                 --><xsl:value-of select="$nextfield" />;</l>
899                 <l>i.data = ((<xsl:value-of select="xcb:get-char-void($cast)" /> *) <!--
900                 -->child.data) + (<!--
901                 --><xsl:apply-templates mode="output-expression">
902                      <xsl:with-param name="field-prefix" select="'R->'" />
903                    </xsl:apply-templates>);</l>
904               </xsl:otherwise>
905             </xsl:choose>
906             <l>i.rem = 0;</l>
907             <l>i.index = (char *) i.data - (char *) R;</l>
908             <l>return i;</l>
909           </function>
910         </xsl:otherwise>
911       </xsl:choose>
912     </xsl:for-each>
913     <xsl:if test="not($kind)">
914       <function type="void" name="{$ref}_next">
915         <field type="{$ref}_iterator_t *" name="i" />
916         <xsl:choose>
917           <xsl:when test="$struct/list[not(@fixed)]">
918             <l><xsl:value-of select="$ref" />_t *R = i->data;</l>
919             <l>xcb_generic_iterator_t child = <!--
920             --><xsl:value-of select="$nextfields/nextfield[last()]" />;</l>
921             <l>--i->rem;</l>
922             <l>i->data = (<xsl:value-of select="$ref" />_t *) child.data;</l>
923             <l>i->index = child.index;</l>
924           </xsl:when>
925           <xsl:otherwise>
926             <l>--i->rem;</l>
927             <l>++i->data;</l>
928             <l>i->index += sizeof(<xsl:value-of select="$ref" />_t);</l>
929           </xsl:otherwise>
930         </xsl:choose>
931       </function>
932       <function type="xcb_generic_iterator_t" name="{$ref}_end">
933         <field type="{$ref}_iterator_t" name="i" />
934         <l>xcb_generic_iterator_t ret;</l>
935         <xsl:choose>
936           <xsl:when test="$struct/list[not(@fixed)]">
937             <l>while(i.rem > 0)</l>
938             <indent>
939               <l><xsl:value-of select="$ref" />_next(&amp;i);</l>
940             </indent>
941             <l>ret.data = i.data;</l>
942             <l>ret.rem = i.rem;</l>
943             <l>ret.index = i.index;</l>
944           </xsl:when>
945           <xsl:otherwise>
946             <l>ret.data = i.data + i.rem;</l>
947             <l>ret.index = i.index + ((char *) ret.data - (char *) i.data);</l>
948             <l>ret.rem = 0;</l>
949           </xsl:otherwise>
950         </xsl:choose>
951         <l>return ret;</l>
952       </function>
953     </xsl:if>
954   </xsl:template>
955
956   <!-- Output the results. -->
957   <xsl:template match="/">
958     <xsl:if test="not(function-available('e:node-set'))">
959       <xsl:message terminate="yes"><!--
960         -->Error: This stylesheet requires the EXSL node-set extension.<!--
961       --></xsl:message>
962     </xsl:if>
963
964     <xsl:if test="not($h) and not($c)">
965       <xsl:message terminate="yes"><!--
966         -->Error: Parameter "mode" must be "header" or "source".<!--
967       --></xsl:message>
968     </xsl:if>
969
970     <xsl:apply-templates select="$result/*" mode="output" />
971   </xsl:template>
972
973   <xsl:template match="xcb" mode="output">
974     <xsl:variable name="guard"><!--
975       -->__<xsl:value-of select="$ucase-header" />_H<!--
976     --></xsl:variable>
977
978 <xsl:text>/*
979  * This file generated automatically from </xsl:text>
980 <xsl:value-of select="$header" /><xsl:text>.xml by c-client.xsl using XSLT.
981  * Edit at your peril.
982  */
983 </xsl:text>
984
985 <xsl:if test="$h"><xsl:text>
986 #ifndef </xsl:text><xsl:value-of select="$guard" /><xsl:text>
987 #define </xsl:text><xsl:value-of select="$guard" /><xsl:text>
988 </xsl:text>
989 #include "xcb.h"
990 <xsl:for-each select="$root/xcb/import">
991 <xsl:text>#include "</xsl:text><xsl:value-of select="." /><xsl:text>.h"
992 </xsl:text>
993 </xsl:for-each>
994 <xsl:text>
995 </xsl:text>
996 </xsl:if>
997
998 <xsl:if test="$c"><xsl:text>
999 #include &lt;assert.h&gt;
1000 #include "xcbext.h"
1001 #include "</xsl:text><xsl:value-of select="$header" /><xsl:text>.h"
1002
1003 </xsl:text></xsl:if>
1004
1005     <xsl:apply-templates mode="output" />
1006
1007 <xsl:if test="$h">
1008 <xsl:text>
1009 #endif
1010 </xsl:text>
1011 </xsl:if>
1012   </xsl:template>
1013
1014   <xsl:template match="constant" mode="output">
1015     <xsl:choose>
1016       <xsl:when test="@type = 'number'">
1017         <xsl:if test="$h">
1018           <xsl:text>#define </xsl:text>
1019           <xsl:value-of select="translate(@name, $lcase, $ucase)" />
1020           <xsl:text> </xsl:text>
1021           <xsl:value-of select="@value" />
1022           <xsl:text>
1023
1024 </xsl:text>
1025         </xsl:if>
1026       </xsl:when>
1027       <xsl:when test="@type = 'string'">
1028         <xsl:if test="$h">
1029           <xsl:text>extern </xsl:text>
1030         </xsl:if>
1031         <xsl:text>const char </xsl:text>
1032         <xsl:value-of select="@name" />
1033         <xsl:text>[]</xsl:text>
1034         <xsl:if test="$c">
1035           <xsl:text> = "</xsl:text>
1036           <xsl:value-of select="@value" />
1037           <xsl:text>"</xsl:text>
1038         </xsl:if>
1039         <xsl:text>;
1040
1041 </xsl:text>
1042       </xsl:when>
1043       <xsl:otherwise>
1044         <xsl:if test="$h">
1045           <xsl:text>extern </xsl:text>
1046         </xsl:if>
1047         <xsl:call-template name="type-and-name" />
1048         <xsl:if test="$c">
1049           <xsl:text> = </xsl:text>
1050           <xsl:value-of select="@value" />
1051         </xsl:if>
1052         <xsl:text>;
1053
1054 </xsl:text>
1055       </xsl:otherwise>
1056     </xsl:choose>
1057   </xsl:template>
1058
1059   <xsl:template match="typedef" mode="output">
1060     <xsl:if test="$h">
1061       <xsl:text>typedef </xsl:text>
1062       <xsl:value-of select="xcb:get-char-void(@oldname)" />
1063       <xsl:text> </xsl:text>
1064       <xsl:value-of select="@newname" />
1065       <xsl:text>;
1066
1067 </xsl:text>
1068     </xsl:if>
1069   </xsl:template>
1070
1071   <xsl:template match="struct" mode="output">
1072     <xsl:if test="$h">
1073       <xsl:variable name="type-lengths">
1074         <xsl:call-template name="type-lengths">
1075           <xsl:with-param name="items" select="field/@type" />
1076         </xsl:call-template>
1077       </xsl:variable>
1078       <xsl:text>typedef </xsl:text>
1079       <xsl:if test="not(@kind)">struct</xsl:if><xsl:value-of select="@kind" />
1080       <xsl:text> {
1081 </xsl:text>
1082       <xsl:for-each select="exprfield|field|list[@fixed]|pad">
1083         <xsl:text>    </xsl:text>
1084         <xsl:apply-templates select=".">
1085           <xsl:with-param name="type-lengths" select="$type-lengths" />
1086         </xsl:apply-templates>
1087         <xsl:text>;
1088 </xsl:text>
1089       </xsl:for-each>
1090       <xsl:text>} </xsl:text>
1091       <xsl:value-of select="@name" />
1092       <xsl:text>;
1093
1094 </xsl:text>
1095     </xsl:if>
1096   </xsl:template>
1097
1098   <xsl:template match="enum" mode="output">
1099     <xsl:if test="$h">
1100       <xsl:text>typedef enum {
1101     </xsl:text>
1102       <xsl:call-template name="list">
1103         <xsl:with-param name="separator"><xsl:text>,
1104     </xsl:text></xsl:with-param>
1105         <xsl:with-param name="items">
1106           <xsl:for-each select="item">
1107             <item>
1108               <xsl:value-of select="@name" />
1109               <xsl:if test="node()"> <!-- If there is an expression -->
1110                 <xsl:text> = </xsl:text>
1111                 <xsl:apply-templates mode="output-expression" />
1112               </xsl:if>
1113             </item>
1114           </xsl:for-each>
1115         </xsl:with-param>
1116       </xsl:call-template>
1117       <xsl:text>
1118 } </xsl:text><xsl:value-of select="@name" /><xsl:text>;
1119
1120 </xsl:text>
1121     </xsl:if>
1122   </xsl:template>
1123
1124   <xsl:template match="function" mode="output">
1125     <xsl:variable name="decl-open" select="concat(@name, ' (')" />
1126     <xsl:variable name="type-lengths">
1127       <xsl:call-template name="type-lengths">
1128         <xsl:with-param name="items" select="field/@type" />
1129       </xsl:call-template>
1130     </xsl:variable>
1131     <xsl:value-of select="@type" />
1132     <xsl:text>
1133 </xsl:text>
1134     <xsl:value-of select="$decl-open" />
1135     <xsl:call-template name="list">
1136       <xsl:with-param name="separator">
1137         <xsl:text>,
1138 </xsl:text>
1139         <xsl:call-template name="repeat">
1140           <xsl:with-param name="count" select="string-length($decl-open)" />
1141         </xsl:call-template>
1142       </xsl:with-param>
1143       <xsl:with-param name="items">
1144         <xsl:for-each select="field">
1145           <item>
1146             <xsl:apply-templates select=".">
1147               <xsl:with-param name="type-lengths" select="$type-lengths" />
1148             </xsl:apply-templates>
1149           </item>
1150         </xsl:for-each>
1151       </xsl:with-param>
1152     </xsl:call-template>
1153     <xsl:text>)</xsl:text>
1154
1155     <xsl:if test="$h"><xsl:text>;
1156
1157 </xsl:text></xsl:if>
1158
1159     <xsl:if test="$c">
1160       <xsl:text>
1161 {
1162 </xsl:text>
1163       <xsl:apply-templates select="l|indent" mode="function-body">
1164         <xsl:with-param name="indent" select="$indent-string" />
1165       </xsl:apply-templates>
1166       <xsl:text>}
1167
1168 </xsl:text>
1169     </xsl:if>
1170   </xsl:template>
1171
1172   <xsl:template match="l" mode="function-body">
1173     <xsl:param name="indent" />
1174     <xsl:value-of select="concat($indent, .)" /><xsl:text>
1175 </xsl:text>
1176   </xsl:template>
1177
1178   <xsl:template match="indent" mode="function-body">
1179     <xsl:param name="indent" />
1180     <xsl:apply-templates select="l|indent" mode="function-body">
1181       <xsl:with-param name="indent" select="concat($indent, $indent-string)" />
1182     </xsl:apply-templates>
1183   </xsl:template>
1184
1185   <xsl:template match="value" mode="output-expression">
1186     <xsl:value-of select="." />
1187   </xsl:template>
1188
1189   <xsl:template match="fieldref" mode="output-expression">
1190     <xsl:param name="field-prefix" />
1191     <xsl:value-of select="concat($field-prefix, .)" />
1192   </xsl:template>
1193
1194   <xsl:template match="op" mode="output-expression">
1195     <xsl:param name="field-prefix" />
1196     <xsl:text>(</xsl:text>
1197     <xsl:apply-templates select="node()[1]" mode="output-expression">
1198       <xsl:with-param name="field-prefix" select="$field-prefix" />
1199     </xsl:apply-templates>
1200     <xsl:text> </xsl:text>
1201     <xsl:value-of select="@op" />
1202     <xsl:text> </xsl:text>
1203     <xsl:apply-templates select="node()[2]" mode="output-expression">
1204       <xsl:with-param name="field-prefix" select="$field-prefix" />
1205     </xsl:apply-templates>
1206     <xsl:text>)</xsl:text>
1207   </xsl:template>
1208
1209   <xsl:template match="bit" mode="output-expression">
1210     <xsl:text>(1 &lt;&lt; </xsl:text>
1211     <xsl:value-of select="." />
1212     <xsl:text>)</xsl:text>
1213   </xsl:template>
1214
1215   <xsl:template match="function-call" mode="output-expression">
1216     <xsl:param name="field-prefix" />
1217     <xsl:value-of select="@name" />
1218     <xsl:text>(</xsl:text>
1219     <xsl:call-template name="list">
1220       <xsl:with-param name="separator" select="', '" />
1221       <xsl:with-param name="items">
1222         <xsl:for-each select="param">
1223           <item><xsl:apply-templates mode="output-expression">
1224             <xsl:with-param name="field-prefix" select="$field-prefix" />
1225           </xsl:apply-templates></item>
1226         </xsl:for-each>
1227       </xsl:with-param>
1228     </xsl:call-template>
1229     <xsl:text>)</xsl:text>
1230   </xsl:template>
1231
1232   <!-- Catch invalid elements in expressions. -->
1233   <xsl:template match="*" mode="output-expression">
1234     <xsl:message terminate="yes">
1235       <xsl:text>Invalid element in expression: </xsl:text>
1236       <xsl:value-of select="name()" />
1237     </xsl:message>
1238   </xsl:template>
1239
1240   <xsl:template match="field|exprfield">
1241     <xsl:param name="type-lengths" select="0" />
1242     <xsl:call-template name="type-and-name">
1243       <xsl:with-param name="type-lengths" select="$type-lengths" />
1244     </xsl:call-template>
1245   </xsl:template>
1246
1247   <xsl:template match="list[@fixed]">
1248     <xsl:param name="type-lengths" select="0" />
1249     <xsl:call-template name="type-and-name">
1250       <xsl:with-param name="type-lengths" select="$type-lengths" />
1251     </xsl:call-template>
1252     <xsl:text>[</xsl:text>
1253     <xsl:apply-templates mode="output-expression" />
1254     <xsl:text>]</xsl:text>
1255   </xsl:template>
1256
1257   <xsl:template match="pad">
1258     <xsl:param name="type-lengths" select="0" />
1259
1260     <xsl:variable name="padnum"><xsl:number /></xsl:variable>
1261
1262     <xsl:call-template name="type-and-name">
1263       <xsl:with-param name="type" select="'uint8_t'" />
1264       <xsl:with-param name="name">
1265         <xsl:text>pad</xsl:text>
1266         <xsl:value-of select="$padnum - 1" />
1267       </xsl:with-param>
1268       <xsl:with-param name="type-lengths" select="$type-lengths" />
1269     </xsl:call-template>
1270     <xsl:if test="@bytes > 1">
1271       <xsl:text>[</xsl:text>
1272       <xsl:value-of select="@bytes" />
1273       <xsl:text>]</xsl:text>
1274     </xsl:if>
1275   </xsl:template>
1276
1277   <!-- Output the given type and name (defaulting to the corresponding
1278        attributes of the context node), with the appropriate spacing.  The
1279        type must consist of a base type (which may contain spaces), then
1280        optionally a single space and a suffix of one or more '*' characters.
1281        If the type-lengths parameter is provided, use it to line up the base
1282        types and suffixs of the type declarations. -->
1283   <xsl:template name="type-and-name">
1284     <xsl:param name="type" select="@type" />
1285     <xsl:param name="name" select="@name" />
1286     <xsl:param name="type-lengths">
1287       <max-type-length>0</max-type-length>
1288       <max-suffix-length>0</max-suffix-length>
1289     </xsl:param>
1290     
1291     <xsl:variable name="type-lengths-ns" select="e:node-set($type-lengths)" />
1292     <xsl:variable name="min-type-length"
1293                   select="$type-lengths-ns/max-type-length" />
1294     <xsl:variable name="min-suffix-length"
1295                   select="$type-lengths-ns/max-suffix-length" />
1296
1297     <xsl:variable name="base-type">
1298       <xsl:choose>
1299         <xsl:when test="contains($type, ' *')">
1300           <xsl:value-of select="substring-before($type, ' *')" />
1301         </xsl:when>
1302         <xsl:otherwise>
1303           <xsl:value-of select="$type" />
1304         </xsl:otherwise>
1305       </xsl:choose>
1306     </xsl:variable>
1307     <xsl:variable name="suffix">
1308       <xsl:if test="contains($type, ' *')">
1309         <xsl:text>*</xsl:text>
1310         <xsl:value-of select="substring-after($type, ' *')" />
1311       </xsl:if>
1312     </xsl:variable>
1313
1314     <xsl:value-of select="$base-type" />
1315     <xsl:if test="string-length($base-type) &lt; $min-type-length">
1316       <xsl:call-template name="repeat">
1317         <xsl:with-param name="count" select="$min-type-length
1318                                              - string-length($base-type)" />
1319       </xsl:call-template>
1320     </xsl:if>
1321     <xsl:text> </xsl:text>
1322     <xsl:if test="string-length($suffix) &lt; $min-suffix-length">
1323       <xsl:call-template name="repeat">
1324         <xsl:with-param name="count" select="$min-suffix-length
1325                                              - string-length($suffix)" />
1326       </xsl:call-template>
1327     </xsl:if>
1328     <xsl:value-of select="$suffix" />
1329     <xsl:value-of select="$name" />
1330   </xsl:template>
1331
1332   <!-- Output a list with a given separator.  Empty items are skipped. -->
1333   <xsl:template name="list">
1334     <xsl:param name="separator" />
1335     <xsl:param name="items" />
1336
1337     <xsl:for-each select="e:node-set($items)/*">
1338       <xsl:value-of select="." />
1339       <xsl:if test="not(position() = last())">
1340         <xsl:value-of select="$separator" />
1341       </xsl:if>
1342     </xsl:for-each>
1343   </xsl:template>
1344
1345   <!-- Repeat a string (space by default) a given number of times. -->
1346   <xsl:template name="repeat">
1347     <xsl:param name="str" select="' '" />
1348     <xsl:param name="count" />
1349
1350     <xsl:if test="$count &gt; 0">
1351       <xsl:value-of select="$str" />
1352       <xsl:call-template name="repeat">
1353         <xsl:with-param name="str"   select="$str" />
1354         <xsl:with-param name="count" select="$count - 1" />
1355       </xsl:call-template>
1356     </xsl:if>
1357   </xsl:template>
1358
1359   <!-- Record the maximum type lengths of a set of types for use as the
1360        max-type-lengths parameter of type-and-name. -->
1361   <xsl:template name="type-lengths">
1362     <xsl:param name="items" />
1363     <xsl:variable name="type-lengths-rtf">
1364       <xsl:for-each select="$items">
1365         <item>
1366           <xsl:choose>
1367             <xsl:when test="contains(., ' *')">
1368               <xsl:value-of select="string-length(
1369                                     substring-before(., ' *'))" />
1370             </xsl:when>
1371             <xsl:otherwise>
1372               <xsl:value-of select="string-length(.)" />
1373             </xsl:otherwise>
1374           </xsl:choose>
1375         </item>
1376       </xsl:for-each>
1377     </xsl:variable>
1378     <xsl:variable name="suffix-lengths-rtf">
1379       <xsl:for-each select="$items">
1380         <item>
1381           <xsl:choose>
1382             <xsl:when test="contains(., ' *')">
1383               <xsl:value-of select="string-length(substring-after(., ' *'))
1384                                     + 1" />
1385             </xsl:when>
1386             <xsl:otherwise>
1387               <xsl:text>0</xsl:text>
1388             </xsl:otherwise>
1389           </xsl:choose>
1390         </item>
1391       </xsl:for-each>
1392     </xsl:variable>
1393     <max-type-length>
1394       <xsl:call-template name="max">
1395         <xsl:with-param name="items"
1396                         select="e:node-set($type-lengths-rtf)/*" />
1397       </xsl:call-template>
1398     </max-type-length>
1399     <max-suffix-length>
1400       <xsl:call-template name="max">
1401         <xsl:with-param name="items"
1402                         select="e:node-set($suffix-lengths-rtf)/*" />
1403       </xsl:call-template>
1404     </max-suffix-length>
1405   </xsl:template>
1406
1407   <!-- Return the maximum number in a set of numbers. -->
1408   <xsl:template name="max">
1409     <xsl:param name="items" />
1410     <xsl:choose>
1411       <xsl:when test="count($items) = 0">
1412         <xsl:text>0</xsl:text>
1413       </xsl:when>
1414       <xsl:otherwise>
1415         <xsl:variable name="head" select="number($items[1])" />
1416         <xsl:variable name="tail-max">
1417           <xsl:call-template name="max">
1418             <xsl:with-param name="items" select="$items[position() > 1]" />
1419           </xsl:call-template>
1420         </xsl:variable>
1421         <xsl:choose>
1422           <xsl:when test="$head > number($tail-max)">
1423             <xsl:value-of select="$head" />
1424           </xsl:when>
1425           <xsl:otherwise>
1426             <xsl:value-of select="$tail-max" />
1427           </xsl:otherwise>
1428         </xsl:choose>
1429       </xsl:otherwise>
1430     </xsl:choose>
1431   </xsl:template>
1432 </xsl:transform>