]> gerrit.simantics Code Review - simantics/platform.git/blob
1a7fb3c6d89e5afe27186ec72aba82456058fc19
[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 Environment createEnvironment(CompilationContext context, ImportDeclaration[] imports) throws ImportFailureException {
28         ArrayList<ImportDeclaration> acceptedBuiltinImports = 
29                 new ArrayList<ImportDeclaration>(builtinImports.length);
30         loop: for(ImportDeclaration builtinImport : builtinImports) {
31             for(ImportDeclaration decl : imports)
32                 if(decl.moduleName.equals(builtinImport.moduleName) &&
33                         decl.localName.equals(builtinImport.localName))
34                     continue loop;
35             acceptedBuiltinImports.add(builtinImport);
36         }
37         ImportDeclaration[] is = Arrays.copyOf(
38                 acceptedBuiltinImports.toArray(new ImportDeclaration[acceptedBuiltinImports.size()]),
39                 acceptedBuiltinImports.size() + imports.length);
40         System.arraycopy(imports, 0, is, acceptedBuiltinImports.size(), imports.length);
41         return environment.createEnvironment(context, is, listener);
42     }
43     
44     @Override
45     public void addBuiltinDependencies(ConcreteModule module) {
46         for(ImportDeclaration decl : builtinImports) {
47             ImportDeclaration decl2 = new ImportDeclaration(decl.moduleName, null);
48             module.addDependency(decl2);
49         }
50     }
51
52 }