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