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