Skip to content

Commit

Permalink
SymTypeInferenceVariable tmp
Browse files Browse the repository at this point in the history
  • Loading branch information
SE-FDr committed Dec 9, 2024
1 parent cb3a59d commit 708a9a7
Show file tree
Hide file tree
Showing 32 changed files with 744 additions and 624 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,17 @@
import de.monticore.ocl.setexpressions._ast.ASTSetEnumeration;
import de.monticore.ocl.setexpressions._visitor.SetExpressionsHandler;
import de.monticore.ocl.setexpressions._visitor.SetExpressionsTraverser;
import de.monticore.types.check.SymTypeInferenceVariable;
import de.monticore.types.check.SymTypeOfFunction;
import de.monticore.types.check.SymTypeVariable;
import de.monticore.types.mccollectiontypes.types3.util.MCCollectionSymTypeFactory;
import de.monticore.types3.generics.util.CompileTimeTypeCalculator;

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

import static de.monticore.types.check.SymTypeExpressionFactory.createBottomType;
import static de.monticore.types.check.SymTypeExpressionFactory.createFunction;
import static de.monticore.types.check.SymTypeExpressionFactory.createInferenceVariable;
import static de.monticore.types.check.SymTypeExpressionFactory.createObscureType;
import static de.monticore.types.check.SymTypeExpressionFactory.createTopType;
import static de.monticore.types.check.SymTypeExpressionFactory.createTypeVariable;

public class SetExpressionsCTTIVisitor extends SetExpressionsTypeVisitor
implements SetExpressionsHandler {
Expand Down Expand Up @@ -77,10 +75,7 @@ public void handle(ASTSetEnumeration expr) {

/** {@code <T> (T...) -> Set<T>} */
protected SymTypeOfFunction getSetEnumerationFunc() {
SymTypeVariable typeVar = createTypeVariable(
createBottomType(),
createTopType()
);
SymTypeInferenceVariable typeVar = createInferenceVariable();
SymTypeOfFunction setEnumerationFunc = createFunction(
MCCollectionSymTypeFactory.createSet(typeVar),
List.of(typeVar),
Expand All @@ -91,10 +86,7 @@ protected SymTypeOfFunction getSetEnumerationFunc() {

/** {@code <T> (T...) -> List<T>} */
protected SymTypeOfFunction getListEnumerationFunc() {
SymTypeVariable typeVar = createTypeVariable(
createBottomType(),
createTopType()
);
SymTypeInferenceVariable typeVar = createInferenceVariable();
SymTypeOfFunction listEnumerationFunc = createFunction(
MCCollectionSymTypeFactory.createList(typeVar),
List.of(typeVar),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,10 @@
import de.monticore.expressions.expressionsbasis._ast.ASTExpression;
import de.monticore.expressions.expressionsbasis._ast.ASTNameExpression;
import de.monticore.expressions.expressionsbasis._symboltable.IExpressionsBasisScope;
import de.monticore.expressions.javaclassexpressions._ast.ASTInstanceofPatternExpression;
import de.monticore.expressions.javaclassexpressions._ast.ASTTypePattern;
import de.monticore.symbols.basicsymbols.BasicSymbolsMill;
import de.monticore.symbols.basicsymbols._symboltable.*;
import de.monticore.symboltable.modifiers.AccessModifier;
import de.monticore.types.check.helpers.*;
import de.monticore.types.mcbasictypes._ast.ASTMCType;
import de.se_rwth.commons.SourcePosition;
import de.se_rwth.commons.logging.Log;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,15 +89,28 @@ public SymTypeOfGenerics asGenericType() {
}

/**
* Am I a type variable?
* Am I a bound type variable?
*/
public boolean isTypeVariable() {
return false;
}

public SymTypeVariable asTypeVariable() {
Log.error("0xFDAA2 internal error: "
+ "tried to convert non-type-variable to a type-variable");
+ "tried to convert non-bound-type-variable to a bound-type-variable");
return null;
}

/**
* Am I a free type variable?
*/
public boolean isInferenceVariable() {
return false;
}

public SymTypeInferenceVariable asInferenceVariable() {
Log.error("0xFDAAF internal error: "
+ "tried to convert non-inference-variable to an inference-variable");
return null;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,24 @@ public static SymTypeVariable createTypeVariable(String name, IBasicSymbolsScope
return createTypeVariable(typeSymbol);
}

public static SymTypeVariable createTypeVariable(TypeVarSymbol typeVarSymbol) {
return new SymTypeVariable(typeVarSymbol);
}

@Deprecated
public static SymTypeVariable createTypeVariable(TypeSymbol typeSymbol) {
return new SymTypeVariable(typeSymbol);
}

/**
* Used to create a (optionally bounded) FREE type variable,
* without bounds.
* These are created internally while inferring types.
*/
public static SymTypeInferenceVariable createInferenceVariable() {
return createInferenceVariable(createBottomType(), createTopType());
}

/**
* used to create a (optionally bounded) FREE type variable.
* These are created internally while inferring types.
Expand All @@ -54,50 +72,37 @@ public static SymTypeVariable createTypeVariable(String name, IBasicSymbolsScope
* e.g., {@code T extends Person & Iterable<Integer>}.
* For no bounds use {@link #createTopType()}.
*/
public static SymTypeVariable createTypeVariable(
public static SymTypeInferenceVariable createInferenceVariable(
SymTypeExpression lowerBound, SymTypeExpression upperBound) {
// free type variables need an identifier
return createTypeVariable(
getUniqueFreeTypeVarName(),
lowerBound, upperBound
);
return createInferenceVariable(lowerBound, upperBound, "FV");
}

/**
* Creates FREE type variable, BUT:
* You most likely do not want this method,
* this is (nearly) only used for deepCloning.
* Use {@link #createTypeVariable(SymTypeExpression, SymTypeExpression)}.
* You most likely do not want this method.
* Use {@link #createInferenceVariable(SymTypeExpression, SymTypeExpression)}.
*/
public static SymTypeVariable createTypeVariable(
String name,
public static SymTypeInferenceVariable createInferenceVariable(
SymTypeExpression lowerBound,
SymTypeExpression upperBound
SymTypeExpression upperBound,
String idStr
) {
return new SymTypeVariable(name, lowerBound, upperBound);
return new SymTypeInferenceVariable(idStr, lowerBound, upperBound);
}

public static SymTypeVariable createTypeVariable(TypeVarSymbol typeVarSymbol) {
// the SymTypeVariable extracts the upper bound from the type itself,
// as such we do not set it here
SymTypeExpression upperBound = createTopType();
// our Symbols have no notion of lower bound,
// as such we use the bottom type
SymTypeExpression lowerBound = createBottomType();
return createTypeVariable(typeVarSymbol, lowerBound, upperBound);
}

public static SymTypeVariable createTypeVariable(
TypeVarSymbol typeVarSymbol,
/**
* Only used for deepcloning:
* You most likely do not want this method.
* Use {@link #createInferenceVariable(SymTypeExpression, SymTypeExpression)}.
*/
public static SymTypeInferenceVariable createInferenceVariable(
SymTypeExpression lowerBound,
SymTypeExpression upperBound
SymTypeExpression upperBound,
String idStr,
int id
) {
return new SymTypeVariable(typeVarSymbol, lowerBound, upperBound);
}

@Deprecated
public static SymTypeVariable createTypeVariable(TypeSymbol typeSymbol) {
return new SymTypeVariable(typeSymbol);
return new SymTypeInferenceVariable(id, idStr, lowerBound, upperBound);
}

/**
Expand Down Expand Up @@ -536,17 +541,4 @@ public static SymTypeExpression createBottomType() {
return createUnion();
}

// Helper

/**
* The Only guarantee is that the names are unique.
* Never test against them.
*/
protected static String getUniqueFreeTypeVarName() {
// naming inspired by JDK
// s.a. https://git.rwth-aachen.de/monticore/monticore/-/issues/4296
return "FV#" + String.format("%05d", typeVarIDCounter++);
}
protected static int typeVarIDCounter = 0;

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,152 @@
/* (c) https://github.com/MontiCore/monticore */
package de.monticore.types.check;

import de.monticore.types3.ISymTypeVisitor;
import de.se_rwth.commons.logging.Log;

public class SymTypeInferenceVariable extends SymTypeExpression {

/**
* This holds a number of the free type variable,
* which acts as an identifier.
* The Only guarantee is that the ids are unique.
* Never test against them.
*/
protected int id;

/**
* a short string, e.g., "FV", that is output with the id while printing
*/
protected String idStr;

protected SymTypeExpression lowerBound;

protected SymTypeExpression upperBound;

public SymTypeInferenceVariable(
String idStr,
SymTypeExpression lowerBound,
SymTypeExpression upperBound
) {
this.id = getUniqueID();
this.idStr = Log.errorIfNull(idStr);
this.lowerBound = Log.errorIfNull(lowerBound);
this.upperBound = Log.errorIfNull(upperBound);
}

/**
* INTERNAL, DO NOT USE: used for deepcloning
*/
public SymTypeInferenceVariable(
int id,
String idStr,
SymTypeExpression lowerBound,
SymTypeExpression upperBound
) {
this.id = id;
this.idStr = Log.errorIfNull(idStr);
this.lowerBound = Log.errorIfNull(lowerBound);
this.upperBound = Log.errorIfNull(upperBound);
}

public SymTypeExpression getLowerBound() {
return lowerBound;
}

public SymTypeExpression getUpperBound() {
return upperBound;
}

/**
* accessible for printing, sorting, etc.
* usually, you do not need/want this.
*/
public int _internal_getID() {
return id;
}

/**
* accessible for printing
* usually, you do not need/want this.
*/
public String _internal_getIDStr() {
return idStr;
}

public boolean isPrimitive() {
/*
* Please note that the var itself is not a primitive type,
* but it might be instantiated into a primitive type
* unless we always assume boxed implementations
* then return false would be correct
* according to the W algorithm of Hindley-Milner,
* we regard a variable a monomorphic type on its own and
* do hence not regard it as primitive type
*/
return false;
}

@Override
public boolean isInferenceVariable() {
return true;
}

@Override
public SymTypeInferenceVariable asInferenceVariable() {
return this;
}

@Override
public SymTypeInferenceVariable deepClone() {
return super.deepClone().asInferenceVariable();
}

/**
* Similar to deepEquals, but only checks
* whether the variables are supposed to be the same variable.
* E.g., bounds are ignored
*/
public boolean denotesSameVar(SymTypeExpression other) {
if (!other.isInferenceVariable()) {
return false;
}
SymTypeInferenceVariable otherVar = other.asInferenceVariable();
return id == otherVar.id;
}

@Override
public boolean deepEquals(SymTypeExpression sym) {
if (sym == this) {
return true;
}
if (!sym.isInferenceVariable()) {
return false;
}
SymTypeInferenceVariable symVar = sym.asInferenceVariable();
if (!denotesSameVar(symVar)) {
return false;
}
if (!getUpperBound().deepEquals(symVar.getUpperBound())) {
return false;
}
else if (!getLowerBound().deepEquals(symVar.getLowerBound())) {
return false;
}
return true;
}

@Override
public void accept(ISymTypeVisitor visitor) {
visitor.visit(this);
}

// id management

protected static int getUniqueID() {
// naming inspired by JDK
// s.a. https://git.rwth-aachen.de/monticore/monticore/-/issues/4296
return typeInfIDCounter++;
}

protected static int typeInfIDCounter = 0;
}
Loading

0 comments on commit 708a9a7

Please sign in to comment.