KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > polyglot > ext > jl > ast > SourceFile_c


1 package polyglot.ext.jl.ast;
2
3 import polyglot.ast.*;
4 import polyglot.types.*;
5 import polyglot.visit.*;
6 import polyglot.util.*;
7 import polyglot.frontend.Source;
8 import polyglot.types.Package;
9 import java.util.*;
10
11 /**
12  * A <code>SourceFile</code> is an immutable representations of a Java
13  * langauge source file. It consists of a package name, a list of
14  * <code>Import</code>s, and a list of <code>GlobalDecl</code>s.
15  */

16 public class SourceFile_c extends Node_c implements SourceFile
17 {
18     protected PackageNode package_;
19     protected List imports;
20     protected List decls;
21     protected ImportTable importTable;
22     protected Source source;
23
24     public SourceFile_c(Position pos, PackageNode package_, List imports, List decls) {
25     super(pos);
26     this.package_ = package_;
27     this.imports = TypedList.copyAndCheck(imports, Import.class, true);
28     this.decls = TypedList.copyAndCheck(decls, TopLevelDecl.class, true);
29     }
30
31     /** Get the source of the source file. */
32     public Source source() {
33     return this.source;
34     }
35
36     /** Set the source of the source file. */
37     public SourceFile source(Source source) {
38     SourceFile_c n = (SourceFile_c) copy();
39     n.source = source;
40     return n;
41     }
42
43     /** Get the package of the source file. */
44     public PackageNode package_() {
45     return this.package_;
46     }
47
48     /** Set the package of the source file. */
49     public SourceFile package_(PackageNode package_) {
50     SourceFile_c n = (SourceFile_c) copy();
51     n.package_ = package_;
52     return n;
53     }
54
55     /** Get the imports of the source file. */
56     public List imports() {
57     return Collections.unmodifiableList(this.imports);
58     }
59
60     /** Set the imports of the source file. */
61     public SourceFile imports(List imports) {
62     SourceFile_c n = (SourceFile_c) copy();
63     n.imports = TypedList.copyAndCheck(imports, Import.class, true);
64     return n;
65     }
66
67     /** Get the declarations of the source file. */
68     public List decls() {
69     return Collections.unmodifiableList(this.decls);
70     }
71
72     /** Set the declarations of the source file. */
73     public SourceFile decls(List decls) {
74     SourceFile_c n = (SourceFile_c) copy();
75     n.decls = TypedList.copyAndCheck(decls, TopLevelDecl.class, true);
76     return n;
77     }
78
79     /** Get the declarations of the source file. */
80     public ImportTable importTable() {
81     return this.importTable;
82     }
83
84     /** Set the declarations of the source file. */
85     public SourceFile importTable(ImportTable importTable) {
86     SourceFile_c n = (SourceFile_c) copy();
87     n.importTable = importTable;
88     return n;
89     }
90
91     /** Reconstruct the source file. */
92     protected SourceFile_c reconstruct(PackageNode package_, List imports, List decls) {
93     if (package_ != this.package_ || ! CollectionUtil.equals(imports, this.imports) || ! CollectionUtil.equals(decls, this.decls)) {
94         SourceFile_c n = (SourceFile_c) copy();
95         n.package_ = package_;
96         n.imports = TypedList.copyAndCheck(imports, Import.class, true);
97         n.decls = TypedList.copyAndCheck(decls, TopLevelDecl.class, true);
98         return n;
99     }
100
101     return this;
102     }
103
104     /** Visit the children of the source file. */
105     public Node visitChildren(NodeVisitor v) {
106         PackageNode package_ = (PackageNode) visitChild(this.package_, v);
107     List imports = visitList(this.imports, v);
108     List decls = visitList(this.decls, v);
109     return reconstruct(package_, imports, decls);
110     }
111
112     /**
113      * Build type objects for the source file. Set the visitor's import table
114      * field before we recurse into the declarations.
115      */

116     public NodeVisitor buildTypesEnter(TypeBuilder tb) throws SemanticException {
117         TypeSystem ts = tb.typeSystem();
118
119         ImportTable it;
120
121         if (package_ != null) {
122             it = ts.importTable(source.name(), package_.package_());
123     }
124     else {
125             it = ts.importTable(source.name(), null);
126     }
127
128         tb.setImportTable(it);
129
130         return tb;
131     }
132
133     /**
134      * Build type objects for the source file. Sets the import table field for
135      * the source.
136      */

137     public Node buildTypes(TypeBuilder tb) throws SemanticException {
138         ImportTable it = tb.importTable();
139
140         // Clear the import table in case we use the same visitor
141
// to visit a different source file.
142
tb.setImportTable(null);
143
144         return importTable(it);
145     }
146
147     public Context enterScope(Context c) {
148         c = c.pushSource(importTable);
149         c = c.pushBlock();
150
151     for (Iterator i = decls.iterator(); i.hasNext();) {
152         TopLevelDecl d = (TopLevelDecl) i.next();
153             Named n = (Named) d.declaration();
154             c.addNamed(n);
155         }
156
157         return c;
158     }
159
160     /** Type check the source file. */
161     public Node typeCheck(TypeChecker tc) throws SemanticException {
162     Set names = new HashSet();
163     boolean hasPublic = false;
164
165     for (Iterator i = decls.iterator(); i.hasNext();) {
166         TopLevelDecl d = (TopLevelDecl) i.next();
167         String JavaDoc s = d.name();
168
169         if (names.contains(s)) {
170         throw new SemanticException("Duplicate declaration: \"" + s +
171             "\".", d.position());
172         }
173
174         names.add(s);
175
176         if (d.flags().isPublic()) {
177         if (hasPublic) {
178             throw new SemanticException(
179             "The source contains more than one public declaration.",
180             d.position());
181         }
182
183         hasPublic = true;
184         }
185     }
186      
187     return this;
188     }
189
190     public String JavaDoc toString() {
191         return "<<<< " + source + " >>>>";
192     }
193
194     /** Write the source file to an output file. */
195     public void prettyPrint(CodeWriter w, PrettyPrinter tr) {
196         w.write("<<<< " + source + " >>>>");
197         w.newline(0);
198
199     if (package_ != null) {
200         w.write("package ");
201         print(package_, w, tr);
202         w.write(";");
203         w.newline(0);
204         w.newline(0);
205     }
206
207     for (Iterator i = imports.iterator(); i.hasNext(); ) {
208         Import im = (Import) i.next();
209         print(im, w, tr);
210     }
211      
212     if (! imports.isEmpty()) {
213         w.newline(0);
214     }
215
216     for (Iterator i = decls.iterator(); i.hasNext(); ) {
217         TopLevelDecl d = (TopLevelDecl) i.next();
218         print(d, w, tr);
219     }
220     }
221 }
222
Popular Tags