package org.simantics.scl.compiler.internal.elaboration.transformations;
-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.common.names.Names;
import org.simantics.scl.compiler.constants.Constant;
import org.simantics.scl.compiler.constants.StringConstant;
import org.simantics.scl.compiler.elaboration.contexts.TypingContext;
import org.simantics.scl.compiler.elaboration.modules.SCLValue;
import org.simantics.scl.compiler.errors.Locations;
import org.simantics.scl.compiler.internal.codegen.references.IVal;
-import org.simantics.scl.compiler.internal.types.TypeHashCodeContext;
+import org.simantics.scl.compiler.internal.types.HashCodeUtils;
import org.simantics.scl.compiler.types.TCon;
import org.simantics.scl.compiler.types.Type;
import org.simantics.scl.compiler.types.Types;
import org.simantics.scl.compiler.types.util.MultiApply;
import org.simantics.scl.compiler.types.util.MultiFunction;
-public class UnifiableFactory {
- private static final TCon Unifiable = Types.con("Unification", "Unifiable");
- private static final Name uVar = Name.create("Unification", "uVar");
- private static final Name uCons = Name.create("Unification", "uCons");
- private static final Name uId = Name.create("Unification", "uId");
- private static final Name uPending = Name.create("Unification", "uPending");
- private static final TCon UTag = Types.con("Unification", "UTag");
- private static final Name uTag = Name.create("Unification", "uTag");
- private static final Name extractWithDefault = Name.create("Unification", "extractWithDefault");
-
- private static final Name putUMap = Name.create("Unification", "putUMap");
- private static final Name putUMapC = Name.create("Unification", "putUMapC");
- private static final Name getUMapWithDefault = Name.create("Unification", "getUMapWithDefault");
-
- private static final Name fail = Name.create("Builtin", "fail");
- private static final Name unsafeCoerce = Name.create("JavaBuiltin", "unsafeCoerce");
- private static final Name newResource = Name.create("Simantics/DB", "newResource");
- private static final Name createElement = Name.create("Data/XML", "createElement");
+import gnu.trove.map.hash.THashMap;
+import gnu.trove.set.hash.THashSet;
- private static final Type XML_ELEMENT = Types.con("Data/XML", "Element");
-
+public class UnifiableFactory {
private final TypingContext context;
/**
* The factory generates here the statements initializing the variables needed in unification.
}
@Override
public Expression toExpression() {
- return Expressions.apply(context, Types.NO_EFFECTS, uId,
+ return Expressions.apply(context.getCompilationContext(), Types.NO_EFFECTS, Names.Unifiable_uId,
constant.getType(), constant);
}
}
variable,
extract(variable.getType(), Expressions.var(uniVariableMap.get(variable))),
expression);
- return Expressions.apply(context, Types.NO_EFFECTS, uPending,
+ return Expressions.apply(context.getCompilationContext(), Types.NO_EFFECTS, Names.Unifiable_uPending,
value.getType(), Expressions.computation(Types.PROC, expression));
}
}
if(uniVariable != null)
return new UniRep(new EVariable(uniVariable));
else
- return new UniRep(Expressions.apply(context, Types.PROC, uVar, variable.getType(), Expressions.punit()));
+ return new UniRep(Expressions.apply(context.getCompilationContext(), Types.PROC, Names.Unifiable_uVar, variable.getType(), Expressions.punit()));
}
apply:
if(expression instanceof EApply) {
for(int i=0;i<arity;++i)
tupleParameters[i] = uniParameters[i].toExpression();
Expression tuple = Expressions.tuple(tupleParameters);
- return new UniRep(Expressions.apply(context, Types.NO_EFFECTS, uCons,
+ return new UniRep(Expressions.apply(context.getCompilationContext(), Types.NO_EFFECTS, Names.Unifiable_uCons,
expression.getType(), tuple.getType(),
getTag(function), tuple));
}
@Override
public int hashCode() {
if(hashCode == 0) {
- TypeHashCodeContext hcContext = new TypeHashCodeContext();
- hcContext.append(function.hashCode());
+ int hash = HashCodeUtils.SEED;
+ hash = HashCodeUtils.update(hash, function.hashCode());
for(Type typeParameter : typeParameters)
- typeParameter.updateHashCode(hcContext);
- hashCode = hcContext.getResult();
+ hash = typeParameter.hashCode(hash);
+ hashCode = hash;
}
return hashCode;
}
Type[] uniParameterTypes = new Type[arity];
for(int i=0;i<arity;++i)
- uniParameterTypes[i] = Types.apply(Unifiable, mfun.parameterTypes[i]);
+ uniParameterTypes[i] = Types.apply(Names.Unifiable_Unifiable, mfun.parameterTypes[i]);
Type tupleType = Types.tuple(uniParameterTypes);
// Destructor
Expression destructor;
if(sclValue.getName().module.equals("Builtin") && sclValue.getName().name.charAt(0)=='(') {
// Tuple constructor is a special case, where we can just cast the value
- destructor = Expressions.constant(context, unsafeCoerce, mfun.returnType, tupleType);
+ destructor = Expressions.constant(context.getCompilationContext(), Names.JavaBuiltin_unsafeCoerce, mfun.returnType, tupleType);
}
else {
Variable[] parameters = new Variable[arity];
Expression pattern = new EApply(constructorExpr.copy(context), Expressions.vars(parameters));
Expression[] tupleParameters = new Expression[arity];
for(int i=0;i<arity;++i)
- tupleParameters[i] = Expressions.apply(context, Types.NO_EFFECTS, uId,
+ tupleParameters[i] = Expressions.apply(context.getCompilationContext(), Types.NO_EFFECTS, Names.Unifiable_uId,
parameters[i].getType(), Expressions.var(parameters[i]));
Expression value = Expressions.tuple(tupleParameters);
destructor = new ELambda(Locations.NO_LOCATION, pattern, value);
constructor = new ELambda(Locations.NO_LOCATION, pattern, value);
}
- tag = new Variable("tag", Types.apply(UTag, mfun.returnType, tupleType));
+ tag = new Variable("tag", Types.apply(Names.Unifiable_UTag, mfun.returnType, tupleType));
mappingStatements.add(new LetStatement(new EVariable(tag),
- Expressions.apply(context, Types.NO_EFFECTS, uTag, tupleType, mfun.returnType,
+ Expressions.apply(context.getCompilationContext(), Types.NO_EFFECTS, Names.Unifiable_uTag, tupleType, mfun.returnType,
Expressions.integer(constructorTag), constructor, destructor)));
constructorTags.put(key, tag);
}
}
private Expression extract(Type type, Expression uni) {
- return Expressions.apply(context, Types.PROC, extractWithDefault,
+ return Expressions.apply(context.getCompilationContext(), Types.PROC, Names.Unifiable_extractWithDefault,
type, getDefaultGenerator(type), uni);
}
//System.out.println("createGenerationExpression(" + type.toString(tuc) + ")");
if(apply.constructor instanceof TCon) {
if(apply.constructor.equals(Types.RESOURCE))
- return Expressions.apply(context, Types.PROC, newResource, Expressions.tuple());
+ return Expressions.apply(context.getCompilationContext(), Types.PROC, Names.Simantics_DB_newResource, Expressions.tuple());
if(apply.constructor.equals(Types.STRING))
return new ELiteral(new StringConstant("")); // FIXME
- if(apply.constructor.equals(XML_ELEMENT))
- return Expressions.apply(context, Types.PROC, createElement, Expressions.string("NO-NAME"));
+ if(apply.constructor.equals(Names.Data_XML_Element))
+ return Expressions.apply(context.getCompilationContext(), Types.PROC, Names.Data_XML_createElement, Expressions.string("NO-NAME"));
TCon con = (TCon)apply.constructor;
if(con.name.charAt(0) == '(') { // (), (,), (,,),...
return Expressions.tuple(parameters);
}
}
- return Expressions.apply(context, Types.NO_EFFECTS, fail,
+ return Expressions.apply(context.getCompilationContext(), Types.NO_EFFECTS, Names.Builtin_fail,
new ELiteral(new StringConstant("Cannot generated default instance for type " + type + ".")));
}
}
public Expression getFromUMap(Expression umap, Expression key, Type valueType) {
- return Expressions.apply(context, Types.PROC,
- getUMapWithDefault,
+ return Expressions.apply(context.getCompilationContext(), Types.PROC,
+ Names.Unifiable_getUMapWithDefault,
valueType,
key.getType(),
getDefaultGenerator(valueType),
public Expression putToUMapUnifiable(
THashSet<Variable> variableSet, THashMap<Variable, Variable> uniVariableMap,
Expression umap, Expression key, Expression value) {
- return Expressions.apply(context, Types.PROC,
- putUMap,
+ return Expressions.apply(context.getCompilationContext(), Types.PROC,
+ Names.Unifiable_putUMap,
key.getType(),
value.getType(),
umap,
}
public Expression putToUMapConstant(Variable umap, Expression key, Expression value) {
- return Expressions.apply(context, Types.PROC,
- putUMapC,
+ return Expressions.apply(context.getCompilationContext(), Types.PROC,
+ Names.Unifiable_putUMapC,
key.getType(), value.getType(),
Expressions.var(umap),
key, value);