From daa33c39567fe27512d55098a8d55112e82a382d Mon Sep 17 00:00:00 2001 From: SE-FDr <88034434+SE-FDr@users.noreply.github.com> Date: Tue, 30 Jul 2024 17:38:29 +0200 Subject: [PATCH 01/24] initial TypeParameters.mc4 --- .../de/monticore/types/TypeParameters.mc4 | 63 +++++++++++++++++++ 1 file changed, 63 insertions(+) create mode 100644 monticore-grammar/src/main/grammars/de/monticore/types/TypeParameters.mc4 diff --git a/monticore-grammar/src/main/grammars/de/monticore/types/TypeParameters.mc4 b/monticore-grammar/src/main/grammars/de/monticore/types/TypeParameters.mc4 new file mode 100644 index 0000000000..c150290ca4 --- /dev/null +++ b/monticore-grammar/src/main/grammars/de/monticore/types/TypeParameters.mc4 @@ -0,0 +1,63 @@ +/* (c) https://github.com/MontiCore/monticore */ +package de.monticore.types; + +/* Alpha-version: This is intended to become a MontiCore stable grammar. */ + +import de.monticore.symbols.BasicSymbols; + +// TODO FDr: remove from JavaDSL after merge +// TODO FDr: documentation in MDs +// TODO FDr: CoCos -> Test and COCO that types fit together + +/** + * This grammar introduces type parameters such as T, U extends Set, + * and type parameter lists such as + * that can be used to declare generic model elements + * such as generic classes and functions. + * + * Like Java, each type parameter can have an upper bound + * consisting of an arbitrary number of types. +*/ + +component grammar TypeParameters + extends BasicSymbols, + MCBasicTypes { + + /** + * ASTTypeParameters are type parameter lists that are can be used to + * declare generic model elements. + * Each type parameter lists contains at least one type parameter. + * Example: + * + * > + */ + TypeParameters = + "<" (TypeParameter || ",")+ ">" + ; + + /** + * ASTTypeParameter represents one type parameter. + * Each type parameter has a name + * and can have an optional upper type bound. + * Example: + * T + * U extends T & Comparable + */ + symbol TypeParameter implements TypeVar = + Name + ("extends" TypeBounds)? + ; + + /** + * ASTTypeBounds represents the (upper) type bounds of a type parameter. + * Each bound list consists of an arbitrary positive number of types. + * Each of those types is a supertype of the corresponding type parameter. + * Example: + * Person + * Person & Comparable + */ + TypeBounds = + (MCType || "&")+ + ; + +} From 55053a732f299753c54ed5db9ab5a80f8c767a8a Mon Sep 17 00:00:00 2001 From: SE-FDr <88034434+SE-FDr@users.noreply.github.com> Date: Tue, 30 Jul 2024 17:38:37 +0200 Subject: [PATCH 02/24] initial TypeParameters.mlc --- .../de/monticore/types/TypeParameters.mlc | 24 +++++++++++++++++++ 1 file changed, 24 insertions(+) create mode 100644 monticore-grammar/src/main/grammars/de/monticore/types/TypeParameters.mlc diff --git a/monticore-grammar/src/main/grammars/de/monticore/types/TypeParameters.mlc b/monticore-grammar/src/main/grammars/de/monticore/types/TypeParameters.mlc new file mode 100644 index 0000000000..3692b79347 --- /dev/null +++ b/monticore-grammar/src/main/grammars/de/monticore/types/TypeParameters.mlc @@ -0,0 +1,24 @@ +package de.monticore.types; + +mlc MCFullGenericTypes { + + //export the grammar + export "$projectDir/src/main/grammars" { + include "de/monticore/types/TypeParameters.mc4"; + } + + //export handwritten code + export "$projectDir/src/main/java" { + include "de/monticore/types/typeparameters/**.java"; + } + + // export all Java files generated from the grammar + export "$projectDir/target/generated-sources/monticore/sourcecode" { + include "de/monticore/types/typeparameters/**.java"; + } + + promote { + mlc "de.monticore.types.TypeParameters"; + } + +} From 0ec9558ef007f4f0abfddbff4b86f28565106a1d Mon Sep 17 00:00:00 2001 From: SE-FDr <88034434+SE-FDr@users.noreply.github.com> Date: Tue, 30 Jul 2024 17:39:13 +0200 Subject: [PATCH 03/24] TypeParameters -> TestGrammars --- .../expressions/CombineExpressionsWithLiterals.mc4 | 1 + .../grammars/de/monticore/types/TypeParametersTest.mc4 | 10 ++++++++++ .../TypeParametersWithoutIntersectionTypesTest.mc4 | 9 +++++++++ 3 files changed, 20 insertions(+) create mode 100644 monticore-grammar/src/test/grammars/de/monticore/types/TypeParametersTest.mc4 create mode 100644 monticore-grammar/src/test/grammars/de/monticore/types/TypeParametersWithoutIntersectionTypesTest.mc4 diff --git a/monticore-grammar/src/test/grammars/de/monticore/expressions/CombineExpressionsWithLiterals.mc4 b/monticore-grammar/src/test/grammars/de/monticore/expressions/CombineExpressionsWithLiterals.mc4 index b1ea472f3c..5bf1997da4 100644 --- a/monticore-grammar/src/test/grammars/de/monticore/expressions/CombineExpressionsWithLiterals.mc4 +++ b/monticore-grammar/src/test/grammars/de/monticore/expressions/CombineExpressionsWithLiterals.mc4 @@ -18,6 +18,7 @@ grammar CombineExpressionsWithLiterals extends de.monticore.types.MCArrayTypes, de.monticore.types.MCFunctionTypes, de.monticore.types.MCStructuralTypes, + de.monticore.types.TypeParameters, de.monticore.regex.RegExType, de.monticore.siunit.SIUnitTypes4Math, de.monticore.siunit.SIUnitTypes4Computing, diff --git a/monticore-grammar/src/test/grammars/de/monticore/types/TypeParametersTest.mc4 b/monticore-grammar/src/test/grammars/de/monticore/types/TypeParametersTest.mc4 new file mode 100644 index 0000000000..0464d5b492 --- /dev/null +++ b/monticore-grammar/src/test/grammars/de/monticore/types/TypeParametersTest.mc4 @@ -0,0 +1,10 @@ +/* (c) https://github.com/MontiCore/monticore */ +package de.monticore.types; + +grammar TypeParametersTest extends + de.monticore.types.TypeParameters, + de.monticore.types.MCFullGenericTypes, + de.monticore.types.MCArrayTypes, + de.monticore.types.MCFunctionTypes, + de.monticore.types.MCStructuralTypes { +} \ No newline at end of file diff --git a/monticore-grammar/src/test/grammars/de/monticore/types/TypeParametersWithoutIntersectionTypesTest.mc4 b/monticore-grammar/src/test/grammars/de/monticore/types/TypeParametersWithoutIntersectionTypesTest.mc4 new file mode 100644 index 0000000000..b6ce614715 --- /dev/null +++ b/monticore-grammar/src/test/grammars/de/monticore/types/TypeParametersWithoutIntersectionTypesTest.mc4 @@ -0,0 +1,9 @@ +/* (c) https://github.com/MontiCore/monticore */ +package de.monticore.types; + +grammar TypeParametersWithoutIntersectionTypesTest extends + de.monticore.types.TypeParameters, + de.monticore.types.MCFullGenericTypes, + de.monticore.types.MCArrayTypes, + de.monticore.types.MCFunctionTypes { +} \ No newline at end of file From 9530acd96c8a6d93579bf683cbde04673eae4997 Mon Sep 17 00:00:00 2001 From: SE-FDr <88034434+SE-FDr@users.noreply.github.com> Date: Tue, 30 Jul 2024 17:39:44 +0200 Subject: [PATCH 04/24] TypeParameterSymbolDeSer --- .../TypeParameterSymbolDeSer.java | 42 +++++++++++++++++++ 1 file changed, 42 insertions(+) create mode 100644 monticore-grammar/src/main/java/de/monticore/types/typeparameters/_symboltable/TypeParameterSymbolDeSer.java diff --git a/monticore-grammar/src/main/java/de/monticore/types/typeparameters/_symboltable/TypeParameterSymbolDeSer.java b/monticore-grammar/src/main/java/de/monticore/types/typeparameters/_symboltable/TypeParameterSymbolDeSer.java new file mode 100644 index 0000000000..4bb49b92c2 --- /dev/null +++ b/monticore-grammar/src/main/java/de/monticore/types/typeparameters/_symboltable/TypeParameterSymbolDeSer.java @@ -0,0 +1,42 @@ +/* (c) https://github.com/MontiCore/monticore */ +package de.monticore.types.typeparameters._symboltable; + +import de.monticore.symboltable.serialization.json.JsonObject; +import de.monticore.types.check.SymTypeExpression; +import de.monticore.types.check.SymTypeExpressionDeSer; + +import java.util.List; + +public class TypeParameterSymbolDeSer extends TypeParameterSymbolDeSerTOP { + + protected static final String SUPERTYPES_NAME = "superTypes"; + + @Override + protected void serializeSuperTypes( + List superTypes, + TypeParametersSymbols2Json s2j + ) { + SymTypeExpressionDeSer.serializeMember( + s2j.getJsonPrinter(), SUPERTYPES_NAME, superTypes + ); + } + + @Override + protected List deserializeSuperTypes( + JsonObject symbolJson + ) { + // support deprecated behavior + return deserializeSuperTypes(null, symbolJson); + } + + @Override + public List deserializeSuperTypes( + ITypeParametersScope enclosingScope, + JsonObject symbolJson + ) { + return SymTypeExpressionDeSer.deserializeListMember( + "superTypes", symbolJson, enclosingScope + ); + } + +} From c53980159e14b8b73271edc8040b27a62886081d Mon Sep 17 00:00:00 2001 From: SE-FDr <88034434+SE-FDr@users.noreply.github.com> Date: Tue, 30 Jul 2024 17:40:15 +0200 Subject: [PATCH 05/24] ASTTypeParameters::moveSymbolsIntoScope --- .../_ast/ASTTypeParameters.java | 23 +++++++++++++++++++ 1 file changed, 23 insertions(+) create mode 100644 monticore-grammar/src/main/java/de/monticore/types/typeparameters/_ast/ASTTypeParameters.java diff --git a/monticore-grammar/src/main/java/de/monticore/types/typeparameters/_ast/ASTTypeParameters.java b/monticore-grammar/src/main/java/de/monticore/types/typeparameters/_ast/ASTTypeParameters.java new file mode 100644 index 0000000000..5f82a6dc3c --- /dev/null +++ b/monticore-grammar/src/main/java/de/monticore/types/typeparameters/_ast/ASTTypeParameters.java @@ -0,0 +1,23 @@ +/* (c) https://github.com/MontiCore/monticore */ +package de.monticore.types.typeparameters._ast; + +import de.monticore.symbols.basicsymbols._symboltable.IBasicSymbolsScope; +import de.monticore.symbols.basicsymbols._symboltable.TypeVarSymbol; +import de.monticore.types.typeparameters._symboltable.ITypeParametersScope; + +public class ASTTypeParameters extends ASTTypeParametersTOP { + + /** + * Removes the typevar symbols in the current scope + * and moves them into the specified one. + */ + public void moveSymbolsIntoScope(IBasicSymbolsScope newScope) { + ITypeParametersScope oldScope = getEnclosingScope(); + for (ASTTypeParameter param : getTypeParameterList()) { + TypeVarSymbol sym = param.getSymbol(); + oldScope.remove(sym); + newScope.add(sym); + } + } + +} From 0c17a474cb0e2fb647b49af96473d7ee57e61612 Mon Sep 17 00:00:00 2001 From: SE-FDr <88034434+SE-FDr@users.noreply.github.com> Date: Tue, 30 Jul 2024 17:40:32 +0200 Subject: [PATCH 06/24] TypeParametersHaveUniqueNames CoCo --- .../cocos/TypeParametersHaveUniqueNames.java | 46 +++++++++++++++++++ 1 file changed, 46 insertions(+) create mode 100644 monticore-grammar/src/main/java/de/monticore/types/typeparameters/cocos/TypeParametersHaveUniqueNames.java diff --git a/monticore-grammar/src/main/java/de/monticore/types/typeparameters/cocos/TypeParametersHaveUniqueNames.java b/monticore-grammar/src/main/java/de/monticore/types/typeparameters/cocos/TypeParametersHaveUniqueNames.java new file mode 100644 index 0000000000..b48ce2376c --- /dev/null +++ b/monticore-grammar/src/main/java/de/monticore/types/typeparameters/cocos/TypeParametersHaveUniqueNames.java @@ -0,0 +1,46 @@ +/* (c) https://github.com/MontiCore/monticore */ +package de.monticore.types.typeparameters.cocos; + +import de.monticore.symbols.basicsymbols._symboltable.TypeSymbolTOP; +import de.monticore.types.typeparameters._ast.ASTTypeParameter; +import de.monticore.types.typeparameters._ast.ASTTypeParameters; +import de.monticore.types.typeparameters._cocos.TypeParametersASTTypeParametersCoCo; +import de.se_rwth.commons.logging.Log; + +import java.util.HashSet; +import java.util.List; +import java.util.Set; +import java.util.stream.Collectors; + +public class TypeParametersHaveUniqueNames + implements TypeParametersASTTypeParametersCoCo { + + @Override + public void check(ASTTypeParameters node) { + List names = node.getTypeParameterList().stream() + .map(ASTTypeParameter::getSymbol) + .map(TypeSymbolTOP::getName) + .collect(Collectors.toList()); + Set duplicates = findDuplicates(names); + for (String dupName : duplicates) { + Log.error("0xFDC14 The same name \"" + dupName + + "\" has been used for multiple type parameters." + ); + } + } + + // Helper + + protected Set findDuplicates(List listContainingDuplicates) { + final Set setToReturn = new HashSet<>(); + final Set set1 = new HashSet<>(); + + for (String modifierName : listContainingDuplicates) { + if (!set1.add(modifierName)) { + setToReturn.add(modifierName); + } + } + return setToReturn; + } + +} From 5cfe864574b9ecdd516455641ca13bf36765e9f3 Mon Sep 17 00:00:00 2001 From: SE-FDr <88034434+SE-FDr@users.noreply.github.com> Date: Tue, 30 Jul 2024 17:40:45 +0200 Subject: [PATCH 07/24] TypeParameters Tests --- .../de/monticore/types/TypeParameterTest.java | 94 ++++++++++++++++ .../TypeParametersPrettyPrinterTest.java | 102 ++++++++++++++++++ 2 files changed, 196 insertions(+) create mode 100644 monticore-grammar/src/test/java/de/monticore/types/TypeParameterTest.java create mode 100644 monticore-grammar/src/test/java/de/monticore/types/prettyprint/TypeParametersPrettyPrinterTest.java diff --git a/monticore-grammar/src/test/java/de/monticore/types/TypeParameterTest.java b/monticore-grammar/src/test/java/de/monticore/types/TypeParameterTest.java new file mode 100644 index 0000000000..55d8f4e7fd --- /dev/null +++ b/monticore-grammar/src/test/java/de/monticore/types/TypeParameterTest.java @@ -0,0 +1,94 @@ +/* (c) https://github.com/MontiCore/monticore */ +package de.monticore.types; + +import de.monticore.types.typeparameters._ast.ASTTypeBounds; +import de.monticore.types.typeparameters._ast.ASTTypeParameter; +import de.monticore.types.typeparameters._ast.ASTTypeParameters; +import de.monticore.types.typeparameterstest.TypeParametersTestMill; +import de.monticore.types.typeparameterswithoutintersectiontypestest.TypeParametersWithoutIntersectionTypesTestMill; +import de.se_rwth.commons.logging.Log; +import de.se_rwth.commons.logging.LogStub; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Disabled; +import org.junit.jupiter.api.Test; + +import java.io.IOException; +import java.util.Optional; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertTrue; + +public class TypeParameterTest { + + @BeforeEach + public void init() { + LogStub.init(); + Log.enableFailQuick(false); + } + + @Test + public void testTypeParameters() throws IOException { + String model = ""; + TypeParametersTestMill.reset(); + TypeParametersTestMill.init(); + Optional tpsOpt = + TypeParametersTestMill.parser().parse_StringTypeParameters(model); + assertTrue(tpsOpt.isPresent()); + ASTTypeParameters tps = tpsOpt.get(); + assertEquals(2, tps.sizeTypeParameters()); + ASTTypeParameter tp0 = tps.getTypeParameter(0); + assertEquals("T", tp0.getName()); + assertFalse(tp0.isPresentTypeBounds()); + ASTTypeParameter tp1 = tps.getTypeParameter(1); + assertEquals("U", tp1.getName()); + assertTrue(tp1.isPresentTypeBounds()); + ASTTypeBounds tb1 = tp1.getTypeBounds(); + assertEquals(1, tb1.sizeMCTypes()); + assertEquals("A.B", tb1.getMCType(0).printType()); + assertTrue(Log.getFindings().isEmpty()); + } + + // todo FDr write Trafo iff required -> discuss + @Test + @Disabled("needs trafo") + public void testTypeParametersMultipleBounds() throws IOException { + String model = ""; + TypeParametersTestMill.reset(); + TypeParametersTestMill.init(); + Optional tpsOpt = + TypeParametersTestMill.parser().parse_StringTypeParameters(model); + assertTrue(tpsOpt.isPresent()); + ASTTypeParameters tps = tpsOpt.get(); + assertEquals(1, tps.sizeTypeParameters()); + ASTTypeParameter tp0 = tps.getTypeParameter(0); + assertEquals("T", tp0.getName()); + assertTrue(tp0.isPresentTypeBounds()); + ASTTypeBounds tb1 = tp0.getTypeBounds(); + assertEquals(2, tb1.sizeMCTypes()); + assertEquals("A", tb1.getMCType(0).printType()); + assertEquals("B", tb1.getMCType(1).printType()); + assertTrue(Log.getFindings().isEmpty()); + } + + @Test + public void testTypeParametersMultipleBoundsWithoutIntersectionTypes() throws IOException { + String model = ""; + TypeParametersWithoutIntersectionTypesTestMill.reset(); + TypeParametersWithoutIntersectionTypesTestMill.init(); + Optional tpsOpt = + TypeParametersWithoutIntersectionTypesTestMill.parser().parse_StringTypeParameters(model); + assertTrue(tpsOpt.isPresent()); + ASTTypeParameters tps = tpsOpt.get(); + assertEquals(1, tps.sizeTypeParameters()); + ASTTypeParameter tp0 = tps.getTypeParameter(0); + assertEquals("T", tp0.getName()); + assertTrue(tp0.isPresentTypeBounds()); + ASTTypeBounds tb1 = tp0.getTypeBounds(); + assertEquals(2, tb1.sizeMCTypes()); + assertEquals("A", tb1.getMCType(0).printType()); + assertEquals("B", tb1.getMCType(1).printType()); + assertTrue(Log.getFindings().isEmpty()); + } + +} diff --git a/monticore-grammar/src/test/java/de/monticore/types/prettyprint/TypeParametersPrettyPrinterTest.java b/monticore-grammar/src/test/java/de/monticore/types/prettyprint/TypeParametersPrettyPrinterTest.java new file mode 100644 index 0000000000..e2ab6fa9b3 --- /dev/null +++ b/monticore-grammar/src/test/java/de/monticore/types/prettyprint/TypeParametersPrettyPrinterTest.java @@ -0,0 +1,102 @@ +/* (c) https://github.com/MontiCore/monticore */ +package de.monticore.types.prettyprint; + +import de.monticore.antlr4.MCConcreteParser; +import de.monticore.ast.ASTNode; +import de.monticore.types.typeparameterstest.TypeParametersTestMill; +import de.monticore.types.typeparameterstest._parser.TypeParametersTestParser; +import de.monticore.types3.AbstractTypeTest; +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.ValueSource; + +import java.io.IOException; +import java.util.Optional; +import java.util.function.Function; + +public class TypeParametersPrettyPrinterTest extends AbstractTypeTest { + + @BeforeEach + public void init() { + TypeParametersTestMill.reset(); + TypeParametersTestMill.init(); + } + + @ParameterizedTest + @ValueSource(strings = { + "", + " < T > ", + "", + ">", + ", N extends Node>", + "&B, V extends C&D>", + }) + public void testTypeParameters(String model) throws IOException { + TypeParametersTestParser parser = TypeParametersTestMill.parser(); + testPrettyPrinter( + model, parser, parser::parse_StringTypeParameters, + ast -> TypeParametersTestMill.prettyPrint(ast, true) + ); + } + + @ParameterizedTest + @ValueSource(strings = { + "T", + "LongTypeParameterName", + "T extends String", + "T extends Map>", + "T extends A & B", + "T extends A & B & C & D" + }) + public void testTypeParameter(String model) throws IOException { + TypeParametersTestParser parser = TypeParametersTestMill.parser(); + testPrettyPrinter( + model, parser, parser::parse_StringTypeParameter, + ast -> TypeParametersTestMill.prettyPrint(ast, true) + ); + } + + @ParameterizedTest + @ValueSource(strings = { + "String", + "Map>", + "A & B", + "A & B & C & D" + }) + public void testTypeBound(String model) throws IOException { + TypeParametersTestParser parser = TypeParametersTestMill.parser(); + testPrettyPrinter( + model, parser, parser::parse_StringTypeBounds, + ast -> TypeParametersTestMill.prettyPrint(ast, true) + ); + } + + // this function could be used for all pretty printer tests if required, + // however, the parameters are not great. + protected void testPrettyPrinter( + String model, + MCConcreteParser parser, + ParseFunction parseFunc, + Function prettyPrintFunc + ) throws IOException { + Optional astOpt = parseFunc.apply(model); + assertNoFindings(); + Assertions.assertTrue(astOpt.isPresent()); + Assertions.assertFalse(parser.hasErrors()); + N ast = astOpt.get(); + String output = prettyPrintFunc.apply(ast); + assertNoFindings(); + astOpt = parseFunc.apply(output); + assertNoFindings(); + Assertions.assertFalse(parser.hasErrors()); + Assertions.assertTrue(astOpt.isPresent()); + Assertions.assertTrue(ast.deepEquals(astOpt.get())); + } + + @FunctionalInterface + protected interface ParseFunction { + Optional apply(String t) throws IOException; + } + +} From 45252818df1b25457ee5e8dd3d2375573b0f95c0 Mon Sep 17 00:00:00 2001 From: SE-FDr <88034434+SE-FDr@users.noreply.github.com> Date: Wed, 31 Jul 2024 09:53:16 +0200 Subject: [PATCH 08/24] TypeParametersHaveUniqueNames CoCo Test --- .../TypeParametersHaveUniqueNamesTest.java | 73 +++++++++++++++++++ 1 file changed, 73 insertions(+) create mode 100644 monticore-grammar/src/test/java/de/monticore/types/typeparameters/cocos/TypeParametersHaveUniqueNamesTest.java diff --git a/monticore-grammar/src/test/java/de/monticore/types/typeparameters/cocos/TypeParametersHaveUniqueNamesTest.java b/monticore-grammar/src/test/java/de/monticore/types/typeparameters/cocos/TypeParametersHaveUniqueNamesTest.java new file mode 100644 index 0000000000..f3b2e77d7c --- /dev/null +++ b/monticore-grammar/src/test/java/de/monticore/types/typeparameters/cocos/TypeParametersHaveUniqueNamesTest.java @@ -0,0 +1,73 @@ +package de.monticore.types.typeparameters.cocos; + +import de.monticore.types.typeparameters.TypeParametersMill; +import de.monticore.types.typeparameters._ast.ASTTypeParameters; +import de.monticore.types.typeparameterstest.TypeParametersTestMill; +import de.monticore.types.typeparameterstest._cocos.TypeParametersTestCoCoChecker; +import de.monticore.types.typeparameterstest._parser.TypeParametersTestParser; +import de.se_rwth.commons.logging.Log; +import de.se_rwth.commons.logging.LogStub; +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.ValueSource; + +import java.io.IOException; +import java.util.Optional; + +public class TypeParametersHaveUniqueNamesTest { + + TypeParametersTestCoCoChecker checker; + + @BeforeEach + public void init() { + LogStub.init(); + Log.enableFailQuick(false); + TypeParametersTestMill.reset(); + TypeParametersTestMill.init(); + checker = new TypeParametersTestCoCoChecker(); + checker.setTraverser(TypeParametersTestMill.traverser()); + checker.addCoCo(new TypeParametersHaveUniqueNames()); + } + + @ParameterizedTest + @ValueSource(strings = { + "", + "", + ", U extends T>" + }) + public void testValid(String model) throws IOException { + ASTTypeParameters params = parseAndCreateSymTab(model); + checker.checkAll(params); + Assertions.assertTrue(Log.getFindings().isEmpty()); + } + + @ParameterizedTest + @ValueSource(strings = { + "", + "", + ", U extends T, U extends NotT>" + }) + public void testInvalid(String model) throws IOException { + ASTTypeParameters params = parseAndCreateSymTab(model); + checker.checkAll(params); + Assertions.assertFalse(Log.getFindings().isEmpty()); + Assertions.assertEquals( + "0xFDC14", + Log.getFindings().get(0).getMsg().substring(0, 7) + ); + } + + protected ASTTypeParameters parseAndCreateSymTab(String model) + throws IOException { + TypeParametersTestParser parser = TypeParametersTestMill.parser(); + Optional astOpt = + parser.parse_StringTypeParameters(model); + Assertions.assertFalse(parser.hasErrors()); + Assertions.assertTrue(astOpt.isPresent()); + Assertions.assertTrue(Log.getFindings().isEmpty()); + TypeParametersMill.scopesGenitorDelegator().createFromAST(astOpt.get()); + return astOpt.get(); + } + +} From f7364cc1665392e412eae398d722e6aeb353c7db Mon Sep 17 00:00:00 2001 From: SE-FDr <88034434+SE-FDr@users.noreply.github.com> Date: Wed, 31 Jul 2024 16:55:47 +0200 Subject: [PATCH 09/24] TypeParameters -> set dependencies in gradle.build --- monticore-grammar/build.gradle | 1 + 1 file changed, 1 insertion(+) diff --git a/monticore-grammar/build.gradle b/monticore-grammar/build.gradle index c4ac6d7dd7..ae3f2b0861 100644 --- a/monticore-grammar/build.gradle +++ b/monticore-grammar/build.gradle @@ -204,6 +204,7 @@ def grammarDependencies = ext { SIUnitLiterals = ["MCCommonLiterals", "SIUnits"] SIUnitTypes4Math = ["MCBasicTypes", "SIUnits"] SIUnitTypes4Computing = ["MCBasicTypes", "SIUnitTypes4Math"] + TypeParameters = ["BasicSymbols", "MCBasicTypes"] // three dependencies LambdaExpressions = ["BasicSymbols", "MCBasicTypes", "ExpressionsBasis"] From 5b7eaacfc638ea782ae75e52bde8ea083c31801d Mon Sep 17 00:00:00 2001 From: SE-FDr <88034434+SE-FDr@users.noreply.github.com> Date: Wed, 31 Jul 2024 16:56:01 +0200 Subject: [PATCH 10/24] TypeParameterNoCyclicInheritance CoCo --- .../TypeParameterNoCyclicInheritance.java | 59 +++++++++++++++++++ 1 file changed, 59 insertions(+) create mode 100644 monticore-grammar/src/main/java/de/monticore/types/typeparameters/cocos/TypeParameterNoCyclicInheritance.java diff --git a/monticore-grammar/src/main/java/de/monticore/types/typeparameters/cocos/TypeParameterNoCyclicInheritance.java b/monticore-grammar/src/main/java/de/monticore/types/typeparameters/cocos/TypeParameterNoCyclicInheritance.java new file mode 100644 index 0000000000..72c3834c4d --- /dev/null +++ b/monticore-grammar/src/main/java/de/monticore/types/typeparameters/cocos/TypeParameterNoCyclicInheritance.java @@ -0,0 +1,59 @@ +/* (c) https://github.com/MontiCore/monticore */ +package de.monticore.types.typeparameters.cocos; + +import de.monticore.types.check.SymTypeExpression; +import de.monticore.types.check.SymTypeVariable; +import de.monticore.types.typeparameters._ast.ASTTypeParameter; +import de.monticore.types.typeparameters._cocos.TypeParametersASTTypeParameterCoCo; +import de.monticore.types3.SymTypeRelations; +import de.se_rwth.commons.logging.Log; + +import java.util.ArrayList; +import java.util.List; + +import static de.monticore.types.check.SymTypeExpressionFactory.createTypeVariable; + +/** + * Finds instances of circular inheritance, + * e.g., + */ +public class TypeParameterNoCyclicInheritance + implements TypeParametersASTTypeParameterCoCo { + + @Override + public void check(ASTTypeParameter node) { + SymTypeVariable thisVar = createTypeVariable(node.getSymbol()); + checkForCircularInheritance(List.of(thisVar), node); + } + + protected boolean checkForCircularInheritance( + List currentInheritanceList, + ASTTypeParameter node + ) { + List superTypes = SymTypeRelations.getNominalSuperTypes( + currentInheritanceList.get(currentInheritanceList.size() - 1) + ); + for (SymTypeExpression superType : superTypes) { + if (currentInheritanceList.stream().anyMatch(superType::deepEquals)) { + Log.error("0xFDC12 Checked supertypes of type variable \"" + + node.getName() + + "\" and found circular inheritance of type " + + superType.printFullName(), + node.get_SourcePositionStart(), + node.get_SourcePositionEnd() + ); + return false; + } + else { + List nextInheritanceList = new ArrayList<>(); + nextInheritanceList.addAll(currentInheritanceList); + nextInheritanceList.add(superType); + if (!checkForCircularInheritance(nextInheritanceList, node)) { + return false; + } + } + } + return true; + } + +} From d4d8a19dcb377d663c68bcc28fe0311cfdce6f03 Mon Sep 17 00:00:00 2001 From: SE-FDr <88034434+SE-FDr@users.noreply.github.com> Date: Wed, 31 Jul 2024 16:56:53 +0200 Subject: [PATCH 11/24] TypeParameterNoCyclicInheritance CoCo Test --- ...TypeParametersNoCyclicInheritanceTest.java | 96 +++++++++++++++++++ 1 file changed, 96 insertions(+) create mode 100644 monticore-grammar/src/test/java/de/monticore/types/typeparameters/cocos/TypeParametersNoCyclicInheritanceTest.java diff --git a/monticore-grammar/src/test/java/de/monticore/types/typeparameters/cocos/TypeParametersNoCyclicInheritanceTest.java b/monticore-grammar/src/test/java/de/monticore/types/typeparameters/cocos/TypeParametersNoCyclicInheritanceTest.java new file mode 100644 index 0000000000..4209473039 --- /dev/null +++ b/monticore-grammar/src/test/java/de/monticore/types/typeparameters/cocos/TypeParametersNoCyclicInheritanceTest.java @@ -0,0 +1,96 @@ +package de.monticore.types.typeparameters.cocos; + +import de.monticore.types.typeparameters.TypeParametersMill; +import de.monticore.types.typeparameters._ast.ASTTypeParameters; +import de.monticore.types.typeparameters._symboltable.ITypeParametersArtifactScope; +import de.monticore.types.typeparameters._symboltable.TypeParametersSTCompleteTypes; +import de.monticore.types.typeparameterstest.TypeParametersTestMill; +import de.monticore.types.typeparameterstest._cocos.TypeParametersTestCoCoChecker; +import de.monticore.types.typeparameterstest._parser.TypeParametersTestParser; +import de.monticore.types.typeparameterstest._visitor.TypeParametersTestTraverser; +import de.monticore.types3.ITypeCalculator; +import de.monticore.types3.Type4Ast; +import de.monticore.types3.TypeCalculator3; +import de.monticore.types3.generics.context.InferenceContext4Ast; +import de.monticore.types3.util.CombineExpressionsWithLiteralsTypeTraverserFactory; +import de.monticore.visitor.ITraverser; +import de.se_rwth.commons.logging.Log; +import de.se_rwth.commons.logging.LogStub; +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.ValueSource; + +import java.io.IOException; +import java.util.Optional; + +public class TypeParametersNoCyclicInheritanceTest { + + TypeParametersTestCoCoChecker checker; + + @BeforeEach + public void init() { + LogStub.init(); + Log.enableFailQuick(false); + TypeParametersTestMill.reset(); + TypeParametersTestMill.init(); + checker = new TypeParametersTestCoCoChecker(); + checker.setTraverser(TypeParametersTestMill.traverser()); + checker.addCoCo(new TypeParameterNoCyclicInheritance()); + } + + @ParameterizedTest + @ValueSource(strings = { + "", + "" + }) + public void testValid(String model) throws IOException { + ASTTypeParameters params = parseAndCreateSymTab(model); + checker.checkAll(params); + Assertions.assertTrue(Log.getFindings().isEmpty()); + } + + @ParameterizedTest + @ValueSource(strings = { + "", + "", + }) + public void testInvalid(String model) throws IOException { + ASTTypeParameters params = parseAndCreateSymTab(model); + checker.checkAll(params); + Assertions.assertFalse(Log.getFindings().isEmpty()); + Assertions.assertEquals( + "0xFDC12", + Log.getFindings().get(0).getMsg().substring(0, 7) + ); + } + + protected ASTTypeParameters parseAndCreateSymTab(String model) + throws IOException { + TypeParametersTestParser parser = TypeParametersTestMill.parser(); + Optional astOpt = + parser.parse_StringTypeParameters(model); + Assertions.assertFalse(parser.hasErrors()); + Assertions.assertTrue(astOpt.isPresent()); + Assertions.assertTrue(Log.getFindings().isEmpty()); + ITypeParametersArtifactScope artifactScope = TypeParametersMill + .scopesGenitorDelegator().createFromAST(astOpt.get()); + artifactScope.setName("aName"); + TypeParametersTestTraverser stCompleter = + TypeParametersTestMill.traverser(); + ITypeCalculator tc = getTypeCalculator(); + stCompleter.add4TypeParameters(new TypeParametersSTCompleteTypes(tc)); + astOpt.get().accept(stCompleter); + return astOpt.get(); + } + + protected ITypeCalculator getTypeCalculator() { + Type4Ast type4Ast = new Type4Ast(); + InferenceContext4Ast infCtx4Ast = new InferenceContext4Ast(); + ITraverser typeTraverser = + new CombineExpressionsWithLiteralsTypeTraverserFactory() + .createTraverser(type4Ast, infCtx4Ast); + return new TypeCalculator3(typeTraverser, type4Ast, infCtx4Ast); + } + +} From ebbc3e32b960205434085bae0a3209f4f7de97d8 Mon Sep 17 00:00:00 2001 From: SE-FDr <88034434+SE-FDr@users.noreply.github.com> Date: Wed, 31 Jul 2024 16:58:00 +0200 Subject: [PATCH 12/24] TypeParametersSTCompleteTypes --- .../TypeParametersSTCompleteTypes.java | 40 +++++++++++++++++++ 1 file changed, 40 insertions(+) create mode 100644 monticore-grammar/src/main/java/de/monticore/types/typeparameters/_symboltable/TypeParametersSTCompleteTypes.java diff --git a/monticore-grammar/src/main/java/de/monticore/types/typeparameters/_symboltable/TypeParametersSTCompleteTypes.java b/monticore-grammar/src/main/java/de/monticore/types/typeparameters/_symboltable/TypeParametersSTCompleteTypes.java new file mode 100644 index 0000000000..3afbca4d4f --- /dev/null +++ b/monticore-grammar/src/main/java/de/monticore/types/typeparameters/_symboltable/TypeParametersSTCompleteTypes.java @@ -0,0 +1,40 @@ +package de.monticore.types.typeparameters._symboltable; + +import de.monticore.types.check.SymTypeExpression; +import de.monticore.types.mcbasictypes._ast.ASTMCType; +import de.monticore.types.typeparameters._ast.ASTTypeParameter; +import de.monticore.types.typeparameters._visitor.TypeParametersVisitor2; +import de.monticore.types3.ITypeCalculator; + +import java.util.ArrayList; +import java.util.List; + +/** + * Sets the superTypes of the type parameter symbols. + *

+ * IMPORTANT: usually, the symbols are moved from the scope they are in + * into their corresponding types scope. + * In this case, they cannot be found from within the scope enclosing + * of ASTNode referencing the superTypes. + * As such, this must be run before any such transformation takes place! + */ +public class TypeParametersSTCompleteTypes implements TypeParametersVisitor2 { + + ITypeCalculator tc; + + public TypeParametersSTCompleteTypes(ITypeCalculator tc) { + this.tc = tc; + } + + @Override + public void visit(ASTTypeParameter node) { + if (node.isPresentTypeBounds()) { + List bounds = new ArrayList<>(); + for (ASTMCType astTypeBound : node.getTypeBounds().getMCTypeList()) { + bounds.add(tc.symTypeFromAST(astTypeBound)); + } + // error logged if obscure + node.getSymbol().setSuperTypesList(bounds); + } + } +} From bcf1e522d949b298492da4d954d0f0987cda0cc6 Mon Sep 17 00:00:00 2001 From: SE-FDr <88034434+SE-FDr@users.noreply.github.com> Date: Wed, 31 Jul 2024 18:36:19 +0200 Subject: [PATCH 13/24] fix bound check for subtypes of type variable symbols --- .../basicsymbols/_symboltable/IBasicSymbolsScope.java | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/monticore-grammar/src/main/java/de/monticore/symbols/basicsymbols/_symboltable/IBasicSymbolsScope.java b/monticore-grammar/src/main/java/de/monticore/symbols/basicsymbols/_symboltable/IBasicSymbolsScope.java index eb33d4730c..3c3285624a 100644 --- a/monticore-grammar/src/main/java/de/monticore/symbols/basicsymbols/_symboltable/IBasicSymbolsScope.java +++ b/monticore-grammar/src/main/java/de/monticore/symbols/basicsymbols/_symboltable/IBasicSymbolsScope.java @@ -15,7 +15,11 @@ public interface IBasicSymbolsScope extends IBasicSymbolsScopeTOP { * e.g. class C {} // T is bound within the class */ default boolean isTypeVariableBound(TypeVarSymbol typeVar) { - if (getLocalTypeVarSymbols().stream() + List localVars = resolveTypeVarLocallyMany( + false, typeVar.getName(), + AccessModifier.ALL_INCLUSION, (tv) -> true + ); + if (localVars.stream() .anyMatch(otherTypeVar -> otherTypeVar == typeVar)) { return true; } From 55a231029680854b91743855e8ee48d09d3f832b Mon Sep 17 00:00:00 2001 From: SE-FDr <88034434+SE-FDr@users.noreply.github.com> Date: Wed, 31 Jul 2024 18:36:52 +0200 Subject: [PATCH 14/24] fix infinite loop in SymTypeVariable::deepEquals --- .../main/java/de/monticore/types/check/SymTypeVariable.java | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/monticore-grammar/src/main/java/de/monticore/types/check/SymTypeVariable.java b/monticore-grammar/src/main/java/de/monticore/types/check/SymTypeVariable.java index d47b8f5aaa..4869940341 100644 --- a/monticore-grammar/src/main/java/de/monticore/types/check/SymTypeVariable.java +++ b/monticore-grammar/src/main/java/de/monticore/types/check/SymTypeVariable.java @@ -320,14 +320,16 @@ public boolean deepEquals(SymTypeExpression sym) { return true; } SymTypeVariable symVar = (SymTypeVariable) sym; + if (!denotesSameVar(symVar)) { + return false; + } if (!getUpperBound().deepEquals(symVar.getUpperBound())) { return false; } else if (!getLowerBound().deepEquals(symVar.getLowerBound())) { return false; } - // cannot identify without a name at this point - return denotesSameVar(symVar); + return true; } @Override From efba36cad907bfa4540578a5dcd2ca9ccfc0d2fa Mon Sep 17 00:00:00 2001 From: Flo Drux Date: Thu, 1 Aug 2024 09:31:31 +0200 Subject: [PATCH 15/24] TypeParameters.mc4 moved todo notes --- .../src/main/grammars/de/monticore/types/TypeParameters.mc4 | 4 ---- 1 file changed, 4 deletions(-) diff --git a/monticore-grammar/src/main/grammars/de/monticore/types/TypeParameters.mc4 b/monticore-grammar/src/main/grammars/de/monticore/types/TypeParameters.mc4 index c150290ca4..dadef709a3 100644 --- a/monticore-grammar/src/main/grammars/de/monticore/types/TypeParameters.mc4 +++ b/monticore-grammar/src/main/grammars/de/monticore/types/TypeParameters.mc4 @@ -5,10 +5,6 @@ package de.monticore.types; import de.monticore.symbols.BasicSymbols; -// TODO FDr: remove from JavaDSL after merge -// TODO FDr: documentation in MDs -// TODO FDr: CoCos -> Test and COCO that types fit together - /** * This grammar introduces type parameters such as T, U extends Set, * and type parameter lists such as From 6eec50464d558c2d9c25029af2cc727780ce4e9d Mon Sep 17 00:00:00 2001 From: SE-FDr <88034434+SE-FDr@users.noreply.github.com> Date: Thu, 1 Aug 2024 09:44:01 +0200 Subject: [PATCH 16/24] remove unnecessary TypeParameterSymbol --- .../de/monticore/types/TypeParameters.mc4 | 2 +- .../TypeParameterSymbolDeSer.java | 42 ------------------- 2 files changed, 1 insertion(+), 43 deletions(-) delete mode 100644 monticore-grammar/src/main/java/de/monticore/types/typeparameters/_symboltable/TypeParameterSymbolDeSer.java diff --git a/monticore-grammar/src/main/grammars/de/monticore/types/TypeParameters.mc4 b/monticore-grammar/src/main/grammars/de/monticore/types/TypeParameters.mc4 index dadef709a3..03097a1206 100644 --- a/monticore-grammar/src/main/grammars/de/monticore/types/TypeParameters.mc4 +++ b/monticore-grammar/src/main/grammars/de/monticore/types/TypeParameters.mc4 @@ -39,7 +39,7 @@ component grammar TypeParameters * T * U extends T & Comparable */ - symbol TypeParameter implements TypeVar = + TypeParameter implements TypeVar = Name ("extends" TypeBounds)? ; diff --git a/monticore-grammar/src/main/java/de/monticore/types/typeparameters/_symboltable/TypeParameterSymbolDeSer.java b/monticore-grammar/src/main/java/de/monticore/types/typeparameters/_symboltable/TypeParameterSymbolDeSer.java deleted file mode 100644 index 4bb49b92c2..0000000000 --- a/monticore-grammar/src/main/java/de/monticore/types/typeparameters/_symboltable/TypeParameterSymbolDeSer.java +++ /dev/null @@ -1,42 +0,0 @@ -/* (c) https://github.com/MontiCore/monticore */ -package de.monticore.types.typeparameters._symboltable; - -import de.monticore.symboltable.serialization.json.JsonObject; -import de.monticore.types.check.SymTypeExpression; -import de.monticore.types.check.SymTypeExpressionDeSer; - -import java.util.List; - -public class TypeParameterSymbolDeSer extends TypeParameterSymbolDeSerTOP { - - protected static final String SUPERTYPES_NAME = "superTypes"; - - @Override - protected void serializeSuperTypes( - List superTypes, - TypeParametersSymbols2Json s2j - ) { - SymTypeExpressionDeSer.serializeMember( - s2j.getJsonPrinter(), SUPERTYPES_NAME, superTypes - ); - } - - @Override - protected List deserializeSuperTypes( - JsonObject symbolJson - ) { - // support deprecated behavior - return deserializeSuperTypes(null, symbolJson); - } - - @Override - public List deserializeSuperTypes( - ITypeParametersScope enclosingScope, - JsonObject symbolJson - ) { - return SymTypeExpressionDeSer.deserializeListMember( - "superTypes", symbolJson, enclosingScope - ); - } - -} From 870c73a95127dcabc8b4d4de561ef441b8575baf Mon Sep 17 00:00:00 2001 From: SE-FDr <88034434+SE-FDr@users.noreply.github.com> Date: Tue, 6 Aug 2024 09:01:21 +0200 Subject: [PATCH 17/24] TypeParameter Docu --- .../src/main/grammars/de/monticore/Grammars.md | 12 ++++++++++++ .../src/main/grammars/de/monticore/types/Types.md | 12 ++++++++++++ 2 files changed, 24 insertions(+) diff --git a/monticore-grammar/src/main/grammars/de/monticore/Grammars.md b/monticore-grammar/src/main/grammars/de/monticore/Grammars.md index 18d51fd0ef..9a32c0e295 100644 --- a/monticore-grammar/src/main/grammars/de/monticore/Grammars.md +++ b/monticore-grammar/src/main/grammars/de/monticore/Grammars.md @@ -186,6 +186,18 @@ be issued (instead of runtime errors only). * `String` could be treated identical to `R".*"`, but it may be that the chosen typecheck wants to enforce explicit coercion. +### [TypeParameters.mc4](types/TypeParameters.mc4) (alpha) + +This grammar offers ways to define type parameters for, +e.g., classes or functions. +Modeling elements with type parameters represent +type-level functions that take type arguments; +For Example, the generic type `List` has one type parameter `T` +and therefore takes one type argument. +`List` or `List` cannot be used directly in a model, +instead, providing a type argument (e.g., `int`) +yields a type `List` that can be used. + ## Symbols: List of Grammars in package `de.monticore.symbols` These two grammars do not provide syntax themselves, but diff --git a/monticore-grammar/src/main/grammars/de/monticore/types/Types.md b/monticore-grammar/src/main/grammars/de/monticore/types/Types.md index fec008c79a..79f369b951 100644 --- a/monticore-grammar/src/main/grammars/de/monticore/types/Types.md +++ b/monticore-grammar/src/main/grammars/de/monticore/types/Types.md @@ -17,6 +17,7 @@ the following language components: * [`MCArrayTypes`](MCArrayTypes.mc4) * [`MCFunctionTypes`](MCFunctionTypes.mc4) * [`MCStructuralTypes`](MCStructuralTypes.mc4) +* [`TypeParameters`](TypeParameters.mc4) * [`SIUnitTypes4Math`](../siunit/SIUnitTypes4Math.mc4) * [`SIUnitTypes4Computing`](../siunit/SIUnitTypes4Computing.mc4) * [`RegExType`](../regex/RegExType.mc4) @@ -65,6 +66,17 @@ arguments including wild card types. When using this language component, types such as `Person`, `Map, ? extends Person>`, and `a.b.C.E.F.H` are expressible. +## [`TypeParameters`](TypeParameters.mc4) + +This language component offers ways to define type parameter lists +for generic model elements, e.g., generic classes such as `Map`. +The resulting Symbols can than be used with, e.g., +[`MCSimpleGenericTypes`](MCSimpleGenericTypes.mc4), or +[`MCFullGenericTypes`](MCFullGenericTypes.mc4) +to define types by providing the corresponding type arguments. +Additionally, type parameters may optionally have upper bounds, +e.g., `T extends Person`, `U extends T & Comparable`. + ## [`MCArrayTypes`](MCArrayTypes.mc4) This language component allows to express array types like `Person[]` or From f789011d5bd5e320a652aabe1c4dbe7609146465 Mon Sep 17 00:00:00 2001 From: SE-FDr <88034434+SE-FDr@users.noreply.github.com> Date: Tue, 6 Aug 2024 12:31:39 +0200 Subject: [PATCH 18/24] TypeParameters.mlc fix --- monticore-grammar/build.gradle | 6 ++++++ .../main/grammars/de/monticore/types/TypeParameters.mlc | 9 +++++++-- 2 files changed, 13 insertions(+), 2 deletions(-) diff --git a/monticore-grammar/build.gradle b/monticore-grammar/build.gradle index ae3f2b0861..cded2e5671 100644 --- a/monticore-grammar/build.gradle +++ b/monticore-grammar/build.gradle @@ -373,6 +373,9 @@ checkArtifactsMCFullGenericTypes.dependsOn(checkArtifactsMCSimpleGenericTypes) checkArtifactsMCSimpleGenericTypes.dependsOn(checkArtifactsMCCollectionTypes) +checkArtifactsTypeParameters.dependsOn(checkArtifactsBasicSymbols) +checkArtifactsTypeParameters.dependsOn(checkArtifactsMCBasicTypes) + checkArtifactsCardinality.dependsOn(checkArtifactsMCBasics) checkArtifactsCardinality.dependsOn(checkArtifactsMCCommonLiterals) @@ -504,6 +507,9 @@ showArtifactsMCFullGenericTypes.dependsOn(showArtifactsMCSimpleGenericTypes) showArtifactsMCSimpleGenericTypes.dependsOn(showArtifactsMCCollectionTypes) +showArtifactsTypeParameters.dependsOn(showArtifactsBasicSymbols) +showArtifactsTypeParameters.dependsOn(showArtifactsMCBasicTypes) + showArtifactsCardinality.dependsOn(showArtifactsMCBasics) showArtifactsCardinality.dependsOn(showArtifactsMCCommonLiterals) diff --git a/monticore-grammar/src/main/grammars/de/monticore/types/TypeParameters.mlc b/monticore-grammar/src/main/grammars/de/monticore/types/TypeParameters.mlc index 3692b79347..253407d0ee 100644 --- a/monticore-grammar/src/main/grammars/de/monticore/types/TypeParameters.mlc +++ b/monticore-grammar/src/main/grammars/de/monticore/types/TypeParameters.mlc @@ -1,6 +1,6 @@ package de.monticore.types; -mlc MCFullGenericTypes { +mlc TypeParameters { //export the grammar export "$projectDir/src/main/grammars" { @@ -18,7 +18,12 @@ mlc MCFullGenericTypes { } promote { - mlc "de.monticore.types.TypeParameters"; + include "$projectDir/src/main/java/de/monticore/types3/*.java"; + } + + promote { + mlc "de.monticore.symbols.BasicSymbols"; + mlc "de.monticore.types.MCBasicTypes"; } } From b7f57fdc104002cee58fef6c65365ac996aef256 Mon Sep 17 00:00:00 2001 From: SE-FDr <88034434+SE-FDr@users.noreply.github.com> Date: Tue, 6 Aug 2024 12:33:18 +0200 Subject: [PATCH 19/24] JavaLight use TypeParameters --- .../src/main/grammars/de/monticore/JavaLight.mc4 | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/monticore-grammar/src/main/grammars/de/monticore/JavaLight.mc4 b/monticore-grammar/src/main/grammars/de/monticore/JavaLight.mc4 index 62143b8e80..faac0027a1 100644 --- a/monticore-grammar/src/main/grammars/de/monticore/JavaLight.mc4 +++ b/monticore-grammar/src/main/grammars/de/monticore/JavaLight.mc4 @@ -27,7 +27,8 @@ component grammar JavaLight extends AssignmentExpressions, JavaClassExpressions, MCCommonStatements, MCArrayStatements, - MCReturnStatements { + MCReturnStatements, + TypeParameters { interface ClassBodyDeclaration; @@ -45,12 +46,12 @@ symbolrule JavaMethod = isDefault: boolean; MethodDeclaration implements JavaMethod, ClassBodyDeclaration, InterfaceBodyDeclaration - = MCModifier* ("<"(MCTypeArgument||",")+">")? + = MCModifier* TypeParameters? MCReturnType Name FormalParameters (dim:"[" "]")* ("throws" Throws)? (MCJavaBlock | ";"); ConstructorDeclaration implements JavaMethod, ClassBodyDeclaration - = MCModifier* ("<"(MCTypeArgument||",")+">")? Name FormalParameters + = MCModifier* TypeParameters? Name FormalParameters ("throws" Throws)? MCJavaBlock; ConstDeclaration extends LocalVariableDeclarationStatement From dbe5bc19543cb87366d27ef854f546f9e0c499ec Mon Sep 17 00:00:00 2001 From: SE-FDr <88034434+SE-FDr@users.noreply.github.com> Date: Tue, 6 Aug 2024 15:54:02 +0200 Subject: [PATCH 20/24] JavaLight.mlc use TypeParameters --- monticore-grammar/src/main/grammars/de/monticore/JavaLight.mlc | 1 + 1 file changed, 1 insertion(+) diff --git a/monticore-grammar/src/main/grammars/de/monticore/JavaLight.mlc b/monticore-grammar/src/main/grammars/de/monticore/JavaLight.mlc index abfe0684ba..f605e371e6 100644 --- a/monticore-grammar/src/main/grammars/de/monticore/JavaLight.mlc +++ b/monticore-grammar/src/main/grammars/de/monticore/JavaLight.mlc @@ -24,6 +24,7 @@ mlc JavaLight { mlc "de.monticore.statements.MCCommonStatements"; mlc "de.monticore.statements.MCArrayStatements"; mlc "de.monticore.statements.MCReturnStatements"; + mlc "de.monticore.types.TypeParameters"; } uses { From 38ef6bc5abba80b290e9aee7f361eace59d0ff99 Mon Sep 17 00:00:00 2001 From: SE-FDr <88034434+SE-FDr@users.noreply.github.com> Date: Tue, 13 Aug 2024 09:42:22 +0200 Subject: [PATCH 21/24] TypeParameters small cleanup --- .../_ast/ASTTypeParameters.java | 23 ---------------- .../TypeParametersSTCompleteTypes.java | 19 +++++--------- .../de/monticore/types/TypeParameterTest.java | 26 +++++++------------ 3 files changed, 16 insertions(+), 52 deletions(-) delete mode 100644 monticore-grammar/src/main/java/de/monticore/types/typeparameters/_ast/ASTTypeParameters.java diff --git a/monticore-grammar/src/main/java/de/monticore/types/typeparameters/_ast/ASTTypeParameters.java b/monticore-grammar/src/main/java/de/monticore/types/typeparameters/_ast/ASTTypeParameters.java deleted file mode 100644 index 5f82a6dc3c..0000000000 --- a/monticore-grammar/src/main/java/de/monticore/types/typeparameters/_ast/ASTTypeParameters.java +++ /dev/null @@ -1,23 +0,0 @@ -/* (c) https://github.com/MontiCore/monticore */ -package de.monticore.types.typeparameters._ast; - -import de.monticore.symbols.basicsymbols._symboltable.IBasicSymbolsScope; -import de.monticore.symbols.basicsymbols._symboltable.TypeVarSymbol; -import de.monticore.types.typeparameters._symboltable.ITypeParametersScope; - -public class ASTTypeParameters extends ASTTypeParametersTOP { - - /** - * Removes the typevar symbols in the current scope - * and moves them into the specified one. - */ - public void moveSymbolsIntoScope(IBasicSymbolsScope newScope) { - ITypeParametersScope oldScope = getEnclosingScope(); - for (ASTTypeParameter param : getTypeParameterList()) { - TypeVarSymbol sym = param.getSymbol(); - oldScope.remove(sym); - newScope.add(sym); - } - } - -} diff --git a/monticore-grammar/src/main/java/de/monticore/types/typeparameters/_symboltable/TypeParametersSTCompleteTypes.java b/monticore-grammar/src/main/java/de/monticore/types/typeparameters/_symboltable/TypeParametersSTCompleteTypes.java index 3afbca4d4f..458dcd5320 100644 --- a/monticore-grammar/src/main/java/de/monticore/types/typeparameters/_symboltable/TypeParametersSTCompleteTypes.java +++ b/monticore-grammar/src/main/java/de/monticore/types/typeparameters/_symboltable/TypeParametersSTCompleteTypes.java @@ -11,12 +11,6 @@ /** * Sets the superTypes of the type parameter symbols. - *

- * IMPORTANT: usually, the symbols are moved from the scope they are in - * into their corresponding types scope. - * In this case, they cannot be found from within the scope enclosing - * of ASTNode referencing the superTypes. - * As such, this must be run before any such transformation takes place! */ public class TypeParametersSTCompleteTypes implements TypeParametersVisitor2 { @@ -28,13 +22,12 @@ public TypeParametersSTCompleteTypes(ITypeCalculator tc) { @Override public void visit(ASTTypeParameter node) { - if (node.isPresentTypeBounds()) { - List bounds = new ArrayList<>(); - for (ASTMCType astTypeBound : node.getTypeBounds().getMCTypeList()) { - bounds.add(tc.symTypeFromAST(astTypeBound)); - } - // error logged if obscure - node.getSymbol().setSuperTypesList(bounds); + List bounds = new ArrayList<>(); + for (ASTMCType astTypeBound : node.getMCTypeList()) { + bounds.add(tc.symTypeFromAST(astTypeBound)); } + // error logged if obscure + node.getSymbol().setSuperTypesList(bounds); } + } diff --git a/monticore-grammar/src/test/java/de/monticore/types/TypeParameterTest.java b/monticore-grammar/src/test/java/de/monticore/types/TypeParameterTest.java index 55d8f4e7fd..73b599ffe0 100644 --- a/monticore-grammar/src/test/java/de/monticore/types/TypeParameterTest.java +++ b/monticore-grammar/src/test/java/de/monticore/types/TypeParameterTest.java @@ -1,7 +1,6 @@ /* (c) https://github.com/MontiCore/monticore */ package de.monticore.types; -import de.monticore.types.typeparameters._ast.ASTTypeBounds; import de.monticore.types.typeparameters._ast.ASTTypeParameter; import de.monticore.types.typeparameters._ast.ASTTypeParameters; import de.monticore.types.typeparameterstest.TypeParametersTestMill; @@ -39,13 +38,12 @@ public void testTypeParameters() throws IOException { assertEquals(2, tps.sizeTypeParameters()); ASTTypeParameter tp0 = tps.getTypeParameter(0); assertEquals("T", tp0.getName()); - assertFalse(tp0.isPresentTypeBounds()); + assertTrue(tp0.isEmptyMCTypes()); ASTTypeParameter tp1 = tps.getTypeParameter(1); assertEquals("U", tp1.getName()); - assertTrue(tp1.isPresentTypeBounds()); - ASTTypeBounds tb1 = tp1.getTypeBounds(); - assertEquals(1, tb1.sizeMCTypes()); - assertEquals("A.B", tb1.getMCType(0).printType()); + assertFalse(tp1.isEmptyMCTypes()); + assertEquals(1, tp1.sizeMCTypes()); + assertEquals("A.B", tp1.getMCType(0).printType()); assertTrue(Log.getFindings().isEmpty()); } @@ -63,11 +61,9 @@ public void testTypeParametersMultipleBounds() throws IOException { assertEquals(1, tps.sizeTypeParameters()); ASTTypeParameter tp0 = tps.getTypeParameter(0); assertEquals("T", tp0.getName()); - assertTrue(tp0.isPresentTypeBounds()); - ASTTypeBounds tb1 = tp0.getTypeBounds(); - assertEquals(2, tb1.sizeMCTypes()); - assertEquals("A", tb1.getMCType(0).printType()); - assertEquals("B", tb1.getMCType(1).printType()); + assertEquals(2, tp0.sizeMCTypes()); + assertEquals("A", tp0.getMCType(0).printType()); + assertEquals("B", tp0.getMCType(1).printType()); assertTrue(Log.getFindings().isEmpty()); } @@ -83,11 +79,9 @@ public void testTypeParametersMultipleBoundsWithoutIntersectionTypes() throws IO assertEquals(1, tps.sizeTypeParameters()); ASTTypeParameter tp0 = tps.getTypeParameter(0); assertEquals("T", tp0.getName()); - assertTrue(tp0.isPresentTypeBounds()); - ASTTypeBounds tb1 = tp0.getTypeBounds(); - assertEquals(2, tb1.sizeMCTypes()); - assertEquals("A", tb1.getMCType(0).printType()); - assertEquals("B", tb1.getMCType(1).printType()); + assertEquals(2, tp0.sizeMCTypes()); + assertEquals("A", tp0.getMCType(0).printType()); + assertEquals("B", tp0.getMCType(1).printType()); assertTrue(Log.getFindings().isEmpty()); } From ed08064e81b835cedcfb97319c24746560ef7c5d Mon Sep 17 00:00:00 2001 From: SE-FDr <88034434+SE-FDr@users.noreply.github.com> Date: Thu, 8 Aug 2024 10:18:29 +0200 Subject: [PATCH 22/24] remove TypeParameters.mc4 TypeBounds --- .../de/monticore/types/TypeParameters.mc4 | 18 ++++-------------- .../TypeParametersPrettyPrinterTest.java | 15 --------------- 2 files changed, 4 insertions(+), 29 deletions(-) diff --git a/monticore-grammar/src/main/grammars/de/monticore/types/TypeParameters.mc4 b/monticore-grammar/src/main/grammars/de/monticore/types/TypeParameters.mc4 index 03097a1206..5ab3691971 100644 --- a/monticore-grammar/src/main/grammars/de/monticore/types/TypeParameters.mc4 +++ b/monticore-grammar/src/main/grammars/de/monticore/types/TypeParameters.mc4 @@ -34,26 +34,16 @@ component grammar TypeParameters /** * ASTTypeParameter represents one type parameter. * Each type parameter has a name - * and can have an optional upper type bound. + * and can have an optional list of upper type bounds. + * Each bound list consists of an arbitrary positive number of types. + * Each of those types is a supertype of the corresponding type parameter. * Example: * T * U extends T & Comparable */ TypeParameter implements TypeVar = Name - ("extends" TypeBounds)? - ; - - /** - * ASTTypeBounds represents the (upper) type bounds of a type parameter. - * Each bound list consists of an arbitrary positive number of types. - * Each of those types is a supertype of the corresponding type parameter. - * Example: - * Person - * Person & Comparable - */ - TypeBounds = - (MCType || "&")+ + ("extends" (MCType || "&")+)? ; } diff --git a/monticore-grammar/src/test/java/de/monticore/types/prettyprint/TypeParametersPrettyPrinterTest.java b/monticore-grammar/src/test/java/de/monticore/types/prettyprint/TypeParametersPrettyPrinterTest.java index e2ab6fa9b3..d07d874a87 100644 --- a/monticore-grammar/src/test/java/de/monticore/types/prettyprint/TypeParametersPrettyPrinterTest.java +++ b/monticore-grammar/src/test/java/de/monticore/types/prettyprint/TypeParametersPrettyPrinterTest.java @@ -57,21 +57,6 @@ public void testTypeParameter(String model) throws IOException { ); } - @ParameterizedTest - @ValueSource(strings = { - "String", - "Map>", - "A & B", - "A & B & C & D" - }) - public void testTypeBound(String model) throws IOException { - TypeParametersTestParser parser = TypeParametersTestMill.parser(); - testPrettyPrinter( - model, parser, parser::parse_StringTypeBounds, - ast -> TypeParametersTestMill.prettyPrint(ast, true) - ); - } - // this function could be used for all pretty printer tests if required, // however, the parameters are not great. protected void testPrettyPrinter( From 0656bd3a69803ff31fbfc2859436374bf7e13848 Mon Sep 17 00:00:00 2001 From: SE-FDr <88034434+SE-FDr@users.noreply.github.com> Date: Thu, 8 Aug 2024 10:25:41 +0200 Subject: [PATCH 23/24] TypeParameters.mc4 clarify symbol --- .../src/main/grammars/de/monticore/types/TypeParameters.mc4 | 1 + 1 file changed, 1 insertion(+) diff --git a/monticore-grammar/src/main/grammars/de/monticore/types/TypeParameters.mc4 b/monticore-grammar/src/main/grammars/de/monticore/types/TypeParameters.mc4 index 5ab3691971..e580c5d8a2 100644 --- a/monticore-grammar/src/main/grammars/de/monticore/types/TypeParameters.mc4 +++ b/monticore-grammar/src/main/grammars/de/monticore/types/TypeParameters.mc4 @@ -33,6 +33,7 @@ component grammar TypeParameters /** * ASTTypeParameter represents one type parameter. + * It itself is represented in the symbol table as a TypeVarSymbol. * Each type parameter has a name * and can have an optional list of upper type bounds. * Each bound list consists of an arbitrary positive number of types. From e7c65f8ca1e3844dc7469ab1de27d52e2b1ee784 Mon Sep 17 00:00:00 2001 From: SE-FDr <88034434+SE-FDr@users.noreply.github.com> Date: Tue, 1 Oct 2024 08:36:07 +0200 Subject: [PATCH 24/24] TypeParameters.mc4 is in Beta --- .../src/main/grammars/de/monticore/types/TypeParameters.mc4 | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/monticore-grammar/src/main/grammars/de/monticore/types/TypeParameters.mc4 b/monticore-grammar/src/main/grammars/de/monticore/types/TypeParameters.mc4 index e580c5d8a2..2ef5628a26 100644 --- a/monticore-grammar/src/main/grammars/de/monticore/types/TypeParameters.mc4 +++ b/monticore-grammar/src/main/grammars/de/monticore/types/TypeParameters.mc4 @@ -1,7 +1,7 @@ /* (c) https://github.com/MontiCore/monticore */ package de.monticore.types; -/* Alpha-version: This is intended to become a MontiCore stable grammar. */ +/* Beta-version: This is intended to become a MontiCore stable grammar. */ import de.monticore.symbols.BasicSymbols;