resources/styles/gccxml2def-utils.xsl
author Jan Vrany <jan.vrany@fit.cvut.cz>
Thu, 02 Jul 2015 09:00:12 +0100
changeset 35 69765ac6008b
parent 21 899da9dea8a9
permissions -rw-r--r--
Do NOT result typedefs during XML transformation. Let the generator / mappings to decide whether they need to be resolved or not.

<?xml version="1.0" encoding="iso-8859-2"?>
<xsl:stylesheet
    version="1.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

  <xsl:template name="c-type-string">
    <xsl:param name="c-type-id"/>
    <xsl:param name="c-name-id" select="$c-type-id"/>

    <xsl:choose>
      <xsl:when test="/GCC_XML/PointerType[@id = $c-type-id]">
	<xsl:text>(pointer-to </xsl:text>

	<xsl:choose>
	  <!-- Pointer is a part of the user defined type -->
	  <!-- save the name that type -->
	  <xsl:when test="/GCC_XML/Typedef[@type = $c-type-id]">
	    <xsl:call-template name="c-type-string">
	      <xsl:with-param name="c-type-id" select="/GCC_XML/PointerType[@id = $c-type-id]/@type"/>
	      <xsl:with-param name="c-name-id" select="/GCC_XML/Typedef[@type = $c-type-id]/@name"/>
	    </xsl:call-template>
	  </xsl:when>

	  <xsl:otherwise>
	    <xsl:call-template name="c-type-string">
	      <xsl:with-param name="c-type-id" select="/GCC_XML/PointerType[@id = $c-type-id]/@type"/>
	      <xsl:with-param name="c-name-id" select="/GCC_XML/PointerType[@id = $c-type-id]/@type"/>
	    </xsl:call-template>
	  </xsl:otherwise>
	</xsl:choose>
	<xsl:text>)</xsl:text>
      </xsl:when>

      <xsl:when test="/GCC_XML/CvQualifiedType[@id = $c-type-id]">
	<xsl:if test="/GCC_XML/CvQualifiedType[@id = $c-type-id]/@const = 1"><xsl:text>const </xsl:text></xsl:if>
	<xsl:call-template name="c-type-string">
	  <xsl:with-param name="c-type-id" select="/GCC_XML/CvQualifiedType[@id = $c-type-id]/@type"/>
	</xsl:call-template>
      </xsl:when>

      <xsl:when test="/GCC_XML/FundamentalType[@id = $c-type-id]">
	<xsl:value-of select="/GCC_XML/FundamentalType[@id = $c-type-id]/@name"/>
      </xsl:when>

      <xsl:when test="/GCC_XML/Typedef[@id = $c-type-id]">
        <xsl:value-of select="/GCC_XML/Typedef[@id = $c-type-id]/@name"/>
      </xsl:when>

      <xsl:when test="/GCC_XML/FunctionType[@id = $c-type-id]">
	<xsl:text>(function-type </xsl:text>
	<xsl:value-of select="$c-name-id"/>
	<xsl:text>&#10;&#09;&#09;&#09;(</xsl:text>

	<xsl:for-each select="/GCC_XML/FunctionType[@id = $c-type-id]/Argument">
	  <xsl:text>&#10;&#09;&#09;&#09;&#09;(argument (</xsl:text>
	  <xsl:call-template name="c-type-string">
	    <xsl:with-param name="c-type-id" select="@type"/>
	  </xsl:call-template>
	  <xsl:text>) </xsl:text>
	  <xsl:value-of select="@name"/>
	  <xsl:text>)</xsl:text>
	</xsl:for-each>
	<xsl:text>)&#10;&#09;&#09;&#09;(return </xsl:text>
	<xsl:call-template name="c-type-string">
	  <xsl:with-param name="c-type-id" select="/GCC_XML/FunctionType[@id = $c-type-id]/@returns"/>
	</xsl:call-template>
	<xsl:text>))</xsl:text>
      </xsl:when>

      <xsl:when test="/GCC_XML/Enumeration[@id = $c-type-id]">
	<xsl:value-of select="/GCC_XML/Enumeration[@id = $c-type-id]/@name"/>
      </xsl:when>

      <xsl:when test="/GCC_XML/Struct[@id = $c-type-id]">
	<xsl:choose>
	  <xsl:when test="/GCC_XML/Struct[@id = $c-type-id and not(@name)] or /GCC_XML/Struct[@id = $c-type-id]/@name = ''">
	    <xsl:value-of select="/GCC_XML/Struct[@id = $c-type-id]/@id"/>
	  </xsl:when>
	  <xsl:otherwise>
	    <xsl:value-of select="/GCC_XML/Struct[@id = $c-type-id]/@name"/>
	  </xsl:otherwise>
	</xsl:choose>
      </xsl:when>

      <xsl:when test="/GCC_XML/ArrayType[@id = $c-type-id]">
	<xsl:text>(array (</xsl:text>
	<xsl:call-template name="c-type-string">
	  <xsl:with-param name="c-type-id" select="/GCC_XML/ArrayType[@id = $c-type-id]/@type"/>
	</xsl:call-template>
	<xsl:text>) </xsl:text>
	<xsl:value-of select="/GCC_XML/ArrayType[@id = $c-type-id]/@max"/>
	<xsl:text>)</xsl:text>
      </xsl:when>

      <xsl:when test="/GCC_XML/Union[@id = $c-type-id]">
	<xsl:choose>
	  <xsl:when test="/GCC_XML/Union[@id = $c-type-id and not(@name)] or /GCC_XML/Union[@id = $c-type-id]/@name = ''">
	    <xsl:value-of select="/GCC_XML/Union[@id = $c-type-id]/@id"/>
	  </xsl:when>
	  <xsl:otherwise>
	    <xsl:value-of select="/GCC_XML/Union[@id = $c-type-id]/@name"/>
	  </xsl:otherwise>
	</xsl:choose>
      </xsl:when>

      <xsl:otherwise>
	<xsl:message>
	  <xsl:text>&#10;#Error: --c-type-string-- Unknown C type with id </xsl:text>
	  <xsl:value-of select="$c-type-id"/>
	</xsl:message>
      </xsl:otherwise>
    </xsl:choose>
  </xsl:template>


  <!--- ========== Tokenizer ================== -->
  <xsl:template name="tokenize">
    <xsl:param name="string" select="''" />
    <xsl:param name="delimiter" select="' '" />
    <xsl:param name="indent" select="1"/>
    <xsl:choose>
      <xsl:when test="not($string)" />
      <xsl:when test="not($delimiter)" />
      <xsl:when test="$string = ''" />
      <xsl:otherwise>
	<xsl:call-template name="_tokenize-delimiter">
	  <xsl:with-param name="string" select="$string" />
	  <xsl:with-param name="delimiter" select="$delimiter" />
	  <xsl:with-param name="indent" select="$indent" />
	</xsl:call-template>
      </xsl:otherwise>
    </xsl:choose>
  </xsl:template>

  <xsl:template name="_tokenize-delimiter">
    <xsl:param name="string" />
    <xsl:param name="delimiter" />
    <xsl:param name="indent" />
    <xsl:choose>
      <xsl:when test="not($string)" />

      <xsl:when test="contains($string, $delimiter)">

	<xsl:call-template name="find-field">
	  <xsl:with-param name="field-id" select="substring-before($string, $delimiter)"/>
	  <xsl:with-param name="string" select="$string"/>
	  <xsl:with-param name="indent" select="$indent" />
	</xsl:call-template>

	<xsl:call-template name="_tokenize-delimiter">
	  <xsl:with-param name="string" select="substring-after($string, $delimiter)" />
	  <xsl:with-param name="delimiter" select="$delimiter" />
	  <xsl:with-param name="indent" select="$indent" />
	</xsl:call-template>
      </xsl:when>

      <xsl:otherwise>
	<xsl:message>
	  <xsl:text>&#10;#Error: --tokenizer-- Nothing happend for string </xsl:text>
	  <xsl:value-of select="$string"/>
	</xsl:message>
      </xsl:otherwise>
    </xsl:choose>

  </xsl:template>



  <!--- ========== Find Field ================== -->
  <xsl:template name="find-field">
    <xsl:param name="field-id"/>
    <xsl:param name="field" select="/GCC_XML/*[@id = $field-id]"/>
    <xsl:param name="field-type" select="/GCC_XML/*[@id = $field/@type]"/>
    <xsl:param name="string"/>
    <xsl:param name="indent" />
    <xsl:choose>

      <xsl:when test="not($field)">
	<xsl:message>
	  <xsl:text>&#10;#Error: find-field: Field with id </xsl:text>
	  <xsl:value-of select="$field-id"/>
	  <xsl:text> not found!</xsl:text>
	</xsl:message>
      </xsl:when>

      <xsl:when test="local-name($field) = 'Struct'"/>
      <xsl:when test="local-name($field) = 'Enumeration'"/>
      <xsl:when test="local-name($field) = 'Union'"/>

      <xsl:when test="local-name($field) = 'Constructor'"/>
      <xsl:when test="local-name($field) = 'Destructor'"/>
      <xsl:when test="local-name($field) = 'OperatorMethod'"/>

      <xsl:when test="$field">
	<xsl:text>&#10;</xsl:text>
	<xsl:call-template name="indent">
	  <xsl:with-param name="count" select="$indent"/>
	</xsl:call-template>
	<xsl:text>(field (</xsl:text>
	<xsl:choose>
	  <xsl:when test="not($field-type/@name)">
	    <xsl:text>&#10;</xsl:text>
	    <xsl:call-template name="indent">
	      <xsl:with-param name="count" select="$indent+1"/>
	    </xsl:call-template>
	    <xsl:choose>
	      <xsl:when test="
		   local-name($field-type) = 'PointerType'
		or local-name($field-type) = 'FundamentalType'
		or local-name($field-type) = 'CvQualifiedType'
		or local-name($field-type) =  'ArrayType'
		">
		<xsl:call-template name="c-type-string">
		  <xsl:with-param name="c-type-id" select="/GCC_XML/Field[@id = $field-id]/@type"/>
		</xsl:call-template>
	      </xsl:when>
	      <xsl:otherwise>
		<xsl:apply-templates select="$field-type"/>
	      </xsl:otherwise>
	    </xsl:choose>
	    <xsl:text>&#10;</xsl:text>
	  </xsl:when>
	  <xsl:otherwise>
	    <xsl:call-template name="c-type-string">
	      <xsl:with-param name="c-type-id" select="/GCC_XML/Field[@id = $field-id]/@type"/>
	    </xsl:call-template>
	  </xsl:otherwise>
	</xsl:choose>
	<xsl:text>) </xsl:text>

	<xsl:if test="/GCC_XML/Field[@id = $field-id]/@name = ''">
	  <!-- Field has not defined name -->
	  <xsl:text>unknown</xsl:text>
	  <xsl:value-of select="/GCC_XML/Field[@id = $field-id]/@id"/>
	</xsl:if>
	<xsl:value-of select="/GCC_XML/Field[@id = $field-id]/@name"/>

	<xsl:text> :offset </xsl:text>
	<xsl:value-of select="/GCC_XML/Field[@id = $field-id]/@offset"/>

	<xsl:text>)</xsl:text>
      </xsl:when>

      <xsl:otherwise>
	<xsl:message>
	  <xsl:text>&#10;#Error: find-field: Unknown error for id </xsl:text>
	  <xsl:value-of select="$field-id"/>
	</xsl:message>
      </xsl:otherwise>
    </xsl:choose>
  </xsl:template>



  <!--- ========== indent ================== -->
  <xsl:template name="indent">
    <xsl:param name="count" select="1"/>

    <xsl:if test="$count > 0">
      <xsl:text>&#09;</xsl:text>
      <xsl:call-template name="indent">
	<xsl:with-param name="count" select="$count - 1"/>
      </xsl:call-template>
    </xsl:if>
  </xsl:template>


</xsl:stylesheet>