Skip to content

Commit

Permalink
Adapt to typecheck 3
Browse files Browse the repository at this point in the history
  • Loading branch information
schmalzing committed Oct 28, 2024
1 parent 1c4b49c commit 41a1dc9
Show file tree
Hide file tree
Showing 13 changed files with 308 additions and 139 deletions.
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
/* (c) https://github.com/MontiCore/monticore */
package de.monticore.statements.mccommonstatements.cocos;

import com.google.common.base.Preconditions;
import de.monticore.statements.mcassertstatements._ast.ASTAssertStatement;
import de.monticore.statements.mcassertstatements._cocos.MCAssertStatementsASTAssertStatementCoCo;
import de.monticore.types.check.SymTypeExpression;
import de.monticore.types.check.TypeCheck;
import de.monticore.types.check.TypeCalculator;
import de.monticore.types3.SymTypeRelations;
import de.monticore.types3.TypeCheck3;
import de.se_rwth.commons.logging.Log;

public class AssertIsValid implements MCAssertStatementsASTAssertStatementCoCo {
Expand All @@ -17,27 +19,46 @@ public class AssertIsValid implements MCAssertStatementsASTAssertStatementCoCo {
public static final String ERROR_CODE_2 = "0xA0902";

public static final String ERROR_MSG_FORMAT_2 = "Assert-statement must not be of void type.";

TypeCalculator typeCheck;

public AssertIsValid(TypeCalculator typeCheck){


/**
* @deprecated use default constructor
*/
@Deprecated
public AssertIsValid(TypeCalculator typeCheck) {
this.typeCheck = typeCheck;

}

@Override
public void check(ASTAssertStatement node) {
Preconditions.checkNotNull(node);

SymTypeExpression assertion;

if (typeCheck != null) {
// support deprecated behavior
assertion = typeCheck.typeOf(node.getAssertion());
} else {
assertion = TypeCheck3.typeOf(node.getAssertion());
}

SymTypeExpression result = typeCheck.typeOf(node.getAssertion());

if(!TypeCheck.isBoolean(result)){
if(!SymTypeRelations.isBoolean(assertion)){
Log.error(ERROR_CODE + ERROR_MSG_FORMAT, node.getAssertion().get_SourcePositionStart());
}

if(node.isPresentMessage()) {
result = typeCheck.typeOf(node.getMessage());
if (!TypeCheck.isVoid(result)) {

SymTypeExpression message;

if (typeCheck != null) {
// support deprecated behavior
message = typeCheck.typeOf(node.getMessage());
} else {
message = TypeCheck3.typeOf(node.getMessage());
}

if (message.isVoidType()) {
Log.error(ERROR_CODE_2 + ERROR_MSG_FORMAT_2, node.getMessage().get_SourcePositionStart());
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,30 +1,32 @@
/* (c) https://github.com/MontiCore/monticore */
package de.monticore.statements.mccommonstatements.cocos;

import com.google.common.base.Preconditions;
import de.monticore.statements.mcexceptionstatements._ast.ASTCatchClause;
import de.monticore.statements.mcexceptionstatements._cocos.MCExceptionStatementsASTCatchClauseCoCo;
import de.monticore.types.check.SymTypeExpression;
import de.monticore.types.check.SymTypeExpressionFactory;
import de.monticore.types.check.TypeCheck;
import de.monticore.types.check.TypeCalculator;
import de.monticore.types3.SymTypeRelations;
import de.se_rwth.commons.logging.Log;

public class CatchIsValid implements MCExceptionStatementsASTCatchClauseCoCo {

TypeCalculator typeCheck;

public static final String ERROR_CODE = "0xA0903";

public static final String ERROR_MSG_FORMAT = "Parameter in catch-statement has to be throwable or subtype of it.";

public CatchIsValid(TypeCalculator typeCheck){

this.typeCheck = typeCheck;

}


/**
* @deprecated use default constructor
*/
@Deprecated
public CatchIsValid(TypeCalculator typeCheck) { }

public CatchIsValid() { }

@Override
public void check(ASTCatchClause node) {
Preconditions.checkNotNull(node);

SymTypeExpression throwable = SymTypeExpressionFactory.createTypeObject("java.lang.Throwable", node.getEnclosingScope());

Expand All @@ -33,7 +35,7 @@ public void check(ASTCatchClause node) {

SymTypeExpression result = SymTypeExpressionFactory.createTypeObject(name, node.getEnclosingScope());

if (!TypeCheck.isSubtypeOf(result, throwable)) {
if (!SymTypeRelations.isSubTypeOf(result, throwable)) {
Log.error(ERROR_CODE + ERROR_MSG_FORMAT, node.get_SourcePositionStart());
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,33 +1,50 @@
/* (c) https://github.com/MontiCore/monticore */
package de.monticore.statements.mccommonstatements.cocos;

import com.google.common.base.Preconditions;
import de.monticore.statements.mccommonstatements._ast.ASTDoWhileStatement;
import de.monticore.statements.mccommonstatements._cocos.MCCommonStatementsASTDoWhileStatementCoCo;
import de.monticore.types.check.SymTypeExpression;
import de.monticore.types.check.TypeCheck;
import de.monticore.types.check.TypeCalculator;
import de.monticore.types3.SymTypeRelations;
import de.monticore.types3.TypeCheck3;
import de.se_rwth.commons.logging.Log;

public class DoWhileConditionHasBooleanType implements MCCommonStatementsASTDoWhileStatementCoCo {


@Deprecated
TypeCalculator typeCheck;

public static final String ERROR_CODE = "0xA0905";

public static final String ERROR_MSG_FORMAT = "Condition in do-statement must be a boolean expression.";

public DoWhileConditionHasBooleanType(TypeCalculator typeCheck){

/**
* @deprecated use default constructor
*/
@Deprecated
public DoWhileConditionHasBooleanType(TypeCalculator typeCheck) {
this.typeCheck = typeCheck;
}


public DoWhileConditionHasBooleanType() { }

@Override
public void check(ASTDoWhileStatement node) {

SymTypeExpression result = typeCheck.typeOf(node.getCondition());

if(!TypeCheck.isBoolean(result)){
Preconditions.checkNotNull(node);

SymTypeExpression result;

if (typeCheck != null) {
// support deprecated behavior
result = typeCheck.typeOf(node.getCondition());
} else {
result = TypeCheck3.typeOf(node.getCondition());
}

if (!SymTypeRelations.isBoolean(result)) {
Log.error(ERROR_CODE + ERROR_MSG_FORMAT, node.get_SourcePositionStart());
}

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,22 +22,22 @@ public ExpressionStatementIsValid(TypeCalculator typeCheck) {
this.typeCheck = typeCheck;
}

public ExpressionStatementIsValid() {
}
public ExpressionStatementIsValid() { }

@Deprecated
protected TypeCalculator getTypeCheck() {
return this.typeCheck;
}

@Override
public void check(ASTExpressionStatement statement) {
Preconditions.checkNotNull(statement);
if(typeCheck != null) {
// support deprecated behavior
this.getTypeCheck().typeOf(statement.getExpression());
public void check(ASTExpressionStatement node) {
Preconditions.checkNotNull(node);

if (typeCheck != null) {
// support deprecated behavior
this.getTypeCheck().typeOf(node.getExpression());
} else {
TypeCheck3.typeOf(statement.getExpression());
TypeCheck3.typeOf(node.getExpression());
}
}
}
Original file line number Diff line number Diff line change
@@ -1,38 +1,54 @@
/* (c) https://github.com/MontiCore/monticore */
package de.monticore.statements.mccommonstatements.cocos;

import com.google.common.base.Preconditions;
import de.monticore.statements.mccommonstatements._ast.ASTCommonForControl;
import de.monticore.statements.mccommonstatements._ast.ASTForStatement;
import de.monticore.statements.mccommonstatements._cocos.MCCommonStatementsASTForStatementCoCo;
import de.monticore.types.check.SymTypeExpression;
import de.monticore.types.check.TypeCheck;
import de.monticore.types.check.TypeCalculator;
import de.monticore.types3.SymTypeRelations;
import de.monticore.types3.TypeCheck3;
import de.se_rwth.commons.logging.Log;

public class ForConditionHasBooleanType implements MCCommonStatementsASTForStatementCoCo {


@Deprecated
TypeCalculator typeCheck;

public static final String ERROR_CODE = "0xA0906";

public static final String ERROR_MSG_FORMAT = "Condition of for-loop must be a boolean expression.";

public ForConditionHasBooleanType(TypeCalculator typeCheck){

/**
* @deprecated use default constructor
*/
@Deprecated
public ForConditionHasBooleanType(TypeCalculator typeCheck) {
this.typeCheck = typeCheck;
}


public ForConditionHasBooleanType() { }

@Override
public void check(ASTForStatement node) {
Preconditions.checkNotNull(node);

// todo replace with typedispatcher as soon as issues are fixed
if(!(node.getForControl() instanceof ASTCommonForControl)) {
if (!(node.getForControl() instanceof ASTCommonForControl)) {
return;
}
SymTypeExpression result = typeCheck.typeOf(((ASTCommonForControl)node.getForControl()).getCondition());

if(!TypeCheck.isBoolean(result)){
SymTypeExpression result;

if (typeCheck != null) {
// support deprecated behavior
result = typeCheck.typeOf(((ASTCommonForControl) node.getForControl()).getCondition());
} else {
result = TypeCheck3.typeOf(((ASTCommonForControl) node.getForControl()).getCondition());
}

if (!SymTypeRelations.isBoolean(result)) {
Log.error(ERROR_CODE + ERROR_MSG_FORMAT, node.get_SourcePositionStart());
}

}
}
Original file line number Diff line number Diff line change
@@ -1,35 +1,53 @@
/* (c) https://github.com/MontiCore/monticore */
package de.monticore.statements.mccommonstatements.cocos;

import com.google.common.base.Preconditions;
import de.monticore.statements.mccommonstatements._ast.ASTEnhancedForControl;
import de.monticore.statements.mccommonstatements._cocos.MCCommonStatementsASTEnhancedForControlCoCo;
import de.monticore.types.check.SymTypeExpression;
import de.monticore.types.check.SymTypeExpressionFactory;
import de.monticore.types.check.TypeCheck;
import de.monticore.types.check.TypeCalculator;
import de.monticore.types3.SymTypeRelations;
import de.monticore.types3.TypeCheck3;
import de.se_rwth.commons.logging.Log;

public class ForEachIsValid implements MCCommonStatementsASTEnhancedForControlCoCo {

@Deprecated
TypeCalculator typeCheck;

public static final String ERROR_CODE = "0xA0907 ";

public static final String ERROR_MSG_FORMAT = "For-each loop expression must be an array of subtype of list.";

/**
* @deprecated use default constructor
*/
@Deprecated
public ForEachIsValid(TypeCalculator typeCheck) {
this.typeCheck = typeCheck;
}

public ForEachIsValid() { }

@Override
public void check(ASTEnhancedForControl node) {
Preconditions.checkNotNull(node);

SymTypeExpression expression;

if (typeCheck != null) {
// support deprecated behavior
expression = typeCheck.typeOf(node.getExpression());
} else {
expression = TypeCheck3.typeOf(node.getExpression());
}

SymTypeExpression expression = typeCheck.typeOf(node.getExpression());
SymTypeExpression arrays = SymTypeExpressionFactory.createTypeObject("java.util.Arrays", node.getEnclosingScope());
SymTypeExpression lists = SymTypeExpressionFactory.createTypeObject("java.lang.Iterable", node.getEnclosingScope());

if (!TypeCheck.isSubtypeOf(expression, arrays)) {
if (!TypeCheck.isSubtypeOf(expression, lists)) {
if (!SymTypeRelations.isSubTypeOf(expression, arrays)) {
if (!SymTypeRelations.isSubTypeOf(expression, lists)) {
Log.error(ERROR_CODE + ERROR_MSG_FORMAT, node.get_SourcePositionStart());
}
}
Expand Down
Loading

0 comments on commit 41a1dc9

Please sign in to comment.