-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'TypeParameters-after-LQT' into 'dev'
Type parameters See merge request monticore/monticore!1034
- Loading branch information
Showing
19 changed files
with
626 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
50 changes: 50 additions & 0 deletions
50
monticore-grammar/src/main/grammars/de/monticore/types/TypeParameters.mc4
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
/* (c) https://github.com/MontiCore/monticore */ | ||
package de.monticore.types; | ||
|
||
/* Beta-version: This is intended to become a MontiCore stable grammar. */ | ||
|
||
import de.monticore.symbols.BasicSymbols; | ||
|
||
/** | ||
* This grammar introduces type parameters such as T, U extends Set<int>, | ||
* and type parameter lists such as <T, S extends T> | ||
* 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: | ||
* <T> | ||
* <T, U extends T & Comparable<U>> | ||
*/ | ||
TypeParameters = | ||
"<" (TypeParameter || ",")+ ">" | ||
; | ||
|
||
/** | ||
* 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. | ||
* Each of those types is a supertype of the corresponding type parameter. | ||
* Example: | ||
* T | ||
* U extends T & Comparable<U> | ||
*/ | ||
TypeParameter implements TypeVar = | ||
Name | ||
("extends" (MCType || "&")+)? | ||
; | ||
|
||
} |
29 changes: 29 additions & 0 deletions
29
monticore-grammar/src/main/grammars/de/monticore/types/TypeParameters.mlc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
package de.monticore.types; | ||
|
||
mlc TypeParameters { | ||
|
||
//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 { | ||
include "$projectDir/src/main/java/de/monticore/types3/*.java"; | ||
} | ||
|
||
promote { | ||
mlc "de.monticore.symbols.BasicSymbols"; | ||
mlc "de.monticore.types.MCBasicTypes"; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
33 changes: 33 additions & 0 deletions
33
...in/java/de/monticore/types/typeparameters/_symboltable/TypeParametersSTCompleteTypes.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
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. | ||
*/ | ||
public class TypeParametersSTCompleteTypes implements TypeParametersVisitor2 { | ||
|
||
ITypeCalculator tc; | ||
|
||
public TypeParametersSTCompleteTypes(ITypeCalculator tc) { | ||
this.tc = tc; | ||
} | ||
|
||
@Override | ||
public void visit(ASTTypeParameter node) { | ||
List<SymTypeExpression> bounds = new ArrayList<>(); | ||
for (ASTMCType astTypeBound : node.getMCTypeList()) { | ||
bounds.add(tc.symTypeFromAST(astTypeBound)); | ||
} | ||
// error logged if obscure | ||
node.getSymbol().setSuperTypesList(bounds); | ||
} | ||
|
||
} |
59 changes: 59 additions & 0 deletions
59
...c/main/java/de/monticore/types/typeparameters/cocos/TypeParameterNoCyclicInheritance.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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., <T extends U, U extends Person & T> | ||
*/ | ||
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<SymTypeExpression> currentInheritanceList, | ||
ASTTypeParameter node | ||
) { | ||
List<SymTypeExpression> 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<SymTypeExpression> nextInheritanceList = new ArrayList<>(); | ||
nextInheritanceList.addAll(currentInheritanceList); | ||
nextInheritanceList.add(superType); | ||
if (!checkForCircularInheritance(nextInheritanceList, node)) { | ||
return false; | ||
} | ||
} | ||
} | ||
return true; | ||
} | ||
|
||
} |
46 changes: 46 additions & 0 deletions
46
.../src/main/java/de/monticore/types/typeparameters/cocos/TypeParametersHaveUniqueNames.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<String> names = node.getTypeParameterList().stream() | ||
.map(ASTTypeParameter::getSymbol) | ||
.map(TypeSymbolTOP::getName) | ||
.collect(Collectors.toList()); | ||
Set<String> duplicates = findDuplicates(names); | ||
for (String dupName : duplicates) { | ||
Log.error("0xFDC14 The same name \"" + dupName | ||
+ "\" has been used for multiple type parameters." | ||
); | ||
} | ||
} | ||
|
||
// Helper | ||
|
||
protected Set<String> findDuplicates(List<String> listContainingDuplicates) { | ||
final Set<String> setToReturn = new HashSet<>(); | ||
final Set<String> set1 = new HashSet<>(); | ||
|
||
for (String modifierName : listContainingDuplicates) { | ||
if (!set1.add(modifierName)) { | ||
setToReturn.add(modifierName); | ||
} | ||
} | ||
return setToReturn; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
10 changes: 10 additions & 0 deletions
10
monticore-grammar/src/test/grammars/de/monticore/types/TypeParametersTest.mc4
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 { | ||
} |
9 changes: 9 additions & 0 deletions
9
...ammar/src/test/grammars/de/monticore/types/TypeParametersWithoutIntersectionTypesTest.mc4
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 { | ||
} |
Oops, something went wrong.