de34096f96f00f550ecbbfe06c3e758d7c0a4c6b
[barrelfish] / tools / sockeye / MachineModelCode.xsl
1 <?xml version="1.0" encoding="UTF-8"?>
2 <xsl:stylesheet version="1.0"
3     xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xs="http://www.w3.org/2001/XMLSchema">
4     <xsl:output method="text" indent="no" />
5
6     <xsl:strip-space elements="xs:sequence"/>
7
8     <!-- Generate a name from an element -->
9     <xsl:template name="gen-name">
10         <xsl:param name="element"></xsl:param>
11         <xsl:value-of select="/xs:schema/@id" />_<xsl:value-of select="$element/@name" />
12     </xsl:template>
13
14     <!-- Convert a type name to a name without namespace -->
15     <xsl:template name="GetRefName">
16         <xsl:param name="ref" />
17         <xsl:choose>
18             <xsl:when test="contains($ref, ':')">
19                 <!-- Ref has namespace prefix -->
20                 <xsl:value-of select="substring-after($ref, ':')" />
21             </xsl:when>
22             <xsl:otherwise>
23                 <!-- Ref has no namespace prefix -->
24                 <xsl:value-of select="$ref" />
25             </xsl:otherwise>
26         </xsl:choose>
27     </xsl:template>
28
29     <!-- Generate the C type from a xs:restriction -->
30     <xsl:template name="gen-type">
31         <xsl:param name="restriction" />
32         <xsl:choose>
33             <xsl:when test="$restriction/@base='xs:int'">
34                 <xsl:choose>
35                     <xsl:when test="$restriction/xs:minInclusive/@value = 0">
36                         <xsl:choose>
37                             <xsl:when test="$restriction/xs:maxInclusive/@value &lt; 256">
38                                 <xsl:text>uint8</xsl:text>
39                             </xsl:when>
40                             <xsl:when test="$restriction/xs:maxInclusive/@value &lt; 65536">
41                                 <xsl:text>uint16</xsl:text>
42                             </xsl:when>
43                             <xsl:when test="$restriction/xs:maxInclusive/@value &lt; 4294967296">
44                                 <xsl:text>uint32</xsl:text>
45                             </xsl:when>
46                             <xsl:otherwise>
47                                 <xsl:text>uint64</xsl:text>
48                             </xsl:otherwise>
49                         </xsl:choose>
50                     </xsl:when>
51                     <xsl:otherwise>
52                         <xsl:choose>
53                             <xsl:when test="$restriction/xs:maxInclusive/@value &lt; 127">
54                                 <xsl:text>int8</xsl:text>
55                             </xsl:when>
56                             <xsl:when test="$restriction/xs:maxInclusive/@value &lt; 32768">
57                                 <xsl:text>int16</xsl:text>
58                             </xsl:when>
59                             <xsl:when test="$restriction/xs:maxInclusive/@value &lt; 2147483648">
60                                 <xsl:text>int32</xsl:text>
61                             </xsl:when>
62                             <xsl:otherwise>
63                                 <xsl:text>int64</xsl:text>
64                             </xsl:otherwise>
65                         </xsl:choose>
66                     </xsl:otherwise>
67                 </xsl:choose>
68             </xsl:when>
69             <xsl:otherwise>
70                 <xsl:message terminate="yes">
71                     Unknown type: <xsl:value-of select="$restriction" />
72                 </xsl:message>
73             </xsl:otherwise>
74         </xsl:choose>
75     </xsl:template>
76
77     <!-- Start of processing -->
78     <xsl:template match="/xs:schema">
79         <xsl:text>
80 <![CDATA[
81 /* Generated C code */
82 #include <barrelfish/barrelfish.h>
83 ]]></xsl:text>
84         <xsl:apply-templates select="xs:annotation"></xsl:apply-templates>
85         <xsl:for-each select="xs:simpleType">
86             <xsl:call-template name="typedef-type"></xsl:call-template>
87         </xsl:for-each>
88         <xsl:for-each select="xs:complexType">
89             <xsl:call-template name="complex-struct"></xsl:call-template>
90         </xsl:for-each>
91         <xsl:text>&#10;</xsl:text>
92         <xsl:for-each select="xs:complexType">
93             <xsl:call-template name="gen-function-header">
94                 <xsl:with-param name="type" select="current()" />
95             </xsl:call-template>
96         </xsl:for-each>
97     </xsl:template>
98
99     <xsl:template match="xs:simpleType" name="typedef-type">
100         <xsl:apply-templates select="xs:annotation"></xsl:apply-templates>
101         <xsl:variable name="type">
102             <xsl:call-template name="gen-type">
103                 <xsl:with-param name="restriction" select="xs:restriction"/>
104             </xsl:call-template>
105         </xsl:variable>
106         <xsl:text>&#10;typedef </xsl:text>
107         <xsl:value-of select="$type" />
108         <xsl:text> </xsl:text>
109         <xsl:value-of select="@name" />
110         <xsl:text>;</xsl:text>
111     </xsl:template>
112
113     <xsl:template match="xs:complexType" name="complex-struct">
114         <xsl:apply-templates select="xs:annotation"></xsl:apply-templates>
115         <xsl:variable name="name">
116             <xsl:call-template name="gen-name">
117                 <xsl:with-param name="element" select="." />
118             </xsl:call-template>
119         </xsl:variable>
120         <xsl:text>&#10;&#10;struct </xsl:text>
121         <xsl:value-of select="$name" /><xsl:text> {</xsl:text>
122         <xsl:apply-templates select="xs:sequence|xs:attribute"></xsl:apply-templates>
123         <xsl:text>&#10;};</xsl:text>
124     </xsl:template>
125
126     <xsl:template match="xs:sequence">
127         <xsl:apply-templates select="xs:element"/>
128     </xsl:template>
129
130     <xsl:template match="xs:element">
131         <xsl:apply-templates select="xs:annotation"></xsl:apply-templates>
132         <xsl:variable name="typeName">
133             <xsl:call-template name="GetRefName">
134                 <xsl:with-param name="ref" select="@type"/>
135             </xsl:call-template>
136         </xsl:variable>
137
138         <xsl:text>&#10;    struct </xsl:text>
139         <xsl:call-template name="gen-name">
140             <xsl:with-param name="element" select="/xs:schema/xs:complexType[@name = $typeName]">
141             </xsl:with-param>
142         </xsl:call-template>
143         <xsl:text> </xsl:text>
144         <xsl:value-of select="@name" />
145         <xsl:text>;</xsl:text>
146     </xsl:template>
147
148     <xsl:template match="xs:attribute">
149         <xsl:variable name="typeName">
150             <xsl:call-template name="GetRefName">
151                 <xsl:with-param name="ref" select="@type"/>
152             </xsl:call-template>
153         </xsl:variable>
154         <xsl:apply-templates select="xs:annotation"></xsl:apply-templates>
155         <xsl:text>&#10;    </xsl:text>
156         <xsl:value-of select="$typeName"></xsl:value-of>
157         <xsl:text> </xsl:text>
158         <xsl:value-of select="@name"></xsl:value-of>
159         <xsl:text>;</xsl:text>
160     </xsl:template>
161
162     <xsl:template name="gen-function-header">
163         <xsl:param name="type" />
164         <xsl:text>&#10;errval_t </xsl:text>
165
166         <xsl:variable name="name">
167             <xsl:call-template name="gen-name">
168                 <xsl:with-param name="element" select="$type" />
169             </xsl:call-template>
170         </xsl:variable>
171
172         <xsl:value-of select="$name" />
173         <xsl:text>_add(struct </xsl:text>
174         <xsl:value-of select="$name" />
175         <xsl:text>*);</xsl:text>
176     </xsl:template>
177
178     <xsl:template match="xs:annotation">
179 /**
180 <xsl:for-each select="tokenize(current()/xs:documentation, '\r?\n')">
181  * <xsl:sequence select="."/>
182 </xsl:for-each> 
183  */
184     </xsl:template>
185 </xsl:stylesheet>