Commit 6f2ada7f authored by Florian Hölzl's avatar Florian Hölzl
Browse files

first version of enumeration generation

refs 118
parent e0afd070
......@@ -17,7 +17,21 @@ $Id$
+--------------------------------------------------------------------------*/
package org.fortiss.af3.expression.generator.c;
import static org.fortiss.af3.generator.common.utils.CLanguageModelElementFactory.createCEnumerationDeclaration;
import static org.fortiss.af3.generator.common.utils.CLanguageModelElementFactory.createCEnumerationMember;
import static org.fortiss.af3.generator.common.utils.CLanguageModelElementFactory.createCSourcePackage;
import static org.fortiss.af3.generator.common.utils.CLanguageModelElementFactory.createHeaderFile;
import org.fortiss.af3.expression.model.definitions.Enumeration;
import org.fortiss.af3.expression.model.definitions.EnumerationMember;
import org.fortiss.af3.expression.model.definitions.FunctionDefinition;
import org.fortiss.af3.expression.model.definitions.TypeDefinition;
import org.fortiss.af3.expression.model.generator.Program;
import org.fortiss.af3.generator.common.model.c.CSourcePackage;
import org.fortiss.af3.generator.common.model.c.HeaderFile;
import org.fortiss.af3.generator.common.model.c.ImplementationFile;
import org.fortiss.af3.generator.common.model.c.types.CEnumerationDeclaration;
import org.fortiss.af3.generator.common.model.c.types.CEnumerationMember;
import org.fortiss.af3.generator.common.model.source.SourcePackage;
import org.fortiss.tooling.kernel.extension.ITransformationProvider;
import org.fortiss.tooling.kernel.extension.data.ITransformationContext;
......@@ -38,31 +52,61 @@ public class ProgramTransformation implements ITransformationProvider {
/** {@inheritDoc} */
@Override
public Class<?> getTargetClass() {
return null;
return CSourcePackage.class;
}
/** {@inheritDoc} */
@Override
public boolean
canHandleChainTransformation(Class<?> sourceClass, ITransformationContext context) {
// TODO Auto-generated method stub
return false;
return Program.class.isAssignableFrom(sourceClass);
}
/** {@inheritDoc} */
@Override
public boolean canTransform(Object source, ITransformationContext context) {
// TODO Auto-generated method stub
return false;
return source instanceof Program;
}
/** {@inheritDoc} */
@Override
public Object transform(Object source, ITransformationContext context)
throws TransformationFailedException {
// TODO Auto-generated method stub
Program p = (Program)source;
CSourcePackage pack = createCSourcePackage();
pack.getIncGenPackage().getUnitsList().add(createHeader(p));
pack.getSrcGenPackage().getUnitsList().add(createImplemenation(p));
return pack;
}
/** Creates the implementation source code. */
private ImplementationFile createImplemenation(Program p) {
return null;
}
/** Creates the header source code. */
private HeaderFile createHeader(Program p) {
HeaderFile h = createHeaderFile(p.getName());
for(TypeDefinition td : p.getTypesList()) {
if(td instanceof Enumeration) {
addCEnumeration((Enumeration)td, h);
}
}
for(FunctionDefinition fd : p.getFunctionsList()) {
// TODO
}
return h;
}
/** Adds the C enumeration for the given enumeration to the given header file. */
private void addCEnumeration(Enumeration e, HeaderFile h) {
CEnumerationDeclaration decl = createCEnumerationDeclaration(e.getName());
int val = 0;
for(EnumerationMember mem : e.getMembersList()) {
CEnumerationMember m = createCEnumerationMember(mem.getName(), val);
decl.getMembersList().add(m);
val++;
}
h.getDeclarationsList().add(decl);
}
}
......@@ -12,6 +12,9 @@ Require-Bundle: org.fortiss.af3.project;bundle-version="2.1.0";visibility:=reexp
Export-Package: org.fortiss.af3.generator.common,
org.fortiss.af3.generator.common.model.c,
org.fortiss.af3.generator.common.model.c.impl,
org.fortiss.af3.generator.common.model.c.types,
org.fortiss.af3.generator.common.model.c.types.impl,
org.fortiss.af3.generator.common.model.c.types.util,
org.fortiss.af3.generator.common.model.c.util,
org.fortiss.af3.generator.common.model.oldc,
org.fortiss.af3.generator.common.model.oldsource,
......@@ -19,4 +22,5 @@ Export-Package: org.fortiss.af3.generator.common,
org.fortiss.af3.generator.common.model.source.impl,
org.fortiss.af3.generator.common.model.source.util,
org.fortiss.af3.generator.common.model.vhdl,
org.fortiss.af3.generator.common.textgen.c,
org.fortiss.af3.generator.common.utils
......@@ -21,6 +21,13 @@
</eAnnotations>
<eParameters name="rootLocation" eType="ecore:EDataType http://www.eclipse.org/emf/2002/Ecore#//EString"/>
</eOperations>
<eOperations name="getSubPackage" eType="#//source/SourcePackage">
<eAnnotations source="http://www.eclipse.org/emf/2002/GenModel">
<details key="body" value="return org.fortiss.af3.generator.common.utils.SourceUtils.findSubPackageByName(name, this);"/>
<details key="documentation" value="Returns the sub-package with the given name."/>
</eAnnotations>
<eParameters name="name" eType="ecore:EDataType http://www.eclipse.org/emf/2002/Ecore#//EString"/>
</eOperations>
<eStructuralFeatures xsi:type="ecore:EAttribute" name="baseLocation" eType="ecore:EDataType http://www.eclipse.org/emf/2002/Ecore#//EString"/>
<eStructuralFeatures xsi:type="ecore:EReference" name="subPackages" upperBound="-1"
eType="#//source/SourcePackage" containment="true"/>
......@@ -97,6 +104,48 @@
<eStructuralFeatures xsi:type="ecore:EReference" name="declaration" eType="#//c/FunctionDeclaration"
eOpposite="#//c/FunctionDeclaration/definition"/>
</eClassifiers>
<eClassifiers xsi:type="ecore:EClass" name="CSourcePackage" eSuperTypes="#//source/SourcePackage"/>
<eClassifiers xsi:type="ecore:EClass" name="CSourcePackage" eSuperTypes="#//source/SourcePackage">
<eOperations name="getIncGenPackage" eType="#//source/SourcePackage">
<eAnnotations source="http://www.eclipse.org/emf/2002/GenModel">
<details key="body" value="return getSubPackage(&quot;inc-gen&quot;);"/>
<details key="documentation" value="Returns the sub-package with the given name."/>
</eAnnotations>
</eOperations>
<eOperations name="getSrcGenPackage" eType="#//source/SourcePackage">
<eAnnotations source="http://www.eclipse.org/emf/2002/GenModel">
<details key="body" value="return getSubPackage(&quot;src-gen&quot;);"/>
<details key="documentation" value="Returns the sub-package with the given name."/>
</eAnnotations>
</eOperations>
<eOperations name="getIncPackage" eType="#//source/SourcePackage">
<eAnnotations source="http://www.eclipse.org/emf/2002/GenModel">
<details key="body" value="return getSubPackage(&quot;inc&quot;);"/>
<details key="documentation" value="Returns the sub-package with the given name."/>
</eAnnotations>
</eOperations>
<eOperations name="getSrcPackage" eType="#//source/SourcePackage">
<eAnnotations source="http://www.eclipse.org/emf/2002/GenModel">
<details key="body" value="return getSubPackage(&quot;src&quot;);"/>
<details key="documentation" value="Returns the sub-package with the given name."/>
</eAnnotations>
</eOperations>
</eClassifiers>
<eClassifiers xsi:type="ecore:EClass" name="TypeDeclaration" eSuperTypes="#//source/Declaration ../../org.fortiss.tooling.kernel/model/kernel.ecore#//INamedElement"/>
<eSubpackages name="types" nsURI="http://www.fortiss.org/af3/generator/common/languages/c/types"
nsPrefix="org-fortiss-af3-generator-common-languages-c-types">
<eClassifiers xsi:type="ecore:EClass" name="CEnumerationDeclaration" eSuperTypes="#//c/TypeDeclaration">
<eOperations name="toString" eType="ecore:EDataType http://www.eclipse.org/emf/2002/Ecore#//EString">
<eAnnotations source="http://www.eclipse.org/emf/2002/GenModel">
<details key="body" value="return org.fortiss.af3.generator.common.textgen.c.CLanguageTypesTextGenerator.enumerationToString(this);"/>
</eAnnotations>
</eOperations>
<eStructuralFeatures xsi:type="ecore:EReference" name="members" upperBound="-1"
eType="#//c/types/CEnumerationMember" containment="true"/>
</eClassifiers>
<eClassifiers xsi:type="ecore:EClass" name="CEnumerationMember">
<eStructuralFeatures xsi:type="ecore:EAttribute" name="name" eType="ecore:EDataType http://www.eclipse.org/emf/2002/Ecore#//EString"/>
<eStructuralFeatures xsi:type="ecore:EAttribute" name="value" eType="ecore:EDataType http://www.eclipse.org/emf/2002/Ecore#//EInt"/>
</eClassifiers>
</eSubpackages>
</eSubpackages>
</ecore:EPackage>
/*--------------------------------------------------------------------------+
$Id$
| |
| Copyright 2012 ForTISS GmbH |
| |
| Licensed 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 |
| |
| http://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. |
+--------------------------------------------------------------------------*/
package org.fortiss.af3.generator.common.textgen.c;
import org.fortiss.af3.generator.common.model.c.types.CEnumerationDeclaration;
import org.fortiss.af3.generator.common.model.c.types.CEnumerationMember;
/**
* Text generator for C language types.
*
* @author hoelzl
* @author $Author$
* @version $Rev$
* @ConQAT.Rating RED Hash:
*/
public class CLanguageTypesTextGenerator {
/** Creates the C enumeration type declaration text. */
public static String enumerationToString(CEnumerationDeclaration cenum) {
StringBuffer buf = new StringBuffer();
buf.append("typedef enum Enum_").append(cenum.getName()).append("{\n");
boolean first = true;
for(CEnumerationMember mem : cenum.getMembersList()) {
if(!first) {
buf.append(",\n");
}
buf.append(mem.getName()).append(" = ").append(mem.getValue()).append("\n");
}
buf.append("#ifdef USE_GCC\n");
buf.append("} __attribute__ ((packed)) TYPE_").append(cenum.getName()).append(";\n");
buf.append("#else\n");
buf.append("} TYPE_").append(cenum.getName()).append(";\n");
buf.append("#endif\n");
buf.append("\n");
return buf.toString();
}
}
/*--------------------------------------------------------------------------+
$Id$
| |
| Copyright 2012 ForTISS GmbH |
| |
| Licensed 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 |
| |
| http://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. |
+--------------------------------------------------------------------------*/
package org.fortiss.af3.generator.common.utils;
import org.fortiss.af3.generator.common.model.c.AF3GeneratorCommonLanguagesCFactory;
import org.fortiss.af3.generator.common.model.c.AF3GeneratorCommonLanguagesCPackage;
import org.fortiss.af3.generator.common.model.c.CSourcePackage;
import org.fortiss.af3.generator.common.model.c.HeaderFile;
import org.fortiss.af3.generator.common.model.c.ImplementationFile;
import org.fortiss.af3.generator.common.model.c.types.CEnumerationDeclaration;
import org.fortiss.af3.generator.common.model.c.types.CEnumerationMember;
import org.fortiss.af3.generator.common.model.c.types.TypesFactory;
/**
* Model element factory for {@link AF3GeneratorCommonLanguagesCPackage} elements.
*
* @author hoelzl
* @author $Author$
* @version $Rev$
* @ConQAT.Rating RED Hash:
*/
public class CLanguageModelElementFactory {
/**
* Creates a CSourcePackage with the default sub-packages {@code inc}, {@code inc-gen},
* {@code src}, and {@code src-gen}.
*/
public static CSourcePackage createCSourcePackage() {
CSourcePackage cPack = AF3GeneratorCommonLanguagesCFactory.eINSTANCE.createCSourcePackage();
CSourcePackage sub = AF3GeneratorCommonLanguagesCFactory.eINSTANCE.createCSourcePackage();
sub.setBaseLocation("inc");
cPack.getSubPackagesList().add(sub);
sub = AF3GeneratorCommonLanguagesCFactory.eINSTANCE.createCSourcePackage();
sub.setBaseLocation("inc-gen");
cPack.getSubPackagesList().add(sub);
sub = AF3GeneratorCommonLanguagesCFactory.eINSTANCE.createCSourcePackage();
sub.setBaseLocation("src");
cPack.getSubPackagesList().add(sub);
sub = AF3GeneratorCommonLanguagesCFactory.eINSTANCE.createCSourcePackage();
sub.setBaseLocation("src-gen");
cPack.getSubPackagesList().add(sub);
return cPack;
}
/** Creates a {@link HeaderFile} with the given name. */
public static HeaderFile createHeaderFile(String name) {
HeaderFile hFile = AF3GeneratorCommonLanguagesCFactory.eINSTANCE.createHeaderFile();
hFile.setName(name);
return hFile;
}
/** Creates an {@link ImplementationFile} with the given name. */
public static ImplementationFile createImplementationFile(String name) {
ImplementationFile iFile =
AF3GeneratorCommonLanguagesCFactory.eINSTANCE.createImplementationFile();
iFile.setName(name);
return iFile;
}
/** Creates a {@link CEnumerationDeclaration} with the given name. */
public static CEnumerationDeclaration createCEnumerationDeclaration(String name) {
CEnumerationDeclaration decl = TypesFactory.eINSTANCE.createCEnumerationDeclaration();
decl.setName(name);
return decl;
}
/** Creates a {@link CEnumerationMember} with the given name and value. */
public static CEnumerationMember createCEnumerationMember(String name, int value) {
CEnumerationMember mem = TypesFactory.eINSTANCE.createCEnumerationMember();
mem.setName(name);
mem.setValue(value);
return mem;
}
}
/*--------------------------------------------------------------------------+
$Id$
| |
| Copyright 2012 ForTISS GmbH |
| |
| Licensed 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 |
| |
| http://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. |
+--------------------------------------------------------------------------*/
package org.fortiss.af3.generator.common.utils;
import org.fortiss.af3.generator.common.model.c.CSourcePackage;
/**
* Utility methods for the C language model.
*
* @author hoelzl
* @author $Author$
* @version $Rev$
* @ConQAT.Rating RED Hash:
*/
public class CLanguageUtils {
/**
* Returns the {@code inc-gen} sub-package for the given root package.
*
* @param root
* the root package to be searched
* @return the {@code inc-gen} package or {@code null} if not present
*/
public static CSourcePackage getGeneratedHeaderPackage(CSourcePackage root) {
return findSubPackageByName("inc-gen", root);
}
/**
* Finds the sub-package with the given name in the given root package.
*
* @param name
* the sub-package name
* @param root
* the root package to be searched
* @return the sub-package or {@code null}
*/
private static CSourcePackage findSubPackageByName(String name, CSourcePackage root) {
return null;
}
}
/*--------------------------------------------------------------------------+
$Id$
| |
| Copyright 2012 ForTISS GmbH |
| |
| Licensed 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 |
| |
| http://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. |
+--------------------------------------------------------------------------*/
package org.fortiss.af3.generator.common.utils;
import org.fortiss.af3.generator.common.model.source.SourcePackage;
/**
* Utility methods for the source model.
*
* @author hoelzl
* @author $Author$
* @version $Rev$
* @ConQAT.Rating RED Hash:
*/
public class SourceUtils {
/**
* Finds the sub-package with the given name in the given root package.
*
* @param name
* the sub-package name
* @param root
* the root package to be searched
* @return the sub-package or {@code null}
*/
public static SourcePackage findSubPackageByName(String name, SourcePackage root) {
for(SourcePackage sub : root.getSubPackagesList()) {
if(name.equals(sub.getBaseLocation())) {
return sub;
}
}
return null;
}
}
......@@ -21,7 +21,7 @@ import static org.eclipse.emf.common.util.URI.createPlatformPluginURI;
import static org.fortiss.af3.expression.language.evaluation.NoVal.isNoValConst;
import static org.fortiss.af3.expression.model.terms.impl.BoolConstStaticImpl.FALSE;
import static org.fortiss.af3.expression.model.terms.impl.BoolConstStaticImpl.TRUE;
import static org.fortiss.af3.generator.common.utils.CSourcePackageUtils.findSubPackageByName;
import static org.fortiss.af3.generator.common.utils.OldCSourcePackageUtils.findSubPackageByName;
import static org.fortiss.af3.project.utils.FileUtils.loadTextFileFromPlugin;
import static org.fortiss.af3.project.utils.ProjectUtils.loadProjectFromPlugin;
import static org.fortiss.af3.testing.utils.TestingUtils.getTestOutputFor;
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment