]> gerrit.simantics Code Review - simantics/platform.git/commitdiff
(refs #7595) Started SCL/Reflection module 91/1191/2
authorHannu Niemistö <hannu.niemisto@semantum.fi>
Fri, 3 Nov 2017 13:00:00 +0000 (15:00 +0200)
committerTuukka Lehtonen <tuukka.lehtonen@semantum.fi>
Fri, 3 Nov 2017 13:15:06 +0000 (15:15 +0200)
Change-Id: I9c7d798ddfb9d39a8dfc8f7bdd31eb2e3399a172

bundles/org.simantics.scl.compiler/scl/SCL/Reflection.scl [new file with mode: 0644]
bundles/org.simantics.scl.compiler/src/org/simantics/scl/compiler/compilation/CompilationContext.java
bundles/org.simantics.scl.compiler/src/org/simantics/scl/compiler/compilation/Elaboration.java
bundles/org.simantics.scl.compiler/src/org/simantics/scl/compiler/elaboration/java/ReflectionJavaModule.java [new file with mode: 0644]
bundles/org.simantics.scl.compiler/src/org/simantics/scl/compiler/environment/EnvironmentFactory.java
bundles/org.simantics.scl.compiler/src/org/simantics/scl/compiler/environment/EnvironmentFactoryImpl.java
bundles/org.simantics.scl.compiler/src/org/simantics/scl/compiler/source/repository/BuiltinModuleSourceRepository.java

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 (file)
index 0000000..cf8b88e
--- /dev/null
@@ -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 -> <Proc> a
+    
+    @JavaName getSourceRepository
+    moduleSourceRepositoryOf :: ModuleRepository -> ModuleSourceRepository
+    
+importJava "org.simantics.scl.compiler.source.repository.ModuleSourceRepository" where
+    data ModuleSourceRepository
+    
+    @JavaName getModuleNames
+    sclModuleNames_ :: ModuleSourceRepository -> [String]
+    
+unsafeSclValueByName :: String -> <Proc> a
+unsafeSclValueByName = unsafeSclValueByName_ MODULE_REPOSITORY
+
+possibleUnsafeSclValueByName :: String -> <Proc> Maybe a
+possibleUnsafeSclValueByName name = Just (unsafeSclValueByName name) `catch` \(_ :: Exception) -> Nothing
+
+sclModuleNames :: <Proc> [String]
+sclModuleNames = sclModuleNames_ (moduleSourceRepositoryOf MODULE_REPOSITORY)
index 411b93fe22eb72cb58523d62a88c4af61f6ce067..b9625af78c5817621bb8f68245e6179f5336ea02 100644 (file)
@@ -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;
index 5a82823a1b27f2d8a200bb8d1c03a8ce8ffc64a6..26bd31ec26cb96beebcac814e8dbce8c49b05212 100644 (file)
@@ -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 (file)
index 0000000..dab0056
--- /dev/null
@@ -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());
+    }
+
+}
index 1faa8f6459c3373382add3dde58a5d832b2dff6c..3f714b497c75a82159b23e52378d352459642438 100644 (file)
@@ -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();
 
 }
index 1a7fb3c6d89e5afe27186ec72aba82456058fc19..04408726092b5f5d768d3992c4878ca635f05ae3 100644 (file)
@@ -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 {
index 65a68a712106fb78480f23e4951a35c2f6e129a3..650fa1d19cb4d7a77bdc0630c24063cc1326c084 100644 (file)
@@ -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());
     }