X-Git-Url: https://gerrit.simantics.org/r/gitweb?a=blobdiff_plain;f=bundles%2Forg.simantics.scl.compiler%2Fsrc%2Forg%2Fsimantics%2Fscl%2Fcompiler%2Felaboration%2Fcontexts%2FTypingContext.java;h=68468076d32fa155d81d3e7c82371d33162eff97;hb=HEAD;hp=de58a05cdb6c7838d02f4a108a77ac4127b98444;hpb=969bd23cab98a79ca9101af33334000879fb60c5;p=simantics%2Fplatform.git diff --git a/bundles/org.simantics.scl.compiler/src/org/simantics/scl/compiler/elaboration/contexts/TypingContext.java b/bundles/org.simantics.scl.compiler/src/org/simantics/scl/compiler/elaboration/contexts/TypingContext.java index de58a05cd..68468076d 100644 --- a/bundles/org.simantics.scl.compiler/src/org/simantics/scl/compiler/elaboration/contexts/TypingContext.java +++ b/bundles/org.simantics.scl.compiler/src/org/simantics/scl/compiler/elaboration/contexts/TypingContext.java @@ -1,16 +1,12 @@ package org.simantics.scl.compiler.elaboration.contexts; -import gnu.trove.map.hash.THashMap; -import gnu.trove.set.hash.THashSet; - import java.util.ArrayList; import org.simantics.scl.compiler.common.exceptions.InternalCompilerError; -import org.simantics.scl.compiler.common.names.Name; +import org.simantics.scl.compiler.compilation.CompilationContext; import org.simantics.scl.compiler.constants.NoRepConstant; +import org.simantics.scl.compiler.elaboration.expressions.EAmbiguous; import org.simantics.scl.compiler.elaboration.expressions.EApply; -import org.simantics.scl.compiler.elaboration.expressions.EConstant; -import org.simantics.scl.compiler.elaboration.expressions.EError; import org.simantics.scl.compiler.elaboration.expressions.ELiteral; import org.simantics.scl.compiler.elaboration.expressions.EPlaceholder; import org.simantics.scl.compiler.elaboration.expressions.EVariable; @@ -19,14 +15,14 @@ import org.simantics.scl.compiler.elaboration.expressions.Variable; import org.simantics.scl.compiler.elaboration.modules.SCLValue; import org.simantics.scl.compiler.environment.Environment; import org.simantics.scl.compiler.errors.ErrorLog; -import org.simantics.scl.compiler.errors.Locations; import org.simantics.scl.compiler.internal.elaboration.constraints.Constraint; import org.simantics.scl.compiler.internal.elaboration.constraints.ConstraintEnvironment; import org.simantics.scl.compiler.internal.elaboration.constraints.ConstraintSolver; import org.simantics.scl.compiler.internal.elaboration.constraints.ExpressionAugmentation; import org.simantics.scl.compiler.internal.elaboration.constraints.ReducedConstraints; -import org.simantics.scl.compiler.internal.elaboration.subsumption.SubSolver; import org.simantics.scl.compiler.internal.elaboration.subsumption.Subsumption; +import org.simantics.scl.compiler.internal.elaboration.subsumption2.SubSolver2; +import org.simantics.scl.compiler.types.Skeletons; import org.simantics.scl.compiler.types.TApply; import org.simantics.scl.compiler.types.TCon; import org.simantics.scl.compiler.types.TForAll; @@ -39,11 +35,15 @@ import org.simantics.scl.compiler.types.Type; import org.simantics.scl.compiler.types.Types; import org.simantics.scl.compiler.types.exceptions.UnificationException; import org.simantics.scl.compiler.types.kinds.Kinds; +import org.simantics.scl.compiler.types.util.Polarity; import org.simantics.scl.compiler.types.util.TypeUnparsingContext; -public class TypingContext implements EnvironmentalContext { +import gnu.trove.map.hash.THashMap; +import gnu.trove.set.hash.THashSet; + +public class TypingContext { - private ErrorLog errorLog; + private CompilationContext compilationContext; // Subsumption private ArrayList effectSubsumptions = new ArrayList(); @@ -64,14 +64,16 @@ public class TypingContext implements EnvironmentalContext { public THashSet recursiveValues; public ArrayList recursiveReferences; + // Overloading + public ArrayList overloadedExpressions = new ArrayList(); + //TypeUnparsingContext tuc = new TypeUnparsingContext(); Environment environment; - THashMap constants = new THashMap(); - public TypingContext(ErrorLog errorLog, Environment environment) { - this.errorLog = errorLog; - this.environment = environment; + public TypingContext(CompilationContext compilationContext) { + this.compilationContext = compilationContext; + this.environment = compilationContext.environment; } /** @@ -90,7 +92,7 @@ public class TypingContext implements EnvironmentalContext { try { Types.unify(a, Types.NO_EFFECTS); } catch(UnificationException e) { - errorLog.log(loc, "No side-effects allowed here."); + compilationContext.errorLog.log(loc, "No side-effects allowed here."); return; } } @@ -116,8 +118,10 @@ public class TypingContext implements EnvironmentalContext { if(a instanceof TMetaVar) { TMetaVar aVar = (TMetaVar)a; - if(b instanceof TMetaVar) + if(b instanceof TMetaVar) { + Skeletons.unifySkeletons(a, b); subsumptions.add(new Subsumption(loc, a, b)); + } else { if(b.contains(aVar)) throw new UnificationException(a, b); @@ -182,6 +186,11 @@ public class TypingContext implements EnvironmentalContext { else if(type instanceof TMetaVar) { TMetaVar var = (TMetaVar)type; TMetaVar newVar = Types.metaVar(var.getKind()); + try { + newVar.setSkeletonRef(var); + } catch (UnificationException e) { + throw new InternalCompilerError(loc, e); + } subsumptions.add(new Subsumption(loc, newVar, var)); return newVar; } @@ -232,6 +241,11 @@ public class TypingContext implements EnvironmentalContext { else if(type instanceof TMetaVar) { TMetaVar var = (TMetaVar)type; TMetaVar newVar = Types.metaVar(var.getKind()); + try { + newVar.setSkeletonRef(var); + } catch (UnificationException e) { + throw new InternalCompilerError(loc, e); + } subsumptions.add(new Subsumption(loc, var, newVar)); return newVar; } @@ -258,7 +272,7 @@ public class TypingContext implements EnvironmentalContext { * Instantiates type abstractions and constraints from the value. */ public Expression instantiate(Expression expr) { - Type type = Types.weakCanonical(expr.getType()); + Type type = Types.canonical(expr.getType()); while(type instanceof TForAll) { TForAll forAll = (TForAll)type; TVar var = forAll.var; @@ -297,7 +311,7 @@ public class TypingContext implements EnvironmentalContext { * type applications, lambdas and effect subsumptions. */ public Expression subsume(Expression expr, Type b) { - b = Types.weakCanonical(b); + b = Types.canonical(b); /*if(b instanceof TForAll) { TForAll forAll = (TForAll)b; TVar var = forAll.var; @@ -332,7 +346,7 @@ public class TypingContext implements EnvironmentalContext { try { subsume(sub.loc, a, b); } catch (UnificationException e) { - errorLog.log(sub.loc, "Type " + a + " is not a subtype of " + b + "."); + compilationContext.errorLog.log(sub.loc, "Type " + a + " is not a subtype of " + b + "."); } nontrivialSubs = true; } @@ -357,7 +371,7 @@ public class TypingContext implements EnvironmentalContext { try { Types.unify(sub.a, sub.b); } catch (UnificationException e) { - errorLog.log(sub.loc, "Unification of types failed."); + compilationContext.errorLog.log(sub.loc, "Unification of types failed."); return false; } @@ -372,7 +386,7 @@ public class TypingContext implements EnvironmentalContext { Types.unify(sub.a, sub.b); } catch (UnificationException e) { // Should not happen. Both types should be metavars. - throw new InternalCompilerError(); + throw new InternalCompilerError(e); } subsumptions.clear(); return true; @@ -382,7 +396,7 @@ public class TypingContext implements EnvironmentalContext { if(type instanceof TMetaVar) return ((TMetaVar)type).getKind() == Kinds.EFFECT; else if(type instanceof TCon) - return environment.getTypeConstructor((TCon)type).kind == Kinds.EFFECT; + return environment.getTypeDescriptor((TCon)type).getKind() == Kinds.EFFECT; else if(type instanceof TVar) return ((TVar)type).getKind() == Kinds.EFFECT; else if(type instanceof TUnion) @@ -393,8 +407,28 @@ public class TypingContext implements EnvironmentalContext { } public void solveSubsumptions(long globalLoc) { - if(expandSubsumptions()) - new SubSolver(errorLog, effectSubsumptions, potentialSingletonEffects, globalLoc).solve(); + if(expandSubsumptions()) { + if(!effectSubsumptions.isEmpty()) + SubSolver2.solve(compilationContext.errorLog, effectSubsumptions); + + if(!potentialSingletonEffects.isEmpty()) + try { + THashSet vars = new THashSet(4); + for(Type type : potentialSingletonEffects) + type.collectMetaVars(vars); + for(TMetaVar var : vars) { + if(var.getRef() == null) { + Polarity polarity = var.getPolarity(); + if(!polarity.isNegative()) + + var.setRef(Types.NO_EFFECTS); + } + } + } catch(UnificationException e) { + throw new InternalCompilerError(e); + } + //new SubSolver(compilationContext.errorLog, effectSubsumptions, potentialSingletonEffects, globalLoc).solve(); + } } public void declareEffect(long loc, Type effect) { @@ -411,7 +445,7 @@ public class TypingContext implements EnvironmentalContext { } public ErrorLog getErrorLog() { - return errorLog; + return compilationContext.errorLog; } public boolean isInPattern() { @@ -466,13 +500,13 @@ public class TypingContext implements EnvironmentalContext { public void typeError(long loc, Type requiredType, Type givenType) { TypeUnparsingContext tuc = new TypeUnparsingContext(); - errorLog.log(loc, "Expected <" + requiredType.toString(tuc) + "> got <" + givenType.toString(tuc) + ">."); + compilationContext.errorLog.log(loc, "Expected <" + requiredType.toString(tuc) + "> got <" + givenType.toString(tuc) + ">."); } public Expression solveConstraints(Environment environment, Expression expression) { ArrayList constraintDemand = getConstraintDemand(); if(!constraintDemand.isEmpty()) { - ConstraintEnvironment ce = new ConstraintEnvironment(environment); + ConstraintEnvironment ce = new ConstraintEnvironment(compilationContext); ReducedConstraints red = ConstraintSolver.solve( ce, new ArrayList(0), constraintDemand, true); @@ -482,33 +516,20 @@ public class TypingContext implements EnvironmentalContext { expression); for(Constraint c : red.unsolvedConstraints) - errorLog.log(c.getDemandLocation(), "There is no instance for <"+c.constraint+">."); + compilationContext.errorLog.log(c.getDemandLocation(), "There is no instance for <"+c.constraint+">."); } else expression = expression.decomposeMatching(); return expression; } - - public SCLValue getValue(Name name) { - if(constants.containsKey(name)) - return constants.get(name); - SCLValue value = environment.getValue(name); - if(value == null) - errorLog.log(Locations.NO_LOCATION, "Couldn't find " + name + "."); - constants.put(name, value); - return value; - } - - public Expression getConstant(Name name, Type ... typeParameters) { - SCLValue value = getValue(name); - if(value == null) - return new EError(Locations.NO_LOCATION); - return new EConstant(value, typeParameters); - } public Environment getEnvironment() { - return environment; + return compilationContext.environment; + } + + public CompilationContext getCompilationContext() { + return compilationContext; } }