Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
The diff you're trying to view is too large. We only load the first 3000 changed files.
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import freemarker.cache.ClassTemplateLoader;
import freemarker.template.*;
import org.apache.plc4x.plugins.codegenerator.language.LanguageOutput;
import org.apache.plc4x.plugins.codegenerator.types.definitions.ConstantsTypeDefinition;
import org.apache.plc4x.plugins.codegenerator.types.definitions.EnumTypeDefinition;
import org.apache.plc4x.plugins.codegenerator.types.definitions.DataIoTypeDefinition;
import org.apache.plc4x.plugins.codegenerator.types.definitions.TypeDefinition;
Expand All @@ -31,10 +32,7 @@
import java.io.*;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.*;

public abstract class FreemarkerLanguageOutput implements LanguageOutput {

Expand All @@ -53,13 +51,15 @@ public void generate(File outputDir, String version, String languageName, String
freemarkerConfiguration.setTemplateLoader(classTemplateLoader);

// Initialize all templates
List<Template> specTemplates;
List<Template> specTemplatesList;
List<Template> constantsTemplateList;
List<Template> complexTypesTemplateList;
List<Template> enumTypesTemplateList;
List<Template> dataIoTemplateList;
List<Template> miscTemplateList;
try {
specTemplates = getSpecTemplates(freemarkerConfiguration);
specTemplatesList = getSpecTemplates(freemarkerConfiguration);
constantsTemplateList = getConstantsTemplates(freemarkerConfiguration);
complexTypesTemplateList = getComplexTypeTemplates(freemarkerConfiguration);
enumTypesTemplateList = getEnumTypeTemplates(freemarkerConfiguration);
dataIoTemplateList = getDataIoTemplates(freemarkerConfiguration);
Expand All @@ -69,7 +69,7 @@ public void generate(File outputDir, String version, String languageName, String
}

// Generate output that's global for the entire mspec
if (!specTemplates.isEmpty()) {
if (!specTemplatesList.isEmpty()) {
Map<String, Object> typeContext = new HashMap<>();
typeContext.put("languageName", languageName);
typeContext.put("protocolName", protocolName);
Expand All @@ -78,7 +78,7 @@ public void generate(File outputDir, String version, String languageName, String
typeContext.put("tracer", Tracer.start("global"));
typeContext.putAll(options);

for (Template template : specTemplates) {
for (Template template : specTemplatesList) {
try {
renderTemplate(outputDir, template, typeContext);
} catch (IOException | TemplateException e) {
Expand All @@ -87,8 +87,36 @@ public void generate(File outputDir, String version, String languageName, String
}
}

// Generate constants types
Optional<TypeDefinition> constantsTypeDefinition = types.values().stream().filter(type -> type instanceof ConstantsTypeDefinition).findFirst();
if (!constantsTemplateList.isEmpty() && constantsTypeDefinition.isPresent()) {
TypeDefinition typeDefinition = constantsTypeDefinition.get();
Map<String, Object> typeContext = new HashMap<>();
typeContext.put("languageName", languageName);
typeContext.put("protocolName", protocolName);
typeContext.put("outputFlavor", outputFlavor);
typeContext.put("typeName", typeDefinition.getName());
typeContext.put("type", typeDefinition);
typeContext.put("helper", getHelper(typeDefinition, protocolName, outputFlavor, types, options));
typeContext.put("tracer", Tracer.start("global"));
typeContext.putAll(options);

for (Template template : constantsTemplateList) {
try {
renderTemplate(outputDir, template, typeContext);
} catch (IOException | TemplateException e) {
throw new GenerationException("Error generating constants.", e);
}
}
}

// Iterate over the types and have content generated for each one
for (Map.Entry<String, TypeDefinition> typeEntry : types.entrySet()) {
// "Constants" types are handled separately.
if (typeEntry.getValue() instanceof ConstantsTypeDefinition) {
continue;
}

// Prepare a new generation context
Map<String, Object> typeContext = new HashMap<>();
typeContext.put("languageName", languageName);
Expand Down Expand Up @@ -202,6 +230,8 @@ private Configuration getFreemarkerConfiguration() throws GenerationException {

protected abstract List<Template> getSpecTemplates(Configuration freemarkerConfiguration) throws IOException;

protected abstract List<Template> getConstantsTemplates(Configuration freemarkerConfiguration) throws IOException;

protected abstract List<Template> getComplexTypeTemplates(Configuration freemarkerConfiguration) throws IOException;

protected abstract List<Template> getEnumTypeTemplates(Configuration freemarkerConfiguration) throws IOException;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,11 +53,20 @@ protected List<Template> getSpecTemplates(Configuration freemarkerConfiguration)
return Collections.emptyList();
}

@Override
protected List<Template> getConstantsTemplates(Configuration freemarkerConfiguration) throws IOException {
return Arrays.asList(
freemarkerConfiguration.getTemplate("templates/c/constants-template.h.ftlh"),
freemarkerConfiguration.getTemplate("templates/c/constants-template.c.ftlh")
);
}

@Override
protected List<Template> getComplexTypeTemplates(Configuration freemarkerConfiguration) throws IOException {
return Arrays.asList(
freemarkerConfiguration.getTemplate("templates/c/complex-type-template.h.ftlh"),
freemarkerConfiguration.getTemplate("templates/c/complex-type-template.c.ftlh"));
freemarkerConfiguration.getTemplate("templates/c/complex-type-template.c.ftlh")
);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -137,19 +137,25 @@ public String getLanguageTypeNameForField(Field field) {
return getLanguageTypeNameForTypeReference(typeReference);
}

public List<Pair<ConstField, ComplexTypeDefinition>> getAllConstFields() {
public List<Pair<ConstField, String>> getAllConstFields() {
// Note: a map is not an option here as ConstFields are duplicated
List<Pair<ConstField, ComplexTypeDefinition>> constFields = new LinkedList<>();
ComplexTypeDefinition complexTypeDefinition = (ComplexTypeDefinition) this.thisType;
complexTypeDefinition.getConstFields()
.forEach(constField -> constFields.add(Pair.of(constField, complexTypeDefinition)));
complexTypeDefinition.getSwitchField()
.map(SwitchField::getCases)
.ifPresent(discriminatedComplexTypeDefinitions ->
discriminatedComplexTypeDefinitions.forEach(switchCase ->
switchCase.getConstFields().forEach(constField -> constFields.add(Pair.of(constField, switchCase)))
)
);
List<Pair<ConstField, String>> constFields = new LinkedList<>();
if(this.thisType instanceof ComplexTypeDefinition) {
ComplexTypeDefinition complexTypeDefinition = (ComplexTypeDefinition) this.thisType;
complexTypeDefinition.getConstFields()
.forEach(constField -> constFields.add(Pair.of(constField, complexTypeDefinition.getName())));
complexTypeDefinition.getSwitchField()
.map(SwitchField::getCases)
.ifPresent(discriminatedComplexTypeDefinitions ->
discriminatedComplexTypeDefinitions.forEach(switchCase ->
switchCase.getConstFields().forEach(constField -> constFields.add(Pair.of(constField, switchCase.getName())))
)
);
} else if (this.thisType instanceof ConstantsTypeDefinition) {
ConstantsTypeDefinition constantsTypeDefinition = (ConstantsTypeDefinition) this.thisType;
constantsTypeDefinition.getConstFields()
.forEach(constField -> constFields.add(Pair.of(constField, constantsTypeDefinition.getName())));
}
return constFields;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,10 +98,10 @@ ${helper.getCTypeName(type.name)} ${helper.getCTypeName(type.name)}_null() {
// Constant values.
<#list helper.getAllConstFields() as entry>
<#assign constField=entry.key>
<#assign parentType=entry.value>
static const ${helper.getLanguageTypeNameForField(constField)} ${helper.getCTypeName(parentType.name)?upper_case}_${helper.camelCaseToSnakeCase(constField.name)?upper_case}_const = ${helper.toParseExpression(type, constField, constField.referenceValue, parserArguments)};
${helper.getLanguageTypeNameForField(constField)} ${helper.getCTypeName(parentType.name)?upper_case}_${helper.camelCaseToSnakeCase(constField.name)?upper_case}() {
return ${helper.getCTypeName(parentType.name)?upper_case}_${helper.camelCaseToSnakeCase(constField.name)?upper_case}_const;
<#assign parentTypeName=entry.value>
static const ${helper.getLanguageTypeNameForField(constField)} ${helper.getCTypeName(parentTypeName)?upper_case}_${helper.camelCaseToSnakeCase(constField.name)?upper_case}_const = ${helper.toParseExpression(type, constField, constField.referenceValue, parserArguments)};
${helper.getLanguageTypeNameForField(constField)} ${helper.getCTypeName(parentTypeName)?upper_case}_${helper.camelCaseToSnakeCase(constField.name)?upper_case}() {
return ${helper.getCTypeName(parentTypeName)?upper_case}_${helper.camelCaseToSnakeCase(constField.name)?upper_case}_const;
}
</#list>
</#if>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -122,8 +122,8 @@ ${helper.getCTypeName(type.name)}_discriminator ${helper.getCTypeName(type.name)
// Constant values.
<#list helper.getAllConstFields() as entry>
<#assign constField=entry.key>
<#assign parentType=entry.value>
${helper.getLanguageTypeNameForField(constField)} ${helper.getCTypeName(parentType.name)?upper_case}_${helper.camelCaseToSnakeCase(constField.name)?upper_case}();
<#assign parentTypeName=entry.value>
${helper.getLanguageTypeNameForField(constField)} ${helper.getCTypeName(parentTypeName)?upper_case}_${helper.camelCaseToSnakeCase(constField.name)?upper_case}();
</#list>
</#if>

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
<#--
Licensed to the Apache Software Foundation (ASF) under one
or more contributor license agreements. See the NOTICE file
distributed with this work for additional information
regarding copyright ownership. The ASF licenses this file
to you under the Apache License, Version 2.0 (the
"License"); you may not use this file except in compliance
with the License. You may obtain a copy of the License at

https://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing,
software distributed under the License is distributed on an
"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
KIND, either express or implied. See the License for the
specific language governing permissions and limitations
under the License.
-->
<#-- Prevent freemarker from escaping stuff -->
<#outputformat "undefined">
<#-- Declare the name and type of variables passed in to the template -->
<#-- @ftlvariable name="languageName" type="java.lang.String" -->
<#-- @ftlvariable name="protocolName" type="java.lang.String" -->
<#-- @ftlvariable name="outputFlavor" type="java.lang.String" -->
<#-- @ftlvariable name="helper" type="org.apache.plc4x.language.c.CLanguageTemplateHelper" -->
<#-- @ftlvariable name="tracer" type="org.apache.plc4x.plugins.codegenerator.protocol.freemarker.Tracer" -->
<#-- @ftlvariable name="type" type="org.apache.plc4x.plugins.codegenerator.types.definitions.ComplexTypeDefinition" -->
<#-- Declare the name and type of variables declared locally inside the template -->
<#-- @ftlvariable name="field" type="org.apache.plc4x.plugins.codegenerator.types.fields.Field" -->
${helper.getSourceDirectory()?replace(".", "/")}/${helper.camelCaseToSnakeCase(type.name)}.c
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

#include <stdbool.h>
#include <stdint.h>
#include <plc4c/spi/context.h>
#include <plc4c/spi/read_buffer.h>
#include <plc4c/spi/write_buffer.h>
#include <plc4c/utils/list.h>

// Code generated by code-generation. DO NOT EDIT.

<#--
When using const fields, output the constant reference values
as global const values so we can use them elsewhere.
-->
<#if helper.getAllConstFields()?has_content>

// Constant values.
<#list helper.getAllConstFields() as entry>
<#assign constField=entry.key>
<#assign parentTypeName=entry.value>
static const ${helper.getLanguageTypeNameForField(constField)} ${helper.getCTypeName(parentTypeName)?upper_case}_${helper.camelCaseToSnakeCase(constField.name)?upper_case}_const = ${helper.toParseExpression(type, constField, constField.referenceValue, parserArguments)};
${helper.getLanguageTypeNameForField(constField)} ${helper.getCTypeName(parentTypeName)?upper_case}_${helper.camelCaseToSnakeCase(constField.name)?upper_case}() {
return ${helper.getCTypeName(parentTypeName)?upper_case}_${helper.camelCaseToSnakeCase(constField.name)?upper_case}_const;
}
</#list>
</#if>

</#outputformat>
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
<#--
Licensed to the Apache Software Foundation (ASF) under one
or more contributor license agreements. See the NOTICE file
distributed with this work for additional information
regarding copyright ownership. The ASF licenses this file
to you under the Apache License, Version 2.0 (the
"License"); you may not use this file except in compliance
with the License. You may obtain a copy of the License at

https://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing,
software distributed under the License is distributed on an
"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
KIND, either express or implied. See the License for the
specific language governing permissions and limitations
under the License.
-->
<#-- Prevent freemarker from escaping stuff -->
<#outputformat "undefined">
<#-- Declare the name and type of variables passed in to the template -->
<#-- @ftlvariable name="languageName" type="java.lang.String" -->
<#-- @ftlvariable name="protocolName" type="java.lang.String" -->
<#-- @ftlvariable name="outputFlavor" type="java.lang.String" -->
<#-- @ftlvariable name="helper" type="org.apache.plc4x.language.c.CLanguageTemplateHelper" -->
<#-- @ftlvariable name="tracer" type="org.apache.plc4x.plugins.codegenerator.protocol.freemarker.Tracer" -->
<#-- @ftlvariable name="type" type="org.apache.plc4x.plugins.codegenerator.types.definitions.ComplexTypeDefinition" -->
<#-- Declare the name and type of variables declared locally inside the template -->
<#-- @ftlvariable name="field" type="org.apache.plc4x.plugins.codegenerator.types.fields.Field" -->
${helper.getIncludesDirectory()?replace(".", "/")}/${helper.camelCaseToSnakeCase(type.name)}.h
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

#ifndef ${helper.getCTypeName(type.name)?upper_case}_H_
#define ${helper.getCTypeName(type.name)?upper_case}_H_

#include <stdbool.h>
#include <stdint.h>
#include <plc4c/spi/context.h>
#include <plc4c/spi/read_buffer.h>
#include <plc4c/spi/write_buffer.h>
#include <plc4c/utils/list.h>

// Code generated by code-generation. DO NOT EDIT.

<#--#ifdef __cplusplus
extern "C" {
#endif-->

<#--
When using const fields, output the constant reference values
as global const values so we can use them elsewhere.
-->
<#if helper.getAllConstFields()?has_content>

// Constant values.
<#list helper.getAllConstFields() as entry>
<#assign constField=entry.key>
<#assign parentTypeName=entry.value>
${helper.getLanguageTypeNameForField(constField)} ${helper.getCTypeName(parentTypeName)?upper_case}_${helper.camelCaseToSnakeCase(constField.name)?upper_case}();
</#list>
</#if>

<#--#ifdef __cplusplus
}
#endif-->
#endif // ${helper.getCTypeName(type.name)?upper_case}_H_
</#outputformat>
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,11 @@ protected List<Template> getSpecTemplates(Configuration freemarkerConfiguration)
return Collections.emptyList();
}

@Override
protected List<Template> getConstantsTemplates(Configuration freemarkerConfiguration) throws IOException {
return Collections.emptyList();
}

@Override
protected List<Template> getComplexTypeTemplates(Configuration freemarkerConfiguration) throws IOException {
return Collections.singletonList(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,11 @@ protected List<Template> getSpecTemplates(Configuration freemarkerConfiguration)
freemarkerConfiguration.getTemplate("templates/go/xml-parser-factory-template.go.ftlh"));
}

@Override
protected List<Template> getConstantsTemplates(Configuration freemarkerConfiguration) throws IOException {
return List.of(freemarkerConfiguration.getTemplate("templates/go/constants-template.go.ftlh"));
}

@Override
protected List<Template> getComplexTypeTemplates(Configuration freemarkerConfiguration) throws IOException {
return Collections.singletonList(
Expand Down
Loading
Loading