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.names.Name;
10 import org.simantics.scl.compiler.constants.JavaTypeInstanceConstructor;
11 import org.simantics.scl.compiler.constants.SCLConstructor;
12 import org.simantics.scl.compiler.constants.StringConstant;
13 import org.simantics.scl.compiler.constants.generic.CallJava;
14 import org.simantics.scl.compiler.constants.generic.ClassRef;
15 import org.simantics.scl.compiler.constants.generic.ConvertToListFilter;
16 import org.simantics.scl.compiler.constants.generic.MethodRef;
17 import org.simantics.scl.compiler.constants.generic.OutputFilter;
18 import org.simantics.scl.compiler.constants.generic.ParameterStackItem;
19 import org.simantics.scl.compiler.constants.generic.Pop2OutputFilter;
20 import org.simantics.scl.compiler.constants.generic.PopOutputFilter;
21 import org.simantics.scl.compiler.constants.generic.StackItem;
22 import org.simantics.scl.compiler.constants.generic.ThreadLocalStackItem;
23 import org.simantics.scl.compiler.elaboration.contexts.TranslationContext;
24 import org.simantics.scl.compiler.elaboration.contexts.TypeTranslationContext;
25 import org.simantics.scl.compiler.elaboration.errors.NotPatternException;
26 import org.simantics.scl.compiler.elaboration.expressions.EApply;
27 import org.simantics.scl.compiler.elaboration.expressions.EGetConstraint;
28 import org.simantics.scl.compiler.elaboration.expressions.EIntegerLiteral;
29 import org.simantics.scl.compiler.elaboration.expressions.EListLiteral;
30 import org.simantics.scl.compiler.elaboration.expressions.ELiteral;
31 import org.simantics.scl.compiler.elaboration.expressions.EVar;
32 import org.simantics.scl.compiler.elaboration.expressions.Expression;
33 import org.simantics.scl.compiler.elaboration.expressions.Variable;
34 import org.simantics.scl.compiler.elaboration.fundeps.Fundep;
35 import org.simantics.scl.compiler.elaboration.java.JavaMethodDeclaration;
36 import org.simantics.scl.compiler.elaboration.macros.StandardMacroRule;
37 import org.simantics.scl.compiler.elaboration.modules.InlineProperty;
38 import org.simantics.scl.compiler.elaboration.modules.MethodImplementation;
39 import org.simantics.scl.compiler.elaboration.modules.PrivateProperty;
40 import org.simantics.scl.compiler.elaboration.modules.SCLValue;
41 import org.simantics.scl.compiler.elaboration.modules.TypeAlias;
42 import org.simantics.scl.compiler.elaboration.modules.TypeClass;
43 import org.simantics.scl.compiler.elaboration.modules.TypeClassInstance;
44 import org.simantics.scl.compiler.elaboration.modules.TypeClassMethod;
45 import org.simantics.scl.compiler.elaboration.query.Query;
46 import org.simantics.scl.compiler.elaboration.query.pre.QPreGuard;
47 import org.simantics.scl.compiler.elaboration.relations.ConcreteRelation;
48 import org.simantics.scl.compiler.elaboration.rules.MappingRelation;
49 import org.simantics.scl.compiler.elaboration.rules.SectionName;
50 import org.simantics.scl.compiler.elaboration.rules.TransformationRule;
51 import org.simantics.scl.compiler.environment.AmbiguousNameException;
52 import org.simantics.scl.compiler.environment.Environment;
53 import org.simantics.scl.compiler.environment.EnvironmentFactory;
54 import org.simantics.scl.compiler.environment.Environments;
55 import org.simantics.scl.compiler.errors.ErrorLog;
56 import org.simantics.scl.compiler.errors.Locations;
57 import org.simantics.scl.compiler.internal.codegen.effects.EffectConstructor;
58 import org.simantics.scl.compiler.internal.codegen.effects.ThreadLocalVariable;
59 import org.simantics.scl.compiler.internal.codegen.types.JavaReferenceValidator;
60 import org.simantics.scl.compiler.internal.codegen.types.JavaTypeTranslator;
61 import org.simantics.scl.compiler.internal.codegen.types.StandardTypeConstructor;
62 import org.simantics.scl.compiler.internal.codegen.utils.Constants;
63 import org.simantics.scl.compiler.internal.codegen.utils.JavaNamingPolicy;
64 import org.simantics.scl.compiler.internal.codegen.utils.MethodBuilderBase;
65 import org.simantics.scl.compiler.internal.deriving.InstanceDeriver;
66 import org.simantics.scl.compiler.internal.deriving.InstanceDerivers;
67 import org.simantics.scl.compiler.internal.elaboration.profiling.BranchPointInjector;
68 import org.simantics.scl.compiler.internal.elaboration.utils.StronglyConnectedComponents;
69 import org.simantics.scl.compiler.internal.parsing.declarations.ConstructorAst;
70 import org.simantics.scl.compiler.internal.parsing.declarations.DAnnotationAst;
71 import org.simantics.scl.compiler.internal.parsing.declarations.DClassAst;
72 import org.simantics.scl.compiler.internal.parsing.declarations.DDataAst;
73 import org.simantics.scl.compiler.internal.parsing.declarations.DDerivingInstanceAst;
74 import org.simantics.scl.compiler.internal.parsing.declarations.DEffectAst;
75 import org.simantics.scl.compiler.internal.parsing.declarations.DFixityAst;
76 import org.simantics.scl.compiler.internal.parsing.declarations.DInstanceAst;
77 import org.simantics.scl.compiler.internal.parsing.declarations.DMappingRelationAst;
78 import org.simantics.scl.compiler.internal.parsing.declarations.DRelationAst;
79 import org.simantics.scl.compiler.internal.parsing.declarations.DRuleAst;
80 import org.simantics.scl.compiler.internal.parsing.declarations.DTypeAst;
81 import org.simantics.scl.compiler.internal.parsing.declarations.DValueAst;
82 import org.simantics.scl.compiler.internal.parsing.declarations.DValueTypeAst;
83 import org.simantics.scl.compiler.internal.parsing.exceptions.SCLSyntaxErrorException;
84 import org.simantics.scl.compiler.internal.parsing.translation.ProcessedDClassAst;
85 import org.simantics.scl.compiler.internal.parsing.translation.ProcessedDInstanceAst;
86 import org.simantics.scl.compiler.internal.parsing.translation.RelationRepository;
87 import org.simantics.scl.compiler.internal.parsing.translation.ValueRepository;
88 import org.simantics.scl.compiler.internal.parsing.types.TypeAst;
89 import org.simantics.scl.compiler.module.ConcreteModule;
90 import org.simantics.scl.compiler.module.ImportDeclaration;
91 import org.simantics.scl.compiler.module.repository.ImportFailure;
92 import org.simantics.scl.compiler.module.repository.ImportFailureException;
93 import org.simantics.scl.compiler.types.TCon;
94 import org.simantics.scl.compiler.types.TForAll;
95 import org.simantics.scl.compiler.types.TFun;
96 import org.simantics.scl.compiler.types.TPred;
97 import org.simantics.scl.compiler.types.TVar;
98 import org.simantics.scl.compiler.types.Type;
99 import org.simantics.scl.compiler.types.Types;
100 import org.simantics.scl.compiler.types.kinds.Kind;
101 import org.simantics.scl.compiler.types.kinds.Kinds;
102 import org.simantics.scl.compiler.types.util.MultiFunction;
103 import org.simantics.scl.runtime.profiling.BranchPoint;
105 import gnu.trove.list.array.TIntArrayList;
106 import gnu.trove.map.hash.THashMap;
107 import gnu.trove.map.hash.TObjectIntHashMap;
108 import gnu.trove.procedure.TObjectObjectProcedure;
109 import gnu.trove.set.hash.THashSet;
110 import gnu.trove.set.hash.TIntHashSet;
112 public class Elaboration {
114 private final CompilationContext compilationContext;
115 private final ErrorLog errorLog;
116 private final String moduleName;
117 private final ArrayList<ImportDeclaration> importsAst;
118 final JavaReferenceValidator<Object, Object, Object, Object> javaReferenceValidator;
119 private final ValueRepository valueDefinitionsAst;
120 private final RelationRepository relationDefinitionsAst;
123 ConcreteModule module;
124 Environment importedEnvironment;
125 ArrayList<SupplementedValueType> supplementedTypeAnnotations = new ArrayList<SupplementedValueType>();
127 JavaTypeTranslator javaTypeTranslator;
128 ArrayList<StandardTypeConstructor> dataTypes = new ArrayList<StandardTypeConstructor>();
129 THashMap<String, ClassRef> classRefs = new THashMap<String, ClassRef>();
130 THashMap<String, BranchPoint[]> branchPoints;
132 @SuppressWarnings("unchecked")
133 public Elaboration(CompilationContext compilationContext, CompilationTimer timer, EnvironmentFactory localEnvironmentFactory,
134 String moduleName, ArrayList<ImportDeclaration> importsAst,
135 JavaReferenceValidator<?, ?, ?, ?> javaReferenceValidator,
136 ValueRepository valueDefinitionsAst,
137 RelationRepository relationDefinitionsAst) {
138 this.compilationContext = compilationContext;
139 this.errorLog = compilationContext.errorLog;
140 this.moduleName = moduleName;
141 importsAst = processRelativeImports(importsAst);
142 this.importsAst = importsAst;
143 this.javaReferenceValidator = (JavaReferenceValidator<Object, Object, Object, Object>)javaReferenceValidator;
144 this.valueDefinitionsAst = valueDefinitionsAst;
145 this.relationDefinitionsAst = relationDefinitionsAst;
147 module = new ConcreteModule(moduleName);
148 compilationContext.module = module;
151 timer.suspendTimer();
152 importedEnvironment = localEnvironmentFactory.createEnvironment(
153 importsAst.toArray(new ImportDeclaration[importsAst.size()]));
155 timer.continueTimer();
156 compilationContext.environment = new EnvironmentOfModule(importedEnvironment, module);
157 } catch (ImportFailureException e) {
158 for(ImportFailure failure : e.failures)
159 errorLog.log(failure.location, failure.toString());
162 for(ImportDeclaration importAst : importsAst)
163 this.module.addDependency(new ImportDeclaration(
164 importAst.moduleName,
165 importAst.reexport ? importAst.localName : null,
168 localEnvironmentFactory.addBuiltinDependencies(module);
169 compilationContext.namingPolicy = new JavaNamingPolicy(moduleName);
172 private ArrayList<ImportDeclaration> processRelativeImports(ArrayList<ImportDeclaration> relativeImports) {
173 ArrayList<ImportDeclaration> absoluteImports = new ArrayList<ImportDeclaration>(relativeImports.size());
174 for(ImportDeclaration relativeImport : relativeImports) {
175 if(relativeImport.moduleName.startsWith(".")) {
176 String absoluteModuleName = convertRelativeModulePath(relativeImport.location, relativeImport.moduleName);
177 if(absoluteModuleName != null) {
178 ImportDeclaration absoluteImport = new ImportDeclaration(
179 absoluteModuleName, relativeImport.localName,
180 relativeImport.reexport, relativeImport.spec);
181 absoluteImport.location = relativeImport.location;
182 absoluteImports.add(absoluteImport);
186 absoluteImports.add(relativeImport);
188 return absoluteImports;
191 private String convertRelativeModulePath(long location, String relativeModuleName) {
192 String originalRelativeModuleName = relativeModuleName;
193 int p = moduleName.lastIndexOf('/');
194 String parentPackage = p < 0 ? "" : moduleName.substring(0, p);
195 while(relativeModuleName.startsWith(".")) {
196 if(relativeModuleName.startsWith("./")) {
197 relativeModuleName = relativeModuleName.substring(2);
199 else if(relativeModuleName.startsWith("../")) {
200 relativeModuleName = relativeModuleName.substring(3);
201 if(parentPackage.isEmpty()) {
202 errorLog.log(location, "Couldn't resolve the relative module name " + originalRelativeModuleName + " when the current module name is " + moduleName + ".");
205 p = parentPackage.lastIndexOf('/');
206 parentPackage = p < 0 ? "" : parentPackage.substring(0, p);
209 errorLog.log(location, "Couldn't resolve the relative module name " + originalRelativeModuleName + ". It has an invalid syntax.");
213 return parentPackage + "/" + relativeModuleName;
216 public void addTypesToEnvironment(
217 ArrayList<DDataAst> dataTypesAst,
218 ArrayList<DTypeAst> typeAliasesAst,
219 ArrayList<DEffectAst> effectsAst) {
220 for(DDataAst dataType : dataTypesAst) {
221 dataType.parameterKinds = new Kind[dataType.parameters.length];
222 Kind constructorKind = Kinds.STAR;
223 for(int i=dataType.parameters.length-1;i>=0;--i) {
224 Kind kind = Kinds.metaVar();
225 dataType.parameterKinds[i] = kind;
226 constructorKind = Kinds.arrow(kind, constructorKind);
229 StandardTypeConstructor typeConstructor = new StandardTypeConstructor(
230 Types.con(moduleName, dataType.name), constructorKind);
232 NameExistenceChecks.checkIfTypeExists(errorLog,
233 dataType.location, importedEnvironment, dataType.name);
234 if(module.addTypeDescriptor(dataType.name, typeConstructor))
235 errorLog.log(dataType.location, "Type "+dataType.name+" has already been defined in this module.");
236 dataType.typeConstructor = typeConstructor;
239 for(DTypeAst typeAlias : typeAliasesAst) {
240 TypeAlias alias = new TypeAlias(Types.con(moduleName, typeAlias.name), typeAlias.parameters.length);
241 NameExistenceChecks.checkIfTypeExists(errorLog,
242 typeAlias.location, importedEnvironment, typeAlias.name);
243 if(module.addTypeDescriptor(typeAlias.name, alias)) {
244 errorLog.log(typeAlias.location, "Type alias "+typeAlias.name+" has already been defined in this module.");
248 for(DEffectAst effect : effectsAst) {
249 EffectConstructor effectConstructor = new EffectConstructor(Types.con(moduleName, effect.name));
250 effectConstructor.addThreadLocalVariable(
251 new ThreadLocalVariable(
253 TypeDesc.forClass(effect.threadLocalType)
255 if(module.addEffectConstructor(effect.name, effectConstructor))
256 errorLog.log(effect.location, "Type "+effect.name+" has already been defined in this module.");
258 javaTypeTranslator = new JavaTypeTranslator(compilationContext.environment);
259 compilationContext.javaTypeTranslator = javaTypeTranslator;
262 private static final int[] EMPTY_INT_ARRAY = new int[0];
264 public void processTypeAliases(ArrayList<DTypeAst> typeAliasesAst) {
265 TObjectIntHashMap<String> typeAliasMap = new TObjectIntHashMap<String>();
266 for(int i=0;i<typeAliasesAst.size();++i)
267 typeAliasMap.put(typeAliasesAst.get(i).name, i);
268 TIntHashSet tempIntSet = new TIntHashSet();
269 ArrayList<DTypeAst> orderedTypeAliases = new ArrayList<DTypeAst>(typeAliasesAst.size());
270 //for(int i=0;i<typeAliasesAst.size();++i)
271 // System.out.println(i + "# " + typeAliasesAst.get(i).name);
272 new StronglyConnectedComponents(typeAliasesAst.size()) {
274 protected int[] findDependencies(int u) {
275 typeAliasesAst.get(u).type.collectReferences(typeAliasMap, tempIntSet);
276 if(tempIntSet.isEmpty())
277 return EMPTY_INT_ARRAY;
278 if(tempIntSet.contains(u)) {
279 errorLog.log(typeAliasesAst.get(u).location, "Type alias has a self reference.");
280 tempIntSet.remove(u);
282 int[] result = tempIntSet.toArray();
284 //System.out.println(u + " -> " + Arrays.toString(result));
288 protected void reportComponent(int[] component) {
289 //System.out.println("component: " + Arrays.toString(component));
290 if(component.length > 1) {
291 StringBuilder b = new StringBuilder();
292 b.append("Recursively defined type alias (");
293 long minLocation = typeAliasesAst.get(component[0]).location;
294 boolean first = true;
295 for(int u : component) {
296 DTypeAst typeAlias = typeAliasesAst.get(u);
301 b.append(typeAlias.name);
302 if(Locations.beginOf(typeAlias.location) < Locations.beginOf(minLocation))
303 minLocation = typeAlias.location;
306 errorLog.log(minLocation, b.toString());
309 orderedTypeAliases.add(typeAliasesAst.get(component[0]));
313 if(errorLog.isEmpty()) {
314 for(DTypeAst typeAlias : orderedTypeAliases) {
315 TypeAlias alias = (TypeAlias)module.getTypeDescriptor(typeAlias.name);
316 TypeTranslationContext context = createTypeTranslationContext();
317 for(int i=0;i<typeAlias.parameters.length;++i)
318 context.pushTypeVar(typeAlias.parameters[i]);
319 alias.body = typeAlias.type.toType(context, Kinds.metaVar());
320 for(int i=0;i<typeAlias.parameters.length;++i)
321 alias.parameters[i] = context.popTypeVar(typeAlias.parameters[i], null);
326 public void processDataTypes(ArrayList<DDataAst> dataTypesAst) {
327 for(DDataAst dataTypeAst : dataTypesAst) {
328 TypeTranslationContext context = createTypeTranslationContext();
329 TVar[] typeParameters = new TVar[dataTypeAst.parameters.length];
330 for(int i=0;i<dataTypeAst.parameters.length;++i)
331 typeParameters[i] = context.addTypeVar(dataTypeAst.parameters[i]);
332 Constructor[] constructors = new Constructor[dataTypeAst.constructors.length];
333 String className = null;
334 boolean external = false;
335 for(DAnnotationAst annotation : dataTypeAst.getAnnotations()) {
336 if(annotation.id.text.equals("@JavaType")) {
337 if(annotation.parameters.length != 1 ||
338 !(annotation.parameters[0] instanceof ELiteral) ||
339 !(((ELiteral)annotation.parameters[0]).getValue() instanceof StringConstant))
340 errorLog.log(annotation.location, "Invalid parameters. Expected @JavaType \"className\".");
342 className = ((StringConstant)((ELiteral)annotation.parameters[0]).getValue()).getValue().replace('.', '/');
349 boolean trivialDataType = dataTypeAst.constructors.length == 1 &&
350 dataTypeAst.constructors[0].parameters.length == 1;
351 if(className == null && !trivialDataType)
352 className = compilationContext.namingPolicy.getDataTypeClassName(dataTypeAst.name);
354 StandardTypeConstructor dataType = dataTypeAst.typeConstructor;
356 dataType.setType(Types.con(moduleName, dataTypeAst.name), typeParameters);
357 dataType.setConstructors(constructors);
359 dataType.setTypeDesc(TypeDesc.forClass(className));
360 if(!external || dataTypeAst.constructors.length > 0) {
361 dataType.isOpen = false;
363 dataType.external = external;
364 dataTypes.add(dataType);
365 for(int j=0;j<constructors.length;++j) {
366 ConstructorAst constructor = dataTypeAst.constructors[j];
367 String name = constructor.name;
368 Type[] parameterTypes = new Type[constructor.parameters.length];
369 for(int i=constructor.parameters.length-1;i>=0;--i)
370 parameterTypes[i] = context.toType(constructor.parameters[i]);
371 String javaName = constructors.length == 1 ? className
372 : compilationContext.namingPolicy.getConstructorClassName(name);
373 String[] fieldNames = null;
374 for(DAnnotationAst annotation : constructor.annotations)
375 if(annotation.id.text.equals("@JavaType")) {
377 javaName = ((StringConstant)((ELiteral)annotation.parameters[0])
378 .getValue()).getValue();
379 } catch(Exception e) {
380 errorLog.log(annotation.parameters[0].location, "Invalid annotation parameter.");
383 else if(annotation.id.text.equals("@FieldNames")) {
385 EListLiteral literal = (EListLiteral)annotation.parameters[0];
386 fieldNames = new String[literal.getComponents().length];
387 for(int i=0;i<fieldNames.length;++i) {
388 Expression component = literal.getComponents()[i];
389 if(component instanceof EVar)
390 fieldNames[i] = ((EVar)component).name;
391 else if(component instanceof ELiteral)
392 fieldNames[i] = ((StringConstant)((ELiteral)component).getValue()).getValue();
394 } catch(Exception e) {
395 errorLog.log(annotation.parameters[0].location, "Invalid annotation parameter.");
400 constructors[j] = new Constructor(constructor.location, dataType,
401 Name.create(moduleName, name),
402 parameterTypes, javaName);
403 constructors[j].fieldNames = fieldNames;
404 constructors[j].recordFieldNames = constructor.fieldNames;
409 public void processTypeClasses(ArrayList<ProcessedDClassAst> typeClassesAst) {
410 for(ProcessedDClassAst pClassAst : typeClassesAst) {
411 DClassAst classAst = pClassAst.orig;
413 if(module.getTypeClass(classAst.name) != null) {
414 errorLog.log(classAst.location, "Class "+classAst.name+" has already been defined in this module.");
418 TypeTranslationContext context = createTypeTranslationContext();
420 TPred[] classContext = new TPred[classAst.context.length];
421 for(int i=0;i<classContext.length;++i)
422 classContext[i] = context.toTFuncApply(classAst.context[i]);
424 TVar[] parameters = new TVar[classAst.parameters.length];
426 if(classAst.name.equals("Functor") || classAst.name.equals("Monad")) {
428 parameters[0] = context.resolveTypeVariable(pClassAst.orig.location, classAst.parameters[0], Kinds.STAR_TO_STAR);
431 for(int i=0;i<parameters.length;++i)
432 parameters[i] = context.resolveTypeVariable(pClassAst.orig.location, classAst.parameters[i], Kinds.metaVar());
435 TCon con = Types.con(moduleName, classAst.name);
436 TypeClass typeClass = new TypeClass(classAst.location,
439 compilationContext.namingPolicy.getTypeClassInterfaceName(con),
441 Fundep.mapFundeps(classAst.parameters, classAst.fundeps));
443 THashMap<String, TypeClassMethod> methods = typeClass.methods;
444 for(DValueTypeAst methodDecl : pClassAst.typeDeclarations) {
448 type = context.toType(methodDecl.type);
449 } catch(SCLSyntaxErrorException e) {
450 errorLog.log(e.location, e.getMessage());
453 for(EVar name : methodDecl.names) {
454 typeClass.methodNames.add(name.name);
455 methods.put(name.name,
456 new TypeClassMethod(typeClass, name.name,
457 compilationContext.namingPolicy.getMethodName(name.name),
458 type, Types.getArity(type),
462 } catch(RuntimeException e) {
463 errorLog.setExceptionPosition(methodDecl.location);
468 for(String methodName : pClassAst.defaultImplementations.getValueNames()) {
469 String fullName = "_" + classAst.name + "_default_" + methodName;
470 ArrayList<DValueAst> defs = pClassAst.defaultImplementations.getDefinition(methodName);
472 TypeClassMethod method = typeClass.methods.get(methodName);
474 errorLog.log(defs.get(0).location, "Method " + methodName + " is not defined in this class.");
477 method.setDefaultImplementation(Name.create(moduleName, fullName));
479 valueDefinitionsAst.addDefinitions(fullName, defs);
480 /*valueDefinitionsAst.addAnnotation(fullName, new DAnnotationAst(new EVar("@private"),
481 Collections.<Expression>emptyList()));*/
482 supplementedTypeAnnotations.add(new SupplementedValueType(defs.get(0).location, fullName, method.getType()));
485 module.addTypeClass(classAst.name, typeClass);
489 private TypeTranslationContext createTypeTranslationContext() {
490 return new TypeTranslationContext(compilationContext);
493 public void processDerivingInstances(ArrayList<DDerivingInstanceAst> derivingInstancesAst,
494 ArrayList<ProcessedDInstanceAst> instancesAst) {
495 for(DDerivingInstanceAst derivingInstance : derivingInstancesAst) {
496 String name = derivingInstance.name.name;
499 con = Environments.getTypeClassName(compilationContext.environment, name);
500 } catch (AmbiguousNameException e) {
501 errorLog.log(derivingInstance.name.location, e.getMessage());
505 errorLog.log(derivingInstance.name.location, "Couldn't resolve class " + name + ".");
508 InstanceDeriver deriver = InstanceDerivers.get(con);
510 errorLog.log(derivingInstance.location, "Doesn't know how to derive " + name + ".");
512 deriver.derive(errorLog, compilationContext.environment, instancesAst, derivingInstance);
516 public void processInstances(ArrayList<ProcessedDInstanceAst> instancesAst) {
517 THashSet<TPred> instanceClashCheckSet = new THashSet<TPred>();
518 for(ProcessedDInstanceAst pInstanceAst : instancesAst) {
519 DInstanceAst instanceAst = pInstanceAst.orig;
521 TypeTranslationContext context = createTypeTranslationContext();
523 String name = instanceAst.name.name;
526 typeClassCon = Environments.getTypeClassName(compilationContext.environment, name);
527 } catch (AmbiguousNameException e) {
528 errorLog.log(instanceAst.name.location, e.getMessage());
531 if(typeClassCon == null) {
532 errorLog.log(instanceAst.name.location, "Couldn't resolve class " + name + ".");
535 TypeClass typeClass = compilationContext.environment.getTypeClass(typeClassCon);
536 pInstanceAst.typeClass = typeClass;
538 if(instanceAst.types.length != typeClass.parameters.length) {
539 errorLog.log(instanceAst.location, "Wrong number of parameters to type class " + typeClassCon.name + ".");
542 Type[] parameters = new Type[instanceAst.types.length];
543 for(int i=0;i<parameters.length;++i)
544 parameters[i] = context.toType(instanceAst.types[i], typeClass.parameters[i].getKind() /* FIXME */);
545 TPred instance = Types.pred(typeClassCon, parameters);
547 if(!instanceClashCheckSet.add(instance)) {
548 errorLog.log(instanceAst.location, "Duplicate definition of the instance " + instance + ".");
552 TPred[] instanceContext = new TPred[instanceAst.context.length];
553 for(int i=0;i<instanceContext.length;++i)
554 instanceContext[i] = context.toTFuncApply(instanceAst.context[i]);
556 String javaName = compilationContext.namingPolicy.getInstanceClassName(instance);
557 String instancePrefix = instance.toName() + "$";
559 ValueRepository valueDefs = pInstanceAst.valueDefs;
561 THashMap<String, MethodImplementation> methodImplementations =
562 new THashMap<String, MethodImplementation>();
563 for(String valueName : valueDefs.getValueNames()) {
564 String fullName = instancePrefix + valueName;
565 long loc = valueDefs.getDefinition(valueName).get(0).location;
566 valueDefinitionsAst.addFrom(valueDefs, valueName, fullName);
567 /*valueDefinitionsAst.addAnnotation(fullName, new DAnnotationAst(new EVar("@private"),
568 Collections.<Expression>emptyList()));*/
569 TypeClassMethod method = typeClass.methods.get(valueName);
571 errorLog.log(loc, "Method " + valueName + " is not defined in the type class " + typeClass.name.name + ".");
574 Type type = method.getBaseType().replace(typeClass.parameters, parameters);
575 for(int i=instanceContext.length-1;i>=0;--i)
576 type = Types.constrained(instanceContext[i], type);
577 //System.out.println(valueName + " :: " + type);
578 supplementedTypeAnnotations.add(
579 new SupplementedValueType(loc, fullName, type));
580 methodImplementations.put(valueName,
581 new MethodImplementation(Name.create(moduleName, fullName), false));
584 // Are all necessary methods defined
585 for(String methodName : typeClass.methods.keySet())
586 if(!methodImplementations.containsKey(methodName)) {
587 Name defaultImplementation = typeClass.methods.get(methodName).getDefaultImplementation();
588 if(defaultImplementation == null)
589 errorLog.log(instanceAst.location, "Method " + methodName + " is not defined.");
591 methodImplementations.put(methodName,
592 new MethodImplementation(defaultImplementation, true));
595 JavaTypeInstanceConstructor generator = new JavaTypeInstanceConstructor(javaName, instance, instanceContext);
596 if(instanceContext.length == 0)
597 generator.setHasStaticInstance(true);
599 TypeClassInstance typeClassInstance =
600 new TypeClassInstance(instanceAst.location, typeClass, generator,
601 generator.getTypeParameters(),
602 instanceContext, instance, methodImplementations,
605 generator.setInstance(typeClassInstance);
607 module.addTypeClassInstance(typeClassCon, typeClassInstance);
608 } catch(RuntimeException e) {
609 errorLog.setExceptionPosition(instanceAst.location);
614 // Generate superclass functions
615 for(ArrayList<TypeClassInstance> instanceArray : module.getTypeInstances().values())
616 for(TypeClassInstance instance : instanceArray) {
618 TypeClass typeClass = instance.typeClass;
619 int superCount = typeClass.context.length;
621 instance.superExpressions = new SCLValue[superCount];
622 for(int i=0;i<superCount;++i) {
623 TPred type = (TPred)typeClass.context[i]
624 .replace(typeClass.parameters, instance.instance.parameters);
625 SCLValue value = new SCLValue(Name.create(moduleName,
626 instance.javaName.substring(instance.javaName.indexOf('$')+1) + "_super" + i));
627 //value.addProperty(PrivateProperty.INSTANCE);
628 module.addValue(value);
629 Expression expression = new EGetConstraint(instance.location, type);
630 value.setExpression(expression);
631 value.setType(Types.forAll(instance.generatorParameters, Types.constrained(instance.context, type)));
632 value.getProperties().add(new InlineProperty(instance.context.length, 0xffffffff));
633 //TypeUnparsingContext tuc = new TypeUnparsingContext();
634 // TODO error handling
635 instance.superExpressions[i] = value;
637 } catch(RuntimeException e) {
638 errorLog.setExceptionPosition(instance.getLocation());
644 public void processJavaMethods(ArrayList<JavaMethodDeclaration> javaMethodDeclarations) {
645 for(JavaMethodDeclaration javaMethod : javaMethodDeclarations) {
646 String name = javaMethod.methodName.name;
647 String javaName = name;
648 ArrayList<DAnnotationAst> annotations =
649 valueDefinitionsAst.getAnnotations(name);
650 boolean isPrivate = false;
651 if(annotations != null) {
652 for(DAnnotationAst annotation : annotations)
653 if(annotation.id.text.equals("@JavaName")) {
654 Expression p0 = annotation.parameters[0];
655 if(p0 instanceof EVar)
656 javaName = ((EVar)p0).name;
657 else if(p0 instanceof ELiteral) {
658 ELiteral lit = (ELiteral)p0;
659 javaName = ((StringConstant)lit.getValue()).getValue();
662 else if(annotation.id.text.equals("@private")) {
667 Type type = createTypeTranslationContext().toType(javaMethod.type);
669 CallJava callJava = resolveMethod(
671 javaMethod.className,
674 if(callJava != null) {
675 NameExistenceChecks.checkIfValueExists(errorLog, javaMethod.location,
676 importedEnvironment, name);
677 SCLValue value = module.addValue(name, callJava);
678 value.definitionLocation = javaMethod.methodName.location;
680 value.addProperty(PrivateProperty.INSTANCE);
686 * Convert a java class method into a {@link CallJava} instance.
687 * Compilation errors are logged for failures in finding the named class or in accessing the method.
688 * The return value for errors is {@code null}.
690 * @param loc SCL source location
691 * @param className Name of the class
692 * @param methodName Name of the method
693 * @param type The expected SCL type of the method
694 * @return A new JavaCall instance, or null if the given method can not be instantiated.
696 private CallJava resolveMethod(long loc,
697 String className, String methodName, Type type) {
699 ClassRef classRef = classRefs.get(className);
700 if(classRef == null) {
701 if(classRefs.containsKey(className))
703 classRef = javaReferenceValidator.getClassRef(className);
704 classRefs.put(className, classRef);
705 if(classRef == null) {
706 errorLog.log(loc, "Didn't find class " + className + ".");
709 if(!javaReferenceValidator.isPublic(classRef.getClass())) {
710 errorLog.log(loc, "Class " + className + " is not public.");
717 List<MethodRef> methodRefs = classRef.getMethodRefs(methodName);
718 if(methodRefs.isEmpty()) {
719 errorLog.log(loc, "Didn't find any public method or field with name '" + methodName + "' from class " + className + ".");
723 ArrayList<CallJava> candidates = new ArrayList<CallJava>(2);
724 for(MethodRef methodRef : methodRefs) {
725 CallJava candidate = matchType(methodRef, type);
726 if(candidate != null)
727 candidates.add(candidate);
730 if(candidates.isEmpty()) {
731 StringBuilder b = new StringBuilder();
732 b.append("Didn't find any public method or field matching the type " + type + ". The following methods and fields were tried:");
733 for(MethodRef methodRef : methodRefs) {
737 b.append("\nbut expected something like:");
739 MultiFunction mFun = Types.matchFunction(type);
740 b.append("\n public static ");
741 b.append(javaTypeTranslator.toTypeDesc(mFun.returnType).getFullName()).append(' ').append(methodName).append('(');
742 boolean first = true;
743 for(Type parameter : mFun.parameterTypes) {
748 b.append(javaTypeTranslator.toTypeDesc(parameter).getFullName());
752 errorLog.log(loc, b.toString());
755 else if(candidates.size() == 1)
756 return candidates.get(0);
758 // Try to find the best match
759 ArrayList<CallJava> bestCandidates = new ArrayList<CallJava>(candidates.size());
760 loop: for(CallJava candidate : candidates) {
761 Iterator<CallJava> it = bestCandidates.iterator();
762 while(it.hasNext()) {
763 CallJava b = it.next();
764 switch(candidate.compareTo(javaReferenceValidator, b)) {
768 case CallJava.GREATER:
771 case CallJava.INCOMPARABLE:
775 bestCandidates.add(candidate);
778 if(bestCandidates.size() == 1) {
779 // System.out.println("Choose: " + bestCandidates.get(0).getMethodRef());
780 return bestCandidates.get(0);
783 StringBuilder b = new StringBuilder();
784 b.append("Found more than one incomparable public methods the type " + type + ":");
785 for(CallJava callJava : bestCandidates) {
787 b.append(callJava.getMethodRef());
789 errorLog.log(loc, b.toString());
795 private CallJava matchType(MethodRef methodRef, Type type) {
796 //System.out.println("-----------------------------------------------------");
797 //System.out.println("method: " + methodRef);
798 //System.out.println("type: " + type);
800 type = Types.canonical(type);
801 while(type instanceof TForAll)
802 type = Types.canonical(((TForAll)type).type);
804 TypeDesc[] expectedParameterTypes = methodRef.getParameterTypes();
806 ArrayList<Type> parameterTypes = new ArrayList<Type>();
807 Type effect = Types.NO_EFFECTS;
808 Type returnType = type;
810 StackItem[] stackItems = new StackItem[expectedParameterTypes.length];
811 TIntArrayList unresolvedItems = new TIntArrayList();
814 for(int i=0;i<expectedParameterTypes.length;++i) {
816 TypeDesc providedParameterType;
818 if(effect != Types.NO_EFFECTS || !(returnType instanceof TFun)) {
819 while(i < expectedParameterTypes.length)
820 unresolvedItems.add(i++);
823 TFun fun = (TFun)returnType;
824 parameterType = Types.canonical(fun.domain);
825 parameterTypes.add(parameterType);
826 effect = Types.canonical(fun.effect);
827 returnType = Types.canonical(fun.range);
828 providedParameterType = javaTypeTranslator.toTypeDesc(parameterType);
829 } while(providedParameterType.equals(TypeDesc.VOID));
832 TypeDesc expectedParameterType = expectedParameterTypes[i];
833 if(javaReferenceValidator.isAssignableFrom(expectedParameterType,
834 providedParameterType)) {
835 stackItems[i] = new ParameterStackItem(
836 parameterTypes.size()-1,
841 unresolvedItems.add(i++);
842 if(i == expectedParameterTypes.length) {
843 parameterTypes.remove(parameterTypes.size()-1);
844 returnType = Types.functionE(parameterType, effect, returnType);
849 //System.out.println("returnType: " + returnType);
850 if(!unresolvedItems.isEmpty()) {
851 ArrayList<TCon> concreteEffects = new ArrayList<TCon>();
852 effect.collectConcreteEffects(concreteEffects);
854 for(TCon eff : concreteEffects) {
855 EffectConstructor effC = compilationContext.environment.getEffectConstructor(eff);
856 for(ThreadLocalVariable var : effC.getThreadLocalVariables()) {
857 for(int i=0;i<unresolvedItems.size();++i) {
858 int id = unresolvedItems.get(i);
859 if(!expectedParameterTypes[id].equals(TypeDesc.OBJECT) &&
860 javaReferenceValidator.isAssignableFrom(expectedParameterTypes[id], var.type)) {
861 stackItems[id] = new ThreadLocalStackItem(var.variableName, var.type);
862 unresolvedItems.removeAt(i);
869 if(!unresolvedItems.isEmpty())
871 /*System.out.print("Unresolved: ");
872 boolean first = true;
873 for(int i=0;i<unresolvedItems.size();++i) {
877 System.out.print(", ");
878 System.out.print(expectedParameterTypes[unresolvedItems.get(i)]);
880 System.out.println();*/
882 OutputFilter filter = null;
884 TypeDesc providedReturnType = methodRef.getReturnType();
885 if(!providedReturnType.equals(Constants.FUNCTION)) {
886 while(returnType instanceof TFun && effect == Types.NO_EFFECTS) {
887 TFun fun = (TFun)returnType;
888 Type parameterType = Types.canonical(fun.domain);
889 if(!javaTypeTranslator.toTypeDesc(parameterType).equals(TypeDesc.VOID))
891 parameterTypes.add(parameterType);
892 effect = Types.canonical(fun.effect);
893 returnType = Types.canonical(fun.range);
896 TypeDesc expectedReturnType =
897 javaTypeTranslator.toTypeDesc(returnType);
898 if(!javaReferenceValidator.isAssignableFrom(expectedReturnType,
899 providedReturnType)) {
900 if(expectedReturnType.equals(TypeDesc.VOID)) {
901 if(providedReturnType.equals(TypeDesc.DOUBLE) || providedReturnType.equals(TypeDesc.LONG))
902 filter = Pop2OutputFilter.INSTANCE;
904 filter = PopOutputFilter.INSTANCE;
906 else if(expectedReturnType.equals(Constants.LIST)
907 && providedReturnType.equals(Constants.COLLECTION))
908 filter = ConvertToListFilter.INSTANCE;
914 CallJava result = new CallJava(
915 Types.freeVarsArray(type),
918 parameterTypes.toArray(new Type[parameterTypes.size()]),
922 //System.out.println(result.getType());
926 public void processMappingRelations(ArrayList<DMappingRelationAst> mappingRelationsAst) {
927 for(DMappingRelationAst mappingRelation : mappingRelationsAst) {
928 TypeAst[] parameterTypesAst = mappingRelation.parameterTypes;
929 Type[] parameterTypes = new Type[parameterTypesAst.length];
930 TypeTranslationContext typeTranslationContext = createTypeTranslationContext();
931 for(int i=0;i<parameterTypes.length;++i)
932 parameterTypes[i] = parameterTypesAst[i].toType(typeTranslationContext, Kinds.STAR);
933 MappingRelation mRel =
934 new MappingRelation(Name.create(moduleName, mappingRelation.name), parameterTypes);
935 mRel.location = mappingRelation.location;
936 if(module.addMappingRelation(mRel))
937 errorLog.log(mappingRelation.location, "Mapping relation " + mappingRelation.name +
938 " has already been defined.");
942 THashMap<String, DRuleAst> ruleAstMap = new THashMap<String, DRuleAst>();
944 public void processRules(ArrayList<DRuleAst> rulesAst) {
945 // Find implied mapping relations
946 for(DRuleAst ruleA : rulesAst) {
947 ArrayList<Query> whereSection = ruleA.getSections().get("where");
948 if(whereSection != null)
949 for(Query query : whereSection) {
950 if(!(query instanceof QPreGuard))
952 QPreGuard guard = (QPreGuard)query;
953 if(!(guard.guard instanceof EApply))
955 EApply apply = (EApply)guard.guard;
956 if(!(apply.getFunction() instanceof EVar))
959 String name = ((EVar)apply.getFunction()).name;
960 if(module.getMappingRelation(name) != null)
963 int arity = apply.getParameters().length;
964 Type[] parameterTypes = new Type[arity];
965 for(int i=0;i<arity;++i)
966 parameterTypes[i] = Types.metaVar(Kinds.STAR);
967 MappingRelation mRel =
968 new MappingRelation(Name.create(moduleName, name), parameterTypes);
969 mRel.location = query.location;
970 module.addMappingRelation(mRel);
972 if(ruleAstMap.put(ruleA.name, ruleA) != null)
973 errorLog.log(ruleA.location, "Rule " + ruleA.name +
974 " has already been defined.");
978 for(String ruleName : ruleAstMap.keySet())
979 processRule(ruleName);
982 private TransformationRule processRule(String ruleName) {
983 TransformationRule rule = module.getRule(ruleName);
987 DRuleAst ruleAst = ruleAstMap.get(ruleName);
988 if(ruleAst.alreadyProcessing) {
989 errorLog.log(ruleAst.location, "Cyclic chain of rule extensions.");
993 TransformationRule[] extendsRules;
994 int length = ruleAst.extendsNames.length;
996 extendsRules = TransformationRule.EMPTY_ARRAY;
998 extendsRules = new TransformationRule[length];
999 for(int i=0;i<length;++i) {
1001 String extendsName = ruleAst.extendsNames[i];
1002 TransformationRule extendsRule;
1003 if(ruleAstMap.containsKey(extendsName))
1004 extendsRule = processRule(extendsName);
1006 extendsRule = Environments.getRule(compilationContext.environment, extendsName);
1007 if(extendsRule == null)
1008 errorLog.log(ruleAst.location,
1009 "Couldn't resolve rule name " + extendsName + ".");
1011 extendsRules[i] = extendsRule;
1012 } catch(AmbiguousNameException e) {
1013 errorLog.log(ruleAst.location, e.getMessage());
1018 final THashMap<SectionName, Query[]> sections = new THashMap<SectionName, Query[]>();
1020 final TranslationContext context = createTranslationContext();
1022 THashMap<String, Variable> variables = context.getVariables();
1023 for(TransformationRule extendsRule : extendsRules) {
1024 if(extendsRule == null)
1026 for(Variable var : extendsRule.variables)
1027 if(variables.put(var.getName(), var) != null) {
1028 errorLog.log(ruleAst.location,
1029 "Variable " + var.getName() + " is defined in more than one base rule, which is not currently allowed.");
1033 context.pushExistentialFrame();
1034 ruleAst.getSections().forEachEntry(new TObjectObjectProcedure<String, ArrayList<Query>>() {
1036 public boolean execute(String sectionNameString, ArrayList<Query> queries) {
1037 Query[] resolvedQueries = new Query[queries.size()];
1038 for(int i=0;i<resolvedQueries.length;++i)
1039 resolvedQueries[i] = queries.get(i).resolve(context);
1041 SectionName sectionName = SectionName.getSectionName(sectionNameString);
1042 if(sectionName == null)
1043 context.getErrorLog().log(queries.get(0).location,
1044 "Invalid section name " + sectionNameString + ".");
1046 sections.put(sectionName, resolvedQueries);
1051 Variable[] variables = context.getVariables().values().toArray(new Variable[context.getVariables().size()]);
1053 rule = new TransformationRule(ruleAst.isAbstract,
1054 Name.create(moduleName, ruleAst.name),
1056 sections, variables);
1057 rule.location = ruleAst.location;
1058 module.addRule(rule);
1062 public void addDataTypesToEnvironment() {
1063 for(StandardTypeConstructor dataType : dataTypes) {
1064 int constructorTag = 0;
1065 for(Constructor constructor : dataType.constructors) {
1066 SCLValue value = new SCLValue(constructor.name);
1067 SCLConstructor sclConstructor =
1069 constructor.name.name,
1070 constructor.javaName,
1071 constructor.getTypeVariables(),
1073 constructor.getReturnType(),
1074 constructor.fieldNames == null
1075 ? SCLConstructor.DEFAULT_FIELD_NAMES[constructor.getParameterTypes().length]
1076 : constructor.fieldNames,
1077 constructor.recordFieldNames,
1078 constructor.getParameterTypes());
1079 if(dataType.constructors.length == 1 && (
1080 dataType.getTypeDesc() == null ||
1081 dataType.constructors[0].javaName.equals(MethodBuilderBase.getClassName(dataType.getTypeDesc()))))
1082 sclConstructor.setOnlyConstructor(true);
1083 value.setValue(sclConstructor);
1084 value.setType(constructor.getType());
1086 NameExistenceChecks.checkIfValueExists(errorLog, constructor.loc,
1087 importedEnvironment, constructor.name.name);
1088 if(module.addValue(value)) {
1089 errorLog.log(constructor.loc,
1090 "Value " + constructor.name.name + " is already defined.");
1096 public void addTypeClassesToEnvironment() {
1097 for(TypeClass typeClass : module.getTypeClasses()) {
1098 for(TypeClassMethod method : typeClass.methods.values()) {
1099 SCLValue value = method.createValue();
1100 value.definitionLocation = method.location;
1101 NameExistenceChecks.checkIfValueExists(errorLog, Locations.NO_LOCATION,
1102 importedEnvironment, value.getName().name);
1104 if(module.addValue(value)) {
1105 String name = method.getName();
1106 long location = Locations.NO_LOCATION;
1107 ArrayList<DValueAst> definitions = valueDefinitionsAst.getDefinition(name);
1108 if(definitions != null && !definitions.isEmpty())
1109 location = definitions.get(0).location;
1110 errorLog.log(location, "Value " + name + " is already defined.");
1116 public void preprocessValueDefinitions(ArrayList<DValueTypeAst> typeAnnotationsAst) {
1117 for(String name : valueDefinitionsAst.getValueNames()) {
1118 SCLValue value = new SCLValue(Name.create(moduleName, name));
1120 long location = valueDefinitionsAst.getLocation(name);
1121 NameExistenceChecks.checkIfValueExists(errorLog, location,
1122 importedEnvironment, value.getName().name);
1123 value.definitionLocation = location;
1124 if(module.addValue(value))
1125 errorLog.log(location, "Value " + name + " is already defined.");
1127 for(DValueTypeAst valueTypeAst : typeAnnotationsAst)
1128 for(EVar name : valueTypeAst.names) {
1129 SCLValue value = module.getValue(name.name);
1131 errorLog.log(valueTypeAst.location, name.name + " is not defined.");
1132 value = new SCLValue(Name.create(moduleName, name.name));
1133 module.addValue(value);
1135 value.definitionLocation = name.location;
1137 for(String name : relationDefinitionsAst.getRelationNames()) {
1138 ConcreteRelation relation = new ConcreteRelation(name);
1139 module.addRelation(name, relation);
1143 public void addValueDefinitionsToEnvironment(ArrayList<DValueTypeAst> typeAnnotationsAst) {
1144 THashMap<String, DValueTypeAst> typeMap = new THashMap<String, DValueTypeAst>();
1145 for(DValueTypeAst valueTypeAst : typeAnnotationsAst)
1146 for(EVar name : valueTypeAst.names) {
1147 if(typeMap.containsKey(name.name))
1148 errorLog.log(valueTypeAst.location, "Type of "+name.name+" has already been declared in this module.");
1150 typeMap.put(name.name, valueTypeAst);
1153 for(String name : valueDefinitionsAst.getValueNames()) {
1154 ArrayList<DValueAst> defs = valueDefinitionsAst.getDefinition(name);
1156 SCLValue value = module.getValue(name);
1157 TranslationContext context = createTranslationContext();
1158 Expression expression = context.translateCases2(defs);
1159 value.setExpression(expression);
1161 DValueTypeAst valueTypeAst = typeMap.remove(name);
1162 if(valueTypeAst != null)
1163 value.setType(Types.closure(context.toType(valueTypeAst.type)));
1165 ArrayList<DAnnotationAst> annotations = valueDefinitionsAst.getAnnotations(name);
1166 if(annotations != null)
1167 for(DAnnotationAst annotation : annotations) {
1168 handleAnnotation(value, defs, annotation);
1170 } catch(RuntimeException e) {
1171 errorLog.setExceptionPosition(defs.get(0).location);
1175 for(String name : relationDefinitionsAst.getRelationNames()) {
1176 ArrayList<DRelationAst> definitions = relationDefinitionsAst.getDefinition(name);
1177 if(definitions.size() > 1) {
1178 errorLog.log(definitions.get(1).location,
1179 "Does not yet support definition of relations by more than one rule.");
1183 DRelationAst definition = definitions.get(0);
1184 ConcreteRelation relation = (ConcreteRelation)module.getRelation(name);
1185 relation.location = definition.location;
1186 TranslationContext context = createTranslationContext();
1187 definition.translateTo(context, relation);
1191 private TranslationContext createTranslationContext() {
1192 return new TranslationContext(compilationContext, null);
1195 private void handleAnnotation(SCLValue value, ArrayList<DValueAst> defs, DAnnotationAst annotation) {
1196 if(annotation.id.text.equals("@macro")) {
1197 value.setMacroRule(new StandardMacroRule());
1199 else if(annotation.id.text.equals("@inline")) {
1201 int arity = defs.get(0).lhs.getFunctionDefinitionPatternArity();
1202 int phaseMask = 0xffffffff;
1203 if(annotation.parameters.length > 0) {
1204 phaseMask = Integer.parseInt(((EIntegerLiteral)annotation.parameters[0]).getValue());
1206 value.addProperty(new InlineProperty(arity, phaseMask));
1207 } catch(NotPatternException e) {
1208 errorLog.log(annotation.location, "Inline annotation is invalid: this is not a function.");
1211 else if(annotation.id.text.equals("@private")) {
1212 value.addProperty(PrivateProperty.INSTANCE);
1214 else if(annotation.id.text.equals("@deprecated")) {
1218 errorLog.log(annotation.location, "Unknown annotation.");
1221 public void addSupplementedTypeAnnotationsToEnvironment() {
1222 for(SupplementedValueType valueType : supplementedTypeAnnotations) {
1223 Type type = Types.closure(valueType.type);
1224 String name = valueType.name;
1225 SCLValue value = module.getValue(name);
1227 errorLog.log(valueType.position,
1228 name + " is not defined.");
1229 else if(value.getType()==null)
1230 value.setType(type);
1232 errorLog.log(valueType.position,
1233 "Type of "+name+" has already been declared in this module.");
1237 public void addFixityToEnvironment(ArrayList<DFixityAst> fixityAst) {
1238 for(DFixityAst fixity : fixityAst) {
1239 for(EVar symbol : fixity.symbols) {
1240 String name = symbol.name;
1241 SCLValue value = module.getValue(name);
1243 errorLog.log(symbol.location,
1244 name + " is not defined.");
1246 value.setPrecedence(fixity.precedence);
1251 public void addCoverageBranchPoints() {
1252 branchPoints = new THashMap<String, BranchPoint[]>();
1253 BranchPointInjector injector = new BranchPointInjector();
1254 for(String valueName : valueDefinitionsAst.getValueNames()) {
1255 for(DValueAst valueAst : valueDefinitionsAst.getDefinition(valueName))
1256 valueAst.value = injector.injectBranchPoint(valueAst.value);
1257 branchPoints.put(valueName, injector.getAndClearBranchPoints());