Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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 @@ -6,16 +6,18 @@
import de.monticore.cdinterfaceandenum._ast.ASTCDEnum;
import de.monticore.cdinterfaceandenum._ast.ASTCDInterface;
import de.monticore.symbols.oosymbols._symboltable.OOTypeSymbol;
import de.monticore.types.check.SymTypeExpression;
import de.monticore.types.mcbasictypes._ast.ASTMCObjectType;
import de.monticore.types.mcbasictypes._ast.ASTMCQualifiedType;
import de.monticore.types.mccollectiontypes._ast.ASTMCGenericType;
import de.monticore.types3.TypeCheck3;
import de.se_rwth.commons.logging.Log;
import java.util.List;
import java.util.Optional;

/** Checks that only interfaces are implemented. */
public abstract class ImplementOnlyInterfaces {

public static final String CLASS_ERROR_CODE = "0xCDCF4";
public static final String ENUM_ERROR_CODE = "0xCDCF5";
public static final String INTERFACE_ERROR_CODE = "0xCDCF6";

/**
* Actual check that the class's interfaces are really interfaces.
*
Expand All @@ -27,32 +29,23 @@ public void check(ASTCDClass node) {
if (!node.isPresentCDInterfaceUsage()) {
return;
}
final List<ASTMCObjectType> interfaceList = node.getCDInterfaceUsage().getInterfaceList();
interfaceList.stream().map(s -> symbol.getEnclosingScope().resolveOOType(getObjectName(s)))
.filter(Optional::isPresent).map(Optional::get).filter(e -> !e.isIsInterface()).forEach(
e -> Log.error(String.format(
"0xCDCF4: Class %s cannot implement %s %s. A class may only implement interfaces.",
node.getName(), CDMill.cDTypeKindPrinter().print(e), e.getName()), node
.get_SourcePositionStart()));
}

protected String getObjectName(ASTMCObjectType type) {
// TODO: This is a fix for the following types:
// class State_Pat implements IState, IElem<IState>
// class Opt<T> implements IElem<T>
// interface IElem<T> { }
// -> this tries to resolve "IElem<T>.class", which fails HARD due to illegal chars!
// TODO: Discuss where to place such a thing

if (type instanceof ASTMCQualifiedType qualifiedType) {
return qualifiedType.getMCQualifiedName().getQName();
}
else if (type instanceof ASTMCGenericType genericType) {
return genericType.printWithoutTypeArguments();
}
else {
throw new IllegalStateException("Unhandled MCObjectType " + type.getClass() + " of `" + type
.printType() + "` when trying to resolve source");
for (ASTMCObjectType typeRef : node.getCDInterfaceUsage().getInterfaceList()) {
SymTypeExpression steRef = TypeCheck3.symTypeFromAST(typeRef);
if (steRef.hasTypeInfo()) {
if (!CoCoHelper.isInterface(steRef.getTypeInfo()))
Log.error(String.format(
"%s: Class %s cannot implement %s %s. A class may only implement interfaces.",
CLASS_ERROR_CODE, node.getName(), CDMill.cDTypeKindPrinter().print(steRef
.getTypeInfo()), steRef.getTypeInfo().getName()), node.get_SourcePositionStart());

}
else {
Log.error(String.format(
"%s: Class %s cannot implement <missing type info>. A class may only implement interfaces.",
CLASS_ERROR_CODE, node.getName()), node.get_SourcePositionStart());

}
}
}

Expand All @@ -68,10 +61,10 @@ public void check(ASTCDEnum node) {
}
symbol.streamSuperTypes().filter(i -> !CoCoHelper.isInterface(i.getTypeInfo())).forEach(e -> Log
.error(String.format(
"0xCDCF5: The %s %s cannot implement %s %s. Only interfaces may be implemented.", CDMill
.cDTypeKindPrinter().print(node), symbol.getName(), CDMill.cDTypeKindPrinter()
.print(e.getTypeInfo()), e.getTypeInfo().getName()), node
.get_SourcePositionStart()));
"%s: The %s %s cannot implement %s %s. Only interfaces may be implemented.",
ENUM_ERROR_CODE, CDMill.cDTypeKindPrinter().print(node), symbol.getName(), CDMill
.cDTypeKindPrinter().print(e.getTypeInfo()), e.getTypeInfo().getName()), node
.get_SourcePositionStart()));
}

/**
Expand All @@ -85,11 +78,10 @@ public void check(ASTCDInterface node) {
return;
}
symbol.streamSuperTypes().filter(i -> !CoCoHelper.isInterface(i.getTypeInfo())).forEach(e -> Log
.error(String.format(
"0xCDCF6: The %s %s cannot extend %s %s. Only interfaces may be extended.", CDMill
.cDTypeKindPrinter().print(node), symbol.getName(), CDMill.cDTypeKindPrinter()
.print(e.getTypeInfo()), e.getTypeInfo().getName()), node
.get_SourcePositionStart()));
.error(String.format("%s: The %s %s cannot extend %s %s. Only interfaces may be extended.",
INTERFACE_ERROR_CODE, CDMill.cDTypeKindPrinter().print(node), symbol.getName(), CDMill
.cDTypeKindPrinter().print(e.getTypeInfo()), e.getTypeInfo().getName()), node
.get_SourcePositionStart()));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import de.monticore.cd.methodtemplates.CD4C;
import de.monticore.cd4code.CD4CodeMill;
import de.monticore.cd4code._visitor.CD4CodeTraverser;
import de.monticore.cd4codebasis._ast.ASTCDClass;
import de.monticore.cd4codebasis._ast.ASTCDInterface;
import de.monticore.cd4codebasis._ast.ASTCDMethod;
import de.monticore.cd4codebasis._ast.ASTCDParameter;
Expand All @@ -18,9 +19,14 @@
import de.monticore.types.MCTypeFacade;
import de.monticore.types.mcbasictypes._ast.ASTMCQualifiedType;
import de.monticore.types.mcbasictypes._ast.ASTMCType;
import de.monticore.types.mccollectiontypes._ast.ASTMCTypeArgument;
import de.monticore.types.mcfullgenerictypes._ast.ASTMCWildcardTypeArgumentBuilder;
import de.monticore.types.typeparameters._ast.ASTTypeParameter;
import de.se_rwth.commons.logging.Log;

import javax.annotation.Nullable;
import java.util.*;
import java.util.stream.Collectors;

import static de.monticore.cd.codegen.CD2JavaTemplates.EMPTY_BODY;

Expand Down Expand Up @@ -77,6 +83,17 @@
this._interfaceVisitorArtifact = null;
}

static ASTMCTypeArgument typeParamToArg(ASTTypeParameter parameter) {
ASTMCWildcardTypeArgumentBuilder wildcardTypeArgumentBuilder = CD4CodeMill
.mCWildcardTypeArgumentBuilder();
if (!parameter.getMCTypeList().isEmpty()) {
// without extends
Log.error("0xTODO: NYI multiple extends of ASTTypeParameter " + parameter, parameter

Check warning on line 91 in cdlang/src/main/java/de/monticore/cd/codegen/decorators/VisitorDecorator.java

View workflow job for this annotation

GitHub Actions / check-error-codes

ErrorCodeInvalid

Invalid error code 0xTODO (./cdlang/src/main/java/de/monticore/cd/codegen/decorators/VisitorDecorator.java:91)
.get_SourcePositionStart(), parameter.get_SourcePositionEnd());
}
return wildcardTypeArgumentBuilder.build();
}

@Override
public void visit(ASTCDType type) {
if (decoratorData.shouldDecorate(this.getClass(), type)) {
Expand All @@ -94,6 +111,26 @@
.setMCType(visitorInterfaceQualifiedType).build();
//create a type of the class
ASTMCType classType = MCTypeFacade.getInstance().createQualifiedType(type.getName());

// Workaround to add <?> type paras
if (type instanceof ASTCDClass typeOfClass) {
if (typeOfClass.isPresentTypeParameters() && !typeOfClass.getTypeParameters()
.getTypeParameterList().isEmpty()) {
classType = CD4CodeMill.mCBasicGenericTypeBuilder().addName(type.getName())
.addAllMCTypeArguments(typeOfClass.getTypeParameters().getTypeParameterList().stream()
.map(VisitorDecorator::typeParamToArg).collect(Collectors.toList())).build();
}
}
else if (type instanceof ASTCDInterface typeOfInterface) {
if (typeOfInterface.isPresentTypeParameters() && !typeOfInterface.getTypeParameters()
.getTypeParameterList().isEmpty()) {
classType = CD4CodeMill.mCBasicGenericTypeBuilder().addName(type.getName())
.addAllMCTypeArguments(typeOfInterface.getTypeParameters().getTypeParameterList()
.stream().map(VisitorDecorator::typeParamToArg).collect(Collectors.toList()))
.build();
}
}

ASTCDParameter classParameter = CD4CodeMill.cDParameterBuilder().setName("node").setMCType(
classType).build();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import java.util.Objects;
import java.util.Optional;
import java.util.Stack;
import java.util.stream.Collectors;

import static de.monticore.cd.codegen.CD2JavaTemplates.EMPTY_BODY;

Expand Down Expand Up @@ -107,6 +108,15 @@
if (decoratorData.shouldDecorate(this.getClass(), clazz)) {
ASTCDClass decClazz = decoratorData.getAsDecorated(clazz);
ASTMCType classType = MCTypeFacade.getInstance().createQualifiedType(clazz.getName());
// Workaround to add <?> type paras
if (clazz instanceof de.monticore.cd4codebasis._ast.ASTCDClass typeOfClass) {
if (typeOfClass.isPresentTypeParameters() && !typeOfClass.getTypeParameters()
.getTypeParameterList().isEmpty()) {
classType = CD4CodeMill.mCBasicGenericTypeBuilder().addName(clazz.getName())
.addAllMCTypeArguments(typeOfClass.getTypeParameters().getTypeParameterList().stream()
.map(VisitorDecorator::typeParamToArg).collect(Collectors.toList())).build();
}
}
ASTCDParameter classParameter = CD4CodeMill.cDParameterBuilder().setName("node").setMCType(
classType).build();

Expand Down Expand Up @@ -148,7 +158,7 @@
if (attrInfo.getSymTypeExpression().getTypeInfo().isPresentAstNode() && !decoratorData
.shouldDecorate(VisitorDecorator.class, attrInfo.getSymTypeExpression().getTypeInfo()
.getAstNode())) {
warn(attribute, "0xTODO: Visitor implementation of association `" + attribute.getSymbol()

Check warning on line 161 in cdlang/src/main/java/de/monticore/cd/codegen/decorators/VisitorImplementationDecorator.java

View workflow job for this annotation

GitHub Actions / check-error-codes

ErrorCodeInvalid

Invalid error code 0xTODO (./cdlang/src/main/java/de/monticore/cd/codegen/decorators/VisitorImplementationDecorator.java:161)
.getFullName() + "` is not possible due to missing visitor of the class `" + attrInfo
.getSymTypeExpression().getTypeInfo().getFullName() + "`");
return;
Expand Down Expand Up @@ -199,7 +209,7 @@
}

protected void warnMissingGetter(ASTCDAttribute attribute) {
String message = "0xTODO: Attribute `" + attribute.getSymbol().getFullName()

Check warning on line 212 in cdlang/src/main/java/de/monticore/cd/codegen/decorators/VisitorImplementationDecorator.java

View workflow job for this annotation

GitHub Actions / check-error-codes

ErrorCodeInvalid

Invalid error code 0xTODO (./cdlang/src/main/java/de/monticore/cd/codegen/decorators/VisitorImplementationDecorator.java:212)
+ "` has no getter information provided. Unable to include in "
+ getVisitorImplementationClass().getName();
warn(attribute, message);
Expand All @@ -223,6 +233,12 @@
public void visit(ASTCDInterface cdInterface) {
if (decoratorData.shouldDecorate(this.getClass(), cdInterface)) {
ASTMCType classType = MCTypeFacade.getInstance().createQualifiedType(cdInterface.getName());
if (cdInterface.isPresentTypeParameters() && !cdInterface.getTypeParameters()
.getTypeParameterList().isEmpty()) {
classType = CD4CodeMill.mCBasicGenericTypeBuilder().addName(cdInterface.getName())
.addAllMCTypeArguments(cdInterface.getTypeParameters().getTypeParameterList().stream()
.map(VisitorDecorator::typeParamToArg).collect(Collectors.toList())).build();
}
ASTCDParameter classParameter = CD4CodeMill.cDParameterBuilder().setName("node").setMCType(
classType).build();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import de.monticore.cd.cocos.CoCoParent;
import de.monticore.cd4code._cocos.CD4CodeCoCoChecker;
import de.monticore.cdbasis.cocos.ebnf.CDAttributeOverridden;
import de.monticore.types.mcbasictypes.cocos.QualifiedTypeHasNoTypeParameters;
import de.monticore.types.typeparameters.cocos.TypeParameterNoCyclicInheritance;
import de.monticore.types.typeparameters.cocos.TypeParametersHaveUniqueNames;

Expand Down Expand Up @@ -31,6 +32,8 @@ public CD4CodeCoCoChecker addCheckerForEbnfCoCos(CD4CodeCoCoChecker checker) {
checker.addCoCo(new TypeParametersHaveUniqueNames());
// TypeParameter
checker.addCoCo(new TypeParameterNoCyclicInheritance());
// MCBasicTypes
checker.addCoCo(new QualifiedTypeHasNoTypeParameters());
return checker;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ protected boolean matchConstructor(ArrayList<SymTypeExpression> paramTypes,
for (ASTCDConstructor constructor : cdConstructorList) {
List<VariableSymbol> formalParams = constructor.getSymbol().getParameterList();
if (paramTypes.size() != formalParams.size()) {
return false;
continue;
}
boolean success = true;
for (int i = 0; i < formalParams.size(); i++) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -124,8 +124,8 @@ public void endVisit(ASTCDAssociation node) {
CDAssociationSymbolTableCompleter.addRoleToTheirType(l.getSymbol(), rType.getTypeInfo());
}
else {
Log.error("0xCDCD1 Right type for role symbol " + l.getSymbol().getName()
+ " not available.");
Log.error("0xCDCD1: Right type for role symbol " + l.getSymbol().getName()
+ " not available.", node.get_SourcePositionStart(), node.get_SourcePositionEnd());
}
}
if (r.isPresentSymbol()) {
Expand All @@ -134,7 +134,7 @@ public void endVisit(ASTCDAssociation node) {
}
else {
Log.error("0xCDCD2 Left type for role symbol " + r.getSymbol().getName()
+ " not available.");
+ " not available.", node.get_SourcePositionStart(), node.get_SourcePositionEnd());
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,35 +1,46 @@
/* (c) https://github.com/MontiCore/monticore */
package de.monticore.cdinterfaceandenum.cocos.ebnf;

import de.monticore.cd.CDMill;
import de.monticore.cd.cocos.CoCoHelper;
import de.monticore.cdbasis._ast.ASTCDClass;
import de.monticore.cdbasis._cocos.CDBasisASTCDClassCoCo;
import de.monticore.symbols.oosymbols._symboltable.OOTypeSymbol;
import de.monticore.types.check.SymTypeExpression;
import de.monticore.types.mcbasictypes._ast.ASTMCObjectType;
import de.monticore.types3.TypeCheck3;
import de.se_rwth.commons.logging.Log;
import java.util.List;
import java.util.Optional;

/** Checks that classes do only extend other classes. */
public class CDClassExtendsOnlyClasses implements CDBasisASTCDClassCoCo {

// TODO SVa: provide printer for the types,
// so that a user can provide their own printer

public static final String ERROR_CODE = "0xCDC08";

@Override
public void check(ASTCDClass clazz) {
OOTypeSymbol symbol = clazz.getSymbol();

if (!clazz.isPresentCDExtendUsage()) {
return;
}
final List<ASTMCObjectType> superclassList = clazz.getCDExtendUsage().getSuperclassList();
superclassList.stream().map(s -> symbol.getEnclosingScope().resolveOOType(s.printType()))
.filter(Optional::isPresent).map(Optional::get).filter(e -> !e.isIsClass()).forEach(e -> Log
.error(String.format(
"0xCDC08: Class %s cannot extend %s %s. A class may only extend classes.", clazz
.getName(), CDMill.cDTypeKindPrinter().print(e), e.getName()), clazz
.get_SourcePositionStart()));

for (ASTMCObjectType typeRef : clazz.getCDExtendUsage().getSuperclassList()) {
SymTypeExpression steRef = TypeCheck3.symTypeFromAST(typeRef);
if (steRef.hasTypeInfo()) {
if (!CoCoHelper.isClass(steRef.getTypeInfo()))
Log.error(String.format(
"%s: Class %s cannot extend %s %s. A class may only extend classes.", ERROR_CODE,
clazz.getName(), steRef.getTypeInfo().getClass().getSimpleName(), steRef.getTypeInfo()
.getName()), clazz.get_SourcePositionStart());
}
else {
Log.error(String.format(
"%s: Class %s cannot extend <missing type info>. A class may only extend classes.",
ERROR_CODE, clazz.getName()), clazz.get_SourcePositionStart());
}
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ public class C5 {}
association [0..1] C <-> (optC) C2 [0..1];
association [*] C <-> (manyManyC) C3 [*];
association C <-> (manyC4) C4 [*];
class WithTO<T> {}

}""");

Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
/* (c) https://github.com/MontiCore/monticore */
package de.monticore.cd4code._symboltable;

import de.monticore.cd4code.CD4CodeMill;
import de.monticore.cd4code.CD4CodeTestBasis;
import de.monticore.cd4code.trafo.CD4CodeAfterParseTrafo;
import de.monticore.cdbasis._ast.ASTCDCompilationUnit;
import java.io.IOException;
import java.util.Optional;

import org.junit.jupiter.api.Test;

@SuppressWarnings("OptionalGetWithoutIsPresent")
Expand All @@ -20,7 +20,7 @@ public void completeModel() throws IOException {
final ASTCDCompilationUnit node = astcdCompilationUnit.get();
new CD4CodeAfterParseTrafo().transform(node);

CD4CodeMill.scopesGenitorDelegator().createFromAST(node);
prepareST(node);
checkLogError();

cd4CodeCoCos.getCheckerForAllCoCos().checkAll(node);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,27 +3,30 @@

import static org.junit.jupiter.api.Assertions.assertNotNull;

import de.monticore.cd4code.CD4CodeMill;
import de.monticore.cd4code.CD4CodeTestBasis;
import de.monticore.cd4code._symboltable.ICD4CodeArtifactScope;
import de.monticore.cd4code.trafo.CD4CodeAfterParseTrafo;
import de.monticore.cdbasis._ast.ASTCDCompilationUnit;
import java.io.IOException;
import java.util.Optional;

import org.junit.jupiter.api.Test;

@SuppressWarnings("OptionalGetWithoutIsPresent")
/**
* Tests CD4Code (and all super language's) CoCos
*/
public class CD4CodeCoCoTest extends CD4CodeTestBasis {

@Test
public void importModel() throws IOException {
// When this test fails: check, that the Simple.cdsym is correct!
final Optional<ASTCDCompilationUnit> astcdCompilationUnit = p.parse(getFilePath(
"cdbasis/parser/Import.cd"));
checkNullAndPresence(p, astcdCompilationUnit);
final ASTCDCompilationUnit node = astcdCompilationUnit.get();
final ASTCDCompilationUnit node = astcdCompilationUnit.orElseThrow();
new CD4CodeAfterParseTrafo().transform(node);

final ICD4CodeArtifactScope scope = CD4CodeMill.scopesGenitorDelegator().createFromAST(node);
final ICD4CodeArtifactScope scope = prepareST(astcdCompilationUnit.orElseThrow());
checkLogError();

assertNotNull(scope.resolveCDType("C"));
Expand All @@ -36,9 +39,9 @@ public void completeCDBasisModel() throws IOException {
final Optional<ASTCDCompilationUnit> astcdCompilationUnit = p.parse(getFilePath(
"cdbasis/parser/Complete.cd"));
checkNullAndPresence(p, astcdCompilationUnit);
final ASTCDCompilationUnit node = astcdCompilationUnit.get();
final ASTCDCompilationUnit node = astcdCompilationUnit.orElseThrow();

CD4CodeMill.scopesGenitorDelegator().createFromAST(node);
prepareST(astcdCompilationUnit.orElseThrow());
checkLogError();

cd4CodeCoCos.getCheckerForAllCoCos().checkAll(node);
Expand All @@ -49,7 +52,7 @@ public void completeModel() throws IOException {
final Optional<ASTCDCompilationUnit> astcdCompilationUnit = p.parse(getFilePath(
"cd4code/parser/Complete.cd"));
checkNullAndPresence(p, astcdCompilationUnit);
final ASTCDCompilationUnit node = astcdCompilationUnit.get();
final ASTCDCompilationUnit node = astcdCompilationUnit.orElseThrow();

prepareST(node);
checkLogError();
Expand Down
Loading
Loading