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.precedence.Associativity;
8 import org.simantics.scl.compiler.common.precedence.Precedence;
9 import org.simantics.scl.compiler.compilation.CompilationContext;
10 import org.simantics.scl.compiler.elaboration.chr.CHRRuleset;
11 import org.simantics.scl.compiler.elaboration.chr.relations.CHRConstraint;
12 import org.simantics.scl.compiler.elaboration.expressions.Case;
13 import org.simantics.scl.compiler.elaboration.expressions.EAmbiguous;
14 import org.simantics.scl.compiler.elaboration.expressions.EConstant;
15 import org.simantics.scl.compiler.elaboration.expressions.EError;
16 import org.simantics.scl.compiler.elaboration.expressions.EFieldAccess;
17 import org.simantics.scl.compiler.elaboration.expressions.ELambda;
18 import org.simantics.scl.compiler.elaboration.expressions.EVar;
19 import org.simantics.scl.compiler.elaboration.expressions.EVariable;
20 import org.simantics.scl.compiler.elaboration.expressions.Expression;
21 import org.simantics.scl.compiler.elaboration.expressions.Variable;
22 import org.simantics.scl.compiler.elaboration.expressions.accessor.FieldAccessor;
23 import org.simantics.scl.compiler.elaboration.expressions.accessor.IdAccessor;
24 import org.simantics.scl.compiler.elaboration.expressions.block.LetStatement;
25 import org.simantics.scl.compiler.elaboration.modules.SCLValue;
26 import org.simantics.scl.compiler.elaboration.query.pre.PreQuery;
27 import org.simantics.scl.compiler.elaboration.relations.SCLRelation;
28 import org.simantics.scl.compiler.environment.AmbiguousNameException;
29 import org.simantics.scl.compiler.environment.Environments;
30 import org.simantics.scl.compiler.environment.LocalEnvironment;
31 import org.simantics.scl.compiler.environment.Namespace;
32 import org.simantics.scl.compiler.environment.filter.AcceptAllNamespaceFilter;
33 import org.simantics.scl.compiler.errors.Locations;
34 import org.simantics.scl.compiler.internal.parsing.declarations.DValueAst;
35 import org.simantics.scl.compiler.module.debug.ModuleDebugInfo;
36 import org.simantics.scl.compiler.module.debug.SymbolReference;
37 import org.simantics.scl.compiler.top.SCLCompilerConfiguration;
38 import org.simantics.scl.compiler.types.Type;
40 import gnu.trove.list.array.TIntArrayList;
41 import gnu.trove.map.hash.THashMap;
42 import gnu.trove.procedure.TObjectProcedure;
43 import gnu.trove.set.hash.THashSet;
45 public class TranslationContext extends TypeTranslationContext implements EnvironmentalContext {
47 public static class ExistentialFrame {
48 THashSet<String> variables = new THashSet<String>(4);
49 ArrayList<Variable> blanks = new ArrayList<Variable>(2);
50 public boolean disallowNewExistentials;
52 public EVariable createBlank(long location) {
53 Variable variable = new Variable("_");
55 EVariable result = new EVariable(variable);
56 result.location = location;
61 THashMap<String, Variable> variables = new THashMap<String, Variable>();
62 ArrayList<Entry> variableEntries = new ArrayList<Entry>();
63 LocalEnvironment localEnvironment;
64 TIntArrayList frames = new TIntArrayList();
65 ArrayList<THashSet<String>> frameNameSets = new ArrayList<THashSet<String>>();
66 ArrayList<ExistentialFrame> existentialFrames = new ArrayList<ExistentialFrame>(2);
67 SCLValue bindFunction;
69 public PreQuery currentPreQuery;
71 THashMap<String, SCLRelation> relations = new THashMap<String, SCLRelation>();
72 TIntArrayList relationFrames = new TIntArrayList();
73 ArrayList<RelationEntry> relationEntries = new ArrayList<RelationEntry>();
75 THashMap<String, CHRConstraint> chrConstraints = new THashMap<String, CHRConstraint>();
76 TIntArrayList chrConstraintFrames = new TIntArrayList();
77 ArrayList<CHRConstraintEntry> chrConstraintEntries = new ArrayList<CHRConstraintEntry>();
79 public CHRRuleset currentRuleset;
81 public ModuleDebugInfo moduleDebugInfo;
83 private String definitionName;
88 public Entry(String name, Variable variable) {
90 this.variable = variable;
94 static class RelationEntry {
97 public RelationEntry(String name, SCLRelation relation) {
99 this.relation = relation;
103 static class CHRConstraintEntry {
105 CHRConstraint constraint;
106 public CHRConstraintEntry(String name, CHRConstraint constraint) {
108 this.constraint = constraint;
112 public TranslationContext(CompilationContext compilationContext, LocalEnvironment localEnvironment, String definitionName) {
113 super(compilationContext);
114 this.localEnvironment = localEnvironment;
115 this.moduleDebugInfo = compilationContext.moduleDebugInfo;
116 this.definitionName = definitionName;
119 public static boolean isConstructorName(String name) {
120 int p = name.lastIndexOf('.');
121 char firstChar = name.charAt(p<0 ? 0 : p+1);
122 return Character.isUpperCase(firstChar) || firstChar == '(';
125 /* Tries to resolve name as a local variable. It is assumed
126 * that name does not contain '.'.
128 private Expression resolveLocalVariable(long location, String name) {
129 Variable variable = variables.get(name);
131 return new EVariable(location, variable);
133 char c = name.charAt(0);
136 ExistentialFrame existentialFrame = getCurrentExistentialFrame();
137 if(existentialFrame == null || existentialFrame.disallowNewExistentials) {
138 errorLog.log(location, "New existential variables can be defined only in queries.");
139 return new EError(location);
141 variable = new Variable(name);
142 variables.put(name, variable);
143 existentialFrame.variables.add(name);
144 return new EVariable(variable);
147 if(name.length()==1) {
148 ExistentialFrame existentialFrame = getCurrentExistentialFrame();
149 if(existentialFrame == null || existentialFrame.disallowNewExistentials) {
150 errorLog.log(location, "Blank variables can be used only in queries.");
151 return new EError(location);
153 return existentialFrame.createBlank(location);
161 public ExistentialFrame getCurrentExistentialFrame() {
162 int size = existentialFrames.size();
166 return existentialFrames.get(size-1);
169 private Expression resolveFieldAccess(long location, Expression base, int pos, String name) {
171 int p = findSeparator(name, pos+1);
172 int endPos = p==-1 ? name.length() : p;
173 FieldAccessor accessor = new IdAccessor(
175 name.substring(pos+1, endPos));
176 accessor.location = Locations.sublocation(location, pos+1, endPos);
177 base = new EFieldAccess(base, accessor);
178 base.location = Locations.sublocation(location, 0, endPos);
184 private static int findSeparator(String name, int fromIndex) {
185 ++fromIndex; // the first character (that can be #) is never a separator
186 while(fromIndex < name.length()) {
187 char c = name.charAt(fromIndex);
188 if(c == '.' || c == '#')
195 public SCLValue resolveSCLValue(long location, Namespace namespace, String name) throws AmbiguousNameException {
196 SCLValue value = namespace.getValue(name);
199 String deprecatedDescription = value.isDeprecated();
200 if(deprecatedDescription != null)
201 errorLog.logWarning(location, "Deprecated value " + value.getName().name + "." + (deprecatedDescription.isEmpty() ? "" : " " + deprecatedDescription));
202 if(moduleDebugInfo != null)
203 moduleDebugInfo.symbolReferences.add(new SymbolReference(value.getName(), Name.create(compilationContext.module.getName(), definitionName), location));
207 public Expression resolveValue(long location, Namespace namespace, String name) {
209 SCLValue value = resolveSCLValue(location, namespace, name);
212 return new EConstant(location, value);
213 } catch (AmbiguousNameException e) {
214 if(SCLCompilerConfiguration.ALLOW_OVERLOADING)
215 return resolveAmbigious(location, e.conflictingModules, name);
217 errorLog.log(location, e.getMessage());
218 return new EError(location);
223 private Expression resolveAmbigious(long location, String[] conflictingModules, String name) {
224 EAmbiguous.Alternative[] alternatives = new EAmbiguous.Alternative[conflictingModules.length];
225 for(int i=0;i<conflictingModules.length;++i) {
226 Name altName = Name.create(conflictingModules[i], name);
227 SCLValue altValue = environment.getValue(altName);
228 alternatives[i] = new EAmbiguous.Alternative() {
230 public Type getType() {
231 return altValue.getType();
235 public Expression realize() {
236 EConstant expression = new EConstant(altValue);
237 expression.location = location;
238 if(moduleDebugInfo != null)
239 moduleDebugInfo.symbolReferences.add(new SymbolReference(altValue.getName(), Name.create(compilationContext.module.getName(), definitionName), location));
244 public String toString() {
245 return altValue.getName().toString().replace('/', '.');
249 EAmbiguous expression = new EAmbiguous(alternatives);
250 expression.location = location;
254 public Expression resolveVariable(long location, Namespace namespace, String name, int begin) {
255 int end = findSeparator(name, begin);
256 String part = end == -1 ? (begin == 0 ? name : name.substring(begin)) : name.substring(begin, end);
259 Expression result = resolveLocalVariable(location, part);
261 return end == -1 ? result : resolveFieldAccess(location, result, end, name);
263 // FIXME.. support for records
264 if(localEnvironment != null) {
265 result = localEnvironment.resolve(environment, name);
267 result.setLocationDeep(location);
272 if(end != -1 && name.charAt(end) == '.') {
273 Namespace childNamespace = namespace.getNamespace(part);
274 if(childNamespace != null)
275 return resolveVariable(location, childNamespace, name, end+1);
278 Expression result = null;
280 for(int end2 = name.length();end2 > end;end2 = name.lastIndexOf('.', end2-1)) {
281 part = name.substring(begin, end2);
282 result = resolveValue(location, namespace, part);
284 end = end2 == name.length() ? -1 : end2;
290 result = resolveValue(location, namespace, part);
292 return end == -1 ? result : resolveFieldAccess(location, result, end, name);
294 reportResolveFailure(location, namespace, part);
295 return new EError(location);
298 public SCLValue resolveRecordConstructor(long location, String name) throws AmbiguousNameException {
299 return resolveRecordConstructor(location, getEnvironment().getLocalNamespace(), name, 0);
302 public SCLValue resolveRecordConstructor(long location, Namespace namespace, String name, int begin) throws AmbiguousNameException {
303 int end = findSeparator(name, begin);
304 String part = end == -1 ? (begin == 0 ? name : name.substring(begin)) : name.substring(begin, end);
306 if(end != -1 && name.charAt(end) == '.') {
307 Namespace childNamespace = namespace.getNamespace(part);
308 if(childNamespace == null)
311 return resolveRecordConstructor(location, childNamespace, name, end+1);
314 return resolveSCLValue(location, namespace, part);
317 private void reportResolveFailure(long location, Namespace namespace, String name) {
318 StringBuilder message = new StringBuilder();
319 message.append("Couldn't resolve ").append(name).append(".");
321 final THashSet<String> candidateNames = new THashSet<String>(4);
322 namespace.findValuesForPrefix("", AcceptAllNamespaceFilter.INSTANCE,
323 new TObjectProcedure<SCLValue>() {
325 public boolean execute(SCLValue value) {
327 new Exception().printStackTrace();
330 String valueName = value.getName().name;
331 if(name.equalsIgnoreCase(valueName))
332 candidateNames.add(valueName);
336 if(localEnvironment != null)
337 localEnvironment.forNames(new TObjectProcedure<String>() {
339 public boolean execute(String valueName) {
340 if(name.equalsIgnoreCase(valueName))
341 candidateNames.add(valueName);
346 if(candidateNames.size() > 0) {
347 message.append(" Did you mean ");
348 String[] ns = candidateNames.toArray(new String[candidateNames.size()]);
350 for(int i=0;i<ns.length;++i) {
352 message.append(", ");
354 message.append("or ");
356 message.append(ns[i]);
361 errorLog.log(location, message.toString());
364 public Expression resolveVariable(long location, String name) {
365 return resolveVariable(location, environment.getLocalNamespace(), name, 0);
368 public Expression resolvePattern(EVar name) {
369 char firstChar = name.name.charAt(0);
370 if(firstChar == '_' && name.name.length()==1) {
371 return new EVariable(new Variable("_"));
373 else if(!Character.isUpperCase(firstChar)) {
374 if(!frameNameSets.get(frameNameSets.size()-1).add(name.name))
375 errorLog.log(name.location, "Repeated variable "+name.name+" in pattern.");
376 return new EVariable(name.location, newVariable(name.name));
379 return resolveVariable(name.location, name.name);
383 * Starts a new environment frame. New variables defined in this frame shadow
384 * the old variables and when the frame is popped, the old variables are again
387 public void pushFrame() {
388 frames.add(variableEntries.size());
389 frameNameSets.add(new THashSet<String>());
393 * Ends an environment frame. See {@link #pushFrame}.
395 public void popFrame() {
396 int frame = frames.removeAt(frames.size()-1);
397 int i = variableEntries.size();
400 Entry entry = variableEntries.remove(i);
401 if(entry.variable == null)
402 variables.remove(entry.name);
404 variables.put(entry.name, entry.variable);
406 frameNameSets.remove(frameNameSets.size()-1);
409 public void pushRelationFrame() {
410 relationFrames.add(relationEntries.size());
413 public void popRelationFrame() {
414 int frame = relationFrames.removeAt(relationFrames.size()-1);
415 int i = relationEntries.size();
418 RelationEntry entry = relationEntries.remove(i);
419 if(entry.relation == null)
420 relations.remove(entry.name);
422 relations.put(entry.name, entry.relation);
426 public void pushCHRConstraintFrame() {
427 chrConstraintFrames.add(chrConstraintEntries.size());
430 public void popCHRConstraintFrame(CHRRuleset ruleset) {
431 int frame = chrConstraintFrames.removeAt(chrConstraintFrames.size()-1);
432 int i = chrConstraintEntries.size();
435 CHRConstraintEntry entry = chrConstraintEntries.remove(i);
436 CHRConstraint newConstraint;
437 if(entry.constraint == null)
438 newConstraint = chrConstraints.remove(entry.name);
440 newConstraint = chrConstraints.put(entry.name, entry.constraint);
441 if(newConstraint.implicitlyDeclared)
442 ruleset.addConstraint(newConstraint);
446 public void pushExistentialFrame() {
448 existentialFrames.add(new ExistentialFrame());
451 public Variable[] popExistentialFrame() {
453 ExistentialFrame frame = existentialFrames.remove(existentialFrames.size()-1);
454 Variable[] result = new Variable[frame.variables.size() + frame.blanks.size()];
456 for(String name : frame.variables)
457 result[i++] = variables.remove(name);
458 for(Variable blank : frame.blanks)
463 public Variable newVariable(String name) {
464 Variable variable = new Variable(name);
465 Variable oldVariable = variables.put(name, variable);
466 variableEntries.add(new Entry(name, oldVariable));
470 public THashMap<String, Variable> getVariables() {
474 public void newRelation(String name, SCLRelation relation) {
475 SCLRelation oldRelation = relations.put(name, relation);
476 relationEntries.add(new RelationEntry(name, oldRelation));
479 public void newCHRConstraint(String name, CHRConstraint constraint) {
480 CHRConstraint oldConstraint = chrConstraints.put(name, constraint);
481 chrConstraintEntries.add(new CHRConstraintEntry(name, oldConstraint));
484 public Precedence getPrecedence(Name op) {
485 Precedence prec = environment.getValue(op).getPrecedence();
487 return new Precedence(1, Associativity.NONASSOC);
492 public Case translateCase(Expression lhs, Expression rhs) {
493 ArrayList<Expression> parameters = new ArrayList<Expression>(4);
494 lhs.getParameters(this, parameters);
495 Expression[] patterns = new Expression[parameters.size()];
497 for(int i=0;i<patterns.length;++i) {
498 Expression pattern = parameters.get(i);
499 pattern = pattern.resolveAsPattern(this);
500 patterns[i] = pattern;
502 rhs = rhs.resolve(this);
504 Case case_ = new Case(patterns, rhs);
505 case_.setLhs(lhs.location);
509 public Expression translateCases2(ArrayList<DValueAst> definitions) {
510 Case[] cases = new Case[definitions.size()];
511 for(int i=0;i<cases.length;++i) {
512 DValueAst def = definitions.get(i);
513 cases[i] = translateCase(def.lhs, def.value);
515 // check arity consistency
516 int arity = cases[0].patterns.length;
517 for(int i=1;i<cases.length;++i)
518 if(cases[i].patterns.length != arity)
519 errorLog.log(definitions.get(i).lhs.location,
520 "Inconsistent arity. " +
521 "This case has arity " + cases[i].patterns.length +
522 " while previous cases had arity " + arity + ".");
523 if(cases.length == 1 && cases[0].patterns.length == 0)
524 return cases[0].value;
527 Locations.combine(definitions.get(0).location, definitions.get(definitions.size()-1).location),
531 public Expression translateCases(ArrayList<LetStatement> definitions) {
532 Case[] cases = new Case[definitions.size()];
533 for(int i=0;i<cases.length;++i) {
534 LetStatement def = definitions.get(i);
535 cases[i] = translateCase(def.pattern, def.value);
537 // check arity concistency
538 int arity = cases[0].patterns.length;
539 for(int i=1;i<cases.length;++i)
540 if(cases[i].patterns.length != arity)
541 errorLog.log(definitions.get(i).pattern.location,
542 "Inconsistent arity. " +
543 "This case has arity " + cases[i].patterns.length +
544 " while previous cases had arity " + arity + ".");
547 errorLog.log(cases[1].value.location, "Cannot give multiple cases for arity 0 function.");
548 return cases[0].value;
551 Locations.combine(definitions.get(0).location, definitions.get(definitions.size()-1).location),
555 public SCLRelation resolveRelation(long location, String name) {
556 SCLRelation relation = relations.get(name);
561 relation = Environments.getRelation(environment, name);
562 /*if(relation == null) {
563 errorLog.log(location, "Couldn't resolve relation " + name + ".");
567 } catch (AmbiguousNameException e) {
568 errorLog.log(location, e.getMessage());
573 public CHRConstraint resolveCHRConstraint(String name) {
574 return chrConstraints.get(name);
578 public SCLValue getValue(Name name) {
579 return environment.getValue(name);
582 public CHRRuleset resolveRuleset(String name) throws AmbiguousNameException {
583 return Environments.getRuleset(environment, name);
586 public void disallowNewExistentials() {
587 getCurrentExistentialFrame().disallowNewExistentials = true;