X-Git-Url: https://gerrit.simantics.org/r/gitweb?p=simantics%2Fplatform.git;a=blobdiff_plain;f=bundles%2Forg.simantics.scl.compiler%2Fsrc%2Forg%2Fsimantics%2Fscl%2Fcompiler%2Fcompilation%2FElaboration.java;h=ff5ee3b2902fa0384258f246776d534dbed0d617;hp=898f6c0a5680c88bad669e56c5395a5fe65d0164;hb=84b211a0aa05c956d33e038a1106bb0464ce373a;hpb=53072c542bfdb7646792fc338264f260f66b4bdf diff --git a/bundles/org.simantics.scl.compiler/src/org/simantics/scl/compiler/compilation/Elaboration.java b/bundles/org.simantics.scl.compiler/src/org/simantics/scl/compiler/compilation/Elaboration.java index 898f6c0a5..ff5ee3b29 100644 --- a/bundles/org.simantics.scl.compiler/src/org/simantics/scl/compiler/compilation/Elaboration.java +++ b/bundles/org.simantics.scl.compiler/src/org/simantics/scl/compiler/compilation/Elaboration.java @@ -6,7 +6,9 @@ import java.util.List; import org.cojen.classfile.TypeDesc; import org.simantics.scl.compiler.common.datatypes.Constructor; +import org.simantics.scl.compiler.common.exceptions.InternalCompilerError; import org.simantics.scl.compiler.common.names.Name; +import org.simantics.scl.compiler.constants.Constant; import org.simantics.scl.compiler.constants.JavaTypeInstanceConstructor; import org.simantics.scl.compiler.constants.SCLConstructor; import org.simantics.scl.compiler.constants.StringConstant; @@ -14,12 +16,14 @@ import org.simantics.scl.compiler.constants.generic.CallJava; import org.simantics.scl.compiler.constants.generic.ClassRef; import org.simantics.scl.compiler.constants.generic.ConvertToListFilter; import org.simantics.scl.compiler.constants.generic.MethodRef; +import org.simantics.scl.compiler.constants.generic.MethodRef.FieldRef; import org.simantics.scl.compiler.constants.generic.OutputFilter; import org.simantics.scl.compiler.constants.generic.ParameterStackItem; import org.simantics.scl.compiler.constants.generic.Pop2OutputFilter; import org.simantics.scl.compiler.constants.generic.PopOutputFilter; import org.simantics.scl.compiler.constants.generic.StackItem; import org.simantics.scl.compiler.constants.generic.ThreadLocalStackItem; +import org.simantics.scl.compiler.constants.singletons.SafeCoerce; import org.simantics.scl.compiler.elaboration.contexts.TranslationContext; import org.simantics.scl.compiler.elaboration.contexts.TypeTranslationContext; import org.simantics.scl.compiler.elaboration.errors.NotPatternException; @@ -28,6 +32,7 @@ import org.simantics.scl.compiler.elaboration.expressions.EGetConstraint; import org.simantics.scl.compiler.elaboration.expressions.EIntegerLiteral; import org.simantics.scl.compiler.elaboration.expressions.EListLiteral; import org.simantics.scl.compiler.elaboration.expressions.ELiteral; +import org.simantics.scl.compiler.elaboration.expressions.EPreCHRRulesetConstructor; import org.simantics.scl.compiler.elaboration.expressions.EVar; import org.simantics.scl.compiler.elaboration.expressions.Expression; import org.simantics.scl.compiler.elaboration.expressions.Variable; @@ -36,6 +41,7 @@ import org.simantics.scl.compiler.elaboration.fundeps.Fundep; import org.simantics.scl.compiler.elaboration.java.JavaMethodDeclaration; import org.simantics.scl.compiler.elaboration.macros.StandardMacroRule; import org.simantics.scl.compiler.elaboration.modules.DeprecatedProperty; +import org.simantics.scl.compiler.elaboration.modules.DerivedProperty; import org.simantics.scl.compiler.elaboration.modules.InlineProperty; import org.simantics.scl.compiler.elaboration.modules.MethodImplementation; import org.simantics.scl.compiler.elaboration.modules.PrivateProperty; @@ -44,6 +50,7 @@ import org.simantics.scl.compiler.elaboration.modules.TypeAlias; import org.simantics.scl.compiler.elaboration.modules.TypeClass; import org.simantics.scl.compiler.elaboration.modules.TypeClassInstance; import org.simantics.scl.compiler.elaboration.modules.TypeClassMethod; +import org.simantics.scl.compiler.elaboration.modules.TypeDescriptor; import org.simantics.scl.compiler.elaboration.query.Query; import org.simantics.scl.compiler.elaboration.query.pre.QPreGuard; import org.simantics.scl.compiler.elaboration.relations.ConcreteRelation; @@ -54,7 +61,9 @@ import org.simantics.scl.compiler.environment.AmbiguousNameException; import org.simantics.scl.compiler.environment.Environment; import org.simantics.scl.compiler.environment.EnvironmentFactory; import org.simantics.scl.compiler.environment.Environments; +import org.simantics.scl.compiler.errors.CompilationError; import org.simantics.scl.compiler.errors.ErrorLog; +import org.simantics.scl.compiler.errors.ErrorSeverity; import org.simantics.scl.compiler.errors.Locations; import org.simantics.scl.compiler.internal.codegen.effects.EffectConstructor; import org.simantics.scl.compiler.internal.codegen.effects.ThreadLocalVariable; @@ -81,6 +90,7 @@ import org.simantics.scl.compiler.internal.parsing.declarations.DInstanceAst; import org.simantics.scl.compiler.internal.parsing.declarations.DMappingRelationAst; import org.simantics.scl.compiler.internal.parsing.declarations.DRelationAst; import org.simantics.scl.compiler.internal.parsing.declarations.DRuleAst; +import org.simantics.scl.compiler.internal.parsing.declarations.DRulesetAst; import org.simantics.scl.compiler.internal.parsing.declarations.DTypeAst; import org.simantics.scl.compiler.internal.parsing.declarations.DValueAst; import org.simantics.scl.compiler.internal.parsing.declarations.DValueTypeAst; @@ -159,6 +169,8 @@ public class Elaboration { module = new ConcreteModule(moduleName); compilationContext.module = module; + if(moduleHeader != null && moduleHeader.defaultLocalName != null) + module.setDefaultLocalName(moduleHeader.defaultLocalName); try { if(timer != null) timer.suspendTimer(); @@ -169,7 +181,7 @@ public class Elaboration { compilationContext.environment = new EnvironmentOfModule(importedEnvironment, module); } catch (ImportFailureException e) { for(ImportFailure failure : e.failures) - errorLog.log(failure.location, failure.toString()); + errorLog.log(new CompilationError(failure.location, failure.toString(), ErrorSeverity.IMPORT_ERROR)); return; } for(ImportDeclaration importAst : importsAst) @@ -206,7 +218,8 @@ public class Elaboration { public void addTypesToEnvironment( ArrayList dataTypesAst, ArrayList typeAliasesAst, - ArrayList effectsAst) { + ArrayList effectsAst, + ArrayList rulesetsAst) { for(DDataAst dataType : dataTypesAst) { dataType.parameterKinds = new Kind[dataType.parameters.length]; Kind constructorKind = Kinds.STAR; @@ -221,8 +234,7 @@ public class Elaboration { NameExistenceChecks.checkIfTypeExists(errorLog, dataType.location, importedEnvironment, dataType.name); - if(module.addTypeDescriptor(dataType.name, typeConstructor)) - errorLog.log(dataType.location, "Type "+dataType.name+" has already been defined in this module."); + addTypeDescriptor(dataType.location, dataType.name, typeConstructor); dataType.typeConstructor = typeConstructor; } @@ -230,9 +242,7 @@ public class Elaboration { TypeAlias alias = new TypeAlias(Types.con(moduleName, typeAlias.name), typeAlias.parameters.length); NameExistenceChecks.checkIfTypeExists(errorLog, typeAlias.location, importedEnvironment, typeAlias.name); - if(module.addTypeDescriptor(typeAlias.name, alias)) { - errorLog.log(typeAlias.location, "Type alias "+typeAlias.name+" has already been defined in this module."); - } + addTypeDescriptor(typeAlias.location, typeAlias.name, alias); } for(DEffectAst effect : effectsAst) { @@ -243,11 +253,24 @@ public class Elaboration { TypeDesc.forClass(effect.threadLocalType) )); if(module.addEffectConstructor(effect.name, effectConstructor)) - errorLog.log(effect.location, "Type "+effect.name+" has already been defined in this module."); - } + errorLog.log(effect.location, "Effect "+effect.name+" has already been defined in this module."); + } + for(DRulesetAst ruleset : rulesetsAst) { + ruleset.type = Types.con(moduleName, ruleset.name); + ruleset.className = compilationContext.namingPolicy.getDataTypeClassName(ruleset.name); + StandardTypeConstructor typeConstructor = new StandardTypeConstructor(ruleset.type, Kinds.STAR, + TypeDesc.forClass(ruleset.className), ruleset.documentation == null ? null : ruleset.documentation.documentation); + typeConstructor.external = true; + addTypeDescriptor(ruleset.location, ruleset.name, typeConstructor); + } javaTypeTranslator = new JavaTypeTranslator(compilationContext.environment); compilationContext.javaTypeTranslator = javaTypeTranslator; } + + private void addTypeDescriptor(long location, String name, TypeDescriptor typeDescriptor) { + if(module.addTypeDescriptor(name, typeDescriptor)) + errorLog.log(location, "Type "+name+" has already been defined in this module."); + } private static final int[] EMPTY_INT_ARRAY = new int[0]; @@ -314,6 +337,7 @@ public class Elaboration { } public void processDataTypes(ArrayList dataTypesAst) { + ArrayList fieldAccessorGenerators = new ArrayList(); for(DDataAst dataTypeAst : dataTypesAst) { TypeTranslationContext context = createTypeTranslationContext(); TVar[] typeParameters = new TVar[dataTypeAst.parameters.length]; @@ -354,7 +378,7 @@ public class Elaboration { dataTypes.add(dataType); for(int j=0;j=0;--i) parameterTypes[i] = context.toType(constructor.parameters[i]); @@ -393,7 +417,34 @@ public class Elaboration { constructors[j].fieldNames = fieldNames; constructors[j].recordFieldNames = constructor.fieldNames; } + if(constructors.length == 1) { + Constructor constructor = constructors[0]; + if(constructor.recordFieldNames != null) { + fieldAccessorGenerators.add(new Runnable() { + @Override + public void run() { + Type in = Types.apply(dataType.name, dataType.parameters); + for(int i=0;i typeClassesAst) { @@ -554,6 +605,7 @@ public class Elaboration { String fullName = instancePrefix + valueName; long loc = valueDefs.getDefinition(valueName).get(0).location; valueDefinitionsAst.addFrom(valueDefs, valueName, fullName); + valueDefinitionsAst.setDerived(fullName); /*valueDefinitionsAst.addAnnotation(fullName, new DAnnotationAst(new EVar("@private"), Collections.emptyList()));*/ TypeClassMethod method = typeClass.methods.get(valueName); @@ -669,6 +721,18 @@ public class Elaboration { } } + public void processRulesets(ArrayList rulesetsAst) { + for(DRulesetAst ruleset : rulesetsAst) { + String constructorName = "create" + ruleset.name; + supplementedTypeAnnotations.add(new SupplementedValueType(ruleset.location, constructorName, Types.functionE(Types.PUNIT, Types.PROC, ruleset.type))); + try { + valueDefinitionsAst.add(new DValueAst(new EVar(constructorName), new EPreCHRRulesetConstructor(ruleset))); + } catch (NotPatternException e) { + throw new InternalCompilerError(ruleset.location, e); + } + } + } + /** * Convert a java class method into a {@link CallJava} instance. * Compilation errors are logged for failures in finding the named class or in accessing the method. @@ -1051,6 +1115,7 @@ public class Elaboration { int constructorTag = 0; for(Constructor constructor : dataType.constructors) { SCLValue value = new SCLValue(constructor.name); + value.definitionLocation = constructor.loc; SCLConstructor sclConstructor = new SCLConstructor( constructor.name.name, @@ -1065,7 +1130,8 @@ public class Elaboration { constructor.getParameterTypes()); if(dataType.constructors.length == 1 && ( dataType.getTypeDesc() == null || - dataType.constructors[0].javaName.equals(MethodBuilderBase.getClassName(dataType.getTypeDesc())))) + (dataType.constructors[0].javaName != null && + dataType.constructors[0].javaName.equals(MethodBuilderBase.getClassName(dataType.getTypeDesc()))))) sclConstructor.setOnlyConstructor(true); value.setValue(sclConstructor); value.setType(constructor.getType()); @@ -1110,6 +1176,8 @@ public class Elaboration { value.definitionLocation = location; if(module.addValue(value)) errorLog.log(location, "Value " + name + " is already defined."); + if(valueDefinitionsAst.isDerived(name)) + value.addProperty(DerivedProperty.INSTANCE); } for(DValueTypeAst valueTypeAst : typeAnnotationsAst) for(EVar name : valueTypeAst.names) { @@ -1137,6 +1205,14 @@ public class Elaboration { typeMap.put(name.name, valueTypeAst); } + THashMap exportMap = null; + if(moduleHeader != null && moduleHeader.export != null) { + exportMap = new THashMap(); + for(EVar export : moduleHeader.export) + if(exportMap.put(export.name, export) != null) + errorLog.log(export.location, "The symbol " + export.name + " is exported multiple times."); + } + for(String name : valueDefinitionsAst.getValueNames()) { ArrayList defs = valueDefinitionsAst.getDefinition(name); try { @@ -1154,11 +1230,16 @@ public class Elaboration { for(DAnnotationAst annotation : annotations) { handleAnnotation(value, defs, annotation); } + if(exportMap != null && exportMap.remove(name) == null) + value.addProperty(PrivateProperty.INSTANCE); } catch(RuntimeException e) { errorLog.setExceptionPosition(defs.get(0).location); throw e; } } + if(exportMap != null) + for(EVar export : exportMap.values()) + errorLog.log(export.location, "The symbol " + export.name + " is not defined in the module."); for(String name : relationDefinitionsAst.getRelationNames()) { ArrayList definitions = relationDefinitionsAst.getDefinition(name); if(definitions.size() > 1) { @@ -1196,6 +1277,8 @@ public class Elaboration { } } else if(annotation.id.text.equals("@private")) { + if(moduleHeader != null && moduleHeader.export != null) + errorLog.log(annotation.location, "Annotation @private is not used when module header contains export property."); value.addProperty(PrivateProperty.INSTANCE); } else if(annotation.id.text.equals("@deprecated")) {