]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - bundles/org.simantics.scl.compiler/src/org/simantics/scl/compiler/environment/Environments.java
Merged changes from feature/scl to master.
[simantics/platform.git] / bundles / org.simantics.scl.compiler / src / org / simantics / scl / compiler / environment / Environments.java
index 5a55a6ca130f81132d59a9ccf5dbe51884c9e346..4328d15e0b02535dfe695e06602702c19ea41218 100644 (file)
@@ -6,17 +6,16 @@ import java.util.List;
 import java.util.function.Consumer;
 
 import org.simantics.scl.compiler.common.names.Name;
+import org.simantics.scl.compiler.compilation.CompilationContext;
 import org.simantics.scl.compiler.elaboration.contexts.TypeTranslationContext;
 import org.simantics.scl.compiler.elaboration.modules.SCLValue;
-import org.simantics.scl.compiler.elaboration.modules.TypeAlias;
 import org.simantics.scl.compiler.elaboration.modules.TypeClass;
-import org.simantics.scl.compiler.elaboration.modules.TypeConstructor;
+import org.simantics.scl.compiler.elaboration.modules.TypeDescriptor;
 import org.simantics.scl.compiler.elaboration.relations.SCLEntityType;
 import org.simantics.scl.compiler.elaboration.relations.SCLRelation;
 import org.simantics.scl.compiler.elaboration.rules.MappingRelation;
 import org.simantics.scl.compiler.elaboration.rules.TransformationRule;
 import org.simantics.scl.compiler.environment.filter.AcceptAllNamespaceFilter;
-import org.simantics.scl.compiler.errors.ErrorLog;
 import org.simantics.scl.compiler.internal.codegen.effects.EffectConstructor;
 import org.simantics.scl.compiler.internal.parsing.exceptions.SCLSyntaxErrorException;
 import org.simantics.scl.compiler.internal.parsing.parser.SCLParserImpl;
@@ -80,8 +79,8 @@ public class Environments {
      * @return  A TypeConstructor instance, or null if not found.
      * @throws AmbiguousNameException  if the same name is found in multiple imported modules.
      */
-    public static TypeConstructor getTypeConstructor(Environment environment, String localName) throws AmbiguousNameException {
-       return getEnvironmentEntry(environment, localName, getTypeConstructor);
+    public static TypeDescriptor getTypeDescriptor(Environment environment, String localName) throws AmbiguousNameException {
+       return getEnvironmentEntry(environment, localName, getTypeDescriptor);
     }
     
     /**
@@ -107,18 +106,6 @@ public class Environments {
     public static TypeClass getTypeClass(Environment environment, String localName) throws AmbiguousNameException {
        return getEnvironmentEntry(environment, localName, getTypeClass);
     }
-    
-    /**
-     * Get the TypeAlias object representing a type alias defined in a given environment.
-     * The name can be a local name or a fully scoped name with modules separated by periods. 
-     * @param environment  the environment
-     * @param localName  the name to be searched for
-     * @return  A TypeAlias instance, or null if not found.
-     * @throws AmbiguousNameException  if the same name is found in multiple imported modules.
-     */
-    public static TypeAlias getTypeAlias(Environment environment, String localName) throws AmbiguousNameException {
-       return getEnvironmentEntry(environment, localName, getTypeAlias);
-    }
 
     /**
      * Get the Name object representing an SCL value defined in a given environment.
@@ -144,12 +131,12 @@ public class Environments {
      * @return  A TCon instance, or null if not found.
      * @throws AmbiguousNameException  if the same name is used in multiple imported modules.
      */
-    public static TCon getTypeConstructorName(Environment environment, String localName) throws AmbiguousNameException {
-        TypeConstructor typeConstructor = getTypeConstructor(environment, localName);
-        if(typeConstructor == null)
+    public static TCon getTypeDescriptorName(Environment environment, String localName) throws AmbiguousNameException {
+        TypeDescriptor typeDescriptor = getTypeDescriptor(environment, localName);
+        if(typeDescriptor == null)
             return null;
         else
-            return typeConstructor.name;
+            return typeDescriptor.name;
     }
     
     /**
@@ -193,19 +180,20 @@ public class Environments {
      */
     public static Type getType(Environment environment, String typeText) throws SCLExpressionCompilationException {
         SCLParserImpl parser = new SCLParserImpl(new StringReader(typeText));
-        ErrorLog errorLog = new ErrorLog();
+        CompilationContext compilationContext = new CompilationContext();
+        compilationContext.environment = environment;
         try {
             TypeAst typeAst = (TypeAst)parser.parseType();
-            TypeTranslationContext context = new TypeTranslationContext(errorLog, environment);
+            TypeTranslationContext context = new TypeTranslationContext(compilationContext);
             Type type = context.toType(typeAst);
-            if(errorLog.isEmpty())
+            if(compilationContext.errorLog.isEmpty())
                 return type;
         } catch(SCLSyntaxErrorException e) {
-            errorLog.log(e.location, e.getMessage());
+            compilationContext.errorLog.log(e.location, e.getMessage());
         } catch(Exception e) {
-            errorLog.log(e);
+            compilationContext.errorLog.log(e);
         }
-        throw new SCLExpressionCompilationException(errorLog.getErrors());
+        throw new SCLExpressionCompilationException(compilationContext.errorLog.getErrors());
     }
 
     /**
@@ -324,10 +312,10 @@ public class Environments {
                }               
        };
     
-    private static final NamespaceValueAccessor<TypeConstructor> getTypeConstructor = new NamespaceValueAccessor<TypeConstructor>() {
+    private static final NamespaceValueAccessor<TypeDescriptor> getTypeDescriptor = new NamespaceValueAccessor<TypeDescriptor>() {
                @Override
-               public TypeConstructor get(Namespace ns, String name) throws AmbiguousNameException {
-                       return ns.getTypeConstructor(name);
+               public TypeDescriptor get(Namespace ns, String name) throws AmbiguousNameException {
+                       return ns.getTypeDescriptor(name);
                }               
        };
     
@@ -345,13 +333,6 @@ public class Environments {
                }               
        };
     
-    private static final NamespaceValueAccessor<TypeAlias> getTypeAlias = new NamespaceValueAccessor<TypeAlias>() {
-               @Override
-               public TypeAlias get(Namespace ns, String name) throws AmbiguousNameException {
-                       return ns.getTypeAlias(name);
-               }               
-       };
-    
        private static <T> T getEnvironmentEntry(Environment environment, String localName, NamespaceValueAccessor<T> accessor) throws AmbiguousNameException {
         Namespace namespace = environment.getLocalNamespace();
         int curPos = 0;
@@ -359,9 +340,10 @@ public class Environments {
             int pos = localName.indexOf('.', curPos);
             if(pos < 0)
                 return accessor.get(namespace, localName.substring(curPos));
-            namespace = namespace.getNamespace(localName.substring(curPos, pos));
-            if(namespace == null)
-                return null;
+            Namespace newNamespace = namespace.getNamespace(localName.substring(curPos, pos));
+            if(newNamespace == null)
+                return accessor.get(namespace, localName.substring(curPos));
+            namespace = newNamespace;
             curPos = pos + 1;
         }
     }