Code section items#

CodeElement subclasses#

Each code section item is represented by one of the following classes, which all inherit from CodeElement, thus provide the get_model_element() method to access the associated model element. (see Model section items)

Functions and Parameters

Variables and Macros

Type Definitions

Code Container#

A CodeContainer represents a code unit associated with a pair of C files:

  • an interface file (.h header)

  • an optional implementation file (.c source)

Each container holds a list of CDeclaration items (functions, variables, types, macros, …) that are declared or defined in those files.

A container can also be marked as user-provided, meaning it needs to be provided by the user.

The full set of containers is accessible from get_all_code_containers().

API Documentation#

Code module for Swan Code Generator mapping data.

This module implements classes representing C code elements generated from Swan language. Each code item has a unique integer ID and can reference other code items or model items by their IDs.

class ansys.scadeone.core.svc.cgmapping.code.CArray(id: int, name: str, base_type: int, size: int, copy: int | None = None, equality: int | None = None, equality_use: str | None = None)#

Bases: CodeTypeBase

C array type definition.

Parameters:
idint

Unique identifier for this array type.

namestr

Name of the array type.

base_typeint

Reference ID to the base type of the array.

sizeint

Size of the array.

copyint | None

Reference ID to copy function (if any).

equalityint | None

Reference ID to equality function (if any).

equality_usestr | None

Usage string for equality function.

class ansys.scadeone.core.svc.cgmapping.code.CEnum(id: int, name: str, values: list[CEnumValue])#

Bases: CodeElement

C enum type definition.

Parameters:
idint

Unique identifier for this enum type.

namestr

Name of the enum type.

valueslist[CEnumValue]

List of enum values.

class ansys.scadeone.core.svc.cgmapping.code.CEnumValue(id: int, name: str, user_value: str | None = None)#

Bases: CodeElement

C enum type value.

Parameters:
idint

Unique identifier for this enum value.

namestr

Name of the enum value.

user_valuestr | None

User-defined constant expression for the enum value.

class ansys.scadeone.core.svc.cgmapping.code.CExternalType(id: int, name: str, scalar: bool | None = None, init: int | None = None, copy: int | None = None, equality: int | None = None)#

Bases: CodeTypeBase

C external type.

Parameters:
idint

Unique identifier for this external type.

namestr

Name of the external type.

scalarbool | None

Whether this is a scalar type.

initint | None

Reference ID to initialization function (if any).

copyint | None

Reference ID to copy function (if any).

equalityint | None

Reference ID to equality function (if any).

class ansys.scadeone.core.svc.cgmapping.code.CFunction(id: int, name: str, parameters: list[CParameter], return_type: int | None = None)#

Bases: CodeElement

C function declaration.

Parameters:
idint

Unique identifier for this function.

namestr

Name of the function.

return_typeint | None

Reference ID to the return type (None for void).

parameterslist[CParameter]

List of function parameters.

class ansys.scadeone.core.svc.cgmapping.code.CGlobal(id: int, name: str, type: int)#

Bases: CodeElement

C global variable.

Parameters:
idint

Unique identifier for this global variable.

namestr

Name of the global variable.

typeint

Reference ID to the type of the variable.

class ansys.scadeone.core.svc.cgmapping.code.CMacro(id: int, name: str)#

Bases: CodeElement

C macro definition.

Parameters:
idint

Unique identifier for this macro.

namestr

Name of the macro.

class ansys.scadeone.core.svc.cgmapping.code.CParameter(id: int, name: str, type: int, pointer: bool | None = None, const: bool | None = None)#

Bases: CodeElement

C function parameter.

Parameters:
idint

Unique identifier for this parameter.

namestr

Name of the parameter.

typeint

Reference ID to the type of the parameter.

pointerbool | None

Whether this parameter is a pointer.

constbool | None

Whether this parameter is const.

class ansys.scadeone.core.svc.cgmapping.code.CPredefinedType(id: int, name: str)#

Bases: CodeElement

C predefined type.

Parameters:
idint

Unique identifier for this type.

namestr

Name of the predefined type (e.g., int, float, double).

class ansys.scadeone.core.svc.cgmapping.code.CStruct(id: int, name: str, fields: list[CStructField], copy: int | None = None, equality: int | None = None, equality_use: str | None = None)#

Bases: CodeTypeBase

C structure type definition.

Parameters:
idint

Unique identifier for this struct type.

namestr

Name of the struct type.

fieldslist[CStructField]

List of fields in the structure.

copyint | None

Reference ID to copy function (if any).

equalityint | None

Reference ID to equality function (if any).

equality_usestr | None

Usage string for equality function.

class ansys.scadeone.core.svc.cgmapping.code.CStructField(id: int, name: str, type: int, pointer: bool | None = None, size: list[int] | None = None)#

Bases: CodeElement

C structure field.

Parameters:
idint

Unique identifier for this field.

namestr

Name of the field.

typeint

Reference ID to the type of the field.

pointerbool | None

Whether this field is a pointer.

sizelist[int] | None

Array dimensions if this field is an array.

get_model_element() → ModelElement | None#

Get the model element for this code element.

Returns:
ModelElement | None

The model element mapped to this code element, or None if not found.

Raises:
ValueError

If no cgmapping is set on this instance.

AttributeError

If the class does not have an ‘id’ attribute.

class ansys.scadeone.core.svc.cgmapping.code.CTypedef(id: int, name: str, type: int, copy: int | None = None, equality: int | None = None)#

Bases: CodeTypeBase

C typedef definition.

Parameters:
idint

Unique identifier for this typedef.

namestr

Name of the typedef.

typeint

Reference ID to the underlying type.

copyint | None

Reference ID to copy function (if any).

equalityint | None

Reference ID to equality function (if any).

class ansys.scadeone.core.svc.cgmapping.code.CUnion(id: int, name: str, variants: list[CUnionVariant], copy: int | None = None, equality: int | None = None, equality_use: str | None = None)#

Bases: CodeTypeBase

C union type definition.

Parameters:
idint

Unique identifier for this union type.

namestr

Name of the union type.

variantslist[CUnionVariant]

List of union variants.

copyint | None

Reference ID to copy function (if any).

equalityint | None

Reference ID to equality function (if any).

equality_usestr | None

Usage string for equality function.

class ansys.scadeone.core.svc.cgmapping.code.CUnionVariant(id: int, name: str, enum_value: int, type: int)#

Bases: CodeElement

C union type variant.

Parameters:
idint

Unique identifier for this variant.

namestr

Name of the variant.

enum_valueint

Reference ID to the corresponding enum value.

typeint

Reference ID to the type of this variant.

class ansys.scadeone.core.svc.cgmapping.code.CodeContainer(interface_file: str, declarations: list[CDeclaration], implementation_file: str | None = None, user_provided: bool | None = None)#

Bases: object

Generated code item with interface file, optional implementation file, and declarations.

class ansys.scadeone.core.svc.cgmapping.code.CodeElement#

Bases: object

Base class for all C code elements.

This class provides common functionality for all code elements, including access to their corresponding model elements through the mapping system.

All code elements that inherit from this class must have an ‘id’ attribute.

get_model_element() → ModelElement | None#

Get the model element for this code element.

Returns:
ModelElement | None

The model element mapped to this code element, or None if not found.

Raises:
ValueError

If no cgmapping is set on this instance.

AttributeError

If the class does not have an ‘id’ attribute.

get_type() → CodeTypeBase | None#

Get the type of this code element if it has a type attribute.

Returns:
CodeTypeBase | None

The code type if this element has a type, or None otherwise.

Raises:
ValueError

If no cgmapping is set on this instance.

class ansys.scadeone.core.svc.cgmapping.code.CodeTypeBase#

Bases: CodeElement

Base class for code elements that represent types.

get_copy_function_name() → str | None#

Get copy function name if it exists.

Returns:
str | None

The name of the copy function, None if not found.

Raises:
ValueError

If no cgmapping is set on this instance.

get_equality_function() → CFunction | None#

Get the equality function for this type if it exists.

Returns:
CFunction | None

The equality CFunction, or None if not found.

Raises:
ValueError

If no cgmapping is set on this instance.

get_equality_use_macro() → str | None#

Get the equality use macro string for this type if it exists. It is the name of the C macro that conditionates equality function compilation. CArray, CStruct, and `CUnion types can have an equality use macro. CExternalType and CTypedef don’t have equality use macro thus always return None.

Returns:
str | None

The equality use macro string if it exists.

type ansys.scadeone.core.svc.cgmapping.code.CDeclaration = tuple[str, CFunction | CGlobal | CMacro | CPredefinedType | CStruct | CArray | CEnum | CUnion | CTypedef | CExternalType]#

Type alias for C declarations (discriminated union)