1 package org.simantics.scl.compiler.elaboration.contexts;
3 import java.util.ArrayList;
4 import java.util.Arrays;
6 import org.simantics.scl.compiler.common.names.Name;
7 import org.simantics.scl.compiler.common.names.Names;
8 import org.simantics.scl.compiler.common.precedence.Associativity;
9 import org.simantics.scl.compiler.common.precedence.Precedence;
10 import org.simantics.scl.compiler.compilation.CompilationContext;
11 import org.simantics.scl.compiler.elaboration.chr.CHRRuleset;
12 import org.simantics.scl.compiler.elaboration.chr.relations.CHRConstraint;
13 import org.simantics.scl.compiler.elaboration.expressions.Case;
14 import org.simantics.scl.compiler.elaboration.expressions.EAmbiguous;
15 import org.simantics.scl.compiler.elaboration.expressions.EConstant;
16 import org.simantics.scl.compiler.elaboration.expressions.EError;
17 import org.simantics.scl.compiler.elaboration.expressions.EFieldAccess;
18 import org.simantics.scl.compiler.elaboration.expressions.ELambda;
19 import org.simantics.scl.compiler.elaboration.expressions.EVar;
20 import org.simantics.scl.compiler.elaboration.expressions.EVariable;
21 import org.simantics.scl.compiler.elaboration.expressions.Expression;
22 import org.simantics.scl.compiler.elaboration.expressions.Variable;
23 import org.simantics.scl.compiler.elaboration.expressions.accessor.FieldAccessor;
24 import org.simantics.scl.compiler.elaboration.expressions.accessor.IdAccessor;
25 import org.simantics.scl.compiler.elaboration.expressions.block.LetStatement;
26 import org.simantics.scl.compiler.elaboration.modules.SCLValue;
27 import org.simantics.scl.compiler.elaboration.query.pre.PreQuery;
28 import org.simantics.scl.compiler.elaboration.relations.SCLRelation;
29 import org.simantics.scl.compiler.environment.AmbiguousNameException;
30 import org.simantics.scl.compiler.environment.Environments;
31 import org.simantics.scl.compiler.environment.LocalEnvironment;
32 import org.simantics.scl.compiler.environment.Namespace;
33 import org.simantics.scl.compiler.environment.filter.AcceptAllNamespaceFilter;
34 import org.simantics.scl.compiler.errors.Locations;
35 import org.simantics.scl.compiler.internal.parsing.declarations.DValueAst;
36 import org.simantics.scl.compiler.top.SCLCompilerConfiguration;
37 import org.simantics.scl.compiler.types.Type;
39 import gnu.trove.list.array.TIntArrayList;
40 import gnu.trove.map.hash.THashMap;
41 import gnu.trove.procedure.TObjectProcedure;
42 import gnu.trove.set.hash.THashSet;
44 public class TranslationContext extends TypeTranslationContext implements EnvironmentalContext {
46 THashMap<String, Variable> variables = new THashMap<String, Variable>();
47 ArrayList<Entry> variableEntries = new ArrayList<Entry>();
48 LocalEnvironment localEnvironment;
49 TIntArrayList frames = new TIntArrayList();
50 ArrayList<THashSet<String>> frameNameSets = new ArrayList<THashSet<String>>();
51 ArrayList<THashSet<String>> existentialFrames = new ArrayList<THashSet<String>>();
52 ArrayList<ArrayList<Variable>> blanksInExistentialFrame = new ArrayList<ArrayList<Variable>>();
53 SCLValue bindFunction;
55 public PreQuery currentPreQuery;
57 THashMap<String, SCLRelation> relations = new THashMap<String, SCLRelation>();
58 TIntArrayList relationFrames = new TIntArrayList();
59 ArrayList<RelationEntry> relationEntries = new ArrayList<RelationEntry>();
61 THashMap<String, CHRConstraint> chrConstraints = new THashMap<String, CHRConstraint>();
62 TIntArrayList chrConstraintFrames = new TIntArrayList();
63 ArrayList<CHRConstraintEntry> chrConstraintEntries = new ArrayList<CHRConstraintEntry>();
68 public Entry(String name, Variable variable) {
70 this.variable = variable;
74 static class RelationEntry {
77 public RelationEntry(String name, SCLRelation relation) {
79 this.relation = relation;
83 static class CHRConstraintEntry {
85 CHRConstraint constraint;
86 public CHRConstraintEntry(String name, CHRConstraint constraint) {
88 this.constraint = constraint;
92 public TranslationContext(CompilationContext compilationContext, LocalEnvironment localEnvironment) {
93 super(compilationContext);
94 this.localEnvironment = localEnvironment;
97 public static boolean isConstructorName(String name) {
98 int p = name.lastIndexOf('.');
99 char firstChar = name.charAt(p<0 ? 0 : p+1);
100 return Character.isUpperCase(firstChar);
103 /* Tries to resolve name as a local variable. It is assumed
104 * that name does not contain '.'.
106 private Expression resolveLocalVariable(long location, String name) {
107 Variable variable = variables.get(name);
109 return new EVariable(location, variable);
111 char c = name.charAt(0);
114 if(existentialFrames.isEmpty()) {
115 errorLog.log(location, "Existential variables can be used only in queries.");
116 return new EError(location);
118 variable = new Variable(name);
119 variables.put(name, variable);
120 existentialFrames.get(existentialFrames.size()-1).add(name);
121 return new EVariable(variable);
123 if(name.length()==1) {
124 variable = new Variable("_");
125 if(blanksInExistentialFrame.isEmpty()) {
126 errorLog.log(location, "Cannot use blank variables in this context.");
127 return new EError(location);
129 blanksInExistentialFrame.get(blanksInExistentialFrame.size()-1).add(variable);
130 return new EVariable(variable);
137 private Expression resolveFieldAccess(Expression base, int pos, String name) {
139 int p = findSeparator(name, pos+1);
140 FieldAccessor accessor = new IdAccessor(
142 name.substring(pos+1, p==-1 ? name.length() : p-1));
143 base = new EFieldAccess(base, accessor);
149 private static int findSeparator(String name, int fromIndex) {
150 ++fromIndex; // the first character (that can be #) is never a separator
151 while(fromIndex < name.length()) {
152 char c = name.charAt(fromIndex);
153 if(c == '.' || c == '#')
160 public Expression resolveValue(long location, Namespace namespace, String name) {
162 SCLValue value = namespace.getValue(name);
165 String deprecatedDescription = value.isDeprecated();
166 if(deprecatedDescription != null)
167 errorLog.logWarning(location, "Deprecated value " + value.getName().name + "." + (deprecatedDescription.isEmpty() ? "" : " " + deprecatedDescription));
168 return new EConstant(location, value);
169 } catch (AmbiguousNameException e) {
170 if(SCLCompilerConfiguration.ALLOW_OVERLOADING)
171 return resolveAmbigious(location, e.conflictingModules, name);
173 errorLog.log(location, e.getMessage());
174 return new EError(location);
179 private Expression resolveAmbigious(long location, String[] conflictingModules, String name) {
180 EAmbiguous.Alternative[] alternatives = new EAmbiguous.Alternative[conflictingModules.length];
181 for(int i=0;i<conflictingModules.length;++i) {
182 Name altName = Name.create(conflictingModules[i], name);
183 SCLValue altValue = environment.getValue(altName);
184 alternatives[i] = new EAmbiguous.Alternative() {
186 public Type getType() {
187 return altValue.getType();
191 public Expression realize() {
192 EConstant expression = new EConstant(altValue);
193 expression.location = location;
198 public String toString() {
199 return altValue.getName().toString().replace('/', '.');
203 EAmbiguous expression = new EAmbiguous(alternatives);
204 expression.location = location;
208 public Expression resolveVariable(long location, Namespace namespace, String name, int begin) {
209 int end = findSeparator(name, begin);
210 String part = end == -1 ? (begin == 0 ? name : name.substring(begin)) : name.substring(begin, end);
213 Expression result = resolveLocalVariable(location, part);
215 return end == -1 ? result : resolveFieldAccess(result, end, name);
217 // FIXME.. support for records
218 if(localEnvironment != null) {
219 result = localEnvironment.resolve(environment, name);
221 result.setLocationDeep(location);
226 if(end != -1 && name.charAt(end) == '.') {
227 Namespace childNamespace = namespace.getNamespace(part);
228 if(childNamespace != null)
229 return resolveVariable(location, childNamespace, name, end+1);
232 Expression result = null;
234 for(int end2 = name.length();end2 > end;end2 = name.lastIndexOf('.', end2-1)) {
235 part = name.substring(begin, end2);
236 result = resolveValue(location, namespace, part);
238 end = end2 == name.length() ? -1 : end2;
244 result = resolveValue(location, namespace, part);
246 return end == -1 ? result : resolveFieldAccess(result, end, name);
248 reportResolveFailure(location, namespace, part);
249 return new EError(location);
252 private void reportResolveFailure(long location, Namespace namespace, String name) {
253 StringBuilder message = new StringBuilder();
254 message.append("Couldn't resolve ").append(name).append(".");
256 final THashSet<String> candidateNames = new THashSet<String>(4);
257 namespace.findValuesForPrefix("", AcceptAllNamespaceFilter.INSTANCE,
258 new TObjectProcedure<SCLValue>() {
260 public boolean execute(SCLValue value) {
262 new Exception().printStackTrace();
265 String valueName = value.getName().name;
266 if(name.equalsIgnoreCase(valueName))
267 candidateNames.add(valueName);
271 if(localEnvironment != null)
272 localEnvironment.forNames(new TObjectProcedure<String>() {
274 public boolean execute(String valueName) {
275 if(name.equalsIgnoreCase(valueName))
276 candidateNames.add(valueName);
281 if(candidateNames.size() > 0) {
282 message.append(" Did you mean ");
283 String[] ns = candidateNames.toArray(new String[candidateNames.size()]);
285 for(int i=0;i<ns.length;++i) {
287 message.append(", ");
289 message.append("or ");
291 message.append(ns[i]);
296 errorLog.log(location, message.toString());
299 public Expression resolveVariable(long location, String name) {
300 return resolveVariable(location, environment.getLocalNamespace(), name, 0);
303 public Expression resolvePattern(EVar name) {
304 char firstChar = name.name.charAt(0);
305 if(firstChar == '_' && name.name.length()==1) {
306 return new EVariable(new Variable("_"));
308 else if(!Character.isUpperCase(firstChar)) {
309 if(!frameNameSets.get(frameNameSets.size()-1).add(name.name))
310 errorLog.log(name.location, "Repeated variable "+name.name+" in pattern.");
311 return new EVariable(name.location, newVariable(name.name));
314 return resolveVariable(name.location, name.name);
318 * Starts a new environment frame. New variables defined in this frame shadow
319 * the old variables and when the frame is popped, the old variables are again
322 public void pushFrame() {
323 frames.add(variableEntries.size());
324 frameNameSets.add(new THashSet<String>());
328 * Ends an environment frame. See {@link #pushFrame}.
330 public void popFrame() {
331 int frame = frames.removeAt(frames.size()-1);
332 int i = variableEntries.size();
335 Entry entry = variableEntries.remove(i);
336 if(entry.variable == null)
337 variables.remove(entry.name);
339 variables.put(entry.name, entry.variable);
341 frameNameSets.remove(frameNameSets.size()-1);
344 public void pushRelationFrame() {
345 relationFrames.add(relationEntries.size());
348 public void popRelationFrame() {
349 int frame = relationFrames.removeAt(relationFrames.size()-1);
350 int i = relationEntries.size();
353 RelationEntry entry = relationEntries.remove(i);
354 if(entry.relation == null)
355 relations.remove(entry.name);
357 relations.put(entry.name, entry.relation);
361 public void pushCHRConstraintFrame() {
362 chrConstraintFrames.add(chrConstraintEntries.size());
365 public void popCHRConstraintFrame(ArrayList<CHRConstraint> constraints) {
366 int frame = chrConstraintFrames.removeAt(chrConstraintFrames.size()-1);
367 int i = chrConstraintEntries.size();
370 CHRConstraintEntry entry = chrConstraintEntries.remove(i);
371 CHRConstraint newConstraint;
372 if(entry.constraint == null)
373 newConstraint = chrConstraints.remove(entry.name);
375 newConstraint = chrConstraints.put(entry.name, entry.constraint);
376 if(newConstraint.implicitlyDeclared)
377 constraints.add(newConstraint);
381 public void pushExistentialFrame() {
383 existentialFrames.add(new THashSet<String>());
384 blanksInExistentialFrame.add(new ArrayList<Variable>(2));
387 public Variable[] popExistentialFrame() {
389 THashSet<String> set = existentialFrames.remove(existentialFrames.size()-1);
390 ArrayList<Variable> blanks = blanksInExistentialFrame.remove(blanksInExistentialFrame.size()-1);
391 Variable[] result = new Variable[set.size() + blanks.size()];
393 for(String name : set)
394 result[i++] = variables.remove(name);
395 for(Variable blank : blanks)
400 public Variable newVariable(String name) {
401 Variable variable = new Variable(name);
402 Variable oldVariable = variables.put(name, variable);
403 variableEntries.add(new Entry(name, oldVariable));
407 public THashMap<String, Variable> getVariables() {
411 public void newRelation(String name, SCLRelation relation) {
412 SCLRelation oldRelation = relations.put(name, relation);
413 relationEntries.add(new RelationEntry(name, oldRelation));
416 public void newCHRConstraint(String name, CHRConstraint constraint) {
417 CHRConstraint oldConstraint = chrConstraints.put(name, constraint);
418 chrConstraintEntries.add(new CHRConstraintEntry(name, oldConstraint));
421 public Precedence getPrecedence(Name op) {
422 Precedence prec = environment.getValue(op).getPrecedence();
424 return new Precedence(1, Associativity.NONASSOC);
429 public Case translateCase(Expression lhs, Expression rhs) {
430 ArrayList<Expression> parameters = new ArrayList<Expression>(4);
431 lhs.getParameters(this, parameters);
432 Expression[] patterns = new Expression[parameters.size()];
434 for(int i=0;i<patterns.length;++i) {
435 Expression pattern = parameters.get(i);
436 pattern = pattern.resolveAsPattern(this);
437 patterns[i] = pattern;
439 rhs = rhs.resolve(this);
441 Case case_ = new Case(patterns, rhs);
442 case_.setLhs(lhs.location);
446 public Expression translateCases2(ArrayList<DValueAst> definitions) {
447 Case[] cases = new Case[definitions.size()];
448 for(int i=0;i<cases.length;++i) {
449 DValueAst def = definitions.get(i);
450 cases[i] = translateCase(def.lhs, def.value);
452 // check arity consistency
453 int arity = cases[0].patterns.length;
454 for(int i=1;i<cases.length;++i)
455 if(cases[i].patterns.length != arity)
456 errorLog.log(definitions.get(i).lhs.location,
457 "Inconsistent arity. " +
458 "This case has arity " + cases[i].patterns.length +
459 " while previous cases had arity " + arity + ".");
460 if(cases.length == 1 && cases[0].patterns.length == 0)
461 return cases[0].value;
464 Locations.combine(definitions.get(0).location, definitions.get(definitions.size()-1).location),
468 public Expression translateCases(ArrayList<LetStatement> definitions) {
469 Case[] cases = new Case[definitions.size()];
470 for(int i=0;i<cases.length;++i) {
471 LetStatement def = definitions.get(i);
472 cases[i] = translateCase(def.pattern, def.value);
474 // check arity concistency
475 int arity = cases[0].patterns.length;
476 for(int i=1;i<cases.length;++i)
477 if(cases[i].patterns.length != arity)
478 errorLog.log(definitions.get(i).pattern.location,
479 "Inconsistent arity. " +
480 "This case has arity " + cases[i].patterns.length +
481 " while previous cases had arity " + arity + ".");
484 errorLog.log(cases[1].value.location, "Cannot give multiple cases for arity 0 function.");
485 return cases[0].value;
488 Locations.combine(definitions.get(0).location, definitions.get(definitions.size()-1).location),
492 public SCLValue getBindFunction() {
493 if(bindFunction == null) {
494 bindFunction = getEnvironment().getValue(Names.Prelude_bind);
499 public SCLRelation resolveRelation(long location, String name) {
500 SCLRelation relation = relations.get(name);
505 relation = Environments.getRelation(environment, name);
506 /*if(relation == null) {
507 errorLog.log(location, "Couldn't resolve relation " + name + ".");
511 } catch (AmbiguousNameException e) {
512 errorLog.log(location, e.getMessage());
517 public CHRConstraint resolveCHRConstraint(String name) {
518 return chrConstraints.get(name);
522 public SCLValue getValue(Name name) {
523 return environment.getValue(name);
526 public CHRRuleset resolveRuleset(String name) throws AmbiguousNameException {
527 return Environments.getRuleset(environment, name);