]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.scl.compiler/src/org/simantics/scl/compiler/module/ImportDeclaration.java
(refs #7365) Fixed the bug in the test CHR11.scl
[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(String moduleName, String localName, boolean reexport, ImportSpec spec) {
57         if(spec == null)
58             throw new NullPointerException();
59         this.moduleName = moduleName;
60         this.localName = localName;
61         this.reexport = reexport;
62         this.spec = spec;
63     }
64     
65     public ImportDeclaration(String moduleName, String localName) {
66         this(moduleName, localName, false);
67     }
68
69     @Override
70     public void toString(int indentation, StringBuilder b) {
71         for(int i=0;i<indentation;++i) b.append("    ");
72         if (reexport)
73             b.append("include \"");
74         else
75             b.append("import \"");
76         b.append(moduleName);
77         b.append("\" as ");
78         b.append(localName);
79         
80     }
81
82     public String getSpecString() {
83         return spec.toString();
84     }
85     
86     @Override
87     public int hashCode() {
88         final int prime = 31;
89         int result = 1;
90         result = prime * result
91                 + ((localName == null) ? 0 : localName.hashCode());
92         result = prime * result
93                 + ((moduleName == null) ? 0 : moduleName.hashCode());
94         result = prime * result + (reexport ? 1231 : 1237);
95         return result;
96     }
97
98     @Override
99     public boolean equals(Object obj) {
100         if (this == obj)
101             return true;
102         if (obj == null)
103             return false;
104         if (getClass() != obj.getClass())
105             return false;
106         ImportDeclaration other = (ImportDeclaration) obj;
107         if (localName == null) {
108             if (other.localName != null)
109                 return false;
110         } else if (!localName.equals(other.localName))
111             return false;
112         if (moduleName == null) {
113             if (other.moduleName != null)
114                 return false;
115         } else if (!moduleName.equals(other.moduleName))
116             return false;
117         if (reexport != other.reexport)
118             return false;
119         return true;
120     }    
121     
122 }