1 package org.simantics.scl.compiler.compilation;
3 import java.util.ArrayList;
4 import java.util.Iterator;
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;
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;
130 public class Elaboration {
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;
143 ConcreteModule module;
144 Environment importedEnvironment;
145 ArrayList<SupplementedValueType> supplementedTypeAnnotations = new ArrayList<SupplementedValueType>();
147 JavaTypeTranslator javaTypeTranslator;
148 ArrayList<StandardTypeConstructor> dataTypes = new ArrayList<StandardTypeConstructor>();
149 THashMap<String, ClassRef> classRefs = new THashMap<String, ClassRef>();
150 THashMap<String, BranchPoint[]> branchPoints;
152 THashMap<String, EVar> exportMap = null;
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;
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);
186 timer.suspendTimer();
187 importedEnvironment = localEnvironmentFactory.createEnvironment(
189 importsAst.toArray(new ImportDeclaration[importsAst.size()]));
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));
199 for(ImportDeclaration importAst : importsAst)
200 this.module.addDependency(new ImportDeclaration(
201 importAst.moduleName,
202 importAst.reexport ? importAst.localName : null,
205 localEnvironmentFactory.addBuiltinDependencies(module);
206 compilationContext.namingPolicy = new JavaNamingPolicy(moduleName);
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(".")) {
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) {
222 errorLog.log(relativeImport.location, e.getMessage());
226 absoluteImports.add(relativeImport);
228 return absoluteImports;
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);
245 StandardTypeConstructor typeConstructor = new StandardTypeConstructor(
246 Types.con(moduleName, dataType.name), constructorKind);
248 NameExistenceChecks.checkIfTypeExists(errorLog,
249 dataType.location, importedEnvironment, dataType.name);
250 addTypeDescriptor(dataType.location, dataType.name, typeConstructor);
251 dataType.typeConstructor = typeConstructor;
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);
261 for(DEffectAst effect : effectsAst) {
262 EffectConstructor effectConstructor = new EffectConstructor(Types.con(moduleName, effect.name));
263 effectConstructor.addThreadLocalVariable(
264 new ThreadLocalVariable(
266 TypeDesc.forClass(effect.threadLocalType)
268 if(module.addEffectConstructor(effect.name, effectConstructor))
269 errorLog.log(effect.location, "Effect "+effect.name+" has already been defined in this module.");
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);
279 javaTypeTranslator = new JavaTypeTranslator(compilationContext.environment);
280 compilationContext.javaTypeTranslator = javaTypeTranslator;
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.");
288 private static final int[] EMPTY_INT_ARRAY = new int[0];
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()) {
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);
308 int[] result = tempIntSet.toArray();
310 //System.out.println(u + " -> " + Arrays.toString(result));
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);
327 b.append(typeAlias.name);
328 if(Locations.beginOf(typeAlias.location) < Locations.beginOf(minLocation))
329 minLocation = typeAlias.location;
332 errorLog.log(minLocation, b.toString());
335 orderedTypeAliases.add(typeAliasesAst.get(component[0]));
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);
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\".");
369 className = ((StringConstant)((ELiteral)annotation.parameters[0]).getValue()).getValue().replace('.', '/');
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);
381 StandardTypeConstructor dataType = dataTypeAst.typeConstructor;
383 dataType.setType(Types.con(moduleName, dataTypeAst.name), typeParameters);
384 dataType.setConstructors(constructors);
386 dataType.setTypeDesc(TypeDesc.forClass(className));
387 if(!external || dataTypeAst.constructors.length > 0) {
388 dataType.isOpen = false;
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")) {
404 javaName = ((StringConstant)((ELiteral)annotation.parameters[0])
405 .getValue()).getValue();
406 } catch(Exception e) {
407 errorLog.log(annotation.parameters[0].location, "Invalid annotation parameter.");
410 else if(annotation.id.text.equals("@FieldNames")) {
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());
421 } catch(Exception e) {
422 errorLog.log(annotation.parameters[0].location, "Invalid annotation parameter.");
423 componentAccesses = null;
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;
433 if(constructors.length == 1) {
434 Constructor constructor = constructors[0];
435 if(constructor.recordFieldNames != null) {
436 fieldAccessorGenerators.add(new Runnable() {
439 Type in = Types.apply(dataType.name, dataType.parameters);
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];
448 accessor = new SafeCoerce(dataType.parameters, in, out);
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)),
454 module.addFieldAccessor(constructor.recordFieldNames[i], accessor);
462 for(Runnable fieldAccessorGenerator : fieldAccessorGenerators)
463 fieldAccessorGenerator.run();
466 public void processTypeClasses(ArrayList<ProcessedDClassAst> typeClassesAst) {
467 for(ProcessedDClassAst pClassAst : typeClassesAst) {
468 DClassAst classAst = pClassAst.orig;
470 if(module.getTypeClass(classAst.name) != null) {
471 errorLog.log(classAst.location, "Class "+classAst.name+" has already been defined in this module.");
475 TypeTranslationContext context = createTypeTranslationContext();
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]);
481 TVar[] parameters = new TVar[classAst.parameters.length];
483 if(classAst.name.equals("Functor") || classAst.name.equals("Monad")) {
485 parameters[0] = context.resolveTypeVariable(pClassAst.orig.location, classAst.parameters[0], Kinds.STAR_TO_STAR);
488 for(int i=0;i<parameters.length;++i)
489 parameters[i] = context.resolveTypeVariable(pClassAst.orig.location, classAst.parameters[i], Kinds.metaVar());
492 TCon con = Types.con(moduleName, classAst.name);
493 TypeClass typeClass = new TypeClass(classAst.location,
496 compilationContext.namingPolicy.getTypeClassInterfaceName(con),
498 Fundep.mapFundeps(classAst.parameters, classAst.fundeps));
500 THashMap<String, TypeClassMethod> methods = typeClass.methods;
501 for(DValueTypeAst methodDecl : pClassAst.typeDeclarations) {
505 type = context.toType(methodDecl.type);
506 } catch(SCLSyntaxErrorException e) {
507 errorLog.log(e.location, e.getMessage());
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),
519 } catch(RuntimeException e) {
520 errorLog.setExceptionPosition(methodDecl.location);
525 for(String methodName : pClassAst.defaultImplementations.getValueNames()) {
526 String fullName = "_" + classAst.name + "_default_" + methodName;
527 ArrayList<DValueAst> defs = pClassAst.defaultImplementations.getDefinition(methodName);
529 TypeClassMethod method = typeClass.methods.get(methodName);
531 errorLog.log(defs.get(0).location, "Method " + methodName + " is not defined in this class.");
534 method.setDefaultImplementation(Name.create(moduleName, fullName));
536 valueDefinitionsAst.addDefinitions(fullName, defs);
537 supplementedTypeAnnotations.add(new SupplementedValueType(defs.get(0).location, fullName, method.getType()));
538 valueDefinitionsAst.setDerived(fullName);
541 module.addTypeClass(classAst.name, typeClass);
545 private TypeTranslationContext createTypeTranslationContext() {
546 return new TypeTranslationContext(compilationContext);
549 public void processDerivingInstances(ArrayList<DDerivingInstanceAst> derivingInstancesAst,
550 ArrayList<ProcessedDInstanceAst> instancesAst) {
551 for(DDerivingInstanceAst derivingInstance : derivingInstancesAst) {
552 String name = derivingInstance.name.name;
555 con = Environments.getTypeClassName(compilationContext.environment, name);
556 } catch (AmbiguousNameException e) {
557 errorLog.log(derivingInstance.name.location, e.getMessage());
561 errorLog.log(derivingInstance.name.location, "Couldn't resolve class " + name + ".");
564 InstanceDeriver deriver = InstanceDerivers.get(con);
566 errorLog.log(derivingInstance.location, "Doesn't know how to derive " + name + ".");
568 deriver.derive(errorLog, compilationContext.environment, instancesAst, derivingInstance);
572 public void processInstances(ArrayList<ProcessedDInstanceAst> instancesAst) {
573 THashSet<TPred> instanceClashCheckSet = new THashSet<TPred>();
574 for(ProcessedDInstanceAst pInstanceAst : instancesAst) {
575 DInstanceAst instanceAst = pInstanceAst.orig;
577 TypeTranslationContext context = createTypeTranslationContext();
579 String name = instanceAst.name.name;
582 typeClassCon = Environments.getTypeClassName(compilationContext.environment, name);
583 } catch (AmbiguousNameException e) {
584 errorLog.log(instanceAst.name.location, e.getMessage());
587 if(typeClassCon == null) {
588 errorLog.log(instanceAst.name.location, "Couldn't resolve class " + name + ".");
591 TypeClass typeClass = compilationContext.environment.getTypeClass(typeClassCon);
592 pInstanceAst.typeClass = typeClass;
594 if(instanceAst.types.length != typeClass.parameters.length) {
595 errorLog.log(instanceAst.location, "Wrong number of parameters to type class " + typeClassCon.name + ".");
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);
603 if(!instanceClashCheckSet.add(instance)) {
604 errorLog.log(instanceAst.location, "Duplicate definition of the instance " + instance + ".");
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]);
612 String javaName = compilationContext.namingPolicy.getInstanceClassName(instance);
613 String instancePrefix = instance.toName() + "$";
615 ValueRepository valueDefs = pInstanceAst.valueDefs;
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);
628 errorLog.log(loc, "Method " + valueName + " is not defined in the type class " + typeClass.name.name + ".");
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));
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.");
648 methodImplementations.put(methodName,
649 new MethodImplementation(defaultImplementation, true));
652 JavaTypeInstanceConstructor generator = new JavaTypeInstanceConstructor(javaName, instance, instanceContext);
653 if(instanceContext.length == 0)
654 generator.setHasStaticInstance(true);
656 TypeClassInstance typeClassInstance =
657 new TypeClassInstance(instanceAst.location, typeClass, generator,
658 generator.getTypeParameters(),
659 instanceContext, instance, methodImplementations,
662 generator.setInstance(typeClassInstance);
664 module.addTypeClassInstance(typeClassCon, typeClassInstance);
665 } catch(RuntimeException e) {
666 errorLog.setExceptionPosition(instanceAst.location);
671 // Generate superclass functions
672 for(ArrayList<TypeClassInstance> instanceArray : module.getTypeInstances().values())
673 for(TypeClassInstance instance : instanceArray) {
675 TypeClass typeClass = instance.typeClass;
676 int superCount = typeClass.context.length;
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;
694 } catch(RuntimeException e) {
695 errorLog.setExceptionPosition(instance.getLocation());
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);
715 else if(annotation.id.text.equals("@private")) {
716 AnnotationUtils.processTagAnnotation(errorLog, annotation);
720 if(exportMap != null)
721 isPrivate = exportMap.remove(name) == null;
723 Type type = createTypeTranslationContext().toType(javaMethod.type);
725 CallJava callJava = resolveMethod(
727 javaMethod.className,
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;
736 value.addProperty(PrivateProperty.INSTANCE);
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)));
746 valueDefinitionsAst.add(new DValueAst(new EVar(constructorName), new EPreCHRRulesetConstructor(ruleset)));
747 } catch (NotPatternException e) {
748 throw new InternalCompilerError(ruleset.location, e);
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}.
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.
764 private CallJava resolveMethod(long loc,
765 String className, String methodName, Type type) {
767 ClassRef classRef = classRefs.get(className);
768 if(classRef == null) {
769 if(classRefs.containsKey(className))
771 classRef = javaReferenceValidator.getClassRef(className);
772 classRefs.put(className, classRef);
773 if(classRef == null) {
774 errorLog.log(loc, "Didn't find class " + className + ".");
777 if(!javaReferenceValidator.isPublic(classRef.getClass())) {
778 errorLog.log(loc, "Class " + className + " is not public.");
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 + ".");
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);
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) {
805 b.append("\nbut expected something like:");
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) {
816 b.append(javaTypeTranslator.toTypeDesc(parameter).getFullName());
820 errorLog.log(loc, b.toString());
823 else if(candidates.size() == 1)
824 return candidates.get(0);
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)) {
836 case CallJava.GREATER:
839 case CallJava.INCOMPARABLE:
843 bestCandidates.add(candidate);
846 if(bestCandidates.size() == 1) {
847 // System.out.println("Choose: " + bestCandidates.get(0).getMethodRef());
848 return bestCandidates.get(0);
851 StringBuilder b = new StringBuilder();
852 b.append("Found more than one incomparable public methods the type " + type + ":");
853 for(CallJava callJava : bestCandidates) {
855 b.append(callJava.getMethodRef());
857 errorLog.log(loc, b.toString());
863 private CallJava matchType(MethodRef methodRef, Type type) {
864 //System.out.println("-----------------------------------------------------");
865 //System.out.println("method: " + methodRef);
866 //System.out.println("type: " + type);
868 type = Types.canonical(type);
869 while(type instanceof TForAll)
870 type = Types.canonical(((TForAll)type).type);
872 TypeDesc[] expectedParameterTypes = methodRef.getParameterTypes();
874 ArrayList<Type> parameterTypes = new ArrayList<Type>();
875 Type effect = Types.NO_EFFECTS;
876 Type returnType = type;
878 StackItem[] stackItems = new StackItem[expectedParameterTypes.length];
879 TIntArrayList unresolvedItems = new TIntArrayList();
882 for(int i=0;i<expectedParameterTypes.length;++i) {
884 TypeDesc providedParameterType;
886 if(effect != Types.NO_EFFECTS || !(returnType instanceof TFun)) {
887 while(i < expectedParameterTypes.length)
888 unresolvedItems.add(i++);
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));
900 TypeDesc expectedParameterType = expectedParameterTypes[i];
901 if(javaReferenceValidator.isAssignableFrom(expectedParameterType,
902 providedParameterType)) {
903 stackItems[i] = new ParameterStackItem(
904 parameterTypes.size()-1,
909 unresolvedItems.add(i++);
910 if(i == expectedParameterTypes.length) {
911 parameterTypes.remove(parameterTypes.size()-1);
912 returnType = Types.functionE(parameterType, effect, returnType);
917 //System.out.println("returnType: " + returnType);
918 if(!unresolvedItems.isEmpty()) {
919 ArrayList<TCon> concreteEffects = new ArrayList<TCon>();
920 effect.collectConcreteEffects(concreteEffects);
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);
937 if(!unresolvedItems.isEmpty())
939 /*System.out.print("Unresolved: ");
940 boolean first = true;
941 for(int i=0;i<unresolvedItems.size();++i) {
945 System.out.print(", ");
946 System.out.print(expectedParameterTypes[unresolvedItems.get(i)]);
948 System.out.println();*/
950 OutputFilter filter = null;
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))
959 parameterTypes.add(parameterType);
960 effect = Types.canonical(fun.effect);
961 returnType = Types.canonical(fun.range);
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;
972 filter = PopOutputFilter.INSTANCE;
974 else if(expectedReturnType.equals(Constants.LIST)
975 && providedReturnType.equals(Constants.COLLECTION))
976 filter = ConvertToListFilter.INSTANCE;
982 CallJava result = new CallJava(
983 Types.freeVarsArray(type),
986 parameterTypes.toArray(new Type[parameterTypes.size()]),
990 //System.out.println(result.getType());
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.");
1010 THashMap<String, DRuleAst> ruleAstMap = new THashMap<String, DRuleAst>();
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))
1020 QPreGuard guard = (QPreGuard)query;
1021 if(!(guard.guard instanceof EApply))
1023 EApply apply = (EApply)guard.guard;
1024 if(!(apply.getFunction() instanceof EVar))
1027 String name = ((EVar)apply.getFunction()).name;
1028 if(module.getMappingRelation(name) != null)
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);
1040 if(ruleAstMap.put(ruleA.name, ruleA) != null)
1041 errorLog.log(ruleA.location, "Rule " + ruleA.name +
1042 " has already been defined.");
1046 for(String ruleName : ruleAstMap.keySet())
1047 processRule(ruleName);
1050 private TransformationRule processRule(String ruleName) {
1051 TransformationRule rule = module.getRule(ruleName);
1055 DRuleAst ruleAst = ruleAstMap.get(ruleName);
1056 if(ruleAst.alreadyProcessing) {
1057 errorLog.log(ruleAst.location, "Cyclic chain of rule extensions.");
1061 TransformationRule[] extendsRules;
1062 int length = ruleAst.extendsNames.length;
1064 extendsRules = TransformationRule.EMPTY_ARRAY;
1066 extendsRules = new TransformationRule[length];
1067 for(int i=0;i<length;++i) {
1069 String extendsName = ruleAst.extendsNames[i];
1070 TransformationRule extendsRule;
1071 if(ruleAstMap.containsKey(extendsName))
1072 extendsRule = processRule(extendsName);
1074 extendsRule = Environments.getRule(compilationContext.environment, extendsName);
1075 if(extendsRule == null)
1076 errorLog.log(ruleAst.location,
1077 "Couldn't resolve rule name " + extendsName + ".");
1079 extendsRules[i] = extendsRule;
1080 } catch(AmbiguousNameException e) {
1081 errorLog.log(ruleAst.location, e.getMessage());
1086 final THashMap<SectionName, Query[]> sections = new THashMap<SectionName, Query[]>();
1088 final TranslationContext context = createTranslationContext(ruleName);
1090 THashMap<String, Variable> variables = context.getVariables();
1091 for(TransformationRule extendsRule : extendsRules) {
1092 if(extendsRule == null)
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.");
1101 context.pushExistentialFrame();
1102 ruleAst.getSections().forEachEntry(new TObjectObjectProcedure<String, ArrayList<Query>>() {
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);
1109 SectionName sectionName = SectionName.getSectionName(sectionNameString);
1110 if(sectionName == null)
1111 context.getErrorLog().log(queries.get(0).location,
1112 "Invalid section name " + sectionNameString + ".");
1114 sections.put(sectionName, resolvedQueries);
1119 Variable[] variables = context.getVariables().values().toArray(new Variable[context.getVariables().size()]);
1121 rule = new TransformationRule(ruleAst.isAbstract,
1122 Name.create(moduleName, ruleAst.name),
1124 sections, variables);
1125 rule.location = ruleAst.location;
1126 module.addRule(rule);
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 =
1139 constructor.name.name,
1140 constructor.javaName,
1141 constructor.getTypeVariables(),
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());
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.");
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);
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.");
1186 public void preprocessValueDefinitions(ArrayList<DValueTypeAst> typeAnnotationsAst) {
1187 for(String name : valueDefinitionsAst.getValueNames()) {
1188 SCLValue value = new SCLValue(Name.create(moduleName, name));
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);
1199 for(DValueTypeAst valueTypeAst : typeAnnotationsAst)
1200 for(EVar name : valueTypeAst.names) {
1201 SCLValue value = module.getValue(name.name);
1203 errorLog.log(valueTypeAst.location, name.name + " is not defined.");
1204 value = new SCLValue(Name.create(moduleName, name.name));
1205 module.addValue(value);
1207 value.definitionLocation = name.location;
1209 for(String name : relationDefinitionsAst.getRelationNames()) {
1210 ConcreteRelation relation = new ConcreteRelation(name);
1211 module.addRelation(name, relation);
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.");
1222 typeMap.put(name.name, valueTypeAst);
1225 for(String name : valueDefinitionsAst.getValueNames()) {
1226 ArrayList<DValueAst> defs = valueDefinitionsAst.getDefinition(name);
1227 if(defs.size() != 1 || !(defs.get(0).value instanceof EPreCHRRulesetConstructor))
1230 SCLValue value = module.getValue(name);
1231 TranslationContext context = createTranslationContext(name);
1232 Expression expression = context.translateCases2(defs);
1233 value.setExpression(expression);
1235 if(exportMap != null && exportMap.remove(name) == null)
1236 value.addProperty(PrivateProperty.INSTANCE);
1237 } catch(RuntimeException e) {
1238 errorLog.setExceptionPosition(defs.get(0).location);
1242 for(String name : valueDefinitionsAst.getValueNames()) {
1243 ArrayList<DValueAst> defs = valueDefinitionsAst.getDefinition(name);
1244 if(defs.size() == 1 && defs.get(0).value instanceof EPreCHRRulesetConstructor)
1247 SCLValue value = module.getValue(name);
1248 TranslationContext context = createTranslationContext(name);
1249 Expression expression = context.translateCases2(defs);
1250 value.setExpression(expression);
1252 DValueTypeAst valueTypeAst = typeMap.remove(name);
1253 if(valueTypeAst != null)
1254 value.setType(Types.closure(context.toType(valueTypeAst.type)));
1256 ArrayList<DAnnotationAst> annotations = valueDefinitionsAst.getAnnotations(name);
1257 if(annotations != null)
1258 for(DAnnotationAst annotation : annotations) {
1259 handleAnnotation(value, defs, annotation);
1261 if(exportMap != null && exportMap.remove(name) == null)
1262 value.addProperty(PrivateProperty.INSTANCE);
1263 } catch(RuntimeException e) {
1264 errorLog.setExceptionPosition(defs.get(0).location);
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.");
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);
1284 private TranslationContext createTranslationContext(String definitionName) {
1285 return new TranslationContext(compilationContext, null, definitionName);
1288 private void handleAnnotation(SCLValue value, ArrayList<DValueAst> defs, DAnnotationAst annotation) {
1289 if(annotation.id.text.equals("@macro")) {
1290 value.setMacroRule(new StandardMacroRule());
1292 else if(annotation.id.text.equals("@inline")) {
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());
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.");
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);
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.");
1315 String temp = AnnotationUtils.extractString(annotation.parameters[0]);
1317 errorLog.log(annotation.location, "Invalid parameter, expected one string.");
1322 value.addProperty(new DeprecatedProperty(description));
1325 errorLog.log(annotation.location, "Unknown annotation.");
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);
1334 errorLog.log(valueType.position,
1335 name + " is not defined.");
1336 else if(value.getType()==null)
1337 value.setType(type);
1339 errorLog.log(valueType.position,
1340 "Type of "+name+" has already been declared in this module.");
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);
1350 errorLog.log(symbol.location,
1351 name + " is not defined.");
1353 value.setPrecedence(fixity.precedence);
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());
1368 public void collectDebugInfo() {
1369 module.moduleDebugInfo = compilationContext.moduleDebugInfo = new ModuleDebugInfo();
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.");
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.");