8b951cb1c372cc1d474b01d50099f3a38a5b5c88
[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   <xsl:variable name="need-string-h" select="//request/pad[@bytes != 1]" />
52   
53   <!-- String used to indent lines of code. -->
54   <xsl:variable name="indent-string" select="'    '" />
55
56   <xsl:variable name="ucase" select="'ABCDEFGHIJKLMNOPQRSTUVWXYZ'" />
57   <xsl:variable name="lcase" select="'abcdefghijklmnopqrstuvwxyz'" />
58   <xsl:variable name="letters" select="concat($ucase, $lcase)" />
59   <xsl:variable name="digits" select="'0123456789'" />
60
61   <xsl:variable name="header" select="/xcb/@header" />
62   <xsl:variable name="ucase-header"
63                 select="translate($header,$lcase,$ucase)" />
64
65   <xsl:variable name="ext" select="/xcb/@extension-name" />
66
67   <!-- Other protocol descriptions to search for types in, after checking the
68        current protocol description. -->
69   <xsl:variable name="search-path-rtf">
70     <xsl:for-each select="/xcb/import">
71       <path><xsl:value-of select="concat($extension-path, ., '.xml')" /></path>
72     </xsl:for-each>
73   </xsl:variable>
74   <xsl:variable name="search-path" select="e:node-set($search-path-rtf)/path"/>
75
76   <xsl:variable name="root" select="/" />
77   
78   <!-- First pass: Store everything in a variable. -->
79   <xsl:variable name="pass1-rtf">
80     <xsl:apply-templates select="/" mode="pass1" />
81   </xsl:variable>
82   <xsl:variable name="pass1" select="e:node-set($pass1-rtf)" />
83   
84   <xsl:template match="xcb" mode="pass1">
85     <xcb>
86       <xsl:copy-of select="@*" />
87       <xsl:if test="$ext">
88         <constant type="xcb_extension_t" name="{xcb:xcb-prefix()}_id">
89           <xsl:attribute name="value">{ "<xsl:value-of select="@extension-xname" />" }</xsl:attribute>
90         </constant>
91       </xsl:if>
92       <xsl:apply-templates mode="pass1" />
93     </xcb>
94   </xsl:template>
95
96   <func:function name="xcb:xcb-prefix">
97     <xsl:param name="name" />
98     <func:result>
99       <xsl:text>xcb</xsl:text>
100       <xsl:choose>
101         <xsl:when test="/xcb/@extension-name = 'RandR'">
102           <xsl:text>_randr</xsl:text>
103         </xsl:when>
104         <xsl:when test="/xcb/@extension-name = 'ScreenSaver'">
105           <xsl:text>_screensaver</xsl:text>
106         </xsl:when>
107         <xsl:when test="/xcb/@extension-name = 'XF86Dri'">
108           <xsl:text>_xf86dri</xsl:text>
109         </xsl:when>
110         <xsl:when test="/xcb/@extension-name = 'XFixes'">
111           <xsl:text>_xfixes</xsl:text>
112         </xsl:when>
113         <xsl:when test="/xcb/@extension-name = 'XvMC'">
114           <xsl:text>_xvmc</xsl:text>
115         </xsl:when>
116         <xsl:when test="/xcb/@extension-name">
117           <xsl:text>_</xsl:text>
118           <xsl:call-template name="camelcase-to-underscore">
119             <xsl:with-param name="camelcase" select="/xcb/@extension-name" />
120           </xsl:call-template>
121         </xsl:when>
122         <xsl:otherwise>
123         </xsl:otherwise>
124       </xsl:choose>
125       <xsl:if test="$name">
126         <xsl:text>_</xsl:text>
127         <xsl:call-template name="camelcase-to-underscore">
128           <xsl:with-param name="camelcase" select="$name" />
129         </xsl:call-template>
130       </xsl:if>
131     </func:result>
132   </func:function>
133
134   <func:function name="xcb:lowercase">
135     <xsl:param name="name" />
136     <func:result>
137       <xsl:call-template name="camelcase-to-underscore">
138         <xsl:with-param name="camelcase" select="$name" />
139       </xsl:call-template>
140     </func:result>
141   </func:function>
142
143   <func:function name="xcb:get-char-void">
144     <xsl:param name="name" />
145     <xsl:variable name="ctype" select="substring-before($name, '_t')" />
146     <func:result>
147       <xsl:choose>
148         <xsl:when test="$ctype = 'char' or $ctype = 'void' or $ctype = 'float' or $ctype = 'double'">
149           <xsl:value-of select="$ctype" />    
150         </xsl:when>
151         <xsl:otherwise>
152           <xsl:value-of select="$name" />
153         </xsl:otherwise>
154       </xsl:choose>
155     </func:result>
156   </func:function>
157
158   <func:function name="xcb:remove-void">
159     <xsl:param name="name" />
160     <xsl:variable name="ctype" select="substring-before($name, '_t')" />
161     <func:result>
162       <xsl:choose>
163         <xsl:when test="$ctype = 'char' or $ctype = 'void' or $ctype = 'float' or $ctype = 'double'">
164           <xsl:choose>
165             <xsl:when test="$ctype = 'void'">
166               <xsl:text>char</xsl:text>
167             </xsl:when>
168             <xsl:otherwise>
169               <xsl:value-of select="$ctype" />
170             </xsl:otherwise>
171           </xsl:choose>
172         </xsl:when>
173         <xsl:otherwise>
174           <xsl:value-of select="$name" />
175         </xsl:otherwise>
176       </xsl:choose>
177     </func:result>
178   </func:function>
179
180   <!-- split camel case into words and insert underscore -->
181   <xsl:template name="camelcase-to-underscore">
182     <xsl:param name="camelcase"/>
183     <xsl:choose>
184       <xsl:when test="$camelcase='CHAR2B' or $camelcase='INT64'
185                       or $camelcase='FLOAT32' or $camelcase='FLOAT64'
186                       or $camelcase='BOOL32' or $camelcase='STRING8'
187                       or $camelcase='Family_DECnet'">
188         <xsl:value-of select="translate($camelcase, $ucase, $lcase)"/>
189       </xsl:when>
190       <xsl:otherwise>
191         <xsl:for-each select="str:split($camelcase, '')">
192           <xsl:variable name="a" select="."/>
193           <xsl:variable name="b" select="following::*[1]"/>
194           <xsl:variable name="c" select="following::*[2]"/>
195           <xsl:value-of select="translate(., $ucase, $lcase)"/>
196           <xsl:if test="($b and contains($lcase, $a) and contains($ucase, $b))
197                         or ($b and contains($digits, $a)
198                             and contains($letters, $b))
199                         or ($b and contains($letters, $a)
200                             and contains($digits, $b))
201                         or ($c and contains($ucase, $a)
202                             and contains($ucase, $b)
203                             and contains($lcase, $c))">
204             <xsl:text>_</xsl:text>
205           </xsl:if>
206         </xsl:for-each>
207       </xsl:otherwise>
208     </xsl:choose>
209   </xsl:template>
210
211   <!-- Modify names that conflict with C++ keywords by prefixing them with an
212        underscore.  If the name parameter is not specified, it defaults to the
213        value of the name attribute on the context node. -->
214   <xsl:template name="canonical-var-name">
215     <xsl:param name="name" select="@name" />
216     <xsl:if test="$name='new' or $name='delete'
217                   or $name='class' or $name='operator'">
218       <xsl:text>_</xsl:text>
219     </xsl:if>
220     <xsl:value-of select="$name" />
221   </xsl:template>
222
223   <!-- List of core types, for use in canonical-type-name. -->
224   <xsl:variable name="core-types-rtf">
225     <type name="BOOL" newname="uint8" />
226     <type name="BYTE" newname="uint8" />
227     <type name="CARD8" newname="uint8" />
228     <type name="CARD16" newname="uint16" />
229     <type name="CARD32" newname="uint32" />
230     <type name="INT8" newname="int8" />
231     <type name="INT16" newname="int16" />
232     <type name="INT32" newname="int32" />
233
234     <type name="char" newname="char" />
235     <type name="void" newname="void" />
236     <type name="float" newname="float" />
237     <type name="double" newname="double" />
238   </xsl:variable>
239   <xsl:variable name="core-types" select="e:node-set($core-types-rtf)" />
240
241   <!--
242     Output the canonical name for a type.  This will be
243     xcb_{extension-containing-type-if-any}_type, wherever the type is found in
244     the search path, or just type if not found.  If the type parameter is not
245     specified, it defaults to the value of the type attribute on the context
246     node.
247   -->
248   <xsl:template name="canonical-type-name">
249     <xsl:param name="type" select="string(@type)" />
250
251     <xsl:variable name="is-unqualified" select="not(contains($type, ':'))"/>
252     <xsl:variable name="namespace" select="substring-before($type, ':')" />
253     <xsl:variable name="unqualified-type">
254       <xsl:choose>
255         <xsl:when test="$is-unqualified">
256           <xsl:value-of select="$type" />
257         </xsl:when>
258         <xsl:otherwise>
259           <xsl:value-of select="substring-after($type, ':')" />
260         </xsl:otherwise>
261       </xsl:choose>
262     </xsl:variable>
263
264     <xsl:choose>
265       <xsl:when test="$is-unqualified and $core-types/type[@name=$type]">
266         <xsl:value-of select="$core-types/type[@name=$type]/@newname" />
267       </xsl:when>
268       <xsl:otherwise>
269         <xsl:variable name="type-definitions"
270                       select="(/xcb|document($search-path)/xcb
271                               )[$is-unqualified or @header=$namespace]
272                                /*[((self::struct or self::union or self::enum
273                                     or self::xidtype or self::xidunion
274                                     or self::event or self::eventcopy
275                                     or self::error or self::errorcopy)
276                                    and @name=$unqualified-type)
277                                   or (self::typedef
278                                       and @newname=$unqualified-type)]" />
279         <xsl:choose>
280           <xsl:when test="count($type-definitions) = 1">
281             <xsl:for-each select="$type-definitions">
282               <xsl:value-of select="xcb:xcb-prefix($unqualified-type)" />
283             </xsl:for-each>
284           </xsl:when>
285           <xsl:when test="count($type-definitions) > 1">
286             <xsl:message terminate="yes">
287               <xsl:text>Multiple definitions of type "</xsl:text>
288               <xsl:value-of select="$type" />
289               <xsl:text>" found.</xsl:text>
290               <xsl:if test="$is-unqualified">
291                 <xsl:for-each select="$type-definitions">
292                   <xsl:text>
293     </xsl:text>
294                   <xsl:value-of select="concat(/xcb/@header, ':', $type)" />
295                 </xsl:for-each>
296               </xsl:if>
297             </xsl:message>
298           </xsl:when>
299           <xsl:otherwise>
300             <xsl:message terminate="yes">
301               <xsl:text>No definitions of type "</xsl:text>
302               <xsl:value-of select="$type" />
303               <xsl:text>" found</xsl:text>
304               <xsl:if test="$is-unqualified">
305                 <xsl:text>, and it is not a known core type</xsl:text>
306               </xsl:if>
307               <xsl:text>.</xsl:text>
308             </xsl:message>
309           </xsl:otherwise>
310         </xsl:choose>   
311       </xsl:otherwise>
312     </xsl:choose>
313   </xsl:template>
314   
315   <!-- Helper template for requests, that outputs the cookie type.  The
316        parameter "request" must be the request node, which defaults to the
317        context node. -->
318   <xsl:template name="cookie-type">
319     <xsl:param name="request" select="." />
320     <xsl:choose>
321       <xsl:when test="$request/reply">
322         <xsl:value-of select="xcb:xcb-prefix($request/@name)" />
323       </xsl:when>
324       <xsl:otherwise>
325         <xsl:text>xcb_void</xsl:text>
326       </xsl:otherwise>
327     </xsl:choose>
328     <xsl:text>_cookie_t</xsl:text>
329   </xsl:template>
330
331   <xsl:template name="request-function">
332     <xsl:param name="checked" />
333     <xsl:param name="req" />
334     <function>
335       <xsl:attribute name="name">
336         <xsl:value-of select="xcb:xcb-prefix($req/@name)" />
337         <xsl:if test="$checked='true' and not($req/reply)">_checked</xsl:if>
338         <xsl:if test="$checked='false' and $req/reply">_unchecked</xsl:if>
339       </xsl:attribute>
340       <xsl:attribute name="type">
341         <xsl:call-template name="cookie-type">
342           <xsl:with-param name="request" select="$req" />
343         </xsl:call-template>
344       </xsl:attribute>
345       <field type="xcb_connection_t *" name="c" />
346       <xsl:apply-templates select="$req/*[not(self::reply)]" mode="param" />
347       <do-request ref="{xcb:xcb-prefix($req/@name)}_request_t" opcode="{translate(xcb:xcb-prefix($req/@name), $lcase, $ucase)}"
348                   checked="{$checked}">
349         <xsl:if test="$req/reply">
350           <xsl:attribute name="has-reply">true</xsl:attribute>
351         </xsl:if>
352       </do-request>
353     </function>
354   </xsl:template>
355   
356   <xsl:template match="request" mode="pass1">
357     <xsl:variable name="req" select="." />
358     <xsl:if test="reply">
359       <struct name="{xcb:xcb-prefix(@name)}_cookie_t">
360         <field type="unsigned int" name="sequence" />
361       </struct>
362     </xsl:if>
363     <constant type="number" name="{xcb:xcb-prefix($req/@name)}" value="{$req/@opcode}" />
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" 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 or self::pad)
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="pad" mode="assign">
785     <xsl:variable name="padnum"><xsl:number /></xsl:variable>
786     <l><xsl:choose>
787         <xsl:when test="@bytes = 1">xcb_out.pad<xsl:value-of select="$padnum - 1" /> = 0;</xsl:when>
788         <xsl:otherwise>memset(xcb_out.pad<xsl:value-of select="$padnum - 1" />, 0, <xsl:value-of select="@bytes" />);</xsl:otherwise>
789     </xsl:choose></l>
790   </xsl:template>
791
792   <xsl:template match="iterator" mode="pass2">
793     <struct name="{@ref}_iterator_t">
794       <field type="{@ref}_t *" name="data" />
795       <field type="int" name="rem" />
796       <field type="int" name="index" />
797     </struct>
798   </xsl:template>
799
800   <xsl:template match="iterator-functions" mode="pass2">
801     <xsl:variable name="ref" select="@ref" />
802     <xsl:variable name="kind" select="@kind" />
803     <xsl:variable name="struct"
804                   select="$pass1/xcb/struct[@name=concat($ref, $kind, '_t')]" />
805     <xsl:variable name="nextfields-rtf">
806       <nextfield>R + 1</nextfield>
807       <xsl:for-each select="$struct/list[not(@fixed)]">
808         <xsl:choose>
809           <xsl:when test="substring(@type, 1, 3) = 'xcb'">
810             <nextfield><xsl:value-of select="substring(@type, 1, string-length(@type)-2)" />_end(<!--
811             --><xsl:value-of select="$ref" />_<!--
812             --><xsl:value-of select="string(@name)" />_iterator(R))</nextfield>
813           </xsl:when>
814           <xsl:otherwise>
815             <nextfield><xsl:value-of select="$ref" />_<!--
816             --><xsl:value-of select="string(@name)" />_end(R)</nextfield>
817           </xsl:otherwise>
818         </xsl:choose>
819       </xsl:for-each>
820     </xsl:variable>
821     <xsl:variable name="nextfields" select="e:node-set($nextfields-rtf)" />
822     <xsl:for-each select="$struct/list[not(@fixed)]">
823       <xsl:variable name="number"
824                     select="1+count(preceding-sibling::list[not(@fixed)])" />
825       <xsl:variable name="nextfield" select="$nextfields/nextfield[$number]" />
826       <xsl:variable name="is-first"
827                     select="not(preceding-sibling::list[not(@fixed)])" />
828       <xsl:variable name="field-name" select="@name" />
829       <xsl:variable name="is-variable"
830                     select="$pass1/xcb/struct[@name=current()/@type]/list
831                             or document($search-path)/xcb
832                                /struct[concat(xcb:xcb-prefix(@name), '_t')
833                                        = current()/@type]
834                                /*[self::valueparam
835                                   or self::list[.//*[not(self::value
836                                                          or self::op)]]]" />
837       <xsl:if test="not($is-variable)">
838         <function type="{xcb:get-char-void(@type)} *" name="{$ref}_{xcb:lowercase($field-name)}">
839           <field type="const {$ref}{$kind}_t *" name="R" />
840           <xsl:choose>
841             <xsl:when test="$is-first">
842               <l>return (<xsl:value-of select="xcb:get-char-void(@type)" /> *) <!--
843               -->(<xsl:value-of select="$nextfield" />);</l>
844             </xsl:when>
845             <xsl:otherwise>
846               <l>xcb_generic_iterator_t prev = <!--
847               --><xsl:value-of select="$nextfield" />;</l>
848               <l>return (<xsl:value-of select="xcb:get-char-void(@type)" /> *) <!--
849               -->((char *) prev.data + XCB_TYPE_PAD(<!--
850               --><xsl:value-of select="xcb:get-char-void(@type)" />, prev.index));</l>
851             </xsl:otherwise>
852           </xsl:choose>
853         </function>
854       </xsl:if>
855       <function type="int" name="{$ref}_{xcb:lowercase($field-name)}_length">
856         <field type="const {$ref}{$kind}_t *" name="R" />
857         <l>return <xsl:apply-templates mode="output-expression">
858                     <xsl:with-param name="field-prefix" select="'R->'" />
859                   </xsl:apply-templates>;</l>
860       </function>
861       <xsl:choose>
862         <xsl:when test="substring(@type, 1, 3) = 'xcb'">
863           <function type="{substring(@type, 1, string-length(@type)-2)}_iterator_t" name="{$ref}_{xcb:lowercase($field-name)}_iterator">
864             <field type="const {$ref}{$kind}_t *" name="R" />
865             <l><xsl:value-of select="substring(@type, 1, string-length(@type)-2)" />_iterator_t i;</l>
866             <xsl:choose>
867               <xsl:when test="$is-first">
868                 <l>i.data = (<xsl:value-of select="@type" /> *) <!--
869                 -->(<xsl:value-of select="$nextfield" />);</l>
870               </xsl:when>
871               <xsl:otherwise>
872                 <l>xcb_generic_iterator_t prev = <!--
873                 --><xsl:value-of select="$nextfield" />;</l>
874                 <l>i.data = (<xsl:value-of select="@type" /> *) <!--
875                 -->((char *) prev.data + XCB_TYPE_PAD(<!--
876                 --><xsl:value-of select="@type" />, prev.index));</l>
877               </xsl:otherwise>
878             </xsl:choose>
879             <l>i.rem = <xsl:apply-templates mode="output-expression">
880                          <xsl:with-param name="field-prefix" select="'R->'" />
881                        </xsl:apply-templates>;</l>
882             <l>i.index = (char *) i.data - (char *) R;</l>
883             <l>return i;</l>
884           </function>
885         </xsl:when>
886         <xsl:otherwise>
887           <xsl:variable name="cast">
888             <xsl:choose>
889               <xsl:when test="@type='void'">char</xsl:when>
890               <xsl:otherwise><xsl:value-of select="@type" /></xsl:otherwise>
891             </xsl:choose>
892           </xsl:variable>
893           <function type="xcb_generic_iterator_t" name="{$ref}_{xcb:lowercase($field-name)}_end">
894             <field type="const {$ref}{$kind}_t *" name="R" />
895             <l>xcb_generic_iterator_t i;</l>
896             <xsl:choose>
897               <xsl:when test="$is-first">
898                 <l>i.data = ((<xsl:value-of select="xcb:remove-void($cast)" /> *) <!--
899                 -->(<xsl:value-of select="$nextfield" />)) + (<!--
900                 --><xsl:apply-templates mode="output-expression">
901                      <xsl:with-param name="field-prefix" select="'R->'" />
902                    </xsl:apply-templates>);</l>
903               </xsl:when>
904               <xsl:otherwise>
905                 <l>xcb_generic_iterator_t child = <!--
906                 --><xsl:value-of select="$nextfield" />;</l>
907                 <l>i.data = ((<xsl:value-of select="xcb:get-char-void($cast)" /> *) <!--
908                 -->child.data) + (<!--
909                 --><xsl:apply-templates mode="output-expression">
910                      <xsl:with-param name="field-prefix" select="'R->'" />
911                    </xsl:apply-templates>);</l>
912               </xsl:otherwise>
913             </xsl:choose>
914             <l>i.rem = 0;</l>
915             <l>i.index = (char *) i.data - (char *) R;</l>
916             <l>return i;</l>
917           </function>
918         </xsl:otherwise>
919       </xsl:choose>
920     </xsl:for-each>
921     <xsl:if test="not($kind)">
922       <function type="void" name="{$ref}_next">
923         <field type="{$ref}_iterator_t *" name="i" />
924         <xsl:choose>
925           <xsl:when test="$struct/list[not(@fixed)]">
926             <l><xsl:value-of select="$ref" />_t *R = i->data;</l>
927             <l>xcb_generic_iterator_t child = <!--
928             --><xsl:value-of select="$nextfields/nextfield[last()]" />;</l>
929             <l>--i->rem;</l>
930             <l>i->data = (<xsl:value-of select="$ref" />_t *) child.data;</l>
931             <l>i->index = child.index;</l>
932           </xsl:when>
933           <xsl:otherwise>
934             <l>--i->rem;</l>
935             <l>++i->data;</l>
936             <l>i->index += sizeof(<xsl:value-of select="$ref" />_t);</l>
937           </xsl:otherwise>
938         </xsl:choose>
939       </function>
940       <function type="xcb_generic_iterator_t" name="{$ref}_end">
941         <field type="{$ref}_iterator_t" name="i" />
942         <l>xcb_generic_iterator_t ret;</l>
943         <xsl:choose>
944           <xsl:when test="$struct/list[not(@fixed)]">
945             <l>while(i.rem > 0)</l>
946             <indent>
947               <l><xsl:value-of select="$ref" />_next(&amp;i);</l>
948             </indent>
949             <l>ret.data = i.data;</l>
950             <l>ret.rem = i.rem;</l>
951             <l>ret.index = i.index;</l>
952           </xsl:when>
953           <xsl:otherwise>
954             <l>ret.data = i.data + i.rem;</l>
955             <l>ret.index = i.index + ((char *) ret.data - (char *) i.data);</l>
956             <l>ret.rem = 0;</l>
957           </xsl:otherwise>
958         </xsl:choose>
959         <l>return ret;</l>
960       </function>
961     </xsl:if>
962   </xsl:template>
963
964   <!-- Output the results. -->
965   <xsl:template match="/">
966     <xsl:if test="not(function-available('e:node-set'))">
967       <xsl:message terminate="yes"><!--
968         -->Error: This stylesheet requires the EXSL node-set extension.<!--
969       --></xsl:message>
970     </xsl:if>
971
972     <xsl:if test="not($h) and not($c)">
973       <xsl:message terminate="yes"><!--
974         -->Error: Parameter "mode" must be "header" or "source".<!--
975       --></xsl:message>
976     </xsl:if>
977
978     <xsl:apply-templates select="$result/*" mode="output" />
979   </xsl:template>
980
981   <xsl:template match="xcb" mode="output">
982     <xsl:variable name="guard"><!--
983       -->__<xsl:value-of select="$ucase-header" />_H<!--
984     --></xsl:variable>
985
986 <xsl:text>/*
987  * This file generated automatically from </xsl:text>
988 <xsl:value-of select="$header" /><xsl:text>.xml by c-client.xsl using XSLT.
989  * Edit at your peril.
990  */
991 </xsl:text>
992 <xsl:if test="$h"><xsl:text>
993 /**
994  * @defgroup XCB_</xsl:text><xsl:value-of select="$ext" /><xsl:text>_API XCB </xsl:text><xsl:value-of select="$ext" /><xsl:text> API
995  * @brief </xsl:text><xsl:value-of select="$ext" /><xsl:text> XCB Protocol Implementation.</xsl:text>
996 <xsl:text>
997  * @{
998  **/
999 </xsl:text>
1000
1001 <xsl:text>
1002 #ifndef </xsl:text><xsl:value-of select="$guard" /><xsl:text>
1003 #define </xsl:text><xsl:value-of select="$guard" /><xsl:text>
1004 </xsl:text>
1005 #include "xcb.h"
1006 <xsl:for-each select="$root/xcb/import">
1007 <xsl:text>#include "</xsl:text><xsl:value-of select="." /><xsl:text>.h"
1008 </xsl:text>
1009 </xsl:for-each>
1010 <xsl:text>
1011 </xsl:text>
1012 </xsl:if>
1013 <xsl:if test="$h">
1014     <xsl:choose>
1015         <xsl:when test="string($ext)">
1016   <xsl:text>#define XCB_</xsl:text><xsl:value-of select="translate($ext, $lcase, $ucase)"/><xsl:text>_MAJOR_VERSION </xsl:text><xsl:value-of select="/xcb/@major-version" /><xsl:text>
1017 </xsl:text>
1018   <xsl:text>#define XCB_</xsl:text><xsl:value-of select="translate($ext, $lcase, $ucase)"/><xsl:text>_MINOR_VERSION </xsl:text><xsl:value-of select="/xcb/@minor-version" />
1019   <xsl:text>
1020   
1021 </xsl:text>
1022     </xsl:when>
1023   </xsl:choose>
1024 </xsl:if>
1025
1026 <xsl:if test="$c">
1027 <xsl:if test="$need-string-h">
1028 #include &lt;string.h&gt;</xsl:if>
1029 <xsl:text>
1030 #include &lt;assert.h&gt;
1031 #include "xcbext.h"
1032 #include "</xsl:text><xsl:value-of select="$header" /><xsl:text>.h"
1033
1034 </xsl:text></xsl:if>
1035
1036     <xsl:apply-templates mode="output" />
1037
1038 <xsl:if test="$h">
1039 <xsl:text>
1040 #endif
1041
1042 /**
1043  * @}
1044  */
1045 </xsl:text>
1046 </xsl:if>
1047   </xsl:template>
1048
1049   <xsl:template match="constant" mode="output">
1050     <xsl:choose>
1051       <xsl:when test="@type = 'number'">
1052         <xsl:if test="$h">
1053           <xsl:text>/** Opcode for </xsl:text><xsl:value-of select="@name"/><xsl:text>. */
1054 </xsl:text>
1055           <xsl:text>#define </xsl:text>
1056           <xsl:value-of select="translate(@name, $lcase, $ucase)" />
1057           <xsl:text> </xsl:text>
1058           <xsl:value-of select="@value" />
1059           <xsl:text>
1060
1061 </xsl:text>
1062         </xsl:if>
1063       </xsl:when>
1064       <xsl:when test="@type = 'string'">
1065         <xsl:if test="$h">
1066           <xsl:text>extern </xsl:text>
1067         </xsl:if>
1068         <xsl:text>const char </xsl:text>
1069         <xsl:value-of select="@name" />
1070         <xsl:text>[]</xsl:text>
1071         <xsl:if test="$c">
1072           <xsl:text> = "</xsl:text>
1073           <xsl:value-of select="@value" />
1074           <xsl:text>"</xsl:text>
1075         </xsl:if>
1076         <xsl:text>;
1077
1078 </xsl:text>
1079       </xsl:when>
1080       <xsl:otherwise>
1081         <xsl:if test="$h">
1082           <xsl:text>extern </xsl:text>
1083         </xsl:if>
1084         <xsl:call-template name="type-and-name" />
1085         <xsl:if test="$c">
1086           <xsl:text> = </xsl:text>
1087           <xsl:value-of select="@value" />
1088         </xsl:if>
1089         <xsl:text>;
1090
1091 </xsl:text>
1092       </xsl:otherwise>
1093     </xsl:choose>
1094   </xsl:template>
1095
1096   <xsl:template match="typedef" mode="output">
1097     <xsl:if test="$h">
1098       <xsl:text>typedef </xsl:text>
1099       <xsl:value-of select="xcb:get-char-void(@oldname)" />
1100       <xsl:text> </xsl:text>
1101       <xsl:value-of select="@newname" />
1102       <xsl:text>;
1103
1104 </xsl:text>
1105     </xsl:if>
1106   </xsl:template>
1107
1108   <xsl:template match="struct" mode="output">
1109     <xsl:if test="$h">
1110       <xsl:variable name="type-lengths">
1111         <xsl:call-template name="type-lengths">
1112           <xsl:with-param name="items" select="field/@type" />
1113         </xsl:call-template>
1114       </xsl:variable>
1115       <xsl:text>/**
1116  * @brief </xsl:text><xsl:value-of select="@name" /><xsl:text>
1117  **/
1118 </xsl:text>
1119       <xsl:text>typedef </xsl:text>
1120       <xsl:if test="not(@kind)">struct</xsl:if><xsl:value-of select="@kind" />
1121       <xsl:text> </xsl:text>
1122       <xsl:value-of select="@name" />
1123       <xsl:text> {
1124 </xsl:text>
1125       <xsl:for-each select="exprfield|field|list[@fixed]|pad">
1126         <xsl:text>    </xsl:text>
1127         <xsl:apply-templates select=".">
1128           <xsl:with-param name="type-lengths" select="$type-lengths" />
1129         </xsl:apply-templates>
1130         <xsl:text>; /**&lt; </xsl:text><xsl:text> */
1131 </xsl:text>
1132       </xsl:for-each>
1133       <xsl:text>} </xsl:text>
1134       <xsl:value-of select="@name" />
1135       <xsl:text>;
1136
1137 </xsl:text>
1138     </xsl:if>
1139   </xsl:template>
1140
1141   <xsl:template match="enum" mode="output">
1142     <xsl:if test="$h">
1143       <xsl:text>typedef enum </xsl:text>
1144       <xsl:value-of select="@name" />
1145       <xsl:text> {
1146     </xsl:text>
1147       <xsl:call-template name="list">
1148         <xsl:with-param name="separator"><xsl:text>,
1149     </xsl:text></xsl:with-param>
1150         <xsl:with-param name="items">
1151           <xsl:for-each select="item">
1152             <item>
1153               <xsl:value-of select="@name" />
1154               <xsl:if test="node()"> <!-- If there is an expression -->
1155                 <xsl:text> = </xsl:text>
1156                 <xsl:apply-templates mode="output-expression" />
1157               </xsl:if>
1158             </item>
1159           </xsl:for-each>
1160         </xsl:with-param>
1161       </xsl:call-template>
1162       <xsl:text>
1163 } </xsl:text><xsl:value-of select="@name" /><xsl:text>;
1164
1165 </xsl:text>
1166     </xsl:if>
1167   </xsl:template>
1168
1169   <xsl:template match="function" mode="output">
1170     <xsl:variable name="decl-open" select="concat(@name, ' (')" />
1171     <xsl:variable name="type-lengths">
1172       <xsl:call-template name="type-lengths">
1173         <xsl:with-param name="items" select="field/@type" />
1174       </xsl:call-template>
1175   </xsl:variable>
1176   <!-- Doxygen for functions in header. -->
1177 /*****************************************************************************
1178  **
1179  ** <xsl:value-of select="@type" />
1180  <xsl:text> </xsl:text>
1181  <xsl:value-of select="@name" />
1182  ** <xsl:call-template name="list">
1183      <xsl:with-param name="items">
1184          <xsl:for-each select="field">
1185              <item>
1186                  <xsl:text>
1187  ** @param </xsl:text>
1188                  <xsl:apply-templates select=".">
1189                      <xsl:with-param name="type-lengths" select="$type-lengths" />
1190                  </xsl:apply-templates>
1191              </item>
1192          </xsl:for-each>
1193      </xsl:with-param>
1194  </xsl:call-template>
1195  ** @returns <xsl:value-of select="@type" />
1196  **
1197  *****************************************************************************/
1198  
1199 <xsl:value-of select="@type" />
1200     <xsl:text>
1201 </xsl:text>
1202     <xsl:value-of select="$decl-open" />
1203     <xsl:call-template name="list">
1204       <xsl:with-param name="separator">
1205         <xsl:text>,
1206 </xsl:text>
1207         <xsl:call-template name="repeat">
1208           <xsl:with-param name="count" select="string-length($decl-open)" />
1209         </xsl:call-template>
1210       </xsl:with-param>
1211       <xsl:with-param name="items">
1212         <xsl:for-each select="field">
1213           <item>
1214             <xsl:apply-templates select=".">
1215               <xsl:with-param name="type-lengths" select="$type-lengths" />
1216             </xsl:apply-templates>
1217             <xsl:text>  /**&lt; */</xsl:text>
1218           </item>
1219         </xsl:for-each>
1220       </xsl:with-param>
1221     </xsl:call-template>
1222     <xsl:text>)</xsl:text>
1223
1224     <xsl:if test="$h"><xsl:text>;
1225
1226 </xsl:text></xsl:if>
1227
1228     <xsl:if test="$c">
1229       <xsl:text>
1230 {
1231 </xsl:text>
1232       <xsl:apply-templates select="l|indent" mode="function-body">
1233         <xsl:with-param name="indent" select="$indent-string" />
1234       </xsl:apply-templates>
1235       <xsl:text>}
1236
1237 </xsl:text>
1238     </xsl:if>
1239   </xsl:template>
1240
1241   <xsl:template match="l" mode="function-body">
1242     <xsl:param name="indent" />
1243     <xsl:value-of select="concat($indent, .)" /><xsl:text>
1244 </xsl:text>
1245   </xsl:template>
1246
1247   <xsl:template match="indent" mode="function-body">
1248     <xsl:param name="indent" />
1249     <xsl:apply-templates select="l|indent" mode="function-body">
1250       <xsl:with-param name="indent" select="concat($indent, $indent-string)" />
1251     </xsl:apply-templates>
1252   </xsl:template>
1253
1254   <xsl:template match="value" mode="output-expression">
1255     <xsl:value-of select="." />
1256   </xsl:template>
1257
1258   <xsl:template match="fieldref" mode="output-expression">
1259     <xsl:param name="field-prefix" />
1260     <xsl:value-of select="concat($field-prefix, .)" />
1261   </xsl:template>
1262
1263   <xsl:template match="op" mode="output-expression">
1264     <xsl:param name="field-prefix" />
1265     <xsl:text>(</xsl:text>
1266     <xsl:apply-templates select="node()[1]" mode="output-expression">
1267       <xsl:with-param name="field-prefix" select="$field-prefix" />
1268     </xsl:apply-templates>
1269     <xsl:text> </xsl:text>
1270     <xsl:value-of select="@op" />
1271     <xsl:text> </xsl:text>
1272     <xsl:apply-templates select="node()[2]" mode="output-expression">
1273       <xsl:with-param name="field-prefix" select="$field-prefix" />
1274     </xsl:apply-templates>
1275     <xsl:text>)</xsl:text>
1276   </xsl:template>
1277
1278   <xsl:template match="bit" mode="output-expression">
1279     <xsl:text>(1 &lt;&lt; </xsl:text>
1280     <xsl:value-of select="." />
1281     <xsl:text>)</xsl:text>
1282   </xsl:template>
1283
1284   <xsl:template match="function-call" mode="output-expression">
1285     <xsl:param name="field-prefix" />
1286     <xsl:value-of select="@name" />
1287     <xsl:text>(</xsl:text>
1288     <xsl:call-template name="list">
1289       <xsl:with-param name="separator" select="', '" />
1290       <xsl:with-param name="items">
1291         <xsl:for-each select="param">
1292           <item><xsl:apply-templates mode="output-expression">
1293             <xsl:with-param name="field-prefix" select="$field-prefix" />
1294           </xsl:apply-templates></item>
1295         </xsl:for-each>
1296       </xsl:with-param>
1297     </xsl:call-template>
1298     <xsl:text>)</xsl:text>
1299   </xsl:template>
1300
1301   <!-- Catch invalid elements in expressions. -->
1302   <xsl:template match="*" mode="output-expression">
1303     <xsl:message terminate="yes">
1304       <xsl:text>Invalid element in expression: </xsl:text>
1305       <xsl:value-of select="name()" />
1306     </xsl:message>
1307   </xsl:template>
1308
1309   <xsl:template match="field|exprfield">
1310     <xsl:param name="type-lengths" select="0" />
1311     <xsl:call-template name="type-and-name">
1312       <xsl:with-param name="type-lengths" select="$type-lengths" />
1313     </xsl:call-template>
1314   </xsl:template>
1315
1316   <xsl:template match="list[@fixed]">
1317     <xsl:param name="type-lengths" select="0" />
1318     <xsl:call-template name="type-and-name">
1319       <xsl:with-param name="type-lengths" select="$type-lengths" />
1320     </xsl:call-template>
1321     <xsl:text>[</xsl:text>
1322     <xsl:apply-templates mode="output-expression" />
1323     <xsl:text>]</xsl:text>
1324   </xsl:template>
1325
1326   <xsl:template match="pad">
1327     <xsl:param name="type-lengths" select="0" />
1328
1329     <xsl:variable name="padnum"><xsl:number /></xsl:variable>
1330
1331     <xsl:call-template name="type-and-name">
1332       <xsl:with-param name="type" select="'uint8_t'" />
1333       <xsl:with-param name="name">
1334         <xsl:text>pad</xsl:text>
1335         <xsl:value-of select="$padnum - 1" />
1336       </xsl:with-param>
1337       <xsl:with-param name="type-lengths" select="$type-lengths" />
1338     </xsl:call-template>
1339     <xsl:if test="@bytes > 1">
1340       <xsl:text>[</xsl:text>
1341       <xsl:value-of select="@bytes" />
1342       <xsl:text>]</xsl:text>
1343     </xsl:if>
1344   </xsl:template>
1345
1346   <!-- Output the given type and name (defaulting to the corresponding
1347        attributes of the context node), with the appropriate spacing.  The
1348        type must consist of a base type (which may contain spaces), then
1349        optionally a single space and a suffix of one or more '*' characters.
1350        If the type-lengths parameter is provided, use it to line up the base
1351        types and suffixs of the type declarations. -->
1352   <xsl:template name="type-and-name">
1353     <xsl:param name="type" select="@type" />
1354     <xsl:param name="name" select="@name" />
1355     <xsl:param name="type-lengths">
1356       <max-type-length>0</max-type-length>
1357       <max-suffix-length>0</max-suffix-length>
1358     </xsl:param>
1359     
1360     <xsl:variable name="type-lengths-ns" select="e:node-set($type-lengths)" />
1361     <xsl:variable name="min-type-length"
1362                   select="$type-lengths-ns/max-type-length" />
1363     <xsl:variable name="min-suffix-length"
1364                   select="$type-lengths-ns/max-suffix-length" />
1365
1366     <xsl:variable name="base-type">
1367       <xsl:choose>
1368         <xsl:when test="contains($type, ' *')">
1369           <xsl:value-of select="substring-before($type, ' *')" />
1370         </xsl:when>
1371         <xsl:otherwise>
1372           <xsl:value-of select="$type" />
1373         </xsl:otherwise>
1374       </xsl:choose>
1375     </xsl:variable>
1376     <xsl:variable name="suffix">
1377       <xsl:if test="contains($type, ' *')">
1378         <xsl:text>*</xsl:text>
1379         <xsl:value-of select="substring-after($type, ' *')" />
1380       </xsl:if>
1381     </xsl:variable>
1382
1383     <xsl:value-of select="$base-type" />
1384     <xsl:if test="string-length($base-type) &lt; $min-type-length">
1385       <xsl:call-template name="repeat">
1386         <xsl:with-param name="count" select="$min-type-length
1387                                              - string-length($base-type)" />
1388       </xsl:call-template>
1389     </xsl:if>
1390     <xsl:text> </xsl:text>
1391     <xsl:if test="string-length($suffix) &lt; $min-suffix-length">
1392       <xsl:call-template name="repeat">
1393         <xsl:with-param name="count" select="$min-suffix-length
1394                                              - string-length($suffix)" />
1395       </xsl:call-template>
1396     </xsl:if>
1397     <xsl:value-of select="$suffix" />
1398     <xsl:value-of select="$name" />
1399   </xsl:template>
1400
1401   <!-- Output a list with a given separator.  Empty items are skipped. -->
1402   <xsl:template name="list">
1403     <xsl:param name="separator" />
1404     <xsl:param name="items" />
1405
1406     <xsl:for-each select="e:node-set($items)/*">
1407       <xsl:value-of select="." />
1408       <xsl:if test="not(position() = last())">
1409         <xsl:value-of select="$separator" />
1410       </xsl:if>
1411     </xsl:for-each>
1412   </xsl:template>
1413
1414   <!-- Repeat a string (space by default) a given number of times. -->
1415   <xsl:template name="repeat">
1416     <xsl:param name="str" select="' '" />
1417     <xsl:param name="count" />
1418
1419     <xsl:if test="$count &gt; 0">
1420       <xsl:value-of select="$str" />
1421       <xsl:call-template name="repeat">
1422         <xsl:with-param name="str"   select="$str" />
1423         <xsl:with-param name="count" select="$count - 1" />
1424       </xsl:call-template>
1425     </xsl:if>
1426   </xsl:template>
1427
1428   <!-- Record the maximum type lengths of a set of types for use as the
1429        max-type-lengths parameter of type-and-name. -->
1430   <xsl:template name="type-lengths">
1431     <xsl:param name="items" />
1432     <xsl:variable name="type-lengths-rtf">
1433       <xsl:for-each select="$items">
1434         <item>
1435           <xsl:choose>
1436             <xsl:when test="contains(., ' *')">
1437               <xsl:value-of select="string-length(
1438                                     substring-before(., ' *'))" />
1439             </xsl:when>
1440             <xsl:otherwise>
1441               <xsl:value-of select="string-length(.)" />
1442             </xsl:otherwise>
1443           </xsl:choose>
1444         </item>
1445       </xsl:for-each>
1446     </xsl:variable>
1447     <xsl:variable name="suffix-lengths-rtf">
1448       <xsl:for-each select="$items">
1449         <item>
1450           <xsl:choose>
1451             <xsl:when test="contains(., ' *')">
1452               <xsl:value-of select="string-length(substring-after(., ' *'))
1453                                     + 1" />
1454             </xsl:when>
1455             <xsl:otherwise>
1456               <xsl:text>0</xsl:text>
1457             </xsl:otherwise>
1458           </xsl:choose>
1459         </item>
1460       </xsl:for-each>
1461     </xsl:variable>
1462     <max-type-length>
1463       <xsl:call-template name="max">
1464         <xsl:with-param name="items"
1465                         select="e:node-set($type-lengths-rtf)/*" />
1466       </xsl:call-template>
1467     </max-type-length>
1468     <max-suffix-length>
1469       <xsl:call-template name="max">
1470         <xsl:with-param name="items"
1471                         select="e:node-set($suffix-lengths-rtf)/*" />
1472       </xsl:call-template>
1473     </max-suffix-length>
1474   </xsl:template>
1475
1476   <!-- Return the maximum number in a set of numbers. -->
1477   <xsl:template name="max">
1478     <xsl:param name="items" />
1479     <xsl:choose>
1480       <xsl:when test="count($items) = 0">
1481         <xsl:text>0</xsl:text>
1482       </xsl:when>
1483       <xsl:otherwise>
1484         <xsl:variable name="head" select="number($items[1])" />
1485         <xsl:variable name="tail-max">
1486           <xsl:call-template name="max">
1487             <xsl:with-param name="items" select="$items[position() > 1]" />
1488           </xsl:call-template>
1489         </xsl:variable>
1490         <xsl:choose>
1491           <xsl:when test="$head > number($tail-max)">
1492             <xsl:value-of select="$head" />
1493           </xsl:when>
1494           <xsl:otherwise>
1495             <xsl:value-of select="$tail-max" />
1496           </xsl:otherwise>
1497         </xsl:choose>
1498       </xsl:otherwise>
1499     </xsl:choose>
1500   </xsl:template>
1501 </xsl:transform>