Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -5,22 +5,37 @@
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.TypeCalculator;
import de.monticore.types3.SymTypeRelations;
import de.monticore.types3.TypeCheck3;
import de.se_rwth.commons.logging.Log;

import java.util.List;
import java.util.Optional;

import static de.monticore.types3.SymTypeRelations.isCompatible;
import static de.monticore.types3.SymTypeRelationsOfIterables.getIterationType;
import static java.lang.String.format;

public class ForEachIsValid implements MCCommonStatementsASTEnhancedForControlCoCo {

public static final String FOR_EACH_EXPR_NOT_ITERABLE_ERROR_CODE = "0xA0907";

public static final String FOR_EACH_EXPR_NOT_ITERABLE_ERROR_MSG =
"For-each loop expression must be iterable. Instead, the type is '%s'";

public static final String FOR_EACH_TYPE_MISMATCH_ERROR_CODE = "0xA0908";

public static final String FOR_EACH_TYPE_MISMATCH_ERROR_MSG =
"Type mismatch, expected '%s' but provided '%s'";

@Deprecated
TypeCalculator typeCheck;

public static final String ERROR_CODE = "0xA0907";
@Deprecated
public static final String ERROR_CODE = FOR_EACH_EXPR_NOT_ITERABLE_ERROR_CODE;

public static final String ERROR_MSG_FORMAT =
"For-each loop expression must be an array or a list."
+ " Instead, the type is ";
@Deprecated
public static final String ERROR_MSG_FORMAT = FOR_EACH_EXPR_NOT_ITERABLE_ERROR_MSG;

/**
* @deprecated use default constructor
Expand All @@ -37,17 +52,36 @@ public ForEachIsValid() {
public void check(ASTEnhancedForControl node) {
Preconditions.checkNotNull(node);

SymTypeExpression expression = TypeCheck3.typeOf(node.getExpression());
SymTypeExpression typeOfVariable = TypeCheck3.symTypeFromAST(node.getFormalParameter().getMCType());
SymTypeExpression typeOfExpression = TypeCheck3.typeOf(node.getExpression());

SymTypeExpression arrays = SymTypeExpressionFactory.createTypeObjectViaSurrogate("java.util.Arrays", node.getEnclosingScope());
SymTypeExpression lists = SymTypeExpressionFactory.createTypeObjectViaSurrogate("java.lang.Iterable", node.getEnclosingScope());
if (typeOfVariable.isObscureType() || typeOfExpression.isObscureType()) {
return;
}

Optional<SymTypeExpression> symTypeOfIteration = getIterationType(typeOfExpression);

if (symTypeOfIteration.isEmpty()) {
Log.error(FOR_EACH_EXPR_NOT_ITERABLE_ERROR_CODE + " "
+ format(FOR_EACH_EXPR_NOT_ITERABLE_ERROR_MSG, typeOfExpression.printFullName()),
node.getExpression().get_SourcePositionStart(),
node.getExpression().get_SourcePositionEnd()
);
return;
}

if (symTypeOfIteration.get().isObscureType()) {
return;
}

if (!expression.isObscureType()
&& !SymTypeRelations.isSubTypeOf(expression, arrays)
&& !SymTypeRelations.isSubTypeOf(expression, lists)) {
Log.error(ERROR_CODE + " " + ERROR_MSG_FORMAT
+ expression.printFullName(),
node.get_SourcePositionStart()
if (!isCompatible(typeOfVariable, symTypeOfIteration.get())) {
Log.error(FOR_EACH_TYPE_MISMATCH_ERROR_CODE + " " +
format(FOR_EACH_TYPE_MISMATCH_ERROR_MSG,
symTypeOfIteration.get().printFullName(),
typeOfVariable.printFullName()
),
node.getFormalParameter().get_SourcePositionStart(),
node.getFormalParameter().get_SourcePositionEnd()
);
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
// (c) https://github.com/MontiCore/monticore
package de.monticore.types3;

import com.google.common.base.Preconditions;
import de.monticore.types.check.SymTypeExpression;
import de.monticore.types.check.SymTypeOfGenerics;
import de.se_rwth.commons.logging.Log;

import java.util.Optional;

/**
* Relations of SymTypeExpressions that are iterable (e.g., arrays, List, Iterable)
*/
public class SymTypeRelationsOfIterables {

protected static SymTypeRelationsOfIterables delegate;

/**
* Whether the type is an array (e.g., int[])
*
* @see SymTypeExpression#isArrayType()
*/
public static boolean isArrayType(SymTypeExpression type) {
return getDelegate()._isArrayType(type);
}

protected boolean _isArrayType(SymTypeExpression type) {
return type.isArrayType();
}

/**
* Whether the type is of the raw type Iterable (e.g., java.lang.Iterable)
*
* @see SymTypeExpression#isArrayType()
*/
protected static boolean isOfTypeIterable(SymTypeExpression type) {
return getDelegate()._isOfTypeIterable(type);
}

protected boolean _isOfTypeIterable(SymTypeExpression type) {
if (!type.isGenericType()) {
return false;
}
SymTypeOfGenerics generic = type.asGenericType();
String name = generic.getTypeConstructorFullName();
if (!name.equals("Iterable") && !name.equals("java.lang.Iterable")) {
return false;
}
return generic.sizeArguments() == 1;
}

/**
* Whether the type if of or a subtype of the raw type Iterable (e.g., java.lang.Iterable)
*
* @see SymTypeExpression#isArrayType()
*/
protected static boolean isOfTypeIterableOrSubType(SymTypeExpression type) {
return getDelegate()._isOfTypeIterable(type);
}

protected boolean _isOfTypeIterableOrSubType(SymTypeExpression type) {
if (_isOfTypeIterable(type)) {
return true;
} else {
for (SymTypeExpression superType : SymTypeRelations.getNominalSuperTypes(type)) {
if (_isOfTypeIterableOrSubType(superType)) {
return true;
}
}
}
return false;
}

/**
* @return an {@code Optional} of the Element type of the iterable
* or an {@link Optional#empty()} if the type is not iterable
*/
public static Optional<SymTypeExpression> getIterationType(SymTypeExpression type) {
return getDelegate()._getIterationType(type);
}

protected Optional<SymTypeExpression> _getIterationType(SymTypeExpression type) {
Optional<SymTypeExpression> iterationType = _getIterationTypeOfArray(type);

if (iterationType.isPresent()) {
return iterationType;
}

return _getIterationTypeOfTypeIterable(type);
}

protected Optional<SymTypeExpression> _getIterationTypeOfArray(SymTypeExpression type) {
if (_isArrayType(type)) {
return Optional.of(type.asArrayType().cloneWithLessDim(1));
} else {
return Optional.empty();
}
}

protected Optional<SymTypeExpression> _getIterationTypeOfTypeIterable(SymTypeExpression type) {
if (_isOfTypeIterable(type)) {
return Optional.of(type.asGenericType().getArgument(0));
}
for (SymTypeExpression superType : SymTypeRelations.getNominalSuperTypes(type)) {
Optional<SymTypeExpression> iterationType = _getIterationTypeOfTypeIterable(superType);
if (iterationType.isPresent()) {
return iterationType;
}
}
return Optional.empty();
}

// static delegate

public static void init() {
Log.trace("init default IterableSymTypeRelations", "TypeCheck setup");
setDelegate(new SymTypeRelationsOfIterables());
}

public static void reset() {
SymTypeRelationsOfIterables.delegate = null;
}

protected static void setDelegate(SymTypeRelationsOfIterables newDelegate) {
SymTypeRelationsOfIterables.delegate = Preconditions.checkNotNull(newDelegate);
}

protected static SymTypeRelationsOfIterables getDelegate() {
if (SymTypeRelationsOfIterables.delegate == null) {
init();
}
return SymTypeRelationsOfIterables.delegate;
}

}
Loading
Loading