]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.scl.compiler/src/org/simantics/scl/compiler/module/ImportDeclaration.java
Removed extra modules OntologyModule imported to namespace
[simantics/platform.git] / bundles / org.simantics.scl.compiler / src / org / simantics / scl / compiler / module / ImportDeclaration.java
1 package org.simantics.scl.compiler.module;
2
3 import org.simantics.scl.compiler.elaboration.expressions.EVar;
4 import org.simantics.scl.compiler.internal.parsing.Symbol;
5 import org.simantics.scl.compiler.internal.parsing.declarations.DeclarationAst;
6
7
8 public class ImportDeclaration extends DeclarationAst {
9     public static final ImportSpec DEFAULT_SPEC = new ImportSpec(true, new EVar[0]);
10     
11     public static final ImportDeclaration[] EMPTY_ARRAY = new ImportDeclaration[0];
12
13     public static final ImportDeclaration[] ONLY_BUILTINS =
14             new ImportDeclaration[] { new ImportDeclaration("Builtin", "") };
15
16     
17     public final String moduleName;
18     public final String localName;
19     public final boolean reexport;
20     public final ImportSpec spec;
21     
22     public static class ImportSpec extends Symbol {
23         public final boolean hiding;
24         public final EVar[] values;
25         
26         public ImportSpec(boolean hiding, EVar[] values) {
27             this.hiding = hiding;
28             this.values = values;
29         }
30         
31         @Override
32         public String toString() {
33             if(hiding && values.length == 0)
34                 return "";
35             StringBuilder b = new StringBuilder();
36             if(hiding)
37                 b.append("hiding ");
38             b.append('(');
39             for(int i=0;i<values.length;++i) {
40                 if(i > 0)
41                     b.append(',');
42                 b.append(values[i].name);
43             }
44             b.append(')');
45             return b.toString();
46         }
47     }
48     
49     public ImportDeclaration(String moduleName, String localName, boolean reexport) {
50         this.moduleName = moduleName;
51         this.localName = localName;
52         this.reexport = reexport;
53         this.spec = DEFAULT_SPEC;
54     }
55     
56     public ImportDeclaration(long location, String moduleName, String localName, boolean reexport, ImportSpec spec) {
57         this(moduleName, localName, reexport);
58         this.location = location;
59     }
60     
61     public ImportDeclaration(String moduleName, String localName, boolean reexport, ImportSpec spec) {
62         if(spec == null)
63             throw new NullPointerException();
64         this.moduleName = moduleName;
65         this.localName = localName;
66         this.reexport = reexport;
67         this.spec = spec;
68     }
69     
70     public ImportDeclaration(String moduleName, String localName) {
71         this(moduleName, localName, false);
72     }
73
74     @Override
75     public void toString(int indentation, StringBuilder b) {
76         for(int i=0;i<indentation;++i) b.append("    ");
77         if (reexport)
78             b.append("include \"");
79         else
80             b.append("import \"");
81         b.append(moduleName);
82         b.append("\" as ");
83         b.append(localName);
84         
85     }
86
87     public String getSpecString() {
88         return spec.toString();
89     }
90     
91     @Override
92     public int hashCode() {
93         final int prime = 31;
94         int result = 1;
95         result = prime * result
96                 + ((localName == null) ? 0 : localName.hashCode());
97         result = prime * result
98                 + ((moduleName == null) ? 0 : moduleName.hashCode());
99         result = prime * result + (reexport ? 1231 : 1237);
100         return result;
101     }
102
103     @Override
104     public boolean equals(Object obj) {
105         if (this == obj)
106             return true;
107         if (obj == null)
108             return false;
109         if (getClass() != obj.getClass())
110             return false;
111         ImportDeclaration other = (ImportDeclaration) obj;
112         if (localName == null) {
113             if (other.localName != null)
114                 return false;
115         } else if (!localName.equals(other.localName))
116             return false;
117         if (moduleName == null) {
118             if (other.moduleName != null)
119                 return false;
120         } else if (!moduleName.equals(other.moduleName))
121             return false;
122         if (reexport != other.reexport)
123             return false;
124         return true;
125     }
126
127     public ImportDeclaration hidden() {
128         return new ImportDeclaration(moduleName, null);
129     }    
130     
131 }