Commit 461ee8ec authored by Thomas Michael Timmermanns's avatar Thomas Michael Timmermanns Committed by Thomas Michael Timmermanns

Implemented parameterization/instances for ArchitectureSymbol and CNNArchCompilationSymbol.

parent 15df474f
......@@ -28,36 +28,39 @@ import de.se_rwth.commons.logging.Log;
//check all cocos
public class CNNArchCocos {
public static void checkAll(ArchitectureSymbol architecture){
public static ArchitectureSymbol checkAll(ArchitectureSymbol architecture){
ArchitectureSymbol resolvedArchitecture = null;
ASTCNNArchNode node = (ASTCNNArchNode) architecture.getAstNode().get();
int findings = Log.getFindings().size();
createPreResolveChecker().checkAll(node);
if (findings == Log.getFindings().size()){
architecture.resolve();
resolvedArchitecture = architecture.resolve();
if (findings == Log.getFindings().size()){
createPostResolveChecker().checkAll(node);
createPostResolveChecker().checkAll(resolvedArchitecture);
}
}
return resolvedArchitecture;
}
public static void checkAll(CNNArchCompilationUnitSymbol compilationUnit){
public static ArchitectureSymbol checkAll(CNNArchCompilationUnitSymbol compilationUnit){
ArchitectureSymbol resolvedArchitecture = null;
ASTCNNArchNode node = (ASTCNNArchNode) compilationUnit.getAstNode().get();
int findings = Log.getFindings().size();
createPreResolveChecker().checkAll(node);
if (findings == Log.getFindings().size()){
compilationUnit.getArchitecture().resolve();
resolvedArchitecture = compilationUnit.getArchitecture().resolve();
if (findings == Log.getFindings().size()){
createPostResolveChecker().checkAll(node);
createPostResolveChecker().checkAll(resolvedArchitecture);
}
}
return resolvedArchitecture;
}
public static CNNArchCoCoChecker createPostResolveChecker() {
return new CNNArchCoCoChecker()
public static CNNArchExtendedCoCoChecker createPostResolveChecker() {
return new CNNArchExtendedCoCoChecker()
.addCoCo(new CheckIOType())
.addCoCo(new CheckLayerInputs())
.addCoCo(new CheckIOAccessAndIOMissing())
.addCoCo(new CheckUnusedASTIODeclaration())
.addCoCo(new CheckArchitectureFinished());
}
......
/**
*
* ******************************************************************************
* 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.monticar.cnnarch._cocos;
import de.monticore.lang.monticar.cnnarch._symboltable.ArchitectureSymbol;
import java.util.ArrayList;
import java.util.List;
public class CNNArchExtendedCoCoChecker{
private List<CNNArchSymbolCoCo> cocos = new ArrayList<>();
public List<CNNArchSymbolCoCo> getCocos() {
return cocos;
}
public CNNArchExtendedCoCoChecker addCoCo(CNNArchSymbolCoCo coco) {
getCocos().add(coco);
return this;
}
public void checkAll(ArchitectureSymbol resolvedArchitecture) {
if (!resolvedArchitecture.isResolved()){
throw new IllegalStateException("Architecture has to be resolved to check symboltable cocos");
}
for (CNNArchSymbolCoCo coco : getCocos()){
coco.check(resolvedArchitecture);
}
}
}
......@@ -20,20 +20,9 @@
*/
package de.monticore.lang.monticar.cnnarch._cocos;
import de.monticore.lang.monticar.cnnarch._ast.ASTIODeclaration;
import de.monticore.lang.monticar.cnnarch._symboltable.IODeclarationSymbol;
import de.monticore.lang.monticar.cnnarch.helper.ErrorCodes;
import de.se_rwth.commons.logging.Log;
import de.monticore.lang.monticar.cnnarch._symboltable.ArchitectureSymbol;
public class CheckUnusedASTIODeclaration implements CNNArchASTIODeclarationCoCo {
@Override
public void check(ASTIODeclaration node) {
IODeclarationSymbol ioDeclaration = (IODeclarationSymbol) node.getSymbol().get();
if (ioDeclaration.getConnectedLayers().isEmpty()){
Log.error("0" + ErrorCodes.MISSING_IO + " Input or output with name '" + ioDeclaration.getName() + "' was declared but not used. "
, ioDeclaration.getSourcePosition());
}
}
public interface CNNArchSymbolCoCo {
void check(ArchitectureSymbol architecture);
}
......@@ -25,23 +25,22 @@ import de.monticore.lang.monticar.cnnarch._symboltable.ArchitectureSymbol;
import de.monticore.lang.monticar.cnnarch.helper.ErrorCodes;
import de.se_rwth.commons.logging.Log;
public class CheckArchitectureFinished implements CNNArchASTArchitectureCoCo {
public class CheckArchitectureFinished implements CNNArchSymbolCoCo {
@Override
public void check(ASTArchitecture node) {
ArchitectureSymbol architecture = (ArchitectureSymbol) node.getSymbol().get();
public void check(ArchitectureSymbol architecture) {
if (!architecture.getBody().getOutputTypes().isEmpty()){
Log.error("0" + ErrorCodes.UNFINISHED_ARCHITECTURE + " The architecture is not finished. " +
"There are still open streams at the end of the architecture. "
, node.get_SourcePositionEnd());
, architecture.getSourcePosition());
}
if (architecture.getInputs().isEmpty()){
Log.error("0" + ErrorCodes.UNFINISHED_ARCHITECTURE + " The architecture has no inputs. "
, node.get_SourcePositionStart());
, architecture.getSourcePosition());
}
if (architecture.getOutputs().isEmpty()){
Log.error("0" + ErrorCodes.UNFINISHED_ARCHITECTURE + " The architecture has no outputs. "
, node.get_SourcePositionStart());
, architecture.getSourcePosition());
}
}
......
......@@ -34,12 +34,10 @@ import java.util.Optional;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
public class CheckIOAccessAndIOMissing implements CNNArchASTArchitectureCoCo {
public class CheckIOAccessAndIOMissing implements CNNArchSymbolCoCo {
@Override
public void check(ASTArchitecture node) {
ArchitectureSymbol architecture = (ArchitectureSymbol) node.getSymbol().get();
public void check(ArchitectureSymbol architecture) {
for (IODeclarationSymbol ioDeclaration : architecture.getIODeclarations()){
if (ioDeclaration.getArrayLength() == 1){
checkSingleIO(ioDeclaration);
......@@ -50,7 +48,6 @@ public class CheckIOAccessAndIOMissing implements CNNArchASTArchitectureCoCo {
}
}
private void checkSingleIO(IODeclarationSymbol ioDeclaration){
if (ioDeclaration.getConnectedLayers().isEmpty()){
Log.error("0" + ErrorCodes.MISSING_IO + " Input or output '" + ioDeclaration.getName() + "' was declared but never used."
......
......@@ -30,12 +30,10 @@ import de.se_rwth.commons.logging.Log;
import java.util.Optional;
public class CheckIOType implements CNNArchASTArchitectureCoCo {
public class CheckIOType implements CNNArchSymbolCoCo {
@Override
public void check(ASTArchitecture node) {
ArchitectureSymbol architecture = (ArchitectureSymbol) node.getSymbol().get();
public void check(ArchitectureSymbol architecture) {
for (IODeclarationSymbol ioDeclaration : architecture.getIODeclarations()){
check(ioDeclaration);
}
......
......@@ -23,10 +23,10 @@ package de.monticore.lang.monticar.cnnarch._cocos;
import de.monticore.lang.monticar.cnnarch._ast.ASTArchitecture;
import de.monticore.lang.monticar.cnnarch._symboltable.ArchitectureSymbol;
public class CheckLayerInputs implements CNNArchASTArchitectureCoCo {
public class CheckLayerInputs implements CNNArchSymbolCoCo {
@Override
public void check(ASTArchitecture node) {
ArchitectureSymbol architecture = (ArchitectureSymbol) node.getSymbol().get();
public void check(ArchitectureSymbol architecture) {
architecture.getBody().checkInput();
}
}
......@@ -22,6 +22,7 @@ package de.monticore.lang.monticar.cnnarch._symboltable;
import de.monticore.symboltable.CommonSymbol;
import de.monticore.symboltable.MutableScope;
import de.monticore.symboltable.Scope;
import de.monticore.symboltable.Symbol;
import java.util.*;
......@@ -359,13 +360,13 @@ abstract public class ArchExpressionSymbol extends CommonSymbol {
*/
abstract public boolean isResolved();
abstract public ArchExpressionSymbol copy();
protected void putInScope(MutableScope scope){
protected void putInScope(Scope scope){
Collection<Symbol> symbolsInScope = scope.getLocalSymbols().get(getName());
if (symbolsInScope == null || !symbolsInScope.contains(this)) {
scope.add(this);
scope.getAsMutableScope().add(this);
}
}
abstract protected ArchExpressionSymbol preResolveDeepCopy();
}
......@@ -21,6 +21,7 @@
package de.monticore.lang.monticar.cnnarch._symboltable;
import de.monticore.symboltable.MutableScope;
import de.monticore.symboltable.Scope;
import java.util.ArrayList;
import java.util.List;
......@@ -152,15 +153,6 @@ public class ArchRangeExpressionSymbol extends ArchAbstractSequenceExpression {
unresolvableVariables.addAll(getEndSymbol().getUnresolvableVariables());
}
public ArchRangeExpressionSymbol copy(){
ArchRangeExpressionSymbol copy = new ArchRangeExpressionSymbol();
copy.setParallel(isParallel());
copy.setStartSymbol(getStartSymbol().copy());
copy.setEndSymbol(getEndSymbol().copy());
copy.setUnresolvableVariables(getUnresolvableVariables());
return copy;
}
@Override
public String getTextualRepresentation() {
String separator = isParallel() ? "|" : "->";
......@@ -168,12 +160,25 @@ public class ArchRangeExpressionSymbol extends ArchAbstractSequenceExpression {
}
@Override
protected void putInScope(MutableScope scope) {
protected void putInScope(Scope scope) {
super.putInScope(scope);
getStartSymbol().putInScope(scope);
getEndSymbol().putInScope(scope);
}
@Override
public ArchRangeExpressionSymbol preResolveDeepCopy(){
ArchRangeExpressionSymbol copy = new ArchRangeExpressionSymbol();
if (getAstNode().isPresent()){
copy.setAstNode(getAstNode().get());
}
copy.setParallel(isParallel());
copy.setStartSymbol(getStartSymbol().preResolveDeepCopy());
copy.setEndSymbol(getEndSymbol().preResolveDeepCopy());
return copy;
}
public static ArchRangeExpressionSymbol of(ArchSimpleExpressionSymbol start, ArchSimpleExpressionSymbol end, boolean parallel){
ArchRangeExpressionSymbol sym = new ArchRangeExpressionSymbol();
sym.setStartSymbol(start);
......
......@@ -22,6 +22,7 @@ package de.monticore.lang.monticar.cnnarch._symboltable;
import com.google.common.base.Joiner;
import de.monticore.symboltable.MutableScope;
import de.monticore.symboltable.Scope;
import java.util.ArrayList;
import java.util.List;
......@@ -115,21 +116,6 @@ public class ArchSequenceExpressionSymbol extends ArchAbstractSequenceExpression
}
}
public ArchSequenceExpressionSymbol copy(){
ArchSequenceExpressionSymbol copy = new ArchSequenceExpressionSymbol();
List<List<ArchSimpleExpressionSymbol>> elementsCopy = new ArrayList<>(getElements().get().size());
for (List<ArchSimpleExpressionSymbol> serialList : getElements().get()){
List<ArchSimpleExpressionSymbol> serialListCopy = new ArrayList<>(serialList.size());
for (ArchSimpleExpressionSymbol element : serialList){
serialListCopy.add(element.copy());
}
elementsCopy.add(serialListCopy);
}
copy.setElements(getElements().get());
copy.setUnresolvableVariables(getUnresolvableVariables());
return copy;
}
@Override
public String getTextualRepresentation() {
List<String> parallelExpressions = new ArrayList<>();
......@@ -140,7 +126,7 @@ public class ArchSequenceExpressionSymbol extends ArchAbstractSequenceExpression
}
@Override
protected void putInScope(MutableScope scope) {
protected void putInScope(Scope scope) {
super.putInScope(scope);
for (List<ArchSimpleExpressionSymbol> serialList : _getElements()){
for (ArchSimpleExpressionSymbol element : serialList){
......@@ -149,6 +135,25 @@ public class ArchSequenceExpressionSymbol extends ArchAbstractSequenceExpression
}
}
@Override
public ArchSequenceExpressionSymbol preResolveDeepCopy(){
ArchSequenceExpressionSymbol copy = new ArchSequenceExpressionSymbol();
if (getAstNode().isPresent()){
copy.setAstNode(getAstNode().get());
}
List<List<ArchSimpleExpressionSymbol>> elementsCopy = new ArrayList<>(_getElements().size());
for (List<ArchSimpleExpressionSymbol> serialList : _getElements()){
List<ArchSimpleExpressionSymbol> serialListCopy = new ArrayList<>(serialList.size());
for (ArchSimpleExpressionSymbol element : serialList){
serialListCopy.add(element.preResolveDeepCopy());
}
elementsCopy.add(serialListCopy);
}
copy.setElements(elementsCopy);
return copy;
}
public static ArchSequenceExpressionSymbol of(List<List<ArchSimpleExpressionSymbol>> elements){
ArchSequenceExpressionSymbol sym = new ArchSequenceExpressionSymbol();
sym.setElements(elements);
......
......@@ -23,6 +23,9 @@ package de.monticore.lang.monticar.cnnarch._symboltable;
import de.monticore.lang.math.math._symboltable.expression.*;
import de.monticore.lang.monticar.cnnarch.helper.Calculator;
import de.monticore.lang.monticar.cnnarch.helper.Utils;
import de.monticore.symboltable.MutableScope;
import de.monticore.symboltable.Scope;
import de.monticore.symboltable.Symbol;
import java.util.*;
......@@ -214,11 +217,31 @@ public class ArchSimpleExpressionSymbol extends ArchExpressionSymbol {
return getValue().isPresent() || !getMathExpression().isPresent();
}
public ArchSimpleExpressionSymbol copy(){
@Override
protected void putInScope(Scope scope) {
super.putInScope(scope);
if (getMathExpression().isPresent()){
for (MathExpressionSymbol exp : Utils.createSubExpressionList(getMathExpression().get())) {
Collection<Symbol> symbolsInScope = scope.getLocalSymbols().get(exp.getName());
if (symbolsInScope == null || !symbolsInScope.contains(exp)) {
scope.getAsMutableScope().add(exp);
}
}
}
}
@Override
public ArchSimpleExpressionSymbol preResolveDeepCopy(){
ArchSimpleExpressionSymbol copy = new ArchSimpleExpressionSymbol();
copy.setMathExpression(mathExpression);
copy.setValue(value);
copy.setUnresolvableVariables(getUnresolvableVariables());
if (getAstNode().isPresent()){
copy.setAstNode(getAstNode().get());
}
if (getMathExpression().isPresent()){
copy.setMathExpression(Utils.copy(getMathExpression().get()));
}
else {
copy.setValue(value);
}
return copy;
}
......
......@@ -23,6 +23,8 @@ package de.monticore.lang.monticar.cnnarch._symboltable;
import de.monticore.lang.monticar.types2._ast.ASTElementType;
import de.monticore.symboltable.CommonSymbol;
import de.monticore.symboltable.MutableScope;
import de.monticore.symboltable.Scope;
import de.monticore.symboltable.Symbol;
import java.util.*;
......@@ -37,10 +39,9 @@ public class ArchTypeSymbol extends CommonSymbol {
private int channelIndex = -1;
private int heightIndex = -1;
private int widthIndex = -1;
private List<ArchSimpleExpressionSymbol> dimensions = new ArrayList<>();
public ArchTypeSymbol() {
super("", KIND);
ASTElementType elementType = new ASTElementType();
......@@ -190,6 +191,35 @@ public class ArchTypeSymbol extends CommonSymbol {
}
}
public void putInScope(Scope scope) {
Collection<Symbol> symbolsInScope = scope.getLocalSymbols().get(getName());
if (symbolsInScope == null || !symbolsInScope.contains(this)) {
scope.getAsMutableScope().add(this);
for (ArchSimpleExpressionSymbol dimension : getDimensionSymbols()){
dimension.putInScope(scope);
}
}
}
public ArchTypeSymbol preResolveDeepCopy() {
ArchTypeSymbol copy = new ArchTypeSymbol();
if (getAstNode().isPresent()){
copy.setAstNode(getAstNode().get());
}
copy.setElementType(getElementType());
copy.setWidthIndex(getWidthIndex());
copy.setChannelIndex(getChannelIndex());
copy.setHeightIndex(getHeightIndex());
List<ArchSimpleExpressionSymbol> dimensionCopies = new ArrayList<>();
for (ArchSimpleExpressionSymbol dimension : getDimensionSymbols()){
dimensionCopies.add(dimension.preResolveDeepCopy());
}
copy.setDimensionSymbols(dimensionCopies);
return copy;
}
public static class Builder{
private int height = 1;
private int width = 1;
......
......@@ -23,7 +23,12 @@
package de.monticore.lang.monticar.cnnarch._symboltable;
import de.monticore.lang.monticar.cnnarch.helper.Utils;
import de.monticore.lang.monticar.cnnarch.predefined.AllPredefinedMethods;
import de.monticore.lang.monticar.cnnarch.predefined.AllPredefinedVariables;
import de.monticore.symboltable.CommonScopeSpanningSymbol;
import de.monticore.symboltable.Scope;
import de.monticore.symboltable.Symbol;
import javax.annotation.Nullable;
import java.util.*;
......@@ -36,11 +41,16 @@ public class ArchitectureSymbol extends CommonScopeSpanningSymbol {
private List<IOLayerSymbol> inputs = new ArrayList<>();
private List<IOLayerSymbol> outputs = new ArrayList<>();
private Map<String, IODeclarationSymbol> ioDeclarationMap = new HashMap<>();
private boolean isCopy = false;
public ArchitectureSymbol() {
super("", KIND);
}
public ArchitectureSymbol(String name) {
super(name, KIND);
}
public LayerSymbol getBody() {
return body;
}
......@@ -57,6 +67,10 @@ public class ArchitectureSymbol extends CommonScopeSpanningSymbol {
return outputs;
}
public boolean isCopy() {
return isCopy;
}
//called in IOLayer to get IODeclaration; only null if error; will be checked in coco CheckIOName
@Nullable
protected IODeclarationSymbol resolveIODeclaration(String name){
......@@ -80,16 +94,30 @@ public class ArchitectureSymbol extends CommonScopeSpanningSymbol {
return getSpannedScope().resolveLocally(MethodDeclarationSymbol.KIND);
}
public void resolve(){
getBody().checkIfResolvable();
try{
getBody().resolveOrError();
//useful to resolve the architecture in a component instance
public ArchitectureSymbol resolveInScope(Scope scope){
if (isCopy()){
getBody().checkIfResolvable();
try{
getBody().resolveOrError();
}
catch (ArchResolveException e){
//do nothing; error is already logged
}
return this;
}
catch (ArchResolveException e){
//do nothing; error is already logged
else {
ArchitectureSymbol copy = preResolveDeepCopy();
copy.putInScope(scope);
copy.resolve();
return copy;
}
}
public ArchitectureSymbol resolve(){
return resolveInScope(getEnclosingScope());
}
public List<LayerSymbol> getFirstLayers(){
if (!getBody().isResolved()){
resolve();
......@@ -104,4 +132,35 @@ public class ArchitectureSymbol extends CommonScopeSpanningSymbol {
public boolean isResolvable(){
return getBody().isResolvable();
}
public void putInScope(Scope scope){
Collection<Symbol> symbolsInScope = scope.getLocalSymbols().get(getName());
if (symbolsInScope == null || !symbolsInScope.contains(this)){
scope.getAsMutableScope().add(this);
getSpannedScope().getAsMutableScope().setResolvingFilters(scope.getResolvingFilters());
Utils.recursiveSetResolvingFilters(getSpannedScope(), scope.getResolvingFilters());
}
}
public ArchitectureSymbol preResolveDeepCopy(){
ArchitectureSymbol copy = new ArchitectureSymbol("instance");
copy.setBody(getBody().preResolveDeepCopy());
if (getAstNode().isPresent()){
copy.setAstNode(getAstNode().get());
}
copy.getSpannedScope().getAsMutableScope().add(AllPredefinedVariables.createTrueConstant());
copy.getSpannedScope().getAsMutableScope().add(AllPredefinedVariables.createFalseConstant());
for (MethodDeclarationSymbol methodDeclaration : AllPredefinedMethods.createList()){
copy.getSpannedScope().getAsMutableScope().add(methodDeclaration);
}
for (MethodDeclarationSymbol methodDeclaration : getSpannedScope().<MethodDeclarationSymbol>resolveLocally(MethodDeclarationSymbol.KIND)){
if (!methodDeclaration.isPredefined()) {
copy.getSpannedScope().getAsMutableScope().add(methodDeclaration.deepCopy());
}
}
copy.getBody().putInScope(copy.getSpannedScope());
copy.isCopy = true;
return copy;
}
}
......@@ -177,14 +177,13 @@ public class ArgumentSymbol extends CommonSymbol {
}
}
public ArgumentSymbol copy(){
ArgumentSymbol copy = new Builder()
.parameter(getParameter())
.value(getRhs().copy())
.build();
protected ArgumentSymbol preResolveDeepCopy(){
ArgumentSymbol copy = new ArgumentSymbol(getName());
if (getAstNode().isPresent()){
copy.setAstNode(getAstNode().get());
}
copy.setRhs(getRhs().preResolveDeepCopy());
return copy;
}
......
......@@ -21,16 +21,20 @@
package de.monticore.lang.monticar.cnnarch._symboltable;
import de.monticore.lang.monticar.cnnarch.helper.ErrorCodes;
import de.monticore.lang.monticar.cnnarch.helper.Utils;
import de.se_rwth.commons.logging.Log;
import org.jscience.mathematics.number.Rational;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
public class CNNArchCompilationUnitSymbol extends CNNArchCompilationUnitSymbolTOP{
private List<VariableSymbol> parameters;
private List<IODeclarationSymbol> ioDeclarations;
private ArchitectureSymbol architecture;
private boolean isCopy = false;
public CNNArchCompilationUnitSymbol(String name) {
super(name);
......@@ -44,17 +48,29 @@ public class CNNArchCompilationUnitSymbol extends CNNArchCompilationUnitSymbolTO
this.architecture = architecture;
}
public List<IODeclarationSymbol> getIoDeclarations() {
return ioDeclarations;
}
public void setIoDeclarations(List<IODeclarationSymbol> ioDeclarations) {
this.ioDeclarations = ioDeclarations;
}