Skip to content

Commit

Permalink
Merge branch 'feature/cleaned-up-type-references' into develop
Browse files Browse the repository at this point in the history
  • Loading branch information
sruehl committed Feb 25, 2022
2 parents ec0694a + 34d0887 commit 08dbabb
Show file tree
Hide file tree
Showing 226 changed files with 6,096 additions and 5,846 deletions.

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -102,16 +102,13 @@ public void generate(File outputDir, String languageName, String protocolName, S
} else if (typeEntry.getValue() instanceof DataIoTypeDefinition) {
templateList = dataIoTemplateList;
} else {
// Skip outputting the sub-types of io-types.
if (typeEntry.getValue().getParentType() instanceof DataIoTypeDefinition) {
continue;
}
templateList = complexTypesTemplateList;
}

// Generate the output for the given type.
LOGGER.info("Generating type {}", typeEntry.getKey());
for (Template template : templateList) {
LOGGER.debug("Applying template {}", template.getName());
try {
renderTemplate(outputDir, template, typeContext);
} catch (IOException | TemplateException e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -120,9 +120,8 @@ public String getLanguageTypeNameForField(Field field) {
}
TypedField typedField = field.asTypedField().orElseThrow(IllegalStateException::new);
TypeReference typeReference = typedField.getType();
if (typeReference.isComplexTypeReference()) {
final TypeDefinition typeDefinition = getTypeDefinitionForTypeReference(typeReference);
if (typeDefinition instanceof DataIoTypeDefinition) {
if (typeReference.isNonSimpleTypeReference()) {
if (typeReference.asNonSimpleTypeReference().orElseThrow().getTypeDefinition() instanceof DataIoTypeDefinition) {
return "plc4c_data*";
}
}
Expand Down Expand Up @@ -255,7 +254,7 @@ public String getLanguageTypeNameForTypeReference(TypeReference typeReference) {
}
throw new FreemarkerException("Unsupported simple type. " + simpleTypeReference.getBaseType());
} else {
return getCTypeName(((ComplexTypeReference) typeReference).getName());
return getCTypeName(((NonSimpleTypeReference) typeReference).getName());
}
}

Expand Down Expand Up @@ -323,8 +322,8 @@ public String escapeValue(TypeReference typeReference, String valueString) {
}
if ("null".equals(valueString)) {
// C doesn't like NULL values for enums, so we have to return something else (we'll treat -1 as NULL)
if (typeReference instanceof ComplexTypeReference) {
if (getTypeDefinitionForTypeReference(typeReference) instanceof EnumTypeDefinition) {
if (typeReference.isNonSimpleTypeReference()) {
if (typeReference.asNonSimpleTypeReference().orElseThrow().getTypeDefinition().isEnumTypeDefinition()) {
return "-1";
}
}
Expand Down Expand Up @@ -513,8 +512,8 @@ public String getNullValueForTypeReference(TypeReference typeReference) {
throw new FreemarkerException("Unsupported type.");
}
} else {
ComplexTypeReference complexTypeReference = (ComplexTypeReference) typeReference;
return getCTypeName(complexTypeReference.getName()) + "_null()";
NonSimpleTypeReference nonSimpleTypeReference = (NonSimpleTypeReference) typeReference;
return getCTypeName(nonSimpleTypeReference.getName()) + "_null()";
}
}

Expand Down Expand Up @@ -640,14 +639,18 @@ private String toVariableLiteralExpression(TypeDefinition baseType, VariableLite
String lengthExpression;
if (variableLiteral.getName().equals("lengthInBytes")) {
tracer = tracer.dive("lengthInBytes contained in variable name");
lengthType = (baseType.getParentType() == null) ? baseType : (ComplexTypeDefinition) baseType.getParentType();
lengthType = baseType;
Optional<ComplexTypeDefinition> parentType = baseType.asComplexTypeDefinition()
.flatMap(ComplexTypeDefinition::getParentType);
if (parentType.isPresent()) {
lengthType = parentType.get();
}
lengthExpression = "_message";
} else {
final Optional<TypeReference> typeReferenceForProperty = ((ComplexTypeDefinition) baseType).getTypeReferenceForProperty(variableLiteral.getName());
if (typeReferenceForProperty.isEmpty()) {
throw new FreemarkerException("Unknown type for property " + variableLiteral.getName());
}
lengthType = getTypeDefinitionForTypeReference(typeReferenceForProperty.get());
final TypeReference typeReferenceForProperty = ((ComplexTypeDefinition) baseType)
.getTypeReferenceForProperty(variableLiteral.getName())
.orElseThrow(()->new FreemarkerException("Unknown type for property " + variableLiteral.getName()));
lengthType = typeReferenceForProperty.asNonSimpleTypeReference().orElseThrow().getTypeDefinition();
lengthExpression = variableExpressionGenerator.apply(variableLiteral);
}
return tracer + getCTypeName(lengthType.getName()) + "_length_in_bytes(" + lengthExpression + ")";
Expand Down Expand Up @@ -721,8 +724,15 @@ public String toVariableParseExpression(TypeDefinition baseType, Field field, Va
return name;
}

if (propertyType instanceof ArrayTypeReference) {
if (variableLiteral.getChild().isPresent()) {
throw new FreemarkerException("array property '" + name + "' doesn't have child properties.");
}
return name;
}

// If it references a complex, type we need to get that type's definition first.
final TypeDefinition propertyTypeDefinition = getTypeDefinitions().get(((ComplexTypeReference) propertyType).getName());
final TypeDefinition propertyTypeDefinition = getTypeDefinitions().get(((NonSimpleTypeReference) propertyType).getName());
// If we're not accessing any child property, no need to handle anything special.
if (variableLiteral.getChild().isEmpty()) {
return name;
Expand Down Expand Up @@ -833,15 +843,16 @@ private String toCastVariableParseExpression(TypeDefinition baseType, Field fiel
// access next.
StringBuilder sb = new StringBuilder();
sb.append("((");
if (castType.getParentType() != null) {
sb.append(getCTypeName(castType.getParentType().getName()));
Optional<ComplexTypeDefinition> potentialParentType = castType.asComplexTypeDefinition().flatMap(ComplexTypeDefinition::getParentType);
if (potentialParentType.isPresent()) {
sb.append(getCTypeName(potentialParentType.get().getName()));
} else {
sb.append(getCTypeName(castType.getName()));
}
sb.append("*) (");
sb.append(toVariableParseExpression(baseType, field, firstArgument, parserArguments)).append("))");
if (variableLiteral.getChild().isPresent()) {
if (castType.getParentType() != null) {
if (potentialParentType.isPresent()) {
// Change the name of the property to contain the sub-type-prefix.
sb.append("->").append(camelCaseToSnakeCase(castType.getName())).append("_");
appendVariableExpressionRest(sb, baseType, variableLiteral.getChild().get());
Expand Down Expand Up @@ -976,9 +987,8 @@ private String toVariableSerializationExpression(TypeDefinition baseType, Field
((ComplexTypeDefinition) baseType).getTypeReferenceForProperty(variableLiteral.getName());
if (typeReferenceForProperty.isPresent()) {
final TypeReference typeReference = typeReferenceForProperty.get();
if (typeReference instanceof ComplexTypeReference) {
final TypeDefinition typeDefinitionForTypeReference =
getTypeDefinitionForTypeReference(typeReference);
if (typeReference instanceof NonSimpleTypeReference) {
final TypeDefinition typeDefinitionForTypeReference = typeReference.asNonSimpleTypeReference().orElseThrow().getTypeDefinition();
if ((typeDefinitionForTypeReference instanceof EnumTypeDefinition) && (variableLiteral.getChild().isPresent())) {
tracer = tracer.dive("is enum type definition");
sb.append(camelCaseToSnakeCase(variableLiteral.getName()));
Expand Down Expand Up @@ -1175,12 +1185,16 @@ public int getNumBits(SimpleTypeReference simpleTypeReference) {
}

public String getLengthInBitsFunctionNameForComplexTypedField(Field field) {
return field.asTypedField()
TypeReference typeReference = field.asTypedField()
.map(TypedField::getType)
.orElseThrow(() -> new FreemarkerException("lengthInBits functions only exist for TypedFields"))
.asComplexTypeReference()
.map(complexTypeReference -> getCTypeName(complexTypeReference.getName()) + "_length_in_bits")
.orElseThrow(() -> new FreemarkerException("lengthInBits functions only exist for complex types"));
.orElseThrow(() -> new FreemarkerException("lengthInBits functions only exist for TypedFields"));
if (typeReference.isArrayTypeReference()){
typeReference = typeReference.asArrayTypeReference().orElseThrow().getElementTypeReference();
}
return typeReference
.asNonSimpleTypeReference()
.map(nonSimpleTypeReference -> getCTypeName(nonSimpleTypeReference.getName()) + "_length_in_bits")
.orElseThrow(() -> new FreemarkerException("lengthInBits functions only exist for non simple type references"));
}

public String getEnumExpression(String expression) {
Expand Down

0 comments on commit 08dbabb

Please sign in to comment.