Skip to content

#770 PLCgen: Introduce declared types.

Albert Hofkamp requested to merge 770-introduce-declared-type into develop

The desire exists to compute types inside expression context. For this to happen, type information in expressions must be complete enough to do that. Eg, projection on a tuple type should be computable in a simple way, enum literals need to know their enumeration type.
Currently that is not the case (for structures and enumerations in particular here).


  • PlcEnumType and PlcStructType exist. They are a PlcTypeand contain the definition. They do not know their name that is used in the type declaration. Conversion to Box produces the definition.
  • PlcTypeDeclaration is not a type. It contains a type-def name and the type being used (ie Plc{Enum,Struct}Type). It connect the name of the type to its definition. Conversion to Box creates a type definition (printing the name, and delegating printing the definition to the type thay have).
  • PlcDerivedType is a type. It only has name (the type name). Conversion to Box produces the name.

Currently, PlcDerivedType is used as type for enumerations and structures. It nicely prints the name of the definition.
For computing types in expressions however, it's completely useless. It doesn't have the associated definition, it doesn't even know if the name represents an enumeration or a structure type. Instead, Plc{Enum,Struct}Type should be available in the expression. You can derive that information through the type generator, but that is awkward and complicated.

From that idea, this follows:

  • Plc{Enum,Struct}Typeshould be extended with knowledge about their type name, so they can use that as default way to express their type. Full expansion of the their definition becomes a special case in generating type definitions.
  • Since any type may want to have a type definition, something is needed to express that. This lead to an empty PlcDerivedType interface.
  • With types knowing their own type name, PlcDerivedType becomes useless in PLCgen.
  • PlcTypeDecl Is solely used to generate the type definition text, but since the types themselves now know both their type name and their definition in such cases, PlcTypeDecl has no added value any more either.

Steps done in the patch (Each of the 4 steps in one commit.):

  1. Introduce PlcDeclaredType, apply it to PlcTypeDecl.
  2. Rework PlcStructType.
  3. Rework PlcEnumType
  4. Delete PlcTypeDecl. (PlcDerivedType is still used in function block context, but I expect that to change when improving function blocks handling in expressions and types.)

For 2 and 3 the steps are (and it's likely useful to read the commit in that order):

  • Extend the type.
  • Drop the useless "type name to definition" method in the TypeGenerator.
  • Adapt DefaultTypeGenerator. Generally it drops the "name to type" map, and the code that generates that.
  • Extend the writers to print the type definition of the extended type. Mostly copy/paste of PlcTypeDecl printing code for the specific extended type.
  • Fix Unit tests. (Expression generator test.)

Output of the oee.cif.tests doesn't change.

Addresses #770

Edited by Dennis Hendriks

Merge request reports