Commit c7034e15 authored by Thomas Michael Timmermanns's avatar Thomas Michael Timmermanns Committed by Thomas Michael Timmermanns
Browse files

Improved instance creation.

Modified cocos such that they work with instances.
parent 461ee8ec
...@@ -28,46 +28,55 @@ import de.se_rwth.commons.logging.Log; ...@@ -28,46 +28,55 @@ import de.se_rwth.commons.logging.Log;
//check all cocos //check all cocos
public class CNNArchCocos { public class CNNArchCocos {
public static ArchitectureSymbol checkAll(ArchitectureSymbol architecture){ public static void checkAll(ArchitectureSymbol architecture){
ArchitectureSymbol resolvedArchitecture = null;
ASTCNNArchNode node = (ASTCNNArchNode) architecture.getAstNode().get(); ASTCNNArchNode node = (ASTCNNArchNode) architecture.getAstNode().get();
int findings = Log.getFindings().size(); int findings = Log.getFindings().size();
createPreResolveChecker().checkAll(node); createASTChecker().checkAll(node);
if (findings == Log.getFindings().size()){ if (findings == Log.getFindings().size()) {
resolvedArchitecture = architecture.resolve(); createCNNArchPreResolveSymbolChecker().checkAll(architecture);
if (findings == Log.getFindings().size()){ if (findings == Log.getFindings().size()) {
createPostResolveChecker().checkAll(resolvedArchitecture); architecture.resolve();
if (findings == Log.getFindings().size()) {
createCNNArchPostResolveSymbolChecker().checkAll(architecture);
}
} }
} }
return resolvedArchitecture;
} }
public static ArchitectureSymbol checkAll(CNNArchCompilationUnitSymbol compilationUnit){ public static void checkAll(CNNArchCompilationUnitSymbol compilationUnit){
ArchitectureSymbol resolvedArchitecture = null;
ASTCNNArchNode node = (ASTCNNArchNode) compilationUnit.getAstNode().get(); ASTCNNArchNode node = (ASTCNNArchNode) compilationUnit.getAstNode().get();
int findings = Log.getFindings().size(); int findings = Log.getFindings().size();
createPreResolveChecker().checkAll(node); createASTChecker().checkAll(node);
if (findings == Log.getFindings().size()){ if (findings == Log.getFindings().size()) {
resolvedArchitecture = compilationUnit.getArchitecture().resolve(); createCNNArchPreResolveSymbolChecker().checkAll(compilationUnit);
if (findings == Log.getFindings().size()){ if (findings == Log.getFindings().size()) {
createPostResolveChecker().checkAll(resolvedArchitecture); compilationUnit.getArchitecture().resolve();
if (findings == Log.getFindings().size()) {
createCNNArchPostResolveSymbolChecker().checkAll(compilationUnit);
}
} }
} }
return resolvedArchitecture;
} }
public static CNNArchExtendedCoCoChecker createPostResolveChecker() { //checks cocos based on symbols after the resolve method of the ArchitectureSymbol is called
return new CNNArchExtendedCoCoChecker() public static CNNArchSymbolCoCoChecker createCNNArchPostResolveSymbolChecker() {
return new CNNArchSymbolCoCoChecker()
.addCoCo(new CheckIOType()) .addCoCo(new CheckIOType())
.addCoCo(new CheckLayerInputs()) .addCoCo(new CheckLayerInputs())
.addCoCo(new CheckIOAccessAndIOMissing()) .addCoCo(new CheckIOAccessAndIOMissing())
.addCoCo(new CheckArchitectureFinished()); .addCoCo(new CheckArchitectureFinished());
} }
public static CNNArchCoCoChecker createPreResolveChecker() { //checks cocos based on symbols before the resolve method of the ArchitectureSymbol is called
return new CNNArchCoCoChecker() public static CNNArchSymbolCoCoChecker createCNNArchPreResolveSymbolChecker() {
return new CNNArchSymbolCoCoChecker()
.addCoCo(new CheckIOName()) .addCoCo(new CheckIOName())
.addCoCo(new CheckNameExpression()) .addCoCo(new CheckNameExpression());
}
//checks all normal cocos
public static CNNArchCoCoChecker createASTChecker() {
return new CNNArchCoCoChecker()
.addCoCo(new CheckMethodLayer()) .addCoCo(new CheckMethodLayer())
.addCoCo(new CheckRangeOperators()) .addCoCo(new CheckRangeOperators())
.addCoCo(new CheckVariableName()) .addCoCo(new CheckVariableName())
......
...@@ -20,9 +20,87 @@ ...@@ -20,9 +20,87 @@
*/ */
package de.monticore.lang.monticar.cnnarch._cocos; package de.monticore.lang.monticar.cnnarch._cocos;
import de.monticore.lang.monticar.cnnarch._symboltable.ArchitectureSymbol; import de.monticore.lang.math.math._symboltable.expression.MathExpressionSymbol;
import de.monticore.lang.monticar.cnnarch._symboltable.*;
import de.monticore.symboltable.Symbol;
public interface CNNArchSymbolCoCo { public class CNNArchSymbolCoCo {
void check(ArchitectureSymbol architecture); public void check(Symbol sym){
if (sym instanceof ArchitectureSymbol){
check((ArchitectureSymbol) sym);
}
else if (sym instanceof MethodDeclarationSymbol){
check((MethodDeclarationSymbol) sym);
}
else if (sym instanceof LayerSymbol){
check((LayerSymbol) sym);
}
else if (sym instanceof ArchExpressionSymbol){
check((ArchExpressionSymbol) sym);
}
else if (sym instanceof ArchTypeSymbol){
check((ArchTypeSymbol) sym);
}
else if (sym instanceof VariableSymbol){
check((VariableSymbol) sym);
}
else if (sym instanceof ArgumentSymbol){
check((ArgumentSymbol) sym);
}
else if (sym instanceof CNNArchCompilationUnitSymbol){
check((CNNArchCompilationUnitSymbol) sym);
}
else if (sym instanceof IODeclarationSymbol){
check((IODeclarationSymbol) sym);
}
else if (sym instanceof MathExpressionSymbol){
check((MathExpressionSymbol) sym);
}
else{
throw new IllegalStateException("Symbol class is unknown in CNNArchSymbolCoCo: "
+ sym.getClass().getSimpleName());
}
}
public void check(ArchitectureSymbol sym){
//Override if needed
}
public void check(MethodDeclarationSymbol sym){
//Override if needed
}
public void check(LayerSymbol sym){
//Override if needed
}
public void check(ArchExpressionSymbol sym){
//Override if needed
}
public void check(ArchTypeSymbol sym){
//Override if needed
}
public void check(VariableSymbol sym){
//Override if needed
}
public void check(ArgumentSymbol sym){
//Override if needed
}
public void check(CNNArchCompilationUnitSymbol sym){
//Override if needed
}
public void check(IODeclarationSymbol sym){
//Override if needed
}
public void check(MathExpressionSymbol sym){
//Override if needed
}
} }
...@@ -20,12 +20,13 @@ ...@@ -20,12 +20,13 @@
*/ */
package de.monticore.lang.monticar.cnnarch._cocos; package de.monticore.lang.monticar.cnnarch._cocos;
import de.monticore.lang.monticar.cnnarch._symboltable.ArchitectureSymbol; import de.monticore.symboltable.Scope;
import de.monticore.symboltable.ScopeSpanningSymbol;
import de.monticore.symboltable.Symbol;
import java.util.ArrayList; import java.util.*;
import java.util.List;
public class CNNArchExtendedCoCoChecker{ public class CNNArchSymbolCoCoChecker {
private List<CNNArchSymbolCoCo> cocos = new ArrayList<>(); private List<CNNArchSymbolCoCo> cocos = new ArrayList<>();
...@@ -33,17 +34,42 @@ public class CNNArchExtendedCoCoChecker{ ...@@ -33,17 +34,42 @@ public class CNNArchExtendedCoCoChecker{
return cocos; return cocos;
} }
public CNNArchExtendedCoCoChecker addCoCo(CNNArchSymbolCoCo coco) { public CNNArchSymbolCoCoChecker addCoCo(CNNArchSymbolCoCo coco) {
getCocos().add(coco); getCocos().add(coco);
return this; return this;
} }
public void checkAll(ArchitectureSymbol resolvedArchitecture) { public void checkAll(Symbol sym) {
if (!resolvedArchitecture.isResolved()){ handle(sym, new HashSet<>());
throw new IllegalStateException("Architecture has to be resolved to check symboltable cocos"); }
}
public void check(Symbol sym){
for (CNNArchSymbolCoCo coco : getCocos()){ for (CNNArchSymbolCoCo coco : getCocos()){
coco.check(resolvedArchitecture); coco.check(sym);
}
}
public void handle(Symbol sym, Set<Symbol> checkedSymbols){
if (!checkedSymbols.contains(sym)) {
check(sym);
checkedSymbols.add(sym);
if (sym instanceof ScopeSpanningSymbol) {
traverse(((ScopeSpanningSymbol) sym).getSpannedScope(), checkedSymbols);
}
} }
} }
public void traverse(Scope scope, Set<Symbol> checkedSymbols){
for (Collection<Symbol> collection : scope.getLocalSymbols().values()){
for (Symbol sym : collection){
handle(sym, checkedSymbols);
}
}
for (Scope subScope : scope.getSubScopes()){
if (!subScope.isSpannedBySymbol()){
traverse(subScope, checkedSymbols);
}
}
}
} }
...@@ -20,12 +20,11 @@ ...@@ -20,12 +20,11 @@
*/ */
package de.monticore.lang.monticar.cnnarch._cocos; package de.monticore.lang.monticar.cnnarch._cocos;
import de.monticore.lang.monticar.cnnarch._ast.ASTArchitecture;
import de.monticore.lang.monticar.cnnarch._symboltable.ArchitectureSymbol; import de.monticore.lang.monticar.cnnarch._symboltable.ArchitectureSymbol;
import de.monticore.lang.monticar.cnnarch.helper.ErrorCodes; import de.monticore.lang.monticar.cnnarch.helper.ErrorCodes;
import de.se_rwth.commons.logging.Log; import de.se_rwth.commons.logging.Log;
public class CheckArchitectureFinished implements CNNArchSymbolCoCo { public class CheckArchitectureFinished extends CNNArchSymbolCoCo {
@Override @Override
public void check(ArchitectureSymbol architecture) { public void check(ArchitectureSymbol architecture) {
......
...@@ -20,7 +20,6 @@ ...@@ -20,7 +20,6 @@
*/ */
package de.monticore.lang.monticar.cnnarch._cocos; package de.monticore.lang.monticar.cnnarch._cocos;
import de.monticore.lang.monticar.cnnarch._ast.ASTArchitecture;
import de.monticore.lang.monticar.cnnarch._symboltable.ArchitectureSymbol; import de.monticore.lang.monticar.cnnarch._symboltable.ArchitectureSymbol;
import de.monticore.lang.monticar.cnnarch._symboltable.IODeclarationSymbol; import de.monticore.lang.monticar.cnnarch._symboltable.IODeclarationSymbol;
import de.monticore.lang.monticar.cnnarch._symboltable.IOLayerSymbol; import de.monticore.lang.monticar.cnnarch._symboltable.IOLayerSymbol;
...@@ -34,7 +33,7 @@ import java.util.Optional; ...@@ -34,7 +33,7 @@ import java.util.Optional;
import java.util.stream.Collectors; import java.util.stream.Collectors;
import java.util.stream.IntStream; import java.util.stream.IntStream;
public class CheckIOAccessAndIOMissing implements CNNArchSymbolCoCo { public class CheckIOAccessAndIOMissing extends CNNArchSymbolCoCo {
@Override @Override
public void check(ArchitectureSymbol architecture) { public void check(ArchitectureSymbol architecture) {
......
...@@ -20,8 +20,9 @@ ...@@ -20,8 +20,9 @@
*/ */
package de.monticore.lang.monticar.cnnarch._cocos; package de.monticore.lang.monticar.cnnarch._cocos;
import de.monticore.lang.monticar.cnnarch._ast.ASTIOLayer;
import de.monticore.lang.monticar.cnnarch._symboltable.IODeclarationSymbol; import de.monticore.lang.monticar.cnnarch._symboltable.IODeclarationSymbol;
import de.monticore.lang.monticar.cnnarch._symboltable.IOLayerSymbol;
import de.monticore.lang.monticar.cnnarch._symboltable.LayerSymbol;
import de.monticore.lang.monticar.cnnarch.helper.ErrorCodes; import de.monticore.lang.monticar.cnnarch.helper.ErrorCodes;
import de.se_rwth.commons.logging.Log; import de.se_rwth.commons.logging.Log;
...@@ -29,18 +30,24 @@ import java.util.Collection; ...@@ -29,18 +30,24 @@ import java.util.Collection;
import java.util.HashSet; import java.util.HashSet;
import java.util.Set; import java.util.Set;
public class CheckIOName implements CNNArchASTIOLayerCoCo { public class CheckIOName extends CNNArchSymbolCoCo {
private Set<IODeclarationSymbol> checkedIODeclarations = new HashSet<>(); private Set<IODeclarationSymbol> checkedIODeclarations = new HashSet<>();
@Override @Override
public void check(ASTIOLayer node) { public void check(LayerSymbol sym) {
Collection<IODeclarationSymbol> ioDeclarations = node.getEnclosingScope().get().<IODeclarationSymbol>resolveMany(node.getName(), IODeclarationSymbol.KIND); if (sym instanceof IOLayerSymbol){
checkIOLayer((IOLayerSymbol) sym);
}
}
public void checkIOLayer(IOLayerSymbol ioLayer) {
Collection<IODeclarationSymbol> ioDeclarations = ioLayer.getEnclosingScope().resolveMany(ioLayer.getName(), IODeclarationSymbol.KIND);
if (ioDeclarations.isEmpty()){ if (ioDeclarations.isEmpty()){
Log.error("0" + ErrorCodes.UNKNOWN_IO + " Unknown input or output name. " + Log.error("0" + ErrorCodes.UNKNOWN_IO + " Unknown input or output name. " +
"The input or output '" + node.getName() + "' does not exist" "The input or output '" + ioLayer.getName() + "' does not exist"
, node.get_SourcePositionStart()); , ioLayer.getSourcePosition());
} }
else { else {
IODeclarationSymbol ioDeclaration = ioDeclarations.iterator().next(); IODeclarationSymbol ioDeclaration = ioDeclarations.iterator().next();
......
...@@ -20,7 +20,6 @@ ...@@ -20,7 +20,6 @@
*/ */
package de.monticore.lang.monticar.cnnarch._cocos; package de.monticore.lang.monticar.cnnarch._cocos;
import de.monticore.lang.monticar.cnnarch._ast.ASTArchitecture;
import de.monticore.lang.monticar.cnnarch._symboltable.ArchSimpleExpressionSymbol; import de.monticore.lang.monticar.cnnarch._symboltable.ArchSimpleExpressionSymbol;
import de.monticore.lang.monticar.cnnarch._symboltable.ArchTypeSymbol; import de.monticore.lang.monticar.cnnarch._symboltable.ArchTypeSymbol;
import de.monticore.lang.monticar.cnnarch._symboltable.ArchitectureSymbol; import de.monticore.lang.monticar.cnnarch._symboltable.ArchitectureSymbol;
...@@ -30,16 +29,16 @@ import de.se_rwth.commons.logging.Log; ...@@ -30,16 +29,16 @@ import de.se_rwth.commons.logging.Log;
import java.util.Optional; import java.util.Optional;
public class CheckIOType implements CNNArchSymbolCoCo { public class CheckIOType extends CNNArchSymbolCoCo {
@Override @Override
public void check(ArchitectureSymbol architecture) { public void check(ArchitectureSymbol architecture) {
for (IODeclarationSymbol ioDeclaration : architecture.getIODeclarations()){ for (IODeclarationSymbol ioDeclaration : architecture.getIODeclarations()){
check(ioDeclaration); checkIO(ioDeclaration);
} }
} }
public void check(IODeclarationSymbol ioDeclaration) { public void checkIO(IODeclarationSymbol ioDeclaration) {
ArchTypeSymbol type = ioDeclaration.getType(); ArchTypeSymbol type = ioDeclaration.getType();
if (type.getElementType().isIsComplex() || type.getElementType().isIsBoolean()){ if (type.getElementType().isIsComplex() || type.getElementType().isIsBoolean()){
......
...@@ -20,10 +20,9 @@ ...@@ -20,10 +20,9 @@
*/ */
package de.monticore.lang.monticar.cnnarch._cocos; package de.monticore.lang.monticar.cnnarch._cocos;
import de.monticore.lang.monticar.cnnarch._ast.ASTArchitecture;
import de.monticore.lang.monticar.cnnarch._symboltable.ArchitectureSymbol; import de.monticore.lang.monticar.cnnarch._symboltable.ArchitectureSymbol;
public class CheckLayerInputs implements CNNArchSymbolCoCo { public class CheckLayerInputs extends CNNArchSymbolCoCo {
@Override @Override
public void check(ArchitectureSymbol architecture) { public void check(ArchitectureSymbol architecture) {
......
...@@ -22,7 +22,7 @@ package de.monticore.lang.monticar.cnnarch._cocos; ...@@ -22,7 +22,7 @@ package de.monticore.lang.monticar.cnnarch._cocos;
import de.monticore.lang.math.math._symboltable.expression.MathExpressionSymbol; import de.monticore.lang.math.math._symboltable.expression.MathExpressionSymbol;
import de.monticore.lang.math.math._symboltable.expression.MathNameExpressionSymbol; import de.monticore.lang.math.math._symboltable.expression.MathNameExpressionSymbol;
import de.monticore.lang.monticar.cnnarch._ast.ASTArchSimpleExpression; import de.monticore.lang.monticar.cnnarch._symboltable.ArchExpressionSymbol;
import de.monticore.lang.monticar.cnnarch._symboltable.ArchSimpleExpressionSymbol; import de.monticore.lang.monticar.cnnarch._symboltable.ArchSimpleExpressionSymbol;
import de.monticore.lang.monticar.cnnarch._symboltable.VariableSymbol; import de.monticore.lang.monticar.cnnarch._symboltable.VariableSymbol;
import de.monticore.lang.monticar.cnnarch.helper.ErrorCodes; import de.monticore.lang.monticar.cnnarch.helper.ErrorCodes;
...@@ -31,18 +31,23 @@ import de.se_rwth.commons.logging.Log; ...@@ -31,18 +31,23 @@ import de.se_rwth.commons.logging.Log;
import java.util.Collection; import java.util.Collection;
public class CheckNameExpression implements CNNArchASTArchSimpleExpressionCoCo { public class CheckNameExpression extends CNNArchSymbolCoCo {
@Override @Override
public void check(ASTArchSimpleExpression node) { public void check(ArchExpressionSymbol sym) {
ArchSimpleExpressionSymbol expression = (ArchSimpleExpressionSymbol) node.getSymbol().get(); if (sym instanceof ArchSimpleExpressionSymbol){
checkSimpleExpression((ArchSimpleExpressionSymbol) sym);
}
}
public void checkSimpleExpression(ArchSimpleExpressionSymbol expression) {
if (expression.getMathExpression().isPresent()){ if (expression.getMathExpression().isPresent()){
MathExpressionSymbol mathExpression = expression.getMathExpression().get(); MathExpressionSymbol mathExpression = expression.getMathExpression().get();
for (MathExpressionSymbol subMathExp : Utils.createSubExpressionList(mathExpression)){ for (MathExpressionSymbol subMathExp : Utils.createSubExpressionList(mathExpression)){
if (subMathExp instanceof MathNameExpressionSymbol){ if (subMathExp instanceof MathNameExpressionSymbol){
String name = ((MathNameExpressionSymbol) subMathExp).getNameToAccess(); String name = ((MathNameExpressionSymbol) subMathExp).getNameToAccess();
Collection<VariableSymbol> variableCollection = node.getEnclosingScope().get().resolveMany(name, VariableSymbol.KIND); Collection<VariableSymbol> variableCollection = expression.getEnclosingScope().resolveMany(name, VariableSymbol.KIND);
if (variableCollection.isEmpty()){ if (variableCollection.isEmpty()){
Log.error("0" + ErrorCodes.UNKNOWN_VARIABLE_NAME + " Unknown variable name. " + Log.error("0" + ErrorCodes.UNKNOWN_VARIABLE_NAME + " Unknown variable name. " +
......
...@@ -21,7 +21,6 @@ ...@@ -21,7 +21,6 @@
package de.monticore.lang.monticar.cnnarch._symboltable; package de.monticore.lang.monticar.cnnarch._symboltable;
import de.monticore.symboltable.CommonSymbol; import de.monticore.symboltable.CommonSymbol;
import de.monticore.symboltable.MutableScope;
import de.monticore.symboltable.Scope; import de.monticore.symboltable.Scope;
import de.monticore.symboltable.Symbol; import de.monticore.symboltable.Symbol;
......
...@@ -20,7 +20,6 @@ ...@@ -20,7 +20,6 @@
*/ */
package de.monticore.lang.monticar.cnnarch._symboltable; package de.monticore.lang.monticar.cnnarch._symboltable;
import de.monticore.symboltable.MutableScope;
import de.monticore.symboltable.Scope; import de.monticore.symboltable.Scope;
import java.util.ArrayList; import java.util.ArrayList;
......
...@@ -21,7 +21,6 @@ ...@@ -21,7 +21,6 @@
package de.monticore.lang.monticar.cnnarch._symboltable; package de.monticore.lang.monticar.cnnarch._symboltable;
import com.google.common.base.Joiner; import com.google.common.base.Joiner;
import de.monticore.symboltable.MutableScope;
import de.monticore.symboltable.Scope; import de.monticore.symboltable.Scope;
import java.util.ArrayList; import java.util.ArrayList;
......
...@@ -23,9 +23,9 @@ package de.monticore.lang.monticar.cnnarch._symboltable; ...@@ -23,9 +23,9 @@ package de.monticore.lang.monticar.cnnarch._symboltable;
import de.monticore.lang.math.math._symboltable.expression.*; import de.monticore.lang.math.math._symboltable.expression.*;
import de.monticore.lang.monticar.cnnarch.helper.Calculator; import de.monticore.lang.monticar.cnnarch.helper.Calculator;
import de.monticore.lang.monticar.cnnarch.helper.Utils; import de.monticore.lang.monticar.cnnarch.helper.Utils;
import de.monticore.symboltable.MutableScope;
import de.monticore.symboltable.Scope; import de.monticore.symboltable.Scope;
import de.monticore.symboltable.Symbol; import de.monticore.symboltable.Symbol;
import org.jscience.mathematics.number.Rational;
import java.util.*; import java.util.*;
...@@ -246,6 +246,17 @@ public class ArchSimpleExpressionSymbol extends ArchExpressionSymbol { ...@@ -246,6 +246,17 @@ public class ArchSimpleExpressionSymbol extends ArchExpressionSymbol {
} }
public static ArchSimpleExpressionSymbol of(Rational value){
ArchSimpleExpressionSymbol res = new ArchSimpleExpressionSymbol();
if (value.getDivisor().intValue() == 1){
res.setValue(value.getDividend().intValue());