From: Hannu Niemistö Date: Fri, 3 Nov 2017 13:00:00 +0000 (+0200) Subject: (refs #7595) Started SCL/Reflection module X-Git-Tag: v1.31.0~61 X-Git-Url: https://gerrit.simantics.org/r/gitweb?a=commitdiff_plain;h=db54e4ca935111cfe3574a01bb9be3cf083258cc;p=simantics%2Fplatform.git (refs #7595) Started SCL/Reflection module Change-Id: I9c7d798ddfb9d39a8dfc8f7bdd31eb2e3399a172 --- diff --git a/bundles/org.simantics.scl.compiler/scl/SCL/Reflection.scl b/bundles/org.simantics.scl.compiler/scl/SCL/Reflection.scl new file mode 100644 index 000000000..cf8b88eea --- /dev/null +++ b/bundles/org.simantics.scl.compiler/scl/SCL/Reflection.scl @@ -0,0 +1,27 @@ +module { + export = [possibleUnsafeSclValueByName, unsafeSclValueByName, sclModuleNames] +} + +include "SCL/ReflectionJava" + +importJava "org.simantics.scl.compiler.module.repository.ModuleRepository" where + @JavaName getValue + unsafeSclValueByName_ :: ModuleRepository -> String -> a + + @JavaName getSourceRepository + moduleSourceRepositoryOf :: ModuleRepository -> ModuleSourceRepository + +importJava "org.simantics.scl.compiler.source.repository.ModuleSourceRepository" where + data ModuleSourceRepository + + @JavaName getModuleNames + sclModuleNames_ :: ModuleSourceRepository -> [String] + +unsafeSclValueByName :: String -> a +unsafeSclValueByName = unsafeSclValueByName_ MODULE_REPOSITORY + +possibleUnsafeSclValueByName :: String -> Maybe a +possibleUnsafeSclValueByName name = Just (unsafeSclValueByName name) `catch` \(_ :: Exception) -> Nothing + +sclModuleNames :: [String] +sclModuleNames = sclModuleNames_ (moduleSourceRepositoryOf MODULE_REPOSITORY) diff --git a/bundles/org.simantics.scl.compiler/src/org/simantics/scl/compiler/compilation/CompilationContext.java b/bundles/org.simantics.scl.compiler/src/org/simantics/scl/compiler/compilation/CompilationContext.java index 411b93fe2..b9625af78 100644 --- a/bundles/org.simantics.scl.compiler/src/org/simantics/scl/compiler/compilation/CompilationContext.java +++ b/bundles/org.simantics.scl.compiler/src/org/simantics/scl/compiler/compilation/CompilationContext.java @@ -13,12 +13,14 @@ import org.simantics.scl.compiler.internal.codegen.types.JavaTypeTranslator; import org.simantics.scl.compiler.internal.codegen.utils.JavaNamingPolicy; import org.simantics.scl.compiler.internal.header.ModuleHeader; import org.simantics.scl.compiler.module.ConcreteModule; +import org.simantics.scl.compiler.module.repository.ModuleRepository; import org.simantics.scl.compiler.types.Type; import gnu.trove.map.hash.THashMap; public class CompilationContext implements EnvironmentalContext { public final ErrorLog errorLog = new ErrorLog(); + public ModuleRepository moduleRepository; public Environment environment; public JavaTypeTranslator javaTypeTranslator; public JavaNamingPolicy namingPolicy; 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 5a82823a1..26bd31ec2 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 @@ -169,6 +169,7 @@ public class Elaboration { module = new ConcreteModule(moduleName); compilationContext.module = module; + compilationContext.moduleRepository = localEnvironmentFactory.getModuleRepository(); if(moduleHeader != null) { if(moduleHeader.defaultLocalName != null) module.setDefaultLocalName(moduleHeader.defaultLocalName); diff --git a/bundles/org.simantics.scl.compiler/src/org/simantics/scl/compiler/elaboration/java/ReflectionJavaModule.java b/bundles/org.simantics.scl.compiler/src/org/simantics/scl/compiler/elaboration/java/ReflectionJavaModule.java new file mode 100644 index 000000000..dab0056ae --- /dev/null +++ b/bundles/org.simantics.scl.compiler/src/org/simantics/scl/compiler/elaboration/java/ReflectionJavaModule.java @@ -0,0 +1,51 @@ +package org.simantics.scl.compiler.elaboration.java; + +import org.cojen.classfile.TypeDesc; +import org.osgi.service.component.annotations.Component; +import org.simantics.scl.compiler.common.names.Name; +import org.simantics.scl.compiler.elaboration.contexts.SimplificationContext; +import org.simantics.scl.compiler.elaboration.expressions.EApply; +import org.simantics.scl.compiler.elaboration.expressions.EExternalConstant; +import org.simantics.scl.compiler.elaboration.expressions.Expression; +import org.simantics.scl.compiler.elaboration.macros.MacroRule; +import org.simantics.scl.compiler.elaboration.modules.SCLValue; +import org.simantics.scl.compiler.internal.codegen.types.StandardTypeConstructor; +import org.simantics.scl.compiler.module.ConcreteModule; +import org.simantics.scl.compiler.module.repository.ModuleRepository; +import org.simantics.scl.compiler.types.TCon; +import org.simantics.scl.compiler.types.Type; +import org.simantics.scl.compiler.types.Types; +import org.simantics.scl.compiler.types.kinds.Kinds; + +@Component +public class ReflectionJavaModule extends ConcreteModule { + + public static ReflectionJavaModule INSTANCE = new ReflectionJavaModule(); + + public ReflectionJavaModule() { + super("SCL/ReflectionJava"); + + // ModuleRepository type + TCon ModuleRepository = Types.con(getName(), "ModuleRepository"); + StandardTypeConstructor markerConstructor = new StandardTypeConstructor(ModuleRepository, + Kinds.STAR, TypeDesc.forClass(ModuleRepository.class)); + markerConstructor.external = true; + addTypeDescriptor("ModuleRepository", markerConstructor); + + // MODULE_REPOSITORY value + SCLValue value = new SCLValue(Name.create(getName(), "MODULE_REPOSITORY")); + value.setType(Types.functionE(Types.PUNIT, Types.NO_EFFECTS, ModuleRepository)); + value.setMacroRule(new MacroRule() { + @Override + public Expression apply(SimplificationContext context, Type[] typeParameters, EApply apply) { + if(apply.parameters.length != 1) + return null; + return new EExternalConstant(context.getCompilationContext().moduleRepository, ModuleRepository); + } + }); + addValue(value); + + setParentClassLoader(getClass().getClassLoader()); + } + +} diff --git a/bundles/org.simantics.scl.compiler/src/org/simantics/scl/compiler/environment/EnvironmentFactory.java b/bundles/org.simantics.scl.compiler/src/org/simantics/scl/compiler/environment/EnvironmentFactory.java index 1faa8f645..3f714b497 100644 --- a/bundles/org.simantics.scl.compiler/src/org/simantics/scl/compiler/environment/EnvironmentFactory.java +++ b/bundles/org.simantics.scl.compiler/src/org/simantics/scl/compiler/environment/EnvironmentFactory.java @@ -4,10 +4,12 @@ import org.simantics.scl.compiler.compilation.CompilationContext; import org.simantics.scl.compiler.module.ConcreteModule; import org.simantics.scl.compiler.module.ImportDeclaration; import org.simantics.scl.compiler.module.repository.ImportFailureException; +import org.simantics.scl.compiler.module.repository.ModuleRepository; public interface EnvironmentFactory { Environment createEnvironment(CompilationContext context, ImportDeclaration[] array) throws ImportFailureException; void addBuiltinDependencies(ConcreteModule module); + ModuleRepository getModuleRepository(); } diff --git a/bundles/org.simantics.scl.compiler/src/org/simantics/scl/compiler/environment/EnvironmentFactoryImpl.java b/bundles/org.simantics.scl.compiler/src/org/simantics/scl/compiler/environment/EnvironmentFactoryImpl.java index 1a7fb3c6d..044087260 100644 --- a/bundles/org.simantics.scl.compiler/src/org/simantics/scl/compiler/environment/EnvironmentFactoryImpl.java +++ b/bundles/org.simantics.scl.compiler/src/org/simantics/scl/compiler/environment/EnvironmentFactoryImpl.java @@ -22,6 +22,11 @@ public class EnvironmentFactoryImpl implements EnvironmentFactory { this.builtinImports = builtinImports; this.listener = listener; } + + @Override + public ModuleRepository getModuleRepository() { + return environment; + } @Override public Environment createEnvironment(CompilationContext context, ImportDeclaration[] imports) throws ImportFailureException { diff --git a/bundles/org.simantics.scl.compiler/src/org/simantics/scl/compiler/source/repository/BuiltinModuleSourceRepository.java b/bundles/org.simantics.scl.compiler/src/org/simantics/scl/compiler/source/repository/BuiltinModuleSourceRepository.java index 65a68a712..650fa1d19 100644 --- a/bundles/org.simantics.scl.compiler/src/org/simantics/scl/compiler/source/repository/BuiltinModuleSourceRepository.java +++ b/bundles/org.simantics.scl.compiler/src/org/simantics/scl/compiler/source/repository/BuiltinModuleSourceRepository.java @@ -5,12 +5,14 @@ import org.simantics.scl.compiler.elaboration.java.Builtins; import org.simantics.scl.compiler.elaboration.java.JavaModule; import org.simantics.scl.compiler.elaboration.java.LoggingModule; import org.simantics.scl.compiler.elaboration.java.MinigraphModule; +import org.simantics.scl.compiler.elaboration.java.ReflectionJavaModule; @Component public class BuiltinModuleSourceRepository extends MapModuleSourceRepository implements ModuleSourceRepository { public BuiltinModuleSourceRepository() { super(Builtins.INSTANCE, JavaModule.INSTANCE, + ReflectionJavaModule.INSTANCE, MinigraphModule.INSTANCE, new LoggingModule()); }