]> gerrit.simantics Code Review - simantics/platform.git/blob
04408726092b5f5d768d3992c4878ca635f05ae3
[simantics/platform.git] /
1 package org.simantics.scl.compiler.environment;
2
3 import java.util.ArrayList;
4 import java.util.Arrays;
5
6 import org.simantics.scl.compiler.compilation.CompilationContext;
7 import org.simantics.scl.compiler.module.ConcreteModule;
8 import org.simantics.scl.compiler.module.ImportDeclaration;
9 import org.simantics.scl.compiler.module.repository.ImportFailureException;
10 import org.simantics.scl.compiler.module.repository.ModuleRepository;
11 import org.simantics.scl.compiler.module.repository.UpdateListener;
12
13 public class EnvironmentFactoryImpl implements EnvironmentFactory {
14
15     private final ModuleRepository environment;
16     private final ImportDeclaration[] builtinImports;
17     private final UpdateListener listener;
18     
19     public EnvironmentFactoryImpl(ModuleRepository environment,
20             ImportDeclaration[] builtinImports, UpdateListener listener) {
21         this.environment = environment;
22         this.builtinImports = builtinImports;
23         this.listener = listener;
24     }
25     
26     @Override
27     public ModuleRepository getModuleRepository() {
28         return environment;
29     }
30
31     @Override
32     public Environment createEnvironment(CompilationContext context, ImportDeclaration[] imports) throws ImportFailureException {
33         ArrayList<ImportDeclaration> acceptedBuiltinImports = 
34                 new ArrayList<ImportDeclaration>(builtinImports.length);
35         loop: for(ImportDeclaration builtinImport : builtinImports) {
36             for(ImportDeclaration decl : imports)
37                 if(decl.moduleName.equals(builtinImport.moduleName) &&
38                         decl.localName.equals(builtinImport.localName))
39                     continue loop;
40             acceptedBuiltinImports.add(builtinImport);
41         }
42         ImportDeclaration[] is = Arrays.copyOf(
43                 acceptedBuiltinImports.toArray(new ImportDeclaration[acceptedBuiltinImports.size()]),
44                 acceptedBuiltinImports.size() + imports.length);
45         System.arraycopy(imports, 0, is, acceptedBuiltinImports.size(), imports.length);
46         return environment.createEnvironment(context, is, listener);
47     }
48     
49     @Override
50     public void addBuiltinDependencies(ConcreteModule module) {
51         for(ImportDeclaration decl : builtinImports) {
52             ImportDeclaration decl2 = new ImportDeclaration(decl.moduleName, null);
53             module.addDependency(decl2);
54         }
55     }
56
57 }