]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.scl.compiler/src/org/simantics/scl/compiler/compilation/Elaboration.java
c59df828d93a852cf6c5b03d848f8cad9b3f3792
[simantics/platform.git] / bundles / org.simantics.scl.compiler / src / org / simantics / scl / compiler / compilation / Elaboration.java
1 package org.simantics.scl.compiler.compilation;
2
3 import java.util.ArrayList;
4 import java.util.Iterator;
5 import java.util.List;
6
7 import org.cojen.classfile.TypeDesc;
8 import org.simantics.scl.compiler.common.datatypes.Constructor;
9 import org.simantics.scl.compiler.common.exceptions.InternalCompilerError;
10 import org.simantics.scl.compiler.common.names.Name;
11 import org.simantics.scl.compiler.constants.Constant;
12 import org.simantics.scl.compiler.constants.JavaTypeInstanceConstructor;
13 import org.simantics.scl.compiler.constants.SCLConstructor;
14 import org.simantics.scl.compiler.constants.StringConstant;
15 import org.simantics.scl.compiler.constants.generic.CallJava;
16 import org.simantics.scl.compiler.constants.generic.ClassRef;
17 import org.simantics.scl.compiler.constants.generic.ConvertToListFilter;
18 import org.simantics.scl.compiler.constants.generic.MethodRef;
19 import org.simantics.scl.compiler.constants.generic.MethodRef.FieldRef;
20 import org.simantics.scl.compiler.constants.generic.OutputFilter;
21 import org.simantics.scl.compiler.constants.generic.ParameterStackItem;
22 import org.simantics.scl.compiler.constants.generic.Pop2OutputFilter;
23 import org.simantics.scl.compiler.constants.generic.PopOutputFilter;
24 import org.simantics.scl.compiler.constants.generic.StackItem;
25 import org.simantics.scl.compiler.constants.generic.ThreadLocalStackItem;
26 import org.simantics.scl.compiler.constants.singletons.SafeCoerce;
27 import org.simantics.scl.compiler.elaboration.contexts.TranslationContext;
28 import org.simantics.scl.compiler.elaboration.contexts.TypeTranslationContext;
29 import org.simantics.scl.compiler.elaboration.errors.NotPatternException;
30 import org.simantics.scl.compiler.elaboration.expressions.EApply;
31 import org.simantics.scl.compiler.elaboration.expressions.EGetConstraint;
32 import org.simantics.scl.compiler.elaboration.expressions.EIntegerLiteral;
33 import org.simantics.scl.compiler.elaboration.expressions.EListLiteral;
34 import org.simantics.scl.compiler.elaboration.expressions.ELiteral;
35 import org.simantics.scl.compiler.elaboration.expressions.EPreCHRRulesetConstructor;
36 import org.simantics.scl.compiler.elaboration.expressions.EVar;
37 import org.simantics.scl.compiler.elaboration.expressions.Expression;
38 import org.simantics.scl.compiler.elaboration.expressions.Variable;
39 import org.simantics.scl.compiler.elaboration.expressions.annotations.AnnotationUtils;
40 import org.simantics.scl.compiler.elaboration.fundeps.Fundep;
41 import org.simantics.scl.compiler.elaboration.java.JavaMethodDeclaration;
42 import org.simantics.scl.compiler.elaboration.macros.StandardMacroRule;
43 import org.simantics.scl.compiler.elaboration.modules.DeprecatedProperty;
44 import org.simantics.scl.compiler.elaboration.modules.DerivedProperty;
45 import org.simantics.scl.compiler.elaboration.modules.InlineProperty;
46 import org.simantics.scl.compiler.elaboration.modules.MethodImplementation;
47 import org.simantics.scl.compiler.elaboration.modules.PrivateProperty;
48 import org.simantics.scl.compiler.elaboration.modules.SCLValue;
49 import org.simantics.scl.compiler.elaboration.modules.TypeAlias;
50 import org.simantics.scl.compiler.elaboration.modules.TypeClass;
51 import org.simantics.scl.compiler.elaboration.modules.TypeClassInstance;
52 import org.simantics.scl.compiler.elaboration.modules.TypeClassMethod;
53 import org.simantics.scl.compiler.elaboration.modules.TypeDescriptor;
54 import org.simantics.scl.compiler.elaboration.query.Query;
55 import org.simantics.scl.compiler.elaboration.query.pre.QPreGuard;
56 import org.simantics.scl.compiler.elaboration.relations.ConcreteRelation;
57 import org.simantics.scl.compiler.elaboration.rules.MappingRelation;
58 import org.simantics.scl.compiler.elaboration.rules.SectionName;
59 import org.simantics.scl.compiler.elaboration.rules.TransformationRule;
60 import org.simantics.scl.compiler.environment.AmbiguousNameException;
61 import org.simantics.scl.compiler.environment.Environment;
62 import org.simantics.scl.compiler.environment.EnvironmentFactory;
63 import org.simantics.scl.compiler.environment.Environments;
64 import org.simantics.scl.compiler.errors.CompilationError;
65 import org.simantics.scl.compiler.errors.ErrorLog;
66 import org.simantics.scl.compiler.errors.ErrorSeverity;
67 import org.simantics.scl.compiler.errors.Locations;
68 import org.simantics.scl.compiler.internal.codegen.effects.EffectConstructor;
69 import org.simantics.scl.compiler.internal.codegen.effects.ThreadLocalVariable;
70 import org.simantics.scl.compiler.internal.codegen.types.JavaReferenceValidator;
71 import org.simantics.scl.compiler.internal.codegen.types.JavaReferenceValidatorFactory;
72 import org.simantics.scl.compiler.internal.codegen.types.JavaTypeTranslator;
73 import org.simantics.scl.compiler.internal.codegen.types.StandardTypeConstructor;
74 import org.simantics.scl.compiler.internal.codegen.utils.Constants;
75 import org.simantics.scl.compiler.internal.codegen.utils.JavaNamingPolicy;
76 import org.simantics.scl.compiler.internal.codegen.utils.MethodBuilderBase;
77 import org.simantics.scl.compiler.internal.deriving.InstanceDeriver;
78 import org.simantics.scl.compiler.internal.deriving.InstanceDerivers;
79 import org.simantics.scl.compiler.internal.elaboration.profiling.BranchPointInjector;
80 import org.simantics.scl.compiler.internal.elaboration.utils.StronglyConnectedComponents;
81 import org.simantics.scl.compiler.internal.header.ModuleHeader;
82 import org.simantics.scl.compiler.internal.parsing.Token;
83 import org.simantics.scl.compiler.internal.parsing.declarations.ConstructorAst;
84 import org.simantics.scl.compiler.internal.parsing.declarations.DAnnotationAst;
85 import org.simantics.scl.compiler.internal.parsing.declarations.DClassAst;
86 import org.simantics.scl.compiler.internal.parsing.declarations.DDataAst;
87 import org.simantics.scl.compiler.internal.parsing.declarations.DDerivingInstanceAst;
88 import org.simantics.scl.compiler.internal.parsing.declarations.DEffectAst;
89 import org.simantics.scl.compiler.internal.parsing.declarations.DFixityAst;
90 import org.simantics.scl.compiler.internal.parsing.declarations.DInstanceAst;
91 import org.simantics.scl.compiler.internal.parsing.declarations.DMappingRelationAst;
92 import org.simantics.scl.compiler.internal.parsing.declarations.DRelationAst;
93 import org.simantics.scl.compiler.internal.parsing.declarations.DRuleAst;
94 import org.simantics.scl.compiler.internal.parsing.declarations.DRulesetAst;
95 import org.simantics.scl.compiler.internal.parsing.declarations.DTypeAst;
96 import org.simantics.scl.compiler.internal.parsing.declarations.DValueAst;
97 import org.simantics.scl.compiler.internal.parsing.declarations.DValueTypeAst;
98 import org.simantics.scl.compiler.internal.parsing.exceptions.SCLSyntaxErrorException;
99 import org.simantics.scl.compiler.internal.parsing.parser.SCLTerminals;
100 import org.simantics.scl.compiler.internal.parsing.translation.ProcessedDClassAst;
101 import org.simantics.scl.compiler.internal.parsing.translation.ProcessedDInstanceAst;
102 import org.simantics.scl.compiler.internal.parsing.translation.RelationRepository;
103 import org.simantics.scl.compiler.internal.parsing.translation.ValueRepository;
104 import org.simantics.scl.compiler.internal.parsing.types.TypeAst;
105 import org.simantics.scl.compiler.module.ConcreteModule;
106 import org.simantics.scl.compiler.module.ImportDeclaration;
107 import org.simantics.scl.compiler.module.InvalidModulePathException;
108 import org.simantics.scl.compiler.module.ModuleUtils;
109 import org.simantics.scl.compiler.module.debug.ModuleDebugInfo;
110 import org.simantics.scl.compiler.module.repository.ImportFailure;
111 import org.simantics.scl.compiler.module.repository.ImportFailureException;
112 import org.simantics.scl.compiler.top.SCLCompilerConfiguration;
113 import org.simantics.scl.compiler.types.TCon;
114 import org.simantics.scl.compiler.types.TForAll;
115 import org.simantics.scl.compiler.types.TFun;
116 import org.simantics.scl.compiler.types.TPred;
117 import org.simantics.scl.compiler.types.TVar;
118 import org.simantics.scl.compiler.types.Type;
119 import org.simantics.scl.compiler.types.Types;
120 import org.simantics.scl.compiler.types.kinds.Kind;
121 import org.simantics.scl.compiler.types.kinds.Kinds;
122 import org.simantics.scl.compiler.types.util.MultiFunction;
123 import org.simantics.scl.runtime.profiling.BranchPoint;
124
125 import gnu.trove.list.array.TIntArrayList;
126 import gnu.trove.map.hash.THashMap;
127 import gnu.trove.map.hash.TObjectIntHashMap;
128 import gnu.trove.procedure.TObjectObjectProcedure;
129 import gnu.trove.set.hash.THashSet;
130 import gnu.trove.set.hash.TIntHashSet;
131
132 public class Elaboration {
133     // inputs
134     private final CompilationContext compilationContext;
135     private final ErrorLog errorLog;
136     private final String moduleName;
137     private final ModuleHeader moduleHeader;
138     private final ArrayList<ImportDeclaration> importsAst;
139     private final JavaReferenceValidatorFactory jrvFactory;
140     final JavaReferenceValidator<Object, Object, Object, Object> javaReferenceValidator;
141     private final ValueRepository valueDefinitionsAst;
142     private final RelationRepository relationDefinitionsAst;
143
144     // creates
145     ConcreteModule module;
146     Environment importedEnvironment;
147     ArrayList<SupplementedValueType> supplementedTypeAnnotations = new ArrayList<SupplementedValueType>();
148     
149     JavaTypeTranslator javaTypeTranslator;
150     ArrayList<StandardTypeConstructor> dataTypes = new ArrayList<StandardTypeConstructor>();
151     THashMap<String, ClassRef> classRefs = new THashMap<String, ClassRef>();
152     THashMap<String, BranchPoint[]> branchPoints; 
153
154     THashMap<String, EVar> exportMap = null;
155     
156     public Elaboration(CompilationContext compilationContext, CompilationTimer timer, EnvironmentFactory localEnvironmentFactory,
157             String moduleName, ModuleHeader moduleHeader, ArrayList<ImportDeclaration> importsAst,
158             JavaReferenceValidatorFactory jrvFactory,
159             ValueRepository valueDefinitionsAst,
160             RelationRepository relationDefinitionsAst) {
161         this.compilationContext = compilationContext;
162         this.errorLog = compilationContext.errorLog;
163         this.moduleName = moduleName;
164         this.moduleHeader = moduleHeader;
165         if(moduleName != null)
166             importsAst = processRelativeImports(compilationContext.errorLog, moduleName, importsAst);
167         this.importsAst = importsAst;
168         this.jrvFactory = jrvFactory;
169         this.javaReferenceValidator = moduleHeader == null || moduleHeader.classLoader == null
170                 ? jrvFactory.getDefaultJavaReferenceValidator()
171                 : jrvFactory.getJavaReferenceValidator(moduleHeader.classLoader);
172         if(javaReferenceValidator == null)
173             errorLog.log(moduleHeader.classLoaderLocation, "Didn't find the specified class loader.");
174         this.valueDefinitionsAst = valueDefinitionsAst;
175         this.relationDefinitionsAst = relationDefinitionsAst;
176
177         module = new ConcreteModule(moduleName);
178         compilationContext.module = module;
179         compilationContext.moduleRepository = localEnvironmentFactory.getModuleRepository();
180         if(moduleHeader != null) {
181             if(moduleHeader.defaultLocalName != null)
182                 module.setDefaultLocalName(moduleHeader.defaultLocalName);
183             if(moduleHeader.deprecated != null)
184                 module.setDeprecation(moduleHeader.deprecated);
185         }
186         try {
187             if(timer != null)
188                 timer.suspendTimer();
189             importedEnvironment = localEnvironmentFactory.createEnvironment(
190                     compilationContext,
191                     importsAst.toArray(new ImportDeclaration[importsAst.size()]));
192             if(timer != null)
193                 timer.continueTimer();
194             compilationContext.environment = new EnvironmentOfModule(importedEnvironment, module);
195         } catch (ImportFailureException e) {
196             for(ImportFailure failure : e.failures)
197                 errorLog.log(new CompilationError(failure.location, failure.toString(),
198                         failure.reason == ImportFailure.MODULE_DOES_NOT_EXIST_REASON ? ErrorSeverity.ERROR : ErrorSeverity.IMPORT_ERROR));
199             return;
200         }
201         for(ImportDeclaration importAst : importsAst)
202             this.module.addDependency(new ImportDeclaration(
203                     importAst.moduleName,
204                     importAst.reexport ? importAst.localName : null,
205                     false,
206                     importAst.spec));
207         localEnvironmentFactory.addBuiltinDependencies(module);
208         compilationContext.namingPolicy = new JavaNamingPolicy(moduleName);
209     }
210     
211     public static ArrayList<ImportDeclaration> processRelativeImports(ErrorLog errorLog, String moduleName, ArrayList<ImportDeclaration> relativeImports) {
212         ArrayList<ImportDeclaration> absoluteImports = new ArrayList<ImportDeclaration>(relativeImports.size());
213         for(ImportDeclaration relativeImport : relativeImports) {
214             if(relativeImport.moduleName.startsWith(".")) {
215                                 try {
216                                         String absoluteModuleName = ModuleUtils.resolveAbsolutePath(moduleName, relativeImport.moduleName);
217                     ImportDeclaration absoluteImport = new ImportDeclaration(
218                             relativeImport.location,
219                             absoluteModuleName, relativeImport.localName,
220                             relativeImport.reexport, relativeImport.spec);
221                     absoluteImports.add(absoluteImport);
222                                 } catch (InvalidModulePathException e) {
223                                     if(errorLog != null)
224                                         errorLog.log(relativeImport.location, e.getMessage());
225                                 }
226             }
227             else
228                 absoluteImports.add(relativeImport);
229         }
230         return absoluteImports;
231     }
232
233     public void addTypesToEnvironment(
234             ArrayList<DDataAst> dataTypesAst,
235             ArrayList<DTypeAst> typeAliasesAst,
236             ArrayList<DEffectAst> effectsAst,
237             ArrayList<DRulesetAst> rulesetsAst) {
238         for(DDataAst dataType : dataTypesAst) {
239             dataType.parameterKinds = new Kind[dataType.parameters.length];
240             Kind constructorKind = Kinds.STAR;
241             for(int i=dataType.parameters.length-1;i>=0;--i) {
242                 Kind kind = Kinds.metaVar();
243                 dataType.parameterKinds[i] = kind;
244                 constructorKind = Kinds.arrow(kind, constructorKind);
245             }
246             
247             StandardTypeConstructor typeConstructor = new StandardTypeConstructor(
248                     Types.con(moduleName, dataType.name), constructorKind);
249
250             NameExistenceChecks.checkIfTypeExists(errorLog,
251                     dataType.location, importedEnvironment, dataType.name);
252             addTypeDescriptor(dataType.location, dataType.name, typeConstructor);
253             dataType.typeConstructor = typeConstructor;
254         }
255         
256         for(DTypeAst typeAlias : typeAliasesAst) {
257             TypeAlias alias = new TypeAlias(Types.con(moduleName, typeAlias.name), typeAlias.parameters.length);
258             NameExistenceChecks.checkIfTypeExists(errorLog,
259                     typeAlias.location, importedEnvironment, typeAlias.name);
260             addTypeDescriptor(typeAlias.location, typeAlias.name, alias);
261         }
262         
263         for(DEffectAst effect : effectsAst) {
264             EffectConstructor effectConstructor = new EffectConstructor(Types.con(moduleName, effect.name));
265             effectConstructor.addThreadLocalVariable(
266                     new ThreadLocalVariable(
267                             effect.variableName,
268                             TypeDesc.forClass(effect.threadLocalType)
269                             ));
270             if(module.addEffectConstructor(effect.name, effectConstructor))
271                 errorLog.log(effect.location, "Effect "+effect.name+" has already been defined in this module.");
272         }
273         for(DRulesetAst ruleset : rulesetsAst) {
274             ruleset.type = Types.con(moduleName, ruleset.name);
275             ruleset.className = compilationContext.namingPolicy.getDataTypeClassName(ruleset.name);
276             StandardTypeConstructor typeConstructor = new StandardTypeConstructor(ruleset.type, Kinds.STAR,
277                     TypeDesc.forClass(ruleset.className), ruleset.documentation == null ? null : ruleset.documentation.documentation);
278             typeConstructor.external = true;
279             addTypeDescriptor(ruleset.location, ruleset.name, typeConstructor);
280         }
281         javaTypeTranslator = new JavaTypeTranslator(compilationContext.environment);
282         compilationContext.javaTypeTranslator = javaTypeTranslator;
283     }
284         
285     private void addTypeDescriptor(long location, String name, TypeDescriptor typeDescriptor) {
286         if(module.addTypeDescriptor(name, typeDescriptor))
287             errorLog.log(location, "Type "+name+" has already been defined in this module.");
288     }
289     
290     private static final int[] EMPTY_INT_ARRAY = new int[0];
291     
292     public void processTypeAliases(ArrayList<DTypeAst> typeAliasesAst) {
293         TObjectIntHashMap<String> typeAliasMap = new TObjectIntHashMap<String>();
294         for(int i=0;i<typeAliasesAst.size();++i)
295             typeAliasMap.put(typeAliasesAst.get(i).name, i); 
296         TIntHashSet tempIntSet = new TIntHashSet();
297         ArrayList<DTypeAst> orderedTypeAliases = new ArrayList<DTypeAst>(typeAliasesAst.size()); 
298         //for(int i=0;i<typeAliasesAst.size();++i)
299         //    System.out.println(i + "# " + typeAliasesAst.get(i).name);
300         new StronglyConnectedComponents(typeAliasesAst.size()) {
301             @Override
302             protected int[] findDependencies(int u) {
303                 typeAliasesAst.get(u).type.collectReferences(typeAliasMap, tempIntSet);
304                 if(tempIntSet.isEmpty())
305                     return EMPTY_INT_ARRAY;
306                 if(tempIntSet.contains(u)) {
307                     errorLog.log(typeAliasesAst.get(u).location, "Type alias has a self reference.");
308                     tempIntSet.remove(u);
309                 }
310                 int[] result = tempIntSet.toArray();
311                 tempIntSet.clear();
312                 //System.out.println(u + " -> " + Arrays.toString(result));
313                 return result; 
314             }
315             @Override
316             protected void reportComponent(int[] component) {
317                 //System.out.println("component: " + Arrays.toString(component));
318                 if(component.length > 1) {
319                     StringBuilder b = new StringBuilder();
320                     b.append("Recursively defined type alias (");
321                     long minLocation = typeAliasesAst.get(component[0]).location;
322                     boolean first = true;
323                     for(int u : component) {
324                         DTypeAst typeAlias = typeAliasesAst.get(u);
325                         if(first)
326                             first = false;
327                         else
328                             b.append(", ");
329                         b.append(typeAlias.name);
330                         if(Locations.beginOf(typeAlias.location) < Locations.beginOf(minLocation))
331                             minLocation = typeAlias.location;
332                     }
333                     b.append(").");
334                     errorLog.log(minLocation, b.toString());
335                 }
336                 else
337                     orderedTypeAliases.add(typeAliasesAst.get(component[0]));
338             }
339         }.findComponents();
340         
341         if(errorLog.hasNoErrors()) {
342             for(DTypeAst typeAlias : orderedTypeAliases) {
343                 TypeAlias alias = (TypeAlias)module.getTypeDescriptor(typeAlias.name);
344                 TypeTranslationContext context = createTypeTranslationContext();
345                 for(int i=0;i<typeAlias.parameters.length;++i)
346                     context.pushTypeVar(typeAlias.parameters[i]);
347                 alias.body = typeAlias.type.toType(context, Kinds.metaVar());
348                 for(int i=0;i<typeAlias.parameters.length;++i)
349                     alias.parameters[i] = context.popTypeVar(typeAlias.parameters[i], null);
350             }
351         }
352     }
353     
354     public void processDataTypes(ArrayList<DDataAst> dataTypesAst) {
355         ArrayList<Runnable> fieldAccessorGenerators = new ArrayList<Runnable>(); 
356         for(DDataAst dataTypeAst : dataTypesAst) {
357             TypeTranslationContext context = createTypeTranslationContext();
358             TVar[] typeParameters = new TVar[dataTypeAst.parameters.length];
359             for(int i=0;i<dataTypeAst.parameters.length;++i)
360                 typeParameters[i] = context.addTypeVar(dataTypeAst.parameters[i]);
361             Constructor[] constructors = new Constructor[dataTypeAst.constructors.length];
362             String className = null;
363             boolean external = false;
364             for(DAnnotationAst annotation : dataTypeAst.getAnnotations()) {
365                 if(annotation.id.text.equals("@JavaType")) {
366                     if(annotation.parameters.length != 1 || 
367                             !(annotation.parameters[0] instanceof ELiteral) ||
368                             !(((ELiteral)annotation.parameters[0]).getValue() instanceof StringConstant))
369                         errorLog.log(annotation.location, "Invalid parameters. Expected @JavaType \"className\".");
370                     else {
371                         className = ((StringConstant)((ELiteral)annotation.parameters[0]).getValue()).getValue().replace('.', '/');
372                         external = true;                        
373                         break;
374                     }
375                 }
376             }
377             
378             boolean trivialDataType = dataTypeAst.constructors.length == 1 &&
379                     dataTypeAst.constructors[0].parameters.length == 1;
380             if(className == null && !trivialDataType)
381                 className = compilationContext.namingPolicy.getDataTypeClassName(dataTypeAst.name);
382             
383             StandardTypeConstructor dataType = dataTypeAst.typeConstructor;
384             
385             dataType.setType(Types.con(moduleName, dataTypeAst.name), typeParameters);
386             dataType.setConstructors(constructors);
387             if(!trivialDataType)
388                 dataType.setTypeDesc(TypeDesc.forClass(className));            
389             if(!external || dataTypeAst.constructors.length > 0) {
390                 dataType.isOpen = false;
391             }
392             dataType.external = external;
393             dataTypes.add(dataType);
394             for(int j=0;j<constructors.length;++j) {
395                 ConstructorAst constructor = dataTypeAst.constructors[j];
396                 String name = constructor.name.text;
397                 Type[] parameterTypes = new Type[constructor.parameters.length];
398                 for(int i=constructor.parameters.length-1;i>=0;--i)
399                     parameterTypes[i] = context.toType(constructor.parameters[i]);
400                 String javaName = constructors.length == 1 ? className 
401                         : compilationContext.namingPolicy.getConstructorClassName(name);
402                 String[] fieldNames = null;
403                 for(DAnnotationAst annotation : constructor.annotations)
404                     if(annotation.id.text.equals("@JavaType")) {
405                         try {
406                             javaName = ((StringConstant)((ELiteral)annotation.parameters[0])
407                                     .getValue()).getValue();
408                         } catch(Exception e) {
409                             errorLog.log(annotation.parameters[0].location, "Invalid annotation parameter.");
410                         }
411                     }
412                     else if(annotation.id.text.equals("@FieldNames")) {
413                         try {
414                             EListLiteral literal = (EListLiteral)annotation.parameters[0];
415                             fieldNames = new String[literal.getComponents().length];
416                             for(int i=0;i<fieldNames.length;++i) {
417                                 Expression component = literal.getComponents()[i];
418                                 if(component instanceof EVar)
419                                     fieldNames[i] = ((EVar)component).name;
420                                 else if(component instanceof ELiteral)
421                                     fieldNames[i] = ((StringConstant)((ELiteral)component).getValue()).getValue();
422                             }
423                         } catch(Exception e) {
424                             errorLog.log(annotation.parameters[0].location, "Invalid annotation parameter.");
425                             fieldNames = null;
426                         }
427                     }   
428                 
429                 constructors[j] = new Constructor(constructor.location, dataType,
430                         Name.create(moduleName, name), 
431                         parameterTypes, javaName);
432                 constructors[j].fieldNames = fieldNames;
433                 constructors[j].recordFieldNames = constructor.fieldNames;
434             }
435             if(constructors.length == 1) {
436                 Constructor constructor = constructors[0];
437                 if(constructor.recordFieldNames != null) {
438                     fieldAccessorGenerators.add(new Runnable() {
439                         @Override
440                         public void run() {
441                             Type in = Types.apply(dataType.name, dataType.parameters);
442                             for(int i=0;i<constructor.recordFieldNames.length;++i) {
443                                 Type out = constructor.parameterTypes[i];
444                                 Constant accessor;
445                                 if(trivialDataType)
446                                     accessor = new SafeCoerce(dataType.parameters, in, out);
447                                 else
448                                     accessor = new CallJava(dataType.parameters, Types.NO_EFFECTS, out,
449                                             new Type[] {in}, new StackItem[] {new ParameterStackItem(0, in)},
450                                             new FieldRef(constructor.javaName, constructor.fieldNames != null ? constructor.fieldNames[i] : "c" + i,
451                                                     javaTypeTranslator.toTypeDesc(out)),
452                                             null);
453                                 module.addFieldAccessor(constructor.recordFieldNames[i], accessor);
454                             }
455                         }
456                     });
457                 }
458             }
459         }
460         
461         for(Runnable fieldAccessorGenerator : fieldAccessorGenerators)
462             fieldAccessorGenerator.run();
463     }
464     
465     public void processTypeClasses(ArrayList<ProcessedDClassAst> typeClassesAst) {
466         for(ProcessedDClassAst pClassAst : typeClassesAst) {
467             DClassAst classAst = pClassAst.orig;
468             
469             if(module.getTypeClass(classAst.name) != null) {
470                 errorLog.log(classAst.location, "Class "+classAst.name+" has already been defined in this module.");
471                 continue;
472             }
473             
474             TypeTranslationContext context = createTypeTranslationContext();            
475             
476             TPred[] classContext = new TPred[classAst.context.length];
477             for(int i=0;i<classContext.length;++i)
478                 classContext[i] = context.toTFuncApply(classAst.context[i]);
479             
480             TVar[] parameters = new TVar[classAst.parameters.length];
481             
482             if(classAst.name.equals("Functor") || classAst.name.equals("Monad")) {
483                 // FIXME hack
484                 parameters[0] = context.resolveTypeVariable(pClassAst.orig.location, classAst.parameters[0], Kinds.STAR_TO_STAR);
485             }
486             else {
487                 for(int i=0;i<parameters.length;++i)
488                     parameters[i] = context.resolveTypeVariable(pClassAst.orig.location, classAst.parameters[i], Kinds.metaVar());
489             }
490                         
491             TCon con = Types.con(moduleName, classAst.name);
492             TypeClass typeClass = new TypeClass(classAst.location, 
493                     classContext, 
494                     con, 
495                     compilationContext.namingPolicy.getTypeClassInterfaceName(con),
496                     parameters,
497                     Fundep.mapFundeps(classAst.parameters, classAst.fundeps));
498             
499             THashMap<String, TypeClassMethod> methods = typeClass.methods;
500             for(DValueTypeAst methodDecl : pClassAst.typeDeclarations) {
501                 try {
502                     Type type;
503                     try {
504                         type = context.toType(methodDecl.type);
505                     } catch(SCLSyntaxErrorException e) {
506                         errorLog.log(e.location, e.getMessage());
507                         continue;
508                     }
509                     for(EVar name : methodDecl.names) {
510                         typeClass.methodNames.add(name.name);
511                         methods.put(name.name,
512                                 new TypeClassMethod(typeClass, name.name, 
513                                         compilationContext.namingPolicy.getMethodName(name.name),
514                                         type, Types.getArity(type),
515                                         name.location)
516                                 );
517                     }
518                 } catch(RuntimeException e) {
519                     errorLog.setExceptionPosition(methodDecl.location);
520                     throw e;
521                 }
522             }
523             
524             for(String methodName : pClassAst.defaultImplementations.getValueNames()) {
525                 String fullName = "_" + classAst.name + "_default_" + methodName;                
526                 ArrayList<DValueAst> defs = pClassAst.defaultImplementations.getDefinition(methodName);
527                 
528                 TypeClassMethod method = typeClass.methods.get(methodName);
529                 if(method == null) {
530                     errorLog.log(defs.get(0).location, "Method " + methodName + " is not defined in this class.");
531                     continue;
532                 }
533                 method.setDefaultImplementation(Name.create(moduleName, fullName));
534                 
535                 valueDefinitionsAst.addDefinitions(fullName, defs);
536                 supplementedTypeAnnotations.add(new SupplementedValueType(defs.get(0).location, fullName, method.getType()));
537                 valueDefinitionsAst.setDerived(fullName);
538             }
539             
540             module.addTypeClass(classAst.name, typeClass);
541         }
542     }
543
544     private TypeTranslationContext createTypeTranslationContext() {
545         return new TypeTranslationContext(compilationContext);
546     }
547     
548     public void processDerivingInstances(ArrayList<DDerivingInstanceAst> derivingInstancesAst,
549             ArrayList<ProcessedDInstanceAst> instancesAst) {
550         for(DDerivingInstanceAst derivingInstance : derivingInstancesAst) {
551             String name = derivingInstance.name.name;
552             TCon con;
553             try {
554                 con = Environments.getTypeClassName(compilationContext.environment, name);
555             } catch (AmbiguousNameException e) {
556                 errorLog.log(derivingInstance.name.location, e.getMessage());
557                 continue;
558             }
559             if(con == null) {
560                 errorLog.log(derivingInstance.name.location, "Couldn't resolve class " + name + ".");
561                 continue;
562             }
563             InstanceDeriver deriver = InstanceDerivers.get(con);
564             if(deriver == null)
565                 errorLog.log(derivingInstance.location, "Doesn't know how to derive " + name + ".");
566             else
567                 deriver.derive(errorLog, compilationContext.environment, instancesAst, derivingInstance);
568         }
569     }
570     
571     public void processInstances(ArrayList<ProcessedDInstanceAst> instancesAst) {
572         THashSet<TPred> instanceClashCheckSet = new THashSet<TPred>(); 
573         for(ProcessedDInstanceAst pInstanceAst : instancesAst) {
574             DInstanceAst instanceAst = pInstanceAst.orig;
575             try {
576                 TypeTranslationContext context = createTypeTranslationContext();
577                 
578                 String name = instanceAst.name.name;
579                 TCon typeClassCon;
580                 try {
581                     typeClassCon = Environments.getTypeClassName(compilationContext.environment, name);
582                 } catch (AmbiguousNameException e) {
583                     errorLog.log(instanceAst.name.location, e.getMessage());
584                     continue;
585                 }
586                 if(typeClassCon == null) {
587                     errorLog.log(instanceAst.name.location, "Couldn't resolve class " + name + ".");
588                     continue;
589                 }
590                 TypeClass typeClass = compilationContext.environment.getTypeClass(typeClassCon);
591                 pInstanceAst.typeClass = typeClass;
592                 
593                 if(instanceAst.types.length != typeClass.parameters.length) {
594                     errorLog.log(instanceAst.location, "Wrong number of parameters to type class " + typeClassCon.name + ".");
595                     continue;
596                 }
597                 Type[] parameters = new Type[instanceAst.types.length];
598                 for(int i=0;i<parameters.length;++i)
599                     parameters[i] = context.toType(instanceAst.types[i], typeClass.parameters[i].getKind() /* FIXME */);
600                 TPred instance = Types.pred(typeClassCon, parameters);
601                 
602                 if(!instanceClashCheckSet.add(instance)) {
603                     errorLog.log(instanceAst.location, "Duplicate definition of the instance " + instance + ".");
604                     continue;
605                 }
606                 
607                 TPred[] instanceContext = new TPred[instanceAst.context.length];
608                 for(int i=0;i<instanceContext.length;++i)
609                     instanceContext[i] = context.toTFuncApply(instanceAst.context[i]);
610                 
611                 String javaName = compilationContext.namingPolicy.getInstanceClassName(instance);
612                 String instancePrefix = instance.toName() + "$";
613     
614                 ValueRepository valueDefs = pInstanceAst.valueDefs;
615     
616                 THashMap<String, MethodImplementation> methodImplementations =
617                         new THashMap<String, MethodImplementation>();
618                 for(String valueName : valueDefs.getValueNames()) {
619                     String fullName = instancePrefix + valueName;
620                     long loc = valueDefs.getDefinition(valueName).get(0).location;
621                     valueDefinitionsAst.addFrom(valueDefs, valueName, fullName);
622                     valueDefinitionsAst.setDerived(fullName);
623                     /*valueDefinitionsAst.addAnnotation(fullName, new DAnnotationAst(new EVar("@private"), 
624                             Collections.<Expression>emptyList()));*/
625                     TypeClassMethod method = typeClass.methods.get(valueName);
626                     if(method == null) {
627                         errorLog.log(loc, "Method " + valueName + " is not defined in the type class " + typeClass.name.name + ".");
628                         continue;
629                     }
630                     Type type = method.getBaseType().replace(typeClass.parameters, parameters);
631                     for(int i=instanceContext.length-1;i>=0;--i)
632                         type = Types.constrained(instanceContext[i], type);
633                     //System.out.println(valueName + " :: " + type);
634                     supplementedTypeAnnotations.add(
635                             new SupplementedValueType(loc, fullName, type));
636                     methodImplementations.put(valueName,
637                             new MethodImplementation(Name.create(moduleName, fullName), false));
638                 }
639                 
640                 // Are all necessary methods defined
641                 for(String methodName : typeClass.methods.keySet())
642                     if(!methodImplementations.containsKey(methodName)) {
643                         Name defaultImplementation = typeClass.methods.get(methodName).getDefaultImplementation();
644                         if(defaultImplementation == null)
645                             errorLog.log(instanceAst.location, "Method " + methodName + " is not defined.");
646                         else
647                             methodImplementations.put(methodName, 
648                                     new MethodImplementation(defaultImplementation, true));
649                     }
650                 
651                 JavaTypeInstanceConstructor generator = new JavaTypeInstanceConstructor(javaName, instance, instanceContext);
652                 if(instanceContext.length == 0)
653                     generator.setHasStaticInstance(true);
654                 
655                 TypeClassInstance typeClassInstance = 
656                         new TypeClassInstance(instanceAst.location, typeClass, generator,
657                                 generator.getTypeParameters(),
658                                 instanceContext, instance, methodImplementations,
659                                 javaName);
660                 
661                 generator.setInstance(typeClassInstance);
662                 
663                 module.addTypeClassInstance(typeClassCon, typeClassInstance);
664             } catch(RuntimeException e) {
665                 errorLog.setExceptionPosition(instanceAst.location);
666                 throw e;
667             }
668         }
669         
670         // Generate superclass functions
671         for(ArrayList<TypeClassInstance> instanceArray : module.getTypeInstances().values())
672             for(TypeClassInstance instance : instanceArray) {
673                 try {
674                     TypeClass typeClass = instance.typeClass;
675                     int superCount = typeClass.context.length;
676                     
677                     instance.superExpressions = new SCLValue[superCount];
678                     for(int i=0;i<superCount;++i) {
679                         TPred type = (TPred)typeClass.context[i]
680                                 .replace(typeClass.parameters, instance.instance.parameters);
681                         SCLValue value = new SCLValue(Name.create(moduleName, 
682                                 instance.javaName.substring(instance.javaName.indexOf('$')+1) + "_super" + i));
683                         //value.addProperty(PrivateProperty.INSTANCE);
684                         module.addValue(value);
685                         Expression expression = new EGetConstraint(instance.location, type);
686                         value.setExpression(expression);
687                         value.setType(Types.forAll(instance.generatorParameters, Types.constrained(instance.context, type)));
688                         value.getProperties().add(new InlineProperty(instance.context.length, 0xffffffff));
689                         //TypeUnparsingContext tuc = new TypeUnparsingContext();
690                         // TODO error handling
691                         instance.superExpressions[i] = value;
692                     }
693                 } catch(RuntimeException e) {
694                     errorLog.setExceptionPosition(instance.getLocation());
695                     throw e;
696                 }
697             }
698     }
699     
700     public void processJavaMethods(ArrayList<JavaMethodDeclaration> javaMethodDeclarations) {
701         for(JavaMethodDeclaration javaMethod : javaMethodDeclarations) {
702             String name = javaMethod.methodName.name;
703             String javaName = name;
704             ArrayList<DAnnotationAst> annotations = 
705                     valueDefinitionsAst.getAnnotations(name);
706             boolean isPrivate = false;
707             if(annotations != null) {
708                 for(DAnnotationAst annotation : annotations)
709                     if(annotation.id.text.equals("@JavaName")) {
710                         String temp = AnnotationUtils.processStringAnnotation(errorLog, annotation);
711                         if(temp != null)
712                             javaName = temp;
713                     }
714                     else if(annotation.id.text.equals("@private")) {
715                         AnnotationUtils.processTagAnnotation(errorLog, annotation);
716                         isPrivate = true;
717                     }
718             }
719             if(exportMap != null)
720                 isPrivate = exportMap.remove(name) == null;
721             
722             Type type = createTypeTranslationContext().toType(javaMethod.type);
723
724             CallJava callJava = resolveMethod(
725                     javaMethod.location,
726                     javaMethod.className,
727                     javaName,
728                     type);
729             if(callJava != null) {
730                 NameExistenceChecks.checkIfValueExists(errorLog, javaMethod.location,
731                         importedEnvironment, module, name);
732                 SCLValue value = module.addValue(name, callJava);
733                 value.definitionLocation = javaMethod.methodName.location;
734                 if(isPrivate)
735                     value.addProperty(PrivateProperty.INSTANCE);
736             }
737         }
738     }
739     
740     public void processRulesets(ArrayList<DRulesetAst> rulesetsAst) {
741         for(DRulesetAst ruleset : rulesetsAst) {
742             String constructorName = "create" + ruleset.name;
743             supplementedTypeAnnotations.add(new SupplementedValueType(ruleset.location, constructorName, Types.functionE(Types.PUNIT, Types.PROC, ruleset.type)));
744             try {
745                 valueDefinitionsAst.add(new DValueAst(new EVar(constructorName), new EPreCHRRulesetConstructor(ruleset)));
746             } catch (NotPatternException e) {
747                 throw new InternalCompilerError(ruleset.location, e);
748             }
749         }
750     }
751     
752     /**
753      * Convert a java class method into a {@link CallJava} instance.
754      * Compilation errors are logged for failures in finding the named class or in accessing the method.
755      * The return value for errors is {@code null}.
756      * 
757      * @param loc  SCL source location
758      * @param className  Name of the class
759      * @param methodName  Name of the method
760      * @param type  The expected SCL type of the method 
761      * @return  A new JavaCall instance, or null if the given method can not be instantiated.
762      */
763     private CallJava resolveMethod(long loc,
764             String className, String methodName, Type type) {
765         // Resolve class
766         ClassRef classRef = classRefs.get(className);
767         if(classRef == null) {
768             if(classRefs.containsKey(className))
769                 return null;
770             classRef = javaReferenceValidator.getClassRef(className);
771             classRefs.put(className, classRef);
772             if(classRef == null) {
773                 errorLog.log(loc, "Didn't find class " + className + ".");
774                 return null;
775             }
776             if(!javaReferenceValidator.isPublic(classRef.getClass())) {
777                 errorLog.log(loc, "Class " + className + " is not public.");
778                 return null;
779             }
780             
781         }
782         
783         // Resolve method
784         List<MethodRef> methodRefs = classRef.getMethodRefs(methodName);
785         if(methodRefs.isEmpty()) {
786             errorLog.log(loc, "Didn't find any public method or field with name '" + methodName + "' from class " + className + ".");
787             return null;
788         }
789         
790         ArrayList<CallJava> candidates = new ArrayList<CallJava>(2); 
791         for(MethodRef methodRef : methodRefs) {
792             CallJava candidate = matchType(methodRef, type);
793             if(candidate != null)
794                 candidates.add(candidate);
795         }
796
797         if(candidates.isEmpty()) {
798             StringBuilder b = new StringBuilder();
799             b.append("Didn't find any public method or field matching the type " + type + ". The following methods and fields were tried:");
800             for(MethodRef methodRef : methodRefs) {
801                 b.append("\n    ");
802                 b.append(methodRef);
803             }
804             b.append("\nbut expected something like:");
805             {
806                 MultiFunction mFun = Types.matchFunction(type);
807                 b.append("\n    public static ");
808                 b.append(javaTypeTranslator.toTypeDesc(mFun.returnType).getFullName()).append(' ').append(methodName).append('(');
809                 boolean first = true;
810                 for(Type parameter : mFun.parameterTypes) {
811                     if(first)
812                         first = false;
813                     else
814                         b.append(", ");
815                     b.append(javaTypeTranslator.toTypeDesc(parameter).getFullName());
816                 }
817                 b.append(')');
818             }
819             errorLog.log(loc, b.toString());
820             return null;
821         }
822         else if(candidates.size() == 1)
823             return candidates.get(0);
824         else {
825             // Try to find the best match
826             ArrayList<CallJava> bestCandidates = new ArrayList<CallJava>(candidates.size());
827             loop: for(CallJava candidate : candidates) {
828                 Iterator<CallJava> it = bestCandidates.iterator();
829                 while(it.hasNext()) {
830                     CallJava b = it.next();
831                     switch(candidate.compareTo(javaReferenceValidator, b)) {
832                     case CallJava.LESS:
833                     case CallJava.EQUAL:
834                         continue loop;
835                     case CallJava.GREATER:
836                         it.remove();
837                         break;
838                     case CallJava.INCOMPARABLE:
839                         break;
840                     }
841                 }
842                 bestCandidates.add(candidate);
843             }
844             
845             if(bestCandidates.size() == 1) {
846                 // System.out.println("Choose: " + bestCandidates.get(0).getMethodRef());
847                 return bestCandidates.get(0);
848             }
849             else { 
850                 StringBuilder b = new StringBuilder();
851                 b.append("Found more than one incomparable public methods the type " + type + ":");
852                 for(CallJava callJava : bestCandidates) {
853                     b.append("\n    ");
854                     b.append(callJava.getMethodRef());
855                 }
856                 errorLog.log(loc, b.toString());
857                 return null;
858             }
859         }
860     }
861
862     private CallJava matchType(MethodRef methodRef, Type type) {
863         //System.out.println("-----------------------------------------------------");
864         //System.out.println("method: " + methodRef);
865         //System.out.println("type: " + type);
866
867         type = Types.canonical(type);
868         while(type instanceof TForAll)
869             type = Types.canonical(((TForAll)type).type);
870             
871         TypeDesc[] expectedParameterTypes = methodRef.getParameterTypes();
872         
873         ArrayList<Type> parameterTypes = new ArrayList<Type>();
874         Type effect = Types.NO_EFFECTS;
875         Type returnType = type;
876         
877         StackItem[] stackItems = new StackItem[expectedParameterTypes.length];
878         TIntArrayList unresolvedItems = new TIntArrayList();
879         
880         loop:
881         for(int i=0;i<expectedParameterTypes.length;++i) {
882             Type parameterType;
883             TypeDesc providedParameterType;
884             do {
885                 if(effect != Types.NO_EFFECTS || !(returnType instanceof TFun)) {
886                     while(i < expectedParameterTypes.length)
887                         unresolvedItems.add(i++);
888                     break loop;
889                 }
890                 TFun fun = (TFun)returnType;
891                 parameterType = Types.canonical(fun.domain);
892                 parameterTypes.add(parameterType);
893                 effect = Types.canonical(fun.effect);
894                 returnType = Types.canonical(fun.range);
895                 providedParameterType = javaTypeTranslator.toTypeDesc(parameterType);
896             } while(providedParameterType.equals(TypeDesc.VOID));
897             
898             while(true) {
899                 TypeDesc expectedParameterType = expectedParameterTypes[i];
900                 if(javaReferenceValidator.isAssignableFrom(expectedParameterType,
901                         providedParameterType)) {
902                     stackItems[i] = new ParameterStackItem(
903                             parameterTypes.size()-1, 
904                             parameterType);
905                     break;
906                 }
907                 else
908                     unresolvedItems.add(i++);
909                 if(i == expectedParameterTypes.length) {
910                     parameterTypes.remove(parameterTypes.size()-1);
911                     returnType = Types.functionE(parameterType, effect, returnType);
912                     break;
913                 }
914             } 
915         }
916         //System.out.println("returnType: " + returnType);
917         if(!unresolvedItems.isEmpty()) {
918             ArrayList<TCon> concreteEffects = new ArrayList<TCon>();
919             effect.collectConcreteEffects(concreteEffects);
920             
921             for(TCon eff : concreteEffects) {
922                 EffectConstructor effC = compilationContext.environment.getEffectConstructor(eff);
923                 for(ThreadLocalVariable var : effC.getThreadLocalVariables()) {
924                     for(int i=0;i<unresolvedItems.size();++i) {
925                         int id = unresolvedItems.get(i);
926                         if(!expectedParameterTypes[id].equals(TypeDesc.OBJECT) &&
927                                 javaReferenceValidator.isAssignableFrom(expectedParameterTypes[id], var.type)) {
928                             stackItems[id] = new ThreadLocalStackItem(var.variableName, var.type);
929                             unresolvedItems.removeAt(i);
930                             --i;
931                         }
932                     }
933                 }
934             }
935             
936             if(!unresolvedItems.isEmpty())
937                 return null;
938             /*System.out.print("Unresolved: ");
939             boolean first = true;
940             for(int i=0;i<unresolvedItems.size();++i) {
941                 if(first)
942                     first = false;
943                 else
944                     System.out.print(", ");
945                 System.out.print(expectedParameterTypes[unresolvedItems.get(i)]);
946             }
947             System.out.println();*/            
948         }   
949         OutputFilter filter = null;
950         {
951             TypeDesc providedReturnType = methodRef.getReturnType();
952             if(!providedReturnType.equals(Constants.FUNCTION)) {
953                 while(returnType instanceof TFun && effect == Types.NO_EFFECTS) {
954                     TFun fun = (TFun)returnType;
955                     Type parameterType = Types.canonical(fun.domain);
956                     if(!javaTypeTranslator.toTypeDesc(parameterType).equals(TypeDesc.VOID))
957                         return null;                    
958                     parameterTypes.add(parameterType);
959                     effect = Types.canonical(fun.effect);
960                     returnType = Types.canonical(fun.range);
961                 }
962             }
963             TypeDesc expectedReturnType = 
964                     javaTypeTranslator.toTypeDesc(returnType);
965             if(!javaReferenceValidator.isAssignableFrom(expectedReturnType,
966                     providedReturnType)) {
967                 if(expectedReturnType.equals(TypeDesc.VOID)) {
968                     if(providedReturnType.equals(TypeDesc.DOUBLE) || providedReturnType.equals(TypeDesc.LONG))
969                         filter = Pop2OutputFilter.INSTANCE;
970                     else
971                         filter = PopOutputFilter.INSTANCE;
972                 }
973                 else if(expectedReturnType.equals(Constants.LIST)
974                         && providedReturnType.equals(Constants.COLLECTION))
975                     filter = ConvertToListFilter.INSTANCE;
976                 else
977                     return null;
978             }
979         }
980         
981         CallJava result = new CallJava(
982                 Types.freeVarsArray(type),
983                 effect,
984                 returnType,
985                 parameterTypes.toArray(new Type[parameterTypes.size()]),
986                 stackItems, 
987                 methodRef,
988                 filter);
989         //System.out.println(result.getType());
990         return result;
991     }
992
993     public void processMappingRelations(ArrayList<DMappingRelationAst> mappingRelationsAst) {
994         for(DMappingRelationAst mappingRelation : mappingRelationsAst) {
995             TypeAst[] parameterTypesAst = mappingRelation.parameterTypes;
996             Type[] parameterTypes = new Type[parameterTypesAst.length];
997             TypeTranslationContext typeTranslationContext = createTypeTranslationContext();
998             for(int i=0;i<parameterTypes.length;++i)
999                 parameterTypes[i] = parameterTypesAst[i].toType(typeTranslationContext, Kinds.STAR);
1000             MappingRelation mRel = 
1001                     new MappingRelation(Name.create(moduleName, mappingRelation.name), parameterTypes);
1002             mRel.location = mappingRelation.location;
1003             if(module.addMappingRelation(mRel))
1004                 errorLog.log(mappingRelation.location, "Mapping relation " + mappingRelation.name + 
1005                         " has already been defined.");
1006         }
1007     }
1008
1009     THashMap<String, DRuleAst> ruleAstMap = new THashMap<String, DRuleAst>();
1010     
1011     public void processRules(ArrayList<DRuleAst> rulesAst) {
1012         // Find implied mapping relations
1013         for(DRuleAst ruleA : rulesAst) {
1014             ArrayList<Query> whereSection = ruleA.getSections().get("where");
1015             if(whereSection != null)
1016                 for(Query query : whereSection) {
1017                     if(!(query instanceof QPreGuard))
1018                         continue;
1019                     QPreGuard guard = (QPreGuard)query;
1020                     if(!(guard.guard instanceof EApply))
1021                         continue;
1022                     EApply apply = (EApply)guard.guard;
1023                     if(!(apply.getFunction() instanceof EVar))
1024                         continue;
1025
1026                     String name = ((EVar)apply.getFunction()).name;
1027                     if(module.getMappingRelation(name) != null)
1028                         continue;
1029
1030                     int arity = apply.getParameters().length;
1031                     Type[] parameterTypes = new Type[arity];
1032                     for(int i=0;i<arity;++i)
1033                         parameterTypes[i] = Types.metaVar(Kinds.STAR);
1034                     MappingRelation mRel = 
1035                             new MappingRelation(Name.create(moduleName, name), parameterTypes);
1036                     mRel.location = query.location;
1037                     module.addMappingRelation(mRel);
1038                 }
1039             if(ruleAstMap.put(ruleA.name, ruleA) != null)
1040                 errorLog.log(ruleA.location, "Rule " + ruleA.name + 
1041                         " has already been defined.");
1042         }
1043         
1044         // 
1045         for(String ruleName : ruleAstMap.keySet())
1046             processRule(ruleName);
1047     }
1048     
1049     private TransformationRule processRule(String ruleName) {
1050         TransformationRule rule = module.getRule(ruleName);
1051         if(rule != null)
1052             return rule;
1053         
1054         DRuleAst ruleAst = ruleAstMap.get(ruleName);
1055         if(ruleAst.alreadyProcessing) {
1056             errorLog.log(ruleAst.location, "Cyclic chain of rule extensions.");
1057             return null;
1058         }
1059         
1060         TransformationRule[] extendsRules;
1061         int length = ruleAst.extendsNames.length;
1062         if(length == 0)
1063             extendsRules = TransformationRule.EMPTY_ARRAY;
1064         else {
1065             extendsRules = new TransformationRule[length];
1066             for(int i=0;i<length;++i) {
1067                 try {
1068                     String extendsName = ruleAst.extendsNames[i];
1069                     TransformationRule extendsRule;
1070                     if(ruleAstMap.containsKey(extendsName))
1071                         extendsRule = processRule(extendsName);
1072                     else {
1073                         extendsRule = Environments.getRule(compilationContext.environment, extendsName);
1074                         if(extendsRule == null)
1075                             errorLog.log(ruleAst.location,
1076                                     "Couldn't resolve rule name " + extendsName + ".");
1077                     }
1078                     extendsRules[i] = extendsRule;
1079                 } catch(AmbiguousNameException e) {
1080                     errorLog.log(ruleAst.location, e.getMessage());
1081                 }
1082             }
1083         }
1084         
1085         final THashMap<SectionName, Query[]> sections = new THashMap<SectionName, Query[]>();
1086
1087         final TranslationContext context = createTranslationContext(ruleName);
1088         if(length > 0) {
1089             THashMap<String, Variable> variables = context.getVariables();
1090             for(TransformationRule extendsRule : extendsRules) {
1091                 if(extendsRule == null)
1092                     continue;
1093                 for(Variable var : extendsRule.variables)
1094                     if(variables.put(var.getName(), var) != null) {
1095                         errorLog.log(ruleAst.location, 
1096                                 "Variable " + var.getName() + " is defined in more than one base rule, which is not currently allowed.");
1097                     }
1098             }
1099         }
1100         context.pushExistentialFrame();
1101         ruleAst.getSections().forEachEntry(new TObjectObjectProcedure<String, ArrayList<Query>>() {
1102             @Override
1103             public boolean execute(String sectionNameString, ArrayList<Query> queries) {
1104                 Query[] resolvedQueries = new Query[queries.size()];
1105                 for(int i=0;i<resolvedQueries.length;++i)
1106                     resolvedQueries[i] = queries.get(i).resolve(context);
1107
1108                 SectionName sectionName = SectionName.getSectionName(sectionNameString);
1109                 if(sectionName == null)
1110                     context.getErrorLog().log(queries.get(0).location,
1111                             "Invalid section name " + sectionNameString + ".");
1112                 else
1113                     sections.put(sectionName, resolvedQueries);
1114                 return true;
1115             }
1116         });
1117         
1118         Variable[] variables = context.getVariables().values().toArray(new Variable[context.getVariables().size()]);
1119
1120         rule = new TransformationRule(ruleAst.isAbstract,
1121                 Name.create(moduleName, ruleAst.name),
1122                 extendsRules,
1123                 sections, variables);
1124         rule.location = ruleAst.location;
1125         module.addRule(rule);
1126         return rule;
1127     }
1128
1129     public void addDataTypesToEnvironment() {
1130         for(StandardTypeConstructor dataType : dataTypes) {
1131             int constructorTag = 0;
1132             for(Constructor constructor : dataType.constructors) {
1133                 SCLValue value = new SCLValue(constructor.name);
1134                 value.definitionLocation = constructor.loc;
1135                 SCLConstructor sclConstructor = 
1136                         new SCLConstructor(
1137                                 constructor.name.name,
1138                                 constructor.javaName, 
1139                                 constructor.getTypeVariables(),
1140                                 constructorTag++,
1141                                 constructor.getReturnType(),
1142                                 constructor.fieldNames == null 
1143                                         ? SCLConstructor.DEFAULT_FIELD_NAMES[constructor.getParameterTypes().length] 
1144                                         : constructor.fieldNames,
1145                                 constructor.recordFieldNames,
1146                                 constructor.getParameterTypes());
1147                 if(dataType.constructors.length == 1 && (
1148                         dataType.getTypeDesc() == null ||
1149                         (dataType.constructors[0].javaName != null &&
1150                         dataType.constructors[0].javaName.equals(MethodBuilderBase.getClassName(dataType.getTypeDesc())))))
1151                     sclConstructor.setOnlyConstructor(true);
1152                 value.setValue(sclConstructor);
1153                 value.setType(constructor.getType());
1154                 
1155                 NameExistenceChecks.checkIfValueExists(errorLog, constructor.loc,
1156                         importedEnvironment, module, constructor.name.name);
1157                 if(module.addValue(value)) {
1158                     errorLog.log(constructor.loc,
1159                             "Value " + constructor.name.name + " is already defined.");
1160                 }
1161             }
1162         }
1163     }
1164     
1165     public void addTypeClassesToEnvironment() {
1166         for(TypeClass typeClass : module.getTypeClasses()) {
1167             for(TypeClassMethod method : typeClass.methods.values()) {
1168                 SCLValue value = method.createValue();
1169                 value.definitionLocation = method.location;
1170                 NameExistenceChecks.checkIfValueExists(errorLog, Locations.NO_LOCATION,
1171                         importedEnvironment, module, value.getName().name);
1172
1173                 if(module.addValue(value)) {
1174                     String name = method.getName();
1175                     long location = Locations.NO_LOCATION;
1176                     ArrayList<DValueAst> definitions = valueDefinitionsAst.getDefinition(name);
1177                     if(definitions != null && !definitions.isEmpty())
1178                         location = definitions.get(0).location;
1179                     errorLog.log(location, "Value " + name + " is already defined.");
1180                 }
1181             }
1182         }
1183     }
1184     
1185     public void preprocessValueDefinitions(ArrayList<DValueTypeAst> typeAnnotationsAst) {
1186         for(String name : valueDefinitionsAst.getValueNames()) {
1187             SCLValue value = new SCLValue(Name.create(moduleName, name));
1188             
1189             long location = valueDefinitionsAst.getLocation(name);
1190             NameExistenceChecks.checkIfValueExists(errorLog, location,
1191                     importedEnvironment, module, value.getName().name);
1192             value.definitionLocation = location;
1193             if(module.addValue(value))
1194                 errorLog.log(location, "Value " + name + " is already defined.");
1195             if(valueDefinitionsAst.isDerived(name))
1196                 value.addProperty(DerivedProperty.INSTANCE);
1197         }
1198         for(DValueTypeAst valueTypeAst : typeAnnotationsAst)
1199             for(EVar name : valueTypeAst.names) {
1200                 SCLValue value = module.getValue(name.name); 
1201                 if(value == null) {
1202                     errorLog.log(valueTypeAst.location, name.name + " is not defined.");
1203                     value = new SCLValue(Name.create(moduleName, name.name));
1204                     module.addValue(value);
1205                 }
1206                 value.definitionLocation = name.location;
1207             }
1208         for(String name : relationDefinitionsAst.getRelationNames()) {
1209             ConcreteRelation relation = new ConcreteRelation(name);
1210             module.addRelation(name, relation);
1211         }
1212     }
1213     
1214     public void addValueDefinitionsToEnvironment(ArrayList<DValueTypeAst> typeAnnotationsAst) {
1215         THashMap<String, DValueTypeAst> typeMap = new THashMap<String, DValueTypeAst>();
1216         for(DValueTypeAst valueTypeAst : typeAnnotationsAst)
1217             for(EVar name : valueTypeAst.names) {
1218                 if(typeMap.containsKey(name.name))
1219                     errorLog.log(valueTypeAst.location, "Type of "+name.name+" has already been declared in this module.");
1220                 else
1221                     typeMap.put(name.name, valueTypeAst);
1222             }
1223         
1224         for(String name : valueDefinitionsAst.getValueNames()) {
1225             ArrayList<DValueAst> defs = valueDefinitionsAst.getDefinition(name);
1226             if(defs.size() != 1 || !(defs.get(0).value instanceof EPreCHRRulesetConstructor))
1227                 continue;
1228             try {
1229                 SCLValue value = module.getValue(name);
1230                 TranslationContext context = createTranslationContext(name);
1231                 Expression expression = context.translateCases2(defs);
1232                 value.setExpression(expression);
1233                 
1234                 if(exportMap != null && exportMap.remove(name) == null)
1235                     value.addProperty(PrivateProperty.INSTANCE);
1236             } catch(RuntimeException e) {
1237                 errorLog.setExceptionPosition(defs.get(0).location);
1238                 throw e;
1239             }
1240         }
1241         for(String name : valueDefinitionsAst.getValueNames()) {
1242             ArrayList<DValueAst> defs = valueDefinitionsAst.getDefinition(name);
1243             if(defs.size() == 1 && defs.get(0).value instanceof EPreCHRRulesetConstructor)
1244                 continue;
1245             try {
1246                 SCLValue value = module.getValue(name);
1247                 TranslationContext context = createTranslationContext(name);
1248                 Expression expression = context.translateCases2(defs);
1249                 value.setExpression(expression);
1250                 
1251                 DValueTypeAst valueTypeAst = typeMap.remove(name);
1252                 if(valueTypeAst != null)
1253                     value.setType(Types.closure(context.toType(valueTypeAst.type)));
1254                 
1255                 ArrayList<DAnnotationAst> annotations = valueDefinitionsAst.getAnnotations(name);
1256                 if(annotations != null)
1257                     for(DAnnotationAst annotation : annotations) {
1258                         handleAnnotation(value, defs, annotation);
1259                     }
1260                 if(exportMap != null && exportMap.remove(name) == null)
1261                     value.addProperty(PrivateProperty.INSTANCE);
1262             } catch(RuntimeException e) {
1263                 errorLog.setExceptionPosition(defs.get(0).location);
1264                 throw e;
1265             }
1266         }
1267         for(String name : relationDefinitionsAst.getRelationNames()) {
1268             ArrayList<DRelationAst> definitions = relationDefinitionsAst.getDefinition(name);
1269             if(definitions.size() > 1) {
1270                 errorLog.log(definitions.get(1).location,
1271                         "Does not yet support definition of relations by more than one rule.");
1272                 continue;
1273             }
1274             
1275             DRelationAst definition = definitions.get(0);
1276             ConcreteRelation relation = (ConcreteRelation)module.getRelation(name);
1277             relation.location = definition.location;
1278             TranslationContext context = createTranslationContext(name);
1279             definition.translateTo(context, relation);
1280         }
1281     }
1282     
1283     private TranslationContext createTranslationContext(String definitionName) {
1284         return new TranslationContext(compilationContext, null, definitionName);
1285     }
1286     
1287     private void handleAnnotation(SCLValue value, ArrayList<DValueAst> defs, DAnnotationAst annotation) {
1288         if(annotation.id.text.equals("@macro")) {
1289             value.setMacroRule(new StandardMacroRule());
1290         }
1291         else if(annotation.id.text.equals("@inline")) {
1292             try {
1293                 int arity = defs.get(0).lhs.getFunctionDefinitionPatternArity();
1294                 int phaseMask = 0xffffffff;
1295                 if(annotation.parameters.length > 0) {
1296                     phaseMask = Integer.parseInt(((EIntegerLiteral)annotation.parameters[0]).getValue());
1297                 }
1298                 value.addProperty(new InlineProperty(arity, phaseMask));
1299             } catch(NotPatternException e) {
1300                 errorLog.log(annotation.location, "Inline annotation is invalid: this is not a function.");
1301             }
1302         }
1303         else if(annotation.id.text.equals("@private")) {
1304             if(moduleHeader != null && moduleHeader.export != null)
1305                 errorLog.log(annotation.location, "Annotation @private is not used when module header contains export property.");
1306             value.addProperty(PrivateProperty.INSTANCE);
1307         }
1308         else if(annotation.id.text.equals("@deprecated")) {
1309             String description = "";
1310             if(annotation.parameters.length > 0) {
1311                 if(annotation.parameters.length > 1)
1312                     errorLog.log(annotation.location, "Invalid number of parameters, expected one string.");
1313                 else {
1314                     String temp = AnnotationUtils.extractString(annotation.parameters[0]);
1315                     if(temp == null)
1316                         errorLog.log(annotation.location, "Invalid parameter, expected one string.");
1317                     else
1318                         description = temp;
1319                 }
1320             }
1321             value.addProperty(new DeprecatedProperty(description));
1322         }
1323         else
1324             errorLog.log(annotation.location, "Unknown annotation.");
1325     }
1326
1327     public void addSupplementedTypeAnnotationsToEnvironment() {
1328         for(SupplementedValueType valueType : supplementedTypeAnnotations) {
1329             Type type = Types.closure(valueType.type);
1330             String name = valueType.name;
1331             SCLValue value = module.getValue(name);
1332             if(value == null)
1333                 errorLog.log(valueType.position,
1334                         name + " is not defined.");
1335             else if(value.getType()==null)
1336                 value.setType(type);
1337             else
1338                 errorLog.log(valueType.position, 
1339                         "Type of "+name+" has already been declared in this module.");
1340         }
1341     }
1342     
1343     public void addFixityToEnvironment(ArrayList<DFixityAst> fixityAst) {
1344         for(DFixityAst fixity : fixityAst) {
1345             for(EVar symbol : fixity.symbols) {
1346                 String name = symbol.name;
1347                 SCLValue value = module.getValue(name);
1348                 if(value == null)
1349                     errorLog.log(symbol.location,
1350                             name + " is not defined.");
1351                 else
1352                     value.setPrecedence(fixity.precedence);
1353             }
1354         }
1355     }
1356
1357     public void addCoverageBranchPoints() {
1358         branchPoints = new THashMap<String, BranchPoint[]>();
1359         BranchPointInjector injector = new BranchPointInjector(); 
1360         for(String valueName : valueDefinitionsAst.getValueNames()) {
1361             for(DValueAst valueAst : valueDefinitionsAst.getDefinition(valueName))
1362                 valueAst.value = injector.injectBranchPoint(valueAst.value);
1363             branchPoints.put(valueName, injector.getAndClearBranchPoints());
1364         }
1365     }
1366
1367     public void collectDebugInfo() {
1368         module.moduleDebugInfo = compilationContext.moduleDebugInfo = new ModuleDebugInfo();
1369     }
1370
1371     public void prepareExports() {
1372         if(moduleHeader != null && moduleHeader.export != null) {
1373             exportMap = new THashMap<String, EVar>();
1374             for(EVar export : moduleHeader.export)
1375                 if(exportMap.put(export.name, export) != null)
1376                     errorLog.log(export.location, "The symbol " + export.name + " is exported multiple times.");
1377         }
1378     }
1379     
1380     public void checkExports() {
1381         if(exportMap != null)
1382             for(EVar export : exportMap.values())
1383                     errorLog.log(export.location, "The symbol " + export.name + " is not defined in the module.");
1384     }
1385 }