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