Browse Source

ig-c99: extract lm-c99-api and refactoring

main
heck 2 years ago
parent
commit
c657a2fc3c
  1. 301
      lm-c99/ig-c99.ysl2

301
lm-c99/ig-c99.ysl2

@ -8,6 +8,119 @@ tstylesheet {
include standardlib.ysl2 include standardlib.ysl2
include idl_api.ysl2 include idl_api.ysl2
// ----------------------------------------------------------------------
// pEpACIDLang LM-C99-API
// The C99 Language mapping API
// ----------------------------------------------------------------------
def "func:c99_typename_of_basetype" {
param "idltype";
choose {
when "func:idl_type_is_defined($idltype)"
choose {
when "$idltype = 'string'"
result > char*
when "$idltype = 'binary'"
result > char*
when "$idltype = 'uint'"
result > unsigned int
when "$idltype = 'size'"
result > size_t
when "$idltype = 'opaque'"
result > void*
when "$idltype = 'int'"
result > int
when "$idltype = 'bool'"
result > bool
otherwise
error > func:c99_typename_of_basetype - basetype not supported by ig-c99: «$idltype»
}
otherwise
error > func:c99_typename_of_basetype - idltype: «$idltype» is not defined
}
}
def "func:c99_typename_of_enum" {
param "idl_enum_name";
result "yml:ucase(concat(func:idl_get_package_name(), '_', $idl_enum_name))";
}
def "func:c99_typename_of_struct" {
param "idl_struct_name";
result > «yml:capit(func:idl_get_package_name())»_«yml:capit($idl_struct_name)»;
}
def "func:c99_typename_of_idltype" {
param "idltype";
const "idl_typekind", "func:idl_type_get_typekind_of_type($idltype)";
choose {
when "func:idl_type_is_defined($idltype)"
choose {
when "$idl_typekind = 'basetype'"
result "func:c99_typename_of_basetype($idltype)";
when "$idl_typekind = 'deftype'"
result "$idltype";
when "$idl_typekind = 'enum'"
result "func:c99_typename_of_enum($idltype)";
when "$idl_typekind = 'struct'"
result "func:c99_typename_of_struct($idltype)";
when "$idl_typekind = 'generic'"
error > c99_typename_of_type - idltype «$idltype» is a generic type and can only be used in deftype declarations.
otherwise
error > c99_typename_of_type - cant determine c99 typename for idltype: «$idltype»
}
otherwise
error > c99_typename_of_idltype - is not an idltype: «$idltype»
}
}
// typeclass is either
// * primitive
// * object
def "func:c99_typeclass_of_idltype" {
param "idltype";
const "resolved_type", "func:idl_type_get_resolved_type($idltype)";
const "resolved_typekind", "func:idl_type_get_typekind_of_type($resolved_type)";
choose {
when "func:idl_type_is_defined($idltype)"
choose {
when "$resolved_typekind = 'basetype'"
choose {
when "$resolved_type = 'string'"
result > object
when "$resolved_type = 'binary'"
result > object
otherwise
result > primitive
}
when "$resolved_typekind = 'enum'"
result > primitive
when "$resolved_typekind = 'struct'"
result > object
when "$resolved_typekind = 'generic'"
result > object
otherwise
error > c99_typeclass_of_idltype - cant determine typeclass of: «$idltype»
}
otherwise
error > c99_typeclass_of_idltype - is not an idltype: «$idltype»
}
};
def "func:c99_itemname_of_enumitem" {
param "idl_enum_name";
param "idl_item_name";
result "yml:ucase(concat($idl_enum_name, '_', $idl_item_name))";
}
// ---------------------------------------------------------------------- // ----------------------------------------------------------------------
// pEpACIDLang IG-C99 // pEpACIDLang IG-C99
// The C99 Interface Generator for pEpACIDLang // The C99 Interface Generator for pEpACIDLang
@ -37,46 +150,31 @@ tstylesheet {
template "basetypes" { template "basetypes" {
for "*" { for "*" {
const "name", "name()"; const "name", "name()";
if "func:c99_typename_of_basetype($name) != $name" { if "func:c99_typename_of_idltype($name) != $name" {
> «func:typedef(func:c99_typename_of_basetype($name), $name)» | «func:typedef(func:c99_typename_of_idltype($name), $name)»
} }
} }
} }
template "api" document "ig-c99/{@name}_api.h", "text" { template "api" document "ig-c99/{@name}_api.h", "text" {
const "pkg_name", "../@name";
const "pkg_version", "../version/text()";
const "pkg_license", "../license/text()";
const "pkg_copyleft", "../copyleft/text()";
const "api_name", "@name"; const "api_name", "@name";
|| ||
// «yml:ucase($pkg_name)» «$pkg_version» // «yml:ucase(func:idl_get_package_name())» version «func:idl_get_package_version()»
// «$api_name»_api.h // «$api_name»_api.h
// «$pkg_license» // «func:idl_get_package_license()»
// «$pkg_copyleft»
#pragma once #pragma once
#ifdef __cplusplus #ifdef __cplusplus
extern "C" { extern "C" {
#endif #endif
#include "idl_core.h" #include "idl_core.h"
// deftypes ``call "deftypes"
// --------
``apply "deftype", 0
``call "enums" ``call "enums"
``call "structs"
// Structs ``call "methods"
// -------
``apply "struct", 0
// Functions
// ---------
``apply "method", 0
#ifdef __cplusplus #ifdef __cplusplus
} // extern "C" } // extern "C"
@ -86,110 +184,46 @@ tstylesheet {
def "func:c99_typename_of_basetype" { // DEFTYPE
param "idltype"; // -------
// TODO: test for is_basetype function "deftypes" {
choose { ||
when "$idltype = 'string'" // Deftypes
result > char* // --------
when "$idltype = 'binary'"
result > char*
when "$idltype = 'uint'"
result > unsigned int
when "$idltype = 'size'"
result > size_t
when "$idltype = 'opaque'"
result > void*
when "$idltype = 'int'"
result > int
when "$idltype = 'bool'"
result > bool
otherwise
error > func:c99_typename_of_basetype - basetype not supported by ig-c99: «$idltype»
}
}
def "func:c99_typename_of_enum" {
param "name";
param "pkg_name";
result "yml:ucase(concat($pkg_name, '_', $name))"; ``apply "deftype"
}
def "func:c99_itemname_of_enumitem" { ||
param "enum_name";
param "idl_item_name";
result "yml:ucase(concat($enum_name, '_', $idl_item_name))";
} }
def "func:c99_typename_of_struct" {
param "pkg_name";
param "idl_name";
result "concat(yml:capit($pkg_name), '_', yml:capit($idl_name))"; template "deftype" {
call "deftype"
with "new_typename", "@name",
with "aliased_type_or_typekind", "as/@type";
} }
// typeclass is either function "deftype" {
// * primitive param "new_typename";
// * object param "aliased_type_or_typekind";
def "func:c99_typeclass_of_type" {
param "idltype";
const "resolved_type", "func:idl_type_get_resolved_type($idltype)";
const "resolved_typekind", "func:idl_type_get_typekind_of_type($resolved_type)";
choose {
when "func:idl_type_is_defined($idltype)"
choose {
when "$resolved_typekind = 'basetype'"
choose {
when "$resolved_type = 'string'"
result > object
when "$resolved_type = 'binary'"
result > object
otherwise
result > primitive
}
when "$resolved_typekind = 'enum'"
result > primitive
when "$resolved_typekind = 'struct'"
result > object
when "$resolved_typekind = 'generic'"
result > object
otherwise
error > c99_typeclass_of_type - cant determine typeclass of: «$idltype»
}
otherwise
error > c99_typeclass_of_type - is not a type: «$idltype»
}
};
// TYPEALIAS
// ---------
template "deftype" {
const "alias_name", "@name";
const "type_or_typekind", "as/@type";
choose { choose {
when 'func:idl_typekind_is_generic($type_or_typekind)' when 'func:idl_typekind_is_generic($aliased_type_or_typekind)'
choose { choose {
when "$type_or_typekind = 'list'" when "$aliased_type_or_typekind = 'list'"
> «func:typedef($type_or_typekind, $alias_name)» | «func:typedef($aliased_type_or_typekind, $new_typename)»
otherwise otherwise
error > deftype - typekind must be generic «$type_or_typeind» error > deftype - typekind must be generic «$type_or_typeind»
} }
otherwise otherwise
> «func:typedef($type_or_typekind, $alias_name)» | «func:typedef($aliased_type_or_typekind, $new_typename)»
} }
} }
def "func:typedef" { def "func:typedef" {
param "is_type"; param "is_type";
param "alias_name"; param "new_typename";
result > typedef «$is_type» «$alias_name»;\n result > typedef «$is_type» «$new_typename»;
} }
@ -203,19 +237,17 @@ tstylesheet {
// ----- // -----
``apply "enum" ``apply "enum"
|| ||
} }
template "enum" { template "enum" {
call "enum" call "enum" with "idl_enum_name", "@name";
with "idl_enum_name", "@name",
with "idl_pkg_name", "../../@name";
} }
function "enum" { function "enum" {
param "idl_enum_name"; param "idl_enum_name";
param "idl_pkg_name"; const "enum_name", "func:c99_typename_of_idltype($idl_enum_name)";
const "enum_name", "func:c99_typename_of_enum($idl_enum_name, $idl_pkg_name)";
|| ||
typedef enum _«$enum_name» { typedef enum _«$enum_name» {
``apply "item" with "enum_name", "$enum_name" ``apply "item" with "enum_name", "$enum_name"
@ -229,13 +261,13 @@ tstylesheet {
const "idl_item_name", "@name"; const "idl_item_name", "@name";
const "c99_item_name", "func:c99_itemname_of_enumitem($enum_name, $idl_item_name)"; const "c99_item_name", "func:c99_itemname_of_enumitem($enum_name, $idl_item_name)";
const "idl_value", "text()"; const "idl_value", "text()";
const "c99_value", "func:enum_item_value($idl_value)"; const "c99_value", "func:c99_enum_item_value($idl_value)";
apply "@doc", 0, mode="multiline"; apply "@doc", 0, mode="multiline";
| «$c99_item_name»«$c99_value»«func:autocomma()»`apply "@doc" , 0, mode="oneline"` | «$c99_item_name»«$c99_value»«func:autocomma()»`apply "@doc" , 0, mode="oneline"`
} }
def "func:enum_item_value" { def "func:c99_enum_item_value" {
param "idl_value"; param "idl_value";
choose { choose {
when "string-length($idl_value) > 0" when "string-length($idl_value) > 0"
@ -243,7 +275,6 @@ tstylesheet {
otherwise otherwise
result > result >
} }
} }
// STRUCT // STRUCT
@ -255,17 +286,22 @@ tstylesheet {
// ------- // -------
``apply "struct" ``apply "struct"
|| ||
} }
template "struct" { template "struct" {
const "idl_name", "@name"; call "struct" with "idl_struct_name", "@name";
const "pkg_name", "../../@name"; }
const "c_name", "func:c99_typename_of_struct($pkg_name, $idl_name)";
function "struct" {
param "idl_struct_name";
const "c99_struct_name", "func:c99_typename_of_idltype($idl_struct_name)";
|| ||
typedef struct _«$c_name» { typedef struct _«$c99_struct_name» {
``apply "field" ``apply "field"
} «$c_name»; } «$c99_struct_name»;
|| ||
} }
@ -282,10 +318,25 @@ tstylesheet {
// METHOD // METHOD
// ------ // ------
function "methods" {
||
// Functions
// ---------
``apply "method"
||
}
template "method" { template "method" {
const "name", "@name"; call "method"
with "idl_method_name", "@name";
}
function "method" {
param "idl_method_name";
|| ||
PEP_STATUS «$name»( PEP_STATUS «$idl_method_name»(
``apply "param", 0 ``apply "param", 0
); );
@ -302,7 +353,7 @@ tstylesheet {
def "func:idlparam_to_cparam" { def "func:idlparam_to_cparam" {
param "idltype"; param "idltype";
param "mode"; param "mode";
const "typeclass", "func:c99_typeclass_of_type($idltype)"; const "typeclass", "func:c99_typeclass_of_idltype($idltype)";
choose { choose {
when "$mode = 'borrow'" when "$mode = 'borrow'"

Loading…
Cancel
Save