Commit 05944c76 authored by Florian Hölzl's avatar Florian Hölzl
Browse files

removed c package

refs 118
parent 3eb94566
/*--------------------------------------------------------------------------+
$Id$
| |
| Copyright 2011 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.model.c;
import org.fortiss.af3.project.model.typesystem.IType;
import org.fortiss.af3.project.model.typesystem.impl.ITypeImpl;
/**
* C base type instance, e.g. for type defined in supplementary C libraries.
*
* @author hoelzl
* @author $Author$
* @version $Rev$
* @ConQAT.Rating GREEN Hash: BF2A2F7834FFD3DB4620E1226B0124B6
*/
public final class BaseType extends ITypeImpl {
/** Stores the name. */
private String name;
/** Returns name. */
public String getName() {
return name;
}
/** Sets name. */
public void setName(String name) {
this.name = name;
}
/** Creates BaseType instance with the given identifier. */
public static IType create(String identifier) {
BaseType result = new BaseType();
result.setName(identifier);
return result;
}
}
/*--------------------------------------------------------------------------+
$Id$
| |
| Copyright 2011 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.model.c;
import static org.fortiss.af3.generator.common.model.c.Modifier.EXTERN;
import static org.fortiss.af3.generator.common.model.c.Modifier.STATIC;
import static org.fortiss.af3.generator.common.model.c.Modifier.modifierString;
import java.util.Iterator;
import java.util.List;
import org.fortiss.af3.generator.common.model.source.DeclarationBase;
import org.fortiss.af3.generator.common.model.source.DefinitionBase;
import org.fortiss.af3.project.model.typesystem.ITerm;
import org.fortiss.af3.project.model.typesystem.IType;
/**
* C function model.
*
* @author hoelzl
* @author $Author$
* @version $Rev$
* @ConQAT.Rating GREEN Hash: 8D9C858FD8870B220E6C22D023E4D311
*/
public class Function {
/** Stores function modifier. */
private final Modifier modifier;
/** Stores result type. */
private final IType type;
/** Stores the name of the function. */
private final String name;
/** Stores the parameter list. */
private final List<Variable> parameters;
/** Stores the local variables list. */
private final List<Variable> variables;
/** Stores the body of the function. */
private final ITerm body;
/** Stores the type string generator. */
private final ITypeStringGenerator generator;
/** Constructor. */
public Function(Modifier modifier, IType type, String name,
List<Variable> parameters, List<Variable> variables, ITerm body,
ITypeStringGenerator generator) {
this.modifier = modifier;
this.type = type;
this.name = name;
this.parameters = parameters;
this.variables = variables;
this.body = body;
this.generator = generator;
}
/** Constructor. */
public Function(Modifier modifier, IType type, String name,
List<Variable> parameters, List<Variable> variables, ITerm body) {
this(modifier, type, name, parameters, variables, body,
new IdentityTypeStringGenerator());
}
/** Generates forward declaration. */
public DeclarationBase getDeclaration() {
return new DeclarationBase() {
@Override
public String toString() {
String sig = getSignature();
sig = modifierString(modifier) + sig;
return sig + ";\n";
}
};
}
/** Generates function implementation. */
public DefinitionBase getDefinition() {
return new DefinitionBase() {
@Override
public String toString() {
StringBuffer buf = new StringBuffer();
if (!EXTERN.equals(modifier)) {
buf.append(modifierString(modifier));
}
buf.append(getSignature()).append(" {\n");
for (Variable cv : variables) {
buf.append(cv.toString()).append(";\n");
}
buf.append(body.toString());
buf.append("}\n");
return buf.toString();
}
};
}
/** Returns function signature. */
public String getSignature() {
StringBuffer buf = new StringBuffer();
buf.append(generator.getTypeString(type)).append(' ');
buf.append(name).append('(');
if (!parameters.isEmpty()) {
for (Iterator<Variable> it = parameters.iterator(); it.hasNext();) {
Variable locvar = it.next();
buf.append(locvar.toString());
if (it.hasNext()) {
buf.append(", ");
}
}
} else {
buf.append("void");
}
buf.append(')');
return buf.toString();
}
/** Returns the function name. */
public String getName() {
return name;
}
/** Creates external function. */
public static Function createExternFunction(IType type, String name,
List<Variable> parameters, List<Variable> variables, ITerm body,
ITypeStringGenerator generator) {
return new Function(EXTERN, type, name, parameters, variables, body,
generator);
}
/** Creates internal function. */
public static Function createInternFunction(IType type, String name,
List<Variable> parameters, List<Variable> variables, ITerm body,
ITypeStringGenerator generator) {
return new Function(STATIC, type, name, parameters, variables, body,
generator);
}
}
/*--------------------------------------------------------------------------+
$Id$
| |
| Copyright 2011 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.model.c;
import java.util.Date;
import org.fortiss.af3.generator.common.model.source.DeclarationBase;
import org.fortiss.af3.generator.common.model.source.ImportBase;
import org.fortiss.af3.generator.common.model.source.SourceUnitBase;
/**
* Model for C header files.
*
* @author hoelzl
* @author $Author$
* @version $Rev$
* @ConQAT.Rating GREEN Hash: 5AFF62153136FF33922647E5F7620FB6
*/
public class HeaderFile extends SourceUnitBase {
/** Constructor. */
public HeaderFile(String headerName) {
super(headerName + ".h");
}
/** {@inheritDoc} */
@Override
public String toString() {
StringBuffer buf = new StringBuffer();
String ifdefString = unitName.replaceAll("\\W", "_");
buf.append("/* generated by AutoFOCUS 3 (")
.append(new Date().toString()).append(") */\n");
buf.append("#ifndef __HEADER_").append(ifdefString).append('\n');
buf.append("#define __HEADER_").append(ifdefString).append('\n');
for (ImportBase i : importList) {
buf.append(i.toString()).append('\n');
}
for (DeclarationBase d : declarationList) {
buf.append(d.toString()).append('\n');
}
buf.append("#endif // __HEADER_").append(ifdefString).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.model.c;
import org.fortiss.af3.project.model.typesystem.IType;
/**
* Extension interface for conversion of IType instances to C code strings.
*
* @author hoelzl
* @author $Author$
* @version $Rev$
* @ConQAT.Rating GREEN Hash: 81F12DF4DD20112412384DFC6EC4B5CA
*/
public interface ITypeStringGenerator {
/** Returns the string representation for the given IType. */
String getTypeString(IType type);
}
/*--------------------------------------------------------------------------+
$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.model.c;
import org.fortiss.af3.project.model.typesystem.IType;
/**
* Default type string generator, which uses {@link IType#toString()}.
*
* @author hoelzl
* @author $Author$
* @version $Rev$
* @ConQAT.Rating GREEN Hash: B584B429D1F248DA52C477E1AC2FB992
*/
public class IdentityTypeStringGenerator implements ITypeStringGenerator {
/** {@inheritDoc} */
@Override
public String getTypeString(IType type) {
return type.toString();
}
}
/*--------------------------------------------------------------------------+
$Id$
| |
| Copyright 2011 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.model.c;
import java.util.Date;
import org.fortiss.af3.generator.common.model.source.SourceUnitBase;
/**
* Model for C implementation file.
*
* @author hoelzl
* @author $Author$
* @version $Rev$
* @ConQAT.Rating GREEN Hash: 87C5985716F9F8C6A3AC4DC66ECFDD6B
*/
public class ImplementationFile extends SourceUnitBase {
/** Constructor. */
public ImplementationFile(String unitName) {
super(unitName + ".c");
}
/** {@inheritDoc} */
@Override
public String toString() {
StringBuffer buf = new StringBuffer();
buf.append("/* generated by AutoFOCUS 3 (")
.append(new Date().toString()).append(") */\n");
buf.append(super.toString());
return buf.toString();
}
}
/*--------------------------------------------------------------------------+
$Id$
| |
| Copyright 2011 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.model.c;
import org.fortiss.af3.generator.common.model.source.ImportBase;
/**
* C header file include.
*
* @author hoelzl
* @author $Author$
* @version $Rev$
* @ConQAT.Rating GREEN Hash: 0597B0D5498B3EC46E25CF47415F6045
*/
public final class Include extends ImportBase {
/** Included header file. */
private final HeaderFile headerFile;
/** Includes directory to be used. */
private final String includeDir;
/** Constructor. */
public Include(HeaderFile headerFile) {
this(headerFile, "inc-gen/");
}
/** Constructor. */
public Include(HeaderFile headerFile, String includeDir) {
super(headerFile.getUnitName());
this.headerFile = headerFile;
this.includeDir = includeDir;
}
/** {@inheritDoc} */
@Override
public String toString() {
return "#include \"" + includeDir + sourceUnitReference + "\"\n";
}
/** Returns included header file. */
public HeaderFile getHeaderFile() {
return headerFile;
}
}
/*--------------------------------------------------------------------------+
$Id$
| |
| Copyright 2011 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.model.c;
/**
* C modifier enumeration.
*
* @author hoelzl
* @author $Author$
* @version $Rev$
* @ConQAT.Rating GREEN Hash: FF165F8B586D9D2A8AE413699C453AE0
*/
public enum Modifier {
/** Static modifier (e.g. locals). */
STATIC,
/** Extern modifier (e.g. globals). */
EXTERN,
/** None modifier (e.g. parameter variables) */
NONE;
/** Returns textual modifier. */
public static String modifierString(Modifier modifier) {
switch (modifier) {
case STATIC:
return "static ";
case EXTERN:
return "extern ";
default:
return "";
}
}
}
/*--------------------------------------------------------------------------+
$Id$
| |
| Copyright 2011 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.model.c;
import org.fortiss.af3.generator.common.model.source.DeclarationBase;
/**
* Model for <code>#define</code> pre-processor declarations.
*
* @author hoelzl
* @author $Author$
* @version $Rev$
* @ConQAT.Rating GREEN Hash: D45BE6E6ADFA65B69700C57A4523DE66
*/
public final class PreProcessorDefine extends DeclarationBase {
/** Stores the key string of the pre-processor definition. */
private final String key;
/** Stores the value string of the pre-processor definition. */
private final String value;
/** Constructor. */
public PreProcessorDefine(String key, String value) {
this.key = key;
this.value = value;
}
/** {@inheritDoc} */
@Override
public String toString() {
return "#ifndef " + key + "\n#define " + key + " " + value
+ "\n#endif\n";
}
}
/*--------------------------------------------------------------------------+
$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 |
| |