]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - bundles/org.simantics.scl.compiler/src/org/simantics/scl/compiler/elaboration/contexts/TranslationContext.java
Refactoring CHR handling code
[simantics/platform.git] / bundles / org.simantics.scl.compiler / src / org / simantics / scl / compiler / elaboration / contexts / TranslationContext.java
index 0331f9a50b86a7715405afc9bb37da2441d8b5b2..757aca8b5e272880e7c6526048f1b20837709f82 100644 (file)
@@ -4,7 +4,6 @@ import java.util.ArrayList;
 import java.util.Arrays;
 
 import org.simantics.scl.compiler.common.names.Name;
-import org.simantics.scl.compiler.common.names.Names;
 import org.simantics.scl.compiler.common.precedence.Associativity;
 import org.simantics.scl.compiler.common.precedence.Precedence;
 import org.simantics.scl.compiler.compilation.CompilationContext;
@@ -33,6 +32,8 @@ import org.simantics.scl.compiler.environment.Namespace;
 import org.simantics.scl.compiler.environment.filter.AcceptAllNamespaceFilter;
 import org.simantics.scl.compiler.errors.Locations;
 import org.simantics.scl.compiler.internal.parsing.declarations.DValueAst;
+import org.simantics.scl.compiler.module.debug.ModuleDebugInfo;
+import org.simantics.scl.compiler.module.debug.SymbolReference;
 import org.simantics.scl.compiler.top.SCLCompilerConfiguration;
 import org.simantics.scl.compiler.types.Type;
 
@@ -75,6 +76,12 @@ public class TranslationContext extends TypeTranslationContext implements Enviro
     TIntArrayList chrConstraintFrames = new TIntArrayList();
     ArrayList<CHRConstraintEntry> chrConstraintEntries = new ArrayList<CHRConstraintEntry>();
     
+    public CHRRuleset currentRuleset;
+    
+    public ModuleDebugInfo moduleDebugInfo;
+    
+    private String definitionName;
+    
     static class Entry {
         String name;
         Variable variable;
@@ -102,15 +109,17 @@ public class TranslationContext extends TypeTranslationContext implements Enviro
         }
     }
     
-    public TranslationContext(CompilationContext compilationContext, LocalEnvironment localEnvironment) {
+    public TranslationContext(CompilationContext compilationContext, LocalEnvironment localEnvironment, String definitionName) {
         super(compilationContext);
         this.localEnvironment = localEnvironment;
+        this.moduleDebugInfo = compilationContext.moduleDebugInfo;
+        this.definitionName = definitionName;
     }
     
     public static boolean isConstructorName(String name) {
         int p = name.lastIndexOf('.');
         char firstChar = name.charAt(p<0 ? 0 : p+1);
-        return Character.isUpperCase(firstChar);
+        return Character.isUpperCase(firstChar) || firstChar == '(';
     }
     
     /* Tries to resolve name as a local variable. It is assumed
@@ -157,13 +166,16 @@ public class TranslationContext extends TypeTranslationContext implements Enviro
             return existentialFrames.get(size-1);
     }
     
-    private Expression resolveFieldAccess(Expression base, int pos, String name) {
+    private Expression resolveFieldAccess(long location, Expression base, int pos, String name) {
         while(pos != -1) {
             int p = findSeparator(name, pos+1);
+            int endPos = p==-1 ? name.length() : p;
             FieldAccessor accessor = new IdAccessor(
                     name.charAt(pos),
-                    name.substring(pos+1, p==-1 ? name.length() : p-1));
+                    name.substring(pos+1, endPos));
+            accessor.location = Locations.sublocation(location, pos+1, endPos);
             base = new EFieldAccess(base, accessor);
+            base.location = Locations.sublocation(location, 0, endPos);
             pos = p;
         }
         return base;
@@ -188,6 +200,8 @@ public class TranslationContext extends TypeTranslationContext implements Enviro
             String deprecatedDescription = value.isDeprecated();
             if(deprecatedDescription != null)
                 errorLog.logWarning(location, "Deprecated value " + value.getName().name + "." + (deprecatedDescription.isEmpty() ? "" : " " + deprecatedDescription));
+            if(moduleDebugInfo != null)
+                moduleDebugInfo.symbolReferences.add(new SymbolReference(value.getName(), Name.create(compilationContext.module.getName(), definitionName), location));
             return new EConstant(location, value);
         } catch (AmbiguousNameException e) {
             if(SCLCompilerConfiguration.ALLOW_OVERLOADING)
@@ -214,6 +228,8 @@ public class TranslationContext extends TypeTranslationContext implements Enviro
                 public Expression realize() {
                     EConstant expression = new EConstant(altValue);
                     expression.location = location;
+                    if(moduleDebugInfo != null)
+                        moduleDebugInfo.symbolReferences.add(new SymbolReference(altValue.getName(), Name.create(compilationContext.module.getName(), definitionName), location));
                     return expression;
                 }
 
@@ -235,7 +251,7 @@ public class TranslationContext extends TypeTranslationContext implements Enviro
         if(begin == 0) {
             Expression result = resolveLocalVariable(location, part);
             if(result != null)
-                return end == -1 ? result : resolveFieldAccess(result, end, name);
+                return end == -1 ? result : resolveFieldAccess(location, result, end, name);
             
             // FIXME.. support for records
             if(localEnvironment != null) {
@@ -266,7 +282,7 @@ public class TranslationContext extends TypeTranslationContext implements Enviro
             if(result == null)
                 result = resolveValue(location, namespace, part);
             if(result != null)
-                return end == -1 ? result : resolveFieldAccess(result, end, name);
+                return end == -1 ? result : resolveFieldAccess(location, result, end, name);
         }
         reportResolveFailure(location, namespace, part);
         return new EError(location);
@@ -385,7 +401,7 @@ public class TranslationContext extends TypeTranslationContext implements Enviro
         chrConstraintFrames.add(chrConstraintEntries.size());
     }
     
-    public void popCHRConstraintFrame(ArrayList<CHRConstraint> constraints) {
+    public void popCHRConstraintFrame(CHRRuleset ruleset) {
         int frame = chrConstraintFrames.removeAt(chrConstraintFrames.size()-1);
         int i = chrConstraintEntries.size();
         while(i > frame) {
@@ -397,7 +413,7 @@ public class TranslationContext extends TypeTranslationContext implements Enviro
             else
                 newConstraint = chrConstraints.put(entry.name, entry.constraint);
             if(newConstraint.implicitlyDeclared)
-                constraints.add(newConstraint);
+                ruleset.addConstraint(newConstraint);
         }
     }
     
@@ -509,13 +525,6 @@ public class TranslationContext extends TypeTranslationContext implements Enviro
                 Locations.combine(definitions.get(0).location, definitions.get(definitions.size()-1).location),
                 cases);
     }
-    
-    public SCLValue getBindFunction() {
-        if(bindFunction == null) {
-            bindFunction = getEnvironment().getValue(Names.Prelude_bind);
-        }
-        return bindFunction;
-    }
 
     public SCLRelation resolveRelation(long location, String name) {
         SCLRelation relation = relations.get(name);