]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - bundles/org.simantics.scl.compiler/src/org/simantics/scl/compiler/internal/elaboration/transformations/UnifiableFactory.java
Merged changes from feature/scl to master.
[simantics/platform.git] / bundles / org.simantics.scl.compiler / src / org / simantics / scl / compiler / internal / elaboration / transformations / UnifiableFactory.java
index 7b3face61c962d3b89dbe54c4c0361914762d311..75b083f0c9fbadc3a138e77b0b8a7a65b613bed4 100644 (file)
@@ -3,7 +3,7 @@ package org.simantics.scl.compiler.internal.elaboration.transformations;
 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;
@@ -34,26 +34,6 @@ import gnu.trove.map.hash.THashMap;
 import gnu.trove.set.hash.THashSet;
 
 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");
-
-    private static final Type XML_ELEMENT = Types.con("Data/XML", "Element");
-    
     private final TypingContext context;    
     /**
      * The factory generates here the statements initializing the variables needed in unification.
@@ -93,7 +73,7 @@ public class UnifiableFactory {
         }
         @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);
         }
     }
@@ -116,7 +96,7 @@ public class UnifiableFactory {
                         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));
         }
     }
@@ -148,7 +128,7 @@ public class UnifiableFactory {
             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) {
@@ -189,7 +169,7 @@ public class UnifiableFactory {
                 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));
             }
@@ -276,14 +256,14 @@ public class UnifiableFactory {
             
             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(contextunsafeCoerce, mfun.returnType, tupleType);
+                destructor = Expressions.constant(context.getCompilationContext(), Names.JavaBuiltin_unsafeCoerce, mfun.returnType, tupleType);
             }
             else {
                 Variable[] parameters = new Variable[arity];
@@ -292,7 +272,7 @@ public class UnifiableFactory {
                 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);
@@ -312,9 +292,9 @@ public class UnifiableFactory {
                 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);
         }
@@ -322,7 +302,7 @@ public class UnifiableFactory {
     }
     
     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);
     }
     
@@ -347,13 +327,13 @@ public class UnifiableFactory {
         //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) == '(') { // (), (,), (,,),...
@@ -369,7 +349,7 @@ public class UnifiableFactory {
                 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 + ".")));
     }
         
@@ -378,8 +358,8 @@ public class UnifiableFactory {
     }
     
     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),
@@ -390,8 +370,8 @@ public class UnifiableFactory {
     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,
@@ -400,8 +380,8 @@ public class UnifiableFactory {
     }
 
     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);