Commit bb243c36 authored by Alexander Ryndin's avatar Alexander Ryndin
Browse files

issue #2 add code to make EMA2Kotlin work

parent 9df0008c
......@@ -35,6 +35,7 @@
<!-- == PROJECT DEPENDENCIES ============================================= -->
<properties>
<kotlin.version>1.1.51</kotlin.version>
<wagon.provider.version>2.6</wagon.provider.version>
<!-- DSLs -->
<javadsl.version>4.3.9</javadsl.version>
......@@ -47,6 +48,7 @@
<mc.grammars.assembly.version>0.0.6-SNAPSHOT</mc.grammars.assembly.version>
<languages.version>4.0.1-SNAPSHOT</languages.version>
<SIUnit.version>0.0.6-SNAPSHOT</SIUnit.version>
<struct.version>0.0.3-SNAPSHOT</struct.version>
<Common-MontiCar.version>0.0.3-SNAPSHOT</Common-MontiCar.version>
......@@ -77,6 +79,20 @@
</properties>
<dependencies>
<dependency>
<groupId>de.monticore.lang.monticar</groupId>
<artifactId>struct</artifactId>
<version>${struct.version}</version>
</dependency>
<dependency>
<groupId>de.monticore.lang.monticar</groupId>
<artifactId>struct</artifactId>
<version>${struct.version}</version>
<classifier>${grammars.classifier}</classifier>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>de.se_rwth.commons</groupId>
<artifactId>se-commons-logging</artifactId>
......@@ -218,7 +234,19 @@
<artifactId>jscience</artifactId>
<version>${jscience.version}</version>
</dependency>
<dependency>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-stdlib-jre8</artifactId>
<version>${kotlin.version}</version>
</dependency>
<dependency>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-test-junit</artifactId>
<version>${kotlin.version}</version>
<scope>test</scope>
</dependency>
</dependencies>
......@@ -287,6 +315,38 @@
</plugin>
<!-- Other Configuration -->
<plugin>
<artifactId>kotlin-maven-plugin</artifactId>
<configuration>
<jvmTarget>1.8</jvmTarget>
</configuration>
<groupId>org.jetbrains.kotlin</groupId>
<version>${kotlin.version}</version>
<executions>
<execution>
<id>compile</id>
<phase>compile</phase>
<goals>
<goal>compile</goal>
</goals>
<configuration>
<sourceDirs>
<source>src/main/java</source>
<source>target/generated-sources/monticore/sourcecode</source>
<source>src/main/kotlin</source>
</sourceDirs>
</configuration>
</execution>
<execution>
<id>test-compile</id>
<phase>test-compile</phase>
<goals>
<goal>test-compile</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>${compiler.plugin}</version>
......@@ -401,6 +461,26 @@
<aggregate>true</aggregate>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<executions>
<execution>
<id>compile</id>
<phase>compile</phase>
<goals>
<goal>compile</goal>
</goals>
</execution>
<execution>
<id>testCompile</id>
<phase>test-compile</phase>
<goals>
<goal>testCompile</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
......
/**
*
* ******************************************************************************
* MontiCAR Modeling Family, www.se-rwth.de
* Copyright (c) 2017, Software Engineering Group at RWTH Aachen,
* All rights reserved.
*
* This project is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 3.0 of the License, or (at your option) any later version.
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this project. If not, see <http://www.gnu.org/licenses/>.
* *******************************************************************************
*/
package de.monticore.lang.embeddedmontiarc;
import de.monticore.lang.monticar.struct.model.type.StructFieldTypeInfo;
import javax.annotation.Nonnull;
public class FormalTypeParameterFieldType implements StructFieldTypeInfo {
@Nonnull
private final String formalTypeParameterName;
public FormalTypeParameterFieldType(@Nonnull String formalTypeParameterName) {
this.formalTypeParameterName = formalTypeParameterName;
}
@Nonnull
public String getFormalTypeParameterName() {
return formalTypeParameterName;
}
@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
FormalTypeParameterFieldType that = (FormalTypeParameterFieldType) o;
return getFormalTypeParameterName().equals(that.getFormalTypeParameterName());
}
@Override
public int hashCode() {
return getFormalTypeParameterName().hashCode();
}
}
......@@ -24,6 +24,7 @@ import de.monticore.ast.ASTNode;
//import de.monticore.common.common._ast.ASTStereoValue;
import de.monticore.java.symboltable.JavaSymbolFactory;
import de.monticore.java.symboltable.JavaTypeSymbolReference;
import de.monticore.lang.embeddedmontiarc.FormalTypeParameterFieldType;
import de.monticore.lang.embeddedmontiarc.embeddedmontiarc._ast.*;
import de.monticore.lang.embeddedmontiarc.embeddedmontiarc.types.TypesPrinter;
import de.monticore.lang.embeddedmontiarc.helper.ArcTypePrinter;
......@@ -43,6 +44,7 @@ import de.monticore.lang.monticar.resolution._ast.ASTTypeArgument;
import de.monticore.lang.monticar.si._symboltable.ResolutionDeclarationSymbol;
import de.monticore.lang.monticar.si._symboltable.ResolutionDeclarationSymbolReference;
import de.monticore.lang.monticar.si._symboltable.SIUnitRangesSymbolReference;
import de.monticore.lang.monticar.struct.model.type.StructFieldTypeInfo;
import de.monticore.symboltable.*;
import de.monticore.symboltable.modifiers.BasicAccessModifier;
import de.monticore.symboltable.types.JFieldSymbol;
......@@ -173,6 +175,9 @@ public class EmbeddedMontiArcSymbolTableCreator extends EmbeddedMontiArcSymbolTa
typeName.append(ArcTypePrinter.printTypeWithoutTypeArgumentsAndDimension(astType));
//Log.debug(astType.toString(),"TYPE:");
//Log.debug(typeName,"TYPEName:");
if ("".equals(typeName.toString())) {
typeName.append("java.lang.Object");
}
typeRef = new CommonJTypeReference<JTypeSymbol>(typeName.toString(), JTypeSymbol.KIND, currentScope().get());
typeRef.setDimension(TypesHelper.getArrayDimensionIfArrayOrZero(astType));
addTypeArgumentsToTypeSymbol(typeRef, astType);
......@@ -245,6 +250,7 @@ public class EmbeddedMontiArcSymbolTableCreator extends EmbeddedMontiArcSymbolTa
} else {
// create PortSymbol with same content as PortArraySymbol
createPort(node, name, node.isIncoming(), pas.getStereotype(), typeRef, pas);
((PortSymbol)node.getSymbol().get()).setTypeInfo(pas.getTypeInfo());
}
}
......@@ -307,6 +313,25 @@ public class EmbeddedMontiArcSymbolTableCreator extends EmbeddedMontiArcSymbolTa
PortArraySymbol pas = new PortArraySymbol(name, nameTO);
pas.setTypeReference(typeRef);
StructFieldTypeInfo typeInfo = null;
if (astType instanceof ASTSimpleReferenceType && componentStack.peek().hasFormalTypeParameters()) {
List<String> names = ((ASTSimpleReferenceType) astType).getNames();
if (names != null && names.size() == 1) {
String possibleFormalTypeParamName = names.get(0);
boolean isFormalTypeParameterOfParent = componentStack
.peek()
.getFormalTypeParameters()
.stream()
.anyMatch(p -> p.isFormalTypeParameter() && p.getName().equals(possibleFormalTypeParamName));
if (isFormalTypeParameterOfParent) {
typeInfo = new FormalTypeParameterFieldType(possibleFormalTypeParamName);
}
}
}
if (typeInfo == null) {
typeInfo = StructFieldTypeInfo.tryRepresentASTType(astType, currentScope().orElse(null));
}
pas.setTypeInfo(typeInfo);
pas.setDirection(node.isIncoming());
// stereotype
......
......@@ -23,6 +23,7 @@ package de.monticore.lang.embeddedmontiarc.embeddedmontiarc._symboltable;
import de.monticore.lang.embeddedmontiarc.helper.SymbolPrinter;
import de.monticore.lang.monticar.stream._symboltable.NamedStreamSymbol;
import de.monticore.lang.montiarc.tagging._symboltable.TaggingSymbol;
import de.monticore.lang.monticar.struct.model.type.StructFieldTypeInfo;
import de.monticore.symboltable.CommonScope;
import de.monticore.symboltable.MutableScope;
import de.monticore.symboltable.SymbolKind;
......@@ -54,6 +55,11 @@ public class PortSymbol extends TaggingSymbol implements ElementInstance {
private JTypeReference<? extends JTypeSymbol> typeReference;
/**
* Structs do not use JTypeReference but StructFieldTypeInfo.
*/
private StructFieldTypeInfo typeInfo;
private MutableScope locallyDefinedStreams = new CommonScope();
protected Optional<String> nameDependsOn = Optional.empty();
......@@ -108,6 +114,14 @@ public class PortSymbol extends TaggingSymbol implements ElementInstance {
this.typeReference = typeReference;
}
public StructFieldTypeInfo getTypeInfo() {
return typeInfo;
}
public void setTypeInfo(StructFieldTypeInfo typeInfo) {
this.typeInfo = typeInfo;
}
/**
* returns the component which defines the connector
* this is independent from the component to which the source and target ports
......
......@@ -97,12 +97,25 @@ public class TypesPrinter {
if (type instanceof ASTPrintType) {
return ((ASTPrintType) type).printType();
}else if(type instanceof ASTElementType){
ASTElementType elementType = (ASTElementType) type;
if (elementType.isIsBoolean()) {
return "scalar.B";
}
if (elementType.isIsComplex()) {
return "scalar.C";
}
if (elementType.isIsRational()) {
return "scalar.Q";
}
if (elementType.isIsWholeNumberNumber()) {
return "scalar.Z";
}
return "ElementType";
}
Log.info(type.toString(),"Type:");
Log.error("Type can not be handled!");
return "";
Log.warn("Type can not be handled!");
return "java.lang.Object";
}
public static String printTypeParameters(ASTTypeParameters params) {
......
/**
*
* ******************************************************************************
* MontiCAR Modeling Family, www.se-rwth.de
* Copyright (c) 2017, Software Engineering Group at RWTH Aachen,
* All rights reserved.
*
* This project is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 3.0 of the License, or (at your option) any later version.
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this project. If not, see <http://www.gnu.org/licenses/>.
* *******************************************************************************
*/
package de.monticore.lang.embeddedmontiarc
class ComponentParametersSubstitutions(
val formalTypeParametersSubstitutions: FormalTypeParametersSubstitutions,
val configurationParametersSubstitutions: ConfigurationParametersSubstitutions
)
/**
*
* ******************************************************************************
* MontiCAR Modeling Family, www.se-rwth.de
* Copyright (c) 2017, Software Engineering Group at RWTH Aachen,
* All rights reserved.
*
* This project is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 3.0 of the License, or (at your option) any later version.
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this project. If not, see <http://www.gnu.org/licenses/>.
* *******************************************************************************
*/
package de.monticore.lang.embeddedmontiarc
import de.monticore.lang.embeddedmontiarc.embeddedmontiarc._ast.ASTComponent
import de.monticore.lang.embeddedmontiarc.embeddedmontiarc._ast.ASTSubComponent
import de.monticore.lang.embeddedmontiarc.embeddedmontiarc._symboltable.ComponentInstanceSymbol
import de.monticore.lang.embeddedmontiarc.embeddedmontiarc._symboltable.ComponentSymbol
import de.monticore.lang.embeddedmontiarc.embeddedmontiarc._visitor.EmbeddedMontiArcParentAwareVisitor
import de.monticore.lang.embeddedmontiarc.embeddedmontiarc._visitor.EmbeddedMontiArcVisitor
import de.monticore.lang.monticar.literals2._ast.ASTBooleanLiteral
import de.monticore.lang.monticar.mcexpressions._ast.ASTExpression
import de.monticore.lang.monticar.mcexpressions._ast.ASTLiteralExpression
import de.monticore.lang.monticar.mcexpressions._ast.ASTNameExpression
import de.monticore.lang.monticar.ranges._ast.ASTUnitNumberExpression
import de.monticore.lang.monticar.resolution._ast.ASTTypeArgument
import de.monticore.lang.monticar.struct._symboltable.StructSymbol
import de.monticore.lang.monticar.struct._symboltable.StructSymbolReference
import de.monticore.lang.monticar.struct.model.type.ScalarStructFieldType
import de.monticore.lang.monticar.struct.model.type.StructFieldTypeInfo
import de.monticore.lang.monticar.struct.model.type.StructReferenceFieldType
import de.monticore.lang.monticar.struct.model.type.VectorStructFieldType
import de.monticore.lang.monticar.types2._ast.ASTType
import de.monticore.lang.monticar.types2._ast.ASTTypeArguments
import de.monticore.lang.monticar.types2._ast.ASTUnitNumberResolution
import de.monticore.symboltable.types.references.JTypeReference
import de.se_rwth.commons.logging.Log
class ComponentParametersSubstitutionsBuilder(
private val component: ComponentSymbol
) : EmbeddedMontiArcParentAwareVisitor() {
private var theRealThis: EmbeddedMontiArcVisitor = this
private val formalTypeParametersSubstitutions = FormalTypeParametersSubstitutions()
private val configurationParametersSubstitutions = ConfigurationParametersSubstitutions()
private var currentChild: ComponentInstanceSymbol? = null
private val childFormalTypeParameters: MutableList<String> = mutableListOf()
private val childConfigurationParameters: MutableList<String> = mutableListOf()
override fun getRealThis(): EmbeddedMontiArcVisitor {
return theRealThis
}
override fun setRealThis(realThis: EmbeddedMontiArcVisitor) {
theRealThis = realThis
}
fun build(): ComponentParametersSubstitutions {
if (component.hasFormalTypeParameters()) {
formalTypeParametersSubstitutions.parentFormalTypeParameterNames.addAll(
component.formalTypeParameters.map { it.name }
)
}
if (component.hasConfigParameters()) {
component.configParameters.forEach {
val type = tryConvertToStructFieldTypeInfo(it.type)
if (type != null) {
configurationParametersSubstitutions
.parentConfigurationParameters
.put(it.name, type)
} else {
Log.error(
"cannot handle configuration" +
" parameter $it" +
" in component ${component.fullName}",
it.sourcePosition
)
}
}
}
val astNode = component.astNode.get() as ASTComponent
astNode.accept(theRealThis)
return ComponentParametersSubstitutions(
formalTypeParametersSubstitutions,
configurationParametersSubstitutions
)
}
override fun visit(node: ASTSubComponent) {
val subComp = node.symbol.get() as ComponentInstanceSymbol
currentChild = subComp
childFormalTypeParameters.clear()
childFormalTypeParameters.addAll(
subComp.componentType.referencedSymbol.formalTypeParameters.map { it.name }
)
childConfigurationParameters.clear()
childConfigurationParameters.addAll(
subComp.componentType.referencedSymbol.configParameters.map { it.name }
)
val astArguments = node.arguments
if (astArguments.size != childConfigurationParameters.size) {
Log.error(
"number of type arguments do not match number of type parameters",
node._SourcePositionStart
)
return
}
astArguments.forEachIndexed { index, astArg ->
val name = childConfigurationParameters[index]
if (!processArgument(name, astArg)) {
Log.error(
"cannot handle" +
" configuration argument $astArg" +
" in instance $currentChild" +
" of component ${component.fullName}",
astArg._SourcePositionStart
)
}
}
}
override fun endVisit(node: ASTSubComponent) {
currentChild = null
childFormalTypeParameters.clear()
childConfigurationParameters.clear()
}
override fun visit(node: ASTTypeArguments) {
if (currentChild == null) {
return
}
val typeArgs = node.typeArguments.asSortedByStartSourcePosition()
typeArgs.forEachIndexed { index, astNode ->
val formalParameterName = childFormalTypeParameters[index]
if (!processTypeArgument(formalParameterName, astNode)) {
Log.error(
"cannot handle" +
" type argument $astNode" +
" in instance $currentChild" +
" of component ${component.fullName}",
astNode._SourcePositionStart
)
}
}
}
private fun processTypeArgument(
formalParameterName: String,
typeArgument: ASTTypeArgument
): Boolean {
when (typeArgument) {
is ASTUnitNumberResolution -> {
val actualParameterName = typeArgument.name.get()
if (isParentHasTypeParameter(actualParameterName)) {
formalTypeParametersSubstitutions.parametersPassedToChildren.add(
PassedParameter(
actualParameterName,
currentChild!!.name,
formalParameterName
)
)
return true
} else {
// it must be struct
val struct = tryResolveStructure(actualParameterName) ?: return false
addLiteralTypeParameter(formalParameterName, struct)
return true
}
}
is ASTType -> {
val p = StructFieldTypeInfo.tryRepresentASTType(
typeArgument,
component.enclosingScope
) ?: return false
addLiteralTypeParameter(formalParameterName, p)
return true
}
}
return false
}
private fun processArgument(
name: String,
arg: ASTExpression
): Boolean {
when (arg) {
is ASTNameExpression -> {
if (isParentHasConfigurationParameter(arg.name)) {
configurationParametersSubstitutions.parametersPassedToChildren.add(
PassedParameter(
arg.name,
currentChild!!.name,
name
)
)
return true
}
}
is ASTUnitNumberExpression -> {
val tUnitNumber = arg.tUnitNumber
if (!tUnitNumber.isPresent) {
return false
}
val literalValue = tUnitNumber.get().toDoubleOrNull() ?: return false
addLiteralConfigurationParameter(name, literalValue)
return true
}
is ASTLiteralExpression -> {
val literal = arg.literal
if (literal is ASTBooleanLiteral) {
addLiteralConfigurationParameter(name, literal.value)
return true
}
}
}
return false
}
private fun addLiteralTypeParameter(
formalParamName: String,
type: StructFieldTypeInfo
) {
val p = LiteralParameterSubstitution(formalParamName, type)
val literalParams = formalTypeParametersSubstitutions.literalChildParameters[currentChild!!.name]
if (literalParams != null) {
literalParams.add(p)
} else {
formalTypeParametersSubstitutions.literalChildParameters.put(
currentChild!!.name,
mutableListOf(p)
)
}
}
private fun addLiteralConfigurationParameter(
name: String,
literalValue: Any
) {